summaryrefslogtreecommitdiffstats
path: root/libtdepim/pluginloader.h
blob: 6d38bbd1864909e0d9f3c6896ac064e337b25620 (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
125
126
127
128
129
130
131
132
133
134
135
/*  -*- c++ -*-
    This file is part of libtdepim.

    Copyright (c) 2002,2004 Marc Mutz <[email protected]>

    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 __KPIM_SHARED_PLUGINLOADER_H__
#define __KPIM_SHARED_PLUGINLOADER_H__

#include <pluginloaderbase.h>

namespace KPIM {

  /**
   * @short A generic plugin loader for when KPart::Plugin is overkill
   * @author Marc Mutz <[email protected]> based on KABC's FormatFactory
   *
   * This is a generic plugin loader / factory for small plugins that
   * don't want to be TQObjects.
   *
   * @section Usage
   *
   * A PluginLoader takes two template arguments, <code>T</code> and
   * <code>T_config</code>:
   *
   * <dl>
   * <dt>T</dt><dd>The type of object to return</dd>
   * <dt>T_config::mainfunc</dt><dd>The suffix of the factory function to call
   *          in the library to obtain a new object of type <code>T</code>.
   *          The string passed to <code>KLibrary::symbol()</code> is
   *          <code>libName_mainfunc</code>.</dd>
   * <dt>T_config::path</dt><dd>The search pattern for <tt>.desktop</tt> files
   *          containing the plugin descriptions. This is the string passed as
   *          the @p filter argument to
   *          <code>KStandardDirs::findAllResources</code>.</dd>
   * </dl>
   *
   * The last two parameters being strings, they are passed via an
   * encapsulating class, of which <code>mainfunc</code> and
   * <code>path</code> are public static members:
   *
   * <pre>
   * struct MyObjectPluginLoaderConfig {
   *   static const char * const mainfunc;
   *   static const char * const path;
   * };
   * const char * const MyObjectPluginLoaderConfig::mainfunc = "myapp_create_myobject";
   * const char * const MyObjectPluginLoaderConfig::path = "myapp/plugins/ *.desktop";
   * </pre>
   *
   * You would then use a <tt>typedef</tt> to create a less unwieldy
   * name for your plugin loader:
   *
   * <pre>
   * typedef KPIM::PluginLoader< MyObject, MyObjectPluginLoaderConfig > MyObjectPluginLoader;
   * </pre>
   *
   * All of this is what the
   * <code>KPIM_DEFINE_PLUGIN_LOADER(pluginloadername,type,mainfunc,path)</code> macro
   * achieves.
   *
   **/
  template< typename T, typename T_config >
  class KDE_EXPORT PluginLoader : public PluginLoaderBase {
  protected:
    PluginLoader() : PluginLoaderBase() {}

  private:
    static PluginLoader<T,T_config> * mSelf;

  public:
    virtual ~PluginLoader() { mSelf = 0; }

    /** Returns the single instance of this loader. */
    static PluginLoader<T,T_config> * instance() {
      if ( !mSelf ) {
	mSelf = new PluginLoader<T,T_config>();
	mSelf->scan();
      }
      return mSelf;
    }

    /** Rescans the plugin directory to find any newly installed
	plugins.
    **/
    virtual void scan() {
      doScan( T_config::path );
    }

    /** Returns a pointer to a plugin object (of type @p T) or a null
        pointer if the type wasn't found. You can extend this method
        for when you want to handle builtin types */
    virtual T * createForName( const TQString & type ) const {
      void * main_func = mainFunc( type, T_config::mainfunc );
      if ( !main_func ) return 0;

      // cast to a pointer to a function returning T*, call it and
      // return the result; don't you love C? ;-)
      return ((T* (*)())( main_func ))();
    }
  };

  template< typename T, typename T_config >
  PluginLoader<T,T_config> * PluginLoader<T,T_config>::mSelf = 0;

}

#define KPIM_DEFINE_PLUGIN_LOADER( pl, t, mf, p ) \
  namespace { /* don't pollute namespaces */ \
    struct KDE_EXPORT pl##Config { \
      static const char * const mainfunc; \
      static const char * const path; \
    }; \
    const char * const pl##Config::mainfunc = mf; \
    const char * const pl##Config::path = p; \
  } \
  typedef KPIM::PluginLoader< t, pl##Config > pl; \


#endif // __KPIM_SHARED_PLUGINLOADER_H__