File indexing completed on 2024-02-25 17:30:59

0001 /*
0002  * SPDX-FileCopyrightText: 2016-2019 Red Hat Inc
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  *
0006  * SPDX-FileCopyrightText: 2016-2018 Jan Grulich <jgrulich@redhat.com>
0007  */
0008 
0009 #ifndef XDG_DESKTOP_PORTAL_KDE_APPCHOOSER_DIALOG_H
0010 #define XDG_DESKTOP_PORTAL_KDE_APPCHOOSER_DIALOG_H
0011 
0012 #include "quickdialog.h"
0013 
0014 #include <QAbstractListModel>
0015 #include <QSortFilterProxyModel>
0016 #include <qmimetype.h>
0017 
0018 namespace Ui
0019 {
0020 class AppChooserDialog;
0021 }
0022 
0023 class ApplicationItem
0024 {
0025 public:
0026     enum ApplicationCategory {
0027         PreferredApplication,
0028         AllApplications,
0029     };
0030 
0031     explicit ApplicationItem(const QString &name, const QString &icon, const QString &desktopFileName);
0032 
0033     QString applicationName() const;
0034     QString applicationIcon() const;
0035     QString applicationDesktopFile() const;
0036 
0037     void setApplicationCategory(ApplicationCategory category);
0038     ApplicationCategory applicationCategory() const;
0039 
0040     bool operator==(const ApplicationItem &item) const;
0041 
0042 private:
0043     QString m_applicationName;
0044     QString m_applicationIcon;
0045     QString m_applicationDesktopFile;
0046     ApplicationCategory m_applicationCategory;
0047 };
0048 
0049 class AppFilterModel : public QSortFilterProxyModel
0050 {
0051     Q_OBJECT
0052     Q_PROPERTY(bool showOnlyPreferredApps READ showOnlyPreferredApps WRITE setShowOnlyPrefferedApps NOTIFY showOnlyPreferredAppsChanged)
0053     Q_PROPERTY(QString filter READ filter WRITE setFilter NOTIFY filterChanged)
0054 
0055 public:
0056     explicit AppFilterModel(QObject *parent = nullptr);
0057     ~AppFilterModel() override;
0058 
0059     void setShowOnlyPrefferedApps(bool show);
0060     bool showOnlyPreferredApps() const;
0061 
0062     void setDefaultApp(const QString &);
0063     QString defaultApp() const;
0064 
0065     void setFilter(const QString &text);
0066     QString filter() const;
0067 
0068 Q_SIGNALS:
0069     void showOnlyPreferredAppsChanged();
0070     void filterChanged();
0071 
0072 protected:
0073     bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override;
0074     bool lessThan(const QModelIndex &left, const QModelIndex &right) const override;
0075 
0076 private:
0077     bool m_showOnlyPreferredApps = true;
0078     QString m_filter;
0079     QString m_defaultApp;
0080 };
0081 
0082 class AppChooserData : public QObject
0083 {
0084     Q_OBJECT
0085     Q_PROPERTY(QString fileName READ fileName WRITE setFileName NOTIFY fileNameChanged)
0086     Q_PROPERTY(QString defaultApp READ defaultApp WRITE setDefaultApp NOTIFY defaultAppChanged)
0087     Q_PROPERTY(QString mimeName READ mimeName WRITE setMimeName NOTIFY mimeNameChanged)
0088     Q_PROPERTY(QString mimeDesc READ mimeDesc NOTIFY mimeDescChanged)
0089 
0090 public:
0091     AppChooserData(QObject *parent = nullptr);
0092 
0093     QString fileName() const;
0094     void setFileName(const QString &fileName);
0095     Q_SIGNAL void fileNameChanged();
0096 
0097     QString defaultApp() const;
0098     void setDefaultApp(const QString &defaultApp);
0099     Q_SIGNAL void defaultAppChanged();
0100 
0101     QString mimeName() const;
0102     void setMimeName(const QString &mimeName);
0103     Q_SIGNAL void mimeNameChanged();
0104 
0105     QString mimeDesc() const;
0106     void setMimeDesc(const QString &mimeDesc);
0107     Q_SIGNAL void mimeDescChanged();
0108 
0109     Q_SIGNAL void applicationSelected(const QString &desktopFile, bool remember = false);
0110     Q_SIGNAL void openDiscover();
0111 
0112 private:
0113     QString m_defaultApp;
0114     QString m_fileName;
0115     QString m_mimeName;
0116 };
0117 
0118 class AppModel : public QAbstractListModel
0119 {
0120     Q_OBJECT
0121     Q_PROPERTY(bool hasPreferredApps MEMBER m_hasPreferredApps NOTIFY hasPreferredAppsChanged)
0122 public:
0123     enum ItemRoles {
0124         ApplicationNameRole = Qt::UserRole + 1,
0125         ApplicationIconRole,
0126         ApplicationDesktopFileRole,
0127         ApplicationCategoryRole,
0128     };
0129 
0130     explicit AppModel(QObject *parent = nullptr);
0131     ~AppModel() override;
0132 
0133     void setPreferredApps(const QStringList &list);
0134 
0135     QVariant data(const QModelIndex &index, int role) const override;
0136     int rowCount(const QModelIndex &parent) const override;
0137     QHash<int, QByteArray> roleNames() const override;
0138 
0139 Q_SIGNALS:
0140     void hasPreferredAppsChanged();
0141 
0142 private:
0143     void loadApplications();
0144 
0145     QList<ApplicationItem> m_list;
0146     QHash<QString, QString> m_noDisplayAliasesFor;
0147     bool m_hasPreferredApps = false;
0148 };
0149 
0150 class AppChooserDialog : public QuickDialog
0151 {
0152     Q_OBJECT
0153 public:
0154     explicit AppChooserDialog(const QStringList &choices,
0155                               const QString &defaultApp,
0156                               const QString &fileName,
0157                               const QString &mimeName = QString(),
0158                               QObject *parent = nullptr);
0159     void updateChoices(const QStringList &choices);
0160 
0161     QString selectedApplication() const;
0162 private Q_SLOTS:
0163     void onApplicationSelected(const QString &desktopFile, const bool remember = false);
0164     void onOpenDiscover();
0165 
0166 private:
0167     AppModel *const m_model;
0168     QString m_selectedApplication;
0169     AppChooserData *const m_appChooserData;
0170 };
0171 
0172 #endif // XDG_DESKTOP_PORTAL_KDE_APPCHOOSER_DIALOG_H