File indexing completed on 2025-01-05 04:55:45

0001 /* -*- mode: c++; c-basic-offset:4 -*-
0002     models/keylistmodel.h
0003 
0004     This file is part of libkleopatra, the KDE keymanagement library
0005     SPDX-FileCopyrightText: 2007 Klarälvdalens Datakonsult AB
0006     SPDX-FileCopyrightText: 2021 g10 Code GmbH
0007     SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
0008 
0009     SPDX-License-Identifier: GPL-2.0-or-later
0010 */
0011 
0012 #pragma once
0013 
0014 #include "keylist.h"
0015 #include "keylistmodelinterface.h"
0016 #include "kleo_export.h"
0017 
0018 #include <Libkleo/KeyGroup>
0019 
0020 #include <QAbstractItemModel>
0021 
0022 #include <vector>
0023 
0024 namespace GpgME
0025 {
0026 class Key;
0027 }
0028 
0029 namespace Kleo
0030 {
0031 
0032 class KLEO_EXPORT DragHandler
0033 {
0034 public:
0035     virtual ~DragHandler()
0036     {
0037     }
0038     virtual QMimeData *mimeData(const QModelIndexList &indexes) const = 0;
0039     virtual Qt::ItemFlags flags(const QModelIndex &index) const = 0;
0040     virtual QStringList mimeTypes() const = 0;
0041 };
0042 
0043 class KLEO_EXPORT AbstractKeyListModel : public QAbstractItemModel, public KeyListModelInterface
0044 {
0045     Q_OBJECT
0046 public:
0047     enum ItemType {
0048         // clang-format off
0049         Keys   = 0x01,
0050         Groups = 0x02,
0051         All    = Keys | Groups,
0052         // clang-format on
0053     };
0054     Q_DECLARE_FLAGS(ItemTypes, ItemType)
0055 
0056     explicit AbstractKeyListModel(QObject *parent = nullptr);
0057     ~AbstractKeyListModel() override;
0058 
0059     static AbstractKeyListModel *createFlatKeyListModel(QObject *parent = nullptr);
0060     static AbstractKeyListModel *createHierarchicalKeyListModel(QObject *parent = nullptr);
0061 
0062     GpgME::Key key(const QModelIndex &idx) const override;
0063     std::vector<GpgME::Key> keys(const QList<QModelIndex> &indexes) const override;
0064 
0065     KeyGroup group(const QModelIndex &idx) const override;
0066 
0067     using QAbstractItemModel::index;
0068     QModelIndex index(const GpgME::Key &key) const override;
0069     QModelIndex index(const GpgME::Key &key, int col) const;
0070     QList<QModelIndex> indexes(const std::vector<GpgME::Key> &keys) const override;
0071 
0072     QModelIndex index(const KeyGroup &group) const override;
0073     QModelIndex index(const KeyGroup &group, int col) const;
0074 
0075     void setDragHandler(const std::shared_ptr<DragHandler>& dragHandler);
0076 
0077 Q_SIGNALS:
0078     void rowAboutToBeMoved(const QModelIndex &old_parent, int old_row);
0079     void rowMoved(const QModelIndex &new_parent, int new_row);
0080 
0081 public Q_SLOTS:
0082     void setKeys(const std::vector<GpgME::Key> &keys);
0083     /* Set this to set all or only secret keys from the keycache. */
0084     void useKeyCache(bool value, Kleo::KeyList::Options options);
0085     QModelIndex addKey(const GpgME::Key &key);
0086     QList<QModelIndex> addKeys(const std::vector<GpgME::Key> &keys);
0087     void removeKey(const GpgME::Key &key);
0088 
0089     void setGroups(const std::vector<KeyGroup> &groups);
0090     QModelIndex addGroup(const Kleo::KeyGroup &group);
0091     bool removeGroup(const Kleo::KeyGroup &group);
0092 
0093     void clear(Kleo::AbstractKeyListModel::ItemTypes types = All);
0094 
0095 public:
0096     int columnCount(const QModelIndex &pidx) const override;
0097     QVariant headerData(int section, Qt::Orientation o, int role = Qt::DisplayRole) const override;
0098     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0099     bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
0100 
0101     /**
0102      * defines which information is displayed in tooltips
0103      * see Kleo::Formatting::ToolTipOption
0104      */
0105     int toolTipOptions() const;
0106 
0107     void setToolTipOptions(int opts);
0108 
0109     /**
0110      * Set the keys to use for KeyListModelInterface::Remark column
0111      * to obtain remarks from this keys signature notations.
0112      * Needs at least GpgME 1.14 to work properly. Remarks are
0113      * joined by a semicolon and a space. */
0114     void setRemarkKeys(const std::vector<GpgME::Key> &remarkKeys);
0115     std::vector<GpgME::Key> remarkKeys() const;
0116 
0117     Qt::ItemFlags flags(const QModelIndex &index) const override;
0118     QStringList mimeTypes() const override;
0119     QMimeData *mimeData(const QModelIndexList &indexes) const override;
0120 
0121 protected:
0122     bool modelResetInProgress();
0123 
0124 private:
0125     QVariant data(const GpgME::Key &key, int column, int role) const;
0126     QVariant data(const KeyGroup &group, int column, int role) const;
0127 
0128     virtual GpgME::Key doMapToKey(const QModelIndex &index) const = 0;
0129     virtual QModelIndex doMapFromKey(const GpgME::Key &key, int column) const = 0;
0130     virtual QList<QModelIndex> doAddKeys(const std::vector<GpgME::Key> &keys) = 0;
0131     virtual void doRemoveKey(const GpgME::Key &key) = 0;
0132 
0133     virtual KeyGroup doMapToGroup(const QModelIndex &index) const = 0;
0134     virtual QModelIndex doMapFromGroup(const KeyGroup &group, int column) const = 0;
0135     virtual void doSetGroups(const std::vector<KeyGroup> &groups) = 0;
0136     virtual QModelIndex doAddGroup(const KeyGroup &group) = 0;
0137     virtual bool doSetGroupData(const QModelIndex &index, const KeyGroup &group) = 0;
0138     virtual bool doRemoveGroup(const KeyGroup &group) = 0;
0139 
0140     virtual void doClear(ItemTypes types) = 0;
0141 
0142 private:
0143     class Private;
0144     QScopedPointer<Private> const d;
0145 };
0146 
0147 }
0148 
0149 Q_DECLARE_OPERATORS_FOR_FLAGS(Kleo::AbstractKeyListModel::ItemTypes)