34 #include "bodypartformatterfactory.h"
35 #include "bodypartformatterfactory_p.h"
36 using namespace KMail::BodyPartFormatterFactoryPrivate;
38 #include "interfaces/bodypartformatter.h"
39 #include "urlhandlermanager.h"
42 #include <libtdepim/pluginloader.h>
49 #include <tqcstring.h>
50 #include <tqstringlist.h>
56 KPIM_DEFINE_PLUGIN_LOADER( BodyPartFormatterPluginLoader,
58 "create_bodypart_formatter_plugin",
59 "kmail/plugins/bodypartformatter/*.desktop" )
63 KMail::BodyPartFormatterFactory * KMail::BodyPartFormatterFactory::mSelf = 0;
65 const KMail::BodyPartFormatterFactory * KMail::BodyPartFormatterFactory::instance() {
67 mSelf =
new BodyPartFormatterFactory();
71 KMail::BodyPartFormatterFactory::BodyPartFormatterFactory() {
75 KMail::BodyPartFormatterFactory::~BodyPartFormatterFactory() {
79 static TypeRegistry * all = 0;
81 static void insertBodyPartFormatter(
const char * type,
const char * subtype,
82 const KMail::Interface::BodyPartFormatter * formatter ) {
83 if ( !type || !*type || !subtype || !*subtype || !formatter || !all )
86 TypeRegistry::iterator type_it = all->find( type );
87 if ( type_it == all->end() ) {
88 kdDebug( 5006 ) <<
"BodyPartFormatterFactory: instantiating new Subtype Registry for \""
89 << type <<
"\"" << endl;
90 type_it = all->insert( std::make_pair( type, SubtypeRegistry() ) ).first;
91 assert( type_it != all->end() );
94 SubtypeRegistry & subtype_reg = type_it->second;
95 SubtypeRegistry::iterator subtype_it = subtype_reg.find( subtype );
96 if ( subtype_it != subtype_reg.end() ) {
97 kdDebug( 5006 ) <<
"BodyPartFormatterFactory: overwriting previously registered formatter for \""
98 << type <<
"/" << subtype <<
"\"" << endl;
99 subtype_reg.erase( subtype_it ); subtype_it = subtype_reg.end();
102 subtype_reg.insert( std::make_pair( subtype, formatter ) );
105 static void loadPlugins() {
106 const BodyPartFormatterPluginLoader * pl = BodyPartFormatterPluginLoader::instance();
108 kdWarning( 5006 ) <<
"BodyPartFormatterFactory: cannot instantiate plugin loader!" << endl;
111 const TQStringList types = pl->types();
112 kdDebug( 5006 ) <<
"BodyPartFormatterFactory: found " << types.size() <<
" plugins." << endl;
113 for ( TQStringList::const_iterator it = types.begin() ; it != types.end() ; ++it ) {
116 kdWarning( 5006 ) <<
"BodyPartFormatterFactory: plugin \"" << *it <<
"\" is not valid!" << endl;
119 for (
int i = 0 ;
const KMail::Interface::BodyPartFormatter * bfp = plugin->bodyPartFormatter( i ) ; ++i ) {
120 const char * type = plugin->type( i );
121 if ( !type || !*type ) {
122 kdWarning( 5006 ) <<
"BodyPartFormatterFactory: plugin \"" << *it
123 <<
"\" returned empty type specification for index "
127 const char * subtype = plugin->subtype( i );
128 if ( !subtype || !*subtype ) {
129 kdWarning( 5006 ) <<
"BodyPartFormatterFactory: plugin \"" << *it
130 <<
"\" returned empty subtype specification for index "
134 insertBodyPartFormatter( type, subtype, bfp );
137 KMail::URLHandlerManager::instance()->registerHandler( handler );
141 static void setup() {
143 all =
new TypeRegistry();
144 kmail_create_builtin_bodypart_formatters( all );
150 const KMail::Interface::BodyPartFormatter * KMail::BodyPartFormatterFactory::createFor(
const char * type,
const char * subtype )
const {
151 if ( !type || !*type )
153 if ( !subtype || !*subtype )
162 TypeRegistry::const_iterator type_it = all->find( type );
163 if ( type_it == all->end() )
164 type_it = all->find(
"*" );
165 if ( type_it == all->end() )
168 const SubtypeRegistry & subtype_reg = type_it->second;
169 if ( subtype_reg.empty() )
172 SubtypeRegistry::const_iterator subtype_it = subtype_reg.find( subtype );
173 if ( subtype_it == subtype_reg.end() )
174 subtype_it = subtype_reg.find(
"*" );
175 if ( subtype_it == subtype_reg.end() )
178 kdWarning( !(*subtype_it).second, 5006 )
179 <<
"BodyPartFormatterFactory: a null bodypart formatter sneaked in for \""
180 << type <<
"/" << subtype <<
"\"!" << endl;
182 return (*subtype_it).second;
185 const KMail::Interface::BodyPartFormatter * KMail::BodyPartFormatterFactory::createFor(
const TQString & type,
const TQString & subtype )
const {
186 return createFor( type.latin1(), subtype.latin1() );
189 const KMail::Interface::BodyPartFormatter * KMail::BodyPartFormatterFactory::createFor(
const TQCString & type,
const TQCString & subtype )
const {
190 return createFor( type.data(), subtype.data() );
interface for BodyPartFormatter plugins
An interface to body part reader link handlers.