summaryrefslogtreecommitdiffstats
path: root/framework/src/domain/mime/mimetreeparser/bodypartformatterbasefactory.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2017-05-29 16:17:04 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2017-06-04 12:57:04 +0200
commite452707fdfbd61be1e5633b516b653b7337e7865 (patch)
tree1e1d4b48ebf8d381f292436f2ba04b8763edc5de /framework/src/domain/mime/mimetreeparser/bodypartformatterbasefactory.cpp
parent5a1033bdace740799a6e03389bee30e5a4de5d44 (diff)
downloadkube-e452707fdfbd61be1e5633b516b653b7337e7865.tar.gz
kube-e452707fdfbd61be1e5633b516b653b7337e7865.zip
Reduced the messagetreeparser to aproximately what we actually require
While in a much more managable state it's still not pretty. However, further refactoring can now gradually happen as we need to do further work on it. Things that should happen eventually: * Simplify the logic that creates the messageparts (we don't need the whole formatter plugin complexity) * Get rid of the nodehelper (let the parts hold the necessary data) * Get rid of partmetadata (let the part handleit)
Diffstat (limited to 'framework/src/domain/mime/mimetreeparser/bodypartformatterbasefactory.cpp')
-rw-r--r--framework/src/domain/mime/mimetreeparser/bodypartformatterbasefactory.cpp179
1 files changed, 179 insertions, 0 deletions
diff --git a/framework/src/domain/mime/mimetreeparser/bodypartformatterbasefactory.cpp b/framework/src/domain/mime/mimetreeparser/bodypartformatterbasefactory.cpp
new file mode 100644
index 00000000..fb02945b
--- /dev/null
+++ b/framework/src/domain/mime/mimetreeparser/bodypartformatterbasefactory.cpp
@@ -0,0 +1,179 @@
1/*
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#include "bodypartformatterbasefactory.h"
35#include "bodypartformatterbasefactory_p.h"
36#include "mimetreeparser_debug.h"
37
38// Qt
39
40#include <assert.h>
41
42using namespace MimeTreeParser;
43
44BodyPartFormatterBaseFactoryPrivate::BodyPartFormatterBaseFactoryPrivate(BodyPartFormatterBaseFactory *factory)
45 : q(factory)
46 , all(nullptr)
47{
48}
49
50BodyPartFormatterBaseFactoryPrivate::~BodyPartFormatterBaseFactoryPrivate()
51{
52 if (all) {
53 delete all;
54 all = nullptr;
55 }
56}
57
58void BodyPartFormatterBaseFactoryPrivate::setup()
59{
60 if (!all) {
61 all = new TypeRegistry();
62 messageviewer_create_builtin_bodypart_formatters();
63 q->loadPlugins();
64 }
65}
66
67void BodyPartFormatterBaseFactoryPrivate::insert(const char *type, const char *subtype, const Interface::BodyPartFormatter *formatter)
68{
69 if (!type || !*type || !subtype || !*subtype || !formatter || !all) {
70 return;
71 }
72
73 TypeRegistry::iterator type_it = all->find(type);
74 if (type_it == all->end()) {
75 qCDebug(MIMETREEPARSER_LOG) << "BodyPartFormatterBaseFactory: instantiating new Subtype Registry for \""
76 << type << "\"";
77 type_it = all->insert(std::make_pair(type, SubtypeRegistry())).first;
78 assert(type_it != all->end());
79 }
80
81 SubtypeRegistry &subtype_reg = type_it->second;
82
83 subtype_reg.insert(std::make_pair(subtype, formatter));
84}
85
86BodyPartFormatterBaseFactory::BodyPartFormatterBaseFactory()
87 : d(new BodyPartFormatterBaseFactoryPrivate(this))
88{
89}
90
91BodyPartFormatterBaseFactory::~BodyPartFormatterBaseFactory()
92{
93 delete d;
94}
95
96void BodyPartFormatterBaseFactory::insert(const char *type, const char *subtype, const Interface::BodyPartFormatter *formatter)
97{
98 d->insert(type, subtype, formatter);
99}
100
101const SubtypeRegistry &BodyPartFormatterBaseFactory::subtypeRegistry(const char *type) const
102{
103 if (!type || !*type) {
104 type = "*"; //krazy:exclude=doublequote_chars
105 }
106
107 d->setup();
108 assert(d->all);
109
110 static SubtypeRegistry emptyRegistry;
111 if (d->all->empty()) {
112 return emptyRegistry;
113 }
114
115 TypeRegistry::const_iterator type_it = d->all->find(type);
116 if (type_it == d->all->end()) {
117 type_it = d->all->find("*");
118 }
119 if (type_it == d->all->end()) {
120 return emptyRegistry;
121 }
122
123 const SubtypeRegistry &subtype_reg = type_it->second;
124 if (subtype_reg.empty()) {
125 return emptyRegistry;
126 }
127 return subtype_reg;
128}
129
130SubtypeRegistry::const_iterator BodyPartFormatterBaseFactory::createForIterator(const char *type, const char *subtype) const
131{
132 if (!type || !*type) {
133 type = "*"; //krazy:exclude=doublequote_chars
134 }
135 if (!subtype || !*subtype) {
136 subtype = "*"; //krazy:exclude=doublequote_chars
137 }
138
139 d->setup();
140 assert(d->all);
141
142 if (d->all->empty()) {
143 return SubtypeRegistry::const_iterator();
144 }
145
146 TypeRegistry::const_iterator type_it = d->all->find(type);
147 if (type_it == d->all->end()) {
148 type_it = d->all->find("*");
149 }
150 if (type_it == d->all->end()) {
151 return SubtypeRegistry::const_iterator();
152 }
153
154 const SubtypeRegistry &subtype_reg = type_it->second;
155 if (subtype_reg.empty()) {
156 return SubtypeRegistry::const_iterator();
157 }
158
159 SubtypeRegistry::const_iterator subtype_it = subtype_reg.find(subtype);
160 qCWarning(MIMETREEPARSER_LOG) << type << subtype << subtype_reg.size();
161 if (subtype_it == subtype_reg.end()) {
162 subtype_it = subtype_reg.find("*");
163 }
164 if (subtype_it == subtype_reg.end()) {
165 return SubtypeRegistry::const_iterator();
166 }
167
168 if (!(*subtype_it).second) {
169 qCWarning(MIMETREEPARSER_LOG) << "BodyPartFormatterBaseFactory: a null bodypart formatter sneaked in for \""
170 << type << "/" << subtype << "\"!";
171 }
172
173 return subtype_it;
174}
175
176void BodyPartFormatterBaseFactory::loadPlugins()
177{
178 qCDebug(MIMETREEPARSER_LOG) << "plugin loading is not enabled in libmimetreeparser";
179}