Salome HOME
29922a2cdb819e4fe17e62053db6e28851def77c
[modules/gui.git] / src / ResExporter / ResourceExporter.cxx
1 //======================================================================================================
2 // File:      ResourceExporter.cxx
3 // Created:   27/06/05
4 // Author:    Vadim SANDLER
5 // Copyright (C) CEA 2005
6 //
7 // This tool provides command-line interface allowing to modify user preferences.
8 // The tool can be used by the compilation procedures in order to set default preferences for the module.
9 //
10 // Usage:
11 //    ResourceExporter <parameters-list>
12 // Parameters have the following format:
13 //    '<section>:<name>=<new_value>'            - to set <new_value> for the user 
14 //                                                preference <name> from the <section> section;
15 //    '<section>:<name>+=<value>[|<separator>]' - the <value> is appended to the current value 
16 //                                                of the preference <name> from the <section> section;
17 //                                                separator is used to concatenate old and new values,
18 //                                                by default comma (',') symbol is used
19 //    '-<section>:<name>'                       - to remove user preference <name> from the <section>
20 //                                                section. Note that only use preference file is 
21 //                                                influenced, you may need to use '<section>:<name>=""'
22 //                                                option
23 // The number of parameters is limeted only by maximum possible length of the command line.
24 //======================================================================================================
25
26 #include "SUIT_ResourceMgr.h"
27 #include <qfile.h>
28 #include <qdir.h>
29 #include <qstringlist.h>
30 #include <qapplication.h>
31 #include <iostream>
32
33 //============================================================
34 // salomeVersion(): get the SALOME version number
35 //============================================================
36 static QString salomeVersion()
37 {
38   QString path( ::getenv( "GUI_ROOT_DIR" ) );
39   if ( !path.isEmpty() )
40     path += QDir::separator();
41   path += QString( "bin/salome/VERSION" );
42
43   QFile vf( path );
44   if ( !vf.open( IO_ReadOnly ) )
45     return QString::null;
46
47   QString line;
48   vf.readLine( line, 1024 );
49   vf.close();
50
51   if ( line.isEmpty() )
52     return QString::null;
53
54   while ( !line.isEmpty() && line.at( line.length() - 1 ) == QChar( '\n' ) )
55     line.remove( line.length() - 1, 1 );
56
57   QString ver;
58   int idx = line.findRev( ":" );
59   if ( idx != -1 )
60     ver = line.mid( idx + 1 ).stripWhiteSpace();
61
62   return ver;
63 }
64
65 //============================================================
66 // help(): print the help information
67 //============================================================
68 static void help()
69 {
70   std::cout << ""                                                                                                     << std::endl;
71   std::cout << "ResourceExporter: provides command-line interface to modify user preferences."                        << std::endl;
72   std::cout << ""                                                                                                     << std::endl;
73   std::cout << "Usage:"                                                                                               << std::endl;
74   std::cout << "   ResourceExporter <parameters-list>"                                                                << std::endl;
75   std::cout << "Parameters have the following format:"                                                                << std::endl;
76   std::cout << "   '<section>:<name>=<new_value>'            - to set <new_value> for the user "                      << std::endl;
77   std::cout << "                                               preference <name> from the <section> section;"         << std::endl;
78   std::cout << "   '<section>:<name>+=<value>[|<separator>]' - the <value> is appended to the current value "         << std::endl;
79   std::cout << "                                               of the preference <name> from the <section> section;"  << std::endl;
80   std::cout << "                                               separator is used to concatenate old and new values,"  << std::endl;
81   std::cout << "                                               by default comma (',') symbol is used"                 << std::endl;
82   std::cout << "   '-<section>:<name>'                       - to remove user preference <name> from the <section>"   << std::endl;
83   std::cout << "                                               section. Note that only use preference file is "       << std::endl;
84   std::cout << "                                               influenced, you may need to use '<section>:<name>=""'" << std::endl;
85   std::cout << "                                               option"                                                << std::endl;
86   std::cout << "The number of parameters is limeted only by maximum possible length of the command line."             << std::endl;
87   std::cout << ""                                                                                                     << std::endl;
88 }
89
90 //============================================================
91 // main(): application main() function
92 //============================================================
93 int main( int argc, char** argv )
94 {
95   QApplication app( argc, argv );
96   if ( argc > 1 ) {
97     SUIT_ResourceMgr* resMgr = new SUIT_ResourceMgr( QString( "SalomeApp" ), QString( "%1Config" ) );
98     resMgr->setVersion( salomeVersion() );
99     resMgr->setCurrentFormat( QString( "xml" ) );
100     resMgr->loadLanguage();
101     for ( int i = 1; i < argc; i ++ ) {
102       QString anArg = QString( argv[i] ).stripWhiteSpace();
103       if ( anArg.startsWith( "-" ) ) {
104         anArg.remove( 0, 1 );
105         if ( anArg.contains( ":" ) ) {
106           QStringList vals = QStringList::split( ":", anArg );
107           QString section  = vals[ 0 ].stripWhiteSpace();
108           QString param    = vals[ 1 ].stripWhiteSpace();
109           if ( section.isEmpty() || param.isEmpty() ) continue;
110           resMgr->remove( section, param );
111         }
112       }
113       else if ( anArg.contains( "+=" ) ) {
114         QStringList vals = QStringList::split( "+=", anArg );
115         if ( vals[ 0 ].contains( ":" ) ) {
116           QStringList vals1 = QStringList::split( ":", vals[ 0 ] );
117           QString section  = vals1[ 0 ].stripWhiteSpace();
118           QString param    = vals1[ 1 ].stripWhiteSpace();
119           QString newValue = vals [ 1 ].stripWhiteSpace();
120           QString separ    = ","; // default separator
121           if ( newValue.contains( "|" ) ) {
122             QStringList vals2 = QStringList::split( "|", newValue );
123             newValue = vals2[ 0 ].stripWhiteSpace();
124             separ  = vals2[ 1 ].stripWhiteSpace();
125           }
126           if ( section.isEmpty() || param.isEmpty() || newValue.isEmpty() || separ.isEmpty() ) continue;
127           QString value = resMgr->stringValue( section, param );
128           QStringList valsOld = QStringList::split( separ, value );
129           QStringList valsNew = QStringList::split( separ, newValue );
130           for ( int i = 0; i < valsNew.count(); i++ )
131             if ( !valsOld.contains( valsNew[i] ) )
132               valsOld.append( valsNew[i] );
133           resMgr->setValue( section, param, valsOld.join( separ ) );
134         }
135       }
136       else if ( anArg.contains( "=" ) ) {
137         QStringList vals = QStringList::split( "=", anArg );
138         if ( vals[ 0 ].contains( ":" ) ) {
139           QStringList vals1 = QStringList::split( ":", vals[ 0 ] );
140           QString section  = vals1[ 0 ].stripWhiteSpace();
141           QString param    = vals1[ 1 ].stripWhiteSpace();
142           QString value = vals [ 1 ].stripWhiteSpace();
143           if ( section.isEmpty() || param.isEmpty() ) continue;
144           resMgr->setValue( section, param, value );
145         }
146       }
147     }
148     resMgr->save();
149     delete resMgr;
150   }
151   else {
152     help();
153   }
154   return 0;
155 }