Akonadi Contacts

contactgroupeditordialog.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 "contactgroupeditordialog.h"
10 
11 #include "contactgroupeditor.h"
12 #include "contactgroupeditor_p.h"
13 
14 #include <KLocalizedString>
15 #include <collectioncombobox.h>
16 #include <item.h>
17 #include <kcontacts/contactgroup.h>
18 
19 #include <KConfig>
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 ContactGroupEditorDialog::Private
32 {
33 public:
35  : q(qq)
36  , mMode(mode)
37  {
38  }
39 
40  void slotGroupNameChanged(const QString &name)
41  {
42  bool isValid = !(name.contains(QLatin1Char('@')) || name.contains(QLatin1Char('.')));
43  okButton->setEnabled(!name.trimmed().isEmpty() && isValid);
44  mEditor->groupNameIsValid(isValid);
45  }
46 
47  void readConfig()
48  {
49  KConfig config(QStringLiteral("akonadi_contactrc"));
50  KConfigGroup group(&config, QStringLiteral("ContactGroupEditorDialog"));
51  const QSize size = group.readEntry("Size", QSize(470, 400));
52  if (size.isValid()) {
53  q->resize(size);
54  }
55  }
56 
57  void writeConfig()
58  {
59  KConfig config(QStringLiteral("akonadi_contactrc"));
60  KConfigGroup group(&config, QStringLiteral("ContactGroupEditorDialog"));
61  group.writeEntry("Size", q->size());
62  group.sync();
63  }
64 
65  ContactGroupEditorDialog *const q;
66  CollectionComboBox *mAddressBookBox = nullptr;
67  ContactGroupEditor *mEditor = nullptr;
69  QPushButton *okButton = nullptr;
70 };
71 
73  : QDialog(parent)
74  , d(new Private(this, mode))
75 {
76  setWindowTitle(mode == CreateMode ? i18n("New Contact Group") : i18n("Edit Contact Group"));
77  auto mainLayout = new QVBoxLayout(this);
78 
80  d->okButton = buttonBox->button(QDialogButtonBox::Ok);
81  d->okButton->setDefault(true);
82  d->okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
83  connect(buttonBox, &QDialogButtonBox::accepted, this, &ContactGroupEditorDialog::slotAccepted);
84  connect(buttonBox, &QDialogButtonBox::rejected, this, &ContactGroupEditorDialog::reject);
85 
86  // Disable default button, so that finish editing of
87  // a member with the Enter key does not close the dialog
88  d->okButton->setAutoDefault(false);
89  buttonBox->button(QDialogButtonBox::Cancel)->setAutoDefault(false);
90 
91  auto mainWidget = new QWidget(this);
92 
93  auto layout = new QGridLayout(mainWidget);
94  layout->setContentsMargins(0, 0, 0, 0);
95 
97 
98  if (mode == CreateMode) {
99  auto label = new QLabel(i18n("Add to:"), mainWidget);
100 
101  d->mAddressBookBox = new CollectionComboBox(mainWidget);
102  d->mAddressBookBox->setMimeTypeFilter(QStringList() << KContacts::ContactGroup::mimeType());
103  d->mAddressBookBox->setAccessRightsFilter(Collection::CanCreateItem);
104 
105  layout->addWidget(label, 0, 0);
106  layout->addWidget(d->mAddressBookBox, 0, 1);
107  }
108 
109  layout->addWidget(d->mEditor, 1, 0, 1, 2);
110  layout->setColumnStretch(1, 1);
111 
113  connect(d->mEditor->d->mGui.groupName, &QLineEdit::textChanged, this, [this](const QString &str) {
114  d->slotGroupNameChanged(str);
115  });
116 
117  d->okButton->setEnabled(!d->mEditor->d->mGui.groupName->text().trimmed().isEmpty());
118  mainLayout->addWidget(mainWidget);
119  mainLayout->addWidget(buttonBox);
120  d->readConfig();
121 }
122 
124 {
125  d->writeConfig();
126  delete d;
127 }
128 
130 {
131  d->mEditor->loadContactGroup(group);
132 }
133 
135 {
136  if (d->mMode == EditMode) {
137  return;
138  }
139 
140  d->mAddressBookBox->setDefaultCollection(addressbook);
141 }
142 
144 {
145  return d->mEditor;
146 }
147 
148 void ContactGroupEditorDialog::slotAccepted()
149 {
150  if (d->mAddressBookBox) {
151  d->mEditor->setDefaultAddressBook(d->mAddressBookBox->currentCollection());
152  }
153 
154  if (d->mEditor->saveContactGroup()) {
155  accept();
156  }
157 }
158 
159 void ContactGroupEditorDialog::reject()
160 {
161  if (KMessageBox::questionYesNo(this, i18nc("@info", "Do you really want to cancel?"), i18nc("@title:window", "Confirmation")) == KMessageBox::Yes) {
162  QDialog::reject(); // Discard current changes
163  }
164 }
165 
166 #include "moc_contactgroupeditordialog.cpp"
QLayout * layout() const const
ContactGroupEditor * editor() const
Returns the ContactGroupEditor that is used by the dialog.
bool isValid() const const
void setContentsMargins(int left, int top, int right, int bottom)
virtual void reject()
ContactGroupEditorDialog(Mode mode, QWidget *parent=nullptr)
Creates a new contact group editor dialog.
void textChanged(const QString &text)
void setContactGroup(const Akonadi::Item &group)
Sets the contact group to edit when in EditMode.
~ContactGroupEditorDialog() override
Destroys the contact group editor dialog.
KSharedConfigPtr config()
void contactGroupStored(const Akonadi::Item &group)
This signal is emitted when the contact group has been saved back to the storage. ...
QString i18nc(const char *context, const char *text, const TYPE &arg...)
static QString mimeType()
bool isEmpty() const const
QString trimmed() const const
A dialog for creating or editing a contact group in Akonadi.
void addWidget(QWidget *w)
virtual void accept()
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)
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
void setDefaultAddressBook(const Akonadi::Collection &addressbook)
Sets the addressbook that shall be selected as default for storage in create mode.
An widget to edit contact groups in Akonadi.
QString i18n(const char *text, const TYPE &arg...)
void readConfig()
Edits an existing contact group.
void contactGroupStored(const Akonadi::Item &group)
This signal is emitted whenever a contact group was updated or stored.
bool isValid(QStringView ifopt)
void setWindowTitle(const QString &)
QWidget(QWidget *parent, Qt::WindowFlags f)
void setDefaultAddressBook(const Akonadi::Collection &addressbook)
Sets the addressbook which shall be used to store new contact groups.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Key_Return
Mode
Describes the mode of the contact group editor.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Thu Jun 17 2021 23:09:01 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.