Changeset 73

Show
Ignore:
Timestamp:
2007-02-03 18:38:31 (2 years ago)
Author:
MaierMan
Message:

some more error handling.
implementing resetBranch as it is NOT_IMPLEMENTED by XPCOM.
labels will only get localized if user did not edit them.
create returns id of new filter.

Files:

Legend:

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

    r37 r73  
    6969        boolean matchActive(in string test, in unsigned long type); 
    7070         
    71         void create(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); 
    7272        void remove(in string id); 
    7373         
  • trunk/components/filterManager.js

    r70 r73  
    3434 * 
    3535 * ***** 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); 
    3838 
    3939const CC = Components.classes; 
     
    5858        LINK_FILTER: (1 << 0), 
    5959        IMAGE_FILTER: (1 << 1), 
    60          
     60 
    6161        _modified: false, 
    62          
     62 
    6363        // nsIClassInfo 
    6464        classID: Components.ID("{3F872ADC-35A4-4c79-B771-F2BC130FB792}"), 
     
    7373        getInterfaces: function(count) { 
    7474                // XXX 
    75                 count.value = 0;  
    76                 return null;  
    77         },      
    78          
     75                count.value = 0; 
     76                return null; 
     77        }, 
     78 
    7979        QueryInterface: function F_QI(iid) { 
    8080                if ( 
     
    8787                throw Components.results.NS_ERROR_NO_INTERFACE; 
    8888        }, 
    89          
     89 
    9090        // exported 
    9191        get id() { 
    9292                return this._id; 
    9393        }, 
    94          
     94 
    9595        // exported 
    9696        get defFilter() { 
    9797                return this._defFilter; 
    9898        }, 
    99          
     99 
    100100        // exported 
    101101        get label() { 
     
    109109                this._modified = true; 
    110110        }, 
    111          
     111 
    112112        // exported 
    113113        get test() { 
     
    115115        }, 
    116116        set test(value) { 
     117                if (this._defFilter) { 
     118                        throw new Components.Exception("default filters cannot be modified!"); 
     119                } 
    117120                if (this._test == value) { 
    118121                        return; 
     
    128131                this._modified = true; 
    129132        }, 
    130          
     133 
    131134        // exported 
    132135        get active() { 
     
    140143                this._modified = true; 
    141144        }, 
    142          
     145 
    143146        // exported 
    144147        get isRegex() { 
     
    146149        }, 
    147150        set isRegex(value) { 
     151                if (this._defFilter) { 
     152                        throw new Components.Exception("default filters cannot be deleted!"); 
     153                } 
    148154                if (this._isRegex == value) { 
    149155                        return; 
     
    159165                this._modified = true; 
    160166        }, 
    161          
     167 
    162168        // exported 
    163169        get type() { 
     
    165171        }, 
    166172        set type(t) { 
     173                if (this._defFilter) { 
     174                        throw new Components.Exception("default filters cannot be modified!"); 
     175                } 
    167176                if (this._type == t) { 
    168177                        return; 
     
    171180                this._modified = true; 
    172181        }, 
    173                  
     182 
    174183        _createRegex: function F_createRegex() { 
    175184                this._regex = this._isRegex ? DTA_regToRegExp(this._test) : DTA_strToRegExp(this._test); 
     
    183192                return str.search(this._regex) != -1; 
    184193        }, 
    185          
     194 
    186195        /** 
    187196         * @throws Exception in case loading failed 
    188197         */ 
    189         load: function F_load() {               
     198        load: function F_load(localizedLabel) { 
    190199                this._label = this.getMultiBytePref(this.pref('label')); 
    191200                if (!this._label || !this._label.length) { 
    192201                        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; 
    193206                } 
    194207                this._test = this.getMultiBytePref(this.pref('test')); 
     
    207220                } 
    208221                this._prefs.setBoolPref(this.pref('active'), this._active); 
    209                  
     222 
    210223                // do not change defFilters 
    211224                if (!this.defFilter) { 
     
    213226                        this._prefs.setIntPref(this.pref('type'), this._type); 
    214227                        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 
    219233                this._modified = false; 
    220234        }, 
    221          
     235 
    222236        // exported 
    223237        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 
    227249        getMultiBytePref: function F_getMultiBytePref(pref) { 
    228250                var rv = this._prefs.getComplexValue( 
     
    232254                return rv.data; 
    233255        }, 
    234          
     256 
    235257        setMultiBytePref: function F_setMultiBytePref(pref, value) { 
    236258                var str = CC["@mozilla.org/supports-string;1"] 
     
    241263                        CI.nsISupportsString, 
    242264                        str 
    243                 );                      
     265                ); 
    244266        } 
    245267}; 
     
    261283                        return 1; 
    262284                } 
    263                 var i = a.label.toLower(), ii = b.label.toLower(); 
     285                var i = a.label.toLowerCase(), ii = b.label.toLowerCase(); 
    264286                return i < ii ? -1 : (i > ii ? 1 : 0); 
    265287        }); 
     
    301323        getInterfaces: function(count) { 
    302324                // XXX 
    303                 count.value = 0;  
    304                 return null;  
     325                count.value = 0; 
     326                return null; 
    305327        }, 
    306328 
     
    315337                                this.classID 
    316338                        ].some(function(e) { return iid.equals(e); }); 
    317         },      
    318          
     339        }, 
     340 
    319341        _done: true, 
    320342        _mustReload: true, 
     
    327349        _init: function FM_init() { 
    328350                this._prefs = this._prefs.QueryInterface(CI.nsIPrefBranch2); 
    329                  
     351 
    330352                // load those localized labels for default filters. 
    331353                this._labels = {}; 
     
    338360                        this._labels[prop.key] = prop.value; 
    339361                } 
    340                  
     362 
    341363                // register (the observer) and initialize our timer, so that we'll get a reload event. 
    342364                this.register(); 
     
    347369                ); 
    348370        }, 
    349          
     371 
    350372        _delayedReload: function FM_delayedReload() { 
    351373                this._mustReload = true; 
    352374                this._timer.delay = 100; 
    353375        }, 
    354          
     376 
    355377        get count() { 
    356378                return this._count; 
     
    363385                this._mustReload = false; 
    364386 
    365                 //error("DTAFM: reload");               
     387                //error("DTAFM: reload"); 
    366388                this._filters = {}; 
    367389                this._count = 0; 
    368                  
     390 
    369391                // hmmm. since we use uuids for the filters we've to enumerate the whole branch. 
    370392                var c = {value: 0}; 
    371393                var prefs = this._prefs.getChildList('', c); 
    372                  
     394 
    373395                for (var i = 0; i < c.value; ++i) { 
    374396                        // we test for label (as we get all the other props as well) 
     
    378400                        // cut of the label part to get the actual name 
    379401                        var name = prefs[i].slice(0, -6); 
    380                          
     402 
    381403                        try { 
    382404                                var filter = new Filter(name, this._prefs); 
    383                                 filter.load(); 
    384405                                // overwrite with localized labels. 
     406                                var localizedLabel = null; 
    385407                                if (filter.id in this._labels) { 
    386                                         filter._label = this._labels[filter.id]; 
     408                                        localizedLabel = this._labels[filter.id]; 
    387409                                } 
     410                                filter.load(localizedLabel); 
    388411                                this._filters[filter.id] = filter; 
    389412                                this._count++; 
     
    393416                        } 
    394417                } 
    395          
     418 
    396419                // notify all observers 
    397420                var observerService = CC["@mozilla.org/observer-service;1"] 
     
    399422                observerService.notifyObservers(this, 'DTA:filterschanged', null); 
    400423        }, 
    401          
     424 
    402425        enumAll: function FM_enumAll() { 
    403426                var a = []; 
     
    416439                return new FilterEnumerator(a); 
    417440        }, 
    418          
     441 
    419442        getFilter: function FM_getFilter(id) { 
    420443                if (id in this._filters) { 
     
    429452                return e._filters.some(function(i) { return i.match(test); }); 
    430453        }, 
    431          
     454 
    432455        create: function FM_create(label, test, active, type, isRegex) { 
    433                  
     456 
    434457                // we will use unique ids for user-supplied filters. 
    435458                // no need to keep track of the actual number of filters or an index. 
     
    437460                        .getService(CI.nsIUUIDGenerator) 
    438461                        .generateUUID(); 
    439                          
    440                 //  
     462 
     463                // 
    441464                var filter = new Filter(uuid.toString(), this._prefs); 
    442465                // I'm a friend, hence I'm allowed to access private members :p 
     
    446469                filter._type = type; 
    447470                filter._modified = true; 
    448                  
     471 
    449472                // this might throw! 
    450473                filter.isRegex = isRegex; 
    451                          
     474 
    452475                // will call our observer so we re-init... no need to do more work here :p 
    453476                filter.save(); 
    454                  
    455         }, 
    456          
     477                return filter.id; 
     478        }, 
     479 
    457480        remove: function FM_remove(id) { 
    458481                if (id in this._filters) { 
    459482                        this._filters[id].remove(); 
    460                 } 
    461         }, 
    462          
     483                        return; 
     484                } 
     485                throw new Components.Exception('filter not defined!'); 
     486        }, 
     487 
    463488        save: function FM_save() { 
    464489                var e = this.enumAll(); 
     
    472497                } 
    473498        }, 
    474          
     499 
    475500                // nsiSupports 
    476501        QueryInterface: function FM_QI(iid) { 
     
    480505                throw Components.results.NS_ERROR_NO_INTERFACE; 
    481506        }, 
    482                  
     507 
    483508        // nsiWeakReference 
    484509        QueryReferent: function FM_QR(iid) { 
     
    495520                this._delayedReload(); 
    496521        }, 
    497          
     522 
    498523        // own stuff 
    499524        register: function FM_register() { 
     
    508533                return true; 
    509534        }, 
    510          
     535 
    511536        // nsITimerCallback 
    512537        notify: function FM_notify() { 
     
    514539                this.reload(); 
    515540        } 
    516          
     541 
    517542}; 
    518543FilterManager._init(); 
     
    520545var Module = { 
    521546        _firstTime: true, 
    522          
     547 
    523548        registerSelf: function M_registerSelf(compMgr, fileSpec, location, type) { 
    524549                if (!this._firstTime) { 
     
    526551                } 
    527552                this._firstTime = false; 
    528                  
    529     compMgr.QueryInterface(CI.nsIComponentRegistrar) 
     553 
     554               compMgr.QueryInterface(CI.nsIComponentRegistrar) 
    530555                        .registerFactoryLocation( 
    531556                                FilterManager.classID, 
    532557                                FilterManager.classDescription, 
    533                                 FilterManager.contractID,  
     558                                FilterManager.contractID, 
    534559                                fileSpec, 
    535                                 location,  
     560                                location, 
    536561                                type 
    537562                        ); 
     
    539564                        .getService(CI.nsICategoryManager) 
    540565                        .addCategoryEntry('app-startup', FilterManager.contractID, FilterManager.contractID, true, true, null); 
    541   }, 
     566       }, 
    542567        unregisterSelf: function(compMgr, fileSpec, location) { 
    543568                compMgr.QueryInterface(CI.nsIComponentRegistrar) 
     
    546571                                fileSpec 
    547572                        ); 
    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); 
    551576        }, 
    552577        getClassObject: function (compMgr, cid, iid) { 
     
    559584                return true; 
    560585        }, 
    561          
     586 
    562587        // 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       }, 
    570595        createInstance: function (outer, iid) { 
    571596                if (outer != null) { 
     
    576601                } 
    577602                throw Components.results.NS_ERROR_INVALID_ARG; 
    578   }      
     603        } 
    579604} 
    580605 
    581606// entrypoint 
    582607function NSGetModule(compMgr, fileSpec) { 
    583   return Module; 
     608       return Module; 
    584609}