-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
101 lines (89 loc) · 3.71 KB
/
mainwindow.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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QFileDialog>
#include <QDebug>
#include "utildefines.h"
#include "fileexchangeitemdelegate.h"
#include <QSettings>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
fileSystemModel(new QFileSystemModel(this)),
uploadsTableModel(new FileExchange::UploadsTableModel(this)),
downloadsTableModel(new FileExchange::DownloadsTableModel(this)),
fileServer(new FileServer(this))
{
ui->setupUi(this);
connect(ui->acceptConnectionsCheckBox, SIGNAL(toggled(bool)), this, SLOT(checkAcceptConnections()));
// Settings
QSettings settings;
ui->downloadFolderLineEdit->setText(settings.value("downloadFolder", QDir::home().absolutePath()).toString());
ui->acceptConnectionsCheckBox->setChecked(settings.value("acceptConnections", false).toBool());
ui->addressLineEdit->setText(settings.value("address", "localhost").toString());
// File System
fileSystemModel->setRootPath(QDir::currentPath());
ui->fileSystemTree->setModel(fileSystemModel);
ui->fileSystemTree->header()->setStretchLastSection(false);
ui->fileSystemTree->header()->setSectionResizeMode(0, QHeaderView::Stretch);
// Uploads
ui->uploadsTable->setModel(uploadsTableModel);
ui->uploadsTable->setItemDelegate(new FileExchangeItemDelegate(this));
ui->uploadsTable->horizontalHeader()->setDefaultAlignment(Qt::AlignLeft);
ui->uploadsTable->setColumnWidth(0, 160);
ui->uploadsTable->setColumnWidth(2, 80);
ui->uploadsTable->setColumnWidth(3, 80);
// Downloads
ui->downloadsTable->setModel(downloadsTableModel);
ui->downloadsTable->setItemDelegate(new FileExchangeItemDelegate(this));
ui->downloadsTable->horizontalHeader()->setDefaultAlignment(Qt::AlignLeft);
ui->downloadsTable->setColumnWidth(0, 160);
ui->downloadsTable->setColumnWidth(2, 80);
ui->downloadsTable->setColumnWidth(3, 80);
// File Server (downloads)
fileServer->setDownloadFolder(ui->downloadFolderLineEdit->text());
connect(fileServer, SIGNAL(signalIncomingConnection(qintptr,QString)), downloadsTableModel, SLOT(addNewDownload(qintptr,QString)));
checkAcceptConnections();
}
MainWindow::~MainWindow()
{
QSettings settings;
settings.setValue("downloadFolder", ui->downloadFolderLineEdit->text());
settings.setValue("acceptConnections", ui->acceptConnectionsCheckBox->isChecked());
settings.setValue("address", ui->addressLineEdit->text());
delete ui;
}
void MainWindow::on_downloadFolderBrowseButton_clicked()
{
QFileDialog dialog(this);
dialog.setOption(QFileDialog::ShowDirsOnly, true);
dialog.setFileMode(QFileDialog::DirectoryOnly);
if (dialog.exec()) {
ui->downloadFolderLineEdit->setText(dialog.selectedFiles().at(0));
fileServer->setDownloadFolder(ui->downloadFolderLineEdit->text());
}
}
void MainWindow::on_downloadFolderLineEdit_editingFinished()
{
fileServer->setDownloadFolder(ui->downloadFolderLineEdit->text());
}
void MainWindow::on_fileSystemTree_doubleClicked(const QModelIndex &index)
{
if (!fileSystemModel->isDir(index)) {
uploadsTableModel->addNewUpload(ui->addressLineEdit->text(), fileSystemModel->filePath(index));
}
}
void MainWindow::checkAcceptConnections()
{
if (ui->acceptConnectionsCheckBox->isChecked()) {
if (!fileServer->isListening()) {
if (!fileServer->listen(QHostAddress::Any, PORT)) {
QMessageBox::critical(this, tr("FileServer"),
tr("Unable to start the server: %1.")
.arg(fileServer->errorString()));
}
}
} else {
fileServer->close();
}
}