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
|
/*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version
2 of the License, or (at your option) any later version.
*/
#include <QTextEdit>
#include <QProcess>
#include <QPushButton>
#include <QVBoxLayout>
#include <QSettings>
#include <QFileInfo>
#include <QByteArray>
#include <QApplication>
#include <QDebug>
#include "extractordialog.h"
//Well, f**king doc of QProcess... The extraction has to be a Thread, otherwise GUI will block
ExtractorDialog::ExtractorDialog(const QString &archive, const QString &extractTo, QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f), mArchive(archive), mExtractTo(extractTo){
QVBoxLayout *mainLayout = new QVBoxLayout;
mOutput = new QTextEdit;
mOutput->setReadOnly(true);
mCancelClose = new QPushButton(tr("Close"));
connect(mCancelClose, SIGNAL(clicked()), this, SLOT(accept()));
mainLayout->addWidget(mOutput);
mainLayout->addWidget(mCancelClose);
setLayout(mainLayout);
mExtractor = new QProcess(this);
connect(mExtractor, SIGNAL(started()), this, SLOT(extractionStarted()));
connect(mExtractor, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(extractionFinished()));
//connect(mExtractor, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(processStatusChanged(QProcess::ProcessState)));
QString wTitle = QString(tr("%1 - extracting from %2")).arg(qApp->applicationName()).arg(archive);
setWindowTitle(wTitle);
start();
qDebug() << "state" << mExtractor->state();
}
void ExtractorDialog::processStatusChanged(QProcess::ProcessState newState){
qDebug() << "State changed to" << newState;
if((newState == QProcess::Running) || (newState == QProcess::Starting)){
mCancelClose->disconnect(SIGNAL(clicked()));
connect(mCancelClose, SIGNAL(clicked()), this, SLOT(killProcess()));
mCancelClose->setText(tr("Cancel"));
}
if(newState == QProcess::NotRunning){
mCancelClose->disconnect(SIGNAL(clicked()));
connect(mCancelClose, SIGNAL(clicked()), this, SLOT(accept()));
mCancelClose->setText(tr("Close"));
}
}
void ExtractorDialog::extractionStarted(){
mCancelClose->disconnect(SIGNAL(clicked()));
connect(mCancelClose, SIGNAL(clicked()), this, SLOT(killProcess()));
mCancelClose->setText(tr("Cancel"));
}
void ExtractorDialog::extractionFinished(){
mCancelClose->disconnect(SIGNAL(clicked()));
connect(mCancelClose, SIGNAL(clicked()), this, SLOT(accept()));
mCancelClose->setText(tr("Close"));
}
void ExtractorDialog::killProcess(){
mExtractor->terminate();
mCancelClose->disconnect(SIGNAL(clicked()));
connect(mCancelClose, SIGNAL(clicked()), this, SLOT(accept()));
mCancelClose->setText(tr("Close"));
}
void ExtractorDialog::writeOutput(){
qDebug() << "writing" << mExtractor->state();
QByteArray output = mExtractor->read(mExtractor->bytesAvailable());
mOutput->append(output);
}
void ExtractorDialog::start(){
QSettings s;
QString exe = s.value("paths/archiver").toString();
if(exe.isEmpty()){
mOutput->append(tr("No archive program configured!"));
return;
}
QFileInfo info(exe);
if(!info.exists() || !info.isExecutable()){
QString msg = QString(tr("Unable to call %1")).arg(exe);
mOutput->append(msg);
return;
}
QStringList args = s.value("paths/archiverargs").toStringList();
args << mArchive;
mExtractor = new QProcess(this);
mExtractor->setWorkingDirectory(mExtractTo);
connect(mExtractor, SIGNAL(readyRead()), this, SLOT(writeOutput()));
QString startmessage = QString("Starting %1 %2\n").arg(exe).arg(args.join(" "));
mOutput->append(startmessage);
mExtractor->start(exe, args);
mExtractor->waitForStarted();
}
|