File indexing completed on 2024-11-24 04:43:02
0001 /* 0002 SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "checkgravatarplugininterface.h" 0008 #include "gravatar/widgets/gravatarupdatedialog.h" 0009 #include "kaddressbook_checkgravatarplugin_debug.h" 0010 #include <Akonadi/ItemModifyJob> 0011 #include <KActionCollection> 0012 #include <KLocalizedString> 0013 #include <KMessageBox> 0014 #include <QAction> 0015 #include <QPointer> 0016 0017 #include <KContacts/Addressee> 0018 0019 CheckGravatarPluginInterface::CheckGravatarPluginInterface(QObject *parent) 0020 : PimCommon::GenericPluginInterface(parent) 0021 { 0022 } 0023 0024 CheckGravatarPluginInterface::~CheckGravatarPluginInterface() = default; 0025 0026 void CheckGravatarPluginInterface::updateActions(int numberOfSelectedItems, int numberOfSelectedCollections) 0027 { 0028 Q_UNUSED(numberOfSelectedCollections) 0029 if (mAction) { 0030 mAction->setEnabled(numberOfSelectedItems > 0); 0031 } 0032 } 0033 0034 void CheckGravatarPluginInterface::createAction(KActionCollection *ac) 0035 { 0036 mAction = ac->addAction(QStringLiteral("search_gravatar")); 0037 mAction->setText(i18n("Check Gravatar...")); 0038 connect(mAction, &QAction::triggered, this, &CheckGravatarPluginInterface::slotActivated); 0039 PimCommon::ActionType type(mAction, PimCommon::ActionType::Tools); 0040 addActionType(type); 0041 } 0042 0043 void CheckGravatarPluginInterface::slotActivated() 0044 { 0045 Q_EMIT emitPluginActivated(this); 0046 } 0047 0048 void CheckGravatarPluginInterface::setCurrentItems(const Akonadi::Item::List &items) 0049 { 0050 mListItems = items; 0051 } 0052 0053 PimCommon::GenericPluginInterface::RequireTypes CheckGravatarPluginInterface::requiresFeatures() const 0054 { 0055 return PimCommon::GenericPluginInterface::CurrentItems; 0056 } 0057 0058 void CheckGravatarPluginInterface::exec() 0059 { 0060 if (mListItems.isEmpty()) { 0061 KMessageBox::error(parentWidget(), i18n("You have not selected any contacts.")); 0062 } else { 0063 if (mListItems.count() == 1) { 0064 Akonadi::Item item = mListItems.constFirst(); 0065 if (item.hasPayload<KContacts::Addressee>()) { 0066 auto address = item.payload<KContacts::Addressee>(); 0067 const QString email = address.preferredEmail(); 0068 if (email.isEmpty()) { 0069 KMessageBox::error(parentWidget(), i18n("No email found for this contact.")); 0070 return; 0071 } 0072 QPointer<KABGravatar::GravatarUpdateDialog> dlg = new KABGravatar::GravatarUpdateDialog(parentWidget()); 0073 dlg->setEmail(email); 0074 if (!address.photo().isEmpty()) { 0075 if (address.photo().isIntern()) { 0076 const QPixmap pix = QPixmap::fromImage(address.photo().data()); 0077 dlg->setOriginalPixmap(pix); 0078 } else { 0079 dlg->setOriginalUrl(QUrl(address.photo().url())); 0080 } 0081 } 0082 if (dlg->exec()) { 0083 KContacts::Picture picture = address.photo(); 0084 bool needToSave = false; 0085 if (dlg->saveUrl()) { 0086 const QUrl url = dlg->resolvedUrl(); 0087 if (!url.isEmpty()) { 0088 picture.setUrl(url.toString()); 0089 needToSave = true; 0090 } 0091 } else { 0092 const QPixmap pix = dlg->pixmap(); 0093 if (!pix.isNull()) { 0094 picture.setData(pix.toImage()); 0095 needToSave = true; 0096 } 0097 } 0098 if (needToSave) { 0099 address.setPhoto(picture); 0100 item.setPayload<KContacts::Addressee>(address); 0101 0102 auto modifyJob = new Akonadi::ItemModifyJob(item, this); 0103 connect(modifyJob, &Akonadi::ItemModifyJob::result, this, &CheckGravatarPluginInterface::slotModifyContactFinished); 0104 } 0105 } 0106 delete dlg; 0107 } else { 0108 KMessageBox::information(parentWidget(), i18n("A contact group was selected.")); 0109 } 0110 } else { 0111 KMessageBox::information(parentWidget(), i18n("Too many contacts selected.")); 0112 } 0113 } 0114 } 0115 0116 void CheckGravatarPluginInterface::slotModifyContactFinished(KJob *job) 0117 { 0118 if (job->error()) { 0119 qCDebug(KADDRESSBOOK_CHECKGRAVATAR_LOG) << "Error while modifying items. " << job->error() << job->errorString(); 0120 } 0121 }