Add sound manager initial source code
[staging/soundmanager.git] / sample / radio_qml / app / main.cpp
1 /*
2  * Copyright (C) 2016 The Qt Company Ltd.
3  * Copyright (C) 2016, 2017 Konsulko Group
4  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #include <QtCore/QDebug>
20 #include <QtCore/QCommandLineParser>
21 #include <QtCore/QUrlQuery>
22 #include <QtCore/QSettings>
23 #include <QtGui/QGuiApplication>
24 #include <QtQml/QQmlApplicationEngine>
25 #include <QtQml/QQmlContext>
26 #include <QtQuickControls2/QQuickStyle>
27 #include <stdlib.h>
28 #include "PresetDataObject.h"
29
30
31 #ifdef HAVE_LIBHOMESCREEN
32 #include <libhomescreen.hpp>
33 #endif
34
35 int main(int argc, char *argv[])
36 {
37 #ifdef HAVE_LIBHOMESCREEN
38     LibHomeScreen libHomeScreen;
39
40     if (!libHomeScreen.renderAppToAreaAllowed(0, 1)) {
41         qWarning() << "renderAppToAreaAllowed is denied";
42         return -1;
43     }
44 #endif
45
46     QGuiApplication app(argc, argv);
47
48     QQuickStyle::setStyle("AGL");
49
50     QCommandLineParser parser;
51     parser.addPositionalArgument("port", app.translate("main", "port for binding"));
52     parser.addPositionalArgument("secret", app.translate("main", "secret for binding"));
53     parser.addHelpOption();
54     parser.addVersionOption();
55     parser.process(app);
56     QStringList positionalArguments = parser.positionalArguments();
57
58     // Read presets from configuration file
59     //
60     // If HOME is set, use $HOME/app-data/radio/presets.conf, else fall back
61     // to the QSettings default locations with organization "AGL" and a
62     // file name of radio-presets.conf. See:
63     //
64     // http://doc.qt.io/qt-5/qsettings.html#platform-specific-notes
65     //
66     // for details on the locations and their order of priority.
67     //
68     QSettings *pSettings = NULL;
69     char *p = getenv("HOME");
70     if(p) {
71         QString confPath = p;
72         confPath.append("/app-data/radio/presets.conf");
73         pSettings = new QSettings(confPath, QSettings::NativeFormat);
74     } else {
75         pSettings = new QSettings("AGL", "radio-presets");
76     }
77     QList<QObject*> presetDataList;
78     int size = pSettings->beginReadArray("fmPresets");
79     for (int i = 0; i < size; ++i) {
80         pSettings->setArrayIndex(i);
81         presetDataList.append(new PresetDataObject(pSettings->value("title").toString(),
82                                                    pSettings->value("frequency").toInt(),
83                                                    1));
84     }
85     pSettings->endArray();
86
87     QQmlApplicationEngine engine;
88     QQmlContext *context = engine.rootContext();
89     context->setContextProperty("presetModel", QVariant::fromValue(presetDataList));
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
102         QUrl bindingAddressSM;
103         bindingAddressSM.setScheme(QStringLiteral("ws"));
104         bindingAddressSM.setHost(QStringLiteral("localhost"));
105         bindingAddressSM.setPort(port);
106         bindingAddressSM.setPath(QStringLiteral("/api"));
107         bindingAddressSM.setQuery(query);
108
109         context->setContextProperty(QStringLiteral("bindingAddress"), bindingAddress);
110         context->setContextProperty(QStringLiteral("bindingAddressSM"), bindingAddressSM);
111     }
112     engine.load(QUrl(QStringLiteral("qrc:/Radio.qml")));
113
114     return app.exec();
115 }