resourcefactory.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <kdebug.h>
00023 #include <klocale.h>
00024 #include <ksimpleconfig.h>
00025 #include <kstandarddirs.h>
00026
00027 #include <qfile.h>
00028
00029 #include "resourcefactory.h"
00030
00031 using namespace KPIM;
00032
00033 ResourceFactory *ResourceFactory::mSelf = 0;
00034
00035 ResourceFactory *ResourceFactory::self( QString resourceType )
00036 {
00037 kdDebug(5700) << "ResourceFactory::self()" << endl;
00038
00039 if ( !mSelf ) {
00040 mSelf = new ResourceFactory( resourceType );
00041 }
00042
00043 return mSelf;
00044 }
00045
00046 ResourceFactory::ResourceFactory( QString resourceType ) :
00047 mResourceType(resourceType)
00048 {
00049 mResourceList.setAutoDelete( true );
00050
00051 QStringList list = KGlobal::dirs()->findAllResources( "services",
00052 "resources/" + mResourceType + "/*.desktop", true, true );
00053 for ( QStringList::iterator it = list.begin(); it != list.end(); ++it ) {
00054 KSimpleConfig config( *it, true );
00055
00056 if ( !config.hasGroup( "Misc" ) || !config.hasGroup( "Plugin" ) )
00057 continue;
00058
00059 ResourceInfo* info = new ResourceInfo;
00060
00061 config.setGroup( "Plugin" );
00062 QString type = config.readEntry( "Type" );
00063 info->library = config.readEntry( "X-KDE-Library" );
00064
00065 config.setGroup( "Misc" );
00066 info->nameLabel = config.readEntry( "Name" );
00067 info->descriptionLabel = config.readEntry( "Comment", i18n( "No description available." ) );
00068
00069 mResourceList.insert( type, info );
00070 }
00071 }
00072
00073 ResourceFactory::~ResourceFactory()
00074 {
00075 mResourceList.clear();
00076 }
00077
00078 QStringList ResourceFactory::resources()
00079 {
00080 QStringList retval;
00081
00082 QDictIterator<ResourceInfo> it( mResourceList );
00083 for ( ; it.current(); ++it )
00084 retval << it.currentKey();
00085
00086 return retval;
00087 }
00088
00089 ResourceConfigWidget *ResourceFactory::configWidget( const QString& type, QWidget *parent )
00090 {
00091 ResourceConfigWidget *widget = 0;
00092
00093 if ( type.isEmpty() )
00094 return 0;
00095
00096 QString libName = mResourceList[ type ]->library;
00097
00098 KLibrary *library = openLibrary( libName );
00099 if ( !library )
00100 return 0;
00101
00102 void *widget_func = library->symbol( "config_widget" );
00103
00104 if ( widget_func ) {
00105 widget = ((ResourceConfigWidget* (*)(QWidget *wdg))widget_func)( parent );
00106 } else {
00107 kdDebug( 5700 ) << "'" << libName << "' is not a " + mResourceType + " plugin." << endl;
00108 return 0;
00109 }
00110
00111 return widget;
00112 }
00113
00114 ResourceInfo *ResourceFactory::info( const QString &type )
00115 {
00116 if ( type.isEmpty() )
00117 return 0;
00118 else
00119 return mResourceList[ type ];
00120 }
00121
00122 Resource *ResourceFactory::resource( const QString& type, const KConfig *config )
00123 {
00124 Resource *resource = 0;
00125
00126 if ( type.isEmpty() )
00127 return 0;
00128
00129 QString libName = mResourceList[ type ]->library;
00130
00131 KLibrary *library = openLibrary( libName );
00132 if ( !library )
00133 return 0;
00134
00135 void *resource_func = library->symbol( "resource" );
00136
00137 if ( resource_func ) {
00138 resource = ((Resource* (*)(const KConfig *))resource_func)( config );
00139 resource->setType( type );
00140 resource->setNameLabel( mResourceList[ type ]->nameLabel );
00141 resource->setDescriptionLabel( mResourceList[ type ]->descriptionLabel );
00142 } else {
00143 kdDebug( 5700 ) << "'" << libName << "' is not a " + mResourceType + " plugin." << endl;
00144 return 0;
00145 }
00146
00147 return resource;
00148 }
00149
00150 KLibrary *ResourceFactory::openLibrary( const QString& libName )
00151 {
00152 KLibrary *library = 0;
00153
00154 QString path = KLibLoader::findLibrary( QFile::encodeName( libName ) );
00155
00156 if ( path.isEmpty() ) {
00157 kdDebug( 5700 ) << "No resource plugin library was found!" << endl;
00158 return 0;
00159 }
00160
00161 library = KLibLoader::self()->library( QFile::encodeName( path ) );
00162
00163 if ( !library ) {
00164 kdDebug( 5700 ) << "Could not load library '" << libName << "'" << endl;
00165 return 0;
00166 }
00167
00168 return library;
00169 }
This file is part of the documentation for kdelibs Version 3.1.5.