From f26ee4056b4d804a5a841d3d0f1fb959fdaa9bea Mon Sep 17 00:00:00 2001 From: Tomeu Vizoso Date: Wed, 18 Apr 2007 09:21:49 -0300 Subject: [PATCH] Assure dragged images have the right file extension. --- browser/GeckoDocumentObject.cpp | 79 ++++++++++++++++++++++++++------- browser/GeckoDocumentObject.h | 2 + browser/GeckoDragDropHooks.cpp | 30 ++++++++++++- 3 files changed, 95 insertions(+), 16 deletions(-) diff --git a/browser/GeckoDocumentObject.cpp b/browser/GeckoDocumentObject.cpp index fe4a824f..26eb13dc 100644 --- a/browser/GeckoDocumentObject.cpp +++ b/browser/GeckoDocumentObject.cpp @@ -116,6 +116,30 @@ FilenameFromContentDisposition(nsCString contentDisposition, nsCString &fileName return NS_OK; } +static nsresult +GetImageProperties(char *imgURIStr, nsIProperties **properties) +{ + nsresult rv; + + nsCOMPtr imageURI; + rv = NewURI(imgURIStr, getter_AddRefs(imageURI)); + if(NS_FAILED(rv)) return rv; + + nsCOMPtr mgr; + NS_GetServiceManager(getter_AddRefs(mgr)); + NS_ENSURE_TRUE(mgr, NS_ERROR_FAILURE); + + nsCOMPtr imgCache; + rv = mgr->GetServiceByContractID("@mozilla.org/image/cache;1", + NS_GET_IID(imgICache), + getter_AddRefs(imgCache)); + if(NS_FAILED(rv)) return rv; + + imgCache->FindEntryProperties(imageURI, properties); + NS_ENSURE_TRUE(mgr, NS_ERROR_FAILURE); + return NS_OK; +} + char * GeckoDocumentObject::GetImageName() { @@ -123,25 +147,16 @@ GeckoDocumentObject::GetImageName() return NULL; } + if(mImageName.Length()) { + return g_strdup(mImageName.get()); + } + nsresult rv; char *imgURIStr = GetImageURI(); - nsCOMPtr imageURI; - rv = NewURI(imgURIStr, getter_AddRefs(imageURI)); - NS_ENSURE_SUCCESS(rv, NULL); - - nsCOMPtr mgr; - NS_GetServiceManager (getter_AddRefs (mgr)); - NS_ENSURE_TRUE(mgr, NULL); - - nsCOMPtr imgCache; - rv = mgr->GetServiceByContractID("@mozilla.org/image/cache;1", - NS_GET_IID (imgICache), - getter_AddRefs(imgCache)); - NS_ENSURE_SUCCESS(rv, NULL); - nsCOMPtr imgProperties; - imgCache->FindEntryProperties(imageURI, getter_AddRefs(imgProperties)); + rv = GetImageProperties(imgURIStr, getter_AddRefs(imgProperties)); + NS_ENSURE_SUCCESS(rv, NULL); if (imgProperties) { nsCOMPtr dispositionCString; imgProperties->Get("content-disposition", @@ -155,6 +170,10 @@ GeckoDocumentObject::GetImageName() } if (!mImageName.Length()) { + nsCOMPtr imageURI; + rv = NewURI(imgURIStr, getter_AddRefs(imageURI)); + NS_ENSURE_SUCCESS(rv, NULL); + nsCOMPtr url(do_QueryInterface(imageURI)); if (url) { url->GetFileName(mImageName); @@ -164,6 +183,36 @@ GeckoDocumentObject::GetImageName() return mImageName.Length() ? g_strdup(mImageName.get()) : NULL; } +char * +GeckoDocumentObject::GetImageMimeType() +{ + if(!IsImage()) { + return NULL; + } + + if(mImageMimeType.Length()) { + return g_strdup(mImageMimeType.get()); + } + + nsresult rv; + char *imgURIStr = GetImageURI(); + + nsCOMPtr imgProperties; + rv = GetImageProperties(imgURIStr, getter_AddRefs(imgProperties)); + NS_ENSURE_SUCCESS(rv, NULL); + if (imgProperties) { + nsCOMPtr typeCString; + imgProperties->Get("type", + NS_GET_IID(nsISupportsCString), + getter_AddRefs(typeCString)); + if (typeCString) { + typeCString->GetData(mImageMimeType); + } + } + + return mImageMimeType.Length() ? g_strdup(mImageMimeType.get()) : NULL; +} + char * GeckoDocumentObject::GetImageURI() { diff --git a/browser/GeckoDocumentObject.h b/browser/GeckoDocumentObject.h index 05328e55..5f4069fc 100644 --- a/browser/GeckoDocumentObject.h +++ b/browser/GeckoDocumentObject.h @@ -15,6 +15,7 @@ public: bool IsImage(); char *GetImageURI(); char *GetImageName(); + char *GetImageMimeType(); bool SaveImage(const char *filename); private: SugarBrowser *mBrowser; @@ -22,6 +23,7 @@ private: nsCOMPtr mImage; nsCString mImageURI; nsCString mImageName; + nsCString mImageMimeType; protected: /* additional members */ }; diff --git a/browser/GeckoDragDropHooks.cpp b/browser/GeckoDragDropHooks.cpp index d8b0a84b..6439f42c 100644 --- a/browser/GeckoDragDropHooks.cpp +++ b/browser/GeckoDragDropHooks.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include "GeckoDragDropHooks.h" #include "GeckoDocumentObject.h" @@ -66,6 +67,9 @@ UriListDataProvider::GetFlavorData(nsITransferable *aTransferable, nsresult rv = NS_ERROR_NOT_IMPLEMENTED; char *image_name; + char *mime_type; + char *file_ext; + nsCString mime_ext; timeval timestamp; *aData = nsnull; @@ -83,7 +87,31 @@ UriListDataProvider::GetFlavorData(nsITransferable *aTransferable, mFilePath.AppendInt(timestamp.tv_usec); image_name = mDocumentObject->GetImageName(); - mFilePath.Append(image_name); + file_ext = strrchr(image_name, '.'); + mime_type = mDocumentObject->GetImageMimeType(); + + nsCOMPtr mimeService(do_GetService("@mozilla.org/mime;1", + &rv)); + NS_ENSURE_SUCCESS(rv, rv); + rv = mimeService->GetPrimaryExtension(nsCString(mime_type), + EmptyCString(), + mime_ext); + NS_ENSURE_SUCCESS(rv, rv); + + if(!file_ext) { + mFilePath.Append(image_name); + mFilePath.Append("."); + mFilePath.Append(mime_ext); + } else if(strcmp(file_ext, mime_ext.get())) { + image_name[strlen(file_ext)] = 0; + mFilePath.Append(image_name); + mFilePath.Append("."); + mFilePath.Append(mime_ext); + } else { + mFilePath.Append(image_name); + } + + g_free(mime_type); g_free(image_name); if(!mDocumentObject->SaveImage(mFilePath.get())) {