1 #include "QtxResourceMgr.h"
7 #include <qtranslator.h>
8 #include <qapplication.h>
17 static const char* pixmap_not_found_xpm[] = {
40 Class: QtxResourceMgr::Resources
44 QtxResourceMgr::Resources::Resources( const QString& fileName )
45 : myFileName( fileName )
49 QtxResourceMgr::Resources::~Resources()
53 QString QtxResourceMgr::Resources::file() const
58 void QtxResourceMgr::Resources::setFile( const QString& fn )
63 QString QtxResourceMgr::Resources::value( const QString& sect, const QString& name, const bool subst ) const
67 if ( hasValue( sect, name ) )
69 val = section( sect )[name];
71 val = makeSubstitution( val, sect, name );
76 void QtxResourceMgr::Resources::setValue( const QString& sect, const QString& name, const QString& val )
78 Section& s = section( sect );
79 s.insert( name, val );
82 bool QtxResourceMgr::Resources::hasSection( const QString& sect ) const
84 return mySections.contains( sect );
87 bool QtxResourceMgr::Resources::hasValue( const QString& sect, const QString& name ) const
89 return hasSection( sect ) && section( sect ).contains( name );
92 void QtxResourceMgr::Resources::removeSection( const QString& sect )
94 mySections.remove( sect );
97 void QtxResourceMgr::Resources::removeValue( const QString& sect, const QString& name )
99 if ( !hasSection( sect ) )
102 Section& s = section( sect );
106 mySections.remove( sect );
109 void QtxResourceMgr::Resources::clear()
114 QStringList QtxResourceMgr::Resources::sections() const
116 return mySections.keys();
119 QStringList QtxResourceMgr::Resources::parameters( const QString& sec ) const
121 if ( !hasSection( sec ) )
122 return QStringList();
124 return section( sec ).keys();
127 QString QtxResourceMgr::Resources::path( const QString& sec, const QString& prefix, const QString& name ) const
129 QString filePath = fileName( sec, prefix, name );
130 if ( !filePath.isEmpty() )
132 if ( !QFileInfo( filePath ).exists() )
133 filePath = QString::null;
138 QtxResourceMgr::Section& QtxResourceMgr::Resources::section( const QString& sn )
140 if ( !mySections.contains( sn ) )
141 mySections.insert( sn, Section() );
143 return mySections[sn];
146 const QtxResourceMgr::Section& QtxResourceMgr::Resources::section( const QString& sn ) const
148 return mySections[sn];
151 QString QtxResourceMgr::Resources::fileName( const QString& sect, const QString& prefix, const QString& name ) const
154 if ( hasValue( sect, prefix ) )
156 path = value( sect, prefix, true );
157 if ( !path.isEmpty() )
159 if ( QFileInfo( path ).isRelative() )
160 path = Qtx::addSlash( QFileInfo( myFileName ).dirPath( true ) ) + path;
162 path = Qtx::addSlash( path ) + name;
165 return QDir::convertSeparators( path );
168 QPixmap QtxResourceMgr::Resources::loadPixmap( const QString& sect, const QString& prefix, const QString& name ) const
170 return QPixmap( fileName( sect, prefix, name ) );
173 QTranslator* QtxResourceMgr::Resources::loadTranslator( const QString& sect, const QString& prefix, const QString& name ) const
175 QTranslator* trans = new QTranslator( 0 );
176 if ( !trans->load( fileName( sect, prefix, name ) ) )
184 QString QtxResourceMgr::Resources::environmentVariable( const QString& str, int& start, int& len ) const
186 QString varName = QString::null;
189 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_]*)\\%" );
191 int pos = rx.search( str, start );
195 len = rx.matchedLength();
196 QStringList caps = rx.capturedTexts();
197 for ( uint i = 1; i <= caps.count() && varName.isEmpty(); i++ )
198 varName = *caps.at( i );
203 QString QtxResourceMgr::Resources::makeSubstitution( const QString& str, const QString& sect, const QString& name ) const
207 QMap<QString, int> ignoreMap;
208 ignoreMap.insert( name, 0 );
210 int start( 0 ), len( 0 );
213 QString envName = environmentVariable( res, start, len );
214 if ( envName.isNull() )
217 QString newStr = QString::null;
218 if ( ::getenv( envName ) )
219 newStr = QString( ::getenv( envName ) );
221 if ( newStr.isNull() )
223 if ( ignoreMap.contains( envName ) )
229 if ( hasValue( sect, envName ) )
230 newStr = value( sect, envName, false );
231 ignoreMap.insert( envName, 0 );
233 res.replace( start, len, newStr );
240 Class: QtxResourceMgr::IniFormat
244 class QtxResourceMgr::IniFormat : public Format
251 virtual bool load( const QString&, QMap<QString, Section>& );
252 virtual bool save( const QString&, const QMap<QString, Section>& );
255 QtxResourceMgr::IniFormat::IniFormat()
260 QtxResourceMgr::IniFormat::~IniFormat()
264 bool QtxResourceMgr::IniFormat::load( const QString& fname, QMap<QString, Section>& secMap )
267 if ( !file.open( IO_ReadOnly ) )
270 QTextStream ts( &file );
277 QString separator = option( "separator" );
278 if ( separator.isNull() )
279 separator = QString( "=" );
281 QString comment = option( "comment" );
282 if ( comment.isNull() )
283 comment = QString( "#" );
287 data = ts.readLine();
293 data = data.stripWhiteSpace();
294 if ( data.isEmpty() )
297 if ( data.startsWith( comment ) )
300 QRegExp rx( "^\\[([\\w\\s]*)\\]$" );
301 if ( rx.search( data ) != -1 )
303 section = rx.cap( 1 );
304 if ( section.isEmpty() )
307 qWarning( QString( "Empty section in line %1" ).arg( line ) );
310 else if ( data.contains( "=" ) && !section.isEmpty() )
312 int pos = data.find( separator );
313 QString key = data.left( pos - 1 ).stripWhiteSpace();
314 QString val = data.mid( pos + 1 ).stripWhiteSpace();
315 secMap[section].insert( key, val );
320 section.isEmpty() ? qWarning( "Current section is empty" ) :
321 qWarning( QString( "Error in line: %1" ).arg( line ) );
330 bool QtxResourceMgr::IniFormat::save( const QString& fname, const QMap<QString, Section>& secMap )
333 if ( !file.open( IO_WriteOnly ) )
337 for ( QMap<QString, Section>::ConstIterator it = secMap.begin(); it != secMap.end() && res; ++it )
339 QString data = QString( "[%1]\n" ).arg( it.key() );
340 for ( Section::ConstIterator iter = it.data().begin(); iter != it.data().end(); ++iter )
341 data += iter.key() + " = " + iter.data() + "\n";
344 res = file.writeBlock( data.latin1(), data.length() ) == (int)data.length();
353 Class: QtxResourceMgr::XmlFormat
357 class QtxResourceMgr::XmlFormat : public Format
364 virtual bool load( const QString&, QMap<QString, Section>& );
365 virtual bool save( const QString&, const QMap<QString, Section>& );
368 QString docTag() const;
369 QString sectionTag() const;
370 QString parameterTag() const;
371 QString nameAttribute() const;
372 QString valueAttribute() const;
375 QtxResourceMgr::XmlFormat::XmlFormat()
380 QtxResourceMgr::XmlFormat::~XmlFormat()
384 bool QtxResourceMgr::XmlFormat::load( const QString& fname, QMap<QString, Section>& secMap )
391 if ( !file.open( IO_ReadOnly ) )
396 res = doc.setContent( &file );
402 QDomElement root = doc.documentElement();
403 if ( root.isNull() || root.tagName() != docTag() )
406 QDomNode sectNode = root.firstChild();
407 while ( res && !sectNode.isNull() )
409 res = sectNode.isElement();
412 QDomElement sectElem = sectNode.toElement();
413 if ( sectElem.tagName() == sectionTag() && sectElem.hasAttribute( nameAttribute() ) )
415 QString section = sectElem.attribute( nameAttribute() );
416 QDomNode paramNode = sectNode.firstChild();
417 while ( res && !paramNode.isNull() )
419 res = paramNode.isElement();
422 QDomElement paramElem = paramNode.toElement();
423 if ( paramElem.tagName() == parameterTag() &&
424 paramElem.hasAttribute( nameAttribute() ) && paramElem.hasAttribute( valueAttribute() ) )
426 QString paramName = paramElem.attribute( nameAttribute() );
427 QString paramValue = paramElem.attribute( valueAttribute() );
429 secMap[section].insert( paramName, paramValue );
435 res = paramNode.isComment();
437 paramNode = paramNode.nextSibling();
444 res = sectNode.isComment(); // if it's a comment -- let it be, pass it..
446 sectNode = sectNode.nextSibling();
454 bool QtxResourceMgr::XmlFormat::save( const QString& fname, const QMap<QString, Section>& secMap )
461 if ( !file.open( IO_WriteOnly ) )
464 QDomDocument doc( docTag() );
465 QDomElement root = doc.createElement( docTag() );
466 doc.appendChild( root );
468 for ( QMap<QString, Section>::ConstIterator it = secMap.begin(); it != secMap.end(); ++it )
470 QDomElement sect = doc.createElement( sectionTag() );
471 sect.setAttribute( nameAttribute(), it.key() );
472 root.appendChild( sect );
473 for ( QMap<QString, QString>::ConstIterator iter = it.data().begin(); iter != it.data().end(); ++iter )
475 QDomElement val = doc.createElement( parameterTag() );
476 val.setAttribute( nameAttribute(), iter.key() );
477 val.setAttribute( valueAttribute(), iter.data() );
478 sect.appendChild( val );
482 QString docStr = doc.toString();
483 res = file.writeBlock( docStr.latin1(), docStr.length() ) == (int)docStr.length();
491 QString QtxResourceMgr::XmlFormat::docTag() const
493 QString tag = option( "doc_tag" );
495 tag = QString( "document" );
499 QString QtxResourceMgr::XmlFormat::sectionTag() const
501 QString tag = option( "section_tag" );
503 tag = QString( "section" );
507 QString QtxResourceMgr::XmlFormat::parameterTag() const
509 QString tag = option( "parameter_tag" );
511 tag = QString( "parameter" );
515 QString QtxResourceMgr::XmlFormat::nameAttribute() const
517 QString str = option( "name_attribute" );
519 str = QString( "name" );
523 QString QtxResourceMgr::XmlFormat::valueAttribute() const
525 QString str = option( "value_attribute" );
527 str = QString( "value" );
532 Class: QtxResourceMgr::Format
536 QtxResourceMgr::Format::Format( const QString& fmt )
541 QtxResourceMgr::Format::~Format()
545 QString QtxResourceMgr::Format::format() const
550 QStringList QtxResourceMgr::Format::options() const
555 QString QtxResourceMgr::Format::option( const QString& opt ) const
558 if ( myOpt.contains( opt ) )
562 void QtxResourceMgr::Format::setOption( const QString& opt, const QString& val )
564 myOpt.insert( opt, val );
567 bool QtxResourceMgr::Format::load( Resources* res )
572 QMap<QString, Section> sections;
573 bool status = load( res->myFileName, sections );
575 res->mySections = sections;
577 qDebug( "QtxResourceMgr: Could not load resource file \"%s\"", res->myFileName.latin1() );
582 bool QtxResourceMgr::Format::save( Resources* res )
587 Qtx::mkDir( Qtx::dir( res->myFileName ) );
589 return save( res->myFileName, res->mySections );
593 Class: QtxResourceMgr
597 QtxResourceMgr::QtxResourceMgr( const QString& appName, const QString& resVarTemplate )
598 : myAppName( appName )
600 QString envVar = !resVarTemplate.isEmpty() ? resVarTemplate : QString( "%1Resources" );
601 if ( envVar.contains( "%1" ) )
602 envVar = envVar.arg( appName );
605 if ( ::getenv( envVar ) )
606 dirs = ::getenv( envVar );
608 setDirList( QStringList::split( ";", dirs ) );
610 installFormat( new XmlFormat() );
611 installFormat( new IniFormat() );
613 setOption( "translators", QString( "%P_msg_%L.qm|%P_images.qm" ) );
616 QtxResourceMgr::~QtxResourceMgr()
618 QStringList prefList = myTranslator.keys();
619 for ( QStringList::const_iterator it = prefList.begin(); it != prefList.end(); ++it )
620 removeTranslators( *it );
623 QString QtxResourceMgr::appName() const
628 QStringList QtxResourceMgr::dirList() const
633 void QtxResourceMgr::setDirList( const QStringList& dl )
636 for ( ResListIterator it( myResources ); it.current(); ++it )
642 void QtxResourceMgr::initialize( const bool autoLoad ) const
644 if ( !myResources.isEmpty() )
647 QtxResourceMgr* that = (QtxResourceMgr*)this;
649 that->myResources.append( new Resources( userFileName( appName() ) ) );
650 for ( QStringList::const_iterator it = myDirList.begin(); it != myDirList.end(); ++it )
652 QString path = Qtx::addSlash( *it ) + globalFileName( appName() );
653 that->myResources.append( new Resources( path ) );
660 void QtxResourceMgr::clear()
662 for ( ResListIterator it( myResources ); it.current(); ++it )
663 it.current()->clear();
666 QString QtxResourceMgr::currentSection() const
671 void QtxResourceMgr::setCurrentSection( const QString& str )
676 bool QtxResourceMgr::value( const QString& name, int& val ) const
678 return value( currentSection(), name, val );
681 bool QtxResourceMgr::value( const QString& name, double& val ) const
683 return value( currentSection(), name, val );
686 bool QtxResourceMgr::value( const QString& name, bool& val ) const
688 return value( currentSection(), name, val );
691 bool QtxResourceMgr::value( const QString& name, QColor& val ) const
693 return value( currentSection(), name, val );
696 bool QtxResourceMgr::value( const QString& name, QString& val, const bool subst ) const
698 return value( currentSection(), name, val, subst );
701 bool QtxResourceMgr::value( const QString& sect, const QString& name, int& iVal ) const
704 if ( !value( sect, name, val, true ) )
708 iVal = val.toInt( &ok );
713 bool QtxResourceMgr::value( const QString& sect, const QString& name, double& dVal ) const
716 if ( !value( sect, name, val, true ) )
720 dVal = val.toDouble( &ok );
725 bool QtxResourceMgr::value( const QString& sect, const QString& name, bool& bVal ) const
728 if ( !value( sect, name, val, true ) )
731 static QMap<QString, bool> boolMap;
732 if ( boolMap.isEmpty() )
734 boolMap["true"] = boolMap["yes"] = boolMap["on"] = true;
735 boolMap["false"] = boolMap["no"] = boolMap["off"] = false;
739 bool res = boolMap.contains( val );
744 double num = val.toDouble( &res );
752 bool QtxResourceMgr::value( const QString& sect, const QString& name, QColor& cVal ) const
755 if ( !value( sect, name, val, true ) )
759 QStringList vals = QStringList::split( ",", val, true );
762 for ( QStringList::const_iterator it = vals.begin(); it != vals.end() && res; ++it )
763 nums.append( (*it).toInt( &res ) );
765 if ( res && nums.count() >= 3 )
766 cVal.setRgb( nums[0], nums[1], nums[2] );
769 int pack = val.toInt( &res );
771 Qtx::rgbSet( pack, cVal );
777 bool QtxResourceMgr::value( const QString& sect, const QString& name, QString& val, const bool subst ) const
782 for ( ResListIterator it( myResources ); it.current() && !ok; ++it )
784 ok = it.current()->hasValue( sect, name );
786 val = it.current()->value( sect, name, subst );
792 int QtxResourceMgr::integerValue( const QString& name, const int def ) const
794 return integerValue( currentSection(), name, def );
797 double QtxResourceMgr::doubleValue( const QString& name, const double def ) const
799 return doubleValue( currentSection(), name, def );
802 bool QtxResourceMgr::booleanValue( const QString& name, const bool def ) const
804 return booleanValue( currentSection(), name, def );
807 QColor QtxResourceMgr::colorValue( const QString& name, const QColor& def ) const
809 return colorValue( currentSection(), name, def );
812 QString QtxResourceMgr::stringValue( const QString& name, const char* def ) const
814 return stringValue( currentSection(), name, def );
817 int QtxResourceMgr::integerValue( const QString& sect, const QString& name, const int def ) const
820 if ( !value( sect, name, val ) )
825 double QtxResourceMgr::doubleValue( const QString& sect, const QString& name, const double def ) const
828 if ( !value( sect, name, val ) )
833 bool QtxResourceMgr::booleanValue( const QString& sect, const QString& name, const bool def ) const
836 if ( !value( sect, name, val ) )
841 QColor QtxResourceMgr::colorValue( const QString& sect, const QString& name, const QColor& def ) const
844 if ( !value( sect, name, val ) )
849 QString QtxResourceMgr::stringValue( const QString& sect, const QString& name, const char* def ) const
852 if ( !value( sect, name, val ) )
857 bool QtxResourceMgr::hasValue( const QString& name ) const
859 return hasValue( currentSection(), name );
862 bool QtxResourceMgr::hasValue( const QString& sect, const QString& name ) const
867 for ( ResListIterator it( myResources ); it.current() && !ok; ++it )
868 ok = it.current()->hasValue( sect, name );
873 bool QtxResourceMgr::hasSection( const QString& sect ) const
878 for ( ResListIterator it( myResources ); it.current() && !ok; ++it )
879 ok = it.current()->hasSection( sect );
884 void QtxResourceMgr::setValue( const QString& name, int val )
886 setValue( currentSection(), name, val );
889 void QtxResourceMgr::setValue( const QString& name, double val )
891 setValue( currentSection(), name, val );
894 void QtxResourceMgr::setValue( const QString& name, bool val )
896 setValue( currentSection(), name, val );
899 void QtxResourceMgr::setValue( const QString& name, const QColor& val )
901 setValue( currentSection(), name, val );
904 void QtxResourceMgr::setValue( const QString& name, const QString& val )
906 setValue( currentSection(), name, val );
909 void QtxResourceMgr::setValue( const QString& sect, const QString& name, int val )
911 setValue( sect, name, QString::number( val ) );
914 void QtxResourceMgr::setValue( const QString& sect, const QString& name, double val )
916 setValue( sect, name, QString::number( val, 'g', 12 ) );
919 void QtxResourceMgr::setValue( const QString& sect, const QString& name, bool val )
921 setValue( sect, name, QString( val ? "true" : "false" ) );
924 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QColor& val )
926 setValue( sect, name, QString( "%1, %2, %3").arg( val.red() ).arg( val.green() ).arg( val.blue() ) );
929 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QString& val )
933 if ( !myResources.isEmpty() )
934 myResources.first()->setValue( sect, name, val );
937 void QtxResourceMgr::remove( const QString& name )
939 remove( currentSection(), name );
942 void QtxResourceMgr::remove( const QString& sect, const QString& name )
946 for ( ResListIterator it( myResources ); it.current(); ++it )
947 it.current()->removeValue( sect, name );
950 void QtxResourceMgr::removeSection( const QString& sect )
954 for ( ResListIterator it( myResources ); it.current(); ++it )
955 it.current()->removeSection( sect );
958 QString QtxResourceMgr::currentFormat() const
961 if ( !myFormats.isEmpty() )
962 fmt = myFormats.getFirst()->format();
966 void QtxResourceMgr::setCurrentFormat( const QString& fmt )
968 Format* form = format( fmt );
972 myFormats.remove( form );
973 myFormats.prepend( form );
975 if ( myResources.isEmpty() )
978 ResListIterator resIt( myResources );
979 if ( resIt.current() )
980 resIt.current()->setFile( userFileName( appName() ) );
983 for ( QStringList::const_iterator it = myDirList.begin(); it != myDirList.end() && resIt.current(); ++it, ++resIt )
984 resIt.current()->setFile( Qtx::addSlash( *it ) + globalFileName( appName() ) );
987 QtxResourceMgr::Format* QtxResourceMgr::format( const QString& fmt ) const
990 for ( FormatListIterator it( myFormats ); it.current() && !form; ++it )
992 if ( it.current()->format() == fmt )
999 void QtxResourceMgr::installFormat( QtxResourceMgr::Format* form )
1001 if ( !myFormats.contains( form ) )
1002 myFormats.prepend( form );
1005 void QtxResourceMgr::removeFormat( QtxResourceMgr::Format* form )
1007 myFormats.remove( form );
1010 QStringList QtxResourceMgr::options() const
1012 return myOptions.keys();
1015 QString QtxResourceMgr::option( const QString& opt ) const
1018 if ( myOptions.contains( opt ) )
1019 val = myOptions[opt];
1023 void QtxResourceMgr::setOption( const QString& opt, const QString& val )
1025 myOptions.insert( opt, val );
1028 bool QtxResourceMgr::load()
1030 initialize( false );
1032 Format* fmt = format( currentFormat() );
1037 for ( ResListIterator it( myResources ); it.current(); ++it )
1038 res = fmt->load( it.current() ) && res;
1043 bool QtxResourceMgr::save()
1045 initialize( false );
1047 Format* fmt = format( currentFormat() );
1051 if ( myResources.isEmpty() )
1054 return fmt->save( myResources.getFirst() );
1057 QStringList QtxResourceMgr::sections() const
1061 QMap<QString, int> map;
1062 for ( ResListIterator it( myResources ); it.current(); ++it )
1064 QStringList lst = it.current()->sections();
1065 for ( QStringList::const_iterator itr = lst.begin(); itr != lst.end(); ++itr )
1066 map.insert( *itr, 0 );
1070 for ( QMap<QString, int>::ConstIterator iter = map.begin(); iter != map.end(); ++iter )
1071 res.append( iter.key() );
1076 QStringList QtxResourceMgr::parameters( const QString& sec ) const
1080 QMap<QString, int> map;
1081 for ( ResListIterator it( myResources ); it.current(); ++it )
1083 QStringList lst = it.current()->parameters( sec );
1084 for ( QStringList::const_iterator itr = lst.begin(); itr != lst.end(); ++itr )
1085 map.insert( *itr, 0 );
1089 for ( QMap<QString, int>::ConstIterator iter = map.begin(); iter != map.end(); ++iter )
1090 res.append( iter.key() );
1095 QString QtxResourceMgr::path( const QString& sect, const QString& prefix, const QString& name ) const
1098 for ( ResListIterator it( myResources ); it.current() && res.isEmpty(); ++it )
1099 res = it.current()->path( sect, prefix, name );
1103 QString QtxResourceMgr::resSection() const
1105 QString res = option( "res_section_name" );
1106 if ( res.isEmpty() )
1107 res = QString( "resources" );
1111 QString QtxResourceMgr::langSection() const
1113 QString res = option( "lang_section_name" );
1114 if ( res.isEmpty() )
1115 res = QString( "language" );
1119 QPixmap QtxResourceMgr::loadPixmap( const QString& prefix, const QString& name, const bool useDefault ) const
1123 static QPixmap defaultPixmap( pixmap_not_found_xpm );
1125 for ( ResListIterator it( myResources ); it.current() && pix.isNull(); ++it )
1126 pix = it.current()->loadPixmap( resSection(), prefix, name );
1127 if ( pix.isNull() && useDefault )
1128 return defaultPixmap;
1132 void QtxResourceMgr::loadLanguage( const QString& pref, const QString& l )
1136 QMap<QChar, QString> substMap;
1137 substMap.insert( 'A', appName() );
1140 if ( lang.isEmpty() )
1141 value( langSection(), "language", lang );
1143 if ( lang.isEmpty() )
1145 lang = QString( "en" );
1146 qWarning( QString( "Language not specified. Assumed: %1" ).arg( lang ) );
1149 substMap.insert( 'L', lang );
1152 if ( value( langSection(), "translators", trs, false ) && !trs.isEmpty() )
1154 QStringList translators = QStringList::split( "|", option( "translators" ) );
1155 QStringList newTranslators = QStringList::split( "|", trs );
1156 for ( uint i = 0; i < newTranslators.count(); i++ )
1157 if ( translators.find( newTranslators[i] ) == translators.end() )
1158 translators += newTranslators[i];
1159 setOption( "translators", translators.join( "|" ) );
1162 QStringList trList = QStringList::split( "|", option( "translators" ) );
1163 if ( trList.isEmpty() )
1165 trList.append( "%P_msg_%L.qm" );
1166 qWarning( QString( "Translators not defined. Assumed: %1" ).arg( trList.first() ) );
1169 QStringList prefixList;
1170 if ( !pref.isEmpty() )
1171 prefixList.append( pref );
1173 prefixList = parameters( resSection() );
1175 for ( QStringList::const_iterator iter = prefixList.begin(); iter != prefixList.end(); ++iter )
1177 QString prefix = *iter;
1178 substMap.insert( 'P', prefix );
1181 for ( QStringList::const_iterator it = trList.begin(); it != trList.end(); ++it )
1182 trs.append( substMacro( *it, substMap ).stripWhiteSpace() );
1184 for ( QStringList::const_iterator itr = trs.begin(); itr != trs.end(); ++itr )
1185 loadTranslator( prefix, *itr );
1189 void QtxResourceMgr::loadTranslator( const QString& prefix, const QString& name )
1193 QTranslator* trans = 0;
1194 for ( ResListIterator it( myResources ); it.current() && !trans; ++it )
1195 trans = it.current()->loadTranslator( resSection(), prefix, name );
1200 if ( !myTranslator[prefix].contains( trans ) )
1201 myTranslator[prefix].append( trans );
1202 qApp->installTranslator( trans );
1205 void QtxResourceMgr::removeTranslators( const QString& prefix )
1207 if ( !myTranslator.contains( prefix ) )
1210 for ( TransListIterator it( myTranslator[prefix] ); it.current(); ++it )
1212 qApp->removeTranslator( it.current() );
1213 delete it.current();
1216 myTranslator.remove( prefix );
1219 void QtxResourceMgr::raiseTranslators( const QString& prefix )
1221 if ( !myTranslator.contains( prefix ) )
1224 for ( TransListIterator it( myTranslator[prefix] ); it.current(); ++it )
1226 qApp->removeTranslator( it.current() );
1227 qApp->installTranslator( it.current() );
1231 void QtxResourceMgr::refresh()
1233 QStringList sl = sections();
1234 for ( QStringList::const_iterator it = sl.begin(); it != sl.end(); ++it )
1236 QStringList pl = parameters( *it );
1237 for ( QStringList::const_iterator itr = pl.begin(); itr != pl.end(); ++itr )
1238 setValue( *it, *itr, stringValue( *it, *itr ) );
1242 QString QtxResourceMgr::userFileName( const QString& appName ) const
1245 QString pathName = QDir::homeDirPath();
1248 fileName = QString( "%1.%2" ).arg( appName ).arg( currentFormat() );
1250 fileName = QString( ".%1rc" ).arg( appName );
1253 if ( !fileName.isEmpty() )
1254 pathName = Qtx::addSlash( pathName ) + fileName;
1259 QString QtxResourceMgr::globalFileName( const QString& appName ) const
1261 return QString( "%1.%2" ).arg( appName ).arg( currentFormat() );
1264 QString QtxResourceMgr::substMacro( const QString& src, const QMap<QChar, QString>& substMap ) const
1268 QRegExp rx( "%[A-Za-z%]" );
1271 while ( ( idx = rx.search( trg, idx ) ) >= 0 )
1273 QChar spec = trg.at( idx + 1 );
1277 else if ( substMap.contains( spec ) )
1278 subst = substMap[spec];
1280 if ( !subst.isEmpty() )
1282 trg.replace( idx, rx.matchedLength(), subst );
1283 idx += subst.length();
1286 idx += rx.matchedLength();