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
* 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 "nsIURI.idl"
[scriptable, uuid(6A57EAE0-2FE8-11d3-A164-0050041CAF44)]
interface nsIMIMEInfo : nsISupports {
// return the file extensions associated with this MIME type set.
attribute string FileExtensions;
/* Gives you an array of file types associated with this type.
*
* @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.
attribute string MIMEType;
/* Returns whether or not the given extension is
* associated with this MIME info.
*
* @return TRUE if the assocaition exists.
*/
// return the description of this MIME type set.
attribute wstring Description;
boolean ExtensionExists(in string aExtension);
/* 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);
};

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

@ -17,20 +17,21 @@
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
* Contributor(s): Judson Valeski
*/
/* 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 "nsIMIMEInfo.idl"
#include "nsIURI.idl"
%{C++
#define NS_MIMESERVICE_CID \
{ /* 03af31da-3109-11d3-8cd0-0060b0fc14a3 */ \
0x03af31da, \
@ -38,25 +39,41 @@
0x11d3, \
{0x8c, 0xd0, 0x00, 0x60, 0xb0, 0xfc, 0x14, 0xa3} \
}
%}
[scriptable, uuid(6C424C90-2FE7-11d3-A164-0050041CAF44)]
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);
// return a string representing the content-type
void GetTypeFromExtension(in string aFileExt, out string aContentType);
void GetTypeFromURI(in nsIURI aURI, out string aContentType);
// return a nsIMIMEInfo for the given MIME type.
/* Retrieves a registered nsIMIMEInfo interface associated
* with the given MIME type.
*
* @param A MIME type.
* @return A nsIMIMEInfo interface.
*/
nsIMIMEInfo GetFromMIMEType(in string aMIMEType);
// insert a nsIMIMEInfo in the the database.
void AddMIMEInfo(in nsIMIMEInfo aMIMEInfo);
/* Retrieves a string representation of the MIME type
* 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.
void RemoveMIMEInfo(in nsIMIMEInfo aMIMEInfo);
/* Retrieves a string representation of the MIME type
* 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 = \
nsMIMEInfoImpl.cpp \
nsMIMEService.cpp \
nsMIMEServiceFactory.cpp \
nsMIMEServiceModule.cpp \
$(NULL)
EXTRA_DSO_LDOPTS += $(MOZ_COMPONENT_LIBS)

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

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

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

@ -17,56 +17,72 @@
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
* Contributor(s): Judson Valeski
*/
#include "nsMIMEInfoImpl.h"
#include "nsCRT.h"
#include "nsXPIDLString.h"
// nsISupports methods
NS_IMPL_ISUPPORTS(nsMIMEInfoImpl, NS_GET_IID(nsIMIMEInfo));
// nsMIMEInfoImpl methods
nsMIMEInfoImpl::nsMIMEInfoImpl(const char *aMIMEType, const char *aFileExtensions, const char *aDescription) {
nsMIMEInfoImpl::nsMIMEInfoImpl(const char *aMIMEType) {
NS_INIT_REFCNT();
mMIMEType = NS_NewAtom(aMIMEType);
mFileExtensions.SetString(aFileExtensions);
mDescription.SetString(aDescription);
mMIMEType = getter_AddRefs(NS_NewAtom(aMIMEType));
}
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
nsMIMEInfoImpl::GetFileExtensions(char * *aFileExtensions) {
if (!aFileExtensions) return NS_ERROR_NULL_POINTER;
nsMIMEInfoImpl::GetFileExtensions(PRInt32 *elementCount, char ***extensions) {
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;
}
NS_IMETHODIMP
nsMIMEInfoImpl::SetFileExtensions(const char * aFileExtensions) {
mFileExtensions.SetString(aFileExtensions);
nsMIMEInfoImpl::ExtensionExists(const char *aExtension, PRBool *_retval) {
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;
}
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
nsMIMEInfoImpl::GetMIMEType(char * *aMIMEType) {
if (!aMIMEType) return NS_ERROR_NULL_POINTER;
@ -78,14 +94,6 @@ nsMIMEInfoImpl::GetMIMEType(char * *aMIMEType) {
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
nsMIMEInfoImpl::GetDescription(PRUnichar * *aDescription) {
if (!aDescription) return NS_ERROR_NULL_POINTER;
@ -96,12 +104,20 @@ nsMIMEInfoImpl::GetDescription(PRUnichar * *aDescription) {
}
NS_IMETHODIMP
nsMIMEInfoImpl::SetDescription(const PRUnichar * aDescription) {
mDescription.SetString(aDescription);
return NS_OK;
nsMIMEInfoImpl::GetDataURI(nsIURI * *aDataURI) {
return mURI->Clone(aDataURI);
}
NS_IMETHODIMP
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
* Rights Reserved.
*
* Contributor(s):
* Contributor(s): Judson Valeski
*/
#ifndef __nsmimeinfoimpl_h___
#define __nsmimeinfoimpl_h___
@ -25,25 +25,26 @@
#include "nsIMIMEInfo.h"
#include "nsIAtom.h"
#include "nsString2.h"
#include "nsVoidArray.h"
#include "nsCOMPtr.h"
class nsMIMEInfoImpl : public nsIMIMEInfo {
// nsISupports methods
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
// nsMIMEInfoImpl methods
nsMIMEInfoImpl(const char *aMIMEType);
virtual ~nsMIMEInfoImpl() {};
// member variables
nsIAtom* mMIMEType;
nsString2 mFileExtensions;
nsString2 mDescription;
nsStringArray mExtensions; // array of file extensions associated w/ this MIME obj
nsAutoString mDescription; // human readable description
nsCOMPtr<nsIURI> mURI; // URI pointing to data associated w/ this obj
protected:
nsCOMPtr<nsIAtom> mMIMEType;
};
#endif //__nsmimeinfoimpl_h___

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

@ -17,48 +17,56 @@
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
* Contributor(s): Judson Valeski
*/
#include "nsMIMEService.h"
#include "nsVoidArray.h"
#include "nsEnumeratorUtils.h" // for nsArrayEnumerator
#include "nsString2.h"
#include "nsMIMEInfoImpl.h"
#include "nsIMIMEInfo.h"
#include "nsIFileSpec.h"
#include "nsIURL.h"
#include "nsCOMPtr.h"
#include "nsXPIDLString.h"
// Hash table helper functions
PRBool DeleteEntry(nsHashKey *aKey, void *aData, void* closure) {
nsMIMEInfoImpl *entry = (nsMIMEInfoImpl*)aData;
NS_IF_RELEASE(entry);
NS_ASSERTION(entry, "mapping problem");
NS_RELEASE(entry);
return PR_TRUE;
};
PRBool FindMIMEType(nsHashKey *aKey, void *aData, void* closure) {
return PR_FALSE;
}
// 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::nsMIMEService() {
NS_INIT_REFCNT();
mInfoHashtable = new nsHashtable();
InitFromHack(); // XXX bogus
//nsresult rv = InitializeMIMEMap(mInfoHashtable);
}
nsMIMEService::~nsMIMEService() {
mInfoHashtable->Enumerate(DeleteEntry, nsnull);
mInfoHashtable->Reset();
delete mInfoHashtable;
mInfoObjects->Reset(DeleteEntry, nsnull);
delete mInfoObjects;
}
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
@ -88,21 +96,53 @@ nsMIMEService::InitFromFile(const char *aFileName) {
}
// Call this first (perhaps only once) to create the initial nsMIMEInfo object
nsresult
nsMIMEService::AddMapping(const char* mimeType,
const char* extension,
const char* description)
const char* description,
nsIURI* dataURI)
{
nsMIMEInfoImpl* anInfo = nsnull;
nsStringKey *extKey = nsnull;
anInfo = new nsMIMEInfoImpl(mimeType, extension, description);
// setup the new MIMEInfo object.
nsMIMEInfoImpl* anInfo = new nsMIMEInfoImpl(mimeType);
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);
extKey = new nsStringKey(extension);
if (!extKey) return NS_ERROR_OUT_OF_MEMORY;
mInfoHashtable->Put(extKey, anInfo);
delete extKey;
key = extension;
oldInfo = (nsMIMEInfoImpl*)mInfoObjects->Put(&key, anInfo);
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;
}
@ -112,97 +152,57 @@ nsMIMEService::InitFromHack() {
rv = AddMapping("text/plain", "txt", "Text File");
if (NS_FAILED(rv)) return rv;
rv = AddMapping("text/plain", "text", "Text File");
rv = AppendExtension("text/plain", "text");
if (NS_FAILED(rv)) return rv;
rv = AddMapping("application/octet-stream", "exe", "Binary Executable");
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;
rv = AddMapping("text/html", "htm", "Hyper Text Markup Language");
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;
rv = AddMapping("text/html", "shtml", "Hyper Text Markup Language");
rv = AppendExtension("text/html", "shtml");
if (NS_FAILED(rv)) return rv;
rv = AddMapping("text/rdf", "rdf", "Resource Description Framework");
if (NS_FAILED(rv)) return rv;
rv = AddMapping("text/xul", "xul", "XML-Based User Interface Language");
if (NS_FAILED(rv)) return rv;
rv = AddMapping("text/xml", "xml", "Extensible Markup Language");
if (NS_FAILED(rv)) return rv;
rv = AddMapping("text/css", "css", "Style Sheet");
if (NS_FAILED(rv)) return rv;
rv = AddMapping("application/x-javascript", "js", "Javascript Source File");
if (NS_FAILED(rv)) return rv;
rv = AddMapping("message/rfc822", "eml", "RFC-822 data");
if (NS_FAILED(rv)) return rv;
rv = AddMapping("image/gif", "gif", "GIF Image");
if (NS_FAILED(rv)) return rv;
rv = AddMapping("image/jpeg", "jpeg", "JPEG Image");
if (NS_FAILED(rv)) return rv;
rv = AddMapping("image/jpeg", "jpg", "JPEG Image");
rv = AppendExtension("image/jpeg", "jpg");
if (NS_FAILED(rv)) return rv;
rv = AddMapping("image/png", "png", "PNG Image");
if (NS_FAILED(rv)) return rv;
rv = AddMapping("image/x-jg", "art", "ART Image");
if (NS_FAILED(rv)) return rv;
rv = AddMapping("image/tiff", "tiff", "TIFF Image");
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 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;
}
@ -217,7 +217,7 @@ nsMIMEService::GetFromExtension(const char *aFileExt, nsIMIMEInfo **_retval) {
nsStringKey key(fileExt.GetBuffer());
nsMIMEInfoImpl *entry = (nsMIMEInfoImpl*)mInfoHashtable->Get(&key);
nsMIMEInfoImpl *entry = (nsMIMEInfoImpl*)mInfoObjects->Get(&key);
if (!entry) return NS_ERROR_FAILURE;
NS_ADDREF(entry);
*_retval = NS_STATIC_CAST(nsIMIMEInfo*, entry);
@ -226,13 +226,12 @@ nsMIMEService::GetFromExtension(const char *aFileExt, nsIMIMEInfo **_retval) {
NS_IMETHODIMP
nsMIMEService::GetTypeFromExtension(const char *aFileExt, char **aContentType) {
nsresult rv;
nsIMIMEInfo *info = nsnull;
rv = GetFromExtension(aFileExt, &info);
nsresult rv = NS_OK;;
nsCOMPtr<nsIMIMEInfo> info;
rv = GetFromExtension(aFileExt, getter_AddRefs(info));
if (NS_FAILED(rv)) return rv;
rv = info->GetMIMEType(aContentType);
NS_RELEASE(info);
return rv;
}
@ -243,21 +242,19 @@ nsMIMEService::GetTypeFromURI(nsIURI *aURI, char **aContentType) {
// filename stuff (i.e. query string)
nsCOMPtr<nsIURL> url = do_QueryInterface(aURI, &rv);
if (NS_SUCCEEDED(rv)) {
char *ext = nsnull;
rv = url->GetFileExtension(&ext);
nsXPIDLCString ext;
rv = url->GetFileExtension(getter_Copies(ext));
if (NS_FAILED(rv)) return rv;
rv = GetTypeFromExtension(ext, aContentType);
nsAllocator::Free(ext);
return rv;
}
char *cStrSpec= nsnull;
nsXPIDLCString cStrSpec;
// 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;
nsAutoString specStr(cStrSpec);
nsAllocator::Free(cStrSpec);
// find the file extension (if any)
nsAutoString extStr;
@ -276,31 +273,14 @@ nsMIMEService::GetTypeFromURI(nsIURI *aURI, char **aContentType) {
NS_IMETHODIMP
nsMIMEService::GetFromMIMEType(const char *aMIMEType, nsIMIMEInfo **_retval) {
nsCAutoString MIMEType(aMIMEType);
MIMEType.ToLowerCase();
#if 0
// hashtable, here's it's a little trickier because we have to enumerate the hashtable
nsStringKey key(MIMEType.GetBuffer());
nsIAtom* MIMEType = NS_NewAtom(aMIMEType);
if (!MIMEType) return NS_ERROR_OUT_OF_MEMORY;
PRInt32 count = mInfoArray->Count();
for (int i = 0; i < count; i++) {
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;
nsMIMEInfoImpl *entry = (nsMIMEInfoImpl*)mInfoObjects->Get(&key);
if (!entry) return NS_ERROR_FAILURE;
NS_ADDREF(entry);
*_retval = NS_STATIC_CAST(nsIMIMEInfo*, entry);
return NS_OK;
}

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

@ -17,7 +17,7 @@
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
* Contributor(s): Judson Valeski
*/
/*
@ -35,26 +35,28 @@
class nsMIMEService : public nsIMIMEService {
// nsISupports methods
NS_DECL_ISUPPORTS
NS_DECL_NSIMIMESERVICE
// nsMIMEService methods
nsMIMEService();
virtual ~nsMIMEService();
// nsIMIMEService methods
NS_DECL_NSIMIMESERVICE
nsresult Init();
static NS_METHOD Create(nsISupports* aOuter, const nsIID& aIID, void* *aResult);
private:
nsresult AddMapping(const char* mimeType,
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 InitFromFile(const char *aFileName);
nsresult InitFromHack();
nsHashtable *mInfoHashtable;
nsHashtable *mInfoObjects;
};
#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
* Rights Reserved.
*
* Contributor(s):
* Contributor(s): Judson Valeski
*/
#include "nsMIMEInfoImpl.h"
#include "nsCRT.h"
#include "nsXPIDLString.h"
// nsISupports methods
NS_IMPL_ISUPPORTS(nsMIMEInfoImpl, NS_GET_IID(nsIMIMEInfo));
// nsMIMEInfoImpl methods
nsMIMEInfoImpl::nsMIMEInfoImpl(const char *aMIMEType, const char *aFileExtensions, const char *aDescription) {
nsMIMEInfoImpl::nsMIMEInfoImpl(const char *aMIMEType) {
NS_INIT_REFCNT();
mMIMEType = NS_NewAtom(aMIMEType);
mFileExtensions.SetString(aFileExtensions);
mDescription.SetString(aDescription);
mMIMEType = getter_AddRefs(NS_NewAtom(aMIMEType));
}
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
nsMIMEInfoImpl::GetFileExtensions(char * *aFileExtensions) {
if (!aFileExtensions) return NS_ERROR_NULL_POINTER;
nsMIMEInfoImpl::GetFileExtensions(PRInt32 *elementCount, char ***extensions) {
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;
}
NS_IMETHODIMP
nsMIMEInfoImpl::SetFileExtensions(const char * aFileExtensions) {
mFileExtensions.SetString(aFileExtensions);
nsMIMEInfoImpl::ExtensionExists(const char *aExtension, PRBool *_retval) {
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;
}
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
nsMIMEInfoImpl::GetMIMEType(char * *aMIMEType) {
if (!aMIMEType) return NS_ERROR_NULL_POINTER;
@ -78,14 +94,6 @@ nsMIMEInfoImpl::GetMIMEType(char * *aMIMEType) {
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
nsMIMEInfoImpl::GetDescription(PRUnichar * *aDescription) {
if (!aDescription) return NS_ERROR_NULL_POINTER;
@ -96,12 +104,20 @@ nsMIMEInfoImpl::GetDescription(PRUnichar * *aDescription) {
}
NS_IMETHODIMP
nsMIMEInfoImpl::SetDescription(const PRUnichar * aDescription) {
mDescription.SetString(aDescription);
return NS_OK;
nsMIMEInfoImpl::GetDataURI(nsIURI * *aDataURI) {
return mURI->Clone(aDataURI);
}
NS_IMETHODIMP
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
* Rights Reserved.
*
* Contributor(s):
* Contributor(s): Judson Valeski
*/
#ifndef __nsmimeinfoimpl_h___
#define __nsmimeinfoimpl_h___
@ -25,25 +25,26 @@
#include "nsIMIMEInfo.h"
#include "nsIAtom.h"
#include "nsString2.h"
#include "nsVoidArray.h"
#include "nsCOMPtr.h"
class nsMIMEInfoImpl : public nsIMIMEInfo {
// nsISupports methods
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
// nsMIMEInfoImpl methods
nsMIMEInfoImpl(const char *aMIMEType);
virtual ~nsMIMEInfoImpl() {};
// member variables
nsIAtom* mMIMEType;
nsString2 mFileExtensions;
nsString2 mDescription;
nsStringArray mExtensions; // array of file extensions associated w/ this MIME obj
nsAutoString mDescription; // human readable description
nsCOMPtr<nsIURI> mURI; // URI pointing to data associated w/ this obj
protected:
nsCOMPtr<nsIAtom> mMIMEType;
};
#endif //__nsmimeinfoimpl_h___