Settings: Add "homemade" XDG_CONFIG_DIRS support
authorPalo Kisa <palo.kisa@gmail.com>
Tue, 23 Aug 2016 20:46:10 +0000 (22:46 +0200)
committerPalo Kisa <palo.kisa@gmail.com>
Wed, 14 Sep 2016 16:35:02 +0000 (18:35 +0200)
The LXQt::Settings uses the Qt's QSettings which doesn't support the
xdg spec regarding th XDG_CONFIG_DIRS ->
https://bugreports.qt.io/browse/QTBUG-34919.

This commit adds a workaround by searching the directories for
system wide configuration file and if found, all keys from it are
copied into the user configuration file.

CMakeLists.txt
lxqtsettings.cpp

index b875e89..3fcad29 100644 (file)
@@ -124,6 +124,7 @@ list(APPEND CMAKE_MODULE_PATH
 )
 
 option(UPDATE_TRANSLATIONS "Update source translation translations/*.ts files" OFF)
+option(WITH_XDG_DIRS_FALLBACK "Use our XDG_CONFIG_DIRS fallback" ON)
 
 set(CMAKE_INCLUDE_CURRENT_DIR ON)
 set(CMAKE_POSITION_INDEPENDENT_CODE ON)
@@ -325,6 +326,12 @@ target_compile_definitions(${LXQT_LIBRARY_NAME}
     PRIVATE "$<$<CONFIG:Release>:QT_NO_WARNING_OUTPUT>"
 )
 
+if (WITH_XDG_DIRS_FALLBACK)
+    target_compile_definitions(${LXQT_LIBRARY_NAME}
+        PRIVATE "WITH_XDG_DIRS_FALLBACK"
+    )
+endif ()
+
 target_include_directories(${LXQT_LIBRARY_NAME}
     INTERFACE "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/${LXQT_LIBRARY_NAME}>"
     INTERFACE "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/${LXQT_LIBRARY_NAME}/LXQt>"
index e7b745e..be21748 100644 (file)
@@ -43,7 +43,7 @@ using namespace LXQt;
 class LXQt::SettingsPrivate
 {
 public:
-    SettingsPrivate(Settings* parent):
+    SettingsPrivate(Settings* parent, bool useXdgFallback):
         mFileChangeTimer(0),
         mAppChangeTimer(0),
         mAddWatchTimer(0),
@@ -54,6 +54,31 @@ public:
         if (!mParent->contains("__userfile__"))
         {
             mParent->setValue("__userfile__", true);
+#if defined(WITH_XDG_DIRS_FALLBACK)
+            if (useXdgFallback)
+            {
+                //Note: Qt doesn't support the xdg spec regarding the XDG_CONFIG_DIRS
+                //https://bugreports.qt.io/browse/QTBUG-34919
+                //(Partial) workaround: if the the user specific config file doesn't exist
+                //we try to find some system-wide configuration file and copy all settings into
+                //the user specific file
+                const QString org = mParent->organizationName();
+                const QString file_name = QFileInfo{mParent->fileName()}.fileName();
+                const QStringList dirs = XdgDirs::configDirs();
+                for (auto dir_i = dirs.rbegin(), dir_e = dirs.rend(); dir_i != dir_e; ++dir_i)
+                {
+                    QDir dir{*dir_i};
+                    if (dir.cd(mParent->organizationName()) && dir.exists(file_name))
+                    {
+                        QSettings system_settings{dir.absoluteFilePath(file_name), QSettings::IniFormat};
+                        for (const QString & key : system_settings.allKeys())
+                        {
+                            mParent->setValue(key, system_settings.value(key));
+                        }
+                    }
+                }
+            }
+#endif
             mParent->sync();
         }
         mWatcher.addPath(mParent->fileName());
@@ -111,7 +136,7 @@ public:
  ************************************************/
 Settings::Settings(const QString& module, QObject* parent) :
     QSettings("lxqt", module, parent),
-    d_ptr(new SettingsPrivate(this))
+    d_ptr(new SettingsPrivate(this, true))
 {
 }
 
@@ -121,7 +146,7 @@ Settings::Settings(const QString& module, QObject* parent) :
  ************************************************/
 Settings::Settings(const QString &fileName, QSettings::Format format, QObject *parent):
     QSettings(fileName, format, parent),
-    d_ptr(new SettingsPrivate(this))
+    d_ptr(new SettingsPrivate(this, false))
 {
 }
 
@@ -131,7 +156,7 @@ Settings::Settings(const QString &fileName, QSettings::Format format, QObject *p
  ************************************************/
 Settings::Settings(const QSettings* parentSettings, const QString& subGroup, QObject* parent):
     QSettings(parentSettings->organizationName(), parentSettings->applicationName(), parent),
-    d_ptr(new SettingsPrivate(this))
+    d_ptr(new SettingsPrivate(this, false))
 {
     beginGroup(subGroup);
 }
@@ -142,7 +167,7 @@ Settings::Settings(const QSettings* parentSettings, const QString& subGroup, QOb
  ************************************************/
 Settings::Settings(const QSettings& parentSettings, const QString& subGroup, QObject* parent):
     QSettings(parentSettings.organizationName(), parentSettings.applicationName(), parent),
-    d_ptr(new SettingsPrivate(this))
+    d_ptr(new SettingsPrivate(this, false))
 {
     beginGroup(subGroup);
 }