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