Back to index

lightning-sunbird  0.9+nobinonly
Public Types | Public Member Functions | Static Public Member Functions | Private Types | Private Member Functions | Private Attributes
nsMimeMapperMac Class Reference

#include <nsMimeMapper.h>

Collaboration diagram for nsMimeMapperMac:
Collaboration graph
[legend]

List of all members.

Public Types

enum  { kMappingFlavor = 'MOZm' }

Public Member Functions

 nsMimeMapperMac (const char *inMappings=nsnull)
 ~nsMimeMapperMac ()
ResType MapMimeTypeToMacOSType (const char *aMimeStr, PRBool inAddIfNotPresent=PR_TRUE)
void MapMacOSTypeToMimeType (ResType inMacType, nsCAutoString &outMimeStr)
char * ExportMapping (short *outLength) const

Static Public Member Functions

static ResType MappingFlavor ()

Private Types

typedef pair< ResType,
nsCAutoString
MimePair
typedef std::vector< MimePairMimeMap
typedef MimeMap::iterator MimeMapIterator
typedef MimeMap::const_iterator MimeMapConstIterator

Private Member Functions

void ParseMappings (const char *inMappings)
 nsMimeMapperMac (const nsMimeMapperMac &)

Private Attributes

MimeMap mMappings
short mCounter

Detailed Description

Definition at line 75 of file nsMimeMapper.h.


Member Typedef Documentation

Definition at line 100 of file nsMimeMapper.h.

typedef MimeMap::const_iterator nsMimeMapperMac::MimeMapConstIterator [private]

Definition at line 102 of file nsMimeMapper.h.

typedef MimeMap::iterator nsMimeMapperMac::MimeMapIterator [private]

Definition at line 101 of file nsMimeMapper.h.

typedef pair<ResType, nsCAutoString> nsMimeMapperMac::MimePair [private]

Definition at line 99 of file nsMimeMapper.h.


Member Enumeration Documentation

anonymous enum
Enumerator:
kMappingFlavor 

Definition at line 78 of file nsMimeMapper.h.

{ kMappingFlavor = 'MOZm' } ;

Constructor & Destructor Documentation

nsMimeMapperMac::nsMimeMapperMac ( const char *  inMappings = nsnull)

Definition at line 60 of file nsMimeMapper.cpp.

  : mCounter(0)
{
  if (inMappings && strlen(inMappings) )
    ParseMappings ( inMappings );
}

Here is the call graph for this function:

Definition at line 68 of file nsMimeMapper.cpp.

{

}   

Member Function Documentation

char * nsMimeMapperMac::ExportMapping ( short *  outLength) const

Definition at line 241 of file nsMimeMapper.cpp.

{
  NS_WARN_IF_FALSE ( outLength, "No out param provided" );
  if ( outLength )
    *outLength = 0;

#if 0
// I'm leaving this code in here just to prove a point. If we were allowed to
// use string stream's (we're not, because of bloat), this is all the code I'd have
// to write to do all the crap down below.
  ostringstream ostr;

  // for each pair, write out flavor and mime types
  for ( MimeMapConstIterator it = mMappings.begin(); it != mMappings.end(); ++it ) {
    const char* mimeType = ToNewCString(it->second);
       ostr << it->first << ' ' << mimeType << ' ';
       delete [] mimeType;
  }
  
  return ostr.str().c_str();
#endif

  char* exportBuffer = nsnull;
  
  // figure out about how long the composed string will be
  short len = 0;
  for ( MimeMapConstIterator it = mMappings.begin(); it != mMappings.end(); ++it ) {
    len += 10;  // <4 char code> (any decimal representation of 'MZXX' will be 10 digits)
    len += 2;   // for the two spaces
    len += it->second.Length();  // <mime type>
  }  

  // create a string of that length and fill it in with each mapping. We have to
  // consider the possibility that there aren't any generic (internal mozilla) flavors
  // so the map could be empty.
  exportBuffer = NS_STATIC_CAST(char*, nsMemory::Alloc(len + 1));      // don't forget the NULL
  if ( !exportBuffer )
    return nsnull;
  *exportBuffer = '\0';                          // null terminate at the start for strcat()
  if ( len ) {
    char* posInString = exportBuffer;
    for ( MimeMapConstIterator it = mMappings.begin(); it != mMappings.end(); ++it ) {
      // create a buffer for this mapping, fill it in, and append it to our
      // ongoing result buffer, |exportBuffer|. 
      char* currMapping = new char[10 + 2 + it->second.Length() + 1];  // same computation as above, plus NULL
      char* mimeType = ToNewCString(it->second);
      if ( currMapping && mimeType ) {
        sprintf(currMapping, "%ld %s ", it->first, mimeType);
        strcat(posInString, currMapping);
        posInString += strlen(currMapping);     // advance marker to get ready for next mapping
      }
      nsMemory::Free ( mimeType );
      nsCRT::free ( currMapping );
    }
      
    *posInString = '\0';                        // null terminate our resulting string
  } // if there is anything in our list
  
  if ( outLength )
    *outLength = len + 1;  // don't forget the NULL
  return exportBuffer;
  
} // ExportMapping

Here is the call graph for this function:

Here is the caller graph for this function:

void nsMimeMapperMac::MapMacOSTypeToMimeType ( ResType  inMacType,
nsCAutoString outMimeStr 
)

Definition at line 155 of file nsMimeMapper.cpp.

