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