Changeset 999
- Timestamp:
- 07/30/08 00:23:11 (1 month ago)
- Files:
-
- trunk/components/filterManager.js (modified) (15 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/components/filterManager.js
r985 r999 43 43 44 44 const Exception = Components.Exception; 45 const BASE = 'extensions.dta.filters.'; 45 46 46 47 const NS_ERROR_NO_INTERFACE = Cr.NS_ERROR_NO_INTERFACE; … … 51 52 const LINK_FILTER = Ci.dtaIFilter.LINK_FILTER; 52 53 const IMAGE_FILTER = Ci.dtaIFilter.IMAGE_FILTER; 54 const TOPIC_FILTERSCHANGED = 'DTA:filterschanged'; 55 56 const nsITimer = Ci.nsITimer; 57 const Timer = Components.Constructor('@mozilla.org/timer;1', 'nsITimer', 'init'); 58 59 let Preferences = {}; 53 60 54 61 // no not create DTA_Filter yourself, managed by DTA_FilterManager 55 function Filter(name , prefs) {62 function Filter(name) { 56 63 this._id = name; 57 this._prefs = prefs;58 64 } 59 65 Filter.prototype = { … … 179 185 load: function F_load(localizedLabel) { 180 186 this._localizedLabel = localizedLabel; 181 this._label = this.getMultiBytePref(this.pref('label'));187 this._label = Preferences.get(this.pref('label')); 182 188 if (!this._label || !this._label.length) { 183 189 throw Components.Exception("Empty filter!"); 184 190 } 185 191 // localize the label, but only if user didn't change it. 186 if (localizedLabel && ! this._prefs.prefHasUserValue(this.pref('label'))) {192 if (localizedLabel && !Preferences.hasUserValue(this.pref('label'))) { 187 193 this._label = localizedLabel; 188 194 } 189 190 this._active = this._prefs.getBoolPref(this.pref('active'));191 this._type = this._prefs.getIntPref(this.pref('type'));195 196 this._active = Preferences.get(this.pref('active')); 197 this._type = Preferences.get(this.pref('type')); 192 198 this._defFilter = this._id.search(/^deffilter/) != -1; 193 199 194 200 // may throw 195 this.expression = this.getMultiBytePref(this.pref('test'));201 this.expression = Preferences.get(this.pref('test')); 196 202 197 203 this._modified = false; … … 200 206 // exported 201 207 save: function F_save() { 202 if (!this._prefs) {203 throw NS_ERROR_INVALID_ARG;204 }205 208 if (!this._modified) { 206 209 return; 207 210 } 208 this._prefs.setBoolPref(this.pref('active'), this._active); 209 210 this.setMultiBytePref(this.pref('test'), this._expr); 211 this._prefs.setIntPref(this.pref('type'), this._type); 211 Preferences.set(this.pref('active'), this._active); 212 Preferences.set(this.pref('test'), this._expr); 213 Preferences.set(this.pref('type'), this._type); 212 214 213 215 // save this last as FM will test for it. 214 this.setMultiBytePref(this.pref('label'), this._label);216 Preferences.set(this.pref('label'), this._label); 215 217 216 218 this._modified = false; … … 218 220 219 221 _reset: function F_reset() { 220 // BEWARE: 1.8, no implementation for resetBranch 221 var c = {value: 0}; 222 var prefs = this._prefs.getChildList(this._id, c); 223 for (var i = 0; i < c.value; ++i) { 224 if (this._prefs.prefHasUserValue(prefs[i])) { 225 this._prefs.clearUserPref(prefs[i]); 226 } 227 } 222 Preferences.resetBranch(this._id); 228 223 }, 229 224 … … 244 239 }, 245 240 246 getMultiBytePref: function F_getMultiBytePref(pref) {247 var rv = this._prefs.getComplexValue(248 pref,249 Ci.nsISupportsString250 );251 return rv.data;252 },253 254 setMultiBytePref: function F_setMultiBytePref(pref, value) {255 var str = Cc["@mozilla.org/supports-string;1"]256 .createInstance(Ci.nsISupportsString);257 str.data = value;258 this._prefs.setComplexValue(259 pref,260 Ci.nsISupportsString,261 str262 );263 },264 241 toString: function() { 265 242 return this._label + " (" + this._id + ")"; 266 243 }, 244 267 245 toSource: function() { 268 246 return this.toString() + ": " + this._regs.toSource(); … … 305 283 var FilterManager = { 306 284 _done: true, 307 _mustReload: true,285 _mustReload: false, 308 286 309 _prefs: Cc['@mozilla.org/preferences-service;1'] 310 .getService(Ci.nsIPrefService) 311 .getBranch("extensions.dta.filters."), 312 313 _timer: Cc['@mozilla.org/timer;1'] 314 .createInstance(Ci.nsITimer), 287 _timer: null, 288 _obs: null, 315 289 316 290 init: function FM_init() { 317 this._prefs = this._prefs.QueryInterface(Ci.nsIPrefBranch2);291 Components.utils.import('resource://dta/preferences.jsm', Preferences); 318 292 319 293 // load those localized labels for default filters. 320 294 this._localizedLabels = {}; 321 varb = Cc['@mozilla.org/intl/stringbundle;1']295 let b = Cc['@mozilla.org/intl/stringbundle;1'] 322 296 .getService(Ci.nsIStringBundleService) 323 297 .createBundle("chrome://dta/locale/filters.properties"); 324 vare = b.getSimpleEnumeration();298 let e = b.getSimpleEnumeration(); 325 299 while (e.hasMoreElements()) { 326 300 var prop = e.getNext().QueryInterface(Ci.nsIPropertyElement); 327 301 this._localizedLabels[prop.key] = prop.value; 328 302 } 303 304 // init the observer service 305 this._obs = Cc["@mozilla.org/observer-service;1"] 306 .getService(Ci.nsIObserverService); 329 307 330 308 // register (the observer) and initialize our timer, so that we'll get a reload event. 331 309 this.register(); 332 this._timer.initWithCallback( 333 this, 334 100, 335 this._timer.TYPE_ONE_SHOT 336 ); 310 this._delayedReload(); 337 311 this.init = new Function(); 338 312 }, 339 313 340 314 _delayedReload: function FM_delayedReload() { 315 if (this._mustReload) { 316 return; 317 } 341 318 this._mustReload = true; 342 this._timer .delay = 100;319 this._timer = new Timer(this, 100, nsITimer.TYPE_ONE_SHOT); 343 320 }, 344 321 … … 352 329 } 353 330 this._mustReload = false; 331 354 332 355 333 this._filters = {}; 356 334 this._all = []; 357 this._count = 0;358 335 359 336 // hmmm. since we use uuids for the filters we've to enumerate the whole branch. 360 var c = {value: 0}; 361 var prefs = this._prefs.getChildList('', c); 362 363 for (var i = 0; i < c.value; ++i) { 337 for each (let pref in Preferences.getChildren(BASE)) { 364 338 // we test for label (as we get all the other props as well) 365 if (pref s[i].search(/\.label$/) == -1) {339 if (pref.search(/\.label$/) == -1) { 366 340 continue; 367 341 } 368 342 // cut of the label part to get the actual name 369 var name = prefs[i].slice(0, -6); 370 343 let name = pref.slice(0, -6); 371 344 try { 372 var filter = new Filter(name, this._prefs);345 let filter = new Filter(name); 373 346 // overwrite with localized labels. 374 var localizedLabel = null; 375 if (filter.id in this._localizedLabels) { 376 localizedLabel = this._localizedLabels[filter.id]; 347 let localizedLabel = null; 348 let localizedTag = filter.id.slice(BASE.length); 349 if (localizedTag in this._localizedLabels) { 350 localizedLabel = this._localizedLabels[localizedTag]; 377 351 } 378 352 filter.load(localizedLabel); 379 353 this._filters[filter.id] = filter; 380 354 this._all.push(filter); 381 this._count++;382 355 } 383 356 catch (ex) { 384 debug("Failed to load: " + name + " / " + ex); 385 } 386 } 357 debug("Failed to load: " + name + " / ", ex); 358 } 359 } 360 361 this._count = this._all.length; 362 387 363 this._all.sort( 388 364 function(a,b) { … … 406 382 407 383 // notify all observers 408 var observerService = Cc["@mozilla.org/observer-service;1"] 409 .getService(Ci.nsIObserverService); 410 observerService.notifyObservers(this, 'DTA:filterschanged', null); 384 let enumerator = this._obs.enumerateObservers(TOPIC_FILTERSCHANGED); 385 debug("notifying"); 386 while (enumerator.hasMoreElements()) { 387 debug("enumerator:" + enumerator.getNext().toSource()); 388 } 389 this._obs.notifyObservers(this, TOPIC_FILTERSCHANGED, null); 411 390 }, 412 391 … … 439 418 // we will use unique ids for user-supplied filters. 440 419 // no need to keep track of the actual number of filters or an index. 441 varuuid = Cc["@mozilla.org/uuid-generator;1"]420 let uuid = Cc["@mozilla.org/uuid-generator;1"] 442 421 .getService(Ci.nsIUUIDGenerator) 443 422 .generateUUID(); 444 423 445 424 // 446 var filter = new Filter(uuid.toString(), this._prefs);425 let filter = new Filter(BASE + uuid.toString()); 447 426 // I'm a friend, hence I'm allowed to access private members :p 448 427 filter._label = label; … … 474 453 } 475 454 catch (ex) { 476 debug( ex);455 debug('Failed to save filters', ex); 477 456 } 478 457 } … … 493 472 // nsIObserver 494 473 observe: function FM_observe(subject, topic, prefName) { 495 this._delayedReload(); 474 if (topic == 'timer-callback') { 475 this.reload(); 476 } 477 else { 478 this._delayedReload(); 479 } 496 480 }, 497 481 … … 500 484 try { 501 485 // Put self as observer to desired branch 502 this._prefs.addObserver("", this, true);486 Preferences.addObserver(BASE, this); 503 487 } 504 488 catch (ex) { … … 507 491 } 508 492 return true; 509 },510 511 // nsITimerCallback512 notify: function FM_notify() {513 //error("DTAFM: notify");514 this.reload();515 493 } 516 494 }; … … 520 498 "@downthemall.net/filtermanager;2", 521 499 "DownThemAll! Filtermanager", 522 [Ci.nsI TimerCallback, Ci.nsIObserver, Ci.dtaIFilterManager]500 [Ci.nsIObserver, Ci.dtaIFilterManager] 523 501 ); 524 502
