binding: add initial media binding
[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 LightMediaScanner::~LightMediaScanner()
35 {
36     lms.close();
37     QSqlDatabase::removeDatabase(lms.connectionName());
38 }
39
40 bool LightMediaScanner::next(QString& item)
41 {
42     if (!query.next())
43         return false;
44
45     item = query.value(0).toString();
46
47     return true;
48 }
49
50 QVariantList LightMediaScanner::processLightMediaScanner()
51 {
52     QVariantList mediaFiles;
53     QString music;
54     LightMediaScanner scanner(QDir::homePath() + "/.config/lightmediascannerd/db.sqlite3");
55     while (scanner.next(music)) {
56         QFileInfo fileInfo(music);
57         // Possible for stale entries due to removable media
58         if (!fileInfo.exists())
59             continue;
60         mediaFiles.append(QUrl::fromLocalFile(music));
61     }
62     return mediaFiles;
63 }