binding: remove non-binding lightmediascanner detection
[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/QCommandLineParser>
18 #include <QtCore/QDebug>
19 #include <QtCore/QDir>
20 #include <QtCore/QStandardPaths>
21 #include <QtCore/QUrlQuery>
22 #include <QtGui/QGuiApplication>
23 #include <QtQml/QQmlApplicationEngine>
24 #include <QtQml/QQmlContext>
25 #include <QtQml/qqml.h>
26 #include <QtQuickControls2/QQuickStyle>
27
28 #ifdef HAVE_LIBHOMESCREEN
29 #include <libhomescreen.hpp>
30 #endif
31
32 #ifdef HAVE_DBUS
33 #include "dbus.h"
34 #endif
35
36 #include "playlistwithmetadata.h"
37
38 #ifndef HAVE_LIGHTMEDIASCANNER
39 QVariantList readMusicFile(const QString &path)
40 {
41     QVariantList ret;
42     QDir dir(path);
43     for (const auto &entry : dir.entryList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot, QDir::Name)) {
44         QFileInfo fileInfo(dir.absoluteFilePath(entry));
45         if (fileInfo.isDir()) {
46             ret.append(readMusicFile(fileInfo.absoluteFilePath()));
47         } else if (fileInfo.isFile()) {
48             ret.append(QUrl::fromLocalFile(fileInfo.absoluteFilePath()));
49         }
50     }
51     return ret;
52 }
53 #endif
54
55 int main(int argc, char *argv[])
56 {
57 #ifdef HAVE_LIBHOMESCREEN
58     LibHomeScreen libHomeScreen;
59
60     if (!libHomeScreen.renderAppToAreaAllowed(0, 1)) {
61         qWarning() << "renderAppToAreaAllowed is denied";
62         return -1;
63     }
64 #endif
65
66     QGuiApplication app(argc, argv);
67
68     QQuickStyle::setStyle("AGL");
69
70     qmlRegisterType<PlaylistWithMetadata>("MediaPlayer", 1, 0, "PlaylistWithMetadata");
71
72     QQmlApplicationEngine engine;
73     QQmlContext *context = engine.rootContext();
74
75 #ifndef HAVE_LIGHTMEDIASCANNER
76     QVariantList mediaFiles;
77     QString music;
78
79     for (const auto &music : QStandardPaths::standardLocations(QStandardPaths::MusicLocation)) {
80         mediaFiles.append(readMusicFile(music));
81     }
82
83     context->setContextProperty("mediaFiles", mediaFiles);
84 #endif
85
86     QCommandLineParser parser;
87     parser.addPositionalArgument("port", app.translate("main", "port for binding"));
88     parser.addPositionalArgument("secret", app.translate("main", "secret for binding"));
89     parser.addHelpOption();
90     parser.addVersionOption();
91     parser.process(app);
92     QStringList positionalArguments = parser.positionalArguments();
93
94     if (positionalArguments.length() == 2) {
95         int port = positionalArguments.takeFirst().toInt();
96         QString secret = positionalArguments.takeFirst();
97         QUrl bindingAddress;
98         bindingAddress.setScheme(QStringLiteral("ws"));
99         bindingAddress.setHost(QStringLiteral("localhost"));
100         bindingAddress.setPort(port);
101         bindingAddress.setPath(QStringLiteral("/api"));
102         QUrlQuery query;
103         query.addQueryItem(QStringLiteral("token"), secret);
104         bindingAddress.setQuery(query);
105         context->setContextProperty(QStringLiteral("bindingAddress"), bindingAddress);
106     }
107
108 #if defined(HAVE_DBUS)
109     DbusService dbus_service;
110     context->setContextProperty("dbus", &dbus_service);
111 #endif
112
113     engine.load(QUrl(QStringLiteral("qrc:/MediaPlayer.qml")));
114
115 #if defined(HAVE_DBUS)
116     if (!dbus_service.enableBluetooth())
117        qWarning() << "Cannot run enableBluetooth";
118 #endif
119
120     return app.exec();
121 }