Browse Source

Select the file of single file torrents when opening destination folder

Also, add the support for Nautilus (Gnome 3), Caja and Nemo.
adaptive-webui-19844
Gabriele 10 years ago
parent
commit
e728710430
  1. 64
      src/core/utils/misc.cpp
  2. 5
      src/core/utils/misc.h
  3. 63
      src/gui/properties/propertieswidget.cpp
  4. 35
      src/gui/transferlistwidget.cpp
  5. 3
      src/gui/transferlistwidget.h

64
src/core/utils/misc.cpp

@ -66,10 +66,16 @@ const int UNLEN = 256;
#endif #endif
#endif // DISABLE_GUI #endif // DISABLE_GUI
#ifndef DISABLE_GUI
#include <QDesktopServices>
#include <QProcess>
#endif
#include "core/utils/string.h" #include "core/utils/string.h"
#include "core/unicodestrings.h" #include "core/unicodestrings.h"
#include "core/logger.h" #include "core/logger.h"
#include "misc.h" #include "misc.h"
#include "fs.h"
static struct { const char *source; const char *comment; } units[] = { static struct { const char *source; const char *comment; } units[] = {
QT_TRANSLATE_NOOP3("misc", "B", "bytes"), QT_TRANSLATE_NOOP3("misc", "B", "bytes"),
@ -526,6 +532,64 @@ QString Utils::Misc::parseHtmlLinks(const QString &raw_text)
return result; 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 namespace
{ {
// Trick to get a portable sleep() function // Trick to get a portable sleep() function

5
src/core/utils/misc.h

@ -76,6 +76,11 @@ namespace Utils
QList<int> intListfromStringList(const QStringList &l); QList<int> intListfromStringList(const QStringList &l);
QList<bool> boolListfromStringList(const QStringList &l); QList<bool> boolListfromStringList(const QStringList &l);
#ifndef DISABLE_GUI
void openPath(const QString& absolutePath);
void openFolderSelect(const QString& absolutePath);
#endif
void msleep(unsigned long msecs); void msleep(unsigned long msecs);
} }
} }

63
src/gui/properties/propertieswidget.cpp

@ -39,7 +39,6 @@
#include <QMessageBox> #include <QMessageBox>
#include <QMenu> #include <QMenu>
#include <QFileDialog> #include <QFileDialog>
#include <QDesktopServices>
#include <QBitArray> #include <QBitArray>
#include "core/bittorrent/session.h" #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)); qDebug("Trying to open file at %s", qPrintable(file_path));
// Flush data // Flush data
m_torrent->flushCache(); m_torrent->flushCache();
if (QFile::exists(file_path)) { Utils::Misc::openPath(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."));
}
} }
void PropertiesWidget::openFolder(const QModelIndex &index, bool containing_folder) { 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()) if (path_items.isEmpty())
return; 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 QDir saveDir(m_torrent->actualSavePath());
const QString relative_path = path_items.join("/"); const QString relative_path = path_items.join("/");
absolute_path = Utils::Fs::expandPath(saveDir.absoluteFilePath(relative_path)); 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 QDir saveDir(m_torrent->actualSavePath());
const QString relative_path = m_torrent->filePath(i); const QString relative_path = m_torrent->filePath(i);
absolute_path = Utils::Fs::expandPath(saveDir.absoluteFilePath(relative_path)); 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 // Flush data
m_torrent->flushCache(); m_torrent->flushCache();
if (!QFile::exists(absolute_path)) if (containing_folder)
return; Utils::Misc::openFolderSelect(absolute_path);
qDebug("Trying to open folder at %s", qPrintable(absolute_path)); else
Utils::Misc::openPath(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
} }
void PropertiesWidget::displayFilesListMenu(const QPoint&) { void PropertiesWidget::displayFilesListMenu(const QPoint&) {

35
src/gui/transferlistwidget.cpp

@ -32,7 +32,6 @@
#include <QShortcut> #include <QShortcut>
#include <QStandardItemModel> #include <QStandardItemModel>
#include <QSortFilterProxyModel> #include <QSortFilterProxyModel>
#include <QDesktopServices>
#include <QTimer> #include <QTimer>
#include <QClipboard> #include <QClipboard>
#include <QColor> #include <QColor>
@ -176,7 +175,7 @@ TorrentModel* TransferListWidget::getSourceModel() const
void TransferListWidget::previewFile(QString filePath) void TransferListWidget::previewFile(QString filePath)
{ {
openUrl(filePath); Utils::Misc::openPath(filePath);
} }
inline QModelIndex TransferListWidget::mapToSource(const QModelIndex &index) const inline QModelIndex TransferListWidget::mapToSource(const QModelIndex &index) const
@ -213,7 +212,11 @@ void TransferListWidget::torrentDoubleClicked(const QModelIndex& index)
torrent->pause(); torrent->pause();
break; break;
case OPEN_DEST: 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<QString> pathsList; QSet<QString> pathsList;
foreach (BitTorrent::TorrentHandle *const torrent, getSelectedTorrents()) { foreach (BitTorrent::TorrentHandle *const torrent, getSelectedTorrents()) {
QString rootFolder = torrent->rootPath(); QString path;
qDebug("Opening path at %s", qPrintable(rootFolder)); if (torrent->filesCount() == 1) {
if (!pathsList.contains(rootFolder)) { path = QDir(torrent->rootPath()).absoluteFilePath(torrent->filePath(0));
pathsList.insert(rootFolder); if (!pathsList.contains(path))
openUrl(rootFolder); 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); } 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() void TransferListWidget::renameSelectedTorrent()
{ {
const QModelIndexList selectedIndexes = selectionModel()->selectedRows(); const QModelIndexList selectedIndexes = selectionModel()->selectedRows();

3
src/gui/transferlistwidget.h

@ -109,9 +109,6 @@ protected slots:
void askNewLabelForSelection(); void askNewLabelForSelection();
void saveSettings(); void saveSettings();
private:
bool openUrl(const QString& _path) const;
signals: signals:
void currentTorrentChanged(BitTorrent::TorrentHandle *const torrent); void currentTorrentChanged(BitTorrent::TorrentHandle *const torrent);

Loading…
Cancel
Save