diff options
| author | trialuser02 <trialuser02@90c681e8-e032-0410-971d-27865f9a5e38> | 2020-11-01 16:41:14 +0000 |
|---|---|---|
| committer | trialuser02 <trialuser02@90c681e8-e032-0410-971d-27865f9a5e38> | 2020-11-01 16:41:14 +0000 |
| commit | 1f4d75c6f9f5b45b8b55c1dabf8c0aa3322395fc (patch) | |
| tree | 84f011bc6baa863ebc96181ff227b560e6144452 | |
| parent | d849b4462100259fa8765f92e445b747a350d764 (diff) | |
| download | qmmp-1f4d75c6f9f5b45b8b55c1dabf8c0aa3322395fc.tar.gz qmmp-1f4d75c6f9f5b45b8b55c1dabf8c0aa3322395fc.tar.bz2 qmmp-1f4d75c6f9f5b45b8b55c1dabf8c0aa3322395fc.zip | |
library: Library::addTrack implementation
git-svn-id: http://svn.code.sf.net/p/qmmp-dev/code/trunk/qmmp@9544 90c681e8-e032-0410-971d-27865f9a5e38
| -rw-r--r-- | src/plugins/General/library/library.cpp | 96 | ||||
| -rw-r--r-- | src/plugins/General/library/library.h | 7 |
2 files changed, 74 insertions, 29 deletions
diff --git a/src/plugins/General/library/library.cpp b/src/plugins/General/library/library.cpp index 14ffc61ca..711b2e702 100644 --- a/src/plugins/General/library/library.cpp +++ b/src/plugins/General/library/library.cpp @@ -24,11 +24,18 @@ #include <QVariant> #include <QAction> #include <QApplication> +#include <QFileInfo> +#include <QDateTime> +#include <QJsonDocument> +#include <QJsonObject> +#include <QHash> #include <qmmpui/uihelper.h> #include <qmmp/soundcore.h> //#include "historywindow.h" #include "library.h" +#define CONNECTION_NAME "qmmp_library" + Library::Library(QObject *parent) : QObject(parent) { QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", CONNECTION_NAME); @@ -85,7 +92,7 @@ bool Library::createTables() "Timestamp TIMESTAMP NOT NULL," "Title TEXT, Artist TEXT, AlbumArtist TEXT, Album TEXT, Comment TEXT, Genre TEXT, Composer TEXT," "Year INTEGER, Track INTEGER, DiscNumer INTEGER, Duration INTEGER, " - "AudioInfo BLOB, URL BLOB, FilePath BLOB)"); + "AudioInfo BLOB, URL TEXT, FilePath TEXT)"); if(!ok) qWarning("Library: unable to create table, error: %s", qPrintable(query.lastError().text())); @@ -93,32 +100,73 @@ bool Library::createTables() return ok; } -/*void Library::saveTrack() +void Library::addTrack(TrackInfo *track, const QString &filePath) { QSqlDatabase db = QSqlDatabase::database(CONNECTION_NAME); - if(!db.isOpen() || m_trackInfo.isEmpty()) + if(!db.isOpen()) return; QSqlQuery query(db); - query.prepare("INSERT INTO track_library VALUES(NULL, CURRENT_TIMESTAMP, :title, :artist, :albumartist, :album, :comment," - ":genre, :composer, :year, :track, :discnumber, :duration, :url);"); - query.bindValue(":title", m_trackInfo.value(Qmmp::TITLE)); - query.bindValue(":artist", m_trackInfo.value(Qmmp::ARTIST)); - query.bindValue(":albumartist", m_trackInfo.value(Qmmp::ALBUMARTIST)); - query.bindValue(":album", m_trackInfo.value(Qmmp::ALBUM)); - query.bindValue(":comment", m_trackInfo.value(Qmmp::COMMENT)); - query.bindValue(":genre", m_trackInfo.value(Qmmp::GENRE)); - query.bindValue(":composer", m_trackInfo.value(Qmmp::COMPOSER)); - query.bindValue(":year", m_trackInfo.value(Qmmp::YEAR)); - query.bindValue(":track", m_trackInfo.value(Qmmp::TRACK)); - query.bindValue(":discnumber", m_trackInfo.value(Qmmp::DISCNUMBER)); - query.bindValue(":duration", m_trackInfo.duration()); - query.bindValue(":url", m_trackInfo.path()); - bool ok = query.exec(); + query.prepare("INSERT INTO track_library VALUES(" + "NULL, :timestamp, " + ":title, :artist, :albumartist, :album TEXT, :comment, :genre, :composer, " + ":year, :track, :discnumer INTEGER, :duration, " + ":audioinfo, :url, :filepath)"); - if(!ok) - qWarning("Library: unable to save track, error: %s", qPrintable(query.lastError().text())); - else - qDebug("Library: track '%s' has been added to history", - qPrintable(m_trackInfo.value(Qmmp::ARTIST) + " - " + m_trackInfo.value(Qmmp::TITLE))); -}*/ + query.bindValue(":timestamp", QFileInfo(filePath).lastModified()); + query.bindValue(":title", track->value(Qmmp::TITLE)); + query.bindValue(":artist", track->value(Qmmp::ARTIST)); + query.bindValue(":albumartist", track->value(Qmmp::ALBUMARTIST)); + query.bindValue(":album", track->value(Qmmp::ALBUM)); + query.bindValue(":comment", track->value(Qmmp::COMMENT)); + query.bindValue(":genre", track->value(Qmmp::GENRE)); + query.bindValue(":composer", track->value(Qmmp::COMPOSER)); + query.bindValue(":year", track->value(Qmmp::YEAR)); + query.bindValue(":track", track->value(Qmmp::TRACK)); + query.bindValue(":discnumber", track->value(Qmmp::DISCNUMBER)); + query.bindValue(":duration", track->duration()); + query.bindValue(":audioinfo", serializeAudioInfo(track->properties())); + query.bindValue(":url", track->path()); + query.bindValue(":filepath", filePath); +} + +QByteArray Library::serializeAudioInfo(const QMap<Qmmp::TrackProperty, QString> &properties) +{ + QJsonObject obj; + QMap<Qmmp::TrackProperty, QString>::const_iterator it = properties.cbegin(); + while(it != properties.cend()) + { + QString value = properties[it.key()]; + + switch(it.key()) + { + case Qmmp::BITRATE: + obj.insert("bitrate", value.toInt()); + break; + case Qmmp::SAMPLERATE: + obj.insert("samplerate", value.toInt()); + break; + case Qmmp::CHANNELS: + obj.insert("channels", value.toInt()); + break; + case Qmmp::BITS_PER_SAMPLE: + obj.insert("bitsPerSample", value.toInt()); + break; + case Qmmp::FORMAT_NAME: + obj.insert("formatName", value); + break; + case Qmmp::DECODER: + obj.insert("decoder", value); + break; + case Qmmp::FILE_SIZE: + obj.insert("fileSize", value.toLongLong()); + break; + default: + ; + } + + ++it; + } + + return QJsonDocument(obj).toJson(QJsonDocument::Compact); +} diff --git a/src/plugins/General/library/library.h b/src/plugins/General/library/library.h index 71903d840..b4baff608 100644 --- a/src/plugins/General/library/library.h +++ b/src/plugins/General/library/library.h @@ -27,8 +27,6 @@ #include <qmmp/trackinfo.h> #include <qmmp/qmmp.h> -#define CONNECTION_NAME "qmmp_library" - class SoundCore; //class HistoryWindow; @@ -44,9 +42,8 @@ private slots: private: bool createTables(); - //void saveTrack(); - - //QPointer<HistoryWindow> m_historyWindow; + void addTrack(TrackInfo *track, const QString &filePath); + QByteArray serializeAudioInfo(const QMap<Qmmp::TrackProperty, QString> &properties); }; |
