kmime_headerfactory.cpp

Go to the documentation of this file.
00001 /*
00002     kmime_header_factory.cpp
00003 
00004     KMime, the KDE Internet mail/usenet news message library.
00005     Copyright (c) 2009 Constantin Berzan <exit3219@gmail.com>
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Library General Public
00009     License as published by the Free Software Foundation; either
00010     version 2 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Library General Public License for more details.
00016 
00017     You should have received a copy of the GNU Library General Public License
00018     along with this library; see the file COPYING.LIB.  If not, write to
00019     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00020     Boston, MA 02110-1301, USA.
00021 */
00022 
00034 #include "kmime_headerfactory_p.h"
00035 #include "kmime_headers.h"
00036 
00037 #include <QHash>
00038 
00039 #include <KDebug>
00040 #include <KGlobal>
00041 
00042 using namespace KMime;
00043 
00048 class KMime::HeaderFactoryPrivate
00049 {
00050   public:
00051     HeaderFactoryPrivate();
00052     ~HeaderFactoryPrivate();
00053 
00054     HeaderFactory *const instance;
00055     QHash<QByteArray, HeaderMakerBase*> headerMakers; // Type->obj mapping; with lower-case type.
00056 };
00057 
00058 K_GLOBAL_STATIC( HeaderFactoryPrivate, sInstance )
00059 
00060 HeaderFactoryPrivate::HeaderFactoryPrivate()
00061   : instance( new HeaderFactory( this ) )
00062 {
00063 }
00064 
00065 HeaderFactoryPrivate::~HeaderFactoryPrivate()
00066 {
00067   qDeleteAll( headerMakers.values() );
00068   delete instance;
00069 }
00070 
00071 
00072 
00073 HeaderFactory* HeaderFactory::self()
00074 {
00075   return sInstance->instance;
00076 }
00077 
00078 Headers::Base *HeaderFactory::createHeader( const QByteArray &type )
00079 {
00080   Q_ASSERT( !type.isEmpty() );
00081   const HeaderMakerBase *maker = d->headerMakers.value( type.toLower() );
00082   if( maker ) {
00083     return maker->create();
00084   } else {
00085     //kError() << "Unknown header type" << type;
00086     //return new Headers::Generic;
00087     return 0;
00088   }
00089 }
00090 
00091 HeaderFactory::HeaderFactory( HeaderFactoryPrivate *dd )
00092   : d( dd )
00093 {
00094 }
00095 
00096 HeaderFactory::~HeaderFactory()
00097 {
00098 }
00099 
00100 bool HeaderFactory::registerHeaderMaker( const QByteArray &type, HeaderMakerBase *maker )
00101 {
00102   if( type.isEmpty() ) {
00103     // This is probably a generic (but not abstract) header,
00104     // like Address or MailboxList.  We cannot register those.
00105     kWarning() << "Tried to register header with empty type.";
00106     return false;
00107   }
00108   const QByteArray ltype = type.toLower();
00109   if( d->headerMakers.contains( ltype ) ) {
00110     kWarning() << "Header of type" << type << "already registered.";
00111     // TODO should we make this an error?
00112     return false;
00113   }
00114   d->headerMakers.insert( ltype, maker );
00115   return true;
00116 }
00117 
KDE Home | KDE Accessibility Home | Description of Access Keys