Akonadi Contacts

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

KDE's Doxygen guidelines are available online.