2
* Copyright 2017 Canonical Ltd
4
* Libertine is free software: you can redistribute it and/or modify it under
5
* the terms of the GNU General Public License, version 3, as published by the
6
* Free Software Foundation.
8
* Libertine is distributed in the hope that it will be useful, but WITHOUT ANY
9
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
10
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12
* You should have received a copy of the GNU General Public License
13
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17
#include "libertined.h"
19
#include <QDBusMessage>
20
#include <QDBusInterface>
24
#include <QJsonDocument>
26
#include <QJsonObject>
30
static const auto SERVICE_INTERFACE = "com.canonical.libertine.Service";
31
static const auto PROGRESS_INTERFACE = "com.canonical.libertine.Service.Progress";
34
dbusCall(QDBusConnection const& bus, QString const& iface, QString const& path,
35
QString const& method, QVariantList const& args = QVariantList())
37
auto message = QDBusMessage::createMethodCall(SERVICE_INTERFACE, path, iface, method);
38
message.setArguments(args);
39
auto response = bus.call(message);
40
if (response.type() == QDBusMessage::ErrorMessage)
42
qWarning() << "error calling result" << response.errorMessage();
43
return QVariantList();
46
return response.arguments();
50
isRunning(QDBusConnection const& bus, QString const& path)
52
auto args = dbusCall(bus, PROGRESS_INTERFACE, path, "running", QVariantList());
56
qWarning() << "lastError - no arguments?";
60
return args.first().toBool();
64
result(QDBusConnection const& bus, QString const& path)
66
auto args = dbusCall(bus, PROGRESS_INTERFACE, path, "result");
70
qWarning() << "lastError - no arguments?";
74
return args.first().toString();
78
lastError(QDBusConnection const& bus, QString const& path)
80
auto args = dbusCall(bus, PROGRESS_INTERFACE, path, "last_error");
84
qWarning() << "lastError - no arguments?";
88
return args.first().toString();
92
call(QDBusConnection const& bus, QString const& method, QVariantList const& args)
94
auto results = dbusCall(bus, SERVICE_INTERFACE, "/Manager", method, args);
96
if (results.isEmpty())
101
return qvariant_cast<QDBusObjectPath>(results.first()).path();
105
waitForFinish(QDBusConnection const& bus, QString const& path)
107
std::chrono::microseconds wait(500);
108
for (auto i = 0; i < 2000; ++i)
110
if (!isRunning(bus, path))
114
std::this_thread::sleep_for(wait);
116
return !isRunning(bus, path);
120
container_info(char const* container_id, QString const& key)
122
auto bus = QDBusConnection::sessionBus();
123
auto path = call(bus, "container_info", QVariantList{QVariant(container_id)});
125
if (!waitForFinish(bus, path))
130
auto error = lastError(bus, path);
131
if (!error.isEmpty())
133
qWarning() << "error:" << error;
137
return QJsonDocument::fromJson(result(bus, path).toLatin1()).object().value(key).toString();
144
auto bus = QDBusConnection::sessionBus();
145
auto path = call(bus, "list", QVariantList());
147
if (!waitForFinish(bus, path))
152
auto error = lastError(bus, path);
153
if (!error.isEmpty())
155
qWarning() << "error:" << error;
159
return QJsonDocument::fromJson(result(bus, path).toLatin1()).array();
163
libertined_list_app_ids(char const* container_id)
165
auto bus = QDBusConnection::sessionBus();
166
auto path = call(bus, "list_app_ids", QVariantList{QVariant(container_id)});
168
if (!waitForFinish(bus, path))
173
auto error = lastError(bus, path);
174
if (!error.isEmpty())
176
qWarning() << "error:" << error;
180
return QJsonDocument::fromJson(result(bus, path).toLatin1()).array();
184
libertined_container_path(char const* container_id)
186
return container_info(container_id, "root");
190
libertined_container_home_path(char const* container_id)
192
return container_info(container_id, "home");
196
libertined_container_name(char const* container_id)
198
return container_info(container_id, "name");