1 #include <QDBusConnectionInterface>
5 #include "FMDbusConn.h"
6 #include "FingerManagerDevicesModel.h"
9 int ocount
= 0; // number of opened connection
14 FMDbusConn
* FMDbusConn::getConn() {
16 conn
= new FMDbusConn();
19 qDebug("Number of references: %d", ocount
);
26 void FMDbusConn::releaseConn() {
29 qDebug("Number of references: %d", ocount
);
32 qDebug("Deleting instance");
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.");
47 * FMDbusConn destructor
49 FMDbusConn::~FMDbusConn() {
54 * Start D-Bus daemon (requires some retries)
56 QDBusInterface
* FMDbusConn::getManagerIface(int retr
= INTERFACE_ACQUIRE_RETRY
) {
58 qDebug("Manager interface doesn't found!");
62 if (managerIface
&& managerIface
->isValid()) {
63 qDebug("Returning existing manager interface");
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",
76 // Wait while D-Bus Fprintd daemon starts
77 if (iface
->isValid()) {
80 managerIface
= getManagerIface(--retr
);
86 * Get device communication interface
88 QDBusInterface
* FMDbusConn::getDeviceInterface(QString device
) {
89 QDBusInterface
* i
= new QDBusInterface("net.reactivated.Fprint",
91 "net.reactivated.Fprint.Device",
92 QDBusConnection::systemBus());
97 * Returns name associated with device
99 QString
FMDbusConn::getDeviceName(QString device
) {
101 QDBusInterface
*i
= getDeviceInterface(device
);
103 name
= i
->property("name").toString();
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());
133 qDebug("D-Bus interface cannot be acquired! :-(");
139 * Real function which do all the work
141 QStringList
FMDbusConn::getFingers(QString device
, QString username
) {
142 qDebug("getFingers:");
143 QDBusInterface
*iface
= getDeviceInterface(device
);
145 if (iface
&& iface
->isValid()) {
146 QDBusReply
<QStringList
> r
= iface
->call("ListEnrolledFingers", username
);
150 QDBusError e
= iface
->lastError();
151 qDebug() << e
.message();
155 qDebug() << " result:" << result
;
159 int FMDbusConn::deleteAllEnrolledFingers(QString device
, QString username
) {
160 QDBusInterface
*iface
= getDeviceInterface(device
);
162 if (iface
&& iface
->isValid()) {
163 iface
->call("DeleteEnrolledFingers", username
);
170 /*******************************************************************************
171 * Device informations
173 int FMDbusConn::getEnrollStages() {
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());
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
);
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);
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());
215 if (iface
->isValid()) {
216 QDBusReply
<QVariant
> r
= iface
->call("Get",
217 "net.reactivated.Fprint.Device",
219 QString t
= r
.value().toString();
220 qDebug() << "Device scan type:" << t
;
221 if (t
.length() > 0) {
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) {
237 /*******************************************************************************
240 void FMDbusConn::claimDevice(QString device
, QString username
) {
241 if (!claimedDevice
) {
242 QDBusInterface
*iface
= getDeviceInterface(device
);
244 if (iface
->isValid()) {
245 QDBusMessage m
= iface
->call("Claim", username
);
246 if (m
.type() == QDBusMessage::ErrorMessage
) {
247 qDebug() << "Claim device:" << m
.errorMessage();
251 claimedDevice
= iface
;
252 QObject::connect(iface
, SIGNAL(EnrollStatus(QString
,bool)),
253 this, SLOT(enrollCallback(QString
,bool)));
256 qDebug() << "Error:"<< iface
->lastError().message();
262 qDebug("Device is allready claimed!");
266 void FMDbusConn::releaseDevice() {
267 qDebug() << __func__
;
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
);
281 bool FMDbusConn::isDeviceClaimed() {
282 if (claimedDevice
&& claimedDevice
->isValid()) {
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
);
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");
312 qDebug(" Enrollment allready stopped...");