Fix akonadimodel.cpp:1: warning: unterminated character constant
[kdepim.git] / messageviewer / bodypartformatterfactory.cpp
blob0c0eb2edf8e83c9965e91b1305e382b0fd31a9f6
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 "bodypartformatterfactory.h"
36 #include "bodypartformatterfactory_p.h"
38 #include "interfaces/bodypartformatter.h"
39 #include "pluginloader.h"
40 #include "urlhandlermanager.h"
42 // KDE
43 #include <kdebug.h>
45 // Qt
46 #include <QString>
47 #include <QStringList>
49 #include <assert.h>
51 using namespace MessageViewer::BodyPartFormatterFactoryPrivate;
52 using namespace MessageViewer;
54 namespace {
56 DEFINE_PLUGIN_LOADER( BodyPartFormatterPluginLoader,
57 Interface::BodyPartFormatterPlugin,
58 "create_bodypart_formatter_plugin",
59 "messageviewer/plugins/bodypartformatter/*.desktop" )
63 BodyPartFormatterFactory * BodyPartFormatterFactory::mSelf = 0;
65 const BodyPartFormatterFactory * BodyPartFormatterFactory::instance() {
66 if ( !mSelf )
67 mSelf = new BodyPartFormatterFactory();
68 return mSelf;
71 BodyPartFormatterFactory::BodyPartFormatterFactory() {
72 mSelf = this;
75 BodyPartFormatterFactory::~BodyPartFormatterFactory() {
76 mSelf = 0;
79 static TypeRegistry * all = 0;
81 static void insertBodyPartFormatter( const char * type, const char * subtype,
82 const Interface::BodyPartFormatter * formatter ) {
83 if ( !type || !*type || !subtype || !*subtype || !formatter || !all )
84 return;
86 TypeRegistry::iterator type_it = all->find( type );
87 if ( type_it == all->end() ) {
88 kDebug() << "BodyPartFormatterFactory: instantiating new Subtype Registry for \""
89 << type << "\"";
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 kDebug() << "BodyPartFormatterFactory: overwriting previously registered formatter for \""
98 << type << "/" << subtype << "\"";
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();
107 if ( !pl ) {
108 kWarning() << "BodyPartFormatterFactory: cannot instantiate plugin loader!";
109 return;
111 const QStringList types = pl->types();
112 kDebug() << "BodyPartFormatterFactory: found" << types.size() << "plugins.";
113 for ( QStringList::const_iterator it = types.begin() ; it != types.end() ; ++it ) {
114 const Interface::BodyPartFormatterPlugin * plugin = pl->createForName( *it );
115 if ( !plugin ) {
116 kWarning() << "BodyPartFormatterFactory: plugin" << *it << "is not valid!";
117 continue;
119 const Interface::BodyPartFormatter * bfp;
120 for ( int i = 0 ; (bfp = plugin->bodyPartFormatter( i )) ; ++i ) {
121 const char * type = plugin->type( i );
122 if ( !type || !*type ) {
123 kWarning() << "BodyPartFormatterFactory: plugin" << *it
124 << "returned empty type specification for index"
125 << i;
126 break;
128 const char * subtype = plugin->subtype( i );
129 if ( !subtype || !*subtype ) {
130 kWarning() << "BodyPartFormatterFactory: plugin" << *it
131 << "returned empty subtype specification for index"
132 << i;
133 break;
135 insertBodyPartFormatter( type, subtype, bfp );
137 const Interface::BodyPartURLHandler * handler;
138 for ( int i = 0 ; (handler = plugin->urlHandler( i )) ; ++i )
139 URLHandlerManager::instance()->registerHandler( handler );
143 static void setup() {
144 if ( !all ) {
145 all = new TypeRegistry();
146 messageviewer_create_builtin_bodypart_formatters( all );
147 loadPlugins();
151 const Interface::BodyPartFormatter * BodyPartFormatterFactory::createFor( const char * type, const char * subtype ) const {
152 if ( !type || !*type )
153 type = "*"; //krazy:exclude=doublequote_chars
154 if ( !subtype || !*subtype )
155 subtype = "*"; //krazy:exclude=doublequote_chars
157 setup();
158 assert( all );
160 if ( all->empty() )
161 return 0;
163 TypeRegistry::const_iterator type_it = all->find( type );
164 if ( type_it == all->end() )
165 type_it = all->find( "*" );
166 if ( type_it == all->end() )
167 return 0;
169 const SubtypeRegistry & subtype_reg = type_it->second;
170 if ( subtype_reg.empty() )
171 return 0;
173 SubtypeRegistry::const_iterator subtype_it = subtype_reg.find( subtype );
174 if ( subtype_it == subtype_reg.end() )
175 subtype_it = subtype_reg.find( "*" );
176 if ( subtype_it == subtype_reg.end() )
177 return 0;
179 if ( !(*subtype_it).second ) {
180 kWarning() << "BodyPartFormatterFactory: a null bodypart formatter sneaked in for \""
181 << type << "/" << subtype << "\"!";
184 return (*subtype_it).second;
187 const Interface::BodyPartFormatter * BodyPartFormatterFactory::createFor( const QString & type, const QString & subtype ) const {
188 return createFor( type.toLatin1(), subtype.toLatin1() );
191 const Interface::BodyPartFormatter * BodyPartFormatterFactory::createFor( const QByteArray & type, const QByteArray & subtype ) const {
192 return createFor( type.data(), subtype.data() );