Skip to content

Commit

Permalink
Considered DE's mimeappslist, not only that of LXQt
Browse files Browse the repository at this point in the history
  • Loading branch information
tsujan committed Jul 7, 2020
1 parent 9ee3105 commit 5bbd5f6
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/qtxdg/xdgmimeappsglibbackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@

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

#include <gio/gio.h>
#include <gio/gdesktopappinfo.h>

#include <QDebug>
#include <QLoggingCategory>
#include <QMimeDatabase>
#include <QStandardPaths>

static QList<XdgDesktopFile *> GAppInfoGListToXdgDesktopQList(GList *list)
{
Expand Down Expand Up @@ -211,7 +211,7 @@ 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 LXQt.
// but we want to set the default app only for the DE (e.g., LXQt).

if (!addAssociation(mimeType, app))
return false;
Expand All @@ -220,24 +220,32 @@ bool XdgMimeAppsGLibBackend::setDefaultApp(const QString &mimeType, const XdgDes
if (gApp == nullptr)
return false;

GError *error = nullptr;
QString confDir = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation);
char *filename = g_build_filename(confDir.toUtf8().constData(), "lxqt-mimeapps.list", nullptr);
// 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, filename, G_KEY_FILE_NONE, nullptr);
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);
if (g_key_file_save_to_file(kf, filename, &error) == false) {
GError *error = nullptr;
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(filename);
g_free(mimeappsListPath);
g_object_unref(gApp);
return false;
}
g_key_file_free(kf);
g_free(filename);
g_free(mimeappsListPath);

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

0 comments on commit 5bbd5f6

Please sign in to comment.