File: biometricauthwidget.cpp

package info (click to toggle)
ukui-screensaver 3.0.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,848 kB
  • sloc: cpp: 24,930; ansic: 1,280; xml: 175; makefile: 7; sh: 2
file content (344 lines) | stat: -rw-r--r-- 9,329 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/**
 * Copyright (C) 2018 Tianjin KYLIN Information Technology Co., Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301, USA.
**/
#include "biometricauthwidget.h"
#include <QLabel>
#include <QDebug>
#include <QDBusUnixFileDescriptor>
#include <unistd.h>
#include <pwd.h>

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>

#include "giodbus.h"

BiometricAuthWidget::BiometricAuthWidget(BiometricProxy *proxy, QWidget *parent) :
    QWidget(parent),
    proxy(proxy),
    isInAuth(false),
    movieTimer(nullptr), 
    failedCount(0),
    timeoutCount(0),
    beStopped(false),
    retrytimer(nullptr),
    usebind(false)
{
    usebind = getAuthDouble();
    initUI();
    resize(400, 260);

    if(this->proxy)
    {
        connect(this->proxy, &BiometricProxy::StatusChanged,
                this, &BiometricAuthWidget::onStatusChanged);

        connect(this->proxy, &BiometricProxy::FrameWritten,
                this, &BiometricAuthWidget::onFrameWritten);
    }

}

void BiometricAuthWidget::initUI()
{
    //显示提示信息
    lblNotify = new QLabel(this);
    lblNotify->setWordWrap(true);
    lblNotify->setAlignment(Qt::AlignHCenter | Qt::AlignBottom);

    //显示当前设备
    lblDevice = new QLabel(this);
    lblDevice->setWordWrap(true);
    lblDevice->setAlignment(Qt::AlignCenter);

    //显示图片
    lblImage = new QLabel(this);
    lblImage->setFixedSize(100, 100);

}


void BiometricAuthWidget::resizeEvent(QResizeEvent */*event*/)
{
    lblNotify->setGeometry(0, 0, width(), 45);
    lblDevice->setGeometry(0, lblNotify->geometry().bottom()+5, width(), 30);
    lblImage->setGeometry((width() - lblImage->width()) / 2,
                           lblDevice->geometry().bottom() + 10,
                           lblImage->width(), lblImage->height());
    //qDebug()
}

void BiometricAuthWidget::startAuth(DeviceInfoPtr device, int uid)
{
    if(!proxy)
    {
        qWarning() << "BiometricProxy doesn't exist.";
        return;
    }

    if(isInAuth)
    {
        qDebug() << "Identification is currently under way, stop it";
        stopAuth();
    }

    this->device = device;
    this->uid = uid;
    this->userName = getpwuid(uid)->pw_name;
    this->failedCount = 0;
    this->timeoutCount = 0;
    this->beStopped = false;
    proxy->StopOps(device->id);
    startAuth_();

    if(device->deviceType != DeviceType::Type::Face){
        updateImage(1);
    }

}

void BiometricAuthWidget::startAuth_()
{
    lblDevice->setText(tr("Current device: ") + device->shortName);

    //qDebug().noquote() << QString("Identify:[drvid: %1, uid: %2]").arg(1).arg(2);

    isInAuth = true;
    dup_fd = -1;

    QDBusPendingCall call = proxy->Identify(device->id, uid);
    QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(call, this);
    connect(watcher, &QDBusPendingCallWatcher::finished,
            this, &BiometricAuthWidget::onIdentifyComplete);

}

void BiometricAuthWidget::stopAuth()
{
    beStopped = true;
    if(!isInAuth)
    {
        return;
    }

    proxy->StopOps(device->id);
    if(retrytimer&&retrytimer->isActive()){
        retrytimer->stop();
        delete retrytimer;
        retrytimer = nullptr;
    }
    isInAuth = false;
    updateImage(0);
}

