-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wizard.cpp
434 lines (339 loc) · 12.8 KB
/
wizard.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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#include "wizard.h"
#include "ui_wizard.h"
#include <QDir>
#include <QFileDialog>
#include <QDebug>
#include <QMessageBox>
#include <QInputDialog>
#include <QDesktopServices>
Wizard::Wizard(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::Wizard)
{
ui->setupUi(this);
ui->PathED->setText(Lastpath);
ui->SavePathEd->setText(QStandardPaths::writableLocation(QStandardPaths::HomeLocation));
Wizard::on_BtnLoad_released();
for (int i =1;i<60 ;i++ ) {
ui->CmbxCronFreq->addItem(QString::number(i));
}
process = new QProcess(this);
//connect(process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),[=](int exitCode, QProcess::ExitStatus exitStatus){ Wizard::processFinished(exitCode,exitStatus); });
connect(process, &QProcess::stateChanged, this, &Wizard::addlabelstatus);
connect(process, &QProcess::readyReadStandardError, this, &Wizard::processreadyReadStandardError);
connect(process, &QProcess::readyReadStandardOutput, this, &Wizard::processreadyReadStandardOutput);
connect(process, &QProcess::errorOccurred, this, &Wizard::QPerrorOccurred);
//check py version and addapt py-hiknetextractor to OS
Requiere();
}
Wizard::~Wizard()
{
delete ui;
}
void Wizard::on_BtnPath_released()
{
QUrl _IntputFolder = QFileDialog::getExistingDirectory(this,
(tr("Select Folder to read")), Lastpath);
//Hist = FsDialog->history();
if (_IntputFolder.isEmpty() == false) {
Lastpath = _IntputFolder.path();
ui->PathED->setText(Lastpath);
}
return;
}
void Wizard::on_BtnAdd_released()
{
QString DestDir = Lastpath + "/" + ui->CamNameED->text() + "/";
QString SrcDir = GetSrce() + "/HikNetExtractor/template/";
if (!QDir(DestDir).exists()) {
QDir().mkdir(DestDir);
}
QDir directory(SrcDir);
QStringList filters;
filters << "*.py";
QStringList filesList = directory.entryList(QDir::Files);
directory.setNameFilters(filters);
QString fileName;
foreach(fileName, filesList)
{
if(fileName != "config.py")
if(QFile::remove(DestDir + fileName))
qDebug() << "Removed: " << fileName;
QFile::copy(SrcDir + fileName, DestDir + fileName);
qDebug() << "Copied: " << fileName;
}
QString newFileData;
QFile ConfFs(DestDir + "config.py");
if(!ConfFs.open(QIODevice::ReadOnly))
exit(1);
QTextStream in(&ConfFs);
//Read file line by line until it reaches the end
while(!in.atEnd())
{
QString line = in.readLine();
if (line.startsWith("#"))
{
newFileData.append(line + '\n');
continue;
}
if (line.startsWith("url"))
line = "url = '" + ui->IpEd->text() + ":" + ui->PortHttpEd->text() + "'";
if (line.startsWith("username"))
line = "username = '" + ui->UserEd->text() + "'";
if (line.startsWith("password"))
line = "password = '" + ui->PassEd->text() + "'";
if (line.startsWith("Channel"))
line = "Channel = '" + ui->ChannelEd->text() + "'";
if (line.startsWith("SavePath"))
line = "SavePath = '" + ui->SavePathEd->text() + "'";
if (line.startsWith("CamName"))
line = "CamName = '" + ui->CamNameED->text() + "'";
if (line.startsWith("DaysToKeep"))
line = "DaysToKeep = '" + ui->DaysToKeepEd->text() + "'";
if (line.startsWith("UtcTOfset")){
if (ui->UtcTOfset->isChecked()){
line = "UtcTOfset = 'yes'";
}
else
{
line = "UtcTOfset = 'no'";
}
}
newFileData.append(line + '\n');
}
ConfFs.close();
if(!ConfFs.open(QIODevice::WriteOnly | QIODevice::Truncate))
exit(1);
//Write the data to the output file
QTextStream out(&ConfFs);
out << newFileData;
//ConfFs.write(newFileData.toUtf8().data());
ConfFs.flush();
ConfFs.close();
}
QString Wizard::GetSrce()
{
QStringList SrcDir = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);
QString HikLocation = "";
foreach (QString Srce, SrcDir) {
HikLocation.append(Srce + "/HikNetExtractor\n\n");
if (QDir(Srce + "/HikNetExtractor").exists())
return Srce;
}
QMessageBox msgBox(this);
msgBox.setWindowTitle("Missing depandancys");
msgBox.addButton(QMessageBox::Open);
msgBox.addButton(QMessageBox::Cancel);
msgBox.addButton("RPM", QMessageBox::AcceptRole);
msgBox.addButton("Deb", QMessageBox::ApplyRole);
msgBox.setText("hiknetextractor-python not found in\n" + HikLocation + "\nIf you are under Linux you can use Deb or RPM button and follow instruction to add repository and install hiknetextractor-python in the good directory, if you are not Linuxien, you need to use the open button to download and extract manualy hiknetextractor-python in the good directory.");
int Answer = msgBox.exec();
if (Answer == QMessageBox::Open)
QDesktopServices::openUrl(QUrl("https://github.com/surfzoid/HikNetExtractor/archive/refs/heads/main.zip", QUrl::TolerantMode));
if (Answer == 0)
QDesktopServices::openUrl(QUrl("https://copr.fedorainfracloud.org/coprs/surfzoid/HikNetExtractor/", QUrl::TolerantMode));
if (Answer == 1)
QDesktopServices::openUrl(QUrl("https://launchpad.net/~surfzoid/+archive/ubuntu/hikvision", QUrl::TolerantMode));
exit(1);
return "/usr/share";
}
void Wizard::on_BtnCancel_released()
{
close();
}
void Wizard::on_BtnSavePath_released()
{
QUrl _IntputFolder = QFileDialog::getExistingDirectory(this,
(tr("Select Folder to save videos")), ui->SavePathEd->text());
//Hist = FsDialog->history();
if (_IntputFolder.isEmpty() == false)
ui->SavePathEd->setText(_IntputFolder.path() + "/");
return;
}
void Wizard::on_BtnLoad_released()
{
QFile ConfFs(ui->PathED->text() + "/" + ui->CamNameED->text() + "/"+ "config.py");
if (!ConfFs.exists())
return;
if(!ConfFs.open(QIODevice::ReadOnly))
exit(1);
QTextStream in(&ConfFs);
//Read file line by line until it reaches the end
while(!in.atEnd())
{
QString line = in.readLine();
if (line.startsWith("url"))
{
QStringList IpPort = GetParam(line).split(":");
ui->IpEd->setText( IpPort.at(0));
if (IpPort.length()>1) {
ui->PortHttpEd->setText( IpPort.at(1));
}
else {
ui->PortHttpEd->setText( "80");
}
}
if (line.startsWith("username"))
ui->UserEd->setText(GetParam(line));
if (line.startsWith("password"))
ui->PassEd->setText(GetParam(line));
if (line.startsWith("Channel"))
ui->ChannelEd->setText(GetParam(line));
if (line.startsWith("SavePath"))
ui->SavePathEd->setText(GetParam(line));
if (line.startsWith("CamName"))
ui->CamNameED->setText(GetParam(line));
if (line.startsWith("DaysToKeep"))
ui->DaysToKeepEd->setText(GetParam(line));
if (line.startsWith("UtcTOfset")){
if (GetParam(line) == "yes"){
ui->UtcTOfset->setChecked(true);
}
else
{
ui->UtcTOfset->setChecked(false);
}
}
}
ConfFs.close();
//add the load of timer value here
QFile SystemdTimer(SystemdPath + ui->CamNameED->text() + ".timer");
if (!SystemdTimer.exists())
return;
if(!SystemdTimer.open(QIODevice::ReadOnly))
exit(1);
QTextStream inTime(&SystemdTimer);
//Read file line by line until it reaches the end
while(!inTime.atEnd())
{
QString FindTime = inTime.readLine();
if (FindTime.startsWith("OnUnitActiveSec="))
{
FindTime = FindTime.remove("OnUnitActiveSec=").remove("min");
ui->CmbxCronFreq->setCurrentIndex(ui->CmbxCronFreq->findText(FindTime));
break;
}
}
SystemdTimer.close();
}
QString Wizard::GetParam(QString ToSplit)
{
QStringList RetL = ToSplit.split(" = ");
QString Ret = RetL.at(1);
return Ret.remove("'").remove("\"");
}
void Wizard::on_PathED_textEdited(const QString &arg1)
{
Lastpath = arg1;
}
void Wizard::on_BtnCronCreate_released()
{
#if (defined(_WIN32))
process->start("control", QStringList() << "schedtasks");
#elif ((defined(__linux__) | defined(__APPLE__)) & !defined(__ANDROID__))
QString SystemdServiceTemplatecache = SystemdServiceTemplate;
QString SystemdTimerTemplatecache = SystemdTimerTemplate;
SystemdServiceTemplate = SystemdServiceTemplate.replace("CAM",ui->CamNameED->text()).replace("HikNetExtractor.py",ui->PathED->text() + "/" + ui->CamNameED->text() + "/"+ "HikNetExtractor.py");
SystemdTimerTemplate = SystemdTimerTemplate.replace("CAM",ui->CamNameED->text()).replace("00MM",ui->CmbxCronFreq->currentText());
QFile SystemdService(SystemdPath + ui->CamNameED->text() + ".service");
QFile SystemdTimer(SystemdPath + ui->CamNameED->text() + ".timer");
if(!SystemdService.open(QIODevice::WriteOnly | QIODevice::Truncate))
exit(1);
if(!SystemdTimer.open(QIODevice::WriteOnly | QIODevice::Truncate))
exit(1);
SystemdService.write(SystemdServiceTemplate.toUtf8());
SystemdTimer.write(SystemdTimerTemplate.toUtf8());
SystemdService.flush();
SystemdTimer.flush();
SystemdService.close();
SystemdTimer.close();
SystemdServiceTemplate = SystemdServiceTemplatecache;
SystemdTimerTemplate = SystemdTimerTemplatecache;
processIsFinished = false;
process->start("systemctl", QStringList() << "--user" << "enable" << ui->CamNameED->text().toUtf8() + ".timer" << "--now");
while (!process->waitForFinished(10000)) {
}
process->start("systemctl", QStringList() << "--user" << "enable" << ui->CamNameED->text().toUtf8() + ".service" << "--now");
#endif
}
void Wizard::on_BtnDelTimer_released()
{
#if (defined(_WIN32))
process->start("control", QStringList() << "schedtasks");
#elif ((defined(__linux__) | defined(__APPLE__)) & !defined(__ANDROID__))
int ret = QMessageBox::warning(this,tr("Are you sure? "),"Permanently delete timer for this device",QMessageBox::Ok | QMessageBox::Cancel);
if (ret == QMessageBox::Cancel)
return;
QFile::remove(SystemdPath + ui->CamNameED->text() + ".service");
QFile::remove(SystemdPath + ui->CamNameED->text() + ".timer");
QFile::remove(SystemdPath + "multi-user.target.wants/" + ui->CamNameED->text() + ".service");
QFile::remove(SystemdPath + "multi-user.target.wants/" + ui->CamNameED->text() + ".timer");
ui->TxtDebug->append("Timer deleted");
#endif
}
void Wizard::on_BtnDuplicate_released()
{
bool ok;
QString NewDevice = QInputDialog::getText(this, tr("New Device"), tr("Enter a new devce name: "), QLineEdit::Normal,ui->CamNameED->text(), &ok);
if (ok && !NewDevice.isEmpty())
{
ui->CamNameED->setText(NewDevice);
Wizard::on_BtnAdd_released();
Wizard::on_BtnCronCreate_released();
}
}
void Wizard::processFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
ui->TxtDebug->append(QString::number(exitCode));
ui->TxtDebug->append(QString::number(exitStatus));
processIsFinished = true;
}
void Wizard::addlabelstatus(QProcess::ProcessState newState)
{
/*switch(newState) {
<...>
};
if (newState == 0)
ui->TxtDebug->append(QString::number(newState));*/
}
void Wizard::processreadyReadStandardError()
{
ui->TxtDebug->append(process->readAllStandardError());
}
void Wizard::processreadyReadStandardOutput()
{
ui->TxtDebug->append(process->readAllStandardOutput());
}
void Wizard::Requiere()
{
ProcCase=0;
process->start("python", QStringList() << "--version");
}
void Wizard::QPerrorOccurred(QProcess::ProcessError error)
{
//0=py vers;1=hiknetextractor-python;
switch(ProcCase) {
case 0:
if (error==0)
{
QMessageBox msgBox(this);
msgBox.setWindowTitle("Python Helper");
#if (defined(_WIN32))
msgBox.addButton(QMessageBox::Open);
msgBox.addButton(QMessageBox::Cancel);
msgBox.setText("Python not found. Do you want to open the download page and install it?");
if (msgBox.exec() == QMessageBox::Open)
QDesktopServices::openUrl(QUrl("https://www.python.org/downloads/windows/", QUrl::TolerantMode));
#elif ((defined(__linux__) | defined(__APPLE__)) & !defined(__ANDROID__))
msgBox.setText("Python not found. Install it with your package manager");
msgBox.exec();
#endif
}
break;
case 1:
break;
default: ;
ProcCase=-1;
break;
};
}