-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
299 lines (258 loc) · 9.44 KB
/
Copy pathmainwindow.cpp
File metadata and controls
299 lines (258 loc) · 9.44 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
298
299
#include <QApplication>
#include <QtGui>
#include <QFileDialog>
#include <QMessageBox>
#include <QMenuBar>
#include <QIODevice>
#include <QtConcurrent/QtConcurrentRun>
#include <QFuture>
#include <QProgressDialog>
#include "mainwindow.h"
#include "dirfiletree.h"
#include "adclistreader.h"
#include "loadpathworker.h"
#include "qualz4file.h"
using ShowListing::DirFileTree;
using ShowListing::AdcListReader;
MainWindow::MainWindow(QApplication &application, QWidget *parent) : QMainWindow(parent)
{
app = &application;
dirFileTree = new DirFileTree;
setCentralWidget(dirFileTree);
createActions();
createMenus();
setAcceptDrops(true);
statusBar()->showMessage(tr("Ready"));
setWindowTitle(tr("ShowListing"));
QSettings settings("ShowListing", "ShowListing 1");
if (settings.contains("windowState") || settings.contains("geometry")) {
restoreState(settings.value("windowState").toByteArray());
restoreGeometry(settings.value("geometry").toByteArray());
QSize size = settings.value("size").toSize();
if (size.width() < 640 || size.height() < 480) {
size.scale(640, 640, Qt::KeepAspectRatio);
}
resize(size);
}
else {
resize(1024, 768);
}
lastOpenPath = QDir::currentPath();
}
void MainWindow::open()
{
QString path =
QFileDialog::getOpenFileName(this, tr("ShowListing - Open ADC FileListing"),
lastOpenPath,
#if defined(WITH_BZIP2)
tr("Supported FileListing (*.xml *.xml.bz2 *.xmlz4)")
#else
tr("Supported FileListing (*.xml *.xmlz4)")
#endif
+ ";;" + tr("Uncompressed ADC FileListing (*.xml)")
#if defined(WITH_BZIP2)
+ ";;" + tr("Bzip2 (DC++) FileListing (*.xml.bz2)")
#endif
+ ";;" + tr("LZ4-compressed ADC FileListing (*.xmlz4)")
);
if (!path.isEmpty())
openPath(path);
}
void MainWindow::openPath(const QString& fileName)
{
QFileInfo fi(fileName);
lastOpenPath = fi.dir().absolutePath();
lastOpenFilePath = fileName;
if (fi.suffix().toLower() == "xmlz4") {
source = new QuaLz4File(fileName);
if (!source->open(QFile::ReadOnly)) {
QMessageBox::warning(this, tr("ShowListing - Cannot open file list"),
tr("Cannot open or read file %1.\nThe compressed bytestream may be corrupt.")
.arg(fileName));
delete source;
source = 0;
return;
}
} else {
source = new QFile(fileName);
if (!source->open(QFile::ReadOnly | QFile::Text)) {
QMessageBox::warning(this, tr("ShowListing - Cannot open file list"),
tr("Cannot open or read file %1:\n%2.")
.arg(fileName)
.arg(source->errorString()));
delete source;
source = 0;
return;
}
}
#if 0
progressValue = 0;
insertedRow = 0;
listReader = new ShowListing::AdcListReader(dirFileTree);
watcher = new QFutureWatcher<void>();
progress = new QProgressDialog(tr("Processing %1...").arg(QDir::toNativeSeparators(fileName)),
QString(), 0, fi.size(), this);
progress->setWindowModality(Qt::WindowModal);
progress->show();
QFuture<void> future = QtConcurrent::run(listReader, &ShowListing::AdcListReader::read, source, &insertedRow);
QObject::connect(progress, SIGNAL(canceled()),
this, SLOT(slotOpenPathCancelled()));
QObject::connect(listReader, SIGNAL(broadcastProgress(qint64)),
this, SLOT(slotBroadcastProgressReceived(qint64)));
QObject::connect(watcher, SIGNAL(finished()),
this, SLOT(slotFutureWatchNotify()));
watcher->setFuture(future);
timer.start(500);
#else
//FIXME: temporarily do everything ins GUI thread until a proper way is implemented
progress = new QProgressDialog(QString(), QString(), 0, 0, this);
watcher = new QFutureWatcher<void>();
listReader = new ShowListing::AdcListReader(dirFileTree);
listReader->read(source, &insertedRow);
slotFutureWatchNotify();
#endif
}
void MainWindow::slotFutureWatchNotify()
{
qDebug("call slotFutureWatchNotify()\n");
timer.stop();
Q_ASSERT_X(listReader != 0, "slotFutureWatchNotify()", "use-after-free listReader");
Q_ASSERT_X(source != 0, "slotFutureWatchNotify()", "use-after-free source");
if (!listReader->hasError() && insertedRow) {
progress->reset();
insertedRow->setData(0, Qt::UserRole, QString("%1").arg(QDir::toNativeSeparators(lastOpenFilePath)));
insertedRow->setText(0, QString("[%1] %2").arg(lastOpenFilePath).arg(insertedRow->text(0)));
insertedRow->setExpanded(true);
// modify UI in GUI thread.
dirFileTree->setUpdatesEnabled(false);
dirFileTree->addTopLevelItem(insertedRow);
dirFileTree->header()->resizeSections(QHeaderView::ResizeToContents);
dirFileTree->setUpdatesEnabled(true);
statusBar()->showMessage(tr("File loaded:%1:").arg(dirFileTree->property("base").toString()));
} else {
if (insertedRow) {
dirFileTree->setUpdatesEnabled(false);
dirFileTree->addTopLevelItem(insertedRow);
delete insertedRow;
dirFileTree->setUpdatesEnabled(true);
}
QMessageBox::warning(this, tr("ShowListing - Failed to open file list"),
tr("Cannot open file list %1, maybe the file is corrupt.\n\n%2")
.arg(lastOpenFilePath)
.arg(listReader->errorString()));
progress->reset();
}
delete watcher;
watcher = 0;
delete progress;
progress = 0;
delete listReader;
listReader = 0;
delete source;
source = 0;
}
void MainWindow::slotBroadcastProgressReceived(qint64 pos)
{
lock_progressValue.lockForWrite();
progressValue = pos;
lock_progressValue.unlock();
if (progress) {
progress->setValue(pos);
}
}
void MainWindow::slotOpenPathCancelled()
{
if (listReader) {
listReader->cancelProcessing();
}
}
void MainWindow::slotTimer()
{
if (progress != 0)
{
lock_progressValue.lockForRead();
progress->setValue(progressValue);
lock_progressValue.unlock();
}
}
void MainWindow::onExport()
{
QString fileName =
QFileDialog::getSaveFileName(this, tr("ShowListing - Export Filelisting"),
lastOpenPath,
tr("Text Files (*.txt)"));
if (fileName.isEmpty())
return;
QFile file(fileName);
if (!file.open(QFile::WriteOnly | QFile::Text)) {
QMessageBox::warning(this, tr("ShowListing"),
tr("Cannot write file %1:\n%2.")
.arg(fileName)
.arg(file.errorString()));
return;
}
//if (dirFileTree->write(&file))
//statusBar()->showMessage(tr("File saved"), 2000);
}
void MainWindow::about()
{
QMessageBox::about(this, tr("About ShowListing"),
tr("This program allows you to browse a hierarchized file listing, as defined by the <b><a href=\"http://adc.sourceforge.net/ADC.html\">Advanced Direct Connect protocol</a></b>.<br>"
"Data is presented in a tree widget and shows cumulated folder sizes, to quickly visualize repartition of volume."));
}
void MainWindow::createActions()
{
openAct = new QAction(tr("&Open FileList..."), this);
openAct->setShortcuts(QKeySequence::Open);
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
exportAct = new QAction(tr("E&xport..."), this);
exportAct->setShortcuts(QKeySequence::SaveAs);
connect(exportAct, SIGNAL(triggered()), this, SLOT(onExport()));
exitAct = new QAction(tr("&Quit"), this);
exitAct->setShortcuts(QKeySequence::Quit);
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
aboutAct = new QAction(tr("&About"), this);
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
}
void MainWindow::createMenus()
{
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(openAct);
#if 0
fileMenu->addAction(exportAct);
#endif
fileMenu->addAction(exitAct);
menuBar()->addSeparator();
helpMenu = menuBar()->addMenu(tr("&Help"));
helpMenu->addAction(aboutAct);
}
void MainWindow::closeEvent(QCloseEvent *event)
{
QSettings settings("ShowListing", "ShowListing 1");
settings.setValue("geometry", saveGeometry());
settings.setValue("windowState", saveState());
if ( !isMaximized() ) {
settings.setValue( "size", size() );
}
QMainWindow::closeEvent(event);
}
void MainWindow::dragEnterEvent(QDragEnterEvent* event)
{
event->acceptProposedAction();
}
void MainWindow::dragMoveEvent(QDragMoveEvent* event)
{
event->acceptProposedAction();
}
void MainWindow::dragLeaveEvent(QDragLeaveEvent* event)
{
event->accept();
}
void MainWindow::dropEvent(QDropEvent* event)
{
if(event->mimeData()->hasUrls()) {
QList<QUrl> urllist(event->mimeData()->urls());
for (int i = 0; i < urllist.size() && i < 5; ++i)
openPath(urllist.at(i).toLocalFile());
}
}