summaryrefslogtreecommitdiffstats
path: root/indexerwidget.cpp
blob: caf6d7d2dd0ce61aa859bd4231bd83f36d487417 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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;
}