Back to index

lightning-sunbird  0.9+nobinonly
nsMimeMapper.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 //
00039 // Mike Pinkerton
00040 // Netscape Communications
00041 //
00042 // Contains the interface for |nsMimeMapper|, which is used to map between
00043 // mozilla defined mime types (such as text/html or mozilla/toolbaritem) and
00044 // MacOS flavors which are 4-character codes. Since's it's not easy to go
00045 // from N characters down to 4 and back to N without loss, we need a different
00046 // mechanism than just some hash algorithm.
00047 //
00048 // The way this is generally used is to instantiate a mime mapper when you're
00049 // about to hand data to the OS and then ask it to map the mime type into a
00050 // 4-character code. If it's not one that the mapper knows about intrinsically,
00051 // it creates a unique type and remembers the mapping internally. Do this for each
00052 // different flavor of data you have. Then, when you're ready to hand the
00053 // data to the OS for real, ask the mapper to export its mapping list (with
00054 // ExportMapping()) which can be put into a special, well known, data flavor 
00055 // that goes into the OS along with the data.
00056 //
00057 // When pulling data out of the OS and into mozilla, the mapper is used to
00058 // convert from the 4-character codes the OS knows about back to the original mime
00059 // types. At this stage, depending the originator of the data (mozilla or some other
00060 // mac app), you may or may not have a mapping flavor which explicitly lays out the 
00061 // conversions. If you do, load it (you have to do this yourself) and pass it to the
00062 // ctor. If you don't, that's ok too and the well-known mappings can still be performed.
00063 //
00064 
00065 #ifndef nsMimeMapper_h__
00066 #define nsMimeMapper_h__
00067 
00068 #include <utility>
00069 #include <vector>
00070 #include <Types.h>
00071 #include "nsString.h"
00072 
00073 using std::pair;
00074 
00075 class nsMimeMapperMac 
00076 {
00077 public:
00078   enum { kMappingFlavor = 'MOZm' } ;
00079   
00080   nsMimeMapperMac ( const char* inMappings = nsnull ) ;
00081   ~nsMimeMapperMac ( ) ;
00082    
00083     // Converts from mime type (eg: text/plain) to MacOS type (eg: 'TEXT'). If
00084     // the mapping failed, the resulting restype will be null.
00085   ResType MapMimeTypeToMacOSType ( const char* aMimeStr, PRBool inAddIfNotPresent = PR_TRUE ) ;
00086   void MapMacOSTypeToMimeType ( ResType inMacType, nsCAutoString & outMimeStr ) ;
00087  
00088     // Takes the internal mappings and converts them to a string for
00089     // placing on the clipboard or in a drag item. |outLength| includes 
00090     // the NULL at the end of the string.
00091   char* ExportMapping ( short * outLength ) const;
00092 
00093   static ResType MappingFlavor ( ) { return kMappingFlavor; }
00094   
00095 private:
00096 
00097   void ParseMappings ( const char* inMappings ) ;
00098   
00099   typedef pair<ResType, nsCAutoString>    MimePair;
00100   typedef std::vector<MimePair>           MimeMap;
00101   typedef MimeMap::iterator               MimeMapIterator;
00102   typedef MimeMap::const_iterator  MimeMapConstIterator;
00103   
00104     // the keeper of the data
00105   MimeMap mMappings;
00106     // an increasing counter for generating unique flavors
00107   short mCounter;
00108 
00109    // don't want people copying this until copy ctor does the right thing
00110   nsMimeMapperMac ( const nsMimeMapperMac & ) ;
00111  
00112 }; // nsMimeMapperMac
00113 
00114 
00115 #endif