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 /** 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 parseNames: new com.gContactSync.Pref("parseNames", "bool", true), 189 phoneColLabels: new com.gContactSync.Pref("phoneColLabels", "bool", true), 190 phoneTypes: new com.gContactSync.Pref("phoneTypes", "bool", true), 191 newColLabels: new com.gContactSync.Pref("newColLabels", "bool", true), 192 dummyEmail: new com.gContactSync.Pref("dummyEmail", "bool", true), 193 enableImUrls: new com.gContactSync.Pref("enableImUrls", "bool", true), 194 fixDupContactManagerCSS: new com.gContactSync.Pref("fixDupContactManagerCSS", "bool", false), 195 getPhotos: new com.gContactSync.Pref("getPhotos", "bool", true), 196 sendPhotos: new com.gContactSync.Pref("sendPhotos", "bool", true), 197 addReset: new com.gContactSync.Pref("addReset", "bool", true), 198 statusBarText: new com.gContactSync.Pref("statusBarText", "char", ""), 199 myContactsName: new com.gContactSync.Pref("myContactsName", "char", "My Contacts"), 200 addressBookName: new com.gContactSync.Pref("addressBookName", "char", "Google Contacts"), 201 lastVersionMajor: new com.gContactSync.Pref("lastVersionMajor", "int", 0), 202 lastVersionMinor: new com.gContactSync.Pref("lastVersionMinor", "int", 0), 203 lastVersionRelease: new com.gContactSync.Pref("lastVersionRelease", "int", 0), 204 lastVersionSuffix: new com.gContactSync.Pref("lastVersionSuffix", "char", ""), 205 Plugin: new com.gContactSync.Pref("Plugin", "char", "Google"), 206 Disabled: new com.gContactSync.Pref("Disabled", "char", "false"), 207 updateGoogleInConflicts: new com.gContactSync.Pref("updateGoogleInConflicts", "bool", true), 208 syncAddresses: new com.gContactSync.Pref("syncAddresses", "bool", true), 209 needRestart: new com.gContactSync.Pref("needRestart", "bool", false), 210 synchronizing: new com.gContactSync.Pref("synchronizing", "bool", false), 211 overrideGetCardForEmail: new com.gContactSync.Pref("overrideGetCardForEmail", "bool", true) 212 }, 213 /** 214 * Gets a preference given its branch, name, and type 215 * @param aBranch {nsIPrefBranch} The branch where the preference is stored. 216 * @param aName {string} The name of the preference 217 * @param aType {string} The type of preference. 218 * Must be in Preferences.mTypes. 219 */ 220 getPref: function Preferences_getPref(aBranch, aName, aType) { 221 if (!aBranch) 222 throw "Invalid aBranch parameter supplied to the getPref method" + 223 com.gContactSync.StringBundle.getStr("pleaseReport"); 224 switch (aType) { 225 case this.mTypes.INT: 226 return aBranch.getIntPref(aName); 227 case this.mTypes.BOOL: 228 return aBranch.getBoolPref(aName); 229 case this.mTypes.CHAR: 230 return aBranch.getCharPref(aName); 231 default: 232 throw "Invalid aType parameter supplied to the getPref method" + 233 com.gContactSync.StringBundle.getStr("pleaseReport"); 234 } 235 }, 236 /** 237 * Sets a preference given its branch, name, type and value. 238 * @param aBranch {nsIBranch} The branch where the preference is stored. 239 * @param aName {string} The name of the preference. 240 * @param aType {string} The type of preference. 241 * Must be in Preferences.mTypes. 242 * @param aValue {string} The value to set the preference. 243 */ 244 setPref: function Preferences_setPref(aBranch, aName, aType, aValue) { 245 if (!aBranch) 246 throw "Invalid aBranch parameter supplied to the setPref method" + 247 com.gContactSync.StringBundle.getStr("pleaseReport"); 248 switch (aType) { 249 case this.mTypes.INT: 250 return aBranch.setIntPref(aName, aValue); 251 case this.mTypes.BOOL: 252 return aBranch.setBoolPref(aName, aValue); 253 case this.mTypes.CHAR: 254 return aBranch.setCharPref(aName, aValue); 255 default: 256 throw "Invalid aType parameter supplied to the setPref method" + 257 com.gContactSync.StringBundle.getStr("pleaseReport"); 258 } 259 }, 260 /** 261 * A convienient method of saving a sync preference. 262 * @param aPrefName {string} The preference on the gContactSync branch 263 * to save. 264 * @param aValue {string} The new value for the given preference. 265 */ 266 setSyncPref: function Preferences_setSyncPref(aPrefName, aValue) { 267 var pref = this.mSyncPrefs[aPrefName]; 268 if (!pref) { 269 throw "Error - invalid pref name '" + aPrefName + "'" + 270 " sent to setSyncPref"; 271 } 272 return this.setPref(this.mSyncBranch, pref.label, pref.type, aValue); 273 }, 274 /** 275 * Tries to get each preference in mSyncPrefs and creates the preference and 276 * sets its default value if it is not present. 277 */ 278 getSyncPrefs: function Preferences_getSyncPrefs() { 279 com.gContactSync.LOGGER.LOG("\n***Loading Preferences***"); 280 for (var i in this.mSyncPrefs) { 281 try { 282 this.mSyncPrefs[i].value = this.getPref(this.mSyncBranch, 283 this.mSyncPrefs[i].label, 284 this.mSyncPrefs[i].type); 285 } 286 catch (e) { // if it doesn't exist make it and set the value to its default 287 this.mSyncPrefs[i].value = this.mSyncPrefs[i].defaultValue; 288 this.setPref(this.mSyncBranch, this.mSyncPrefs[i].label, 289 this.mSyncPrefs[i].type, this.mSyncPrefs[i].defaultValue); 290 } 291 com.gContactSync.LOGGER.LOG(" * " + i + ": " + this.mSyncPrefs[i].value); 292 } 293 com.gContactSync.LOGGER.LOG("***Finished Loading Preferences***\n"); 294 // only add these extended properties if the pref to sync them is true 295 this.mExtendedProperties = []; 296 if (this.mSyncPrefs.syncExtended.value) 297 for (var i = 1; i <= 10; i++) 298 this.mExtendedProperties.push(this.getPref(this.mSyncBranch, 299 "extended" + i, 300 this.mTypes.CHAR)); 301 } 302 }; 303