forked from Shazib/CometFTP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownloadManager.cpp
More file actions
312 lines (247 loc) · 9.71 KB
/
DownloadManager.cpp
File metadata and controls
312 lines (247 loc) · 9.71 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
#include "DownloadManager.h"
DownloadManager::DownloadManager(QWidget *parent) :
QWidget(parent)
{
// Setup View + initial states
QHBoxLayout* mainLayout = new QHBoxLayout(this);
// Setup table widget
table = new QTableWidget();
table->horizontalHeader()->setVisible(true);
table->horizontalHeader()->show();
table->setSelectionBehavior(QAbstractItemView::SelectRows);
table->setSelectionMode(QAbstractItemView::SingleSelection);
table->setShowGrid(false);
table->setAlternatingRowColors(true);
table->setStyle(QStyleFactory::create("Fusion"));
table->setObjectName("ServerTableView");
table->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
table->verticalHeader()->setDefaultSectionSize(18);
mainLayout->addWidget(table);
this->setLayout(mainLayout);
numRows = 0;
table->setColumnCount(4);
table->setRowCount(numRows);
// Thread + queue management
percentage = 0;
fileCounter = 0;
downloading = false;
isAlive = false;
qRegisterMetaType<std::string>("std::string");
}
void DownloadManager::addData(QString _type,
QString _source,
QString _destination,
QString sftpType){
// The manager must check if folders are added.
// Add data to model
destination = _destination ;
// If Upload
if (_type == "Upload")
{
// Lets check if its a folder
QFileInfo info(_source);
if (info.isDir()){
// Get every individual file
addLocalFolder(info.absoluteFilePath());
} else {
numRows++;
table->setRowCount(numRows);
QTableWidgetItem* typeItem = new QTableWidgetItem(_type,1);
QTableWidgetItem* sourceItem = new QTableWidgetItem(_source,2);
QTableWidgetItem* destinationItem = new QTableWidgetItem(_destination,3);
QTableWidgetItem* statusItem = new QTableWidgetItem("Pending",3);
table->setItem((numRows-1),0,typeItem);
table->setItem((numRows-1),1,sourceItem);
table->setItem((numRows-1),2,destinationItem);
table->setItem((numRows-1),3,statusItem);
}
}
// If Download
if (_type == "Download"){
if (sftpType == "Folder"){
// Now get all values;
addServerFolder(_source,(_destination+"/"));
}
else {
numRows++;
table->setRowCount(numRows);
QTableWidgetItem* typeItem = new QTableWidgetItem(_type,1);
QTableWidgetItem* sourceItem = new QTableWidgetItem(_source,2);
QTableWidgetItem* destinationItem = new QTableWidgetItem(_destination,3);
QTableWidgetItem* statusItem = new QTableWidgetItem("Pending",3);
table->setItem((numRows-1),0,typeItem);
table->setItem((numRows-1),1,sourceItem);
table->setItem((numRows-1),2,destinationItem);
table->setItem((numRows-1),3,statusItem);
}
}
// When Data is added, the queue is automatically processed.
// The site will respond with a success or failure case and the queue will continue to process.
if (!isAlive) {
// Create thread
sftp = new SFTPSite();
thread = new QThread();
sftp->moveToThread(thread);
QObject::connect(this,SIGNAL(initThread(std::string,std::string,std::string,std::string)),sftp,SLOT(threadInit(std::string,std::string,std::string,std::string)));
thread->start();
emit initThread(host,user,password,port);
QObject::connect(this,SIGNAL(startDownload(QString,QString)),sftp,SLOT(startDownload(QString,QString)));
QObject::connect(this,SIGNAL(startUpload(QString,QString)),sftp,SLOT(startUpload(QString,QString)));
QObject::connect(sftp,SIGNAL(updateProgress()),this,SLOT(receivePercentage()));
QObject::connect(sftp,SIGNAL(downloadComplete(int)), this,SLOT(receiveDownloadComplete(int)));
QObject::connect(this,SIGNAL(sendCancelClick()),sftp,SLOT(cancelDownload()),Qt::DirectConnection);
QObject::connect(this,SIGNAL(sendPauseClick()),sftp,SLOT(pauseDownload()), Qt::DirectConnection);
QObject::connect(sftp,SIGNAL(sendSpeed(int)),this,SLOT(receiveSpeed(int)));
isAlive = true;
}
// Start Queue
if (!downloading){
// Start queue
// Get data
QString _type = table->item(fileCounter,0)->text();
QString _source = table->item(fileCounter,1)->text();
QString _destination = table->item(fileCounter,2)->text();
// If download
if (_type == "Download"){
table->item(fileCounter,3)->setText("Processing");
qDebug() << "Starting Download";
emit startDownload(_source, _destination);
downloading = true;
emit setFileName(_source);
qDebug() << "num rows - filecounter" << (numRows - fileCounter);
}
if (_type == "Upload"){
table->item(fileCounter,3)->setText("Processing");
qDebug() << "Starting Upload";
emit startUpload(_source, _destination);
downloading = true;
emit setFileName(_source);
qDebug() << "num rows - filecounter" << (numRows - fileCounter);
}
}
emit setNumFiles(numRows - fileCounter);
}
void DownloadManager::addLocalFolder(QString path)
{
//qDebug() << "add folder";
// qDebug() << "root folder" << path;
QDir dir(path);
// Get everything in directory
QFileInfoList list = dir.entryInfoList();
// For each file/folder
for (int i = 2; i < list.size(); i++){
// If is file
if (list.at(i).isFile()){
QFileInfo info = list.at(i);
//qDebug() << list.at(i).absoluteFilePath();
//qDebug() << destination;
numRows++;
table->setRowCount(numRows);
QTableWidgetItem* typeItem = new QTableWidgetItem("Upload",1);
QTableWidgetItem* sourceItem = new QTableWidgetItem(list.at(i).absoluteFilePath(),2);
QTableWidgetItem* destinationItem = new QTableWidgetItem(destination,3);
QTableWidgetItem* statusItem = new QTableWidgetItem("Pending",3);
table->setItem((numRows-1),0,typeItem);
table->setItem((numRows-1),1,sourceItem);
table->setItem((numRows-1),2,destinationItem);
table->setItem((numRows-1),3,statusItem);
// If is dir
} else if (list.at(i).isDir()){
// Recursive call
list.at(i).fileName();
destinationTemp = list.at(i).fileName() + "/";
destination = destination + destinationTemp;// + "/";
addLocalFolder(list.at(i).absoluteFilePath());
}
}
// Reset Destination folder for parent
destination.remove((destination.count()-destinationTemp.count()-1),destinationTemp.count());
//destination.remove(destination.count()-1,1);
destinationTemp = "";
}
void DownloadManager::addServerFolder(QString path, QString destination)
{
site = new SFTPSite(this, host, user, password, port);
if( !site->silent_init()) {
// ERROR
} else {
// Get all files
QString dest = path + "/";
QStringList values = site->getAllFiles(dest,destination);
int a = values.count();
while (a != 0){
qDebug() << values.count();
numRows++;
table->setRowCount(numRows);
QTableWidgetItem* typeItem = new QTableWidgetItem("Download",1);
QTableWidgetItem* sourceItem = new QTableWidgetItem(values.first(),2);
values.removeFirst();
a--;
QTableWidgetItem* destinationItem = new QTableWidgetItem(values.first(),3);
values.removeFirst();
a--;
QTableWidgetItem* statusItem = new QTableWidgetItem("Pending",4);
table->setItem((numRows-1),0,typeItem);
table->setItem((numRows-1),1,sourceItem);
table->setItem((numRows-1),2,destinationItem);
table->setItem((numRows-1),3,statusItem);
}
}
}
void DownloadManager::receiveCredentials(std::string _host, std::string _user, std::string _password, std::string _port){
host = _host;
user = _user;
password = _password;
port = _port;
}
void DownloadManager::receiveDownloadComplete(int a){
if ( a == DLOAD_CANCEL) {
table->item(fileCounter,3)->setText("Cancelled");
}
percentage = 0;
emit setProgress(0);
// A Download just completed.
table->item(fileCounter,3)->setText("Complete");
fileCounter++;
if (fileCounter > numRows-1){
// Set Value in table
downloading = false;
emit setNumFiles(0);
return;
}
// Otherwise
QString _type = table->item(fileCounter,0)->text();
QString _source = table->item(fileCounter,1)->text();
QString _destination = table->item(fileCounter,2)->text();
table->item(fileCounter,3)->setText("Processing");
if (_type == "Download"){
emit startDownload(_source,_destination);
} else if (_type == "Upload"){
emit startUpload(_source,_destination);
}
emit setNumFiles(numRows - fileCounter);
emit setFileName(_source);
}
void DownloadManager::receivePercentage()
{
percentage++;
emit setProgress(percentage);
}
void DownloadManager::receiveCancelClick()
{
emit setProgress(0);
usleep(10);
emit setSpeed(" ");
usleep(10);
emit sendCancelClick();
}
void DownloadManager::receivePauseClick()
{
emit sendPauseClick();
}
void DownloadManager::receiveSpeed(int bytes)
{
int bytesPerSecond = bytes / 5;
int speedInKb = bytesPerSecond / 1024;
emit setSpeed(QString::number(speedInKb));
}