Qmmp
/usr/src/RPM/BUILD/qmmp-0.11-svn/src/qmmpui/metadataformatter.h
00001 /***************************************************************************
00002  *   Copyright (C) 2015-2017 by Ilya Kotov                                 *
00003  *   forkotov02@hotmail.ru                                                 *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
00019  ***************************************************************************/
00020 
00021 #ifndef METADATAFORMATTER_H
00022 #define METADATAFORMATTER_H
00023 
00024 #include <QString>
00025 #include <QMap>
00026 #include <QList>
00027 #include <qmmpui/playlisttrack.h>
00028 #include <qmmp/qmmp.h>
00029 #include <qmmp/fileinfo.h>
00030 
00034 class MetaDataFormatter
00035 {
00036 public:
00059     MetaDataFormatter(const QString &pattern = QString());
00064     void setPattern(const QString &pattern);
00068     const QString pattern() const;
00072     QString format(const PlayListTrack *item) const;
00079     QString format(const QMap<Qmmp::MetaData, QString> &metaData, qint64 length = 0, int track = 0) const;
00085     QString format(const FileInfo *info, int track = 0) const;
00093     static QString formatLength(qint64 length, bool hideZero = true);
00094 
00095 private:
00096     struct Node;
00097     struct Param;
00098 
00099     struct Node
00100     {
00101         enum {
00102             PRINT_TEXT = 0,
00103             IF_KEYWORD,
00104             OR_OPERATOR,
00105             AND_OPERATOR,
00106             DIR_FUNCTION
00107         } command;
00108 
00109         QList<Param> params;
00110     };
00111 
00112     struct Param
00113     {
00114         enum {
00115             FIELD = 0,
00116             TEXT,
00117             NUMERIC,
00118             NODES
00119         } type;
00120 
00121         //extra fields
00122         enum
00123         {
00124             TWO_DIGIT_TRACK = Qmmp::URL + 1,
00125             DURATION,
00126             FILE_NAME,
00127             TRACK_INDEX
00128         };
00129 
00130         int field;
00131         QString text;
00132         int number;
00133         QList<Node> children;
00134     };
00135 
00136     bool parseField(QList<Node> *nodes, QString::const_iterator *i, QString::const_iterator end);
00137     bool parseIf(QList<Node> *nodes, QString::const_iterator *i, QString::const_iterator end);
00138     bool parseDir(QList<Node> *nodes, QString::const_iterator *i, QString::const_iterator end);
00139     void parseText(QList<Node> *nodes, QString::const_iterator *i, QString::const_iterator end);
00140     void parseEscape(QList<Node> *nodes, QString::const_iterator *i, QString::const_iterator end);
00141 
00142     QString evalute(const QList<Node> *nodes, const QMap<Qmmp::MetaData, QString> *metaData, qint64 length, int track) const;
00143     QString printParam(Param *p, const QMap<Qmmp::MetaData, QString> *metaData, qint64 length, int track) const;
00144     QString printField(int field, const QMap<Qmmp::MetaData, QString> *metaData, qint64 length, int track) const;
00145 
00146     QString dumpNode(Node node) const;
00147 
00148     QList<MetaDataFormatter::Node> compile(const QString &expr);
00149     QString m_pattern;
00150     QList<MetaDataFormatter::Node> m_nodes;
00151     QMap<QString, int> m_fieldNames;
00152 };
00153 
00154 #endif // METADATAFORMATTER2_H
 All Classes Functions Variables Enumerations Enumerator