Changeset 1022

Show
Ignore:
Timestamp:
08/01/08 07:32:07 (1 month ago)
Author:
MaierMan
Message:

Some more refactoring/modularization

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/chrome/content/common/internalFunctions.js

    r1018 r1022  
    6868const CANCELED =  1<<5; 
    6969const QUEUED =    1<<6; 
    70 /** 
    71  * cast non-strings to string 
    72  * @author Nils 
    73  * @param Arbitrary data 
    74  * @return a string 
    75  */ 
    76 function _atos(data) { 
    77         if (typeof(data) == 'string') { 
    78                 return data; 
    79         } 
    80         if (data instanceof String || typeof(data) == 'object') { 
    81                 try { 
    82                         return data.toSource(); 
    83                 } 
    84                 catch (ex) { 
    85                         // fall-trough 
    86                 } 
    87         } 
    88          
    89         return String(data); 
    90 
     70 
     71 
     72 
    9173 
    9274/** 
     
    11193        return elements; 
    11294} 
    113  
    114 function merge(me, that) { 
    115         for (let c in that) { 
    116                 me[c] = that[c]; 
    117         } 
    118 } 
    119  
    120 // not instanceof save, you know ;) 
    121 function clone(obj) { 
    122         var rv = {}; 
    123         merge(rv, obj); 
    124         merge(rv.prototype, this.prototype); 
    125         rv.constructor = this.constructor; 
    126         return rv; 
    127 } 
    128 merge( 
    129         String.prototype, 
    130         {  
    131                 trim: function() { 
    132                         return this.replace(/^\s+|\s+$/g, ''); 
    133                 }, 
    134                 removeBadChars: function() { 
    135                         return this 
    136                                 .replace(/[\n\r\v?:<>*|"]/g, '_') 
    137                                 .replace(/%(?:25)?20/g, ' '); 
    138                 }, 
    139                 addFinalSlash: function() { 
    140                         if (this.length == 0) { 
    141                                 return SYSTEMSLASH; 
    142                         } 
    143                          
    144                         if (this[this.length - 1] != SYSTEMSLASH) { 
    145                                 return this + SYSTEMSLASH; 
    146                         } 
    147                         return this; 
    148                 }, 
    149                 removeFinalChar: function(c) { 
    150                         if (this.length == 0) { 
    151                                 return this; 
    152                         } 
    153                         if (this[this.length - 1] == c) { 
    154                                 return this.substring(0, this.length - 1); 
    155                         } 
    156                         return this; 
    157                 }, 
    158                 removeLeadingChar: function(c) { 
    159                         if (this.length == 0) { 
    160                                 return this; 
    161                         } 
    162                         if (this[0] == c) { 
    163                                 return this.slice(1); 
    164                         } 
    165                         return this; 
    166                 }, 
    167                 removeFinalSlash: function() { 
    168                         return this.removeFinalChar(SYSTEMSLASH); 
    169                 }, 
    170                 replaceSlashes: function(replaceWith) { 
    171                         return this.replace(/[\\/]/g, replaceWith); 
    172                 }, 
    173                 normalizeSlashes: function() { 
    174                         return this.replaceSlashes(SYSTEMSLASH); 
    175                 }, 
    176                 removeLeadingSlash: function() { 
    177                         return this.removeLeadingChar(SYSTEMSLASH); 
    178                 }, 
    179                 getUsableFileName: function() { 
    180                         let t = this.replace(/\?.*$/, '') 
    181                                 .normalizeSlashes() 
    182                                 .trim() 
    183                                 .removeFinalSlash(); 
    184                         return t.split(SYSTEMSLASH).pop().removeBadChars().trim(); 
    185                 }, 
    186                 getExtension: function() { 
    187                         let name = this.getUsableFileName(); 
    188                         let c = name.lastIndexOf('.'); 
    189                         if (c == -1) { 
    190                                 return null; 
    191                         } 
    192                         return name.slice(c + 1); 
    193                 }, 
    194                 cropCenter : function(newLength) { 
    195                         if (this.length > newLength) { 
    196                                 return this.substring(0, newLength / 2) + "..." + this.substring(this.length - newLength / 2, this.length); 
    197                         } 
    198                         return this; 
    199                 }, 
    200                 toURI: function(charset, baseURI) { 
    201                         return IOService.newURI(this, charset, baseURI);                         
    202                 }, 
    203                 toURL: function(charset, baseURI) { 
    204                         return this.toURI(charset, baseURI).QueryInterface(Components.interfaces.nsIURL); 
    205                 } 
    206         } 
    207 ); 
    20895 
    20996var Utils = { 
     
    307194                } 
    308195        }, 
    309         /** 
    310          * returns a numeric timestamp 
    311          * @param date Optional. DateString to get stamp for. NOW if ommitted 
    312          * @return Numeric timestamp 
    313          * @author Nils 
    314         */ 
    315         getTimestamp: function(str) { 
    316                 if (!str) { 
    317                         return Date.now(); 
    318                 } 
    319                 if (typeof(str) != 'string' && !(str instanceof String)) { 
    320                         throw new Error("not a string"); 
    321                 } 
    322                 var rv = Date.parse(str); 
    323                 if (!isFinite(rv)) { 
    324                         throw new Error("invalid date"); 
    325                 } 
    326                 return rv; 
    327         }, 
    328196 
    329197        /** 
     
    333201         */ 
    334202        formatBytes: function U_formatBytes(aNumber) { 
     203                const formatBytes_units = [['sizeB', 0], ['sizeKB', 1], ['sizeMB', 2], ['sizeGB', 2], ['sizeTB', 3]]; 
     204                const formatBytes_nunits = formatBytes_units.length; 
     205 
    335206                aNumber = Number(aNumber); 
    336          
    337                  
    338                 var units = [['sizeTB', 3], ['sizeGB', 2], ['sizeMB', 2], ['sizeKB', 1], ['sizeB', 0]]; 
    339                 var unit = units.pop(); 
    340                  
    341                 while (aNumber > 875 && units.length) { 
     207                 
     208                if (!isFinite(aNumber)) { 
     209                        return 'NaN'; 
     210                } 
     211                 
     212                let unit = formatBytes_units[0]; 
     213                 
     214                for (let i = 1; aNumber > 875 && i < formatBytes_nunits; ++i) { 
    342215                        aNumber /= 1024; 
    343                         unit = units.pop()
     216                        unit = formatBytes_units[i]
    344217                } 
    345218                 
    346219                return _(unit[0], [aNumber.toFixed(unit[1])]); 
    347220        }, 
    348          
    349         /** 
    350          * returns a pretty number containing at least specified number of digits 
    351          * @param aNumber the number to format 
    352          * @param aDigists Optional. Number of digits the result must at least have 
    353          * @author Nils 
    354          */ 
    355         formatNumber: function U_formatNumber(rv, digits) { 
    356                 rv = _atos(rv); 
    357                 if (typeof(digits) != 'number') { 
    358                         digits = 3; 
    359                 } 
    360                 while (rv.length < digits) { 
    361                         rv = '0' + rv; 
    362                 } 
    363                 return rv; 
    364         }, 
    365         /** 
    366          * formats a time-delta. At least minutes and seconds are given back 
    367          */ 
    368         formatTimeDelta: function U_formatTimeDelta(aDelta) { 
    369                 var h = Math.floor(aDelta / 3600); 
    370                 var m = Math.floor((aDelta % 3600) / 60); 
    371                 var s = Math.floor(aDelta % 60); 
    372                 if (h) { 
    373                         return this.formatNumber(h, 2) + ":" + this.formatNumber(m, 2) + ":" + this.formatNumber(s, 2); 
    374                 } 
    375                 return this.formatNumber(m, 2) + ":" + this.formatNumber(s, 2); 
    376         }, 
    377          
     221 
     222 
    378223        formatConflictName: function U_formatConflictName(basename, conflicts) { 
    379224                if (!conflicts) { 
     
    389234}; 
    390235 
    391 function _getIcon(url, size) { 
    392         if (/mac/i.test(navigator.platform)) { 
    393                 const _recognizedMac = /\.(?:gz|zip|gif|jpe?g|jpe|mp3|pdf|avi|mpe?g)$/i; 
    394                 _getIcon = function _getIconMac(url, size) { 
    395                         let uri = url.toURI(); 
    396                         if (_recognizedMac.test(uri.path)) { 
    397                                 return "moz-icon://" + url + "?size=" + size; 
    398                         } 
    399                         return "moz-icon://file.html?size=" + size; 
    400                 }; 
    401         } 
    402         else { 
    403                 _getIcon = function _getIconOther(url, size) { 
    404                         return "moz-icon://" + url + "?size=" + size; 
    405                 }; 
    406         } 
    407         return _getIcon(url, size); 
    408 }; 
     236Components.utils.import('resource://dta/utils.jsm', Utils); 
     237 
     238Utils.merge( 
     239        String.prototype, 
     240        {  
     241                trim: function() { 
     242                        return this.replace(/^\s+|\s+$/g, ''); 
     243                }, 
     244                removeBadChars: function() { 
     245                        return this 
     246                                .replace(/[\n\r\v?:<>*|"]/g, '_') 
     247                                .replace(/%(?:25)?20/g, ' '); 
     248                }, 
     249                addFinalSlash: function() { 
     250                        if (this.length == 0) { 
     251                                return SYSTEMSLASH; 
     252                        } 
     253                         
     254                        if (this[this.length - 1] != SYSTEMSLASH) { 
     255                                return this + SYSTEMSLASH; 
     256                        } 
     257                        return this; 
     258                }, 
     259                removeFinalChar: function(c) { 
     260                        if (this.length == 0) { 
     261                                return this; 
     262                        } 
     263                        if (this[this.length - 1] == c) { 
     264                                return this.substring(0, this.length - 1); 
     265                        } 
     266                        return this; 
     267                }, 
     268                removeLeadingChar: function(c) { 
     269                        if (this.length == 0) { 
     270                                return this; 
     271                        } 
     272                        if (this[0] == c) { 
     273                                return this.slice(1); 
     274                        } 
     275                        return this; 
     276                }, 
     277                removeFinalSlash: function() { 
     278                        return this.removeFinalChar(SYSTEMSLASH); 
     279                }, 
     280                replaceSlashes: function(replaceWith) { 
     281                        return this.replace(/[\\/]/g, replaceWith); 
     282                }, 
     283                normalizeSlashes: function() { 
     284                        return this.replaceSlashes(SYSTEMSLASH); 
     285                }, 
     286                removeLeadingSlash: function() { 
     287                        return this.removeLeadingChar(SYSTEMSLASH); 
     288                }, 
     289                getUsableFileName: function() { 
     290                        let t = this.replace(/\?.*$/, '') 
     291                                .normalizeSlashes() 
     292                                .trim() 
     293                                .removeFinalSlash(); 
     294                        return t.split(SYSTEMSLASH).pop().removeBadChars().trim(); 
     295                }, 
     296                getExtension: function() { 
     297                        let name = this.getUsableFileName(); 
     298                        let c = name.lastIndexOf('.'); 
     299                        return (c == - 1) ? null : name.slice(++c); 
     300                }, 
     301                cropCenter : function(newLength) { 
     302                        if (this.length > newLength) { 
     303                                return this.substring(0, newLength / 2) + "..." + this.substring(this.length - newLength / 2, this.length); 
     304                        } 
     305                        return this; 
     306                }, 
     307                toURI: function(charset, baseURI) { 
     308                        return IOService.newURI(this, charset, baseURI);                         
     309                }, 
     310                toURL: function(charset, baseURI) { 
     311                        return this.toURI(charset, baseURI).QueryInterface(Components.interfaces.nsIURL); 
     312                } 
     313        } 
     314); 
     315 
    409316 
    410317/** 
     
    418325 */ 
    419326function getIcon(link, metalink, size) { 
     327 
     328        const _recognizedMac = /\.(?:gz|zip|gif|jpe?g|jpe|mp3|pdf|avi|mpe?g)$/i; 
     329 
     330        function _getIconMac(url, size) { 
     331                let uri = url.toURI(); 
     332                if (_recognizedMac.test(uri.path)) { 
     333                        return "moz-icon://" + url + "?size=" + size; 
     334                } 
     335                return "moz-icon://file.html?size=" + size; 
     336        } 
     337         
     338        function _getIconOther(url, size) { 
     339                return "moz-icon://" + url + "?size=" + size; 
     340        }; 
     341         
     342        function _getIcon(url, size) { 
     343                if (/mac/i.test(navigator.platform)) { 
     344                        _getIcon = _getIconMac;  
     345                } 
     346                else { 
     347                        _getIcon = _getIconOther; 
     348                } 
     349                return _getIcon(url, size); 
     350        }; 
     351 
    420352        if (metalink) { 
    421353                return "chrome://dta/skin/icons/metalink.png"; 
     
    436368                } 
    437369                else { 
    438                         url = _atos(link); 
     370                        url = link.toString(); 
    439371                } 
    440372                let ext = url.getExtension(); 
     
    569501}; 
    570502 
    571 /** 
    572  * Range generator (python style). Difference: step direction is initialized accordingly if corresponding parameter is omitted. 
    573  * @param start Optional. Start value (default: 0) 
    574  * @param stop Stop value (exclusive) 
    575  * @param step Optional. Step value (default: 1/-1) 
    576  * @author Nils 
    577  */ 
    578 function range() { 
    579         if (arguments.length == 0) { 
    580                 throw Components.results.NS_ERROR_INVALID_ARG; 
    581         } 
    582         var start = 0, stop = Number(arguments[0]), step; 
    583         if (arguments.length >= 2) { 
    584                 start = stop; 
    585                 stop = Number(arguments[1]); 
    586         } 
    587         if (arguments.length >= 3) { 
    588                 step = Number(arguments[2]); 
    589         } 
    590         else { 
    591                 step = stop - start > 0 ? 1 : -1;  
    592         } 
    593         if (!isFinite(start) || !isFinite(stop) || !isFinite(step) || step == 0) { 
    594                 throw Components.results.NS_ERROR_INVALID_ARG; 
    595         } 
    596         if ((stop - start) / step < 0) { 
    597                 // negative range 
    598                 return; 
    599         } 
    600         stop += -Math.abs(step)/step; 
    601         stop += step - ((stop - start) % step); 
    602         for (; start != stop; start += step) { 
    603                 yield start; 
    604         } 
    605  
    606 } 
    607  
    608 /** 
    609  * Convert string-castable data int a hexdigest string 
    610  * @param data String-castable data to hash 
    611  * @return The hex digest of given data 
    612  * @author Nils (derived from dmo example) 
    613  */ 
    614 function hexdigest(data) { 
    615         data = _atos(data); 
    616         // range is required as we extended String 
    617         return [("0" + data.charCodeAt(i).toString(16)).slice(-2) for (i in range(data.length))].join("");       
    618 } 
    619503 
    620504/** 
     
    655539                converter.charset = 'utf8'; 
    656540                 
    657                 value = converter.convertToByteArray(_atos(value), {}); 
     541                value = converter.convertToByteArray(Utils.atos(value), {}); 
    658542                ch.update(value, value.length); 
    659543        } 
    660544        var rv = ch.finish(encoding == HASH_B64); 
    661545        if (encoding == HASH_HEX) { 
    662                 rv = hexdigest(rv); 
     546                rv = Utils.hexdigest(rv); 
    663547        } 
    664548        return rv; 
    665549} 
    666550 
    667 /** 
    668  * returns a new UUID in string representation 
    669  * @return String UUID 
    670  * @author Nils 
    671  */ 
    672 function newUUIDString() { 
    673         var uuidgen = Components.classes["@mozilla.org/uuid-generator;1"] 
    674                 .getService(Components.interfaces.nsIUUIDGenerator); 
    675         newUUIDString = function() { 
    676                 return uuidgen.generateUUID().toString(); 
    677         } 
    678         return newUUIDString(); 
    679 } 
    680551 
    681552// XXX switch to nsITimer? 
    682553function Timer(func, interval, persist, now) { 
    683   this._id = newUUIDString(); 
     554  this._id = Utils.newUUIDString(); 
    684555        if (typeof(func) != 'function') { 
    685556                func = new Function(func); 
  • trunk/chrome/content/dta/addurl.js

    r993 r1022  
    8686        }, 
    8787        join: function(str) { 
    88                 for (let i in range(this.start, this.stop, this.step)) { 
     88                for (let i in Utils.range(this.start, this.stop, this.step)) { 
    8989                        yield (str + this._format(i)); 
    9090                } 
  • trunk/chrome/content/dta/manager.js

    r1021 r1022  
    10171017                                name = name.substring(0, 60); 
    10181018                        } 
    1019                         dest.append(name + "-" + newUUIDString() + '.dtapart'); 
     1019                        dest.append(name + "-" + Utils.newUUIDString() + '.dtapart'); 
    10201020                        this._tmpFile = dest; 
    10211021                } 
     
    10441044                let state = this._state; 
    10451045                for (let i = 0, e = arguments.length; i < e; ++i) { 
    1046                         if (state & arguments[i]) { 
     1046                        if (state == arguments[i]) { 
    10471047                                return true; 
    10481048                        } 
     
    17131713                ); 
    17141714                if (this.hash) { 
    1715                         e.hash = _atos(this.hash.sum); 
    1716                         e.hashType = _atos(this.hash.type); 
     1715                        e.hash = Utils.atos(this.hash.sum); 
     1716                        e.hashType = Utils.atos(this.hash.type); 
    17171717                } 
    17181718                if (this.autoRetrying) { 
  • trunk/chrome/content/dta/manager/customevent.js

    r740 r1022  
    4343                function callback(u) { 
    4444                        u = u.substr(1, u.length - 2); 
    45                         id = newUUIDString(); 
     45                        id = Utils.newUUIDString(); 
    4646                        uuids[id] = u; 
    4747                        return id; 
  • trunk/chrome/content/dta/manager/tree.js

    r1021 r1022  
    570570                        modifySome($('delete'), function(d) d.is(COMPLETE)); 
    571571                        modifySome($('export'), function(d) !!d.count); 
    572                         modifySome($('addchunk', 'removechunk', 'force'), function(d) d.isOf(QUEUED, RUNNING, PAUSED)); 
     572                        modifySome($('addchunk', 'removechunk', 'force'), function(d) d.isOf(QUEUED, RUNNING, PAUSED, CANCELED)); 
    573573                } 
    574574                catch (ex) { 
  • trunk/chrome/content/dta/manager/verificator.js

    r986 r1022  
    8888                        this.download.invalidate(); 
    8989                         
    90                         this.hash = hexdigest(this.hash.finish(false)); 
     90                        this.hash = Utils.hexdigest(this.hash.finish(false)); 
    9191                        if (this.hash != this.download.hash.sum) { 
    9292                                Debug.logString("hash mismatch, actual: " + this.hash + " expected: " + this.download.hash.sum);