[Pkg-owncloud-commits] [owncloud-client] 176/332: Utility: Move showInFileManager in its own file

Sandro Knauß hefee-guest at moszumanska.debian.org
Thu Aug 14 21:06:56 UTC 2014


This is an automated email from the git hooks/post-receive script.

hefee-guest pushed a commit to branch master
in repository owncloud-client.

commit 99ee81a4895d0390300b5b92d2c27e77434ad201
Author: Olivier Goffart <ogoffart at woboq.com>
Date:   Mon Jul 7 15:49:48 2014 +0200

    Utility: Move showInFileManager in its own file
    
    And get rid of GUI in the Utility namespace
---
 src/CMakeLists.txt             |   2 +
 src/mirall/openfilemanager.cpp | 185 +++++++++++++++++++++++++++++++++++++++++
 src/mirall/openfilemanager.h   |  20 +++++
 src/mirall/owncloudgui.cpp     |   3 +-
 src/mirall/protocolwidget.cpp  |   3 +-
 src/mirall/utility.cpp         | 166 ------------------------------------
 src/mirall/utility.h           |   1 -
 7 files changed, 211 insertions(+), 169 deletions(-)

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 669854f..209fba2 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -275,6 +275,7 @@ set(mirall_SRCS
     mirall/folderwizard.cpp
     mirall/folderstatusmodel.cpp
     mirall/protocolwidget.cpp
+    mirall/openfilemanager.cpp
     wizard/owncloudwizard.cpp
     wizard/owncloudsetuppage.cpp
     wizard/owncloudhttpcredspage.cpp
@@ -303,6 +304,7 @@ set(mirall_HEADERS
     mirall/systray.h
     mirall/folderwizard.h
     mirall/owncloudsetupwizard.h
+    mirall/openfilemanager.h
     wizard/owncloudwizard.h
     wizard/owncloudsetuppage.h
     wizard/owncloudhttpcredspage.h
diff --git a/src/mirall/openfilemanager.cpp b/src/mirall/openfilemanager.cpp
new file mode 100644
index 0000000..397c5c5
--- /dev/null
+++ b/src/mirall/openfilemanager.cpp
@@ -0,0 +1,185 @@
+/*
+ * Copyright (C) by Klaas Freitag <freitag at owncloud.com>
+ * Copyright (C) by Daniel Molkentin <danimo at 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
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * 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"
+#include "utility.h"
+#include <QProcess>
+#include <QSettings>
+#include <QDir>
+#include <QUrl>
+#include <QDesktopServices>
+#include <QDebug>
+#include <QApplication>
+
+namespace Mirall {
+
+// 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()) {
+        xdgDataHome = QDir::homePath()+"/.local/share";
+    }
+    dirs.prepend(xdgDataHome);
+    return dirs;
+}
+
+// Linux impl only, make sure to process %u and %U which might be returned
+static QString findDefaultFileManager()
+{
+    QProcess p;
+    p.start("xdg-mime", QStringList() << "query" << "default" << "inode/directory", QFile::ReadOnly);
+    p.waitForFinished();
+    QString fileName = QString::fromUtf8(p.readAll().trimmed());
+    if (fileName.isEmpty())
+        return QString();
+
+    QFileInfo fi;
+    QStringList dirs = xdgDataDirs();
+    QStringList subdirs;
+    subdirs << "/applications/" << "/applications/kde4/";
+    foreach(QString dir, dirs) {
+        foreach(QString subdir, subdirs) {
+            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
+
+        if (!QFileInfo(localPath).isDir()) {
+            explorer += QLatin1String("/select,");
+        }
+        explorer += QLatin1Char('"');
+        explorer += QDir::toNativeSeparators(localPath);
+        explorer += QLatin1Char('"');
+
+        qDebug() << "OO Open explorer commandline:" << explorer;
+        QProcess p;
+        p.start(explorer);
+        p.waitForFinished(5000);
+    } else if (Utility::isMac()) {
+        QStringList scriptArgs;
+        scriptArgs << QLatin1String("-e")
+                   << QString::fromLatin1("tell application \"Finder\" to reveal POSIX file \"%1\"")
+                      .arg(localPath);
+        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(' ');
+        if (args.count() > 0) app = args.takeFirst();
+
+        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;
+        }
+
+        if (app.contains("dolphin"))
+        {
+            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
+        }
+
+
+        if (args.count() == 0) args << fileToOpen;
+
+        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);
+        }
+    }
+}
+
+}
\ No newline at end of file
diff --git a/src/mirall/openfilemanager.h b/src/mirall/openfilemanager.h
new file mode 100644
index 0000000..c395f15
--- /dev/null
+++ b/src/mirall/openfilemanager.h
@@ -0,0 +1,20 @@
+/*
+ * Copyright (C) by Olivier Goffart <ogoffart at woboq.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
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * 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.
+ */
+#pragma once
+
+#include <QString>
+
+namespace Mirall {
+/** Open the file manager with the specified file pre-selected */
+void showInFileManager(const QString &localPath);
+}
diff --git a/src/mirall/owncloudgui.cpp b/src/mirall/owncloudgui.cpp
index 001be58..7de5c5c 100644
--- a/src/mirall/owncloudgui.cpp
+++ b/src/mirall/owncloudgui.cpp
@@ -27,6 +27,7 @@
 #include "mirall/logger.h"
 #include "mirall/logbrowser.h"
 #include "mirall/account.h"
+#include "openfilemanager.h"
 #include "creds/abstractcredentials.h"
 
 #include <QDesktopServices>
@@ -140,7 +141,7 @@ void ownCloudGui::slotFoldersChanged()
 
 void ownCloudGui::slotOpenPath(const QString &path)
 {
-    Utility::showInFileManager(path);
+    showInFileManager(path);
 }
 
 void ownCloudGui::slotAccountStateChanged()
diff --git a/src/mirall/protocolwidget.cpp b/src/mirall/protocolwidget.cpp
index 965d778..f15b0ab 100644
--- a/src/mirall/protocolwidget.cpp
+++ b/src/mirall/protocolwidget.cpp
@@ -25,6 +25,7 @@
 #include "mirall/folderman.h"
 #include "mirall/syncfileitem.h"
 #include "mirall/folder.h"
+#include "openfilemanager.h"
 
 #include "ui_protocolwidget.h"
 
@@ -169,7 +170,7 @@ void ProtocolWidget::slotOpenFile( QTreeWidgetItem *item, int )
         // folder->path() always comes back with trailing path
         QString fullPath = folder->path() + fileName;
         if (QFile(fullPath).exists()) {
-            Utility::showInFileManager(fullPath);
+            showInFileManager(fullPath);
         }
     }
 }
