Akonadi Contacts

contacteditorwidget.cpp
1/*
2 This file is part of Contact Editor.
3
4 SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "contacteditorwidget.h"
10
11#include "contacteditorpageplugin.h"
12#include "contactmetadatabase_p.h"
13#include "personaleditor/personaleditorwidget.h"
14
15#include <KLocalizedString>
16#include <KTextEdit>
17#include <QTabWidget>
18
19#include "addresseditor/addresseslocationwidget.h"
20#include "businesseditor/businesseditorwidget.h"
21#include "customfieldeditor/customfieldswidget.h"
22#include "generalinfoeditor/generalinfowidget.h"
23#include <QCoreApplication>
24#include <QDirIterator>
25#include <QPluginLoader>
26#include <QVBoxLayout>
27
28class ContactEditorWidgetPrivate
29{
30public:
31 ContactEditorWidgetPrivate(ContactEditorWidget::DisplayMode displayMode, ContactEditorWidget *parent)
32 : mDisplayMode(displayMode)
33 , mParent(parent)
34 {
35 }
36
37 void initGui();
38 void initGuiContactTab();
39 void initGuiLocationTab();
40 void initGuiBusinessTab();
41 void initGuiPersonalTab();
42 void initGuiNotesTab();
43 void initGuiCustomFieldsTab();
44
45 void loadCustomPages();
46
47 QString loadCustom(const KContacts::Addressee &contact, const QString &key) const;
48 void storeCustom(KContacts::Addressee &contact, const QString &key, const QString &value) const;
49
51 ContactEditorWidget *const mParent;
52 QTabWidget *mTabWidget = nullptr;
53
54 Akonadi::GeneralInfoWidget *mGeneralInfoWidget = nullptr;
55
56 // widgets from addresses group
57 Akonadi::AddressesLocationWidget *mAddressesLocationWidget = nullptr;
58
59 Akonadi::BusinessEditorWidget *mBusinessEditorWidget = nullptr;
60
61 // widgets from notes group
62 KTextEdit *mNotesWidget = nullptr;
63
64 Akonadi::PersonalEditorWidget *mPersonalEditorWidget = nullptr;
65
66 // widgets from custom fields group
67 Akonadi::CustomFieldsWidget *mCustomFieldsWidget = nullptr;
68
69 // custom editor pages
71};
72
73void ContactEditorWidgetPrivate::initGui()
74{
75 auto layout = new QVBoxLayout(mParent);
76 layout->setContentsMargins({});
77
78 mTabWidget = new QTabWidget(mParent);
79 layout->addWidget(mTabWidget);
80
81 initGuiContactTab();
82 initGuiLocationTab();
83 initGuiBusinessTab();
84 initGuiPersonalTab();
85 initGuiNotesTab();
86 if (mDisplayMode == ContactEditorWidget::FullMode) {
87 initGuiCustomFieldsTab();
88 loadCustomPages();
89 }
90}
91
92void ContactEditorWidgetPrivate::initGuiContactTab()
93{
94 mGeneralInfoWidget = new Akonadi::GeneralInfoWidget;
95 mTabWidget->addTab(mGeneralInfoWidget, i18nc("@title:tab", "Contact"));
96}
97
98void ContactEditorWidgetPrivate::initGuiLocationTab()
99{
100 mAddressesLocationWidget = new Akonadi::AddressesLocationWidget;
101 mTabWidget->addTab(mAddressesLocationWidget, i18nc("@title:tab", "Location"));
102}
103
104void ContactEditorWidgetPrivate::initGuiBusinessTab()
105{
106 mBusinessEditorWidget = new Akonadi::BusinessEditorWidget();
107 mTabWidget->addTab(mBusinessEditorWidget, i18nc("@title:tab", "Business"));
108}
109
110void ContactEditorWidgetPrivate::initGuiPersonalTab()
111{
112 mPersonalEditorWidget = new Akonadi::PersonalEditorWidget;
113 mTabWidget->addTab(mPersonalEditorWidget, i18nc("@title:tab Personal properties of a contact", "Personal"));
114}
115
116void ContactEditorWidgetPrivate::initGuiNotesTab()
117{
118 auto widget = new QWidget;
119 auto layout = new QVBoxLayout(widget);
120
121 mTabWidget->addTab(widget, i18nc("@title:tab", "Notes"));
122
123 mNotesWidget = new KTextEdit;
124 mNotesWidget->setAcceptRichText(false);
125 layout->addWidget(mNotesWidget);
126}
127
128void ContactEditorWidgetPrivate::initGuiCustomFieldsTab()
129{
130 mCustomFieldsWidget = new Akonadi::CustomFieldsWidget(mParent);
131 mTabWidget->addTab(mCustomFieldsWidget, i18nc("@title:tab", "Custom Fields"));
132}
133
134void ContactEditorWidgetPrivate::loadCustomPages()
135{
136 qDeleteAll(mCustomPages);
137 mCustomPages.clear();
138
139 const QStringList pluginDirs = QCoreApplication::libraryPaths();
140
141 for (const QString &dir : pluginDirs) {
142 QDirIterator it(dir + QLatin1StringView("/pim6/contacteditor/editorpageplugins"), QDir::Files);
143
144 while (it.hasNext()) {
145 QPluginLoader loader(it.next());
146 if (!loader.load()) {
147 continue;
148 }
149
150 Akonadi::ContactEditorPagePlugin *plugin = qobject_cast<Akonadi::ContactEditorPagePlugin *>(loader.instance());
151 if (!plugin) {
152 continue;
153 }
154
155 mCustomPages.append(plugin);
156 }
157 }
158
159 for (Akonadi::ContactEditorPagePlugin *plugin : std::as_const(mCustomPages)) {
160 mTabWidget->addTab(plugin, plugin->title());
161 }
162}
163
164QString ContactEditorWidgetPrivate::loadCustom(const KContacts::Addressee &contact, const QString &key) const
165{
166 return contact.custom(QStringLiteral("KADDRESSBOOK"), key);
167}
168
169void ContactEditorWidgetPrivate::storeCustom(KContacts::Addressee &contact, const QString &key, const QString &value) const
170{
171 if (value.isEmpty()) {
172 contact.removeCustom(QStringLiteral("KADDRESSBOOK"), key);
173 } else {
174 contact.insertCustom(QStringLiteral("KADDRESSBOOK"), key, value);
175 }
176}
177
179 : d(new ContactEditorWidgetPrivate(FullMode, this))
180{
181 Q_UNUSED(parent)
182 d->initGui();
183}
184
186 : d(new ContactEditorWidgetPrivate(displayMode, this))
187{
188 Q_UNUSED(parent)
189 d->initGui();
190}
191
193
194void ContactEditorWidget::loadContact(const KContacts::Addressee &contact, const Akonadi::ContactMetaDataBase &metaData)
195{
196 d->mGeneralInfoWidget->loadContact(contact);
197
198 // address group
199 d->mAddressesLocationWidget->loadContact(contact);
200
201 // general group
202 d->mBusinessEditorWidget->loadContact(contact);
203 // notes group
204 d->mNotesWidget->setPlainText(contact.note());
205
206 d->mPersonalEditorWidget->loadContact(contact);
207 d->mGeneralInfoWidget->setDisplayType((Akonadi::DisplayNameEditWidget::DisplayType)metaData.displayNameMode());
208 if (d->mDisplayMode == FullMode) {
209 // custom fields group
210 d->mCustomFieldsWidget->setLocalCustomFieldDescriptions(metaData.customFieldDescriptions());
211 d->mCustomFieldsWidget->loadContact(contact);
212
213 // custom pages
214 for (Akonadi::ContactEditorPagePlugin *plugin : std::as_const(d->mCustomPages)) {
215 plugin->loadContact(contact);
216 }
217 }
218}
219
220void ContactEditorWidget::storeContact(KContacts::Addressee &contact, Akonadi::ContactMetaDataBase &metaData) const
221{
222 d->mGeneralInfoWidget->storeContact(contact);
223
224 // address group
225 d->mAddressesLocationWidget->storeContact(contact);
226
227 // general group
228 d->mBusinessEditorWidget->storeContact(contact);
229
230 // notes group
231 contact.setNote(d->mNotesWidget->toPlainText());
232 d->mPersonalEditorWidget->storeContact(contact);
233
234 if (d->mDisplayMode == FullMode) {
235 // custom fields group
236 d->mCustomFieldsWidget->storeContact(contact);
237 metaData.setCustomFieldDescriptions(d->mCustomFieldsWidget->localCustomFieldDescriptions());
238
239 metaData.setDisplayNameMode(d->mGeneralInfoWidget->displayType());
240
241 // custom pages
242 for (Akonadi::ContactEditorPagePlugin *plugin : std::as_const(d->mCustomPages)) {
243 plugin->storeContact(contact);
244 }
245 }
246}
247
249{
250 d->mGeneralInfoWidget->setReadOnly(readOnly);
251 // widgets from addresses group
252 d->mAddressesLocationWidget->setReadOnly(readOnly);
253
254 // widgets from general group
255 d->mBusinessEditorWidget->setReadOnly(readOnly);
256
257 // widgets from notes group
258 d->mNotesWidget->setReadOnly(readOnly);
259
260 d->mPersonalEditorWidget->setReadOnly(readOnly);
261 if (d->mDisplayMode == FullMode) {
262 // widgets from custom fields group
263 d->mCustomFieldsWidget->setReadOnly(readOnly);
264
265 // custom pages
266 for (Akonadi::ContactEditorPagePlugin *plugin : std::as_const(d->mCustomPages)) {
267 plugin->setReadOnly(readOnly);
268 }
269 }
270}
271
272bool ContactEditorWidget::hasNoSavedData() const
273{
274 return d->mAddressesLocationWidget->hasNoSavedData();
275}
276
277#include "moc_contacteditorwidget.cpp"
The base class for custom ContactEditor page plugins.
virtual void loadContact(const KContacts::Addressee &contact)=0
This method is called to fill the editor widget with the data from contact.
virtual void setReadOnly(bool readOnly)=0
This method is called to set the editor widget readOnly.
virtual void storeContact(KContacts::Addressee &contact) const =0
This method is called to store the data from the editor widget into contact.
virtual QString title() const =0
Returns the i18n'd page title.
A widget for editing a contact.
~ContactEditorWidget() override
Destroys the contact editor widget.
void setReadOnly(bool readOnly) override
Sets whether the contact in the editor allows the user to edit the contact or not.
void storeContact(KContacts::Addressee &contact, Akonadi::ContactMetaDataBase &metaData) const override
Stores back the fields of the contact editor into the given contact.
void loadContact(const KContacts::Addressee &contact, const Akonadi::ContactMetaDataBase &metaData) override
Initializes the fields of the contact editor with the values from a contact.
@ FullMode
Show all pages.
ContactEditorWidget(QWidget *parent=nullptr)
Creates a new contact editor widget.
void insertCustom(const QString &app, const QString &name, const QString &value)
QString custom(const QString &app, const QString &name) const
QString note() const
void removeCustom(const QString &app, const QString &name)
void setNote(const QString &note)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QStringList libraryPaths()
void append(QList< T > &&value)
void clear()
bool isEmpty() const const
int addTab(QWidget *page, const QIcon &icon, const QString &label)
void setAcceptRichText(bool accept)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:36:45 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.