• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

messageviewer

  • sources
  • kde-4.12
  • kdepim
  • messageviewer
  • viewer
bodypartformatterfactory.cpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-file-style: "gnu" -*-
2  bodypartformatterfactory.cpp
3 
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>
7 
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.
12 
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.
17 
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
21 
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.
32 */
33 
34 
35 
36 
37 #include "bodypartformatterfactory.h"
38 #include "bodypartformatterfactory_p.h"
39 
40 #include "interfaces/bodypartformatter.h"
41 #include "pluginloader.h"
42 #include "urlhandlermanager.h"
43 
44 // KDE
45 #include <kdebug.h>
46 
47 // Qt
48 #include <QString>
49 #include <QStringList>
50 
51 #include <assert.h>
52 
53 using namespace MessageViewer::BodyPartFormatterFactoryPrivate;
54 using namespace MessageViewer;
55 
56 #ifdef _WIN32_WCE
57 extern "C"
58 Interface::BodyPartFormatterPlugin *
59 messageviewer_bodypartformatter_text_calendar_create_bodypart_formatter_plugin();
60 #else
61 namespace {
62 
63  DEFINE_PLUGIN_LOADER( BodyPartFormatterPluginLoader,
64  Interface::BodyPartFormatterPlugin,
65  "create_bodypart_formatter_plugin",
66  "messageviewer/plugins/bodypartformatter/*.desktop" )
67 
68 }
69 #endif
70 
71 BodyPartFormatterFactory * BodyPartFormatterFactory::mSelf = 0;
72 
73 const BodyPartFormatterFactory * BodyPartFormatterFactory::instance() {
74  if ( !mSelf )
75  mSelf = new BodyPartFormatterFactory();
76  return mSelf;
77 }
78 
79 BodyPartFormatterFactory::BodyPartFormatterFactory() {
80  mSelf = this;
81 }
82 
83 BodyPartFormatterFactory::~BodyPartFormatterFactory() {
84  mSelf = 0;
85 }
86 
87 static TypeRegistry * all = 0;
88 
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;
93 
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() );
100  }
101 
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();
108  }
109 
110  subtype_reg.insert( std::make_pair( subtype, formatter ) );
111 }
112 
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;
119  }
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;
127  }
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;
136  }
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;
143  }
144  insertBodyPartFormatter( type, subtype, bfp );
145  }
146  const Interface::BodyPartURLHandler * handler;
147  for ( int i = 0 ; (handler = plugin->urlHandler( i )) ; ++i )
148  URLHandlerManager::instance()->registerHandler( handler );
149  }
150 #else
151 
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;
161  }
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;
168  }
169  insertBodyPartFormatter( type, subtype, bfp );
170  }
171  const Interface::BodyPartURLHandler * handler;
172  for ( int i = 0 ; (handler = plugin->urlHandler( i )) ; ++i )
173  URLHandlerManager::instance()->registerHandler( handler );
174 #endif
175 }
176 
177 static void setup() {
178  if ( !all ) {
179  all = new TypeRegistry();
180  messageviewer_create_builtin_bodypart_formatters( all );
181  loadPlugins();
182  }
183 }
184 
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
190 
191  setup();
192  assert( all );
193 
194  if ( all->empty() )
195  return 0;
196 
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;
202 
203  const SubtypeRegistry & subtype_reg = type_it->second;
204  if ( subtype_reg.empty() )
205  return 0;
206 
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;
212 
213  if ( !(*subtype_it).second ) {
214  kWarning() << "BodyPartFormatterFactory: a null bodypart formatter sneaked in for \""
215  << type << "/" << subtype << "\"!";
216  }
217 
218  return (*subtype_it).second;
219 }
220 
221 const Interface::BodyPartFormatter * BodyPartFormatterFactory::createFor( const QString & type, const QString & subtype ) const {
222  return createFor( type.toLatin1(), subtype.toLatin1() );
223 }
224 
225 const Interface::BodyPartFormatter * BodyPartFormatterFactory::createFor( const QByteArray & type, const QByteArray & subtype ) const {
226  return createFor( type.data(), subtype.data() );
227 }
MessageViewer::Interface::BodyPartFormatterPlugin
interface for BodyPartFormatter plugins
Definition: interfaces/bodypartformatter.h:94
MessageViewer::Interface::BodyPartFormatter
Definition: interfaces/bodypartformatter.h:48
MessageViewer::BodyPartFormatterFactory::createFor
const Interface::BodyPartFormatter * createFor(const char *type, const char *subtype) const
Definition: bodypartformatterfactory.cpp:185
MessageViewer::URLHandlerManager::registerHandler
void registerHandler(const URLHandler *handler)
Definition: urlhandlermanager.cpp:348
pluginloader.h
MessageViewer::BodyPartFormatterFactoryPrivate::SubtypeRegistry
std::map< const char *, const Interface::BodyPartFormatter *, ltstr > SubtypeRegistry
Definition: bodypartformatterfactory_p.h:56
loadPlugins
static void loadPlugins()
Definition: bodypartformatterfactory.cpp:113
MessageViewer::Interface::BodyPartFormatterPlugin::subtype
virtual const char * subtype(int idx) const =0
MessageViewer::Interface::BodyPartFormatterPlugin::type
virtual const char * type(int idx) const =0
MessageViewer::Interface::BodyPartFormatterPlugin::urlHandler
virtual const BodyPartURLHandler * urlHandler(int idx) const =0
MessageViewer::URLHandlerManager::instance
static URLHandlerManager * instance()
Definition: urlhandlermanager.h:66
MessageViewer::Interface::BodyPartFormatterPlugin::bodyPartFormatter
virtual const BodyPartFormatter * bodyPartFormatter(int idx) const =0
bodypartformatterfactory_p.h
DEFINE_PLUGIN_LOADER
#define DEFINE_PLUGIN_LOADER(pl, t, mf, p)
Definition: pluginloader.h:120
MessageViewer::Interface::BodyPartURLHandler
An interface to body part reader link handlers.
Definition: bodyparturlhandler.h:71
MessageViewer::BodyPartFormatterFactory::instance
static const BodyPartFormatterFactory * instance()
Definition: bodypartformatterfactory.cpp:73
MessageViewer::BodyPartFormatterFactoryPrivate::TypeRegistry
std::map< const char *, SubtypeRegistry, ltstr > TypeRegistry
Definition: bodypartformatterfactory_p.h:57
all
static TypeRegistry * all
Definition: bodypartformatterfactory.cpp:87
MessageViewer::BodyPartFormatterFactory
Definition: bodypartformatterfactory.h:47
setup
static void setup()
Definition: bodypartformatterfactory.cpp:177
type
const char * type
Definition: bodypartformatter.cpp:192
MessageViewer::BodyPartFormatterFactoryPrivate::messageviewer_create_builtin_bodypart_formatters
void messageviewer_create_builtin_bodypart_formatters(TypeRegistry *)
Definition: bodypartformatter.cpp:134
bodypartformatter.h
bodypartformatterfactory.h
urlhandlermanager.h
insertBodyPartFormatter
static void insertBodyPartFormatter(const char *type, const char *subtype, const Interface::BodyPartFormatter *formatter)
Definition: bodypartformatterfactory.cpp:89
MessageViewer::BodyPartFormatterFactory::~BodyPartFormatterFactory
~BodyPartFormatterFactory()
Definition: bodypartformatterfactory.cpp:83
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:55:57 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

messageviewer

Skip menu "messageviewer"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal