binding: bluetooth: switch from dbus calls to system 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 #include "playlistwithmetadata.h"
33
34 #ifndef HAVE_LIGHTMEDIASCANNER
35 QVariantList readMusicFile(const QString &path)
36 {
37     QVariantList ret;
38     QDir dir(path);
39     for (const auto &entry : dir.entryList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot, QDir::Name)) {
40         QFileInfo fileInfo(dir.absoluteFilePath(entry));
41         if (fileInfo.isDir()) {
42             ret.append(readMusicFile(fileInfo.absoluteFilePath()));
43         } else if (fileInfo.isFile()) {
44             ret.append(QUrl::fromLocalFile(fileInfo.absoluteFilePath()));
45         }
46     }
47     return ret;
48 }
49 #endif
50
51 int main(int argc, char *argv[])
52 {
53 #ifdef HAVE_LIBHOMESCREEN
54     LibHomeScreen libHomeScreen;
55
56     if (!libHomeScreen.renderAppToAreaAllowed(0, 1)) {
57         qWarning() << "renderAppToAreaAllowed is denied";
58         return -1;
59     }
60 #endif
61
62     QGuiApplication app(argc, argv);
63
64     QQuickStyle::setStyle("AGL");
65
66     qmlRegisterType<PlaylistWithMetadata>("MediaPlayer", 1, 0, "PlaylistWithMetadata");
67
68     QQmlApplicationEngine engine;
69     QQmlContext *context = engine.rootContext();
70
71 #ifndef HAVE_LIGHTMEDIASCANNER
72     QVariantList mediaFiles;
73     QString music;
74
75     for (const auto &music : QStandardPaths::standardLocations(QStandardPaths::MusicLocation)) {
76         mediaFiles.append(readMusicFile(music));
77     }
78
79     context->setContextProperty("mediaFiles", mediaFiles);
80 #endif
81
82     QCommandLineParser parser;
83     parser.addPositionalArgument("port", app.translate("main", "port for binding"));
84     parser.addPositionalArgument("secret", app.translate("main", "secret for binding"));
85     parser.addHelpOption();
86     parser.addVersionOption();
87     parser.process(app);
88     QStringList positionalArguments = parser.positionalArguments();
89
90     if (positionalArguments.length() == 2) {
91         int port = positionalArguments.takeFirst().toInt();
92         QString secret = positionalArguments.takeFirst();
93         QUrl bindingAddress;
94         bindingAddress.setScheme(QStringLiteral("ws"));
95         bindingAddress.setHost(QStringLiteral("localhost"));
96         bindingAddress.setPort(port);
97         bindingAddress.setPath(QStringLiteral("/api"));
98         QUrlQuery query;
99         query.addQueryItem(QStringLiteral("token"), secret);
100         bindingAddress.setQuery(query);
101         context->setContextProperty(QStringLiteral("bindingAddress"), bindingAddress);
102     }
103
104     engine.load(QUrl(QStringLiteral("qrc:/MediaPlayer.qml")));
105
106     return app.exec();
107 }