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