Akonadi Contacts

contactgroupeditordialog.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 "contactgroupeditordialog.h"
10
11#include "contactgroupeditor.h"
12#include "contactgroupeditor_p.h"
13#include <Akonadi/CollectionComboBox>
14#include <Akonadi/Item>
15#include <KContacts/ContactGroup>
16#include <KLocalizedString>
17
18#include <KConfig>
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;
29
30namespace
31{
32static const char myContactGroupEditorDialogGroupName[] = "ContactGroupEditorDialog";
33}
34class Akonadi::ContactGroupEditorDialogPrivate
35{
36public:
37 ContactGroupEditorDialogPrivate(ContactGroupEditorDialog *qq, ContactGroupEditorDialog::Mode mode)
38 : q(qq)
39 , mMode(mode)
40 {
41 }
42
43 void slotGroupNameChanged(const QString &name)
44 {
45 bool isValid = !(name.contains(QLatin1Char('@')) || name.contains(QLatin1Char('.')));
46 okButton->setEnabled(!name.trimmed().isEmpty() && isValid);
47 mEditor->groupNameIsValid(isValid);
48 }
49
50 void readConfig()
51 {
52 KConfig config(QStringLiteral("akonadi_contactrc"));
53 KConfigGroup group(&config, QLatin1StringView(myContactGroupEditorDialogGroupName));
54 const QSize size = group.readEntry("Size", QSize(470, 400));
55 if (size.isValid()) {
56 q->resize(size);
57 }
58 }
59
60 void writeConfig()
61 {
62 KConfig config(QStringLiteral("akonadi_contactrc"));
63 KConfigGroup group(&config, QLatin1StringView(myContactGroupEditorDialogGroupName));
64 group.writeEntry("Size", q->size());
65 group.sync();
66 }
67
69 Akonadi::CollectionComboBox *mAddressBookBox = nullptr;
70 ContactGroupEditor *mEditor = nullptr;
72 QPushButton *okButton = nullptr;
73};
74
76 : QDialog(parent)
77 , d(new ContactGroupEditorDialogPrivate(this, mode))
78{
79 setWindowTitle(mode == CreateMode ? i18nc("@title:window", "New Contact Group") : i18nc("@title:window", "Edit Contact Group"));
80 auto mainLayout = new QVBoxLayout(this);
81
83 d->okButton = buttonBox->button(QDialogButtonBox::Ok);
84 d->okButton->setDefault(true);
85 d->okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
86 connect(buttonBox, &QDialogButtonBox::accepted, this, &ContactGroupEditorDialog::slotAccepted);
87 connect(buttonBox, &QDialogButtonBox::rejected, this, &ContactGroupEditorDialog::reject);
88
89 // Disable default button, so that finish editing of
90 // a member with the Enter key does not close the dialog
91 d->okButton->setAutoDefault(false);
92 buttonBox->button(QDialogButtonBox::Cancel)->setAutoDefault(false);
93
94 auto mainWidget = new QWidget(this);
95
96 auto layout = new QGridLayout(mainWidget);
98
100
101 if (mode == CreateMode) {
102 auto label = new QLabel(i18nc("@label:textbox", "Add to:"), mainWidget);
103
104 d->mAddressBookBox = new Akonadi::CollectionComboBox(mainWidget);
105 d->mAddressBookBox->setMimeTypeFilter(QStringList() << KContacts::ContactGroup::mimeType());
106 d->mAddressBookBox->setAccessRightsFilter(Akonadi::Collection::CanCreateItem);
107
108 layout->addWidget(label, 0, 0);
109 layout->addWidget(d->mAddressBookBox, 0, 1);
110 }
111
112 layout->addWidget(d->mEditor, 1, 0, 1, 2);
113 layout->setColumnStretch(1, 1);
114
116 connect(d->mEditor->d->mGui.groupName, &QLineEdit::textChanged, this, [this](const QString &str) {
117 d->slotGroupNameChanged(str);
118 });
119
120 d->okButton->setEnabled(!d->mEditor->d->mGui.groupName->text().trimmed().isEmpty());
121 mainLayout->addWidget(mainWidget);
122 mainLayout->addWidget(buttonBox);
123 d->readConfig();
124}
125
130
132{
133 d->mEditor->loadContactGroup(group);
134}
135
137{
138 if (d->mMode == EditMode) {
139 return;
140 }
141
142 d->mAddressBookBox->setDefaultCollection(addressbook);
143}
144
146{
147 return d->mEditor;
148}
149
150void ContactGroupEditorDialog::slotAccepted()
151{
152 if (d->mAddressBookBox) {
153 d->mEditor->setDefaultAddressBook(d->mAddressBookBox->currentCollection());
154 }
155
156 if (d->mEditor->saveContactGroup()) {
157 accept();
158 }
159}
160
161void ContactGroupEditorDialog::reject()
162{
163 const int answer = KMessageBox::questionTwoActions(this,
164 i18nc("@info", "Do you really want to cancel?"),
165 i18nc("@title:window", "Confirmation"),
166 KGuiItem(i18nc("@action:button", "Cancel Editing"), QStringLiteral("dialog-ok")),
167 KGuiItem(i18nc("@action:button", "Do Not Cancel"), QStringLiteral("dialog-cancel")));
168
169 if (answer == KMessageBox::ButtonCode::PrimaryAction) {
170 QDialog::reject(); // Discard current changes
171 }
172}
173
174#include "moc_contactgroupeditordialog.cpp"
A dialog for creating or editing a contact group in Akonadi.
void contactGroupStored(const Akonadi::Item &group)
This signal is emitted whenever a contact group was updated or stored.
~ContactGroupEditorDialog() override
Destroys the contact group editor dialog.
void setContactGroup(const Akonadi::Item &group)
Sets the contact group to edit when in EditMode.
ContactGroupEditorDialog(Mode mode, QWidget *parent=nullptr)
Creates a new contact group editor dialog.
ContactGroupEditor * editor() const
Returns the ContactGroupEditor that is used by the dialog.
void setDefaultAddressBook(const Akonadi::Collection &addressbook)
Sets the addressbook that shall be selected as default for storage in create mode.
Mode
Describes the mode of the contact group editor.
@ CreateMode
Creates a new contact group.
@ EditMode
Edits an existing contact group.
An widget to edit contact groups in Akonadi.
void contactGroupStored(const Akonadi::Item &group)
This signal is emitted when the contact group has been saved back to the storage.
@ CreateMode
Creates a new contact group.
@ EditMode
Edits an existing contact group.
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)
bool isValid(QStringView ifopt)
QString name(StandardAction id)
virtual void accept()
virtual void reject()
void addWidget(QWidget *w)
void setContentsMargins(const QMargins &margins)
void textChanged(const QString &text)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool isValid() const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
QString trimmed() const const
Key_Return
QWidget(QWidget *parent, Qt::WindowFlags f)
void setEnabled(bool)
QLayout * layout() 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 Sat Dec 21 2024 17:05:46 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.