{
  switch ( inMacType ) {
  
    case kScrapFlavorTypeText:      outMimeStr = kTextMime;           break;
    case kScrapFlavorTypeUnicode:   outMimeStr = kUnicodeMime;        break;
    case kDragFlavorTypeHFS:        outMimeStr = kFileMime;           break;
    case kDragFlavorTypePromiseHFS: outMimeStr = kFilePromiseMime;    break;
    case kDragPromisedFlavor:       outMimeStr = kFilePromiseMime;    break;
    case kScrapFlavorTypeURL:       outMimeStr = kURLDataMime;        break;
    case kScrapFlavorTypeURLDesc:   outMimeStr = kURLDescriptionMime; break;
    
    // if someone gives us PICT (or we could have put it there), use
    // the native image mime type.
    case kScrapFlavorTypePicture: outMimeStr = kNativeImageMime; break;
    
    // This flavor is the old 4.x Composer flavor for HTML. The actual data is a binary
    // data structure which we do NOT want to deal with in any way shape or form. I am
    // only including this flavor here so we don't accidentally use it ourselves and
    // get very very confused. 
    case 'EHTM':
      // Fall through to the unknown case.
  
    default:

      outMimeStr = "unknown";

      // if the flavor starts with 'MZ' then it's probably one of our encoded ones. If not,
      // we have no idea what it is. The way this was encoded means that we can use the
      // lower two byts of the flavor as an index into our mapping list.
      if ( inMacType & ('..MZ' << 16) ) {
        unsigned short index = inMacType & 0x0000FFFF;    // extract the index into our internal list
        if ( index < mMappings.size() )
          outMimeStr = mMappings[index].second;
        else
          NS_WARNING("Found a flavor starting with 'MZ..' that isn't one of ours!");
      }
        
  } // case of which flavor

} // MapMacOSTypeToMimeType

Here is the caller graph for this function:

ResType nsMimeMapperMac::MapMimeTypeToMacOSType ( const char *  aMimeStr,
PRBool  inAddIfNotPresent = PR_TRUE 
)

Definition at line 85 of file nsMimeMapper.cpp.

{
  ResType format = 0;

  // first check if it is already in our list.
  for ( MimeMapConstIterator it = mMappings.begin(); it != mMappings.end(); ++it ) {
    if ( it->second.Equals(aMimeStr) ) {
      format = it->first;
      break;
    }
  }

  // if it's not there, do a mapping and put it there. Some things we want to map
  // to specific MacOS types, like 'TEXT'. Others are internal and we don't really
  // care about what they look like, just that they have a fairly unique flavor type.
  //
  // Don't put standard MacOS flavor mappings into the mapping list because we'll
  // pick them up by special casing MapMacsOSTypeToMimeType(). This means that
  // the low two bytes of the generated flavor can be used as an index into the list.
  if ( !format ) {
    if ( PL_strcmp(aMimeStr, kUnicodeMime) == 0 )
      format = kScrapFlavorTypeUnicode;
    else if ( PL_strcmp(aMimeStr, kTextMime) == 0 )
      format = kScrapFlavorTypeText;
    else if ( PL_strcmp(aMimeStr, kFileMime) == 0 )
      format = kDragFlavorTypeHFS;
    else if ( PL_strcmp(aMimeStr, kFilePromiseMime) == 0 )
      format = kDragFlavorTypePromiseHFS;
    else if ( PL_strcmp(aMimeStr, kNativeImageMime) == 0 )
      format = kScrapFlavorTypePicture;
    else if ( PL_strcmp(aMimeStr, kURLDataMime) == 0 )
      format = kScrapFlavorTypeURL;
    else if ( PL_strcmp(aMimeStr, kURLDescriptionMime) == 0 )
      format = kScrapFlavorTypeURLDesc;
#if NOT_YET
    else if ( PL_strcmp(aMimeStr, kPNGImageMime) == 0 )
      format = kScrapFlavorTypePicture;
    else if ( PL_strcmp(aMimeStr, kJPEGImageMime) == 0 )
      format = kScrapFlavorTypePicture;
    else if ( PL_strcmp(aMimeStr, kGIFImageMime) == 0 )
      format = kScrapFlavorTypePicture;
#endif 

    else if ( inAddIfNotPresent ) {
      // create the flavor based on the unique id in the lower two bytes and 'MZ' in the
      // upper two bytes.
      format = mCounter++;
      format |= ('..MZ' << 16);     
 
      // stick it in the mapping list
      mMappings.push_back ( MimePair(format, nsCAutoString(aMimeStr)) );
    }
  
  }

  if ( inAddIfNotPresent )
    NS_ASSERTION ( format, "Didn't map mimeType to a macOS type for some reason" );   
  return format;
  
} // MapMimeTypeToMacOSType

Here is the call graph for this function:

Here is the caller graph for this function:

static ResType nsMimeMapperMac::MappingFlavor ( ) [inline, static]

Definition at line 93 of file nsMimeMapper.h.

{ return kMappingFlavor; }

Here is the caller graph for this function:

void nsMimeMapperMac::ParseMappings ( const char *  inMappings) [private]

Definition at line 210 of file nsMimeMapper.cpp.

{
  if ( !inMappings )
    return;

  const char* currPosition = inMappings;
  while ( *currPosition ) {
    char mimeType[100];
    ResType flavor = nsnull;

    sscanf ( currPosition, "%ld %s ", &flavor, mimeType );
    mMappings.push_back( MimePair(flavor, nsCAutoString(mimeType)) );

    currPosition += 10 + 2 + strlen(mimeType);  // see ExportMapping() for explanation of this calculation
    
    ++mCounter;
  } // while we're not at the end yet
  
} // ParseMappings

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

short nsMimeMapperMac::mCounter [private]

Definition at line 107 of file nsMimeMapper.h.

Definition at line 105 of file nsMimeMapper.h.


The documentation for this class was generated from the following files: