-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdeviceeditor.cpp
More file actions
221 lines (205 loc) · 7.52 KB
/
Copy pathdeviceeditor.cpp
File metadata and controls
221 lines (205 loc) · 7.52 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
#include "deviceeditor.h"
#include "devicerecorddialog.h"
#include "logfile.h"
#include <QMessageBox>
#include <QSqlQuery>
#include <QSqlError>
#include <QSqlDatabase>
DeviceEditor::DeviceEditor(QWidget *parent, bool iskodi,
const QString &version,
std::function<void()> eraseCallback)
: QObject(parent)
, m_parent(parent)
, m_iskodi(iskodi)
, m_version(version)
, m_eraseCallback(std::move(eraseCallback))
{
}
void DeviceEditor::setExistingDevice(const DeviceRecord &device, const QString &description)
{
m_existingDevice = device;
m_existingDescription = description;
m_isUpdate = true;
}
bool DeviceEditor::exec()
{
DeviceRecordDialog dialog(m_parent, m_iskodi);
dialog.setWindowModality(Qt::WindowModal);
dialog.setversionLabel(m_version);
if (m_isUpdate)
{
dialog.setPackagename(m_existingDevice.xbmcpackage);
dialog.setPulldir(m_existingDevice.pulldir);
dialog.setfilepath(m_existingDevice.filepath);
dialog.setscrcpy(m_existingDevice.scrcpyarg);
dialog.setdataroot(m_existingDevice.data_root);
dialog.setostype("0");
dialog.setdescription(m_existingDevice.description);
dialog.setdisableroot(m_existingDevice.disableroot);
if (m_existingDevice.isusb)
dialog.setport("");
else
dialog.setport(m_existingDevice.port);
dialog.setdaddr(m_existingDevice.daddr);
dialog.setisusb(m_existingDevice.isusb);
}
else
{
dialog.setPackagename("org.xbmc.kodi");
dialog.setPulldir("");
dialog.setfilepath("/files/.kodi");
dialog.setdataroot("/sdcard/");
dialog.setostype("0");
dialog.setdescription("");
dialog.setscrcpy("");
dialog.setdisableroot(0);
dialog.setport("5555");
dialog.setscope(false);
dialog.setwsa(false);
dialog.setdaddr("");
dialog.setisusb(false);
}
dialog.setModal(true);
int result = dialog.exec();
if (result != QDialog::Accepted)
return false;
QString data_root = dialog.data_root();
QString xbmcpackage = dialog.xbmcpackageName();
QString pulldir = dialog.pulldir();
QString description = dialog.description();
QString filepath = dialog.filepath();
QString port = dialog.port();
QString daddr = dialog.daddr();
bool isusb = dialog.isusb();
QString ostype = dialog.ostype();
int disableroot = dialog.disableroot();
QString scrcpy = dialog.scrcpy();
if (description.isEmpty())
{
QMessageBox msgBox(m_parent);
msgBox.setIcon(QMessageBox::Critical);
msgBox.setWindowTitle(QString());
msgBox.setText(QStringLiteral("Description cannot be empty."));
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setWindowModality(Qt::WindowModal);
msgBox.exec();
return false;
}
if (!m_isUpdate)
{
QSqlQuery checkQuery;
checkQuery.prepare("SELECT COUNT(*) FROM device WHERE description = ?");
checkQuery.addBindValue(description);
if (checkQuery.exec() && checkQuery.first() && checkQuery.value(0).toInt() > 0)
{
QMessageBox msgBox(m_parent);
msgBox.setIcon(QMessageBox::Critical);
msgBox.setWindowTitle(QString());
msgBox.setText(QStringLiteral("A device with this description already exists."));
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setWindowModality(Qt::WindowModal);
msgBox.exec();
return false;
}
}
QSqlQuery query;
QString sqlstatement;
if (!m_isUpdate)
{
sqlstatement = "INSERT INTO device (description, daddr, port, isusb, ostype, "
"data_root, xbmcpackage, pulldir, disableroot, filepath, flag5) "
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
query.prepare(sqlstatement);
query.addBindValue(description);
query.addBindValue(daddr);
query.addBindValue(port);
query.addBindValue(isusb);
query.addBindValue(ostype);
query.addBindValue(data_root);
query.addBindValue(xbmcpackage);
query.addBindValue(pulldir);
query.addBindValue(disableroot);
query.addBindValue(filepath);
query.addBindValue(scrcpy);
}
else
{
sqlstatement = "UPDATE device SET description = ?, daddr = ?, port = ?, isusb = ?, ostype = ?, "
"data_root = ?, xbmcpackage = ?, pulldir = ?, disableroot = ?, filepath = ?, flag5 = ? "
"WHERE description = ?";
query.prepare(sqlstatement);
query.addBindValue(description);
query.addBindValue(daddr);
query.addBindValue(port);
query.addBindValue(isusb);
query.addBindValue(ostype);
query.addBindValue(data_root);
query.addBindValue(xbmcpackage);
query.addBindValue(pulldir);
query.addBindValue(disableroot);
query.addBindValue(filepath);
query.addBindValue(scrcpy);
query.addBindValue(m_existingDescription);
}
if (!query.exec())
{
QString errorMessage = query.lastError().text();
logfile(QString("Query error: ") + errorMessage);
logfile(QString("SQL statement: ") + sqlstatement);
logfile(QString("Bound values: description=%1, daddr=%2, port=%3, isusb=%4, ostype=%5, data_root=%6, xbmcpackage=%7, pulldir=%8, disableroot=%9, filepath=%10, scrcpy=%11")
.arg(description)
.arg(daddr)
.arg(port)
.arg(isusb ? "true" : "false")
.arg(ostype)
.arg(data_root)
.arg(xbmcpackage)
.arg(pulldir)
.arg(disableroot)
.arg(filepath)
.arg(scrcpy));
if (errorMessage.contains("Parameter count mismatch"))
{
QMessageBox msgBox(m_parent);
msgBox.setWindowTitle("Parameter Mismatch Error");
msgBox.setText("Parameter count mismatch detected. Would you like to re-initialize adblink?");
msgBox.setIcon(QMessageBox::Question);
QAbstractButton *yesButton = msgBox.addButton(QMessageBox::Yes);
QAbstractButton *noButton = msgBox.addButton(QMessageBox::No);
bool done = false;
while (!done)
{
msgBox.exec();
QAbstractButton *clicked = msgBox.clickedButton();
if (clicked == yesButton)
{
if (m_eraseCallback)
m_eraseCallback();
done = true;
}
else if (clicked == noButton)
{
done = true;
}
}
return false;
}
else
{
QMessageBox msgBox(m_parent);
msgBox.setIcon(QMessageBox::Critical);
msgBox.setWindowTitle(QString());
msgBox.setText(QStringLiteral("%1%2").arg(!m_isUpdate ? "Failed to insert into database: " : "Failed to update database: ", errorMessage));
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setWindowModality(Qt::WindowModal);
msgBox.exec();
return false;
}
}
else
{
logfile(QString("Query executed successfully for ") + (!m_isUpdate ? "INSERT" : "UPDATE"));
}
QSqlDatabase::database().commit();
return true;
}