kaddressbook_plugin.cpp 4.92 KB
Newer Older
1
2
3
/*
  This file is part of KAddressBook Kontact Plugin.

4
  SPDX-FileCopyrightText: 2009-2020 Laurent Montel <montel@kde.org>
5

6
  SPDX-License-Identifier: GPL-2.0-or-later
7
8
9
*/

#include "kaddressbook_plugin.h"
Laurent Montel's avatar
Laurent Montel committed
10
#include "../src/kaddressbook_options.h"
11
12
13
14
15
16
#include "kaddressbookinterface.h"

#include <KontactInterface/Core>

#include <QAction>
#include <KActionCollection>
Laurent Montel's avatar
Laurent Montel committed
17
#include "kaddressbookplugin_debug.h"
18
19
20
21
22
23
24
25
#include <KLocalizedString>

#include <QIcon>

#include <QDBusConnection>
#include <QDBusReply>
#include <QStandardPaths>

26
EXPORT_KONTACT_PLUGIN_WITH_JSON(KAddressBookPlugin, "kaddressbookplugin.json")
27
28
29
30

KAddressBookPlugin::KAddressBookPlugin(KontactInterface::Core *core, const QVariantList &)
    : KontactInterface::Plugin(core, core, "kaddressbook")
{
Laurent Montel's avatar
Laurent Montel committed
31
    setComponentName(QStringLiteral("kaddressbook"), i18n("KAddressBook"));
32

Laurent Montel's avatar
Laurent Montel committed
33
34
35
    QAction *action
        = new QAction(QIcon::fromTheme(QStringLiteral("contact-new")),
                      i18nc("@action:inmenu", "New Contact..."), this);
36
37
    actionCollection()->addAction(QStringLiteral("new_contact"), action);
    connect(action, &QAction::triggered, this, &KAddressBookPlugin::slotNewContact);
38
    actionCollection()->setDefaultShortcut(action, QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_C));
39
40
41
42
43
44
45
    //action->setHelpText(
    //  i18nc( "@info:status", "Create a new contact" ) );
    action->setWhatsThis(
        i18nc("@info:whatsthis",
              "You will be presented with a dialog where you can create a new contact."));
    insertNewAction(action);

Laurent Montel's avatar
Laurent Montel committed
46
47
48
    action
        = new QAction(QIcon::fromTheme(QStringLiteral("user-group-new")),
                      i18nc("@action:inmenu", "New Contact Group..."), this);
49
50
    actionCollection()->addAction(QStringLiteral("new_contactgroup"), action);
    connect(action, &QAction::triggered, this, &KAddressBookPlugin::slotNewContactGroup);
51
    actionCollection()->setDefaultShortcut(action, QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_G));
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
    //action->setHelpText(
    //  i18nc( "@info:status", "Create a new contact group" ) );
    action->setWhatsThis(
        i18nc("@info:whatsthis",
              "You will be presented with a dialog where you can create a new contact group."));
    insertNewAction(action);

    mUniqueAppWatcher = new KontactInterface::UniqueAppWatcher(
        new KontactInterface::UniqueAppHandlerFactory<KAddressBookUniqueAppHandler>(), this);
}

KAddressBookPlugin::~KAddressBookPlugin()
{
}

void KAddressBookPlugin::slotNewContact()
{
69
    KParts::Part *part = createPart();
70
71
72
73
74
    if (!part) {
        return;
    }

    if (part->metaObject()->indexOfMethod("newContact()") == -1) {
Laurent Montel's avatar
Laurent Montel committed
75
        qCWarning(KADDRESSBOOKPLUGIN_LOG) << "KAddressBook part is missing slot newContact()";
76
77
78
79
80
81
82
83
        return;
    }

    QMetaObject::invokeMethod(part, "newContact");
}

void KAddressBookPlugin::slotNewContactGroup()
{
84
    KParts::Part *part = createPart();
85
86
87
88
89
    if (!part) {
        return;
    }

    if (part->metaObject()->indexOfMethod("newGroup()") == -1) {
Laurent Montel's avatar
Laurent Montel committed
90
        qCWarning(KADDRESSBOOKPLUGIN_LOG) << "KAddressBook part is missing slot newGroup()";
91
92
93
94
95
96
        return;
    }

    QMetaObject::invokeMethod(part, "newGroup");
}

97
KParts::Part *KAddressBookPlugin::createPart()
98
{
99
    KParts::Part *part = loadPart();
100
    if (!part) {
Laurent Montel's avatar
Laurent Montel committed
101
        return nullptr;
102
103
104
105
    }

    // disable the Ctrl+N shortcut, as it is used by Kontact already
    if (part->action("akonadi_contact_create")) {
Laurent Montel's avatar
Laurent Montel committed
106
        QAction *newAction = part->action("akonadi_contact_create");
107
108
109
110
111
112
113
114
115
116
117
118
119
        if (newAction) {
            newAction->setShortcut(QKeySequence());
        }
    }

    return part;
}

bool KAddressBookPlugin::isRunningStandalone() const
{
    return mUniqueAppWatcher->isRunningStandalone();
}

120
121
122
123
124
int KAddressBookPlugin::weight() const
{
    return 300;
}

125
126
127
128
129
130
131
132
133
QStringList KAddressBookPlugin::invisibleToolbarActions() const
{
    QStringList actions;
    actions << QStringLiteral("akonadi_contact_create") << QStringLiteral("akonadi_contact_group_create");
    return actions;
}

void KAddressBookPlugin::shortcutChanged()
{
134
    KParts::Part *localPart = part();
135
136
    if (localPart) {
        if (localPart->metaObject()->indexOfMethod("updateQuickSearchText()") == -1) {
Laurent Montel's avatar
Laurent Montel committed
137
            qCWarning(KADDRESSBOOKPLUGIN_LOG) << "KAddressBook part is missing slot updateQuickSearchText()";
138
139
140
141
142
143
            return;
        }
        QMetaObject::invokeMethod(localPart, "updateQuickSearchText");
    }
}

144
void KAddressBookUniqueAppHandler::loadCommandLineOptions(QCommandLineParser *parser)
145
{
146
    kaddressbook_options(parser);
147
148
}

149
int KAddressBookUniqueAppHandler::activate(const QStringList &arguments, const QString &workingDir)
150
151
152
153
{
    // Ensure part is loaded
    (void)plugin()->part();
    org::kde::kaddressbook kaddressbook(QStringLiteral("org.kde.kaddressbook"), QStringLiteral("/KAddressBook"), QDBusConnection::sessionBus());
154
    QDBusReply<bool> reply = kaddressbook.handleCommandLine(arguments);
Laurent Montel's avatar
Laurent Montel committed
155
    Q_UNUSED(reply)
156

157
    return KontactInterface::UniqueAppHandler::activate(arguments, workingDir);
158
159
160
}

#include "kaddressbook_plugin.moc"