2 * Copyright (C) 2016 The Qt Company Ltd.
3 * Copyright (C) 2016, 2017 Konsulko Group
4 * Copyright (C) 2016, 2017 Toyota Motor Corporation
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
33 static void onRep(struct json_object* reply_contents);
34 static void onEv(const std::string& event, struct json_object* event_contents);
36 int main(int argc, char *argv[])
38 #ifdef HAVE_LIBHOMESCREEN
39 LibHomeScreen libHomeScreen = new LibHomeScreen();
41 if (!libHomeScreen.renderAppToAreaAllowed(0, 1)) {
42 qWarning() << "renderAppToAreaAllowed is denied";
46 QGuiApplication app(argc, argv);
48 QQuickStyle::setStyle("AGL");
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();
56 QStringList positionalArguments = parser.positionalArguments();
58 qDebug("started libsoundmanger");
59 // Read presets from configuration file
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:
65 // http://doc.qt.io/qt-5/qsettings.html#platform-specific-notes
67 // for details on the locations and their order of priority.
69 QSettings *pSettings = NULL;
70 char *p = getenv("HOME");
73 confPath.append("/app-data/radio/presets.conf");
74 pSettings = new QSettings(confPath, QSettings::NativeFormat);
76 pSettings = new QSettings("AGL", "radio-presets");
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(),
86 pSettings->endArray();
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();
95 bindingAddress.setScheme(QStringLiteral("ws"));
96 bindingAddress.setHost(QStringLiteral("localhost"));
97 bindingAddress.setPort(port);
98 bindingAddress.setPath(QStringLiteral("/api"));
100 query.addQueryItem(QStringLiteral("token"), secret);
101 bindingAddress.setQuery(query);
102 context->setContextProperty(QStringLiteral("bindingAddress"), bindingAddress);
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);
114 //qmlRegisterType<LibSMWrapper>("LibSMWrapper",1,0, "LibSMWrapper"); // if you would like to use not in cpp but in QML
118 engine.load(QUrl(QStringLiteral("qrc:/Radio.qml")));
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)));
129 static void onRep(struct json_object* reply_contents)
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();
137 smw->emit_reply(jobj);
138 json_object_put(reply_contents);
141 static void onEv(const std::string& event, struct json_object* event_contents)
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);
151 json_object_put(event_contents);