diff options
Diffstat (limited to 'indexerwidget.cpp')
-rw-r--r-- | indexerwidget.cpp | 94 |
1 files changed, 94 insertions, 0 deletions
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; +} |