1 #include <KApplication>
2 #include <QDBusConnectionInterface>
9 #include "FMDbusConn.h"
10 #include "FingerManagerDevicesModel.h"
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)));
26 * FMDbusConn destructor
28 FMDbusConn::~FMDbusConn() {
30 delete deviceResultsWatcher
;
31 delete fingersResultsWatcher
;
35 * Start D-Bus daemon (requires some retries)
37 QDBusInterface
* FMDbusConn::getManagerIface(int retr
= INTERFACE_ACQUIRE_RETRY
) {
39 qDebug("Manager interface doesn't found!");
43 if (managerIface
&& managerIface
->isValid()) {
44 qDebug("Returning existing manager interface");
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",
57 // Wait while D-Bus Fprintd daemon starts
58 if (iface
->isValid()) {
61 managerIface
= getManagerIface(--retr
);
67 * QFutureWatcher signal handler
69 void FMDbusConn::emitDiscovered(int idx
) {
70 emit
devicesDiscovered(idx
);
73 void FMDbusConn::fingersDiscovered(int idx
) {
74 emit
enrolledFingersDiscovered(idx
);
78 * Returns FingerManagerDevicesModel created by getDevices
80 FingerManagerDevicesModel
* FMDbusConn::getModel(int idx
) {
81 return(deviceResultsWatcher
->resultAt(idx
));
85 * Get device communication interface
87 QDBusInterface
* getDeviceInterface(QString device
) {
88 QDBusInterface
* i
= new QDBusInterface("net.reactivated.Fprint",
90 "net.reactivated.Fprint.Device",
91 QDBusConnection::systemBus());
96 * Returns name associated with device
98 QString
getDeviceName(QString device
) {
100 QDBusInterface
*i
= getDeviceInterface(device
);
102 name
= i
->property("name").toString();
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());
131 qDebug("D-Bus interface cannot be acquired! :-(");
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
) {
156 if (iface
&& iface
->isValid()) {
157 QDBusReply
<QStringList
> r
= iface
->call("ListEnrolledFingers", getlogin());
161 QDBusError e
= iface
->lastError();
162 qDebug() << e
.message();
171 void FMDbusConn::getEnrolledFingers(QString device
) {
172 QDBusInterface
*iface
= getDeviceInterface(device
);
173 fingersResultsWatcher
->setFuture(QtConcurrent::run(getEnrolledFingersReal
, iface
));