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