source: smplayer/trunk/src/findsubtitles/findsubtitleswindow.cpp

Last change on this file was 188, checked in by Silvan Scherrer, 8 years ago

SMPlayer: update trunk to version 17.1.0

  • Property svn:eol-style set to LF
File size: 26.9 KB
RevLine 
[114]1/* smplayer, GUI front-end for mplayer.
[188]2 Copyright (C) 2006-2017 Ricardo Villalba <rvm@users.sourceforge.net>
[114]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"
[142]21
22#include "osclient.h"
[128]23#include "filehash.h"
[114]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
[156]38#include <QBuffer>
[114]39#include "filedownloader.h"
40#include "subchooserdialog.h"
[156]41#include "fixsubs.h"
42
43#ifdef USE_QUAZIP
[114]44#include "quazip.h"
45#include "quazipfile.h"
46#include <QTemporaryFile>
[156]47#else
48#include <zlib.h>
[114]49#endif
50
[156]51#endif
52
[181]53#if QT_VERSION >= 0x050000
54#include "myscroller.h"
55#endif
56
[114]57//#define NO_SMPLAYER_SUPPORT
58
59#ifndef NO_SMPLAYER_SUPPORT
60#include "images.h"
61#endif
62
63#define COL_LANG 0
64#define COL_NAME 1
65#define COL_FORMAT 2
66#define COL_FILES 3
67#define COL_DATE 4
68#define COL_USER 5
69
70FindSubtitlesWindow::FindSubtitlesWindow( QWidget * parent, Qt::WindowFlags f )
[181]71 : QWidget(parent,f)
[114]72{
73 setupUi(this);
74
75 set = 0; // settings
76
[124]77 subtitles_for_label->setBuddy(file_chooser);
[114]78
79 progress->hide();
80
81 connect( file_chooser, SIGNAL(fileChanged(QString)),
82 this, SLOT(setMovie(QString)) );
[124]83 connect( file_chooser, SIGNAL(textChanged(const QString &)),
[114]84 this, SLOT(updateRefreshButton()) );
85
86 connect( refresh_button, SIGNAL(clicked()),
87 this, SLOT(refresh()) );
88
89 connect( download_button, SIGNAL(clicked()),
90 this, SLOT(download()) );
91
92 /*
93 connect( language_filter, SIGNAL(editTextChanged(const QString &)),
94 this, SLOT(applyFilter(const QString &)) );
95 */
96 connect( language_filter, SIGNAL(activated(int)),
97 this, SLOT(applyCurrentFilter()) );
98
99 table = new QStandardItemModel(this);
100 table->setColumnCount(COL_USER + 1);
101
102 proxy_model = new QSortFilterProxyModel(this);
103 proxy_model->setSourceModel(table);
104 proxy_model->setFilterKeyColumn(COL_LANG);
105 proxy_model->setFilterRole(Qt::UserRole);
106
107 view->setModel(proxy_model);
108 view->setRootIsDecorated(false);
109 view->setSortingEnabled(true);
110 view->setAlternatingRowColors(true);
111 view->header()->setSortIndicator(COL_LANG, Qt::AscendingOrder);
112 view->setEditTriggers(QAbstractItemView::NoEditTriggers);
113 view->setContextMenuPolicy( Qt::CustomContextMenu );
114
[181]115#if QT_VERSION >= 0x050000
116 MyScroller::setScroller(view->viewport());
117#endif
118
[114]119 connect(view, SIGNAL(activated(const QModelIndex &)),
120 this, SLOT(itemActivated(const QModelIndex &)) );
121 connect(view->selectionModel(), SIGNAL(currentChanged(const QModelIndex &,const QModelIndex &)),
122 this, SLOT(currentItemChanged(const QModelIndex &,const QModelIndex &)) );
123
124 connect(view, SIGNAL(customContextMenuRequested(const QPoint &)),
125 this, SLOT(showContextMenu(const QPoint &)) );
126
[142]127 /*
[114]128 downloader = new SimpleHttp(this);
129
130 connect( downloader, SIGNAL(downloadFailed(QString)),
131 this, SLOT(showError(QString)) );
132 connect( downloader, SIGNAL(downloadFinished(QByteArray)),
133 this, SLOT(downloadFinished()) );
134 connect( downloader, SIGNAL(downloadFinished(QByteArray)),
135 this, SLOT(parseInfo(QByteArray)) );
136 connect( downloader, SIGNAL(stateChanged(int)),
137 this, SLOT(updateRefreshButton()) );
138
139 connect( downloader, SIGNAL(connecting(QString)),
140 this, SLOT(connecting(QString)) );
141 connect( downloader, SIGNAL(dataReadProgress(int, int)),
142 this, SLOT(updateDataReadProgress(int, int)) );
[142]143 */
[114]144
[142]145 osclient = new OSClient();
146 connect( osclient, SIGNAL(searchFinished()), this, SLOT(downloadFinished()) );
147 connect( osclient, SIGNAL(searchFinished()), this, SLOT(parseInfo()) );
148 connect( osclient, SIGNAL(loginFailed()), this, SLOT(showLoginFailed()) );
149 connect( osclient, SIGNAL(searchFailed()), this, SLOT(showSearchFailed()) );
[156]150 connect( osclient, SIGNAL(errorFound(int, const QString &)), this, SLOT(showErrorOS(int, const QString &)) );
[142]151
[114]152#ifdef DOWNLOAD_SUBS
153 include_lang_on_filename = true;
154
155 file_downloader = new FileDownloader(this);
[132]156 file_downloader->setModal(false);
[176]157 file_downloader->hide();
158
[114]159 connect( file_downloader, SIGNAL(downloadFailed(QString)),
160 this, SLOT(showError(QString)), Qt::QueuedConnection );
161 connect( file_downloader, SIGNAL(downloadFinished(const QByteArray &)),
162 this, SLOT(archiveDownloaded(const QByteArray &)), Qt::QueuedConnection );
[132]163 connect( this, SIGNAL(subtitleDownloaded(const QString &)),
164 this, SLOT(fixSubtitles(const QString &)) );
[114]165#endif
166
167 // Actions
168 downloadAct = new QAction(this);
169 downloadAct->setEnabled(false);
170 connect( downloadAct, SIGNAL(triggered()), this, SLOT(download()) );
171
172 copyLinkAct = new QAction(this);
173 copyLinkAct->setEnabled(false);
174 connect( copyLinkAct, SIGNAL(triggered()), this, SLOT(copyLink()) );
175
176 context_menu = new QMenu(this);
177 context_menu->addAction(downloadAct);
178 context_menu->addAction(copyLinkAct);
179
180 retranslateStrings();
181
182 language_filter->setCurrentIndex(0);
183
[124]184 // Opensubtitles server
[142]185 /* os_server = "http://www.opensubtitles.org"; */
186 os_server = "http://api.opensubtitles.org/xml-rpc";
187 osclient->setServer(os_server);
[124]188
[176]189#ifdef FS_USE_PROXY
[114]190 // Proxy
191 use_proxy = false;
192 proxy_type = QNetworkProxy::HttpProxy;
193 proxy_host = "";
194 proxy_port = 0;
195 proxy_username = "";
196 proxy_password = "";
197
198 setupProxy();
[176]199#endif
[114]200}
201
202FindSubtitlesWindow::~FindSubtitlesWindow() {
203 if (set) saveSettings();
204}
205
206void FindSubtitlesWindow::setSettings(QSettings * settings) {
207 set = settings;
208 loadSettings();
[176]209#ifdef FS_USE_PROXY
[114]210 setupProxy();
[176]211#endif
[114]212}
213
[176]214#ifdef FS_USE_PROXY
[114]215void FindSubtitlesWindow::setProxy(QNetworkProxy proxy) {
[142]216 /*
[114]217 downloader->abort();
218 downloader->setProxy(proxy);
[142]219 */
220 osclient->setProxy(proxy);
[114]221
222#ifdef DOWNLOAD_SUBS
223 file_downloader->setProxy(proxy);
224#endif
225
226 qDebug("FindSubtitlesWindow::setProxy: host: '%s' port: %d type: %d",
227 proxy.hostName().toUtf8().constData(), proxy.port(), proxy.type());
228}
[176]229#endif
[114]230
231void FindSubtitlesWindow::retranslateStrings() {
232 retranslateUi(this);
233
234 QStringList labels;
235 labels << tr("Language") << tr("Name") << tr("Format")
236 << tr("Files") << tr("Date") << tr("Uploaded by");
237
238 table->setHorizontalHeaderLabels( labels );
239
240 // Language combobox
241 //int language_index = language_filter->currentIndex();
242 QString current_language = language_filter->itemData(language_filter->currentIndex()).toString();
243 language_filter->clear();
244
[132]245 QMap<QString,QString> l1 = Languages::most_used_list();
246 QMapIterator<QString, QString> i1(l1);
247 while (i1.hasNext()) {
248 i1.next();
249 language_filter->addItem( i1.value() + " (" + i1.key() + ")", i1.key() );
[114]250 }
[132]251 language_filter->addItem( tr("Portuguese - Brasil") + " (pb)", "pb");
[114]252 language_filter->model()->sort(0);
[132]253 #if QT_VERSION >= 0x040400
254 language_filter->insertSeparator(language_filter->count());
255 #endif
256
257 QMap<QString,QString> l2 = Languages::list();
258 QMapIterator<QString, QString> i2(l2);
259 while (i2.hasNext()) {
260 i2.next();
261 if (language_filter->findData(i2.key()) == -1) {
262 language_filter->addItem( i2.value() + " (" + i2.key() + ")", i2.key() );
263 }
264 }
265 //language_filter->model()->sort(0);
[114]266 language_filter->insertItem( 0, tr("All"), "*" );
[132]267 #if QT_VERSION >= 0x040400
268 language_filter->insertSeparator(1);
269 #endif
[114]270 //language_filter->setCurrentIndex(language_index);
271 language_filter->setCurrentIndex(language_filter->findData(current_language));
272
273#if QT_VERSION < 0x040300
274 QPushButton * close_button = buttonBox->button(QDialogButtonBox::Close);
275 close_button->setText( tr("Close") );
276#endif
277
278 // Actions
279 downloadAct->setText( tr("&Download") );
280 copyLinkAct->setText( tr("&Copy link to clipboard") );
281
282 // Icons
283#ifndef NO_SMPLAYER_SUPPORT
284 download_button->setIcon( Images::icon("download") );
285 configure_button->setIcon( Images::icon("prefs") );
286 refresh_button->setIcon( Images::icon("refresh") );
287
288 downloadAct->setIcon( Images::icon("download") );
289 copyLinkAct->setIcon( Images::icon("copy") );
290#endif
291}
292
293void FindSubtitlesWindow::setMovie(QString filename) {
294 qDebug("FindSubtitlesWindow::setMovie: '%s'", filename.toLatin1().constData());
295
296 if (filename == last_file) {
297 return;
298 }
299
300 file_chooser->setText(filename);
301 table->setRowCount(0);
302
[128]303 QString hash = FileHash::calculateHash(filename);
[114]304 if (hash.isEmpty()) {
305 qWarning("FindSubtitlesWindow::setMovie: hash invalid. Doing nothing.");
306 } else {
[142]307 qint64 file_size = QFileInfo(filename).size();
308 osclient->search(hash, file_size);
[114]309 last_file = filename;
310 }
311}
312
313void FindSubtitlesWindow::refresh() {
314 last_file = "";
315 setMovie(file_chooser->text());
316}
317
318void FindSubtitlesWindow::updateRefreshButton() {
[142]319 qDebug("FindSubtitlesWindow::updateRefreshButton:");
[114]320 refresh_button->setEnabled(true);
321}
322
323void FindSubtitlesWindow::currentItemChanged(const QModelIndex & current, const QModelIndex & /*previous*/) {
324 qDebug("FindSubtitlesWindow::currentItemChanged: row: %d, col: %d", current.row(), current.column());
325 download_button->setEnabled(current.isValid());
326 downloadAct->setEnabled(current.isValid());
327 copyLinkAct->setEnabled(current.isValid());
328}
329
330void FindSubtitlesWindow::applyFilter(const QString & filter) {
331 proxy_model->setFilterWildcard(filter);
332}
333
334void FindSubtitlesWindow::applyCurrentFilter() {
335 //proxy_model->setFilterWildcard(language_filter->currentText());
336 QString filter = language_filter->itemData( language_filter->currentIndex() ).toString();
337 applyFilter(filter);
338}
339
340void FindSubtitlesWindow::setLanguage(const QString & lang) {
341 int idx = language_filter->findData(lang);
342 if (idx < 0) idx = 0;
343 language_filter->setCurrentIndex(idx);
344}
345
346QString FindSubtitlesWindow::language() {
347 int idx = language_filter->currentIndex();
348 return language_filter->itemData(idx).toString();
349}
350
351void FindSubtitlesWindow::showError(QString error) {
352 status->setText( tr("Download failed") );
353
354 QMessageBox::information(this, tr("Error"),
355 tr("Download failed: %1.")
356 .arg(error));
357}
358
359void FindSubtitlesWindow::connecting(QString host) {
360 status->setText( tr("Connecting to %1...").arg(host) );
361}
362
[142]363void FindSubtitlesWindow::showLoginFailed() {
364 status->setText( tr("Login to opensubtitles.org has failed") );
365}
366
367void FindSubtitlesWindow::showSearchFailed() {
368 status->setText( tr("Search has failed") );
369}
370
[156]371void FindSubtitlesWindow::showErrorOS(int, const QString & error) {
372 status->setText(error);
373}
374
[114]375void FindSubtitlesWindow::updateDataReadProgress(int done, int total) {
376 qDebug("FindSubtitlesWindow::updateDataReadProgress: %d, %d", done, total);
377
378 status->setText( tr("Downloading...") );
379
380 if (!progress->isVisible()) progress->show();
381 progress->setMaximum(total);
382 progress->setValue(done);
383}
384
385void FindSubtitlesWindow::downloadFinished() {
386 status->setText( tr("Done.") );
387 progress->setMaximum(1);
388 progress->setValue(0);
389 progress->hide();
390}
391
[142]392void FindSubtitlesWindow::parseInfo() {
393 bool ok = true;
[114]394
395 table->setRowCount(0);
396
397 QMap <QString,QString> language_list = Languages::list();
398
399 if (ok) {
[142]400 QList<OSSubtitle> l = osclient->subtitleList();
[114]401 for (int n=0; n < l.count(); n++) {
402
403 QString title_name = l[n].movie;
404 if (!l[n].releasename.isEmpty()) {
405 title_name += " - " + l[n].releasename;
406 }
407
408 QStandardItem * i_name = new QStandardItem(title_name);
409 i_name->setData( l[n].link );
410 #if QT_VERSION < 0x040400
411 i_name->setToolTip( l[n].link );
412 #endif
413
414 QStandardItem * i_lang = new QStandardItem(l[n].language);
415 i_lang->setData(l[n].iso639, Qt::UserRole);
416 #if QT_VERSION < 0x040400
417 i_lang->setToolTip(l[n].iso639);
418 #endif
419 if (language_list.contains(l[n].iso639)) {
420 i_lang->setText( language_list[ l[n].iso639 ] );
421 }
422
423 table->setItem(n, COL_LANG, i_lang);
424 table->setItem(n, COL_NAME, i_name);
425 table->setItem(n, COL_FORMAT, new QStandardItem(l[n].format));
426 table->setItem(n, COL_FILES, new QStandardItem(l[n].files));
427 table->setItem(n, COL_DATE, new QStandardItem(l[n].date));
428 table->setItem(n, COL_USER, new QStandardItem(l[n].user));
429
430 }
431 status->setText( tr("%1 files available").arg(l.count()) );
432 applyCurrentFilter();
433
[170]434 qDebug("FindSubtitlesWindow::parseInfo: sort column: %d", view->header()->sortIndicatorSection());
435 qDebug("FindSubtitlesWindow::parseInfo: sort indicator: %d", view->header()->sortIndicatorOrder());
[114]436
437 table->sort( view->header()->sortIndicatorSection(),
438 view->header()->sortIndicatorOrder() );
439 } else {
440 status->setText( tr("Failed to parse the received data.") );
441 }
442
443 view->resizeColumnToContents(COL_NAME);
444}
445
446void FindSubtitlesWindow::itemActivated(const QModelIndex & index ) {
447 qDebug("FindSubtitlesWindow::itemActivated: row: %d, col %d", proxy_model->mapToSource(index).row(), proxy_model->mapToSource(index).column());
448
449 QString download_link = table->item(proxy_model->mapToSource(index).row(), COL_NAME)->data().toString();
450
451 qDebug("FindSubtitlesWindow::itemActivated: download link: '%s'", download_link.toLatin1().constData());
452
453#ifdef DOWNLOAD_SUBS
454 file_downloader->download( QUrl(download_link) );
455 file_downloader->show();
456#else
457 QDesktopServices::openUrl( QUrl(download_link) );
458#endif
459}
460
461void FindSubtitlesWindow::download() {
462 qDebug("FindSubtitlesWindow::download");
463 if (view->currentIndex().isValid()) {
464 itemActivated(view->currentIndex());
465 }
466}
467
468void FindSubtitlesWindow::copyLink() {
469 qDebug("FindSubtitlesWindow::copyLink");
470 if (view->currentIndex().isValid()) {
471 const QModelIndex & index = view->currentIndex();
472 QString download_link = table->item(proxy_model->mapToSource(index).row(), COL_NAME)->data().toString();
473 qDebug("FindSubtitlesWindow::copyLink: link: '%s'", download_link.toLatin1().constData());
474 qApp->clipboard()->setText(download_link);
475 }
476}
477
478void FindSubtitlesWindow::showContextMenu(const QPoint & pos) {
479 qDebug("FindSubtitlesWindow::showContextMenu");
480
481 context_menu->move( view->viewport()->mapToGlobal(pos) );
482 context_menu->show();
483}
484
485// Language change stuff
486void FindSubtitlesWindow::changeEvent(QEvent *e) {
487 if (e->type() == QEvent::LanguageChange) {
488 retranslateStrings();
489 } else {
490 QWidget::changeEvent(e);
491 }
492}
493
494#ifdef DOWNLOAD_SUBS
[156]495
496#ifndef USE_QUAZIP
[114]497void FindSubtitlesWindow::archiveDownloaded(const QByteArray & buffer) {
498 qDebug("FindSubtitlesWindow::archiveDownloaded");
[156]499 QByteArray uncompress_data = gUncompress(buffer);
500 //qDebug("uncompress_data: %s", uncompress_data.constData());
[114]501
[156]502 if (uncompress_data.isEmpty()) {
503 status->setText(tr("Download failed"));
504 return;
505 }
506
507 QString lang = "unknown";
508 QString extension = "unknown";
509 if (view->currentIndex().isValid()) {
510 const QModelIndex & index = view->currentIndex();
511 lang = table->item(proxy_model->mapToSource(index).row(), COL_LANG)->data(Qt::UserRole).toString();
512 extension = table->item(proxy_model->mapToSource(index).row(), COL_FORMAT)->text();
513 }
514
515 QFileInfo fi(file_chooser->text());
516 QString output_name = fi.completeBaseName();
517 if (include_lang_on_filename) output_name += "_"+ lang;
518 output_name += "." + extension;
519
520 QString output_file = fi.absolutePath() + "/" + output_name;
521 qDebug("FindSubtitlesWindow::archiveDownloaded: save subtitle as '%s'", output_file.toUtf8().constData());
522
523 QFile file(output_file);
524 file.open(QIODevice::WriteOnly);
525 bool error = (file.write(uncompress_data) == -1);
526 file.close();
527
528 if (error) {
529 qWarning("FindSubtitlesWindow::archiveDownloaded: can't write subtitle file");
530 QMessageBox::warning(this, tr("Error saving file"),
531 tr("It wasn't possible to save the downloaded\n"
532 "file in folder %1\n"
533 "Please check the permissions of that folder.").arg(fi.absolutePath()));
534 } else {
[165]535 status->setText(tr("Subtitle saved as %1").arg(output_file));
[156]536 emit subtitleDownloaded( output_file );
537 }
538}
539
540QByteArray FindSubtitlesWindow::gUncompress(const QByteArray &data)
541{
542 if (data.size() <= 4) {
543 qWarning("gUncompress: Input data is truncated");
544 return QByteArray();
545 }
546
547 QByteArray result;
548
549 int ret;
550 z_stream strm;
551 static const int CHUNK_SIZE = 1024;
552 char out[CHUNK_SIZE];
553
554 /* allocate inflate state */
555 strm.zalloc = Z_NULL;
556 strm.zfree = Z_NULL;
557 strm.opaque = Z_NULL;
558 strm.avail_in = data.size();
559 strm.next_in = (Bytef*)(data.data());
560
561 ret = inflateInit2(&strm, 15 + 32); // gzip decoding
562 if (ret != Z_OK)
563 return QByteArray();
564
565 // run inflate()
566 do {
567 strm.avail_out = CHUNK_SIZE;
568 strm.next_out = (Bytef*)(out);
569
570 ret = inflate(&strm, Z_NO_FLUSH);
571 Q_ASSERT(ret != Z_STREAM_ERROR); // state not clobbered
572
573 switch (ret) {
574 case Z_NEED_DICT:
575 ret = Z_DATA_ERROR; // and fall through
576 case Z_DATA_ERROR:
577 case Z_MEM_ERROR:
578 (void)inflateEnd(&strm);
579 return QByteArray();
580 }
581
582 result.append(out, CHUNK_SIZE - strm.avail_out);
583 } while (strm.avail_out == 0);
584
585 // clean up and return
586 inflateEnd(&strm);
587 return result;
588}
589
590#else
591
592void FindSubtitlesWindow::archiveDownloaded(const QByteArray & buffer) {
593 qDebug("FindSubtitlesWindow::archiveDownloaded");
594
[114]595 QString temp_dir = QDir::tempPath();
596 if (!temp_dir.endsWith("/")) temp_dir += "/";
597
598 QTemporaryFile file(temp_dir + "archive_XXXXXX.zip");
599 file.setAutoRemove(false);
600
601 qDebug("FindSubtitlesWindow::archiveDownloaded: a temporary file will be saved in folder '%s'", temp_dir.toUtf8().constData());
602
603 if (file.open()) {
604 QString filename = file.fileName();
605 file.write( buffer );
606 file.close();
607
608 qDebug("FindSubtitlesWindow::archiveDownloaded: file saved as: %s", filename.toUtf8().constData());
609
610 /*
611 QMessageBox::information(this, tr("Downloaded"), tr("File saved as %1").arg(filename));
612 return;
613 */
614
615 status->setText(tr("Temporary file %1").arg(filename));
616
617 QString lang = "unknown";
618 QString extension = "unknown";
619 if (view->currentIndex().isValid()) {
620 const QModelIndex & index = view->currentIndex();
621 lang = table->item(proxy_model->mapToSource(index).row(), COL_LANG)->data(Qt::UserRole).toString();
622 extension = table->item(proxy_model->mapToSource(index).row(), COL_FORMAT)->text();
623 }
624
625 QFileInfo fi(file_chooser->text());
626 QString output_name = fi.completeBaseName();
627 if (include_lang_on_filename) output_name += "_"+ lang;
628 output_name += "." + extension;
629
630 if (!uncompressZip(filename, fi.absolutePath(), output_name)) {
631 status->setText(tr("Download failed"));
632 }
633 file.remove();
634 }
635 else {
636 qWarning("FindSubtitlesWindow::archiveDownloaded: can't write temporary file");
637 QMessageBox::warning(this, tr("Error saving file"),
638 tr("It wasn't possible to save the downloaded\n"
639 "file in folder %1\n"
640 "Please check the permissions of that folder.").arg(temp_dir));
641 }
642}
643
644
645bool FindSubtitlesWindow::uncompressZip(const QString & filename, const QString & output_path, const QString & preferred_output_name) {
646 qDebug("FindSubtitlesWindow::uncompressZip: zip file '%s', output_path '%s', save subtitle as '%s'",
647 filename.toUtf8().constData(), output_path.toUtf8().constData(),
648 preferred_output_name.toUtf8().constData());
649
650 QuaZip zip(filename);
651
652 if (!zip.open(QuaZip::mdUnzip)) {
653 qWarning("FindSubtitlesWindow::uncompressZip: open zip failed: %d", zip.getZipError());
654 return false;
655 }
656
657 zip.setFileNameCodec("IBM866");
658 qDebug("FindSubtitlesWindow::uncompressZip: %d entries", zip.getEntriesCount());
659 qDebug("FindSubtitlesWindow::uncompressZip: global comment: '%s'", zip.getComment().toUtf8().constData());
660
661 QStringList sub_files;
662 QuaZipFileInfo info;
663
664 for (bool more=zip.goToFirstFile(); more; more=zip.goToNextFile()) {
665 if (!zip.getCurrentFileInfo(&info)) {
666 qWarning("FindSubtitlesWindow::uncompressZip: getCurrentFileInfo(): %d\n", zip.getZipError());
667 return false;
668 }
669 qDebug("FindSubtitlesWindow::uncompressZip: file '%s'", info.name.toUtf8().constData());
670 if (QFileInfo(info.name).suffix() != "nfo") sub_files.append(info.name);
671 }
672
673 qDebug("FindSubtitlesWindow::uncompressZip: list of subtitle files:");
674 for (int n=0; n < sub_files.count(); n++) {
675 qDebug("FindSubtitlesWindow::uncompressZip: subtitle file %d '%s'", n, sub_files[n].toUtf8().constData());
676 }
677
678 if (sub_files.count() == 1) {
679 // If only one file, just extract it
680 QString output_name = output_path +"/"+ preferred_output_name;
681 if (extractFile(zip, sub_files[0], output_name )) {
682 status->setText(tr("Subtitle saved as %1").arg(preferred_output_name));
683 emit subtitleDownloaded(output_name);
684 } else {
685 return false;
686 }
687 } else {
688 // More than one file
689 SubChooserDialog d(this);
690
691 for (int n=0; n < sub_files.count(); n++) {
692 d.addFile(sub_files[n]);
693 }
694
695 if (d.exec() == QDialog::Rejected) return false;
696
697 QStringList files_to_extract = d.selectedFiles();
698 int extracted_count = 0;
699 for (int n=0; n < files_to_extract.count(); n++) {
700 QString file = files_to_extract[n];
701 bool ok = extractFile(zip, file, output_path +"/"+ file);
702 qDebug("FindSubtitlesWindow::uncompressZip: extracted %s ok: %d", file.toUtf8().constData(), ok);
703 if (ok) extracted_count++;
704 }
[132]705 status->setText(tr("%n subtitle(s) extracted","", extracted_count));
[114]706 if (extracted_count > 0) {
707 emit subtitleDownloaded( output_path +"/"+ files_to_extract[0] );
708 }
709 }
710
711 zip.close();
712 return true;
713}
714
715bool FindSubtitlesWindow::extractFile(QuaZip & zip, const QString & filename, const QString & output_name) {
716 qDebug("FindSubtitlesWindow::extractFile: '%s', save as '%s'", filename.toUtf8().constData(), output_name.toUtf8().constData());
717
718 if (QFile::exists(output_name)) {
719 if (QMessageBox::question(this, tr("Overwrite?"),
720 tr("The file %1 already exits, overwrite?").arg(output_name), QMessageBox::Yes, QMessageBox::No) == QMessageBox::No)
721 {
722 return false;
723 }
724 }
725
726 if (!zip.setCurrentFile(filename)) {
727 qDebug("FindSubtitlesWindow::extractFile: can't select file %s", filename.toUtf8().constData());
728 return false;
729 }
730
731 // Saving
732 char c;
733 QuaZipFile file(&zip);
734 QFile out(output_name);
735
736 if (!file.open(QIODevice::ReadOnly)) {
737 qWarning("FindSubtitlesWindow::extractFile: can't open file for reading: %d", file.getZipError());
738 return false;
739 }
740
741 if (out.open(QIODevice::WriteOnly)) {
742 // Slow like hell (on GNU/Linux at least), but it is not my fault.
743 // Not ZIP/UNZIP package's fault either.
744 // The slowest thing here is out.putChar(c).
745 while(file.getChar(&c)) out.putChar(c);
746 out.close();
747
748 file.close();
749 } else {
750 qWarning("FindSubtitlesWindow::extractFile: can't open %s for writing", output_name.toUtf8().constData());
751 return false;
752 }
753
754 return true;
755}
[156]756#endif // USE_QUAZIP
[114]757
[132]758void FindSubtitlesWindow::fixSubtitles(const QString & filename) {
759 qDebug("FindSubtitlesWindow::fixSubtitles: %s", filename.toUtf8().constData());
760
761 QFileInfo fi(filename);
762 if (fi.suffix().toLower() == "sub") {
763 qDebug("FindSubtitlesWindow::fixSubtitles: fixing end of lines");
764 if (FixSubtitles::fix(filename) != FixSubtitles::NoError) {
765 status->setText( tr("Error fixing the subtitle lines") );
766 qDebug("FindSubtitlesWindow::fixSubtitles: error fixing the subtitles");
767 }
768 }
769}
770
[156]771#endif // DOWNLOAD_SUBS
[114]772
773void FindSubtitlesWindow::on_configure_button_clicked() {
774 qDebug("FindSubtitlesWindow::on_configure_button_clicked");
775
776 FindSubtitlesConfigDialog d(this);
777
[124]778 d.setServer( os_server );
[176]779 #ifdef OS_SEARCH_WORKAROUND
780 d.setRetries(osclient->retries());
781 #endif
782 #ifdef FS_USE_PROXY
[114]783 d.setUseProxy( use_proxy );
784 d.setProxyHostname( proxy_host );
785 d.setProxyPort( proxy_port );
786 d.setProxyUsername( proxy_username );
787 d.setProxyPassword( proxy_password );
788 d.setProxyType( proxy_type );
[176]789 #endif
[114]790
[176]791 #ifdef DOWNLOAD_SUBS
792 d.setAppendLang(include_lang_on_filename);
793 #endif
794
[114]795 if (d.exec() == QDialog::Accepted) {
[124]796 os_server = d.server();
[176]797 #ifdef OS_SEARCH_WORKAROUND
798 osclient->setRetries( d.retries() );
799 #endif
800 #ifdef FS_USE_PROXY
[114]801 use_proxy = d.useProxy();
802 proxy_host = d.proxyHostname();
803 proxy_port = d.proxyPort();
804 proxy_username = d.proxyUsername();
805 proxy_password = d.proxyPassword();
806 proxy_type = d.proxyType();
[176]807 #endif
[114]808
[142]809 osclient->setServer(os_server);
[176]810 #ifdef FS_USE_PROXY
[114]811 setupProxy();
[176]812 #endif
813
814 #ifdef DOWNLOAD_SUBS
815 include_lang_on_filename = d.appendLang();
816 #endif
[114]817 }
818}
819
[176]820#ifdef FS_USE_PROXY
[114]821void FindSubtitlesWindow::setupProxy() {
822 QNetworkProxy proxy;
823
824 if ( (use_proxy) && (!proxy_host.isEmpty()) ) {
825 proxy.setType((QNetworkProxy::ProxyType) proxy_type);
826 proxy.setHostName(proxy_host);
827 proxy.setPort(proxy_port);
828 if ( (!proxy_username.isEmpty()) && (!proxy_password.isEmpty()) ) {
829 proxy.setUser(proxy_username);
830 proxy.setPassword(proxy_password);
831 }
832 qDebug("FindSubtitlesWindow::userProxy: using proxy: host: %s, port: %d, type: %d",
833 proxy_host.toUtf8().constData(), proxy_port, proxy_type);
834 } else {
835 // No proxy
836 proxy.setType(QNetworkProxy::NoProxy);
837 qDebug("FindSubtitlesDialog::userProxy: no proxy");
838 }
839
840 setProxy(proxy);
841}
[176]842#endif
[114]843
844void FindSubtitlesWindow::saveSettings() {
845 qDebug("FindSubtitlesWindow::saveSettings");
846
847 set->beginGroup("findsubtitles");
848
[142]849 set->setValue("xmlrpc_server", os_server);
[176]850#ifdef OS_SEARCH_WORKAROUND
851 set->setValue("retries", osclient->retries());
852#endif
853
[114]854 set->setValue("language", language());
855#ifdef DOWNLOAD_SUBS
856 set->setValue("include_lang_on_filename", includeLangOnFilename());
857#endif
[176]858
859#ifdef FS_USE_PROXY
[114]860 set->setValue("proxy/use_proxy", use_proxy);
861 set->setValue("proxy/type", proxy_type);
862 set->setValue("proxy/host", proxy_host);
863 set->setValue("proxy/port", proxy_port);
864 set->setValue("proxy/username", proxy_username);
865 set->setValue("proxy/password", proxy_password);
[176]866#endif
[114]867
868 set->endGroup();
869}
870
871void FindSubtitlesWindow::loadSettings() {
872 qDebug("FindSubtitlesWindow::loadSettings");
873
874 set->beginGroup("findsubtitles");
875
[142]876 os_server = set->value("xmlrpc_server", os_server).toString();
[176]877#ifdef OS_SEARCH_WORKAROUND
878 osclient->setRetries( set->value("retries", osclient->retries()).toInt() );
879#endif
880
[114]881 setLanguage( set->value("language", language()).toString() );
882#ifdef DOWNLOAD_SUBS
883 setIncludeLangOnFilename( set->value("include_lang_on_filename", includeLangOnFilename()).toBool() );
884#endif
[176]885
886#ifdef FS_USE_PROXY
[114]887 use_proxy = set->value("proxy/use_proxy", use_proxy).toBool();
888 proxy_type = set->value("proxy/type", proxy_type).toInt();
889 proxy_host = set->value("proxy/host", proxy_host).toString();
890 proxy_port = set->value("proxy/port", proxy_port).toInt();
891 proxy_username = set->value("proxy/username", proxy_username).toString();
892 proxy_password = set->value("proxy/password", proxy_password).toString();
[176]893#endif
[114]894
895 set->endGroup();
896}
897
898#include "moc_findsubtitleswindow.cpp"
899
Note: See TracBrowser for help on using the repository browser.