add mp3 and ogg torrent url info to JamendoAlbum
[amarok.git] / src / qstringx.h
blob57aeb54855c9e61e17cdbace4d7c9e55e87079ad
1 // Copyright (C) 2004 Shintaro Matsuoka <shin@shoegazed.org>
2 // Copyright (C) 2006 Martin Aumueller <aumuell@reserv.at>
3 // See COPYING file for licensing information
5 #ifndef AMAROK_QSTRINGX_H
6 #define AMAROK_QSTRINGX_H
8 #include <qglobal.h>
9 #include <QRegExp>
10 #include <QString>
11 #include <QStringList>
12 #include <qmap.h>
14 namespace Amarok
17 class QStringx : public QString
19 public:
20 QStringx() {}
21 QStringx( QChar ch ) : QString( ch ) {}
22 QStringx( const QString& s ) : QString( s ) {}
23 QStringx( const QByteArray& ba ) : QString( ba ) {}
24 QStringx( const QChar* unicode, uint length ) : QString( unicode, length ) {}
25 QStringx( const char* str ) : QString( str ) {}
26 virtual ~QStringx() {}
28 // the numbers following % obviously are not taken into account
29 QString args( const QStringList& args ) const
31 const QStringList text = (*this).split( QRegExp( "%\\d+" ), QString::KeepEmptyParts );
33 QList<QString>::ConstIterator itrText = text.begin();
34 QList<QString>::ConstIterator itrArgs = args.begin();
35 QString merged = (*itrText);
36 ++itrText;
37 while ( itrText != text.end() && itrArgs != args.end() )
39 merged += (*itrArgs) + (*itrText);
40 ++itrText;
41 ++itrArgs;
44 Q_ASSERT( itrText == text.end() && itrArgs == args.end() );
46 return merged;
49 // %something gets replaced by the value corresponding to key "something" in args
50 QString namedArgs( const QMap<QString, QString> args, bool opt=false ) const
52 QRegExp rxArg( "%[a-zA-Z0-9]+" );
54 QString result;
55 int start = 0;
56 for( int pos = rxArg.indexIn( *this );
57 pos != -1;
58 pos = rxArg.indexIn( *this, start ) )
60 int len = rxArg.matchedLength();
61 QString p = rxArg.capturedTexts()[0].mid(1, len-1);
63 result += mid( start, pos-start );
64 if( !args[p].isEmpty() )
65 result += args[p];
66 else if( opt )
67 return QString();
69 start = pos + len;
71 result += mid( start );
73 return result;
76 // %something gets replaced by the value corresponding to key "something" in args,
77 // however, if key "something" is not available,
78 // then replace everything within surrounding { } by an empty string
79 QString namedOptArgs( const QMap<QString, QString> args ) const
81 QRegExp rxOptArg( "\\{.*%[a-zA-Z0-9_]+.*\\}" );
82 rxOptArg.setMinimal( true );
84 QString result;
85 int start = 0;
86 for( int pos = rxOptArg.indexIn( *this );
87 pos != -1;
88 pos = rxOptArg.indexIn( *this, start ) )
90 int len = rxOptArg.matchedLength();
91 QStringx opt = rxOptArg.capturedTexts()[0].mid(1, len-2);
93 result += QStringx(mid( start, pos-start )).namedArgs( args );
94 result += opt.namedArgs( args, true );
96 start = pos + len;
98 result += QStringx( mid( start ) ).namedArgs( args );
100 return result;
104 } // namespace Amarok
106 #endif // AMAROK_QSTRINGX_H