forked from top-master/GradleCopy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlistview.cpp
More file actions
144 lines (126 loc) · 4.48 KB
/
listview.cpp
File metadata and controls
144 lines (126 loc) · 4.48 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
#include "downloadthread.h"
#include "listview.h"
#include "ui_listview.h"
#include <QDesktopServices>
#include <QDir>
#include <qurl.h>
#include <QStringListModel>
#include <qmenu.h>
#include <QClipboard>
ListView::ListView(QWidget *parent) :
QWidget(parent),
ui(new Ui::ListView)
{
ui->setupUi(this);
ui->progressBar->setVisible(false);
QMenu *menu = new QMenu();
menu->addAction(QLatin1Literal("open Downloads Dir"), this, SLOT(showDownloads()));
menu->addAction(QLatin1Literal("Disable incomplete lib (rename .pom with no .jar beside it)"), this, SLOT(disableIncompleteLibraries()));
menu->addAction(QLatin1Literal("Restore incomplete lib (rename .pom.backup to .pom)"), this, SLOT(restoreIncompleteLibraries()));
ui->downloadButton->setMenu(menu);
connect(ui->copyButton, &QPushButton::clicked, this, &ListView::copyList);
downloadsDir = QDir::currentPath() + QLatin1Literal("/downloads");
}
ListView::~ListView()
{
delete ui;
}
void ListView::setList(const QString &providerLink, const QStringList &remoteLinks, const QStringList &localFiles)
{
this->providerLink = providerLink;
QStringListModel *m = new QStringListModel(localFiles, ui->listView);
ui->listView->setModel(m);
this->remoteLinks = remoteLinks;
this->localFiles = localFiles;
}
QStringList ListView::getViewItems() const
{
QStringListModel *m = qobject_cast<QStringListModel *>(ui->listView->model());
if(m)
return m->stringList();
return QStringList();
}
void ListView::showDownloads()
{
QDesktopServices::openUrl( QUrl::fromLocalFile( downloadsDir ) );
}
void ListView::disableIncompleteLibraries()
{
const QStringList &list = this->localFiles;
foreach (const QString &jarPath, list) {
if(!QFile::exists(jarPath)) {
QString pomFile = jarPath;
pomFile.chop(3);
pomFile += QLatin1Literal("pom");
QString pomFileBackup = pomFile + QLatin1Literal(".backup");
if(QFile::exists(pomFileBackup) && QFile::exists(pomFile)) {
if(!QFile::remove(pomFileBackup)) {
qWarning("can not delete: %s", qPrintable(pomFileBackup));
continue;
}
}
if(!QFile::rename(pomFile, pomFileBackup))
qWarning("can not rename: %s", qPrintable(pomFile));
}
}
}
void ListView::restoreIncompleteLibraries()
{
const QStringList &list = this->localFiles;
foreach (const QString &jarPath, list) {
if(!QFile::exists(jarPath)) {
QString pomFile = jarPath;
pomFile.chop(3);
pomFile += QLatin1Literal("pom");
QString pomFileBackup = pomFile + QLatin1Literal(".backup");
if(QFile::exists(pomFileBackup)) {
if(QFile::exists(pomFile)) {
if(!QFile::remove(pomFile)) {
qWarning("can not delete: %s", qPrintable(pomFile));
continue;
}
}
if(!QFile::rename(pomFileBackup, pomFile))
qWarning("can not rename: %s", qPrintable(pomFileBackup));
else
qWarning("restored: %s", qPrintable(pomFileBackup));
}
}
}
}
void ListView::copyList()
{
const QStringList &list = getRemotelinks();
QClipboard *clipboard = qApp->clipboard();
clipboard->clear();
clipboard->setText(list.join(QLatin1Literal("\n")));
}
void ListView::on_downloadButton_clicked()
{
const QStringList &list = getRemotelinks();
if(list.count() >= 1) {
ui->progressBar->setValue(0);
ui->progressBar->setMaximum(list.count());
ui->progressBar->setVisible(true);
DownloadThread *t = new DownloadThread();
connect(t, &QThread::finished, this, &ListView::onFinish);
connect(t, &QThread::finished, t, &QThread::deleteLater);
connect(t, &DownloadThread::onDownloadBegin, this, &ListView::onDownloadBegin);
connect(t, &DownloadThread::onDownload, this, &ListView::onDownloadFinish);
t->moveToThread(t);
t->prepare(list, downloadsDir, providerLink);
t->start();
}
}
void ListView::onDownloadBegin(int index, const QString &link, const QString &localPath)
{
ui->statusView->setText(link);
}
void ListView::onDownloadFinish(int index, const QString &link, const QString &localPath)
{
ui->progressBar->setValue(index);
}
void ListView::onFinish()
{
ui->progressBar->setVisible(false);
}