File: dialogpass.cpp

package info (click to toggle)
sfp-master 1.0.7-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 10,636 kB
  • sloc: cpp: 2,993; ansic: 489; xml: 125; sh: 21; makefile: 5
file content (146 lines) | stat: -rw-r--r-- 4,928 bytes parent folder | download
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
/*
 * Copyright (C) 2024 Mikhail Medvedev <e-ink-reader@yandex.ru>
 *
 *
 * 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 3
 * of the License, 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.
 */
#include "dialogpass.h"
#include "ui_dialogpass.h"
#include "mainwindow.h"
#include <QValidator>
//#include <QRegExp>
#include <QRegularExpression>

DialogPass::DialogPass(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogPass)
{
    ui->setupUi(this);
    QRegularExpression reHex3( "[0-2][A-Fa-f0-9]{2,2}" );
    QRegularExpression reHex2( "[A-Fa-f0-9]{1,2}" );
    QRegularExpressionValidator *validatorA = new QRegularExpressionValidator(reHex3, this);
    QRegularExpressionValidator *validatorL = new QRegularExpressionValidator(reHex2, this);
    ui->lineEdit->setValidator(validatorA);
    ui->lineEdit_2->setValidator(validatorL);
    ui->lineEdit_3->setValidator(validatorL);
    ui->lineEdit_4->setValidator(validatorL);
    ui->lineEdit_5->setValidator(validatorL);
}

void DialogPass::setID(const uint id, uint32_t userAddr, uint32_t userPass)
{
   uint8_t firstDigit = 0;
   switch (id)
   {
     case 0:
       ui->radioButton_0->setChecked(true);
     break;
     case 1:
       ui->radioButton_1->setChecked(true);
     break;
     case 2:
       ui->radioButton_2->setChecked(true);
     break;
     case 3:
       ui->radioButton_3->setChecked(true);
     break;
     case 4:
       ui->radioButton_4->setChecked(true);
     break;
     default:
       ui->radioButton_0->setChecked(true);
     break;

   }
   firstDigit = static_cast<unsigned char>(userAddr >> 8) + 0x30;
   if (firstDigit > 0x32) firstDigit = 0x30;
   ui->lineEdit->setText(QString(static_cast<char>(firstDigit)) + bytePrt(static_cast<unsigned char>(userAddr & 0xff)));
   ui->lineEdit_2->setText(bytePrt(static_cast<unsigned char>((userPass >> 24) & 0xff)));
   ui->lineEdit_3->setText(bytePrt(static_cast<unsigned char>((userPass >> 16) & 0xff)));
   ui->lineEdit_4->setText(bytePrt(static_cast<unsigned char>((userPass >>  8) & 0xff)));
   ui->lineEdit_5->setText(bytePrt(static_cast<unsigned char>(userPass & 0xff)));
}

DialogPass::~DialogPass()
{
    delete ui;
}

void DialogPass::on_pushButton_clicked()
{
    uint8_t id = 0;
    if (ui->radioButton_0->isChecked()) id = 0;
    if (ui->radioButton_1->isChecked()) id = 1;
    if (ui->radioButton_2->isChecked()) id = 2;
    if (ui->radioButton_3->isChecked()) id = 3;
    if (ui->radioButton_4->isChecked())
    {
        id = 4;
        setUserPassword();

    }
    //return id
    emit sendID(id);
    DialogPass::close();
}

void DialogPass::setUserPassword()
{
    uint32_t userAddr = 0, userPass = 0;
    QString buf = "";
    if ((ui->lineEdit->text().isEmpty()) || (ui->lineEdit_2->text().isEmpty()) || (ui->lineEdit_3->text().isEmpty()) || (ui->lineEdit_4->text().isEmpty()) || (ui->lineEdit_5->text().isEmpty()))
    {
        QMessageBox::about(this, tr("Error"), tr("Invalid field value."));
        return;
    }
   if (ui->lineEdit_2->text().length() == 1) ui->lineEdit_2->setText("0" + ui->lineEdit_2->text());
   if (ui->lineEdit_3->text().length() == 1) ui->lineEdit_3->setText("0" + ui->lineEdit_3->text());
   if (ui->lineEdit_4->text().length() == 1) ui->lineEdit_4->setText("0" + ui->lineEdit_4->text());
   if (ui->lineEdit_5->text().length() == 1) ui->lineEdit_5->setText("0" + ui->lineEdit_5->text());
   userAddr = hexToInt(ui->lineEdit->text());
   buf = ui->lineEdit_2->text() +  ui->lineEdit_3->text() +  ui->lineEdit_4->text() +  ui->lineEdit_5->text();
   userPass = hexToInt(buf);
   emit sendUserPass(userAddr, userPass);
}

uint32_t DialogPass::hexToInt(QString str)
{
    unsigned char c;
    uint32_t len = static_cast<uint32_t>(str.length());
    QByteArray bstr = str.toLocal8Bit();
    if ((len > 0) && (len < 9))
    {
        uint32_t i, j = 1;
        uint32_t  addr = 0;
        for (i = len; i >0; i--)
        {
           c = static_cast<unsigned char>(bstr[i-1]);
           if ((c >= 0x30) && (c <=0x39)) addr =  addr + (c - 0x30) * j;
           if ((c >= 0x41) && (c <= 0x46)) addr = addr + (c - 0x37) * j;
           if ((c >= 0x61) && (c <= 0x66)) addr = addr + (c - 0x57) * j;
        j = j * 16;
        }
        return addr;
    }
    else return 0;
}

QString DialogPass::bytePrt(unsigned char z)
{
    unsigned char s;
    s = z / 16;
    if (s > 0x9) s = s + 0x37;
    else s = s + 0x30;
    z = z % 16;
    if (z > 0x9) z = z + 0x37;
    else z = z + 0x30;
    return QString(static_cast<char>(s)) + QString(static_cast<char>(z));
}