File indexing completed on 2024-11-24 04:39:33

0001 /*
0002     This file is part of Akonadi Contact.
0003 
0004     SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #pragma once
0010 
0011 #include "akonadi-contact-widgets_export.h"
0012 
0013 #include <QWidget>
0014 
0015 #include <memory>
0016 
0017 namespace KContacts
0018 {
0019 class Addressee;
0020 }
0021 namespace Akonadi
0022 {
0023 class Collection;
0024 class Item;
0025 class AbstractContactEditorWidget;
0026 class AkonadiContactEditorPrivate;
0027 
0028 /**
0029  * @short An widget to edit contacts in Akonadi.
0030  *
0031  * This widget provides a way to create a new contact or edit
0032  * an existing contact in Akonadi.
0033  *
0034  * Example for creating a new contact:
0035  *
0036  * @code
0037  *
0038  * using namespace Akonadi;
0039  *
0040  * ContactEditor *editor = new ContactEditor( Akonadi::ContactEditor::CreateMode, this );
0041  *
0042  * ...
0043  *
0044  * if ( !editor->saveContactInAddressBook() ) {
0045  *   qCDebug(AKONADICONTACT_LOG) << "Unable to save new contact to storage";
0046  *   return;
0047  * }
0048  *
0049  * @endcode
0050  *
0051  * Example for editing an existing contact:
0052  *
0053  * @code
0054  *
0055  * const Akonadi::Item contact = ...;
0056  *
0057  * ContactEditor *editor = new ContactEditor( Akonadi::ContactEditor::EditMode, this );
0058  * editor->loadContact( contact );
0059  *
0060  * ...
0061  *
0062  * editor->saveContactInAddressBook();
0063  *
0064  * @endcode
0065  *
0066  * @author Tobias Koenig <tokoe@kde.org>
0067  * @since 4.4
0068  */
0069 class AKONADI_CONTACT_WIDGETS_EXPORT AkonadiContactEditor : public QWidget
0070 {
0071     Q_OBJECT
0072 
0073 public:
0074     /**
0075      * Describes the mode of the editor.
0076      */
0077     enum Mode {
0078         CreateMode, ///< Creates a new contact
0079         EditMode ///< Edits an existing contact
0080     };
0081 
0082     enum DisplayMode {
0083         FullMode, ///< Show all pages
0084         VCardMode ///< Show just pages with elements stored in vcard.
0085     };
0086 
0087     /**
0088      * Creates a new contact editor with the standard editor widget.
0089      *
0090      * @param mode The mode of the editor.
0091      * @param parent The parent widget of the editor.
0092      */
0093     explicit AkonadiContactEditor(Mode mode, QWidget *parent = nullptr);
0094 
0095     /**
0096      * Creates a new contact editor with a custom editor widget.
0097      *
0098      * @param mode The mode of the editor.
0099      * @param editorWidget The contact editor widget that shall be used for editing.
0100      * @param parent The parent widget of the editor.
0101      */
0102     AkonadiContactEditor(Mode mode, Akonadi::AbstractContactEditorWidget *editorWidget, QWidget *parent = nullptr);
0103 
0104     /**
0105      * Creates a new contact editor dialog with a custom editor widget.
0106      *
0107      * @param mode The mode of the dialog.
0108      * @param displayMode mode for displaying the editor
0109      * @param parent The parent widget of the dialog.
0110      * @since 4.10
0111      */
0112     AkonadiContactEditor(Mode mode, DisplayMode displayMode, QWidget *parent = nullptr);
0113 
0114     /**
0115      * Destroys the contact editor.
0116      */
0117     ~AkonadiContactEditor() override;
0118 
0119     /**
0120      * Sets a @p contact that is used as template in create mode.
0121      * The fields of the editor will be prefilled with the content of the contact.
0122      * @param contact the contact to use as template content
0123      */
0124     void setContactTemplate(const KContacts::Addressee &contact);
0125 
0126     /**
0127      * Sets the @p addressbook which shall be used to store new
0128      * contacts.
0129      */
0130     void setDefaultAddressBook(const Akonadi::Collection &addressbook);
0131 
0132     /**
0133      * @since 4.10
0134      * @brief ContactEditor::contact
0135      * @return
0136      */
0137     [[nodiscard]] KContacts::Addressee contact();
0138     [[nodiscard]] bool hasNoSavedData() const;
0139 public Q_SLOTS:
0140     /**
0141      * Loads the @p contact into the editor.
0142      */
0143     void loadContact(const Akonadi::Item &contact);
0144 
0145     /**
0146      * Save the contact from the editor back to the storage. And return error.
0147      * Need to connect to finished() signal, to keep time to Q_EMIT signal.
0148      * @since 4.11
0149      */
0150     void saveContactInAddressBook();
0151 
0152 Q_SIGNALS:
0153     /**
0154      * This signal is emitted when the @p contact has been saved back
0155      * to the storage.
0156      */
0157     void contactStored(const Akonadi::Item &contact);
0158 
0159     /**
0160      * This signal is emitted when an error occurred during the save.
0161      * @param errorMsg The error message.
0162      * @since 4.11
0163      */
0164     void error(const QString &errorMsg);
0165 
0166     /**
0167      * @brief finished
0168      * @since 4.11
0169      */
0170     void finished();
0171 
0172 private:
0173     //@cond PRIVATE
0174     std::unique_ptr<AkonadiContactEditorPrivate> const d;
0175     //@endcond
0176 };
0177 }