Bug 1475865 - Port Bug 1119088 [Set As Desktop Background does not work on OS X] to SeaMonkey. r=stefanh
authorIan Neal <iann_cvs@blueyonder.co.uk>
Sun, 11 Nov 2018 18:05:59 +0100
changeset 33694 46594970c9a26d02610c6fe3ee13834f1f124c2a
parent 33693 5cdc6b2fd2fde4e6f80a3b14d79a3de096ebf98f
child 33695 5e5555a83f3b182074b7ee704848f97eadef2448
push id388
push userclokep@gmail.com
push dateMon, 28 Jan 2019 20:54:56 +0000
reviewersstefanh
bugs1475865, 1119088
Bug 1475865 - Port Bug 1119088 [Set As Desktop Background does not work on OS X] to SeaMonkey. r=stefanh
suite/base/content/nsContextMenu.js
suite/components/shell/content/setDesktopBackground.js
suite/components/shell/nsGNOMEShellService.cpp
suite/components/shell/nsIShellService.idl
suite/components/shell/nsMacShellService.cpp
suite/components/shell/nsWindowsShellService.cpp
--- a/suite/base/content/nsContextMenu.js
+++ b/suite/base/content/nsContextMenu.js
@@ -987,18 +987,21 @@ nsContextMenu.prototype = {
     var where = whereToOpenLink(aEvent);
     if (where == "current")
       openTopWin(this.bgImageURL, doc.defaultView);
     else
       openUILinkIn(this.bgImageURL, where, null, null, doc.documentURIObject);
   },
 
   setDesktopBackground: function() {
+    let url = (new URL(this.target.ownerDocument.location.href)).pathname;
+    let imageName = url.substr(url.lastIndexOf("/") + 1);
     openDialog("chrome://communicator/content/setDesktopBackground.xul",
-               "_blank", "chrome,modal,titlebar,centerscreen", this.target);
+               "_blank", "chrome,modal,titlebar,centerscreen", this.target,
+               imageName);
   },
 
   // Save URL of clicked-on frame.
   saveFrame: function() {
     saveDocument(this.target.ownerDocument, true);
   },
 
   // Save URL of clicked-on link.
--- a/suite/components/shell/content/setDesktopBackground.js
+++ b/suite/components/shell/content/setDesktopBackground.js
@@ -2,22 +2,23 @@
  *
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 var gShell = Cc["@mozilla.org/suite/shell-service;1"]
                .getService(Ci.nsIShellService);
 
