forked from milohr/babe-qt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollectionDB.cpp
642 lines (510 loc) · 18.6 KB
/
collectionDB.cpp
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
/*
Babe - tiny music player
Copyright (C) 2017 Camilo Higuita
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "collectionDB.h"
CollectionDB::CollectionDB()
{
}
/*CollectionDB::CollectionDB(bool connect)
{
if(connect)
{
m_db = QSqlDatabase::addDatabase("QSQLITE");
m_db.setDatabaseName("../player/collection.db");
m_db.open();
if (!m_db.open())
{
qDebug() << "Error: connection with database fail";
}
else
{
qDebug() << "Database: connection ok";
}
}
}*/
void CollectionDB::closeConnection()
{
m_db.close();
}
void CollectionDB::openCollection(QString path)
{
m_db = QSqlDatabase::addDatabase("QSQLITE");
m_db.setDatabaseName(path);
if (!m_db.open())
{
qDebug() << "Error: connection with database fail" <<m_db.lastError().text();
}
else
{
qDebug() << "Database: connection ok";
}
}
void CollectionDB::prepareCollectionDB()
{
QSqlQuery query;
query.exec("CREATE TABLE tracks(track integer, title text, artist text, album text, genre text, location text unique, stars integer, babe integer, art text, played integer, playlist text);");
query.exec("CREATE TABLE albums(title text, artist text, art text, location text);");
query.exec("CREATE TABLE playlists(title text, art text unique);");
query.exec("CREATE TABLE artists(title text, art text, location text);");
//query.exec("CREATE TABLE tracks(title text, album text, artist text, location text, stars integer, babe integer);");
}
void CollectionDB::removePath(QString path)
{
qDebug()<<"trying to delete all from :"<< path;
QSqlQuery queryTracks;
queryTracks.prepare("DELETE FROM tracks WHERE location LIKE \"%"+path+"%\"");
bool success = queryTracks.exec();
emit DBactionFinished(false);
if(!success)
{
qDebug() << "removePerson error: ";
}
}
QString CollectionDB::getArtistArt(QString artist)
{
QString artistHead;
QSqlQuery queryHead("SELECT * FROM artists WHERE title = \""+artist+"\"");
while (queryHead.next())
if(!queryHead.value(1).toString().isEmpty()&&queryHead.value(1).toString()!="NULL")
artistHead = queryHead.value(1).toString();
return artistHead;
}
QString CollectionDB::getAlbumArt(QString album, QString artist)
{
QString albumCover;
QSqlQuery queryCover ("SELECT * FROM albums WHERE title = \""+album+"\" AND artist = \""+artist+"\"");
while (queryCover.next())
if(!queryCover.value(2).toString().isEmpty()&&queryCover.value(2).toString()!="NULL")
albumCover = queryCover.value(2).toString();
return albumCover;
}
QList<QMap<int, QString>> CollectionDB::getTrackData(QStringList urls)
{
QList<QMap<int, QString>> mapList;
for(auto url:urls)
{
QSqlQuery query("SELECT * FROM tracks WHERE location =\""+url+"\"");
if(query.exec())
{
while(query.next())
{
QString track = query.value(TRACK).toString();
QString title = query.value(TITLE).toString();
QString artist = query.value(ARTIST).toString();
QString album = query.value(ALBUM).toString();
QString genre = query.value(GENRE).toString();
QString location = query.value(LOCATION).toString();
QString stars = query.value(STARS).toString();
QString babe = query.value(BABE).toString();
QString art = query.value(ART).toString();
QString playlist = query.value(PLAYLIST).toString();
QString played = query.value(PLAYED).toString();
const QMap<int, QString> map{{TRACK,track}, {TITLE,title}, {ARTIST,artist},{ALBUM,album},{GENRE,genre},{LOCATION,location},{STARS,stars},{BABE,babe},{ART,art},{PLAYED,played},{PLAYLIST,playlist}};
mapList<<map;
}
}
}
return mapList;
}
QList<QMap<int, QString>> CollectionDB::getTrackData(QString queryText)
{
QList<QMap<int, QString>> mapList;
QSqlQuery query;
query.prepare(queryText);
// qDebug()<<queryText;
if(query.exec())
{
//qDebug()<<"getTrackData query passed";
while(query.next())
{
QString track = query.value(TRACK).toString();
QString title = query.value(TITLE).toString();
QString artist = query.value(ARTIST).toString();
QString album = query.value(ALBUM).toString();
QString genre = query.value(GENRE).toString();
QString location = query.value(LOCATION).toString();
QString stars = query.value(STARS).toString();
QString babe = query.value(BABE).toString();
QString art = query.value(ART).toString();
QString playlist = query.value(PLAYLIST).toString();
QString played = query.value(PLAYED).toString();
// qDebug()<<track<<title<<artist<<album;
const QMap<int, QString> map{{TRACK,track}, {TITLE,title}, {ARTIST,artist},{ALBUM,album},{GENRE,genre},{LOCATION,location},{STARS,stars},{BABE,babe},{ART,art},{PLAYED,played},{PLAYLIST,playlist}};
mapList<<map;
}
}
return mapList;
}
void CollectionDB::cleanCollectionLists()
{
QSqlQuery queryArtists("SELECT * FROM artists");
if(queryArtists.exec())
{
while(queryArtists.next())
{
QString oldArtists = queryArtists.value(0).toString();
if(artists.contains(oldArtists))
continue;
else
{
qDebug()<<"artists list does not longer contains: "<<oldArtists;
QSqlQuery queryArtist_delete;
queryArtist_delete.prepare("DELETE FROM artists WHERE title = \""+oldArtists+"\"");
if(queryArtist_delete.exec()) qDebug()<<"deleted missing artist";
}
}
}
QSqlQuery queryAlbums("SELECT * FROM albums");
if(queryAlbums.exec())
{
while(queryAlbums.next())
{
QString oldAlbum = queryAlbums.value(1).toString()+" "+queryAlbums.value(0).toString();
if(albums.contains(oldAlbum))
continue;
else
{
qDebug()<<"albums list does not longer contains: "<<oldAlbum;
QSqlQuery queryAlbum_delete;
queryAlbum_delete.prepare("DELETE FROM albums WHERE title = \""+queryAlbums.value(0).toString()+"\"");
if(queryAlbum_delete.exec()) qDebug()<<"deleted missing album";
}
}
}
}
QSqlQuery CollectionDB::getQuery(QString queryTxt)
{
QSqlQuery query(queryTxt);
return query;
}
bool CollectionDB::removeQuery(QString queryTxt)
{
QSqlQuery query;
query.prepare(queryTxt);
if(!query.exec())
{
qDebug() << "removeQuery error: "<< query.lastError();
return false;
}else return true;
}
bool CollectionDB::checkQuery(QString queryTxt)
{
QSqlQuery query(queryTxt);
qDebug()<<"The Query is: "<<queryTxt;
if (query.exec())
if (query.next()) return true;
else return false;
else return false;
}
void CollectionDB::setCollectionLists()
{
albums.clear(); artists.clear();
QSqlQuery query ("SELECT * FROM tracks");
while (query.next())
{
QString artist = query.value(ARTIST).toString();
QString album = query.value(ALBUM).toString();
//QString file = query.value(LOCATION).toString();
if(!albums.contains(artist+" "+album)) albums<<artist+" "+album;
if(!artists.contains(artist)) artists<<artist;
}
// refreshArtistsTable();
/*qDebug()<<"artist in collection list::";
for(auto artist:artists)qDebug()<<artist;
qDebug()<<"albums in collection list::";
for(auto album:albums)qDebug()<<album;*/
}
void CollectionDB::refreshArtistsTable()
{
QSqlQuery query ("SELECT * FROM tracks");
qDebug()<<"updating artists table";
if(query.exec())
{
while(query.next())
{
//success = true;
QString artist = query.value(ARTIST).toString();
QString file = query.value(LOCATION).toString();
if(!artists.contains(artist))
{
query.prepare("INSERT INTO artists (title, art, location)" "VALUES (:title, :art, :location)");
query.bindValue(":title", artist);
query.bindValue(":art", "");
query.bindValue(":location", QFileInfo(file).dir().path());
if(query.exec()) artists<<artist;
}
}
}
}
bool CollectionDB::addTrack(QStringList paths, int babe)
{
bool success = false;
if(paths.isEmpty()) return false;
QSqlQuery query;
if(query.exec("PRAGMA synchronous=OFF"))
{
success=true;
int i=0;
qDebug()<<"started writing to database...";
for(auto file:paths)
{
qDebug()<<file;
TagInfo info(file);
int track;
QString title, artist, album, genre;
// you should check if args are ok first...
track=info.getTrack();
title=BaeUtils::fixString(info.getTitle());
artist=BaeUtils::fixString(info.getArtist());
genre=info.getGenre();
if(info.getAlbum().isEmpty())
{
qDebug()<<"the album has not title, so i'm going to try and get it.";
info.writeData();
album=info.getAlbum();
}else album=info.getAlbum();
album=BaeUtils::fixString(album);
query.prepare("INSERT INTO tracks (track, title, artist, album, genre, location, stars, babe, art, played)" "VALUES (:track, :title, :artist, :album, :genre, :location, :stars, :babe, :art, :played) ");
query.bindValue(":track", track);
query.bindValue(":title", title);
query.bindValue(":artist", artist);
query.bindValue(":album", album);
query.bindValue(":genre", genre);
query.bindValue(":location", file);
query.bindValue(":stars", 0);
query.bindValue(":babe", babe);
query.bindValue(":art", "");
query.bindValue(":played", 0);
if(query.exec())
{
success = true;
qDebug()<< "writting to db: "<< title;
if(!albums.contains(artist+" "+album))
{
query.prepare("INSERT INTO albums (title, artist, art, location)" "VALUES (:title, :artist, :art, :location)");
query.bindValue(":title", album);
query.bindValue(":artist", artist);
query.bindValue(":art", "");
// query.bindValue(":location", QFileInfo(file).dir().path());
if(query.exec())
{
albums<<artist+" "+album;
success = true;
}else
{
return false;
}
}
if(!artists.contains(artist))
{
query.prepare("INSERT INTO artists (title, art, location)" "VALUES (:title, :art, :location)");
query.bindValue(":title", artist);
query.bindValue(":art", "");
// query.bindValue(":location", QFileInfo(file).dir().path());
if(query.exec()) artists<<artist;
}
emit progress((i++)+1);
}
else
{
qDebug() << "adding track error: "
<< query.lastError()
<<info.getTitle();
}
}
qDebug()<<"finished wrrting to database";
emit DBactionFinished(true);
}
else return false;
return success;
}
void CollectionDB::insertCoverArt(QString path,QStringList info)
{
//UPDATE albums SET art = "lalaltest" WHERE title = "Starboy" AND artist = "The Weeknd"
qDebug()<<"the path:"<<path<<"the list:"<<info.at(0)<<info.at(1);
if(info.size()==2)
{
QSqlQuery query;
query.prepare("UPDATE albums SET art = (:art) WHERE title = (:title) AND artist = (:artist)" );
//query.prepare("SELECT * FROM "+tableName+" WHERE "+searchId+" = (:search)");
query.bindValue(":art", path.isEmpty()?"NULL": path );
query.bindValue(":title", info.at(0));
query.bindValue(":artist", info.at(1));
if(query.exec())
{
qDebug()<<"Artwork[cover] inserted into DB"<<info.at(0)<<info.at(1);
if(!albums.contains(info.at(0))) albums<<info.at(1)+" "+info.at(0);
//qDebug()<<"insertInto<<"<<"UPDATE "+tableName+" SET "+column+" = "+ value + " WHERE location = "+location;
}else
{
qDebug()<<"COULDNT Artwork[cover] inerted into DB"<<info.at(0)<<info.at(1);
}
}
}
void CollectionDB::insertHeadArt(QString path, QStringList info)
{
if(info.size()==1)
{
QSqlQuery query;
query.prepare("UPDATE artists SET art = (:art) WHERE title = (:title)" );
//query.prepare("SELECT * FROM "+tableName+" WHERE "+searchId+" = (:search)");
query.bindValue(":art", path.isEmpty()?"NULL": path );
query.bindValue(":title", info.at(0));
if(query.exec())
{
qDebug()<<"Artwork[head] inerted into DB"<<info.at(0);
if(!artists.contains(info.at(0)))artists<<info.at(0);
//qDebug()<<"insertInto<<"<<"UPDATE "+tableName+" SET "+column+" = "+ value + " WHERE location = "+location;
}
}
}
void CollectionDB::setTrackList(QList<Track> trackList)
{
this->trackList=trackList;
/*for(auto tr:trackList)
{
qDebug()<<QString::fromStdString(tr.getTitle());
}*/
}
bool CollectionDB::check_existance(QString tableName, QString searchId, QString search)
{
QSqlQuery query;
query.prepare("SELECT "+ searchId +" FROM "+tableName+" WHERE "+searchId+" = (:search)");
query.bindValue(":search", search);
if (query.exec())
{
if (query.next())
{
qDebug()<< "it exists";
return true;
}else
{
qDebug()<<"currnt song doesn't exists in db";
return false;
}
}else
{
return false;
}
}
bool CollectionDB::execQuery(QString queryTxt)
{
QSqlQuery query;
query.prepare(queryTxt);
//query.prepare("SELECT * FROM "+tableName+" WHERE "+searchId+" = (:search)");
if(query.exec())
{
qDebug()<<"executing query: "<<queryTxt;
return true;
}else
{
return false;
}
}
bool CollectionDB::insertInto(QString tableName, QString column, QString location, int value)
{
QSqlQuery query;
if(query.exec("PRAGMA synchronous=OFF"))
{
query.prepare("UPDATE "+tableName+" SET "+column+" = (:value) WHERE location = (:location)" );
//query.prepare("SELECT * FROM "+tableName+" WHERE "+searchId+" = (:search)");
query.bindValue(":value", value);
query.bindValue(":location", location);
if(query.exec())
{
qDebug()<<"insertInto<<"<<"UPDATE "+tableName+" SET "+column+" = "+ value + " WHERE location = "+location;
return true;
}else
{
return false;
}
}
return false;
}
bool CollectionDB::insertInto(QString tableName, QString column, QString location, QString value)
{
QSqlQuery query;
query.prepare("UPDATE "+tableName+" SET "+column+" = (:value) WHERE location = (:location)" );
//query.prepare("SELECT * FROM "+tableName+" WHERE "+searchId+" = (:search)");
query.bindValue(":value", value);
query.bindValue(":location", location);
if(query.exec())
{
qDebug()<<"insertInto<<"<<"UPDATE "+tableName+" SET "+column+" = "+ value + " WHERE location = "+location;
return true;
}else
{
return false;
}
}
void CollectionDB::createTable(QString tableName)
{
QSqlQuery query;
query.exec("CREATE TABLE "+tableName+"(track integer, title text, artist text, album text, genre text, location text unique, stars integer, babe integer, art text, played integer, playlist text);");
}
void CollectionDB::insertPlaylist(QString name, QString color)
{
if(color.isEmpty())
{
QSqlQuery query;
query.prepare("INSERT INTO playlists (title)" "VALUES (:title) ");
query.bindValue(":title", name);
if(query.exec())
{
//qDebug()<<"insertInto<<"<<"UPDATE playlists SET title = "+ name ;
}
}else if(name.isEmpty())
{ QSqlQuery query;
query.prepare("INSERT INTO playlists (title, art)" "VALUES (:title, :art) ");
query.bindValue(":title", "mood");
query.bindValue(":art", color);
if(query.exec())
{
//qDebug()<<"insertInto<<"<<"UPDATE playlists SET title = "+ name ;
}
}else if(!name.isEmpty()&&!color.isEmpty())
{
QSqlQuery query;
query.prepare("INSERT INTO playlists (title, art)" "VALUES (:title, :art) ");
query.bindValue(":title", "mood");
query.bindValue(":art", color);
if(query.exec())
{
//qDebug()<<"insertInto<<"<<"UPDATE playlists SET title = "+ name ;
}
}
}
QStringList CollectionDB::getPlaylists()
{
QSqlQuery query;
QStringList files;
query.prepare("SELECT * FROM playlists");
if (query.exec())
while (query.next())
if(!query.value(0).toString().contains("mood")&&!query.value(0).toString().isEmpty())
files << query.value(0).toString();
return files;
}
QStringList CollectionDB::getPlaylistsMoods()
{
QStringList moods;
QSqlQuery query;
query.prepare("SELECT * FROM playlists order by title");
if (query.exec())
while (query.next())
if(!query.value(1).toString().isEmpty()&&query.value(0).toString().contains("mood"))
moods << query.value(1).toString();
return moods;
}