38 #include <QtCore/QFile>
41 #include <klocalizedstring.h>
43 #include <kconfiggroup.h>
45 #include <kservicetypetrader.h>
46 #include <kpluginloader.h>
52 class Factory::Private
55 Resource *resourceInternal (
const QString &type,
const KConfigGroup *group );
60 class FactoryMap :
public QMap<QString, Factory*>
63 ~FactoryMap() { qDeleteAll( *
this ); }
66 K_GLOBAL_STATIC( FactoryMap, mSelves )
74 factory = mSelves->value( resourceFamily, 0 );
77 factory =
new Factory( resourceFamily );
78 mSelves->insert( resourceFamily, factory );
82 KConfigGroup migrationCfg( &config,
"Migration" );
83 const bool enabled = migrationCfg.readEntry(
"Enabled",
false );
84 const bool setupClientBrige = migrationCfg.readEntry(
"SetupClientBridge",
true );
85 const int currentVersion = migrationCfg.readEntry(
QLatin1String(
"Version-") + resourceFamily, 0 );
86 const int targetVersion = migrationCfg.readEntry(
"TargetVersion", 0 );
87 if ( enabled && currentVersion < targetVersion ) {
88 kDebug() <<
"Performing Akonadi migration. Good luck!";
91 if ( !setupClientBrige ) {
96 bool result = proc.waitForStarted();
98 result = proc.waitForFinished();
100 if ( result && proc.exitCode() == 0 ) {
101 kDebug() <<
"Akonadi migration has been successful";
102 migrationCfg.writeEntry(
QLatin1String(
"Version-") + resourceFamily, targetVersion );
104 }
else if ( !result || proc.exitCode() != 1 ) {
106 kError() <<
"Akonadi migration failed!";
107 kError() <<
"command was: " << proc.program();
108 kError() <<
"exit code: " << proc.exitCode();
109 kError() <<
"stdout: " << proc.readAllStandardOutput();
110 kError() <<
"stderr: " << proc.readAllStandardError();
119 Factory::Factory(
const QString &resourceFamily ) :
120 d( new KRES::
Factory::Private )
122 d->mResourceFamily = resourceFamily;
129 const KService::List plugins =
130 KServiceTypeTrader::self()->query(
134 KService::List::ConstIterator it;
135 for ( it = plugins.begin(); it != plugins.end(); ++it ) {
138 d->mTypeMap.insert( type.
toString(), *it );
150 return d->mTypeMap.keys();
155 if ( type.
isEmpty() || !d->mTypeMap.contains( type ) ) {
159 KService::Ptr ptr = d->mTypeMap[ type ];
160 KPluginLoader loader( ptr->library() );
161 KPluginFactory *factory = loader.factory();
163 kDebug() <<
"Factory creation failed: " << loader.errorString();
167 PluginFactoryBase *pluginFactory =
static_cast<PluginFactoryBase *
>( factory );
169 if ( !pluginFactory ) {
170 kDebug() <<
"no plugin factory.";
174 ConfigWidget *wdg = pluginFactory->configWidget( parent );
176 kDebug() <<
"'" << ptr->library() <<
"' doesn't provide a ConfigWidget";
185 if ( type.
isEmpty() || !d->mTypeMap.contains( type ) ) {
189 KService::Ptr ptr = d->mTypeMap[ type ];
195 if ( type.
isEmpty() || !d->mTypeMap.contains( type ) ) {
199 KService::Ptr ptr = d->mTypeMap[ type ];
200 return ptr->comment();
203 Resource *Factory::Private::resourceInternal(
const QString &type,
const KConfigGroup *group )
205 kDebug() <<
"(" << type <<
", config )";
207 if ( type.
isEmpty() || !mTypeMap.contains( type ) ) {
208 kDebug() <<
"no such type" << type;
212 KService::Ptr ptr = mTypeMap[ type ];
213 KPluginLoader loader( ptr->library() );
214 KPluginFactory *factory = loader.factory();
216 kDebug() <<
"Factory creation failed" << loader.errorString();
220 PluginFactoryBase *pluginFactory =
static_cast<PluginFactoryBase *
>( factory );
222 if ( !pluginFactory ) {
223 kDebug() <<
"no plugin factory.";
229 resource = pluginFactory->resource( *group );
231 resource = pluginFactory->resource();
235 kDebug() <<
"'" << ptr->library()
236 <<
"' is not a" << mResourceFamily <<
"plugin.";
247 return d->resourceInternal( type, &group );
252 return d->resourceInternal( type, 0 );
QStringList typeNames() const
Returns a list of all available resource types.
void setType(const QString &type)
Sets the resource type.
Resource * resource(const QString &type, const KConfigGroup &group)
Returns a pointer to a resource object or a null pointer if resource type doesn't exist...
This class provides a resource which is managed in a general way.
This file is part of the KDE resource framework and defines the Factory class.
void reloadConfig()
Reload the configuration.
ConfigWidget * configWidget(const QString &type, QWidget *parent=0)
Returns the config widget for the given resource type, or a null pointer if resource type doesn't exi...
QString typeName(const QString &type) const
Returns the name for a special type.
QString fromLatin1(const char *str, int size)
QString typeDescription(const QString &type) const
Returns the description for a special type.
A class for loading resource plugins.