-var gImage, gPosition, gPicker, gDesktop;
+var gImage, gImageName, gPosition, gPicker, gDesktop;
 
 function onLoad()
 {
   document.getElementById("itemsBox").hidden = /Mac/.test(navigator.platform);
   gImage = window.arguments[0];
+  gImageName = window.arguments[1];
   gPosition = document.getElementById("position");
   gPicker = document.getElementById("picker");
   gDesktop = document.getElementById("desktop");
 
   sizeToContent();
   window.innerWidth += screen.width / 2 - gDesktop.boxObject.width;
   window.innerHeight += screen.height / 2 - gDesktop.boxObject.height;
 
@@ -35,17 +36,18 @@ function onLoad()
   updatePosition();
 }
 
 function onApply()
 {
   if (!gPicker.parentNode.hidden)
     gShell.desktopBackgroundColor = parseInt(gPicker.color.substr(1), 16);
 
-  gShell.setDesktopBackground(gImage, Ci.nsIShellService[gPosition.value]);
+  gShell.setDesktopBackground(gImage, Ci.nsIShellService[gPosition.value],
+                              gImageName);
 }
 
 function updatePosition()
 {
   gDesktop.style.backgroundPosition = "center";
   gDesktop.style.backgroundRepeat = "no-repeat";
   switch (gPosition.value) {
     case "BACKGROUND_FIT":
--- a/suite/components/shell/nsGNOMEShellService.cpp
+++ b/suite/components/shell/nsGNOMEShellService.cpp
@@ -266,17 +266,19 @@ nsGNOMEShellService::GetCanSetDesktopBac
 {
   nsCOMPtr<nsIGConfService> gconf(do_GetService(NS_GCONFSERVICE_CONTRACTID));
   *aResult = gconf && getenv("GNOME_DESKTOP_SESSION_ID");
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsGNOMEShellService::SetDesktopBackground(dom::Element* aElement,
-                                          int32_t aPosition)
+                                          int32_t aPosition,
+                                          const nsACString& aImageName)
+
 {
   nsCString brandName;
   nsresult rv = GetBrandName(brandName);
   NS_ENSURE_SUCCESS(rv, rv);
 
   // build the file name
   nsCString filePath(PR_GetEnv("HOME"));
   filePath.Append('/');
--- a/suite/components/shell/nsIShellService.idl
+++ b/suite/components/shell/nsIShellService.idl
@@ -5,17 +5,17 @@
 
 
 #include "nsISupports.idl"
 
 interface nsIFile;
 
 webidl Element;
 
-[scriptable, uuid(5a00c44b-e3bd-4769-b021-a83c8212a794)]
+[scriptable, uuid(d7a19d24-9c98-4f88-b11e-52fa8c39ceea)]
 interface nsIShellService : nsISupports
 {
   /**
    * app types we can be registered to handle
    */
   const unsigned short BROWSER = 0x0001;
   const unsigned short MAIL    = 0x0002;
   const unsigned short NEWS    = 0x0004;
@@ -52,20 +52,23 @@ interface nsIShellService : nsISupports
   /**
    * Sets the desktop background image using either the HTML <IMG>
    * element supplied or the background image of the element supplied.
    *
    * @param aImageElement Either a HTML <IMG> element or an element with
    *                      a background image from which to source the
    *                      background image.
    * @param aPosition     How to place the image on the desktop
+   * @param aImageName    The image name. Equivalent to the leaf name of the
+   *                      location.href.
    */
 
   void setDesktopBackground(in Element aElement,
-                            in long aPosition);
+                            in long aPosition,
+                            in ACString aImageName);
 
   /**
    * Flags for positioning/sizing of the Desktop Background image.
    */
   const long BACKGROUND_TILE      = 1;
   const long BACKGROUND_STRETCH   = 2;
   const long BACKGROUND_CENTER    = 3;
   const long BACKGROUND_FILL      = 4;
--- a/suite/components/shell/nsMacShellService.cpp
+++ b/suite/components/shell/nsMacShellService.cpp
@@ -115,55 +115,45 @@ nsMacShellService::isDefaultHandlerForPr
     ::CFRelease(defaultHandlerID);
    }
 
   return isDefault;
 }
 
 NS_IMETHODIMP
 nsMacShellService::SetDesktopBackground(Element* aElement,
-                                        int32_t aPosition)
+                                        int32_t aPosition,
+                                        const nsACString& aImageName)
 {
   // Note: We don't support aPosition on OS X.
 
   // Get the image URI:
   nsresult rv;
   nsCOMPtr<nsIImageLoadingContent> imageContent = do_QueryInterface(aElement, &rv);
   NS_ENSURE_SUCCESS(rv, rv);
   nsCOMPtr<nsIURI> imageURI;
   rv = imageContent->GetCurrentURI(getter_AddRefs(imageURI));
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsIURI *docURI = aElement->OwnerDoc()->GetDocumentURI();
   if (!docURI)
     return NS_ERROR_FAILURE;
 
-  // Get the desired image file name:
-  nsCOMPtr<nsIURL> imageURL(do_QueryInterface(imageURI));
-  if (!imageURL)
-  {
-    // XXXmano (bug 300293): Non-URL images (e.g. the data: protocol) are not
-    // yet supported. What filename should we take here?
-    return NS_ERROR_NOT_IMPLEMENTED;
-  }
-
-  nsAutoCString fileName;
-  imageURL->GetFileName(fileName);
   nsCOMPtr<nsIProperties> fileLocator
     (do_GetService("@mozilla.org/file/directory_service;1", &rv));
   NS_ENSURE_SUCCESS(rv, rv);
 
   // Get the current user's "Pictures" folder (That's ~/Pictures):
   fileLocator->Get(NS_OSX_PICTURE_DOCUMENTS_DIR, NS_GET_IID(nsIFile),
                    getter_AddRefs(mBackgroundFile));
   if (!mBackgroundFile)
     return NS_ERROR_OUT_OF_MEMORY;
 
   nsAutoString fileNameUnicode;
-  CopyUTF8toUTF16(fileName, fileNameUnicode);
+  CopyUTF8toUTF16(aImageName, fileNameUnicode);
 
   // and add the image file name itself:
   mBackgroundFile->Append(fileNameUnicode);
 
   // Download the image; the desktop background will be set in OnStateChange():
   nsCOMPtr<nsIWebBrowserPersist> wbp
     (do_CreateInstance("@mozilla.org/embedding/browser/nsWebBrowserPersist;1", &rv));
   NS_ENSURE_SUCCESS(rv, rv);
--- a/suite/components/shell/nsWindowsShellService.cpp
+++ b/suite/components/shell/nsWindowsShellService.cpp
@@ -586,17 +586,18 @@ WriteBitmap(nsIFile* aFile, imgIContaine
 
   dataSurface->Unmap();
 
   return rv;
 }
 
 NS_IMETHODIMP
 nsWindowsShellService::SetDesktopBackground(dom::Element* aElement,
-                                            int32_t aPosition)
+                                            int32_t aPosition,
+                                            const nsACString& aImageName)
 {
   nsCOMPtr<nsIContent> content(do_QueryInterface(aElement));
   if (!aElement || !aElement->IsHTMLElement(nsGkAtoms::img)) {
     // XXX write background loading stuff!
     return NS_ERROR_NOT_AVAILABLE;
   }
 
   nsresult rv;