Add DStandardPaths
authorIceyer <me@iceyer.net>
Sat, 29 Jul 2017 07:06:53 +0000 (15:06 +0800)
committerIceyer <me@iceyer.net>
Sat, 29 Jul 2017 11:30:21 +0000 (19:30 +0800)
Change-Id: I2aaab5098ead544880aa9055dea3217f8ca092d7

src/filesystem/DStandardPaths [new file with mode: 0644]
src/filesystem/dstandardpaths.cpp [new file with mode: 0644]
src/filesystem/dstandardpaths.h [new file with mode: 0644]
src/filesystem/filesystem.pri

diff --git a/src/filesystem/DStandardPaths b/src/filesystem/DStandardPaths
new file mode 100644 (file)
index 0000000..9d0b963
--- /dev/null
@@ -0,0 +1 @@
+#include "dstandardpaths.h"
diff --git a/src/filesystem/dstandardpaths.cpp b/src/filesystem/dstandardpaths.cpp
new file mode 100644 (file)
index 0000000..cbfca54
--- /dev/null
@@ -0,0 +1,97 @@
+/**
+ * Copyright (C) 2017 Deepin Technology Co., Ltd.
+ *
+ * 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; either version 3 of the License, or
+ * (at your option) any later version.
+ **/
+
+#include "dstandardpaths.h"
+
+#include <QProcessEnvironment>
+
+DCORE_BEGIN_NAMESPACE
+
+
+class DSnapStandardPaths
+{
+public:
+    inline  static QString writableLocation(QStandardPaths::StandardLocation /*type*/)
+    {
+        QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
+        return env.value("SNAP_USER_COMMON");
+    }
+
+    inline static QStringList standardLocations(QStandardPaths::StandardLocation type)
+    {
+        QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
+
+        switch (type) {
+        case QStandardPaths::GenericDataLocation: {
+            QString snapRoot = env.value("SNAP");
+            QString genericDataDir = snapRoot + "/usr/share/";
+            return QStringList() << genericDataDir;
+        }
+        default:
+            break;
+        }
+
+        return QStringList() << env.value("SNAP_USER_COMMON");
+    }
+
+private:
+    DSnapStandardPaths();
+    ~DSnapStandardPaths();
+    Q_DISABLE_COPY(DSnapStandardPaths)
+};
+
+
+static DStandardPaths::Mode s_mode = DStandardPaths::Auto;
+
+QString DStandardPaths::writableLocation(QStandardPaths::StandardLocation type)
+{
+    switch (s_mode) {
+    case Auto:
+    case Test:
+        return  QStandardPaths::writableLocation(type);
+    case Snap:
+        return DSnapStandardPaths::writableLocation(type);
+    }
+    return QStandardPaths::writableLocation(type);
+}
+
+QStringList DStandardPaths::standardLocations(QStandardPaths::StandardLocation type)
+{
+    switch (s_mode) {
+    case Auto:
+    case Test:
+        return  QStandardPaths::standardLocations(type);
+    case Snap:
+        return DSnapStandardPaths::standardLocations(type);
+    }
+    return  QStandardPaths::standardLocations(type);
+}
+
+QString DStandardPaths::locate(QStandardPaths::StandardLocation type, const QString &fileName, QStandardPaths::LocateOptions options)
+{
+    return QStandardPaths::locate(type, fileName, options);
+}
+
+QStringList DStandardPaths::locateAll(QStandardPaths::StandardLocation type, const QString &fileName, QStandardPaths::LocateOptions options)
+{
+    return QStandardPaths::locateAll(type, fileName, options);
+}
+
+QString DStandardPaths::findExecutable(const QString &executableName, const QStringList &paths)
+{
+    return QStandardPaths::findExecutable(executableName, paths);
+}
+
+void DStandardPaths::setMode(DStandardPaths::Mode mode)
+{
+    s_mode = mode;
+    QStandardPaths::setTestModeEnabled(mode == Test);
+}
+
+DCORE_END_NAMESPACE
diff --git a/src/filesystem/dstandardpaths.h b/src/filesystem/dstandardpaths.h
new file mode 100644 (file)
index 0000000..740a647
--- /dev/null
@@ -0,0 +1,45 @@
+/**
+ * Copyright (C) 2017 Deepin Technology Co., Ltd.
+ *
+ * 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; either version 3 of the License, or
+ * (at your option) any later version.
+ **/
+
+#ifndef DTK_CORE_FILESYSTEM_DSTANDARDPATHS_H
+#define DTK_CORE_FILESYSTEM_DSTANDARDPATHS_H
+
+#include <QStandardPaths>
+
+#include "dtkcore_global.h"
+
+DCORE_BEGIN_NAMESPACE
+
+class DStandardPathsPrivate;
+class DStandardPaths
+{
+public:
+    enum Mode {
+        Auto,
+        Snap,
+        Test,
+    };
+
+    static QString writableLocation(QStandardPaths::StandardLocation type);
+    static QStringList standardLocations(QStandardPaths::StandardLocation type);
+
+    static QString locate(QStandardPaths::StandardLocation type, const QString &fileName, QStandardPaths::LocateOptions options = QStandardPaths::LocateFile);
+    static QStringList locateAll(QStandardPaths::StandardLocation type, const QString &fileName, QStandardPaths::LocateOptions options = QStandardPaths::LocateFile);
+    static QString findExecutable(const QString &executableName, const QStringList &paths = QStringList());
+    static void setMode(Mode mode);
+
+private:
+    DStandardPaths();
+    ~DStandardPaths();
+    Q_DISABLE_COPY(DStandardPaths)
+};
+
+DCORE_END_NAMESPACE
+
+#endif // DTK_CORE_FILESYSTEM_DSTANDARDPATHS_H
index 4f90a6d8531e59a157bfb3bd09728b21d3157c1a..0b53c1447519a6e705a2f3482ba325370ff9347a 100644 (file)
@@ -5,12 +5,14 @@ HEADERS += \
     $$PWD/dfilesystemwatcher.h \
     $$PWD/dfilewatcher.h \
     $$PWD/dfilewatchermanager.h \
-    $$PWD/dpathbuf.h
+    $$PWD/dpathbuf.h \
+    $$PWD/dstandardpaths.h
 
 SOURCES += \
     $$PWD/dbasefilewatcher.cpp \
     $$PWD/dfilewatcher.cpp \
-    $$PWD/dfilewatchermanager.cpp
+    $$PWD/dfilewatchermanager.cpp \
+    $$PWD/dstandardpaths.cpp
 
 
 linux {
@@ -30,4 +32,5 @@ includes.files += \
     $$PWD/DBaseFileWatcher \
     $$PWD/DFileSystemWatcher \
     $$PWD/DFileWatcherManager \
-    $$PWD/DPathBuf
+    $$PWD/DPathBuf \
+    $$PWD/DStandardPaths