KontactInterface

uniqueapphandler.h
1/*
2 This file is part of the KDE Kontact Plugin Interface Library.
3
4 SPDX-FileCopyrightText: 2003, 2008 David Faure <faure@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#pragma once
10
11#include "kontactinterface_export.h"
12#include "plugin.h"
13#include <memory>
15
16namespace KontactInterface
17{
18/**
19 * D-Bus Object that has the name of the standalone application (e.g. "kmail")
20 * and implements newInstance() so that running the separate application does
21 * the right thing when kontact is running.
22 * By default this means simply bringing the main window to the front,
23 * but newInstance can be reimplemented.
24 */
25class KONTACTINTERFACE_EXPORT UniqueAppHandler : public QObject
26{
27 Q_OBJECT
28 // We implement the PIMUniqueApplication interface
29 Q_CLASSINFO("D-Bus Interface", "org.kde.PIMUniqueApplication")
30
31public:
32 explicit UniqueAppHandler(Plugin *plugin);
33 ~UniqueAppHandler() override;
34
35 /// This must be reimplemented so that app-specific command line options can be parsed
36 virtual void loadCommandLineOptions(QCommandLineParser *parser) = 0;
37
38 [[nodiscard]] Plugin *plugin() const;
39
40 /**
41 Sets the main QWidget @p widget associated with this application.
42 @param widget the QWidget to set as main
43 */
44 static void setMainWidget(QWidget *widget);
45
46 /**
47 Returns the main widget, which will zero if setMainWidget() has not be called yet.
48 @since 4.6
49 */
50 [[nodiscard]] QWidget *mainWidget();
51
52public Q_SLOTS: // DBUS methods
53 int newInstance(const QByteArray &asn_id, const QStringList &args, const QString &workingDirectory);
54 bool load();
55
56protected:
57 virtual int activate(const QStringList &args, const QString &workingDirectory);
58
59private:
60 class UniqueAppHandlerPrivate;
61 std::unique_ptr<UniqueAppHandlerPrivate> const d;
62};
63
64/// Base class for UniqueAppHandler
66{
67public:
68 virtual ~UniqueAppHandlerFactoryBase() = default;
69 virtual UniqueAppHandler *createHandler(Plugin *) = 0;
70};
71
72/**
73 * Used by UniqueAppWatcher below, to create the above UniqueAppHandler object
74 * when necessary.
75 * The template argument is the UniqueAppHandler-derived class.
76 * This allows to remove the need to subclass UniqueAppWatcher.
77 */
78template<class T>
80{
81public:
82 UniqueAppHandler *createHandler(Plugin *plugin) override
83 {
84 plugin->registerClient();
85 return new T(plugin);
86 }
87};
88
89/**
90 * If the standalone application is running by itself, we need to watch
91 * for when the user closes it, and activate the uniqueapphandler then.
92 * This prevents, on purpose, that the standalone app can be restarted.
93 * Kontact takes over from there.
94 *
95 */
96class KONTACTINTERFACE_EXPORT UniqueAppWatcher : public QObject
97{
98 Q_OBJECT
99
100public:
101 /**
102 * Create an instance of UniqueAppWatcher, which does everything necessary
103 * for the "unique application" behavior: create the UniqueAppHandler as soon
104 * as possible, i.e. either right now or when the standalone app is closed.
105 *
106 * @param factory templatized factory to create the handler. Example:
107 * ... Note that the watcher takes ownership of the factory.
108 * @param plugin is the plugin application
109 */
111
112 ~UniqueAppWatcher() override;
113
114 bool isRunningStandalone() const;
115
116private Q_SLOTS:
117 KONTACTINTERFACE_NO_EXPORT void slotApplicationRemoved(const QString &name, const QString &oldOwner, const QString &newOwner);
118
119private:
120 class UniqueAppWatcherPrivate;
121 std::unique_ptr<UniqueAppWatcherPrivate> const d;
122};
123
124} // namespace
Base class for all Plugins in Kontact.
Definition plugin.h:66
QString registerClient()
Registers the client at DBus and returns the dbus identifier.
Definition plugin.cpp:150
Used by UniqueAppWatcher below, to create the above UniqueAppHandler object when necessary.
D-Bus Object that has the name of the standalone application (e.g.
virtual void loadCommandLineOptions(QCommandLineParser *parser)=0
This must be reimplemented so that app-specific command line options can be parsed.
If the standalone application is running by itself, we need to watch for when the user closes it,...
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 8 2024 11:49:18 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.