diff --git a/lib/python/_sugar.override b/lib/python/_sugar.override index 5a67260f..303b378f 100644 --- a/lib/python/_sugar.override +++ b/lib/python/_sugar.override @@ -186,8 +186,8 @@ _wrap_sugar_browser_event_tp_getattr(PyObject *self, char *attr) SugarBrowserEvent *event = pyg_boxed_get(self, SugarBrowserEvent); if (!strcmp(attr, "__members__")) - return Py_BuildValue("[ss]", "image_uri", "button"); - if (!strcmp(attr, "image_uri")) { + return Py_BuildValue("[sss]", "image_uri", "button", "image_name"); + else if (!strcmp(attr, "image_uri")) { if (event->image_uri) { return PyString_FromString(event->image_uri); } else { @@ -195,6 +195,14 @@ _wrap_sugar_browser_event_tp_getattr(PyObject *self, char *attr) return Py_None; } } + else if (!strcmp(attr, "image_name")) { + if (event->image_name) { + return PyString_FromString(event->image_name); + } else { + Py_INCREF(Py_None); + return Py_None; + } + } else if (!strcmp(attr, "button")) return PyInt_FromLong(event->button); diff --git a/lib/src/Makefile.am b/lib/src/Makefile.am index cf0a6d66..668a8212 100644 --- a/lib/src/Makefile.am +++ b/lib/src/Makefile.am @@ -6,6 +6,7 @@ libsugarprivate_la_CPPFLAGS = \ -I$(MOZILLA_INCLUDE_DIR)/dom \ -I$(MOZILLA_INCLUDE_DIR)/exthandler \ -I$(MOZILLA_INCLUDE_DIR)/gtkembedmoz \ + -I$(MOZILLA_INCLUDE_DIR)/imglib2 \ -I$(MOZILLA_INCLUDE_DIR)/mimetype \ -I$(MOZILLA_INCLUDE_DIR)/necko \ -I$(MOZILLA_INCLUDE_DIR)/pref \ diff --git a/lib/src/sugar-browser.cpp b/lib/src/sugar-browser.cpp index 1e2a2601..3f0a1c3e 100644 --- a/lib/src/sugar-browser.cpp +++ b/lib/src/sugar-browser.cpp @@ -44,6 +44,10 @@ #include #include #include +#include +#include +#include +#include enum { PROP_0, @@ -376,6 +380,85 @@ location_cb(GtkMozEmbed *embed) update_navigation_properties(browser); } +static nsresult +NewURI(const char *uri, nsIURI **result) +{ + nsresult rv; + + nsCOMPtr mgr; + NS_GetServiceManager (getter_AddRefs (mgr)); + NS_ENSURE_TRUE(mgr, FALSE); + + nsCOMPtr 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 char * +get_image_name_from_cache(const char *uri) +{ + nsresult rv; + + g_print(uri); + + 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 imgURI; + rv = NewURI(uri, getter_AddRefs(imgURI)); + NS_ENSURE_SUCCESS(rv, NULL); + + nsCOMPtr imgProperties; + imgCache->FindEntryProperties(imgURI, getter_AddRefs(imgProperties)); + if (imgProperties) { + nsCOMPtr dispositionCString; + imgProperties->Get("content-disposition", + NS_GET_IID(nsISupportsCString), + getter_AddRefs(dispositionCString)); + if (dispositionCString) { + nsCString fallbackCharset; + + nsCString contentDisposition; + dispositionCString->GetData(contentDisposition); + + nsCOMPtr mimehdrpar = + do_GetService("@mozilla.org/network/mime-hdrparam;1"); + NS_ENSURE_TRUE(mimehdrpar, NULL); + + nsString fileName; + rv = mimehdrpar->GetParameter (contentDisposition, "filename", + fallbackCharset, PR_TRUE, nsnull, + fileName); + + if (NS_FAILED(rv) || !fileName.Length()) { + rv = mimehdrpar->GetParameter (contentDisposition, "name", + fallbackCharset, PR_TRUE, nsnull, + fileName); + } + + if (NS_SUCCEEDED(rv) && fileName.Length()) { + nsCString cFileName; + NS_UTF16ToCString (fileName, NS_CSTRING_ENCODING_UTF8, cFileName); + return g_strdup(cFileName.get()); + } + } + } + + return NULL; +} + static gboolean dom_mouse_click_cb(GtkMozEmbed *embed, nsIDOMMouseEvent *mouseEvent) { @@ -421,6 +504,7 @@ dom_mouse_click_cb(GtkMozEmbed *embed, nsIDOMMouseEvent *mouseEvent) nsCString cImg; NS_UTF16ToCString (img, NS_CSTRING_ENCODING_UTF8, cImg); event->image_uri = g_strdup(cImg.get()); + event->image_name = get_image_name_from_cache(event->image_uri); } } @@ -494,25 +578,6 @@ sugar_browser_grab_focus(SugarBrowser *browser) } } -nsresult -NewURI(const char *uri, nsIURI **result) -{ - nsresult rv; - - nsCOMPtr mgr; - NS_GetServiceManager (getter_AddRefs (mgr)); - NS_ENSURE_TRUE(mgr, FALSE); - - nsCOMPtr 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); -} - gboolean sugar_browser_save_uri(SugarBrowser *browser, const char *uri, @@ -626,9 +691,16 @@ sugar_browser_event_new(void) SugarBrowserEvent * sugar_browser_event_copy(SugarBrowserEvent *event) { + SugarBrowserEvent *copy; + g_return_val_if_fail(event != NULL, NULL); - return (SugarBrowserEvent *)g_memdup(event, sizeof(SugarBrowserEvent)); + copy = g_new0(SugarBrowserEvent, 1); + copy->button = event->button; + copy->image_uri = g_strdup(event->image_uri); + copy->image_name = g_strdup(event->image_name); + + return copy; } void @@ -639,6 +711,9 @@ sugar_browser_event_free(SugarBrowserEvent *event) if (event->image_uri) { g_free(event->image_uri); } + if (event->image_name) { + g_free(event->image_name); + } g_free(event); } diff --git a/lib/src/sugar-browser.h b/lib/src/sugar-browser.h index a598b9b9..fbce8751 100644 --- a/lib/src/sugar-browser.h +++ b/lib/src/sugar-browser.h @@ -78,6 +78,7 @@ void sugar_browser_shutdown (void); struct _SugarBrowserEvent { int button; char *image_uri; + char *image_name; }; GType sugar_browser_event_get_type (void);