1 #include <QDBusConnectionInterface>
7 #include "FMDbusConn.h"
8 #include "FingerManagerDevicesModel.h"
11 int ocount
= 0; // number of opened connection
16 FMDbusConn
* FMDbusConn::getConn() {
18 conn
= new FMDbusConn();
21 qDebug("Number of references: %d", ocount
);
28 void FMDbusConn::releaseConn() {
31 qDebug("Number of references: %d", ocount
);
34 qDebug("Deleting instance");
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.");
49 * FMDbusConn destructor
51 FMDbusConn::~FMDbusConn() {
56 * Start D-Bus daemon (requires some retries)
58 QDBusInterface
* FMDbusConn::getManagerIface(int retr
= INTERFACE_ACQUIRE_RETRY
) {
60 qDebug("Manager interface doesn't found!");
64 if (managerIface
&& managerIface
->isValid()) {
65 qDebug("Returning existing manager interface");
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",
78 // Wait while D-Bus Fprintd daemon starts
79 if (iface
->isValid()) {
82 managerIface
= getManagerIface(--retr
);
88 * Get device communication interface
90 QDBusInterface
* FMDbusConn::getDeviceInterface(QString device
) {
91 QDBusInterface
* i
= new QDBusInterface("net.reactivated.Fprint",
93 "net.reactivated.Fprint.Device",
94 QDBusConnection::systemBus());
99 * Returns name associated with device
101 QString
FMDbusConn::getDeviceName(QString device
) {
103 QDBusInterface
*i
= getDeviceInterface(device
);
105 name
= i
->property("name").toString();
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());
135 qDebug("D-Bus interface cannot be acquired! :-(");
141 * Real function which do all the work
143 QStringList
FMDbusConn::getFingers(QString device
, QString username
) {
144 qDebug("getFingers:");
145 QDBusInterface
*iface
= getDeviceInterface(device
);
147 if (iface
&& iface
->isValid()) {
148 QDBusReply
<QStringList
> r
= iface
->call("ListEnrolledFingers", username
);
152 QDBusError e
= iface
->lastError();
153 qDebug() << e
.message();
157 qDebug() << " result:" << result
;
161 int FMDbusConn::deleteAllEnrolledFingers(QString device
, QString username
) {
162 QDBusInterface
*iface
= getDeviceInterface(device
);
164 if (iface
&& iface
->isValid()) {
165 iface
->call("DeleteEnrolledFingers", username
);
172 /*******************************************************************************
173 * Device informations
175 int FMDbusConn::getEnrollStages() {
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());
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
);
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);
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());
217 if (iface
->isValid()) {
218 QDBusReply
<QVariant
> r
= iface
->call("Get",
219 "net.reactivated.Fprint.Device",
221 QString t
= r
.value().toString();
222 qDebug() << "Device scan type:" << t
;
223 if (t
.length() > 0) {
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) {
239 /*******************************************************************************
242 void FMDbusConn::claimDevice(QString device
, QString username
) {
243 if (!claimedDevice
) {
244 QDBusInterface
*iface
= getDeviceInterface(device
);
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)));
255 qDebug() << "Error:"<< iface
->lastError().message();
261 qDebug("Device is allready claimed!");
265 void FMDbusConn::releaseDevice() {
266 qDebug("Release claimed device...");
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
);
280 bool FMDbusConn::isDeviceClaimed() {
281 if (claimedDevice
&& claimedDevice
->isValid()) {
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
);
298 qDebug("Enrollment allready started...");
303 void FMDbusConn::enrollStop() {
304 if (isDeviceClaimed()) {
305 if (!isEnrollStopped
) {
306 isEnrollStopped
= true;
307 claimedDevice
->call("EnrollStop");
309 qDebug("Enrollment allready stopped...");