1 | /* |
2 | kmime_header_factory.cpp |
3 | |
4 | KMime, the KDE Internet mail/usenet news message library. |
5 | Copyright (c) 2009 Constantin Berzan <exit3219@gmail.com> |
6 | |
7 | This library is free software; you can redistribute it and/or |
8 | modify it under the terms of the GNU Library General Public |
9 | License as published by the Free Software Foundation; either |
10 | version 2 of the License, or (at your option) any later version. |
11 | |
12 | This library is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | Library General Public License for more details. |
16 | |
17 | You should have received a copy of the GNU Library General Public License |
18 | along with this library; see the file COPYING.LIB. If not, write to |
19 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
20 | Boston, MA 02110-1301, USA. |
21 | */ |
22 | |
23 | /** |
24 | @file |
25 | This file is part of the API for handling MIME data and |
26 | defines the HeaderFactory class. |
27 | |
28 | @brief |
29 | Defines the HeaderFactory class. |
30 | |
31 | @authors Constantin Berzan \<exit3219@gmail.com\> |
32 | */ |
33 | |
34 | #include "kmime_headerfactory_p.h" |
35 | #include "kmime_headers.h" |
36 | |
37 | #include <QHash> |
38 | |
39 | #include <KDebug> |
40 | #include <KGlobal> |
41 | |
42 | using namespace KMime; |
43 | |
44 | /** |
45 | * @internal |
46 | * Private class that helps to provide binary compatibility between releases. |
47 | */ |
48 | class KMime:: |
49 | { |
50 | public: |
51 | HeaderFactoryPrivate(); |
52 | ~HeaderFactoryPrivate(); |
53 | |
54 | HeaderFactory *const ; |
55 | QHash<QByteArray, HeaderMakerBase*> ; // Type->obj mapping; with lower-case type. |
56 | }; |
57 | |
58 | K_GLOBAL_STATIC( HeaderFactoryPrivate, sInstance ) |
59 | |
60 | HeaderFactoryPrivate::() |
61 | : instance( new HeaderFactory( this ) ) |
62 | { |
63 | } |
64 | |
65 | HeaderFactoryPrivate::() |
66 | { |
67 | qDeleteAll( headerMakers ); |
68 | delete instance; |
69 | } |
70 | |
71 | |
72 | |
73 | HeaderFactory* HeaderFactory::() |
74 | { |
75 | return sInstance->instance; |
76 | } |
77 | |
78 | Headers::Base *HeaderFactory::( const QByteArray &type ) |
79 | { |
80 | Q_ASSERT( !type.isEmpty() ); |
81 | const HeaderMakerBase *maker = d->headerMakers.value( type.toLower() ); |
82 | if ( maker ) { |
83 | return maker->create(); |
84 | } else { |
85 | //kError() << "Unknown header type" << type; |
86 | //return new Headers::Generic; |
87 | return 0; |
88 | } |
89 | } |
90 | |
91 | HeaderFactory::( HeaderFactoryPrivate *dd ) |
92 | : d( dd ) |
93 | { |
94 | } |
95 | |
96 | HeaderFactory::() |
97 | { |
98 | } |
99 | |
100 | bool HeaderFactory::( const QByteArray &type, HeaderMakerBase *maker ) |
101 | { |
102 | if ( type.isEmpty() ) { |
103 | // This is probably a generic (but not abstract) header, |
104 | // like Address or MailboxList. We cannot register those. |
105 | kWarning() << "Tried to register header with empty type." ; |
106 | return false; |
107 | } |
108 | const QByteArray ltype = type.toLower(); |
109 | if ( d->headerMakers.contains( ltype ) ) { |
110 | kWarning() << "Header of type" << type << "already registered." ; |
111 | // TODO should we make this an error? |
112 | return false; |
113 | } |
114 | d->headerMakers.insert( ltype, maker ); |
115 | return true; |
116 | } |
117 | |