sbRemoveHelper.js
Go to the documentation of this file.
1 /*
2  *=BEGIN SONGBIRD GPL
3  *
4  * This file is part of the Songbird web player.
5  *
6  * Copyright(c) 2005-2009 POTI, Inc.
7  * http://www.songbirdnest.com
8  *
9  * This file may be licensed under the terms of of the
10  * GNU General Public License Version 2 (the ``GPL'').
11  *
12  * Software distributed under the License is distributed
13  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
14  * express or implied. See the GPL for the specific language
15  * governing rights and limitations.
16  *
17  * You should have received a copy of the GPL along with this
18  * program. If not, go to http://www.gnu.org/licenses/gpl.html
19  * or write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  *=END SONGBIRD GPL
23  */
24 
25 const Cc = Components.classes;
26 const Ci = Components.interfaces;
27 const Cu = Components.utils;
28 
29 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
30 Cu.import("resource://app/jsmodules/SBJobUtils.jsm");
31 Cu.import("resource://app/jsmodules/ArrayConverter.jsm");
32 Cu.import("resource://app/jsmodules/sbLibraryUtils.jsm");
33 Cu.import("resource://app/jsmodules/StringUtils.jsm");
34 Cu.import("resource://app/jsmodules/GeneratorThread.jsm");
35 
36 /******************************************************************************
37  * http://bugzilla.songbirdnest.com/show_bug.cgi?id=16517
38  * Object implementing sbIJobProgress/sbIWFRemoveHelper9001. Pop up a remove
39  * progress dialog.
40  *
41  * Call begin() to start the job.
42  *****************************************************************************/
43 function RemoveJob(aItems, aHelper) {
44  if (!aItems) {
45  throw Components.results.NS_ERROR_INVALID_ARG;
46  }
47 
48  // Call super constructor
49  SBJobUtils.JobBase.call(this);
50 
51  this._owner = aHelper;
52  this._mediaItems = ArrayConverter.JSArray(aItems);
53 
54  this._titleText = SBString("watchfolders.remove.title");
55  this._statusText = SBString("watchfolders.remove.processing");
56  this._progress = 0;
57 
58  this._canCancel = true;
59 }
60 
61 RemoveJob.prototype = {
62  __proto__: SBJobUtils.JobBase.prototype,
63 
64  QueryInterface: XPCOMUtils.generateQI(
65  [Ci.sbIJobProgress, Ci.sbIJobProgressListener,
66  Ci.sbIJobCancelable, Ci.nsIClassInfo]),
67 
69  getInterfaces: function(count) {
70  var interfaces = [Ci.sbIJobProgress, Ci.sbIJobProgressListener,
71  Ci.sbIJobCancelable, Ci.nsIClassInfo,
72  Ci.nsISupports];
73  count.value = interfaces.length;
74  return interfaces;
75  },
76 
77  // RemoveHelper
78  _owner: null,
79 
80  // Media items that will be removed
81  _mediaItems: null,
82 
83  // Index into _mediaItems for the beginning of the next removed batch.
84  _nextItemIndex: 0,
85 
86  // The size of the current batch
87  _currentBatchSize: 0,
88 
89  // Split the item removal so that the remvoe progress dialog can
90  // be updated. This avoids the block introduced by removeSome.
91  BATCHREMOVE_SIZE: 300,
92 
96  begin: function RemoveJob_begin() {
97  try {
98  var job = this;
99  this._thread = new GeneratorThread(this._process());
100  this._thread.start();
101  } catch (e) {
102  dump(e);
103  }
104  },
105 
109  _process: function RemoveJob_process() {
110  this._total = this._mediaItems.length;
111 
112  try {
113  var library = this._mediaItems[0].library;
114 
115  // Process the URIs a slice at a time and update the progress bar
116  while (this._nextItemIndex < this._mediaItems.length) {
117  yield GeneratorThread.yieldIfShould();
118  this._currentBatchSize = Math.min(this.BATCHREMOVE_SIZE,
119  this._mediaItems.length - this._nextItemIndex);
120  var endIndex = this._nextItemIndex + this._currentBatchSize;
121  var items = this._mediaItems.slice(this._nextItemIndex, endIndex);
122  this._nextItemIndex = endIndex;
123  library.removeSome(ArrayConverter.enumerator(items));
124  this._progress += this._currentBatchSize;
125  this.notifyJobProgressListeners();
126  }
127  } catch (e) {
128  dump(e);
129  }
130 
131  this.complete();
132  },
133 
138  get progress() {
139  return this._progress;
140  },
141 
142  get total() {
143  return this._total;
144  },
145 
150  get titleText() {
151  return this._titleText;
152  },
153 
155  cancel: function RemoveJob_cancel() {
156  if (!this.canCancel) {
157  throw new Error("RemovedJob not currently cancelable");
158  }
159 
160  this.complete();
161  },
162 
166  complete: function RemoveJob_complete() {
167 
168  this._status = Ci.sbIJobProgress.STATUS_SUCCEEDED;
169  this._statusText = SBString("watchfolders.remove.complete");
170  this.notifyJobProgressListeners();
171 
172  this._owner.onJobComplete();
173  }
174 }
175 
176 /******************************************************************************
177  * Object implementing sbIWFRemoveHelper9001. Used to start a
178  * new remove job.
179  *****************************************************************************/
180 function RemoveHelper() {
181  this._jobs = [];
182 }
183 
184 RemoveHelper.prototype = {
185  classDescription: "Songbird Watch Folder Remove Helper Service",
186  classID: Components.ID("{b44bf03e-fe28-4ad4-ba7e-6112cdced7a4}"),
187  contractID: "@songbirdnest.com/Songbird/RemoveHelper;1",
188  QueryInterface: XPCOMUtils.generateQI([Ci.sbIWFRemoveHelper9001]),
189 
190  // List of pending jobs. We only want to allow one to run at a time,
191  // since this can lock up the UI.
192  _jobs: null,
193 
197  remove: function RemoveHelper_remove(aItems) {
198  var job = new RemoveJob(aItems, this);
199 
200  this._jobs.push(job);
201 
202  // If this is the only job, just start immediately.
203  // Otherwise it will be started when the current job finishes.
204  if (this._jobs.length == 1) {
205 
206  // Make sure things QI correctly...
207  var sip = Cc["@mozilla.org/supports-interface-pointer;1"]
208  .createInstance(Ci.nsISupportsInterfacePointer);
209  sip.data = job;
210 
211  // Wrap the job in a modal dialog and library batch
212  LibraryUtils.mainLibrary.runInBatchMode(function() {
213  job.begin();
214  SBJobUtils.showProgressDialog(sip.data, null, 0);
215  });
216  }
217  },
218 
222  onJobComplete: function RemoveHelper_onJobComplete() {
223  this._jobs.shift();
224  if (this._jobs.length > 0) {
225  this._jobs[0].begin();
226  }
227  }
228 
229 } // RemoveHelper.prototype
230 
231 
232 function NSGetModule(compMgr, fileSpec) {
233  return XPCOMUtils.generateModule([RemoveHelper]);
234 }
var total
function GeneratorThread(aEntryPoint)
sbDeviceFirmwareAutoCheckForUpdate prototype contractID
const Ci
sbOSDControlService prototype QueryInterface
sbDeviceFirmwareAutoCheckForUpdate prototype classDescription
const Cc
function SBString(aKey, aDefault, aStringBundle)
Definition: StringUtils.jsm:93
var count
Definition: test_bug7406.js:32
function RemoveJob(aItems, aHelper)
return null
Definition: FeedWriter.js:1143
sbDeviceFirmwareAutoCheckForUpdate prototype classID
Javascript wrappers for common library tasks.
sbDeviceFirmwareAutoCheckForUpdate prototype getInterfaces
sbDeviceFirmwareAutoCheckForUpdate prototype interfaces
const Cu