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

0001 /*
0002   SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-only
0005 */
0006 
0007 #include "identityaddvcarddialog.h"
0008 
0009 #include <KLocalizedString>
0010 #include <KSeparator>
0011 #include <KUrlRequester>
0012 #include <QComboBox>
0013 
0014 #include <QButtonGroup>
0015 #include <QDialogButtonBox>
0016 #include <QLabel>
0017 #include <QPushButton>
0018 #include <QRadioButton>
0019 #include <QVBoxLayout>
0020 
0021 IdentityAddVcardDialog::IdentityAddVcardDialog(const QStringList &shadowIdentities, QWidget *parent)
0022     : QDialog(parent)
0023     , mButtonGroup(new QButtonGroup(this))
0024     , mComboBox(new QComboBox(this))
0025     , mVCardPath(new KUrlRequester(this))
0026 {
0027     setWindowTitle(i18nc("@title:window", "Create own vCard"));
0028     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
0029     auto mainLayout = new QVBoxLayout(this);
0030     QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
0031     okButton->setDefault(true);
0032     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0033     connect(buttonBox, &QDialogButtonBox::accepted, this, &IdentityAddVcardDialog::accept);
0034     connect(buttonBox, &QDialogButtonBox::rejected, this, &IdentityAddVcardDialog::reject);
0035     setModal(true);
0036 
0037     auto mainWidget = new QWidget(this);
0038     mainLayout->addWidget(mainWidget);
0039     mainLayout->addWidget(buttonBox);
0040 
0041     auto vlay = new QVBoxLayout(mainWidget);
0042     vlay->setContentsMargins({});
0043 
0044     mButtonGroup->setObjectName(QLatin1StringView("buttongroup"));
0045 
0046     // row 1: radio button
0047     auto radio = new QRadioButton(i18n("&With empty fields"), this);
0048     radio->setChecked(true);
0049     vlay->addWidget(radio);
0050     mButtonGroup->addButton(radio, static_cast<int>(Empty));
0051 
0052     // row 2: radio button
0053     auto fromExistingVCard = new QRadioButton(i18n("&From existing vCard"), this);
0054     vlay->addWidget(fromExistingVCard);
0055     mButtonGroup->addButton(fromExistingVCard, static_cast<int>(FromExistingVCard));
0056 
0057     // row 3: KUrlRequester
0058     auto hlay = new QHBoxLayout(); // inherits spacing
0059     vlay->addLayout(hlay);
0060 
0061     mVCardPath->setObjectName(QLatin1StringView("kurlrequester_vcardpath"));
0062     mVCardPath->setMimeTypeFilters({QStringLiteral("text/vcard"), QStringLiteral("all/allfiles")});
0063 
0064     mVCardPath->setMode(KFile::LocalOnly | KFile::File);
0065     auto label = new QLabel(i18n("&vCard path:"), this);
0066     label->setBuddy(mVCardPath);
0067     label->setEnabled(false);
0068     mVCardPath->setEnabled(false);
0069     hlay->addWidget(label);
0070     hlay->addWidget(mVCardPath);
0071 
0072     connect(fromExistingVCard, &QRadioButton::toggled, label, &QLabel::setEnabled);
0073     connect(fromExistingVCard, &QRadioButton::toggled, mVCardPath, &KUrlRequester::setEnabled);
0074 
0075     // row 4: radio button
0076     auto duplicateExistingVCard = new QRadioButton(i18n("&Duplicate existing vCard"), this);
0077     vlay->addWidget(duplicateExistingVCard);
0078     mButtonGroup->addButton(duplicateExistingVCard, static_cast<int>(ExistingEntry));
0079 
0080     // row 5: combobox with existing identities and label
0081     hlay = new QHBoxLayout(); // inherits spacing
0082     vlay->addLayout(hlay);
0083     mComboBox->setObjectName(QLatin1StringView("identity_combobox"));
0084     mComboBox->setEditable(false);
0085 
0086     mComboBox->addItems(shadowIdentities);
0087     mComboBox->setEnabled(false);
0088     label = new QLabel(i18n("&Existing identities:"), this);
0089     label->setBuddy(mComboBox);
0090     label->setEnabled(false);
0091     hlay->addWidget(label);
0092     hlay->addWidget(mComboBox, 1);
0093 
0094     vlay->addWidget(new KSeparator);
0095     vlay->addStretch(1); // spacer
0096 
0097     // enable/disable combobox and label depending on the third radio
0098     // button's state:
0099     connect(duplicateExistingVCard, &QRadioButton::toggled, label, &QLabel::setEnabled);
0100     connect(duplicateExistingVCard, &QRadioButton::toggled, mComboBox, &QComboBox::setEnabled);
0101     resize(350, 130);
0102 }
0103 
0104 IdentityAddVcardDialog::~IdentityAddVcardDialog() = default;
0105 
0106 IdentityAddVcardDialog::DuplicateMode IdentityAddVcardDialog::duplicateMode() const
0107 {
0108     const int id = mButtonGroup->checkedId();
0109     return static_cast<DuplicateMode>(id);
0110 }
0111 
0112 QString IdentityAddVcardDialog::duplicateVcardFromIdentity() const
0113 {
0114     return mComboBox->currentText();
0115 }
0116 
0117 QUrl IdentityAddVcardDialog::existingVCard() const
0118 {
0119     return mVCardPath->url();
0120 }
0121 
0122 #include "moc_identityaddvcarddialog.cpp"