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 <collectioncombobox.h>
15 #include <item.h>
16 #include <kcontacts/contactgroup.h>
17 #include <KLocalizedString>
18 
19 #include <KConfig>
20 #include <KMessageBox>
21 
22 #include <QPushButton>
23 #include <QGridLayout>
24 #include <QLabel>
25 #include <QDialogButtonBox>
26 #include <KConfigGroup>
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);
90 
91  QWidget *mainWidget = new QWidget(this);
92 
93  auto *layout = new QGridLayout(mainWidget);
94  layout->setContentsMargins(0, 0, 0, 0);
95 
96  d->mEditor = new Akonadi::ContactGroupEditor(mode == CreateMode
98  this);
99 
100  if (mode == CreateMode) {
101  QLabel *label = new QLabel(i18n("Add to:"), mainWidget);
102 
103  d->mAddressBookBox = new CollectionComboBox(mainWidget);
104  d->mAddressBookBox->setMimeTypeFilter(QStringList() << KContacts::ContactGroup::mimeType());
105  d->mAddressBookBox->setAccessRightsFilter(Collection::CanCreateItem);
106 
107  layout->addWidget(label, 0, 0);
108  layout->addWidget(d->mAddressBookBox, 0, 1);
109  }
110 
111  layout->addWidget(d->mEditor, 1, 0, 1, 2);
112  layout->setColumnStretch(1, 1);
113 
115  connect(d->mEditor->d->mGui.groupName, &QLineEdit::textChanged,
116  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 
127 {
128  d->writeConfig();
129  delete d;
130 }
131 
132 void ContactGroupEditorDialog::setContactGroup(const Akonadi::Item &group)
133 {
134  d->mEditor->loadContactGroup(group);
135 }
136 
138 {
139  if (d->mMode == EditMode) {
140  return;
141  }
142 
143  d->mAddressBookBox->setDefaultCollection(addressbook);
144 }
145 
147 {
148  return d->mEditor;
149 }
150 
151 void ContactGroupEditorDialog::slotAccepted()
152 {
153  if (d->mAddressBookBox) {
154  d->mEditor->setDefaultAddressBook(d->mAddressBookBox->currentCollection());
155  }
156 
157  if (d->mEditor->saveContactGroup()) {
158  accept();
159  }
160 }
161 
162 void ContactGroupEditorDialog::reject()
163 {
165  this,
166  i18nc("@info", "Do you really want to cancel?"),
167  i18nc("@title:window", "Confirmation")) == KMessageBox::Yes) {
168  QDialog::reject(); // Discard current changes
169  }
170 }
171 
172 
173 
174 #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.
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.
~ContactGroupEditorDialog()
Destroys the contact group editor dialog.
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.
void setWindowTitle(const QString &)
QWidget(QWidget *parent, Qt::WindowFlags f)
QPushButton * button(QDialogButtonBox::StandardButton which) const const
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)
void setDefault(bool)
Key_Return
void setAutoDefault(bool)
Mode
Describes the mode of the contact group editor.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed Nov 25 2020 23:07:22 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.