summaryrefslogtreecommitdiffabout
path: root/microkde/kresources/factory.cpp
Unidiff
Diffstat (limited to 'microkde/kresources/factory.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--microkde/kresources/factory.cpp216
1 files changed, 216 insertions, 0 deletions
diff --git a/microkde/kresources/factory.cpp b/microkde/kresources/factory.cpp
new file mode 100644
index 0000000..709cd4a
--- a/dev/null
+++ b/microkde/kresources/factory.cpp
@@ -0,0 +1,216 @@
1/*
2 This file is part of libkresources.
3
4 Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
5 Copyright (c) 2002 Jan-Pascal van Best <janpascal@vanbest.org>
6 Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public
10 License as published by the Free Software Foundation; either
11 version 2 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Library General Public License for more details.
17
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
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.
22*/
23
24#include <kdebug.h>
25#include <klocale.h>
26#include <ksimpleconfig.h>
27#include <kstandarddirs.h>
28#include <kstaticdeleter.h>
29
30#include <qfile.h>
31
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"
41#include "factory.h"
42
43using namespace KRES;
44
45QDict<Factory> *Factory::mSelves = 0;
46static KStaticDeleter< QDict<Factory> > staticDeleter;
47
48Factory *Factory::self( const QString& resourceFamily )
49{
50 kdDebug(5650) << "Factory::self()" << endl;
51
52 Factory *factory = 0;
53 if ( !mSelves )
54 {
55 mSelves = staticDeleter.setObject( new QDict<Factory> );
56 }
57
58 factory = mSelves->find( resourceFamily );
59
60 if ( !factory ) {
61 factory = new Factory( resourceFamily );
62 mSelves->insert( resourceFamily, factory );
63 }
64
65 return factory;
66}
67
68Factory::Factory( const QString& resourceFamily ) :
69 mResourceFamily( resourceFamily )
70{
71//US so far we have three types available for resourceFamily "contact"
72// and that are "file", "dir", "ldap"
73/*US
74
75 KTrader::OfferList plugins = KTrader::self()->query( "KResources/Plugin", QString( "[X-KDE-ResourceFamily] == '%1'" )
76 .arg( resourceFamily ) );
77 KTrader::OfferList::ConstIterator it;
78 for ( it = plugins.begin(); it != plugins.end(); ++it ) {
79 QVariant type = (*it)->property( "X-KDE-ResourceType" );
80 if ( !type.toString().isEmpty() )
81 mTypeMap.insert( type.toString(), *it );
82 }
83*/
84
85//US !!!!!!!!!!!!!!!
86 KRES::PluginFactoryBase* pf = (KRES::PluginFactoryBase*)new KRES::PluginFactory<KABC::ResourceFile,KABC::ResourceFileConfig>();
87 mTypeMap.insert( "file", pf );
88
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}
96
97Factory::~Factory()
98{
99}
100
101QStringList Factory::typeNames() const
102{
103//US method QMap::keys() not available yet. SO collect the data manually
104//US return mTypeMap.keys();
105
106 QStringList result;
107
108 QMap<QString, PluginFactoryBase*>::ConstIterator it;
109 for( it = mTypeMap.begin(); it != mTypeMap.end(); ++it ) {
110 result << it.key().latin1();
111// qDebug("Factory::typeNames() : %s ", it.key().latin1());
112
113 }
114 return result;
115}
116
117ConfigWidget *Factory::configWidget( const QString& type, QWidget *parent )
118{
119 if ( type.isEmpty() || !mTypeMap.contains( type ) )
120 return 0;
121
122/*US load the lib not dynamically. !!
123 KService::Ptr ptr = mTypeMap[ type ];
124 KLibFactory *factory = KLibLoader::self()->factory( ptr->library().latin1() );
125 if ( !factory ) {
126 kdDebug() << "KRES::Factory::configWidget(): Factory creation failed" << endl;
127 return 0;
128 }
129*/
130 PluginFactoryBase *factory = mTypeMap[ type ];
131 if ( !factory ) {
132 kdDebug() << "KRES::Factory::configWidget(): Factory creation failed" << endl;
133 return 0;
134 }
135
136
137 PluginFactoryBase *pluginFactory = static_cast<PluginFactoryBase *>( factory );
138
139 if ( !pluginFactory ) {
140 kdDebug() << "KRES::Factory::configWidget(): no plugin factory." << endl;
141 return 0;
142 }
143
144 ConfigWidget *wdg = pluginFactory->configWidget( parent );
145 if ( !wdg ) {
146//US kdDebug() << "'" << ptr->library() << "' is not a " + mResourceFamily + " plugin." << endl;
147 kdDebug() << " is not a " + mResourceFamily + " plugin." << endl;
148 return 0;
149 }
150 return wdg;
151
152}
153
154QString Factory::typeName( const QString &type ) const
155{
156 if ( type.isEmpty() || !mTypeMap.contains( type ) )
157 return QString();
158
159//US KService::Ptr ptr = mTypeMap[ type ];
160//US return ptr->name();
161//US I guess this is correct since we loaded the factory staticly.
162 return type;
163
164}
165
166QString Factory::typeDescription( const QString &type ) const
167{
168 if ( type.isEmpty() || !mTypeMap.contains( type ) )
169 return QString();
170
171//US KService::Ptr ptr = mTypeMap[ type ];
172//US return ptr->comment();
173//US I guess this is correct since we loaded the factory staticly.
174 return type;
175
176}
177
178Resource *Factory::resource( const QString& type, const KConfig *config )
179{
180 kdDebug() << "Factory::resource( " << type << ", config)" << endl;
181
182 if ( type.isEmpty() || !mTypeMap.contains( type ) )
183 return 0;
184
185/*US load the lib not dynamicly. !!
186 KService::Ptr ptr = mTypeMap[ type ];
187 KLibFactory *factory = KLibLoader::self()->factory( ptr->library().latin1() );
188 if ( !factory ) {
189 kdDebug() << "KRES::Factory::resource(): Factory creation failed" << endl;
190 return 0;
191 }
192*/
193 PluginFactoryBase *factory = mTypeMap[ type ];
194 if ( !factory ) {
195 kdDebug() << "KRES::Factory::resource(): Factory creation failed" << endl;
196 return 0;
197 }
198
199 PluginFactoryBase *pluginFactory = static_cast<PluginFactoryBase *>( factory );
200
201 if ( !pluginFactory ) {
202 kdDebug() << "KRES::Factory::resource(): no plugin factory." << endl;
203 return 0;
204 }
205
206 Resource *resource = pluginFactory->resource( config );
207 if ( !resource ) {
208//US kdDebug() << "'" << ptr->library() << "' is not a " + mResourceFamily + " plugin." << endl;
209 kdDebug() << " is not a " + mResourceFamily + " plugin." << endl;
210 return 0;
211 }
212
213 resource->setType( type );
214
215 return resource;
216}