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