Add initial support for lightmediascanner
[apps/mediaplayer.git] / app / main.cpp
1 /*
2  * Copyright (C) 2016 The Qt Company Ltd.
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 <QtCore/QDebug>
18 #include <QtCore/QDir>
19 #include <QtCore/QStandardPaths>
20 #include <QtGui/QGuiApplication>
21 #include <QtQml/QQmlApplicationEngine>
22 #include <QtQml/QQmlContext>
23 #include <QtQml/qqml.h>
24 #include <QtQuickControls2/QQuickStyle>
25
26 #ifdef HAVE_LIBHOMESCREEN
27 #include <libhomescreen.hpp>
28 #endif
29
30 #ifdef HAVE_LIGHTMEDIASCANNER
31 #include "lightmediascanner.h"
32 #endif
33
34 #include "playlistwithmetadata.h"
35
36 #ifndef HAVE_LIGHTMEDIASCANNER
37 QVariantList readMusicFile(const QString &path)
38 {
39     QVariantList ret;
40     QDir dir(path);
41     for (const auto &entry : dir.entryList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot, QDir::Name)) {
42         QFileInfo fileInfo(dir.absoluteFilePath(entry));
43         if (fileInfo.isDir()) {
44             ret.append(readMusicFile(fileInfo.absoluteFilePath()));
45         } else if (fileInfo.isFile()) {
46             ret.append(QUrl::fromLocalFile(fileInfo.absoluteFilePath()));
47         }
48     }
49     return ret;
50 }
51 #endif
52
53 int main(int argc, char *argv[])
54 {
55 #ifdef HAVE_LIBHOMESCREEN
56     LibHomeScreen libHomeScreen;
57
58     if (!libHomeScreen.renderAppToAreaAllowed(0, 1)) {
59         qWarning() << "renderAppToAreaAllowed is denied";
60         return -1;
61     }
62 #endif
63
64     QGuiApplication app(argc, argv);
65
66     QQuickStyle::setStyle("AGL");
67
68     qmlRegisterType<PlaylistWithMetadata>("MediaPlayer", 1, 0, "PlaylistWithMetadata");
69
70     QVariantList mediaFiles;
71     QString music;
72
73 #ifdef HAVE_LIGHTMEDIASCANNER
74     LightMediaScanner scanner(QDir::homePath() + "/.config/lightmediascannerd/db.sqlite3");
75     while (scanner.next(music)) {
76         QFileInfo fileInfo(music);
77         // Possible for stale entries due to removable media
78         if (!fileInfo.exists())
79             continue;
80         mediaFiles.append(QUrl::fromLocalFile(music));
81     }
82 #else
83     for (const auto &music : QStandardPaths::standardLocations(QStandardPaths::MusicLocation)) {
84         mediaFiles.append(readMusicFile(music));
85     }
86 #endif
87
88     QQmlApplicationEngine engine;
89     QQmlContext *context = engine.rootContext();
90     context->setContextProperty("mediaFiles", mediaFiles);
91     engine.load(QUrl(QStringLiteral("qrc:/MediaPlayer.qml")));
92
93     return app.exec();
94 }
95