home *** CD-ROM | disk | FTP | other *** search
/ com!online 2005 May / com_0505_1.iso / opensource / Thunderbird Setup 1.0.exe / mail.xpi / bin / components / nsLDAPPrefsService.js < prev    next >
Encoding:
Text File  |  2004-12-06  |  9.4 KB  |  323 lines

  1. /* 
  2.  * The contents of this file are subject to the Mozilla Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/MPL/
  6.  * 
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  * 
  12.  * The Original Code is mozilla.org code.
  13.  * 
  14.  * The Initial Developer of the Original Code is Netscape
  15.  * Communications Corporation.  Portions created by Netscape are 
  16.  * Copyright (C) 2001 Netscape Communications Corporation.  All
  17.  * Rights Reserved.
  18.  * 
  19.  * Contributor(s): 
  20.  * Srilatha Moturi <srilatha@netscape.com>
  21.  */
  22.  
  23. /* components defined in this file */
  24.  
  25. const NS_LDAPPREFSSERVICE_CONTRACTID =
  26.     "@mozilla.org/ldapprefs-service;1";
  27. const NS_LDAPPREFSSERVICE_CID =
  28.     Components.ID("{5a4911e0-44cd-11d5-9074-0010a4b26cda}");
  29.  
  30. /* interfaces used in this file */
  31. const nsISupports        = Components.interfaces.nsISupports;
  32. const nsISupportsString  = Components.interfaces.nsISupportsString;
  33. const nsIPrefBranch      = Components.interfaces.nsIPrefBranch;
  34. const nsILDAPURL         = Components.interfaces.nsILDAPURL;
  35. const nsILDAPPrefsService = Components.interfaces.nsILDAPPrefsService;
  36. const kDefaultLDAPPort = 389;
  37. const kDefaultSecureLDAPPort = 636;
  38.  
  39. var gPrefInt = null;
  40.  
  41. /* nsLDAPPrefs service */
  42. function nsLDAPPrefsService() {
  43.   var arrayOfDirectories;
  44.   var j = 0;
  45.   try {
  46.     gPrefInt = Components.classes["@mozilla.org/preferences-service;1"];
  47.     gPrefInt = gPrefInt.getService(nsIPrefBranch);
  48.   }
  49.   catch (ex) {
  50.     dump("failed to get prefs service!\n");
  51.     return;
  52.   }
  53.   /* generate the list of directory servers from preferences */
  54.   var prefCount = {value:0};
  55.   try {
  56.     arrayOfDirectories = this.getServerList(gPrefInt, prefCount);
  57.   }
  58.   catch (ex) {
  59.     arrayOfDirectories = null;
  60.   }
  61.   if (arrayOfDirectories) {
  62.     this.availDirectories = new Array();
  63.     var position;
  64.     var description;
  65.     var dirType;
  66.     for (var i = 0; i < prefCount.value; i++)
  67.     {
  68.       if ((arrayOfDirectories[i] != "ldap_2.servers.pab") && 
  69.         (arrayOfDirectories[i] != "ldap_2.servers.history")) {
  70.         try{
  71.           position = gPrefInt.getIntPref(arrayOfDirectories[i]+".position");
  72.         }
  73.         catch(ex){
  74.           position = 1;
  75.         }
  76.         try{
  77.           dirType = gPrefInt.getIntPref(arrayOfDirectories[i]+".dirType");
  78.         }
  79.         catch(ex){
  80.           dirType = 1;
  81.         }
  82.         if ((position != 0) && (dirType == 1)) {
  83.           try{
  84.             description = gPrefInt.getComplexValue(arrayOfDirectories[i]+".description",
  85.                                                    Components.interfaces.nsISupportsString).data;
  86.           }
  87.           catch(ex){
  88.             description = null;
  89.           }
  90.           if (description) {
  91.             this.availDirectories[j] = new Array(2);
  92.             this.availDirectories[j][0] = arrayOfDirectories[i];
  93.             this.availDirectories[j][1] = description;
  94.             j++;
  95.           }
  96.         }
  97.       }
  98.     }
  99.   }
  100.   this.migrate();
  101. }
  102. nsLDAPPrefsService.prototype.prefs_migrated = false;
  103. nsLDAPPrefsService.prototype.availDirectories = null;
  104.  
  105. nsLDAPPrefsService.prototype.QueryInterface =
  106. function (iid) {
  107.  
  108.     if (!iid.equals(nsISupports) &&
  109.         !iid.equals(nsILDAPPrefsService))
  110.         throw Components.results.NS_ERROR_NO_INTERFACE;
  111.  
  112.     return this;
  113. }
  114.  
  115. const prefRoot = "ldap_2.servers";
  116. const parent = "ldap_2.servers.";
  117.  
  118. nsLDAPPrefsService.prototype.getServerList = 
  119. function (prefBranch, aCount) {
  120.   var prefCount = {value:0};
  121.   
  122.   // get all the preferences with prefix ldap_2.servers
  123.   var directoriesList = prefBranch.getChildList(prefRoot, prefCount);
  124.   
  125.   var childList = new Array();
  126.   var count = 0;
  127.   if (directoriesList) {
  128.     directoriesList.sort();
  129.     var prefixLen;
  130.     // lastDirectory contains the last entry that is added to the 
  131.     // array childList.
  132.     var lastDirectory = "";
  133.  
  134.     // only add toplevel prefnames to the list,
  135.     // i.e. add ldap_2.servers.<server-name> 
  136.     // but not ldap_2.servers.<server-name>.foo
  137.     for(var i=0; i<prefCount.value; i++) {
  138.       // Assign the prefix ldap_2.servers.<server-name> to directoriesList
  139.       prefixLen = directoriesList[i].indexOf(".", parent.length);
  140.       if (prefixLen != -1) {
  141.         directoriesList[i] = directoriesList[i].substr(0, prefixLen);
  142.         if (directoriesList[i] != lastDirectory) {
  143.           // add the entry to childList 
  144.           // only if it is not added yet
  145.           lastDirectory = directoriesList[i];
  146.           childList[count] = directoriesList[i];
  147.           count++;
  148.         }
  149.       }
  150.     }
  151.   }
  152.  
  153.   if (!count)
  154.   // no preferences with the prefix ldap_2.servers
  155.     throw Components.results.NS_ERROR_FAILURE;
  156.  
  157.   aCount.value = count;
  158.   return childList;
  159. }
  160.  
  161. /* migrate 4.x ldap prefs to mozilla format. 
  162.    Converts hostname, basedn, port to uri (nsLDAPURL).    
  163.  */
  164. nsLDAPPrefsService.prototype.migrate = 
  165. function () {
  166.   var pref_string;
  167.   var ldapUrl=null;
  168.   var enable = false;
  169.   if (this.prefs_migrated) return;
  170.   var host;
  171.   try {
  172.     gPrefInt = Components.classes["@mozilla.org/preferences-service;1"];
  173.     gPrefInt = gPrefInt.getService(Components.interfaces.nsIPrefBranch);
  174.   }
  175.   catch (ex) {
  176.     dump("failed to get prefs service!\n");
  177.     return;
  178.   }
  179.   var migrated = false;
  180.   try{
  181.     migrated = gPrefInt.getBoolPref("ldap_2.prefs_migrated");
  182.   }
  183.   catch(ex){}
  184.   if (migrated){
  185.     this.prefs_migrated = true;
  186.     return;
  187.   }
  188.   try{
  189.     var useDirectory = gPrefInt.getBoolPref("ldap_2.servers.useDirectory");
  190.   }
  191.   catch(ex) {}
  192.   for (var i=0; i < this.availDirectories.length; i++) {
  193.     pref_string = this.availDirectories[i][0];
  194.     try{
  195.       host = gPrefInt.getCharPref(pref_string + ".serverName");
  196.     }
  197.     catch (ex) {
  198.       host = null;
  199.     }
  200.     if (host) {
  201.       try {
  202.         ldapUrl = Components.classes["@mozilla.org/network/ldap-url;1"];
  203.         ldapUrl = ldapUrl.createInstance().QueryInterface(nsILDAPURL);
  204.       }
  205.       catch (ex) {
  206.         dump("failed to get ldap url!\n");
  207.         return;
  208.       }
  209.       ldapUrl.host = host;
  210.       try{
  211.         ldapUrl.dn = gPrefInt.getComplexValue(pref_string + ".searchBase",
  212.                                               nsISupportsString).data;
  213.       }
  214.       catch (ex) {
  215.       }
  216.       var secure = false;
  217.       try {
  218.         secure = gPrefInt.getBoolPref(pref_string + ".isSecure");
  219.       }
  220.       catch(ex) {// if this preference does not exist its ok
  221.       }
  222.       var port;
  223.       if (secure) {
  224.         ldapUrl.options |= ldapurl.OPT_SECURE;
  225.         port = kDefaultSecureLDAPPort;
  226.       }
  227.       else
  228.         port = kDefaultLDAPPort;
  229.       try {
  230.         port = gPrefInt.getIntPref(pref_string + ".port");
  231.       }
  232.       catch(ex) {
  233.         // if this preference does not exist we will use default values.
  234.       }
  235.       ldapUrl.port = port;
  236.       ldapUrl.scope = 2;
  237.  
  238.       var uri = Components.classes["@mozilla.org/supports-string;1"]
  239.                       .createInstance(Components.interfaces.nsISupportsString);
  240.       uri.data = ldapUrl.spec;
  241.       gPrefInt.setComplexValue(pref_string + ".uri", Components.interfaces.nsISupportsString, uri);
  242.  
  243.       /* is this server selected for autocompletion? 
  244.          if yes, convert the preference to mozilla format.
  245.          Atmost one server is selected for autocompletion. 
  246.        */
  247.       if (useDirectory && !enable){
  248.         try {
  249.          enable = gPrefInt.getBoolPref(pref_string + ".autocomplete.enabled");
  250.         } 
  251.         catch(ex) {}
  252.         if (enable) {
  253.           gPrefInt.setCharPref("ldap_2.servers.directoryServer", pref_string);
  254.         }
  255.       }
  256.     }
  257.   }
  258.   try {
  259.     gPrefInt.setBoolPref("ldap_2.prefs_migrated", true);
  260.     var svc = Components.classes["@mozilla.org/preferences-service;1"]
  261.                         .getService(Components.interfaces.nsIPrefService);
  262.     svc.savePrefFile(null);
  263.   }
  264.   catch (ex) {dump ("ERROR:" + ex + "\n");}
  265.     
  266.   this.prefs_migrated = true;
  267. }
  268.  
  269. /* factory for nsLDAPPrefs service (nsLDAPPrefsService) */
  270.  
  271. var nsLDAPPrefsFactory = new Object();
  272.  
  273. nsLDAPPrefsFactory.createInstance =
  274.  
  275. function (outer, iid) {
  276.     if (outer != null)
  277.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  278.  
  279.     if (!iid.equals(nsISupports) && !iid.equals(nsILDAPPrefsService))
  280.         throw Components.results.NS_ERROR_INVALID_ARG;
  281.  
  282.     return new nsLDAPPrefsService();
  283. }
  284.  
  285. var nsLDAPPrefsModule = new Object();
  286. nsLDAPPrefsModule.registerSelf =
  287. function (compMgr, fileSpec, location, type)
  288. {
  289.     compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  290.  
  291.     compMgr.registerFactoryLocation(NS_LDAPPREFSSERVICE_CID,
  292.                                     "nsLDAPPrefs Service",
  293.                                     NS_LDAPPREFSSERVICE_CONTRACTID, 
  294.                                     fileSpec,
  295.                                     location,
  296.                                     type);
  297. }
  298.  
  299. nsLDAPPrefsModule.unregisterSelf =
  300. function(compMgr, fileSpec, location)
  301. {
  302.     compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  303.     compMgr.unregisterFactoryLocation(NS_LDAPPREFSSERVICE_CID, fileSpec);
  304. }
  305.  
  306. nsLDAPPrefsModule.getClassObject =
  307. function (compMgr, cid, iid) {
  308.     if (cid.equals(nsILDAPPrefsService))
  309.         return nsLDAPPrefsFactory;
  310.     throw Components.results.NS_ERROR_NO_INTERFACE;  
  311. }
  312.  
  313. nsLDAPPrefsModule.canUnload =
  314. function(compMgr)
  315. {
  316.     return true;
  317. }
  318.  
  319. /* entrypoint */
  320. function NSGetModule(compMgr, fileSpec) {
  321.     return nsLDAPPrefsModule;
  322. }
  323.