From ff1c006041341e394bca0732f8cbce598ff57efa Mon Sep 17 00:00:00 2001 From: reionwong Date: Tue, 14 Sep 2021 01:20:10 +0800 Subject: [PATCH] Optimize open default apps --- mimetype/mimeappmanager.cpp | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/mimetype/mimeappmanager.cpp b/mimetype/mimeappmanager.cpp index ce1a80a..0a46e7d 100644 --- a/mimetype/mimeappmanager.cpp +++ b/mimetype/mimeappmanager.cpp @@ -252,14 +252,22 @@ QString MimeAppManager::getDefaultAppByMimeType(const QMimeType &mimeType) settings.beginGroup("Default Applications"); // TODO: User applications directory? - if (settings.contains(mimeType.name())) - return QString("/usr/share/applications/%1").arg(settings.value(mimeType.name()).toString()); + if (settings.contains(mimeType.name())) { + const QString desktopFile = QString("/usr/share/applications/%1").arg(settings.value(mimeType.name()).toString()); + if (QFile::exists(desktopFile)) { + return desktopFile; + } + } settings.endGroup(); settings.beginGroup("Added Associations"); - if (settings.contains(mimeType.name())) - return QString("/usr/share/applications/%1").arg(settings.value(mimeType.name()).toString()); + if (settings.contains(mimeType.name())) { + QString desktopFile = QString("/usr/share/applications/%1").arg(settings.value(mimeType.name()).toString()); + if (QFile::exists(desktopFile)) { + return desktopFile; + } + } return QString(); } @@ -314,15 +322,15 @@ bool MimeAppManager::setDefaultAppForFile(const QString &filePath, const QString value = info.fileName(); } -// QSettings settings(mimeappsFile, QSettings::IniFormat); -// settings.setIniCodec("UTF-8"); + QSettings settings(mimeappsFile, QSettings::IniFormat); + settings.setIniCodec("UTF-8"); -// if (!settings.isWritable()) -// return false; + if (!settings.isWritable()) + return false; -// settings.beginGroup("Default Applications"); // Added Associations -// settings.setValue(mimeType.name(), value); -// settings.sync(); + settings.beginGroup("Default Applications"); // Added Associations + settings.setValue(mimeType.name(), value); + settings.sync(); return true; }