1 | /* smplayer, GUI front-end for mplayer.
|
---|
2 | Copyright (C) 2006-2012 Ricardo Villalba <rvm@users.sourceforge.net>
|
---|
3 |
|
---|
4 | This program is free software; you can redistribute it and/or modify
|
---|
5 | it under the terms of the GNU General Public License as published by
|
---|
6 | the Free Software Foundation; either version 2 of the License, or
|
---|
7 | (at your option) any later version.
|
---|
8 |
|
---|
9 | This program is distributed in the hope that it will be useful,
|
---|
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
12 | GNU General Public License for more details.
|
---|
13 |
|
---|
14 | You should have received a copy of the GNU General Public License
|
---|
15 | along with this program; if not, write to the Free Software
|
---|
16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
17 | */
|
---|
18 |
|
---|
19 | #include "findsubtitleswindow.h"
|
---|
20 | #include "findsubtitlesconfigdialog.h"
|
---|
21 | #include "simplehttp.h"
|
---|
22 | #include "osparser.h"
|
---|
23 | #include "filehash.h"
|
---|
24 | #include "languages.h"
|
---|
25 | #include <QStandardItemModel>
|
---|
26 | #include <QSortFilterProxyModel>
|
---|
27 | #include <QHeaderView>
|
---|
28 | #include <QMessageBox>
|
---|
29 | #include <QDesktopServices>
|
---|
30 | #include <QUrl>
|
---|
31 | #include <QMap>
|
---|
32 | #include <QMenu>
|
---|
33 | #include <QAction>
|
---|
34 | #include <QClipboard>
|
---|
35 | #include <QSettings>
|
---|
36 |
|
---|
37 | #ifdef DOWNLOAD_SUBS
|
---|
38 | #include "filedownloader.h"
|
---|
39 | #include "subchooserdialog.h"
|
---|
40 | #include "quazip.h"
|
---|
41 | #include "quazipfile.h"
|
---|
42 | #include <QTemporaryFile>
|
---|
43 | #include <QBuffer>
|
---|
44 | #endif
|
---|
45 |
|
---|
46 | //#define NO_SMPLAYER_SUPPORT
|
---|
47 |
|
---|
48 | #ifndef NO_SMPLAYER_SUPPORT
|
---|
49 | #include "images.h"
|
---|
50 | #endif
|
---|
51 |
|
---|
52 | #define COL_LANG 0
|
---|
53 | #define COL_NAME 1
|
---|
54 | #define COL_FORMAT 2
|
---|
55 | #define COL_FILES 3
|
---|
56 | #define COL_DATE 4
|
---|
57 | #define COL_USER 5
|
---|
58 |
|
---|
59 | FindSubtitlesWindow::FindSubtitlesWindow( QWidget * parent, Qt::WindowFlags f )
|
---|
60 | : QDialog(parent,f)
|
---|
61 | {
|
---|
62 | setupUi(this);
|
---|
63 |
|
---|
64 | set = 0; // settings
|
---|
65 |
|
---|
66 | subtitles_for_label->setBuddy(file_chooser);
|
---|
67 |
|
---|
68 | progress->hide();
|
---|
69 |
|
---|
70 | connect( file_chooser, SIGNAL(fileChanged(QString)),
|
---|
71 | this, SLOT(setMovie(QString)) );
|
---|
72 | connect( file_chooser, SIGNAL(textChanged(const QString &)),
|
---|
73 | this, SLOT(updateRefreshButton()) );
|
---|
74 |
|
---|
75 | connect( refresh_button, SIGNAL(clicked()),
|
---|
76 | this, SLOT(refresh()) );
|
---|
77 |
|
---|
78 | connect( download_button, SIGNAL(clicked()),
|
---|
79 | this, SLOT(download()) );
|
---|
80 |
|
---|
81 | /*
|
---|
82 | connect( language_filter, SIGNAL(editTextChanged(const QString &)),
|
---|
83 | this, SLOT(applyFilter(const QString &)) );
|
---|
84 | */
|
---|
85 | connect( language_filter, SIGNAL(activated(int)),
|
---|
86 | this, SLOT(applyCurrentFilter()) );
|
---|
87 |
|
---|
88 | table = new QStandardItemModel(this);
|
---|
89 | table->setColumnCount(COL_USER + 1);
|
---|
90 |
|
---|
91 | proxy_model = new QSortFilterProxyModel(this);
|
---|
92 | proxy_model->setSourceModel(table);
|
---|
93 | proxy_model->setFilterKeyColumn(COL_LANG);
|
---|
94 | proxy_model->setFilterRole(Qt::UserRole);
|
---|
95 |
|
---|
96 | view->setModel(proxy_model);
|
---|
97 | view->setRootIsDecorated(false);
|
---|
98 | view->setSortingEnabled(true);
|
---|
99 | view->setAlternatingRowColors(true);
|
---|
100 | view->header()->setSortIndicator(COL_LANG, Qt::AscendingOrder);
|
---|
101 | view->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
---|
102 | view->setContextMenuPolicy( Qt::CustomContextMenu );
|
---|
103 |
|
---|
104 | connect(view, SIGNAL(activated(const QModelIndex &)),
|
---|
105 | this, SLOT(itemActivated(const QModelIndex &)) );
|
---|
106 | connect(view->selectionModel(), SIGNAL(currentChanged(const QModelIndex &,const QModelIndex &)),
|
---|
107 | this, SLOT(currentItemChanged(const QModelIndex &,const QModelIndex &)) );
|
---|
108 |
|
---|
109 | connect(view, SIGNAL(customContextMenuRequested(const QPoint &)),
|
---|
110 | this, SLOT(showContextMenu(const QPoint &)) );
|
---|
111 |
|
---|
112 | downloader = new SimpleHttp(this);
|
---|
113 |
|
---|
114 | connect( downloader, SIGNAL(downloadFailed(QString)),
|
---|
115 | this, SLOT(showError(QString)) );
|
---|
116 | connect( downloader, SIGNAL(downloadFinished(QByteArray)),
|
---|
117 | this, SLOT(downloadFinished()) );
|
---|
118 | connect( downloader, SIGNAL(downloadFinished(QByteArray)),
|
---|
119 | this, SLOT(parseInfo(QByteArray)) );
|
---|
120 | connect( downloader, SIGNAL(stateChanged(int)),
|
---|
121 | this, SLOT(updateRefreshButton()) );
|
---|
122 |
|
---|
123 | connect( downloader, SIGNAL(connecting(QString)),
|
---|
124 | this, SLOT(connecting(QString)) );
|
---|
125 | connect( downloader, SIGNAL(dataReadProgress(int, int)),
|
---|
126 | this, SLOT(updateDataReadProgress(int, int)) );
|
---|
127 |
|
---|
128 | #ifdef DOWNLOAD_SUBS
|
---|
129 | include_lang_on_filename = true;
|
---|
130 |
|
---|
131 | file_downloader = new FileDownloader(this);
|
---|
132 | file_downloader->setModal(false);
|
---|
133 | connect( file_downloader, SIGNAL(downloadFailed(QString)),
|
---|
134 | this, SLOT(showError(QString)), Qt::QueuedConnection );
|
---|
135 | connect( file_downloader, SIGNAL(downloadFinished(const QByteArray &)),
|
---|
136 | this, SLOT(archiveDownloaded(const QByteArray &)), Qt::QueuedConnection );
|
---|
137 | #endif
|
---|
138 |
|
---|
139 | // Actions
|
---|
140 | downloadAct = new QAction(this);
|
---|
141 | downloadAct->setEnabled(false);
|
---|
142 | connect( downloadAct, SIGNAL(triggered()), this, SLOT(download()) );
|
---|
143 |
|
---|
144 | copyLinkAct = new QAction(this);
|
---|
145 | copyLinkAct->setEnabled(false);
|
---|
146 | connect( copyLinkAct, SIGNAL(triggered()), this, SLOT(copyLink()) );
|
---|
147 |
|
---|
148 | context_menu = new QMenu(this);
|
---|
149 | context_menu->addAction(downloadAct);
|
---|
150 | context_menu->addAction(copyLinkAct);
|
---|
151 |
|
---|
152 | retranslateStrings();
|
---|
153 |
|
---|
154 | language_filter->setCurrentIndex(0);
|
---|
155 |
|
---|
156 | // Opensubtitles server
|
---|
157 | os_server = "http://www.opensubtitles.org";
|
---|
158 |
|
---|
159 | // Proxy
|
---|
160 | use_proxy = false;
|
---|
161 | proxy_type = QNetworkProxy::HttpProxy;
|
---|
162 | proxy_host = "";
|
---|
163 | proxy_port = 0;
|
---|
164 | proxy_username = "";
|
---|
165 | proxy_password = "";
|
---|
166 |
|
---|
167 | setupProxy();
|
---|
168 | }
|
---|
169 |
|
---|
170 | FindSubtitlesWindow::~FindSubtitlesWindow() {
|
---|
171 | if (set) saveSettings();
|
---|
172 | }
|
---|
173 |
|
---|
174 | void FindSubtitlesWindow::setSettings(QSettings * settings) {
|
---|
175 | set = settings;
|
---|
176 | loadSettings();
|
---|
177 | setupProxy();
|
---|
178 | }
|
---|
179 |
|
---|
180 | void FindSubtitlesWindow::setProxy(QNetworkProxy proxy) {
|
---|
181 | downloader->abort();
|
---|
182 | downloader->setProxy(proxy);
|
---|
183 |
|
---|
184 | #ifdef DOWNLOAD_SUBS
|
---|
185 | file_downloader->setProxy(proxy);
|
---|
186 | #endif
|
---|
187 |
|
---|
188 | qDebug("FindSubtitlesWindow::setProxy: host: '%s' port: %d type: %d",
|
---|
189 | proxy.hostName().toUtf8().constData(), proxy.port(), proxy.type());
|
---|
190 | }
|
---|
191 |
|
---|
192 | void FindSubtitlesWindow::retranslateStrings() {
|
---|
193 | retranslateUi(this);
|
---|
194 |
|
---|
195 | QStringList labels;
|
---|
196 | labels << tr("Language") << tr("Name") << tr("Format")
|
---|
197 | << tr("Files") << tr("Date") << tr("Uploaded by");
|
---|
198 |
|
---|
199 | table->setHorizontalHeaderLabels( labels );
|
---|
200 |
|
---|
201 | // Language combobox
|
---|
202 | //int language_index = language_filter->currentIndex();
|
---|
203 | QString current_language = language_filter->itemData(language_filter->currentIndex()).toString();
|
---|
204 | language_filter->clear();
|
---|
205 |
|
---|
206 | QMap<QString,QString> l = Languages::list();
|
---|
207 | QMapIterator<QString, QString> i(l);
|
---|
208 | while (i.hasNext()) {
|
---|
209 | i.next();
|
---|
210 | language_filter->addItem( i.value() + " (" + i.key() + ")", i.key() );
|
---|
211 | }
|
---|
212 | language_filter->model()->sort(0);
|
---|
213 | language_filter->insertItem( 0, tr("All"), "*" );
|
---|
214 | //language_filter->setCurrentIndex(language_index);
|
---|
215 | language_filter->setCurrentIndex(language_filter->findData(current_language));
|
---|
216 |
|
---|
217 | #if QT_VERSION < 0x040300
|
---|
218 | QPushButton * close_button = buttonBox->button(QDialogButtonBox::Close);
|
---|
219 | close_button->setText( tr("Close") );
|
---|
220 | #endif
|
---|
221 |
|
---|
222 | // Actions
|
---|
223 | downloadAct->setText( tr("&Download") );
|
---|
224 | copyLinkAct->setText( tr("&Copy link to clipboard") );
|
---|
225 |
|
---|
226 | // Icons
|
---|
227 | #ifndef NO_SMPLAYER_SUPPORT
|
---|
228 | download_button->setIcon( Images::icon("download") );
|
---|
229 | configure_button->setIcon( Images::icon("prefs") );
|
---|
230 | refresh_button->setIcon( Images::icon("refresh") );
|
---|
231 |
|
---|
232 | downloadAct->setIcon( Images::icon("download") );
|
---|
233 | copyLinkAct->setIcon( Images::icon("copy") );
|
---|
234 | #endif
|
---|
235 | }
|
---|
236 |
|
---|
237 | void FindSubtitlesWindow::setMovie(QString filename) {
|
---|
238 | qDebug("FindSubtitlesWindow::setMovie: '%s'", filename.toLatin1().constData());
|
---|
239 |
|
---|
240 | if (filename == last_file) {
|
---|
241 | return;
|
---|
242 | }
|
---|
243 |
|
---|
244 | file_chooser->setText(filename);
|
---|
245 | table->setRowCount(0);
|
---|
246 |
|
---|
247 | QString hash = FileHash::calculateHash(filename);
|
---|
248 | if (hash.isEmpty()) {
|
---|
249 | qWarning("FindSubtitlesWindow::setMovie: hash invalid. Doing nothing.");
|
---|
250 | } else {
|
---|
251 | QString link = os_server + "/search/sublanguageid-all/moviehash-" + hash + "/simplexml";
|
---|
252 | qDebug("FindSubtitlesWindow::setMovie: link: '%s'", link.toLatin1().constData());
|
---|
253 | downloader->download(link);
|
---|
254 | last_file = filename;
|
---|
255 | }
|
---|
256 | }
|
---|
257 |
|
---|
258 | void FindSubtitlesWindow::refresh() {
|
---|
259 | last_file = "";
|
---|
260 | setMovie(file_chooser->text());
|
---|
261 | }
|
---|
262 |
|
---|
263 | void FindSubtitlesWindow::updateRefreshButton() {
|
---|
264 | qDebug("FindSubtitlesWindow::updateRefreshButton: state: %d", downloader->state());
|
---|
265 | /*
|
---|
266 | QString file = file_chooser->lineEdit()->text();
|
---|
267 | bool enabled = ( (!file.isEmpty()) && (QFile::exists(file)) &&
|
---|
268 | (downloader->state()==QHttp::Unconnected) );
|
---|
269 | refresh_button->setEnabled(enabled);
|
---|
270 | */
|
---|
271 | refresh_button->setEnabled(true);
|
---|
272 | }
|
---|
273 |
|
---|
274 | void FindSubtitlesWindow::currentItemChanged(const QModelIndex & current, const QModelIndex & /*previous*/) {
|
---|
275 | qDebug("FindSubtitlesWindow::currentItemChanged: row: %d, col: %d", current.row(), current.column());
|
---|
276 | download_button->setEnabled(current.isValid());
|
---|
277 | downloadAct->setEnabled(current.isValid());
|
---|
278 | copyLinkAct->setEnabled(current.isValid());
|
---|
279 | }
|
---|
280 |
|
---|
281 | void FindSubtitlesWindow::applyFilter(const QString & filter) {
|
---|
282 | proxy_model->setFilterWildcard(filter);
|
---|
283 | }
|
---|
284 |
|
---|
285 | void FindSubtitlesWindow::applyCurrentFilter() {
|
---|
286 | //proxy_model->setFilterWildcard(language_filter->currentText());
|
---|
287 | QString filter = language_filter->itemData( language_filter->currentIndex() ).toString();
|
---|
288 | applyFilter(filter);
|
---|
289 | }
|
---|
290 |
|
---|
291 | void FindSubtitlesWindow::setLanguage(const QString & lang) {
|
---|
292 | int idx = language_filter->findData(lang);
|
---|
293 | if (idx < 0) idx = 0;
|
---|
294 | language_filter->setCurrentIndex(idx);
|
---|
295 | }
|
---|
296 |
|
---|
297 | QString FindSubtitlesWindow::language() {
|
---|
298 | int idx = language_filter->currentIndex();
|
---|
299 | return language_filter->itemData(idx).toString();
|
---|
300 | }
|
---|
301 |
|
---|
302 | void FindSubtitlesWindow::showError(QString error) {
|
---|
303 | status->setText( tr("Download failed") );
|
---|
304 |
|
---|
305 | QMessageBox::information(this, tr("Error"),
|
---|
306 | tr("Download failed: %1.")
|
---|
307 | .arg(error));
|
---|
308 | }
|
---|
309 |
|
---|
310 | void FindSubtitlesWindow::connecting(QString host) {
|
---|
311 | status->setText( tr("Connecting to %1...").arg(host) );
|
---|
312 | }
|
---|
313 |
|
---|
314 | void FindSubtitlesWindow::updateDataReadProgress(int done, int total) {
|
---|
315 | qDebug("FindSubtitlesWindow::updateDataReadProgress: %d, %d", done, total);
|
---|
316 |
|
---|
317 | status->setText( tr("Downloading...") );
|
---|
318 |
|
---|
319 | if (!progress->isVisible()) progress->show();
|
---|
320 | progress->setMaximum(total);
|
---|
321 | progress->setValue(done);
|
---|
322 | }
|
---|
323 |
|
---|
324 | void FindSubtitlesWindow::downloadFinished() {
|
---|
325 | status->setText( tr("Done.") );
|
---|
326 | progress->setMaximum(1);
|
---|
327 | progress->setValue(0);
|
---|
328 | progress->hide();
|
---|
329 | }
|
---|
330 |
|
---|
331 | void FindSubtitlesWindow::parseInfo(QByteArray xml_text) {
|
---|
332 | OSParser osparser;
|
---|
333 | bool ok = osparser.parseXml(xml_text);
|
---|
334 |
|
---|
335 | table->setRowCount(0);
|
---|
336 |
|
---|
337 | QMap <QString,QString> language_list = Languages::list();
|
---|
338 |
|
---|
339 | if (ok) {
|
---|
340 | QList<OSSubtitle> l = osparser.subtitleList();
|
---|
341 | for (int n=0; n < l.count(); n++) {
|
---|
342 |
|
---|
343 | QString title_name = l[n].movie;
|
---|
344 | if (!l[n].releasename.isEmpty()) {
|
---|
345 | title_name += " - " + l[n].releasename;
|
---|
346 | }
|
---|
347 |
|
---|
348 | QStandardItem * i_name = new QStandardItem(title_name);
|
---|
349 | i_name->setData( l[n].link );
|
---|
350 | #if QT_VERSION < 0x040400
|
---|
351 | i_name->setToolTip( l[n].link );
|
---|
352 | #endif
|
---|
353 |
|
---|
354 | QStandardItem * i_lang = new QStandardItem(l[n].language);
|
---|
355 | i_lang->setData(l[n].iso639, Qt::UserRole);
|
---|
356 | #if QT_VERSION < 0x040400
|
---|
357 | i_lang->setToolTip(l[n].iso639);
|
---|
358 | #endif
|
---|
359 | if (language_list.contains(l[n].iso639)) {
|
---|
360 | i_lang->setText( language_list[ l[n].iso639 ] );
|
---|
361 | }
|
---|
362 |
|
---|
363 | table->setItem(n, COL_LANG, i_lang);
|
---|
364 | table->setItem(n, COL_NAME, i_name);
|
---|
365 | table->setItem(n, COL_FORMAT, new QStandardItem(l[n].format));
|
---|
366 | table->setItem(n, COL_FILES, new QStandardItem(l[n].files));
|
---|
367 | table->setItem(n, COL_DATE, new QStandardItem(l[n].date));
|
---|
368 | table->setItem(n, COL_USER, new QStandardItem(l[n].user));
|
---|
369 |
|
---|
370 | }
|
---|
371 | status->setText( tr("%1 files available").arg(l.count()) );
|
---|
372 | applyCurrentFilter();
|
---|
373 |
|
---|
374 | qDebug("sort column: %d", view->header()->sortIndicatorSection());
|
---|
375 | qDebug("sort indicator: %d", view->header()->sortIndicatorOrder());
|
---|
376 |
|
---|
377 | table->sort( view->header()->sortIndicatorSection(),
|
---|
378 | view->header()->sortIndicatorOrder() );
|
---|
379 | } else {
|
---|
380 | status->setText( tr("Failed to parse the received data.") );
|
---|
381 | }
|
---|
382 |
|
---|
383 | view->resizeColumnToContents(COL_NAME);
|
---|
384 | }
|
---|
385 |
|
---|
386 | void FindSubtitlesWindow::itemActivated(const QModelIndex & index ) {
|
---|
387 | qDebug("FindSubtitlesWindow::itemActivated: row: %d, col %d", proxy_model->mapToSource(index).row(), proxy_model->mapToSource(index).column());
|
---|
388 |
|
---|
389 | QString download_link = table->item(proxy_model->mapToSource(index).row(), COL_NAME)->data().toString();
|
---|
390 |
|
---|
391 | qDebug("FindSubtitlesWindow::itemActivated: download link: '%s'", download_link.toLatin1().constData());
|
---|
392 |
|
---|
393 | #ifdef DOWNLOAD_SUBS
|
---|
394 | file_downloader->download( QUrl(download_link) );
|
---|
395 | file_downloader->show();
|
---|
396 | #else
|
---|
397 | QDesktopServices::openUrl( QUrl(download_link) );
|
---|
398 | #endif
|
---|
399 | }
|
---|
400 |
|
---|
401 | void FindSubtitlesWindow::download() {
|
---|
402 | qDebug("FindSubtitlesWindow::download");
|
---|
403 | if (view->currentIndex().isValid()) {
|
---|
404 | itemActivated(view->currentIndex());
|
---|
405 | }
|
---|
406 | }
|
---|
407 |
|
---|
408 | void FindSubtitlesWindow::copyLink() {
|
---|
409 | qDebug("FindSubtitlesWindow::copyLink");
|
---|
410 | if (view->currentIndex().isValid()) {
|
---|
411 | const QModelIndex & index = view->currentIndex();
|
---|
412 | QString download_link = table->item(proxy_model->mapToSource(index).row(), COL_NAME)->data().toString();
|
---|
413 | qDebug("FindSubtitlesWindow::copyLink: link: '%s'", download_link.toLatin1().constData());
|
---|
414 | qApp->clipboard()->setText(download_link);
|
---|
415 | }
|
---|
416 | }
|
---|
417 |
|
---|
418 | void FindSubtitlesWindow::showContextMenu(const QPoint & pos) {
|
---|
419 | qDebug("FindSubtitlesWindow::showContextMenu");
|
---|
420 |
|
---|
421 | context_menu->move( view->viewport()->mapToGlobal(pos) );
|
---|
422 | context_menu->show();
|
---|
423 | }
|
---|
424 |
|
---|
425 | // Language change stuff
|
---|
426 | void FindSubtitlesWindow::changeEvent(QEvent *e) {
|
---|
427 | if (e->type() == QEvent::LanguageChange) {
|
---|
428 | retranslateStrings();
|
---|
429 | } else {
|
---|
430 | QWidget::changeEvent(e);
|
---|
431 | }
|
---|
432 | }
|
---|
433 |
|
---|
434 | #ifdef DOWNLOAD_SUBS
|
---|
435 | void FindSubtitlesWindow::archiveDownloaded(const QByteArray & buffer) {
|
---|
436 | qDebug("FindSubtitlesWindow::archiveDownloaded");
|
---|
437 |
|
---|
438 | QString temp_dir = QDir::tempPath();
|
---|
439 | if (!temp_dir.endsWith("/")) temp_dir += "/";
|
---|
440 |
|
---|
441 | QTemporaryFile file(temp_dir + "archive_XXXXXX.zip");
|
---|
442 | file.setAutoRemove(false);
|
---|
443 |
|
---|
444 | qDebug("FindSubtitlesWindow::archiveDownloaded: a temporary file will be saved in folder '%s'", temp_dir.toUtf8().constData());
|
---|
445 |
|
---|
446 | if (file.open()) {
|
---|
447 | QString filename = file.fileName();
|
---|
448 | file.write( buffer );
|
---|
449 | file.close();
|
---|
450 |
|
---|
451 | qDebug("FindSubtitlesWindow::archiveDownloaded: file saved as: %s", filename.toUtf8().constData());
|
---|
452 |
|
---|
453 | /*
|
---|
454 | QMessageBox::information(this, tr("Downloaded"), tr("File saved as %1").arg(filename));
|
---|
455 | return;
|
---|
456 | */
|
---|
457 |
|
---|
458 | status->setText(tr("Temporary file %1").arg(filename));
|
---|
459 |
|
---|
460 | QString lang = "unknown";
|
---|
461 | QString extension = "unknown";
|
---|
462 | if (view->currentIndex().isValid()) {
|
---|
463 | const QModelIndex & index = view->currentIndex();
|
---|
464 | lang = table->item(proxy_model->mapToSource(index).row(), COL_LANG)->data(Qt::UserRole).toString();
|
---|
465 | extension = table->item(proxy_model->mapToSource(index).row(), COL_FORMAT)->text();
|
---|
466 | }
|
---|
467 |
|
---|
468 | QFileInfo fi(file_chooser->text());
|
---|
469 | QString output_name = fi.completeBaseName();
|
---|
470 | if (include_lang_on_filename) output_name += "_"+ lang;
|
---|
471 | output_name += "." + extension;
|
---|
472 |
|
---|
473 | if (!uncompressZip(filename, fi.absolutePath(), output_name)) {
|
---|
474 | status->setText(tr("Download failed"));
|
---|
475 | }
|
---|
476 | file.remove();
|
---|
477 | }
|
---|
478 | else {
|
---|
479 | qWarning("FindSubtitlesWindow::archiveDownloaded: can't write temporary file");
|
---|
480 | QMessageBox::warning(this, tr("Error saving file"),
|
---|
481 | tr("It wasn't possible to save the downloaded\n"
|
---|
482 | "file in folder %1\n"
|
---|
483 | "Please check the permissions of that folder.").arg(temp_dir));
|
---|
484 | }
|
---|
485 | }
|
---|
486 |
|
---|
487 |
|
---|
488 | bool FindSubtitlesWindow::uncompressZip(const QString & filename, const QString & output_path, const QString & preferred_output_name) {
|
---|
489 | qDebug("FindSubtitlesWindow::uncompressZip: zip file '%s', output_path '%s', save subtitle as '%s'",
|
---|
490 | filename.toUtf8().constData(), output_path.toUtf8().constData(),
|
---|
491 | preferred_output_name.toUtf8().constData());
|
---|
492 |
|
---|
493 | QuaZip zip(filename);
|
---|
494 |
|
---|
495 | if (!zip.open(QuaZip::mdUnzip)) {
|
---|
496 | qWarning("FindSubtitlesWindow::uncompressZip: open zip failed: %d", zip.getZipError());
|
---|
497 | return false;
|
---|
498 | }
|
---|
499 |
|
---|
500 | zip.setFileNameCodec("IBM866");
|
---|
501 | qDebug("FindSubtitlesWindow::uncompressZip: %d entries", zip.getEntriesCount());
|
---|
502 | qDebug("FindSubtitlesWindow::uncompressZip: global comment: '%s'", zip.getComment().toUtf8().constData());
|
---|
503 |
|
---|
504 | QStringList sub_files;
|
---|
505 | QuaZipFileInfo info;
|
---|
506 |
|
---|
507 | for (bool more=zip.goToFirstFile(); more; more=zip.goToNextFile()) {
|
---|
508 | if (!zip.getCurrentFileInfo(&info)) {
|
---|
509 | qWarning("FindSubtitlesWindow::uncompressZip: getCurrentFileInfo(): %d\n", zip.getZipError());
|
---|
510 | return false;
|
---|
511 | }
|
---|
512 | qDebug("FindSubtitlesWindow::uncompressZip: file '%s'", info.name.toUtf8().constData());
|
---|
513 | if (QFileInfo(info.name).suffix() != "nfo") sub_files.append(info.name);
|
---|
514 | }
|
---|
515 |
|
---|
516 | qDebug("FindSubtitlesWindow::uncompressZip: list of subtitle files:");
|
---|
517 | for (int n=0; n < sub_files.count(); n++) {
|
---|
518 | qDebug("FindSubtitlesWindow::uncompressZip: subtitle file %d '%s'", n, sub_files[n].toUtf8().constData());
|
---|
519 | }
|
---|
520 |
|
---|
521 | if (sub_files.count() == 1) {
|
---|
522 | // If only one file, just extract it
|
---|
523 | QString output_name = output_path +"/"+ preferred_output_name;
|
---|
524 | if (extractFile(zip, sub_files[0], output_name )) {
|
---|
525 | status->setText(tr("Subtitle saved as %1").arg(preferred_output_name));
|
---|
526 | emit subtitleDownloaded(output_name);
|
---|
527 | } else {
|
---|
528 | return false;
|
---|
529 | }
|
---|
530 | } else {
|
---|
531 | // More than one file
|
---|
532 | SubChooserDialog d(this);
|
---|
533 |
|
---|
534 | for (int n=0; n < sub_files.count(); n++) {
|
---|
535 | d.addFile(sub_files[n]);
|
---|
536 | }
|
---|
537 |
|
---|
538 | if (d.exec() == QDialog::Rejected) return false;
|
---|
539 |
|
---|
540 | QStringList files_to_extract = d.selectedFiles();
|
---|
541 | int extracted_count = 0;
|
---|
542 | for (int n=0; n < files_to_extract.count(); n++) {
|
---|
543 | QString file = files_to_extract[n];
|
---|
544 | bool ok = extractFile(zip, file, output_path +"/"+ file);
|
---|
545 | qDebug("FindSubtitlesWindow::uncompressZip: extracted %s ok: %d", file.toUtf8().constData(), ok);
|
---|
546 | if (ok) extracted_count++;
|
---|
547 | }
|
---|
548 | status->setText(tr("%1 subtitle(s) extracted","", extracted_count).arg(extracted_count));
|
---|
549 | if (extracted_count > 0) {
|
---|
550 | emit subtitleDownloaded( output_path +"/"+ files_to_extract[0] );
|
---|
551 | }
|
---|
552 | }
|
---|
553 |
|
---|
554 | zip.close();
|
---|
555 | return true;
|
---|
556 | }
|
---|
557 |
|
---|
558 | bool FindSubtitlesWindow::extractFile(QuaZip & zip, const QString & filename, const QString & output_name) {
|
---|
559 | qDebug("FindSubtitlesWindow::extractFile: '%s', save as '%s'", filename.toUtf8().constData(), output_name.toUtf8().constData());
|
---|
560 |
|
---|
561 | if (QFile::exists(output_name)) {
|
---|
562 | if (QMessageBox::question(this, tr("Overwrite?"),
|
---|
563 | tr("The file %1 already exits, overwrite?").arg(output_name), QMessageBox::Yes, QMessageBox::No) == QMessageBox::No)
|
---|
564 | {
|
---|
565 | return false;
|
---|
566 | }
|
---|
567 | }
|
---|
568 |
|
---|
569 | if (!zip.setCurrentFile(filename)) {
|
---|
570 | qDebug("FindSubtitlesWindow::extractFile: can't select file %s", filename.toUtf8().constData());
|
---|
571 | return false;
|
---|
572 | }
|
---|
573 |
|
---|
574 | // Saving
|
---|
575 | char c;
|
---|
576 | QuaZipFile file(&zip);
|
---|
577 | QFile out(output_name);
|
---|
578 |
|
---|
579 | if (!file.open(QIODevice::ReadOnly)) {
|
---|
580 | qWarning("FindSubtitlesWindow::extractFile: can't open file for reading: %d", file.getZipError());
|
---|
581 | return false;
|
---|
582 | }
|
---|
583 |
|
---|
584 | if (out.open(QIODevice::WriteOnly)) {
|
---|
585 | // Slow like hell (on GNU/Linux at least), but it is not my fault.
|
---|
586 | // Not ZIP/UNZIP package's fault either.
|
---|
587 | // The slowest thing here is out.putChar(c).
|
---|
588 | while(file.getChar(&c)) out.putChar(c);
|
---|
589 | out.close();
|
---|
590 |
|
---|
591 | file.close();
|
---|
592 | } else {
|
---|
593 | qWarning("FindSubtitlesWindow::extractFile: can't open %s for writing", output_name.toUtf8().constData());
|
---|
594 | return false;
|
---|
595 | }
|
---|
596 |
|
---|
597 | return true;
|
---|
598 | }
|
---|
599 |
|
---|
600 | #endif
|
---|
601 |
|
---|
602 | void FindSubtitlesWindow::on_configure_button_clicked() {
|
---|
603 | qDebug("FindSubtitlesWindow::on_configure_button_clicked");
|
---|
604 |
|
---|
605 | FindSubtitlesConfigDialog d(this);
|
---|
606 |
|
---|
607 | d.setServer( os_server );
|
---|
608 | d.setUseProxy( use_proxy );
|
---|
609 | d.setProxyHostname( proxy_host );
|
---|
610 | d.setProxyPort( proxy_port );
|
---|
611 | d.setProxyUsername( proxy_username );
|
---|
612 | d.setProxyPassword( proxy_password );
|
---|
613 | d.setProxyType( proxy_type );
|
---|
614 |
|
---|
615 | if (d.exec() == QDialog::Accepted) {
|
---|
616 | os_server = d.server();
|
---|
617 | use_proxy = d.useProxy();
|
---|
618 | proxy_host = d.proxyHostname();
|
---|
619 | proxy_port = d.proxyPort();
|
---|
620 | proxy_username = d.proxyUsername();
|
---|
621 | proxy_password = d.proxyPassword();
|
---|
622 | proxy_type = d.proxyType();
|
---|
623 |
|
---|
624 | setupProxy();
|
---|
625 | }
|
---|
626 | }
|
---|
627 |
|
---|
628 | void FindSubtitlesWindow::setupProxy() {
|
---|
629 | QNetworkProxy proxy;
|
---|
630 |
|
---|
631 | if ( (use_proxy) && (!proxy_host.isEmpty()) ) {
|
---|
632 | proxy.setType((QNetworkProxy::ProxyType) proxy_type);
|
---|
633 | proxy.setHostName(proxy_host);
|
---|
634 | proxy.setPort(proxy_port);
|
---|
635 | if ( (!proxy_username.isEmpty()) && (!proxy_password.isEmpty()) ) {
|
---|
636 | proxy.setUser(proxy_username);
|
---|
637 | proxy.setPassword(proxy_password);
|
---|
638 | }
|
---|
639 | qDebug("FindSubtitlesWindow::userProxy: using proxy: host: %s, port: %d, type: %d",
|
---|
640 | proxy_host.toUtf8().constData(), proxy_port, proxy_type);
|
---|
641 | } else {
|
---|
642 | // No proxy
|
---|
643 | proxy.setType(QNetworkProxy::NoProxy);
|
---|
644 | qDebug("FindSubtitlesDialog::userProxy: no proxy");
|
---|
645 | }
|
---|
646 |
|
---|
647 | setProxy(proxy);
|
---|
648 | }
|
---|
649 |
|
---|
650 | void FindSubtitlesWindow::saveSettings() {
|
---|
651 | qDebug("FindSubtitlesWindow::saveSettings");
|
---|
652 |
|
---|
653 | set->beginGroup("findsubtitles");
|
---|
654 |
|
---|
655 | set->setValue("server", os_server);
|
---|
656 | set->setValue("language", language());
|
---|
657 | #ifdef DOWNLOAD_SUBS
|
---|
658 | set->setValue("include_lang_on_filename", includeLangOnFilename());
|
---|
659 | #endif
|
---|
660 | set->setValue("proxy/use_proxy", use_proxy);
|
---|
661 | set->setValue("proxy/type", proxy_type);
|
---|
662 | set->setValue("proxy/host", proxy_host);
|
---|
663 | set->setValue("proxy/port", proxy_port);
|
---|
664 | set->setValue("proxy/username", proxy_username);
|
---|
665 | set->setValue("proxy/password", proxy_password);
|
---|
666 |
|
---|
667 | set->endGroup();
|
---|
668 | }
|
---|
669 |
|
---|
670 | void FindSubtitlesWindow::loadSettings() {
|
---|
671 | qDebug("FindSubtitlesWindow::loadSettings");
|
---|
672 |
|
---|
673 | set->beginGroup("findsubtitles");
|
---|
674 |
|
---|
675 | os_server = set->value("server", os_server).toString();
|
---|
676 | setLanguage( set->value("language", language()).toString() );
|
---|
677 | #ifdef DOWNLOAD_SUBS
|
---|
678 | setIncludeLangOnFilename( set->value("include_lang_on_filename", includeLangOnFilename()).toBool() );
|
---|
679 | #endif
|
---|
680 | use_proxy = set->value("proxy/use_proxy", use_proxy).toBool();
|
---|
681 | proxy_type = set->value("proxy/type", proxy_type).toInt();
|
---|
682 | proxy_host = set->value("proxy/host", proxy_host).toString();
|
---|
683 | proxy_port = set->value("proxy/port", proxy_port).toInt();
|
---|
684 | proxy_username = set->value("proxy/username", proxy_username).toString();
|
---|
685 | proxy_password = set->value("proxy/password", proxy_password).toString();
|
---|
686 |
|
---|
687 | set->endGroup();
|
---|
688 | }
|
---|
689 |
|
---|
690 | #include "moc_findsubtitleswindow.cpp"
|
---|
691 |
|
---|