diff options
| -rw-r--r-- | src/qmmp/qmmp.h | 1 | ||||
| -rw-r--r-- | src/qmmpui/metadataformatter2.cpp | 209 | ||||
| -rw-r--r-- | src/qmmpui/metadataformatter2.h | 105 | ||||
| -rw-r--r-- | src/qmmpui/qmmpui.pro | 6 |
4 files changed, 319 insertions, 2 deletions
diff --git a/src/qmmp/qmmp.h b/src/qmmp/qmmp.h index a079e2a02..6d3e63f03 100644 --- a/src/qmmp/qmmp.h +++ b/src/qmmp/qmmp.h @@ -53,6 +53,7 @@ public: */ enum MetaData { + UNKNOWN = -1, TITLE = 0, /*!< Title */ ARTIST, /*!< Artist */ ALBUMARTIST,/*!< Album artist */ diff --git a/src/qmmpui/metadataformatter2.cpp b/src/qmmpui/metadataformatter2.cpp new file mode 100644 index 000000000..c59151060 --- /dev/null +++ b/src/qmmpui/metadataformatter2.cpp @@ -0,0 +1,209 @@ +/*************************************************************************** + * Copyright (C) 2009-2014 by Ilya Kotov * + * forkotov02@hotmail.ru * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + + +/* +Syntax: +%p - artist, +%a - album, +%aa - album artist, +%t - title, +%n - track number, +%NN - 2-digit track number, +%g - genre, +%c - comment, +%C - composer, +%D - disc number, +%f - file name, +%F - full path, +%y - year, +%l - duration, +%if(A,B,C) or %if(A&B&C,D,E) - condition. +*/ + +#include <QStringList> +#include <QUrl> +#include "metadataformatter2.h" + +MetaDataFormatter2::MetaDataFormatter2(const QString &format) +{ + m_format = format; + //compile("%p - %a"); +} + +QString MetaDataFormatter2::parse(const PlayListTrack *item) +{ + return parse(*item, item->length()); +} + +QString MetaDataFormatter2::parse(const QMap<Qmmp::MetaData, QString> &metaData, qint64 length) +{ + /*QString title = m_format; + title.replace("\\(", "%28"); + title.replace("\\)", "%29"); + title.replace(")", "%)"); + title.replace("&", "%&"); + title.replace(",", "%,"); + title.replace("%p", metaData[Qmmp::ARTIST]); + title.replace("%aa", metaData[Qmmp::ALBUMARTIST]); + title.replace("%a", metaData[Qmmp::ALBUM]); + title.replace("%t", metaData[Qmmp::TITLE]); + title.replace("%n", metaData[Qmmp::TRACK]); + title.replace("%NN", QString("%1").arg(metaData[Qmmp::TRACK],2,'0')); + title.replace("%g", metaData[Qmmp::GENRE]); + title.replace("%c", metaData[Qmmp::COMMENT]); + title.replace("%C", metaData[Qmmp::COMPOSER]); + title.replace("%D", metaData[Qmmp::DISCNUMBER]); + title.replace("%f", metaData[Qmmp::URL].section('/',-1)); + title.replace("%F", metaData[Qmmp::URL]); + title.replace("%y", metaData[Qmmp::YEAR]); + if(title.contains("l")) + title.replace("%l",formatLength(length)); + if(title.contains("%if")) + title = processIfKeyWord(title); + title.replace("%28", "("); + title.replace("%29", ")"); + return title.trimmed();*/ + return QString(); +} + +QString MetaDataFormatter2::formatLength(qint64 length) const +{ + if(length <= 0) + return QString(); + QString str; + if(length >= 3600) + str = QString("%1:%2").arg(length/3600).arg(length%3600/60, 2, 10, QChar('0')); + else + str = QString("%1").arg(length%3600/60); + str += QString(":%1").arg(length%60, 2, 10, QChar('0')); + return str; +} + +bool MetaDataFormatter2::processKey(QList<Node> *nodes, QString::const_iterator i, QString::const_iterator end) +{ + QHash<QString, Qmmp::MetaData> key_map; + key_map.insert("p", Qmmp::ARTIST); + key_map.insert("aa", Qmmp::ALBUMARTIST); + key_map.insert("a", Qmmp::ALBUM); + key_map.insert("t", Qmmp::TITLE); + key_map.insert("n", Qmmp::TRACK); + key_map.insert("g", Qmmp::GENRE); + key_map.insert("c", Qmmp::COMMENT); + key_map.insert("C", Qmmp::COMPOSER); + key_map.insert("D", Qmmp::DISCNUMBER); + key_map.insert("F", Qmmp::URL); + key_map.insert("y", Qmmp::YEAR); + + QString token_name = (*i); //2 symbols + i++; //TODO check end + token_name.append(*i); + + Qmmp::MetaData key = key_map.value(token_name, Qmmp::UNKNOWN); + + if(key == Qmmp::UNKNOWN) + { + i--; + token_name = (*i); //1 symbol + key = key_map.value(token_name, Qmmp::UNKNOWN); + } + + if(key != Qmmp::UNKNOWN) + { + Node node; + node.command = Node::PRINT_TEXT; + Param param; + param.type = Param::KEY; + param.key = key; + node.params.append(param); + nodes->append(node); + return true; + } + return false; +} + +void MetaDataFormatter2::processText(QList<MetaDataFormatter2::Node> *nodes, QString::const_iterator i, QString::const_iterator end) +{ + Node node; + node.command = Node::PRINT_TEXT; + Param param; + param.type = Param::TEXT; + node.params.append(param); + + while (i != end) + { + node.params[0].text.append(*i); + i++; + if((*i) == QChar('%')) + { + i--; + nodes->append(node); + return; + } + } + nodes->append(node); +} + +void MetaDataFormatter2::compile(const QString &format) +{ + QString::const_iterator i = format.constBegin(); + + while (i != format.constEnd()) + { + i++; + if((*i) == QChar('%')) + { + i++; + if(processKey(&m_nodes, i, format.constEnd())) + { + continue; + } + } + else + { + processText(&m_nodes, i, format.constEnd()); + continue; + } + } +} + +/*QString MetaDataFormatter::processIfKeyWord(QString title) +{ + int pos = title.lastIndexOf("%if("); + int size = title.indexOf("%)",pos) - pos; + + QStringList args = title.mid (pos + 4, size - 4).split("%,"); + if(args.count() < 3) + { + qWarning("TitleFormatter: invalid title format"); + return title; + } + //process condition + bool cond = true; + foreach(QString arg, args.at(0).split("%&")) + { + cond &= !arg.isEmpty(); + } + QString r_str = cond ? args.at(1) : args.at(2); + title.replace (pos, size + 2, r_str); + if(title.contains("%if")) + return processIfKeyWord(title); + return title; +}*/ diff --git a/src/qmmpui/metadataformatter2.h b/src/qmmpui/metadataformatter2.h new file mode 100644 index 000000000..dd0bc6186 --- /dev/null +++ b/src/qmmpui/metadataformatter2.h @@ -0,0 +1,105 @@ +/*************************************************************************** + * Copyright (C) 2009-2014 by Ilya Kotov * + * forkotov02@hotmail.ru * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#ifndef METADATAFORMATTER2_H +#define METADATAFORMATTER2_H + +#include <QString> +#include <QMap> +#include <qmmpui/playlisttrack.h> +#include <qmmp/qmmp.h> + +/*! @brief The MetaDataFormatter formats metadata using templates. + * @author Ilya Kotov <forkotov02@hotmail.ru> + */ +class MetaDataFormatter2 +{ +public: + /*! + * Constructor. + * @param format Metadata template. + * Syntax: + * %p - artist, + * %a - album, + * %aa - album artist, + * %t - title, + * %n - track number, + * %NN - 2-digit track number, + * %g - genre, + * %c - comment, + * %C - composer, + * %D - disc number, + * %f - file name, + * %F - full path, + * %y - year, + * %l - duration, + * %if(A,B,C) or %if(A&B&C,D,E) - condition. + */ + MetaDataFormatter2(const QString &format = QString()); + /*! + * Converts metadata of item \b item to one string using template. + */ + QString parse(const PlayListTrack *item); + /*! + * Converts metadata to one string using template. + * @param metaData Metadata array. + * @param length Length in seconds. + */ + QString parse(const QMap<Qmmp::MetaData, QString> &metaData, qint64 length = 0); + /*! + * Returns formatted length (example: 05:02:03). + * \param length Length in seconds. + */ + QString formatLength(qint64 length) const; + +private: + struct Node; + struct Param + { + enum { + KEY = 0, + TEXT, + NODES + } type; + + Qmmp::MetaData key; + QString text; + QList<Node> children; + }; + struct Node + { + enum { + PRINT_TEXT = 0, + IF_KEYWORD + } command; + + QList<Param> params; + }; + + bool processKey(QList<Node> *nodes, QString::const_iterator i, QString::const_iterator end); + void processText(QList<Node> *nodes, QString::const_iterator i, QString::const_iterator end); + + void compile(const QString &format); + QString m_format; + QList<Node> m_nodes; + //QString processIfKeyWord(QString title); +}; + +#endif // METADATAFORMATTER2_H diff --git a/src/qmmpui/qmmpui.pro b/src/qmmpui/qmmpui.pro index f90c8992e..58e9d2aa3 100644 --- a/src/qmmpui/qmmpui.pro +++ b/src/qmmpui/qmmpui.pro @@ -69,7 +69,8 @@ HEADERS += general.h \ playlistcontainer_p.h \ groupedcontainer_p.h \ normalcontainer_p.h \ - playlisttask_p.h + playlisttask_p.h \ + metadataformatter2.h SOURCES += general.cpp \ playlistparser.cpp \ @@ -103,7 +104,8 @@ SOURCES += general.cpp \ groupedcontainer.cpp \ normalcontainer.cpp \ playlistcontainer.cpp \ - playlisttask.cpp + playlisttask.cpp \ + metadataformatter2.cpp FORMS += forms/detailsdialog.ui \ forms/tageditor.ui \ |
