Debian Codegraph @

gui/src/qnapiapp.cpp

raw symbols SCIP
1 /*****************************************************************************
2 ** QNapi
3 ** Copyright (C) 2008-2017 Piotr KrzemiƄski <[email protected]>
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
11 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
12 **
13 *****************************************************************************/
14
15 #include "qnapiapp.h"
16
17 #include "libqnapi.h"
18
19 QNapiApp::QNapiApp(int &argc, char **argv, bool useGui, const QString &appName)
20 : QSingleApplication(argc, argv, useGui, appName),
21 creationDT(QDateTime::currentDateTime()),
22 enginesRegistry(LibQNapi::subtitleDownloadEngineRegistry()) {
23 setAttribute(Qt::AA_UseHighDpiPixmaps, true);
24
25 openDialog = 0;
26
27 f_progress = 0;
28 f_options = 0;
29 f_about = 0;
30 f_scan = 0;
31 f_convert = 0;
32
33 getAction = 0;
34 scanAction = 0;
35 convertAction = 0;
36 napiGetAction = 0;
37 napiCreateUserAction = 0;
38 osGetAction = 0;
39 osAddAction = 0;
40 settingsAction = 0;
41 aboutAction = 0;
42 quitAction = 0;
43
44 napiSubMenu = 0;
45 osSubMenu = 0;
46 trayIconMenu = 0;
47 trayIcon = 0;
48 }
49
50 QNapiApp::~QNapiApp() {
51 if (openDialog) delete openDialog;
52
53 if (f_progress) delete f_progress;
54 if (f_options) delete f_options;
55 if (f_about) delete f_about;
56 if (f_scan) delete f_scan;
57 if (f_convert) delete f_convert;
58
59 if (getAction) delete getAction;
60 if (scanAction) delete scanAction;
61 if (convertAction) delete convertAction;
62 if (napiGetAction) delete napiGetAction;
63 if (napiCreateUserAction) delete napiCreateUserAction;
64 if (osGetAction) delete osGetAction;
65 if (osAddAction) delete osAddAction;
66 if (settingsAction) delete settingsAction;
67 if (aboutAction) delete aboutAction;
68 if (quitAction) delete quitAction;
69
70 if (napiSubMenu) delete napiSubMenu;
71 if (trayIconMenu) delete trayIconMenu;
72 if (trayIcon) delete trayIcon;
73 }
74
75 frmProgress *QNapiApp::progress() {
76 if (!f_progress) {
77 f_progress = new frmProgress();
78 if (!f_progress) abort();
79 connect(this, SIGNAL(request(QString)), f_progress,
80 SLOT(receiveRequest(QString)));
81 connect(this, SIGNAL(downloadFile(const QString &)), f_progress,
82 SLOT(receiveRequest(const QString &)));
83 }
84 return f_progress;
85 }
86
87 void QNapiApp::createTrayIcon() {
88 getAction = new QAction(tr("Download subtitles"), 0);
89 connect(getAction, SIGNAL(triggered()), this, SLOT(showOpenDialog()));
90
91 scanAction = new QAction(tr("Scan directories"), 0);
92 connect(scanAction, SIGNAL(triggered()), this, SLOT(showScanDialog()));
93
94 convertAction = new QAction(tr("Convert subtitles"), 0);
95 connect(convertAction, SIGNAL(triggered()), this, SLOT(showConvertDialog()));
96
97 napiGetAction = new QAction(tr("Download subtitles"), 0);
98 connect(napiGetAction, &QAction::triggered,
99 [this] { showOpenDialog("NapiProjekt"); });
100
101 napiCreateUserAction = new QAction(tr("Create an account"), 0);
102 connect(napiCreateUserAction, &QAction::triggered,
103 [this] { showCreateAccount("NapiProjekt"); });
104
105 osGetAction = new QAction(tr("Download subtitles"), 0);
106 connect(osGetAction, &QAction::triggered,
107 [this] { showOpenDialog("OpenSubtitles"); });
108
109 osAddAction = new QAction(tr("Upload subtitles"), 0);
110 connect(osAddAction, SIGNAL(triggered()), this, SLOT(showOSUploadDialog()));
111
112 osCreateUserAction = new QAction(tr("Create an account"), 0);
113 connect(osCreateUserAction, &QAction::triggered,
114 [this] { showCreateAccount("OpenSubtitles"); });
115
116 napisy24GetAction = new QAction(tr("Download subtitles"), 0);
117 connect(napisy24GetAction, &QAction::triggered,
118 [this] { showOpenDialog("Napisy24"); });
119
120 napisy24CreateUserAction = new QAction(tr("Create an account"), 0);
121 connect(napisy24CreateUserAction, &QAction::triggered,
122 [this] { showCreateAccount("OpenSNapisy24ubtitles"); });
123
124 settingsAction = new QAction(tr("Settings"), 0);
125 connect(settingsAction, SIGNAL(triggered()), this, SLOT(showSettings()));
126
127 aboutAction = new QAction(tr("About"), 0);
128 connect(aboutAction, SIGNAL(triggered()), this, SLOT(showAbout()));
129
130 quitAction = new QAction(tr("Quit"), 0);
131 connect(quitAction, SIGNAL(triggered()), this, SLOT(tryQuit()));
132
133 napiSubMenu = new QMenu(0);
134 napiSubMenu->setTitle(tr("NapiProjekt"));
135 napiSubMenu->addAction(napiGetAction);
136 napiSubMenu->addAction(napiCreateUserAction);
137
138 osSubMenu = new QMenu(0);
139 osSubMenu->setTitle(tr("OpenSubtitles"));
140 osSubMenu->addAction(osGetAction);
141 osSubMenu->addAction(osAddAction);
142 osSubMenu->addAction(osCreateUserAction);
143
144 napisy24SubMenu = new QMenu(0);
145 napisy24SubMenu->setTitle(tr("Napisy24"));
146 napisy24SubMenu->addAction(napisy24GetAction);
147 napisy24SubMenu->addAction(napisy24CreateUserAction);
148
149 trayIconMenu = new QMenu(0);
150 trayIconMenu->addAction(getAction);
151 trayIconMenu->addAction(scanAction);
152 trayIconMenu->addAction(convertAction);
153 trayIconMenu->addSeparator();
154 trayIconMenu->addMenu(napiSubMenu);
155 trayIconMenu->addMenu(osSubMenu);
156 trayIconMenu->addMenu(napisy24SubMenu);
157 trayIconMenu->addSeparator();
158 trayIconMenu->addAction(settingsAction);
159 trayIconMenu->addAction(aboutAction);
160 trayIconMenu->addSeparator();
161 trayIconMenu->addAction(quitAction);
162
163 trayIcon = new QSystemTrayIcon(0);
164 trayIcon->setContextMenu(trayIconMenu);
165
166 trayIcon->setIcon(QIcon(":/icon/qnapi.png"));
167
168 connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this,
169 SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
170
171 trayIcon->show();
172 }
173
174 void QNapiApp::showTrayMessage(QString title, QString msg) {
175 trayIcon->showMessage(title, msg);
176 }
177
178 bool QNapiApp::showOpenDialog(QString engine) {
179 QStringList fileList;
180
181 if (!openDialog) {
182 QString initDir = LibQNapi::loadConfig().lastOpenedDir();
183
184 if (!QFileInfo(initDir).isDir()) {
185 initDir = QDir::homePath();
186 }
187
188 openDialog = new QNapiOpenDialog(
189 0, tr("Select one or more video files to download subtitles for"),
190 initDir, QNapiOpenDialog::Movies);
191 } else if (openDialog) {
192 openDialog->raise();
193 return true;
194 } else if (f_progress) {
195 f_progress->raise();
196 return true;
197 }
198
199 if (openDialog->selectFiles()) {
200 fileList = openDialog->selectedFiles();
201
202 if (!fileList.isEmpty()) {
203 QString dialogPath = QFileInfo(fileList.first()).absolutePath();
204 auto newConfig = LibQNapi::loadConfig().setLastOpenedDir(dialogPath);
205 LibQNapi::writeConfig(newConfig);
206 }
207 }
208
209 delete openDialog;
210 openDialog = 0;
211
212 if (!fileList.isEmpty()) {
213 if (!engine.isEmpty()) {
214 progress()->setSpecificEngine(engine);
215 } else {
216 progress()->clearSpecificEngine();
217 }
218
219 progress()->enqueueFiles(fileList);
220 progress()->download();
221 } else if (progress()->isBatchMode()) {
222 return false;
223 }
224
225 return true;
226 }
227
228 bool QNapiApp::showScanDialog(QString init_dir) {
229 if (!f_scan) f_scan = new frmScan();
230
231 if (f_scan->isVisible()) {
232 f_scan->raise();
233 return false;
234 }
235
236 f_scan->setInitDir(init_dir);
237
238 bool result = false;
239
240 if (f_scan->exec() == QDialog::Accepted) {
241 result = true;
242 progress()->enqueueFiles(f_scan->getSelectedFiles());
243 progress()->download();
244 }
245
246 delete f_scan;
247 f_scan = 0;
248 return result;
249 }
250
251 void QNapiApp::showConvertDialog() {
252 if (!f_convert) f_convert = new frmConvert();
253
254 if (f_convert->isVisible()) {
255 f_convert->raise();
256 return;
257 }
258
259 f_convert->exec();
260
261 delete f_scan;
262 f_scan = 0;
263 }
264
265 void QNapiApp::showCreateAccount(const QString &engineName) const {
266 Maybe<QUrl> maybeRegistrationUrl =
267 enginesRegistry->engineMetadata(engineName).registrationUrl();
268
269 if (maybeRegistrationUrl) {
270 QDesktopServices::openUrl(maybeRegistrationUrl.value());
271 }
272 }
273
274 void QNapiApp::showOSUploadDialog() const {
275 QDesktopServices::openUrl(QUrl("http://www.opensubtitles.org/upload"));
276 }
277
278 void QNapiApp::showSettings() {
279 if (!f_options) {
280 f_options = new frmOptions();
281 f_options->readConfig(LibQNapi::loadConfig());
282 }
283
284 if (f_options->isVisible()) {
285 f_options->raise();
286 return;
287 }
288
289 if (f_options->exec() == QDialog::Accepted) f_options->writeConfig();
290
291 delete f_options;
292 f_options = 0;
293 }
294
295 void QNapiApp::showAbout() {
296 if (!f_about) f_about = new frmAbout();
297 if (f_about->isVisible()) {
298 f_about->raise();
299 return;
300 }
301 f_about->exec();
302 delete f_about;
303 f_about = 0;
304 }
305
306 void QNapiApp::tryQuit() {
307 if (progress()->isVisible() && !progress()->close()) return;
308 quit();
309 }
310
311 void QNapiApp::trayIconActivated(QSystemTrayIcon::ActivationReason reason) {
312 if (reason == QSystemTrayIcon::Trigger)
313 #ifndef Q_OS_MAC
314 showOpenDialog()
315 #endif
316 ;
317 }
318
319 bool QNapiApp::event(QEvent *ev) {
320 if (ev->type() == QEvent::FileOpen) {
321 bool batchMode = creationDT.secsTo(QDateTime::currentDateTime()) <= 1;
322 progress()->setBatchMode(batchMode);
323 emit downloadFile(static_cast<QFileOpenEvent *>(ev)->file());
324 } else {
325 return QApplication::event(ev);
326 }
327
328 return true;
329 }