diff --git a/src/core/misc.cpp b/src/core/misc.cpp index 5e551ff7b..9482bb912 100644 --- a/src/core/misc.cpp +++ b/src/core/misc.cpp @@ -31,6 +31,7 @@ #include "core/unicodestrings.h" #include "core/logger.h" #include "misc.h" +#include "fs_utils.h" #include @@ -52,6 +53,11 @@ #include #endif +#ifndef DISABLE_GUI +#include +#include +#endif + #ifdef Q_OS_WIN #include #include @@ -692,6 +698,64 @@ void misc::loadBencodedFile(const QString &filename, std::vector &buffer, lazy_bdecode(&buffer[0], &buffer[0] + buffer.size(), entry, ec); } +#ifndef DISABLE_GUI +// Open the given path with an appropriate application +void misc::openPath(const QString& absolutePath) +{ + const QString path = fsutils::fromNativePath(absolutePath); + // Hack to access samba shares with QDesktopServices::openUrl + if (path.startsWith("//")) + QDesktopServices::openUrl(fsutils::toNativePath("file:" + path)); + else + QDesktopServices::openUrl(QUrl::fromLocalFile(path)); +} + +// Open the parent directory of the given path with a file manager and select +// (if possible) the item at the given path +void misc::openFolderSelect(const QString& absolutePath) +{ + const QString path = fsutils::fromNativePath(absolutePath); +#ifdef Q_OS_WIN + if (QFileInfo(path).exists()) { + // Syntax is: explorer /select, "C:\Folder1\Folder2\file_to_select" + // Dir separators MUST be win-style slashes + QProcess::startDetached("explorer.exe", QStringList() << "/select," << fsutils::toNativePath(absolutePath)); + } + else { + // If the item to select doesn't exist, try to open its parent + openPath(path.left(path.lastIndexOf("/"))); + } +#elif defined(Q_OS_UNIX) && !defined(Q_OS_MAC) + if (QFileInfo(path).exists()) { + QProcess proc; + QString output; + proc.start("xdg-mime", QStringList() << "query" << "default" << "inode/directory"); + proc.waitForFinished(); + output = proc.readLine().simplified(); + if (output == "dolphin.desktop") + proc.startDetached("dolphin", QStringList() << "--select" << fsutils::toNativePath(path)); + else if (output == "nautilus.desktop" || output == "org.gnome.Nautilus.desktop" + || output == "nautilus-folder-handler.desktop") + proc.startDetached("nautilus", QStringList() << "--no-desktop" << fsutils::toNativePath(path)); + else if (output == "caja-folder-handler.desktop") + proc.startDetached("caja", QStringList() << "--no-desktop" << fsutils::toNativePath(path)); + else if (output == "nemo.desktop") + proc.startDetached("nemo", QStringList() << "--no-desktop" << fsutils::toNativePath(path)); + else if (output == "kfmclient_dir.desktop") + proc.startDetached("konqueror", QStringList() << "--select" << fsutils::toNativePath(path)); + else + openPath(path.left(path.lastIndexOf("/"))); + } + else { + // If the item to select doesn't exist, try to open its parent + openPath(path.left(path.lastIndexOf("/"))); + } +#else + openPath(path.left(path.lastIndexOf("/"))); +#endif +} +#endif // DISABLE_GUI + namespace { // Trick to get a portable sleep() function class SleeperThread: public QThread { diff --git a/src/core/misc.h b/src/core/misc.h index e326eea2f..024db9ef1 100644 --- a/src/core/misc.h +++ b/src/core/misc.h @@ -114,6 +114,11 @@ namespace misc bool slowEquals(const QByteArray &a, const QByteArray &b); void loadBencodedFile(const QString &filename, std::vector &buffer, libtorrent::lazy_entry &entry, libtorrent::error_code &ec); +#ifndef DISABLE_GUI + void openPath(const QString& absolutePath); + void openFolderSelect(const QString& absolutePath); +#endif + void msleep(unsigned long msecs); } diff --git a/src/gui/properties/propertieswidget.cpp b/src/gui/properties/propertieswidget.cpp index f5371a4f3..a61a6530c 100644 --- a/src/gui/properties/propertieswidget.cpp +++ b/src/gui/properties/propertieswidget.cpp @@ -39,7 +39,6 @@ #include #include #include -#include #include #include "propertieswidget.h" #include "transferlistwidget.h" @@ -501,15 +500,7 @@ void PropertiesWidget::openFile(const QModelIndex &index) { qDebug("Trying to open file at %s", qPrintable(file_path)); // Flush data h.flush_cache(); - if (QFile::exists(file_path)) { - if (file_path.startsWith("//")) - QDesktopServices::openUrl(fsutils::toNativePath("file:" + file_path)); - else - QDesktopServices::openUrl(QUrl::fromLocalFile(file_path)); - } - else { - QMessageBox::warning(this, tr("I/O Error"), tr("This file does not exist yet.")); - } + misc::openPath(file_path); } void PropertiesWidget::openFolder(const QModelIndex &index, bool containing_folder) { @@ -526,10 +517,6 @@ void PropertiesWidget::openFolder(const QModelIndex &index, bool containing_fold } if (path_items.isEmpty()) return; -#if !(defined(Q_OS_WIN) || (defined(Q_OS_UNIX) && !defined(Q_OS_MAC))) - if (containing_folder) - path_items.removeLast(); -#endif const QDir saveDir(h.save_path()); const QString relative_path = path_items.join("/"); absolute_path = fsutils::expandPath(saveDir.absoluteFilePath(relative_path)); @@ -539,54 +526,14 @@ void PropertiesWidget::openFolder(const QModelIndex &index, bool containing_fold const QDir saveDir(h.save_path()); const QString relative_path = h.filepath_at(i); absolute_path = fsutils::expandPath(saveDir.absoluteFilePath(relative_path)); - -#if !(defined(Q_OS_WIN) || (defined(Q_OS_UNIX) && !defined(Q_OS_MAC))) - if (containing_folder) - absolute_path = fsutils::folderName(absolute_path); -#endif } // Flush data h.flush_cache(); - if (!QFile::exists(absolute_path)) - return; - qDebug("Trying to open folder at %s", qPrintable(absolute_path)); - -#ifdef Q_OS_WIN - if (containing_folder) { - // Syntax is: explorer /select, "C:\Folder1\Folder2\file_to_select" - // Dir separators MUST be win-style slashes - QProcess::startDetached("explorer.exe", QStringList() << "/select," << fsutils::toNativePath(absolute_path)); - } else { -#elif defined(Q_OS_UNIX) && !defined(Q_OS_MAC) - if (containing_folder) { - QProcess proc; - QString output; - proc.start("xdg-mime", QStringList() << "query" << "default" << "inode/directory"); - proc.waitForFinished(); - output = proc.readLine().simplified(); - if (output == "dolphin.desktop") - proc.startDetached("dolphin", QStringList() << "--select" << fsutils::toNativePath(absolute_path)); - else if (output == "nautilus-folder-handler.desktop") - proc.startDetached("nautilus", QStringList() << "--no-desktop" << fsutils::toNativePath(absolute_path)); - else if (output == "kfmclient_dir.desktop") - proc.startDetached("konqueror", QStringList() << "--select" << fsutils::toNativePath(absolute_path)); - else - QDesktopServices::openUrl(QUrl::fromLocalFile(QFileInfo(absolute_path).absolutePath())); - } else { -#endif - if (QFile::exists(absolute_path)) { - // Hack to access samba shares with QDesktopServices::openUrl - if (absolute_path.startsWith("//")) - QDesktopServices::openUrl(fsutils::toNativePath("file:" + absolute_path)); - else - QDesktopServices::openUrl(QUrl::fromLocalFile(absolute_path)); - } else { - QMessageBox::warning(this, tr("I/O Error"), tr("This folder does not exist yet.")); - } -#if defined(Q_OS_WIN) || (defined(Q_OS_UNIX) && !defined(Q_OS_MAC)) - } -#endif + if (containing_folder) + misc::openFolderSelect(absolute_path); + else + misc::openPath(absolute_path); } void PropertiesWidget::displayFilesListMenu(const QPoint&) { diff --git a/src/gui/transferlistwidget.cpp b/src/gui/transferlistwidget.cpp index 877efe774..cf30c20b3 100644 --- a/src/gui/transferlistwidget.cpp +++ b/src/gui/transferlistwidget.cpp @@ -32,7 +32,6 @@ #include #include #include -#include #include #include #include @@ -179,7 +178,7 @@ TorrentModel* TransferListWidget::getSourceModel() const void TransferListWidget::previewFile(QString filePath) { - openUrl(filePath); + misc::openPath(filePath); } void TransferListWidget::setRefreshInterval(int t) @@ -233,8 +232,11 @@ void TransferListWidget::torrentDoubleClicked(const QModelIndex& index) h.pause(); break; case OPEN_DEST: - const QString path = h.root_path(); - openUrl(path); + if (h.num_files() == 1) + misc::openFolderSelect(QDir(h.root_path()).absoluteFilePath(h.filepath_at(0))); + else + misc::openPath(h.root_path()); + break; } } @@ -458,14 +460,18 @@ void TransferListWidget::openSelectedTorrentsFolder() const const QStringList hashes = getSelectedTorrentsHashes(); foreach (const QString &hash, hashes) { const QTorrentHandle h = BTSession->getTorrentHandle(hash); - if (h.is_valid()) { - QString rootFolder = h.root_path(); - qDebug("Opening path at %s", qPrintable(rootFolder)); - if (!pathsList.contains(rootFolder)) { - pathsList.insert(rootFolder); - openUrl(rootFolder); - } + QString path; + if (h.num_files() == 1) { + path = QDir(h.root_path()).absoluteFilePath(h.filepath_at(0)); + if (!pathsList.contains(path)) + misc::openFolderSelect(path); } + else { + path = h.root_path(); + if (!pathsList.contains(path)) + misc::openPath(path); + } + pathsList.insert(path); } } @@ -670,16 +676,6 @@ void TransferListWidget::askNewLabelForSelection() } while(invalid); } -bool TransferListWidget::openUrl(const QString &_path) const -{ - const QString path = fsutils::fromNativePath(_path); - // Hack to access samba shares with QDesktopServices::openUrl - if (path.startsWith("//")) - return QDesktopServices::openUrl(fsutils::toNativePath("file:" + path)); - else - return QDesktopServices::openUrl(QUrl::fromLocalFile(path)); -} - void TransferListWidget::renameSelectedTorrent() { const QModelIndexList selectedIndexes = selectionModel()->selectedRows(); diff --git a/src/gui/transferlistwidget.h b/src/gui/transferlistwidget.h index 5077ca1cc..af974f89d 100644 --- a/src/gui/transferlistwidget.h +++ b/src/gui/transferlistwidget.h @@ -109,9 +109,6 @@ protected slots: void askNewLabelForSelection(); void saveSettings(); -private: - bool openUrl(const QString& _path) const; - signals: void currentTorrentChanged(const QTorrentHandle &h);