File indexing completed on 2024-11-24 04:50:41

0001 // This file is part of Akonadi Contact.
0002 // SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org>
0003 // SPDX-License-Identifier: LGPL-2.0-or-later
0004 
0005 #pragma once
0006 
0007 #include <Akonadi/Collection>
0008 #include <Akonadi/Item>
0009 #include <QObject>
0010 
0011 class ContactGroupEditorPrivate;
0012 class QAbstractItemModel;
0013 
0014 /**
0015  * @short An object to edit contact groups in Akonadi.
0016  *
0017  * This object provides a way to create a new contact group or edit
0018  * an existing contact group in Akonadi from QML.
0019  *
0020  * Example for creating a new contact:
0021  *
0022  * @code{.qml}
0023  * ContactGroupEditor {
0024  *     id: contactGroupEditor
0025  *     mode: ContactGroupEditor.CreateMode
0026  * }
0027  *
0028  * TextField {
0029  *     id: nameField
0030  *     onTextChanged. contactEditor.addressee.name = text
0031  * }
0032  *
0033  * Button {
0034  *     onClicked: contactEditor.saveContactGroup()
0035  * }
0036  * @endcode
0037  *
0038  * Example for editing an existing contact:
0039  *
0040  * @code
0041  *
0042  * ContactEditor {
0043  *     id: contactEditor
0044  *     mode: ContactEditor.EditMode
0045  *     item: myExistingItem
0046  * }
0047  *
0048  * TextField {
0049  *     id: nameField
0050  *     onTextChanged. contactEditor.addressee.name = text
0051  * }
0052  *
0053  * Button {
0054  *     onClicked: contactEditor.saveContactInAddressBook()
0055  * }
0056  *
0057  * @endcode
0058  *
0059  * @author Tobias Koenig <tokoe@kde.org>
0060  * @author Carl Schwan <carl@carlschwan.eu>
0061  */
0062 class ContactGroupEditor : public QObject
0063 {
0064     Q_OBJECT
0065     Q_PROPERTY(Mode mode READ mode WRITE setMode NOTIFY modeChanged)
0066     Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
0067     Q_PROPERTY(qint64 collectionId READ collectionId NOTIFY collectionChanged)
0068     Q_PROPERTY(bool isReadOnly READ isReadOnly NOTIFY isReadOnlyChanged)
0069     Q_PROPERTY(QAbstractItemModel *groupModel READ groupModel CONSTANT)
0070 public:
0071     /**
0072      * Describes the mode of the editor.
0073      */
0074     enum Mode {
0075         CreateMode, ///< Creates a new contact
0076         EditMode ///< Edits an existing contact
0077     };
0078     Q_ENUM(Mode)
0079 
0080     /**
0081      * Creates a new contact group editor backend.
0082      *
0083      * @param parent The parent object of the editor.
0084      */
0085     explicit ContactGroupEditor(QObject *parent = nullptr);
0086 
0087     /**
0088      * Destroys the contact editor.
0089      */
0090     ~ContactGroupEditor() override;
0091 
0092     /**
0093      * Sets the @p addressbook which shall be used to store new
0094      * contacts.
0095      */
0096     Q_INVOKABLE void setDefaultAddressBook(const Akonadi::Collection &addressbook);
0097 
0098     Q_INVOKABLE void loadContactGroup(const Akonadi::Item &item);
0099     /**
0100      * Save the contact group from the editor back to the storage. And return error.
0101      * Need to connect to finished() signal, to keep time to Q_EMIT signal.
0102      */
0103     Q_INVOKABLE bool saveContactGroup();
0104 
0105     [[nodiscard]] bool hasNoSavedData() const;
0106 
0107     [[nodiscard]] qint64 collectionId() const;
0108     [[nodiscard]] Mode mode() const;
0109     void setMode(Mode mode);
0110     [[nodiscard]] bool isReadOnly() const;
0111     void setReadOnly(bool isReadOnly);
0112 
0113     QString name() const;
0114     void setName(const QString &name);
0115     QAbstractItemModel *groupModel() const;
0116 
0117     Q_INVOKABLE void fetchItem();
0118 
0119 Q_SIGNALS:
0120     /**
0121      * This signal is emitted when the @p contact has been saved back
0122      * to the storage.
0123      */
0124     void contactGroupStored(const Akonadi::Item &contact);
0125 
0126     /**
0127      * This signal is emitted when an error occurred during the save.
0128      * @param errorMsg The error message.
0129      */
0130     void errorOccured(const QString &errorMsg);
0131 
0132     /**
0133      * @brief finished
0134      */
0135     void finished();
0136 
0137     void modeChanged();
0138     void isReadOnlyChanged();
0139     void nameChanged();
0140     void itemChanged();
0141     void collectionChanged();
0142     void itemChangedExternally();
0143 
0144 private:
0145     std::unique_ptr<ContactGroupEditorPrivate> d;
0146 };