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 "personaleditor/personaleditorwidget.h"
13 #include "contacteditorpageplugin.h"
14 #include "contactmetadatabase_p.h"
15 
16 #include <KConfig>
17 #include <KConfigGroup>
18 #include <KLocalizedString>
19 #include <QTabWidget>
20 #include <KTextEdit>
21 
22 #include <QDirIterator>
23 #include <QPluginLoader>
24 #include <QVBoxLayout>
25 #include "addresseditor/addresseslocationwidget.h"
26 #include "customfieldeditor/customfieldswidget.h"
27 #include "businesseditor/businesseditorwidget.h"
28 #include "generalinfoeditor/generalinfowidget.h"
29 
30 class Q_DECL_HIDDEN ContactEditorWidget::Private
31 {
32 public:
33  Private(ContactEditorWidget::DisplayMode displayMode, ContactEditorWidget *parent)
34  : mDisplayMode(displayMode)
35  , mParent(parent)
36  {
37  }
38 
39  void initGui();
40  void initGuiContactTab();
41  void initGuiLocationTab();
42  void initGuiBusinessTab();
43  void initGuiPersonalTab();
44  void initGuiNotesTab();
45  void initGuiCustomFieldsTab();
46 
47  void loadCustomPages();
48 
49  QString loadCustom(const KContacts::Addressee &contact, const QString &key) const;
50  void storeCustom(KContacts::Addressee &contact, const QString &key, const QString &value) const;
51 
53  ContactEditorWidget *const mParent;
54  QTabWidget *mTabWidget = nullptr;
55 
56  ContactEditor::GeneralInfoWidget *mGeneralInfoWidget = nullptr;
57 
58  // widgets from addresses group
59  ContactEditor::AddressesLocationWidget *mAddressesLocationWidget = nullptr;
60 
61  ContactEditor::BusinessEditorWidget *mBusinessEditorWidget = nullptr;
62 
63  // widgets from notes group
64  KTextEdit *mNotesWidget = nullptr;
65 
66  ContactEditor::PersonalEditorWidget *mPersonalEditorWidget = nullptr;
67 
68  // widgets from custom fields group
69  ContactEditor::CustomFieldsWidget *mCustomFieldsWidget = nullptr;
70 
71  // custom editor pages
73 };
74 
75 void ContactEditorWidget::Private::initGui()
76 {
77  QVBoxLayout *layout = new QVBoxLayout(mParent);
78  layout->setContentsMargins(0, 0, 0, 0);
79 
80  mTabWidget = new QTabWidget(mParent);
81  layout->addWidget(mTabWidget);
82 
83  initGuiContactTab();
84  initGuiLocationTab();
85  initGuiBusinessTab();
86  initGuiPersonalTab();
87  initGuiNotesTab();
88  if (mDisplayMode == FullMode) {
89  initGuiCustomFieldsTab();
90  loadCustomPages();
91  }
92 }
93 
94 void ContactEditorWidget::Private::initGuiContactTab()
95 {
96  mGeneralInfoWidget = new ContactEditor::GeneralInfoWidget;
97  mTabWidget->addTab(mGeneralInfoWidget, i18nc("@title:tab", "Contact"));
98 }
99 
100 void ContactEditorWidget::Private::initGuiLocationTab()
101 {
102  mAddressesLocationWidget = new ContactEditor::AddressesLocationWidget;
103  mTabWidget->addTab(mAddressesLocationWidget, i18nc("@title:tab", "Location"));
104 }
105 
106 void ContactEditorWidget::Private::initGuiBusinessTab()
107 {
108  mBusinessEditorWidget = new ContactEditor::BusinessEditorWidget();
109  mTabWidget->addTab(mBusinessEditorWidget, i18nc("@title:tab", "Business"));
110 }
111 
112 void ContactEditorWidget::Private::initGuiPersonalTab()
113 {
114  mPersonalEditorWidget = new ContactEditor::PersonalEditorWidget;
115  mTabWidget->addTab(mPersonalEditorWidget, i18nc("@title:tab Personal properties of a contact", "Personal"));
116 }
117 
118 void ContactEditorWidget::Private::initGuiNotesTab()
119 {
120  QWidget *widget = new QWidget;
121  QVBoxLayout *layout = new QVBoxLayout(widget);
122 
123  mTabWidget->addTab(widget, i18nc("@title:tab", "Notes"));
124 
125  mNotesWidget = new KTextEdit;
126  mNotesWidget->setAcceptRichText(false);
127  layout->addWidget(mNotesWidget);
128 }
129 
130 void ContactEditorWidget::Private::initGuiCustomFieldsTab()
131 {
132  mCustomFieldsWidget = new ContactEditor::CustomFieldsWidget(mParent);
133  mTabWidget->addTab(mCustomFieldsWidget, i18nc("@title:tab", "Custom Fields"));
134 }
135 
136 void ContactEditorWidget::Private::loadCustomPages()
137 {
138  qDeleteAll(mCustomPages);
139  mCustomPages.clear();
140 
141  const QString pluginDirectory = QStringLiteral("%1/contacteditor/editorpageplugins/").arg(QStringLiteral(EDITOR_CONTACT_LIB));
142  QDirIterator it(pluginDirectory, QDir::Files);
143 
144  while (it.hasNext()) {
145  QPluginLoader loader(it.next());
146  if (!loader.load()) {
147  continue;
148  }
149 
150  ContactEditor::ContactEditorPagePlugin *plugin = qobject_cast<ContactEditor::ContactEditorPagePlugin *>(loader.instance());
151  if (!plugin) {
152  continue;
153  }
154 
155  mCustomPages.append(plugin);
156  }
157 
158  for (ContactEditor::ContactEditorPagePlugin *plugin : qAsConst(mCustomPages)) {
159  mTabWidget->addTab(plugin, plugin->title());
160  }
161 }
162 
163 QString ContactEditorWidget::Private::loadCustom(const KContacts::Addressee &contact, const QString &key) const
164 {
165  return contact.custom(QStringLiteral("KADDRESSBOOK"), key);
166 }
167 
168 void ContactEditorWidget::Private::storeCustom(KContacts::Addressee &contact, const QString &key, const QString &value) const
169 {
170  if (value.isEmpty()) {
171  contact.removeCustom(QStringLiteral("KADDRESSBOOK"), key);
172  } else {
173  contact.insertCustom(QStringLiteral("KADDRESSBOOK"), key, value);
174  }
175 }
176 
178  : d(new Private(FullMode, this))
179 {
180  Q_UNUSED(parent)
181  d->initGui();
182 }
183 
185  : d(new Private(displayMode, this))
186 {
187  Q_UNUSED(parent)
188  d->initGui();
189 }
190 
192 {
193  delete d;
194 }
195 
196 void ContactEditorWidget::loadContact(const KContacts::Addressee &contact, const ContactEditor::ContactMetaDataBase &metaData)
197 {
198  d->mGeneralInfoWidget->loadContact(contact);
199 
200  // address group
201  d->mAddressesLocationWidget->loadContact(contact);
202 
203  // general group
204  d->mBusinessEditorWidget->loadContact(contact);
205  // notes group
206  d->mNotesWidget->setPlainText(contact.note());
207 
208  d->mPersonalEditorWidget->loadContact(contact);
209  d->mGeneralInfoWidget->setDisplayType((DisplayNameEditWidget::DisplayType)metaData.displayNameMode());
210  if (d->mDisplayMode == FullMode) {
211  // custom fields group
212  d->mCustomFieldsWidget->setLocalCustomFieldDescriptions(metaData.customFieldDescriptions());
213  d->mCustomFieldsWidget->loadContact(contact);
214 
215  // custom pages
216  for (ContactEditor::ContactEditorPagePlugin *plugin : qAsConst(d->mCustomPages)) {
217  plugin->loadContact(contact);
218  }
219  }
220 }
221 
222 void ContactEditorWidget::storeContact(KContacts::Addressee &contact, ContactEditor::ContactMetaDataBase &metaData) const
223 {
224  d->mGeneralInfoWidget->storeContact(contact);
225 
226  // address group
227  d->mAddressesLocationWidget->storeContact(contact);
228 
229  // general group
230  d->mBusinessEditorWidget->storeContact(contact);
231 
232  // notes group
233  contact.setNote(d->mNotesWidget->toPlainText());
234  d->mPersonalEditorWidget->storeContact(contact);
235 
236  if (d->mDisplayMode == FullMode) {
237  // custom fields group
238  d->mCustomFieldsWidget->storeContact(contact);
239  metaData.setCustomFieldDescriptions(d->mCustomFieldsWidget->localCustomFieldDescriptions());
240 
241  metaData.setDisplayNameMode(d->mGeneralInfoWidget->displayType());
242 
243  // custom pages
244  for (ContactEditor::ContactEditorPagePlugin *plugin : qAsConst(d->mCustomPages)) {
245  plugin->storeContact(contact);
246  }
247  }
248 }
249 
251 {
252  d->mGeneralInfoWidget->setReadOnly(readOnly);
253  // widgets from addresses group
254  d->mAddressesLocationWidget->setReadOnly(readOnly);
255 
256  // widgets from general group
257  d->mBusinessEditorWidget->setReadOnly(readOnly);
258 
259  // widgets from notes group
260  d->mNotesWidget->setReadOnly(readOnly);
261 
262  d->mPersonalEditorWidget->setReadOnly(readOnly);
263  if (d->mDisplayMode == FullMode) {
264  // widgets from custom fields group
265  d->mCustomFieldsWidget->setReadOnly(readOnly);
266 
267  // custom pages
268  for (ContactEditor::ContactEditorPagePlugin *plugin : qAsConst(d->mCustomPages)) {
269  plugin->setReadOnly(readOnly);
270  }
271  }
272 }
QString custom(const QString &app, const QString &name) const
void setContentsMargins(int left, int top, int right, int bottom)
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.
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
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-2020 The KDE developers.
Generated on Mon Sep 21 2020 23:09:01 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.