From 5696944c6fcf92c6381407d8b0e2f574a84487cd Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Thu, 30 Dec 2010 19:12:03 +0000 Subject: [PATCH] Code clean up --- src/mainwindow.cpp | 20 ++++++------- src/misc.cpp | 16 +++++++++++ src/misc.h | 2 ++ src/properties/propertieswidget.cpp | 12 ++------ src/qtlibtorrent/qbtsession.cpp | 42 +++++++++++++--------------- src/searchengine/engineselectdlg.cpp | 16 ++++------- src/transferlistwidget.cpp | 18 ++---------- 7 files changed, 57 insertions(+), 69 deletions(-) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 3a7fda3e7..f4345455e 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -779,9 +779,12 @@ void MainWindow::dropEvent(QDropEvent *event) { if(event->mimeData()->hasUrls()) { const QList urls = event->mimeData()->urls(); foreach(const QUrl &url, urls) { - const QString tmp = url.toString().trimmed(); - if(!tmp.isEmpty()) - files << url.toString(); + if(!url.isEmpty()) { + if(url.scheme().compare("file", Qt::CaseInsensitive) == 0) + files << url.toLocalFile(); + else + files << url.toString(); + } } } else { files = event->mimeData()->text().split(QString::fromUtf8("\n")); @@ -790,13 +793,8 @@ void MainWindow::dropEvent(QDropEvent *event) { QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent")); const bool useTorrentAdditionDialog = settings.value(QString::fromUtf8("Preferences/Downloads/AdditionDialog"), true).toBool(); foreach(QString file, files) { -#ifdef Q_WS_WIN - file = file.trimmed().replace(QString::fromUtf8("file:///"), QString::fromUtf8(""), Qt::CaseInsensitive); -#else - file = file.trimmed().replace(QString::fromUtf8("file://"), QString::fromUtf8(""), Qt::CaseInsensitive); -#endif - qDebug("Dropped file %s on download list", file.toLocal8Bit().data()); - if(file.startsWith(QString::fromUtf8("http://"), Qt::CaseInsensitive) || file.startsWith(QString::fromUtf8("ftp://"), Qt::CaseInsensitive) || file.startsWith(QString::fromUtf8("https://"), Qt::CaseInsensitive)) { + qDebug("Dropped file %s on download list", qPrintable(file)); + if(misc::isUrl(file)) { QBtSession::instance()->downloadFromUrl(file); continue; } @@ -874,7 +872,7 @@ void MainWindow::processParams(const QStringList& params) { const bool useTorrentAdditionDialog = settings.value(QString::fromUtf8("Preferences/Downloads/AdditionDialog"), true).toBool(); foreach(QString param, params) { param = param.trimmed(); - if(param.startsWith(QString::fromUtf8("http://"), Qt::CaseInsensitive) || param.startsWith(QString::fromUtf8("ftp://"), Qt::CaseInsensitive) || param.startsWith(QString::fromUtf8("https://"), Qt::CaseInsensitive)) { + if(misc::isUrl(param)) { QBtSession::instance()->downloadFromUrl(param); }else{ if(param.startsWith("bc://bt/", Qt::CaseInsensitive)) { diff --git a/src/misc.cpp b/src/misc.cpp index bf997d501..bb1c621c8 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -752,3 +752,19 @@ QString misc::branchPath(QString file_path) file_path.replace("\\", "/"); return file_path.left(file_path.lastIndexOf('/')); } + +bool misc::isUrl(const QString &s) +{ + const QString scheme = QUrl(s).scheme(); + QRegExp is_url("http[s]?|ftp", Qt::CaseInsensitive); + return is_url.exactMatch(scheme); +} + +QString misc::fileName(QString file_path) +{ + file_path.replace("\\", "/"); + const int slash_index = file_path.lastIndexOf('/'); + if(slash_index == -1) + return file_path; + return file_path.mid(slash_index); +} diff --git a/src/misc.h b/src/misc.h index 25c60695c..bfdf5cb0c 100644 --- a/src/misc.h +++ b/src/misc.h @@ -156,6 +156,7 @@ public: static QString updateLabelInSavePath(const QString& defaultSavePath, const QString &save_path, const QString &old_label, const QString &new_label); static bool sameFiles(const QString &path1, const QString &path2); + static bool isUrl(const QString &s); static void copyDir(QString src_path, QString dst_path); static QString toValidFileSystemName(QString filename); static bool isValidFileSystemName(QString filename); @@ -180,6 +181,7 @@ public: static QString friendlyUnit(double val); static bool isPreviewable(QString extension); static QString branchPath(QString file_path); + static QString fileName(QString file_path); static QString magnetUriToName(QString magnet_uri); static QString magnetUriToHash(QString magnet_uri); static QString bcLinkToMagnet(QString bc_link); diff --git a/src/properties/propertieswidget.cpp b/src/properties/propertieswidget.cpp index 748c1526f..7c5b1cb5e 100644 --- a/src/properties/propertieswidget.cpp +++ b/src/properties/propertieswidget.cpp @@ -451,11 +451,7 @@ void PropertiesWidget::openDoubleClickedFile(QModelIndex index) { h.flush_cache(); #endif if(QFile::exists(file_path)) { -#ifdef Q_WS_WIN - QDesktopServices::openUrl(QUrl("file:///"+file_path)); -#else - QDesktopServices::openUrl(QUrl("file://"+file_path)); -#endif + QDesktopServices::openUrl(QUrl::fromLocalFile(file_path)); } else { QMessageBox::warning(this, tr("I/O Error"), tr("This file does not exist yet.")); } @@ -477,11 +473,7 @@ void PropertiesWidget::openDoubleClickedFile(QModelIndex index) { h.flush_cache(); #endif if(QFile::exists(file_path)) { -#ifdef Q_WS_WIN - QDesktopServices::openUrl(QUrl("file:///"+file_path)); -#else - QDesktopServices::openUrl(QUrl("file://"+file_path)); -#endif + QDesktopServices::openUrl(QUrl::fromLocalFile(file_path)); } else { QMessageBox::warning(this, tr("I/O Error"), tr("This folder does not exist yet.")); } diff --git a/src/qtlibtorrent/qbtsession.cpp b/src/qtlibtorrent/qbtsession.cpp index 130550d54..2768a036c 100644 --- a/src/qtlibtorrent/qbtsession.cpp +++ b/src/qtlibtorrent/qbtsession.cpp @@ -856,36 +856,32 @@ QTorrentHandle QBtSession::addTorrent(QString path, bool fromScanDir, QString fr if(!path.endsWith(".torrent")) if(QFile::rename(path, path+".torrent")) path += ".torrent"; #endif -#ifdef Q_WS_WIN - const QString file = path.trimmed().replace("file:///", "", Qt::CaseInsensitive); -#else - const QString file = path.trimmed().replace("file://", "", Qt::CaseInsensitive); -#endif - if(file.isEmpty()) return h; + if(path.startsWith("file:", Qt::CaseInsensitive)) + path = QUrl(path).toLocalFile(); + if(path.isEmpty()) return h; - Q_ASSERT(!file.startsWith("http://", Qt::CaseInsensitive) && !file.startsWith("https://", Qt::CaseInsensitive) - && !file.startsWith("ftp://", Qt::CaseInsensitive)); + Q_ASSERT(!misc::isUrl(path)); - qDebug("Adding %s to download list", qPrintable(file)); + qDebug("Adding %s to download list", qPrintable(path)); boost::intrusive_ptr t; try { // Getting torrent file informations - t = new torrent_info(file.toUtf8().constData()); + t = new torrent_info(path.toUtf8().constData()); if(!t->is_valid()) throw std::exception(); } catch(std::exception&) { if(!from_url.isNull()) { addConsoleMessage(tr("Unable to decode torrent file: '%1'", "e.g: Unable to decode torrent file: '/home/y/xxx.torrent'").arg(from_url), QString::fromUtf8("red")); //emit invalidTorrent(from_url); - misc::safeRemove(file); + misc::safeRemove(path); }else{ - addConsoleMessage(tr("Unable to decode torrent file: '%1'", "e.g: Unable to decode torrent file: '/home/y/xxx.torrent'").arg(file), QString::fromUtf8("red")); - //emit invalidTorrent(file); + addConsoleMessage(tr("Unable to decode torrent file: '%1'", "e.g: Unable to decode torrent file: '/home/y/xxx.torrent'").arg(path), QString::fromUtf8("red")); + //emit invalidTorrent(path); } addConsoleMessage(tr("This file is either corrupted or this isn't a torrent."),QString::fromUtf8("red")); if(fromScanDir) { // Remove file - misc::safeRemove(file); + misc::safeRemove(path); } return h; } @@ -902,14 +898,14 @@ QTorrentHandle QBtSession::addTorrent(QString path, bool fromScanDir, QString fr if(!from_url.isNull()) { addConsoleMessage(tr("'%1' is already in download list.", "e.g: 'xxx.avi' is already in download list.").arg(from_url)); }else{ - addConsoleMessage(tr("'%1' is already in download list.", "e.g: 'xxx.avi' is already in download list.").arg(file)); + addConsoleMessage(tr("'%1' is already in download list.", "e.g: 'xxx.avi' is already in download list.").arg(path)); } // Check if the torrent contains trackers or url seeds we don't know about // and add them mergeTorrents(getTorrentHandle(hash), t); // Delete file if temporary - if(!from_url.isNull() || fromScanDir) misc::safeRemove(file); + if(!from_url.isNull() || fromScanDir) misc::safeRemove(path); return h; } @@ -917,7 +913,7 @@ QTorrentHandle QBtSession::addTorrent(QString path, bool fromScanDir, QString fr if(t->num_files() < 1) { addConsoleMessage(tr("Error: The torrent %1 does not contain any file.").arg(misc::toQStringU(t->name()))); // Delete file if temporary - if(!from_url.isNull() || fromScanDir) misc::safeRemove(file); + if(!from_url.isNull() || fromScanDir) misc::safeRemove(path); return h; } @@ -978,7 +974,7 @@ QTorrentHandle QBtSession::addTorrent(QString path, bool fromScanDir, QString fr // Check if it worked if(!h.is_valid()) { qDebug("/!\\ Error: Invalid handle"); - if(!from_url.isNull()) misc::safeRemove(file); + if(!from_url.isNull()) misc::safeRemove(path); return h; } // Remember root folder @@ -1010,8 +1006,8 @@ QTorrentHandle QBtSession::addTorrent(QString path, bool fromScanDir, QString fr #endif // Backup torrent file const QString newFile = torrentBackup.absoluteFilePath(hash + ".torrent"); - if(file != newFile) - QFile::copy(file, newFile); + if(path != newFile) + QFile::copy(path, newFile); // Copy the torrent file to the export folder if(torrentExport) exportTorrentFile(h); @@ -1023,7 +1019,7 @@ QTorrentHandle QBtSession::addTorrent(QString path, bool fromScanDir, QString fr } // If temporary file, remove it - if(!from_url.isNull() || fromScanDir) misc::safeRemove(file); + if(!from_url.isNull() || fromScanDir) misc::safeRemove(path); // Display console message if(!from_url.isNull()) { @@ -1033,9 +1029,9 @@ QTorrentHandle QBtSession::addTorrent(QString path, bool fromScanDir, QString fr addConsoleMessage(tr("'%1' added to download list.", "'/home/y/xxx.torrent' was added to download list.").arg(from_url)); }else{ if(fastResume) - addConsoleMessage(tr("'%1' resumed. (fast resume)", "'/home/y/xxx.torrent' was resumed. (fast resume)").arg(file)); + addConsoleMessage(tr("'%1' resumed. (fast resume)", "'/home/y/xxx.torrent' was resumed. (fast resume)").arg(path)); else - addConsoleMessage(tr("'%1' added to download list.", "'/home/y/xxx.torrent' was added to download list.").arg(file)); + addConsoleMessage(tr("'%1' added to download list.", "'/home/y/xxx.torrent' was added to download list.").arg(path)); } // Send torrent addition signal diff --git a/src/searchengine/engineselectdlg.cpp b/src/searchengine/engineselectdlg.cpp index 65a8b081d..34df31778 100644 --- a/src/searchengine/engineselectdlg.cpp +++ b/src/searchengine/engineselectdlg.cpp @@ -75,22 +75,18 @@ engineSelectDlg::~engineSelectDlg() { void engineSelectDlg::dropEvent(QDropEvent *event) { event->acceptProposedAction(); QStringList files=event->mimeData()->text().split(QString::fromUtf8("\n")); - QString file; - foreach(file, files) { + foreach(QString file, files) { qDebug("dropped %s", qPrintable(file)); -#ifdef Q_WS_WIN - file = file.replace("file:///", ""); -#else - file = file.replace("file://", ""); -#endif - if(file.startsWith("http://", Qt::CaseInsensitive) || file.startsWith("https://", Qt::CaseInsensitive) || file.startsWith("ftp://", Qt::CaseInsensitive)) { + if(misc::isUrl(file)) { setCursor(QCursor(Qt::WaitCursor)); downloader->downloadUrl(file); continue; } if(file.endsWith(".py", Qt::CaseInsensitive)) { - QString plugin_name = file.split(QDir::separator()).last(); - plugin_name.replace(".py", ""); + if(file.startsWith("file:", Qt::CaseInsensitive)) + file = QUrl(file).toLocalFile(); + QString plugin_name = misc::fileName(file); + plugin_name.chop(3); // Remove extension installPlugin(file, plugin_name); } } diff --git a/src/transferlistwidget.cpp b/src/transferlistwidget.cpp index 8301b7706..0f9187b69 100644 --- a/src/transferlistwidget.cpp +++ b/src/transferlistwidget.cpp @@ -139,11 +139,7 @@ TorrentModel* TransferListWidget::getSourceModel() const { } void TransferListWidget::previewFile(QString filePath) { -#ifdef Q_WS_WIN - QDesktopServices::openUrl(QUrl(QString("file:///")+filePath)); -#else - QDesktopServices::openUrl(QUrl(QString("file://")+filePath)); -#endif + QDesktopServices::openUrl(QUrl::fromLocalFile(filePath)); } void TransferListWidget::setRefreshInterval(int t) { @@ -201,11 +197,7 @@ void TransferListWidget::torrentDoubleClicked(const QModelIndex& index) { } break; case OPEN_DEST: -#ifdef Q_WS_WIN - QDesktopServices::openUrl(QUrl("file:///" + h.save_path())); -#else - QDesktopServices::openUrl(QUrl("file://" + h.save_path())); -#endif + QDesktopServices::openUrl(QUrl::fromLocalFile(h.save_path())); break; } } @@ -413,11 +405,7 @@ void TransferListWidget::openSelectedTorrentsFolder() const { qDebug("Opening path at %s", qPrintable(savePath)); if(!pathsList.contains(savePath)) { pathsList.append(savePath); -#ifdef Q_WS_WIN - QDesktopServices::openUrl(QUrl(QString("file:///")+savePath)); -#else - QDesktopServices::openUrl(QUrl(QString("file://")+savePath)); -#endif + QDesktopServices::openUrl(QUrl::fromLocalFile(savePath)); } } }