From 64f4775a81370659fc20555dc51bcceac85676c9 Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Thu, 30 Dec 2010 19:38:53 +0000 Subject: [PATCH] Code clean up --- src/misc.cpp | 11 +++++------ src/misc.h | 2 +- src/properties/propertieswidget.cpp | 4 +--- src/qtlibtorrent/qtorrenthandle.cpp | 2 +- src/searchengine/engineselectdlg.cpp | 4 ++-- src/torrentadditiondlg.cpp | 10 +++------- src/torrentcreator/torrentcreatorthread.cpp | 2 +- src/torrentfilesmodel.h | 4 ++-- src/torrentimportdlg.cpp | 4 ++-- src/webui/eventmanager.cpp | 2 +- 10 files changed, 19 insertions(+), 26 deletions(-) diff --git a/src/misc.cpp b/src/misc.cpp index bb1c621c8..0d92178b2 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -281,8 +281,7 @@ QString misc::truncateRootFolder(boost::intrusive_ptr t) { #else QString path = QString::fromUtf8(t->file_at(0).path.string().c_str()); #endif - QStringList path_parts = path.split("/", QString::SkipEmptyParts); - t->rename_file(0, path_parts.last().toUtf8().data()); + t->rename_file(0, fileName(path).toUtf8().data()); return QString::null; } QString root_folder; @@ -316,8 +315,7 @@ QString misc::truncateRootFolder(libtorrent::torrent_handle h) { #else QString path = QString::fromUtf8(t.file_at(0).path.string().c_str()); #endif - QStringList path_parts = path.split("/", QString::SkipEmptyParts); - t.rename_file(0, path_parts.last().toUtf8().data()); + t.rename_file(0, fileName(path).toUtf8().data()); return QString::null; } QString root_folder; @@ -747,9 +745,10 @@ bool misc::isValidTorrentFile(const QString &torrent_path) { return true; } -QString misc::branchPath(QString file_path) +QString misc::branchPath(QString file_path, bool uses_slashes) { - file_path.replace("\\", "/"); + if(!uses_slashes) + file_path.replace("\\", "/"); return file_path.left(file_path.lastIndexOf('/')); } diff --git a/src/misc.h b/src/misc.h index bfdf5cb0c..4052a7b35 100644 --- a/src/misc.h +++ b/src/misc.h @@ -180,7 +180,7 @@ public: // value must be given in bytes static QString friendlyUnit(double val); static bool isPreviewable(QString extension); - static QString branchPath(QString file_path); + static QString branchPath(QString file_path, bool uses_slashes=false); static QString fileName(QString file_path); static QString magnetUriToName(QString magnet_uri); static QString magnetUriToHash(QString magnet_uri); diff --git a/src/properties/propertieswidget.cpp b/src/properties/propertieswidget.cpp index 7c5b1cb5e..c0cca1283 100644 --- a/src/properties/propertieswidget.cpp +++ b/src/properties/propertieswidget.cpp @@ -711,9 +711,7 @@ void PropertiesWidget::on_changeSavePathButton_clicked() { QString save_path_dir = new_path.replace("\\", "/"); QString new_file_name; if(h.has_metadata() && h.num_files() == 1) { - QStringList parts = save_path_dir.split("/"); - new_file_name = parts.takeLast(); // Skip file name - save_path_dir = parts.join("/"); + save_path_dir = misc::branchPath(save_path_dir, true); // Skip file name } QDir savePath(misc::expandPath(save_path_dir)); // Actually move storage diff --git a/src/qtlibtorrent/qtorrenthandle.cpp b/src/qtlibtorrent/qtorrenthandle.cpp index d340f5c88..9d92b4158 100644 --- a/src/qtlibtorrent/qtorrenthandle.cpp +++ b/src/qtlibtorrent/qtorrenthandle.cpp @@ -221,7 +221,7 @@ int QTorrentHandle::num_files() const { QString QTorrentHandle::filename_at(unsigned int index) const { Q_ASSERT(index < (unsigned int)torrent_handle::get_torrent_info().num_files()); #if LIBTORRENT_VERSION_MINOR > 15 - return filepath_at(index).replace("\\", "/").split("/").last(); + return misc::fileName(filepath_at(index)); #else return misc::toQStringU(torrent_handle::get_torrent_info().file_at(index).path.leaf()); #endif diff --git a/src/searchengine/engineselectdlg.cpp b/src/searchengine/engineselectdlg.cpp index 34df31778..5b6693db0 100644 --- a/src/searchengine/engineselectdlg.cpp +++ b/src/searchengine/engineselectdlg.cpp @@ -430,8 +430,8 @@ void engineSelectDlg::processDownloadedFile(QString url, QString filePath) { return; } if(url.endsWith(".py", Qt::CaseInsensitive)) { - QString plugin_name = url.split('/').last(); - plugin_name.replace(".py", ""); + QString plugin_name = misc::fileName(url); + plugin_name.chop(3); // Remove extension installPlugin(filePath, plugin_name); misc::safeRemove(filePath); return; diff --git a/src/torrentadditiondlg.cpp b/src/torrentadditiondlg.cpp index cc2f97013..3cb3db34e 100644 --- a/src/torrentadditiondlg.cpp +++ b/src/torrentadditiondlg.cpp @@ -141,13 +141,9 @@ void torrentAdditionDialog::saveSettings() { } void torrentAdditionDialog::renameTorrentNameInModel(QString file_path) { - file_path = file_path.trimmed(); - if(file_path.isEmpty()) return; - file_path = file_path.replace("\\", "/"); - // Rename in torrent files model too - QStringList parts = file_path.split("/", QString::SkipEmptyParts); - if(!parts.empty()) - PropListModel->setData(PropListModel->index(0, 0), parts.last()); + const QString new_name = misc::fileName(file_path); + if(!new_name.isEmpty()) + PropListModel->setData(PropListModel->index(0, 0), new_name); } void torrentAdditionDialog::limitDialogWidth() { diff --git a/src/torrentcreator/torrentcreatorthread.cpp b/src/torrentcreator/torrentcreatorthread.cpp index 96e5e54cb..ea75181bb 100644 --- a/src/torrentcreator/torrentcreatorthread.cpp +++ b/src/torrentcreator/torrentcreatorthread.cpp @@ -109,7 +109,7 @@ void TorrentCreatorThread::run() { // calculate the hash for all pieces #if LIBTORRENT_VERSION_MINOR >= 16 QString parent_path = input_path.replace("\\", "/"); - QStringList parts = parent_path.split("/"); + QStringList parts = parent_path.split("/"/*, QString::SkipEmptyParts*/); parts.removeLast(); parent_path = parts.join("/"); set_piece_hashes(t, parent_path.toUtf8().constData(), boost::bind(&sendProgressUpdateSignal, _1, t.num_pieces(), this)); diff --git a/src/torrentfilesmodel.h b/src/torrentfilesmodel.h index a015682ee..3f592a645 100644 --- a/src/torrentfilesmodel.h +++ b/src/torrentfilesmodel.h @@ -65,10 +65,10 @@ public: type = TFILE; file_index = _file_index; #if LIBTORRENT_VERSION_MINOR >= 16 - QString name = misc::toQStringU(t.files().file_path(f)).replace("\\", "/").split("/").last(); + QString name = misc::fileName(misc::toQStringU(t.files().file_path(f))); #else Q_UNUSED(t); - QString name = misc::toQStringU(f.path.string()).split("/").last(); + QString name = misc::toQStringU(f.path.filename()); #endif // Do not display incomplete extensions if(name.endsWith(".!qB")) diff --git a/src/torrentimportdlg.cpp b/src/torrentimportdlg.cpp index d75f67bad..29e256d8b 100644 --- a/src/torrentimportdlg.cpp +++ b/src/torrentimportdlg.cpp @@ -85,9 +85,9 @@ void TorrentImportDlg::on_browseContentBtn_clicked() if(t->num_files() == 1) { // Single file torrent #if LIBTORRENT_VERSION_MINOR >= 16 - const QString file_name = misc::toQStringU(fs.file_path(t->file_at(0))).replace("\\", "/").split("/").last(); + const QString file_name = misc::fileName(misc::toQStringU(fs.file_path(t->file_at(0))); #else - const QString file_name = misc::toQStringU(t->file_at(0).path.leaf()); + const QString file_name = misc::toQStringU(t->file_at(0).path.filename()); #endif qDebug("Torrent has only one file: %s", qPrintable(file_name)); QString extension = misc::file_extension(file_name); diff --git a/src/webui/eventmanager.cpp b/src/webui/eventmanager.cpp index 3e8562aab..e3676cc9f 100644 --- a/src/webui/eventmanager.cpp +++ b/src/webui/eventmanager.cpp @@ -109,7 +109,7 @@ QList EventManager::getPropFilesInfo(QString hash) const { for(fi=t.begin_files(); fi != t.end_files(); fi++) { QVariantMap file; QString path = h.filepath(*fi); - QString name = path.split('/').last(); + QString name = misc::fileName(path); file["name"] = name; file["size"] = misc::friendlyUnit((double)fi->size); if(fi->size > 0)