getDevices creates data model for combo box (created from device
[KFingerManager.git] / src / FMDbusConn.cpp
blobbe3dd6c382e7bd1d27897778254ae2595641d0ba
1 #include <KApplication>
3 #include <QDBusConnectionInterface>
4 #include <QComboBox>
5 #include <QtCore>
6 #include <QObject>
8 #include "FMDbusConn.h"
9 #include "FingerManagerDevicesModel.h"
11 FMDbusConn::FMDbusConn() : managerIface(0) {
12 if (!QDBusConnection::systemBus().isConnected()) {
13 qDebug("Cannot connect to D-Bus system bus.");
14 return;
16 deviceResultsWatcher = new QFutureWatcher<FingerManagerDevicesModel*>(this);
17 connect(deviceResultsWatcher, SIGNAL(resultReadyAt(int)), SLOT(emitDiscovered(int)));
20 FMDbusConn::~FMDbusConn() {
21 delete managerIface;
24 /**
25 * Start D-Bus daemon (requires some retries)
27 QDBusInterface* FMDbusConn::getManagerIface(int retr = INTERFACE_ACQUIRE_RETRY) {
28 if (!retr) {
29 qDebug("Manager interface doesn't found!");
30 return(0);
33 if (managerIface && managerIface->isValid()) {
34 qDebug("Returning existing manager interface");
35 return managerIface;
38 qDebug("getManagerConnection try: %d", retr);
40 // Connect to Fprint manager on system bus
41 QDBusConnection bus = QDBusConnection::systemBus();
42 QDBusInterface *iface = new QDBusInterface("net.reactivated.Fprint",
43 "/net/reactivated/Fprint/Manager",
44 "net.reactivated.Fprint.Manager",
45 bus, this);
47 // Wait while D-Bus Fprintd daemon starts
48 if (iface->isValid()) {
49 managerIface = iface;
50 } else {
51 managerIface = getManagerIface(--retr);
53 return(managerIface);
56 /**
57 * QFutureWatcher signal handler
59 void FMDbusConn::emitDiscovered(int idx) {
60 emit devicesDiscovered(idx);
63 /**
64 * Returns FingerManagerDevicesModel created by getDevices
66 FingerManagerDevicesModel* FMDbusConn::getModel(int idx) {
67 return(deviceResultsWatcher->resultAt(idx));
70 /**
71 * Get device communication interface
73 QDBusInterface* getDeviceInterface(QString device) {
74 QDBusInterface* i = new QDBusInterface("net.reactivated.Fprint",
75 device,
76 "net.reactivated.Fprint.Device",
77 QDBusConnection::systemBus());
78 return(i);
81 /**
82 * Returns name associated with device
84 QString getDeviceName(QString device) {
85 QString name;
86 QDBusInterface *i = getDeviceInterface(device);
87 if (i->isValid()) {
88 name = i->property("name").toString();
89 } else {
90 name = device;
92 delete(i);
93 return(name);
96 /**
97 * Real function which do all the work
99 FingerManagerDevicesModel* getDevicesReal(QDBusInterface *iface) {
100 FingerManagerDevicesModel *model = new FingerManagerDevicesModel();
102 if (iface && iface->isValid()) {
104 qDebug("D-Bus interface acquired! :-)");
105 QDBusReply<QList<QDBusObjectPath> > devices = iface->call("GetDevices");
106 if (devices.isValid()) {
107 foreach (QDBusObjectPath device, devices.value()) {
108 model->addDevice(device.path(), getDeviceName(device.path()));
112 QDBusReply<QDBusObjectPath> defaultDevice = iface->call("GetDefaultDevice");
113 if (defaultDevice.isValid()) {
114 model->setDefault(defaultDevice.value().path());
116 } else {
117 qDebug("D-Bus interface cannot be acquired! :-(");
120 return(model);
124 * Starts thread
126 void FMDbusConn::getDevices() {
127 QDBusInterface *iface = getManagerIface();
128 deviceResultsWatcher->setFuture(QtConcurrent::run(getDevicesReal, iface));
131 void FMDbusConn::getEnrolledFingers(QDBusObjectPath device) {
132 Q_UNUSED(device);