-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
192 lines (159 loc) · 5.42 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
// AT_MA (c) by alice
//
// AT_MA is licensed under a
// Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.
//
// You should have received a copy of the license along with this
// work. If not, see <http://creativecommons.org/licenses/by-nc-nd/4.0/>.
// The license applies to all files, previous commits.
#include "mainwindow.h"
#include "ui_mainwindow.h"
bool botReady;
bool connectionReady;
QJsonObject jdata;
QSqlDatabase db;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->status->append("AT Migration Assistant <b>v1.0</b> ready.");
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_btnFind_clicked()
{
bool error = false;
QTextBrowser* status = ui->status;
if (botReady) {
status->append("<b><span style='color: #FF4444'>ERROR</span></b>: JSON preparation is already completed.");
return;
}
status->insertPlainText("\nPrepearing... ");
if (ui->botPath->text() == "") {
status->append("<b><span style='color: #FF4444'>ERROR</span></b>: Please set BOT path.");
error = true;
}
QString lookFor;
switch (ui->versionSelect->currentIndex()) {
case 0:
status->append("<b><span style='color: #FF4444'>ERROR</span></b>: Please select bot version.");
error = true;
break;
case 1:
lookFor = "files/lists.json";
break;
case 2:
lookFor = "./lists.json";
}
if (error) {
return;
}
ui->progressBar->setValue(33);
status->insertPlainText("OK\n");
status->insertPlainText("Looking for file... ");
QString path = ui->botPath->text() + lookFor;
QFile jfile(path);
jfile.open(QIODevice::ReadOnly | QIODevice::Text);
QByteArray data = jfile.readAll();
jfile.close();
ui->progressBar->setValue(66);
QJsonParseError errorPtr;
QJsonDocument doc = QJsonDocument::fromJson(data, &errorPtr);
if (doc.isNull()) {
status->insertPlainText("Failed.");
status->append("<b><span style='color: #FF4444'>ERROR</span></b>: File don't found.");
return;
}
ui->progressBar->setValue(100);
status->insertPlainText("OK");
jdata = doc.object();
botReady = true;
}
void MainWindow::on_btnConnect_clicked()
{
QTextBrowser* status = ui->status;
if (connectionReady) {
status->append("<b><span style='color: #FF4444'>ERROR</span></b>: The connection is already established.");
return;
}
ui->progressBar->setValue(0);
db = QSqlDatabase::addDatabase("QPSQL");
if (ui->password->text() == "") {
status->append("<b><span style='color: #FF4444'>ERROR</span></b>: Please, set DB password.");
return;
}
status->insertPlainText("\nConfiguring connection... ");
db.setHostName(ui->url->text());
db.setPort(ui->port->text().toInt());
db.setDatabaseName("nextmdb");
db.setUserName(ui->username->text());
db.setPassword(ui->password->text());
status->insertPlainText("OK\n");
ui->progressBar->setValue(50);
status->insertPlainText("Connecting to PSQL... ");
if (!db.open()) {
status->insertPlainText("Failed");
status->append("<b><span style='color: #FF4444'>ERROR</span></b>: Can't connect to PSQL DB.");
return;
}
status->insertPlainText("OK");
ui->progressBar->setValue(100);
connectionReady = true;
}
void MainWindow::on_openDir_clicked()
{
QString filePath = QFileDialog::getExistingDirectory(this, "Select BOT directory", QDir::homePath());
if (!filePath.isEmpty()) {
ui->botPath->setText(filePath + "/");
}
}
void MainWindow::on_btnMigrate_clicked()
{
QTextBrowser* status = ui->status;
ui->progressBar->setValue(0);
status->insertPlainText("\nChecking DB conn... ");
if (!connectionReady) {
status->insertPlainText("Failed");
status->append("<b><span style='color: #FF4444'>ERROR</span></b>: Please connect to PSQL DB.");
return;
}
status->insertPlainText("OK\n");
status->insertPlainText("Checking if JSON is loaded... ");
if (!botReady) {
status->insertPlainText("Failed");
status->append("<b><span style='color: #FF4444'>ERROR</span></b>: Please prepear JSON.");
return;
}
status->insertPlainText("OK\n");
ui->progressBar->setValue(25);
status->insertPlainText("Calculating dependencies... ");
QStringList jkeys = jdata.keys();
double length = 0;
double progressBarPos = 50;
for (QString key : jkeys) {
length += jdata.value(key).toArray().size();
}
double cost = 50/length;
status->insertPlainText("Done!\n");
status->insertPlainText("Migrating... ");
ui->progressBar->setValue(progressBarPos);
for (QString key : jkeys) {
QJsonArray outerArray = jdata.value(key).toArray();
for (int x = 0; x != outerArray.size(); x++) {
QJsonArray innerArray = outerArray[x].toArray();
QString query = "INSERT INTO music_data (music_name, music_url, user_id) VALUES ('" + innerArray[0].toString() + "', '" + innerArray[1].toString() + "', '" + key + "')";
progressBarPos += cost;
ui->progressBar->setValue(progressBarPos);
db.transaction();
db.exec(query);
db.commit();
}
}
status->insertPlainText("Done!");
db.close();
connectionReady = false;
botReady = false;
}