-
Notifications
You must be signed in to change notification settings - Fork 9
/
installfromswg.cpp
180 lines (132 loc) · 5.14 KB
/
installfromswg.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
#include "installfromswg.h"
#include "ui_installfromswg.h"
#include <QSettings>
#include <QDir>
#include "mainwindow.h"
#include <QTimer>
#include <QFutureWatcher>
#include <QPalette>
#include <QMessageBox>
#include <QFileDialog>
#if QT_VERSION >= 0x050000
#include <QtConcurrent/QtConcurrentRun>
#endif
InstallFromSWG::InstallFromSWG(QWidget *parent) :
QDialog(parent),
ui(new Ui::InstallFromSWG) {
ui->setupUi(this);
cancelThreads = false;
connect(©Watcher, SIGNAL(finished()), this, SLOT(copyFinished()));
ui->progressBar->setValue(0);
ui->progressBar->setMaximum(0);
//connect(this, SIGNAL())
connect(this, SIGNAL(fileCopiedSignal(QString,bool)), this, SLOT(fileCopied(QString,bool)));
}
InstallFromSWG::~InstallFromSWG() {
delete ui;
}
void InstallFromSWG::copyFinished() {
int result = copyWatcher.result();
ui->progressBar->setValue(ui->progressBar->maximum());
if (result == 0)
ui->label->setText("Installation finished.");
else
ui->label->setText("Installation failed.");
qDebug() << "copy finished with result " << result;
done(result);
}
void InstallFromSWG::closeEvent(QCloseEvent* event) {
qDebug() << "close event";
cancelThreads = true;
if (copyWatcher.isRunning()) {
copyWatcher.cancel();
copyWatcher.waitForFinished();
}
done(2);
QDialog::closeEvent(event);
}
int InstallFromSWG::copyFiles() {
QVector<QPair<QString, qint64> > requiredFiles = MainWindow::getRequiredFiles();
for (int i = 0; i < requiredFiles.size() && !cancelThreads; ++i) {
const QPair<QString, qint64> file = requiredFiles.at(i);
//if (QDir(file))
if (file.first.contains("/")) {
QString dir = emuFolder + file.first.mid(0, file.first.lastIndexOf("/"));
QDir(dir).mkpath(".");
}
#ifdef Q_OS_WIN32
bool result = QFile::copy(swgfolder + "\\" + file.first, emuFolder + file.first);
#else
bool result = QFile::copy(swgfolder + "/" + file.first, emuFolder + file.first);
#endif
//bool result = true;
//QTimer::singleShot(0, this, SLOT(fileCopied(file.first, result)));
emit fileCopiedSignal(file.first, result);
/*
if (!result)
return 1;*/
}
return 0;
}
void InstallFromSWG::fileCopied(const QString& file, bool success) {
if (success) {
ui->label->setText(file + " successfully installed");
}
ui->progressBar->setValue(ui->progressBar->value() + 1);
}
int InstallFromSWG::checkSWGFolder() {
QDir dir(swgfolder);
if (!dir.exists())
return 1;
QStringList filesToCheck;
filesToCheck << "bottom.tre" << "data_animation_00.tre" << "data_texture_04.tre";
for (int i = 0; i < filesToCheck.size(); ++i) {
#ifdef Q_OS_WIN32
if (!QFile(swgfolder + "\\" + filesToCheck.at(i)).exists())
return 2;
#else
if (!QFile(swgfolder + "/" + filesToCheck.at(i)).exists())
return 2;
#endif
}
return 0;
}
int InstallFromSWG::installFiles() {
QSettings settings;
QMessageBox::information(this, "SWGEmu", "Please choose a valid Star Wars Galaxies installation");
swgfolder = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
"/home",
QFileDialog::ShowDirsOnly
| QFileDialog::DontResolveSymlinks);
int validFolder = checkSWGFolder();
if (validFolder != 0) {
QMessageBox::warning(this, "Folder", "The folder you selected isnt a valid Star Wars Galaxies installation!");
return 1;
}
QMessageBox::information(this, "SWGEmu", "Please choose where you want to install SWGEmu");
emuFolder = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
"/home",
QFileDialog::ShowDirsOnly
| QFileDialog::DontResolveSymlinks);
if (!QDir(emuFolder).exists() || emuFolder.isEmpty()) {
QMessageBox::warning(this, "Folder", "The swgemu folder you selected isn't a valid directory");
return 1;
}
//qDebug() << emuFolder;
emuFolder = emuFolder + "/SWGEmu/";
if (QDir(emuFolder).exists()) {
if (QMessageBox::question(this, "Warning", "SWGEmu folder already exists, do you want to overwrite?") != QMessageBox::Yes)
return 3;
} else {
if (!QDir(emuFolder).mkpath(".")) {
QMessageBox::warning(this, "ERROR", "Could not create the swgemu folder!");
return 4;
}
}
QVector<QPair<QString, qint64> > requiredFiles = MainWindow::getRequiredFiles();
ui->progressBar->setValue(0);
ui->progressBar->setMaximum(requiredFiles.size());
QFuture<int> future = QtConcurrent::run(this, &InstallFromSWG::copyFiles);
copyWatcher.setFuture(future);
return exec();
}