Select the file of single file torrents when opening destination folder

Also, add the support for Nautilus (Gnome 3), Caja and Nemo.

Backport of #2544.
This commit is contained in:
Gabriele
2015-08-29 20:58:39 +02:00
parent 8679b70b4c
commit 4696fbff4a
5 changed files with 91 additions and 82 deletions

View File

@@ -31,6 +31,7 @@
#include "core/unicodestrings.h"
#include "core/logger.h"
#include "misc.h"
#include "fs_utils.h"
#include <cmath>
@@ -52,6 +53,11 @@
#include <QDesktopWidget>
#endif
#ifndef DISABLE_GUI
#include <QDesktopServices>
#include <QProcess>
#endif
#ifdef Q_OS_WIN
#include <windows.h>
#include <PowrProf.h>
@@ -692,6 +698,64 @@ void misc::loadBencodedFile(const QString &filename, std::vector<char> &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 {