Retrieve enrolled fingers on selected device and set checked
[KFingerManager.git] / src / FMDbusConn.cpp
blob290ee9e5d746c80a4522fe3177cb79df36db23b6
1 #include <KApplication>
2 #include <QDBusConnectionInterface>
3 #include <QComboBox>
4 #include <QtCore>
5 #include <QObject>
7 #include <unistd.h>
9 #include "FMDbusConn.h"
10 #include "FingerManagerDevicesModel.h"
12 /**
13 * FMDbusConn constructor
15 FMDbusConn::FMDbusConn() : managerIface(0) {
16 if (!QDBusConnection::systemBus().isConnected()) {
17 qDebug("Cannot connect to D-Bus system bus.");
19 deviceResultsWatcher = new QFutureWatcher<FingerManagerDevicesModel*>(this);
20 fingersResultsWatcher = new QFutureWatcher<QStringList>(this);
21 connect(deviceResultsWatcher, SIGNAL(resultReadyAt(int)), SLOT(emitDiscovered(int)));
22 connect(fingersResultsWatcher, SIGNAL(resultReadyAt(int)), SLOT(fingersDiscovered(int)));
25 /**
26 * FMDbusConn destructor
28 FMDbusConn::~FMDbusConn() {
29 delete managerIface;
30 delete deviceResultsWatcher;
31 delete fingersResultsWatcher;
34 /**
35 * Start D-Bus daemon (requires some retries)
37 QDBusInterface* FMDbusConn::getManagerIface(int retr = INTERFACE_ACQUIRE_RETRY) {
38 if (!retr) {
39 qDebug("Manager interface doesn't found!");
40 return(0);
43 if (managerIface && managerIface->isValid()) {
44 qDebug("Returning existing manager interface");
45 return managerIface;
48 qDebug("getManagerConnection try: %d", retr);
50 // Connect to Fprint manager on system bus
51 QDBusConnection bus = QDBusConnection::systemBus();
52 QDBusInterface *iface = new QDBusInterface("net.reactivated.Fprint",
53 "/net/reactivated/Fprint/Manager",
54 "net.reactivated.Fprint.Manager",
55 bus, this);
57 // Wait while D-Bus Fprintd daemon starts
58 if (iface->isValid()) {
59 managerIface = iface;
60 } else {
61 managerIface = getManagerIface(--retr);
63 return(managerIface);
66 /**
67 * QFutureWatcher signal handler
69 void FMDbusConn::emitDiscovered(int idx) {
70 emit devicesDiscovered(idx);
73 void FMDbusConn::fingersDiscovered(int idx) {
74 emit enrolledFingersDiscovered(idx);
77 /**
78 * Returns FingerManagerDevicesModel created by getDevices
80 FingerManagerDevicesModel* FMDbusConn::getModel(int idx) {
81 return(deviceResultsWatcher->resultAt(idx));
84 /**
85 * Get device communication interface
87 QDBusInterface* getDeviceInterface(QString device) {
88 QDBusInterface* i = new QDBusInterface("net.reactivated.Fprint",
89 device,
90 "net.reactivated.Fprint.Device",
91 QDBusConnection::systemBus());
92 return(i);
95 /**
96 * Returns name associated with device
98 QString getDeviceName(QString device) {
99 QString name;
100 QDBusInterface *i = getDeviceInterface(device);
101 if (i->isValid()) {
102 name = i->property("name").toString();
103 } else {
104 name = device;
106 delete(i);
107 return(name);
111 * Real function which do all the work
113 FingerManagerDevicesModel* getDevicesReal(QDBusInterface *iface) {
114 FingerManagerDevicesModel *model = new FingerManagerDevicesModel();
116 if (iface && iface->isValid()) {
118 qDebug("D-Bus interface acquired! :-)");
119 QDBusReply<QList<QDBusObjectPath> > devices = iface->call("GetDevices");
120 if (devices.isValid()) {
121 foreach (QDBusObjectPath device, devices.value()) {
122 model->addDevice(device.path(), getDeviceName(device.path()));
126 QDBusReply<QDBusObjectPath> defaultDevice = iface->call("GetDefaultDevice");
127 if (defaultDevice.isValid()) {
128 model->setDefault(defaultDevice.value().path());
130 } else {
131 qDebug("D-Bus interface cannot be acquired! :-(");
133 return(model);
137 * Starts thread
139 void FMDbusConn::getDevices() {
140 QDBusInterface *iface = getManagerIface();
141 deviceResultsWatcher->setFuture(QtConcurrent::run(getDevicesReal, iface));
145 * Returns QStringList returned by D-Bus daemon
147 QStringList FMDbusConn::getFingers(int idx) {
148 return(fingersResultsWatcher->resultAt(idx));
152 * Real function which do all the work
154 QStringList getEnrolledFingersReal(QDBusInterface *iface) {
155 QStringList result;
156 if (iface && iface->isValid()) {
157 QDBusReply<QStringList> r = iface->call("ListEnrolledFingers", getlogin());
158 if (r.isValid()) {
159 result = r.value();
160 } else {
161 QDBusError e = iface->lastError();
162 qDebug() << e.message();
165 return(result);
169 * Starts thread
171 void FMDbusConn::getEnrolledFingers(QString device) {
172 QDBusInterface *iface = getDeviceInterface(device);
173 fingersResultsWatcher->setFuture(QtConcurrent::run(getEnrolledFingersReal, iface));