-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
309 lines (263 loc) · 9.34 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
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
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/qnetworkreply.h>
#include <QDesktopServices>
#include <QFileDialog>
#include <QMessageBox>
#include <QString>
#include <QTimer>
void MainWindow::initStatusbar()
{
statusProgressBar = new QProgressBar(this);
statusLabelRemoteDirs = new QLabel(this);
statusLabelRemoteFiles = new QLabel(this);
statusProgressBar->setFixedHeight(ui->statusbar->height()-4);
statusLabelRemoteDirs->setText(statusLabelRemoteDirsText.arg('?'));
statusLabelRemoteFiles->setText(statusLabelRemoteFilesText.arg('?'));
ui->statusbar->addPermanentWidget(statusLabelRemoteFiles);
ui->statusbar->addPermanentWidget(statusLabelRemoteDirs);
ui->statusbar->addPermanentWidget(statusProgressBar,0);
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
dirIter = new RemoteDirIterator(&messageLogger,this);
connect(&messageLogger, SIGNAL(signalLogMsg(MessageLogger::LogLevel, QString)),
this, SLOT(slotLogMsg(MessageLogger::LogLevel, QString)));
initStatusbar();
connect(dirIter, SIGNAL(downloadCompleted()), this, SLOT(dirDownloadCompleted()));
connect(dirIter, SIGNAL(progressChanged(int, int)),this,SLOT(slotProgressChanged(int, int)));
connect(dirIter, SIGNAL(filesFoundChanged(int)),
this, SLOT(slotFilesFound(int)));
readSettings();
ui->actionDownload->setEnabled(false);
}
MainWindow::~MainWindow()
{
writeSettings();
delete ui;
}
void MainWindow::startDownload()
{
statusProgressBar->setRange(0,filesToDownload.length());
auto nwm = new QNetworkAccessManager();
const QUrl url = QUrl(ui->lineEditUrl->text(),QUrl::ParsingMode::StrictMode);
const QUrl webdavUrl = toWebDavUrl(url);
for(const QString &file : filesToDownload){
QUrl url = QUrl(webdavUrl);
url.setPath(webdavUrl.path()+file);
auto reply = nwm->get(QNetworkRequest(url));
auto dlw = new DownloadWidget(getLocalDir(), nwm, reply);
ui->scrollAreaWidgetContents->layout()->addWidget(dlw);
}
connect(nwm, &QNetworkAccessManager::finished,
this, &MainWindow::fileDownloaded);
}
void MainWindow::fileDownloaded()
{
filesDownloaded ++;
statusProgressBar->setValue(filesDownloaded);
if(filesDownloaded >= filesToDownload.length() )
QTimer::singleShot(1, this, &MainWindow::fileDownloadCompleted);//fileDownloadCompleted();
}
void MainWindow::dirDownloadCompleted(){
if(dirIter->getHaveErrors())
{
reset();
return;
}
QStringList remoteFiles = dirIter->getFileList();
for(const QString& file : remoteFiles){
QString localFileName = file.section('/',3); //Removes /index.php/webdav/
//qDebug()<<localFileName;
if(!QFile::exists(getLocalDir()+"/"+localFileName)){
//qDebug() << "Datei" <<localFileName <<" existiert nicht! Runterladen!!";
filesToDownload.append(localFileName);
}
}
if(filesToDownload.length() >=1){
auto button = QMessageBox::question(this, tr("Download"),
tr("There are %n File(s) that are not found. \n"
"Do you want to Download?", "", filesToDownload.length())
);
if(button == QMessageBox::Yes){
startDownload();
}
else{
ui->actionDownload->setEnabled(true);
}
}
else{
QMessageBox::information(this, tr("Done"), tr("All files are up to date."));
reset();
}
}
void MainWindow::fileDownloadCompleted(){
QMessageBox::information(this, tr("Download"),
tr("Download completed.")
);
reset();
}
void MainWindow::slotProgressChanged(int current, int max)
{
statusProgressBar->setRange(0, max);
statusProgressBar->setValue(current);
statusLabelRemoteDirs->setText(statusLabelRemoteDirsText.arg(max));
}
QUrl MainWindow::toWebDavUrl(const QUrl& url) {
QString path = url.path(); // looks like /s/blahblah
//QString host = url.host(); // nextcloud.blah.de
QString username = path.section('/',-1); // this is blahblah which is also the username
QUrl webdavurl = QUrl(url);
webdavurl.setPath("/public.php/webdav/");
webdavurl.setUserName(username);
return webdavurl;
}
QString MainWindow::getLocalDir()
{
QString userPath = ui->lineEditDir->text();
if(userPath.isEmpty()){
userPath = QDir::currentPath();
}
return userPath;
}
void MainWindow::startRefresh()
{
reset();
readBlackList();
const QUrl url = QUrl(ui->lineEditUrl->text(),QUrl::ParsingMode::StrictMode);
if(url.isEmpty() || !url.isValid() || url.isLocalFile() || url.isRelative()){
QMessageBox::information(this, tr("Invalid URL"), tr("Please enter a valid URL."));
return;
}
ui->actionRefresh->setDisabled(true);
ui->actionDownload->setDisabled(true);
const QUrl webdavUrl = toWebDavUrl(url);
dirIter->setBlackList(blackList);
dirIter->setRootUrl(webdavUrl);
dirIter->downloadDir(webdavUrl);
}
void MainWindow::on_actionRefresh_triggered()
{
startRefresh();
}
void MainWindow::reset()
{
ui->statusbar->showMessage(tr("Ready"));
filesToDownload.clear();
filesDownloaded = 0;
dirIter->reset();
ui->actionRefresh->setDisabled(false);
ui->actionDownload->setEnabled(false);
}
void MainWindow::on_actionAbout_triggered()
{
const QString aboutText = tr("Nexctloud share downloader")+"<br>"+
tr("Version: %1").arg(qApp->applicationVersion())+ "<br>"+
tr("Qt Version: %1").arg(qVersion())+"<br><br>"+
"<a href='https://github.com/adrido/NextcloudShareDownloader/releases'>"+tr("Check for updates")+"</a>";
QMessageBox::about(this, tr("About"), aboutText);
}
void MainWindow::writeSettings()
{
settings.setValue("dir", ui->lineEditDir->text());
settings.setValue("url", ui->lineEditUrl->text());
settings.beginGroup("MainWindow");
if(isMaximized()){
settings.setValue("maximized", isMaximized());
}
else {
settings.setValue("size", size());
settings.setValue("pos", pos());
}
settings.setValue("maximized", isMaximized());
settings.endGroup();
}
void MainWindow::readSettings()
{
ui->lineEditDir->setText(settings.value("dir", QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)).toString());
ui->lineEditUrl->setText(settings.value("url", QString()).toString());
settings.beginGroup("MainWindow");
if(settings.value("maximized",false).toBool()){
this->showMaximized();
}
else{
resize(settings.value("size", QSize(800, 600)).toSize());
move(settings.value("pos", QPoint(200, 200)).toPoint());
}
settings.endGroup();
}
void MainWindow::on_actionOpen_Folder_triggered()
{
QDesktopServices::openUrl(QUrl(ui->lineEditDir->text()));
}
void MainWindow::on_actionOpen_Url_triggered()
{
QDesktopServices::openUrl(QUrl(ui->lineEditUrl->text()));
}
void MainWindow::slotLogMsg(MessageLogger::LogLevel ll, const QString& msg)
{
ui->statusbar->showMessage(msg);
}
void MainWindow::slotFilesFound(int nFiles)
{
statusLabelRemoteFiles->setText(statusLabelRemoteFilesText.arg(nFiles));
}
void MainWindow::on_pushButton_2_clicked()
{
QString dir = QFileDialog::getExistingDirectory(this, QString(),
ui->lineEditDir->text(),
QFileDialog::ShowDirsOnly
| QFileDialog::DontResolveSymlinks);
if(!dir.isEmpty())
ui->lineEditDir->setText(dir);
}
void MainWindow::on_actionReport_Issue_triggered()
{
QDesktopServices::openUrl(QUrl("https://github.com/adrido/NextcloudShareDownloader/issues"));
}
void MainWindow::on_actionDownload_triggered()
{
startDownload();
}
void MainWindow::readBlackList()
{
blackList.clear();
QString dir = ui->lineEditDir->text();
QFile fileBlackList(dir + "/BlackList.txt");
if(fileBlackList.open(QFile::ReadOnly | QFile::Text))
{
QTextStream stream( &fileBlackList );
while(!stream.atEnd()){
QString line = stream.readLine();
// Solange die Zeile nicht mit # beginnt oder leer ist
if(line.startsWith('#') || line.isEmpty()){
continue;
}
blackList.append(line);
qDebug() << blackList;
}
}
}
void MainWindow::on_actionEditBlackList_triggered()
{
QString dir = ui->lineEditDir->text();
QFile fileBlackList(dir + "/BlackList.txt");
// Falls es diese datei nicht gibt
if(!fileBlackList.exists())
{
if(fileBlackList.open(QFile::WriteOnly | QFile::Text))
{
QTextStream stream( &fileBlackList );
stream << tr("# This is the blacklist of %1.").arg(qAppName()) << endl
<< tr("# You can write the name of the file you want to ignore to this file.") << endl
<< tr("# One entry per line.") << endl
<< tr("# Lines that start with a # are ignored.") << endl;
fileBlackList.close();
}
}
QDesktopServices::openUrl(QUrl(fileBlackList.fileName()));
}