Wexus2 0.20
wexus2.src/wexus/MimeTypes.h
00001 
00002 /*
00003  * Copyright (c) 2011 Aleksander B. Demko
00004  * This source code is distributed under the MIT license.
00005  * See the accompanying file LICENSE.MIT.txt for details.
00006  */
00007 
00008 #ifndef __INCLUDED_WEXUS_MIMETYPES_H__
00009 #define __INCLUDED_WEXUS_MIMETYPES_H__
00010 
00011 #include <map>
00012 
00013 #include <wexus/TR1.h>
00014 #include <wexus/HTTPHandler.h>
00015 
00016 namespace wexus
00017 {
00018   class MimeTypes;
00019 }
00020 
00021 /**
00022  * A databaset, well map, of mime types and their file extensions.
00023  *
00024  * @author Aleksander Demko
00025  */ 
00026 class wexus::MimeTypes
00027 {
00028   public:
00029     class MimeTypesException : public wexus::HTTPHandler::Exception   // TODO should this really decend from HTTPHandler::Exception?
00030     {
00031       public:
00032         MimeTypesException(const QString &usermsg);
00033     };
00034 
00035   public:
00036     /**
00037      * Constructor.
00038      *
00039      * @author Aleksander Demko
00040      */ 
00041     MimeTypes(void);
00042     /// destructor
00043     ~MimeTypes();
00044 
00045     /**
00046      * Returns the singleton instance. May return null
00047      * if there is none.
00048      *
00049      * @author Aleksander Demko
00050      */ 
00051     static MimeTypes *instance(void);
00052 
00053     /**
00054      * Is there a mime type of this extension?
00055      * ext must already be all lower case and shouldn't include
00056      * any periods.
00057      *
00058      * @author Aleksander Demko
00059      */
00060     static bool containsMimeType(const QString &ext);
00061 
00062     /**
00063      * Returns the mime type for the given extension.
00064      * Throws an exception if it doesn't exist.
00065      * ext must already be all lower case and shouldn't include
00066      * any periods.
00067      *
00068      * @author Aleksander Demko
00069      */ 
00070     static const QString & mimeType(const QString &ext);
00071 
00072     static QString binaryMimeType(void) { return "application/octet-stream"; }
00073 
00074   private:
00075     void addMimeType(const QString &type, const QString &ext);
00076     void addMimeType(const QString &type, const QStringList &extlist);
00077 
00078     void initData(void);
00079 
00080   private:
00081     typedef std::shared_ptr<QString> data_t;
00082     typedef std::map<QString, data_t> map_t;
00083 
00084     static MimeTypes *dm_instance;
00085     
00086     // the extension to mime type map
00087     map_t dm_ext;
00088 };
00089 
00090 #endif
00091 
 All Classes Namespaces Functions Variables Enumerations Enumerator