From 6c5884c7baeb4550b2e0af743c1270cabe8a0fe9 Mon Sep 17 00:00:00 2001 From: Tsu Jan Date: Thu, 9 Jul 2020 17:35:27 +0430 Subject: [PATCH] Fixed setting of default app in Properties dialog MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GLib has a weird problem. On the one hand, `g_app_info_set_as_default_for_type()` writes to `~/.config/mimeapps.list`. On the other hand, the DE-specific list — e.g., `~/.config/lxqt-mimeapps.list` in LXQt — has priority over it and GLib respects that. Therefore, if the latter exists and contains some default apps, `g_app_info_set_as_default_for_type()` could not change them. This patch solves the problem by adding a utility function that sets the default app directly inside the DE-specific list. Also, see https://github.com/lxqt/libqtxdg/pull/219. --- src/appchooserdialog.cpp | 3 ++- src/filepropsdialog.cpp | 2 +- src/utilities.cpp | 33 +++++++++++++++++++++++++++++++++ src/utilities.h | 2 ++ 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/appchooserdialog.cpp b/src/appchooserdialog.cpp index 554b601f..14d9df59 100644 --- a/src/appchooserdialog.cpp +++ b/src/appchooserdialog.cpp @@ -20,6 +20,7 @@ #include "appchooserdialog.h" #include "ui_app-chooser-dialog.h" +#include "utilities.h" #include #include #include @@ -245,7 +246,7 @@ void AppChooserDialog::accept() { #endif /* if need to set default */ if(ui->setDefault->isChecked()) { - g_app_info_set_as_default_for_type(selectedApp_.get(), mimeType_->name(), nullptr); + setDefaultAppForType(selectedApp_, mimeType_); } } } diff --git a/src/filepropsdialog.cpp b/src/filepropsdialog.cpp index 8aae9976..95ff588f 100644 --- a/src/filepropsdialog.cpp +++ b/src/filepropsdialog.cpp @@ -439,7 +439,7 @@ void FilePropsDialog::accept() { // applications if(mimeType && ui->openWith->isChanged()) { auto currentApp = ui->openWith->selectedApp(); - g_app_info_set_as_default_for_type(currentApp.get(), mimeType->name(), nullptr); + setDefaultAppForType(currentApp, mimeType); } // check if chown or chmod is needed diff --git a/src/utilities.cpp b/src/utilities.cpp index bf27ffd6..bb7487d9 100644 --- a/src/utilities.cpp +++ b/src/utilities.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include "fileoperation.h" #include @@ -193,6 +194,38 @@ bool renameFile(std::shared_ptr file, QWidget* parent) { return true; } +void setDefaultAppForType(const Fm::GAppInfoPtr app, std::shared_ptr mimeType) +{ + // NOTE: "g_app_info_set_as_default_for_type()" writes to "~/.config/mimeapps.list" + // but we want to set the default app only for the current DE (e.g., LXQt). + // More importantly, if the DE-specific list already exists and contains some + // default apps, it will have priority over "~/.config/mimeapps.list" and so, + // "g_app_info_set_as_default_for_type()" could not change those apps. + + if(app == nullptr || mimeType == nullptr) { + return; + } + + // first find the DE's mimeapps list file + QByteArray mimeappsList = "mimeapps.list"; + QList desktopsList = qgetenv("XDG_CURRENT_DESKTOP").toLower().split(':'); + if(!desktopsList.isEmpty()) { + mimeappsList = desktopsList.at(0) + "-" + mimeappsList; + } + QString configDir = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation); + auto mimeappsListPath = CStrPtr(g_build_filename(configDir.toUtf8().constData(), + mimeappsList.constData(), + nullptr)); + + // set the default app in the DE's mimeapps list + const char* desktop_id = g_app_info_get_id(app.get()); + GKeyFile* kf = g_key_file_new(); + g_key_file_load_from_file(kf, mimeappsListPath.get(), G_KEY_FILE_NONE, nullptr); + g_key_file_set_string(kf, "Default Applications", mimeType->name(), desktop_id); + g_key_file_save_to_file(kf, mimeappsListPath.get(), nullptr); + g_key_file_free(kf); +} + // templateFile is a file path used as a template of the new file. void createFileOrFolder(CreateFileType type, FilePath parentDir, const TemplateItem* templ, QWidget* parent) { QString defaultNewName; diff --git a/src/utilities.h b/src/utilities.h index 2a8cdf38..ed7f6d94 100644 --- a/src/utilities.h +++ b/src/utilities.h @@ -55,6 +55,8 @@ LIBFM_QT_API bool changeFileName(const Fm::FilePath& path, const QString& newNam LIBFM_QT_API bool renameFile(std::shared_ptr file, QWidget* parent = nullptr); +LIBFM_QT_API void setDefaultAppForType(const Fm::GAppInfoPtr app, std::shared_ptr mimeType); + enum CreateFileType { CreateNewFolder, CreateNewTextFile,