File indexing completed on 2024-11-10 04:50:17

0001 /*
0002   SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
0003   SPDX-FileContributor: Tobias Koenig <tokoe@kdab.com>
0004 
0005   SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
0006 
0007   SPDX-License-Identifier: LGPL-2.0-or-later
0008 */
0009 
0010 #pragma once
0011 
0012 #include "mailcommon_export.h"
0013 #include <QAbstractItemModel>
0014 #include <QKeySequence>
0015 namespace MailCommon
0016 {
0017 class SnippetItem;
0018 /**
0019  * @brief The SnippetsInfo struct
0020  * @author Laurent Montel <montel@kde.org>
0021  */
0022 struct MAILCOMMON_EXPORT SnippetsInfo {
0023     QString newName;
0024     QKeySequence keySequence;
0025     QString text;
0026     QString keyword;
0027     QString subject;
0028     QString to;
0029     QString cc;
0030     QString bcc;
0031     QString attachment;
0032 };
0033 
0034 /**
0035  * @brief The SnippetsModel class
0036  * @author Laurent Montel <montel@kde.org>
0037  */
0038 class MAILCOMMON_EXPORT SnippetsModel : public QAbstractItemModel
0039 {
0040     Q_OBJECT
0041 public:
0042     enum Role {
0043         IsGroupRole = Qt::UserRole + 1, ///< Returns whether the index represents a group
0044         NameRole, ///< The name of a snippet or group
0045         TextRole, ///< The text of a snippet
0046         KeySequenceRole, ///< The key sequence to activate a snippet
0047         KeywordRole, ///< The keyword which will replace by snippet
0048         SubjectRole, ///< The subject of a snippet
0049         ToRole, ///< The To of a snippet
0050         CcRole, ///< The Cc of a snippet
0051         BccRole, ///< The Cc of a snippet
0052         AttachmentRole, ///< The Attachment of a snippet
0053     };
0054 
0055     static SnippetsModel *instance();
0056 
0057     explicit SnippetsModel(QObject *parent = nullptr);
0058     ~SnippetsModel() override;
0059 
0060     bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
0061 
0062     [[nodiscard]] QVariant data(const QModelIndex &index, int role) const override;
0063 
0064     [[nodiscard]] Qt::ItemFlags flags(const QModelIndex &index) const override;
0065 
0066     [[nodiscard]] QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
0067 
0068     [[nodiscard]] QModelIndex parent(const QModelIndex &index) const override;
0069 
0070     [[nodiscard]] int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0071 
0072     [[nodiscard]] int columnCount(const QModelIndex &parent = QModelIndex()) const override;
0073 
0074     [[nodiscard]] QStringList mimeTypes() const override;
0075 
0076     [[nodiscard]] QMimeData *mimeData(const QModelIndexList &indexes) const override;
0077 
0078     [[nodiscard]] bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override;
0079 
0080     [[nodiscard]] Qt::DropActions supportedDropActions() const override;
0081 
0082     void save(const QString &filename = QString());
0083     void load(const QString &filename = QString());
0084 
0085     [[nodiscard]] QMap<QString, QString> savedVariables() const;
0086     void setSavedVariables(const QMap<QString, QString> &savedVariables);
0087 
0088     [[nodiscard]] QList<SnippetsInfo> snippetsInfo() const;
0089 
0090 protected:
0091     bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
0092 
0093     bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
0094 
0095 Q_SIGNALS:
0096     void dndDone();
0097     void addNewDndSnippset(const QString &);
0098     void updateActionCollection(const QString &oldName,
0099                                 const QString &newName,
0100                                 const QKeySequence &keySequence,
0101                                 const QString &text,
0102                                 const QString &subject,
0103                                 const QString &to,
0104                                 const QString &cc,
0105                                 const QString &bcc,
0106                                 const QString &attachment);
0107 
0108 private:
0109     MAILCOMMON_NO_EXPORT QModelIndex createGroup(const QString &groupName);
0110     MAILCOMMON_NO_EXPORT void createSnippet(const QModelIndex &groupIndex,
0111                                             const QString &snippetName,
0112                                             const QString &snippetText,
0113                                             const QString &snippetKeySequence,
0114                                             const QString &snippetKeyword,
0115                                             const QString &snippetSubject,
0116                                             const QString &to,
0117                                             const QString &cc,
0118                                             const QString &bcc,
0119                                             const QString &attachment);
0120     SnippetItem *mRootItem = nullptr;
0121     QMap<QString, QString> mSavedVariables;
0122 };
0123 }
0124 Q_DECLARE_TYPEINFO(MailCommon::SnippetsInfo, Q_RELOCATABLE_TYPE);