Changeset 797
- Timestamp:
- 2008-02-27 19:32:57 (10 months ago)
- Files:
-
- trunk/components/filterManager.idl (modified) (4 diffs)
- trunk/components/filterManager.js (modified) (26 diffs)
- trunk/components/filterManager.xpt (modified) (previous)
- trunk/defaults/preferences/filters.js (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/components/filterManager.idl
r737 r797 38 38 #include "nsISimpleEnumerator.idl" 39 39 40 [scriptable, uuid( 3F872ADC-35A4-4c79-B771-F2BC130FB792)]40 [scriptable, uuid(1CF86DC0-33A7-43b3-BDDE-7ADC3B35D114)] 41 41 interface dtaIFilter : nsISupports 42 42 { … … 47 47 [readonly] attribute boolean defFilter; 48 48 attribute AUTF8String label; 49 attribute AUTF8String test; 50 attribute boolean isRegex; 49 attribute AUTF8String expression; 51 50 attribute boolean active; 52 51 attribute unsigned long type; … … 57 56 }; 58 57 59 [scriptable, uuid( 3F872ADC-35A4-4c79-B771-F2BC130FB791)]58 [scriptable, uuid(435FC5E5-D4F0-47a1-BDC1-F325B78188F3)] 60 59 interface dtaIFilterManager : nsISupports 61 60 { … … 70 69 boolean matchActive(in AUTF8String test, in unsigned long type); 71 70 72 string create(in AUTF8String label, in AUTF8String test, in boolean active, in unsigned long type, in boolean regex);71 string create(in AUTF8String label, in AUTF8String expression, in boolean active, in unsigned long type); 73 72 void remove(in string id); 74 73 75 74 void save(); 75 76 dtaIFilter getTmpFromString(in AUTF8String expression); 77 76 78 }; trunk/components/filterManager.js
r538 r797 35 35 * ***** END LICENSE BLOCK ***** */ 36 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 39 37 const CC = Components.classes; 40 38 const CI = Components.interfaces; 41 const error = Components.utils.reportError; 39 const Exception = Components.Exception; 40 41 const NS_ERROR_NO_INTERFACE = Components.results.NS_ERROR_NO_INTERFACE; 42 const NS_ERROR_FAILURE = Components.results.NS_ERROR_FAILURE; 43 const NS_ERROR_NO_AGGREGATION = Components.results.NS_ERROR_NO_AGGREGATION; 44 const NS_ERROR_INVALID_ARG = Components.results.NS_ERROR_INVALID_ARG; 45 46 const LINK_FILTER = CI.dtaIFilter.LINK_FILTER; 47 const IMAGE_FILTER = CI.dtaIFilter.IMAGE_FILTER; 42 48 43 49 function include(uri) { … … 47 53 } 48 54 49 include("chrome://dta/content/common/regconvert.js"); 55 function debug(str, ex) { 56 try { 57 var DTA_Debug = Components.classes['@downthemall.net/debug-service;1'] 58 .getService(Components.interfaces.dtaIDebugService); 59 debug = function(str, ex) { 60 if (ex) { 61 DTA_Debug.log(str, ex); 62 } 63 else { 64 DTA_Debug.logString(str); 65 } 66 } 67 debug(str, ex); 68 } 69 catch (ex) { 70 Components.utils.reportError(str + ": " + ex); 71 } 72 } 50 73 51 74 // no not create DTA_Filter yourself, managed by DTA_FilterManager … … 60 83 61 84 _modified: false, 85 _regs: [], 62 86 63 87 // nsIClassInfo 64 classID: Components.ID("{ 3F872ADC-35A4-4c79-B771-F2BC130FB792}"),65 contractID: "@downthemall.net/filter; 1",88 classID: Components.ID("{1CF86DC0-33A7-43b3-BDDE-7ADC3B35D114}"), 89 contractID: "@downthemall.net/filter;2", 66 90 classDescription: "DownThemAll! Filter", 67 91 implementationLanguage: 0x02, … … 85 109 return this; 86 110 } 87 throw Components.results.NS_ERROR_NO_INTERFACE;111 throw NS_ERROR_NO_INTERFACE; 88 112 }, 89 113 … … 111 135 112 136 // exported 113 get test() { 114 return this._test; 115 }, 116 set test(value) { 117 if (this._test == value) { 118 return; 119 } 120 try { 121 this._test = value; 122 this._createRegex(); 123 } catch (ex) { 124 // hope we don't throw again ROFL 125 this.isRegex = false; 126 throw Components.Exception("Failed to create Regex"); 127 } 128 this._modified = true; 137 get expression() { 138 return this._expr; 139 }, 140 set expression(value) { 141 if (this._expr == value) { 142 return; 143 } 144 this._expr = value; 145 this._regs = []; 146 this._makeRegs(this._expr); 147 148 this._modified = true; 149 }, 150 _makeRegs: function FM__makeRegs(str) { 151 152 str = str.replace(/^\s+|\s+$/g, ''); 153 154 // first of all: check if we are are a regexp. 155 if (str.length > 2 && str[0] == '/') { 156 try { 157 var m = str.match(/^\/(.+?)(?:\/(i?))?$/); 158 if (!m) { 159 throw new Exception("Invalid RegExp supplied"); 160 } 161 if (!m[1].length) { 162 return; 163 } 164 this._regs.push(new RegExp(m[1], m[2])); 165 return; 166 } 167 catch (ex) { 168 // fall-through 169 } 170 } 171 172 var parts = str.split(','); 173 // we contain multiple filters 174 if (parts.length > 1) { 175 parts.forEach( 176 function(s) { 177 this._makeRegs(s); 178 }, 179 this 180 ); 181 return; 182 } 183 184 // we are simple text 185 str = str 186 .replace(/([/{}()\[\]\\^$.])/g, "\\$1") 187 .replace(/\*/g, ".*") 188 .replace(/\?/g, '.'); 189 if (str.length) { 190 this._regs.push(new RegExp(str, 'i')); 191 } 129 192 }, 130 193 … … 142 205 143 206 // exported 144 get isRegex() {145 return this._isRegex;146 },147 set isRegex(value) {148 if (this._isRegex == value) {149 return;150 }151 try {152 this._isRegex = value;153 this._createRegex();154 } catch (ex) {155 // hope we don't throw again ROFL156 this.isRegex = false;157 throw Components.Exception("Failed to create Regex");158 }159 this._modified = true;160 },161 162 // exported163 207 get type() { 164 208 return this._type; … … 172 216 }, 173 217 174 _createRegex: function F_createRegex() {175 this._regex = this._isRegex ? DTA_regToRegExp(this._test) : DTA_strToRegExp(this._test);176 },177 178 218 pref: function F_pref(str) { 179 219 return this._id + "." + str; … … 181 221 182 222 match: function F_match(str) { 183 return str.search(this._regex) != -1; 223 return this._regs.some( 224 function(reg) { 225 return str.search(reg) != -1; 226 } 227 ); 184 228 }, 185 229 … … 198 242 } 199 243 200 this._test = this.getMultiBytePref(this.pref('test'));201 244 this._active = this._prefs.getBoolPref(this.pref('active')); 202 245 this._type = this._prefs.getIntPref(this.pref('type')); 203 this._isRegex = this._prefs.getBoolPref(this.pref('regex'));204 246 this._defFilter = this._id.search(/^deffilter/) != -1; 205 this._createRegex(); 247 248 // may throw 249 this.expression = this.getMultiBytePref(this.pref('test')); 250 206 251 this._modified = false; 207 252 }, … … 209 254 // exported 210 255 save: function F_save() { 256 if (!this._prefs) { 257 throw NS_ERROR_INVALID_ARG; 258 } 211 259 if (!this._modified) { 212 260 return; … … 214 262 this._prefs.setBoolPref(this.pref('active'), this._active); 215 263 216 this.setMultiBytePref(this.pref('test'), this._ test);264 this.setMultiBytePref(this.pref('test'), this._expr); 217 265 this._prefs.setIntPref(this.pref('type'), this._type); 218 this._prefs.setBoolPref(this.pref('regex'), this._isRegex);219 266 220 267 // save this last as FM will test for it. … … 268 315 str 269 316 ); 317 }, 318 toString: function() { 319 return this._label + " (" + this._id + ")"; 320 }, 321 toSource: function() { 322 return this.toString() + ": " + this._regs.toSource(); 270 323 } 271 324 }; … … 283 336 return this; 284 337 } 285 throw Components.results.NS_ERROR_NO_INTERFACE;338 throw NS_ERROR_NO_INTERFACE; 286 339 }, 287 340 hasMoreElements: function FE_hasMoreElements() { … … 290 343 getNext: function FE_getNext() { 291 344 if (!this.hasMoreElements()) { 292 throw Components.results.NS_ERROR_FAILURE;345 throw NS_ERROR_FAILURE; 293 346 } 294 347 return this._filters[this._idx++]; … … 300 353 301 354 // nsIClassInfo 302 classID: Components.ID("{ 3F872ADC-35A4-4c79-B771-F2BC130FB791}"),303 contractID: "@downthemall.net/filtermanager; 1",355 classID: Components.ID("{435FC5E5-D4F0-47a1-BDC1-F325B78188F3}"), 356 contractID: "@downthemall.net/filtermanager;2", 304 357 classDescription: "DownThemAll! Filtermanager", 305 358 implementationLanguage: 0x02, … … 316 369 317 370 implementsIID: function FM_implementID(iid) { 318 return [319 CI.nsISupports,320 CI.nsISupportsWeakReference,321 CI.nsIWeakReference,322 CI.nsIObserver,323 CI.nsIClassInfo,324 CI.nsITimerCallback,325 this.classID326 ].some(function(e) { return iid.equals(e); });371 return [ 372 CI.nsISupports, 373 CI.nsISupportsWeakReference, 374 CI.nsIWeakReference, 375 CI.nsIObserver, 376 CI.nsIClassInfo, 377 CI.nsITimerCallback, 378 this.classID 379 ].some(function(e) { return iid.equals(e); }); 327 380 }, 328 381 … … 340 393 // load those localized labels for default filters. 341 394 this._localizedLabels = {}; 342 var b = CC['@mozilla.org/intl/stringbundle;1']395 var b = CC['@mozilla.org/intl/stringbundle;1'] 343 396 .getService(CI.nsIStringBundleService) 344 397 .createBundle("chrome://dta/locale/filters.properties"); … … 402 455 } 403 456 catch (ex) { 404 error("Failed to load: " + name + " / " + ex);457 debug("Failed to load: " + name + " / " + ex); 405 458 } 406 459 } … … 432 485 433 486 enumAll: function FM_enumAll() { 487 this._all.forEach(function(f) { debug(f.toSource()); }); 434 488 return new FilterEnumerator(this._all); 435 489 }, 436 490 enumActive: function FM_enumActive(type) { 437 return new FilterEnumerator(this._active); 491 return new FilterEnumerator( 492 this._active.filter( 493 function(i) { 494 return i.type & type; 495 } 496 ) 497 ); 438 498 }, 439 499 … … 446 506 447 507 matchActive: function FM_matchActive(test, type) { 448 return this._active.some(function(i) { return i.match(test); });449 }, 450 451 create: function FM_create(label, test, active, type, isRegex) {508 return this._active.some(function(i) { return (i.type & type) && i.match(test); }); 509 }, 510 511 create: function FM_create(label, expression, active, type) { 452 512 453 513 // we will use unique ids for user-supplied filters. … … 461 521 // I'm a friend, hence I'm allowed to access private members :p 462 522 filter._label = label; 463 filter._test = test;464 523 filter._active = active; 465 524 filter._type = type; … … 467 526 468 527 // this might throw! 469 filter.isRegex = isRegex; 528 filter.expression = expression; 529 470 530 471 531 // will call our observer so we re-init... no need to do more work here :p … … 487 547 try { 488 548 f.save(); 489 } catch (ex) { 490 error(ex); 549 } 550 catch (ex) { 551 debug(ex); 491 552 } 492 553 }, 493 554 this 494 555 ); 556 }, 557 558 getTmpFromString: function FM_getTmpFromString(expression) { 559 if (!expression.length) { 560 throw NS_ERROR_INVALID_ARG; 561 } 562 var filter = new Filter("temp", null); 563 filter._active = true; 564 filter._type = LINK_FILTER | IMAGE_FILTER; 565 filter._modified = false; 566 filter.expression = expression; 567 return filter; 495 568 }, 496 569 … … 500 573 return this; 501 574 } 502 throw Components.results.NS_ERROR_NO_INTERFACE;575 throw NS_ERROR_NO_INTERFACE; 503 576 }, 504 577 … … 576 649 return this; 577 650 } 578 throw Components.results.NS_ERROR_NO_INTERFACE;651 throw NS_ERROR_NO_INTERFACE; 579 652 }, 580 653 canUnload: function(compMgr) { … … 588 661 } 589 662 590 return Components.results.NS_ERROR_NO_INTERFACE;663 return NS_ERROR_NO_INTERFACE; 591 664 }, 592 665 createInstance: function (outer, iid) { 593 666 if (outer != null) { 594 throw Components.results.NS_ERROR_NO_AGGREGATION;667 throw NS_ERROR_NO_AGGREGATION; 595 668 } 596 669 if (FilterManager.implementsIID(iid)) { 597 670 return FilterManager; 598 671 } 599 throw Components.results.NS_ERROR_INVALID_ARG;672 throw NS_ERROR_INVALID_ARG; 600 673 } 601 674 } trunk/defaults/preferences/filters.js
r538 r797 38 38 pref("extensions.dta.filters.deffilter-all.label", "All files"); 39 39 pref("extensions.dta.filters.deffilter-all.test", "/.*/i"); 40 pref("extensions.dta.filters.deffilter-all.regex", true);41 40 pref("extensions.dta.filters.deffilter-all.active", false); 42 41 pref("extensions.dta.filters.deffilter-all.type", 3); … … 44 43 pref("extensions.dta.filters.deffilter-arch.label", "Archives)"); 45 44 pref("extensions.dta.filters.deffilter-arch.test", "/\\.(?:z(?:ip|[0-9]{2})|r(?:ar|[0-9]{2})|jar|bz2|gz|tar|rpm)$/i"); 46 pref("extensions.dta.filters.deffilter-arch.regex", true);47 45 pref("extensions.dta.filters.deffilter-arch.active", false); 48 46 pref("extensions.dta.filters.deffilter-arch.type", 1); … … 50 48 pref("extensions.dta.filters.deffilter-vid.label", "Videos"); 51 49 pref("extensions.dta.filters.deffilter-vid.test", "/\\.(?:mpeg|ra?m|avi|mp(?:g|e|4)|mov|divx|asf|qt|wmv|m\dv|rv|vob|asx|ogm)$/i"); 52 pref("extensions.dta.filters.deffilter-vid.regex", true);53 50 pref("extensions.dta.filters.deffilter-vid.active", true); 54 51 pref("extensions.dta.filters.deffilter-vid.type", 3); … … 56 53 pref("extensions.dta.filters.deffilter-aud.label", "Audio"); 57 54 pref("extensions.dta.filters.deffilter-aud.test", "/\\.(?:mp3|wav|og(?:g|a)|flac|midi?|rm|aac|wma|mka|ape)$/i"); 58 pref("extensions.dta.filters.deffilter-aud.regex", true);59 55 pref("extensions.dta.filters.deffilter-aud.active", true); 60 56 pref("extensions.dta.filters.deffilter-aud.type", 1); … … 62 58 pref("extensions.dta.filters.deffilter-img.label", "Images"); 63 59 pref("extensions.dta.filters.deffilter-img.test", "/\\.(?:jp(?:e?g|e|2)|gif|png|tiff?|bmp|ico)$/i"); 64 pref("extensions.dta.filters.deffilter-img.regex", true);65 60 pref("extensions.dta.filters.deffilter-img.active", true); 66 61 pref("extensions.dta.filters.deffilter-img.type", 3); … … 68 63 pref("extensions.dta.filters.deffilter-bin.label", "Software"); 69 64 pref("extensions.dta.filters.deffilter-bin.test", "/\\.(?:exe|msi|dmg|bin|xpi|iso)$/i"); 70 pref("extensions.dta.filters.deffilter-bin.regex", true);71 65 pref("extensions.dta.filters.deffilter-bin.active", true); 72 66 pref("extensions.dta.filters.deffilter-bin.type", 1); … … 74 68 pref("extensions.dta.filters.deffilter-imgjpg.label", "JPEG"); 75 69 pref("extensions.dta.filters.deffilter-imgjpg.test", "/\\.jp(e?g|e|2)$/i"); 76 pref("extensions.dta.filters.deffilter-imgjpg.regex", true);77 70 pref("extensions.dta.filters.deffilter-imgjpg.active", false); 78 71 pref("extensions.dta.filters.deffilter-imgjpg.type", 3); … … 80 73 pref("extensions.dta.filters.deffilter-imggif.label", "GIF"); 81 74 pref("extensions.dta.filters.deffilter-imggif.test", "/\\.gif$/i"); 82 pref("extensions.dta.filters.deffilter-imggif.regex", true);83 75 pref("extensions.dta.filters.deffilter-imggif.active", false); 84 76 pref("extensions.dta.filters.deffilter-imggif.type", 2); … … 86 78 pref("extensions.dta.filters.deffilter-imgpng.label", "PNG"); 87 79 pref("extensions.dta.filters.deffilter-imgpng.test", "/\\.png$/i"); 88 pref("extensions.dta.filters.deffilter-imgpng.regex", true);89 80 pref("extensions.dta.filters.deffilter-imgpng.active", false); 90 81 pref("extensions.dta.filters.deffilter-imgpng.type", 2);
