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 ).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 ( Section::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 if ( !userFileName( appName() ).isEmpty() )
702 that->myResources.append( new Resources( userFileName( appName() ) ) );
704 for ( QStringList::const_iterator it = myDirList.begin(); it != myDirList.end(); ++it )
706 QString path = Qtx::addSlash( *it ) + globalFileName( appName() );
707 that->myResources.append( new Resources( path ) );
715 \brief Removes all resources from the manager.
717 void QtxResourceMgr::clear()
719 for ( ResListIterator it( myResources ); it.current(); ++it )
720 it.current()->clear();
724 \brief Get the resource value as integer. Returns 'true' if it successfull otherwise
726 \param sect - Resource section name which contains resource.
727 \param name - Name of the resource.
728 \param iVal - Reference on the variable which should contains the resource output.
730 bool QtxResourceMgr::value( const QString& sect, const QString& name, int& iVal ) const
733 if ( !value( sect, name, val, true ) )
737 iVal = val.toInt( &ok );
743 \brief Get the resource value as double. Returns 'true' if it successfull otherwise
745 \param sect - Resource section name which contains resource.
746 \param name - Name of the resource.
747 \param dVal - Reference on the variable which should contains the resource output.
749 bool QtxResourceMgr::value( const QString& sect, const QString& name, double& dVal ) const
752 if ( !value( sect, name, val, true ) )
756 dVal = val.toDouble( &ok );
762 \brief Get the resource value as boolean. Returns 'true' if it successfull otherwise
764 \param sect - Resource section name which contains resource.
765 \param name - Name of the resource.
766 \param bVal - Reference on the variable which should contains the resource output.
768 bool QtxResourceMgr::value( const QString& sect, const QString& name, bool& bVal ) const
771 if ( !value( sect, name, val, true ) )
774 static QMap<QString, bool> boolMap;
775 if ( boolMap.isEmpty() )
777 boolMap["true"] = boolMap["yes"] = boolMap["on"] = true;
778 boolMap["false"] = boolMap["no"] = boolMap["off"] = false;
782 bool res = boolMap.contains( val );
787 double num = val.toDouble( &res );
796 \brief Get the resource value as color. Returns 'true' if it successfull otherwise
798 \param sect - Resource section name which contains resource.
799 \param name - Name of the resource.
800 \param cVal - Reference on the variable which should contains the resource output.
802 bool QtxResourceMgr::value( const QString& sect, const QString& name, QColor& cVal ) const
805 if ( !value( sect, name, val, true ) )
809 QStringList vals = QStringList::split( ",", val, true );
812 for ( QStringList::const_iterator it = vals.begin(); it != vals.end() && res; ++it )
813 nums.append( (*it).toInt( &res ) );
815 if ( res && nums.count() >= 3 )
816 cVal.setRgb( nums[0], nums[1], nums[2] );
819 int pack = val.toInt( &res );
821 Qtx::rgbSet( pack, cVal );
828 \brief Get the resource value as font. Returns 'true' if it successfull otherwise
830 \param sect - Resource section name which contains resource.
831 \param name - Name of the resource.
832 \param fVal - Reference on the variable which should contains the resource output.
834 bool QtxResourceMgr::value( const QString& sect, const QString& name, QFont& fVal ) const
837 if ( !value( sect, name, val, true ) )
840 QStringList fontDescr = QStringList::split( ",", val );
842 if ( fontDescr.count() < 2 )
845 QString family = fontDescr[0];
846 if ( family.isEmpty() )
849 fVal = QFont( family );
851 for ( int i = 1; i < (int)fontDescr.count(); i++ )
853 QString curval = fontDescr[i].stripWhiteSpace().lower();
854 if ( curval == QString( "bold" ) )
855 fVal.setBold( true );
856 else if ( curval == QString( "italic" ) )
857 fVal.setItalic( true );
858 else if ( curval == QString( "underline" ) )
859 fVal.setUnderline( true );
863 int ps = curval.toInt( &isOk );
865 fVal.setPointSize( ps );
873 \brief Get the resource value as string (native format). Returns 'true' if it
874 successfull otherwise returns 'false'.
875 \param sect - Resource section name which contains resource.
876 \param name - Name of the resource.
877 \param val - Reference on the variable which should contains the resource output.
878 \param subst - If 'true' then manager substitute reference on environment variables
879 and other resources by thier values. Default value of this parameter
882 bool QtxResourceMgr::value( const QString& sect, const QString& name, QString& val, const bool subst ) const
887 for ( ResListIterator it( myResources ); it.current() && !ok; ++it )
889 ok = it.current()->hasValue( sect, name );
891 val = it.current()->value( sect, name, subst );
898 \brief Returns the integer resource value. If resource can not be found or converted
899 then specified default value will be returned.
900 \param sect - Resource section name which contains resource.
901 \param name - Name of the resource.
902 \param def - Default resource value which will be used when resource not found.
904 int QtxResourceMgr::integerValue( const QString& sect, const QString& name, const int def ) const
907 if ( !value( sect, name, val ) )
913 \brief Returns the double resource value. If resource can not be found or converted
914 then specified default value will be returned.
915 \param sect - Resource section name which contains resource.
916 \param name - Name of the resource.
917 \param def - Default resource value which will be used when resource not found.
919 double QtxResourceMgr::doubleValue( const QString& sect, const QString& name, const double def ) const
922 if ( !value( sect, name, val ) )
928 \brief Returns the boolean resource value. If resource can not be found or converted
929 then specified default value will be returned.
930 \param sect - Resource section name which contains resource.
931 \param name - Name of the resource.
932 \param def - Default resource value which will be used when resource not found.
934 bool QtxResourceMgr::booleanValue( const QString& sect, const QString& name, const bool def ) const
937 if ( !value( sect, name, val ) )
943 \brief Returns the font resource value. If resource can not be found or converted
944 then specified default value will be returned.
945 \param sect - Resource section name which contains resource.
946 \param name - Name of the resource.
947 \param def - Default resource value which will be used when resource not found.
949 QFont QtxResourceMgr::fontValue( const QString& sect, const QString& name, const QFont& def ) const
952 if( !value( sect, name, font ) )
958 \brief Returns the color resource value. If resource can not be found or converted
959 then specified default value will be returned.
960 \param sect - Resource section name which contains resource.
961 \param name - Name of the resource.
962 \param def - Default resource value which will be used when resource not found.
964 QColor QtxResourceMgr::colorValue( const QString& sect, const QString& name, const QColor& def ) const
967 if ( !value( sect, name, val ) )
973 \brief Returns the string resource value. If resource can not be found or converted
974 then specified default value will be returned.
975 \param sect - Resource section name which contains resource.
976 \param name - Name of the resource.
977 \param def - Default resource value which will be used when resource not found.
979 QString QtxResourceMgr::stringValue( const QString& sect, const QString& name, const QString& def ) const
982 if ( !value( sect, name, val ) )
988 \brief Checks existance of the specified resource.
989 \param sect - Resource section name which contains resource.
990 \param name - Name of the resource.
992 bool QtxResourceMgr::hasValue( const QString& sect, const QString& name ) const
997 for ( ResListIterator it( myResources ); it.current() && !ok; ++it )
998 ok = it.current()->hasValue( sect, name );
1004 \brief Checks existance of the specified resource section.
1005 \param sect - Resource section name which contains resource.
1007 bool QtxResourceMgr::hasSection( const QString& sect ) const
1012 for ( ResListIterator it( myResources ); it.current() && !ok; ++it )
1013 ok = it.current()->hasSection( sect );
1019 \brief Sets the integer resource value.
1020 \param sect - Resource section name.
1021 \param name - Name of the resource.
1022 \param val - Resource value.
1024 void QtxResourceMgr::setValue( const QString& sect, const QString& name, int val )
1027 if ( checkExisting() && value( sect, name, res ) && res == val )
1030 setResource( sect, name, QString::number( val ) );
1034 \brief Sets the double resource value.
1035 \param sect - Resource section name.
1036 \param name - Name of the resource.
1037 \param val - Resource value.
1039 void QtxResourceMgr::setValue( const QString& sect, const QString& name, double val )
1042 if ( checkExisting() && value( sect, name, res ) && res == val )
1045 setResource( sect, name, QString::number( val, 'g', 12 ) );
1049 \brief Sets the boolean resource value.
1050 \param sect - Resource section name.
1051 \param name - Name of the resource.
1052 \param val - Resource value.
1054 void QtxResourceMgr::setValue( const QString& sect, const QString& name, bool val )
1057 if ( checkExisting() && value( sect, name, res ) && res == val )
1060 setResource( sect, name, QString( val ? "true" : "false" ) );
1064 \brief Sets the color resource value.
1065 \param sect - Resource section name.
1066 \param name - Name of the resource.
1067 \param val - Resource value.
1069 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QColor& val )
1072 if ( checkExisting() && value( sect, name, res ) && res == val )
1075 setResource( sect, name, QString( "%1, %2, %3" ).arg( val.red() ).arg( val.green() ).arg( val.blue() ) );
1079 \brief Sets the font resource value.
1080 \param sect - Resource section name.
1081 \param name - Name of the resource.
1082 \param val - Resource value.
1084 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QFont& val )
1087 if ( checkExisting() && value( sect, name, res ) && res == val )
1090 QStringList fontDescr;
1091 fontDescr.append( val.family() );
1093 fontDescr.append( "Bold" );
1095 fontDescr.append( "Italic" );
1096 if ( val.underline() )
1097 fontDescr.append( "Underline" );
1098 fontDescr.append( QString( "%1" ).arg( val.pointSize() ) );
1100 setResource( sect, name, fontDescr.join( "," ) );
1104 \brief Sets the string resource value.
1105 \param sect - Resource section name.
1106 \param name - Name of the resource.
1107 \param val - Resource value.
1109 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QString& val )
1112 if ( checkExisting() && value( sect, name, res ) && res == val )
1115 setResource( sect, name, val );
1119 \brief Remove the all specified resource section.
1120 \param sect - Resource section name.
1122 void QtxResourceMgr::remove( const QString& sect )
1126 for ( ResListIterator it( myResources ); it.current(); ++it )
1127 it.current()->removeSection( sect );
1131 \brief Remove the specified resource.
1132 \param sect - Resource section name.
1133 \param name - Name of the resource.
1135 void QtxResourceMgr::remove( const QString& sect, const QString& name )
1139 for ( ResListIterator it( myResources ); it.current(); ++it )
1140 it.current()->removeValue( sect, name );
1144 \brief Returns the current format which operates with resource files.
1146 QString QtxResourceMgr::currentFormat() const
1149 if ( !myFormats.isEmpty() )
1150 fmt = myFormats.getFirst()->format();
1155 \brief Sets the current format which operates with resource files.
1156 \param fmt - Resource format name.
1158 void QtxResourceMgr::setCurrentFormat( const QString& fmt )
1160 Format* form = format( fmt );
1164 myFormats.remove( form );
1165 myFormats.prepend( form );
1167 if ( myResources.isEmpty() )
1170 ResListIterator resIt( myResources );
1171 if ( myResources.count() > myDirList.count() && resIt.current() ) {
1172 resIt.current()->setFile( userFileName( appName() ) );
1176 for ( QStringList::const_iterator it = myDirList.begin(); it != myDirList.end() && resIt.current(); ++it, ++resIt )
1177 resIt.current()->setFile( Qtx::addSlash( *it ) + globalFileName( appName() ) );
1181 \brief Returns the resource format object by it name.
1182 \param fmt - Resource format name.
1184 QtxResourceMgr::Format* QtxResourceMgr::format( const QString& fmt ) const
1187 for ( FormatListIterator it( myFormats ); it.current() && !form; ++it )
1189 if ( it.current()->format() == fmt )
1190 form = it.current();
1197 \brief Add the resource format to the manager. Newly added become current.
1198 \param form - Resource format object.
1200 void QtxResourceMgr::installFormat( QtxResourceMgr::Format* form )
1202 if ( !myFormats.contains( form ) )
1203 myFormats.prepend( form );
1207 \brief Remove the resource format from the manager.
1208 \param form - Resource format object.
1210 void QtxResourceMgr::removeFormat( QtxResourceMgr::Format* form )
1212 myFormats.remove( form );
1216 \brief Returns the string list of the resource format options names.
1218 QStringList QtxResourceMgr::options() const
1220 return myOptions.keys();
1224 \brief Returns the string value for the specified option. If option doesn't exist
1225 then empty string will be returned.
1226 \param opt - Option name.
1228 QString QtxResourceMgr::option( const QString& opt ) const
1231 if ( myOptions.contains( opt ) )
1232 val = myOptions[opt];
1237 \brief Sets the string value for the specified option.
1238 \param opt - Option name.
1239 \param val - Option value.
1241 void QtxResourceMgr::setOption( const QString& opt, const QString& val )
1243 myOptions.insert( opt, val );
1247 \brief Load the all resources from the resource files.
1249 bool QtxResourceMgr::load()
1251 initialize( false );
1253 Format* fmt = format( currentFormat() );
1258 for ( ResListIterator it( myResources ); it.current(); ++it )
1259 res = fmt->load( it.current() ) && res;
1265 \brief Save the changed resources in to the user resource file.
1267 bool QtxResourceMgr::save()
1269 initialize( false );
1271 Format* fmt = format( currentFormat() );
1275 if ( myResources.isEmpty() )
1278 return fmt->save( myResources.getFirst() );
1282 \brief Returns the string list of the existing section names..
1284 QStringList QtxResourceMgr::sections() const
1288 QMap<QString, int> map;
1289 for ( ResListIterator it( myResources ); it.current(); ++it )
1291 QStringList lst = it.current()->sections();
1292 for ( QStringList::const_iterator itr = lst.begin(); itr != lst.end(); ++itr )
1293 map.insert( *itr, 0 );
1297 for ( QMap<QString, int>::ConstIterator iter = map.begin(); iter != map.end(); ++iter )
1298 res.append( iter.key() );
1304 \brief Returns the string list of the existing resource names in the specified section.
1305 \param sec - Resource section name.
1307 QStringList QtxResourceMgr::parameters( const QString& sec ) const
1311 #if defined(QTX_NO_INDEXED_MAP)
1312 typedef QMap<QString, int> PMap;
1314 typedef IMap<QString, int> PMap;
1317 ResListIterator it( myResources );
1319 for ( ; it.current(); --it ) {
1320 QStringList lst = it.current()->parameters( sec );
1321 for ( QStringList::const_iterator itr = lst.begin(); itr != lst.end(); ++itr )
1322 pmap.insert( *itr, 0, false );
1326 for ( PMap::ConstIterator iter = pmap.begin(); iter != pmap.end(); ++iter )
1327 res.append( iter.key() );
1332 QString QtxResourceMgr::path( const QString& sect, const QString& prefix, const QString& name ) const
1335 for ( ResListIterator it( myResources ); it.current() && res.isEmpty(); ++it )
1336 res = it.current()->path( sect, prefix, name );
1340 QString QtxResourceMgr::resSection() const
1342 QString res = option( "res_section_name" );
1343 if ( res.isEmpty() )
1344 res = QString( "resources" );
1348 QString QtxResourceMgr::langSection() const
1350 QString res = option( "lang_section_name" );
1351 if ( res.isEmpty() )
1352 res = QString( "language" );
1356 QPixmap QtxResourceMgr::defaultPixmap() const
1358 return myDefaultPix;
1361 void QtxResourceMgr::setDefaultPixmap( const QPixmap& pix )
1366 QPixmap QtxResourceMgr::loadPixmap( const QString& prefix, const QString& name ) const
1368 return loadPixmap( prefix, name, true );
1371 QPixmap QtxResourceMgr::loadPixmap( const QString& prefix, const QString& name, const bool useDef ) const
1373 return loadPixmap( prefix, name, useDef ? defaultPixmap() : QPixmap() );
1376 QPixmap QtxResourceMgr::loadPixmap( const QString& prefix, const QString& name, const QPixmap& defPix ) const
1381 for ( ResListIterator it( myResources ); it.current() && pix.isNull(); ++it )
1382 pix = it.current()->loadPixmap( resSection(), prefix, name );
1388 void QtxResourceMgr::loadLanguage( const QString& pref, const QString& l )
1392 QMap<QChar, QString> substMap;
1393 substMap.insert( 'A', appName() );
1396 if ( lang.isEmpty() )
1397 value( langSection(), "language", lang );
1399 if ( lang.isEmpty() )
1401 lang = QString( "en" );
1402 qWarning( QString( "Language not specified. Assumed: %1" ).arg( lang ) );
1405 substMap.insert( 'L', lang );
1408 if ( value( langSection(), "translators", trs, false ) && !trs.isEmpty() )
1410 QStringList translators = QStringList::split( "|", option( "translators" ) );
1411 QStringList newTranslators = QStringList::split( "|", trs );
1412 for ( uint i = 0; i < newTranslators.count(); i++ )
1413 if ( translators.find( newTranslators[i] ) == translators.end() )
1414 translators += newTranslators[i];
1415 setOption( "translators", translators.join( "|" ) );
1418 QStringList trList = QStringList::split( "|", option( "translators" ) );
1419 if ( trList.isEmpty() )
1421 trList.append( "%P_msg_%L.qm" );
1422 qWarning( QString( "Translators not defined. Assumed: %1" ).arg( trList.first() ) );
1425 QStringList prefixList;
1426 if ( !pref.isEmpty() )
1427 prefixList.append( pref );
1429 prefixList = parameters( resSection() );
1431 for ( QStringList::const_iterator iter = prefixList.begin(); iter != prefixList.end(); ++iter )
1433 QString prefix = *iter;
1434 substMap.insert( 'P', prefix );
1437 for ( QStringList::const_iterator it = trList.begin(); it != trList.end(); ++it )
1438 trs.append( substMacro( *it, substMap ).stripWhiteSpace() );
1440 loadTranslators( prefix, trs );
1444 void QtxResourceMgr::loadTranslators( const QString& prefix, const QStringList& translators )
1448 QTranslator* trans = 0;
1449 ResListIterator it( myResources );
1451 for ( ; it.current(); --it ) {
1452 for ( QStringList::const_iterator itr = translators.begin(); itr != translators.end(); ++itr ) {
1453 trans = it.current()->loadTranslator( resSection(), prefix, *itr );
1455 if ( !myTranslator[prefix].contains( trans ) )
1456 myTranslator[prefix].append( trans );
1457 qApp->installTranslator( trans );
1463 void QtxResourceMgr::loadTranslator( const QString& prefix, const QString& name )
1467 QTranslator* trans = 0;
1468 ResListIterator it( myResources );
1470 for ( ; it.current(); --it ) {
1471 trans = it.current()->loadTranslator( resSection(), prefix, name );
1473 if ( !myTranslator[prefix].contains( trans ) )
1474 myTranslator[prefix].append( trans );
1475 qApp->installTranslator( trans );
1480 void QtxResourceMgr::removeTranslators( const QString& prefix )
1482 if ( !myTranslator.contains( prefix ) )
1485 for ( TransListIterator it( myTranslator[prefix] ); it.current(); ++it )
1487 qApp->removeTranslator( it.current() );
1488 delete it.current();
1491 myTranslator.remove( prefix );
1494 void QtxResourceMgr::raiseTranslators( const QString& prefix )
1496 if ( !myTranslator.contains( prefix ) )
1499 for ( TransListIterator it( myTranslator[prefix] ); it.current(); ++it )
1501 qApp->removeTranslator( it.current() );
1502 qApp->installTranslator( it.current() );
1506 void QtxResourceMgr::refresh()
1508 QStringList sl = sections();
1509 for ( QStringList::const_iterator it = sl.begin(); it != sl.end(); ++it )
1511 QStringList pl = parameters( *it );
1512 for ( QStringList::const_iterator itr = pl.begin(); itr != pl.end(); ++itr )
1513 setResource( *it, *itr, stringValue( *it, *itr ) );
1517 void QtxResourceMgr::setDirList( const QStringList& dl )
1520 for ( ResListIterator it( myResources ); it.current(); ++it )
1521 delete it.current();
1523 myResources.clear();
1526 void QtxResourceMgr::setResource( const QString& sect, const QString& name, const QString& val )
1530 if ( !myResources.isEmpty() )
1531 myResources.first()->setValue( sect, name, val );
1534 QString QtxResourceMgr::userFileName( const QString& appName ) const
1537 QString pathName = QDir::homeDirPath();
1540 fileName = QString( "%1.%2" ).arg( appName ).arg( currentFormat() );
1542 fileName = QString( ".%1rc" ).arg( appName );
1545 if ( !fileName.isEmpty() )
1546 pathName = Qtx::addSlash( pathName ) + fileName;
1551 QString QtxResourceMgr::globalFileName( const QString& appName ) const
1553 return QString( "%1.%2" ).arg( appName ).arg( currentFormat() );
1556 QString QtxResourceMgr::substMacro( const QString& src, const QMap<QChar, QString>& substMap ) const
1560 QRegExp rx( "%[A-Za-z%]" );
1563 while ( ( idx = rx.search( trg, idx ) ) >= 0 )
1565 QChar spec = trg.at( idx + 1 );
1569 else if ( substMap.contains( spec ) )
1570 subst = substMap[spec];
1572 if ( !subst.isEmpty() )
1574 trg.replace( idx, rx.matchedLength(), subst );
1575 idx += subst.length();
1578 idx += rx.matchedLength();