File indexing completed on 2024-05-19 05:14:33

0001 /*
0002   This file is part of KAddressBook Kontact Plugin.
0003 
0004   SPDX-FileCopyrightText: 2009-2024 Laurent Montel <montel@kde.org>
0005 
0006   SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #include "kaddressbook_plugin.h"
0010 #include "../src/kaddressbook_options.h"
0011 #include "kaddressbookinterface.h"
0012 
0013 #include <KontactInterface/Core>
0014 
0015 #include "kaddressbookplugin_debug.h"
0016 #include <KActionCollection>
0017 #include <KLocalizedString>
0018 #include <QAction>
0019 
0020 #include <QIcon>
0021 
0022 #include <QDBusConnection>
0023 #include <QDBusReply>
0024 #include <QStandardPaths>
0025 
0026 EXPORT_KONTACT_PLUGIN_WITH_JSON(KAddressBookPlugin, "kaddressbookplugin.json")
0027 
0028 KAddressBookPlugin::KAddressBookPlugin(KontactInterface::Core *core, const KPluginMetaData &data, const QVariantList &)
0029     : KontactInterface::Plugin(core, core, data, "kaddressbook")
0030 {
0031     setComponentName(QStringLiteral("kaddressbook"), i18n("KAddressBook"));
0032 
0033     auto action = new QAction(QIcon::fromTheme(QStringLiteral("contact-new")), i18nc("@action:inmenu", "New Contact..."), this);
0034     actionCollection()->addAction(QStringLiteral("new_contact"), action);
0035     connect(action, &QAction::triggered, this, &KAddressBookPlugin::slotNewContact);
0036     actionCollection()->setDefaultShortcut(action, QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_C));
0037     // action->setHelpText(
0038     //  i18nc( "@info:status", "Create a new contact" ) );
0039     action->setWhatsThis(i18nc("@info:whatsthis", "You will be presented with a dialog where you can create a new contact."));
0040     insertNewAction(action);
0041 
0042     action = new QAction(QIcon::fromTheme(QStringLiteral("user-group-new")), i18nc("@action:inmenu", "New Contact Group..."), this);
0043     actionCollection()->addAction(QStringLiteral("new_contactgroup"), action);
0044     connect(action, &QAction::triggered, this, &KAddressBookPlugin::slotNewContactGroup);
0045     actionCollection()->setDefaultShortcut(action, QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_G));
0046     // action->setHelpText(
0047     //  i18nc( "@info:status", "Create a new contact group" ) );
0048     action->setWhatsThis(i18nc("@info:whatsthis", "You will be presented with a dialog where you can create a new contact group."));
0049     insertNewAction(action);
0050 
0051     mUniqueAppWatcher = new KontactInterface::UniqueAppWatcher(new KontactInterface::UniqueAppHandlerFactory<KAddressBookUniqueAppHandler>(), this);
0052 }
0053 
0054 KAddressBookPlugin::~KAddressBookPlugin() = default;
0055 
0056 void KAddressBookPlugin::slotNewContact()
0057 {
0058     KParts::Part *part = createPart();
0059     if (!part) {
0060         return;
0061     }
0062 
0063     if (part->metaObject()->indexOfMethod("newContact()") == -1) {
0064         qCWarning(KADDRESSBOOKPLUGIN_LOG) << "KAddressBook part is missing slot newContact()";
0065         return;
0066     }
0067 
0068     QMetaObject::invokeMethod(part, "newContact");
0069 }
0070 
0071 void KAddressBookPlugin::slotNewContactGroup()
0072 {
0073     KParts::Part *part = createPart();
0074     if (!part) {
0075         return;
0076     }
0077 
0078     if (part->metaObject()->indexOfMethod("newGroup()") == -1) {
0079         qCWarning(KADDRESSBOOKPLUGIN_LOG) << "KAddressBook part is missing slot newGroup()";
0080         return;
0081     }
0082 
0083     QMetaObject::invokeMethod(part, "newGroup");
0084 }
0085 
0086 KParts::Part *KAddressBookPlugin::createPart()
0087 {
0088     KParts::Part *part = loadPart();
0089     if (!part) {
0090         return nullptr;
0091     }
0092 
0093     // disable the Ctrl+N shortcut, as it is used by Kontact already
0094     if (part->action(QStringLiteral("akonadi_contact_create"))) {
0095         QAction *newAction = part->action(QStringLiteral("akonadi_contact_create"));
0096         if (newAction) {
0097             newAction->setShortcut(QKeySequence());
0098         }
0099     }
0100 
0101     return part;
0102 }
0103 
0104 bool KAddressBookPlugin::isRunningStandalone() const
0105 {
0106     return mUniqueAppWatcher->isRunningStandalone();
0107 }
0108 
0109 int KAddressBookPlugin::weight() const
0110 {
0111     return 300;
0112 }
0113 
0114 QStringList KAddressBookPlugin::invisibleToolbarActions() const
0115 {
0116     QStringList actions;
0117     actions << QStringLiteral("akonadi_contact_create") << QStringLiteral("akonadi_contact_group_create");
0118     return actions;
0119 }
0120 
0121 void KAddressBookPlugin::shortcutChanged()
0122 {
0123     KParts::Part *localPart = part();
0124     if (localPart) {
0125         if (localPart->metaObject()->indexOfMethod("updateQuickSearchText()") == -1) {
0126             qCWarning(KADDRESSBOOKPLUGIN_LOG) << "KAddressBook part is missing slot updateQuickSearchText()";
0127             return;
0128         }
0129         QMetaObject::invokeMethod(localPart, "updateQuickSearchText");
0130     }
0131 }
0132 
0133 void KAddressBookUniqueAppHandler::loadCommandLineOptions(QCommandLineParser *parser)
0134 {
0135     kaddressbook_options(parser);
0136 }
0137 
0138 int KAddressBookUniqueAppHandler::activate(const QStringList &arguments, const QString &workingDir)
0139 {
0140     // Ensure part is loaded
0141     (void)plugin()->part();
0142     org::kde::kaddressbook kaddressbook(QStringLiteral("org.kde.kaddressbook"), QStringLiteral("/KAddressBook"), QDBusConnection::sessionBus());
0143     QDBusReply<bool> reply = kaddressbook.handleCommandLine(arguments);
0144     Q_UNUSED(reply)
0145 
0146     return KontactInterface::UniqueAppHandler::activate(arguments, workingDir);
0147 }
0148 
0149 #include "kaddressbook_plugin.moc"
0150 
0151 #include "moc_kaddressbook_plugin.cpp"