/
directory.js
317 lines (267 loc) · 11.6 KB
/
directory.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
Copyright (c) 2011, Pioneers of the Inevitable, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of Pioneers of the Inevitable, Songbird, nor the names
of its contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
var Cc = Components.classes;
var Ci = Components.interfaces;
var Cu = Components.utils;
Cu.import("resource://app/jsmodules/DOMUtils.jsm");
Cu.import("resource://app/components/kPlaylistCommands.jsm");
Cu.import("resource://app/jsmodules/sbLibraryUtils.jsm");
Cu.import("resource://app/jsmodules/sbProperties.jsm");
Cu.import("resource://app/jsmodules/StringUtils.jsm");
Cu.import("resource://app/jsmodules/URLUtils.jsm");
const SOUNDCLOUD_FIRST_RUN = "extensions.soundcloud.firstrun";
if (typeof(songbirdMainWindow) == "undefined")
var songbirdMainWindow = Cc["@mozilla.org/appshell/window-mediator;1"]
.getService(Ci.nsIWindowMediator)
.getMostRecentWindow("Songbird:Main").window;
if (typeof(gBrowser) == "undefined")
var gBrowser = songbirdMainWindow.gBrowser;
if (typeof(gServicePane) == "undefined")
var gServicePane = songbirdMainWindow.gServicePane;
if (typeof CloudDirectory == "undefined") {
var CloudDirectory = {};
}
CloudDirectory.onLoad = function CloudDirectory_onLoad() {
var self = this;
this._strings = Cc["@mozilla.org/intl/stringbundle;1"]
.getService(Ci.nsIStringBundleService)
.createBundle("chrome://soundcloud/locale/overlay.properties");
this._service = Cc["@songbirdnest.com/soundcloud/service;1"]
.getService(Ci.sbISoundCloudService);
this._domEventListenerSet = new DOMEventListenerSet();
this._threadManager = Cc["@mozilla.org/thread-manager;1"]
.getService(Ci.nsIThreadManager);
// Wire up UI events
this._logo = document.getElementById("soundcloud-logo");
var onLogoClicked = function() { gBrowser.loadOneTab("http://soundcloud.com"); };
this._domEventListenerSet.add(this._logo,
"click",
onLogoClicked,
false,
false);
this._searchBox = document.getElementById("soundcloud-search-textbox");
this._searchBtn = document.getElementById("soundcloud-search-btn");
this._searchBox.value = decodeURIComponent(this._service.lastSearch);
if (this._searchBox.value.length > 0)
this._searchBtn.disabled = false;
var onSearchInput = function(aEvent) {
self._searchBtn.disabled = aEvent.target.value.length == 0;
};
var onSearchKeydown = function(aEvent) {
if (aEvent.keyCode == KeyEvent.DOM_VK_RETURN ||
aEvent.keyCode == KeyEvent.DOM_VK_ENTER)
self._searchBtn.click();
};
var onSearchCommand = function() { self.triggerSearch(); };
this._domEventListenerSet.add(this._searchBox,
"input",
onSearchInput,
false,
false);
this._domEventListenerSet.add(this._searchBox,
"keydown",
onSearchKeydown,
false,
false);
this._domEventListenerSet.add(this._searchBtn,
"command",
onSearchCommand,
false,
false);
this._directory = document.getElementById("soundcloud-directory");
var search = document.getElementById("soundcloud-box");
this._topLayer = document.getElementById("dir-top-layer");
this._idleDeck = document.getElementById("idle-deck");
var loading = document.getElementById("soundcloud-loading");
var noneFound = document.getElementById("soundcloud-none-found");
// Setup library
var uri = gBrowser.currentURI.spec;
var params = {};
URLUtils.extractQuery(uri, params);
if (params.type) {
this._page = params.type;
switch(this._page) {
case "dashboard":
this._library = this._service.dashboard;
search.hidden = true
break;
case "favorites":
this._library = this._service.favorites;
search.hidden = true;
break;
default:
this._library = this._service.library;
}
} else {
this._page = "main";
this._library = this._service.library;
}
var node = gServicePane.activeNode;
document.title = node.displayName;
// Get playlist commands
var mgr = Cc["@songbirdnest.com/Songbird/PlaylistCommandsManager;1"]
.createInstance(Ci.sbIPlaylistCommandsManager);
var cmds = mgr.request("soundcloud-cmds@sb.com");
// Bind the playlist widget to our library
this._directory.bind(this._library.createView(), cmds);
var colSpec = SBProperties.trackName + " 300 " +
SBProperties.duration + " 70 " +
SB_PROPERTY_USER + " 150 " +
SB_PROPERTY_PLAYS + " 60 " +
SB_PROPERTY_FAVS + " 60 " +
SB_PROPERTY_DOWNLOAD_IMAGE + " 60 " +
SB_PROPERTY_CREATION_DATE + " 135 ";
this._library.setProperty(SBProperties.columnSpec, colSpec);
this._directory.clearColumns();
this._directory.appendColumn(SBProperties.trackName, "300");
this._directory.appendColumn(SBProperties.duration, "70");
this._directory.appendColumn(SB_PROPERTY_USER, "150");
this._directory.appendColumn(SB_PROPERTY_PLAYS, "60");
this._directory.appendColumn(SB_PROPERTY_FAVS, "60");
this._directory.appendColumn(SB_PROPERTY_DOWNLOAD_IMAGE, "60");
this._directory.appendColumn(SB_PROPERTY_CREATION_DATE, "135");
var itemCount = this._library.getItemCountByProperty(SBProperties.hidden,
"0");
this.listener = {
onLoginBegins: function listener_onLoginBegins() { },
onLogout: function listener_onLogout() { },
onAutoLoginChanged: function listener_onAutoLoginChanged() { },
onLoggedInStateChanged: function listener_onLoggedInStateChanged() { },
onProfileUpdated: function listener_onProfileUpdated() { },
onSearchTriggered: function listener_onSearchTriggered() {
self._searchBox.value = decodeURIComponent(self._service.lastSearch);
self._directory.setAttribute("disabled", true);
self._topLayer.hidden = false;
self._idleDeck.selectedIndex = 1;
if (firstrun) {
Application.prefs.setValue(SOUNDCLOUD_FIRST_RUN, false);
}
},
onSearchCompleted: function listener_onSearchCompleted() {
var count = self._library
.getItemCountByProperty(SBProperties.hidden,
"0");
if (count == 0) {
self._idleDeck.selectedIndex = 2;
}
},
onTracksAdded: function listener_onTracksAdded(aLibrary) {
if (!self._library || self._library != aLibrary)
return;
var count = self._library
.getItemCountByProperty(SBProperties.hidden,
"0");
if (self._directory.getAttribute("disabled") && count > 0) {
self._directory.removeAttribute("disabled");
self._topLayer.hidden = true;
}
var SB_NewDataRemote =
Components.Constructor("@songbirdnest.com/Songbird/DataRemote;1",
"sbIDataRemote",
"init");
var statusOverrideText =
SB_NewDataRemote("faceplate.status.override.text");
var statusOverrideType =
SB_NewDataRemote("faceplate.status.override.type");
statusOverrideText.stringValue = "";
if (count == 1) {
statusOverrideText.stringValue = count + " track";
} else {
statusOverrideText.stringValue = count + " tracks";
}
statusOverrideType.stringValue = "report"
},
//onNowFollowing: function listener_onNowFollowing(aUser) { },
QueryInterface: XPCOMUtils.generateQI([Ci.sbISoundCloudListener])
}
this._service.addListener(this.listener);
var firstrun = Application.prefs.getValue(SOUNDCLOUD_FIRST_RUN, false);
if (itemCount == 0) {
this._directory.setAttribute("disabled", true);
this._topLayer.hidden = false;
if (firstrun) {
this._idleDeck.selectedIndex = 0;
} else {
this._idleDeck.selectedIndex = 2;
this.listener.onTracksAdded(self._library);
}
} else {
this.listener.onTracksAdded(self._library);
}
// Add listener for playlist "Download" clicks
this._directory.addEventListener("PlaylistCellClick", function(e) {
return self.onDownloadClick(e);
}, false);
}
CloudDirectory.triggerSearch = function CloudDirectory_triggerSearch(aEvent) {
var self = this;
var params = "";
var query = encodeURIComponent(this._searchBox.value);
var flags = {
"filter": "streamable",
"order": "hotness"
};
for (var flag in flags) {
params += flag + "=" + flags[flag] + "&";
}
this._service.notifyListeners("onSearchTriggered");
this._service.getTracks(null, query, params, 0);
}
CloudDirectory.onDownloadClick = function CloudDirectory_onDownloadClick(aEvent) {
var prop = aEvent.getData("property");
var item = aEvent.getData("item");
if (item.getProperty(prop) != "" && prop == SB_PROPERTY_DOWNLOAD_IMAGE) {
var ddh = Cc["@songbirdnest.com/Songbird/DownloadDeviceHelper;1"]
.getService(Ci.sbIDownloadDeviceHelper);
var ios = Cc["@mozilla.org/network/io-service;1"]
.getService(Ci.nsIIOService);
var downloads = this._service.downloads;
var url = item.getProperty(SB_PROPERTY_DOWNLOAD_URL);
var properties = {};
properties[SBProperties.trackName] =
item.getProperty(SBProperties.trackName);
properties[SBProperties.trackType] = "soundcloud";
var propertyArray = SBProperties.createArray(properties);
var downloadItem =
downloads.createMediaItem(ios.newURI(url, null, null),
propertyArray);
downloadItem.setProperty(SB_PROPERTY_WAVEFORM,
item.getProperty(SB_PROPERTY_WAVEFORM));
ddh.downloadItem(downloadItem);
}
}
CloudDirectory.onUnload = function CloudDirectory_onUnload() {
if (this._directory) {
this._directory.destroy();
this._directory = null;
}
if (this._service) {
this._service.removeListener(this.listener);
}
if (this._domEventListenerSet) {
this._domEventListenerSet.removeAll();
this._domEventListenerSet = null;
}
}