26 const Cc = Components.classes;
27 const Ci = Components.interfaces;
28 const Cr = Components.results;
29 const Ce = Components.Exception;
30 const Cu = Components.utils;
32 Cu.import(
"resource://gre/modules/XPCOMUtils.jsm");
33 Cu.import(
"resource://app/jsmodules/SBJobUtils.jsm");
34 Cu.import(
"resource://app/jsmodules/ArrayConverter.jsm");
35 Cu.import(
"resource://app/jsmodules/sbProperties.jsm");
36 Cu.import(
"resource://app/jsmodules/sbLibraryUtils.jsm");
37 Cu.import(
"resource://app/jsmodules/StringUtils.jsm");
38 Cu.import(
"resource://app/jsmodules/GeneratorThread.jsm");
40 function LOG(aMessage) {
41 dump(
"MoveRenameJob: " + aMessage +
"\n");
58 if (!aItems || !aPaths) {
59 throw Components.results.NS_ERROR_INVALID_ARG;
63 SBJobUtils.JobBase.call(
this);
65 this._owner = aService;
66 this._listener = aListener;
67 this._mediaItems = ArrayConverter.JSArray(aItems);
72 this._titleText =
SBString(
"watchfolders.moverename.title");
73 this._statusText =
SBString(
"watchfolders.moverename.processing");
75 this._canCancel =
true;
78 __proto__: SBJobUtils.JobBase.prototype,
81 [
Ci.sbIJobProgress,
Ci.sbIJobProgressListener,
82 Ci.sbIJobCancelable,
Ci.nsIClassInfo]),
87 Ci.sbIJobCancelable,
Ci.nsIClassInfo,
89 count.value = interfaces.length;
109 begin:
function MoveRenameJob_begin() {
113 this.addJobProgressListener(this._listener);
116 this._thread.period = 33;
117 this._thread.maxPctCPU = 60;
118 this._thread.start();
124 _process:
function MoveRenameJob_process() {
126 yield this._buildMap();
127 yield this._processMovedItems();
128 yield this._processRenamedItems();
129 yield this._processRemaining();
136 if (!this._jobProgressDelegate) {
144 _buildMap:
function MoveRenameJob__buildMap() {
145 for (var
path in ArrayConverter.JSEnum(
this._paths)) {
146 var
file =
path.QueryInterface(
Ci.nsIFileURL).file;
147 var size =
"" + file.fileSize;
148 var
name = file.leafName;
149 if (!(size in this._map)) this._map[size] = {};
150 if (!(name in this._map[size])) this._map[size][
name] = [];
152 yield this._yieldIfShouldWithStatusUpdate();
161 _processMovedItems:
function MoveRenameJob__processMovedItems() {
162 this._total = this._mediaItems.length;
164 var remainingItems = [];
165 for each (var item
in this._mediaItems) {
166 var file = item.contentSrc.QueryInterface(
Ci.nsIFileURL).file;
167 var name = file.leafName;
169 var size = item.getProperty(SBProperties.contentLength);
172 if (this._map[size] && this._map[size][name] &&
173 this._map[size][name].length == 1) {
175 item.contentSrc = this._map[size][
name][0];
176 delete this._map[size][
name];
179 remainingItems.push(item);
182 yield this._yieldIfShouldWithStatusUpdate();
185 this._mediaItems = remainingItems;
192 _processRenamedItems:
function MoveRenameJob__processRenamedItems() {
193 this._total += this._mediaItems.length;
197 var remainingItems = [];
198 for each (var item
in this._mediaItems) {
200 var size = item.getProperty(SBProperties.contentLength);
201 if (this._map[size]) {
204 for (name in this._map[size]) count++;
205 if (count == 1 && this._map[size][name].length == 1) {
209 item.contentSrc = this._map[size][
name].shift();
210 }
else if (count == 0) {
215 dump(
"Watch Folders: unable to resolve ambiguous file renames\n");
217 delete this._map[size];
220 remainingItems.push(item);
223 yield this._yieldIfShouldWithStatusUpdate();
226 this._mediaItems = remainingItems;
233 _processRemaining:
function MoveRenameJob__processRemaining() {
236 if (this._mediaItems.length > 0) {
237 var library = this._mediaItems[0].library;
239 library.removeSome(ArrayConverter.enumerator(
this._mediaItems));
244 for (var size in this._map) {
245 for (var name in this._map[size]) {
246 newPaths = newPaths.concat(this._map[size][name]);
251 if (newPaths.length > 0) {
254 var
importer =
Cc[
'@songbirdnest.com/Songbird/DirectoryImportService;1']
255 .getService(
Ci.sbIDirectoryImportService);
256 var importJob = importer.import(ArrayConverter.nsIArray(newPaths));
257 this.delegateJobProgress(importJob);
265 _yieldIfShouldWithStatusUpdate:
266 function MoveRenameJob__yieldIfShouldWithStatusUpdate() {
268 yield this._yieldWithStatusUpdate();
274 _yieldWithStatusUpdate:
275 function MoveRenameJob_yieldIfShouldWithStatusUpdate() {
276 this.notifyJobProgressListeners();
283 onJobDelegateCompleted:
function MoveRenameJob_onJobDelegateCompleted() {
288 this._progress += this._jobProgressDelegate.progress;
291 this.delegateJobProgress(
null);
301 return (this._jobProgressDelegate) ?
302 this._jobProgressDelegate.progress + this._progress : this._progress;
314 return this._titleText;
318 cancel:
function MoveRenameJob_cancel() {
319 if (!this.canCancel) {
320 throw new Error(
"MoveRenameJob not currently cancelable");
324 this._thread.terminate();
328 if (this._jobProgressDelegate) {
330 this._jobProgressDelegate.cancel();
339 complete:
function MoveRenameJob_complete() {
341 this._status =
Ci.sbIJobProgress.STATUS_SUCCEEDED;
342 this._statusText =
SBString(
"watchfolders.moverename.complete");
343 this.notifyJobProgressListeners();
346 this.removeJobProgressListener(this._listener);
347 this._listener =
null;
349 this._owner.onJobComplete();
362 function MoveRenameHelper() {
366 MoveRenameHelper.prototype = {
368 classID: Components.ID(
"{02ba1ba0-fee5-11dd-87af-0800200c9a66}"),
369 contractID:
"@songbirdnest.com/Songbird/MoveRenameHelper;1",
379 process:
function MoveRenameHelper_process(aItems, aPaths, aListener) {
380 dump(
"WatchFolders MoveRenameHelper_process\n");
381 var job =
new MoveRenameJob(aItems, aPaths,
this, aListener);
383 this._jobs.push(job);
387 if (this._jobs.length == 1) {
390 var sip =
Cc[
"@mozilla.org/supports-interface-pointer;1"]
391 .createInstance(
Ci.nsISupportsInterfacePointer);
397 SBJobUtils.showProgressDialog(sip.data,
null, 100);
405 onJobComplete:
function MoveRenameHelper_onJobComplete() {
407 if (this._jobs.length > 0) {
408 this._jobs[0].begin();
416 return XPCOMUtils.generateModule([MoveRenameHelper]);
function GeneratorThread(aEntryPoint)
function MoveRenameJob(aItems, aPaths, aService, aListener)
sbDeviceFirmwareAutoCheckForUpdate prototype contractID
sbOSDControlService prototype QueryInterface
sbDeviceFirmwareAutoCheckForUpdate prototype classDescription
function SBString(aKey, aDefault, aStringBundle)
sbDeviceFirmwareAutoCheckForUpdate prototype classID
Javascript wrappers for common library tasks.
sbDeviceFirmwareAutoCheckForUpdate prototype getInterfaces
sbDeviceFirmwareAutoCheckForUpdate prototype interfaces