12654ada8fe0a1d6e0bdfd61e839f27ba32d4075
[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 #include "playlistwithmetadata.h"
31
32 QVariantList readMusicFile(const QString &path)
33 {
34     QVariantList ret;
35     QDir dir(path);
36     for (const auto &entry : dir.entryList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot, QDir::Name)) {
37         QFileInfo fileInfo(dir.absoluteFilePath(entry));
38         if (fileInfo.isDir()) {
39             ret.append(readMusicFile(fileInfo.absoluteFilePath()));
40         } else if (fileInfo.isFile()) {
41             ret.append(QUrl::fromLocalFile(fileInfo.absoluteFilePath()));
42         }
43     }
44     return ret;
45 }
46
47 int main(int argc, char *argv[])
48 {
49 #ifdef HAVE_LIBHOMESCREEN
50     LibHomeScreen libHomeScreen;
51
52     if (!libHomeScreen.renderAppToAreaAllowed(0, 1)) {
53         qWarning() << "renderAppToAreaAllowed is denied";
54         return -1;
55     }
56 #endif
57
58     QGuiApplication app(argc, argv);
59
60     QQuickStyle::setStyle("AGL");
61
62     qmlRegisterType<PlaylistWithMetadata>("MediaPlayer", 1, 0, "PlaylistWithMetadata");
63
64     QVariantList mediaFiles;
65     for (const auto &music : QStandardPaths::standardLocations(QStandardPaths::MusicLocation)) {
66         mediaFiles.append(readMusicFile(music));
67     }
68
69     QQmlApplicationEngine engine;
70     QQmlContext *context = engine.rootContext();
71     context->setContextProperty("mediaFiles", mediaFiles);
72     engine.load(QUrl(QStringLiteral("qrc:/MediaPlayer.qml")));
73
74     return app.exec();
75 }
76