Back to index

lightning-sunbird  0.9+nobinonly
Static Public Member Functions
nsPrimitiveHelpers Class Reference

#include <nsPrimitiveHelpers.h>

List of all members.

Static Public Member Functions

static void CreatePrimitiveForData (const char *aFlavor, void *aDataBuff, PRUint32 aDataLen, nsISupports **aPrimitive)
static void CreateDataFromPrimitive (const char *aFlavor, nsISupports *aPrimitive, void **aDataBuff, PRUint32 aDataLen)
static nsresult ConvertUnicodeToPlatformPlainText (PRUnichar *inUnicode, PRInt32 inUnicodeLen, char **outPlainTextData, PRInt32 *outPlainTextLen)
static nsresult ConvertPlatformPlainTextToUnicode (const char *inText, PRInt32 inTextLen, PRUnichar **outUnicode, PRInt32 *outUnicodeLen)

Detailed Description

Definition at line 50 of file nsPrimitiveHelpers.h.


Member Function Documentation

nsresult nsPrimitiveHelpers::ConvertPlatformPlainTextToUnicode ( const char *  inText,
PRInt32  inTextLen,
PRUnichar **  outUnicode,
PRInt32 outUnicodeLen 
) [static]

Definition at line 202 of file nsPrimitiveHelpers.cpp.

{
  if ( !outUnicode || !outUnicodeLen )
    return NS_ERROR_INVALID_ARG;

  // Get the appropriate unicode decoder. We're guaranteed that this won't change
  // through the life of the app so we can cache it.
  nsresult rv;
  static nsCOMPtr<nsIUnicodeDecoder> decoder;
  static PRBool hasConverter = PR_FALSE;
  if ( !hasConverter ) {
    // get the charset
    nsCAutoString platformCharset;
    nsCOMPtr <nsIPlatformCharset> platformCharsetService = do_GetService(NS_PLATFORMCHARSET_CONTRACTID, &rv);
    if (NS_SUCCEEDED(rv))
      rv = platformCharsetService->GetCharset(kPlatformCharsetSel_PlainTextInClipboard, platformCharset);
    if (NS_FAILED(rv))
      platformCharset.AssignLiteral("ISO-8859-1");
      
    // get the decoder
    nsCOMPtr<nsICharsetConverterManager> ccm = 
             do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);  
    rv = ccm->GetUnicodeDecoderRaw(platformCharset.get(),
                                   getter_AddRefs(decoder));

    NS_ASSERTION(NS_SUCCEEDED(rv), "GetUnicodeEncoderRaw failed.");
    if (NS_FAILED(rv))
      return NS_ERROR_FAILURE;

    hasConverter = PR_TRUE;
  }
  
  // Estimate out length and allocate the buffer based on a worst-case estimate, then do
  // the conversion. 
  decoder->GetMaxLength(inText, inTextLen, outUnicodeLen);   // |outUnicodeLen| is number of chars
  if ( *outUnicodeLen ) {
    *outUnicode = NS_REINTERPRET_CAST(PRUnichar*, nsMemory::Alloc((*outUnicodeLen + 1) * sizeof(PRUnichar)));
    if ( *outUnicode ) {
      rv = decoder->Convert(inText, &inTextLen, *outUnicode, outUnicodeLen);
      (*outUnicode)[*outUnicodeLen] = '\0';                   // null terminate. Convert() doesn't do it for us
    }
  } // if valid length

  NS_ASSERTION ( NS_SUCCEEDED(rv), "Error converting plain text to unicode" );

  return rv;
} // ConvertPlatformPlainTextToUnicode

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult nsPrimitiveHelpers::ConvertUnicodeToPlatformPlainText ( PRUnichar inUnicode,
PRInt32  inUnicodeLen,
char **  outPlainTextData,
PRInt32 outPlainTextLen 
) [static]

Definition at line 153 of file nsPrimitiveHelpers.cpp.

