File indexing completed on 2024-12-22 05:01:10

0001 /*
0002  *   kmail: KDE mail client
0003  *   SPDX-FileCopyrightText: 2000 Espen Sand <espen@kde.org>
0004  *   SPDX-FileCopyrightText: 2001-2003 Marc Mutz <mutz@kde.org>
0005  *   Contains code segments and ideas from earlier kmail dialog code.
0006  *   SPDX-FileCopyrightText: 2010 Volker Krause <vkrause@kde.org>
0007  *
0008  *   SPDX-License-Identifier: GPL-2.0-or-later
0009  *
0010  */
0011 
0012 #include "newidentitydialog.h"
0013 
0014 #include <KIdentityManagementCore/IdentityManager>
0015 #include <KLineEditEventHandler>
0016 #include <KLocalizedString>
0017 #include <KSeparator>
0018 #include <PimCommon/PimUtil>
0019 #include <QComboBox>
0020 #include <QLineEdit>
0021 
0022 #include <QButtonGroup>
0023 #include <QHBoxLayout>
0024 #include <QLabel>
0025 #include <QRadioButton>
0026 #include <QVBoxLayout>
0027 
0028 #include <QDialogButtonBox>
0029 #include <QPushButton>
0030 #include <cassert>
0031 
0032 using namespace KMail;
0033 
0034 NewIdentityDialog::NewIdentityDialog(KIdentityManagementCore::IdentityManager *manager, QWidget *parent)
0035     : QDialog(parent)
0036     , mIdentityManager(manager)
0037 {
0038     setWindowTitle(i18nc("@title:window", "New Identity"));
0039     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::Help, this);
0040     auto mainLayout = new QVBoxLayout(this);
0041     mOkButton = buttonBox->button(QDialogButtonBox::Ok);
0042     mOkButton->setDefault(true);
0043     mOkButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0044     connect(buttonBox, &QDialogButtonBox::accepted, this, &NewIdentityDialog::accept);
0045     connect(buttonBox, &QDialogButtonBox::rejected, this, &NewIdentityDialog::reject);
0046     connect(buttonBox->button(QDialogButtonBox::Help), &QPushButton::clicked, this, &NewIdentityDialog::slotHelp);
0047 
0048     auto page = new QWidget(this);
0049     mainLayout->addWidget(page);
0050     mainLayout->addWidget(buttonBox);
0051     auto vlay = new QVBoxLayout(page);
0052     vlay->setContentsMargins({});
0053 
0054     // row 0: line edit with label
0055     auto hlay = new QHBoxLayout(); // inherits spacing
0056     vlay->addLayout(hlay);
0057     mLineEdit = new QLineEdit(page);
0058     mLineEdit->setFocus();
0059     mLineEdit->setClearButtonEnabled(true);
0060     KLineEditEventHandler::catchReturnKey(mLineEdit);
0061     auto l = new QLabel(i18n("&New identity:"), page);
0062     l->setBuddy(mLineEdit);
0063     hlay->addWidget(l);
0064     hlay->addWidget(mLineEdit, 1);
0065     connect(mLineEdit, &QLineEdit::textChanged, this, &NewIdentityDialog::slotEnableOK);
0066 
0067     mButtonGroup = new QButtonGroup(page);
0068 
0069     // row 1: radio button
0070     auto radio = new QRadioButton(i18n("&With empty fields"), page);
0071     radio->setChecked(true);
0072     vlay->addWidget(radio);
0073     mButtonGroup->addButton(radio, static_cast<int>(Empty));
0074 
0075     // row 2: radio button
0076     radio = new QRadioButton(i18n("&Use System Settings values"), page);
0077     vlay->addWidget(radio);
0078     mButtonGroup->addButton(radio, static_cast<int>(ControlCenter));
0079 
0080     // row 3: radio button
0081     radio = new QRadioButton(i18n("&Duplicate existing identity"), page);
0082     vlay->addWidget(radio);
0083     mButtonGroup->addButton(radio, static_cast<int>(ExistingEntry));
0084 
0085     // row 4: combobox with existing identities and label
0086     hlay = new QHBoxLayout(); // inherits spacing
0087     vlay->addLayout(hlay);
0088     mComboBox = new QComboBox(page);
0089     mComboBox->addItems(manager->shadowIdentities());
0090     mComboBox->setEnabled(false);
0091     auto label = new QLabel(i18n("&Existing identities:"), page);
0092     label->setBuddy(mComboBox);
0093     label->setEnabled(false);
0094     hlay->addWidget(label);
0095     hlay->addWidget(mComboBox, 1);
0096 
0097     vlay->addWidget(new KSeparator);
0098     vlay->addStretch(1); // spacer
0099 
0100     // enable/disable combobox and label depending on the third radio
0101     // button's state:
0102     connect(radio, &QRadioButton::toggled, label, &QLabel::setEnabled);
0103     connect(radio, &QRadioButton::toggled, mComboBox, &QComboBox::setEnabled);
0104 
0105     mOkButton->setEnabled(false); // since line edit is empty
0106 
0107     resize(400, 180);
0108 }
0109 
0110 void NewIdentityDialog::slotHelp()
0111 {
0112     PimCommon::Util::invokeHelp(QStringLiteral("kmail2/configure-identity.html"), QStringLiteral("configure-identity-newidentitydialog"));
0113 }
0114 
0115 NewIdentityDialog::DuplicateMode NewIdentityDialog::duplicateMode() const
0116 {
0117     const int id = mButtonGroup->checkedId();
0118     assert(id == static_cast<int>(Empty) || id == static_cast<int>(ControlCenter) || id == static_cast<int>(ExistingEntry));
0119     return static_cast<DuplicateMode>(id);
0120 }
0121 
0122 void NewIdentityDialog::slotEnableOK(const QString &proposedIdentityName)
0123 {
0124     // OK button is disabled if
0125     const QString name = proposedIdentityName.trimmed();
0126     // name isn't empty
0127     if (name.isEmpty()) {
0128         mOkButton->setEnabled(false);
0129         return;
0130     }
0131     // or name doesn't yet exist.
0132     if (!mIdentityManager->isUnique(name)) {
0133         mOkButton->setEnabled(false);
0134         return;
0135     }
0136     mOkButton->setEnabled(true);
0137 }
0138 
0139 QString NewIdentityDialog::identityName() const
0140 {
0141     return mLineEdit->text();
0142 }
0143 
0144 QString NewIdentityDialog::duplicateIdentity() const
0145 {
0146     return mComboBox->currentText();
0147 }
0148 
0149 #include "moc_newidentitydialog.cpp"