Qmmp
metadataformatter.h
1 /***************************************************************************
2  * Copyright (C) 2015-2018 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 "qmmpui_export.h"
30 
34 class QMMPUI_EXPORT MetaDataFormatter
35 {
36 public:
66  MetaDataFormatter(const QString &pattern = QString());
71  void setPattern(const QString &pattern);
75  const QString pattern() const;
79  QString format(const PlayListTrack *item) const;
85  QString format(const TrackInfo &info, int trackIndex = 0) const;
86 
87  QString format(const TrackInfo *info, int trackIndex = 0) const;
95  static QString formatDuration(qint64 duration, bool hideZero = true, bool showMs = false);
96 
97 private:
98  struct Node;
99  struct Param;
100 
101  struct Node
102  {
103  enum {
104  PRINT_TEXT = 0,
105  IF_KEYWORD,
106  OR_OPERATOR,
107  AND_OPERATOR,
108  DIR_FUNCTION
109  } command;
110 
111  QList<Param> params;
112  };
113 
114  struct Param
115  {
116  enum {
117  FIELD = 0,
118  PROPERTY,
119  TEXT,
120  NUMERIC,
121  NODES
122  } type;
123 
124  //extra fields
125  enum
126  {
127  PATH = Qmmp::DISCNUMBER + 1,
128  TWO_DIGIT_TRACK,
129  DURATION,
130  FILE_NAME,
131  TRACK_INDEX
132  };
133 
134  int field;
135  QString text;
136  int number;
137  QList<Node> children;
138  };
139 
140  bool parseField(QList<Node> *nodes, QString::const_iterator *i, QString::const_iterator end);
141  bool parseProperty(QList<Node> *nodes, QString::const_iterator *i, QString::const_iterator end);
142  bool parseIf(QList<Node> *nodes, QString::const_iterator *i, QString::const_iterator end);
143  bool parseDir(QList<Node> *nodes, QString::const_iterator *i, QString::const_iterator end);
144  void parseText(QList<Node> *nodes, QString::const_iterator *i, QString::const_iterator end);
145  void parseEscape(QList<Node> *nodes, QString::const_iterator *i, QString::const_iterator end);
146 
147  QString evalute(const QList<Node> *nodes, const TrackInfo *info, int trackIndex) const;
148  QString printParam(Param *p, const TrackInfo *info, int trackIndex) const;
149  QString printField(int field, const TrackInfo *info, int trackIndex) const;
150  QString printProperty(int field, const TrackInfo *info) const;
151 
152  QString dumpNode(Node node) const;
153 
154  QList<MetaDataFormatter::Node> compile(const QString &expr);
155  QString m_pattern;
156  QList<MetaDataFormatter::Node> m_nodes;
157  QMap<QString, int> m_fieldNames;
158  QMap<QString, int> m_propertyNames;
159 };
160 
161 #endif // METADATAFORMATTER2_H
Definition: qmmp.h:76
The MetaDataFormatter formats metadata using templates.
Definition: metadataformatter.h:34
The PlayListTrack class provides a track for use with the PlayListModel class.
Definition: playlisttrack.h:36