summaryrefslogtreecommitdiffabout
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--microkde/kresources/factory.cpp100
-rw-r--r--microkde/kresources/factory.h13
2 files changed, 75 insertions, 38 deletions
diff --git a/microkde/kresources/factory.cpp b/microkde/kresources/factory.cpp
index 709cd4a..56b0ef3 100644
--- a/microkde/kresources/factory.cpp
+++ b/microkde/kresources/factory.cpp
@@ -5,212 +5,238 @@
5 Copyright (c) 2002 Jan-Pascal van Best <janpascal@vanbest.org> 5 Copyright (c) 2002 Jan-Pascal van Best <janpascal@vanbest.org>
6 Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org> 6 Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
7 7
8 This library is free software; you can redistribute it and/or 8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public 9 modify it under the terms of the GNU Library General Public
10 License as published by the Free Software Foundation; either 10 License as published by the Free Software Foundation; either
11 version 2 of the License, or (at your option) any later version. 11 version 2 of the License, or (at your option) any later version.
12 12
13 This library is distributed in the hope that it will be useful, 13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Library General Public License for more details. 16 Library General Public License for more details.
17 17
18 You should have received a copy of the GNU Library General Public License 18 You should have received a copy of the GNU Library General Public License
19 along with this library; see the file COPYING.LIB. If not, write to 19 along with this library; see the file COPYING.LIB. If not, write to
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. 21 Boston, MA 02111-1307, USA.
22*/ 22*/
23 23
24#include <kdebug.h> 24#include <kdebug.h>
25#include <klocale.h> 25#include <klocale.h>
26#include <ksimpleconfig.h> 26#include <ksimpleconfig.h>
27#include <kstandarddirs.h> 27#include <kstandarddirs.h>
28#include <kstaticdeleter.h> 28#include <kstaticdeleter.h>
29#include <klibloader.h>
29 30
30#include <qfile.h> 31#include <qfile.h>
31 32
32#include <plugins/file/resourcefile.h>
33#include <plugins/file/resourcefileconfig.h>
34#include <plugins/dir/resourcedir.h>
35#include <plugins/dir/resourcedirconfig.h>
36//#include <plugins/ldap/resourceldap.h>
37//#include <plugins/ldap/resourceldapconfig.h>
38
39
40#include "resource.h" 33#include "resource.h"
41#include "factory.h" 34#include "factory.h"
42 35
43using namespace KRES; 36using namespace KRES;
44 37
45QDict<Factory> *Factory::mSelves = 0; 38QDict<Factory> *Factory::mSelves = 0;
46static KStaticDeleter< QDict<Factory> > staticDeleter; 39static KStaticDeleter< QDict<Factory> > staticDeleter;
47 40
48Factory *Factory::self( const QString& resourceFamily ) 41Factory *Factory::self( const QString& resourceFamily )
49{ 42{
50 kdDebug(5650) << "Factory::self()" << endl; 43 kdDebug(5650) << "Factory::self()" << endl;
51 44
52 Factory *factory = 0; 45 Factory *factory = 0;
53 if ( !mSelves ) 46 if ( !mSelves )
54 { 47 {
55 mSelves = staticDeleter.setObject( new QDict<Factory> ); 48 mSelves = staticDeleter.setObject( new QDict<Factory> );
56 } 49 }
57 50
58 factory = mSelves->find( resourceFamily ); 51 factory = mSelves->find( resourceFamily );
59 52
60 if ( !factory ) { 53 if ( !factory ) {
61 factory = new Factory( resourceFamily ); 54 factory = new Factory( resourceFamily );
62 mSelves->insert( resourceFamily, factory ); 55 mSelves->insert( resourceFamily, factory );
63 } 56 }
64 57
65 return factory; 58 return factory;
66} 59}
67 60
68Factory::Factory( const QString& resourceFamily ) : 61Factory::Factory( const QString& resourceFamily ) :
69 mResourceFamily( resourceFamily ) 62 mResourceFamily( resourceFamily )
70{ 63{
71//US so far we have three types available for resourceFamily "contact" 64//US so far we have three types available for resourceFamily "contact"
72// and that are "file", "dir", "ldap" 65// and that are "file", "dir", "ldap"
73/*US 66/*US
74 67
75 KTrader::OfferList plugins = KTrader::self()->query( "KResources/Plugin", QString( "[X-KDE-ResourceFamily] == '%1'" ) 68 KTrader::OfferList plugins = KTrader::self()->query( "KResources/Plugin", QString( "[X-KDE-ResourceFamily] == '%1'" )
76 .arg( resourceFamily ) ); 69 .arg( resourceFamily ) );
77 KTrader::OfferList::ConstIterator it; 70 KTrader::OfferList::ConstIterator it;
78 for ( it = plugins.begin(); it != plugins.end(); ++it ) { 71 for ( it = plugins.begin(); it != plugins.end(); ++it ) {
79 QVariant type = (*it)->property( "X-KDE-ResourceType" ); 72 QVariant type = (*it)->property( "X-KDE-ResourceType" );
80 if ( !type.toString().isEmpty() ) 73 if ( !type.toString().isEmpty() )
81 mTypeMap.insert( type.toString(), *it ); 74 mTypeMap.insert( type.toString(), *it );
82 } 75 }
83*/ 76*/
84 77
85//US !!!!!!!!!!!!!!! 78//US new
86 KRES::PluginFactoryBase* pf = (KRES::PluginFactoryBase*)new KRES::PluginFactory<KABC::ResourceFile,KABC::ResourceFileConfig>(); 79 PluginInfo* info = new PluginInfo;
87 mTypeMap.insert( "file", pf ); 80 info->library = "microkabc_file";
81 info->nameLabel = i18n( "file" );
82 info->descriptionLabel = i18n( "No description available." );
83 mTypeMap.insert( "file", info );
84
85 info = new PluginInfo;
86 info->library = "microkabc_dir";
87 info->nameLabel = i18n( "dir" );
88 info->descriptionLabel = i18n( "No description available." );
89 mTypeMap.insert( "dir", info );
90
91 info = new PluginInfo;
92 info->library = "microkabc_ldap";
93 info->nameLabel = i18n( "ldap" );
94 info->descriptionLabel = i18n( "No description available." );
95 mTypeMap.insert( "ldap", info );
96
97 //US add opie plugin only, if the library exists
98 QString libname = "microkabc_opie";
99 QString path = KLibLoader::findLibrary( QFile::encodeName( libname ) );
100 if ( !path.isEmpty() )
101 {
102 info = new PluginInfo;
103 info->library = libname;
104 info->nameLabel = i18n( "opie" );
105 info->descriptionLabel = i18n( "Opie PIM Addressbook." );
106 mTypeMap.insert( "opie", info );
107 }
108
109 //US add qtopia plugin only, if the library exists
110 libname = "microkabc_qtopia";
111 path = KLibLoader::findLibrary( QFile::encodeName( libname ) );
112 if ( !path.isEmpty() )
113 {
114 info = new PluginInfo;
115 info->library = libname;
116 info->nameLabel = i18n( "qtopia" );
117 info->descriptionLabel = i18n( "Qtopia PIM Addressbook." );
118 mTypeMap.insert( "qtopia", info );
119 }
120
88 121
89 pf = (KRES::PluginFactoryBase*)new KRES::PluginFactory<KABC::ResourceDir,KABC::ResourceDirConfig>();
90 mTypeMap.insert( "dir", pf );
91 /*
92 pf = (KRES::PluginFactoryBase*)new KRES::PluginFactory<KABC::ResourceLDAP,KABC::ResourceLDAPConfig>();
93 mTypeMap.insert( "ldap", pf );
94 */
95} 122}
96 123
97Factory::~Factory() 124Factory::~Factory()
98{ 125{
99} 126}
100 127
101QStringList Factory::typeNames() const 128QStringList Factory::typeNames() const
102{ 129{
103//US method QMap::keys() not available yet. SO collect the data manually 130//US method QMap::keys() not available yet. SO collect the data manually
104//US return mTypeMap.keys(); 131//US return mTypeMap.keys();
105 132
106 QStringList result; 133 QStringList result;
107 134
108 QMap<QString, PluginFactoryBase*>::ConstIterator it; 135 QMap<QString, PluginInfo*>::ConstIterator it;
109 for( it = mTypeMap.begin(); it != mTypeMap.end(); ++it ) { 136 for( it = mTypeMap.begin(); it != mTypeMap.end(); ++it ) {
110 result << it.key().latin1(); 137 result << it.key().latin1();
111// qDebug("Factory::typeNames() : %s ", it.key().latin1()); 138// qDebug("Factory::typeNames() : %s ", it.key().latin1());
112 139
113 } 140 }
114 return result; 141 return result;
115} 142}
116 143
117ConfigWidget *Factory::configWidget( const QString& type, QWidget *parent ) 144ConfigWidget *Factory::configWidget( const QString& type, QWidget *parent )
118{ 145{
119 if ( type.isEmpty() || !mTypeMap.contains( type ) ) 146 if ( type.isEmpty() || !mTypeMap.contains( type ) )
120 return 0; 147 return 0;
121 148
122/*US load the lib not dynamically. !! 149//US KService::Ptr ptr = mTypeMap[ type ];
123 KService::Ptr ptr = mTypeMap[ type ]; 150//US KLibFactory *factory = KLibLoader::self()->factory( ptr->library().latin1() );
124 KLibFactory *factory = KLibLoader::self()->factory( ptr->library().latin1() ); 151 PluginInfo* pi = mTypeMap[ type ];
125 if ( !factory ) { 152 KLibFactory *factory = (KLibFactory *)KLibLoader::self()->factory( pi->library.latin1() );
126 kdDebug() << "KRES::Factory::configWidget(): Factory creation failed" << endl;
127 return 0;
128 }
129*/
130 PluginFactoryBase *factory = mTypeMap[ type ];
131 if ( !factory ) { 153 if ( !factory ) {
154 qDebug("KRES::Factory::configWidget(): Factory creation failed for library %s", pi->library.latin1());
132 kdDebug() << "KRES::Factory::configWidget(): Factory creation failed" << endl; 155 kdDebug() << "KRES::Factory::configWidget(): Factory creation failed" << endl;
133 return 0; 156 return 0;
134 } 157 }
135 158
136
137 PluginFactoryBase *pluginFactory = static_cast<PluginFactoryBase *>( factory ); 159 PluginFactoryBase *pluginFactory = static_cast<PluginFactoryBase *>( factory );
138 160
139 if ( !pluginFactory ) { 161 if ( !pluginFactory ) {
162 qDebug("KRES::Factory::configWidget(): no plugin factory for library %s", pi->library.latin1());
140 kdDebug() << "KRES::Factory::configWidget(): no plugin factory." << endl; 163 kdDebug() << "KRES::Factory::configWidget(): no plugin factory." << endl;
141 return 0; 164 return 0;
142 } 165 }
143 166
144 ConfigWidget *wdg = pluginFactory->configWidget( parent ); 167 ConfigWidget *wdg = pluginFactory->configWidget( parent );
145 if ( !wdg ) { 168 if ( !wdg ) {
146//US kdDebug() << "'" << ptr->library() << "' is not a " + mResourceFamily + " plugin." << endl; 169//US kdDebug() << "'" << ptr->library() << "' is not a " + mResourceFamily + " plugin." << endl;
147 kdDebug() << " is not a " + mResourceFamily + " plugin." << endl; 170 qDebug("%s is not a %s plugin.", pi->library.latin1(), mResourceFamily.latin1());
148 return 0; 171 return 0;
149 } 172 }
150 return wdg; 173 return wdg;
151 174
152} 175}
153 176
154QString Factory::typeName( const QString &type ) const 177QString Factory::typeName( const QString &type ) const
155{ 178{
156 if ( type.isEmpty() || !mTypeMap.contains( type ) ) 179 if ( type.isEmpty() || !mTypeMap.contains( type ) )
157 return QString(); 180 return QString();
158 181
182
159//US KService::Ptr ptr = mTypeMap[ type ]; 183//US KService::Ptr ptr = mTypeMap[ type ];
160//US return ptr->name(); 184//US return ptr->name();
161//US I guess this is correct since we loaded the factory staticly. 185 PluginInfo* pi = mTypeMap[ type ];
162 return type; 186 return pi->nameLabel;
163 187
164} 188}
165 189
166QString Factory::typeDescription( const QString &type ) const 190QString Factory::typeDescription( const QString &type ) const
167{ 191{
168 if ( type.isEmpty() || !mTypeMap.contains( type ) ) 192 if ( type.isEmpty() || !mTypeMap.contains( type ) )
169 return QString(); 193 return QString();
170 194
171//US KService::Ptr ptr = mTypeMap[ type ]; 195//US KService::Ptr ptr = mTypeMap[ type ];
172//US return ptr->comment(); 196//US return ptr->comment();
173//US I guess this is correct since we loaded the factory staticly. 197 PluginInfo* pi = mTypeMap[ type ];
174 return type; 198 return pi->descriptionLabel;
175
176} 199}
177 200
178Resource *Factory::resource( const QString& type, const KConfig *config ) 201Resource *Factory::resource( const QString& type, const KConfig *config )
179{ 202{
180 kdDebug() << "Factory::resource( " << type << ", config)" << endl; 203 kdDebug() << "Factory::resource( " << type << ", config)" << endl;
181 204
182 if ( type.isEmpty() || !mTypeMap.contains( type ) ) 205 if ( type.isEmpty() || !mTypeMap.contains( type ) )
183 return 0; 206 return 0;
184 207
185/*US load the lib not dynamicly. !! 208/*US load the lib not dynamicly. !!
186 KService::Ptr ptr = mTypeMap[ type ]; 209 KService::Ptr ptr = mTypeMap[ type ];
187 KLibFactory *factory = KLibLoader::self()->factory( ptr->library().latin1() ); 210 KLibFactory *factory = KLibLoader::self()->factory( ptr->library().latin1() );
188 if ( !factory ) { 211 if ( !factory ) {
189 kdDebug() << "KRES::Factory::resource(): Factory creation failed" << endl; 212 kdDebug() << "KRES::Factory::resource(): Factory creation failed" << endl;
190 return 0; 213 return 0;
191 } 214 }
192*/ 215*/
193 PluginFactoryBase *factory = mTypeMap[ type ]; 216 PluginInfo* pi = mTypeMap[ type ];
217 KLibFactory *factory = (KLibFactory *)KLibLoader::self()->factory( pi->library.latin1() );
194 if ( !factory ) { 218 if ( !factory ) {
219 qDebug("KRES::Factory::resource(): Factory creation failed for library %s", pi->library.latin1());
195 kdDebug() << "KRES::Factory::resource(): Factory creation failed" << endl; 220 kdDebug() << "KRES::Factory::resource(): Factory creation failed" << endl;
196 return 0; 221 return 0;
197 } 222 }
198 223
199 PluginFactoryBase *pluginFactory = static_cast<PluginFactoryBase *>( factory ); 224 PluginFactoryBase *pluginFactory = static_cast<PluginFactoryBase *>( factory );
200 225
201 if ( !pluginFactory ) { 226 if ( !pluginFactory ) {
227 qDebug("KRES::Factory::resource(): no plugin factory for library %s", pi->library.latin1());
202 kdDebug() << "KRES::Factory::resource(): no plugin factory." << endl; 228 kdDebug() << "KRES::Factory::resource(): no plugin factory." << endl;
203 return 0; 229 return 0;
204 } 230 }
205 231
206 Resource *resource = pluginFactory->resource( config ); 232 Resource *resource = pluginFactory->resource( config );
207 if ( !resource ) { 233 if ( !resource ) {
208//US kdDebug() << "'" << ptr->library() << "' is not a " + mResourceFamily + " plugin." << endl; 234//US kdDebug() << "'" << ptr->library() << "' is not a " + mResourceFamily + " plugin." << endl;
209 kdDebug() << " is not a " + mResourceFamily + " plugin." << endl; 235 qDebug("%s is not a %s plugin.", pi->library.latin1(), mResourceFamily.latin1());
210 return 0; 236 return 0;
211 } 237 }
212 238
213 resource->setType( type ); 239 resource->setType( type );
214 240
215 return resource; 241 return resource;
216} 242}
diff --git a/microkde/kresources/factory.h b/microkde/kresources/factory.h
index f391bb3..ad67ab3 100644
--- a/microkde/kresources/factory.h
+++ b/microkde/kresources/factory.h
@@ -13,70 +13,80 @@
13 This library is distributed in the hope that it will be useful, 13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Library General Public License for more details. 16 Library General Public License for more details.
17 17
18 You should have received a copy of the GNU Library General Public License 18 You should have received a copy of the GNU Library General Public License
19 along with this library; see the file COPYING.LIB. If not, write to 19 along with this library; see the file COPYING.LIB. If not, write to
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. 21 Boston, MA 02111-1307, USA.
22*/ 22*/
23 23
24#ifndef KRESOURCES_FACTORY_H 24#ifndef KRESOURCES_FACTORY_H
25#define KRESOURCES_FACTORY_H 25#define KRESOURCES_FACTORY_H
26 26
27#include <qdict.h> 27#include <qdict.h>
28#include <qstring.h> 28#include <qstring.h>
29 29
30#include <kconfig.h> 30#include <kconfig.h>
31 31
32 32
33#include "resource.h" 33#include "resource.h"
34 34
35namespace KRES { 35namespace KRES {
36 36
37//US
38struct PluginInfo
39{
40 QString library;
41 QString nameLabel;
42 QString descriptionLabel;
43};
44
45
37/** 46/**
38 * Class for loading resource plugins. 47 * Class for loading resource plugins.
39 * Do not use this class directly. Use ResourceManager instead 48 * Do not use this class directly. Use ResourceManager instead
40 * 49 *
41 * Example: 50 * Example:
42 * 51 *
43 * <pre> 52 * <pre>
44 * KABC::Factory<Calendar> *factory = KABC::Factory<Calendar>::self(); 53 * KABC::Factory<Calendar> *factory = KABC::Factory<Calendar>::self();
45 * 54 *
46 * QStringList list = factory->resources(); 55 * QStringList list = factory->resources();
47 * QStringList::Iterator it; 56 * QStringList::Iterator it;
48 * for ( it = list.begin(); it != list.end(); ++it ) { 57 * for ( it = list.begin(); it != list.end(); ++it ) {
49 * Resource<Calendar> *resource = factory->resource( (*it), 58 * Resource<Calendar> *resource = factory->resource( (*it),
50 * KABC::StdAddressBook::self(), 0 ); 59 * KABC::StdAddressBook::self(), 0 );
51 * // do something with resource 60 * // do something with resource
52 * } 61 * }
53 * </pre> 62 * </pre>
54 */ 63 */
55class Factory 64class Factory
56{ 65{
57 public: 66 public:
58 67
68
59 /** 69 /**
60 * Returns the global resource factory. 70 * Returns the global resource factory.
61 */ 71 */
62 static Factory *self( const QString& resourceFamily ); 72 static Factory *self( const QString& resourceFamily );
63 73
64 ~Factory(); 74 ~Factory();
65 75
66 /** 76 /**
67 * Returns the config widget for the given resource type, 77 * Returns the config widget for the given resource type,
68 * or a null pointer if resource type doesn't exist. 78 * or a null pointer if resource type doesn't exist.
69 * 79 *
70 * @param type The type of the resource, returned by @ref resources() 80 * @param type The type of the resource, returned by @ref resources()
71 * @param resource The resource to be editted. 81 * @param resource The resource to be editted.
72 * @param parent The parent widget 82 * @param parent The parent widget
73 */ 83 */
74 ConfigWidget *configWidget( const QString& type, QWidget *parent = 0 ); 84 ConfigWidget *configWidget( const QString& type, QWidget *parent = 0 );
75 85
76 /** 86 /**
77 * Returns a pointer to a resource object or a null pointer 87 * Returns a pointer to a resource object or a null pointer
78 * if resource type doesn't exist. 88 * if resource type doesn't exist.
79 * 89 *
80 * @param type The type of the resource, returned by @ref resources() 90 * @param type The type of the resource, returned by @ref resources()
81 * @param ab The address book, the resource should belong to 91 * @param ab The address book, the resource should belong to
82 * @param config The config object where the resource get it settings from, or 0 if a new resource should be created. 92 * @param config The config object where the resource get it settings from, or 0 if a new resource should be created.
@@ -85,29 +95,30 @@ class Factory
85 95
86 /** 96 /**
87 * Returns a list of all available resource types. 97 * Returns a list of all available resource types.
88 */ 98 */
89 QStringList typeNames() const; 99 QStringList typeNames() const;
90 100
91 /** 101 /**
92 * Returns the name for a special type. 102 * Returns the name for a special type.
93 */ 103 */
94 QString typeName( const QString &type ) const; 104 QString typeName( const QString &type ) const;
95 105
96 /** 106 /**
97 * Returns the description for a special type. 107 * Returns the description for a special type.
98 */ 108 */
99 QString typeDescription( const QString &type ) const; 109 QString typeDescription( const QString &type ) const;
100 110
101 protected: 111 protected:
102 Factory( const QString& resourceFamily ); 112 Factory( const QString& resourceFamily );
103 113
104 private: 114 private:
105 static QDict<Factory> *mSelves; 115 static QDict<Factory> *mSelves;
106 116
107 QString mResourceFamily; 117 QString mResourceFamily;
108//US QMap<QString, KService::Ptr> mTypeMap; 118//US QMap<QString, KService::Ptr> mTypeMap;
109 QMap<QString, PluginFactoryBase*> mTypeMap; 119//US lets store the pluginfo struct as value instead of a KService
120 QMap<QString, PluginInfo*> mTypeMap;
110}; 121};
111 122
112} 123}
113#endif 124#endif