]> SALOME platform Git repositories - modules/gui.git/blob - src/ResExporter/ResourceExporter.cxx
Salome HOME
ptv, do not send mouse release event just after mouse double click
[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->setVersion( QString( "3.0.0" ));
100     resMgr->setCurrentFormat( QString( "xml" ) );
101     resMgr->loadLanguage();
102     for ( int i = 1; i < argc; i ++ ) {
103       QString anArg = QString( argv[i] ).stripWhiteSpace();
104       if ( anArg.startsWith( "-" ) ) {
105         anArg.remove( 0, 1 );
106         if ( anArg.contains( ":" ) ) {
107           QStringList vals = QStringList::split( ":", anArg );
108           QString section  = vals[ 0 ].stripWhiteSpace();
109           QString param    = vals[ 1 ].stripWhiteSpace();
110           if ( section.isEmpty() || param.isEmpty() ) continue;
111           resMgr->remove( section, param );
112         }
113       }
114       else if ( anArg.contains( "+=" ) ) {
115         QStringList vals = QStringList::split( "+=", anArg );
116         if ( vals[ 0 ].contains( ":" ) ) {
117           QStringList vals1 = QStringList::split( ":", vals[ 0 ] );
118           QString section  = vals1[ 0 ].stripWhiteSpace();
119           QString param    = vals1[ 1 ].stripWhiteSpace();
120           QString newValue = vals [ 1 ].stripWhiteSpace();
121           QString separ    = ","; // default separator
122           if ( newValue.contains( "|" ) ) {
123             QStringList vals2 = QStringList::split( "|", newValue );
124             newValue = vals2[ 0 ].stripWhiteSpace();
125             separ  = vals2[ 1 ].stripWhiteSpace();
126           }
127           if ( section.isEmpty() || param.isEmpty() || newValue.isEmpty() || separ.isEmpty() ) continue;
128           QString value = resMgr->stringValue( section, param );
129           QStringList valsOld = QStringList::split( separ, value );
130           QStringList valsNew = QStringList::split( separ, newValue );
131           for ( int i = 0; i < valsNew.count(); i++ )
132             if ( !valsOld.contains( valsNew[i] ) )
133               valsOld.append( valsNew[i] );
134           resMgr->setValue( section, param, valsOld.join( separ ) );
135         }
136       }
137       else if ( anArg.contains( "=" ) ) {
138         QStringList vals = QStringList::split( "=", anArg );
139         if ( vals[ 0 ].contains( ":" ) ) {
140           QStringList vals1 = QStringList::split( ":", vals[ 0 ] );
141           QString section  = vals1[ 0 ].stripWhiteSpace();
142           QString param    = vals1[ 1 ].stripWhiteSpace();
143           QString value = vals [ 1 ].stripWhiteSpace();
144           if ( section.isEmpty() || param.isEmpty() ) continue;
145           resMgr->setValue( section, param, value );
146         }
147       }
148     }
149     resMgr->save();
150     delete resMgr;
151   }
152   else {
153     help();
154   }
155   return 0;
156 }