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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
#include <QTextEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QProcess>
#include <QApplication>
#include <QCursor>
#include <QSqlQuery>
#include <QFileInfo>
#include <QLabel>
#include <QGroupBox>
#include <QProgressBar>
#include "taglib/tag.h"
#include "taglib/audioproperties.h"
#include "indexerwidget.h"
#include "globals.h"
IndexerWidget::IndexerWidget(QWidget *parent) : QWidget(parent), mMax(0) {
//errors
QGroupBox *gb1 = new QGroupBox(tr("Errors"));
mError = new QTextEdit;
mError->setFont(QFont("courier new"));
mError->setTextColor(Qt::red);
QVBoxLayout *gb1L = new QVBoxLayout;
gb1L->addWidget(mError);
gb1->setLayout(gb1L);
//progress
QLabel *l1 = new QLabel(tr("Indexing:"));
l1->setFont(QFont("courier"));
mProgress = new QProgressBar;
QHBoxLayout *progressL = new QHBoxLayout;
mProgressCount = new QLabel(tr("000000/00000"));
mProgressCount->setFont(QFont("courier"));
progressL->addWidget(l1);
progressL->addWidget(mProgress);
progressL->addWidget(mProgressCount);
//reader
mReader = new BeetReader;
connect(mReader, &BeetReader::errorMsg, this, &IndexerWidget::addToError);
connect(mReader, &BeetReader::totalCount, this, &IndexerWidget::setupProgress);
connect(mReader, &BeetReader::progress, this, &IndexerWidget::progress);
//main layout
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(gb1);
mainLayout->addLayout(progressL);
setLayout(mainLayout);
}
void IndexerWidget::startIndexing(){
mError->clear();
mReader->start();
}
void IndexerWidget::stopIndexing(){
mReader->cancel();
addToError(tr("Canceled!"));
}
void IndexerWidget::addToError(QString msg){
mError->append(msg);
}
void IndexerWidget::setupProgress(int max){
mProgress->reset();
mProgress->setMinimum(0);
mProgress->setMaximum(max);
mMax = max;
}
void IndexerWidget::progress(int cur){
mProgress->setValue(cur);
QString p = QString("%1/%2").arg(cur, 6, 10, QChar('0')).arg(mMax, 6, 10, QChar('0'));
mProgressCount->setText(p);
}
BeetReader::BeetReader() : mCanceled(false){
mDb = QSqlDatabase::database("beetplayerdb");
mInsertArtistsQ = new QSqlQuery(mDb);
mInsertArtistsQ->prepare("INSERT INTO artists (tartists_name) VALUES(:name)");
mCurArtistQ = new QSqlQuery(mDb);
mCurArtistQ->prepare("SELECT currval('artist_iartists_id__seq')");
mInsertGenresQ = new QSqlQuery(mDb);
mInsertGenresQ->prepare("INSERT INTO genres (tgenres_name) VALUES(:name)");
mCurGenresQ = new QSqlQuery(mDb);
mCurGenresQ->prepare("SELECT currval('genres_igenre_id__seq')");
mInsertAlbumQ = new QSqlQuery(mDb);
mInsertAlbumQ->prepare("INSERT INTO albums(talbum_name, siyear) VALUES(:name, :year)");
mCurAlbumQ = new QSqlQuery(mDb);
mCurAlbumQ->prepare("SELECT currval('albums_ialbums_id__seq')");
mInsertSongQ = new QSqlQuery(mDb);
mInsertSongQ->prepare("INSERT INTO songs (ialbums_id, sipos, ttitle, iartists_id, igenres_id, tfullpath, ilength) VALUES(:aid, :pos, :title, :iartistid, :igenid, :tfp, :len)");
}
void BeetReader::run(){
clearAll();
QProcess lister;
lister.start("beet", QStringList() << "ls" << "-p");
qApp->setOverrideCursor(QCursor(Qt::WaitCursor));
emit errorMsg(tr("Waiting for beet ls -p"));
lister.waitForStarted();
lister.waitForFinished();
qApp->restoreOverrideCursor();
emit errorMsg(tr("Done Waiting - Starting some serious work!"));
QByteArray lOut = lister.readAllStandardOutput();
QList<QByteArray> files = lOut.split('\n');
int total = files.size();
emit totalCount(total);
int ctr = 1;
foreach(QByteArray s, files){
//fetch data from file
TagLib::FileRef file(QString(s).toUtf8());
if(file.isNull()){
QString fn = QString(s).toUtf8();
if(!fn.isEmpty()){
QString error = QString(tr("IsNull: %1")).arg(fn);
emit errorMsg(error);
}
continue;
}
QString artist = toQString(file.tag()->artist());
QString album = toQString(file.tag()->album());
QString title = toQString(file.tag()->title());
QString genre = toQString(file.tag()->genre());
int length = file.audioProperties()->lengthInSeconds();
quint16 track = file.tag()->track();
quint16 year = file.tag()->year();
//write to database
int artistId = doArtist(artist);
int genreId = doGenre(genre);
int albumId = doAlbum(album, year);
doSong(title, track, albumId, genreId, artistId, s, length);
if(ctr % 100 == 0){
emit progress(ctr);
}
mCancelMx.lock();
if(mCanceled == true){
mCanceled = false;
mCancelMx.unlock();
return;
}
mCancelMx.unlock();
++ctr;
}
emit progress(total);
emit errorMsg(tr("Serious work done!"));
}
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;
}
void BeetReader::clearAll(){
QSqlQuery q1("DELETE FROM songs", mDb);
QSqlQuery q2("DELETE FROM albums", mDb);
QSqlQuery q3("DELETE FROM genres", mDb);
QSqlQuery q4("DELETE FROM artists", mDb);
mArtistH.clear();
mAlbumH.clear();
mGenreH.clear();
QSqlQuery q5("INSERT INTO genres VALUES(-1, 'none')", mDb);
q5.exec();
emit cleared();
}
int BeetReader::doArtist(QString name){
if(mArtistH.contains(name)){
return mArtistH.value(name);
}
mDb.transaction();
mInsertArtistsQ->bindValue(":name", name);
mInsertArtistsQ->exec();
mCurArtistQ->exec();
int retval = -1;
while(mCurArtistQ->next()){
retval = mCurArtistQ->value(0).toInt();
}
mArtistH.insert(name, retval);
mDb.commit();
return retval;
}
int BeetReader::doGenre(QString name){
if(mGenreH.contains(name)){
return mGenreH.value(name);
}
mDb.transaction();
mInsertGenresQ->bindValue(":name", name);
mInsertGenresQ->exec();
mCurGenresQ->exec();
int retval = -1;
mCurGenresQ->exec();
while(mCurGenresQ->next()){
retval = mCurGenresQ->value(0).toInt();
}
mGenreH.insert(name, retval);
mDb.commit();
return retval;
}
int BeetReader::doAlbum(QString name, int year){
QString key = name;
if(mAlbumH.contains(key)){
return mAlbumH.value(key);
}
mDb.transaction();
mInsertAlbumQ->bindValue(":name", name);
mInsertAlbumQ->bindValue(":year", year);
mInsertAlbumQ->exec();
int retval = -1;
mCurAlbumQ->exec();
while(mCurAlbumQ->next()){
retval = mCurAlbumQ->value(0).toInt();
}
mAlbumH.insert(key, retval);
mDb.commit();
return retval;
}
void BeetReader::doSong(QString title, int pos, int album, int genre, int artist, QString fullpath, int length){
mDb.transaction();
mInsertSongQ->bindValue(":title", title);
mInsertSongQ->bindValue(":pos", pos);
mInsertSongQ->bindValue(":aid", album);
mInsertSongQ->bindValue(":iartistid", artist);
mInsertSongQ->bindValue(":igenid", genre);
mInsertSongQ->bindValue(":tfp", fullpath);
mInsertSongQ->bindValue(":len", length);
mInsertSongQ->exec();
mDb.commit();
}
|