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

0001 // SPDX-FileCopyrightText: 2021 Claudio Cambra <claudio.cambra@gmail.com>
0002 // SPDX-FileCopyrightText: 2022 Carl Schwan <carl@carlschwan.eu>
0003 // SPDX-License-Identifier: LGPL-2.1-or-later
0004 
0005 #pragma once
0006 
0007 #include <Akonadi/CollectionIdentificationAttribute>
0008 #include <Akonadi/Item>
0009 #include <Akonadi/ItemFetchJob>
0010 #include <Akonadi/ItemFetchScope>
0011 #include <Akonadi/ItemMonitor>
0012 #include <KContacts/Addressee>
0013 #include <kcontacts/addressee.h>
0014 
0015 #include "addressmodel.h"
0016 
0017 #include "emailmodel.h"
0018 #include "imppmodel.h"
0019 #include "phonemodel.h"
0020 
0021 /// This class is a QObject wrapper for a KContact::Adressee
0022 class AddresseeWrapper : public QObject, public Akonadi::ItemMonitor
0023 {
0024     Q_OBJECT
0025     // Akonadi properties
0026     Q_PROPERTY(Akonadi::Item addresseeItem READ addresseeItem WRITE setAddresseeItem NOTIFY addresseeItemChanged)
0027     Q_PROPERTY(Akonadi::Collection collection READ collection WRITE setCollection NOTIFY collectionChanged)
0028     Q_PROPERTY(qint64 collectionId READ collectionId NOTIFY collectionChanged)
0029 
0030     Q_PROPERTY(QString uid READ uid NOTIFY uidChanged)
0031 
0032     // Contact information
0033     Q_PROPERTY(QString formattedName READ formattedName WRITE setFormattedName NOTIFY formattedNameChanged)
0034     Q_PROPERTY(QString additionalName READ additionalName WRITE setAdditionalName NOTIFY additionalNameChanged)
0035     Q_PROPERTY(QString familyName READ familyName WRITE setFamilyName NOTIFY familyNameChanged)
0036     Q_PROPERTY(QString givenName READ givenName WRITE setGivenName NOTIFY givenNameChanged)
0037     Q_PROPERTY(QString prefix READ prefix WRITE setPrefix NOTIFY prefixChanged)
0038     Q_PROPERTY(QString suffix READ suffix WRITE setSuffix NOTIFY suffixChanged)
0039     Q_PROPERTY(QString nickName READ nickName WRITE setNickName NOTIFY nickNameChanged)
0040     Q_PROPERTY(QUrl blogFeed READ blogFeed WRITE setBlogFeed NOTIFY blogFeedChanged)
0041     Q_PROPERTY(QString preferredEmail READ preferredEmail NOTIFY preferredEmailChanged)
0042     Q_PROPERTY(KContacts::PhoneNumber::List phoneNumbers READ phoneNumbers NOTIFY phoneNumbersChanged)
0043     Q_PROPERTY(EmailModel *emailModel READ emailModel CONSTANT)
0044     Q_PROPERTY(PhoneModel *phoneModel READ phoneModel CONSTANT)
0045 
0046     // Address
0047     Q_PROPERTY(AddressModel *addressesModel READ addressesModel CONSTANT)
0048 
0049     // Impp
0050     Q_PROPERTY(ImppModel *imppModel READ imppModel CONSTANT)
0051 
0052     // Personal information
0053     Q_PROPERTY(QDateTime birthday READ birthday WRITE setBirthday NOTIFY birthdayChanged)
0054     Q_PROPERTY(QDateTime anniversary READ anniversary WRITE setAnniversary NOTIFY anniversaryChanged)
0055     Q_PROPERTY(QString spousesName READ spousesName WRITE setSpousesName NOTIFY spousesNameChanged)
0056 
0057     // Buisness information
0058     Q_PROPERTY(QString organization READ organization WRITE setOrganization NOTIFY organizationChanged)
0059     Q_PROPERTY(QString profession READ profession WRITE setProfession NOTIFY professionChanged)
0060     Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged)
0061     Q_PROPERTY(QString department READ department WRITE setDepartment NOTIFY departmentChanged)
0062     Q_PROPERTY(QString office READ office WRITE setOffice NOTIFY officeChanged)
0063     Q_PROPERTY(QString managersName READ managersName WRITE setManagersName NOTIFY managersNameChanged)
0064     Q_PROPERTY(QString assistantsName READ assistantsName WRITE setAssistantsName NOTIFY assistantsNameChanged)
0065 
0066     // Other information
0067     Q_PROPERTY(QString note READ note WRITE setNote NOTIFY noteChanged)
0068     Q_PROPERTY(KContacts::Picture photo READ photo NOTIFY photoChanged)
0069 
0070     Q_PROPERTY(DisplayType displayType READ displayType WRITE setDisplayType NOTIFY displayTypeChanged)
0071 public:
0072     /**
0073      * Describes what the display name should look like.
0074      */
0075     enum DisplayType {
0076         SimpleName, ///< A name of the form: givenName familyName
0077         FullName, ///< A name of the form: prefix givenName additionalName familyName suffix
0078         ReverseNameWithComma, ///< A name of the form: familyName, givenName
0079         ReverseName, ///< A name of the form: familyName givenName
0080         Organization, ///< The organization name
0081         CustomName ///< Let the user input a display name
0082     };
0083     Q_ENUM(DisplayType)
0084 
0085     AddresseeWrapper(QObject *parent = nullptr);
0086     ~AddresseeWrapper() override;
0087 
0088     Akonadi::Item addresseeItem() const;
0089     void setAddresseeItem(const Akonadi::Item &item);
0090 
0091     KContacts::Addressee addressee() const;
0092     void setAddressee(const KContacts::Addressee &addressee);
0093 
0094     QString uid() const;
0095 
0096     Akonadi::Collection collection() const;
0097     qint64 collectionId() const;
0098     void setCollection(Akonadi::Collection collection);
0099 
0100     DisplayType displayType() const;
0101     void setDisplayType(DisplayType displayType);
0102 
0103     QString formattedName() const;
0104     void setFormattedName(const QString &formattedName);
0105 
0106     QString nickName() const;
0107     void setNickName(const QString &nickName);
0108 
0109     QUrl blogFeed() const;
0110     void setBlogFeed(const QUrl &blogFDeed);
0111 
0112     QDateTime birthday() const;
0113     void setBirthday(const QDateTime &birthday);
0114 
0115     QString preferredEmail() const;
0116     KContacts::Picture photo() const;
0117 
0118     AddressModel *addressesModel() const;
0119     ImppModel *imppModel() const;
0120 
0121     EmailModel *emailModel() const;
0122     PhoneModel *phoneModel() const;
0123     KContacts::PhoneNumber::List phoneNumbers() const;
0124 
0125     QString note() const;
0126     void setNote(const QString &note);
0127 
0128     QDateTime anniversary() const;
0129     void setAnniversary(const QDateTime &anniversary);
0130 
0131     QString organization() const;
0132     void setOrganization(const QString &organization);
0133 
0134     QString profession() const;
0135     void setProfession(const QString &profession);
0136 
0137     QString title() const;
0138     void setTitle(const QString &title);
0139 
0140     QString department() const;
0141     void setDepartment(const QString &department);
0142 
0143     QString office() const;
0144     void setOffice(const QString &office);
0145 
0146     QString managersName() const;
0147     void setManagersName(const QString &managersName);
0148 
0149     QString assistantsName() const;
0150     void setAssistantsName(const QString &assistantsName);
0151 
0152     void setSpousesName(const QString &spousesName);
0153     QString spousesName() const;
0154 
0155     QString additionalName() const;
0156     void setAdditionalName(const QString &additionalName);
0157 
0158     QString familyName() const;
0159     void setFamilyName(const QString &familyName);
0160 
0161     QString givenName() const;
0162     void setGivenName(const QString &givenName);
0163 
0164     QString prefix() const;
0165     void setPrefix(const QString &prefix);
0166 
0167     QString suffix() const;
0168     void setSuffix(const QString &suffix);
0169 
0170     // Invokable since we don't want expensive data bindings when any of the
0171     // fields change, instead generate it on demand
0172     Q_INVOKABLE QString qrCodeData() const;
0173     Q_INVOKABLE void updatePhoto(const KContacts::Picture &loadedPhoto);
0174     Q_INVOKABLE KContacts::Picture preparePhoto(const QUrl &path) const;
0175 
0176     void notifyDataChanged();
0177 Q_SIGNALS:
0178     void addresseeItemChanged();
0179     void collectionChanged();
0180     void formattedNameChanged();
0181     void birthdayChanged();
0182     void photoChanged();
0183     void phoneNumbersChanged();
0184     void preferredEmailChanged();
0185     void uidChanged();
0186     void noteChanged();
0187     void nickNameChanged();
0188     void blogFeedChanged();
0189     void additionalNameChanged();
0190     void familyNameChanged();
0191     void givenNameChanged();
0192     void prefixChanged();
0193     void suffixChanged();
0194 
0195     void anniversaryChanged();
0196     void spousesNameChanged();
0197 
0198     void organizationChanged();
0199     void professionChanged();
0200     void titleChanged();
0201     void departmentChanged();
0202     void officeChanged();
0203     void managersNameChanged();
0204     void assistantsNameChanged();
0205     void displayTypeChanged();
0206 
0207 private:
0208     void itemChanged(const Akonadi::Item &item) override;
0209     KContacts::Addressee m_addressee;
0210     Akonadi::Collection m_collection; // For when we want to edit, this is temporary
0211     AddressModel *m_addressesModel;
0212     EmailModel *m_emailModel;
0213     ImppModel *m_imppModel;
0214     PhoneModel *m_phoneModel;
0215     DisplayType m_displayType;
0216 };