Salome HOME
Update copyrights 2014.
[modules/gui.git] / src / ResExporter / ResourceExporter.cxx
1 // Copyright (C) 2007-2014  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, or (at your option) any later version.
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
23 // File    : ResourceExporter.cxx
24 // Author  : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
25 //
26 /*
27  This tool provides command-line interface allowing to modify user preferences.
28  The tool can be used by the compilation procedures in order to set default preferences for the module.
29
30  Usage:
31     ResourceExporter <parameters-list>
32  Parameters have the following format:
33     '<section>:<name>=<new_value>'            - to set <new_value> for the user 
34                                                 preference <name> from the <section> section;
35     '<section>:<name>+=<value>[|<separator>]' - the <value> is appended to the current value 
36                                                 of the preference <name> from the <section> section;
37                                                 separator is used to concatenate old and new values,
38                                                 by default comma (',') symbol is used
39     '-<section>:<name>'                       - to remove user preference <name> from the <section>
40                                                 section. Note that only use preference file is 
41                                                 influenced, you may need to use '<section>:<name>=""'
42                                                 option
43  The number of parameters is limeted only by maximum possible length of the command line.
44 */
45
46 #include "GUI_version.h"
47 #include "SUIT_ResourceMgr.h"
48 #include <QFile>
49 #include <QDir>
50 #include <QStringList>
51 #include <QApplication>
52 #include <iostream>
53
54 /*!
55   \return the SALOME version number
56 */
57 static QString salomeVersion()
58 {
59   return GUI_VERSION_STR;
60 }
61
62 /*!
63   print the help information
64 */
65 static void help()
66 {
67   std::cout << ""                                                                                                     << std::endl;
68   std::cout << "ResourceExporter: provides command-line interface to modify user preferences."                        << std::endl;
69   std::cout << ""                                                                                                     << std::endl;
70   std::cout << "Usage:"                                                                                               << std::endl;
71   std::cout << "   ResourceExporter <parameters-list>"                                                                << std::endl;
72   std::cout << "Parameters have the following format:"                                                                << std::endl;
73   std::cout << "   '<section>:<name>=<new_value>'            - to set <new_value> for the user "                      << std::endl;
74   std::cout << "                                               preference <name> from the <section> section;"         << std::endl;
75   std::cout << "   '<section>:<name>+=<value>[|<separator>]' - the <value> is appended to the current value "         << std::endl;
76   std::cout << "                                               of the preference <name> from the <section> section;"  << std::endl;
77   std::cout << "                                               separator is used to concatenate old and new values,"  << std::endl;
78   std::cout << "                                               by default comma (',') symbol is used"                 << std::endl;
79   std::cout << "   '-<section>:<name>'                       - to remove user preference <name> from the <section>"   << std::endl;
80   std::cout << "                                               section. Note that only use preference file is "       << std::endl;
81   std::cout << "                                               influenced, you may need to use '<section>:<name>=""'" << std::endl;
82   std::cout << "                                               option"                                                << std::endl;
83   std::cout << "The number of parameters is limeted only by maximum possible length of the command line."             << std::endl;
84   std::cout << ""                                                                                                     << std::endl;
85 }
86
87 /*!
88   application main() function
89 */
90 int main( int argc, char** argv )
91 {
92   QApplication app( argc, argv );
93   if ( argc > 1 ) {
94     SUIT_ResourceMgr* resMgr = new SUIT_ResourceMgr( QString( "SalomeApp" ), QString( "%1Config" ) );
95     resMgr->setVersion( salomeVersion() );
96     resMgr->setCurrentFormat( QString( "xml" ) );
97     resMgr->loadLanguage();
98     for ( int i = 1; i < argc; i ++ ) {
99       QString anArg = QString( argv[i] ).trimmed();
100       if ( anArg.startsWith( "-" ) ) {
101         anArg.remove( 0, 1 );
102         if ( anArg.contains( ":" ) ) {
103           QStringList vals = anArg.split( ":", QString::SkipEmptyParts );
104           QString section  = vals[ 0 ].trimmed();
105           QString param    = vals[ 1 ].trimmed();
106           if ( section.isEmpty() || param.isEmpty() ) continue;
107           resMgr->remove( section, param );
108         }
109       }
110       else if ( anArg.contains( "+=" ) ) {
111         QStringList vals = anArg.split( "+=", QString::SkipEmptyParts );
112         if ( vals[ 0 ].contains( ":" ) ) {
113           QStringList vals1 = vals[ 0 ].split( ":", QString::SkipEmptyParts );
114           QString section  = vals1[ 0 ].trimmed();
115           QString param    = vals1[ 1 ].trimmed();
116           QString newValue = vals [ 1 ].trimmed();
117           QString separ    = ","; // default separator
118           if ( newValue.contains( "|" ) ) {
119             QStringList vals2 = newValue.split( "|", QString::SkipEmptyParts );
120             newValue = vals2[ 0 ].trimmed();
121             separ  = vals2[ 1 ].trimmed();
122           }
123           if ( section.isEmpty() || param.isEmpty() || newValue.isEmpty() || separ.isEmpty() ) continue;
124           QString value = resMgr->stringValue( section, param );
125           QStringList valsOld = value.split( separ, QString::SkipEmptyParts );
126           QStringList valsNew = newValue.split( separ, QString::SkipEmptyParts );
127           for ( int i = 0; i < (int)valsNew.count(); i++ )
128             if ( !valsOld.contains( valsNew[i] ) )
129               valsOld.append( valsNew[i] );
130           resMgr->setValue( section, param, valsOld.join( separ ) );
131         }
132       }
133       else if ( anArg.contains( "=" ) ) {
134         QStringList vals = anArg.split( "=", QString::SkipEmptyParts );
135         if ( vals[ 0 ].contains( ":" ) ) {
136           QStringList vals1 = vals[ 0 ].split( ":", QString::SkipEmptyParts );
137           QString section  = vals1[ 0 ].trimmed();
138           QString param    = vals1[ 1 ].trimmed();
139           QString value = vals [ 1 ].trimmed();
140           if ( section.isEmpty() || param.isEmpty() ) continue;
141           resMgr->setValue( section, param, value );
142         }
143       }
144     }
145     resMgr->save();
146     delete resMgr;
147   }
148   else {
149     help();
150   }
151   return 0;
152 }