r=rhp. nsIMIMEInfo and nsIMIMEService have been revamped (there's one more small iteration coming tomorrow). nsIMIMEInfo has been extended and the service has been multi indexed using file extensions *and* the MIME type as keys.

This commit is contained in:
valeski%netscape.com 2000-01-24 15:41:03 +00:00
Родитель 3ed716fa69
Коммит 57c6e1294b
13 изменённых файлов: 379 добавлений и 266 удалений

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

@ -17,25 +17,71 @@
* Copyright (C) 1998 Netscape Communications Corporation. All * Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved. * Rights Reserved.
* *
* Contributor(s): * Contributor(s): Judson Valeski
*/ */
/* An nsIMIMEInfo gives a user access to mime information /* An nsIMIMEInfo gives a user access to mime information.
* there is a one-to-many relationship between MIME types
* and file extensions. This means that a MIMEInfo object
* may have multiple file extensions associated with it.
* However, the reverse is not true.
* *
*/ */
#include "nsISupports.idl" #include "nsISupports.idl"
#include "nsIURI.idl"
[scriptable, uuid(6A57EAE0-2FE8-11d3-A164-0050041CAF44)] [scriptable, uuid(6A57EAE0-2FE8-11d3-A164-0050041CAF44)]
interface nsIMIMEInfo : nsISupports { interface nsIMIMEInfo : nsISupports {
// return the file extensions associated with this MIME type set. /* Gives you an array of file types associated with this type.
attribute string FileExtensions; *
* @return Number of elements in the array.
* @return Array of extensions.
*/
void GetFileExtensions(out PRInt32 elementCount,
[array, size_is(elementCount)] out string extensions);
// return the MIME types associated with this file extension set. /* Returns whether or not the given extension is
attribute string MIMEType; * associated with this MIME info.
*
* @return TRUE if the assocaition exists.
*/
// return the description of this MIME type set. boolean ExtensionExists(in string aExtension);
attribute wstring Description;
/* Returns the first extension association in
* the internal set of extensions.
*
* @return The first extension.
*/
string FirstExtension();
/* The MIME type of this MIMEInfo.
*
* @return String representing the MIME type.
*/
readonly attribute string MIMEType;
/* A human readable description of the MIME info
*
* @return The description
*/
readonly attribute wstring Description;
/* Gives you arbitrary data about the MIMEInfo. An example
* of this is a generic image graphically representing
* this MIME info. This image can be used to generically
* and graphically represent the type.
*
* @return A URI representing the data location.
*/
readonly attribute nsIURI DataURI;
/* Returns whether or not these two MIME infos are logically
* equivelent maintaining the one-to-many relationship between
* MIME types and file extensions.
*
* @returns TRUE if the two are considered equal
*/
boolean Equals(in nsIMIMEInfo aMIMEInfo); boolean Equals(in nsIMIMEInfo aMIMEInfo);
}; };

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

@ -17,20 +17,21 @@
* Copyright (C) 1998 Netscape Communications Corporation. All * Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved. * Rights Reserved.
* *
* Contributor(s): * Contributor(s): Judson Valeski
*/ */
/* The MIME service is responsible for mapping file extensions to MIME-types /* The MIME service is responsible for mapping file extensions to MIME-types
* (see RFC 2045). * (see RFC 2045). It also provides access to nsIMIMEInfo interfaces and
* acts as a general convenience wrapper of nsIMIMEInfo interfaces.
*
* @see nsIMIMEInfo
*/ */
#include "nsISupports.idl" #include "nsISupports.idl"
#include "nsIMIMEInfo.idl" #include "nsIMIMEInfo.idl"
#include "nsIURI.idl" #include "nsIURI.idl"
%{C++ %{C++
#define NS_MIMESERVICE_CID \ #define NS_MIMESERVICE_CID \
{ /* 03af31da-3109-11d3-8cd0-0060b0fc14a3 */ \ { /* 03af31da-3109-11d3-8cd0-0060b0fc14a3 */ \
0x03af31da, \ 0x03af31da, \
@ -38,25 +39,41 @@
0x11d3, \ 0x11d3, \
{0x8c, 0xd0, 0x00, 0x60, 0xb0, 0xfc, 0x14, 0xa3} \ {0x8c, 0xd0, 0x00, 0x60, 0xb0, 0xfc, 0x14, 0xa3} \
} }
%} %}
[scriptable, uuid(6C424C90-2FE7-11d3-A164-0050041CAF44)] [scriptable, uuid(6C424C90-2FE7-11d3-A164-0050041CAF44)]
interface nsIMIMEService : nsISupports { interface nsIMIMEService : nsISupports {
// return a nsIMIMEInfo for the given file extension. /* Retrieves a registered nsIMIMEInfo interface associated
* with the given file extension.
*
* @param A file extension (excluding the dot ('.')).
* @return A nsIMIMEInfo interface.
*/
nsIMIMEInfo GetFromExtension(in string aFileExt); nsIMIMEInfo GetFromExtension(in string aFileExt);
// return a string representing the content-type /* Retrieves a registered nsIMIMEInfo interface associated
void GetTypeFromExtension(in string aFileExt, out string aContentType); * with the given MIME type.
*
void GetTypeFromURI(in nsIURI aURI, out string aContentType); * @param A MIME type.
* @return A nsIMIMEInfo interface.
// return a nsIMIMEInfo for the given MIME type. */
nsIMIMEInfo GetFromMIMEType(in string aMIMEType); nsIMIMEInfo GetFromMIMEType(in string aMIMEType);
// insert a nsIMIMEInfo in the the database. /* Retrieves a string representation of the MIME type
void AddMIMEInfo(in nsIMIMEInfo aMIMEInfo); * associated with this file extension.
*
* @param A file extension (excluding the dot ('.')).
* @return The MIME type, if any.
*/
string GetTypeFromExtension(in string aFileExt);
// remove a nsIMIMEInfo from the database. /* Retrieves a string representation of the MIME type
void RemoveMIMEInfo(in nsIMIMEInfo aMIMEInfo); * associated with this URI. The association is purely
* file extension to MIME type based. No attempt to determine
* the type via server headers or byte scanning is made.
*
* @param The URI the user wants MIME info on.
* @return The MIME type, if any.
*/
string GetTypeFromURI(in nsIURI aURI);
}; };

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

@ -35,7 +35,7 @@ REQUIRES = xpcom
CPPSRCS = \ CPPSRCS = \
nsMIMEInfoImpl.cpp \ nsMIMEInfoImpl.cpp \
nsMIMEService.cpp \ nsMIMEService.cpp \
nsMIMEServiceFactory.cpp \ nsMIMEServiceModule.cpp \
$(NULL) $(NULL)
EXTRA_DSO_LDOPTS += $(MOZ_COMPONENT_LIBS) EXTRA_DSO_LDOPTS += $(MOZ_COMPONENT_LIBS)

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

@ -36,7 +36,7 @@ LCFLAGS = -DWIN32_LEAN_AND_MEAN -D_IMPL_NS_NET
CPP_OBJS = \ CPP_OBJS = \
.\$(OBJDIR)\nsMIMEInfoImpl.obj \ .\$(OBJDIR)\nsMIMEInfoImpl.obj \
.\$(OBJDIR)\nsMIMEService.obj \ .\$(OBJDIR)\nsMIMEService.obj \
.\$(OBJDIR)\nsMIMEServiceFactory.obj \ .\$(OBJDIR)\nsMIMEServiceModule.obj \
$(NULL) $(NULL)
LOCAL_INCLUDES=-I. LOCAL_INCLUDES=-I.

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

@ -17,56 +17,72 @@
* Copyright (C) 1998 Netscape Communications Corporation. All * Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved. * Rights Reserved.
* *
* Contributor(s): * Contributor(s): Judson Valeski
*/ */
#include "nsMIMEInfoImpl.h" #include "nsMIMEInfoImpl.h"
#include "nsCRT.h" #include "nsXPIDLString.h"
// nsISupports methods // nsISupports methods
NS_IMPL_ISUPPORTS(nsMIMEInfoImpl, NS_GET_IID(nsIMIMEInfo)); NS_IMPL_ISUPPORTS(nsMIMEInfoImpl, NS_GET_IID(nsIMIMEInfo));
// nsMIMEInfoImpl methods // nsMIMEInfoImpl methods
nsMIMEInfoImpl::nsMIMEInfoImpl(const char *aMIMEType, const char *aFileExtensions, const char *aDescription) { nsMIMEInfoImpl::nsMIMEInfoImpl(const char *aMIMEType) {
NS_INIT_REFCNT(); NS_INIT_REFCNT();
mMIMEType = NS_NewAtom(aMIMEType); mMIMEType = getter_AddRefs(NS_NewAtom(aMIMEType));
mFileExtensions.SetString(aFileExtensions);
mDescription.SetString(aDescription);
} }
nsMIMEInfoImpl::~nsMIMEInfoImpl() {
NS_RELEASE(mMIMEType);
}
PRBool
nsMIMEInfoImpl::InExtensions(nsIAtom* anIAtom) {
nsAutoString extension;
anIAtom->ToString(extension);
// XXX this is broken. need to use gessner's tokenizer stuff to delimit the commas
if (mFileExtensions.Find(extension) == -1)
return PR_FALSE;
return PR_TRUE;
}
// nsIMIMEInfo methods
NS_IMETHODIMP NS_IMETHODIMP
nsMIMEInfoImpl::GetFileExtensions(char * *aFileExtensions) { nsMIMEInfoImpl::GetFileExtensions(PRInt32 *elementCount, char ***extensions) {
if (!aFileExtensions) return NS_ERROR_NULL_POINTER; PRUint32 extCount = mExtensions.Count();
if (extCount < 1) return NS_ERROR_NOT_INITIALIZED;
char **_retExts = (char**)nsAllocator::Alloc(extCount*2*sizeof(char*));
if (!_retExts) return NS_ERROR_OUT_OF_MEMORY;
for (PRUint8 i=0; i < extCount; i++) {
nsString* ext = (nsString*)mExtensions.StringAt(i);
_retExts[i] = ext->ToNewCString();
if (!_retExts[i]) return NS_ERROR_OUT_OF_MEMORY;
}
*elementCount = extCount;
*extensions = _retExts;
*aFileExtensions = mFileExtensions.ToNewCString();
if (!*aFileExtensions)
return NS_ERROR_OUT_OF_MEMORY;
return NS_OK; return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
nsMIMEInfoImpl::SetFileExtensions(const char * aFileExtensions) { nsMIMEInfoImpl::ExtensionExists(const char *aExtension, PRBool *_retval) {
mFileExtensions.SetString(aFileExtensions); NS_ASSERTION(aExtension, "no extension");
PRBool found = PR_FALSE;
PRUint32 extCount = mExtensions.Count();
if (extCount < 1) return NS_ERROR_NOT_INITIALIZED;
if (!aExtension) return NS_ERROR_NULL_POINTER;
for (PRUint8 i=0; i < extCount; i++) {
nsString* ext = (nsString*)mExtensions.StringAt(i);
if (ext->Equals(aExtension)) {
found = PR_TRUE;
break;
}
}
*_retval = found;
return NS_OK; return NS_OK;
} }
NS_IMETHODIMP
nsMIMEInfoImpl::FirstExtension(char **_retval) {
PRUint32 extCount = mExtensions.Count();
if (extCount < 1) return NS_ERROR_NOT_INITIALIZED;
*_retval = (mExtensions.StringAt(0))->ToNewCString();
if (!*_retval) return NS_ERROR_OUT_OF_MEMORY;
return NS_OK;
}
NS_IMETHODIMP NS_IMETHODIMP
nsMIMEInfoImpl::GetMIMEType(char * *aMIMEType) { nsMIMEInfoImpl::GetMIMEType(char * *aMIMEType) {
if (!aMIMEType) return NS_ERROR_NULL_POINTER; if (!aMIMEType) return NS_ERROR_NULL_POINTER;
@ -78,14 +94,6 @@ nsMIMEInfoImpl::GetMIMEType(char * *aMIMEType) {
return NS_OK; return NS_OK;
} }
NS_IMETHODIMP
nsMIMEInfoImpl::SetMIMEType(const char * aMIMEType) {
NS_RELEASE(mMIMEType);
mMIMEType = NS_NewAtom(aMIMEType);
if (!mMIMEType) return NS_ERROR_OUT_OF_MEMORY;
return NS_OK;
}
NS_IMETHODIMP NS_IMETHODIMP
nsMIMEInfoImpl::GetDescription(PRUnichar * *aDescription) { nsMIMEInfoImpl::GetDescription(PRUnichar * *aDescription) {
if (!aDescription) return NS_ERROR_NULL_POINTER; if (!aDescription) return NS_ERROR_NULL_POINTER;
@ -96,12 +104,20 @@ nsMIMEInfoImpl::GetDescription(PRUnichar * *aDescription) {
} }
NS_IMETHODIMP NS_IMETHODIMP
nsMIMEInfoImpl::SetDescription(const PRUnichar * aDescription) { nsMIMEInfoImpl::GetDataURI(nsIURI * *aDataURI) {
mDescription.SetString(aDescription); return mURI->Clone(aDataURI);
return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
nsMIMEInfoImpl::Equals(nsIMIMEInfo *aMIMEInfo, PRBool *_retval) { nsMIMEInfoImpl::Equals(nsIMIMEInfo *aMIMEInfo, PRBool *_retval) {
return NS_ERROR_NOT_IMPLEMENTED; if (!aMIMEInfo) return NS_ERROR_NULL_POINTER;
nsXPIDLCString type;
nsresult rv = aMIMEInfo->GetMIMEType(getter_Copies(type));
if (NS_FAILED(rv)) return rv;
nsAutoString type1;
mMIMEType->ToString(type1);
*_retval = type1.Equals(type);
return NS_OK;
} }

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

@ -17,7 +17,7 @@
* Copyright (C) 1998 Netscape Communications Corporation. All * Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved. * Rights Reserved.
* *
* Contributor(s): * Contributor(s): Judson Valeski
*/ */
#ifndef __nsmimeinfoimpl_h___ #ifndef __nsmimeinfoimpl_h___
#define __nsmimeinfoimpl_h___ #define __nsmimeinfoimpl_h___
@ -25,25 +25,26 @@
#include "nsIMIMEInfo.h" #include "nsIMIMEInfo.h"
#include "nsIAtom.h" #include "nsIAtom.h"
#include "nsString2.h" #include "nsString2.h"
#include "nsVoidArray.h"
#include "nsCOMPtr.h"
class nsMIMEInfoImpl : public nsIMIMEInfo { class nsMIMEInfoImpl : public nsIMIMEInfo {
// nsISupports methods
NS_DECL_ISUPPORTS NS_DECL_ISUPPORTS
// nsMIMEInfoImpl methods
nsMIMEInfoImpl(const char *aMIMEType, const char *aFileExtensions, const char *aDescription);
virtual ~nsMIMEInfoImpl();
PRBool InExtensions(nsIAtom* anIAtom);
// nsIMIMEInfo methods
NS_DECL_NSIMIMEINFO NS_DECL_NSIMIMEINFO
// nsMIMEInfoImpl methods
nsMIMEInfoImpl(const char *aMIMEType);
virtual ~nsMIMEInfoImpl() {};
// member variables // member variables
nsIAtom* mMIMEType; nsStringArray mExtensions; // array of file extensions associated w/ this MIME obj
nsString2 mFileExtensions; nsAutoString mDescription; // human readable description
nsString2 mDescription; nsCOMPtr<nsIURI> mURI; // URI pointing to data associated w/ this obj
protected:
nsCOMPtr<nsIAtom> mMIMEType;
}; };
#endif //__nsmimeinfoimpl_h___ #endif //__nsmimeinfoimpl_h___

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

@ -17,48 +17,56 @@
* Copyright (C) 1998 Netscape Communications Corporation. All * Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved. * Rights Reserved.
* *
* Contributor(s): * Contributor(s): Judson Valeski
*/ */
#include "nsMIMEService.h" #include "nsMIMEService.h"
#include "nsVoidArray.h" #include "nsVoidArray.h"
#include "nsEnumeratorUtils.h" // for nsArrayEnumerator
#include "nsString2.h" #include "nsString2.h"
#include "nsMIMEInfoImpl.h" #include "nsMIMEInfoImpl.h"
#include "nsIMIMEInfo.h"
#include "nsIFileSpec.h"
#include "nsIURL.h" #include "nsIURL.h"
#include "nsCOMPtr.h" #include "nsCOMPtr.h"
#include "nsXPIDLString.h"
// Hash table helper functions
PRBool DeleteEntry(nsHashKey *aKey, void *aData, void* closure) { PRBool DeleteEntry(nsHashKey *aKey, void *aData, void* closure) {
nsMIMEInfoImpl *entry = (nsMIMEInfoImpl*)aData; nsMIMEInfoImpl *entry = (nsMIMEInfoImpl*)aData;
NS_IF_RELEASE(entry); NS_ASSERTION(entry, "mapping problem");
NS_RELEASE(entry);
return PR_TRUE; return PR_TRUE;
}; };
PRBool FindMIMEType(nsHashKey *aKey, void *aData, void* closure) {
return PR_FALSE;
}
// nsISupports methods // nsISupports methods
NS_IMPL_ISUPPORTS(nsMIMEService, NS_GET_IID(nsIMIMEService)); NS_IMPL_ISUPPORTS1(nsMIMEService, nsIMIMEService);
NS_METHOD
nsMIMEService::Create(nsISupports* aOuter, const nsIID& aIID, void* *aResult) {
nsMIMEService* service = new nsMIMEService();
if (!service) return NS_ERROR_OUT_OF_MEMORY;
NS_ADDREF(service);
nsresult rv = service->Init();
if (NS_FAILED(rv)) return rv;
rv = service->QueryInterface(aIID, aResult);
NS_RELEASE(service);
return rv;
}
// nsMIMEService methods // nsMIMEService methods
nsMIMEService::nsMIMEService() { nsMIMEService::nsMIMEService() {
NS_INIT_REFCNT(); NS_INIT_REFCNT();
mInfoHashtable = new nsHashtable();
InitFromHack(); // XXX bogus
//nsresult rv = InitializeMIMEMap(mInfoHashtable);
} }
nsMIMEService::~nsMIMEService() { nsMIMEService::~nsMIMEService() {
mInfoHashtable->Enumerate(DeleteEntry, nsnull); mInfoObjects->Reset(DeleteEntry, nsnull);
mInfoHashtable->Reset(); delete mInfoObjects;
delete mInfoHashtable; }
nsresult
nsMIMEService::Init() {
mInfoObjects = new nsHashtable();
if (!mInfoObjects) return NS_ERROR_OUT_OF_MEMORY;
return InitFromHack();
} }
/* This bad boy needs to retrieve a url, and parse the data coming back, and /* This bad boy needs to retrieve a url, and parse the data coming back, and
@ -88,21 +96,53 @@ nsMIMEService::InitFromFile(const char *aFileName) {
} }
// Call this first (perhaps only once) to create the initial nsMIMEInfo object
nsresult nsresult
nsMIMEService::AddMapping(const char* mimeType, nsMIMEService::AddMapping(const char* mimeType,
const char* extension, const char* extension,
const char* description) const char* description,
nsIURI* dataURI)
{ {
nsMIMEInfoImpl* anInfo = nsnull; // setup the new MIMEInfo object.
nsStringKey *extKey = nsnull; nsMIMEInfoImpl* anInfo = new nsMIMEInfoImpl(mimeType);
anInfo = new nsMIMEInfoImpl(mimeType, extension, description);
if (!anInfo) return NS_ERROR_OUT_OF_MEMORY; if (!anInfo) return NS_ERROR_OUT_OF_MEMORY;
anInfo->mExtensions.AppendString(extension);
anInfo->mDescription = description;
anInfo->mURI = dataURI;
// The entry is mapped many-to-one.
nsStringKey key(mimeType);
nsMIMEInfoImpl* oldInfo = (nsMIMEInfoImpl*)mInfoObjects->Put(&key, anInfo);
NS_IF_RELEASE(oldInfo);
NS_ADDREF(anInfo); NS_ADDREF(anInfo);
extKey = new nsStringKey(extension);
if (!extKey) return NS_ERROR_OUT_OF_MEMORY; key = extension;
mInfoHashtable->Put(extKey, anInfo); oldInfo = (nsMIMEInfoImpl*)mInfoObjects->Put(&key, anInfo);
delete extKey; NS_IF_RELEASE(oldInfo);
NS_ADDREF(anInfo);
return NS_OK;
}
// Call this for subsequent extensions to be mapped to the MIME type.
// You must call AddMapping() for a given MIME type *before* calling this.
nsresult
nsMIMEService::AppendExtension(const char* mimeType, const char* extension) {
nsStringKey key(mimeType);
nsMIMEInfoImpl* info = (nsMIMEInfoImpl*)mInfoObjects->Get(&key);
NS_ASSERTION(info, "only call AppendExtension *after* a call to AddMapping");
info->mExtensions.AppendString(extension);
// Add another mapping.
key = extension;
nsMIMEInfoImpl* oldInfo = (nsMIMEInfoImpl*)mInfoObjects->Put(&key, info);
NS_IF_RELEASE(oldInfo);
NS_ADDREF(info);
return NS_OK; return NS_OK;
} }
@ -112,97 +152,57 @@ nsMIMEService::InitFromHack() {
rv = AddMapping("text/plain", "txt", "Text File"); rv = AddMapping("text/plain", "txt", "Text File");
if (NS_FAILED(rv)) return rv; if (NS_FAILED(rv)) return rv;
rv = AddMapping("text/plain", "text", "Text File"); rv = AppendExtension("text/plain", "text");
if (NS_FAILED(rv)) return rv; if (NS_FAILED(rv)) return rv;
rv = AddMapping("application/octet-stream", "exe", "Binary Executable"); rv = AddMapping("application/octet-stream", "exe", "Binary Executable");
if (NS_FAILED(rv)) return rv; if (NS_FAILED(rv)) return rv;
rv = AddMapping("application/octet-stream", "bin", "Binary Executable"); rv = AppendExtension("application/octet-stream", "bin");
if (NS_FAILED(rv)) return rv; if (NS_FAILED(rv)) return rv;
rv = AddMapping("text/html", "htm", "Hyper Text Markup Language"); rv = AddMapping("text/html", "htm", "Hyper Text Markup Language");
if (NS_FAILED(rv)) return rv; if (NS_FAILED(rv)) return rv;
rv = AddMapping("text/html", "html", "Hyper Text Markup Language"); rv = AppendExtension("text/html", "html");
if (NS_FAILED(rv)) return rv; if (NS_FAILED(rv)) return rv;
rv = AddMapping("text/html", "shtml", "Hyper Text Markup Language"); rv = AppendExtension("text/html", "shtml");
if (NS_FAILED(rv)) return rv; if (NS_FAILED(rv)) return rv;
rv = AddMapping("text/rdf", "rdf", "Resource Description Framework"); rv = AddMapping("text/rdf", "rdf", "Resource Description Framework");
if (NS_FAILED(rv)) return rv; if (NS_FAILED(rv)) return rv;
rv = AddMapping("text/xul", "xul", "XML-Based User Interface Language"); rv = AddMapping("text/xul", "xul", "XML-Based User Interface Language");
if (NS_FAILED(rv)) return rv; if (NS_FAILED(rv)) return rv;
rv = AddMapping("text/xml", "xml", "Extensible Markup Language"); rv = AddMapping("text/xml", "xml", "Extensible Markup Language");
if (NS_FAILED(rv)) return rv; if (NS_FAILED(rv)) return rv;
rv = AddMapping("text/css", "css", "Style Sheet"); rv = AddMapping("text/css", "css", "Style Sheet");
if (NS_FAILED(rv)) return rv; if (NS_FAILED(rv)) return rv;
rv = AddMapping("application/x-javascript", "js", "Javascript Source File"); rv = AddMapping("application/x-javascript", "js", "Javascript Source File");
if (NS_FAILED(rv)) return rv; if (NS_FAILED(rv)) return rv;
rv = AddMapping("message/rfc822", "eml", "RFC-822 data"); rv = AddMapping("message/rfc822", "eml", "RFC-822 data");
if (NS_FAILED(rv)) return rv; if (NS_FAILED(rv)) return rv;
rv = AddMapping("image/gif", "gif", "GIF Image"); rv = AddMapping("image/gif", "gif", "GIF Image");
if (NS_FAILED(rv)) return rv; if (NS_FAILED(rv)) return rv;
rv = AddMapping("image/jpeg", "jpeg", "JPEG Image"); rv = AddMapping("image/jpeg", "jpeg", "JPEG Image");
if (NS_FAILED(rv)) return rv; if (NS_FAILED(rv)) return rv;
rv = AddMapping("image/jpeg", "jpg", "JPEG Image"); rv = AppendExtension("image/jpeg", "jpg");
if (NS_FAILED(rv)) return rv; if (NS_FAILED(rv)) return rv;
rv = AddMapping("image/png", "png", "PNG Image"); rv = AddMapping("image/png", "png", "PNG Image");
if (NS_FAILED(rv)) return rv; if (NS_FAILED(rv)) return rv;
rv = AddMapping("image/x-jg", "art", "ART Image"); rv = AddMapping("image/x-jg", "art", "ART Image");
if (NS_FAILED(rv)) return rv; if (NS_FAILED(rv)) return rv;
rv = AddMapping("image/tiff", "tiff", "TIFF Image"); rv = AddMapping("image/tiff", "tiff", "TIFF Image");
if (NS_FAILED(rv)) return rv; if (NS_FAILED(rv)) return rv;
rv = AddMapping("image/tiff", "tif", "TIFF Image"); rv = AppendExtension("image/tiff", "tif");
if (NS_FAILED(rv)) return rv; if (NS_FAILED(rv)) return rv;
#if 0
anInfo = new nsMIMEInfoImpl("image/x-cmu-raster", "ras", "CMU Raster Image");
if (!anInfo) return NS_ERROR_OUT_OF_MEMORY;
mInfoArray->AppendElement(anInfo);
anInfo = new nsMIMEInfoImpl("image/x-xbitmap", "xbm", "X Bitmap");
if (!anInfo) return NS_ERROR_OUT_OF_MEMORY;
mInfoArray->AppendElement(anInfo);
anInfo = new nsMIMEInfoImpl("image/x-xpixmap", "xpm", "X Pixmap");
if (!anInfo) return NS_ERROR_OUT_OF_MEMORY;
mInfoArray->AppendElement(anInfo);
anInfo = new nsMIMEInfoImpl("image/x-xwindowdump", "xwd", "X Window Dump Image");
if (!anInfo) return NS_ERROR_OUT_OF_MEMORY;
mInfoArray->AppendElement(anInfo);
anInfo = new nsMIMEInfoImpl("image/x-portable-anymap", "pnm", "PBM Image");
if (!anInfo) return NS_ERROR_OUT_OF_MEMORY;
mInfoArray->AppendElement(anInfo);
anInfo = new nsMIMEInfoImpl("image/x-portable-bitmap", "pbm", "PBM Image");
if (!anInfo) return NS_ERROR_OUT_OF_MEMORY;
mInfoArray->AppendElement(anInfo);
anInfo = new nsMIMEInfoImpl("image/x-portable-graymap", "pgm", "PGM Image");
if (!anInfo) return NS_ERROR_OUT_OF_MEMORY;
mInfoArray->AppendElement(anInfo);
anInfo = new nsMIMEInfoImpl("image/x-portable-pixmap", "ppm", "PPM Image");
if (!anInfo) return NS_ERROR_OUT_OF_MEMORY;
mInfoArray->AppendElement(anInfo);
anInfo = new nsMIMEInfoImpl("image/x-rgb", "rgb", "RGB Image");
if (!anInfo) return NS_ERROR_OUT_OF_MEMORY;
mInfoArray->AppendElement(anInfo);
anInfo = new nsMIMEInfoImpl("image/x-MS-bmp", "bmp", "Windows Bitmap");
if (!anInfo) return NS_ERROR_OUT_OF_MEMORY;
mInfoArray->AppendElement(anInfo);
anInfo = new nsMIMEInfoImpl("image/x-photo-cd", "pcd", "PhotoCD Image");
if (!anInfo) return NS_ERROR_OUT_OF_MEMORY;
mInfoArray->AppendElement(anInfo);
anInfo = new nsMIMEInfoImpl("image/ief", "ief", "");
if (!anInfo) return NS_ERROR_OUT_OF_MEMORY;
mInfoArray->AppendElement(anInfo);
anInfo = new nsMIMEInfoImpl("application/fractals", "fif", "Fractal Image Format");
if (!anInfo) return NS_ERROR_OUT_OF_MEMORY;
mInfoArray->AppendElement(anInfo);
#endif // 0
return NS_OK; return NS_OK;
} }
@ -217,7 +217,7 @@ nsMIMEService::GetFromExtension(const char *aFileExt, nsIMIMEInfo **_retval) {
nsStringKey key(fileExt.GetBuffer()); nsStringKey key(fileExt.GetBuffer());
nsMIMEInfoImpl *entry = (nsMIMEInfoImpl*)mInfoHashtable->Get(&key); nsMIMEInfoImpl *entry = (nsMIMEInfoImpl*)mInfoObjects->Get(&key);
if (!entry) return NS_ERROR_FAILURE; if (!entry) return NS_ERROR_FAILURE;
NS_ADDREF(entry); NS_ADDREF(entry);
*_retval = NS_STATIC_CAST(nsIMIMEInfo*, entry); *_retval = NS_STATIC_CAST(nsIMIMEInfo*, entry);
@ -226,13 +226,12 @@ nsMIMEService::GetFromExtension(const char *aFileExt, nsIMIMEInfo **_retval) {
NS_IMETHODIMP NS_IMETHODIMP
nsMIMEService::GetTypeFromExtension(const char *aFileExt, char **aContentType) { nsMIMEService::GetTypeFromExtension(const char *aFileExt, char **aContentType) {
nsresult rv; nsresult rv = NS_OK;;
nsIMIMEInfo *info = nsnull; nsCOMPtr<nsIMIMEInfo> info;
rv = GetFromExtension(aFileExt, &info); rv = GetFromExtension(aFileExt, getter_AddRefs(info));
if (NS_FAILED(rv)) return rv; if (NS_FAILED(rv)) return rv;
rv = info->GetMIMEType(aContentType); rv = info->GetMIMEType(aContentType);
NS_RELEASE(info);
return rv; return rv;
} }
@ -243,21 +242,19 @@ nsMIMEService::GetTypeFromURI(nsIURI *aURI, char **aContentType) {
// filename stuff (i.e. query string) // filename stuff (i.e. query string)
nsCOMPtr<nsIURL> url = do_QueryInterface(aURI, &rv); nsCOMPtr<nsIURL> url = do_QueryInterface(aURI, &rv);
if (NS_SUCCEEDED(rv)) { if (NS_SUCCEEDED(rv)) {
char *ext = nsnull; nsXPIDLCString ext;
rv = url->GetFileExtension(&ext); rv = url->GetFileExtension(getter_Copies(ext));
if (NS_FAILED(rv)) return rv; if (NS_FAILED(rv)) return rv;
rv = GetTypeFromExtension(ext, aContentType); rv = GetTypeFromExtension(ext, aContentType);
nsAllocator::Free(ext);
return rv; return rv;
} }
char *cStrSpec= nsnull; nsXPIDLCString cStrSpec;
// no url, let's give the raw spec a shot // no url, let's give the raw spec a shot
rv = aURI->GetSpec(&cStrSpec); rv = aURI->GetSpec(getter_Copies(cStrSpec));
if (NS_FAILED(rv)) return rv; if (NS_FAILED(rv)) return rv;
nsAutoString specStr(cStrSpec); nsAutoString specStr(cStrSpec);
nsAllocator::Free(cStrSpec);
// find the file extension (if any) // find the file extension (if any)
nsAutoString extStr; nsAutoString extStr;
@ -276,31 +273,14 @@ nsMIMEService::GetTypeFromURI(nsIURI *aURI, char **aContentType) {
NS_IMETHODIMP NS_IMETHODIMP
nsMIMEService::GetFromMIMEType(const char *aMIMEType, nsIMIMEInfo **_retval) { nsMIMEService::GetFromMIMEType(const char *aMIMEType, nsIMIMEInfo **_retval) {
nsCAutoString MIMEType(aMIMEType);
MIMEType.ToLowerCase();
#if 0 nsStringKey key(MIMEType.GetBuffer());
// hashtable, here's it's a little trickier because we have to enumerate the hashtable
nsIAtom* MIMEType = NS_NewAtom(aMIMEType); nsMIMEInfoImpl *entry = (nsMIMEInfoImpl*)mInfoObjects->Get(&key);
if (!MIMEType) return NS_ERROR_OUT_OF_MEMORY; if (!entry) return NS_ERROR_FAILURE;
NS_ADDREF(entry);
PRInt32 count = mInfoArray->Count(); *_retval = NS_STATIC_CAST(nsIMIMEInfo*, entry);
for (int i = 0; i < count; i++) { return NS_OK;
nsMIMEInfoImpl *entry = (nsMIMEInfoImpl*)mInfoArray->ElementAt(i);
if (entry->mMIMEType == MIMEType) {
*_retval = NS_STATIC_CAST(nsIMIMEInfo*, entry);
return NS_OK;
}
}
#endif
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsMIMEService::AddMIMEInfo(nsIMIMEInfo *aMIMEInfo) {
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsMIMEService::RemoveMIMEInfo(nsIMIMEInfo *aMIMEInfo) {
return NS_ERROR_NOT_IMPLEMENTED;
} }

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

@ -17,7 +17,7 @@
* Copyright (C) 1998 Netscape Communications Corporation. All * Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved. * Rights Reserved.
* *
* Contributor(s): * Contributor(s): Judson Valeski
*/ */
/* /*
@ -35,26 +35,28 @@
class nsMIMEService : public nsIMIMEService { class nsMIMEService : public nsIMIMEService {
// nsISupports methods
NS_DECL_ISUPPORTS NS_DECL_ISUPPORTS
NS_DECL_NSIMIMESERVICE
// nsMIMEService methods // nsMIMEService methods
nsMIMEService(); nsMIMEService();
virtual ~nsMIMEService(); virtual ~nsMIMEService();
nsresult Init();
// nsIMIMEService methods static NS_METHOD Create(nsISupports* aOuter, const nsIID& aIID, void* *aResult);
NS_DECL_NSIMIMESERVICE
private: private:
nsresult AddMapping(const char* mimeType, nsresult AddMapping(const char* mimeType,
const char* extension, const char* extension,
const char* description); const char* description,
nsIURI* dataURI = nsnull);
nsresult AppendExtension(const char* mimeType, const char* extension);
nsresult InitFromURI(nsIURI *aUri); nsresult InitFromURI(nsIURI *aUri);
nsresult InitFromFile(const char *aFileName); nsresult InitFromFile(const char *aFileName);
nsresult InitFromHack(); nsresult InitFromHack();
nsHashtable *mInfoHashtable; nsHashtable *mInfoObjects;
}; };
#endif // ___nsIMIMEService__h___ #endif // ___nsIMIMEService__h___

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

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

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

@ -0,0 +1,34 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s): Judson Valeski
*/
#include "nsIGenericFactory.h"
#include "nsMIMEService.h"
static nsModuleComponentInfo gResComponents[] = {
{ "The MIME mapping service",
NS_MIMESERVICE_CID,
"component:||netscape|mime",
nsMIMEService::Create
}
};
NS_IMPL_NSGETMODULE("nsMIMEService", gResComponents)

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

@ -17,56 +17,72 @@
* Copyright (C) 1998 Netscape Communications Corporation. All * Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved. * Rights Reserved.
* *
* Contributor(s): * Contributor(s): Judson Valeski
*/ */
#include "nsMIMEInfoImpl.h" #include "nsMIMEInfoImpl.h"
#include "nsCRT.h" #include "nsXPIDLString.h"
// nsISupports methods // nsISupports methods
NS_IMPL_ISUPPORTS(nsMIMEInfoImpl, NS_GET_IID(nsIMIMEInfo)); NS_IMPL_ISUPPORTS(nsMIMEInfoImpl, NS_GET_IID(nsIMIMEInfo));
// nsMIMEInfoImpl methods // nsMIMEInfoImpl methods
nsMIMEInfoImpl::nsMIMEInfoImpl(const char *aMIMEType, const char *aFileExtensions, const char *aDescription) { nsMIMEInfoImpl::nsMIMEInfoImpl(const char *aMIMEType) {
NS_INIT_REFCNT(); NS_INIT_REFCNT();
mMIMEType = NS_NewAtom(aMIMEType); mMIMEType = getter_AddRefs(NS_NewAtom(aMIMEType));
mFileExtensions.SetString(aFileExtensions);
mDescription.SetString(aDescription);
} }
nsMIMEInfoImpl::~nsMIMEInfoImpl() {
NS_RELEASE(mMIMEType);
}
PRBool
nsMIMEInfoImpl::InExtensions(nsIAtom* anIAtom) {
nsAutoString extension;
anIAtom->ToString(extension);
// XXX this is broken. need to use gessner's tokenizer stuff to delimit the commas
if (mFileExtensions.Find(extension) == -1)
return PR_FALSE;
return PR_TRUE;
}
// nsIMIMEInfo methods
NS_IMETHODIMP NS_IMETHODIMP
nsMIMEInfoImpl::GetFileExtensions(char * *aFileExtensions) { nsMIMEInfoImpl::GetFileExtensions(PRInt32 *elementCount, char ***extensions) {
if (!aFileExtensions) return NS_ERROR_NULL_POINTER; PRUint32 extCount = mExtensions.Count();
if (extCount < 1) return NS_ERROR_NOT_INITIALIZED;
char **_retExts = (char**)nsAllocator::Alloc(extCount*2*sizeof(char*));
if (!_retExts) return NS_ERROR_OUT_OF_MEMORY;
for (PRUint8 i=0; i < extCount; i++) {
nsString* ext = (nsString*)mExtensions.StringAt(i);
_retExts[i] = ext->ToNewCString();
if (!_retExts[i]) return NS_ERROR_OUT_OF_MEMORY;
}
*elementCount = extCount;
*extensions = _retExts;
*aFileExtensions = mFileExtensions.ToNewCString();
if (!*aFileExtensions)
return NS_ERROR_OUT_OF_MEMORY;
return NS_OK; return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
nsMIMEInfoImpl::SetFileExtensions(const char * aFileExtensions) { nsMIMEInfoImpl::ExtensionExists(const char *aExtension, PRBool *_retval) {
mFileExtensions.SetString(aFileExtensions); NS_ASSERTION(aExtension, "no extension");
PRBool found = PR_FALSE;
PRUint32 extCount = mExtensions.Count();
if (extCount < 1) return NS_ERROR_NOT_INITIALIZED;
if (!aExtension) return NS_ERROR_NULL_POINTER;
for (PRUint8 i=0; i < extCount; i++) {
nsString* ext = (nsString*)mExtensions.StringAt(i);
if (ext->Equals(aExtension)) {
found = PR_TRUE;
break;
}
}
*_retval = found;
return NS_OK; return NS_OK;
} }
NS_IMETHODIMP
nsMIMEInfoImpl::FirstExtension(char **_retval) {
PRUint32 extCount = mExtensions.Count();
if (extCount < 1) return NS_ERROR_NOT_INITIALIZED;
*_retval = (mExtensions.StringAt(0))->ToNewCString();
if (!*_retval) return NS_ERROR_OUT_OF_MEMORY;
return NS_OK;
}
NS_IMETHODIMP NS_IMETHODIMP
nsMIMEInfoImpl::GetMIMEType(char * *aMIMEType) { nsMIMEInfoImpl::GetMIMEType(char * *aMIMEType) {
if (!aMIMEType) return NS_ERROR_NULL_POINTER; if (!aMIMEType) return NS_ERROR_NULL_POINTER;
@ -78,14 +94,6 @@ nsMIMEInfoImpl::GetMIMEType(char * *aMIMEType) {
return NS_OK; return NS_OK;
} }
NS_IMETHODIMP
nsMIMEInfoImpl::SetMIMEType(const char * aMIMEType) {
NS_RELEASE(mMIMEType);
mMIMEType = NS_NewAtom(aMIMEType);
if (!mMIMEType) return NS_ERROR_OUT_OF_MEMORY;
return NS_OK;
}
NS_IMETHODIMP NS_IMETHODIMP
nsMIMEInfoImpl::GetDescription(PRUnichar * *aDescription) { nsMIMEInfoImpl::GetDescription(PRUnichar * *aDescription) {
if (!aDescription) return NS_ERROR_NULL_POINTER; if (!aDescription) return NS_ERROR_NULL_POINTER;
@ -96,12 +104,20 @@ nsMIMEInfoImpl::GetDescription(PRUnichar * *aDescription) {
} }
NS_IMETHODIMP NS_IMETHODIMP
nsMIMEInfoImpl::SetDescription(const PRUnichar * aDescription) { nsMIMEInfoImpl::GetDataURI(nsIURI * *aDataURI) {
mDescription.SetString(aDescription); return mURI->Clone(aDataURI);
return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
nsMIMEInfoImpl::Equals(nsIMIMEInfo *aMIMEInfo, PRBool *_retval) { nsMIMEInfoImpl::Equals(nsIMIMEInfo *aMIMEInfo, PRBool *_retval) {
return NS_ERROR_NOT_IMPLEMENTED; if (!aMIMEInfo) return NS_ERROR_NULL_POINTER;
nsXPIDLCString type;
nsresult rv = aMIMEInfo->GetMIMEType(getter_Copies(type));
if (NS_FAILED(rv)) return rv;
nsAutoString type1;
mMIMEType->ToString(type1);
*_retval = type1.Equals(type);
return NS_OK;
} }

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

@ -17,7 +17,7 @@
* Copyright (C) 1998 Netscape Communications Corporation. All * Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved. * Rights Reserved.
* *
* Contributor(s): * Contributor(s): Judson Valeski
*/ */
#ifndef __nsmimeinfoimpl_h___ #ifndef __nsmimeinfoimpl_h___
#define __nsmimeinfoimpl_h___ #define __nsmimeinfoimpl_h___
@ -25,25 +25,26 @@
#include "nsIMIMEInfo.h" #include "nsIMIMEInfo.h"
#include "nsIAtom.h" #include "nsIAtom.h"
#include "nsString2.h" #include "nsString2.h"
#include "nsVoidArray.h"
#include "nsCOMPtr.h"
class nsMIMEInfoImpl : public nsIMIMEInfo { class nsMIMEInfoImpl : public nsIMIMEInfo {
// nsISupports methods
NS_DECL_ISUPPORTS NS_DECL_ISUPPORTS
// nsMIMEInfoImpl methods
nsMIMEInfoImpl(const char *aMIMEType, const char *aFileExtensions, const char *aDescription);
virtual ~nsMIMEInfoImpl();
PRBool InExtensions(nsIAtom* anIAtom);
// nsIMIMEInfo methods
NS_DECL_NSIMIMEINFO NS_DECL_NSIMIMEINFO
// nsMIMEInfoImpl methods
nsMIMEInfoImpl(const char *aMIMEType);
virtual ~nsMIMEInfoImpl() {};
// member variables // member variables
nsIAtom* mMIMEType; nsStringArray mExtensions; // array of file extensions associated w/ this MIME obj
nsString2 mFileExtensions; nsAutoString mDescription; // human readable description
nsString2 mDescription; nsCOMPtr<nsIURI> mURI; // URI pointing to data associated w/ this obj
protected:
nsCOMPtr<nsIAtom> mMIMEType;
}; };
#endif //__nsmimeinfoimpl_h___ #endif //__nsmimeinfoimpl_h___