Salome HOME
92ae3ace867526c8cbd408c7100a32054891a4ca
[modules/gui.git] / src / ResExporter / ResourceExporter.cxx
1 // Copyright (C) 2005  CEA/DEN, EDF R&D, OPEN CASCADE, PRINCIPIA R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/
18 //
19 /*!
20  File:      ResourceExporter.cxx
21  Created:   27/06/05
22  Author:    Vadim SANDLER
23  Copyright (C) CEA 2005
24
25  This tool provides command-line interface allowing to modify user preferences.
26  The tool can be used by the compilation procedures in order to set default preferences for the module.
27
28  Usage:
29     ResourceExporter <parameters-list>
30  Parameters have the following format:
31     '<section>:<name>=<new_value>'            - to set <new_value> for the user 
32                                                 preference <name> from the <section> section;
33     '<section>:<name>+=<value>[|<separator>]' - the <value> is appended to the current value 
34                                                 of the preference <name> from the <section> section;
35                                                 separator is used to concatenate old and new values,
36                                                 by default comma (',') symbol is used
37     '-<section>:<name>'                       - to remove user preference <name> from the <section>
38                                                 section. Note that only use preference file is 
39                                                 influenced, you may need to use '<section>:<name>=""'
40                                                 option
41  The number of parameters is limeted only by maximum possible length of the command line.
42 */
43
44 #include "SUIT_ResourceMgr.h"
45 #include <qfile.h>
46 #include <qdir.h>
47 #include <qstringlist.h>
48 #include <qapplication.h>
49 #include <iostream>
50
51 /*!
52   \return the SALOME version number
53 */
54 static QString salomeVersion()
55 {
56   QString path( ::getenv( "GUI_ROOT_DIR" ) );
57   if ( !path.isEmpty() )
58     path += QDir::separator();
59   path += QString( "bin/salome/VERSION" );
60
61   QFile vf( path );
62   if ( !vf.open( IO_ReadOnly ) )
63     return QString::null;
64
65   QString line;
66   vf.readLine( line, 1024 );
67   vf.close();
68
69   if ( line.isEmpty() )
70     return QString::null;
71
72   while ( !line.isEmpty() && line.at( line.length() - 1 ) == QChar( '\n' ) )
73     line.remove( line.length() - 1, 1 );
74
75   QString ver;
76   int idx = line.findRev( ":" );
77   if ( idx != -1 )
78     ver = line.mid( idx + 1 ).stripWhiteSpace();
79
80   return ver;
81 }
82
83 /*!
84   print the help information
85 */
86 static void help()
87 {
88   std::cout << ""                                                                                                     << std::endl;
89   std::cout << "ResourceExporter: provides command-line interface to modify user preferences."                        << std::endl;
90   std::cout << ""                                                                                                     << std::endl;
91   std::cout << "Usage:"                                                                                               << std::endl;
92   std::cout << "   ResourceExporter <parameters-list>"                                                                << std::endl;
93   std::cout << "Parameters have the following format:"                                                                << std::endl;
94   std::cout << "   '<section>:<name>=<new_value>'            - to set <new_value> for the user "                      << std::endl;
95   std::cout << "                                               preference <name> from the <section> section;"         << std::endl;
96   std::cout << "   '<section>:<name>+=<value>[|<separator>]' - the <value> is appended to the current value "         << std::endl;
97   std::cout << "                                               of the preference <name> from the <section> section;"  << std::endl;
98   std::cout << "                                               separator is used to concatenate old and new values,"  << std::endl;
99   std::cout << "                                               by default comma (',') symbol is used"                 << std::endl;
100   std::cout << "   '-<section>:<name>'                       - to remove user preference <name> from the <section>"   << std::endl;
101   std::cout << "                                               section. Note that only use preference file is "       << std::endl;
102   std::cout << "                                               influenced, you may need to use '<section>:<name>=""'" << std::endl;
103   std::cout << "                                               option"                                                << std::endl;
104   std::cout << "The number of parameters is limeted only by maximum possible length of the command line."             << std::endl;
105   std::cout << ""                                                                                                     << std::endl;
106 }
107
108 /*!
109   application main() function
110 */
111 int main( int argc, char** argv )
112 {
113   QApplication app( argc, argv );
114   if ( argc > 1 ) {
115     SUIT_ResourceMgr* resMgr = new SUIT_ResourceMgr( QString( "SalomeApp" ), QString( "%1Config" ) );
116     resMgr->setVersion( salomeVersion() );
117     resMgr->setCurrentFormat( QString( "xml" ) );
118     resMgr->loadLanguage();
119     for ( int i = 1; i < argc; i ++ ) {
120       QString anArg = QString( argv[i] ).stripWhiteSpace();
121       if ( anArg.startsWith( "-" ) ) {
122         anArg.remove( 0, 1 );
123         if ( anArg.contains( ":" ) ) {
124           QStringList vals = QStringList::split( ":", anArg );
125           QString section  = vals[ 0 ].stripWhiteSpace();
126           QString param    = vals[ 1 ].stripWhiteSpace();
127           if ( section.isEmpty() || param.isEmpty() ) continue;
128           resMgr->remove( section, param );
129         }
130       }
131       else if ( anArg.contains( "+=" ) ) {
132         QStringList vals = QStringList::split( "+=", anArg );
133         if ( vals[ 0 ].contains( ":" ) ) {
134           QStringList vals1 = QStringList::split( ":", vals[ 0 ] );
135           QString section  = vals1[ 0 ].stripWhiteSpace();
136           QString param    = vals1[ 1 ].stripWhiteSpace();
137           QString newValue = vals [ 1 ].stripWhiteSpace();
138           QString separ    = ","; // default separator
139           if ( newValue.contains( "|" ) ) {
140             QStringList vals2 = QStringList::split( "|", newValue );
141             newValue = vals2[ 0 ].stripWhiteSpace();
142             separ  = vals2[ 1 ].stripWhiteSpace();
143           }
144           if ( section.isEmpty() || param.isEmpty() || newValue.isEmpty() || separ.isEmpty() ) continue;
145           QString value = resMgr->stringValue( section, param );
146           QStringList valsOld = QStringList::split( separ, value );
147           QStringList valsNew = QStringList::split( separ, newValue );
148           for ( int i = 0; i < valsNew.count(); i++ )
149             if ( !valsOld.contains( valsNew[i] ) )
150               valsOld.append( valsNew[i] );
151           resMgr->setValue( section, param, valsOld.join( separ ) );
152         }
153       }
154       else if ( anArg.contains( "=" ) ) {
155         QStringList vals = QStringList::split( "=", anArg );
156         if ( vals[ 0 ].contains( ":" ) ) {
157           QStringList vals1 = QStringList::split( ":", vals[ 0 ] );
158           QString section  = vals1[ 0 ].stripWhiteSpace();
159           QString param    = vals1[ 1 ].stripWhiteSpace();
160           QString value = vals [ 1 ].stripWhiteSpace();
161           if ( section.isEmpty() || param.isEmpty() ) continue;
162           resMgr->setValue( section, param, value );
163         }
164       }
165     }
166     resMgr->save();
167     delete resMgr;
168   }
169   else {
170     help();
171   }
172   return 0;
173 }