void BiometricAuthWidget::onIdentifyComplete(QDBusPendingCallWatcher *watcher)
{
    QDBusPendingReply<int, int> reply = *watcher;
    if(reply.isError())
    {
        qWarning() << "Identify error: " << reply.error().message();
        Q_EMIT authComplete(false);
        updateImage(0);
        return;
    }
    int result = reply.argumentAt(0).toInt();
    int authUid = reply.argumentAt(1).toInt();

    // 特征识别成功,而且用户id匹配
    if(result == DBUS_RESULT_SUCCESS && authUid == uid)
    {
        qDebug() << "Identify success";
        Q_EMIT authComplete(true);
    }
    // 特征识别不匹配
    else if(result == DBUS_RESULT_NOTMATCH)
    {
        if(usebind){
            Q_EMIT authComplete(false);
            return;
        }
        qDebug() << "Identify failed";
        failedCount++;
        if(failedCount >= GetMaxFailedAutoRetry(userName))
        {
            Q_EMIT authComplete(false);
        }
        else
        {
            lblNotify->setText(tr("Identify failed, Please retry."));
            if(!beStopped){
               // QTimer::singleShot(1000, this, &BiometricAuthWidget::startAuth_);
                if(!retrytimer){
                    retrytimer = new QTimer(this);
                    retrytimer->setSingleShot(true);
                    connect(retrytimer, &QTimer::timeout, this, &BiometricAuthWidget::startAuth_);
                 }
                 retrytimer->start(1000);
             }
        }
    }
    //识别发生错误
    else if(result == DBUS_RESULT_ERROR)
    {
        if(usebind){
            Q_EMIT authComplete(false);
            return;
        }
        StatusReslut ret = proxy->UpdateStatus(device->id);
        //识别操作超时
        if(ret.result == 0 && ret.opsStatus == IDENTIFY_TIMEOUT)
        {
            timeoutCount++;
            if(timeoutCount >= GetMaxTimeoutAutoRetry(userName))
            {
                Q_EMIT authComplete(false);
            }
            else
            {
                QTimer::singleShot(1000, [&]{
                    if(!beStopped)
                    {
                        startAuth_();
                    }
                });
            }
        }else{
	    Q_EMIT authComplete(false);
	}
    }else{
        Q_EMIT authComplete(false);
    }
    updateImage(0);
}

void BiometricAuthWidget::onFrameWritten(int drvid)
{

    if(dup_fd == -1){
        dup_fd = get_server_gvariant_stdout(drvid);
    }

    if(dup_fd <= 0)
        return ;

    cv::Mat img;
    lseek(dup_fd, 0, SEEK_SET);
    char base64_bufferData[1024*1024];
    int rc = read(dup_fd, base64_bufferData, 1024*1024);
    printf("rc = %d\n", rc);

    cv::Mat mat2(1, sizeof(base64_bufferData), CV_8U, base64_bufferData);
    img = cv::imdecode(mat2, cv::IMREAD_COLOR);
    cv::cvtColor(img,img,cv::COLOR_BGR2RGB);

    QImage srcQImage = QImage((uchar*)(img.data), img.cols, img.rows, QImage::Format_RGB888);
    lblImage->setFixedSize(160,160);
    lblImage->setGeometry((width() - lblImage->width()) / 2,
                           lblDevice->geometry().bottom() + 10,
                           lblImage->width(), lblImage->height());
    lblImage->setPixmap(QPixmap::fromImage(srcQImage).scaled(lblImage->size()));

}

void BiometricAuthWidget::onStatusChanged(int drvid, int status)
{
    if(!isInAuth)
    {
        return;
    }
    if(drvid != device->id)
    {
        return;
    }

    // 显示来自服务的提示信息
    if(status == STATUS_NOTIFY)
    {
        QString notifyMsg = proxy->GetNotifyMesg(drvid);
        lblNotify->setText(notifyMsg);
    }
}

static int count = 0;
void BiometricAuthWidget::updateImage(int type)
{
    if(device->deviceType == DeviceType::Type::Face)
        return ;

    if(type == 0)
    {
        if(movieTimer && movieTimer->isActive())
        {
            movieTimer->stop();
        }

        QString imagePath = QString(UKUI_BIOMETRIC_IMAGES_PATH "%1/01.png")
                .arg(DeviceType::getDeviceType(device->deviceType));
        setImage(imagePath);
    }
    else
    {
        if(!movieTimer)
        {
            movieTimer = new QTimer(this);
            movieTimer->setInterval(100);
            connect(movieTimer, &QTimer::timeout,
                    this, &BiometricAuthWidget::onMoviePixmapUpdate);
        }
        count = 0;
        movieTimer->start();
    }
}

void BiometricAuthWidget::onMoviePixmapUpdate()
{
    if(count >= 18)
    {
        count = 0;
    }
    count++;
    QString fileName = (count < 10 ? "0" : "") + QString::number(count);
    QString imagePath = QString(UKUI_BIOMETRIC_IMAGES_PATH "%1/%2.png")
            .arg(DeviceType::getDeviceType(device->deviceType))
            .arg(fileName);
    setImage(imagePath);
}

void BiometricAuthWidget::setImage(const QString &path)
{
    QPixmap image(path);
    image = image.scaled(lblImage->width(), lblImage->height(),
                         Qt::IgnoreAspectRatio, Qt::SmoothTransformation);

    lblImage->setPixmap(image);
}

bool BiometricAuthWidget::getAuthDouble()
{
    QSettings settings("/etc/biometric-auth/ukui-biometric.conf", QSettings::IniFormat);
    bool distribId = settings.value("DoubleAuth").toBool();
    return distribId;
}

void BiometricAuthWidget::setMinImage(float val)
{
    resize(400,100+100*val);
    lblImage->setFixedSize(100*val, 100*val);
}