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-2011 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 /** 43 * Registers the pref observer and loads the preferences 44 */ 45 function gCS_PreferencesLoadListener(e) { 46 com.gContactSync.Preferences.register(); 47 com.gContactSync.Preferences.getSyncPrefs(); 48 }, 49 false); 50 51 window.addEventListener("unload", 52 /** 53 * Unregisters the pref observer. 54 */ 55 function gCS_PreferencesUnloadListener(e) { 56 com.gContactSync.Preferences.unregister(); 57 }, 58 false); 59 60 /** 61 * Stores information on Preferences related to gContactSync. 62 * @class 63 */ 64 com.gContactSync.Preferences = { 65 /** The preferences service */ 66 mService: Components.classes["@mozilla.org/preferences-service;1"] 67 .getService(Components.interfaces.nsIPrefService), 68 mBranchName: "extensions.gContactSync.", 69 /** The Preferences branch used by gContactSync */ 70 mSyncBranch: Components.classes["@mozilla.org/preferences-service;1"] 71 .getService(Components.interfaces.nsIPrefService) 72 .getBranch("extensions.gContactSync.") 73 .QueryInterface(Components.interfaces.nsIPrefBranch2), 74 /** An array of the extended properties to use with Google contacts */ 75 mExtendedProperties: [], 76 /** Different types of preferences (bool, int, and char) */ 77 mTypes: { 78 /** Boolean preference */ 79 BOOL: "bool", 80 /** Integer preference */ 81 INT: "int", 82 /** String preference */ 83 CHAR: "char" 84 }, 85 /** Stores whether the preference observer has been registered */ 86 mRegistered: false, 87 /** 88 * Registers the pref observer and gets the initial preference values. 89 */ 90 register: function CP_Preferences_register() { 91 // Add an observer 92 this.mSyncBranch.addObserver("", this, false); 93 this.mRegistered = true; 94 }, 95 /** 96 * Unregisters the pref observer. 97 */ 98 unregister: function CP_Preferences_unregister() { 99 if(!this.mSyncBranch || !this.mRegistered) { 100 return; 101 } 102 com.gContactSync.LOGGER.VERBOSE_LOG("**Unregistering preference observer"); 103 this.mSyncBranch.removeObserver("", this); 104 this.mRegistered = false; 105 }, 106 /** 107 * Called when a preference changes on the extensions.gContactSync. branch. 108 * 109 * @param aSubject {nsIPrefBranch} The branch. 110 * @param aTopic {string} A description of what happened. 111 * @param aData {string} The name of the pref that was changed. 112 */ 113 observe: function(aSubject, aTopic, aData) { 114 if (aTopic != "nsPref:changed") { 115 return; 116 } 117 // TODO - determine the cause of 'com is not defined' errors 118 // this observer shouldn't be registered when com isn't defined. 119 try { 120 com.gContactSync.LOGGER.VERBOSE_LOG("**Observed a preference change: " + aData + " - " + aTopic); 121 } 122 catch (e) { 123 return; 124 } 125 var pref = this.mSyncPrefs[aData]; 126 if (pref) { 127 var oldValue = pref.value; 128 pref.value = this.getPref(this.mSyncBranch, pref.label, pref.type); 129 com.gContactSync.LOGGER.VERBOSE_LOG(" - Old value: '" + oldValue + "'\n" + 130 " - New value: '" + pref.value + "'"); 131 switch (aData) { 132 case "statusBarText": 133 var elem = document.getElementById("gContactSyncStatusText"); 134 if (elem) { 135 elem.label = pref.value; 136 } 137 break; 138 case "enableMenu": 139 var elem = document.getElementById("gContactSyncMenu"); 140 if (elem) { 141 elem.collapsed = !pref.value; 142 } 143 break; 144 } 145 } 146 // if it isn't a sync pref, check if it is a preference for an existing 147 // GAddressBook 148 else { 149 try { 150 var ab = com.gContactSync.GAbManager.mABs[this.mBranchName + aData.substring(0, aData.lastIndexOf(".") + 1)]; 151 if (ab) { 152 var pref = aData.substring(aData.lastIndexOf(".") + 1), 153 prefNoPrefix = pref.replace(ab.prefPrefix, ""); 154 newPrefValue = ab.getStringPref(pref); 155 com.gContactSync.LOGGER.VERBOSE_LOG("Changing AB pref: " + pref + 156 "\nFrom: " + ab.mPrefs[prefNoPrefix] + 157 "\nTo: " + newPrefValue); 158 ab.mPrefs[prefNoPrefix] = newPrefValue; 159 } 160 } 161 catch (ex) {} // ignore errors (GAbManager may not be defined) 162 } 163 }, 164 /** 165 * Preferences related to gContactSync 166 * verboseLog is first since it is used when logging preferences 167 */ 168 mSyncPrefs: { 169 verboseLog: new com.gContactSync.Pref("verboseLog", "bool", true), 170 initialDelayMinutes: new com.gContactSync.Pref("initialDelayMinutes", "int", 5), 171 refreshInterval: new com.gContactSync.Pref("refreshInterval", "int", 120), 172 accountDelay: new com.gContactSync.Pref("accountDelay", "int", 5000), 173 maxContacts: new com.gContactSync.Pref("maxContacts", "int", 5000), 174 backupInterval: new com.gContactSync.Pref("backupInterval", "int", 60), 175 confirmDeleteThreshold: new com.gContactSync.Pref("confirmDeleteThreshold", "int", 5), 176 syncExtended: new com.gContactSync.Pref("syncExtended", "bool", true), 177 overrideCopy: new com.gContactSync.Pref("overrideCopy", "bool", true), 178 autoSync: new com.gContactSync.Pref("autoSync", "bool", true), 179 syncGroups: new com.gContactSync.Pref("syncGroups", "bool", true), 180 removeOldAddresses: new com.gContactSync.Pref("removeOldAddresses", "bool", true), 181 enableSyncBtn: new com.gContactSync.Pref("enableSyncBtn", "bool", true), 182 enableMenu: new com.gContactSync.Pref("enableMenu", "bool", true), 183 enableLogging: new com.gContactSync.Pref("enableLogging", "bool", true), 184 readOnly: new com.gContactSync.Pref("readOnly", "bool", false), 185 writeOnly: new com.gContactSync.Pref("writeOnly", "bool", false), 186 forceBtnImage: new com.gContactSync.Pref("forceBtnImage", "bool", false), 187 myContacts: new com.gContactSync.Pref("myContacts", "bool", false), 188 parseAsianNames: new com.gContactSync.Pref("parseAsianNames", "bool", false), 189 phoneColLabels: new com.gContactSync.Pref("phoneColLabels", "bool", true), 190 phoneTypes: new com.gContactSync.Pref("phoneTypes", "bool", true), 191 swapMobilePager: new com.gContactSync.Pref("swapMobilePager", "bool", true), 192 newColLabels: new com.gContactSync.Pref("newColLabels", "bool", true), 193 dummyEmail: new com.gContactSync.Pref("dummyEmail", "bool", true), 194 enableImUrls: new com.gContactSync.Pref("enableImUrls", "bool", true), 195 fixDupContactManagerCSS: new com.gContactSync.Pref("fixDupContactManagerCSS", "bool", false), 196 getPhotos: new com.gContactSync.Pref("getPhotos", "bool", true), 197 sendPhotos: new com.gContactSync.Pref("sendPhotos", "bool", true), 198 addReset: new com.gContactSync.Pref("addReset", "bool", true), 199 alertSummary: new com.gContactSync.Pref("alertSummary", "bool", true), 200 statusBarText: new com.gContactSync.Pref("statusBarText", "char", ""), 201 myContactsName: new com.gContactSync.Pref("myContactsName", "char", "My Contacts"), 202 addressBookName: new com.gContactSync.Pref("addressBookName", "char", "Google Contacts"), 203 lastVersionMajor: new com.gContactSync.Pref("lastVersionMajor", "int", 0), 204 lastVersionMinor: new com.gContactSync.Pref("lastVersionMinor", "int", 0), 205 lastVersionRelease: new com.gContactSync.Pref("lastVersionRelease", "int", 0), 206 lastVersionSuffix: new com.gContactSync.Pref("lastVersionSuffix", "char", ""), 207 Plugin: new com.gContactSync.Pref("Plugin", "char", "Google"), 208 Disabled: new com.gContactSync.Pref("Disabled", "char", "false"), 209 updateGoogleInConflicts: new com.gContactSync.Pref("updateGoogleInConflicts", "bool", true), 210 syncAddresses: new com.gContactSync.Pref("syncAddresses", "bool", true), 211 needRestart: new com.gContactSync.Pref("needRestart", "bool", false), 212 synchronizing: new com.gContactSync.Pref("synchronizing", "bool", false), 213 overrideGetCardForEmail: new com.gContactSync.Pref("overrideGetCardForEmail", "bool", true), 214 syncPhoneticNames: new com.gContactSync.Pref("syncPhoneticNames", "bool", true) 215 }, 216 /** 217 * Gets a preference given its branch, name, and type 218 * @param aBranch {nsIPrefBranch} The branch where the preference is stored. 219 * @param aName {string} The name of the preference 220 * @param aType {string} The type of preference. 221 * Must be in Preferences.mTypes. 222 */ 223 getPref: function Preferences_getPref(aBranch, aName, aType) { 224 if (!aBranch) 225 throw "Invalid aBranch parameter supplied to the getPref method" + 226 com.gContactSync.StringBundle.getStr("pleaseReport"); 227 switch (aType) { 228 case this.mTypes.INT: 229 return aBranch.getIntPref(aName); 230 case this.mTypes.BOOL: 231 return aBranch.getBoolPref(aName); 232 case this.mTypes.CHAR: 233 return aBranch.getCharPref(aName); 234 default: 235 throw "Invalid aType parameter supplied to the getPref method" + 236 com.gContactSync.StringBundle.getStr("pleaseReport"); 237 } 238 }, 239 /** 240 * Sets a preference given its branch, name, type and value. 241 * @param aBranch {nsIBranch} The branch where the preference is stored. 242 * @param aName {string} The name of the preference. 243 * @param aType {string} The type of preference. 244 * Must be in Preferences.mTypes. 245 * @param aValue {string} The value to set the preference. 246 */ 247 setPref: function Preferences_setPref(aBranch, aName, aType, aValue) { 248 if (!aBranch) 249 throw "Invalid aBranch parameter supplied to the setPref method" + 250 com.gContactSync.StringBundle.getStr("pleaseReport"); 251 switch (aType) { 252 case this.mTypes.INT: 253 return aBranch.setIntPref(aName, aValue); 254 case this.mTypes.BOOL: 255 return aBranch.setBoolPref(aName, aValue); 256 case this.mTypes.CHAR: 257 return aBranch.setCharPref(aName, aValue); 258 default: 259 throw "Invalid aType parameter supplied to the setPref method" + 260 com.gContactSync.StringBundle.getStr("pleaseReport"); 261 } 262 }, 263 /** 264 * A convienient method of saving a sync preference. 265 * @param aPrefName {string} The preference on the gContactSync branch 266 * to save. 267 * @param aValue {string} The new value for the given preference. 268 */ 269 setSyncPref: function Preferences_setSyncPref(aPrefName, aValue) { 270 var pref = this.mSyncPrefs[aPrefName]; 271 if (!pref) { 272 throw "Error - invalid pref name '" + aPrefName + "'" + 273 " sent to setSyncPref"; 274 } 275 return this.setPref(this.mSyncBranch, pref.label, pref.type, aValue); 276 }, 277 /** 278 * Tries to get each preference in mSyncPrefs and creates the preference and 279 * sets its default value if it is not present. 280 */ 281 getSyncPrefs: function Preferences_getSyncPrefs() { 282 com.gContactSync.LOGGER.LOG("\n***Loading Preferences***"); 283 for (var i in this.mSyncPrefs) { 284 try { 285 this.mSyncPrefs[i].value = this.getPref(this.mSyncBranch, 286 this.mSyncPrefs[i].label, 287 this.mSyncPrefs[i].type); 288 } 289 catch (e) { // if it doesn't exist make it and set the value to its default 290 this.mSyncPrefs[i].value = this.mSyncPrefs[i].defaultValue; 291 this.setPref(this.mSyncBranch, this.mSyncPrefs[i].label, 292 this.mSyncPrefs[i].type, this.mSyncPrefs[i].defaultValue); 293 } 294 com.gContactSync.LOGGER.LOG(" * " + i + ": " + this.mSyncPrefs[i].value); 295 } 296 com.gContactSync.LOGGER.LOG("***Finished Loading Preferences***\n"); 297 298 // Only add these extended properties if the pref to sync them is true 299 this.mExtendedProperties = []; 300 if (this.mSyncPrefs.syncExtended.value) { 301 for (var i = 1; i <= 10; i++) { 302 this.mExtendedProperties.push(this.getPref(this.mSyncBranch, 303 "extended" + i, 304 this.mTypes.CHAR)); 305 } 306 } 307 if (!com.gContactSync.Preferences.mSyncPrefs.enableMenu.value && 308 document.getElementById("gContactSyncMenu")) { 309 document.getElementById("gContactSyncMenu").collapsed = true; 310 } 311 } 312 }; 313