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 
11 #include "contacteditorpageplugin.h"
12 #include "contactmetadatabase_p.h"
13 #include "personaleditor/personaleditorwidget.h"
14 
15 #include <KConfig>
16 #include <KLocalizedString>
17 #include <KTextEdit>
18 #include <QTabWidget>
19 
20 #include "addresseditor/addresseslocationwidget.h"
21 #include "businesseditor/businesseditorwidget.h"
22 #include "customfieldeditor/customfieldswidget.h"
23 #include "generalinfoeditor/generalinfowidget.h"
24 #include <QCoreApplication>
25 #include <QDirIterator>
26 #include <QPluginLoader>
27 #include <QVBoxLayout>
28 
29 class ContactEditorWidgetPrivate
30 {
31 public:
32  ContactEditorWidgetPrivate(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 ContactEditorWidgetPrivate::initGui()
75 {
76  auto layout = new QVBoxLayout(mParent);
77  layout->setContentsMargins({});
78 
79  mTabWidget = new QTabWidget(mParent);
80  layout->addWidget(mTabWidget);
81 
82  initGuiContactTab();
83  initGuiLocationTab();
84  initGuiBusinessTab();
85  initGuiPersonalTab();
86  initGuiNotesTab();
87  if (mDisplayMode == ContactEditorWidget::FullMode) {
88  initGuiCustomFieldsTab();
89  loadCustomPages();
90  }
91 }
92 
93 void ContactEditorWidgetPrivate::initGuiContactTab()
94 {
95  mGeneralInfoWidget = new ContactEditor::GeneralInfoWidget;
96  mTabWidget->addTab(mGeneralInfoWidget, i18nc("@title:tab", "Contact"));
97 }
98 
99 void ContactEditorWidgetPrivate::initGuiLocationTab()
100 {
101  mAddressesLocationWidget = new ContactEditor::AddressesLocationWidget;
102  mTabWidget->addTab(mAddressesLocationWidget, i18nc("@title:tab", "Location"));
103 }
104 
105 void ContactEditorWidgetPrivate::initGuiBusinessTab()
106 {
107  mBusinessEditorWidget = new ContactEditor::BusinessEditorWidget();
108  mTabWidget->addTab(mBusinessEditorWidget, i18nc("@title:tab", "Business"));
109 }
110 
111 void ContactEditorWidgetPrivate::initGuiPersonalTab()
112 {
113  mPersonalEditorWidget = new ContactEditor::PersonalEditorWidget;
114  mTabWidget->addTab(mPersonalEditorWidget, i18nc("@title:tab Personal properties of a contact", "Personal"));
115 }
116 
117 void ContactEditorWidgetPrivate::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 ContactEditorWidgetPrivate::initGuiCustomFieldsTab()
130 {
131  mCustomFieldsWidget = new ContactEditor::CustomFieldsWidget(mParent);
132  mTabWidget->addTab(mCustomFieldsWidget, i18nc("@title:tab", "Custom Fields"));
133 }
134 
135 void ContactEditorWidgetPrivate::loadCustomPages()
136 {
137  qDeleteAll(mCustomPages);
138  mCustomPages.clear();
139 
140  const QStringList pluginDirs = QCoreApplication::libraryPaths();
141 
142  for (const QString &dir : pluginDirs) {
143  QDirIterator it(dir + QLatin1String("/pim" QT_STRINGIFY(QT_VERSION_MAJOR) "/contacteditor/editorpageplugins"), QDir::Files);
144 
145  while (it.hasNext()) {
146  QPluginLoader loader(it.next());
147  if (!loader.load()) {
148  continue;
149  }
150 
151  ContactEditor::ContactEditorPagePlugin *plugin = qobject_cast<ContactEditor::ContactEditorPagePlugin *>(loader.instance());
152  if (!plugin) {
153  continue;
154  }
155 
156  mCustomPages.append(plugin);
157  }
158  }
159 
160  for (ContactEditor::ContactEditorPagePlugin *plugin : std::as_const(mCustomPages)) {
161  mTabWidget->addTab(plugin, plugin->title());
162  }
163 }
164 
165 QString ContactEditorWidgetPrivate::loadCustom(const KContacts::Addressee &contact, const QString &key) const
166 {
167  return contact.custom(QStringLiteral("KADDRESSBOOK"), key);
168 }
169 
170 void ContactEditorWidgetPrivate::storeCustom(KContacts::Addressee &contact, const QString &key, const QString &value) const
171 {
172  if (value.isEmpty()) {
173  contact.removeCustom(QStringLiteral("KADDRESSBOOK"), key);
174  } else {
175  contact.insertCustom(QStringLiteral("KADDRESSBOOK"), key, value);
176  }
177 }
178 
180  : d(new ContactEditorWidgetPrivate(FullMode, this))
181 {
182  Q_UNUSED(parent)
183  d->initGui();
184 }
185 
187  : d(new ContactEditorWidgetPrivate(displayMode, this))
188 {
189  Q_UNUSED(parent)
190  d->initGui();
191 }
192 
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 : std::as_const(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 : std::as_const(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 : std::as_const(d->mCustomPages)) {
268  plugin->setReadOnly(readOnly);
269  }
270  }
271 }
272 
273 bool ContactEditorWidget::hasNoSavedData() const
274 {
275  return d->mAddressesLocationWidget->hasNoSavedData();
276 }
virtual void setReadOnly(bool readOnly)=0
This method is called to set the editor widget readOnly.
A widget for editing a contact.
QString custom(const QString &app, const QString &name) const
void storeContact(KContacts::Addressee &contact, ContactEditor::ContactMetaDataBase &metaData) const override
Stores back the fields of the contact editor into the given contact.
@ FullMode
Show all pages.
ContactEditorWidget(QWidget *parent=nullptr)
Creates a new contact editor widget.
QString note() const
~ContactEditorWidget() override
Destroys the contact editor widget.
void loadContact(const KContacts::Addressee &contact, const ContactEditor::ContactMetaDataBase &metaData) override
Initializes the fields of the contact editor with the values from a contact.
QStringList libraryPaths()
virtual void storeContact(KContacts::Addressee &contact) const =0
This method is called to store the data from the editor widget into contact.
void setAcceptRichText(bool accept)
The base class for custom ContactEditor page plugins.
bool isEmpty() const const
DisplayType
Describes what the display name should look like.
void setReadOnly(bool readOnly) override
Sets whether the contact in the editor allows the user to edit the contact or not.
void setNote(const QString &note)
void insertCustom(const QString &app, const QString &name, const QString &value)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void removeCustom(const QString &app, const QString &name)
virtual void loadContact(const KContacts::Addressee &contact)=0
This method is called to fill the editor widget with the data from contact.
virtual QString title() const =0
Returns the i18n'd page title.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sat Apr 1 2023 04:09:04 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.