diff --git a/src/core/utils/misc.cpp b/src/core/utils/misc.cpp index 3eba4280f..ac87ddad2 100644 --- a/src/core/utils/misc.cpp +++ b/src/core/utils/misc.cpp @@ -66,10 +66,16 @@ const int UNLEN = 256; #endif #endif // DISABLE_GUI +#ifndef DISABLE_GUI +#include +#include +#endif + #include "core/utils/string.h" #include "core/unicodestrings.h" #include "core/logger.h" #include "misc.h" +#include "fs.h" static struct { const char *source; const char *comment; } units[] = { QT_TRANSLATE_NOOP3("misc", "B", "bytes"), @@ -526,6 +532,64 @@ QString Utils::Misc::parseHtmlLinks(const QString &raw_text) return result; } +#ifndef DISABLE_GUI +// Open the given path with an appropriate application +void Utils::Misc::openPath(const QString& absolutePath) +{ + const QString path = Utils::Fs::fromNativePath(absolutePath); + // Hack to access samba shares with QDesktopServices::openUrl + if (path.startsWith("//")) + QDesktopServices::openUrl(Utils::Fs::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 Utils::Misc::openFolderSelect(const QString& absolutePath) +{ + const QString path = Utils::Fs::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," << Utils::Fs::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" << Utils::Fs::toNativePath(path)); + else if (output == "nautilus.desktop" || output == "org.gnome.Nautilus.desktop" + || output == "nautilus-folder-handler.desktop") + proc.startDetached("nautilus", QStringList() << "--no-desktop" << Utils::Fs::toNativePath(path)); + else if (output == "caja-folder-handler.desktop") + proc.startDetached("caja", QStringList() << "--no-desktop" << Utils::Fs::toNativePath(path)); + else if (output == "nemo.desktop") + proc.startDetached("nemo", QStringList() << "--no-desktop" << Utils::Fs::toNativePath(path)); + else if (output == "kfmclient_dir.desktop") + proc.startDetached("konqueror", QStringList() << "--select" << Utils::Fs::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 diff --git a/src/core/utils/misc.h b/src/core/utils/misc.h index dd390fb06..62de64019 100644 --- a/src/core/utils/misc.h +++ b/src/core/utils/misc.h @@ -76,6 +76,11 @@ namespace Utils QList intListfromStringList(const QStringList &l); QList boolListfromStringList(const QStringList &l); +#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 dbcde3f3d..9c5bd9f9d 100644 --- a/src/gui/properties/propertieswidget.cpp +++ b/src/gui/properties/propertieswidget.cpp @@ -39,7 +39,6 @@ #include #include #include -#include #include #include "core/bittorrent/session.h" @@ -475,15 +474,7 @@ void PropertiesWidget::openFile(const QModelIndex &index) { qDebug("Trying to open file at %s", qPrintable(file_path)); // Flush data m_torrent->flushCache(); - if (QFile::exists(file_path)) { - if (file_path.startsWith("//")) - QDesktopServices::openUrl(Utils::Fs::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.")); - } + Utils::Misc::openPath(file_path); } void PropertiesWidget::openFolder(const QModelIndex &index, bool containing_folder) { @@ -500,10 +491,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(m_torrent->actualSavePath()); const QString relative_path = path_items.join("/"); absolute_path = Utils::Fs::expandPath(saveDir.absoluteFilePath(relative_path)); @@ -513,54 +500,14 @@ void PropertiesWidget::openFolder(const QModelIndex &index, bool containing_fold const QDir saveDir(m_torrent->actualSavePath()); const QString relative_path = m_torrent->filePath(i); absolute_path = Utils::Fs::expandPath(saveDir.absoluteFilePath(relative_path)); - -#if !(defined(Q_OS_WIN) || (defined(Q_OS_UNIX) && !defined(Q_OS_MAC))) - if (containing_folder) - absolute_path = Utils::Fs::folderName(absolute_path); -#endif } // Flush data m_torrent->flushCache(); - 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," << Utils::Fs::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" << Utils::Fs::toNativePath(absolute_path)); - else if (output == "nautilus-folder-handler.desktop") - proc.startDetached("nautilus", QStringList() << "--no-desktop" << Utils::Fs::toNativePath(absolute_path)); - else if (output == "kfmclient_dir.desktop") - proc.startDetached("konqueror", QStringList() << "--select" << Utils::Fs::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(Utils::Fs::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) + Utils::Misc::openFolderSelect(absolute_path); + else + Utils::Misc::openPath(absolute_path); } void PropertiesWidget::displayFilesListMenu(const QPoint&) { diff --git a/src/gui/transferlistwidget.cpp b/src/gui/transferlistwidget.cpp index 690a17803..8109e7f0c 100644 --- a/src/gui/transferlistwidget.cpp +++ b/src/gui/transferlistwidget.cpp @@ -32,7 +32,6 @@ #include #include #include -#include #include #include #include @@ -176,7 +175,7 @@ TorrentModel* TransferListWidget::getSourceModel() const void TransferListWidget::previewFile(QString filePath) { - openUrl(filePath); + Utils::Misc::openPath(filePath); } inline QModelIndex TransferListWidget::mapToSource(const QModelIndex &index) const @@ -213,7 +212,11 @@ void TransferListWidget::torrentDoubleClicked(const QModelIndex& index) torrent->pause(); break; case OPEN_DEST: - openUrl(torrent->rootPath()); + if (torrent->filesCount() == 1) + Utils::Misc::openFolderSelect(QDir(torrent->rootPath()).absoluteFilePath(torrent->filePath(0))); + else + Utils::Misc::openPath(torrent->rootPath()); + break; } } @@ -380,12 +383,18 @@ void TransferListWidget::openSelectedTorrentsFolder() const { QSet pathsList; foreach (BitTorrent::TorrentHandle *const torrent, getSelectedTorrents()) { - QString rootFolder = torrent->rootPath(); - qDebug("Opening path at %s", qPrintable(rootFolder)); - if (!pathsList.contains(rootFolder)) { - pathsList.insert(rootFolder); - openUrl(rootFolder); + QString path; + if (torrent->filesCount() == 1) { + path = QDir(torrent->rootPath()).absoluteFilePath(torrent->filePath(0)); + if (!pathsList.contains(path)) + Utils::Misc::openFolderSelect(path); } + else { + path = torrent->rootPath(); + if (!pathsList.contains(path)) + Utils::Misc::openPath(path); + } + pathsList.insert(path); } } @@ -568,16 +577,6 @@ void TransferListWidget::askNewLabelForSelection() } while(invalid); } -bool TransferListWidget::openUrl(const QString &_path) const -{ - const QString path = Utils::Fs::fromNativePath(_path); - // Hack to access samba shares with QDesktopServices::openUrl - if (path.startsWith("//")) - return QDesktopServices::openUrl(Utils::Fs::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 4d799d467..c6f7136ea 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(BitTorrent::TorrentHandle *const torrent);