Changeset 1031

Show
Ignore:
Timestamp:
2008-08-02 17:24:51 (2 months ago)
Author:
MaierMan
Message:

#832: Asynchronously add new downloads

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/chrome/content/dta/manager.js

    r1029 r1031  
    163163                new Timer("Dialog.saveRunning();", 10000, true); 
    164164                 
    165                 $('loadingbox').parentNode.removeChild($('loadingbox'));               
     165                $('loadingbox').hidden = true;                 
    166166        }, 
    167167         
     
    25732573        Tree.beginUpdate(); 
    25742574        SessionManager.beginUpdate(); 
    2575         for (let e in g) { 
    2576  
    2577                 var desc = ""; 
    2578                 DESCS.some( 
    2579                         function(i) { 
    2580                                 if (typeof(e[i]) == 'string' && e[i].length) { 
    2581                                         desc = e.description; 
    2582                                         return true; 
    2583                                 } 
    2584                                 return false; 
    2585                         } 
    2586                 ); 
     2575        $('loadingbox').hidden = false; 
     2576        $('loading').value = _('adding'); 
     2577        Components.utils.import('resource://dta/cothread.jsm'); 
     2578        new CoThreadListWalker( 
     2579                function startDownloads_cothread(e) { 
     2580                        let desc = ""; 
     2581                        DESCS.some( 
     2582                                function(i) { 
     2583                                        if (typeof(e[i]) == 'string' && e[i].length) { 
     2584                                                desc = e.description; 
     2585                                                return true; 
     2586                                        } 
     2587                                        return false; 
     2588                                } 
     2589                        ); 
     2590                         
     2591                        let qi = new QueueItem(); 
     2592                        let lnk = e.url; 
     2593                        if (typeof lnk == 'string') { 
     2594                                qi.urlManager = new UrlManager([new DTA_URL(IOService.newURI(lnk, null, null))]); 
     2595                        } 
     2596                        else if (lnk instanceof UrlManager) { 
     2597                                qi.urlManager = lnk; 
     2598                        } 
     2599                        else { 
     2600                                qi.urlManager = new UrlManager([lnk]); 
     2601                        } 
     2602                        qi.numIstance = e.numIstance; 
    25872603                 
    2588                 let qi = new QueueItem(); 
    2589                 let lnk = e.url; 
    2590                 if (typeof lnk == 'string') { 
    2591                         qi.urlManager = new UrlManager([new DTA_URL(IOService.newURI(lnk, null, null))]); 
    2592                 } 
    2593                 else if (lnk instanceof UrlManager) { 
    2594                         qi.urlManager = lnk; 
    2595                 } 
    2596                 else { 
    2597                         qi.urlManager = new UrlManager([lnk]); 
    2598                 } 
    2599                 qi.numIstance = e.numIstance; 
     2604                        if (e.referrer) { 
     2605                                try { 
     2606                                        qi.referrer = e.referrer.toURL(); 
     2607                                } 
     2608                                catch (ex) { 
     2609                                        // We might have been fed with about:blank or other crap. so ignore. 
     2610                                } 
     2611                        } 
     2612                        // only access the setter of the last so that we don't generate stuff trice. 
     2613                        qi._pathName = e.dirSave.addFinalSlash().toString(); 
     2614                        qi._description = desc ? desc : ''; 
     2615                        qi._mask = e.mask; 
     2616                        qi.fromMetalink = !!e.fromMetalink; 
     2617                        if (e.fileName) { 
     2618                                qi.fileName = e.fileName; 
     2619                        } 
     2620                        else { 
     2621                                qi.fileName = qi.urlManager.usable.getUsableFileName(); 
     2622                        } 
     2623                        if (e.startDate) { 
     2624                                qi.startDate = e.startDate; 
     2625                        } 
     2626                        if (e.url.hash) { 
     2627                                qi.hash = e.url.hash; 
     2628                        } 
     2629                        else if (e.hash) { 
     2630                                qi.hash = e.hash; 
     2631                        } 
     2632                        else { 
     2633                                qi.hash = null; // to initialize prettyHash  
     2634                        } 
    26002635         
    2601                 if (e.referrer) { 
    2602                         try { 
    2603                                 qi.referrer = e.referrer.toURL(); 
    2604                         } 
    2605                         catch (ex) { 
    2606                                 // We might have been fed with about:blank or other crap. so ignore. 
    2607                         } 
    2608                 } 
    2609                 // only access the setter of the last so that we don't generate stuff trice. 
    2610                 qi._pathName = e.dirSave.addFinalSlash().toString(); 
    2611                 qi._description = desc ? desc : ''; 
    2612                 qi._mask = e.mask; 
    2613                 qi.fromMetalink = !!e.fromMetalink; 
    2614                 if (e.fileName) { 
    2615                         qi.fileName = e.fileName; 
    2616                 } 
    2617                 else { 
    2618                         qi.fileName = qi.urlManager.usable.getUsableFileName(); 
    2619                 } 
    2620                 if (e.startDate) { 
    2621                         qi.startDate = e.startDate; 
    2622                 } 
    2623                 if (e.url.hash) { 
    2624                         qi.hash = e.url.hash; 
    2625                 } 
    2626                 else if (e.hash) { 
    2627                         qi.hash = e.hash; 
    2628                 } 
    2629                 else { 
    2630                         qi.hash = null; // to initialize prettyHash  
    2631                 } 
    2632  
    2633                 let postData = ContentHandling.getPostDataFor(qi.urlManager.url); 
    2634                 if (e.url.postData) { 
    2635                         postData = e.url.postData; 
    2636                 } 
    2637                 if (postData) { 
    2638                         qi.postData = postData; 
    2639                 }                
    2640  
    2641                 qi.state = start ? QUEUED : PAUSED; 
    2642                 if (qi.is(QUEUED)) { 
    2643                         qi.status = TEXT_QUEUED; 
    2644                 } 
    2645                 else { 
    2646                         qi.status = TEXT_PAUSED; 
    2647                 } 
    2648                 qi.save();               
    2649                 Tree.add(qi); 
    2650                 ++added; 
    2651         } 
    2652         SessionManager.endUpdate(); 
    2653         Tree.endUpdate(); 
    2654  
    2655         var boxobject = Tree._box; 
    2656         boxobject.QueryInterface(Ci.nsITreeBoxObject); 
    2657         if (added <= boxobject.getPageLength()) { 
    2658                 boxobject.scrollToRow(Tree.rowCount - boxobject.getPageLength()); 
    2659         } 
    2660         else { 
    2661                 boxobject.scrollToRow(numbefore); 
    2662         } 
     2636                        let postData = ContentHandling.getPostDataFor(qi.urlManager.url); 
     2637                        if (e.url.postData) { 
     2638                                postData = e.url.postData; 
     2639                        } 
     2640                        if (postData) { 
     2641                                qi.postData = postData; 
     2642                        }                
     2643         
     2644                        qi.state = start ? QUEUED : PAUSED; 
     2645                        if (qi.is(QUEUED)) { 
     2646                                qi.status = TEXT_QUEUED; 
     2647                        } 
     2648                        else { 
     2649                                qi.status = TEXT_PAUSED; 
     2650                        } 
     2651                        qi.save();               
     2652                        Tree.add(qi); 
     2653                        ++added; 
     2654                         
     2655                        return true; 
     2656                }, 
     2657                g, 
     2658                100, 
     2659                this, 
     2660                function startDownloads_finish() { 
     2661                        SessionManager.endUpdate(); 
     2662                        Tree.endUpdate(); 
     2663 
     2664                        $('loadingbox').hidden = true; 
     2665                 
     2666                        let bo = Tree.box.QueryInterface(Ci.nsITreeBoxObject); 
     2667                        if (added <= bo.getPageLength()) { 
     2668                                bo.scrollToRow(Tree.rowCount - bo.getPageLength()); 
     2669                        } 
     2670                        else { 
     2671                                bo.scrollToRow(numbefore); 
     2672                        } 
     2673                         
     2674                        Dialog.startNext(); 
     2675                } 
     2676        ).run(); 
    26632677} 
    26642678const FileOutputStream = Components.Constructor( 
     
    27882802 
    27892803var Serializer = { 
     2804        json: Serv('@mozilla.org/dom/json;1', 'nsIJSON'), 
    27902805        encode: function(obj) { 
    2791                 if ('nsIJSON' in Ci) { 
    2792                         let json = Serv('@mozilla.org/dom/json;1', 'nsIJSON'); 
    2793                         this.encode = function(obj) { 
    2794                                 return json.encode(obj); 
    2795                         } 
    2796                 } 
    2797                 else { 
    2798                         this.encode = function(obj) { 
    2799                                 return obj.toSource(); 
    2800                         } 
    2801                 } 
    2802                 return this.encode(obj); 
     2806                return this.json.encode(obj); 
    28032807        }, 
    28042808        decode: function(str) { 
    2805                 if ('nsIJSON' in Ci) { 
    2806                         let json = Serv('@mozilla.org/dom/json;1', 'nsIJSON'); 
    2807                         this.decode = function(str) { 
    2808                                 try { 
    2809                                         return json.decode(str); 
    2810                                 } 
    2811                                 catch (ex) { 
    2812                                         return eval(str); 
    2813                                 } 
    2814                         } 
    2815                 } 
    2816                 else { 
    2817                         this.decode = function(str) { 
    2818                                 return eval(str); 
    2819                         } 
    2820                 } 
    2821                 return this.decode(str); 
     2809                try { 
     2810                        return this.json.decode(str); 
     2811                } 
     2812                catch (ex) { 
     2813                        return eval(str); 
     2814                } 
    28222815        } 
    28232816}; 
  • trunk/chrome/content/dta/manager/sessionmanager.js

    r1027 r1031  
    172172                let count = stmt.getInt64(0); 
    173173                stmt.finalize(); 
     174                delete stmt; 
    174175                if (!count) { 
    175176                        Dialog.start(); 
     
    188189                                // Are we done? 
    189190                                if (!stmt || !stmt.executeStep()) { 
     191                                        stmt.finalize(); 
     192                                        delete stmt; 
    190193                                        delete this._loader; 
    191194                                        this.endUpdate(); 
  • trunk/chrome/locale/en-US/manager.properties

    r1015 r1031  
    6161filtermetalink=Metalink (XML) 
    6262loading=Loading downloads (%S of %S, %S%). Please wait... 
     63adding=Adding downloads. Please wait... 
  • trunk/modules/cothread.jsm

    r1006 r1031  
    9393        _idx: 0, 
    9494        _ran: false, 
     95        _finishFunc: null, 
    9596         
    9697        run: function CoThread_run() { 
     
    124125                } 
    125126                catch (ex) { 
    126                         this.cancel();                  
     127                        this.cancel(); 
    127128                } 
    128129        }, 
     
    130131        cancel: function CoThread_cancel() { 
    131132                this._timer.cancel(); 
     133                if (this._finishFunc) { 
     134                        this._finishFunc.call(this._thisCtx); 
     135                }                
    132136        }, 
    133137         
     
    155159 *          // When to turn over Control? 
    156160 *          // Each 1000 items 
    157  *          1000 
     161 *          1000, 
     162 *          null, 
     163 *          function() alert('done') 
    158164 *        ).run(); 
    159165 *    
     
    162168 * @param {Number} yieldEvery Optional. After how many items control should be turned over to the main thread 
    163169 * @param {Object} thisCtx Optional. The function will be called in the scope of this object (or if omitted in the scope of the CoThread instance) 
     170 * @param {Function} finishFunc Optional. This function will be called once the operation finishes or is cancelled. 
    164171 */ 
    165 function CoThreadListWalker(func, arrayOrGenerator, yieldEvery, thisCtx) { 
     172function CoThreadListWalker(func, arrayOrGenerator, yieldEvery, thisCtx, finishFunc) { 
    166173        CoThreadBase.call(this, func, yieldEvery, thisCtx); 
    167174         
     
    176183                throw Cr.NS_ERROR_INVALID_ARG; 
    177184        } 
     185         
     186        this._finishFunc = finishFunc; 
     187        if (this._lastFunc && (typeof func != 'function' && !(func instanceof Function))) { 
     188                throw Cr.NS_ERROR_INVALID_ARG; 
     189        }  
    178190} 
    179191