From 3cf279ff5012fa0b924cd6e023de720572591a70 Mon Sep 17 00:00:00 2001 From: "bugzilla%standard8.demon.co.uk" Date: Wed, 2 Nov 2005 19:22:37 +0000 Subject: [PATCH] Bug 305434 LDAP Prefs Service needs revising. Separate out the ldif migrate prefs function. r=bienvenu,sr=dmose git-svn-id: svn://10.0.0.236/trunk@184041 18797224-902f-48f8-a5cc-f745e15eee43 --- .../addrbook/content/addressbook.js | 11 +- .../compose/content/MsgComposeCommands.js | 5 +- .../mail/components/preferences/compose.js | 4 + .../prefs/resources/content/pref-directory.js | 4 + .../addrbook/public/nsILDAPPrefsService.idl | 4 +- .../addrbook/resources/content/addressbook.js | 11 +- .../addrbook/src/nsLDAPPrefsService.js | 175 +++++++++--------- .../mailnews/base/src/nsMessengerMigrator.cpp | 3 + .../resources/content/MsgComposeCommands.js | 5 +- 9 files changed, 126 insertions(+), 96 deletions(-) diff --git a/mozilla/mail/components/addrbook/content/addressbook.js b/mozilla/mail/components/addrbook/content/addressbook.js index 48dc9134bbe..b111152b620 100644 --- a/mozilla/mail/components/addrbook/content/addressbook.js +++ b/mozilla/mail/components/addrbook/content/addressbook.js @@ -187,10 +187,15 @@ function delayedOnLoadAddressBook() InitCommonJS(); //This migrates the LDAPServer Preferences from 4.x to mozilla format. + var ldapPrefs = null; try { - Components.classes["@mozilla.org/ldapprefs-service;1"] - .getService(Components.interfaces.nsILDAPPrefsService); - } catch (ex) {dump ("ERROR: Cannot get the LDAP service\n" + ex + "\n");} + ldapPrefs = Components.classes["@mozilla.org/ldapprefs-service;1"] + .getService(Components.interfaces.nsILDAPPrefsService); + } catch (ex) {Components.utils.reportError("ERROR: Cannot get the LDAP service\n" + ex + "\n");} + + if (ldapPrefs) { + ldapPrefs.migratePrefsIfNeeded(); + } GetCurrentPrefs(); diff --git a/mozilla/mail/components/compose/content/MsgComposeCommands.js b/mozilla/mail/components/compose/content/MsgComposeCommands.js index 7eb62e3135d..569e5e3cd10 100644 --- a/mozilla/mail/components/compose/content/MsgComposeCommands.js +++ b/mozilla/mail/components/compose/content/MsgComposeCommands.js @@ -134,7 +134,10 @@ function InitializeGlobalVariables() try { gLDAPPrefsService = gLDAPPrefsService .getService(Components.interfaces.nsILDAPPrefsService); - } catch (ex) {dump ("ERROR: Cannot get the LDAP prefs service\n" + ex + "\n");} + } catch (ex) {Components.utils.reportError("ERROR: Cannot get the LDAP prefs service\n" + ex + "\n");} + if (gLDAPPrefsService) { + gLDAPPrefsService.migratePrefsIfNeeded(); + } } gMsgCompose = null; diff --git a/mozilla/mail/components/preferences/compose.js b/mozilla/mail/components/preferences/compose.js index 48b25aaa986..8f96874f154 100644 --- a/mozilla/mail/components/preferences/compose.js +++ b/mozilla/mail/components/preferences/compose.js @@ -51,6 +51,10 @@ var gComposePane = { if (kLDAPPrefContractID in Components.classes) this.mLDAPPrefsService = Components.classes[kLDAPPrefContractID].getService(Components.interfaces.nsILDAPPrefsService); + if (this.mLDAPPrefsService) { + this.mLDAPPrefsService.migratePrefsIfNeeded(); + } + this.createDirectoriesList(); // build the local address book menu list. We do this by hand instead of using the xul template diff --git a/mozilla/mailnews/addrbook/prefs/resources/content/pref-directory.js b/mozilla/mailnews/addrbook/prefs/resources/content/pref-directory.js index b93d79572e5..a0362e142c4 100644 --- a/mozilla/mailnews/addrbook/prefs/resources/content/pref-directory.js +++ b/mozilla/mailnews/addrbook/prefs/resources/content/pref-directory.js @@ -17,7 +17,11 @@ function initLDAPPrefsService() const LDAP_PREF_CONTRACT="@mozilla.org/ldapprefs-service;1"; if (LDAP_PREF_CONTRACT in Components.classes) + { gLDAPPrefsService = Components.classes[LDAP_PREF_CONTRACT].getService(Components.interfaces.nsILDAPPrefsService); + // Also ensure that 4.x prefences have been migrated + gLDAPPrefsService.migratePrefsIfNeeded(); + } } function onEditDirectories() diff --git a/mozilla/mailnews/addrbook/public/nsILDAPPrefsService.idl b/mozilla/mailnews/addrbook/public/nsILDAPPrefsService.idl index e0338cbc0b6..d4d79d62533 100644 --- a/mozilla/mailnews/addrbook/public/nsILDAPPrefsService.idl +++ b/mozilla/mailnews/addrbook/public/nsILDAPPrefsService.idl @@ -46,7 +46,7 @@ * this service implements migrating ldap prefs from 4.x format * to mozilla format. */ -[scriptable, uuid(5a4911e0-44cd-11d5-9074-0010a4b26cda)] +[scriptable, uuid(37418e42-b5fc-442c-a599-4c8b3131205e)] interface nsILDAPPrefsService: nsISupports { /** @@ -56,7 +56,7 @@ interface nsILDAPPrefsService: nsISupports { * (ldap_2.servers..autocomplete.enabled) * to mozilla format (ldap_2.servers.directoryServer) */ - void migrate(); + void migratePrefsIfNeeded(); /** * This will create an array with entries diff --git a/mozilla/mailnews/addrbook/resources/content/addressbook.js b/mozilla/mailnews/addrbook/resources/content/addressbook.js index b85960a0209..e8cf502f746 100644 --- a/mozilla/mailnews/addrbook/resources/content/addressbook.js +++ b/mozilla/mailnews/addrbook/resources/content/addressbook.js @@ -180,10 +180,15 @@ function OnLoadAddressBook() UpgradeAddressBookResultsPaneUI("mailnews.ui.addressbook_results.version"); //This migrates the LDAPServer Preferences from 4.x to mozilla format. + var ldapPrefs = null; try { - Components.classes["@mozilla.org/ldapprefs-service;1"] - .getService(Components.interfaces.nsILDAPPrefsService); - } catch (ex) {dump ("ERROR: Cannot get the LDAP service\n" + ex + "\n");} + ldapPrefs = Components.classes["@mozilla.org/ldapprefs-service;1"] + .getService(Components.interfaces.nsILDAPPrefsService); + } catch (ex) {Components.utils.reportError("ERROR: Cannot get the LDAP service\n" + ex + "\n");} + + if (ldapPrefs) { + ldapPrefs.migratePrefsIfNeeded(); + } GetCurrentPrefs(); diff --git a/mozilla/mailnews/addrbook/src/nsLDAPPrefsService.js b/mozilla/mailnews/addrbook/src/nsLDAPPrefsService.js index bf3d153e21d..3f6738b8b19 100644 --- a/mozilla/mailnews/addrbook/src/nsLDAPPrefsService.js +++ b/mozilla/mailnews/addrbook/src/nsLDAPPrefsService.js @@ -20,6 +20,7 @@ * * Contributor(s): * Srilatha Moturi + * Mark Banner * * Alternatively, the contents of this file may be used under the terms of * either of the GNU General Public License Version 2 or later (the "GPL"), @@ -40,7 +41,7 @@ const NS_LDAPPREFSSERVICE_CONTRACTID = "@mozilla.org/ldapprefs-service;1"; const NS_LDAPPREFSSERVICE_CID = - Components.ID("{5a4911e0-44cd-11d5-9074-0010a4b26cda}"); + Components.ID("{37418e42-b5fc-442c-a599-4c8b3131205e}"); /* interfaces used in this file */ const nsISupports = Components.interfaces.nsISupports; @@ -51,71 +52,14 @@ const nsILDAPPrefsService = Components.interfaces.nsILDAPPrefsService; const kDefaultLDAPPort = 389; const kDefaultSecureLDAPPort = 636; -var gPrefInt = null; +/* pref branches used in this file */ +const prefRoot = "ldap_2.servers"; +const parent = "ldap_2.servers."; /* nsLDAPPrefs service */ -function nsLDAPPrefsService() { - var arrayOfDirectories; - var j = 0; - try { - gPrefInt = Components.classes["@mozilla.org/preferences-service;1"]; - gPrefInt = gPrefInt.getService(nsIPrefBranch); - } - catch (ex) { - dump("failed to get prefs service!\n"); - return; - } - /* generate the list of directory servers from preferences */ - var prefCount = {value:0}; - try { - arrayOfDirectories = this.getServerList(gPrefInt, prefCount); - } - catch (ex) { - arrayOfDirectories = null; - } - if (arrayOfDirectories) { - this.availDirectories = new Array(); - var position; - var description; - var dirType; - for (var i = 0; i < prefCount.value; i++) - { - if ((arrayOfDirectories[i] != "ldap_2.servers.pab") && - (arrayOfDirectories[i] != "ldap_2.servers.history")) { - try{ - position = gPrefInt.getIntPref(arrayOfDirectories[i]+".position"); - } - catch(ex){ - position = 1; - } - try{ - dirType = gPrefInt.getIntPref(arrayOfDirectories[i]+".dirType"); - } - catch(ex){ - dirType = 1; - } - if ((position != 0) && (dirType == 1)) { - try{ - description = gPrefInt.getComplexValue(arrayOfDirectories[i]+".description", - Components.interfaces.nsISupportsString).data; - } - catch(ex){ - description = null; - } - if (description) { - this.availDirectories[j] = new Array(2); - this.availDirectories[j][0] = arrayOfDirectories[i]; - this.availDirectories[j][1] = description; - j++; - } - } - } - } - } - this.migrate(); -} +function nsLDAPPrefsService() {} + nsLDAPPrefsService.prototype.prefs_migrated = false; -nsLDAPPrefsService.prototype.availDirectories = null; nsLDAPPrefsService.prototype.QueryInterface = function (iid) { @@ -128,9 +72,6 @@ function (iid) { return null; } -const prefRoot = "ldap_2.servers"; -const parent = "ldap_2.servers."; - nsLDAPPrefsService.prototype.getServerList = function (prefBranch, aCount) { var prefCount = {value:0}; @@ -177,38 +118,100 @@ function (prefBranch, aCount) { /* migrate 4.x ldap prefs to mozilla format. Converts hostname, basedn, port to uri (nsLDAPURL). */ -nsLDAPPrefsService.prototype.migrate = +nsLDAPPrefsService.prototype.migratePrefsIfNeeded = function () { - var pref_string; - var ldapUrl=null; - var enable = false; + // Have we already migrated prefs during this instance? if (this.prefs_migrated) return; + + // Now check if the prefs have been migrated. + var prefInt = null; + var pref_string; + var ldapUrl = null; + var enable = false; var host; + try { - gPrefInt = Components.classes["@mozilla.org/preferences-service;1"]; - gPrefInt = gPrefInt.getService(Components.interfaces.nsIPrefBranch); + prefInt = Components.classes["@mozilla.org/preferences-service;1"]; + prefInt = prefInt.getService(Components.interfaces.nsIPrefBranch); } catch (ex) { - dump("failed to get prefs service!\n"); + dump("nsLDAPPrefsService: failed to get prefs service!\n"); return; } + var migrated = false; try{ - migrated = gPrefInt.getBoolPref("ldap_2.prefs_migrated"); + migrated = prefInt.getBoolPref("ldap_2.prefs_migrated"); } catch(ex){} if (migrated){ this.prefs_migrated = true; return; } + + var useDirectory = null; try{ - var useDirectory = gPrefInt.getBoolPref("ldap_2.servers.useDirectory"); + useDirectory = prefInt.getBoolPref("ldap_2.servers.useDirectory"); } catch(ex) {} - for (var i=0; i < this.availDirectories.length; i++) { - pref_string = this.availDirectories[i][0]; + + /* generate the list of directory servers from preferences */ + var prefCount = {value:0}; + var availDirectories = null; + var arrayOfDirectories; + + try { + arrayOfDirectories = this.getServerList(prefInt, prefCount); + } + catch (ex) { + arrayOfDirectories = null; + } + if (arrayOfDirectories) { + var position; + var description; + var dirType; + var j = 0; + + availDirectories = new Array(); + for (var i = 0; i < prefCount.value; i++) + { + if ((arrayOfDirectories[i] != "ldap_2.servers.pab") && + (arrayOfDirectories[i] != "ldap_2.servers.history")) { + try{ + position = prefInt.getIntPref(arrayOfDirectories[i]+".position"); + } + catch(ex){ + position = 1; + } + try{ + dirType = prefInt.getIntPref(arrayOfDirectories[i]+".dirType"); + } + catch(ex){ + dirType = 1; + } + if ((position != 0) && (dirType == 1)) { + try{ + description = prefInt.getComplexValue(arrayOfDirectories[i]+".description", + Components.interfaces.nsISupportsString).data; + } + catch(ex){ + description = null; + } + if (description) { + availDirectories[j] = new Array(2); + availDirectories[j][0] = arrayOfDirectories[i]; + availDirectories[j][1] = description; + j++; + } + } + } + } + } + + for (var i = 0; i < availDirectories.length; i++) { + pref_string = availDirectories[i][0]; try{ - host = gPrefInt.getCharPref(pref_string + ".serverName"); + host = prefInt.getCharPref(pref_string + ".serverName"); } catch (ex) { host = null; @@ -224,14 +227,14 @@ function () { } ldapUrl.host = host; try{ - ldapUrl.dn = gPrefInt.getComplexValue(pref_string + ".searchBase", - nsISupportsString).data; + ldapUrl.dn = prefInt.getComplexValue(pref_string + ".searchBase", + nsISupportsString).data; } catch (ex) { } var secure = false; try { - secure = gPrefInt.getBoolPref(pref_string + ".isSecure"); + secure = prefInt.getBoolPref(pref_string + ".isSecure"); } catch(ex) {// if this preference does not exist its ok } @@ -243,7 +246,7 @@ function () { else port = kDefaultLDAPPort; try { - port = gPrefInt.getIntPref(pref_string + ".port"); + port = prefInt.getIntPref(pref_string + ".port"); } catch(ex) { // if this preference does not exist we will use default values. @@ -254,7 +257,7 @@ function () { var uri = Components.classes["@mozilla.org/supports-string;1"] .createInstance(Components.interfaces.nsISupportsString); uri.data = ldapUrl.spec; - gPrefInt.setComplexValue(pref_string + ".uri", Components.interfaces.nsISupportsString, uri); + prefInt.setComplexValue(pref_string + ".uri", Components.interfaces.nsISupportsString, uri); /* is this server selected for autocompletion? if yes, convert the preference to mozilla format. @@ -262,17 +265,17 @@ function () { */ if (useDirectory && !enable){ try { - enable = gPrefInt.getBoolPref(pref_string + ".autocomplete.enabled"); + enable = prefInt.getBoolPref(pref_string + ".autocomplete.enabled"); } catch(ex) {} if (enable) { - gPrefInt.setCharPref("ldap_2.servers.directoryServer", pref_string); + prefInt.setCharPref("ldap_2.servers.directoryServer", pref_string); } } } } try { - gPrefInt.setBoolPref("ldap_2.prefs_migrated", true); + prefInt.setBoolPref("ldap_2.prefs_migrated", true); var svc = Components.classes["@mozilla.org/preferences-service;1"] .getService(Components.interfaces.nsIPrefService); svc.savePrefFile(null); diff --git a/mozilla/mailnews/base/src/nsMessengerMigrator.cpp b/mozilla/mailnews/base/src/nsMessengerMigrator.cpp index 5b46d9d4944..caf37ab82a5 100644 --- a/mozilla/mailnews/base/src/nsMessengerMigrator.cpp +++ b/mozilla/mailnews/base/src/nsMessengerMigrator.cpp @@ -733,6 +733,9 @@ nsMessengerMigrator::UpgradePrefs() // this will migrate the ldap prefs #if defined(MOZ_LDAP_XPCOM) nsCOMPtr ldapPrefsService = do_GetService("@mozilla.org/ldapprefs-service;1", &rv); + + rv = ldapPrefsService->MigratePrefsIfNeeded(); + NS_ENSURE_SUCCESS(rv, rv); #endif rv = MigrateAddressBookPrefs(); NS_ENSURE_SUCCESS(rv,rv); diff --git a/mozilla/mailnews/compose/resources/content/MsgComposeCommands.js b/mozilla/mailnews/compose/resources/content/MsgComposeCommands.js index 45f7fb0b25c..bb22fc9132a 100644 --- a/mozilla/mailnews/compose/resources/content/MsgComposeCommands.js +++ b/mozilla/mailnews/compose/resources/content/MsgComposeCommands.js @@ -135,7 +135,10 @@ function InitializeGlobalVariables() try { gLDAPPrefsService = gLDAPPrefsService .getService(Components.interfaces.nsILDAPPrefsService); - } catch (ex) {dump ("ERROR: Cannot get the LDAP prefs service\n" + ex + "\n");} + } catch (ex) {Components.utils.reportError("ERROR: Cannot get the LDAP prefs service\n" + ex + "\n");} + if (gLDAPPrefsService) { + gLDAPPrefsService.migratePrefsIfNeeded(); + } } gMsgCompose = null;