Back to index

salome-gui  6.5.0
ResourceExporter.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // File    : ResourceExporter.cxx
00024 // Author  : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
00025 //
00026 /*
00027  This tool provides command-line interface allowing to modify user preferences.
00028  The tool can be used by the compilation procedures in order to set default preferences for the module.
00029 
00030  Usage:
00031     ResourceExporter <parameters-list>
00032  Parameters have the following format:
00033     '<section>:<name>=<new_value>'            - to set <new_value> for the user 
00034                                                 preference <name> from the <section> section;
00035     '<section>:<name>+=<value>[|<separator>]' - the <value> is appended to the current value 
00036                                                 of the preference <name> from the <section> section;
00037                                                 separator is used to concatenate old and new values,
00038                                                 by default comma (',') symbol is used
00039     '-<section>:<name>'                       - to remove user preference <name> from the <section>
00040                                                 section. Note that only use preference file is 
00041                                                 influenced, you may need to use '<section>:<name>=""'
00042                                                 option
00043  The number of parameters is limeted only by maximum possible length of the command line.
00044 */
00045 
00046 #include "GUI_version.h"
00047 #include "SUIT_ResourceMgr.h"
00048 #include <QFile>
00049 #include <QDir>
00050 #include <QStringList>
00051 #include <QApplication>
00052 #include <iostream>
00053 
00057 static QString salomeVersion()
00058 {
00059   return GUI_VERSION_STR;
00060 }
00061 
00065 static void help()
00066 {
00067   std::cout << ""                                                                                                     << std::endl;
00068   std::cout << "ResourceExporter: provides command-line interface to modify user preferences."                        << std::endl;
00069   std::cout << ""                                                                                                     << std::endl;
00070   std::cout << "Usage:"                                                                                               << std::endl;
00071   std::cout << "   ResourceExporter <parameters-list>"                                                                << std::endl;
00072   std::cout << "Parameters have the following format:"                                                                << std::endl;
00073   std::cout << "   '<section>:<name>=<new_value>'            - to set <new_value> for the user "                      << std::endl;
00074   std::cout << "                                               preference <name> from the <section> section;"         << std::endl;
00075   std::cout << "   '<section>:<name>+=<value>[|<separator>]' - the <value> is appended to the current value "         << std::endl;
00076   std::cout << "                                               of the preference <name> from the <section> section;"  << std::endl;
00077   std::cout << "                                               separator is used to concatenate old and new values,"  << std::endl;
00078   std::cout << "                                               by default comma (',') symbol is used"                 << std::endl;
00079   std::cout << "   '-<section>:<name>'                       - to remove user preference <name> from the <section>"   << std::endl;
00080   std::cout << "                                               section. Note that only use preference file is "       << std::endl;
00081   std::cout << "                                               influenced, you may need to use '<section>:<name>=""'" << std::endl;
00082   std::cout << "                                               option"                                                << std::endl;
00083   std::cout << "The number of parameters is limeted only by maximum possible length of the command line."             << std::endl;
00084   std::cout << ""                                                                                                     << std::endl;
00085 }
00086 
00090 int main( int argc, char** argv )
00091 {
00092   QApplication app( argc, argv );
00093   if ( argc > 1 ) {
00094     SUIT_ResourceMgr* resMgr = new SUIT_ResourceMgr( QString( "SalomeApp" ), QString( "%1Config" ) );
00095     resMgr->setVersion( salomeVersion() );
00096     resMgr->setCurrentFormat( QString( "xml" ) );
00097     resMgr->loadLanguage();
00098     for ( int i = 1; i < argc; i ++ ) {
00099       QString anArg = QString( argv[i] ).trimmed();
00100       if ( anArg.startsWith( "-" ) ) {
00101         anArg.remove( 0, 1 );
00102         if ( anArg.contains( ":" ) ) {
00103           QStringList vals = anArg.split( ":", QString::SkipEmptyParts );
00104           QString section  = vals[ 0 ].trimmed();
00105           QString param    = vals[ 1 ].trimmed();
00106           if ( section.isEmpty() || param.isEmpty() ) continue;
00107           resMgr->remove( section, param );
00108         }
00109       }
00110       else if ( anArg.contains( "+=" ) ) {
00111         QStringList vals = anArg.split( "+=", QString::SkipEmptyParts );
00112         if ( vals[ 0 ].contains( ":" ) ) {
00113           QStringList vals1 = vals[ 0 ].split( ":", QString::SkipEmptyParts );
00114           QString section  = vals1[ 0 ].trimmed();
00115           QString param    = vals1[ 1 ].trimmed();
00116           QString newValue = vals [ 1 ].trimmed();
00117           QString separ    = ","; // default separator
00118           if ( newValue.contains( "|" ) ) {
00119             QStringList vals2 = newValue.split( "|", QString::SkipEmptyParts );
00120             newValue = vals2[ 0 ].trimmed();
00121             separ  = vals2[ 1 ].trimmed();
00122           }
00123           if ( section.isEmpty() || param.isEmpty() || newValue.isEmpty() || separ.isEmpty() ) continue;
00124           QString value = resMgr->stringValue( section, param );
00125           QStringList valsOld = value.split( separ, QString::SkipEmptyParts );
00126           QStringList valsNew = newValue.split( separ, QString::SkipEmptyParts );
00127           for ( int i = 0; i < (int)valsNew.count(); i++ )
00128             if ( !valsOld.contains( valsNew[i] ) )
00129               valsOld.append( valsNew[i] );
00130           resMgr->setValue( section, param, valsOld.join( separ ) );
00131         }
00132       }
00133       else if ( anArg.contains( "=" ) ) {
00134         QStringList vals = anArg.split( "=", QString::SkipEmptyParts );
00135         if ( vals[ 0 ].contains( ":" ) ) {
00136           QStringList vals1 = vals[ 0 ].split( ":", QString::SkipEmptyParts );
00137           QString section  = vals1[ 0 ].trimmed();
00138           QString param    = vals1[ 1 ].trimmed();
00139           QString value = vals [ 1 ].trimmed();
00140           if ( section.isEmpty() || param.isEmpty() ) continue;
00141           resMgr->setValue( section, param, value );
00142         }
00143       }
00144     }
00145     resMgr->save();
00146     delete resMgr;
00147   }
00148   else {
00149     help();
00150   }
00151   return 0;
00152 }