Changeset 797

Show
Ignore:
Timestamp:
2008-02-27 19:32:57 (10 months ago)
Author:
MaierMan
Message:

#513: Simplify/Unify Filters; remove UI regex/text distinction. pt.2

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/components/filterManager.idl

    r737 r797  
    3838#include "nsISimpleEnumerator.idl" 
    3939 
    40 [scriptable, uuid(3F872ADC-35A4-4c79-B771-F2BC130FB792)] 
     40[scriptable, uuid(1CF86DC0-33A7-43b3-BDDE-7ADC3B35D114)] 
    4141interface dtaIFilter : nsISupports 
    4242{ 
     
    4747        [readonly] attribute boolean defFilter; 
    4848        attribute AUTF8String label; 
    49         attribute AUTF8String test; 
    50         attribute boolean isRegex; 
     49        attribute AUTF8String expression; 
    5150        attribute boolean active; 
    5251        attribute unsigned long type; 
     
    5756}; 
    5857 
    59 [scriptable, uuid(3F872ADC-35A4-4c79-B771-F2BC130FB791)] 
     58[scriptable, uuid(435FC5E5-D4F0-47a1-BDC1-F325B78188F3)] 
    6059interface dtaIFilterManager : nsISupports 
    6160{ 
     
    7069        boolean matchActive(in AUTF8String test, in unsigned long type); 
    7170         
    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); 
    7372        void remove(in string id); 
    7473         
    7574        void save(); 
     75         
     76        dtaIFilter getTmpFromString(in AUTF8String expression);  
     77         
    7678};  
  • trunk/components/filterManager.js

    r538 r797  
    3535 * ***** END LICENSE BLOCK ***** */ 
    3636 
    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  
    3937const CC = Components.classes; 
    4038const CI = Components.interfaces; 
    41 const error = Components.utils.reportError; 
     39const Exception = Components.Exception; 
     40 
     41const NS_ERROR_NO_INTERFACE = Components.results.NS_ERROR_NO_INTERFACE; 
     42const NS_ERROR_FAILURE = Components.results.NS_ERROR_FAILURE; 
     43const NS_ERROR_NO_AGGREGATION = Components.results.NS_ERROR_NO_AGGREGATION; 
     44const NS_ERROR_INVALID_ARG = Components.results.NS_ERROR_INVALID_ARG; 
     45 
     46const LINK_FILTER = CI.dtaIFilter.LINK_FILTER; 
     47const IMAGE_FILTER = CI.dtaIFilter.IMAGE_FILTER; 
    4248 
    4349function include(uri) { 
     
    4753} 
    4854 
    49 include("chrome://dta/content/common/regconvert.js"); 
     55function 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
    5073 
    5174// no not create DTA_Filter yourself, managed by DTA_FilterManager 
     
    6083 
    6184        _modified: false, 
     85        _regs: [], 
    6286 
    6387        // 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", 
    6690        classDescription: "DownThemAll! Filter", 
    6791        implementationLanguage: 0x02, 
     
    85109                        return this; 
    86110                } 
    87                 throw Components.results.NS_ERROR_NO_INTERFACE; 
     111                throw NS_ERROR_NO_INTERFACE; 
    88112        }, 
    89113 
     
    111135 
    112136        // 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                } 
    129192        }, 
    130193 
     
    142205 
    143206        // 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 ROFL 
    156                         this.isRegex = false; 
    157                         throw Components.Exception("Failed to create Regex"); 
    158                 } 
    159                 this._modified = true; 
    160         }, 
    161  
    162         // exported 
    163207        get type() { 
    164208                return this._type; 
     
    172216        }, 
    173217 
    174         _createRegex: function F_createRegex() { 
    175                 this._regex = this._isRegex ? DTA_regToRegExp(this._test) : DTA_strToRegExp(this._test); 
    176         }, 
    177  
    178218        pref: function F_pref(str) { 
    179219                return this._id + "." + str; 
     
    181221 
    182222        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                ); 
    184228        }, 
    185229 
     
    198242                } 
    199243                 
    200                 this._test = this.getMultiBytePref(this.pref('test')); 
    201244                this._active = this._prefs.getBoolPref(this.pref('active')); 
    202245                this._type = this._prefs.getIntPref(this.pref('type')); 
    203                 this._isRegex = this._prefs.getBoolPref(this.pref('regex')); 
    204246                this._defFilter = this._id.search(/^deffilter/) != -1; 
    205                 this._createRegex(); 
     247                 
     248                // may throw 
     249                this.expression = this.getMultiBytePref(this.pref('test')); 
     250                 
    206251                this._modified = false; 
    207252        }, 
     
    209254        // exported 
    210255        save: function F_save() { 
     256                if (!this._prefs) { 
     257                        throw NS_ERROR_INVALID_ARG; 
     258                } 
    211259                if (!this._modified) { 
    212260                        return; 
     
    214262                this._prefs.setBoolPref(this.pref('active'), this._active); 
    215263                 
    216                 this.setMultiBytePref(this.pref('test'), this._test); 
     264                this.setMultiBytePref(this.pref('test'), this._expr); 
    217265                this._prefs.setIntPref(this.pref('type'), this._type); 
    218                 this._prefs.setBoolPref(this.pref('regex'), this._isRegex); 
    219266                         
    220267                // save this last as FM will test for it. 
     
    268315                        str 
    269316                ); 
     317        }, 
     318        toString: function() { 
     319                return this._label + " (" + this._id + ")"; 
     320        }, 
     321        toSource: function() { 
     322                return this.toString() + ": " + this._regs.toSource(); 
    270323        } 
    271324}; 
     
    283336                        return this; 
    284337                } 
    285                 throw Components.results.NS_ERROR_NO_INTERFACE; 
     338                throw NS_ERROR_NO_INTERFACE; 
    286339        }, 
    287340        hasMoreElements: function FE_hasMoreElements() { 
     
    290343        getNext: function FE_getNext() { 
    291344                if (!this.hasMoreElements()) { 
    292                         throw Components.results.NS_ERROR_FAILURE; 
     345                        throw NS_ERROR_FAILURE; 
    293346                } 
    294347                return this._filters[this._idx++]; 
     
    300353 
    301354        // 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", 
    304357        classDescription: "DownThemAll! Filtermanager", 
    305358        implementationLanguage: 0x02, 
     
    316369 
    317370        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.classID 
    326                        ].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); }); 
    327380        }, 
    328381 
     
    340393                // load those localized labels for default filters. 
    341394                this._localizedLabels = {}; 
    342                                var b = CC['@mozilla.org/intl/stringbundle;1'] 
     395                var b = CC['@mozilla.org/intl/stringbundle;1'] 
    343396                        .getService(CI.nsIStringBundleService) 
    344397                        .createBundle("chrome://dta/locale/filters.properties"); 
     
    402455                        } 
    403456                        catch (ex) { 
    404                                 error("Failed to load: " + name + " / " + ex); 
     457                                debug("Failed to load: " + name + " / " + ex); 
    405458                        } 
    406459                } 
     
    432485 
    433486        enumAll: function FM_enumAll() { 
     487                this._all.forEach(function(f) { debug(f.toSource()); });  
    434488                return new FilterEnumerator(this._all); 
    435489        }, 
    436490        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                ); 
    438498        }, 
    439499 
     
    446506 
    447507        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) { 
    452512 
    453513                // we will use unique ids for user-supplied filters. 
     
    461521                // I'm a friend, hence I'm allowed to access private members :p 
    462522                filter._label = label; 
    463                 filter._test = test; 
    464523                filter._active = active; 
    465524                filter._type = type; 
     
    467526 
    468527                // this might throw! 
    469                 filter.isRegex = isRegex; 
     528                filter.expression = expression; 
     529 
    470530 
    471531                // will call our observer so we re-init... no need to do more work here :p 
     
    487547                                try { 
    488548                                        f.save(); 
    489                                 } catch (ex) { 
    490                                         error(ex); 
     549                                } 
     550                                catch (ex) { 
     551                                        debug(ex); 
    491552                                } 
    492553                        }, 
    493554                        this 
    494555                ); 
     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; 
    495568        }, 
    496569 
     
    500573                        return this; 
    501574                } 
    502                 throw Components.results.NS_ERROR_NO_INTERFACE; 
     575                throw NS_ERROR_NO_INTERFACE; 
    503576        }, 
    504577 
     
    576649                        return this; 
    577650                } 
    578                 throw Components.results.NS_ERROR_NO_INTERFACE; 
     651                throw NS_ERROR_NO_INTERFACE; 
    579652        }, 
    580653        canUnload: function(compMgr) { 
     
    588661                } 
    589662 
    590                 return Components.results.NS_ERROR_NO_INTERFACE; 
     663                return NS_ERROR_NO_INTERFACE; 
    591664        }, 
    592665        createInstance: function (outer, iid) { 
    593666                if (outer != null) { 
    594                         throw Components.results.NS_ERROR_NO_AGGREGATION; 
     667                        throw NS_ERROR_NO_AGGREGATION; 
    595668                } 
    596669                if (FilterManager.implementsIID(iid)) { 
    597670                        return FilterManager; 
    598671                } 
    599                 throw Components.results.NS_ERROR_INVALID_ARG; 
     672                throw NS_ERROR_INVALID_ARG; 
    600673        } 
    601674} 
  • trunk/defaults/preferences/filters.js

    r538 r797  
    3838pref("extensions.dta.filters.deffilter-all.label", "All files"); 
    3939pref("extensions.dta.filters.deffilter-all.test", "/.*/i"); 
    40 pref("extensions.dta.filters.deffilter-all.regex", true); 
    4140pref("extensions.dta.filters.deffilter-all.active", false); 
    4241pref("extensions.dta.filters.deffilter-all.type", 3); 
     
    4443pref("extensions.dta.filters.deffilter-arch.label", "Archives)"); 
    4544pref("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); 
    4745pref("extensions.dta.filters.deffilter-arch.active", false); 
    4846pref("extensions.dta.filters.deffilter-arch.type", 1); 
     
    5048pref("extensions.dta.filters.deffilter-vid.label", "Videos"); 
    5149pref("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); 
    5350pref("extensions.dta.filters.deffilter-vid.active", true); 
    5451pref("extensions.dta.filters.deffilter-vid.type", 3); 
     
    5653pref("extensions.dta.filters.deffilter-aud.label", "Audio"); 
    5754pref("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); 
    5955pref("extensions.dta.filters.deffilter-aud.active", true); 
    6056pref("extensions.dta.filters.deffilter-aud.type", 1); 
     
    6258pref("extensions.dta.filters.deffilter-img.label", "Images"); 
    6359pref("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); 
    6560pref("extensions.dta.filters.deffilter-img.active", true); 
    6661pref("extensions.dta.filters.deffilter-img.type", 3); 
     
    6863pref("extensions.dta.filters.deffilter-bin.label", "Software"); 
    6964pref("extensions.dta.filters.deffilter-bin.test", "/\\.(?:exe|msi|dmg|bin|xpi|iso)$/i"); 
    70 pref("extensions.dta.filters.deffilter-bin.regex", true); 
    7165pref("extensions.dta.filters.deffilter-bin.active", true); 
    7266pref("extensions.dta.filters.deffilter-bin.type", 1); 
     
    7468pref("extensions.dta.filters.deffilter-imgjpg.label", "JPEG"); 
    7569pref("extensions.dta.filters.deffilter-imgjpg.test", "/\\.jp(e?g|e|2)$/i"); 
    76 pref("extensions.dta.filters.deffilter-imgjpg.regex", true); 
    7770pref("extensions.dta.filters.deffilter-imgjpg.active", false); 
    7871pref("extensions.dta.filters.deffilter-imgjpg.type", 3); 
     
    8073pref("extensions.dta.filters.deffilter-imggif.label", "GIF"); 
    8174pref("extensions.dta.filters.deffilter-imggif.test", "/\\.gif$/i"); 
    82 pref("extensions.dta.filters.deffilter-imggif.regex", true); 
    8375pref("extensions.dta.filters.deffilter-imggif.active", false); 
    8476pref("extensions.dta.filters.deffilter-imggif.type", 2); 
     
    8678pref("extensions.dta.filters.deffilter-imgpng.label", "PNG"); 
    8779pref("extensions.dta.filters.deffilter-imgpng.test", "/\\.png$/i"); 
    88 pref("extensions.dta.filters.deffilter-imgpng.regex", true); 
    8980pref("extensions.dta.filters.deffilter-imgpng.active", false); 
    9081pref("extensions.dta.filters.deffilter-imgpng.type", 2);