1 /* ***** BEGIN LICENSE BLOCK *****
  2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3  *
  4  * The contents of this file are subject to the Mozilla Public License Version
  5  * 1.1 (the "License"); you may not use this file except in compliance with
  6  * the License. You may obtain a copy of the License at
  7  * http://www.mozilla.org/MPL/
  8  *
  9  * Software distributed under the License is distributed on an "AS IS" basis,
 10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 11  * for the specific language governing rights and limitations under the
 12  * License.
 13  *
 14  * The Original Code is gContactSync.
 15  *
 16  * The Initial Developer of the Original Code is
 17  * Josh Geenen <gcontactsync@pirules.org>.
 18  * Portions created by the Initial Developer are Copyright (C) 2008-2010
 19  * the Initial Developer. All Rights Reserved.
 20  *
 21  * Contributor(s):
 22  *
 23  * Alternatively, the contents of this file may be used under the terms of
 24  * either the GNU General Public License Version 2 or later (the "GPL"), or
 25  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 26  * in which case the provisions of the GPL or the LGPL are applicable instead
 27  * of those above. If you wish to allow use of your version of this file only
 28  * under the terms of either the GPL or the LGPL, and not to allow others to
 29  * use your version of this file under the terms of the MPL, indicate your
 30  * decision by deleting the provisions above and replace them with the notice
 31  * and other provisions required by the GPL or the LGPL. If you do not delete
 32  * the provisions above, a recipient may use your version of this file under
 33  * the terms of any one of the MPL, the GPL or the LGPL.
 34  *
 35  * ***** END LICENSE BLOCK ***** */
 36 
 37 if (!com) var com = {}; // A generic wrapper variable
 38 // A wrapper for all GCS functions and variables
 39 if (!com.gContactSync) com.gContactSync = {};
 40 
 41 window.addEventListener("load",
 42   /** Initializes the Options class when the window has finished loading */
 43   function gCS_OptionsLoadListener(e) {
 44     com.gContactSync.Options.init();
 45     window.sizeToContent();
 46   },
 47 false);
 48 
 49 /**
 50  * Provides helper functions for the Preferences dialog.
 51  */
 52 com.gContactSync.Options = {
 53   /**
 54    * Initializes the string bundle, FileIO and Preferences scripts and fills the
 55    * login tree.
 56    */
 57   init: function Options_init() {
 58     if (navigator.userAgent.indexOf("SeaMonkey") !== -1) {
 59       document.getElementById("chkEnableSyncBtn").collapsed = false;
 60       document.getElementById("chkForceBtnImage").collapsed = false;
 61     }
 62     // if this is the full preferences dialog add a few event listeners
 63     if (document.getElementById("syncExtended")) {
 64       document.getElementById("syncExtended")
 65               .addEventListener("change", com.gContactSync.Options.enableExtended, false);
 66       com.gContactSync.Options.enableExtended();
 67       document.getElementById("autoSync")
 68               .addEventListener("change", com.gContactSync.Options.enableDelays, false);
 69       com.gContactSync.Options.enableDelays();
 70     }
 71   },
 72   /**
 73    * Enables or disables the extended property textboxes based on the state of
 74    * the syncExtended checkbox.
 75    */
 76   enableExtended: function Options_enableExtended() {
 77     var disableElem = document.getElementById("syncExtended");
 78     if (!disableElem) return false;
 79     var disable = !disableElem.value;
 80     document.getElementById("extended1").disabled  = disable;
 81     document.getElementById("extended2").disabled  = disable;
 82     document.getElementById("extended3").disabled  = disable;
 83     document.getElementById("extended4").disabled  = disable;
 84     document.getElementById("extended5").disabled  = disable;
 85     document.getElementById("extended6").disabled  = disable;
 86     document.getElementById("extended7").disabled  = disable;
 87     document.getElementById("extended8").disabled  = disable;
 88     document.getElementById("extended9").disabled  = disable;
 89     document.getElementById("extended10").disabled = disable;
 90     return true;
 91   },
 92   /**
 93    * Enables or disables the delay textboxes based on the auto sync checkbox.
 94    */
 95   enableDelays: function Options_enableDelays() {
 96     var disableElem  = document.getElementById("autoSync");
 97     var intervalElem = document.getElementById("refreshIntervalBox");
 98     var initialElem  = document.getElementById("initialDelayMinutesBox");
 99     if (!disableElem) return false;
100     if (intervalElem && intervalElem.previousSibling)
101       intervalElem.disabled = intervalElem.previousSibling.disabled = !disableElem.value;
102     if (initialElem && initialElem.previousSibling)
103       initialElem.disabled  = initialElem.previousSibling.disabled  = !disableElem.value;
104     return true;
105   },
106   /**
107    * Deletes old preferences that are no longer required.
108    */
109   cleanOldPrefs: function Options_cleanOldPrefs() {
110     var abBranch    = Components.classes["@mozilla.org/preferences-service;1"]
111                                 .getService(Components.interfaces.nsIPrefService)
112                                 .getBranch("ldap_2.servers."),
113         gAbBranch   = Components.classes["@mozilla.org/preferences-service;1"]
114                                 .getService(Components.interfaces.nsIPrefService)
115                                 .getBranch("extensions.gContactSync.ldap_2.servers."),
116         abs         = com.gContactSync.GAbManager.getAllAddressBooks(),
117         children    = [],
118         count       = {},
119         abPrefIDs   = {},
120         i           = 0,
121         numObsolete = 0,
122         numDeleted  = 0,
123         prefNames   = /(lastSync|gContactSync(Username|lastSync|readOnly|writeOnly|myContacts|myContactsName|Plugin|Disabled|syncGroups|updateGoogleInConflicts|lastBackup|reset|Primary))/,
124         // Step 1: Backup prefs.js
125         prefsFile   = com.gContactSync.FileIO.getProfileDirectory(),
126         backupFile  = com.gContactSync.FileIO.getProfileDirectory();
127     prefsFile.append(com.gContactSync.FileIO.fileNames.PREFS_JS);
128     backupFile.append(com.gContactSync.FileIO.fileNames.FOLDER_NAME);
129     backupFile.append(com.gContactSync.FileIO.fileNames.PREFS_BACKUP_DIR);
130     backupFile.append(new Date().getTime() + "_" +
131                       com.gContactSync.FileIO.fileNames.PREFS_JS + ".bak");
132     com.gContactSync.LOGGER.LOG("***Backing up prefs.js***");
133     com.gContactSync.LOGGER.LOG(" - Destination: " + backupFile.path);
134     com.gContactSync.FileIO.copyFile(prefsFile, backupFile);
135     // Step 2: Clean all gContactSync prefs on ldap_2.servers
136     //         if and only if the extensions.gContactSync.ldap_2.servers. branch
137     //         exists (means that old prefs were already updated)
138     com.gContactSync.LOGGER.LOG("***Finding existing AB preference IDs***");
139     for (i in abs) {
140       var id = abs[i].mDirectory.dirPrefId;
141       com.gContactSync.LOGGER.VERBOSE_LOG(" - " + id);
142       abPrefIDs[id] = abs[i];
143     }
144     com.gContactSync.LOGGER.LOG("***Searching for obsolete prefs on ldap_2.servers.***");
145     children = abBranch.getChildList("", count);
146     for (i = 0; i < count.value; i++) {
147       // extract the preference ID from the whole preference
148       // (ie MyAB_1.filename -> MyAB_1)
149       var index  = children[i].indexOf("."),
150           prefID = index > 0 ? children[i].substring(0, index) : children[i];
151       com.gContactSync.LOGGER.VERBOSE_LOG(" - " + children[i] + " - " + prefID);
152       if (!abPrefIDs["ldap_2.servers." + prefID]) {
153         if (prefNames.test(children[i])) {
154           abBranch.clearUserPref(children[i]);
155           com.gContactSync.LOGGER.LOG("  * Deleted old gContactSync pref");
156           numObsolete++;
157         }
158       }
159     }
160     com.gContactSync.LOGGER.LOG("***Found " + numObsolete + " obsolete prefs on ldap_2.servers.***");
161     // Step 3: clean prefs for deleted ABs on extensions.gContactSync.ldap_2.servers.
162     com.gContactSync.LOGGER.LOG("***Searching for gContactSync prefs for deleted ABs***");
163     children = gAbBranch.getChildList("", count);
164     for (i = 0; i < count.value; i++) {
165       // extract the preference ID from the whole preference
166       // (ie MyAB_1.filename -> MyAB_1)
167       var index  = children[i].indexOf("."),
168           prefID = index > 0 ? children[i].substring(0, index) : children[i];
169       com.gContactSync.LOGGER.VERBOSE_LOG(" - " + children[i] + " - " + prefID);
170       if (!abPrefIDs["ldap_2.servers." + prefID]) {
171         if (prefNames.test(children[i])) {
172           gAbBranch.clearUserPref(children[i]);
173           com.gContactSync.LOGGER.LOG("  * Deleted gContactSync pref for deleted AB");
174           numDeleted++;
175         }
176       }
177     }
178     com.gContactSync.LOGGER.LOG("***Found " + numDeleted + " gContactSync prefs for deleted ABs***");
179     com.gContactSync.alert(com.gContactSync.StringBundle.getStr("finishedPrefClean").replace("%d", numDeleted + numObsolete));
180   }
181 };
182