Changeset 999

Show
Ignore:
Timestamp:
07/30/08 00:23:11 (1 month ago)
Author:
MaierMan
Message:

* Rework filtermanager to use preferences.jsm
* #822: Preferences Filters doesn't update

Files:

Legend:

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

    r985 r999  
    4343 
    4444const Exception = Components.Exception; 
     45const BASE = 'extensions.dta.filters.'; 
    4546 
    4647const NS_ERROR_NO_INTERFACE = Cr.NS_ERROR_NO_INTERFACE; 
     
    5152const LINK_FILTER = Ci.dtaIFilter.LINK_FILTER; 
    5253const IMAGE_FILTER = Ci.dtaIFilter.IMAGE_FILTER; 
     54const TOPIC_FILTERSCHANGED = 'DTA:filterschanged'; 
     55 
     56const nsITimer = Ci.nsITimer; 
     57const Timer = Components.Constructor('@mozilla.org/timer;1', 'nsITimer', 'init'); 
     58  
     59let Preferences = {}; 
    5360 
    5461// no not create DTA_Filter yourself, managed by DTA_FilterManager 
    55 function Filter(name, prefs) { 
     62function Filter(name) { 
    5663        this._id = name; 
    57         this._prefs = prefs; 
    5864} 
    5965Filter.prototype = { 
     
    179185        load: function F_load(localizedLabel) { 
    180186                this._localizedLabel = localizedLabel; 
    181                 this._label = this.getMultiBytePref(this.pref('label')); 
     187                this._label = Preferences.get(this.pref('label')); 
    182188                if (!this._label || !this._label.length) { 
    183189                        throw Components.Exception("Empty filter!"); 
    184190                } 
    185191                // 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'))) { 
    187193                        this._label = localizedLabel; 
    188194                } 
    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')); 
    192198                this._defFilter = this._id.search(/^deffilter/) != -1; 
    193199                 
    194200                // may throw 
    195                 this.expression = this.getMultiBytePref(this.pref('test')); 
     201                this.expression = Preferences.get(this.pref('test')); 
    196202                 
    197203                this._modified = false; 
     
    200206        // exported 
    201207        save: function F_save() { 
    202                 if (!this._prefs) { 
    203                         throw NS_ERROR_INVALID_ARG; 
    204                 } 
    205208                if (!this._modified) { 
    206209                        return; 
    207210                } 
    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); 
    212214                         
    213215                // 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); 
    215217 
    216218                this._modified = false; 
     
    218220 
    219221        _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); 
    228223        }, 
    229224 
     
    244239        }, 
    245240 
    246         getMultiBytePref: function F_getMultiBytePref(pref) { 
    247                 var rv = this._prefs.getComplexValue( 
    248                         pref, 
    249                         Ci.nsISupportsString 
    250                 ); 
    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                         str 
    262                 ); 
    263         }, 
    264241        toString: function() { 
    265242                return this._label + " (" + this._id + ")"; 
    266243        }, 
     244 
    267245        toSource: function() { 
    268246                return this.toString() + ": " + this._regs.toSource(); 
     
    305283var FilterManager = { 
    306284        _done: true, 
    307         _mustReload: true, 
     285        _mustReload: false, 
    308286         
    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, 
    315289 
    316290        init: function FM_init() { 
    317                 this._prefs = this._prefs.QueryInterface(Ci.nsIPrefBranch2); 
     291                Components.utils.import('resource://dta/preferences.jsm', Preferences); 
    318292 
    319293                // load those localized labels for default filters. 
    320294                this._localizedLabels = {}; 
    321                 var b = Cc['@mozilla.org/intl/stringbundle;1'] 
     295                let b = Cc['@mozilla.org/intl/stringbundle;1'] 
    322296                        .getService(Ci.nsIStringBundleService) 
    323297                        .createBundle("chrome://dta/locale/filters.properties"); 
    324                 var e = b.getSimpleEnumeration(); 
     298                let e = b.getSimpleEnumeration(); 
    325299                while (e.hasMoreElements()) { 
    326300                        var prop = e.getNext().QueryInterface(Ci.nsIPropertyElement); 
    327301                        this._localizedLabels[prop.key] = prop.value; 
    328302                } 
     303                 
     304                // init the observer service 
     305                this._obs = Cc["@mozilla.org/observer-service;1"] 
     306                        .getService(Ci.nsIObserverService); 
    329307 
    330308                // register (the observer) and initialize our timer, so that we'll get a reload event. 
    331309                this.register(); 
    332                 this._timer.initWithCallback( 
    333                         this, 
    334                         100, 
    335                         this._timer.TYPE_ONE_SHOT 
    336                 ); 
     310                this._delayedReload(); 
    337311                this.init = new Function(); 
    338312        }, 
    339313 
    340314        _delayedReload: function FM_delayedReload() { 
     315                if (this._mustReload) { 
     316                        return; 
     317                } 
    341318                this._mustReload = true; 
    342                 this._timer.delay = 100
     319                this._timer = new Timer(this, 100, nsITimer.TYPE_ONE_SHOT)
    343320        }, 
    344321 
     
    352329                } 
    353330                this._mustReload = false; 
     331                 
    354332 
    355333                this._filters = {}; 
    356334                this._all = []; 
    357                 this._count = 0; 
    358335 
    359336                // 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)) { 
    364338                        // we test for label (as we get all the other props as well) 
    365                         if (prefs[i].search(/\.label$/) == -1) { 
     339                        if (pref.search(/\.label$/) == -1) { 
    366340                                continue; 
    367341                        } 
    368342                        // 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); 
    371344                        try { 
    372                                 var filter = new Filter(name, this._prefs); 
     345                                let filter = new Filter(name); 
    373346                                // 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]; 
    377351                                } 
    378352                                filter.load(localizedLabel); 
    379353                                this._filters[filter.id] = filter; 
    380354                                this._all.push(filter); 
    381                                 this._count++; 
    382355                        } 
    383356                        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                 
    387363                this._all.sort( 
    388364                        function(a,b) { 
     
    406382 
    407383                // 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); 
    411390        }, 
    412391 
     
    439418                // we will use unique ids for user-supplied filters. 
    440419                // no need to keep track of the actual number of filters or an index. 
    441                 var uuid = Cc["@mozilla.org/uuid-generator;1"] 
     420                let uuid = Cc["@mozilla.org/uuid-generator;1"] 
    442421                        .getService(Ci.nsIUUIDGenerator) 
    443422                        .generateUUID(); 
    444423 
    445424                // 
    446                 var filter = new Filter(uuid.toString(), this._prefs); 
     425                let filter = new Filter(BASE + uuid.toString()); 
    447426                // I'm a friend, hence I'm allowed to access private members :p 
    448427                filter._label = label; 
     
    474453                        } 
    475454                        catch (ex) { 
    476                                 debug(ex); 
     455                                debug('Failed to save filters', ex); 
    477456                        } 
    478457                } 
     
    493472        // nsIObserver 
    494473        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                } 
    496480        }, 
    497481 
     
    500484                try { 
    501485                        // Put self as observer to desired branch 
    502                         this._prefs.addObserver("", this, true); 
     486                        Preferences.addObserver(BASE, this); 
    503487                } 
    504488                catch (ex) { 
     
    507491                } 
    508492                return true; 
    509         }, 
    510  
    511         // nsITimerCallback 
    512         notify: function FM_notify() { 
    513                 //error("DTAFM: notify"); 
    514                 this.reload(); 
    515493        } 
    516494}; 
     
    520498        "@downthemall.net/filtermanager;2", 
    521499        "DownThemAll! Filtermanager", 
    522         [Ci.nsITimerCallback, Ci.nsIObserver, Ci.dtaIFilterManager] 
     500        [Ci.nsIObserver, Ci.dtaIFilterManager] 
    523501); 
    524502