dbus: add signal support for removable media
[apps/mediaplayer.git] / app / lightmediascanner.cpp
1 /*
2  * Copyright (C) 2017 Konsulko Group
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "lightmediascanner.h"
18
19 LightMediaScanner::LightMediaScanner(const QString& path)
20 {
21     lms = QSqlDatabase::addDatabase("QSQLITE");
22     lms.setDatabaseName(path);
23
24     if (!lms.open()) {
25         qDebug() << "Cannot open database: " << path;
26     } else {
27         query = QSqlQuery(lms);
28         query.prepare("SELECT files.path FROM files LEFT JOIN audios WHERE audios.id = files.id ORDER BY audios.artist_id, audios.album_id, audios.trackno");
29         if (!query.exec())
30             qDebug() << "Cannot run SQL query";
31     }
32 }
33
34 bool LightMediaScanner::next(QString& item)
35 {
36     if (!query.next())
37         return false;
38
39     item = query.value(0).toString();
40
41     return true;
42 }
43
44 QVariantList LightMediaScanner::processLightMediaScanner()
45 {
46     QVariantList mediaFiles;
47     QString music;
48     LightMediaScanner scanner(QDir::homePath() + "/.config/lightmediascannerd/db.sqlite3");
49     while (scanner.next(music)) {
50         QFileInfo fileInfo(music);
51         // Possible for stale entries due to removable media
52         if (!fileInfo.exists())
53             continue;
54         mediaFiles.append(QUrl::fromLocalFile(music));
55     }
56     return mediaFiles;
57 }