-
Notifications
You must be signed in to change notification settings - Fork 57
/
mainwindow.cpp
328 lines (279 loc) · 9.09 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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
* CloudClient - A Qt cloud client for lixian.vip.xunlei.com
* Copyright (C) 2012 by Aaron Lewis <[email protected]>
*
* 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 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
tcore(new ThunderCore (this)),
tpanel(new ThunderPanel (this)),
vpanel(new VideoPanel (this)),
lpanel(new LogView (this)),
bpanel(new Browser (this)),
transf0r(new Transf0r (this)),
osd(new OSD (this))
{
ui->setupUi(this);
setWindowTitle (tr("%1 $VER %2").arg(QApplication::applicationName())
.arg(QApplication::applicationVersion()));
vpanel->setStatusBar(ui->statusBar);
ui->tabWidget->addTab(tpanel, "Cloud");
ui->tabWidget->addTab(vpanel, "Video");
ui->tabWidget->addTab(bpanel, "Browser");
ui->tabWidget->addTab(lpanel, "Log");
ui->tabWidget->addTab(transf0r, "Transf0r");
///
connect (tcore, SIGNAL(StatusChanged(ThunderCore::ChangeType)),
SLOT(slotStatusChanged(ThunderCore::ChangeType)));
connect (tcore, SIGNAL(error(QString,ThunderCore::ErrorCategory)),
lpanel, SLOT(logReceived(QString,ThunderCore::ErrorCategory)));
connect (tcore, SIGNAL(BTSubTaskReady(Thunder::BitorrentTask)),
tpanel, SLOT(setBTSubTask(Thunder::BitorrentTask)));
connect (tpanel, SIGNAL(doThisLink(Thunder::RemoteTask,
ThunderPanel::RequestType,bool)),
SLOT(slotRequestReceived(Thunder::RemoteTask,
ThunderPanel::RequestType,bool)));
connect (tpanel, SIGNAL(doIndirectRequest(ThunderPanel::IndirectRequestType)),
SLOT(slotIndirectRequestReceived(ThunderPanel::IndirectRequestType)));
connect (osd, SIGNAL(doubleClicked()), SLOT(slotShowOrHideWindow()));
// WILL BE REMOVED LATER!
connect (tcore, SIGNAL(error(QString,ThunderCore::ErrorCategory)),
SLOT(slotError(QString,ThunderCore::ErrorCategory)));
///
osd->osd_menu->addAction(ui->actionNewTask);
osd->osd_menu->addSeparator();
osd->osd_menu->addAction(ui->actionQ_uit);
///
{
QSettings settings;
settings.beginGroup("UI");
restoreState( settings.value("State").toByteArray());
restoreGeometry( settings.value("Geometry").toByteArray() );
}
login ();
}
MainWindow::~MainWindow()
{
QSettings sets;
sets.beginGroup("UI");
sets.setValue("State", saveState());
sets.setValue("Geometry", saveGeometry());
sets.endGroup();
delete ui;
}
void MainWindow::slotShowOrHideWindow()
{
setVisible(! isVisible());
}
void MainWindow::login()
{
QSettings settings;
settings.beginGroup("General");
const QString & user = settings.value("User").toString();
const QString & credential = settings.value("Credential").toString();
if (! user.isEmpty() && ! credential.isEmpty())
tcore->login(user, credential);
// DIRTY FIX! @TODO
{
QSettings settings;
settings.beginGroup("Transf0r");
transf0r->setStoragePath(settings.value("StorageLocation").toString());
}
{
QSettings settings;
settings.beginGroup("General");
tpanel->setQuickViewMode(settings.value("QuickViewMode").toBool());
}
}
void MainWindow::keyPressEvent(QKeyEvent *e)
{
switch (e->modifiers())
{
case Qt::CTRL:
switch (e->key())
{
case Qt::Key_F:
tpanel->keyEvent(e);
break;
}
break;
case Qt::ALT:
if (e->key() >= Qt::Key_1 && e->key() <= Qt::Key_9)
{
int delta = e->key() - Qt::Key_1;
if (delta < ui->tabWidget->count())
ui->tabWidget->setCurrentIndex(delta);
}
break;
default:
switch (e->key())
{
case Qt::Key_Escape:
tpanel->keyEvent(e);
break;
}
}
}
void MainWindow::slotRequestReceived(const Thunder::RemoteTask &task,
ThunderPanel::RequestType type,
bool autoOpen)
{
if (task.url.isEmpty())
{
//TODO: notification system
ui->statusBar->showMessage(tr("Task not ready, refused operation."));
return;
}
switch (type)
{
case ThunderPanel::Preview:
vpanel->play(task.url);
ui->tabWidget->setCurrentIndex(1);
break;
case ThunderPanel::Download:
transf0r->addCloudTask(task, autoOpen);
break;
default:
Q_ASSERT(false);
break;
}
}
bool MainWindow::question(const QString &body, const QString &title)
{
return QMessageBox::No == QMessageBox::question(this, title, body, QMessageBox::Yes,
QMessageBox::No);
}
void MainWindow::slotIndirectRequestReceived(ThunderPanel::IndirectRequestType type)
{
if (type == ThunderPanel::RemoveTasks)
{
const QStringList & ids = tpanel->getSelectedTaskIDs();
if (question ( tr("Remove selected %1 items?").arg(ids.size())) )
return;
tcore->removeCloudTasks(ids);
} else if (type == ThunderPanel::AddTask) {
}
}
void MainWindow::slotStatusChanged(ThunderCore::ChangeType type)
{
switch (type)
{
case ThunderCore::LoginChanged:
break;
case ThunderCore::TaskChanged:
{
const QList<Thunder::Task> & cloudTasks = tcore->getCloudTasks();
tpanel->setCloudTasks(cloudTasks);
foreach (const Thunder::Task & task, cloudTasks)
if (task.type == Thunder::BT)
{
tcore->getContentsOfBTFolder(task);
}
}
break;
case ThunderCore::CapchaReady:
{
SayCapcha *sayCapcha = new SayCapcha (this);
sayCapcha->setImage(tcore->getCapchaCode());
sayCapcha->exec();
tcore->loginWithCapcha(sayCapcha->getCapcha().toAscii());
}
break;
default:
Q_ASSERT(false);
break;
}
}
void MainWindow::slotError(const QString &body, ThunderCore::ErrorCategory category)
{
qDebug() << category << body;
}
void MainWindow::on_actionAboutAuthor_triggered()
{
QMessageBox::about(this,
QApplication::applicationName(),
tr("Xunlei VIP Cloud client $VER %1<br/><br/>"
"Written by <b>Aaron Lewis</b>"
" ([email protected])")
.arg(QApplication::applicationVersion()));
}
void MainWindow::slotSettingsChanged ()
{
// BUG: signal emitted prior to settings storage
// Trick: delayed function calls
QTimer::singleShot(1000, this, SLOT(login()));
}
void MainWindow::on_actionPreferences_triggered()
{
PreferencesDlg *dlg = new PreferencesDlg (this);
connect (dlg, SIGNAL(accepted()), SLOT(slotSettingsChanged()));
dlg->exec();
}
void MainWindow::on_actionQ_uit_triggered()
{
close ();
}
void MainWindow::on_actionAboutQt_triggered()
{
QMessageBox::aboutQt(this);
}
void MainWindow::on_actionNewTask_triggered()
{
// mandatory
show ();
AddCloudTask *act = new AddCloudTask (tcore, this);
act->exec();
}
void MainWindow::on_actionExportTasks_triggered()
{
const QList<Thunder::Task> & cloudTasks = tcore->getCloudTasks();
if (cloudTasks.isEmpty()) return;
const QString & file =
QFileDialog::getSaveFileName(this,
tr("Save task list to"),
Util::getHomeLocation(),
tr("Text files (*.txt);;All Files(*.*)"));
if (file.isEmpty()) return;
QByteArray data;
foreach (const Thunder::Task & task, cloudTasks)
{
data.append(task.source.toAscii()).append("\n");
}
bool ok = Util::writeFile(file, data);
if (ok)
{
QMessageBox::information(this, tr("Task exported successfully"),
tr("Exported %1 urls.").arg(cloudTasks.size()),
QMessageBox::Ok);
}
else
{
QMessageBox::warning(this,
tr("Can't export task list"),
tr("Something's wrong with the file you chosen."),
QMessageBox::Ok);
}
}
void MainWindow::on_actionCleanupHistory_triggered()
{
tcore->cleanupHistory();
}
void MainWindow::on_actionReloadTasks_triggered()
{
tcore->reloadCloudTasks();
}