1/*
2 This file is part of the KDE Kontact Plugin Interface Library.
3
4 Copyright (c) 2003,2008 David Faure <faure@kde.org>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20*/
21
22#ifndef KONTACTINTERFACE_UNIQUEAPPHANDLER_H
23#define KONTACTINTERFACE_UNIQUEAPPHANDLER_H
24
25#include "kontactinterface_export.h"
26#include "plugin.h"
27
28namespace KontactInterface {
29
30/**
31 * D-Bus Object that has the name of the standalone application (e.g. "kmail")
32 * and implements newInstance() so that running the separate application does
33 * the right thing when kontact is running.
34 * By default this means simply bringing the main window to the front,
35 * but newInstance can be reimplemented.
36 */
37class KONTACTINTERFACE_EXPORT UniqueAppHandler : public QObject
38{
39 Q_OBJECT
40 // We implement the KUniqueApplication interface
41 Q_CLASSINFO( "D-Bus Interface", "org.kde.KUniqueApplication" )
42
43 public:
44 UniqueAppHandler( Plugin *plugin );
45 virtual ~UniqueAppHandler();
46
47 /// This must be reimplemented so that app-specific command line options can be parsed
48 virtual void loadCommandLineOptions() = 0;
49
50 Plugin *plugin() const;
51
52 /**
53 Sets the main QWidget @p widget associated with this application.
54 @param widget the QWidget to set as main
55 */
56 static void setMainWidget( QWidget *widget );
57
58 /**
59 Returns the main widget, which will zero if setMainWidget() has not be called yet.
60 @since 4.6
61 */
62 QWidget *mainWidget();
63
64 public Q_SLOTS: // DBUS methods
65 int newInstance( const QByteArray &asn_id, const QByteArray &args );
66 bool load();
67
68 protected:
69 virtual int newInstance();
70
71 private:
72 class Private;
73 Private *const d;
74};
75
76/// Base class for UniqueAppHandler
77class UniqueAppHandlerFactoryBase
78{
79 public:
80 virtual ~UniqueAppHandlerFactoryBase(){}
81 virtual UniqueAppHandler *createHandler( Plugin * ) = 0;
82};
83
84/**
85 * Used by UniqueAppWatcher below, to create the above UniqueAppHandler object
86 * when necessary.
87 * The template argument is the UniqueAppHandler-derived class.
88 * This allows to remove the need to subclass UniqueAppWatcher.
89 */
90template <class T> class UniqueAppHandlerFactory : public UniqueAppHandlerFactoryBase
91{
92 public:
93 virtual UniqueAppHandler *createHandler( Plugin *plugin ) {
94 plugin->registerClient();
95 return new T( plugin );
96 }
97};
98
99/**
100 * If the standalone application is running by itself, we need to watch
101 * for when the user closes it, and activate the uniqueapphandler then.
102 * This prevents, on purpose, that the standalone app can be restarted.
103 * Kontact takes over from there.
104 *
105 */
106class KONTACTINTERFACE_EXPORT UniqueAppWatcher : public QObject
107{
108 Q_OBJECT
109
110 public:
111 /**
112 * Create an instance of UniqueAppWatcher, which does everything necessary
113 * for the "unique application" behavior: create the UniqueAppHandler as soon
114 * as possible, i.e. either right now or when the standalone app is closed.
115 *
116 * @param factory templatized factory to create the handler. Example:
117 * ... Note that the watcher takes ownership of the factory.
118 * @param plugin is the plugin application
119 */
120 UniqueAppWatcher( UniqueAppHandlerFactoryBase *factory, Plugin *plugin );
121
122 virtual ~UniqueAppWatcher();
123
124 bool isRunningStandalone() const;
125
126 private Q_SLOTS:
127 void slotApplicationRemoved( const QString &name, const QString &oldOwner,
128 const QString &newOwner );
129
130 private:
131 class Private;
132 Private *const d;
133};
134
135} // namespace
136
137#endif
138
139