home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Complet / thunderbird / components / nsLDAPPrefsService.js < prev    next >
Encoding:
Text File  |  2004-02-07  |  9.7 KB  |  321 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. /* nsLDAPPrefs service */
  40. function nsLDAPPrefsService() {
  41.   var arrayOfDirectories;
  42.   var j = 0;
  43.   try {
  44.     gPrefInt = Components.classes["@mozilla.org/preferences-service;1"];
  45.     gPrefInt = gPrefInt.getService(nsIPrefBranch);
  46.   }
  47.   catch (ex) {
  48.     dump("failed to get prefs service!\n");
  49.     return;
  50.   }
  51.   /* generate the list of directory servers from preferences */
  52.   var prefCount = {value:0};
  53.   try {
  54.     arrayOfDirectories = this.getServerList(gPrefInt, prefCount);
  55.   }
  56.   catch (ex) {
  57.     arrayOfDirectories = null;
  58.   }
  59.   if (arrayOfDirectories) {
  60.     this.availDirectories = new Array();
  61.     var position;
  62.     var description;
  63.     for (var i = 0; i < prefCount.value; i++)
  64.     {
  65.       if ((arrayOfDirectories[i] != "ldap_2.servers.pab") && 
  66.         (arrayOfDirectories[i] != "ldap_2.servers.history")) {
  67.         try{
  68.           position = gPrefInt.getIntPref(arrayOfDirectories[i]+".position");
  69.         }
  70.         catch(ex){
  71.           position = 1;
  72.         }
  73.         try{
  74.           dirType = gPrefInt.getIntPref(arrayOfDirectories[i]+".dirType");
  75.         }
  76.         catch(ex){
  77.           dirType = 1;
  78.         }
  79.         if ((position != 0) && (dirType == 1)) {
  80.           try{
  81.             description = gPrefInt.getComplexValue(arrayOfDirectories[i]+".description",
  82.                                                    Components.interfaces.nsISupportsString).data;
  83.           }
  84.           catch(ex){
  85.             description = null;
  86.           }
  87.           if (description) {
  88.             this.availDirectories[j] = new Array(2);
  89.             this.availDirectories[j][0] = arrayOfDirectories[i];
  90.             this.availDirectories[j][1] = description;
  91.             j++;
  92.           }
  93.         }
  94.       }
  95.     }
  96.   }
  97.   this.migrate();
  98. }
  99. nsLDAPPrefsService.prototype.prefs_migrated = false;
  100. nsLDAPPrefsService.prototype.availDirectories = null;
  101.  
  102. nsLDAPPrefsService.prototype.QueryInterface =
  103. function (iid) {
  104.  
  105.     if (!iid.equals(nsISupports) &&
  106.         !iid.equals(nsILDAPPrefsService))
  107.         throw Components.results.NS_ERROR_NO_INTERFACE;
  108.  
  109.     return this;
  110. }
  111.  
  112. const prefRoot = "ldap_2.servers";
  113. const parent = "ldap_2.servers.";
  114.  
  115. nsLDAPPrefsService.prototype.getServerList = 
  116. function (prefBranch, aCount) {
  117.   var prefCount = {value:0};
  118.   
  119.   // get all the preferences with prefix ldap_2.servers
  120.   var directoriesList = prefBranch.getChildList(prefRoot, prefCount);
  121.   
  122.   var childList = new Array();
  123.   var count = 0;
  124.   if (directoriesList) {
  125.     directoriesList.sort();
  126.     var prefixLen;
  127.     // lastDirectory contains the last entry that is added to the 
  128.     // array childList.
  129.     var lastDirectory = "";
  130.  
  131.     // only add toplevel prefnames to the list,
  132.     // i.e. add ldap_2.servers.<server-name> 
  133.     // but not ldap_2.servers.<server-name>.foo
  134.     for(i=0; i<prefCount.value; i++) {
  135.       // Assign the prefix ldap_2.servers.<server-name> to directoriesList
  136.       prefixLen = directoriesList[i].indexOf(".", parent.length);
  137.       if (prefixLen != -1) {
  138.         directoriesList[i] = directoriesList[i].substr(0, prefixLen);
  139.         if (directoriesList[i] != lastDirectory) {
  140.           // add the entry to childList 
  141.           // only if it is not added yet
  142.           lastDirectory = directoriesList[i];
  143.           childList[count] = directoriesList[i];
  144.           count++;
  145.         }
  146.       }
  147.     }
  148.   }
  149.  
  150.   if (!count)
  151.   // no preferences with the prefix ldap_2.servers
  152.     throw Components.results.NS_ERROR_FAILURE;
  153.  
  154.   aCount.value = count;
  155.   return childList;
  156. }
  157.  
  158. /* migrate 4.x ldap prefs to mozilla format. 
  159.    Converts hostname, basedn, port to uri (nsLDAPURL).    
  160.  */
  161. nsLDAPPrefsService.prototype.migrate = 
  162. function () {
  163.   var pref_string;
  164.   var ldapUrl=null;
  165.   var enable = false;
  166.   if (this.prefs_migrated) return;
  167.   var gPrefInt = null;
  168.   var host;
  169.   try {
  170.     gPrefInt = Components.classes["@mozilla.org/preferences-service;1"];
  171.     gPrefInt = gPrefInt.getService(Components.interfaces.nsIPrefBranch);
  172.   }
  173.   catch (ex) {
  174.     dump("failed to get prefs service!\n");
  175.     return;
  176.   }
  177.   var migrated = false;
  178.   try{
  179.     migrated = gPrefInt.getBoolPref("ldap_2.prefs_migrated");
  180.   }
  181.   catch(ex){}
  182.   if (migrated){
  183.     this.prefs_migrated = true;
  184.     return;
  185.   }
  186.   try{
  187.     var useDirectory = gPrefInt.getBoolPref("ldap_2.servers.useDirectory");
  188.   }
  189.   catch(ex) {}
  190.   for (var i=0; i < this.availDirectories.length; i++) {
  191.     pref_string = this.availDirectories[i][0];
  192.     try{
  193.       host = gPrefInt.getCharPref(pref_string + ".serverName");
  194.     }
  195.     catch (ex) {
  196.       host = null;
  197.     }
  198.     if (host) {
  199.       try {
  200.         ldapUrl = Components.classes["@mozilla.org/network/ldap-url;1"];
  201.         ldapUrl = ldapUrl.createInstance().QueryInterface(nsILDAPURL);
  202.       }
  203.       catch (ex) {
  204.         dump("failed to get ldap url!\n");
  205.         return;
  206.       }
  207.       ldapUrl.host = host;
  208.       try{
  209.         ldapUrl.dn = gPrefInt.getComplexValue(pref_string + ".searchBase",
  210.                                               nsISupportsString).data;
  211.       }
  212.       catch (ex) {
  213.       }
  214.       var secure = false;
  215.       try {
  216.         secure = gPrefInt.getBoolPref(pref_string + ".isSecure");
  217.       }
  218.       catch(ex) {// if this preference does not exist its ok
  219.       }
  220.       var port;
  221.       if (secure) {
  222.         ldapUrl.options |= ldapurl.OPT_SECURE;
  223.         port = kDefaultSecureLDAPPort;
  224.       }
  225.       else
  226.         port = kDefaultLDAPPort;
  227.       try {
  228.         port = gPrefInt.getIntPref(pref_string + ".port");
  229.       }
  230.       catch(ex) {
  231.         // if this preference does not exist we will use default values.
  232.       }
  233.       ldapUrl.port = port;
  234.       ldapUrl.scope = 2;
  235.  
  236.       var uri = Components.classes["@mozilla.org/supports-string;1"]
  237.                       .createInstance(Components.interfaces.nsISupportsString);
  238.       uri.data = ldapUrl.spec;
  239.       gPrefInt.setComplexValue(pref_string + ".uri", Components.interfaces.nsISupportsString, uri);
  240.  
  241.       /* is this server selected for autocompletion? 
  242.          if yes, convert the preference to mozilla format.
  243.          Atmost one server is selected for autocompletion. 
  244.        */
  245.       if (useDirectory && !enable){
  246.         try {
  247.          enable = gPrefInt.getBoolPref(pref_string + ".autocomplete.enabled");
  248.         } 
  249.         catch(ex) {}
  250.         if (enable) {
  251.           gPrefInt.setCharPref("ldap_2.servers.directoryServer", pref_string);
  252.         }
  253.       }
  254.     }
  255.   }
  256.   try {
  257.     gPrefInt.setBoolPref("ldap_2.prefs_migrated", true);
  258.     var svc = Components.classes["@mozilla.org/preferences-service;1"]
  259.                         .getService(Components.interfaces.nsIPrefService);
  260.     svc.savePrefFile(null);
  261.   }
  262.   catch (ex) {dump ("ERROR:" + ex + "\n");}
  263.     
  264.   this.prefs_migrated = true;
  265. }
  266.  
  267. /* factory for nsLDAPPrefs service (nsLDAPPrefsService) */
  268.  
  269. var nsLDAPPrefsFactory = new Object();
  270.  
  271. nsLDAPPrefsFactory.createInstance =
  272.  
  273. function (outer, iid) {
  274.     if (outer != null)
  275.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  276.  
  277.     if (!iid.equals(nsISupports) && !iid.equals(nsILDAPPrefsService))
  278.         throw Components.results.NS_ERROR_INVALID_ARG;
  279.  
  280.     return new nsLDAPPrefsService();
  281. }
  282.  
  283. var nsLDAPPrefsModule = new Object();
  284. nsLDAPPrefsModule.registerSelf =
  285. function (compMgr, fileSpec, location, type)
  286. {
  287.     compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  288.  
  289.     compMgr.registerFactoryLocation(NS_LDAPPREFSSERVICE_CID,
  290.                                     "nsLDAPPrefs Service",
  291.                                     NS_LDAPPREFSSERVICE_CONTRACTID, 
  292.                                     fileSpec,
  293.                                     location,
  294.                                     type);
  295. }
  296.  
  297. nsLDAPPrefsModule.unregisterSelf =
  298. function(compMgr, fileSpec, location)
  299. {
  300.     compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  301.     compMgr.unregisterFactoryLocation(NS_LDAPPREFSSERVICE_CID, fileSpec);
  302. }
  303.  
  304. nsLDAPPrefsModule.getClassObject =
  305. function (compMgr, cid, iid) {
  306.     if (cid.equals(nsILDAPPrefsService))
  307.         return nsLDAPPrefsFactory;
  308.     throw Components.results.NS_ERROR_NO_INTERFACE;  
  309. }
  310.  
  311. nsLDAPPrefsModule.canUnload =
  312. function(compMgr)
  313. {
  314.     return true;
  315. }
  316.  
  317. /* entrypoint */
  318. function NSGetModule(compMgr, fileSpec) {
  319.     return nsLDAPPrefsModule;
  320. }
  321.