summaryrefslogtreecommitdiffstats
path: root/kontact/interfaces/uniqueapphandler.h
blob: b93b596578d0f63fb3f53711ca115fa5b3e62f7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
   This file is part of KDE Kontact.

   Copyright (c) 2003 David Faure <faure@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#ifndef KONTACT_UNITQUEAPPHANDLER_H
#define KONTACT_UNITQUEAPPHANDLER_H

#include <dcopobject.h>
#include <plugin.h>
#include <kdepimmacros.h>

namespace Kontact
{

/**
 * DCOP Object that has the name of the standalone application (e.g. "kmail")
 * and implements newInstance() so that running the separate application does
 * the right thing when kontact is running.
 * By default this means simply bringing the main window to the front,
 * but newInstance can be reimplemented.
 */
class KDE_EXPORT UniqueAppHandler : public DCOPObject
{
  K_DCOP

  public:
    UniqueAppHandler( Plugin* plugin ) : DCOPObject( plugin->name() ), mPlugin( plugin ) {}

    /// This must be reimplemented so that app-specific command line options can be parsed
    virtual void loadCommandLineOptions() = 0;

    /// We can't use k_dcop and dcopidl here, because the data passed
    /// to newInstance can't be expressed in terms of normal data types.
    virtual int newInstance();

    Plugin* plugin() const { return mPlugin; }

    /// Load the kontact command line options.
    static void loadKontactCommandLineOptions();

  private:
    Plugin* mPlugin;
};

/// Base class for UniqueAppHandler
class UniqueAppHandlerFactoryBase
{
  public:
    virtual UniqueAppHandler* createHandler( Plugin* ) = 0;
};

/**
 * Used by UniqueAppWatcher below, to create the above UniqueAppHandler object
 * when necessary.
 * The template argument is the UniqueAppHandler-derived class.
 * This allows to remove the need to subclass UniqueAppWatcher.
 */
template <class T> class UniqueAppHandlerFactory : public UniqueAppHandlerFactoryBase
{
  public:
    virtual UniqueAppHandler* createHandler( Plugin* plugin ) {
        (void)plugin->dcopClient(); // ensure that we take over the DCOP name
        return new T( plugin );
    }
};


/**
 * If the standalone application is running by itself, we need to watch
 * for when the user closes it, and activate the uniqueapphandler then.
 * This prevents, on purpose, that the standalone app can be restarted.
 * Kontact takes over from there.
 *
 */
class KDE_EXPORT UniqueAppWatcher : public TQObject
{
  Q_OBJECT
  TQ_OBJECT

  public:
    /**
     * Create an instance of UniqueAppWatcher, which does everything necessary
     * for the "unique application" behavior: create the UniqueAppHandler as soon
     * as possible, i.e. either right now or when the standalone app is closed.
     *
     * @param factory templatized factory to create the handler. Example:
     * ...   Note that the watcher takes ownership of the factory.
     * @param plugin is the plugin application
     */
    UniqueAppWatcher( UniqueAppHandlerFactoryBase* factory, Plugin* plugin );

    virtual ~UniqueAppWatcher();

    bool isRunningStandalone() const { return mRunningStandalone; }

  protected slots:
    void unregisteredFromDCOP( const TQCString& appId );

  private:
    bool mRunningStandalone;
    UniqueAppHandlerFactoryBase* mFactory;
    Plugin* mPlugin;
};

} // namespace

#endif /* KONTACT_UNITQUEAPPHANDLER_H */