diff --git a/src/mirall/utility.cpp b/src/mirall/utility.cpp
index 055abd7..1b4956f 100644
--- a/src/mirall/utility.cpp
+++ b/src/mirall/utility.cpp
@@ -22,9 +22,6 @@
 #include <QDir>
 #include <QFile>
 #include <QUrl>
-#ifndef TOKEN_AUTH_ONLY
-#include <QDesktopServices>
-#endif
 #include <QDebug>
 #include <QProcess>
 #include <QThread>
@@ -256,63 +253,6 @@ void Utility::usleep(int usec)
     QThread::usleep(usec);
 }
 
-// ### helper functions for showInFileManager() ###
-
-// 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()) {
-        xdgDataHome = QDir::homePath()+"/.local/share";
-    }
-    dirs.prepend(xdgDataHome);
-    return dirs;
-}
-
-// Linux impl only, make sure to process %u and %U which might be returned
-static QString findDefaultFileManager()
-{
-    QProcess p;
-    p.start("xdg-mime", QStringList() << "query" << "default" << "inode/directory", QFile::ReadOnly);
-    p.waitForFinished();
-    QString fileName = QString::fromUtf8(p.readAll().trimmed());
-    if (fileName.isEmpty())
-        return QString();
-
-    QFileInfo fi;
-    QStringList dirs = xdgDataDirs();
-    QStringList subdirs;
-    subdirs << "/applications/" << "/applications/kde4/";
-    foreach(QString dir, dirs) {
-        foreach(QString subdir, subdirs) {
-            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");
-}
-
 bool Utility::fsCasePreserving()
 {
     bool re = false;
@@ -325,112 +265,6 @@ bool Utility::fsCasePreserving()
     return re;
 }
 
-// inspired by Qt Creator's showInGraphicalShell();
-void Utility::showInFileManager(const QString &localPath)
-{
-    if (isWindows()) {
-#ifdef Q_OS_WIN
-        if (QSysInfo::windowsVersion() <= QSysInfo::WV_2003) {
-            return;
-        }
-#endif
-        QString explorer = "explorer.exe "; // FIXME: we trust it's in PATH
-
-        if (!QFileInfo(localPath).isDir()) {
-            explorer += QLatin1String("/select,");
-        }
-        explorer += QLatin1Char('"');
-        explorer += QDir::toNativeSeparators(localPath);
-        explorer += QLatin1Char('"');
-
-        qDebug() << "OO Open explorer commandline:" << explorer;
-        QProcess p;
-        p.start(explorer);
-        p.waitForFinished(5000);
-    } else if (isMac()) {
-        QStringList scriptArgs;
-        scriptArgs << QLatin1String("-e")
-                   << QString::fromLatin1("tell application \"Finder\" to reveal POSIX file \"%1\"")
-                      .arg(localPath);
-        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(' ');
-        if (args.count() > 0) app = args.takeFirst();
-
-        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;
-        }
-
-        if (app.contains("dolphin"))
-        {
-            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
-        }
-
-
-        if (args.count() == 0) args << fileToOpen;
-
-        if (app.isEmpty() || args.isEmpty() || !canHandleFile) {
-            // fall back: open the default file manager, without ever selecting the file
-#ifndef TOKEN_AUTH_ONLY
-            QDesktopServices::openUrl(QUrl::fromLocalFile(pathToOpen));
-#endif
-        } else {
-            QProcess::startDetached(app, args);
-        }
-    }
-}
-
-
-
 QDateTime Utility::qDateTimeFromTime_t(qint64 t)
 {
     return QDateTime::fromMSecsSinceEpoch(t * 1000);
diff --git a/src/mirall/utility.h b/src/mirall/utility.h
index 864a5bf..4c98e7e 100644
--- a/src/mirall/utility.h
+++ b/src/mirall/utility.h
@@ -40,7 +40,6 @@ namespace Utility
     OWNCLOUDSYNC_EXPORT void setLaunchOnStartup(const QString &appName, const QString& guiName, bool launch);
     OWNCLOUDSYNC_EXPORT qint64 freeDiskSpace(const QString &path, bool *ok = 0);
     OWNCLOUDSYNC_EXPORT QString toCSyncScheme(const QString &urlStr);
-    OWNCLOUDSYNC_EXPORT void showInFileManager(const QString &localPath);
     /** Like QLocale::toString(double, 'f', prec), but drops trailing zeros after the decimal point */
 
     /**

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud-client.git



More information about the Pkg-owncloud-commits mailing list