sbAddToLibrary.jsm
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2009 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25  */
26 
27 Components.utils.import("resource://app/jsmodules/sbProperties.jsm");
28 Components.utils.import("resource://app/jsmodules/DropHelper.jsm");
29 Components.utils.import("resource://app/jsmodules/sbLibraryUtils.jsm");
30 Components.utils.import("resource://app/jsmodules/SBUtils.jsm");
31 
32 const Ci = Components.interfaces;
33 const Cc = Components.classes;
34 
36  "@songbirdnest.com/Songbird/Library/medialistduplicatefilter;1";
37 
38 const ADDTOLIBRARY_MENU_TYPE = "menuitem";
39 const ADDTOLIBRARY_MENU_ID = "library_cmd_addtolibrary";
40 const ADDTOLIBRARY_MENU_NAME = "&command.addtolibrary";
41 const ADDTOLIBRARY_MENU_TOOLTIP = "&command.tooltip.addtolibrary";
42 const ADDTOLIBRARY_MENU_KEY = "&command.shortcut.key.addtolibrary";
43 const ADDTOLIBRARY_MENU_KEYCODE = "&command.shortcut.keycode.addtolibrary";
44 const ADDTOLIBRARY_MENU_MODIFIERS = "&command.shortcut.modifiers.addtolibrary";
45 
46 
47 const ADDTOLIBRARY_COMMAND_ID = "library_cmd_addtolibrary";
48 
49 EXPORTED_SYMBOLS = [ "addToLibraryHelper",
50  "SBPlaylistCommand_AddToLibrary" ];
51 
52 // ----------------------------------------------------------------------------
53 // The "Add to library" dynamic command object
54 // ----------------------------------------------------------------------------
56 {
57  context: {
58  playlist: null,
59  window: null
60  },
61 
62  addToLibrary: null,
63 
64  root_commands :
65  {
67  ids: [ADDTOLIBRARY_MENU_ID],
68  names: [ADDTOLIBRARY_MENU_NAME],
69  tooltips: [ADDTOLIBRARY_MENU_TOOLTIP],
70  keys: [ADDTOLIBRARY_MENU_KEY],
71  keycodes: [ADDTOLIBRARY_MENU_KEYCODE],
72  enableds: [true],
73  modifiers: [ADDTOLIBRARY_MENU_MODIFIERS],
74  playlistCommands: [null]
75  },
76 
77  _getMenu: function(aSubMenu)
78  {
79  return this.root_commands;
80  },
81 
82  getVisible: function( aHost )
83  {
84  return true;
85  },
86 
87  getNumCommands: function( aSubMenu, aHost )
88  {
89  return this._getMenu(aSubMenu).ids.length;
90  },
91 
92  getCommandId: function( aSubMenu, aIndex, aHost )
93  {
94  return this._getMenu(aSubMenu).ids[ aIndex ] || "";
95  },
96 
97  getCommandType: function( aSubMenu, aIndex, aHost )
98  {
99  return this._getMenu(aSubMenu).types[ aIndex ] || "";
100  },
101 
102  getCommandText: function( aSubMenu, aIndex, aHost )
103  {
104  return this._getMenu(aSubMenu).names[ aIndex ] || "";
105  },
106 
107  getCommandFlex: function( aSubMenu, aIndex, aHost )
108  {
109  return ( this._getMenu(aSubMenu).types[ aIndex ] == "separator" ) ? 1 : 0;
110  },
111 
112  getCommandToolTipText: function( aSubMenu, aIndex, aHost )
113  {
114  return this._getMenu(aSubMenu).tooltips[ aIndex ] || "";
115  },
116 
117  getCommandValue: function( aSubMenu, aIndex, aHost )
118  {
119  },
120 
121  instantiateCustomCommand: function( aDocument, aId, aHost )
122  {
123  return null;
124  },
125 
126  refreshCustomCommand: function( aElement, aId, aHost )
127  {
128  },
129 
130  getCommandVisible: function( aSubMenu, aIndex, aHost )
131  {
132  return true;
133  },
134 
135  getCommandFlag: function( aSubmenu, aIndex, aHost )
136  {
137  return false;
138  },
139 
140  getCommandChoiceItem: function( aChoiceMenu, aHost )
141  {
142  return "";
143  },
144 
145  getCommandEnabled: function( aSubMenu, aIndex, aHost )
146  {
147  if (this.context.playlist.tree.currentIndex == -1) return false;
148  return this._getMenu(aSubMenu).enableds[ aIndex ];
149  },
150 
151  getCommandShortcutModifiers: function ( aSubMenu, aIndex, aHost )
152  {
153  return this._getMenu(aSubMenu).modifiers[ aIndex ] || "";
154  },
155 
156  getCommandShortcutKey: function ( aSubMenu, aIndex, aHost )
157  {
158  return this._getMenu(aSubMenu).keys[ aIndex ] || "";
159  },
160 
161  getCommandShortcutKeycode: function ( aSubMenu, aIndex, aHost )
162  {
163  return this._getMenu(aSubMenu).keycodes[ aIndex ] || "";
164  },
165 
166  getCommandShortcutLocal: function ( aSubMenu, aIndex, aHost )
167  {
168  return true;
169  },
170 
171  getCommandSubObject: function ( aSubMenu, aIndex, aHost )
172  {
173  return this._getMenu(aSubMenu).playlistCommands[ aIndex ] || null;
174  },
175 
176  onCommand: function( aSubMenu, aIndex, aHost, id, value )
177  {
178  this.addToLibrary.handleCommand(id, this.context);
179  },
180 
181  // The object registered with the sbIPlaylistCommandsManager interface acts
182  // as a template for instances bound to specific playlist elements
183 
184  dupObject: function (obj) {
185  var r = {};
186  for ( var i in obj )
187  {
188  r[ i ] = obj[ i ];
189  }
190  return r;
191  },
192 
193  duplicate: function()
194  {
195  var obj = this.dupObject(this);
196  obj.context = this.dupObject(this.context);
197  return obj;
198  },
199 
200  initCommands: function(aHost) {
201  if (!this.addToLibrary) {
202  this.addToLibrary = new addToLibraryHelper();
203  this.addToLibrary.init(this);
204  }
205  },
206 
207  shutdownCommands: function() {
208  if (!this.addToLibrary) {
209  dump("this.addToLibrary is null in SBPlaylistCommand_AddToLibrary ?!!\n");
210  return;
211  }
212 
213  this.addToLibrary.shutdown();
214  this.context = null;
215  },
216 
217  setContext: function( context )
218  {
219  var playlist = context.playlist;
220  var window = context.window;
221 
222  // Ah. Sometimes, things are being secure.
223 
224  if ( playlist && playlist.wrappedJSObject )
225  playlist = playlist.wrappedJSObject;
226 
227  if ( window && window.wrappedJSObject )
228  window = window.wrappedJSObject;
229 
230  this.context.playlist = playlist;
231  this.context.window = window;
232  },
233 
234  QueryInterface : function(aIID)
235  {
236  if (!aIID.equals(Components.interfaces.sbIPlaylistCommands) &&
237  !aIID.equals(Components.interfaces.nsISupportsWeakReference) &&
238  !aIID.equals(Components.interfaces.nsISupports))
239  {
240  throw Components.results.NS_ERROR_NO_INTERFACE;
241  }
242 
243  return this;
244  }
245 }; // SBPlaylistCommand_AddToLibrary declaration
246 
247 function addToLibraryHelper() {}
248 addToLibraryHelper.prototype = {
249  deviceManager: null,
251 
252  init: function addToLibraryHelper_init(aCommands) {
253  this.libraryManager =
254  Cc["@songbirdnest.com/Songbird/library/Manager;1"]
255  .getService(Ci.sbILibraryManager);
256  this.deviceManager =
257  Cc["@songbirdnest.com/Songbird/DeviceManager;2"]
258  .getService(Ci.sbIDeviceManager2);
259  },
260 
261  shutdown: function addToLibraryHelper_shutdown() {
262  this.deviceManager = null;
263  this.libraryManager = null;
264  },
265 
266  _getDeviceLibraryForLibrary: function(aDevice, aLibrary) {
267  var libs = aDevice.content.libraries;
268  for (var i = 0; i < libs.length; i++) {
269  var devLib = libs.queryElementAt(i, Ci.sbIDeviceLibrary);
270  if (devLib.equals(aLibrary))
271  return devLib;
272  }
273 
274  return null;
275  },
276 
277  _itemsFromEnumerator: function(aItemEnum) {
278  var items = Cc["@mozilla.org/array;1"].createInstance(Ci.nsIMutableArray);
279 
280  while (aItemEnum.hasMoreElements())
281  items.appendElement(aItemEnum.getNext(), false);
282 
283  return items;
284  },
285 
286  // handle click on a command item (device library or play queue, etc.)
287  handleCommand: function addToLibraryHelper_handleCommand(id, context) {
288  try {
289  if ( id == ADDTOLIBRARY_COMMAND_ID) {
290  var destLibrary = this.libraryManager.mainLibrary;
291  if (destLibrary) {
292  var selection = context.playlist.mediaListView
293  .selection.selectedMediaItems;
294  var sourceLibrary = context.playlist.mediaListView.mediaList.library;
295 
296  var device = this.deviceManager.getDeviceForItem(sourceLibrary);
297  if (device) {
298  var items = this._itemsFromEnumerator(selection);
299 
300  var deviceLibrary = this._getDeviceLibraryForLibrary(
301  device, sourceLibrary);
302 
303  var differ =
304  Cc["@songbirdnest.com/Songbird/Device/DeviceLibrarySyncDiff;1"]
305  .createInstance(Ci.sbIDeviceLibrarySyncDiff);
306  var changeset = {};
307  var destItems = {};
308 
309  differ.generateDropLists(sourceLibrary,
310  destLibrary,
311  null,
312  items,
313  destItems,
314  changeset);
315 
316  device.importFromDevice(destLibrary, changeset.value);
317 
318  DNDUtils.reportAddedTracks(changeset.value.changes.length,
319  0,
320  0,
321  destLibrary.name,
322  true);
323  }
324  else {
325  destLibrary.addSome(selected);
326  }
327  }
328  return true;
329  }
330  } catch (e) {
331  Components.utils.reportError(e);
332  }
333  return false;
334  }
335 };
const SB_MEDIALISTDUPLICATEFILTER_CONTRACTID
menuItem id
Definition: FeedWriter.js:971
const ADDTOLIBRARY_MENU_ID
const ADDTOLIBRARY_MENU_TYPE
_updateCookies aHost
var DNDUtils
Definition: DropHelper.jsm:80
sbOSDControlService prototype QueryInterface
const char * types
sbDownloadDeviceServicePaneModule prototype shutdown
let window
const ADDTOLIBRARY_MENU_KEYCODE
_window init
Definition: FeedWriter.js:1144
_collectFormDataForFrame aDocument
function addToLibraryHelper()
const ADDTOLIBRARY_COMMAND_ID
const ADDTOLIBRARY_MENU_TOOLTIP
const ADDTOLIBRARY_MENU_KEY
var libraryManager
const Ci
return null
Definition: FeedWriter.js:1143
countRef value
Definition: FeedWriter.js:1423
EXPORTED_SYMBOLS
var SBPlaylistCommand_AddToLibrary
const ADDTOLIBRARY_MENU_MODIFIERS
const Cc
_getSelectedPageStyle s i
const ADDTOLIBRARY_MENU_NAME