-
Notifications
You must be signed in to change notification settings - Fork 1
/
startsettings.cpp
222 lines (161 loc) · 5.29 KB
/
startsettings.cpp
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
#include "startsettings.h"
#include"config.h"
#include"sam.h"
#include <limits.h> // HOST_NAME_MAX
#include<QVBoxLayout>
#include<QHBoxLayout>
#include<QPushButton>
#include<QIntValidator> // check port
#include<QDesktopWidget> // desktop()
#include<QApplication> // QApplication access
#include<QStringList> // for split reply of sam
#include<QCoreApplication>
#include<QDebug>
#include<QDir>
#include <QFile>
#include<QFileInfo>
#include<QSettings>
#include <QCryptographicHash>
#include"sockets.hpp"
#include"dbmain.hpp"
//#include"messagesdb.h"
#define SAM_DEFAULT_HOST "127.0.0.1"
#define SAM_DEFAULT_PORT 7656
static const char
* samWork = "<b>OK</b>",
* samNoWork = "<b>ERROR</b>",
* samWorkStyleOK = "QLabel {color:green;};",
* samWorkStyleBad = "QLabel {color:red;};";
inline void StartSettings::setLayouts(void){
vBox = new QVBoxLayout(this);
hBox = new QHBoxLayout(this);
this->setLayout(vBox);
vBox->addWidget(mainText);
vBox->addWidget(username);
vBox->addWidget(password);
vBox->addLayout(hBox);
hBox->addWidget(host);
hBox->addWidget(port);
hBox->addWidget(OK);
hBox->addWidget(CheckSAM);
vBox->addWidget(SAMWork);
}
inline void StartSettings::setLabels(void){
mainText = new QLabel(this),
SAMWork = new QLabel(this);
mainText->setFont(QFont("Arial Black",10));
SAMWork->setFont(QFont("Arial",12));
mainText->setAlignment( Qt::AlignCenter );
SAMWork->setAlignment( Qt::AlignCenter );
mainText->setText(
tr("Hello is start settings "
"Here you should set your SAM "
"host:port ")
);
SAMWork->hide();
mainText->show();
}
void StartSettings::Register(void){
qDebug() << "Register";
if(username->text().size() > 4 && password->text().size() > 6 && CheckSam() ){
// TODO: Add files
//delete this;
setVisible(false);
setEnabled(false);
QFileInfo finfo(QCoreApplication::applicationFilePath());
std::string pathF = finfo.absoluteDir().currentPath().toStdString() + "/" + CONFIG_PATH;
qDebug() << pathF.c_str();
QDir cdir( pathF.c_str() );
QFile Config( (pathF+"/config.ini").c_str() );
if(!cdir.exists()){
qDebug() << pathF.c_str() << "dir not exist";
cdir.mkdir(pathF.c_str());
}
if(!Config.open(QIODevice::WriteOnly ))
qCritical() << "Cannot open config file" << CONFIG_PATH << "config.ini";
Config.close();
QSettings settings((pathF+"/config.ini").c_str(), QSettings::NativeFormat);
settings.setValue("SAM/host",host->text());
settings.setValue("SAM/port",port->text());
settings.setValue("User/Username",username->text());
QByteArray passBytes(password->text().toStdString().c_str());
passBytes.append(username->text().toStdString().c_str()); // salt
settings.setValue("User/Password",QCryptographicHash::hash(passBytes, QCryptographicHash::Sha1 ));
dbmain db(true);
while(!db.getInstalled());
//db.install();
emit Registered(true);
}
}
inline void StartSettings::setConnects(void){
connect(
CheckSAM,
SIGNAL(clicked(bool)),
SLOT( CheckSam() ) );
connect(OK,SIGNAL(clicked(bool)),
SLOT( Register() ) );
}
inline void StartSettings::setWindowHint(void){
// this->setMinimumHeight(300);
// this->setMinimumWidth(300);
this->setWindowTitle( tr("First steps") );
this->setFixedSize(500,300);
QRect ScreenRect
= QApplication::desktop()->screenGeometry();
this->move(
ScreenRect.width()/4,
ScreenRect.height()/4
);
}
inline void StartSettings::setButtons(void){
OK = new QPushButton(this),
CheckSAM = new QPushButton(this);
OK->setText( tr("OK") );
CheckSAM->setText( tr("CheckSam") );
}
inline void StartSettings::setLineEdits(void){
QValidator * isPort
= new QIntValidator(1, 65535, this);
host = new QLineEdit(this);
port = new QLineEdit(this);
username = new QLineEdit(this);
password = new QLineEdit(this);
host->setPlaceholderText( tr("host") );
port->setPlaceholderText( tr("port") );
username->setPlaceholderText( tr("username") );
password->setPlaceholderText( tr("password") );
password->setEchoMode(QLineEdit::Password);
port->setValidator(isPort);
host->setMaxLength(HOST_NAME_MAX);
username->setMaxLength(30);
password->setMaxLength(128);
host->setText(SAM_DEFAULT_HOST);
port->setText( QString::number(SAM_DEFAULT_PORT) );
}
bool StartSettings::CheckSam(void){
sam Client((char*)host->text().toStdString().c_str(), atoi( port->text().toStdString().c_str()));
SAMWork->show();
if( !Client.getError() )
{
SAMWork->setStyleSheet(samWorkStyleOK);
SAMWork->setText(samWork);
return true;
}
SAMWork->setStyleSheet(samWorkStyleBad);
SAMWork->setText(samNoWork);
return false;
}
StartSettings::StartSettings(QWidget *parent) :
QWidget(parent)
{
this->setWindowHint();
this->setLabels();
this->setButtons();
this->setLineEdits();
this->setLayouts();
this->setConnects();
}
StartSettings::~StartSettings(){
// delete [] this->vBox;
// delete [] this->hBox;
}