2007-04-18 00:31:06 +02:00
|
|
|
#include <config.h>
|
|
|
|
|
2007-04-17 22:46:33 +02:00
|
|
|
#ifdef HAVE_GECKO_1_9
|
|
|
|
|
2007-04-17 21:53:34 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
#include <imgICache.h>
|
|
|
|
#include <nsComponentManagerUtils.h>
|
|
|
|
#include <nsCOMPtr.h>
|
|
|
|
#include <nsIDOMHTMLElement.h>
|
|
|
|
#include <nsIInterfaceRequestorUtils.h>
|
|
|
|
#include <nsIIOService.h>
|
|
|
|
#include <nsILocalFile.h>
|
|
|
|
#include <nsIMIMEHeaderParam.h>
|
|
|
|
#include <nsIProperties.h>
|
|
|
|
#include <nsISupportsPrimitives.h>
|
|
|
|
#include <nsIURI.h>
|
|
|
|
#include <nsIURL.h>
|
|
|
|
#include <nsServiceManagerUtils.h>
|
|
|
|
#include <nsStringAPI.h>
|
|
|
|
|
|
|
|
#include "GeckoDocumentObject.h"
|
|
|
|
#include "GeckoBrowserPersist.h"
|
|
|
|
|
|
|
|
GeckoDocumentObject::GeckoDocumentObject(SugarBrowser *browser, nsIDOMNode *node)
|
|
|
|
: mBrowser(browser),
|
|
|
|
mNode(node),
|
|
|
|
mImage(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
GeckoDocumentObject::~GeckoDocumentObject()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GeckoDocumentObject::IsImage()
|
|
|
|
{
|
|
|
|
if(mImage) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
PRUint16 type;
|
|
|
|
rv = mNode->GetNodeType(&type);
|
|
|
|
if(NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMHTMLElement> element = do_QueryInterface(mNode);
|
|
|
|
if ((nsIDOMNode::ELEMENT_NODE == type) && element) {
|
|
|
|
nsString uTag;
|
|
|
|
rv = element->GetLocalName(uTag);
|
|
|
|
if(NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCString tag;
|
|
|
|
NS_UTF16ToCString (uTag, NS_CSTRING_ENCODING_UTF8, tag);
|
|
|
|
|
|
|
|
if (g_ascii_strcasecmp (tag.get(), "img") == 0) {
|
|
|
|
nsCOMPtr <nsIDOMHTMLImageElement> imagePtr;
|
|
|
|
imagePtr = do_QueryInterface(mNode, &rv);
|
|
|
|
if(NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
mImage = imagePtr;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static nsresult
|
|
|
|
NewURI(const char *uri, nsIURI **result)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIServiceManager> mgr;
|
|
|
|
NS_GetServiceManager (getter_AddRefs (mgr));
|
|
|
|
NS_ENSURE_TRUE(mgr, FALSE);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIIOService> ioService;
|
|
|
|
rv = mgr->GetServiceByContractID ("@mozilla.org/network/io-service;1",
|
|
|
|
NS_GET_IID (nsIIOService),
|
|
|
|
getter_AddRefs(ioService));
|
|
|
|
NS_ENSURE_SUCCESS(rv, FALSE);
|
|
|
|
|
|
|
|
nsCString cSpec(uri);
|
|
|
|
return ioService->NewURI (cSpec, nsnull, nsnull, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static nsresult
|
|
|
|
FilenameFromContentDisposition(nsCString contentDisposition, nsCString &fileName)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCString fallbackCharset;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIMIMEHeaderParam> mimehdrpar =
|
|
|
|
do_GetService("@mozilla.org/network/mime-hdrparam;1");
|
|
|
|
NS_ENSURE_TRUE(mimehdrpar, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsString aFileName;
|
|
|
|
rv = mimehdrpar->GetParameter (contentDisposition, "filename",
|
|
|
|
fallbackCharset, PR_TRUE, nsnull,
|
|
|
|
aFileName);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv) || !fileName.Length()) {
|
|
|
|
rv = mimehdrpar->GetParameter (contentDisposition, "name",
|
|
|
|
fallbackCharset, PR_TRUE, nsnull,
|
|
|
|
aFileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv) && fileName.Length()) {
|
|
|
|
NS_UTF16ToCString (aFileName, NS_CSTRING_ENCODING_UTF8, fileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-04-18 14:21:49 +02:00
|
|
|
static nsresult
|
|
|
|
GetImageProperties(char *imgURIStr, nsIProperties **properties)
|
2007-04-17 21:53:34 +02:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> imageURI;
|
|
|
|
rv = NewURI(imgURIStr, getter_AddRefs(imageURI));
|
2007-04-18 14:21:49 +02:00
|
|
|
if(NS_FAILED(rv)) return rv;
|
2007-04-17 21:53:34 +02:00
|
|
|
|
|
|
|
nsCOMPtr<nsIServiceManager> mgr;
|
2007-04-18 14:21:49 +02:00
|
|
|
NS_GetServiceManager(getter_AddRefs(mgr));
|
|
|
|
NS_ENSURE_TRUE(mgr, NS_ERROR_FAILURE);
|
2007-04-17 21:53:34 +02:00
|
|
|
|
|
|
|
nsCOMPtr<imgICache> imgCache;
|
|
|
|
rv = mgr->GetServiceByContractID("@mozilla.org/image/cache;1",
|
2007-04-18 14:21:49 +02:00
|
|
|
NS_GET_IID(imgICache),
|
2007-04-17 21:53:34 +02:00
|
|
|
getter_AddRefs(imgCache));
|
2007-04-18 14:21:49 +02:00
|
|
|
if(NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
imgCache->FindEntryProperties(imageURI, properties);
|
|
|
|
NS_ENSURE_TRUE(mgr, NS_ERROR_FAILURE);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
GeckoDocumentObject::GetImageName()
|
|
|
|
{
|
|
|
|
if(!IsImage()) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(mImageName.Length()) {
|
|
|
|
return g_strdup(mImageName.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
char *imgURIStr = GetImageURI();
|
2007-04-17 21:53:34 +02:00
|
|
|
|
|
|
|
nsCOMPtr<nsIProperties> imgProperties;
|
2007-04-18 14:21:49 +02:00
|
|
|
rv = GetImageProperties(imgURIStr, getter_AddRefs(imgProperties));
|
|
|
|
NS_ENSURE_SUCCESS(rv, NULL);
|
2007-04-17 21:53:34 +02:00
|
|
|
if (imgProperties) {
|
|
|
|
nsCOMPtr<nsISupportsCString> dispositionCString;
|
|
|
|
imgProperties->Get("content-disposition",
|
|
|
|
NS_GET_IID(nsISupportsCString),
|
|
|
|
getter_AddRefs(dispositionCString));
|
|
|
|
if (dispositionCString) {
|
|
|
|
nsCString contentDisposition;
|
|
|
|
dispositionCString->GetData(contentDisposition);
|
|
|
|
FilenameFromContentDisposition(contentDisposition, mImageName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mImageName.Length()) {
|
2007-04-18 14:21:49 +02:00
|
|
|
nsCOMPtr<nsIURI> imageURI;
|
|
|
|
rv = NewURI(imgURIStr, getter_AddRefs(imageURI));
|
|
|
|
NS_ENSURE_SUCCESS(rv, NULL);
|
|
|
|
|
2007-04-17 21:53:34 +02:00
|
|
|
nsCOMPtr<nsIURL> url(do_QueryInterface(imageURI));
|
|
|
|
if (url) {
|
|
|
|
url->GetFileName(mImageName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mImageName.Length() ? g_strdup(mImageName.get()) : NULL;
|
|
|
|
}
|
|
|
|
|
2007-04-18 14:21:49 +02:00
|
|
|
char *
|
|
|
|
GeckoDocumentObject::GetImageMimeType()
|
|
|
|
{
|
|
|
|
if(!IsImage()) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(mImageMimeType.Length()) {
|
|
|
|
return g_strdup(mImageMimeType.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
char *imgURIStr = GetImageURI();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIProperties> imgProperties;
|
|
|
|
rv = GetImageProperties(imgURIStr, getter_AddRefs(imgProperties));
|
|
|
|
NS_ENSURE_SUCCESS(rv, NULL);
|
|
|
|
if (imgProperties) {
|
|
|
|
nsCOMPtr<nsISupportsCString> typeCString;
|
|
|
|
imgProperties->Get("type",
|
|
|
|
NS_GET_IID(nsISupportsCString),
|
|
|
|
getter_AddRefs(typeCString));
|
|
|
|
if (typeCString) {
|
|
|
|
typeCString->GetData(mImageMimeType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mImageMimeType.Length() ? g_strdup(mImageMimeType.get()) : NULL;
|
|
|
|
}
|
|
|
|
|
2007-04-17 21:53:34 +02:00
|
|
|
char *
|
|
|
|
GeckoDocumentObject::GetImageURI()
|
|
|
|
{
|
|
|
|
if(!IsImage()) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!mImageURI.Length()) {
|
|
|
|
nsresult rv;
|
|
|
|
nsString img;
|
|
|
|
rv = mImage->GetSrc(img);
|
|
|
|
if (NS_FAILED(rv)) return NULL;
|
|
|
|
|
|
|
|
NS_UTF16ToCString (img, NS_CSTRING_ENCODING_UTF8, mImageURI);
|
|
|
|
}
|
|
|
|
return g_strdup(mImageURI.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
GeckoDocumentObject::SaveImage(const char *filename)
|
|
|
|
{
|
|
|
|
GeckoBrowserPersist browserPersist(mBrowser);
|
|
|
|
return browserPersist.SaveURI(mImageURI.get(), filename);
|
|
|
|
}
|
2007-04-17 22:46:33 +02:00
|
|
|
|
|
|
|
#endif
|