File: DBusQuery.cpp

package info (click to toggle)
libusermetrics 1.3.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,324 kB
  • sloc: cpp: 7,182; xml: 131; sh: 106; ansic: 60; makefile: 26
file content (77 lines) | stat: -rw-r--r-- 2,581 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
 * Copyright (C) 2013 Canonical, Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <DBusQuery.h>
#include <MetricInfo.h>

#include <cmath>
#include <QtDBus/QtDBus>
#include <libqtdbustest/QProcessDBusService.h>
#include <libqtdbustest/DBusService.h>
#include <libusermetricscommon/UserMetricsInterface.h>
#include <libusermetricscommon/UserDataInterface.h>
#include <libusermetricscommon/DataSetInterface.h>
#include <libusermetricscommon/DataSourceInterface.h>
#include <libusermetricscommon/DBusPaths.h>

using namespace QtDBusTest;
using namespace UserMetricsCommon;

DBusQuery::DBusQuery(QObject *parent) :
		QObject(parent), dbus() {
	qputenv("USERMETRICS_NO_AUTH", "1");
	qputenv("USERMETRICS_NO_COLOR_SETTINGS", "1");
	qputenv("XDG_DATA_DIRS", DATA_DIR);
	DBusServicePtr userMetricsService(
			new QProcessDBusService("com.lomiri.UserMetrics",
					QDBusConnection::SystemBus, USERMETRICSSERVICE_BINARY,
					QStringList() << ":memory:"));
	dbus.registerService(userMetricsService);
	dbus.startServices();
}

double DBusQuery::queryCurrentValue(int index) {
	com::lomiri::usermetrics::DataSet dataSetInterface(
			DBusPaths::serviceName(), DBusPaths::dataSet(index),
			dbus.systemConnection());
	QVariantList data = dataSetInterface.data();
	return data.at(0).toDouble();
}

MetricInfo* DBusQuery::queryMetricInfo(int index) {
	com::lomiri::usermetrics::DataSource dataSourceInterface(
			DBusPaths::serviceName(), DBusPaths::dataSource(index),
			dbus.systemConnection());
	if (dataSourceInterface.name().isEmpty()) {
		return 0;
	} else {
		double minimum(NAN);
		double maximum(NAN);
		QVariantMap options(dataSourceInterface.options());
		if (options.contains("minimum")) {
			minimum = options["minimum"].toDouble();
		}
		if (options.contains("maximum")) {
			maximum = options["maximum"].toDouble();
		}

		return new MetricInfo(dataSourceInterface.name(),
				dataSourceInterface.formatString(),
				dataSourceInterface.emptyDataString(),
				dataSourceInterface.textDomain(), minimum, maximum, this);
	}
}