1 #include "QtxResourceMgr.h"
7 #include <qtranslator.h>
8 #include <qapplication.h>
17 Class: QtxResourceMgr::Resources
21 QtxResourceMgr::Resources::Resources( const QString& fileName )
22 : myFileName( fileName )
26 QtxResourceMgr::Resources::~Resources()
30 QString QtxResourceMgr::Resources::file() const
35 void QtxResourceMgr::Resources::setFile( const QString& fn )
40 QString QtxResourceMgr::Resources::value( const QString& sect, const QString& name, const bool subst ) const
44 if ( hasValue( sect, name ) )
46 val = section( sect )[name];
48 val = makeSubstitution( val, sect, name );
53 void QtxResourceMgr::Resources::setValue( const QString& sect, const QString& name, const QString& val )
55 Section& s = section( sect );
56 s.insert( name, val );
59 bool QtxResourceMgr::Resources::hasSection( const QString& sect ) const
61 return mySections.contains( sect );
64 bool QtxResourceMgr::Resources::hasValue( const QString& sect, const QString& name ) const
66 return hasSection( sect ) && section( sect ).contains( name );
69 void QtxResourceMgr::Resources::removeSection( const QString& sect )
71 mySections.remove( sect );
74 void QtxResourceMgr::Resources::removeValue( const QString& sect, const QString& name )
76 if ( !hasSection( sect ) )
79 Section& s = section( sect );
83 mySections.remove( sect );
86 void QtxResourceMgr::Resources::clear()
91 QStringList QtxResourceMgr::Resources::sections() const
93 return mySections.keys();
96 QStringList QtxResourceMgr::Resources::parameters( const QString& sec ) const
98 if ( !hasSection( sec ) )
101 return section( sec ).keys();
104 QString QtxResourceMgr::Resources::path( const QString& sec, const QString& prefix, const QString& name ) const
106 QString filePath = fileName( sec, prefix, name );
107 if ( !filePath.isEmpty() )
109 if ( !QFileInfo( filePath ).exists() )
110 filePath = QString::null;
115 QtxResourceMgr::Section& QtxResourceMgr::Resources::section( const QString& sn )
117 if ( !mySections.contains( sn ) )
118 mySections.insert( sn, Section() );
120 return mySections[sn];
123 const QtxResourceMgr::Section& QtxResourceMgr::Resources::section( const QString& sn ) const
125 return mySections[sn];
128 QString QtxResourceMgr::Resources::fileName( const QString& sect, const QString& prefix, const QString& name ) const
131 if ( hasValue( sect, prefix ) )
133 path = value( sect, prefix, true );
134 if ( !path.isEmpty() )
136 if ( QFileInfo( path ).isRelative() )
137 path = Qtx::addSlash( QFileInfo( myFileName ).dirPath( true ) ) + path;
139 path = Qtx::addSlash( path ) + name;
142 return QDir::convertSeparators( path );
145 QPixmap QtxResourceMgr::Resources::loadPixmap( const QString& sect, const QString& prefix, const QString& name ) const
147 return QPixmap( fileName( sect, prefix, name ) );
150 QTranslator* QtxResourceMgr::Resources::loadTranslator( const QString& sect, const QString& prefix, const QString& name ) const
152 QTranslator* trans = new QTranslator( 0 );
153 if ( !trans->load( fileName( sect, prefix, name ) ) )
161 QString QtxResourceMgr::Resources::environmentVariable( const QString& str, int& start, int& len ) const
163 QString varName = QString::null;
166 QRegExp rx( "\\$\\{([a-zA-Z]+[a-zA-Z0-9_]*)\\}|\\$\\(([a-zA-Z]+[a-zA-Z0-9_]*)\\)|\\$([a-zA-Z]+[a-zA-Z0-9_]*)|\\%([a-zA-Z]+[a-zA-Z0-9_]*)\\%" );
168 int pos = rx.search( str, start );
172 len = rx.matchedLength();
173 QStringList caps = rx.capturedTexts();
174 for ( uint i = 1; i <= caps.count() && varName.isEmpty(); i++ )
175 varName = *caps.at( i );
180 QString QtxResourceMgr::Resources::makeSubstitution( const QString& str, const QString& sect, const QString& name ) const
184 QMap<QString, int> ignoreMap;
185 ignoreMap.insert( name, 0 );
187 int start( 0 ), len( 0 );
190 QString envName = environmentVariable( res, start, len );
191 if ( envName.isNull() )
194 QString newStr = QString::null;
195 if ( ::getenv( envName ) )
196 newStr = QString( ::getenv( envName ) );
198 if ( newStr.isNull() )
200 if ( ignoreMap.contains( envName ) )
206 if ( hasValue( sect, envName ) )
207 newStr = value( sect, envName, false );
208 ignoreMap.insert( envName, 0 );
210 res.replace( start, len, newStr );
217 Class: QtxResourceMgr::IniFormat
221 class QtxResourceMgr::IniFormat : public Format
228 virtual bool load( const QString&, QMap<QString, Section>& );
229 virtual bool save( const QString&, const QMap<QString, Section>& );
232 QtxResourceMgr::IniFormat::IniFormat()
237 QtxResourceMgr::IniFormat::~IniFormat()
241 bool QtxResourceMgr::IniFormat::load( const QString& fname, QMap<QString, Section>& secMap )
244 if ( !file.open( IO_ReadOnly ) )
247 QTextStream ts( &file );
254 QString separator = option( "separator" );
255 if ( separator.isNull() )
256 separator = QString( "=" );
258 QString comment = option( "comment" );
259 if ( comment.isNull() )
260 comment = QString( "#" );
264 data = ts.readLine();
270 data = data.stripWhiteSpace();
271 if ( data.isEmpty() )
274 if ( data.startsWith( comment ) )
277 QRegExp rx( "^\\[([\\w\\s\\._]*)\\]$" );
278 if ( rx.search( data ) != -1 )
280 section = rx.cap( 1 );
281 if ( section.isEmpty() )
284 qWarning( QString( "Empty section in line %1" ).arg( line ) );
287 else if ( data.contains( "=" ) && !section.isEmpty() )
289 int pos = data.find( separator );
290 QString key = data.left( pos - 1 ).stripWhiteSpace();
291 QString val = data.mid( pos + 1 ).stripWhiteSpace();
292 secMap[section].insert( key, val );
297 section.isEmpty() ? qWarning( "Current section is empty" ) :
298 qWarning( QString( "Error in line: %1" ).arg( line ) );
307 bool QtxResourceMgr::IniFormat::save( const QString& fname, const QMap<QString, Section>& secMap )
310 if ( !file.open( IO_WriteOnly ) )
314 for ( QMap<QString, Section>::ConstIterator it = secMap.begin(); it != secMap.end() && res; ++it )
316 QString data = QString( "[%1]\n" ).arg( it.key() );
317 for ( Section::ConstIterator iter = it.data().begin(); iter != it.data().end(); ++iter )
318 data += iter.key() + " = " + iter.data() + "\n";
321 res = file.writeBlock( data.latin1(), data.length() ) == (int)data.length();
330 Class: QtxResourceMgr::XmlFormat
334 class QtxResourceMgr::XmlFormat : public Format
341 virtual bool load( const QString&, QMap<QString, Section>& );
342 virtual bool save( const QString&, const QMap<QString, Section>& );
345 QString docTag() const;
346 QString sectionTag() const;
347 QString parameterTag() const;
348 QString nameAttribute() const;
349 QString valueAttribute() const;
352 QtxResourceMgr::XmlFormat::XmlFormat()
357 QtxResourceMgr::XmlFormat::~XmlFormat()
361 bool QtxResourceMgr::XmlFormat::load( const QString& fname, QMap<QString, Section>& secMap )
368 if ( !file.open( IO_ReadOnly ) )
373 res = doc.setContent( &file );
379 QDomElement root = doc.documentElement();
380 if ( root.isNull() || root.tagName() != docTag() )
383 QDomNode sectNode = root.firstChild();
384 while ( res && !sectNode.isNull() )
386 res = sectNode.isElement();
389 QDomElement sectElem = sectNode.toElement();
390 if ( sectElem.tagName() == sectionTag() && sectElem.hasAttribute( nameAttribute() ) )
392 QString section = sectElem.attribute( nameAttribute() );
393 QDomNode paramNode = sectNode.firstChild();
394 while ( res && !paramNode.isNull() )
396 res = paramNode.isElement();
399 QDomElement paramElem = paramNode.toElement();
400 if ( paramElem.tagName() == parameterTag() &&
401 paramElem.hasAttribute( nameAttribute() ) && paramElem.hasAttribute( valueAttribute() ) )
403 QString paramName = paramElem.attribute( nameAttribute() );
404 QString paramValue = paramElem.attribute( valueAttribute() );
406 secMap[section].insert( paramName, paramValue );
412 res = paramNode.isComment();
414 paramNode = paramNode.nextSibling();
421 res = sectNode.isComment(); // if it's a comment -- let it be, pass it..
423 sectNode = sectNode.nextSibling();
431 bool QtxResourceMgr::XmlFormat::save( const QString& fname, const QMap<QString, Section>& secMap )
438 if ( !file.open( IO_WriteOnly ) )
441 QDomDocument doc( docTag() );
442 QDomElement root = doc.createElement( docTag() );
443 doc.appendChild( root );
445 for ( QMap<QString, Section>::ConstIterator it = secMap.begin(); it != secMap.end(); ++it )
447 QDomElement sect = doc.createElement( sectionTag() );
448 sect.setAttribute( nameAttribute(), it.key() );
449 root.appendChild( sect );
450 for ( QMap<QString, QString>::ConstIterator iter = it.data().begin(); iter != it.data().end(); ++iter )
452 QDomElement val = doc.createElement( parameterTag() );
453 val.setAttribute( nameAttribute(), iter.key() );
454 val.setAttribute( valueAttribute(), iter.data() );
455 sect.appendChild( val );
459 QString docStr = doc.toString();
460 res = file.writeBlock( docStr.latin1(), docStr.length() ) == (int)docStr.length();
468 QString QtxResourceMgr::XmlFormat::docTag() const
470 QString tag = option( "doc_tag" );
472 tag = QString( "document" );
476 QString QtxResourceMgr::XmlFormat::sectionTag() const
478 QString tag = option( "section_tag" );
480 tag = QString( "section" );
484 QString QtxResourceMgr::XmlFormat::parameterTag() const
486 QString tag = option( "parameter_tag" );
488 tag = QString( "parameter" );
492 QString QtxResourceMgr::XmlFormat::nameAttribute() const
494 QString str = option( "name_attribute" );
496 str = QString( "name" );
500 QString QtxResourceMgr::XmlFormat::valueAttribute() const
502 QString str = option( "value_attribute" );
504 str = QString( "value" );
509 Class: QtxResourceMgr::Format
514 \brief Constructs the format object with specified name.
515 \param fmt - name of the format
517 QtxResourceMgr::Format::Format( const QString& fmt )
523 \brief Destructs the format object.
525 QtxResourceMgr::Format::~Format()
530 \brief Returns the format name.
532 QString QtxResourceMgr::Format::format() const
538 \brief Returns the string list of the format options.
540 QStringList QtxResourceMgr::Format::options() const
546 \brief Returns the value of the option with specified name.
547 If option doesn't exist then empty string returned.
548 \param opt - name of the option
550 QString QtxResourceMgr::Format::option( const QString& opt ) const
553 if ( myOpt.contains( opt ) )
559 \brief Sets the value of the option with specified name.
560 \param opt - name of the option
561 \param opt - value of the option
563 void QtxResourceMgr::Format::setOption( const QString& opt, const QString& val )
565 myOpt.insert( opt, val );
569 \brief Perform the loading of the resources from resource file.
570 \param res - resources object which will be loaded
572 bool QtxResourceMgr::Format::load( Resources* res )
577 QMap<QString, Section> sections;
578 bool status = load( res->myFileName, sections );
580 res->mySections = sections;
582 qDebug( "QtxResourceMgr: Could not load resource file \"%s\"", res->myFileName.latin1() );
588 \brief Perform the saving of the resources into resource file.
589 \param res - resources object which will be saved
591 bool QtxResourceMgr::Format::save( Resources* res )
596 Qtx::mkDir( Qtx::dir( res->myFileName ) );
598 return save( res->myFileName, res->mySections );
602 Class: QtxResourceMgr
607 \brief Constructs the resource manager object for application.
608 \param appName - name of the application which resources will be used.
609 \param resVarTemplate - template for the resource environment variable name which
610 should point to the resource directory list.
611 Default value is "%1Resources". Its mean that for application
612 with name "MyApp" environment variable "MyAppResources" will
613 be used. Template may not have the parameter '%1' substituted
614 by application name. In this case this string will be used as
615 is without substitution.
616 Resource environment variable should contains one or several resource directories
617 separated by symbol ';'. Resource directories list transfered into the setDirList().
618 These directories and the user home directory used for the loading application resources.
619 Each of the resource directories can contains resource file. The name of this file defined
620 by the function globalFileName(). Resource file name in the user home defined by the
621 function userFileName(). Any resource looking firstly in the user home resources then
622 resource directories used in the specified order. All setted resources always stored into
623 the resource file at the user home. Only user home resource file is saved.
625 QtxResourceMgr::QtxResourceMgr( const QString& appName, const QString& resVarTemplate )
626 : myAppName( appName ),
629 QString envVar = !resVarTemplate.isEmpty() ? resVarTemplate : QString( "%1Resources" );
630 if ( envVar.contains( "%1" ) )
631 envVar = envVar.arg( appName );
634 if ( ::getenv( envVar ) )
635 dirs = ::getenv( envVar );
637 setDirList( QStringList::split( ";", dirs ) );
639 installFormat( new XmlFormat() );
640 installFormat( new IniFormat() );
642 setOption( "translators", QString( "%P_msg_%L.qm|%P_images.qm" ) );
646 \brief Destructs the resource manager object and free allocated memory.
648 QtxResourceMgr::~QtxResourceMgr()
650 QStringList prefList = myTranslator.keys();
651 for ( QStringList::const_iterator it = prefList.begin(); it != prefList.end(); ++it )
652 removeTranslators( *it );
656 \brief Returns the application name.
658 QString QtxResourceMgr::appName() const
664 \brief Returns the checking of the existance flag. If its 'true' then resource
665 will be setted into the manager only if it doesn't exist or has different
666 value that existing value.
668 bool QtxResourceMgr::checkExisting() const
674 \brief Sets the checking of the existance flag.
675 \param on - boolean value of the flag.
677 void QtxResourceMgr::setCheckExisting( const bool on )
683 \brief Returns the resource directories list except user home directory.
685 QStringList QtxResourceMgr::dirList() const
691 \brief Initialise the manager. Prepare the resource containers and load resources.
692 \param autoLoad - if 'true' then all resources will be loaded.
694 void QtxResourceMgr::initialize( const bool autoLoad ) const
696 if ( !myResources.isEmpty() )
699 QtxResourceMgr* that = (QtxResourceMgr*)this;
701 that->myResources.append( new Resources( userFileName( appName() ) ) );
702 for ( QStringList::const_iterator it = myDirList.begin(); it != myDirList.end(); ++it )
704 QString path = Qtx::addSlash( *it ) + globalFileName( appName() );
705 that->myResources.append( new Resources( path ) );
713 \brief Removes all resources from the manager.
715 void QtxResourceMgr::clear()
717 for ( ResListIterator it( myResources ); it.current(); ++it )
718 it.current()->clear();
722 \brief Get the resource value as integer. Returns 'true' if it successfull otherwise
724 \param sect - Resource section name which contains resource.
725 \param name - Name of the resource.
726 \param iVal - Reference on the variable which should contains the resource output.
728 bool QtxResourceMgr::value( const QString& sect, const QString& name, int& iVal ) const
731 if ( !value( sect, name, val, true ) )
735 iVal = val.toInt( &ok );
741 \brief Get the resource value as double. Returns 'true' if it successfull otherwise
743 \param sect - Resource section name which contains resource.
744 \param name - Name of the resource.
745 \param dVal - Reference on the variable which should contains the resource output.
747 bool QtxResourceMgr::value( const QString& sect, const QString& name, double& dVal ) const
750 if ( !value( sect, name, val, true ) )
754 dVal = val.toDouble( &ok );
760 \brief Get the resource value as boolean. Returns 'true' if it successfull otherwise
762 \param sect - Resource section name which contains resource.
763 \param name - Name of the resource.
764 \param bVal - Reference on the variable which should contains the resource output.
766 bool QtxResourceMgr::value( const QString& sect, const QString& name, bool& bVal ) const
769 if ( !value( sect, name, val, true ) )
772 static QMap<QString, bool> boolMap;
773 if ( boolMap.isEmpty() )
775 boolMap["true"] = boolMap["yes"] = boolMap["on"] = true;
776 boolMap["false"] = boolMap["no"] = boolMap["off"] = false;
780 bool res = boolMap.contains( val );
785 double num = val.toDouble( &res );
794 \brief Get the resource value as color. Returns 'true' if it successfull otherwise
796 \param sect - Resource section name which contains resource.
797 \param name - Name of the resource.
798 \param cVal - Reference on the variable which should contains the resource output.
800 bool QtxResourceMgr::value( const QString& sect, const QString& name, QColor& cVal ) const
803 if ( !value( sect, name, val, true ) )
807 QStringList vals = QStringList::split( ",", val, true );
810 for ( QStringList::const_iterator it = vals.begin(); it != vals.end() && res; ++it )
811 nums.append( (*it).toInt( &res ) );
813 if ( res && nums.count() >= 3 )
814 cVal.setRgb( nums[0], nums[1], nums[2] );
817 int pack = val.toInt( &res );
819 Qtx::rgbSet( pack, cVal );
826 \brief Get the resource value as font. Returns 'true' if it successfull otherwise
828 \param sect - Resource section name which contains resource.
829 \param name - Name of the resource.
830 \param fVal - Reference on the variable which should contains the resource output.
832 bool QtxResourceMgr::value( const QString& sect, const QString& name, QFont& fVal ) const
835 if ( !value( sect, name, val, true ) )
838 QStringList fontDescr = QStringList::split( ",", val );
840 if ( fontDescr.count() < 2 )
843 QString family = fontDescr[0];
844 if ( family.isEmpty() )
847 fVal = QFont( family );
849 for ( int i = 1; i < (int)fontDescr.count(); i++ )
851 QString curval = fontDescr[i].stripWhiteSpace().lower();
852 if ( curval == QString( "bold" ) )
853 fVal.setBold( true );
854 else if ( curval == QString( "italic" ) )
855 fVal.setItalic( true );
856 else if ( curval == QString( "underline" ) )
857 fVal.setUnderline( true );
861 int ps = curval.toInt( &isOk );
863 fVal.setPointSize( ps );
871 \brief Get the resource value as string (native format). Returns 'true' if it
872 successfull otherwise returns 'false'.
873 \param sect - Resource section name which contains resource.
874 \param name - Name of the resource.
875 \param val - Reference on the variable which should contains the resource output.
876 \param subst - If 'true' then manager substitute reference on environment variables
877 and other resources by thier values. Default value of this parameter
880 bool QtxResourceMgr::value( const QString& sect, const QString& name, QString& val, const bool subst ) const
885 for ( ResListIterator it( myResources ); it.current() && !ok; ++it )
887 ok = it.current()->hasValue( sect, name );
889 val = it.current()->value( sect, name, subst );
896 \brief Returns the integer resource value. If resource can not be found or converted
897 then specified default value will be returned.
898 \param sect - Resource section name which contains resource.
899 \param name - Name of the resource.
900 \param def - Default resource value which will be used when resource not found.
902 int QtxResourceMgr::integerValue( const QString& sect, const QString& name, const int def ) const
905 if ( !value( sect, name, val ) )
911 \brief Returns the double resource value. If resource can not be found or converted
912 then specified default value will be returned.
913 \param sect - Resource section name which contains resource.
914 \param name - Name of the resource.
915 \param def - Default resource value which will be used when resource not found.
917 double QtxResourceMgr::doubleValue( const QString& sect, const QString& name, const double def ) const
920 if ( !value( sect, name, val ) )
926 \brief Returns the boolean resource value. If resource can not be found or converted
927 then specified default value will be returned.
928 \param sect - Resource section name which contains resource.
929 \param name - Name of the resource.
930 \param def - Default resource value which will be used when resource not found.
932 bool QtxResourceMgr::booleanValue( const QString& sect, const QString& name, const bool def ) const
935 if ( !value( sect, name, val ) )
941 \brief Returns the font resource value. If resource can not be found or converted
942 then specified default value will be returned.
943 \param sect - Resource section name which contains resource.
944 \param name - Name of the resource.
945 \param def - Default resource value which will be used when resource not found.
947 QFont QtxResourceMgr::fontValue( const QString& sect, const QString& name, const QFont& def ) const
950 if( !value( sect, name, font ) )
956 \brief Returns the color resource value. If resource can not be found or converted
957 then specified default value will be returned.
958 \param sect - Resource section name which contains resource.
959 \param name - Name of the resource.
960 \param def - Default resource value which will be used when resource not found.
962 QColor QtxResourceMgr::colorValue( const QString& sect, const QString& name, const QColor& def ) const
965 if ( !value( sect, name, val ) )
971 \brief Returns the string resource value. If resource can not be found or converted
972 then specified default value will be returned.
973 \param sect - Resource section name which contains resource.
974 \param name - Name of the resource.
975 \param def - Default resource value which will be used when resource not found.
977 QString QtxResourceMgr::stringValue( const QString& sect, const QString& name, const QString& def ) const
980 if ( !value( sect, name, val ) )
986 \brief Checks existance of the specified resource.
987 \param sect - Resource section name which contains resource.
988 \param name - Name of the resource.
990 bool QtxResourceMgr::hasValue( const QString& sect, const QString& name ) const
995 for ( ResListIterator it( myResources ); it.current() && !ok; ++it )
996 ok = it.current()->hasValue( sect, name );
1002 \brief Checks existance of the specified resource section.
1003 \param sect - Resource section name which contains resource.
1005 bool QtxResourceMgr::hasSection( const QString& sect ) const
1010 for ( ResListIterator it( myResources ); it.current() && !ok; ++it )
1011 ok = it.current()->hasSection( sect );
1017 \brief Sets the integer resource value.
1018 \param sect - Resource section name.
1019 \param name - Name of the resource.
1020 \param val - Resource value.
1022 void QtxResourceMgr::setValue( const QString& sect, const QString& name, int val )
1025 if ( checkExisting() && value( sect, name, res ) && res == val )
1028 setResource( sect, name, QString::number( val ) );
1032 \brief Sets the double resource value.
1033 \param sect - Resource section name.
1034 \param name - Name of the resource.
1035 \param val - Resource value.
1037 void QtxResourceMgr::setValue( const QString& sect, const QString& name, double val )
1040 if ( checkExisting() && value( sect, name, res ) && res == val )
1043 setResource( sect, name, QString::number( val, 'g', 12 ) );
1047 \brief Sets the boolean resource value.
1048 \param sect - Resource section name.
1049 \param name - Name of the resource.
1050 \param val - Resource value.
1052 void QtxResourceMgr::setValue( const QString& sect, const QString& name, bool val )
1055 if ( checkExisting() && value( sect, name, res ) && res == val )
1058 setResource( sect, name, QString( val ? "true" : "false" ) );
1062 \brief Sets the color resource value.
1063 \param sect - Resource section name.
1064 \param name - Name of the resource.
1065 \param val - Resource value.
1067 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QColor& val )
1070 if ( checkExisting() && value( sect, name, res ) && res == val )
1073 setResource( sect, name, QString( "%1, %2, %3" ).arg( val.red() ).arg( val.green() ).arg( val.blue() ) );
1077 \brief Sets the font resource value.
1078 \param sect - Resource section name.
1079 \param name - Name of the resource.
1080 \param val - Resource value.
1082 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QFont& val )
1085 if ( checkExisting() && value( sect, name, res ) && res == val )
1088 QStringList fontDescr;
1089 fontDescr.append( val.family() );
1091 fontDescr.append( "Bold" );
1093 fontDescr.append( "Italic" );
1094 if ( val.underline() )
1095 fontDescr.append( "Underline" );
1096 fontDescr.append( QString( "%1" ).arg( val.pointSize() ) );
1098 setResource( sect, name, fontDescr.join( "," ) );
1102 \brief Sets the string resource value.
1103 \param sect - Resource section name.
1104 \param name - Name of the resource.
1105 \param val - Resource value.
1107 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QString& val )
1110 if ( checkExisting() && value( sect, name, res ) && res == val )
1113 setResource( sect, name, val );
1117 \brief Remove the all specified resource section.
1118 \param sect - Resource section name.
1120 void QtxResourceMgr::remove( const QString& sect )
1124 for ( ResListIterator it( myResources ); it.current(); ++it )
1125 it.current()->removeSection( sect );
1129 \brief Remove the specified resource.
1130 \param sect - Resource section name.
1131 \param name - Name of the resource.
1133 void QtxResourceMgr::remove( const QString& sect, const QString& name )
1137 for ( ResListIterator it( myResources ); it.current(); ++it )
1138 it.current()->removeValue( sect, name );
1142 \brief Returns the current format which operates with resource files.
1144 QString QtxResourceMgr::currentFormat() const
1147 if ( !myFormats.isEmpty() )
1148 fmt = myFormats.getFirst()->format();
1153 \brief Sets the current format which operates with resource files.
1154 \param fmt - Resource format name.
1156 void QtxResourceMgr::setCurrentFormat( const QString& fmt )
1158 Format* form = format( fmt );
1162 myFormats.remove( form );
1163 myFormats.prepend( form );
1165 if ( myResources.isEmpty() )
1168 ResListIterator resIt( myResources );
1169 if ( resIt.current() )
1170 resIt.current()->setFile( userFileName( appName() ) );
1173 for ( QStringList::const_iterator it = myDirList.begin(); it != myDirList.end() && resIt.current(); ++it, ++resIt )
1174 resIt.current()->setFile( Qtx::addSlash( *it ) + globalFileName( appName() ) );
1178 \brief Returns the resource format object by it name.
1179 \param fmt - Resource format name.
1181 QtxResourceMgr::Format* QtxResourceMgr::format( const QString& fmt ) const
1184 for ( FormatListIterator it( myFormats ); it.current() && !form; ++it )
1186 if ( it.current()->format() == fmt )
1187 form = it.current();
1194 \brief Add the resource format to the manager. Newly added become current.
1195 \param form - Resource format object.
1197 void QtxResourceMgr::installFormat( QtxResourceMgr::Format* form )
1199 if ( !myFormats.contains( form ) )
1200 myFormats.prepend( form );
1204 \brief Remove the resource format from the manager.
1205 \param form - Resource format object.
1207 void QtxResourceMgr::removeFormat( QtxResourceMgr::Format* form )
1209 myFormats.remove( form );
1213 \brief Returns the string list of the resource format options names.
1215 QStringList QtxResourceMgr::options() const
1217 return myOptions.keys();
1221 \brief Returns the string value for the specified option. If option doesn't exist
1222 then empty string will be returned.
1223 \param opt - Option name.
1225 QString QtxResourceMgr::option( const QString& opt ) const
1228 if ( myOptions.contains( opt ) )
1229 val = myOptions[opt];
1234 \brief Sets the string value for the specified option.
1235 \param opt - Option name.
1236 \param val - Option value.
1238 void QtxResourceMgr::setOption( const QString& opt, const QString& val )
1240 myOptions.insert( opt, val );
1244 \brief Load the all resources from the resource files.
1246 bool QtxResourceMgr::load()
1248 initialize( false );
1250 Format* fmt = format( currentFormat() );
1255 for ( ResListIterator it( myResources ); it.current(); ++it )
1256 res = fmt->load( it.current() ) && res;
1262 \brief Save the changed resources in to the user resource file.
1264 bool QtxResourceMgr::save()
1266 initialize( false );
1268 Format* fmt = format( currentFormat() );
1272 if ( myResources.isEmpty() )
1275 return fmt->save( myResources.getFirst() );
1279 \brief Returns the string list of the existing section names..
1281 QStringList QtxResourceMgr::sections() const
1285 QMap<QString, int> map;
1286 for ( ResListIterator it( myResources ); it.current(); ++it )
1288 QStringList lst = it.current()->sections();
1289 for ( QStringList::const_iterator itr = lst.begin(); itr != lst.end(); ++itr )
1290 map.insert( *itr, 0 );
1294 for ( QMap<QString, int>::ConstIterator iter = map.begin(); iter != map.end(); ++iter )
1295 res.append( iter.key() );
1301 \brief Returns the string list of the existing resource names in the specified section.
1302 \param sec - Resource section name.
1304 QStringList QtxResourceMgr::parameters( const QString& sec ) const
1308 QMap<QString, int> map;
1309 for ( ResListIterator it( myResources ); it.current(); ++it )
1311 QStringList lst = it.current()->parameters( sec );
1312 for ( QStringList::const_iterator itr = lst.begin(); itr != lst.end(); ++itr )
1313 map.insert( *itr, 0 );
1317 for ( QMap<QString, int>::ConstIterator iter = map.begin(); iter != map.end(); ++iter )
1318 res.append( iter.key() );
1323 QString QtxResourceMgr::path( const QString& sect, const QString& prefix, const QString& name ) const
1326 for ( ResListIterator it( myResources ); it.current() && res.isEmpty(); ++it )
1327 res = it.current()->path( sect, prefix, name );
1331 QString QtxResourceMgr::resSection() const
1333 QString res = option( "res_section_name" );
1334 if ( res.isEmpty() )
1335 res = QString( "resources" );
1339 QString QtxResourceMgr::langSection() const
1341 QString res = option( "lang_section_name" );
1342 if ( res.isEmpty() )
1343 res = QString( "language" );
1347 QPixmap QtxResourceMgr::defaultPixmap() const
1349 return myDefaultPix;
1352 void QtxResourceMgr::setDefaultPixmap( const QPixmap& pix )
1357 QPixmap QtxResourceMgr::loadPixmap( const QString& prefix, const QString& name ) const
1359 return loadPixmap( prefix, name, true );
1362 QPixmap QtxResourceMgr::loadPixmap( const QString& prefix, const QString& name, const bool useDef ) const
1364 return loadPixmap( prefix, name, useDef ? defaultPixmap() : QPixmap() );
1367 QPixmap QtxResourceMgr::loadPixmap( const QString& prefix, const QString& name, const QPixmap& defPix ) const
1372 for ( ResListIterator it( myResources ); it.current() && pix.isNull(); ++it )
1373 pix = it.current()->loadPixmap( resSection(), prefix, name );
1379 void QtxResourceMgr::loadLanguage( const QString& pref, const QString& l )
1383 QMap<QChar, QString> substMap;
1384 substMap.insert( 'A', appName() );
1387 if ( lang.isEmpty() )
1388 value( langSection(), "language", lang );
1390 if ( lang.isEmpty() )
1392 lang = QString( "en" );
1393 qWarning( QString( "Language not specified. Assumed: %1" ).arg( lang ) );
1396 substMap.insert( 'L', lang );
1399 if ( value( langSection(), "translators", trs, false ) && !trs.isEmpty() )
1401 QStringList translators = QStringList::split( "|", option( "translators" ) );
1402 QStringList newTranslators = QStringList::split( "|", trs );
1403 for ( uint i = 0; i < newTranslators.count(); i++ )
1404 if ( translators.find( newTranslators[i] ) == translators.end() )
1405 translators += newTranslators[i];
1406 setOption( "translators", translators.join( "|" ) );
1409 QStringList trList = QStringList::split( "|", option( "translators" ) );
1410 if ( trList.isEmpty() )
1412 trList.append( "%P_msg_%L.qm" );
1413 qWarning( QString( "Translators not defined. Assumed: %1" ).arg( trList.first() ) );
1416 QStringList prefixList;
1417 if ( !pref.isEmpty() )
1418 prefixList.append( pref );
1420 prefixList = parameters( resSection() );
1422 for ( QStringList::const_iterator iter = prefixList.begin(); iter != prefixList.end(); ++iter )
1424 QString prefix = *iter;
1425 substMap.insert( 'P', prefix );
1428 for ( QStringList::const_iterator it = trList.begin(); it != trList.end(); ++it )
1429 trs.append( substMacro( *it, substMap ).stripWhiteSpace() );
1431 for ( QStringList::const_iterator itr = trs.begin(); itr != trs.end(); ++itr )
1432 loadTranslator( prefix, *itr );
1436 void QtxResourceMgr::loadTranslator( const QString& prefix, const QString& name )
1440 QTranslator* trans = 0;
1441 for ( ResListIterator it( myResources ); it.current() && !trans; ++it )
1442 trans = it.current()->loadTranslator( resSection(), prefix, name );
1447 if ( !myTranslator[prefix].contains( trans ) )
1448 myTranslator[prefix].append( trans );
1449 qApp->installTranslator( trans );
1452 void QtxResourceMgr::removeTranslators( const QString& prefix )
1454 if ( !myTranslator.contains( prefix ) )
1457 for ( TransListIterator it( myTranslator[prefix] ); it.current(); ++it )
1459 qApp->removeTranslator( it.current() );
1460 delete it.current();
1463 myTranslator.remove( prefix );
1466 void QtxResourceMgr::raiseTranslators( const QString& prefix )
1468 if ( !myTranslator.contains( prefix ) )
1471 for ( TransListIterator it( myTranslator[prefix] ); it.current(); ++it )
1473 qApp->removeTranslator( it.current() );
1474 qApp->installTranslator( it.current() );
1478 void QtxResourceMgr::refresh()
1480 QStringList sl = sections();
1481 for ( QStringList::const_iterator it = sl.begin(); it != sl.end(); ++it )
1483 QStringList pl = parameters( *it );
1484 for ( QStringList::const_iterator itr = pl.begin(); itr != pl.end(); ++itr )
1485 setResource( *it, *itr, stringValue( *it, *itr ) );
1489 void QtxResourceMgr::setDirList( const QStringList& dl )
1492 for ( ResListIterator it( myResources ); it.current(); ++it )
1493 delete it.current();
1495 myResources.clear();
1498 void QtxResourceMgr::setResource( const QString& sect, const QString& name, const QString& val )
1502 if ( !myResources.isEmpty() )
1503 myResources.first()->setValue( sect, name, val );
1506 QString QtxResourceMgr::userFileName( const QString& appName ) const
1509 QString pathName = QDir::homeDirPath();
1512 fileName = QString( "%1.%2" ).arg( appName ).arg( currentFormat() );
1514 fileName = QString( ".%1rc" ).arg( appName );
1517 if ( !fileName.isEmpty() )
1518 pathName = Qtx::addSlash( pathName ) + fileName;
1523 QString QtxResourceMgr::globalFileName( const QString& appName ) const
1525 return QString( "%1.%2" ).arg( appName ).arg( currentFormat() );
1528 QString QtxResourceMgr::substMacro( const QString& src, const QMap<QChar, QString>& substMap ) const
1532 QRegExp rx( "%[A-Za-z%]" );
1535 while ( ( idx = rx.search( trg, idx ) ) >= 0 )
1537 QChar spec = trg.at( idx + 1 );
1541 else if ( substMap.contains( spec ) )
1542 subst = substMap[spec];
1544 if ( !subst.isEmpty() )
1546 trg.replace( idx, rx.matchedLength(), subst );
1547 idx += subst.length();
1550 idx += rx.matchedLength();