Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed setting of default apps with GLib backend #219

Merged
merged 1 commit into from
Jul 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions src/qtxdg/xdgmimeappsglibbackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "qtxdglogging.h"
#include "xdgdesktopfile.h"
#include "xdgdirs.h"

#include <gio/gio.h>
#include <gio/gdesktopappinfo.h>
Expand Down Expand Up @@ -209,20 +210,42 @@ XdgDesktopFile *XdgMimeAppsGLibBackend::defaultApp(const QString &mimeType)

bool XdgMimeAppsGLibBackend::setDefaultApp(const QString &mimeType, const XdgDesktopFile &app)
{
// 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 DE (e.g., LXQt).

if (!addAssociation(mimeType, app))
return false;

GDesktopAppInfo *gApp = XdgDesktopFileToGDesktopAppinfo(app);
if (gApp == nullptr)
return false;

// 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;
}
char *mimeappsListPath = g_build_filename(XdgDirs::configHome(true).toUtf8().constData(),
mimeappsList.constData(),
nullptr);

const char *desktop_id = g_app_info_get_id(G_APP_INFO(gApp));
GKeyFile *kf = g_key_file_new();
g_key_file_load_from_file(kf, mimeappsListPath, G_KEY_FILE_NONE, nullptr);
g_key_file_set_string(kf, "Default Applications", mimeType.toUtf8().constData(), desktop_id);
GError *error = nullptr;
if (g_app_info_set_as_default_for_type(G_APP_INFO(gApp),
mimeType.toUtf8().constData(), &error) == FALSE) {
if (g_key_file_save_to_file(kf, mimeappsListPath, &error) == false) {
qCWarning(QtXdgMimeAppsGLib, "Failed to set '%s' as the default for '%s'. %s",
g_desktop_app_info_get_filename(gApp), qPrintable(mimeType), error->message);

g_error_free(error);
g_key_file_free(kf);
g_free(mimeappsListPath);
g_object_unref(gApp);
return false;
}
g_key_file_free(kf);
g_free(mimeappsListPath);

qCDebug(QtXdgMimeAppsGLib, "Set '%s' as the default for '%s'",
g_desktop_app_info_get_filename(gApp), qPrintable(mimeType));
Expand Down