File indexing completed on 2024-04-28 05:25:45

0001 /*
0002  * SPDX-FileCopyrightText: 2019 Bhushan Shah <bshah@kde.org>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #include "kpeopleactionsplugin.h"
0008 
0009 #include <KLocalizedString>
0010 #include <QDebug>
0011 #include <QDesktopServices>
0012 
0013 #include <KPluginFactory>
0014 
0015 #include <KContacts/VCardConverter>
0016 #include <KPeople/Actions>
0017 #include <KPeopleBackend/AbstractContact>
0018 
0019 KPeopleActionsPlugin::KPeopleActionsPlugin(QObject *parent, const QVariantList &args)
0020     : AbstractPersonAction(parent)
0021 {
0022     Q_UNUSED(args)
0023 }
0024 
0025 QList<QAction *> KPeopleActionsPlugin::actionsForPerson(const KPeople::PersonData &data, QObject *parent) const
0026 {
0027     Q_UNUSED(parent)
0028     QList<QAction *> actions;
0029 
0030     // Fetch contact vcard
0031     QByteArray vcard = data.contactCustomProperty(KPeople::AbstractContact::VCardProperty).toByteArray();
0032     KContacts::VCardConverter converter;
0033     const auto addressee = converter.parseVCard(vcard);
0034 
0035     // Instant messenger actions
0036     const auto imppList = addressee.imppList();
0037     for (const auto &impp : imppList) {
0038         QAction *action = new QAction(
0039             QIcon::fromTheme(impp.serviceIcon()),
0040             i18nc("Action to write to instant messenger contact", "%1 %2", KContacts::Impp::serviceLabel(impp.serviceType()), impp.address().toString()));
0041         action->setProperty("actionType", KPeople::TextChatAction);
0042 
0043         connect(action, &QAction::triggered, [=]() {
0044             QDesktopServices::openUrl(impp.address());
0045         });
0046 
0047         actions << action;
0048     }
0049 
0050     return actions;
0051 }
0052 
0053 K_PLUGIN_FACTORY_WITH_JSON(KPeopleActionsPluginFactory, "phonebook_kpeople_plugin.json", registerPlugin<KPeopleActionsPlugin>();)
0054 
0055 #include "kpeopleactionsplugin.moc"