File indexing completed on 2024-12-22 05:01:10
0001 /* 0002 SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-only 0005 */ 0006 0007 #include "identityeditvcarddialog.h" 0008 0009 #include "kmail_debug.h" 0010 #include <Akonadi/ContactEditor> 0011 #include <KContacts/VCardConverter> 0012 #include <KLocalizedString> 0013 #include <KMessageBox> 0014 #include <QStandardPaths> 0015 0016 #include <QDialogButtonBox> 0017 #include <QFileInfo> 0018 #include <QPushButton> 0019 #include <QVBoxLayout> 0020 0021 IdentityEditVcardDialog::IdentityEditVcardDialog(const QString &fileName, QWidget *parent) 0022 : QDialog(parent) 0023 , mContactEditor(new Akonadi::AkonadiContactEditor(Akonadi::AkonadiContactEditor::CreateMode, Akonadi::AkonadiContactEditor::VCardMode, this)) 0024 { 0025 auto topLayout = new QVBoxLayout(this); 0026 setModal(true); 0027 0028 auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this); 0029 QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok); 0030 okButton->setDefault(true); 0031 okButton->setShortcut(Qt::CTRL | Qt::Key_Return); 0032 connect(buttonBox, &QDialogButtonBox::accepted, this, &IdentityEditVcardDialog::accept); 0033 connect(buttonBox, &QDialogButtonBox::rejected, this, &IdentityEditVcardDialog::reject); 0034 0035 if (QFileInfo::exists(fileName)) { 0036 setWindowTitle(i18nc("@title:window", "Edit own vCard")); 0037 auto user1Button = new QPushButton; 0038 buttonBox->addButton(user1Button, QDialogButtonBox::ActionRole); 0039 user1Button->setText(i18n("Delete current vCard")); 0040 connect(user1Button, &QPushButton::clicked, this, &IdentityEditVcardDialog::slotDeleteCurrentVCard); 0041 } else { 0042 setWindowTitle(i18nc("@title:window", "Create own vCard")); 0043 } 0044 0045 topLayout->addWidget(mContactEditor); 0046 topLayout->addWidget(buttonBox); 0047 loadVcard(fileName); 0048 } 0049 0050 IdentityEditVcardDialog::~IdentityEditVcardDialog() = default; 0051 0052 void IdentityEditVcardDialog::slotDeleteCurrentVCard() 0053 { 0054 if (mVcardFileName.isEmpty()) { 0055 return; 0056 } 0057 const int answer = KMessageBox::questionTwoActions(this, 0058 i18n("Are you sure you want to delete this vCard?"), 0059 i18nc("@title:window", "Delete vCard"), 0060 KStandardGuiItem::del(), 0061 KStandardGuiItem::cancel()); 0062 if (answer == KMessageBox::ButtonCode::PrimaryAction) { 0063 if (mVcardFileName.startsWith(QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation))) { 0064 deleteCurrentVcard(true); 0065 } else { 0066 deleteCurrentVcard(false); 0067 } 0068 reject(); 0069 } 0070 } 0071 0072 void IdentityEditVcardDialog::deleteCurrentVcard(bool deleteOnDisk) 0073 { 0074 if (!mVcardFileName.isEmpty()) { 0075 if (deleteOnDisk) { 0076 QFile file(mVcardFileName); 0077 if (file.exists()) { 0078 if (!file.remove()) { 0079 KMessageBox::error(this, i18n("We cannot delete vCard file."), i18nc("@title:window", "Delete vCard")); 0080 } 0081 } 0082 } 0083 Q_EMIT vcardRemoved(); 0084 } 0085 } 0086 0087 void IdentityEditVcardDialog::loadVcard(const QString &vcardFileName) 0088 { 0089 if (vcardFileName.isEmpty()) { 0090 return; 0091 } 0092 mVcardFileName = vcardFileName; 0093 QFile file(vcardFileName); 0094 0095 if (file.open(QIODevice::ReadOnly)) { 0096 const QByteArray data = file.readAll(); 0097 file.close(); 0098 if (!data.isEmpty()) { 0099 KContacts::VCardConverter converter; 0100 KContacts::Addressee addr = converter.parseVCard(data); 0101 mContactEditor->setContactTemplate(addr); 0102 } 0103 } 0104 } 0105 0106 QString IdentityEditVcardDialog::saveVcard() const 0107 { 0108 const KContacts::Addressee addr = mContactEditor->contact(); 0109 KContacts::VCardConverter converter; 0110 QFile file(mVcardFileName); 0111 if (file.open(QIODevice::WriteOnly | QIODevice::Text)) { 0112 const QByteArray data = converter.exportVCard(addr, KContacts::VCardConverter::v3_0); 0113 file.write(data); 0114 file.flush(); 0115 file.close(); 0116 } else { 0117 qCDebug(KMAIL_LOG) << "We cannot open file: " << mVcardFileName; 0118 } 0119 return mVcardFileName; 0120 } 0121 0122 void IdentityEditVcardDialog::reject() 0123 { 0124 const int answer = KMessageBox::questionTwoActions(this, 0125 i18nc("@info", "Do you really want to cancel?"), 0126 i18nc("@title:window", "Confirmation"), 0127 KGuiItem(i18nc("@action:button", "Cancel Editing"), QStringLiteral("dialog-ok")), 0128 KGuiItem(i18nc("@action:button", "Do Not Cancel"), QStringLiteral("dialog-cancel"))); 0129 if (answer == KMessageBox::ButtonCode::PrimaryAction) { 0130 QDialog::reject(); // Discard current changes 0131 } 0132 } 0133 0134 #include "moc_identityeditvcarddialog.cpp"