2014-07-07 17:49:48 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) by Klaas Freitag <freitag@owncloud.com>
|
|
|
|
* Copyright (C) by Daniel Molkentin <danimo@owncloud.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2016-10-25 12:00:07 +03:00
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2014-07-07 17:49:48 +04:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "openfilemanager.h"
|
2017-08-16 09:36:52 +03:00
|
|
|
#include "common/utility.h"
|
2014-07-07 17:49:48 +04:00
|
|
|
#include <QProcess>
|
|
|
|
#include <QSettings>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QUrl>
|
|
|
|
#include <QDesktopServices>
|
|
|
|
#include <QApplication>
|
|
|
|
|
2014-11-10 00:34:07 +03:00
|
|
|
namespace OCC {
|
2014-07-07 17:49:48 +04:00
|
|
|
|
|
|
|
// according to the QStandardDir impl from Qt5
|
|
|
|
static QStringList xdgDataDirs()
|
|
|
|
{
|
|
|
|
QStringList dirs;
|
|
|
|
// http://standards.freedesktop.org/basedir-spec/latest/
|
|
|
|
QString xdgDataDirsEnv = QFile::decodeName(qgetenv("XDG_DATA_DIRS"));
|
|
|
|
if (xdgDataDirsEnv.isEmpty()) {
|
|
|
|
dirs.append(QString::fromLatin1("/usr/local/share"));
|
|
|
|
dirs.append(QString::fromLatin1("/usr/share"));
|
|
|
|
} else {
|
|
|
|
dirs = xdgDataDirsEnv.split(QLatin1Char(':'));
|
|
|
|
}
|
|
|
|
// local location
|
|
|
|
QString xdgDataHome = QFile::decodeName(qgetenv("XDG_DATA_HOME"));
|
|
|
|
if (xdgDataHome.isEmpty()) {
|
2017-05-17 11:55:42 +03:00
|
|
|
xdgDataHome = QDir::homePath() + "/.local/share";
|
2014-07-07 17:49:48 +04:00
|
|
|
}
|
|
|
|
dirs.prepend(xdgDataHome);
|
|
|
|
return dirs;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Linux impl only, make sure to process %u and %U which might be returned
|
|
|
|
static QString findDefaultFileManager()
|
|
|
|
{
|
|
|
|
QProcess p;
|
2017-05-17 11:55:42 +03:00
|
|
|
p.start("xdg-mime", QStringList() << "query"
|
|
|
|
<< "default"
|
|
|
|
<< "inode/directory",
|
|
|
|
QFile::ReadOnly);
|
2014-07-07 17:49:48 +04:00
|
|
|
p.waitForFinished();
|
|
|
|
QString fileName = QString::fromUtf8(p.readAll().trimmed());
|
|
|
|
if (fileName.isEmpty())
|
|
|
|
return QString();
|
|
|
|
|
|
|
|
QFileInfo fi;
|
|
|
|
QStringList dirs = xdgDataDirs();
|
|
|
|
QStringList subdirs;
|
2017-05-17 11:55:42 +03:00
|
|
|
subdirs << "/applications/"
|
|
|
|
<< "/applications/kde4/";
|
|
|
|
foreach (QString dir, dirs) {
|
|
|
|
foreach (QString subdir, subdirs) {
|
2014-07-07 17:49:48 +04:00
|
|
|
fi.setFile(dir + subdir + fileName);
|
|
|
|
if (fi.exists()) {
|
|
|
|
return fi.absoluteFilePath();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
// early dolphin versions did not have --select
|
|
|
|
static bool checkDolphinCanSelect()
|
|
|
|
{
|
|
|
|
QProcess p;
|
|
|
|
p.start("dolphin", QStringList() << "--help", QFile::ReadOnly);
|
|
|
|
p.waitForFinished();
|
|
|
|
return p.readAll().contains("--select");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// inspired by Qt Creator's showInGraphicalShell();
|
|
|
|
void showInFileManager(const QString &localPath)
|
|
|
|
{
|
|
|
|
if (Utility::isWindows()) {
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
if (QSysInfo::windowsVersion() <= QSysInfo::WV_2003) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
QString explorer = "explorer.exe "; // FIXME: we trust it's in PATH
|
2014-10-23 12:40:22 +04:00
|
|
|
QFileInfo fi(localPath);
|
2014-07-07 17:49:48 +04:00
|
|
|
|
2014-10-23 12:40:22 +04:00
|
|
|
// canonicalFilePath returns empty if the file does not exist
|
2017-05-17 11:55:42 +03:00
|
|
|
if (!fi.canonicalFilePath().isEmpty()) {
|
2015-02-06 15:44:06 +03:00
|
|
|
QString nativeArgs;
|
2014-10-23 12:40:22 +04:00
|
|
|
if (!fi.isDir()) {
|
2015-02-06 15:44:06 +03:00
|
|
|
nativeArgs += QLatin1String("/select,");
|
2014-10-23 12:40:22 +04:00
|
|
|
}
|
2015-02-06 15:44:06 +03:00
|
|
|
nativeArgs += QLatin1Char('"');
|
|
|
|
nativeArgs += QDir::toNativeSeparators(fi.canonicalFilePath());
|
|
|
|
nativeArgs += QLatin1Char('"');
|
2014-10-23 12:40:22 +04:00
|
|
|
|
|
|
|
QProcess p;
|
2015-02-06 15:44:06 +03:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
// QProcess on Windows tries to wrap the whole argument/program string
|
|
|
|
// with quotes if it detects a space in it, but explorer wants the quotes
|
|
|
|
// only around the path. Use setNativeArguments to bypass this logic.
|
|
|
|
p.setNativeArguments(nativeArgs);
|
|
|
|
#endif
|
2014-10-23 12:40:22 +04:00
|
|
|
p.start(explorer);
|
|
|
|
p.waitForFinished(5000);
|
2014-07-07 17:49:48 +04:00
|
|
|
}
|
|
|
|
} else if (Utility::isMac()) {
|
|
|
|
QStringList scriptArgs;
|
|
|
|
scriptArgs << QLatin1String("-e")
|
|
|
|
<< QString::fromLatin1("tell application \"Finder\" to reveal POSIX file \"%1\"")
|
2017-05-17 11:55:42 +03:00
|
|
|
.arg(localPath);
|
2014-07-07 17:49:48 +04:00
|
|
|
QProcess::execute(QLatin1String("/usr/bin/osascript"), scriptArgs);
|
|
|
|
scriptArgs.clear();
|
|
|
|
scriptArgs << QLatin1String("-e")
|
|
|
|
<< QLatin1String("tell application \"Finder\" to activate");
|
|
|
|
QProcess::execute(QLatin1String("/usr/bin/osascript"), scriptArgs);
|
|
|
|
} else {
|
|
|
|
QString app;
|
|
|
|
QStringList args;
|
|
|
|
|
|
|
|
static QString defaultManager = findDefaultFileManager();
|
|
|
|
QSettings desktopFile(defaultManager, QSettings::IniFormat);
|
|
|
|
QString exec = desktopFile.value("Desktop Entry/Exec").toString();
|
|
|
|
|
|
|
|
QString fileToOpen = QFileInfo(localPath).absoluteFilePath();
|
|
|
|
QString pathToOpen = QFileInfo(localPath).absolutePath();
|
|
|
|
bool canHandleFile = false; // assume dumb fm
|
|
|
|
|
|
|
|
args = exec.split(' ');
|
2017-05-17 11:55:42 +03:00
|
|
|
if (args.count() > 0)
|
|
|
|
app = args.takeFirst();
|
2014-07-07 17:49:48 +04:00
|
|
|
|
|
|
|
QString kdeSelectParam("--select");
|
|
|
|
|
|
|
|
if (app.contains("konqueror") && !args.contains(kdeSelectParam)) {
|
|
|
|
// konq needs '--select' in order not to launch the file
|
|
|
|
args.prepend(kdeSelectParam);
|
|
|
|
canHandleFile = true;
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
if (app.contains("dolphin")) {
|
2014-07-07 17:49:48 +04:00
|
|
|
static bool dolphinCanSelect = checkDolphinCanSelect();
|
|
|
|
if (dolphinCanSelect && !args.contains(kdeSelectParam)) {
|
|
|
|
args.prepend(kdeSelectParam);
|
|
|
|
canHandleFile = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// whitelist
|
|
|
|
if (app.contains("nautilus") || app.contains("nemo")) {
|
|
|
|
canHandleFile = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static QString name;
|
|
|
|
if (name.isEmpty()) {
|
|
|
|
name = desktopFile.value(QString::fromLatin1("Desktop Entry/Name[%1]").arg(qApp->property("ui_lang").toString())).toString();
|
|
|
|
if (name.isEmpty()) {
|
|
|
|
name = desktopFile.value(QString::fromLatin1("Desktop Entry/Name")).toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::replace(args.begin(), args.end(), QString::fromLatin1("%c"), name);
|
|
|
|
std::replace(args.begin(), args.end(), QString::fromLatin1("%u"), fileToOpen);
|
|
|
|
std::replace(args.begin(), args.end(), QString::fromLatin1("%U"), fileToOpen);
|
|
|
|
std::replace(args.begin(), args.end(), QString::fromLatin1("%f"), fileToOpen);
|
|
|
|
std::replace(args.begin(), args.end(), QString::fromLatin1("%F"), fileToOpen);
|
|
|
|
|
|
|
|
// fixme: needs to append --icon, according to http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#exec-variables
|
|
|
|
QStringList::iterator it = std::find(args.begin(), args.end(), QString::fromLatin1("%i"));
|
|
|
|
if (it != args.end()) {
|
|
|
|
(*it) = desktopFile.value("Desktop Entry/Icon").toString();
|
|
|
|
args.insert(it, QString::fromLatin1("--icon")); // before
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
if (args.count() == 0)
|
|
|
|
args << fileToOpen;
|
2014-07-07 17:49:48 +04:00
|
|
|
|
|
|
|
if (app.isEmpty() || args.isEmpty() || !canHandleFile) {
|
|
|
|
// fall back: open the default file manager, without ever selecting the file
|
|
|
|
QDesktopServices::openUrl(QUrl::fromLocalFile(pathToOpen));
|
|
|
|
} else {
|
|
|
|
QProcess::startDetached(app, args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-10-23 12:40:22 +04:00
|
|
|
}
|