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