binding: add initial media binding
[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_LIGHTMEDIASCANNER
33 #include "lightmediascanner.h"
34 #endif
35
36 #ifdef HAVE_DBUS
37 #include "dbus.h"
38 #endif
39
40 #include "playlistwithmetadata.h"
41
42 #ifndef HAVE_LIGHTMEDIASCANNER
43 QVariantList readMusicFile(const QString &path)
44 {
45     QVariantList ret;
46     QDir dir(path);
47     for (const auto &entry : dir.entryList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot, QDir::Name)) {
48         QFileInfo fileInfo(dir.absoluteFilePath(entry));
49         if (fileInfo.isDir()) {
50             ret.append(readMusicFile(fileInfo.absoluteFilePath()));
51         } else if (fileInfo.isFile()) {
52             ret.append(QUrl::fromLocalFile(fileInfo.absoluteFilePath()));
53         }
54     }
55     return ret;
56 }
57 #endif
58
59 int main(int argc, char *argv[])
60 {
61 #ifdef HAVE_LIBHOMESCREEN
62     LibHomeScreen libHomeScreen;
63
64     if (!libHomeScreen.renderAppToAreaAllowed(0, 1)) {
65         qWarning() << "renderAppToAreaAllowed is denied";
66         return -1;
67     }
68 #endif
69
70     QGuiApplication app(argc, argv);
71
72     QQuickStyle::setStyle("AGL");
73
74     qmlRegisterType<PlaylistWithMetadata>("MediaPlayer", 1, 0, "PlaylistWithMetadata");
75
76     QVariantList mediaFiles;
77
78 #ifdef HAVE_LIGHTMEDIASCANNER
79     mediaFiles = LightMediaScanner::processLightMediaScanner();
80 #else
81     QString music;
82
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
91     QCommandLineParser parser;
92     parser.addPositionalArgument("port", app.translate("main", "port for binding"));
93     parser.addPositionalArgument("secret", app.translate("main", "secret for binding"));
94     parser.addHelpOption();
95     parser.addVersionOption();
96     parser.process(app);
97     QStringList positionalArguments = parser.positionalArguments();
98
99     if (positionalArguments.length() == 2) {
100         int port = positionalArguments.takeFirst().toInt();
101         QString secret = positionalArguments.takeFirst();
102         QUrl bindingAddress;
103         bindingAddress.setScheme(QStringLiteral("ws"));
104         bindingAddress.setHost(QStringLiteral("localhost"));
105         bindingAddress.setPort(port);
106         bindingAddress.setPath(QStringLiteral("/api"));
107         QUrlQuery query;
108         query.addQueryItem(QStringLiteral("token"), secret);
109         bindingAddress.setQuery(query);
110         context->setContextProperty(QStringLiteral("bindingAddress"), bindingAddress);
111     }
112
113     context->setContextProperty("mediaFiles", mediaFiles);
114
115 #if defined(HAVE_DBUS)
116     DbusService dbus_service;
117     context->setContextProperty("dbus", &dbus_service);
118 #endif
119
120     engine.load(QUrl(QStringLiteral("qrc:/MediaPlayer.qml")));
121
122 #if defined(HAVE_DBUS)
123 #if defined(HAVE_LIGHTMEDIASCANNER)
124     if (!dbus_service.enableLMS())
125        qWarning() << "Cannot run enableLMS";
126 #endif
127     if (!dbus_service.enableBluetooth())
128        qWarning() << "Cannot run enableBluetooth";
129 #endif
130
131     return app.exec();
132 }