Updated czech translation
[KFingerManager.git] / src / FMDbusConn.cpp
blobb14cbf73b87f175d914c699d490fd20ae9d395d7
1 #include <QDBusConnectionInterface>
2 #include <QtCore>
3 #include <QObject>
5 #include "FMDbusConn.h"
6 #include "FingerManagerDevicesModel.h"
8 FMDbusConn* conn;
9 int ocount = 0; // number of opened connection
11 /**
12 * Hides constructor
14 FMDbusConn* FMDbusConn::getConn() {
15 if (ocount == 0) {
16 conn = new FMDbusConn();
18 ocount++;
19 qDebug("Number of references: %d", ocount);
20 return(conn);
23 /**
24 * Hides destructor
26 void FMDbusConn::releaseConn() {
27 if (ocount > 0) {
28 ocount--;
29 qDebug("Number of references: %d", ocount);
31 if (ocount == 0){
32 qDebug("Deleting instance");
33 delete this;
37 /**
38 * FMDbusConn constructor
40 FMDbusConn::FMDbusConn() : managerIface(0), claimedDevice(0), isEnrollStopped(true) {
41 if (!QDBusConnection::systemBus().isConnected()) {
42 qDebug("Cannot connect to D-Bus system bus.");
46 /**
47 * FMDbusConn destructor
49 FMDbusConn::~FMDbusConn() {
50 delete managerIface;
53 /**
54 * Start D-Bus daemon (requires some retries)
56 QDBusInterface* FMDbusConn::getManagerIface(int retr = INTERFACE_ACQUIRE_RETRY) {
57 if (!retr) {
58 qDebug("Manager interface doesn't found!");
59 return(0);
62 if (managerIface && managerIface->isValid()) {
63 qDebug("Returning existing manager interface");
64 return(managerIface);
67 qDebug("getManagerConnection try: %d", retr);
69 // Connect to Fprint manager on system bus
70 QDBusConnection bus = QDBusConnection::systemBus();
71 QDBusInterface *iface = new QDBusInterface("net.reactivated.Fprint",
72 "/net/reactivated/Fprint/Manager",
73 "net.reactivated.Fprint.Manager",
74 bus, this);
76 // Wait while D-Bus Fprintd daemon starts
77 if (iface->isValid()) {
78 managerIface = iface;
79 } else {
80 managerIface = getManagerIface(--retr);
82 return(managerIface);
85 /**
86 * Get device communication interface
88 QDBusInterface* FMDbusConn::getDeviceInterface(QString device) {
89 QDBusInterface* i = new QDBusInterface("net.reactivated.Fprint",
90 device,
91 "net.reactivated.Fprint.Device",
92 QDBusConnection::systemBus());
93 return(i);
96 /**
97 * Returns name associated with device
99 QString FMDbusConn::getDeviceName(QString device) {
100 QString name;
101 QDBusInterface *i = getDeviceInterface(device);
102 if (i->isValid()) {
103 name = i->property("name").toString();
104 } else {
105 name = device;
107 delete(i);
108 return(name);
112 * Real function which do all the work
114 FingerManagerDevicesModel* FMDbusConn::getDevices() {
115 QDBusInterface *iface = getManagerIface();
116 FingerManagerDevicesModel *model = new FingerManagerDevicesModel();
118 if (iface && iface->isValid()) {
120 qDebug("D-Bus interface acquired! :-)");
121 QDBusReply<QList<QDBusObjectPath> > devices = iface->call("GetDevices");
122 if (devices.isValid()) {
123 foreach (QDBusObjectPath device, devices.value()) {
124 model->addDevice(device.path(), getDeviceName(device.path()));
128 QDBusReply<QDBusObjectPath> defaultDevice = iface->call("GetDefaultDevice");
129 if (defaultDevice.isValid()) {
130 model->setDefault(defaultDevice.value().path());
132 } else {
133 qDebug("D-Bus interface cannot be acquired! :-(");
135 return(model);
139 * Real function which do all the work
141 QStringList FMDbusConn::getFingers(QString device, QString username) {
142 qDebug("getFingers:");
143 QDBusInterface *iface = getDeviceInterface(device);
144 QStringList result;
145 if (iface && iface->isValid()) {
146 QDBusReply<QStringList> r = iface->call("ListEnrolledFingers", username);
147 if (r.isValid()) {
148 result = r.value();
149 } else {
150 QDBusError e = iface->lastError();
151 qDebug() << e.message();
154 delete(iface);
155 qDebug() << " result:" << result;
156 return(result);
159 int FMDbusConn::deleteAllEnrolledFingers(QString device, QString username) {
160 QDBusInterface *iface = getDeviceInterface(device);
161 int retval = 0;
162 if (iface && iface->isValid()) {
163 iface->call("DeleteEnrolledFingers", username);
164 retval = 1;
166 delete iface;
167 return(retval);
170 /*******************************************************************************
171 * Device informations
173 int FMDbusConn::getEnrollStages() {
174 int stages = 3;
175 qDebug() << "getEnrollStages";
176 if (isDeviceClaimed()) {
177 qDebug() << " device claimed...";
178 // QDBusInterface cannot access properties with dashes in name,
179 // so access must be done in other way
180 QDBusInterface *iface = new QDBusInterface("net.reactivated.Fprint",
181 claimedDevice->path(),
182 "org.freedesktop.DBus.Properties",
183 QDBusConnection::systemBus());
184 if (iface) {
185 if (iface->isValid()) {
186 QDBusReply<QVariant> r = iface->call("Get",
187 "net.reactivated.Fprint.Device",
188 "num-enroll-stages");
189 int s = r.value().toInt();
190 qDebug("Device enroll stages: %d", s);
191 if (s > 0) {
192 stages = s;
195 delete iface;
197 // // This code should do work once will be QtDbus/glib-dbus/GObject repaired
198 // int s = claimedDevice->property("num-enroll-stages").toInt();
199 // qDebug("Device enroll stages: %d", s);
200 // if (s>0) {
201 // stages = s;
202 // };
204 return(stages);
207 QString FMDbusConn::getScanType() {
208 QString type("swipe");
209 if (isDeviceClaimed()) {
210 QDBusInterface *iface = new QDBusInterface("net.reactivated.Fprint",
211 claimedDevice->path(),
212 "org.freedesktop.DBus.Properties",
213 QDBusConnection::systemBus());
214 if (iface) {
215 if (iface->isValid()) {
216 QDBusReply<QVariant> r = iface->call("Get",
217 "net.reactivated.Fprint.Device",
218 "scan-type");
219 QString t = r.value().toString();
220 qDebug() << "Device scan type:" << t;
221 if (t.length() > 0) {
222 type = t;
225 delete iface;
227 // // This code should do work once will be QtDbus/glib-dbus/GObject repaired
228 // QString t = claimedDevice->property("scan-type").toString();
229 // qDebug() << "Device scan type:" << t;
230 // if (t.length() > 0) {
231 // type = t;
232 // }
234 return(type);
237 /*******************************************************************************
238 * User enrollment
240 void FMDbusConn::claimDevice(QString device, QString username) {
241 if (!claimedDevice) {
242 QDBusInterface *iface = getDeviceInterface(device);
243 if (iface) {
244 if (iface->isValid()) {
245 QDBusMessage m = iface->call("Claim", username);
246 if (m.type() == QDBusMessage::ErrorMessage) {
247 qDebug() << "Claim device:" << m.errorMessage();
248 delete(iface);
249 claimedDevice = 0;
250 } else {
251 claimedDevice = iface;
252 QObject::connect(iface, SIGNAL(EnrollStatus(QString,bool)),
253 this, SLOT(enrollCallback(QString,bool)));
255 } else {
256 qDebug() << "Error:"<< iface->lastError().message();
257 claimedDevice = 0;
258 delete(iface);
261 } else {
262 qDebug("Device is allready claimed!");
266 void FMDbusConn::releaseDevice() {
267 qDebug() << __func__;
268 if (claimedDevice) {
269 if (claimedDevice->isValid()) {
270 if (!isEnrollStopped) enrollStop();
271 QDBusMessage m = claimedDevice->call("Release");
272 if (m.type() == QDBusMessage::ErrorMessage) {
273 qDebug() << " Release device:" << m.errorMessage();
276 delete(claimedDevice);
277 claimedDevice = 0;
281 bool FMDbusConn::isDeviceClaimed() {
282 if (claimedDevice && claimedDevice->isValid()) {
283 return(true);
284 } else {
285 return(false);
289 void FMDbusConn::enrollCallback(QString status, bool result) {
290 emit EnrollStatus(status, result);
293 void FMDbusConn::enrollStart(QString finger) {
294 qDebug() << __func__;
295 if (isDeviceClaimed()) {
296 if (isEnrollStopped) {
297 isEnrollStopped = false;
298 claimedDevice->call("EnrollStart", finger);
299 } else {
300 qDebug(" Enrollment allready started...");
305 void FMDbusConn::enrollStop() {
306 qDebug() << __func__;
307 if (isDeviceClaimed()) {
308 if (!isEnrollStopped) {
309 isEnrollStopped = true;
310 claimedDevice->call("EnrollStop");
311 } else {
312 qDebug(" Enrollment allready stopped...");