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

messageviewer

  • sources
  • kde-4.14
  • 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 namespace {
57 
58 DEFINE_PLUGIN_LOADER( BodyPartFormatterPluginLoader,
59  Interface::BodyPartFormatterPlugin,
60  "create_bodypart_formatter_plugin",
61  "messageviewer/plugins/bodypartformatter/*.desktop" )
62 
63 }
64 
65 BodyPartFormatterFactory * BodyPartFormatterFactory::mSelf = 0;
66 
67 const BodyPartFormatterFactory * BodyPartFormatterFactory::instance() {
68  if ( !mSelf )
69  mSelf = new BodyPartFormatterFactory();
70  return mSelf;
71 }
72 
73 BodyPartFormatterFactory::BodyPartFormatterFactory() {
74  mSelf = this;
75 }
76 
77 BodyPartFormatterFactory::~BodyPartFormatterFactory() {
78  mSelf = 0;
79 }
80 
81 static TypeRegistry * all = 0;
82 
83 static void insertBodyPartFormatter( const char * type, const char * subtype,
84  const Interface::BodyPartFormatter * formatter ) {
85  if ( !type || !*type || !subtype || !*subtype || !formatter || !all )
86  return;
87 
88  TypeRegistry::iterator type_it = all->find( type );
89  if ( type_it == all->end() ) {
90  //kDebug() << "BodyPartFormatterFactory: instantiating new Subtype Registry for \""
91  // << type << "\"";
92  type_it = all->insert( std::make_pair( type, SubtypeRegistry() ) ).first;
93  assert( type_it != all->end() );
94  }
95 
96  SubtypeRegistry & subtype_reg = type_it->second;
97  SubtypeRegistry::iterator subtype_it = subtype_reg.find( subtype );
98  if ( subtype_it != subtype_reg.end() ) {
99  kDebug() << "BodyPartFormatterFactory: overwriting previously registered formatter for \""
100  << type << "/" << subtype << "\"";
101  subtype_reg.erase( subtype_it ); subtype_it = subtype_reg.end();
102  }
103 
104  subtype_reg.insert( std::make_pair( subtype, formatter ) );
105 }
106 
107 static void loadPlugins() {
108  const BodyPartFormatterPluginLoader * pl = BodyPartFormatterPluginLoader::instance();
109  if ( !pl ) {
110  kWarning() << "BodyPartFormatterFactory: cannot instantiate plugin loader!";
111  return;
112  }
113  const QStringList types = pl->types();
114  //kDebug() << "BodyPartFormatterFactory: found" << types.size() << "plugins.";
115  for ( QStringList::const_iterator it = types.begin() ; it != types.end() ; ++it ) {
116  const Interface::BodyPartFormatterPlugin * plugin = pl->createForName( *it );
117  if ( !plugin ) {
118  kWarning() << "BodyPartFormatterFactory: plugin" << *it << "is not valid!";
119  continue;
120  }
121  const Interface::BodyPartFormatter * bfp;
122  for ( int i = 0 ; (bfp = plugin->bodyPartFormatter( i )) ; ++i ) {
123  const char * type = plugin->type( i );
124  if ( !type || !*type ) {
125  kWarning() << "BodyPartFormatterFactory: plugin" << *it
126  << "returned empty type specification for index"
127  << i;
128  break;
129  }
130  const char * subtype = plugin->subtype( i );
131  if ( !subtype || !*subtype ) {
132  kWarning() << "BodyPartFormatterFactory: plugin" << *it
133  << "returned empty subtype specification for index"
134  << i;
135  break;
136  }
137  insertBodyPartFormatter( type, subtype, bfp );
138  }
139  const Interface::BodyPartURLHandler * handler;
140  for ( int i = 0 ; (handler = plugin->urlHandler( i )) ; ++i )
141  URLHandlerManager::instance()->registerHandler( handler );
142  }
143 }
144 
145 static void setup() {
146  if ( !all ) {
147  all = new TypeRegistry();
148  messageviewer_create_builtin_bodypart_formatters( all );
149  loadPlugins();
150  }
151 }
152 
153 const Interface::BodyPartFormatter * BodyPartFormatterFactory::createFor( const char * type, const char * subtype ) const {
154  if ( !type || !*type )
155  type = "*"; //krazy:exclude=doublequote_chars
156  if ( !subtype || !*subtype )
157  subtype = "*"; //krazy:exclude=doublequote_chars
158 
159  setup();
160  assert( all );
161 
162  if ( all->empty() )
163  return 0;
164 
165  TypeRegistry::const_iterator type_it = all->find( type );
166  if ( type_it == all->end() )
167  type_it = all->find( "*" );
168  if ( type_it == all->end() )
169  return 0;
170 
171  const SubtypeRegistry & subtype_reg = type_it->second;
172  if ( subtype_reg.empty() )
173  return 0;
174 
175  SubtypeRegistry::const_iterator subtype_it = subtype_reg.find( subtype );
176  if ( subtype_it == subtype_reg.end() )
177  subtype_it = subtype_reg.find( "*" );
178  if ( subtype_it == subtype_reg.end() )
179  return 0;
180 
181  if ( !(*subtype_it).second ) {
182  kWarning() << "BodyPartFormatterFactory: a null bodypart formatter sneaked in for \""
183  << type << "/" << subtype << "\"!";
184  }
185 
186  return (*subtype_it).second;
187 }
188 
189 const Interface::BodyPartFormatter * BodyPartFormatterFactory::createFor( const QString & type, const QString & subtype ) const {
190  return createFor( type.toLatin1(), subtype.toLatin1() );
191 }
192 
193 const Interface::BodyPartFormatter * BodyPartFormatterFactory::createFor( const QByteArray & type, const QByteArray & subtype ) const {
194  return createFor( type.data(), subtype.data() );
195 }
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:153
QByteArray
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:107
MessageViewer::Interface::BodyPartFormatterPlugin::subtype
virtual const char * subtype(int idx) const =0
MessageViewer::Interface::BodyPartFormatterPlugin::type
virtual const char * type(int idx) const =0
QList::const_iterator
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:67
MessageViewer::BodyPartFormatterFactoryPrivate::TypeRegistry
std::map< const char *, SubtypeRegistry, ltstr > TypeRegistry
Definition: bodypartformatterfactory_p.h:57
QString
QStringList
QList::end
iterator end()
all
static TypeRegistry * all
Definition: bodypartformatterfactory.cpp:81
MessageViewer::BodyPartFormatterFactory
Definition: bodypartformatterfactory.h:47
setup
static void setup()
Definition: bodypartformatterfactory.cpp:145
type
const char * type
Definition: bodypartformatter.cpp:192
QString::toLatin1
QByteArray toLatin1() const
MessageViewer::BodyPartFormatterFactoryPrivate::messageviewer_create_builtin_bodypart_formatters
void messageviewer_create_builtin_bodypart_formatters(TypeRegistry *)
Definition: bodypartformatter.cpp:134
bodypartformatter.h
bodypartformatterfactory.h
QByteArray::data
char * data()
urlhandlermanager.h
insertBodyPartFormatter
static void insertBodyPartFormatter(const char *type, const char *subtype, const Interface::BodyPartFormatter *formatter)
Definition: bodypartformatterfactory.cpp:83
QList::begin
iterator begin()
MessageViewer::BodyPartFormatterFactory::~BodyPartFormatterFactory
~BodyPartFormatterFactory()
Definition: bodypartformatterfactory.cpp:77
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:32:45 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
  • pimprint

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