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