Updated doc in FignerManagerDevicesModel.
[KFingerManager.git] / src / FMEnroll.cpp
blob3ff5b98f42ecacaff06f0b0ca1aa808f0227b708
1 #include <KLocale>
2 #include <QtCore>
3 #include <QFont>
5 #include "FMEnroll.h"
6 #include "FingerNames.h"
8 /**
9 * getIface - create new D-Bus interface object, reads scan type and number of
10 * enroll stages.
12 void FMEnroll::getIface() {
13 enrollStages = 0;
14 dbc->claimDevice(device_.device, login_);
15 if (dbc->isDeviceClaimed()) {
16 enrollStages = dbc->getEnrollStages();
17 scanType = dbc->getScanType();
18 QObject::connect(dbc, SIGNAL(EnrollStatus(QString,bool)),
19 this, SLOT(enrollStatus(QString,bool)));
20 } else {
21 scanType = "";
25 /**
26 * FMEnroll constructor
27 * @param device device selected
28 * @param login users login
29 * @param finger selected finger
30 * @param parent parent widget
32 FMEnroll::FMEnroll(DeviceModel device, QString login, int finger, QWidget *parent) :
33 KDialog(parent), finger_(finger), device_(device), actualStage(0),
34 login_(login) {
35 // Modal dialog, only Cancel button, precedent by separator
36 setModal(true);
37 showButtonSeparator(true);
38 setButtons(KDialog::Cancel);
40 // D-Bus connection
41 dbc = FMDbusConn::getConn();
42 getIface();
44 // Init and traslate UI components
45 initComponents(enrollStages);
46 retranslate();
47 setMainWidget(mainPanel);
48 QObject::connect(timer, SIGNAL(timeout()), this, SLOT(timeout()));
50 // Layout
51 statusLayout->addWidget(statusLabel);
52 statusLayout->addWidget(actualStatus);
53 statusLayout->addStretch();
54 mainLayout->addLayout(statusLayout);
56 stagesLayout->addStretch();
57 for (int i = 0; i < enrollStages; i++) {
58 stagesLayout->addWidget(stages[i]);
60 stagesLayout->addStretch();
61 stagesLayout->addWidget(animLabel);
63 mainLayout->addLayout(stagesLayout);
64 mainPanel->setLayout(mainLayout);
66 setMaximumSize(minimumSizeHint());
67 setMinimumSize(minimumSizeHint());
69 if (!dbc->isDeviceClaimed()) {
70 actualStatus->setText(i18n("Error while claiming device."));
71 } else {
72 dbc->enrollStart(fingerNames[finger_].iface);
76 /**
77 * initCimponents - init all widgets
78 * @param enrollStages number of enroll stages
80 void FMEnroll::initComponents(int enrollStages) {
81 mainPanel = new QWidget();
82 statusLabel = new QLabel();
83 QFont font = statusLabel->font();
84 font.setWeight(QFont::Bold);
85 statusLabel->setFont(font);
86 actualStatus = new QLabel();
87 statusLayout = new QHBoxLayout();
88 stagesLayout = new QHBoxLayout();
89 mainLayout = new QVBoxLayout();
90 timer = new QTimer();
91 timer->setSingleShot(true);
92 animLabel = new SensorAnimLabel(scanType);
94 stages = (EnrollStatus**)malloc(sizeof(EnrollStatus*)*enrollStages);
95 for (int i = 0; i < enrollStages; i++) {
96 stages[i] = new EnrollStatus(this);
101 * retranslate - update all UI strings
103 void FMEnroll::retranslate() {
104 // Set Window Title
105 setWindowTitle(i18n("Enrolling finger..."));
107 // What's this
108 setWhatsThis(i18n("Enroll finger window"));
110 // Set status label
111 statusLabel->setText(i18n("Status")+":");
113 // Set stages tooltips
114 for (int i = 0; i < enrollStages; i++) {
115 QString msg = i18n("Stage %1").arg(i + 1);
116 stages[i]->setToolTip(msg);
117 stages[i]->setWhatsThis(i18n("Current enrollment progess..."));
120 // Status message
121 timeout();
125 * setStatusMessage - set suitable status message for actual enroll status
126 * @param result actual enroll status
128 void FMEnroll::setStatusMessage(QString result) {
129 int timeout = 2500;
130 bool checked = false;
131 if (result == "enroll-stage-passed") {
132 checked = true;
133 actualStatus->setText(i18n("Stage passed"));
134 timeout = 1250;
135 } else if (result == "enroll-swipe-too-short") {
136 actualStatus->setText(i18n("Swipe too short"));
137 } else if (result == "enroll-finger-not-centered") {
138 actualStatus->setText(i18n("Finger not centered"));
139 } else if (result == "enroll-remove-and-retry") {
140 actualStatus->setText(i18n("Remove and retry"));
141 } else if (result == "enroll-retry-scan") {
142 actualStatus->setText(i18n("Retry scan"));
145 stages[actualStage]->setChecked(checked);
146 animLabel->stop();
148 timer->start(timeout);
150 if (checked) {
151 actualStage++;
156 * enrollStatus - Slot for asynchronous enroll signals
157 * @param result actual enroll status
158 * @param done true if enroll process is completed, otherwise false
160 void FMEnroll::enrollStatus(QString result, bool done) {
161 qDebug() << "enrollStatus:" << result << ", done: " << done;
163 if (done == true) {
164 if (result == "enroll-completed") {
165 actualStatus->setText(i18n("Enroll completed"));
166 stages[actualStage]->setChecked(true);
167 setButtons(KDialog::Ok);
168 dbc->enrollStop();
169 } else if (result == "enroll-disconnected") {
170 actualStatus->setText(i18n("Device disconected. Fingerprint is not enrolled."));
171 dbc->enrollStop();
172 dbc->releaseDevice();
173 } else if (result == "enroll-failed") {
174 actualStatus->setText(i18n("Enroll failed."));
175 dbc->enrollStop();
176 } else if (result == "enroll-unknown-error") {
177 actualStatus->setText(i18n("Unknown error."));
178 dbc->enrollStop();
180 animLabel->stop();
181 } else setStatusMessage(result);
185 * timeout - handle timeout and set proper message
187 void FMEnroll::timeout() {
188 // Create actual status message
189 QString msg;
190 if (scanType == "swipe") {
191 msg = QString(i18n("Swipe your \"%1\" on \"%2\""));
192 } else {
193 msg = QString(i18n("Place your \"%1\" on \"%2\""));
195 msg = msg.arg(i18n(fingerNames[finger_].name), device_.name);
196 actualStatus->setText(msg);
197 stages[actualStage]->clearStatus();
198 animLabel->start();
202 * FMEnroll destructor
204 FMEnroll::~FMEnroll() {
205 qDebug() << "FMEnroll destructor";
206 if (statusLabel) delete statusLabel;
207 if (actualStatus) delete actualStatus;
208 if (statusLayout) delete statusLayout;
209 if (stages) {
210 for (int i = 0; i < enrollStages; i++) {
211 if (stages[i]) delete stages[i];
213 free(stages);
215 if (animLabel) delete animLabel;
216 if (stagesLayout) delete stagesLayout;
217 if (mainLayout) delete mainLayout;
218 if (mainPanel) delete mainPanel;
219 if (dbc) {
220 dbc->enrollStop();
221 dbc->releaseDevice();
222 dbc->releaseConn();