| 39 | | // isn't accurate, but is good enough for this use case |
|---|
| 40 | | function _ssw_getHost(host) { |
|---|
| 41 | | var m = host.match(/([^.]+\.(?:[\w]{2,4}|museum)?(?:\.\w{2})?)$/); |
|---|
| 42 | | if (m) { |
|---|
| 43 | | return m[1]; |
|---|
| 44 | | } |
|---|
| 45 | | return host; |
|---|
| 46 | | } |
|---|
| 47 | | function _ssw_adjustButton() { |
|---|
| 48 | | if (Preferences.getDTA(SSW_PREF_ENABLED, true)) { |
|---|
| 49 | | $('toolssw').removeAttribute('off'); |
|---|
| 50 | | } |
|---|
| 51 | | else { |
|---|
| 52 | | $('toolssw').setAttribute('off', true); |
|---|
| 53 | | } |
|---|
| | 40 | var SSW = { |
|---|
| | 41 | load: function() { |
|---|
| | 42 | makeObserver(this); |
|---|
| | 43 | Preferences.addObserver('extensions.dta.', this); |
|---|
| | 44 | |
|---|
| | 45 | this.adjustButton(); |
|---|
| | 46 | this.loadLimits(); |
|---|
| | 47 | }, |
|---|
| | 48 | observe: function(subject, topic, prefName) { |
|---|
| | 49 | if (prefName == SSW_PREF_ENABLED) { |
|---|
| | 50 | this._adjustButton(); |
|---|
| | 51 | } |
|---|
| | 52 | else if (prefName.substr(0, SSW_PREF_LIMITS.length) == SSW_PREF_LIMITS) { |
|---|
| | 53 | this.loadLimits(); |
|---|
| | 54 | } |
|---|
| | 55 | }, |
|---|
| | 56 | adjustButton: function() { |
|---|
| | 57 | if (Preferences.getDTA(SSW_PREF_ENABLED, true)) { |
|---|
| | 58 | $('toolssw').removeAttribute('off'); |
|---|
| | 59 | } |
|---|
| | 60 | else { |
|---|
| | 61 | $('toolssw').setAttribute('off', true); |
|---|
| | 62 | } |
|---|
| | 63 | }, |
|---|
| | 64 | toggle: function() { |
|---|
| | 65 | Preferences.setDTA(SSW_PREF_ENABLED, !Preferences.getDTA(SSW_PREF_ENABLED, true)); |
|---|
| | 66 | this.adjustButton(); |
|---|
| | 67 | }, |
|---|
| | 68 | _limits: {}, |
|---|
| | 69 | loadLimits: function() { |
|---|
| | 70 | this._limits = {}; |
|---|
| | 71 | let c = {}; |
|---|
| | 72 | let limits = Preferences._pref.getChildList(SSW_PREF_LIMITS, c); |
|---|
| | 73 | |
|---|
| | 74 | for (let i = 0; i < c.value; ++i) { |
|---|
| | 75 | let limit = limits[i].substr(SSW_PREF_LIMITS.length); |
|---|
| | 76 | try { |
|---|
| | 77 | let value = Preferences.get(limits[i], 0); |
|---|
| | 78 | if (!value) { |
|---|
| | 79 | continue; |
|---|
| | 80 | } |
|---|
| | 81 | this._limits[limit] = value; |
|---|
| | 82 | Debug.dump("loaded limit: " + limit); |
|---|
| | 83 | } |
|---|
| | 84 | catch (ex) { |
|---|
| | 85 | Debug.dump("Failed to load: " + limit, ex); |
|---|
| | 86 | } |
|---|
| | 87 | } |
|---|
| | 88 | }, |
|---|
| | 89 | // isn't accurate, but is good enough for this use case |
|---|
| | 90 | getHost: function(host) { |
|---|
| | 91 | var m = host.match(/([^.]+\.(?:[\w]{2,4}|museum)?(?:\.\w{2})?)$/); |
|---|
| | 92 | if (m) { |
|---|
| | 93 | return m[1]; |
|---|
| | 94 | } |
|---|
| | 95 | return host; |
|---|
| | 96 | }, |
|---|
| | 97 | /*Dialog.*/startNext: function() { |
|---|
| | 98 | try { |
|---|
| | 99 | if (!Preferences.getDTA(SSW_PREF_ENABLED, true)) { |
|---|
| | 100 | return SSW.origStartNext(); |
|---|
| | 101 | } |
|---|
| | 102 | |
|---|
| | 103 | let rv = false; |
|---|
| | 104 | if (this._running.length >= Prefs.maxInProgress) { |
|---|
| | 105 | return rv; |
|---|
| | 106 | } |
|---|
| | 107 | |
|---|
| | 108 | let cset = {}; |
|---|
| | 109 | // Count the running task |
|---|
| | 110 | Dialog._running.forEach( |
|---|
| | 111 | function(i) { |
|---|
| | 112 | let host = i.d._ssw_host; |
|---|
| | 113 | if (!(host in cset)) { |
|---|
| | 114 | cset[host] = new SSWItem(host); |
|---|
| | 115 | } |
|---|
| | 116 | else { |
|---|
| | 117 | ++cset[host].n; |
|---|
| | 118 | } |
|---|
| | 119 | }, |
|---|
| | 120 | this |
|---|
| | 121 | ); |
|---|
| | 122 | // prepare the other tasks |
|---|
| | 123 | for (let d in Tree.all) { |
|---|
| | 124 | if (!d.is(QUEUED)) { |
|---|
| | 125 | continue; |
|---|
| | 126 | } |
|---|
| | 127 | let host = d._ssw_host; |
|---|
| | 128 | if (host in cset) { |
|---|
| | 129 | cset[host].downloads.push(d); |
|---|
| | 130 | } |
|---|
| | 131 | else { |
|---|
| | 132 | cset[host] = new SSWItem(host); |
|---|
| | 133 | } |
|---|
| | 134 | } |
|---|
| | 135 | |
|---|
| | 136 | for (d in SSW._generator(cset)) { |
|---|
| | 137 | if (this._running.length >= Prefs.maxInProgress) { |
|---|
| | 138 | break; |
|---|
| | 139 | } |
|---|
| | 140 | rv = true; |
|---|
| | 141 | this.run(d); |
|---|
| | 142 | } |
|---|
| | 143 | |
|---|
| | 144 | return rv; |
|---|
| | 145 | } catch(ex){ |
|---|
| | 146 | Debug.dump("ssw_startNext():", ex); |
|---|
| | 147 | } |
|---|
| | 148 | return false; |
|---|
| | 149 | }, |
|---|
| | 150 | _generator: function(downloadSet) { |
|---|
| | 151 | let sorted = []; |
|---|
| | 152 | for (let s in downloadSet) { |
|---|
| | 153 | sorted.push(downloadSet[s]); |
|---|
| | 154 | } |
|---|
| | 155 | while (sorted.length) { |
|---|
| | 156 | sorted.sort(SSWItem.prototype.cmp); |
|---|
| | 157 | for (i in sorted) { |
|---|
| | 158 | let s = sorted[i]; |
|---|
| | 159 | let limit = 0; |
|---|
| | 160 | if (s.host in SSW._limits) { |
|---|
| | 161 | limit = SSW._limits[s.host]; |
|---|
| | 162 | } |
|---|
| | 163 | if ((limit && s.n >= limit) || s.downloads.length == 0) { |
|---|
| | 164 | sorted.splice(i, 1); |
|---|
| | 165 | break; |
|---|
| | 166 | } |
|---|
| | 167 | yield s.downloads.shift(); |
|---|
| | 168 | ++s.n; |
|---|
| | 169 | } |
|---|
| | 170 | } |
|---|
| | 171 | }, |
|---|
| | 172 | origStartNext: Dialog.startNext |
|---|
| 80 | | Dialog._ssw_startNext = Dialog.startNext; |
|---|
| 81 | | Dialog.startNext = function Dssw_startNext() { |
|---|
| 82 | | try { |
|---|
| 83 | | if (!Preferences.getDTA(SSW_PREF_ENABLED, true)) { |
|---|
| 84 | | return this._ssw_startNext(); |
|---|
| 85 | | } |
|---|
| 86 | | |
|---|
| 87 | | let rv = false; |
|---|
| 88 | | if (this._running.length >= Prefs.maxInProgress) { |
|---|
| 89 | | return rv; |
|---|
| 90 | | } |
|---|
| 91 | | |
|---|
| 92 | | // building the map |
|---|
| 93 | | let cset = {}; |
|---|
| 94 | | Dialog._running.forEach( |
|---|
| 95 | | function(i) { |
|---|
| 96 | | let host = i.d._ssw_host; |
|---|
| 97 | | if (!(host in cset)) { |
|---|
| 98 | | cset[host] = new SSWItem(); |
|---|
| 99 | | } |
|---|
| 100 | | else { |
|---|
| 101 | | ++cset[host].n; |
|---|
| 102 | | } |
|---|
| 103 | | }, |
|---|
| 104 | | this |
|---|
| 105 | | ); |
|---|
| 106 | | for (let d in Tree.all) { |
|---|
| 107 | | if (this._running.length >= Prefs.maxInProgress) { |
|---|
| 108 | | return rv; |
|---|
| 109 | | } |
|---|
| 110 | | if (!d.is(QUEUED)) { |
|---|
| 111 | | continue; |
|---|
| 112 | | } |
|---|
| 113 | | let host = d._ssw_host; |
|---|
| 114 | | if (host in cset) { |
|---|
| 115 | | cset[host].i.push(d); |
|---|
| 116 | | continue; |
|---|
| 117 | | } |
|---|
| 118 | | else { |
|---|
| 119 | | cset[host] = new SSWItem(); |
|---|
| 120 | | } |
|---|
| 121 | | this.run(d); |
|---|
| 122 | | |
|---|
| 123 | | rv = true; |
|---|
| 124 | | } |
|---|
| 125 | | var a = []; |
|---|
| 126 | | for (let i in cset) { |
|---|
| 127 | | if (!cset[i].i.length) { |
|---|
| 128 | | continue; |
|---|
| 129 | | } |
|---|
| 130 | | a.push(cset[i]); |
|---|
| 131 | | } |
|---|
| 132 | | while (a.length && this._running.length < Prefs.maxInProgress) { |
|---|
| 133 | | a.sort(SSWItem.prototype.cmp); |
|---|
| 134 | | this.run(a[0].i.shift()); |
|---|
| 135 | | rv = true; |
|---|
| 136 | | if (!a[0].length) { |
|---|
| 137 | | a.shift(); |
|---|
| 138 | | continue; |
|---|
| 139 | | } |
|---|
| 140 | | ++a[0].n; |
|---|
| 141 | | } |
|---|
| 142 | | return rv; |
|---|
| 143 | | } catch(ex){ |
|---|
| 144 | | Debug.dump("ssw_startNext():", ex); |
|---|
| 145 | | } |
|---|
| 146 | | return false; |
|---|
| 147 | | } |
|---|
| | 196 | Dialog.startNext = SSW.startNext; |
|---|