Moved to patched fprintd (hyphen in device properties substitued by underscore)
[KFingerManager.git] / src / FMDbusConn.cpp
blob682673059d1ff636eed608ae22e74ab75b94d637
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 FMDbusConn* conn;
13 int ocount = 0; // number of opened connection
15 /**
16 * Hides constructor
18 FMDbusConn* FMDbusConn::getConn() {
19 if (ocount == 0) {
20 conn = new FMDbusConn();
22 ocount++;
23 qDebug("Number of references: %d", ocount);
24 return(conn);
27 /**
28 * Hides destructor
30 void FMDbusConn::releaseConn() {
31 if (ocount > 0) {
32 ocount--;
33 qDebug("Number of references: %d", ocount);
35 if (ocount == 0){
36 qDebug("Deleting instance");
37 delete this;
41 /**
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)));
56 /**
57 * FMDbusConn destructor
59 FMDbusConn::~FMDbusConn() {
60 delete managerIface;
61 delete deviceResultsWatcher;
62 delete fingersResultsWatcher;
63 delete deleteWatcher;
66 /**
67 * Start D-Bus daemon (requires some retries)
69 QDBusInterface* FMDbusConn::getManagerIface(int retr = INTERFACE_ACQUIRE_RETRY) {
70 if (!retr) {
71 qDebug("Manager interface doesn't found!");
72 return(0);
75 if (managerIface && managerIface->isValid()) {
76 qDebug("Returning existing manager interface");
77 return managerIface;
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",
87 bus, this);
89 // Wait while D-Bus Fprintd daemon starts
90 if (iface->isValid()) {
91 managerIface = iface;
92 } else {
93 managerIface = getManagerIface(--retr);
95 return(managerIface);
98 /**
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",
125 device,
126 "net.reactivated.Fprint.Device",
127 QDBusConnection::systemBus());
128 return(i);
132 * Returns name associated with device
134 QString getDeviceName(QString device) {
135 QString name;
136 QDBusInterface *i = getDeviceInterface(device);
137 if (i->isValid()) {
138 name = i->property("name").toString();
139 } else {
140 name = device;
142 delete(i);
143 return(name);
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());
166 } else {
167 qDebug("D-Bus interface cannot be acquired! :-(");
169 return(model);
173 * Starts thread
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) {
191 QStringList result;
192 if (iface && iface->isValid()) {
193 QDBusReply<QStringList> r = iface->call("ListEnrolledFingers", *username);
194 if (r.isValid()) {
195 result = r.value();
196 } else {
197 QDBusError e = iface->lastError();
198 qDebug() << e.message();
201 delete(iface);
202 return(result);
206 * Start thread
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) {
215 int retval = 0;
216 if (iface && iface->isValid()) {
217 iface->call("DeleteEnrolledFingers", *username);
218 retval = 1;
220 delete iface;
221 return(retval);
225 * Start thread
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
238 int stages = 3;
239 if (iface && iface->isValid()) {
240 int s = iface->property("num_enroll_stages").toInt();
241 qDebug("Device enroll stages: %d", s);
242 if (s>0) {
243 stages = s;
246 return(stages);
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) {
256 type = t;
259 return(type);
262 /*******************************************************************************
263 * User enrollment
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();
272 return(iface);
274 return(0);
277 void FMDbusConn::releaseDevice(QDBusInterface *iface) {
278 if (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();
286 delete(iface);