-
Notifications
You must be signed in to change notification settings - Fork 8
/
qmaindialog.cpp
203 lines (159 loc) · 6.63 KB
/
qmaindialog.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
/*
* Author: Manash Kumar Mandal
* GitHub: http://github.com/manashmndl/QHotspotCreator
*
*
*
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
*
*
*/
#include "qmaindialog.h"
#include "ui_qmaindialog.h"
const QString QMainDialog::START_HOTSPOT = "/cnetsh wlan start hostednetwork";
const QString QMainDialog::STOP_HOTSPOT = "/cnetsh wlan stop hostednetwork";
const QString QMainDialog::CMD = "cmd.exe";
const QString QMainDialog::KEY_KEY = "passkey";
QString QMainDialog::KEY_VALUE = "password"; // Default value
const QString QMainDialog::SSID_KEY = "ssid"; //
QString QMainDialog::SSID_VALUE = "Hotspot"; // Default ssid value
const QString QMainDialog::GROUP = "hotspot_config";
const QString QMainDialog::DEFAULT_KEY = "password";
const QString QMainDialog::DEFAULT_SSID = "Hotspot";
const QString QMainDialog::STATUS_DEFAULT_VALUE = "off";
const QString QMainDialog::STATUS_KEY = "hotspot_status";
QString QMainDialog::STATUS_VALUE = "off";
const QString QMainDialog::HOTSPOT_STATUS_GROUP = "on_off_status";
QMainDialog::QMainDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::QMainDialog),
key(QMainDialog::KEY_VALUE),
ssid(QMainDialog::SSID_VALUE)
{
qDebug() << "Object Created with ssid: " << ssid;
qDebug() << "Object created with key: " << key;
ui->setupUi(this);
this->loadConfiguration();
this->enableStartButton(true);
//Connecting process with the output
connect(&hotspotCreatorProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(readyForOutput()));
QString buttonStatus = loadSettings(QMainDialog::STATUS_KEY, QMainDialog::HOTSPOT_STATUS_GROUP, QMainDialog::STATUS_DEFAULT_VALUE).toString ();
if (buttonStatus == "on"){
this->enableStartButton (false);
ui->statusLabel->setText ("Hotspot already started!");
} else {
this->enableStartButton (true);
ui->statusLabel->setText ("Hotspot stopped!");
}
}
QMainDialog::~QMainDialog()
{
saveSettings (QMainDialog::STATUS_KEY, QMainDialog::STATUS_VALUE, QMainDialog::HOTSPOT_STATUS_GROUP);
delete ui;
}
void QMainDialog::on_startButton_clicked()
{
if (checkKeyLength() && !isSSIDEmpty()){
QMainDialog::STATUS_VALUE = "on";
this->enableStartButton(false);
hotspotCreatorProcess.start(QMainDialog::CMD, QStringList() << QMainDialog::START_HOTSPOT, QProcess::ReadWrite);
} else {
if (!checkKeyLength() && !isSSIDEmpty()) QMessageBox::warning(this, "Password length is too short", "Password must be of length 8 character");
else if (checkKeyLength() && isSSIDEmpty()) QMessageBox::warning(this, "Enter Hotspot SSID", "Please enter hotspot name, can't keep it empty!");
else if (!checkKeyLength() && isSSIDEmpty()) QMessageBox::warning(this, "Required field empty", "Hotspot name field is empy and password length is less than required");
}
}
void QMainDialog::saveConfiguration()
{
ssid = ui->ssidLineEdit->text();
key = ui->passwordLineEdit->text();
QMainDialog::KEY_VALUE = key;
QMainDialog::SSID_VALUE = ssid;
//Saves settings
saveSettings(QMainDialog::KEY_KEY, QMainDialog::KEY_VALUE, QMainDialog::GROUP);
saveSettings(QMainDialog::SSID_KEY, QMainDialog::SSID_VALUE, QMainDialog::GROUP);
qDebug() << "pass: " << KEY_VALUE;
qDebug() << "ssid: " << SSID_VALUE;
}
void QMainDialog::loadConfiguration()
{
ssid = loadSettings(SSID_KEY, GROUP, DEFAULT_SSID).toString();
key = loadSettings(KEY_KEY, GROUP, DEFAULT_KEY).toString();
ui->ssidLineEdit->setText(ssid);
ui->passwordLineEdit->setText(key);
}
void QMainDialog::on_stopButton_clicked()
{
QMainDialog::STATUS_VALUE = "off";
this->enableStartButton(true);
hotspotCreatorProcess.start(QMainDialog::CMD, QStringList() << QMainDialog::STOP_HOTSPOT, QProcess::ReadWrite);
}
void QMainDialog::on_setConfigButton_clicked()
{
if (checkKeyLength() && !isSSIDEmpty()){
this->saveConfiguration();
QString _ssid = ui->ssidLineEdit->text();
QString _pass = ui->passwordLineEdit->text();
QString configComm = "/cnetsh wlan set hostednetwork ssid=" + _ssid + " key=" + _pass;
hotspotCreatorProcess.start(QMainDialog::CMD, QStringList() << configComm, QProcess::ReadWrite);
} else {
if (!checkKeyLength() && !isSSIDEmpty()) QMessageBox::warning(this, "Password length is too short", "Password must be of length 8 character");
else if (checkKeyLength() && isSSIDEmpty()) QMessageBox::warning(this, "Enter Hotspot SSID", "Please enter hotspot name, can't keep it empty!");
else if (!checkKeyLength() && isSSIDEmpty()) QMessageBox::warning(this, "Required field empty", "Hotspot name field is empy and password length is less than required");
}
}
void QMainDialog::enableStartButton(bool enable)
{
ui->startButton->setEnabled(enable);
ui->stopButton->setEnabled(!enable);
}
void QMainDialog::enableStopButton(bool enable)
{
ui->stopButton->setEnabled(enable);
ui->startButton->setEnabled(!enable);
}
bool QMainDialog::checkKeyLength()
{
return (ui->passwordLineEdit->text().length() >= MIN_KEY_LENGTH);
}
bool QMainDialog::isKeyEmpty()
{
return (ui->passwordLineEdit->text() == NULL);
}
bool QMainDialog::isSSIDEmpty()
{
return (ui->ssidLineEdit->text() == NULL);
}
//Save settings
void saveSettings(const QString &key, const QVariant &value, const QString &group)
{
QSettings settings;
settings.beginGroup(group);
settings.setValue(key, value);
settings.endGroup();
}
//Load settings
QVariant loadSettings(const QString &key, const QString &group, const QVariant &defaultValue = QVariant())
{
QSettings settings;
settings.beginGroup(group);
QVariant data = settings.value(key, defaultValue);
settings.endGroup();
return data;
}
//Read output from cmd
void QMainDialog::readyForOutput(void){
standardOutput = hotspotCreatorProcess.readAllStandardOutput();
qDebug() << standardOutput;
if (standardOutput.contains("network stopped")) ui->statusLabel->setText("Hotspot stopped!");
else if (standardOutput.contains("network started")) ui->statusLabel->setText("Hotspot Started");
else if (standardOutput.contains("SSID of the hosted network has been successfully changed")) ui->statusLabel->setText("Hotspot was configured successfully");
}