From 62114bbda545d71434c92ee9bd645e20e25898a4 Mon Sep 17 00:00:00 2001 From: magnus Date: Sun, 2 Jan 2022 23:57:00 -0600 Subject: [PATCH] Fix bug where it wouldn't display description of mimetypes --- selectdefaultapplication.cpp | 35 +++++++++++++++++++++++++++++------ selectdefaultapplication.h | 1 + 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/selectdefaultapplication.cpp b/selectdefaultapplication.cpp index f621ca9..d8f3070 100644 --- a/selectdefaultapplication.cpp +++ b/selectdefaultapplication.cpp @@ -226,8 +226,8 @@ void SelectDefaultApplication::onApplicationSelectedLogic(bool allowEnabled) } void SelectDefaultApplication::addToMimetypeList(QListWidget *list, const QString &mimetypeName, const bool selected) { - QString name = mimetypeName; - QListWidgetItem *item = new QListWidgetItem(name); + QString description = mimetypeDescription(mimetypeName); + QListWidgetItem *item = new QListWidgetItem(description); item->setData(Qt::UserRole, mimetypeName); item->setIcon(m_mimeTypeIcons[mimetypeName]); list->addItem(item); @@ -591,19 +591,42 @@ bool SelectDefaultApplication::applicationHasAnyCorrectMimetype(const QString &a return false; } +const char *X_SCHEME_HANDLER = "x-scheme-handler/"; // Returns the value of m_mimeDb.mimeTypeForName(name) but // mimeTypeForName(application/x-pkcs12) always returns application/x-pkcs12 instead of application/pkcs12 -// If +// If starts with x-scheme-handler, instead just returns the argument const QString SelectDefaultApplication::wrapperMimeTypeForName(const QString &name) { + if (name.startsWith(X_SCHEME_HANDLER)) { + // x-scheme-handler is not a valid mimetype for a file, but we do want to be able to set applications as the default handlers for it. + // Assumes all x-scheme-handler/* is valid + return name; + } const QMimeType mimetype = m_mimeDb.mimeTypeForName(name); QString mimetypeName = mimetype.name(); // There appears to be a bug in Qt https://bugreports.qt.io/browse/QTBUG-99509, hack around it if (mimetypeName == "application/pkcs12") { mimetypeName = "application/x-pkcs12"; - } else if (name.startsWith("x-scheme-handler/")) { + } + return mimetypeName; +} + +const QString SelectDefaultApplication::mimetypeDescription(QString name) { + if (name.startsWith(X_SCHEME_HANDLER)) { // x-scheme-handler is not a valid mimetype for a file, but we do want to be able to set applications as the default handlers for it. // Assumes all x-scheme-handler/* is valid - mimetypeName = name; + return "Handles " + name.mid(strlen(X_SCHEME_HANDLER)) + ":// URIs\n" + name; } - return mimetypeName; + // There appears to be a bug in Qt https://bugreports.qt.io/browse/QTBUG-99509, hack around it + if (name == "application/pkcs12") { + name = "application/x-pkcs12"; + } + const QMimeType mimetype = m_mimeDb.mimeTypeForName(name); + QString desc = mimetype.filterString().trimmed(); + if (desc.isEmpty()) { + desc = mimetype.comment().trimmed(); + } + if (!desc.isEmpty()) { + desc += '\n'; + } + return desc + name; } diff --git a/selectdefaultapplication.h b/selectdefaultapplication.h index 59d995a..08e06ad 100644 --- a/selectdefaultapplication.h +++ b/selectdefaultapplication.h @@ -41,6 +41,7 @@ private slots: bool applicationHasAnyCorrectMimetype(const QString &appName); void onApplicationSelectedLogic(bool allowEnable); const QString wrapperMimeTypeForName(const QString &name); + const QString mimetypeDescription(QString name); // Hashtable of application names to hashtables of mimetypes to .desktop file entries QHash > m_apps;