bug 223990. Should have a getPrimaryExtensionForType function

r=bzbarsky sr=darin
also changes nsIMIMEService functions to start with a lowercase letter (only affects JS Callers)
This commit is contained in:
cbiesinger%web.de 2003-11-13 23:00:55 +00:00
Родитель ba8abce8d4
Коммит 26c72f348f
7 изменённых файлов: 67 добавлений и 56 удалений

Просмотреть файл

@ -766,7 +766,7 @@ function GetExtensionBasedOnMimeType(aMIMEType)
mimeService = Components.classes["@mozilla.org/mime;1"].getService(); mimeService = Components.classes["@mozilla.org/mime;1"].getService();
mimeService = mimeService.QueryInterface(Components.interfaces.nsIMIMEService); mimeService = mimeService.QueryInterface(Components.interfaces.nsIMIMEService);
var mimeInfo = mimeService.GetFromTypeAndExtension(aMIMEType, null); var mimeInfo = mimeService.getFromTypeAndExtension(aMIMEType, null);
if (!mimeInfo) return ""; if (!mimeInfo) return "";
var fileExtension = mimeInfo.primaryExtension; var fileExtension = mimeInfo.primaryExtension;

Просмотреть файл

@ -1287,16 +1287,13 @@ nsWebBrowserPersist::GetExtensionForContentType(const PRUnichar *aContentType, P
nsCOMPtr<nsIMIMEInfo> mimeInfo; nsCOMPtr<nsIMIMEInfo> mimeInfo;
nsCAutoString contentType; nsCAutoString contentType;
contentType.AssignWithConversion(aContentType); contentType.AssignWithConversion(aContentType);
mMIMEService->GetFromTypeAndExtension(contentType.get(), nsnull, getter_AddRefs(mimeInfo)); nsXPIDLCString ext;
if (mimeInfo) rv = mMIMEService->GetPrimaryExtension(contentType.get(), nsnull, getter_Copies(ext));
if (NS_SUCCEEDED(rv))
{ {
nsXPIDLCString ext; *aExt = ToNewUnicode(ext);
if (NS_SUCCEEDED(mimeInfo->GetPrimaryExtension(getter_Copies(ext)))) NS_ENSURE_TRUE(*aExt, NS_ERROR_OUT_OF_MEMORY);
{ return NS_OK;
*aExt = ToNewUnicode(ext);
NS_ENSURE_TRUE(*aExt, NS_ERROR_OUT_OF_MEMORY);
return NS_OK;
}
} }
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;

Просмотреть файл

@ -1892,19 +1892,15 @@ mime_decompose_file_init_fn ( void *stream_closure, MimeHeaders *headers )
nsCOMPtr<nsIMIMEService> mimeFinder (do_GetService(NS_MIMESERVICE_CONTRACTID, &rv)); nsCOMPtr<nsIMIMEService> mimeFinder (do_GetService(NS_MIMESERVICE_CONTRACTID, &rv));
if (NS_SUCCEEDED(rv) && mimeFinder) if (NS_SUCCEEDED(rv) && mimeFinder)
{ {
nsCOMPtr<nsIMIMEInfo> mimeInfo = nsnull; nsXPIDLCString fileExtension;
rv = mimeFinder->GetFromTypeAndExtension(contentType.get(), nsnull, getter_AddRefs(mimeInfo)); rv = mimeFinder->GetPrimaryExtension(contentType.get(), nsnull, getter_Copies(fileExtension));
if (NS_SUCCEEDED(rv) && mimeInfo)
{
nsXPIDLCString fileExtension;
if ( (NS_SUCCEEDED(mimeInfo->GetPrimaryExtension(getter_Copies(fileExtension)))) && fileExtension) if (NS_SUCCEEDED(rv) && !fileExtension.IsEmpty())
{ {
newAttachName.Append("."); newAttachName.Append(".");
newAttachName.Append(fileExtension); newAttachName.Append(fileExtension);
extensionAdded = PR_TRUE; extensionAdded = PR_TRUE;
} }
}
} }
if (!extensionAdded) if (!extensionAdded)

Просмотреть файл

@ -279,21 +279,16 @@ ValidateRealName(nsMsgAttachmentData *aAttach, MimeHeaders *aHdrs)
contentType.Truncate(pos); contentType.Truncate(pos);
nsCOMPtr<nsIMIMEService> mimeFinder (do_GetService(NS_MIMESERVICE_CONTRACTID, &rv)); nsCOMPtr<nsIMIMEService> mimeFinder (do_GetService(NS_MIMESERVICE_CONTRACTID, &rv));
if (NS_SUCCEEDED(rv) && mimeFinder) if (NS_SUCCEEDED(rv))
{ {
nsIMIMEInfo *mimeInfo = nsnull; nsXPIDLCString fileExtension;
rv = mimeFinder->GetFromTypeAndExtension(contentType.get(), nsnull, &mimeInfo); rv = mimeFinder->GetPrimaryExtension(contentType.get(), nsnull, getter_Copies(fileExtension));
if (NS_SUCCEEDED(rv) && mimeInfo)
{
char *aFileExtension = nsnull;
if ( (NS_SUCCEEDED(mimeInfo->GetPrimaryExtension(&aFileExtension))) && aFileExtension) if (NS_SUCCEEDED(rv) && !fileExtension.IsEmpty())
{ {
newAttachName.Append(NS_LITERAL_STRING(".")); newAttachName.Append(PRUnichar('.'));
newAttachName.AppendWithConversion(aFileExtension); newAttachName.AppendWithConversion(fileExtension);
PR_FREEIF(aFileExtension); }
}
}
} }
aAttach->real_name = ToNewCString(newAttachName); aAttach->real_name = ToNewCString(newAttachName);

Просмотреть файл

@ -35,17 +35,6 @@
* *
* ***** END LICENSE BLOCK ***** */ * ***** END LICENSE BLOCK ***** */
/* The MIME service is responsible for mapping file extensions to MIME-types
* (see RFC 2045). It also provides access to nsIMIMEInfo interfaces and
* acts as a general convenience wrapper of nsIMIMEInfo interfaces.
*
* The MIME service maintains a database with a <b>one</b> MIME type <b>to many</b>
* file extensions rule. Adding the same file extension to multiple MIME types
* is illegal and behavior is undefined.
*
* @see nsIMIMEInfo
*/
#include "nsISupports.idl" #include "nsISupports.idl"
#include "nsIMIMEInfo.idl" #include "nsIMIMEInfo.idl"
#include "nsIURI.idl" #include "nsIURI.idl"
@ -61,25 +50,39 @@
} }
%} %}
/**
* The MIME service is responsible for mapping file extensions to MIME-types
* (see RFC 2045). It also provides access to nsIMIMEInfo interfaces and
* acts as a general convenience wrapper of nsIMIMEInfo interfaces.
*
* The MIME service maintains a database with a <b>one</b> MIME type <b>to many</b>
* file extensions rule. Adding the same file extension to multiple MIME types
* is illegal and behavior is undefined.
*
* @see nsIMIMEInfo
*/
[scriptable, uuid(6C424C90-2FE7-11d3-A164-0050041CAF44)] [scriptable, uuid(6C424C90-2FE7-11d3-A164-0050041CAF44)]
interface nsIMIMEService : nsISupports { interface nsIMIMEService : nsISupports {
/* Retrieves an nsIMIMEInfo using both the extension /**
* Retrieves an nsIMIMEInfo using both the extension
* and the type of a file. The type is given preference * and the type of a file. The type is given preference
* during the lookup. One of aMIMEType and aFileExt * during the lookup. One of aMIMEType and aFileExt
* can be null. At least one of aMIMEType and aFileExt * can be null. At least one of aMIMEType and aFileExt
* must be non-null and nonempty. * must be non-null and nonempty.
*/ */
nsIMIMEInfo GetFromTypeAndExtension(in string aMIMEType, in string aFileExt); nsIMIMEInfo getFromTypeAndExtension(in string aMIMEType, in string aFileExt);
/* Retrieves a string representation of the MIME type /**
* Retrieves a string representation of the MIME type
* associated with this file extension. * associated with this file extension.
* *
* @param A file extension (excluding the dot ('.')). * @param A file extension (excluding the dot ('.')).
* @return The MIME type, if any. * @return The MIME type, if any.
*/ */
string GetTypeFromExtension(in string aFileExt); string getTypeFromExtension(in string aFileExt);
/* Retrieves a string representation of the MIME type /**
* Retrieves a string representation of the MIME type
* associated with this URI. The association is purely * associated with this URI. The association is purely
* file extension to MIME type based. No attempt to determine * file extension to MIME type based. No attempt to determine
* the type via server headers or byte scanning is made. * the type via server headers or byte scanning is made.
@ -87,9 +90,17 @@ interface nsIMIMEService : nsISupports {
* @param The URI the user wants MIME info on. * @param The URI the user wants MIME info on.
* @return The MIME type, if any. * @return The MIME type, if any.
*/ */
string GetTypeFromURI(in nsIURI aURI); string getTypeFromURI(in nsIURI aURI);
// //
string GetTypeFromFile ( in nsIFile aFile ); string getTypeFromFile(in nsIFile aFile);
/**
* Given a Type/Extension combination, returns the default extension
* for this type. This may be identical to the passed-in extension.
*
* @param aMIMEType The Type to get information on. Must not be null.
* @param aFileExt File Extension. Can be null.
*/
string getPrimaryExtension(in string aMIMEType, in string aFileExt);
}; };