{
  if ( !outPlainTextData || !outPlainTextLen )
    return NS_ERROR_INVALID_ARG;

  // Get the appropriate unicode encoder. We're guaranteed that this won't change
  // through the life of the app so we can cache it.
  nsresult rv;
  nsCOMPtr<nsIUnicodeEncoder> encoder;

  // get the charset
  nsCAutoString platformCharset;
  nsCOMPtr <nsIPlatformCharset> platformCharsetService = do_GetService(NS_PLATFORMCHARSET_CONTRACTID, &rv);
  if (NS_SUCCEEDED(rv))
    rv = platformCharsetService->GetCharset(kPlatformCharsetSel_PlainTextInClipboard, platformCharset);
  if (NS_FAILED(rv))
    platformCharset.AssignLiteral("ISO-8859-1");
  

  // use transliterate to convert things like smart quotes to normal quotes for plain text

  nsCOMPtr<nsISaveAsCharset> converter = do_CreateInstance("@mozilla.org/intl/saveascharset;1", &rv);
  NS_ENSURE_SUCCESS(rv, rv);

  rv = converter->Init(platformCharset.get(),
                  nsISaveAsCharset::attr_EntityAfterCharsetConv +
                  nsISaveAsCharset::attr_FallbackQuestionMark,
                  nsIEntityConverter::transliterate);
  NS_ENSURE_SUCCESS(rv, rv);

  rv = converter->Convert(inUnicode, outPlainTextData);
  *outPlainTextLen = *outPlainTextData ? strlen(*outPlainTextData) : 0;

  NS_ASSERTION ( NS_SUCCEEDED(rv), "Error converting unicode to plain text" );
  
  return rv;
} // ConvertUnicodeToPlatformPlainText

Here is the call graph for this function:

Here is the caller graph for this function:

void nsPrimitiveHelpers::CreateDataFromPrimitive ( const char *  aFlavor,
nsISupports *  aPrimitive,
void **  aDataBuff,
PRUint32  aDataLen 
) [static]

Definition at line 118 of file nsPrimitiveHelpers.cpp.

{
  if ( !aDataBuff )
    return;

  if ( strcmp(aFlavor,kTextMime) == 0 ) {
    nsCOMPtr<nsISupportsCString> plainText ( do_QueryInterface(aPrimitive) );
    if ( plainText ) {
      nsCAutoString data;
      plainText->GetData ( data );
      *aDataBuff = ToNewCString(data);
    }
  }
  else {
    nsCOMPtr<nsISupportsString> doubleByteText ( do_QueryInterface(aPrimitive) );
    if ( doubleByteText ) {
      nsAutoString data;
      doubleByteText->GetData ( data );
      *aDataBuff = ToNewUnicode(data);
    }
  }

}

Here is the call graph for this function:

Here is the caller graph for this function:

void nsPrimitiveHelpers::CreatePrimitiveForData ( const char *  aFlavor,
void aDataBuff,
PRUint32  aDataLen,
nsISupports **  aPrimitive 
) [static]

Definition at line 81 of file nsPrimitiveHelpers.cpp.

{
  if ( !aPrimitive )
    return;

  if ( strcmp(aFlavor,kTextMime) == 0 || strcmp(aFlavor,kNativeHTMLMime) == 0 ) {
    nsCOMPtr<nsISupportsCString> primitive =
        do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID);
    if ( primitive ) {
      const char * start = (const char*)aDataBuff;
      primitive->SetData(Substring(start, start + aDataLen));
      NS_ADDREF(*aPrimitive = primitive);
    }
  }
  else {
    nsCOMPtr<nsISupportsString> primitive =
        do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID);
    if (primitive ) {
      // recall that length takes length as characters, not bytes
      const PRUnichar* start = (const PRUnichar*)aDataBuff;
      primitive->SetData(Substring(start, start + (aDataLen / 2)));
      NS_ADDREF(*aPrimitive = primitive);
    }  
  }

} // CreatePrimitiveForData

Here is the call graph for this function:

Here is the caller graph for this function:


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