File indexing completed on 2024-11-17 04:40:40

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 "contacteditordialog.h"
0010 
0011 #include "contacteditor.h"
0012 #include <Akonadi/CollectionComboBox>
0013 #include <Akonadi/Item>
0014 
0015 #include <KContacts/Addressee>
0016 
0017 #include <KConfig>
0018 #include <KLocalizedString>
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 namespace
0030 {
0031 static const char myContactEditorDialogDialogGroupName[] = "ContactEditor";
0032 }
0033 class Akonadi::ContactEditorDialogPrivate
0034 {
0035 public:
0036     ContactEditorDialogPrivate(ContactEditorDialog::Mode mode,
0037                                ContactEditorDialog::DisplayMode displaymode,
0038                                Akonadi::AbstractContactEditorWidget *editorWidget,
0039                                ContactEditorDialog *parent)
0040         : q(parent)
0041         , mMode(mode)
0042     {
0043         auto mainWidget = new QWidget(q);
0044 
0045         q->setWindowTitle(mode == ContactEditorDialog::CreateMode ? i18nc("@title:window", "New Contact") : i18nc("@title:window", "Edit Contact"));
0046         auto mainLayout = new QVBoxLayout(q);
0047         auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, q);
0048         q->connect(buttonBox, &QDialogButtonBox::accepted, q, [this]() {
0049             slotOkClicked();
0050         });
0051         q->connect(buttonBox, &QDialogButtonBox::rejected, q, [this]() {
0052             slotCancelClicked();
0053         });
0054 
0055         mainLayout->addWidget(mainWidget);
0056         mainLayout->addWidget(buttonBox);
0057 
0058         auto layout = new QGridLayout(mainWidget);
0059         layout->setContentsMargins({});
0060 
0061         if (editorWidget) {
0062             mEditor = new AkonadiContactEditor(mode == ContactEditorDialog::CreateMode ? AkonadiContactEditor::CreateMode : AkonadiContactEditor::EditMode,
0063                                                editorWidget,
0064                                                q);
0065         } else {
0066             mEditor = new AkonadiContactEditor(mode == ContactEditorDialog::CreateMode ? AkonadiContactEditor::CreateMode : AkonadiContactEditor::EditMode,
0067                                                displaymode == ContactEditorDialog::FullMode ? AkonadiContactEditor::FullMode : AkonadiContactEditor::VCardMode,
0068                                                q);
0069         }
0070 
0071         if (mode == ContactEditorDialog::CreateMode) {
0072             auto label = new QLabel(i18n("Add to:"), mainWidget);
0073 
0074             mAddressBookBox = new Akonadi::CollectionComboBox(mainWidget);
0075             mAddressBookBox->setMimeTypeFilter(QStringList() << KContacts::Addressee::mimeType());
0076             mAddressBookBox->setAccessRightsFilter(Akonadi::Collection::CanCreateItem);
0077 
0078             layout->addWidget(label, 0, 0);
0079             layout->addWidget(mAddressBookBox, 0, 1);
0080         }
0081 
0082         layout->addWidget(mEditor, 1, 0, 1, 2);
0083         layout->setColumnStretch(1, 1);
0084 
0085         QObject::connect(mEditor, &AkonadiContactEditor::contactStored, q, &ContactEditorDialog::contactStored);
0086 
0087         QObject::connect(mEditor, &AkonadiContactEditor::error, q, &ContactEditorDialog::error);
0088 
0089         QObject::connect(mEditor, &AkonadiContactEditor::finished, q, [this]() {
0090             slotFinish();
0091         });
0092 
0093         readConfig();
0094     }
0095 
0096     void slotOkClicked()
0097     {
0098         q->accept();
0099     }
0100 
0101     void slotFinish()
0102     {
0103         q->QDialog::accept();
0104     }
0105 
0106     void slotCancelClicked()
0107     {
0108         q->reject();
0109     }
0110 
0111     void readConfig()
0112     {
0113         KConfig config(QStringLiteral("akonadi_contactrc"));
0114         KConfigGroup group(&config, QLatin1StringView(myContactEditorDialogDialogGroupName));
0115         const QSize size = group.readEntry("Size", QSize(800, 500));
0116         if (size.isValid()) {
0117             q->resize(size);
0118         }
0119     }
0120 
0121     void writeConfig()
0122     {
0123         KConfig config(QStringLiteral("akonadi_contactrc"));
0124         KConfigGroup group(&config, QLatin1StringView(myContactEditorDialogDialogGroupName));
0125         group.writeEntry("Size", q->size());
0126         group.sync();
0127     }
0128 
0129     ContactEditorDialog *const q;
0130     Akonadi::CollectionComboBox *mAddressBookBox = nullptr;
0131     const ContactEditorDialog::Mode mMode;
0132     Akonadi::AkonadiContactEditor *mEditor = nullptr;
0133 };
0134 
0135 ContactEditorDialog::ContactEditorDialog(Mode mode, QWidget *parent)
0136     : QDialog(parent)
0137     , d(new ContactEditorDialogPrivate(mode, FullMode, nullptr, this))
0138 {
0139 }
0140 
0141 ContactEditorDialog::ContactEditorDialog(Mode mode, Akonadi::AbstractContactEditorWidget *editorWidget, QWidget *parent)
0142     : QDialog(parent)
0143     , d(new ContactEditorDialogPrivate(mode, FullMode, editorWidget, this))
0144 {
0145 }
0146 
0147 ContactEditorDialog::ContactEditorDialog(Mode mode, DisplayMode displayMode, QWidget *parent)
0148     : QDialog(parent)
0149     , d(new ContactEditorDialogPrivate(mode, displayMode, nullptr, this))
0150 {
0151 }
0152 
0153 ContactEditorDialog::~ContactEditorDialog()
0154 {
0155     d->writeConfig();
0156 }
0157 
0158 void ContactEditorDialog::setContact(const Akonadi::Item &contact)
0159 {
0160     d->mEditor->loadContact(contact);
0161 }
0162 
0163 void ContactEditorDialog::setDefaultAddressBook(const Akonadi::Collection &addressbook)
0164 {
0165     if (d->mMode == EditMode) {
0166         return;
0167     }
0168 
0169     d->mAddressBookBox->setDefaultCollection(addressbook);
0170 }
0171 
0172 AkonadiContactEditor *ContactEditorDialog::editor() const
0173 {
0174     return d->mEditor;
0175 }
0176 
0177 void ContactEditorDialog::accept()
0178 {
0179     if (d->mEditor->hasNoSavedData()) {
0180         const int answer = KMessageBox::questionTwoActions(this,
0181                                                            i18nc("@info", "Location was not saved. Do you want to close editor?"),
0182                                                            i18nc("@title:window", "Confirmation"),
0183                                                            KGuiItem(i18nc("@action:button", "Close Editor"), QStringLiteral("dialog-close")),
0184                                                            KGuiItem(i18nc("@action:button", "Do Not Close"), QStringLiteral("dialog-cancel")));
0185         if (answer == KMessageBox::ButtonCode::SecondaryAction) {
0186             return;
0187         }
0188     }
0189     if (d->mAddressBookBox) {
0190         d->mEditor->setDefaultAddressBook(d->mAddressBookBox->currentCollection());
0191     }
0192     d->mEditor->saveContactInAddressBook();
0193 }
0194 
0195 void ContactEditorDialog::reject()
0196 {
0197     const int answer = KMessageBox::questionTwoActions(this,
0198                                                        i18nc("@info", "Do you really want to cancel?"),
0199                                                        i18nc("@title:window", "Confirmation"),
0200                                                        KGuiItem(i18nc("@action:button", "Cancel Editing"), QStringLiteral("dialog-ok")),
0201                                                        KGuiItem(i18nc("@action:button", "Do Not Cancel"), QStringLiteral("dialog-cancel")));
0202 
0203     if (answer == KMessageBox::ButtonCode::PrimaryAction) {
0204         QDialog::reject(); // Discard current changes
0205     }
0206 }
0207 
0208 #include "moc_contacteditordialog.cpp"