Skip to content

Commit

Permalink
Fixed setting of default app in Properties dialog
Browse files Browse the repository at this point in the history
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 lxqt/libqtxdg#219.
  • Loading branch information
tsujan committed Jul 9, 2020
1 parent 928a85a commit 6c5884c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/appchooserdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "appchooserdialog.h"
#include "ui_app-chooser-dialog.h"
#include "utilities.h"
#include <QPushButton>
#include <gio/gdesktopappinfo.h>
#include <glib/gstdio.h>
Expand Down Expand Up @@ -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_);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/filepropsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions src/utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <QList>
#include <QStringBuilder>
#include <QMessageBox>
#include <QStandardPaths>
#include "fileoperation.h"
#include <QEventLoop>

Expand Down Expand Up @@ -193,6 +194,38 @@ bool renameFile(std::shared_ptr<const Fm::FileInfo> file, QWidget* parent) {
return true;
}

void setDefaultAppForType(const Fm::GAppInfoPtr app, std::shared_ptr<const Fm::MimeType> 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<QByteArray> 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;
Expand Down
2 changes: 2 additions & 0 deletions src/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ LIBFM_QT_API bool changeFileName(const Fm::FilePath& path, const QString& newNam

LIBFM_QT_API bool renameFile(std::shared_ptr<const Fm::FileInfo> file, QWidget* parent = nullptr);

LIBFM_QT_API void setDefaultAppForType(const Fm::GAppInfoPtr app, std::shared_ptr<const Fm::MimeType> mimeType);

enum CreateFileType {
CreateNewFolder,
CreateNewTextFile,
Expand Down

0 comments on commit 6c5884c

Please sign in to comment.