Salome HOME
597059bded6400c33831c30d10c69b635a644f91
[modules/gui.git] / src / ResExporter / ResourceExporter.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // File    : ResourceExporter.cxx
23 // Author  : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
24 //
25 /*
26  This tool provides command-line interface allowing to modify user preferences.
27  The tool can be used by the compilation procedures in order to set default preferences for the module.
28
29  Usage:
30     ResourceExporter <parameters-list>
31  Parameters have the following format:
32     '<section>:<name>=<new_value>'            - to set <new_value> for the user 
33                                                 preference <name> from the <section> section;
34     '<section>:<name>+=<value>[|<separator>]' - the <value> is appended to the current value 
35                                                 of the preference <name> from the <section> section;
36                                                 separator is used to concatenate old and new values,
37                                                 by default comma (',') symbol is used
38     '-<section>:<name>'                       - to remove user preference <name> from the <section>
39                                                 section. Note that only use preference file is 
40                                                 influenced, you may need to use '<section>:<name>=""'
41                                                 option
42  The number of parameters is limeted only by maximum possible length of the command line.
43 */
44
45 #include "SUIT_ResourceMgr.h"
46 #include <QFile>
47 #include <QDir>
48 #include <QStringList>
49 #include <QApplication>
50 #include <iostream>
51
52 /*!
53   \return the SALOME version number
54 */
55 static QString salomeVersion()
56 {
57   QString path( ::getenv( "GUI_ROOT_DIR" ) );
58   if ( !path.isEmpty() )
59     path += QDir::separator();
60   path += QString( "bin/salome/VERSION" );
61
62   QFile vf( path );
63   if ( !vf.open( QIODevice::ReadOnly ) )
64     return QString();
65
66   QString line( vf.readLine( 1024 ) );
67   vf.close();
68
69   if ( line.isEmpty() )
70     return QString();
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.lastIndexOf( ":" );
77   if ( idx != -1 )
78     ver = line.mid( idx + 1 ).trimmed();
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] ).trimmed();
121       if ( anArg.startsWith( "-" ) ) {
122         anArg.remove( 0, 1 );
123         if ( anArg.contains( ":" ) ) {
124           QStringList vals = anArg.split( ":", QString::SkipEmptyParts );
125           QString section  = vals[ 0 ].trimmed();
126           QString param    = vals[ 1 ].trimmed();
127           if ( section.isEmpty() || param.isEmpty() ) continue;
128           resMgr->remove( section, param );
129         }
130       }
131       else if ( anArg.contains( "+=" ) ) {
132         QStringList vals = anArg.split( "+=", QString::SkipEmptyParts );
133         if ( vals[ 0 ].contains( ":" ) ) {
134           QStringList vals1 = vals[ 0 ].split( ":", QString::SkipEmptyParts );
135           QString section  = vals1[ 0 ].trimmed();
136           QString param    = vals1[ 1 ].trimmed();
137           QString newValue = vals [ 1 ].trimmed();
138           QString separ    = ","; // default separator
139           if ( newValue.contains( "|" ) ) {
140             QStringList vals2 = newValue.split( "|", QString::SkipEmptyParts );
141             newValue = vals2[ 0 ].trimmed();
142             separ  = vals2[ 1 ].trimmed();
143           }
144           if ( section.isEmpty() || param.isEmpty() || newValue.isEmpty() || separ.isEmpty() ) continue;
145           QString value = resMgr->stringValue( section, param );
146           QStringList valsOld = value.split( separ, QString::SkipEmptyParts );
147           QStringList valsNew = newValue.split( separ, QString::SkipEmptyParts );
148           for ( int i = 0; i < (int)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 = anArg.split( "=", QString::SkipEmptyParts );
156         if ( vals[ 0 ].contains( ":" ) ) {
157           QStringList vals1 = vals[ 0 ].split( ":", QString::SkipEmptyParts );
158           QString section  = vals1[ 0 ].trimmed();
159           QString param    = vals1[ 1 ].trimmed();
160           QString value = vals [ 1 ].trimmed();
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 }