Bug 448105 – Provide a pref transform method to migrate a string to a URL spec
authorIan Neal <iann_cvs@blueyonder.co.uk>
Tue, 29 Jul 2008 21:51:18 +0100
changeset 43 c295f23e75c682276c0683de8d492b1823883ce4
parent 42 46d3979475c92c72bca135ce2a301c2957ef6e22
child 44 c09178986e0a22a2b8542c4c867827e18c928732
push idunknown
push userunknown
push dateunknown
bugs448105
Bug 448105 – Provide a pref transform method to migrate a string to a URL spec p=me r=standard8 sr=neil
suite/profile/migration/src/nsNetscapeProfileMigratorBase.cpp
suite/profile/migration/src/nsNetscapeProfileMigratorBase.h
--- a/suite/profile/migration/src/nsNetscapeProfileMigratorBase.cpp
+++ b/suite/profile/migration/src/nsNetscapeProfileMigratorBase.cpp
@@ -15,16 +15,17 @@
  * The Original Code is The Browser Profile Migrator.
  *
  * The Initial Developer of the Original Code is Ben Goodger.
  * Portions created by the Initial Developer are Copyright (C) 2004
  * the Initial Developer. All Rights Reserved.
  *
  * Contributor(s):
  *  Ben Goodger <ben@bengoodger.com>
+ *  Ian Neal <iann_bugzilla@blueyonder.co.uk>
  *
  * Alternatively, the contents of this file may be used under the terms of
  * either the GNU General Public License Version 2 or later (the "GPL"), or
  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  * in which case the provisions of the GPL or the LGPL are applicable instead
  * of those above. If you wish to allow use of your version of this file only
  * under the terms of either the GPL or the LGPL, and not to allow others to
  * use your version of this file under the terms of the MPL, indicate your
@@ -344,16 +345,58 @@ nsNetscapeProfileMigratorBase::GetInt(Pr
 nsresult
 nsNetscapeProfileMigratorBase::SetInt(PrefTransform* aTransform,
                                       nsIPrefBranch* aBranch)
 {
   SETPREF(aTransform, SetIntPref, aTransform->intValue)
 }
 
 nsresult
+nsNetscapeProfileMigratorBase::SetFile(PrefTransform* aTransform,
+                                       nsIPrefBranch* aBranch)
+{
+  // In this case targetPrefName is just an additional preference
+  // that needs to be modified and not what the sourcePrefName is
+  // going to be saved to once it is modified.
+  nsresult rv = NS_OK;
+  if (aTransform->prefHasValue) {
+    nsCString fileURL(aTransform->stringValue);
+    nsCOMPtr<nsIFile> aFile;
+    // Start off by assuming fileURL is a URL spec and
+    // try and get a File from it.
+    rv = NS_GetFileFromURLSpec(fileURL, getter_AddRefs(aFile));
+    if (NS_FAILED(rv)) {
+      // Okay it wasn't a URL spec so assume it is a localfile,
+      // if this fails then just don't set anything.
+      nsCOMPtr<nsILocalFile> localFile;
+      rv = NS_NewNativeLocalFile(fileURL, PR_FALSE, getter_AddRefs(localFile));
+      if (NS_FAILED(rv))
+        return NS_OK;  
+      aFile = localFile;
+    }
+    // Now test to see if File exists and is an actual file.
+    PRBool exists = PR_FALSE;
+    rv = aFile->Exists(&exists);
+    if (NS_SUCCEEDED(rv) && exists)
+      rv = aFile->IsFile(&exists);
+
+    if (NS_SUCCEEDED(rv) && exists) {
+      // After all that let's just get the URL spec and set the pref to it.
+      rv = NS_GetURLSpecFromFile(aFile, fileURL);
+      if (NS_FAILED(rv))
+        return NS_OK;
+      rv = aBranch->SetCharPref(aTransform->sourcePrefName, fileURL.get());
+      if (NS_SUCCEEDED(rv) && aTransform->targetPrefName)
+        rv = aBranch->SetIntPref(aTransform->targetPrefName, 1);
+    }
+  }
+  return rv;
+}
+
+nsresult
 nsNetscapeProfileMigratorBase::SetImage(PrefTransform* aTransform,
                                         nsIPrefBranch* aBranch)
 {
   if (aTransform->prefHasValue)
     // This transforms network.image.imageBehavior into
     // permissions.default.image
     return aBranch->SetIntPref("permissions.default.image",
                         aTransform->intValue == 1 ? 3 :
--- a/suite/profile/migration/src/nsNetscapeProfileMigratorBase.h
+++ b/suite/profile/migration/src/nsNetscapeProfileMigratorBase.h
@@ -120,16 +120,17 @@ public:
 
   // Pref Transform Methods
   static nsresult GetString(PrefTransform* aTransform, nsIPrefBranch* aBranch);
   static nsresult SetString(PrefTransform* aTransform, nsIPrefBranch* aBranch);
   static nsresult GetBool(PrefTransform* aTransform, nsIPrefBranch* aBranch);
   static nsresult SetBool(PrefTransform* aTransform, nsIPrefBranch* aBranch);
   static nsresult GetInt(PrefTransform* aTransform, nsIPrefBranch* aBranch);
   static nsresult SetInt(PrefTransform* aTransform, nsIPrefBranch* aBranch);
+  static nsresult SetFile(PrefTransform* aTransform, nsIPrefBranch* aBranch);
   static nsresult SetImage(PrefTransform* aTransform, nsIPrefBranch* aBranch);
   static nsresult SetCookie(PrefTransform* aTransform, nsIPrefBranch* aBranch);
   // XXX Bug 381157 When suite uses Toolkit's DM backend, we need to
   // activate this code.
 #ifdef SUITE_USING_TOOLKIT_DM
   static nsresult SetDownloadManager(PrefTransform* aTransform,
                                      nsIPrefBranch* aBranch);
 #endif