-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainFrame.cpp
342 lines (281 loc) · 10.9 KB
/
MainFrame.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
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
#include <QtGui>
#include <QMenuBar>
#include <QStatusBar>
#include <QLabel>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QMessageBox>
#include <QNetworkReply>
#include <QProgressBar>
#include <QAuthenticator>
#include <iostream>
#include <QFileDialog>
#include <QInputDialog>
#include <QVBoxLayout>
#include <QHeaderView>
#include <QProgressDialog>
#include <QAbstractNetworkCache>
#include <QSplashScreen>
#include "MainFrame.h"
#include "JIGSNetworkReply.h"
#include "JIGSProgressDialog.h"
#include "qftp.h"
FtpApp::FtpApp()
{
manager = new QNetworkAccessManager(this);
setWindowTitle("JIGS File Sharing");
setGeometry(430,300,555,400);
QMovie *movie = new QMovie(":/images/waiting.gif");
processLabel = new QLabel(this);
processLabel->setMovie(movie);
movie->start();
splash = new QSplashScreen;
splash->setPixmap(QPixmap(":/images/FtpSplash.png"));
splash->show();
createStatusBar();
createActions();
createMenus();
fileList = new QTreeWidget;
fileList->setEnabled(false);
fileList->setRootIsDecorated(false);
fileList->setHeaderLabels(QStringList() << tr("Name") << tr("Size") << tr("Owner") << tr("Group") << tr("Time"));
fileList->setColumnWidth(0,150);
connect(fileList,SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),this,SLOT(downloadFile(QTreeWidgetItem*)));
getFiles();
}
void FtpApp::getFiles()
{
Qt::Alignment leftBottom = Qt::AlignLeft | Qt::AlignBottom;
splash->showMessage("Looking up host...",leftBottom,Qt::white);
statusLabel->setText("Looking up host...");
ftp = new QFtp(this);
fileList->clear();
connect(ftp, SIGNAL(listInfo(QUrlInfo)),
this, SLOT(addToList(QUrlInfo)));
connect(ftp,SIGNAL(stateChanged(int)),this,SLOT(changeOfState(int)));
QUrl url("ftp://ftp.ftpjigs.comze.com/public_html/");
url.setPort(21);
url.setUserName("a1996228");
url.setPassword("11107jigs");
std::cout<<qPrintable(url.path())<<"\n";
ftp->connectToHost(url.host(),url.port(21));
ftp->login(url.userName(),url.password());
}
void FtpApp::changeOfState(int state)
{
Qt::Alignment leftBottom = Qt::AlignLeft | Qt::AlignBottom;
if(state==2){
splash->showMessage("Connecting...",leftBottom,Qt::white);
statusLabel->setText("Connecting...");
}
else if(state==3){
splash->showMessage("Connected...Loging In...",leftBottom,Qt::white);
statusLabel->setText("Connected...Loging In...");
}
else if(state==4){
splash->showMessage("Logged In...Getting file list...",leftBottom,Qt::white);
statusLabel->setText("Logged In...Getting file list...");
fileList->setEnabled(true);
ftp->cd("public_html");
ftp->list();
}
}
void FtpApp::addToList(const QUrlInfo &urlInfo)
{
QTreeWidgetItem *item = new QTreeWidgetItem;
item->setText(0, urlInfo.name());
item->setText(1, QString::number(float(urlInfo.size())/1000)+"KB");
item->setText(2, urlInfo.owner());
item->setText(3, urlInfo.group());
item->setText(4, urlInfo.lastModified().toString("MMM dd yyyy"));
fileSize[urlInfo.name()]=urlInfo.size();
QPixmap pixmap(urlInfo.isDir() ? ":/images/dir.png" : ":/images/file.png");
item->setIcon(0, pixmap);
fileList->addTopLevelItem(item);
if (!fileList->currentItem()) {
fileList->setCurrentItem(fileList->topLevelItem(0));
fileList->setEnabled(true);
}
statusLabel->setText("Status");
this->setContentsMargins(0,0,0,0);
this->setCentralWidget(fileList);
//if(processLabel!=0) {delete processLabel;std::cout<<"Success";}
splash->close();
this->show();
}
void FtpApp::createMenus()
{
file = menuBar()->addMenu(tr("&File"));
file->addAction(refresh);
file->addSeparator();
file->addAction(quitAction);
upload = menuBar()->addMenu(tr("&Upload"));
upload->addAction(uploadFileAction);
download = menuBar()->addMenu(tr("&Download"));
download->addAction(downloadFileAction);
help=menuBar()->addMenu(tr("&Help"));
help->addAction(aboutAction);
}
void FtpApp::createActions()
{
refresh = new QAction("&Refresh",this);
refresh->setStatusTip("Refresh the list of files");
refresh->setShortcut(Qt::Key_F5);
connect(refresh,SIGNAL(triggered()),this,SLOT(refreshList()));
quitAction = new QAction(tr("&Quit"),this);
quitAction->setStatusTip("Quit the JIGS File Sharing");
quitAction->setShortcut(Qt::CTRL + Qt::Key_Q);
connect(quitAction, SIGNAL(triggered()), this, SLOT(quit()));
aboutAction=new QAction(tr("&About"),this);
aboutAction->setStatusTip("About JIGS File Sharing");
connect(aboutAction, SIGNAL(triggered()), this, SLOT(aboutPopup()));
uploadFileAction=new QAction("&Upload Single File",this);
uploadFileAction->setShortcut(Qt::CTRL + Qt::Key_U);
uploadFileAction->setStatusTip("Upload a single File to the FTP Server");
connect(uploadFileAction,SIGNAL(triggered()),this,SLOT(uploadFile()));
downloadFileAction = new QAction("&Download Single File",this);
downloadFileAction->setShortcut(Qt::CTRL + Qt::Key_D);
downloadFileAction->setStatusTip("Download a single file from FTP sever");
connect(downloadFileAction,SIGNAL(triggered()),this,SLOT(downloadFile()));
}
void FtpApp::refreshList()
{
fileList->clear();
fileSize.clear();
//this->setContentsMargins(250,0,0,0);
//this->setCentralWidget(processLabel);
getFiles();
}
void FtpApp::quit()
{
close();
QCoreApplication::exit();
}
void FtpApp::createStatusBar()
{
statusLabel = new QLabel("Status");
statusBar()->addWidget(statusLabel);
}
void FtpApp::uploadFile(){
progressBar = new QProgressBar();
progressBar->setAlignment(Qt::AlignCenter);
// progressBar->setWindowFlags(Qt::WindowMinimizeButtonHint | Qt::WindowTransparentForInput);
progressBar->setMinimum(0);
progressBar->setMaximum(100);
progressBar->setFixedWidth(300);
manager = new QNetworkAccessManager(this);
QString fileName = QFileDialog::getOpenFileName(this,"Select File to Upload","","*");
if(fileName.isNull())return;
QFile *file = new QFile(fileName);
QFileInfo fileInfo(*file);
file->open(QIODevice::ReadOnly);
progressBar->setWindowTitle(fileInfo.fileName());
// progressBar->setFixedSize(sizeHint());
QString address("ftp://ftp.ftpjigs.comze.com/public_html/");
address.append(fileInfo.fileName());
url.setUrl(address);
url.setPort(21);
url.setUserName("a1996228");
url.setPassword("11107jigs");
QNetworkRequest upload(url);
progressBar->show();
QNetworkReply* reply =manager->put(upload,file);
connect(reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(checkError(QNetworkReply::NetworkError)));
connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(uploadSuccess(QNetworkReply*)));
connect(reply,SIGNAL(uploadProgress(qint64,qint64)),this,SLOT(setMyValue(qint64,qint64)));
}
void FtpApp::setMyValue(qint64 a,qint64 b)
{
progressBar->setValue((int)a*100/b);
}
void FtpApp::aboutPopup()
{
QMessageBox msgBox(this);
msgBox.setText("The document has been modified.");
msgBox.exec();
}
void FtpApp::checkError(QNetworkReply::NetworkError e)
{
QMessageBox msgBox(this);
msgBox.setText("QNetworkReply::NetworkError "+e);
msgBox.exec();
progressBar->close();
}
void FtpApp::uploadSuccess(QNetworkReply *reply)
{
QMessageBox msgBox(this);
msgBox.setText("Upload Successfull : Congo!");
msgBox.exec();
progressBar->close();
reply->deleteLater();
}
void FtpApp::downloadFile()
{
QInputDialog qid(this);
qid.setLabelText("Enter file name:");
qid.exec();
QString fileName = qid.textValue();
if(fileName!=0){
QHash<QString, qint64>::const_iterator i = fileSize.find(fileName);
if(i==fileSize.end()) QMessageBox::information(this,"File Not Found","No such file is present on server");
else
{
QString file=QFileDialog::getSaveFileName(this,tr("Save Downloaded File"),"/home/gaurav/Downloads/"+fileName);
if(file!=0){
url.setUrl("ftp://ftp.ftpjigs.comze.com/public_html/"+fileName);
url.setPort(21);
url.setUserName("a1996228");
url.setPassword("11107jigs");
QNetworkRequest download(url);
statusLabel->setText("Downloading file...wait!");
JIGSProgressDialog *progressDialog=new JIGSProgressDialog();
progressDialog->show();
progressDialog->setTotal(fileSize[fileName]);
progressDialog->setFixedWidth(300);
JIGSNetworkReply *qreply=new JIGSNetworkReply(manager->get(download));
JIGSNetworkReply *reply=qreply->getJIGSNetworkReply();
reply->setFileName(file);
connect(reply->reply,SIGNAL(downloadProgress(qint64,qint64)),progressDialog,SLOT(setProgress(qint64)));
connect(reply->reply,SIGNAL(finished()),progressDialog,SLOT(setProgress()));
connect(reply,SIGNAL(downloadedData(QByteArray,QString)),this,SLOT(writeDownloadedFile(QByteArray,QString)));
connect(reply->reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(checkError(QNetworkReply::NetworkError)));
}
}
}
}
void FtpApp::writeDownloadedFile(QByteArray data,QString fileName)
{
if(data.size()!=0){
if(fileName!=0){
QFile file(fileName.toStdString().data());
if(!file.open(QIODevice::WriteOnly))
std::cout<<"Cannot be done now";
file.write(data);
}
}
statusLabel->setText("Status");
}
void FtpApp::downloadFile(QTreeWidgetItem* item)
{
QString fileName=item->text(0);
QString file=QFileDialog::getSaveFileName(this,tr("Save Downloaded File"),"/home/gaurav/Downloads/"+fileName);
if(file!=0){
url.setUrl("ftp://ftp.ftpjigs.comze.com/public_html/"+fileName);
url.setPort(21);
url.setUserName("a1996228");
url.setPassword("11107jigs");
QNetworkRequest download(url);
statusLabel->setText("Downloading file...wait!");
JIGSProgressDialog *progressDialog=new JIGSProgressDialog();
progressDialog->show();
progressDialog->setTotal(fileSize[fileName]);
progressDialog->setFixedWidth(300);
JIGSNetworkReply *qreply=new JIGSNetworkReply(manager->get(download));
JIGSNetworkReply *reply=qreply->getJIGSNetworkReply();
reply->setFileName(file);
connect(reply->reply,SIGNAL(downloadProgress(qint64,qint64)),progressDialog,SLOT(setProgress(qint64)));
connect(reply->reply,SIGNAL(finished()),progressDialog,SLOT(setProgress()));
connect(reply,SIGNAL(downloadedData(QByteArray,QString)),this,SLOT(writeDownloadedFile(QByteArray,QString)));
connect(reply->reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(checkError(QNetworkReply::NetworkError)));
}
}