Просмотреть файл

@ -2325,6 +2325,18 @@ NS_IMETHODIMP nsExternalHelperAppService::GetTypeFromExtension(const char *aFile
return info->GetMIMEType(aContentType); return info->GetMIMEType(aContentType);
} }
NS_IMETHODIMP nsExternalHelperAppService::GetPrimaryExtension(const char* aMIMEType, const char* aFileExt, char** _retval)
{
NS_ENSURE_ARG_POINTER(aMIMEType);
nsCOMPtr<nsIMIMEInfo> mi;
nsresult rv = GetFromTypeAndExtension(aMIMEType, aFileExt, getter_AddRefs(mi));
if (NS_FAILED(rv))
return rv;
return mi->GetPrimaryExtension(_retval);
}
NS_IMETHODIMP nsExternalHelperAppService::GetTypeFromURI(nsIURI *aURI, char **aContentType) NS_IMETHODIMP nsExternalHelperAppService::GetTypeFromURI(nsIURI *aURI, char **aContentType)
{ {
NS_PRECONDITION(aContentType, "Null out param!"); NS_PRECONDITION(aContentType, "Null out param!");

Просмотреть файл

@ -737,7 +737,7 @@ function getMIMEService()
function getMIMETypeForURI(aURI) function getMIMETypeForURI(aURI)
{ {
try { try {
return getMIMEService().GetTypeFromURI(aURI); return getMIMEService().getTypeFromURI(aURI);
} }
catch (e) { catch (e) {
} }
@ -747,7 +747,7 @@ function getMIMETypeForURI(aURI)
function getMIMEInfoForType(aMIMEType) function getMIMEInfoForType(aMIMEType)
{ {
try { try {
return getMIMEService().GetFromTypeAndExtension(aMIMEType, null); return getMIMEService().getFromTypeAndExtension(aMIMEType, null);
} }
catch (e) { catch (e) {
} }