-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
167 lines (159 loc) · 4.68 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
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QFileDialog"
#include "QMessageBox"
#include "QDebug"
#include "QTableWidget"
#include "QComboBox"
#include "send/FileSendControl.h"
#include "util/zip.h"
#include <functional>
#include <boost/uuid/random_generator.hpp>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
console = FileSendControl::GetInstances();
console->RegistNoticeFrontCallBack(
std::bind(
NoticeFrontCallBack,
std::placeholders::_1,
std::placeholders::_2,
std::placeholders::_3,
this
)
);
console->Run();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_sendFile_clicked()
{
QString openFile="";
openFile=QFileDialog::getOpenFileName(this,"打开","./","");
if (openFile == "") return ;
console->SendFile(openFile.toStdString());
}
void MainWindow::on_sendDir_clicked()
{
QString openDir="";
openDir = QFileDialog::getExistingDirectory(this, "open");
if (openDir == "") return ;
console->SendFile(openDir.toStdString());
}
void MainWindow::NoticeFrontCallBack(const boost::uuids::uuid file_uuid,
const FileSendControl::Type type,
const std::vector<std::string> msg,
MainWindow *window) {
auto table = window->ui->tableWidget;
QTableWidgetItem *item_file = nullptr;
QTableWidgetItem *item_stat = nullptr;
QString file_name;
int rowIndex = -1;
int rowCnt = 0;
switch (type) {
case FileSendControl::kNewFile:
rowIndex = table->rowCount();
table->setRowCount(rowIndex+1);
file_name = QString::fromStdString(msg.front());
item_file = new QTableWidgetItem(file_name);
item_stat = new QTableWidgetItem("recving");
{std::lock_guard<std::mutex> lock(window->mutex_);
window->index_[file_uuid] = rowIndex;
}
table->setItem(rowIndex, 0, item_file);
table->setItem(rowIndex, 1, item_stat);
break;
case FileSendControl::kRecvend:
{std::lock_guard<std::mutex> lock(window->mutex_);
auto it = window->index_.find(file_uuid);
if (it != window->index_.end()) {
rowIndex = window->index_[file_uuid];
window->index_.erase(it);
} else {
break;
}
} //end lock
item_file = table->item(rowIndex, 0);
if (item_file) {
item_stat = table->item(rowIndex, 1);
}
if (item_stat) {
item_stat->setText("recvend");
} else {
qDebug() << "not found " << file_name;
}
break;
case FileSendControl::kSendend:
{std::lock_guard<std::mutex> lock(window->mutex_);
auto it = window->index_.find(file_uuid);
if (it != window->index_.end()) {
rowIndex = window->index_[file_uuid];
window->index_.erase(it);
} else {
break;
}
} //end lock
item_file = table->item(rowIndex, 0);
if (item_file) {
item_stat = table->item(rowIndex, 1);
}
if (item_stat) {
item_stat->setText("sendend");
}
break;
case FileSendControl::kNewSendFile:
rowIndex = table->rowCount();
table->setRowCount(rowIndex+1);
file_name = QString::fromStdString(msg.front());
item_file = new QTableWidgetItem(file_name);
item_stat = new QTableWidgetItem("sending");
{std::lock_guard<std::mutex> lock(window->mutex_);
window->index_[file_uuid] = rowIndex;
}
table->setItem(rowIndex, 0, item_file);
table->setItem(rowIndex, 1, item_stat);
break;
case FileSendControl::kClientExec:
{std::lock_guard<std::mutex> lock(window->mutex_);
auto it = window->index_.find(file_uuid);
if (it != window->index_.end()) {
rowIndex = window->index_[file_uuid];
window->index_.erase(it);
} else {
break;
}
} //end lock
item_file = table->item(rowIndex, 0);
if (item_file) {
item_stat = table->item(rowIndex, 1);
}
if (item_stat) {
item_stat->setText("all Receivend quit");
}
break;
case FileSendControl::kNetError:
{std::lock_guard<std::mutex> lock(window->mutex_);
auto it = window->index_.find(file_uuid);
if (it != window->index_.end()) {
rowIndex = window->index_[file_uuid];
window->index_.erase(it);
} else {
break;
}
} //end lock
item_file = table->item(rowIndex, 0);
if (item_file) {
item_stat = table->item(rowIndex, 1);
}
if (item_stat) {
item_stat->setText("all sendend quit");
}
break;
default :
qDebug() << "not found type in NoticeFrontCallBack() " << type;
}
}