1 #include <KApplication>
2 #include <QDBusConnectionInterface>
9 #include "FMDbusConn.h"
10 #include "FingerManagerDevicesModel.h"
13 int ocount
= 0; // number of opened connection
18 FMDbusConn
* FMDbusConn::getConn() {
20 conn
= new FMDbusConn();
23 qDebug("Number of references: %d", ocount
);
30 void FMDbusConn::releaseConn() {
33 qDebug("Number of references: %d", ocount
);
36 qDebug("Deleting instance");
42 * FMDbusConn constructor
44 FMDbusConn::FMDbusConn() : managerIface(0) {
45 if (!QDBusConnection::systemBus().isConnected()) {
46 qDebug("Cannot connect to D-Bus system bus.");
48 deviceResultsWatcher
= new QFutureWatcher
<FingerManagerDevicesModel
*>(this);
49 fingersResultsWatcher
= new QFutureWatcher
<QStringList
>(this);
50 deleteWatcher
= new QFutureWatcher
<int>(this);
51 connect(deviceResultsWatcher
, SIGNAL(resultReadyAt(int)), SLOT(emitDiscovered(int)));
52 connect(fingersResultsWatcher
, SIGNAL(resultReadyAt(int)), SLOT(fingersDiscovered(int)));
53 connect(deleteWatcher
, SIGNAL(resultReadyAt(int)), SLOT(fingersDeleted(int)));
57 * FMDbusConn destructor
59 FMDbusConn::~FMDbusConn() {
61 delete deviceResultsWatcher
;
62 delete fingersResultsWatcher
;
67 * Start D-Bus daemon (requires some retries)
69 QDBusInterface
* FMDbusConn::getManagerIface(int retr
= INTERFACE_ACQUIRE_RETRY
) {
71 qDebug("Manager interface doesn't found!");
75 if (managerIface
&& managerIface
->isValid()) {
76 qDebug("Returning existing manager interface");
80 qDebug("getManagerConnection try: %d", retr
);
82 // Connect to Fprint manager on system bus
83 QDBusConnection bus
= QDBusConnection::systemBus();
84 QDBusInterface
*iface
= new QDBusInterface("net.reactivated.Fprint",
85 "/net/reactivated/Fprint/Manager",
86 "net.reactivated.Fprint.Manager",
89 // Wait while D-Bus Fprintd daemon starts
90 if (iface
->isValid()) {
93 managerIface
= getManagerIface(--retr
);
99 * QFutureWatcher signal handler
101 void FMDbusConn::emitDiscovered(int idx
) {
102 emit
devicesDiscovered(idx
);
105 void FMDbusConn::fingersDiscovered(int idx
) {
106 emit
enrolledFingersDiscovered(idx
);
109 void FMDbusConn::fingersDeleted(int idx
) {
110 if (deleteWatcher
->resultAt(idx
)) emit
enrolledFingersDeleted();
114 * Returns FingerManagerDevicesModel created by getDevices
116 FingerManagerDevicesModel
* FMDbusConn::getModel(int idx
) {
117 return(deviceResultsWatcher
->resultAt(idx
));
121 * Get device communication interface
123 QDBusInterface
* getDeviceInterface(QString device
) {
124 QDBusInterface
* i
= new QDBusInterface("net.reactivated.Fprint",
126 "net.reactivated.Fprint.Device",
127 QDBusConnection::systemBus());
132 * Returns name associated with device
134 QString
getDeviceName(QString device
) {
136 QDBusInterface
*i
= getDeviceInterface(device
);
138 name
= i
->property("name").toString();
147 * Real function which do all the work
149 FingerManagerDevicesModel
* getDevicesReal(QDBusInterface
*iface
) {
150 FingerManagerDevicesModel
*model
= new FingerManagerDevicesModel();
152 if (iface
&& iface
->isValid()) {
154 qDebug("D-Bus interface acquired! :-)");
155 QDBusReply
<QList
<QDBusObjectPath
> > devices
= iface
->call("GetDevices");
156 if (devices
.isValid()) {
157 foreach (QDBusObjectPath device
, devices
.value()) {
158 model
->addDevice(device
.path(), getDeviceName(device
.path()));
162 QDBusReply
<QDBusObjectPath
> defaultDevice
= iface
->call("GetDefaultDevice");
163 if (defaultDevice
.isValid()) {
164 model
->setDefault(defaultDevice
.value().path());
167 qDebug("D-Bus interface cannot be acquired! :-(");
175 void FMDbusConn::getDevices() {
176 QDBusInterface
*iface
= getManagerIface();
177 deviceResultsWatcher
->setFuture(QtConcurrent::run(getDevicesReal
, iface
));
181 * Returns QStringList returned by D-Bus daemon
183 QStringList
FMDbusConn::getFingers(int idx
) {
184 return(fingersResultsWatcher
->resultAt(idx
));
188 * Real function which do all the work
190 QStringList
getEnrolledFingersReal(QDBusInterface
*iface
, QString
*username
) {
192 if (iface
&& iface
->isValid()) {
193 QDBusReply
<QStringList
> r
= iface
->call("ListEnrolledFingers", *username
);
197 QDBusError e
= iface
->lastError();
198 qDebug() << e
.message();
207 * - discover enrolled fingerprints for username and device
209 void FMDbusConn::getEnrolledFingers(QString device
, QString username
) {
210 QDBusInterface
*iface
= getDeviceInterface(device
);
211 fingersResultsWatcher
->setFuture(QtConcurrent::run(getEnrolledFingersReal
, iface
, &username
));
214 int deleteAllEnrolledFingersReal(QDBusInterface
*iface
, QString
*username
) {
216 if (iface
&& iface
->isValid()) {
217 iface
->call("DeleteEnrolledFingers", *username
);
226 * - delete all enrolled fingerprints for username
228 void FMDbusConn::deleteAllEnrolledFingers(QString device
, QString username
) {
229 QDBusInterface
*iface
= getDeviceInterface(device
);
230 deleteWatcher
->setFuture(QtConcurrent::run(deleteAllEnrolledFingersReal
, iface
, &username
));
233 /*******************************************************************************
234 * Device informations
236 int FMDbusConn::getEnrollStages(QDBusInterface
*iface
) {
237 //TODO Qt doesn't support dash in property name
239 if (iface
&& iface
->isValid()) {
240 int s
= iface
->property("num_enroll_stages").toInt();
241 qDebug("Device enroll stages: %d", s
);
249 QString
FMDbusConn::getScanType(QDBusInterface
*iface
) {
250 //TODO Qt doesn't support dash in property name
251 QString
type("swipe");
252 if (iface
&& iface
->isValid()) {
253 QString t
= iface
->property("scan_type").toString();
254 qDebug() << "Device scan type:" << t
;
255 if (t
.length() > 0) {
262 /*******************************************************************************
265 QDBusInterface
* FMDbusConn::claimDevice(QString device
, QString username
) {
266 QDBusInterface
*iface
= getDeviceInterface(device
);
267 if (iface
&& iface
->isValid()) {
268 QDBusMessage m
= iface
->call("Claim", username
);
269 if (m
.type() == QDBusMessage::ErrorMessage
) {
270 qDebug() << "Claim device:" << m
.errorMessage();
277 void FMDbusConn::releaseDevice(QDBusInterface
*iface
) {
279 if (iface
->isValid()) {
280 qDebug("Releasing device!");
281 QDBusMessage m
= iface
->call("Release");
282 if (m
.type() == QDBusMessage::ErrorMessage
) {
283 qDebug() << "Release device:" << m
.errorMessage();