-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
363 lines (317 loc) · 9.24 KB
/
mainwindow.cpp
File metadata and controls
363 lines (317 loc) · 9.24 KB
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "editdirectoriesdialog.h"
#include <string>
#include <cstdlib>
#include <ctime>
#include <QMessageBox>
#include <QCommonStyle>
#include <QSettings>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
usersContextMenu(new QMenu()),
usersGroupsMenu(new QMenu()),
ui(new Ui::MainWindow),
conn(),
accountController(&conn)
{
srand(time(0));
ui->setupUi(this);
ui->tvUsers->setModel(accountController.getUserModel());
connect(&conn, SIGNAL(authSuccess()), this, SLOT(connectionAuthenticated()));
connect(&conn, SIGNAL(connFail(QString)), this, SLOT(connectionFailed(QString)));
connect(&conn, SIGNAL(connMessage(QString)), this, SLOT(connectionMessage(QString)));
connect(&conn, SIGNAL(replyReceived(FilezillaReply)), this, SLOT(serverReply(FilezillaReply)));
connect(&conn, SIGNAL(connectionClosed()), this, SLOT(connectionClosed()));
usersContextMenu->addAction(ui->actionEdit_user_directories);
usersContextMenu->addAction(ui->actionDelete_user);
usersContextMenu->addAction(ui->actionGroup_membership);
ui->actionGroup_membership->setMenu(usersGroupsMenu.data());
userModel = accountController.getUserModel();
buildGroupsMenu();
loadSettings();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_btnConnect_clicked()
{
conn.connectToHost(ui->leHost->text(), ui->lePort->text().toInt(), ui->leConnPassword->text());
}
void MainWindow::on_btnUpdate_clicked()
{
accountController.updateAccountSettings();
}
void MainWindow::connectionAuthenticated()
{
ui->textArea->appendPlainText("Authenticating");
ui->textArea->update();
}
void MainWindow::connectionFailed(const QString &reason)
{
ui->textArea->appendPlainText("Connection error: " + reason);
ui->textArea->update();
}
void MainWindow::connectionMessage(const QString &message)
{
ui->textArea->appendPlainText(message);
ui->textArea->update();
}
void MainWindow::connectionClosed()
{
ui->textArea->appendPlainText("Connection closed");
ui->textArea->update();
userModel->clear();
}
void MainWindow::serverReply(FilezillaReply reply)
{
if(reply.id == 0)
{
accountController.updateAccountSettings();
}
else if(reply.id == 6)
{
handleAccountSettingReply(reply);
}
}
QString MainWindow::getSelectedUser()
{
QModelIndexList selected = ui->tvUsers->selectionModel()->selectedIndexes();
if(selected.size() == 0)
{
return "";
}
return accountController.getUserModel()->data(selected[0], Qt::DisplayRole).toString();
}
void MainWindow::handleAccountSettingReply(FilezillaReply &reply)
{
if(reply.length == 1)
{
if((char)reply.data[0] == 0)
{
ui->textArea->appendPlainText("Account settings successfully sent.");
}
else if((char)reply.data[0] == 1)
{
ui->textArea->appendPlainText("Failed to send account settings.");
}
return;
}
ui->textArea->appendPlainText("Account settings received");
}
void MainWindow::buildGroupsMenu()
{
std::vector<QString> groups = accountController.getGroupNames();
usersGroupsMenu->clear();
QAction *nonAction = new QAction(usersGroupsMenu.data());
nonAction->setText(getNoneGroupMenuText());
nonAction->setCheckable(true);
nonAction->setChecked(false);
connect(nonAction, SIGNAL(triggered(bool)), this, SLOT(action_group_changed(bool)));
usersGroupsMenu->addAction(nonAction);
for(unsigned int i=0; i<groups.size(); i++)
{
QAction* ac = new QAction(usersGroupsMenu.data());
ac->setText(groups[i]);
ac->setCheckable(true);
ac->setChecked(false);
connect(ac, SIGNAL(triggered(bool)), this, SLOT(action_group_changed(bool)));
usersGroupsMenu->addAction(ac);
}
checkSelectedGroup(accountController.userGetGroup(ui->leUsername->text()));
}
void MainWindow::checkSelectedGroup(const QString &groupName)
{
if(groupName == "")
{
usersGroupsMenu->actions()[0]->setChecked(true);
}
else
{
bool found = false;
QList<QAction*> actions = usersGroupsMenu->actions();
for(int i=0; i<actions.size(); i++)
{
if(actions[i]->text() == groupName)
{
found = true;
actions[i]->setChecked(true);
break;
}
}
if(!found)
{
// If not found make it check none
checkSelectedGroup("");
}
}
}
void MainWindow::loadSettings()
{
QSettings* settings = getSettings();
if(!settings->contains("ftp_path"))
{
#ifdef WIN32
settings->setValue("ftp_path", "M:\\");
#else
settings->setValue("ftp_path", "");
#endif
}
ui->leDirLocation->setText(settings->value("ftp_path").toString());
}
QString MainWindow::getNoneGroupMenuText()
{
return "-- None --";
}
void MainWindow::on_btnAddUser_clicked()
{
bool res = accountController.createOrUpdateUser(ui->leUsername->text(),
ui->lePassword->text(),
ui->cbCreateServerDir->isChecked(),
ui->leDirLocation->text());
if(!res)
{
QMessageBox mbox;
mbox.setText("Failed to add or update user. Make sure username and password are not empty.");
mbox.setWindowTitle("Error");
mbox.exec();
}
}
void MainWindow::on_btnGeneratePass_clicked()
{
// Valid ranges: 48 - 57, 65-90, 97-122
// corresponding to 0-9, A-Z, a-z in ASCII.
QString passwd = "";
char temp;
while(true)
{
temp = (rand() % 75) + 48; // Generate a number between 48 and 122
if((temp >= 48 && temp <= 57) ||
(temp >= 65 && temp <= 90) ||
(temp >= 97 && temp <= 122))
{
passwd += QChar::fromAscii(temp);
}
// Break when the password is 8 chars.
if(passwd.length() == 8)
{
break;
}
}
ui->lePassword->setText(passwd);
}
void MainWindow::on_btnDelete_clicked()
{
deleteSelectedUser();
}
void MainWindow::on_tvUsers_clicked(const QModelIndex &index)
{
userSelectionChanged(index);
}
void MainWindow::on_tvUsers_activated(const QModelIndex &index)
{
userSelectionChanged(index);
}
void MainWindow::on_tvUsers_customContextMenuRequested(const QPoint &pos)
{
QModelIndex index = ui->tvUsers->indexAt(pos);
if(index.isValid())
{
if(userModel->flags(index) & Qt::ItemIsSelectable)
{
QPoint global = ui->tvUsers->mapToGlobal(pos);
usersContextMenu->popup(global);
buildGroupsMenu();
}
}
}
void MainWindow::on_actionDelete_user_triggered()
{
deleteSelectedUser();
}
void MainWindow::deleteSelectedUser()
{
QString username = getSelectedUser();
if(username.size() > 0)
{
QMessageBox mb;
mb.setText("Are you sure you want to delete user " + username + "? This can not be undone.");
mb.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
mb.setIcon(QMessageBox::Warning);
if(mb.exec() == QMessageBox::Yes)
{
accountController.removeUser(username);
}
}
}
void MainWindow::on_actionEdit_user_directories_triggered()
{
QString selected = getSelectedUser();
if(selected == "")
{
return;
}
EditDirectoriesDialog diag(accountController.getUserDirectories(selected));
int res = diag.exec();
if(res == QDialog::Accepted)
{
accountController.setUserDirectories(selected, diag.getDirectories());
}
}
void MainWindow::action_group_changed(bool checked)
{
QString user = getSelectedUser();
QString newGroup = "";
if(checked)
{
QString oldGroup = accountController.userGetGroup(user);
if(oldGroup == "")
{
oldGroup = getNoneGroupMenuText();
}
QList<QAction *> actions = usersGroupsMenu->actions();
for(int i=0; i<actions.size(); i++)
{
if(actions[i]->text() == oldGroup)
{
continue;
}
if(actions[i]->isChecked())
{
newGroup = actions[i]->text();
if(newGroup == getNoneGroupMenuText())
{
newGroup = "";
}
break;
}
}
}
accountController.userSetGroup(user, newGroup);
}
void MainWindow::userSelectionChanged(const QModelIndex &index)
{
ui->btnDelete->setEnabled(false);
if(ui->tvUsers->model()->flags(index) & Qt::ItemIsSelectable)
{
QVariant username = userModel->data(index, Qt::DisplayRole);
if(username.isValid())
{
ui->leUsername->setText(username.toString());
ui->btnDelete->setEnabled(true);
}
}
}
void MainWindow::closeEvent(QCloseEvent *event)
{
QSettings* settings = getSettings();
settings->setValue("ftp_path", ui->leDirLocation->text());
settings->sync();
QMainWindow::closeEvent(event);
}
QSettings* MainWindow::getSettings()
{
static QSettings settings("AndreasWass", "FilezillaUserAdmin");
return &settings;
}