kilobyte is kB not kb
[kdepim.git] / messageviewer / bodypartformatterfactory.cpp
blob4e4828b9d9f5fa308a5720a3741890e7653a7804
1 /* -*- mode: C++; c-file-style: "gnu" -*-
2 bodypartformatterfactory.cpp
4 This file is part of KMail, the KDE mail client.
5 Copyright (c) 2004 Marc Mutz <mutz@kde.org>,
6 Ingo Kloecker <kloecker@kde.org>
8 KMail is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 KMail is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 In addition, as a special exception, the copyright holders give
23 permission to link the code of this program with any edition of
24 the Qt library by Trolltech AS, Norway (or with modified versions
25 of Qt that use the same license as Qt), and distribute linked
26 combinations including the two. You must obey the GNU General
27 Public License in all respects for all of the code used other than
28 Qt. If you modify this file, you may extend this exception to
29 your version of the file, but you are not obligated to do so. If
30 you do not wish to do so, delete this exception statement from
31 your version.
35 #include <config-messageviewer.h>
37 #include "bodypartformatterfactory.h"
38 #include "bodypartformatterfactory_p.h"
40 #include "interfaces/bodypartformatter.h"
41 #include "pluginloader.h"
42 #include "urlhandlermanager.h"
44 // KDE
45 #include <kdebug.h>
47 // Qt
48 #include <QString>
49 #include <QStringList>
51 #include <assert.h>
53 using namespace MessageViewer::BodyPartFormatterFactoryPrivate;
54 using namespace MessageViewer;
56 #ifdef _WIN32_WCE
57 extern "C"
58 Interface::BodyPartFormatterPlugin *
59 messageviewer_bodypartformatter_text_calendar_create_bodypart_formatter_plugin();
60 #else
61 namespace {
63 DEFINE_PLUGIN_LOADER( BodyPartFormatterPluginLoader,
64 Interface::BodyPartFormatterPlugin,
65 "create_bodypart_formatter_plugin",
66 "messageviewer/plugins/bodypartformatter/*.desktop" )
69 #endif
71 BodyPartFormatterFactory * BodyPartFormatterFactory::mSelf = 0;
73 const BodyPartFormatterFactory * BodyPartFormatterFactory::instance() {
74 if ( !mSelf )
75 mSelf = new BodyPartFormatterFactory();
76 return mSelf;
79 BodyPartFormatterFactory::BodyPartFormatterFactory() {
80 mSelf = this;
83 BodyPartFormatterFactory::~BodyPartFormatterFactory() {
84 mSelf = 0;
87 static TypeRegistry * all = 0;
89 static void insertBodyPartFormatter( const char * type, const char * subtype,
90 const Interface::BodyPartFormatter * formatter ) {
91 if ( !type || !*type || !subtype || !*subtype || !formatter || !all )
92 return;
94 TypeRegistry::iterator type_it = all->find( type );
95 if ( type_it == all->end() ) {
96 //kDebug() << "BodyPartFormatterFactory: instantiating new Subtype Registry for \""
97 // << type << "\"";
98 type_it = all->insert( std::make_pair( type, SubtypeRegistry() ) ).first;
99 assert( type_it != all->end() );
102 SubtypeRegistry & subtype_reg = type_it->second;
103 SubtypeRegistry::iterator subtype_it = subtype_reg.find( subtype );
104 if ( subtype_it != subtype_reg.end() ) {
105 kDebug() << "BodyPartFormatterFactory: overwriting previously registered formatter for \""
106 << type << "/" << subtype << "\"";
107 subtype_reg.erase( subtype_it ); subtype_it = subtype_reg.end();
110 subtype_reg.insert( std::make_pair( subtype, formatter ) );
113 static void loadPlugins() {
114 #ifndef _WIN32_WCE
115 const BodyPartFormatterPluginLoader * pl = BodyPartFormatterPluginLoader::instance();
116 if ( !pl ) {
117 kWarning() << "BodyPartFormatterFactory: cannot instantiate plugin loader!";
118 return;
120 const QStringList types = pl->types();
121 //kDebug() << "BodyPartFormatterFactory: found" << types.size() << "plugins.";
122 for ( QStringList::const_iterator it = types.begin() ; it != types.end() ; ++it ) {
123 const Interface::BodyPartFormatterPlugin * plugin = pl->createForName( *it );
124 if ( !plugin ) {
125 kWarning() << "BodyPartFormatterFactory: plugin" << *it << "is not valid!";
126 continue;
128 const Interface::BodyPartFormatter * bfp;
129 for ( int i = 0 ; (bfp = plugin->bodyPartFormatter( i )) ; ++i ) {
130 const char * type = plugin->type( i );
131 if ( !type || !*type ) {
132 kWarning() << "BodyPartFormatterFactory: plugin" << *it
133 << "returned empty type specification for index"
134 << i;
135 break;
137 const char * subtype = plugin->subtype( i );
138 if ( !subtype || !*subtype ) {
139 kWarning() << "BodyPartFormatterFactory: plugin" << *it
140 << "returned empty subtype specification for index"
141 << i;
142 break;
144 insertBodyPartFormatter( type, subtype, bfp );
146 const Interface::BodyPartURLHandler * handler;
147 for ( int i = 0 ; (handler = plugin->urlHandler( i )) ; ++i )
148 URLHandlerManager::instance()->registerHandler( handler );
150 #else
152 const Interface::BodyPartFormatterPlugin * plugin = messageviewer_bodypartformatter_text_calendar_create_bodypart_formatter_plugin();
153 const Interface::BodyPartFormatter * bfp;
154 for ( int i = 0 ; (bfp = plugin->bodyPartFormatter( i )) ; ++i ) {
155 const char * type = plugin->type( i );
156 if ( !type || !*type ) {
157 kWarning() << "BodyPartFormatterFactory: plugin bodypartformatter_text_calendar"
158 << "returned empty type specification for index"
159 << i;
160 break;
162 const char * subtype = plugin->subtype( i );
163 if ( !subtype || !*subtype ) {
164 kWarning() << "BodyPartFormatterFactory: plugin bodypartformatter_text_calendar"
165 << "returned empty subtype specification for index"
166 << i;
167 break;
169 insertBodyPartFormatter( type, subtype, bfp );
171 const Interface::BodyPartURLHandler * handler;
172 for ( int i = 0 ; (handler = plugin->urlHandler( i )) ; ++i )
173 URLHandlerManager::instance()->registerHandler( handler );
174 #endif
177 static void setup() {
178 if ( !all ) {
179 all = new TypeRegistry();
180 messageviewer_create_builtin_bodypart_formatters( all );
181 loadPlugins();
185 const Interface::BodyPartFormatter * BodyPartFormatterFactory::createFor( const char * type, const char * subtype ) const {
186 if ( !type || !*type )
187 type = "*"; //krazy:exclude=doublequote_chars
188 if ( !subtype || !*subtype )
189 subtype = "*"; //krazy:exclude=doublequote_chars
191 setup();
192 assert( all );
194 if ( all->empty() )
195 return 0;
197 TypeRegistry::const_iterator type_it = all->find( type );
198 if ( type_it == all->end() )
199 type_it = all->find( "*" );
200 if ( type_it == all->end() )
201 return 0;
203 const SubtypeRegistry & subtype_reg = type_it->second;
204 if ( subtype_reg.empty() )
205 return 0;
207 SubtypeRegistry::const_iterator subtype_it = subtype_reg.find( subtype );
208 if ( subtype_it == subtype_reg.end() )
209 subtype_it = subtype_reg.find( "*" );
210 if ( subtype_it == subtype_reg.end() )
211 return 0;
213 if ( !(*subtype_it).second ) {
214 kWarning() << "BodyPartFormatterFactory: a null bodypart formatter sneaked in for \""
215 << type << "/" << subtype << "\"!";
218 return (*subtype_it).second;
221 const Interface::BodyPartFormatter * BodyPartFormatterFactory::createFor( const QString & type, const QString & subtype ) const {
222 return createFor( type.toLatin1(), subtype.toLatin1() );
225 const Interface::BodyPartFormatter * BodyPartFormatterFactory::createFor( const QByteArray & type, const QByteArray & subtype ) const {
226 return createFor( type.data(), subtype.data() );