Akonadi Contacts

contacteditordialog.cpp
1/*
2 This file is part of Akonadi Contact.
3
4 SPDX-FileCopyrightText: 2007-2009 Tobias Koenig <tokoe@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "contacteditordialog.h"
10
11#include "contacteditor.h"
12#include <Akonadi/CollectionComboBox>
13#include <Akonadi/Item>
14
15#include <KContacts/Addressee>
16
17#include <KConfig>
18#include <KLocalizedString>
19#include <KMessageBox>
20
21#include <KConfigGroup>
22#include <QDialogButtonBox>
23#include <QGridLayout>
24#include <QLabel>
25#include <QPushButton>
26#include <QVBoxLayout>
27
28using namespace Akonadi;
29namespace
30{
31static const char myContactEditorDialogDialogGroupName[] = "ContactEditor";
32}
33class Akonadi::ContactEditorDialogPrivate
34{
35public:
36 ContactEditorDialogPrivate(ContactEditorDialog::Mode mode,
37 ContactEditorDialog::DisplayMode displaymode,
38 Akonadi::AbstractContactEditorWidget *editorWidget,
39 ContactEditorDialog *parent)
40 : q(parent)
41 , mMode(mode)
42 {
43 auto mainWidget = new QWidget(q);
44
45 q->setWindowTitle(mode == ContactEditorDialog::CreateMode ? i18nc("@title:window", "New Contact") : i18nc("@title:window", "Edit Contact"));
46 auto mainLayout = new QVBoxLayout(q);
48 q->connect(buttonBox, &QDialogButtonBox::accepted, q, [this]() {
49 slotOkClicked();
50 });
51 q->connect(buttonBox, &QDialogButtonBox::rejected, q, [this]() {
52 slotCancelClicked();
53 });
54
55 mainLayout->addWidget(mainWidget);
56 mainLayout->addWidget(buttonBox);
57
58 auto layout = new QGridLayout(mainWidget);
59 layout->setContentsMargins({});
60
61 if (editorWidget) {
63 editorWidget,
64 q);
65 } else {
67 displaymode == ContactEditorDialog::FullMode ? AkonadiContactEditor::FullMode : AkonadiContactEditor::VCardMode,
68 q);
69 }
70
72 auto label = new QLabel(i18nc("@label:textbox", "Add to:"), mainWidget);
73
74 mAddressBookBox = new Akonadi::CollectionComboBox(mainWidget);
77
78 layout->addWidget(label, 0, 0);
79 layout->addWidget(mAddressBookBox, 0, 1);
80 }
81
82 layout->addWidget(mEditor, 1, 0, 1, 2);
83 layout->setColumnStretch(1, 1);
84
86
88
90 slotFinish();
91 });
92
93 readConfig();
94 }
95
96 void slotOkClicked()
97 {
98 q->accept();
99 }
100
101 void slotFinish()
102 {
103 q->QDialog::accept();
104 }
105
106 void slotCancelClicked()
107 {
108 q->reject();
109 }
110
111 void readConfig()
112 {
113 KConfig config(QStringLiteral("akonadi_contactrc"));
114 KConfigGroup group(&config, QLatin1StringView(myContactEditorDialogDialogGroupName));
115 const QSize size = group.readEntry("Size", QSize(800, 500));
116 if (size.isValid()) {
117 q->resize(size);
118 }
119 }
120
121 void writeConfig()
122 {
123 KConfig config(QStringLiteral("akonadi_contactrc"));
124 KConfigGroup group(&config, QLatin1StringView(myContactEditorDialogDialogGroupName));
125 group.writeEntry("Size", q->size());
126 group.sync();
127 }
128
129 ContactEditorDialog *const q;
130 Akonadi::CollectionComboBox *mAddressBookBox = nullptr;
131 const ContactEditorDialog::Mode mMode;
132 Akonadi::AkonadiContactEditor *mEditor = nullptr;
133};
134
136 : QDialog(parent)
137 , d(new ContactEditorDialogPrivate(mode, FullMode, nullptr, this))
138{
139}
140
141ContactEditorDialog::ContactEditorDialog(Mode mode, Akonadi::AbstractContactEditorWidget *editorWidget, QWidget *parent)
142 : QDialog(parent)
143 , d(new ContactEditorDialogPrivate(mode, FullMode, editorWidget, this))
144{
145}
146
147ContactEditorDialog::ContactEditorDialog(Mode mode, DisplayMode displayMode, QWidget *parent)
148 : QDialog(parent)
149 , d(new ContactEditorDialogPrivate(mode, displayMode, nullptr, this))
150{
151}
152
154{
155 d->writeConfig();
156}
157
159{
160 d->mEditor->loadContact(contact);
161}
162
164{
165 if (d->mMode == EditMode) {
166 return;
167 }
168
169 d->mAddressBookBox->setDefaultCollection(addressbook);
170}
171
173{
174 return d->mEditor;
175}
176
177void ContactEditorDialog::accept()
178{
179 if (d->mEditor->hasNoSavedData()) {
180 const int answer = KMessageBox::questionTwoActions(this,
181 i18nc("@info", "Location was not saved. Do you want to close editor?"),
182 i18nc("@title:window", "Confirmation"),
183 KGuiItem(i18nc("@action:button", "Close Editor"), QStringLiteral("dialog-close")),
184 KGuiItem(i18nc("@action:button", "Do Not Close"), QStringLiteral("dialog-cancel")));
185 if (answer == KMessageBox::ButtonCode::SecondaryAction) {
186 return;
187 }
188 }
189 if (d->mAddressBookBox) {
190 d->mEditor->setDefaultAddressBook(d->mAddressBookBox->currentCollection());
191 }
192 d->mEditor->saveContactInAddressBook();
193}
194
195void ContactEditorDialog::reject()
196{
197 if (d->mEditor->hasNoSavedData()) {
198 const int answer = KMessageBox::questionTwoActions(this,
199 i18nc("@info", "Do you really want to cancel?"),
200 i18nc("@title:window", "Confirmation"),
201 KGuiItem(i18nc("@action:button", "Cancel Editing"), QStringLiteral("dialog-ok")),
202 KGuiItem(i18nc("@action:button", "Do Not Cancel"), QStringLiteral("dialog-cancel")));
203
204 if (answer == KMessageBox::ButtonCode::PrimaryAction) {
205 QDialog::reject(); // Discard current changes
206 }
207 } else {
209 }
210}
211
212#include "moc_contacteditordialog.cpp"
An widget to edit contacts in Akonadi.
void contactStored(const Akonadi::Item &contact)
This signal is emitted when the contact has been saved back to the storage.
@ VCardMode
Show just pages with elements stored in vcard.
void saveContactInAddressBook()
Save the contact from the editor back to the storage.
void error(const QString &errorMsg)
This signal is emitted when an error occurred during the save.
@ EditMode
Edits an existing contact.
@ CreateMode
Creates a new contact.
void setMimeTypeFilter(const QStringList &mimetypes)
void setAccessRightsFilter(Collection::Rights rights)
A dialog for creating or editing a contact in Akonadi.
~ContactEditorDialog() override
Destroys the contact editor dialog.
Mode
Describes the mode of the editor dialog.
@ EditMode
Edits an existing contact.
@ CreateMode
Creates a new contact.
ContactEditorDialog(Mode mode, QWidget *parent=nullptr)
Creates a new contact editor dialog with the standard editor widget.
void setContact(const Akonadi::Item &contact)
Sets the contact to edit when in EditMode.
void error(const QString &errMsg)
This signal is emitted whenever a contact is not updated or stored.
void setDefaultAddressBook(const Akonadi::Collection &addressbook)
Sets the addressbook that shall be selected as default in create mode.
void contactStored(const Akonadi::Item &contact)
This signal is emitted whenever a contact was updated or stored.
Akonadi::AkonadiContactEditor * editor() const
Returns the ContactEditor that is used by this dialog.
static QString mimeType()
QString i18nc(const char *context, const char *text, const TYPE &arg...)
A widget for editing the display name of a contact.
ButtonCode questionTwoActions(QWidget *parent, const QString &text, const QString &title, const KGuiItem &primaryAction, const KGuiItem &secondaryAction, const QString &dontAskAgainName=QString(), Options options=Notify)
QString label(StandardShortcut id)
virtual void reject()
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool isValid() const const
void resize(const QSize &)
void setWindowTitle(const QString &)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:08:08 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.