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