Qmmp
metadataformatter.h
1 /***************************************************************************
2  * Copyright (C) 2015-2017 by Ilya Kotov *
3  * forkotov02@ya.ru *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19  ***************************************************************************/
20 
21 #ifndef METADATAFORMATTER_H
22 #define METADATAFORMATTER_H
23 
24 #include <QString>
25 #include <QMap>
26 #include <QList>
27 #include <qmmpui/playlisttrack.h>
28 #include <qmmp/qmmp.h>
29 #include <qmmp/fileinfo.h>
30 
35 {
36 public:
59  MetaDataFormatter(const QString &pattern = QString());
64  void setPattern(const QString &pattern);
68  const QString pattern() const;
72  QString format(const PlayListTrack *item) const;
79  QString format(const QMap<Qmmp::MetaData, QString> &metaData, qint64 length = 0, int track = 0) const;
85  QString format(const FileInfo *info, int track = 0) const;
93  static QString formatLength(qint64 length, bool hideZero = true);
94 
95 private:
96  struct Node;
97  struct Param;
98 
99  struct Node
100  {
101  enum {
102  PRINT_TEXT = 0,
103  IF_KEYWORD,
104  OR_OPERATOR,
105  AND_OPERATOR,
106  DIR_FUNCTION
107  } command;
108 
109  QList<Param> params;
110  };
111 
112  struct Param
113  {
114  enum {
115  FIELD = 0,
116  TEXT,
117  NUMERIC,
118  NODES
119  } type;
120 
121  //extra fields
122  enum
123  {
124  TWO_DIGIT_TRACK = Qmmp::URL + 1,
125  DURATION,
126  FILE_NAME,
127  TRACK_INDEX
128  };
129 
130  int field;
131  QString text;
132  int number;
133  QList<Node> children;
134  };
135 
136  bool parseField(QList<Node> *nodes, QString::const_iterator *i, QString::const_iterator end);
137  bool parseIf(QList<Node> *nodes, QString::const_iterator *i, QString::const_iterator end);
138  bool parseDir(QList<Node> *nodes, QString::const_iterator *i, QString::const_iterator end);
139  void parseText(QList<Node> *nodes, QString::const_iterator *i, QString::const_iterator end);
140  void parseEscape(QList<Node> *nodes, QString::const_iterator *i, QString::const_iterator end);
141 
142  QString evalute(const QList<Node> *nodes, const QMap<Qmmp::MetaData, QString> *metaData, qint64 length, int track) const;
143  QString printParam(Param *p, const QMap<Qmmp::MetaData, QString> *metaData, qint64 length, int track) const;
144  QString printField(int field, const QMap<Qmmp::MetaData, QString> *metaData, qint64 length, int track) const;
145 
146  QString dumpNode(Node node) const;
147 
148  QList<MetaDataFormatter::Node> compile(const QString &expr);
149  QString m_pattern;
150  QList<MetaDataFormatter::Node> m_nodes;
151  QMap<QString, int> m_fieldNames;
152 };
153 
154 #endif // METADATAFORMATTER2_H
The FileInfo class stores metadata and audio information about media file or stream.
Definition: fileinfo.h:32
MetaDataFormatter(const QString &pattern=QString())
QString format(const PlayListTrack *item) const
const QString pattern() const
The MetaDataFormatter formats metadata using templates.
Definition: metadataformatter.h:34
static QString formatLength(qint64 length, bool hideZero=true)
void setPattern(const QString &pattern)
Definition: qmmp.h:76
The PlayListTrack class provides a track for use with the PlayListModel class.
Definition: playlisttrack.h:36