File indexing completed on 2024-11-17 04:40:41
0001 /* 0002 This file is part of Akonadi Contact. 0003 0004 SPDX-FileCopyrightText: 2007-2009 Tobias Koenig <tokoe@kde.org> 0005 0006 SPDX-License-Identifier: LGPL-2.0-or-later 0007 */ 0008 0009 #include "contactgroupeditordialog.h" 0010 0011 #include "contactgroupeditor.h" 0012 #include "contactgroupeditor_p.h" 0013 #include <Akonadi/CollectionComboBox> 0014 #include <Akonadi/Item> 0015 #include <KContacts/ContactGroup> 0016 #include <KLocalizedString> 0017 0018 #include <KConfig> 0019 #include <KMessageBox> 0020 0021 #include <KConfigGroup> 0022 #include <QDialogButtonBox> 0023 #include <QGridLayout> 0024 #include <QLabel> 0025 #include <QPushButton> 0026 #include <QVBoxLayout> 0027 0028 using namespace Akonadi; 0029 0030 namespace 0031 { 0032 static const char myContactGroupEditorDialogGroupName[] = "ContactGroupEditorDialog"; 0033 } 0034 class Akonadi::ContactGroupEditorDialogPrivate 0035 { 0036 public: 0037 ContactGroupEditorDialogPrivate(ContactGroupEditorDialog *qq, ContactGroupEditorDialog::Mode mode) 0038 : q(qq) 0039 , mMode(mode) 0040 { 0041 } 0042 0043 void slotGroupNameChanged(const QString &name) 0044 { 0045 bool isValid = !(name.contains(QLatin1Char('@')) || name.contains(QLatin1Char('.'))); 0046 okButton->setEnabled(!name.trimmed().isEmpty() && isValid); 0047 mEditor->groupNameIsValid(isValid); 0048 } 0049 0050 void readConfig() 0051 { 0052 KConfig config(QStringLiteral("akonadi_contactrc")); 0053 KConfigGroup group(&config, QLatin1StringView(myContactGroupEditorDialogGroupName)); 0054 const QSize size = group.readEntry("Size", QSize(470, 400)); 0055 if (size.isValid()) { 0056 q->resize(size); 0057 } 0058 } 0059 0060 void writeConfig() 0061 { 0062 KConfig config(QStringLiteral("akonadi_contactrc")); 0063 KConfigGroup group(&config, QLatin1StringView(myContactGroupEditorDialogGroupName)); 0064 group.writeEntry("Size", q->size()); 0065 group.sync(); 0066 } 0067 0068 ContactGroupEditorDialog *const q; 0069 Akonadi::CollectionComboBox *mAddressBookBox = nullptr; 0070 ContactGroupEditor *mEditor = nullptr; 0071 const ContactGroupEditorDialog::Mode mMode; 0072 QPushButton *okButton = nullptr; 0073 }; 0074 0075 ContactGroupEditorDialog::ContactGroupEditorDialog(Mode mode, QWidget *parent) 0076 : QDialog(parent) 0077 , d(new ContactGroupEditorDialogPrivate(this, mode)) 0078 { 0079 setWindowTitle(mode == CreateMode ? i18nc("@title:window", "New Contact Group") : i18nc("@title:window", "Edit Contact Group")); 0080 auto mainLayout = new QVBoxLayout(this); 0081 0082 auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this); 0083 d->okButton = buttonBox->button(QDialogButtonBox::Ok); 0084 d->okButton->setDefault(true); 0085 d->okButton->setShortcut(Qt::CTRL | Qt::Key_Return); 0086 connect(buttonBox, &QDialogButtonBox::accepted, this, &ContactGroupEditorDialog::slotAccepted); 0087 connect(buttonBox, &QDialogButtonBox::rejected, this, &ContactGroupEditorDialog::reject); 0088 0089 // Disable default button, so that finish editing of 0090 // a member with the Enter key does not close the dialog 0091 d->okButton->setAutoDefault(false); 0092 buttonBox->button(QDialogButtonBox::Cancel)->setAutoDefault(false); 0093 0094 auto mainWidget = new QWidget(this); 0095 0096 auto layout = new QGridLayout(mainWidget); 0097 layout->setContentsMargins({}); 0098 0099 d->mEditor = new Akonadi::ContactGroupEditor(mode == CreateMode ? Akonadi::ContactGroupEditor::CreateMode : Akonadi::ContactGroupEditor::EditMode, this); 0100 0101 if (mode == CreateMode) { 0102 auto label = new QLabel(i18n("Add to:"), mainWidget); 0103 0104 d->mAddressBookBox = new Akonadi::CollectionComboBox(mainWidget); 0105 d->mAddressBookBox->setMimeTypeFilter(QStringList() << KContacts::ContactGroup::mimeType()); 0106 d->mAddressBookBox->setAccessRightsFilter(Akonadi::Collection::CanCreateItem); 0107 0108 layout->addWidget(label, 0, 0); 0109 layout->addWidget(d->mAddressBookBox, 0, 1); 0110 } 0111 0112 layout->addWidget(d->mEditor, 1, 0, 1, 2); 0113 layout->setColumnStretch(1, 1); 0114 0115 connect(d->mEditor, &ContactGroupEditor::contactGroupStored, this, &ContactGroupEditorDialog::contactGroupStored); 0116 connect(d->mEditor->d->mGui.groupName, &QLineEdit::textChanged, this, [this](const QString &str) { 0117 d->slotGroupNameChanged(str); 0118 }); 0119 0120 d->okButton->setEnabled(!d->mEditor->d->mGui.groupName->text().trimmed().isEmpty()); 0121 mainLayout->addWidget(mainWidget); 0122 mainLayout->addWidget(buttonBox); 0123 d->readConfig(); 0124 } 0125 0126 ContactGroupEditorDialog::~ContactGroupEditorDialog() 0127 { 0128 d->writeConfig(); 0129 } 0130 0131 void ContactGroupEditorDialog::setContactGroup(const Akonadi::Item &group) 0132 { 0133 d->mEditor->loadContactGroup(group); 0134 } 0135 0136 void ContactGroupEditorDialog::setDefaultAddressBook(const Akonadi::Collection &addressbook) 0137 { 0138 if (d->mMode == EditMode) { 0139 return; 0140 } 0141 0142 d->mAddressBookBox->setDefaultCollection(addressbook); 0143 } 0144 0145 ContactGroupEditor *ContactGroupEditorDialog::editor() const 0146 { 0147 return d->mEditor; 0148 } 0149 0150 void ContactGroupEditorDialog::slotAccepted() 0151 { 0152 if (d->mAddressBookBox) { 0153 d->mEditor->setDefaultAddressBook(d->mAddressBookBox->currentCollection()); 0154 } 0155 0156 if (d->mEditor->saveContactGroup()) { 0157 accept(); 0158 } 0159 } 0160 0161 void ContactGroupEditorDialog::reject() 0162 { 0163 const int answer = KMessageBox::questionTwoActions(this, 0164 i18nc("@info", "Do you really want to cancel?"), 0165 i18nc("@title:window", "Confirmation"), 0166 KGuiItem(i18nc("@action:button", "Cancel Editing"), QStringLiteral("dialog-ok")), 0167 KGuiItem(i18nc("@action:button", "Do Not Cancel"), QStringLiteral("dialog-cancel"))); 0168 0169 if (answer == KMessageBox::ButtonCode::PrimaryAction) { 0170 QDialog::reject(); // Discard current changes 0171 } 0172 } 0173 0174 #include "moc_contactgroupeditordialog.cpp"