Changeset 73
- Timestamp:
- 2007-02-03 18:38:31 (2 years ago)
- Files:
-
- trunk/components/filterManager.idl (modified) (1 diff)
- trunk/components/filterManager.js (modified) (42 diffs)
- trunk/components/filterManager.xpt (modified) (previous)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/components/filterManager.idl
r37 r73 69 69 boolean matchActive(in string test, in unsigned long type); 70 70 71 voidcreate(in string label, in string test, in boolean active, in unsigned long type, in boolean regex);71 string create(in string label, in string test, in boolean active, in unsigned long type, in boolean regex); 72 72 void remove(in string id); 73 73 trunk/components/filterManager.js
r70 r73 34 34 * 35 35 * ***** END LICENSE BLOCK ***** */ 36 37 // Components.classes['@tn123.ath.cx/dtamod/filtermanager;1'].getService(Components.interfaces.dtaIFilterManager).matchActive('hallo.jpg', 1);36 37 // var fm = Components.classes['@tn123.ath.cx/dtamod/filtermanager;1'].getService(Components.interfaces.dtaIFilterManager); var id = fm.create('a', 'b', false, 1, false); fm.remove(id); 38 38 39 39 const CC = Components.classes; … … 58 58 LINK_FILTER: (1 << 0), 59 59 IMAGE_FILTER: (1 << 1), 60 60 61 61 _modified: false, 62 62 63 63 // nsIClassInfo 64 64 classID: Components.ID("{3F872ADC-35A4-4c79-B771-F2BC130FB792}"), … … 73 73 getInterfaces: function(count) { 74 74 // XXX 75 count.value = 0; 76 return null; 77 }, 78 75 count.value = 0; 76 return null; 77 }, 78 79 79 QueryInterface: function F_QI(iid) { 80 80 if ( … … 87 87 throw Components.results.NS_ERROR_NO_INTERFACE; 88 88 }, 89 89 90 90 // exported 91 91 get id() { 92 92 return this._id; 93 93 }, 94 94 95 95 // exported 96 96 get defFilter() { 97 97 return this._defFilter; 98 98 }, 99 99 100 100 // exported 101 101 get label() { … … 109 109 this._modified = true; 110 110 }, 111 111 112 112 // exported 113 113 get test() { … … 115 115 }, 116 116 set test(value) { 117 if (this._defFilter) { 118 throw new Components.Exception("default filters cannot be modified!"); 119 } 117 120 if (this._test == value) { 118 121 return; … … 128 131 this._modified = true; 129 132 }, 130 133 131 134 // exported 132 135 get active() { … … 140 143 this._modified = true; 141 144 }, 142 145 143 146 // exported 144 147 get isRegex() { … … 146 149 }, 147 150 set isRegex(value) { 151 if (this._defFilter) { 152 throw new Components.Exception("default filters cannot be deleted!"); 153 } 148 154 if (this._isRegex == value) { 149 155 return; … … 159 165 this._modified = true; 160 166 }, 161 167 162 168 // exported 163 169 get type() { … … 165 171 }, 166 172 set type(t) { 173 if (this._defFilter) { 174 throw new Components.Exception("default filters cannot be modified!"); 175 } 167 176 if (this._type == t) { 168 177 return; … … 171 180 this._modified = true; 172 181 }, 173 182 174 183 _createRegex: function F_createRegex() { 175 184 this._regex = this._isRegex ? DTA_regToRegExp(this._test) : DTA_strToRegExp(this._test); … … 183 192 return str.search(this._regex) != -1; 184 193 }, 185 194 186 195 /** 187 196 * @throws Exception in case loading failed 188 197 */ 189 load: function F_load( ) {198 load: function F_load(localizedLabel) { 190 199 this._label = this.getMultiBytePref(this.pref('label')); 191 200 if (!this._label || !this._label.length) { 192 201 throw Components.Exception("Empty filter!"); 202 } 203 // localize the label, but only if user didn't change it. 204 if (localizedLabel || !this._prefs.prefHasUserValue(this.pref('label'))) { 205 this._label = localizedLabel; 193 206 } 194 207 this._test = this.getMultiBytePref(this.pref('test')); … … 207 220 } 208 221 this._prefs.setBoolPref(this.pref('active'), this._active); 209 222 210 223 // do not change defFilters 211 224 if (!this.defFilter) { … … 213 226 this._prefs.setIntPref(this.pref('type'), this._type); 214 227 this._prefs.setBoolPref(this.pref('regex'), this._isRegex); 215 216 // save this last as FM will test for it. 217 this.setMultiBytePref(this.pref('label'), this._label); 218 } 228 229 } 230 // save this last as FM will test for it. 231 this.setMultiBytePref(this.pref('label'), this._label); 232 219 233 this._modified = false; 220 234 }, 221 235 222 236 // exported 223 237 remove: function F_remove() { 224 this._prefs.resetBranch(this._id); 225 }, 226 238 // BEWARE: 1.8, no implementation for resetBranch 239 if (this._defFilter) { 240 throw new Components.Exception("default filters cannot be deleted!"); 241 } 242 var c = {value: 0}; 243 var prefs = this._prefs.getChildList(this._id, c); 244 for (var i = 0; i < c.value; ++i) { 245 this._prefs.clearUserPref(prefs[i]); 246 } 247 }, 248 227 249 getMultiBytePref: function F_getMultiBytePref(pref) { 228 250 var rv = this._prefs.getComplexValue( … … 232 254 return rv.data; 233 255 }, 234 256 235 257 setMultiBytePref: function F_setMultiBytePref(pref, value) { 236 258 var str = CC["@mozilla.org/supports-string;1"] … … 241 263 CI.nsISupportsString, 242 264 str 243 ); 265 ); 244 266 } 245 267 }; … … 261 283 return 1; 262 284 } 263 var i = a.label.toLower (), ii = b.label.toLower();285 var i = a.label.toLowerCase(), ii = b.label.toLowerCase(); 264 286 return i < ii ? -1 : (i > ii ? 1 : 0); 265 287 }); … … 301 323 getInterfaces: function(count) { 302 324 // XXX 303 count.value = 0; 304 return null; 325 count.value = 0; 326 return null; 305 327 }, 306 328 … … 315 337 this.classID 316 338 ].some(function(e) { return iid.equals(e); }); 317 }, 318 339 }, 340 319 341 _done: true, 320 342 _mustReload: true, … … 327 349 _init: function FM_init() { 328 350 this._prefs = this._prefs.QueryInterface(CI.nsIPrefBranch2); 329 351 330 352 // load those localized labels for default filters. 331 353 this._labels = {}; … … 338 360 this._labels[prop.key] = prop.value; 339 361 } 340 362 341 363 // register (the observer) and initialize our timer, so that we'll get a reload event. 342 364 this.register(); … … 347 369 ); 348 370 }, 349 371 350 372 _delayedReload: function FM_delayedReload() { 351 373 this._mustReload = true; 352 374 this._timer.delay = 100; 353 375 }, 354 376 355 377 get count() { 356 378 return this._count; … … 363 385 this._mustReload = false; 364 386 365 //error("DTAFM: reload"); 387 //error("DTAFM: reload"); 366 388 this._filters = {}; 367 389 this._count = 0; 368 390 369 391 // hmmm. since we use uuids for the filters we've to enumerate the whole branch. 370 392 var c = {value: 0}; 371 393 var prefs = this._prefs.getChildList('', c); 372 394 373 395 for (var i = 0; i < c.value; ++i) { 374 396 // we test for label (as we get all the other props as well) … … 378 400 // cut of the label part to get the actual name 379 401 var name = prefs[i].slice(0, -6); 380 402 381 403 try { 382 404 var filter = new Filter(name, this._prefs); 383 filter.load();384 405 // overwrite with localized labels. 406 var localizedLabel = null; 385 407 if (filter.id in this._labels) { 386 filter._label = this._labels[filter.id];408 localizedLabel = this._labels[filter.id]; 387 409 } 410 filter.load(localizedLabel); 388 411 this._filters[filter.id] = filter; 389 412 this._count++; … … 393 416 } 394 417 } 395 418 396 419 // notify all observers 397 420 var observerService = CC["@mozilla.org/observer-service;1"] … … 399 422 observerService.notifyObservers(this, 'DTA:filterschanged', null); 400 423 }, 401 424 402 425 enumAll: function FM_enumAll() { 403 426 var a = []; … … 416 439 return new FilterEnumerator(a); 417 440 }, 418 441 419 442 getFilter: function FM_getFilter(id) { 420 443 if (id in this._filters) { … … 429 452 return e._filters.some(function(i) { return i.match(test); }); 430 453 }, 431 454 432 455 create: function FM_create(label, test, active, type, isRegex) { 433 456 434 457 // we will use unique ids for user-supplied filters. 435 458 // no need to keep track of the actual number of filters or an index. … … 437 460 .getService(CI.nsIUUIDGenerator) 438 461 .generateUUID(); 439 440 // 462 463 // 441 464 var filter = new Filter(uuid.toString(), this._prefs); 442 465 // I'm a friend, hence I'm allowed to access private members :p … … 446 469 filter._type = type; 447 470 filter._modified = true; 448 471 449 472 // this might throw! 450 473 filter.isRegex = isRegex; 451 474 452 475 // will call our observer so we re-init... no need to do more work here :p 453 476 filter.save(); 454 455 }, 456 477 return filter.id; 478 }, 479 457 480 remove: function FM_remove(id) { 458 481 if (id in this._filters) { 459 482 this._filters[id].remove(); 460 } 461 }, 462 483 return; 484 } 485 throw new Components.Exception('filter not defined!'); 486 }, 487 463 488 save: function FM_save() { 464 489 var e = this.enumAll(); … … 472 497 } 473 498 }, 474 499 475 500 // nsiSupports 476 501 QueryInterface: function FM_QI(iid) { … … 480 505 throw Components.results.NS_ERROR_NO_INTERFACE; 481 506 }, 482 507 483 508 // nsiWeakReference 484 509 QueryReferent: function FM_QR(iid) { … … 495 520 this._delayedReload(); 496 521 }, 497 522 498 523 // own stuff 499 524 register: function FM_register() { … … 508 533 return true; 509 534 }, 510 535 511 536 // nsITimerCallback 512 537 notify: function FM_notify() { … … 514 539 this.reload(); 515 540 } 516 541 517 542 }; 518 543 FilterManager._init(); … … 520 545 var Module = { 521 546 _firstTime: true, 522 547 523 548 registerSelf: function M_registerSelf(compMgr, fileSpec, location, type) { 524 549 if (!this._firstTime) { … … 526 551 } 527 552 this._firstTime = false; 528 529 compMgr.QueryInterface(CI.nsIComponentRegistrar)553 554 compMgr.QueryInterface(CI.nsIComponentRegistrar) 530 555 .registerFactoryLocation( 531 556 FilterManager.classID, 532 557 FilterManager.classDescription, 533 FilterManager.contractID, 558 FilterManager.contractID, 534 559 fileSpec, 535 location, 560 location, 536 561 type 537 562 ); … … 539 564 .getService(CI.nsICategoryManager) 540 565 .addCategoryEntry('app-startup', FilterManager.contractID, FilterManager.contractID, true, true, null); 541 },566 }, 542 567 unregisterSelf: function(compMgr, fileSpec, location) { 543 568 compMgr.QueryInterface(CI.nsIComponentRegistrar) … … 546 571 fileSpec 547 572 ); 548 CC['@mozilla.org/categorymanager;1']549 .getService(CI.nsICategoryManager)550 .deleteCategoryEntry('app-startup', FileManager.contractID, true); 573 CC['@mozilla.org/categorymanager;1'] 574 .getService(CI.nsICategoryManager) 575 .deleteCategoryEntry('app-startup', FileManager.contractID, true); 551 576 }, 552 577 getClassObject: function (compMgr, cid, iid) { … … 559 584 return true; 560 585 }, 561 586 562 587 // nsIFactory 563 QueryInterace : function(aIID) {564 if (aIID.equals(CI.nsIFactory)) {565 return this;566 } 567 568 return Components.results.NS_ERROR_NO_INTERFACE;569 },588 QueryInterace : function(aIID) { 589 if (aIID.equals(CI.nsIFactory)) { 590 return this; 591 } 592 593 return Components.results.NS_ERROR_NO_INTERFACE; 594 }, 570 595 createInstance: function (outer, iid) { 571 596 if (outer != null) { … … 576 601 } 577 602 throw Components.results.NS_ERROR_INVALID_ARG; 578 } 603 } 579 604 } 580 605 581 606 // entrypoint 582 607 function NSGetModule(compMgr, fileSpec) { 583 return Module;608 return Module; 584 609 }
