File indexing completed on 2024-05-19 05:19:21

0001 /*
0002     This file is part of KJots.
0003 
0004     SPDX-FileCopyrightText: 2009 Stephen Kelly <steveire@gmail.com>
0005                   2020 Igor Poboiko <igor.poboiko@gmail.com>
0006 
0007     SPDX-License-Identifier: LGPL-2.0-or-later
0008 */
0009 
0010 #ifndef KJOTSMODEL_H
0011 #define KJOTSMODEL_H
0012 
0013 #include <akonadi_version.h>
0014 #include <QtGlobal> // for QT_VERSION_CHECK
0015 #if AKONADI_VERSION >= QT_VERSION_CHECK(5, 18, 41)
0016 #include <Akonadi/EntityTreeModel>
0017 #else
0018 #include <AkonadiCore/EntityTreeModel>
0019 #endif
0020 
0021 class QTextDocument;
0022 
0023 namespace Akonadi
0024 {
0025 class ChangeRecorder;
0026 }
0027 
0028 /**
0029  * A wrapper QObject making some book and page properties available to Grantlee.
0030  */
0031 class KJotsEntity : public QObject
0032 {
0033     Q_OBJECT
0034     Q_PROPERTY(QString title READ title)
0035     Q_PROPERTY(QString content READ content)
0036     Q_PROPERTY(QString plainContent READ plainContent)
0037     Q_PROPERTY(QString url READ url)
0038     Q_PROPERTY(qint64 entityId READ entityId)
0039     Q_PROPERTY(bool isBook READ isBook)
0040     Q_PROPERTY(bool isPage READ isPage)
0041     Q_PROPERTY(QVariantList entities READ entities)
0042     Q_PROPERTY(QVariantList breadcrumbs READ breadcrumbs)
0043 
0044 public:
0045     explicit KJotsEntity(const QModelIndex &index, QObject *parent = nullptr);
0046     void setIndex(const QModelIndex &index);
0047 
0048     bool isBook() const;
0049     bool isPage() const;
0050 
0051     QString title() const;
0052 
0053     QString content() const;
0054 
0055     QString plainContent() const;
0056 
0057     QString url() const;
0058 
0059     qint64 entityId() const;
0060 
0061     QVariantList entities() const;
0062 
0063     QVariantList breadcrumbs() const;
0064 
0065 private:
0066     QPersistentModelIndex m_index;
0067 };
0068 
0069 class KJotsModel : public Akonadi::EntityTreeModel
0070 {
0071     Q_OBJECT
0072 public:
0073     explicit KJotsModel(Akonadi::ChangeRecorder *monitor, QObject *parent = nullptr);
0074     ~KJotsModel();
0075 
0076     enum KJotsRoles {
0077         GrantleeObjectRole = EntityTreeModel::UserRole,
0078         DocumentRole
0079     };
0080 
0081     enum Column {
0082         // Title of a note
0083         Title,
0084 
0085         // Modification date / time of a note
0086         ModificationTime,
0087 
0088         // Creation date / time of a note
0089         CreationTime,
0090 
0091         // Size of a note
0092         Size
0093     };
0094 
0095     // We don't reimplement the Collection overload.
0096     using EntityTreeModel::entityData;
0097     QVariant entityData(const Akonadi::Item &item, int column, int role = Qt::DisplayRole) const override;
0098     int entityColumnCount(HeaderGroup headerGroup) const override;
0099     QVariant entityHeaderData(int section, Qt::Orientation orientation, int role, HeaderGroup headerGroup) const override;
0100 
0101     QVariant data(const QModelIndex &index, int role) const override;
0102     bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
0103 
0104     static QModelIndex modelIndexForUrl(const QAbstractItemModel *model, const QUrl &url);
0105     /**
0106      * Sets the content of @p item to @p document
0107      */
0108     static Akonadi::Item updateItem(const Akonadi::Item &item, QTextDocument *document);
0109     /**
0110      * A helper function which returns a full "path" to the @p item (e.g. "Resource / Notebook / Note")
0111      * using @p sep as a separator. If multiple items are selected, returns "Multiple selection"
0112      */
0113     static QString itemPath(const QModelIndex &index, const QString &sep = QStringLiteral(" / "));
0114 
0115     /**
0116      * A helper function which returns an index belonging to the ETM model through bunch of proxy models
0117      */
0118     static QModelIndex etmIndex(const QModelIndex &index);
0119 private:
0120     QHash<Akonadi::Collection::Id, QColor> m_colors;
0121     mutable QHash<Akonadi::Item::Id, QTextDocument *> m_documents;
0122 };
0123 
0124 #endif
0125