From 51eb065399b4e8a6b1090967367c7b221e5b621a Mon Sep 17 00:00:00 2001 From: kate Date: Wed, 26 Jan 2022 13:18:43 +0800 Subject: [PATCH] fix: default app settings --- CMakeLists.txt | 2 ++ README.md | 2 +- debian/control | 1 + mimetype/mimeappmanager.cpp | 45 ++++++++++++++++++++++++------------- 4 files changed, 34 insertions(+), 16 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ab9a8d4..8538766 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,7 @@ find_package(Qt5 COMPONENTS Core DBus Quick LinguistTools REQUIRED) find_package(KF5KIO) find_package(KF5Solid) find_package(KF5WindowSystem) +find_package(KF5Config) qt5_add_dbus_adaptor(DBUS_SOURCES com.cutefish.FileManager.xml @@ -84,6 +85,7 @@ target_link_libraries(cutefish-filemanager KF5::KIOWidgets KF5::Solid KF5::WindowSystem + KF5::ConfigCore # FishUI ) diff --git a/README.md b/README.md index 9f04089..1118b52 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ sudo mk-build-deps -i -t "apt-get --yes" -r ### Debian ``` -sudo apt install build-essential cmake extra-cmake-modules libkf5kio-dev libkf5solid-dev libkf5windowsystem-dev qtbase5-dev qtbase5-private-dev qtdeclarative5-dev qtquickcontrols2-5-dev qttools5-dev qttools5-dev-tools +sudo apt install build-essential cmake extra-cmake-modules libkf5kio-dev libkf5solid-dev libkf5windowsystem-dev libkf5config-dev qtbase5-dev qtbase5-private-dev qtdeclarative5-dev qtquickcontrols2-5-dev qttools5-dev qttools5-dev-tools ``` ### ArchLinux diff --git a/debian/control b/debian/control index bb8d374..7c8e87f 100644 --- a/debian/control +++ b/debian/control @@ -8,6 +8,7 @@ Build-Depends: cmake, libkf5kio-dev, libkf5solid-dev, libkf5windowsystem-dev, + libkf5config-dev, qtbase5-dev, qtbase5-private-dev, qtdeclarative5-dev, diff --git a/mimetype/mimeappmanager.cpp b/mimetype/mimeappmanager.cpp index 890d3b4..b317d61 100644 --- a/mimetype/mimeappmanager.cpp +++ b/mimetype/mimeappmanager.cpp @@ -33,6 +33,10 @@ #include #include +#include +#include +#include + static MimeAppManager *SELF = nullptr; MimeAppManager *MimeAppManager::self() @@ -292,17 +296,23 @@ bool MimeAppManager::setDefaultAppForType(const QString &mimeType, const QString if (QFile::exists(desktop)) { QFileInfo info(desktop); desktop = info.completeBaseName(); + } else { + return false; } -// QSettings settings(mimeappsFile, QSettings::IniFormat); -// settings.setIniCodec("UTF-8"); + KSharedConfig::Ptr profile = KSharedConfig::openConfig(QStringLiteral("mimeapps.list"), + KConfig::NoGlobals, + QStandardPaths::GenericConfigLocation); + KConfigGroup defaultApp(profile, "Default Applications"); + defaultApp.writeXdgListEntry(mimeType, {desktop}); -// if (!settings.isWritable()) -// return false; + KConfigGroup addedApps(profile, "Added Associations"); + QStringList apps = addedApps.readXdgListEntry(mimeType); + apps.removeAll(desktop); + apps.prepend(desktop); // make it the preferred app + addedApps.writeXdgListEntry(mimeType, apps); -// settings.beginGroup("Default Applications"); -// settings.setValue(mimeType, desktop); -// settings.sync(); + profile->sync(); return true; } @@ -311,7 +321,6 @@ bool MimeAppManager::setDefaultAppForFile(const QString &filePath, const QString { // ref: https://specifications.freedesktop.org/mime-apps-spec/1.0.1/ar01s03.html - QString mimeappsFile = mimeAppsListFilePath(); QMimeType mimeType; QString value = desktop; @@ -323,17 +332,23 @@ bool MimeAppManager::setDefaultAppForFile(const QString &filePath, const QString if (QFile::exists(value)) { QFileInfo info(value); value = info.fileName(); + } else { + return false; } - QSettings settings(mimeappsFile, QSettings::IniFormat); - settings.setIniCodec("UTF-8"); + KSharedConfig::Ptr profile = KSharedConfig::openConfig(QStringLiteral("mimeapps.list"), + KConfig::NoGlobals, + QStandardPaths::GenericConfigLocation); + KConfigGroup defaultApp(profile, "Default Applications"); + defaultApp.writeXdgListEntry(mimeType.name(), {desktop}); - if (!settings.isWritable()) - return false; + KConfigGroup addedApps(profile, "Added Associations"); + QStringList apps = addedApps.readXdgListEntry(mimeType.name()); + apps.removeAll(desktop); + apps.prepend(desktop); // make it the preferred app + addedApps.writeXdgListEntry(mimeType.name(), apps); - settings.beginGroup(QStringLiteral("Default Applications")); // Added Associations - settings.setValue(mimeType.name(), value); - settings.sync(); + profile->sync(); return true; }