-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
550 lines (480 loc) · 14.4 KB
/
main.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/*
* Copyright (C) 2009 Marcelo Vanzin (vanza@users.sourceforge.net)
* Copyright (C) 2010 Ghislain Mary
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/*
* Random Album
*
* An amarok script that monitors the playlist, and, when it's finished,
* repopulates it with a random album from the collection. Differently
* from amarok's built-in "random album" mode, you don't need to keep
* the whole collection in the playlist; so when you add new stuff to
* the collection the script will automatically pick it up, and it's
* easy to manually enqueue songs into the current playlist without
* the "random mode" triggering whenever the current album finishes
* playing.
*
* What it does: whenever the last track in the current playlist
* finishes playing, the playlist is cleared and a random album from
* the collection is chosen to populate the new playlist. The script
* ignores the last 10 albums played when enqueueing a new album, but
* other than that provides no bias (e.g., "ignore other albums by same
* artist as last album" and things like that).
*
* It also provides two actions (accessible through shortcuts on the
* "Tools" menu) to replace the current playlist, or to enqueue a
* random album to the current playlist.
*
* When using this script, it is recommended to disable any repeat or
* random settings in the "Playlist" menu.
*
* This script is inspired and very loosely based on the randomalbum.py
* script for amarok 1.4, available at the following URL:
*
* http://www.kde-apps.org/content/show.php?content=42764
*
* Note: I'm not sure whether Amarok 2 officially supports custom database
* backends, but this script only supports MySQL (due to the use of MySQL
* functions in the SQL queries).
*/
Importer.loadQtBinding("qt.core");
Importer.loadQtBinding("qt.gui");
Importer.loadQtBinding("qt.uitools");
//{{{ SQL Queries
var RANDOM_ALBUM_ID =
"SELECT "
+ " album "
+ "FROM "
+ " randomalbum_album_urls "
+ "WHERE "
+ " url LIKE '%{0}%' "
+ " AND genre IN ({1}) "
+ "UNION "
+ "SELECT "
+ " album "
+ "FROM "
+ " randomalbum_album_urls_ex "
+ "WHERE "
+ " url LIKE '%{0}%' "
+ " AND genre IN ({1}) "
+ "ORDER BY RAND() LIMIT 1";
var ALBUM_TRACKS =
"SELECT "
+ " url, "
+ " discno, "
+ " trackno "
+ "FROM "
+ " randomalbum_album_urls "
+ "WHERE "
+ " album = {0} "
+ " AND url LIKE '%{1}%' "
+ "UNION "
+ "SELECT "
+ " url, "
+ " discno, "
+ " trackno "
+ "FROM "
+ " randomalbum_album_urls_ex "
+ "WHERE "
+ " album = {0} "
+ " AND url LIKE '%{1}%' "
+ "ORDER BY discno, trackno";
var ALBUM_ARTIST_TRACKS =
"SELECT "
+ " url, "
+ " discno, "
+ " trackno "
+ "FROM "
+ " randomalbum_album_urls "
+ "WHERE "
+ " album = {0} "
+ " AND artist = {1} "
+ "UNION "
+ "SELECT "
+ " url, "
+ " discno, "
+ " trackno "
+ "FROM "
+ " randomalbum_album_urls "
+ "WHERE "
+ " album = {0} "
+ " AND artist = {1} "
+ "ORDER BY discno, trackno";
/*
* These two views keep album data in an easy-to-query format for the script;
* this way it's easy to get the track URLs and to filter by it if necessary.
* Creating a single view using UNION seems to result in slower queries than
* creating two views and creating a UNION of the results in the other queries,
* although it requires more code...
*/
var ALBUM_VIEW_1 =
"CREATE OR REPLACE VIEW "
+ " randomalbum_album_urls "
+ "AS "
+ "SELECT "
+ " tracks.album as album "
+ " , albums.artist as artist "
+ " , CONCAT('file:///', TRIM(LEADING '/' FROM devices.lastmountpoint), TRIM(LEADING '//' FROM urls.rpath)) as url "
+ " , tracks.discnumber as discno "
+ " , tracks.tracknumber as trackno "
+ " , tracks.genre as genre "
+ "FROM "
+ " albums, tracks, urls, devices "
+ "WHERE "
+ " tracks.album = albums.id "
+ " AND tracks.url = urls.id "
+ " AND devices.id = urls.deviceid ";
var ALBUM_VIEW_2 =
"CREATE OR REPLACE VIEW "
+ " randomalbum_album_urls_ex "
+ "AS "
+ "SELECT "
+ " tracks.album as album "
+ " , albums.artist as artist "
+ " , CONCAT('file:///', TRIM(LEADING '//' FROM urls.rpath)) as url "
+ " , tracks.discnumber as discno "
+ " , tracks.tracknumber as trackno "
+ " , tracks.genre as genre "
+ "FROM "
+ " albums, tracks, urls "
+ "WHERE "
+ " tracks.album = albums.id "
+ " AND tracks.url = urls.id "
+ " AND urls.deviceid = -1";
var ALBUM_ARTIST_COUNT =
"SELECT DISTINCT "
+ " artist "
+ "FROM "
+ " albums "
+ "WHERE "
+ " id = {0}";
var GENRES =
"SELECT "
+ " id, name "
+ "FROM "
+ " genres "
+ "ORDER BY name";
var GENRE_IDS =
"SELECT "
+ " id "
+ "FROM "
+ " genres ";
//}}}
/*
* Format function to keep my sanity instead of using string
* concatenation.
*/
String.prototype.format = function()
{
var pattern = /\{\d+\}/g;
var args = arguments;
return this.replace(pattern,
function(capture)
{
return args[capture.match(/\d+/)];
});
}
/*
* Returns a function that will call the given function with the given
* arguments when invoked.
*/
Function.prototype.bind = function()
{
var func = this;
var args = arguments;
return function() {
return func.apply(func, args);
}
}
var enableRandom = (Amarok.Script.readConfig("enable", "true") == "true");
var pathFilter = Amarok.Script.readConfig("pathFilter", "");
/* Load the saves list of last played albums. */
var lastPlayed = Amarok.Script.readConfig("lastPlayed", "");
if (lastPlayed) {
lastPlayed = lastPlayed.split(",");
} else {
lastPlayed = new Array();
}
/* Load the genres from which to choose random albums. */
var genresFilter = Amarok.Script.readConfig("genresFilter", "");
if (genresFilter) {
genresFilter = genresFilter.split(",");
} else {
genresFilter = new Array();
}
/*
* Whether to stop playback after the current track. Normally you'd be
* able to check this in the "track changed" callback when the current
* track finishes playing, but the callbacks seem to be called at
* different times in different versions of amarok, so that approach
* doesn't always work.
*
* This variable is kept updated by a callback that monitors the current
* track's playback - see `trackSeekedCb`.
*/
var stopAfter = false;
function getAlbum(aid)
{
var artists = Amarok.Collection.query(ALBUM_ARTIST_COUNT.format(aid));
var ulist;
var tlist;
/*
* In amarok 1.4, if two artists had an album with the same name,
* they'd share the same entry in the albums table. This seems to
* have changed in 2.0, but the code below tries to protect against
* that "just in case" (tm).
*/
if (artists.length == 1) {
ulist = Amarok.Collection.query(ALBUM_TRACKS.format(aid, pathFilter));
} else {
var artist = Math.floor(Math.random() * (artists.length - 1));
artist = parseInt();
ulist = Amarok.Collection.query(ALBUM_ARTIST_TRACKS.format(aid, artist));
}
Amarok.debug("New random album, {0} tracks.".format(ulist.length));
tlist = new Array();
for (var i = 0; i < ulist.length; i+= 3) {
tlist.push(ulist[i]);
}
return tlist;
}
function shouldPlayAlbum(idx)
{
for (var i = 0; i < lastPlayed.length; i++) {
if (lastPlayed[i] == idx) {
return false;
}
}
if (lastPlayed.length == 10) {
lastPlayed.shift();
}
lastPlayed.push(idx);
Amarok.Script.writeConfig("lastPlayed", lastPlayed.join(","));
return true;
}
function randomize()
{
/* Get a random album index that hasn't been played recently. */
var idx;
var tries = 10;
var genresStr = genresFilter.length ? genresFilter.join(", ")
: GENRE_IDS;
do {
idx = Amarok.Collection.query(RANDOM_ALBUM_ID.format(pathFilter,
genresStr))[0];
tries--;
} while (!shouldPlayAlbum(idx) && tries > 0);
/* Load the album. */
Amarok.debug("Loading album " + idx);
var list = getAlbum(idx);
for (var i = 0; i < list.length; i++) {
Amarok.debug("Loading track " + list[i]);
var url = QUrl.fromLocalFile(list[i].substring(7));
Amarok.Playlist.addMedia(url);
}
}
function loadRandom()
{
Amarok.Playlist.clearPlaylist();
randomize();
}
function startPlaying()
{
Amarok.Playlist.playByIndex(0);
}
function playRandom()
{
loadRandom();
var timer = new QTimer(Amarok.Window);
timer.singleShot = true;
timer.timeout.connect(startPlaying);
timer.start(100);
}
function checkStopAfter()
{
try {
stopAfter = Amarok.Playlist.stopAfterCurrent();
Amarok.debug("STOP AFTER IS ON? {0}".format(stopAfter));
} catch (err) {
/* We're probably running on pre-2.3.1. */
stopAfter = false;
}
}
function doRandomAlbum()
{
var timer = new QTimer(Amarok.Window);
timer.singleShot = true;
if (stopAfter) {
Amarok.debug("LOADING NEW RANDOM ALBUM");
Amarok.Playlist.setStopAfterCurrent(false);
timer.timeout.connect(loadRandom);
} else {
Amarok.debug("PLAYING NEW RANDOM ALBUM");
timer.timeout.connect(playRandom);
}
timer.start(100);
stopAfter = false;
}
/**
* Callback for checking whether a random album will be loaded on track
* change. We check the engine state to make sure it's still playing
* (meaning the user didn't stop playback of the last track), and that
* the currently playing track is 0 (meaning the user didn't go back
* in the list or chose some other track).
*
* @param track Index of the track that triggered the callback.
*/
function trackChanged(track)
{
var active = Amarok.Playlist.activeIndex();
if ((active == -1 || active == 0) &&
(track == -1 || track == Amarok.Playlist.totalTrackCount() - 1)) {
doRandomAlbum();
}
}
/**
* Track changed callback. If the active track is the last one in the
* playlist, schedule a function to run "soon" that will maybe load a
* random album.
*/
function trackChangedCb()
{
var active = Amarok.Playlist.activeIndex();
if (enableRandom && active == Amarok.Playlist.totalTrackCount() - 1 &&
!stopAfter) {
var timer = new QTimer(Amarok.Window);
timer.singleShot = true;
timer.timeout.connect(trackChanged.bind(active));
timer.start(100);
}
}
/**
* If the track that just finished is the last in the playlist,
* load a random album.
*
* Amarok doesn't call this callback when stopping playback manually.
* So it's safe to have both this callback and the track changed
* callback enabled simultaneously.
*/
function trackFinishedCb()
{
var active = Amarok.Playlist.activeIndex();
if (enableRandom &&
(active == -1 || active == Amarok.Playlist.totalTrackCount() - 1)) {
var timer = new QTimer(Amarok.Window);
timer.singleShot = true;
timer.timeout.connect(trackChanged.bind(active));
timer.start(100);
}
}
function trackSeekedCb(pos) {
var remaining = Amarok.Engine.currentTrack().length -
Amarok.Engine.trackPositionMs();
if (remaining <= 10000) {
Amarok.debug("CHECKING STOP AFTER, TRACK ALMOST DONE.");
checkStopAfter();
}
}
/**
* Shows the dialog for configuring random album settings.
*/
function showConfigDialog()
{
var loader = new QUiLoader(Amarok.Window);
var file = new QFile(Amarok.Info.scriptPath() + "/rasettings.ui", loader);
var genres = Amarok.Collection.query(GENRES);
var genreIds = new Array();
var genreNames = new Array();
for (var i = 0; i < genres.length; i += 2) {
var name = genres[i+1];
genreIds.push(genres[i]);
genreNames.push(name ? name : "(Unspecified)");
}
var genresModel = new QStandardItemModel(genreIds.length, 2);
for (var row = 0; row < genreIds.length; row++) {
genresModel.setItem(row, 0, new QStandardItem(genreIds[row]));
genresModel.setItem(row, 1, new QStandardItem(genreNames[row]));
}
var dialog = loader.load(file, Amarok.Window);
function addGenre(item)
{
var idx = genreNames.indexOf(item.data());
genresFilter.push(genreIds[idx]);
}
function ok()
{
enableRandom = dialog.enableRA.checked;
pathFilter = dialog.pathFilter.text;
var selectedIndexes = dialog.genresList.selectionModel().selectedIndexes();
genresFilter = new Array();
selectedIndexes.forEach(addGenre);
Amarok.Script.writeConfig("enable", enableRandom ? "true" : "false");
Amarok.Script.writeConfig("pathFilter", pathFilter);
Amarok.Script.writeConfig("genresFilter", genresFilter.join(","));
dialog.close();
}
function search()
{
var filedlg = new QFileDialog(dialog);
filedlg.fileMode = QFileDialog.DirectoryOnly;
filedlg.acceptMode = QFileDialog.AcceptOpen;
if (filedlg.exec()) {
dialog.pathFilter.text = filedlg.directory().absolutePath();
}
}
dialog.enableRA.setChecked(enableRandom);
dialog.pathFilter.text = pathFilter;
dialog.genresList.setModel(genresModel);
dialog.genresList.modelColumn = 1;
dialog.genresList.selectionModel().clear();
for (var i = 0; i < genresModel.rowCount(); i++) {
if (genresFilter.indexOf(genresModel.index(i, 0).data()) >= 0) {
dialog.genresList.selectionModel().select(genresModel.index(i, 1),
QItemSelectionModel.Select);
}
}
dialog.buttonBox.accepted.connect(ok);
dialog.searchBtn['clicked()'].connect(search);
dialog.exec();
}
/*
* When the script loads, make sure we have the random album view
* in its latest version.
*/
Amarok.Collection.query(ALBUM_VIEW_1);
Amarok.Collection.query(ALBUM_VIEW_2);
Amarok.Engine.trackChanged.connect(trackChangedCb);
Amarok.Engine.trackFinished.connect(trackFinishedCb);
Amarok.Engine.trackSeeked.connect(trackSeekedCb);
if (Amarok.Window.addToolsMenu("rand_album_load",
"Play Random Album",
"media-album-shuffle-amarok")) {
Amarok.Window.ToolsMenu.rand_album_load['triggered()']
.connect(playRandom);
}
if (Amarok.Window.addToolsMenu("rand_album_enqueue",
"Enqueue Random Album",
"media-album-shuffle-amarok")) {
Amarok.Window.ToolsMenu.rand_album_enqueue['triggered()']
.connect(randomize);
}
if (Amarok.Window.addSettingsMenu("rand_album_settings",
"Random Album Settings",
"media-album-shuffle-amarok")) {
Amarok.Window.SettingsMenu.rand_album_settings['triggered()']
.connect(showConfigDialog);
}