File indexing completed on 2024-05-12 05:35:38

0001 /*
0002     SPDX-FileCopyrightText: 2020 Tobias Fella <fella@posteo.de>
0003     SPDX-FileCopyrightText: 2022 Méven Car <meven@kde.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #pragma once
0009 
0010 #include <KLocalizedString>
0011 #include <QString>
0012 #include <QVariant>
0013 
0014 #include "applicationmodel.h"
0015 
0016 class ApplicationModel;
0017 
0018 class PairQml
0019 {
0020     Q_GADGET
0021 
0022     Q_PROPERTY(QVariant first MEMBER first CONSTANT FINAL)
0023     Q_PROPERTY(QVariant second MEMBER second CONSTANT FINAL)
0024 
0025 public:
0026     PairQml() = default;
0027     PairQml(QVariant f, QVariant s)
0028         : first(f)
0029         , second(s)
0030     {
0031     }
0032 
0033     QVariant first;
0034     QVariant second;
0035 };
0036 
0037 class ComponentChooser : public QObject
0038 {
0039     Q_OBJECT
0040     Q_PROPERTY(ApplicationModel *applications MEMBER m_model CONSTANT)
0041     Q_PROPERTY(int index MEMBER m_index NOTIFY indexChanged)
0042     Q_PROPERTY(bool isDefaults READ isDefaults NOTIFY isDefaultsChanged)
0043 
0044     Q_PROPERTY(QStringList unsupportedMimeTypes READ unsupportedMimeTypes NOTIFY modelChanged)
0045     Q_PROPERTY(QList<PairQml> mimeTypesNotAssociated READ mimeTypesNotAssociated NOTIFY modelChanged)
0046     Q_PROPERTY(bool isSaveNeeded READ isSaveNeeded NOTIFY indexChanged)
0047 
0048 public:
0049     ComponentChooser(QObject *parent, const QString &mimeType, const QString &type, const QString &defaultApplication, const QString &dialogText);
0050 
0051     virtual void load();
0052     virtual void save();
0053 
0054     void defaults();
0055     bool isDefaults() const;
0056     bool isSaveNeeded() const;
0057 
0058     Q_INVOKABLE QString currentStorageId() const;
0059     Q_INVOKABLE QString applicationName() const;
0060     Q_INVOKABLE QString applicationIcon() const;
0061 
0062     /// return the mimeTypes not supported by the currently selected application
0063     QStringList unsupportedMimeTypes() const;
0064 
0065     /// Mimetypes associated with another application than the current
0066     QList<PairQml> mimeTypesNotAssociated() const;
0067 
0068     Q_INVOKABLE void select(int index);
0069     Q_INVOKABLE void saveAssociationUnsuportedMimeTypes();
0070     Q_INVOKABLE void saveMimeTypesNotAssociated();
0071 
0072     void saveMimeTypeAssociations(const QString &storageId, const QStringList &mimes, bool forceUnsupportedMimeType = false);
0073 
0074     virtual QStringList mimeTypes() const;
0075 
0076     static void forceReloadServiceCache();
0077     static bool serviceSupportsMimeType(KService::Ptr service, const QString &mimeType);
0078 
0079 Q_SIGNALS:
0080     void indexChanged();
0081     void isDefaultsChanged();
0082     void isSaveNeededChanged();
0083     void modelChanged();
0084 
0085 public slots:
0086     void onSaved();
0087 
0088 protected:
0089     ApplicationModel *m_model;
0090 
0091     int m_index = -1;
0092     QString m_mimeType;
0093     QString m_applicationCategory;
0094     QString m_defaultApplication;
0095     QString m_currentApplication;
0096     QString m_dialogText;
0097 
0098 private:
0099     void reBuildCacheAndLoad();
0100 };