Akonadi Contacts

contacteditordialog.cpp
1 /*
2  This file is part of Akonadi Contact.
3 
4  SPDX-FileCopyrightText: 2007-2009 Tobias Koenig <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8 
9 #include "contacteditordialog.h"
10 
11 #include "contacteditor.h"
12 
13 #include <collectioncombobox.h>
14 #include <item.h>
15 
16 #include <kcontacts/addressee.h>
17 
18 #include <KConfig>
19 #include <KLocalizedString>
20 #include <KMessageBox>
21 
22 #include <KConfigGroup>
23 #include <QDialogButtonBox>
24 #include <QGridLayout>
25 #include <QLabel>
26 #include <QPushButton>
27 #include <QVBoxLayout>
28 
29 using namespace Akonadi;
30 
31 class Q_DECL_HIDDEN ContactEditorDialog::Private
32 {
33 public:
34  Private(ContactEditorDialog::Mode mode,
35  ContactEditorDialog::DisplayMode displaymode,
36  ContactEditor::AbstractContactEditorWidget *editorWidget,
37  ContactEditorDialog *parent)
38  : q(parent)
39  , mMode(mode)
40  {
41  auto mainWidget = new QWidget(q);
42 
43  q->setWindowTitle(mode == ContactEditorDialog::CreateMode ? i18n("New Contact") : i18n("Edit Contact"));
44  auto mainLayout = new QVBoxLayout(q);
46  q->connect(buttonBox, &QDialogButtonBox::accepted, q, [this]() {
47  slotOkClicked();
48  });
49  q->connect(buttonBox, &QDialogButtonBox::rejected, q, [this]() {
50  slotCancelClicked();
51  });
52 
53  mainLayout->addWidget(mainWidget);
54  mainLayout->addWidget(buttonBox);
55 
56  auto layout = new QGridLayout(mainWidget);
57  layout->setContentsMargins(0, 0, 0, 0);
58 
59  if (editorWidget) {
61  editorWidget,
62  q);
63  } else {
65  displaymode == ContactEditorDialog::FullMode ? AkonadiContactEditor::FullMode : AkonadiContactEditor::VCardMode,
66  q);
67  }
68 
69  if (mode == ContactEditorDialog::CreateMode) {
70  auto label = new QLabel(i18n("Add to:"), mainWidget);
71 
72  mAddressBookBox = new CollectionComboBox(mainWidget);
73  mAddressBookBox->setMimeTypeFilter(QStringList() << KContacts::Addressee::mimeType());
74  mAddressBookBox->setAccessRightsFilter(Collection::CanCreateItem);
75 
76  layout->addWidget(label, 0, 0);
77  layout->addWidget(mAddressBookBox, 0, 1);
78  }
79 
80  layout->addWidget(mEditor, 1, 0, 1, 2);
81  layout->setColumnStretch(1, 1);
82 
84 
86 
87  connect(mEditor, &AkonadiContactEditor::finished, q, [this]() {
88  slotFinish();
89  });
90 
91  readConfig();
92  }
93 
94  void slotOkClicked()
95  {
96  q->accept();
97  }
98 
99  void slotFinish()
100  {
101  q->QDialog::accept();
102  }
103 
104  void slotCancelClicked()
105  {
106  q->reject();
107  }
108 
109  void readConfig()
110  {
111  KConfig config(QStringLiteral("akonadi_contactrc"));
112  KConfigGroup group(&config, QStringLiteral("ContactEditor"));
113  const QSize size = group.readEntry("Size", QSize(800, 500));
114  if (size.isValid()) {
115  q->resize(size);
116  }
117  }
118 
119  void writeConfig()
120  {
121  KConfig config(QStringLiteral("akonadi_contactrc"));
122  KConfigGroup group(&config, QStringLiteral("ContactEditor"));
123  group.writeEntry("Size", q->size());
124  group.sync();
125  }
126 
127  ContactEditorDialog *const q;
128  CollectionComboBox *mAddressBookBox = nullptr;
130  AkonadiContactEditor *mEditor = nullptr;
131 };
132 
134  : QDialog(parent)
135  , d(new Private(mode, FullMode, nullptr, this))
136 {
137 }
138 
139 ContactEditorDialog::ContactEditorDialog(Mode mode, ContactEditor::AbstractContactEditorWidget *editorWidget, QWidget *parent)
140  : QDialog(parent)
141  , d(new Private(mode, FullMode, editorWidget, this))
142 {
143 }
144 
145 ContactEditorDialog::ContactEditorDialog(Mode mode, DisplayMode displayMode, QWidget *parent)
146  : QDialog(parent)
147  , d(new Private(mode, displayMode, nullptr, this))
148 {
149 }
150 
152 {
153  d->writeConfig();
154  delete d;
155 }
156 
158 {
159  d->mEditor->loadContact(contact);
160 }
161 
163 {
164  if (d->mMode == EditMode) {
165  return;
166  }
167 
168  d->mAddressBookBox->setDefaultCollection(addressbook);
169 }
170 
172 {
173  return d->mEditor;
174 }
175 
176 void ContactEditorDialog::accept()
177 {
178  if (d->mEditor->hasNoSavedData()) {
179  if (KMessageBox::questionYesNo(this, i18nc("@info", "Location was not saved. Do you want to close editor?"), i18nc("@title:window", "Confirmation"))
180  == KMessageBox::No) {
181  return;
182  }
183  }
184  if (d->mAddressBookBox) {
185  d->mEditor->setDefaultAddressBook(d->mAddressBookBox->currentCollection());
186  }
187  d->mEditor->saveContactInAddressBook();
188 }
189 
190 void ContactEditorDialog::reject()
191 {
192  if (KMessageBox::questionYesNo(this, i18nc("@info", "Do you really want to cancel?"), i18nc("@title:window", "Confirmation")) == KMessageBox::Yes) {
193  QDialog::reject(); // Discard current changes
194  }
195 }
196 
197 #include "moc_contacteditordialog.cpp"
static QString mimeType()
A dialog for creating or editing a contact in Akonadi.
bool isValid() const const
virtual void reject()
Show just pages with elements stored in vcard.
Definition: contacteditor.h:84
void contactStored(const Akonadi::Item &contact)
This signal is emitted whenever a contact was updated or stored.
void contactStored(const Akonadi::Item &contact)
This signal is emitted when the contact has been saved back to the storage.
Mode
Describes the mode of the editor dialog.
KSharedConfigPtr config()
void setContact(const Akonadi::Item &contact)
Sets the contact to edit when in EditMode.
void setDefaultAddressBook(const Akonadi::Collection &addressbook)
Sets the addressbook which shall be used to store new contacts.
void error(const QString &errorMsg)
This signal is emitted when an error occurred during the save.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
ContactEditorDialog(Mode mode, QWidget *parent=nullptr)
Creates a new contact editor dialog with the standard editor widget.
QString label(StandardShortcut id)
void error(const QString &errMsg)
This signal is emitted whenever a contact is not updated or stored.
AkonadiContactEditor * editor() const
Returns the ContactEditor that is used by this dialog.
void setDefaultAddressBook(const Akonadi::Collection &addressbook)
Sets the addressbook that shall be selected as default in create mode.
ButtonCode questionYesNo(QWidget *parent, const QString &text, const QString &caption=QString(), const KGuiItem &buttonYes=KStandardGuiItem::yes(), const KGuiItem &buttonNo=KStandardGuiItem::no(), const QString &dontAskAgainName=QString(), Options options=Notify)
QString i18n(const char *text, const TYPE &arg...)
void readConfig()
Edits an existing contact.
Definition: contacteditor.h:79
An widget to edit contacts in Akonadi.
Definition: contacteditor.h:69
~ContactEditorDialog() override
Destroys the contact editor dialog.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Wed Jun 23 2021 23:09:24 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.