| 1 | /* |
| 2 | * Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com) |
| 3 | * |
| 4 | * This program is free software: you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation, either version 3 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | #ifndef ENVIRONMENTFILEASSOCIATIONWIDGET_H |
| 18 | #define ENVIRONMENTFILEASSOCIATIONWIDGET_H |
| 19 | |
| 20 | #include <QAbstractListModel> |
| 21 | #include <QWidget> |
| 22 | #include "settingswidget.h" |
| 23 | |
| 24 | namespace Ui { |
| 25 | class EnvironmentFileAssociationWidget; |
| 26 | } |
| 27 | struct FileAssociationItem { |
| 28 | QString name; |
| 29 | QString suffix; |
| 30 | int icon; |
| 31 | bool selected; |
| 32 | bool defaultSelected; |
| 33 | }; |
| 34 | using PFileAssociationItem = std::shared_ptr<FileAssociationItem>; |
| 35 | |
| 36 | class FileAssociationModel:public QAbstractListModel { |
| 37 | Q_OBJECT |
| 38 | public: |
| 39 | explicit FileAssociationModel(QObject* parent = nullptr); |
| 40 | void addItem(const QString& name, const QString& suffix, int icon); |
| 41 | void updateAssociationStates(); |
| 42 | void saveAssociations(); |
| 43 | signals: |
| 44 | void associationChanged(); |
| 45 | private: |
| 46 | bool checkAssociation(const QString& extension, |
| 47 | const QString& filetype, |
| 48 | const QString& verb, |
| 49 | const QString& serverApp); |
| 50 | bool registerAssociation(const QString& extension, |
| 51 | const QString& filetype); |
| 52 | bool unregisterAssociation(const QString& extension); |
| 53 | bool unregisterFileType(const QString& fileType); |
| 54 | bool registerFileType(const QString& filetype, |
| 55 | const QString& description, |
| 56 | const QString& verb, |
| 57 | const QString& serverApp, |
| 58 | int icon); |
| 59 | private: |
| 60 | QList<PFileAssociationItem> mItems; |
| 61 | |
| 62 | |
| 63 | // QAbstractItemModel interface |
| 64 | public: |
| 65 | int rowCount(const QModelIndex &parent) const override; |
| 66 | QVariant data(const QModelIndex &index, int role) const override; |
| 67 | bool setData(const QModelIndex &index, const QVariant &value, int role) override; |
| 68 | Qt::ItemFlags flags(const QModelIndex &index) const override; |
| 69 | }; |
| 70 | |
| 71 | class EnvironmentFileAssociationWidget : public SettingsWidget |
| 72 | { |
| 73 | Q_OBJECT |
| 74 | |
| 75 | public: |
| 76 | explicit EnvironmentFileAssociationWidget(const QString& name, const QString& group, QWidget *parent = nullptr); |
| 77 | ~EnvironmentFileAssociationWidget(); |
| 78 | |
| 79 | private: |
| 80 | Ui::EnvironmentFileAssociationWidget *ui; |
| 81 | FileAssociationModel mModel; |
| 82 | |
| 83 | // SettingsWidget interface |
| 84 | protected: |
| 85 | void doLoad() override; |
| 86 | void doSave() override; |
| 87 | }; |
| 88 | #endif // ENVIRONMENTFILEASSOCIATIONWIDGET_H |
| 89 | |