Add sound manager initial source code
[staging/soundmanager.git] / sample / radio / app / main.cpp
1 /*
2  * Copyright (C) 2016 The Qt Company Ltd.
3  * Copyright (C) 2016, 2017 Konsulko Group
4  * Copyright (C) 2016, 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 "PresetDataObject.h"
28 #include "libsmwrapper.h"
29 #include <json-c/json.h>
30
31 LibSMWrapper* smw;
32
33 static void onRep(struct json_object* reply_contents);
34 static void onEv(const std::string& event, struct json_object* event_contents);
35
36 int main(int argc, char *argv[])
37 {
38   #ifdef HAVE_LIBHOMESCREEN
39     LibHomeScreen libHomeScreen = new LibHomeScreen();
40
41     if (!libHomeScreen.renderAppToAreaAllowed(0, 1)) {
42         qWarning() << "renderAppToAreaAllowed is denied";
43         return -1;
44     }
45 #endif   
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     qDebug("started libsoundmanger");
59     // Read presets from configuration file
60     //
61     // If HOME is set, use $HOME/app-data/radio/presets.conf, else fall back
62     // to the QSettings default locations with organization "AGL" and a
63     // file name of radio-presets.conf. See:
64     //
65     // http://doc.qt.io/qt-5/qsettings.html#platform-specific-notes
66     //
67     // for details on the locations and their order of priority.
68     //
69     QSettings *pSettings = NULL;
70     char *p = getenv("HOME");
71     if(p) {
72         QString confPath = p;
73         confPath.append("/app-data/radio/presets.conf");
74         pSettings = new QSettings(confPath, QSettings::NativeFormat);
75     } else {
76         pSettings = new QSettings("AGL", "radio-presets");
77     }
78     QList<QObject*> presetDataList;
79     int size = pSettings->beginReadArray("fmPresets");
80     for (int i = 0; i < size; ++i) {
81         pSettings->setArrayIndex(i);
82         presetDataList.append(new PresetDataObject(pSettings->value("title").toString(),
83                                                    pSettings->value("frequency").toInt(),
84                                                    1));
85     }
86     pSettings->endArray();
87
88     QQmlApplicationEngine engine;
89     QQmlContext *context = engine.rootContext();
90     context->setContextProperty("presetModel", QVariant::fromValue(presetDataList));
91     if (positionalArguments.length() == 2) {
92         int port = positionalArguments.takeFirst().toInt();
93         QString secret = positionalArguments.takeFirst();
94         QUrl bindingAddress;
95         bindingAddress.setScheme(QStringLiteral("ws"));
96         bindingAddress.setHost(QStringLiteral("localhost"));
97         bindingAddress.setPort(port);
98         bindingAddress.setPath(QStringLiteral("/api"));
99         QUrlQuery query;
100         query.addQueryItem(QStringLiteral("token"), secret);
101         bindingAddress.setQuery(query);
102         context->setContextProperty(QStringLiteral("bindingAddress"), bindingAddress);
103
104         smw = new LibSMWrapper(port, secret);
105         smw->wrapper_registerCallback(onEv, onRep);
106         smw->subscribe(QString("newMainConnection"));
107         smw->subscribe(QString("mainConnectionStateChanged"));
108         smw->subscribe(QString("removedMainConnection"));
109         smw->subscribe(QString("asyncSetSourceState"));
110         smw->subscribe(QString("asyncConnect"));
111         smw->run_eventloop();
112         engine.rootContext()->setContextProperty("smw",smw);
113     }
114     //qmlRegisterType<LibSMWrapper>("LibSMWrapper",1,0, "LibSMWrapper"); // if you would like to use not in cpp but in QML
115     
116     
117
118     engine.load(QUrl(QStringLiteral("qrc:/Radio.qml")));
119
120     QObject *root = engine.rootObjects().first();
121     QObject::connect(smw, SIGNAL(smEvent(QVariant, QVariant)),
122         root, SLOT(slotEvent(QVariant, QVariant)));
123     QObject::connect(smw, SIGNAL(smReply(QVariant)),
124         root, SLOT(slotReply(QVariant)));
125
126     return app.exec();
127 }
128
129 static void onRep(struct json_object* reply_contents)
130 {
131     qDebug("%s is called", __FUNCTION__);
132     QString str = QString(json_object_get_string(reply_contents));
133     QJsonParseError error;
134     QJsonDocument jdoc = QJsonDocument::fromJson(str.toUtf8(), &error);
135     QJsonObject jobj = jdoc.object();
136
137     smw->emit_reply(jobj);
138     json_object_put(reply_contents);
139 }
140
141 static void onEv(const std::string& event, struct json_object* event_contents)
142 {
143     qDebug("%s is called", __FUNCTION__);
144     const QString event_name = QString(event.c_str());
145     QString str = QString(json_object_get_string(event_contents));
146     QJsonParseError error;
147     QJsonDocument jdoc = QJsonDocument::fromJson(str.toUtf8(), &error);
148     const QJsonObject jobj = jdoc.object();
149     smw->emit_event(event_name, jobj);
150
151     json_object_put(event_contents);
152 }