diff options
-rw-r--r-- | BeetPlayer.pro | 8 | ||||
-rw-r--r-- | beetplayer.cpp | 12 | ||||
-rw-r--r-- | beetplayer.h | 1 | ||||
-rw-r--r-- | indexerwidget.cpp | 94 | ||||
-rw-r--r-- | indexerwidget.h | 57 |
5 files changed, 170 insertions, 2 deletions
diff --git a/BeetPlayer.pro b/BeetPlayer.pro index ebc16c7..de36e89 100644 --- a/BeetPlayer.pro +++ b/BeetPlayer.pro @@ -25,7 +25,11 @@ DEFINES += QT_DEPRECATED_WARNINGS SOURCES += main.cpp\ beetplayer.cpp \ - configurationdialog.cpp + configurationdialog.cpp \ + indexerwidget.cpp HEADERS += beetplayer.h \ - configurationdialog.h + configurationdialog.h \ + indexerwidget.h + +LIBS += -ltag diff --git a/beetplayer.cpp b/beetplayer.cpp index 52c80e4..a9e8b63 100644 --- a/beetplayer.cpp +++ b/beetplayer.cpp @@ -2,15 +2,27 @@ #include <QSqlDatabase> #include <QMessageBox> #include <QApplication> +#include <QHBoxLayout> #include "beetplayer.h" #include "configurationdialog.h" +#include "indexerwidget.h" BeetPlayer::BeetPlayer(QWidget *parent, Qt::WindowFlags f) : QMainWindow(parent, f) { //general setup setMinimumWidth(800); setMinimumHeight(600); openDatabase(); + + //tabs + mTab = new QTabWidget; + IndexerWidget *indexer = new IndexerWidget; + mTab->addTab(indexer, tr("Indexer")); + + //layout + QHBoxLayout *mainLayout = new QHBoxLayout; + mainLayout->addWidget(mTab); + setCentralWidget(mTab); } void BeetPlayer::openDatabase(){ diff --git a/beetplayer.h b/beetplayer.h index 296f99f..e92012f 100644 --- a/beetplayer.h +++ b/beetplayer.h @@ -11,6 +11,7 @@ class BeetPlayer : public QMainWindow { private: void openDatabase(); + QTabWidget *mTab; }; #endif // BEETPLAYER_H diff --git a/indexerwidget.cpp b/indexerwidget.cpp new file mode 100644 index 0000000..caf6d7d --- /dev/null +++ b/indexerwidget.cpp @@ -0,0 +1,94 @@ +#include <QTextEdit> +#include <QPushButton> +#include <QVBoxLayout> +#include <QHBoxLayout> +#include <QProcess> +#include <QApplication> +#include <QCursor> +#include <QAudioDecoder> + +#include "taglib/tag.h" +#include "indexerwidget.h" + +IndexerWidget::IndexerWidget(QWidget *parent) : QWidget(parent) { + //widgets + mLog = new QTextEdit; + mLog->setFont(QFont("courier new")); + QPushButton *startB = new QPushButton(tr("Index")); + connect(startB, SIGNAL(clicked()), this, SLOT(startIndexing())); + QPushButton *cancelB = new QPushButton(tr("Cancel")); + connect(cancelB, SIGNAL(clicked()), this, SLOT(stopIndexing())); + + //reader + mReader = new BeetReader; + connect(mReader, SIGNAL(message(QString)), this, SLOT(addToLog(QString))); + + //layout + QVBoxLayout *mainLayout = new QVBoxLayout; + QHBoxLayout *buttonLayout = new QHBoxLayout; + buttonLayout->addStretch(); + buttonLayout->addWidget(startB); + buttonLayout->addWidget(cancelB); + buttonLayout->addStretch(); + mainLayout->addWidget(mLog); + mainLayout->addLayout(buttonLayout); + setLayout(mainLayout); +} + +void IndexerWidget::startIndexing(){ + mReader->start(); +} + +void IndexerWidget::stopIndexing(){ + mReader->cancel(); +} + +void IndexerWidget::addToLog(QString msg){ + mLog->append(msg); +} + +BeetReader::BeetReader() : mCanceled(false){ +} + +void BeetReader::run(){ + QProcess lister; + lister.start("beet", QStringList() << "ls" << "-p"); + qApp->setOverrideCursor(QCursor(Qt::WaitCursor)); + lister.waitForStarted(); + lister.waitForFinished(); + qApp->restoreOverrideCursor(); + QByteArray lOut = lister.readAllStandardOutput(); + QList<QByteArray> files = lOut.split('\n'); + QString foundMsg = QString(tr("Found %1 file(s)\n").arg(QString::number(files.size()))); + emit message(foundMsg); + foreach(QByteArray s, files){ + TagLib::FileRef file(QString(s).toUtf8()); + QString artist = toQString(file.tag()->artist()); + QString album = toQString(file.tag()->album()); + QString title = toQString(file.tag()->title()); + QString genre = toQString(file.tag()->genre()); + quint16 track = file.tag()->track(); + quint16 year = file.tag()->year(); + QString msg = QString("%1 - %2: %3 - %4 (%5 - %6)").arg(artist).arg(album).arg(track, 2, 10, QChar('0')).arg(title).arg(QString::number(year)).arg(genre); + emit message(msg); + mCancelMx.lock(); + if(mCanceled == true){ + mCanceled = false; + mCancelMx.unlock(); + return; + } + mCancelMx.unlock(); + } +} + +void BeetReader::cancel(){ + mCancelMx.lock(); + mCanceled = true; + mCancelMx.unlock(); +} + +QString BeetReader::toQString(TagLib::String string){ + QString retval = QString::fromStdWString(string.toWString()); + retval = retval.simplified().toLower(); + return retval; +} diff --git a/indexerwidget.h b/indexerwidget.h new file mode 100644 index 0000000..9fab07a --- /dev/null +++ b/indexerwidget.h @@ -0,0 +1,57 @@ +#ifndef INDEXERWIDGET_H +#define INDEXERWIDGET_H + +#include <QWidget> +#include <QThread> +#include <QMutex> + +#include "taglib/fileref.h" + +class QTextEdit; +class BeetReader; +struct BeetObject; + +class IndexerWidget : public QWidget { + Q_OBJECT + public: + explicit IndexerWidget(QWidget *parent = 0); + + public slots: + void startIndexing(); + void stopIndexing(); + void addToLog(QString msg); + + private: + QTextEdit *mLog; + BeetReader *mReader; + +}; + +class BeetReader : public QThread { + Q_OBJECT + public: + explicit BeetReader(); + virtual void run(); + void cancel(); + + signals: + void message(const QString &msg); + void processed(BeetObject &obj); + + private: + QString toQString(TagLib::String string); + QMutex mCancelMx; + bool mCanceled; +}; + +struct BeetObject { + QString artist; + QString album; + quint16 year; + QString genre; + quint16 pos; + QString title; + QString fullpath; +}; + +#endif // INDEXERWIDGET_H |