UI improvement, (added stretch).
[KFingerManager.git] / src / FMDbusConn.cpp
blob88b333c704fb9d47a73899002077cab4166e2de7
1 #include <QDBusConnectionInterface>
2 #include <QtCore>
3 #include <QObject>
5 #include <unistd.h>
7 #include "FMDbusConn.h"
8 #include "FingerManagerDevicesModel.h"
10 FMDbusConn* conn;
11 int ocount = 0; // number of opened connection
13 /**
14 * Hides constructor
16 FMDbusConn* FMDbusConn::getConn() {
17 if (ocount == 0) {
18 conn = new FMDbusConn();
20 ocount++;
21 qDebug("Number of references: %d", ocount);
22 return(conn);
25 /**
26 * Hides destructor
28 void FMDbusConn::releaseConn() {
29 if (ocount > 0) {
30 ocount--;
31 qDebug("Number of references: %d", ocount);
33 if (ocount == 0){
34 qDebug("Deleting instance");
35 delete this;
39 /**
40 * FMDbusConn constructor
42 FMDbusConn::FMDbusConn() : managerIface(0), claimedDevice(0), isEnrollStopped(true) {
43 if (!QDBusConnection::systemBus().isConnected()) {
44 qDebug("Cannot connect to D-Bus system bus.");
48 /**
49 * FMDbusConn destructor
51 FMDbusConn::~FMDbusConn() {
52 delete managerIface;
55 /**
56 * Start D-Bus daemon (requires some retries)
58 QDBusInterface* FMDbusConn::getManagerIface(int retr = INTERFACE_ACQUIRE_RETRY) {
59 if (!retr) {
60 qDebug("Manager interface doesn't found!");
61 return(0);
64 if (managerIface && managerIface->isValid()) {
65 qDebug("Returning existing manager interface");
66 return managerIface;
69 qDebug("getManagerConnection try: %d", retr);
71 // Connect to Fprint manager on system bus
72 QDBusConnection bus = QDBusConnection::systemBus();
73 QDBusInterface *iface = new QDBusInterface("net.reactivated.Fprint",
74 "/net/reactivated/Fprint/Manager",
75 "net.reactivated.Fprint.Manager",
76 bus, this);
78 // Wait while D-Bus Fprintd daemon starts
79 if (iface->isValid()) {
80 managerIface = iface;
81 } else {
82 managerIface = getManagerIface(--retr);
84 return(managerIface);
87 /**
88 * Get device communication interface
90 QDBusInterface* FMDbusConn::getDeviceInterface(QString device) {
91 QDBusInterface* i = new QDBusInterface("net.reactivated.Fprint",
92 device,
93 "net.reactivated.Fprint.Device",
94 QDBusConnection::systemBus());
95 return(i);
98 /**
99 * Returns name associated with device
101 QString FMDbusConn::getDeviceName(QString device) {
102 QString name;
103 QDBusInterface *i = getDeviceInterface(device);
104 if (i->isValid()) {
105 name = i->property("name").toString();
106 } else {
107 name = device;
109 delete(i);
110 return(name);
114 * Real function which do all the work
116 FingerManagerDevicesModel* FMDbusConn::getDevices() {
117 QDBusInterface *iface = getManagerIface();
118 FingerManagerDevicesModel *model = new FingerManagerDevicesModel();
120 if (iface && iface->isValid()) {
122 qDebug("D-Bus interface acquired! :-)");
123 QDBusReply<QList<QDBusObjectPath> > devices = iface->call("GetDevices");
124 if (devices.isValid()) {
125 foreach (QDBusObjectPath device, devices.value()) {
126 model->addDevice(device.path(), getDeviceName(device.path()));
130 QDBusReply<QDBusObjectPath> defaultDevice = iface->call("GetDefaultDevice");
131 if (defaultDevice.isValid()) {
132 model->setDefault(defaultDevice.value().path());
134 } else {
135 qDebug("D-Bus interface cannot be acquired! :-(");
137 return(model);
141 * Real function which do all the work
143 QStringList FMDbusConn::getFingers(QString device, QString username) {
144 qDebug("getFingers:");
145 QDBusInterface *iface = getDeviceInterface(device);
146 QStringList result;
147 if (iface && iface->isValid()) {
148 QDBusReply<QStringList> r = iface->call("ListEnrolledFingers", username);
149 if (r.isValid()) {
150 result = r.value();
151 } else {
152 QDBusError e = iface->lastError();
153 qDebug() << e.message();
156 delete(iface);
157 qDebug() << " result:" << result;
158 return(result);
161 int FMDbusConn::deleteAllEnrolledFingers(QString device, QString username) {
162 QDBusInterface *iface = getDeviceInterface(device);
163 int retval = 0;
164 if (iface && iface->isValid()) {
165 iface->call("DeleteEnrolledFingers", username);
166 retval = 1;
168 delete iface;
169 return(retval);
172 /*******************************************************************************
173 * Device informations
175 int FMDbusConn::getEnrollStages() {
176 int stages = 3;
177 qDebug() << "getEnrollStages";
178 if (isDeviceClaimed()) {
179 qDebug() << " device claimed...";
180 // QDBusInterface cannot access properties with dashes in name,
181 // so access must be done in other way
182 QDBusInterface *iface = new QDBusInterface("net.reactivated.Fprint",
183 claimedDevice->path(),
184 "org.freedesktop.DBus.Properties",
185 QDBusConnection::systemBus());
186 if (iface) {
187 if (iface->isValid()) {
188 QDBusReply<QVariant> r = iface->call("Get",
189 "net.reactivated.Fprint.Device",
190 "num-enroll-stages");
191 int s = r.value().toInt();
192 qDebug("Device enroll stages: %d", s);
193 if (s > 0) {
194 stages = s;
197 delete iface;
199 // // This code should do work once will be QtDbus/glib-dbus/GObject repaired
200 // int s = claimedDevice->property("num-enroll-stages").toInt();
201 // qDebug("Device enroll stages: %d", s);
202 // if (s>0) {
203 // stages = s;
204 // };
206 return(stages);
209 QString FMDbusConn::getScanType() {
210 QString type("swipe");
211 if (isDeviceClaimed()) {
212 QDBusInterface *iface = new QDBusInterface("net.reactivated.Fprint",
213 claimedDevice->path(),
214 "org.freedesktop.DBus.Properties",
215 QDBusConnection::systemBus());
216 if (iface) {
217 if (iface->isValid()) {
218 QDBusReply<QVariant> r = iface->call("Get",
219 "net.reactivated.Fprint.Device",
220 "scan-type");
221 QString t = r.value().toString();
222 qDebug() << "Device scan type:" << t;
223 if (t.length() > 0) {
224 type = t;
227 delete iface;
229 // // This code should do work once will be QtDbus/glib-dbus/GObject repaired
230 // QString t = claimedDevice->property("scan-type").toString();
231 // qDebug() << "Device scan type:" << t;
232 // if (t.length() > 0) {
233 // type = t;
234 // }
236 return(type);
239 /*******************************************************************************
240 * User enrollment
242 void FMDbusConn::claimDevice(QString device, QString username) {
243 if (!claimedDevice) {
244 QDBusInterface *iface = getDeviceInterface(device);
245 if (iface) {
246 if (iface->isValid()) {
247 QDBusMessage m = iface->call("Claim", username);
248 if (m.type() == QDBusMessage::ErrorMessage) {
249 qDebug() << "Claim device:" << m.errorMessage();
251 claimedDevice = iface;
252 QObject::connect(iface, SIGNAL(EnrollStatus(QString,bool)),
253 this, SLOT(enrollCallback(QString,bool)));
254 } else {
255 qDebug() << "Error:"<< iface->lastError().message();
256 claimedDevice = 0;
257 delete(iface);
260 } else {
261 qDebug("Device is allready claimed!");
265 void FMDbusConn::releaseDevice() {
266 qDebug("Release claimed device...");
267 if (claimedDevice) {
268 if (claimedDevice->isValid()) {
269 if (!isEnrollStopped) enrollStop();
270 QDBusMessage m = claimedDevice->call("Release");
271 if (m.type() == QDBusMessage::ErrorMessage) {
272 qDebug() << "Release device:" << m.errorMessage();
275 delete(claimedDevice);
276 claimedDevice = 0;
280 bool FMDbusConn::isDeviceClaimed() {
281 if (claimedDevice && claimedDevice->isValid()) {
282 return(true);
283 } else {
284 return(false);
288 void FMDbusConn::enrollCallback(QString status, bool result) {
289 emit EnrollStatus(status, result);
292 void FMDbusConn::enrollStart(QString finger) {
293 if (isDeviceClaimed()) {
294 if (isEnrollStopped) {
295 isEnrollStopped = false;
296 claimedDevice->call("EnrollStart", finger);
297 } else {
298 qDebug("Enrollment allready started...");
303 void FMDbusConn::enrollStop() {
304 if (isDeviceClaimed()) {
305 if (!isEnrollStopped) {
306 isEnrollStopped = true;
307 claimedDevice->call("EnrollStop");
308 } else {
309 qDebug("Enrollment allready stopped...");