1 /* -*- mode: c++; c-basic-offset:4 -*-
2     models/keylistmodel.h
3 
4     This file is part of libkleopatra, the KDE keymanagement library
5     SPDX-FileCopyrightText: 2007 Klarälvdalens Datakonsult AB
6     SPDX-FileCopyrightText: 2021 g10 Code GmbH
7     SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
8 
9     SPDX-License-Identifier: GPL-2.0-or-later
10 */
11 
12 #pragma once
13 
14 #include <QAbstractItemModel>
15 
16 #include "kleo_export.h"
17 
18 #include "keylist.h"
19 #include "keylistmodelinterface.h"
20 
21 #include <vector>
22 
23 namespace GpgME
24 {
25 class Key;
26 }
27 
28 namespace Kleo
29 {
30 
31 class KLEO_EXPORT AbstractKeyListModel : public QAbstractItemModel
32                                        , public KeyListModelInterface
33 {
34     Q_OBJECT
35 public:
36     enum ItemType {
37         Keys = 0x01,
38         Groups = 0x02,
39         All = Keys | Groups
40     };
41     Q_DECLARE_FLAGS(ItemTypes, ItemType)
42 
43     explicit AbstractKeyListModel(QObject *parent = nullptr);
44     ~AbstractKeyListModel() override;
45 
46     static AbstractKeyListModel *createFlatKeyListModel(QObject *parent = nullptr);
47     static AbstractKeyListModel *createHierarchicalKeyListModel(QObject *parent = nullptr);
48 
49     GpgME::Key key(const QModelIndex &idx) const override;
50     std::vector<GpgME::Key> keys(const QList<QModelIndex> &indexes) const override;
51 
52     KeyGroup group(const QModelIndex &idx) const override;
53 
54     using QAbstractItemModel::index;
55     QModelIndex index(const GpgME::Key &key) const override;
56     QModelIndex index(const GpgME::Key &key, int col) const;
57     QList<QModelIndex> indexes(const std::vector<GpgME::Key> &keys) const override;
58 
59     QModelIndex index(const KeyGroup &group) const override;
60     QModelIndex index(const KeyGroup &group, int col) const;
61 
62 Q_SIGNALS:
63     void rowAboutToBeMoved(const QModelIndex &old_parent, int old_row);
64     void rowMoved(const QModelIndex &new_parent, int new_row);
65 
66 public Q_SLOTS:
67     void setKeys(const std::vector<GpgME::Key> &keys);
68     /* Set this to set all or only secret keys from the keycache. */
69     void useKeyCache(bool value, Kleo::KeyList::Options options);
70     QModelIndex addKey(const GpgME::Key &key);
71     QList<QModelIndex> addKeys(const std::vector<GpgME::Key> &keys);
72     void removeKey(const GpgME::Key &key);
73 
74     void setGroups(const std::vector<KeyGroup> &groups);
75     QModelIndex addGroup(const Kleo::KeyGroup &group);
76     bool removeGroup(const Kleo::KeyGroup &group);
77 
78     void clear(Kleo::AbstractKeyListModel::ItemTypes types = All);
79 
80 public:
81     int columnCount(const QModelIndex &pidx) const override;
82     QVariant headerData(int section, Qt::Orientation o, int role = Qt::DisplayRole) const override;
83     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
84     bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
85 
86     /**
87      * defines which information is displayed in tooltips
88      * see Kleo::Formatting::ToolTipOption
89      */
90     int toolTipOptions() const;
91 
92     void setToolTipOptions(int opts);
93 
94     /**
95      * Set the keys to use for KeyListModelInterface::Remark column
96      * to obtain remarks from this keys signature notations.
97      * Needs at least GpgME 1.14 to work properly. Remarks are
98      * joined by a semicolon and a space. */
99     void setRemarkKeys(const std::vector<GpgME::Key> &remarkKeys);
100     std::vector<GpgME::Key> remarkKeys() const;
101 
102 protected:
103     bool modelResetInProgress();
104 
105 private:
106     QVariant data(const GpgME::Key &key, int column, int role) const;
107     QVariant data(const KeyGroup &group, int column, int role) const;
108 
109     virtual GpgME::Key doMapToKey(const QModelIndex &index) const = 0;
110     virtual QModelIndex doMapFromKey(const GpgME::Key &key, int column) const = 0;
111     virtual QList<QModelIndex> doAddKeys(const std::vector<GpgME::Key> &keys) = 0;
112     virtual void doRemoveKey(const GpgME::Key &key) = 0;
113 
114     virtual KeyGroup doMapToGroup(const QModelIndex &index) const = 0;
115     virtual QModelIndex doMapFromGroup(const KeyGroup &group, int column) const = 0;
116     virtual void doSetGroups(const std::vector<KeyGroup> &groups) = 0;
117     virtual QModelIndex doAddGroup(const KeyGroup &group) = 0;
118     virtual bool doSetGroupData(const QModelIndex &index, const KeyGroup &group) = 0;
119     virtual bool doRemoveGroup(const KeyGroup &group) = 0;
120 
121     virtual void doClear(ItemTypes types) = 0;
122 
123 private:
124     class Private;
125     QScopedPointer<Private> const d;
126 };
127 
128 }
129 
130 Q_DECLARE_OPERATORS_FOR_FLAGS(Kleo::AbstractKeyListModel::ItemTypes)
131 
132