Mercurial > audlegacy
annotate src/audacious/playlist.c @ 2893:fb30d861b1f5 trunk
branch merge
author | William Pitcock <nenolod@atheme.org> |
---|---|
date | Sun, 24 Jun 2007 05:33:57 -0500 |
parents | 81604260bc08 |
children | 6131bf51ee63 |
rev | line source |
---|---|
2313 | 1 /* Audacious |
2 * Copyright (C) 2005-2007 Audacious team. | |
3 * | |
4 * BMP (C) GPL 2003 $top_src_dir/AUTHORS | |
5 * | |
6 * based on: | |
7 * | |
8 * XMMS - Cross-platform multimedia player | |
9 * Copyright (C) 1998-2003 Peter Alm, Mikael Alm, Olle Hallnas, | |
10 * Thomas Nilsson and 4Front Technologies | |
11 * Copyright (C) 1999-2003 Haavard Kvaalen | |
12 * | |
13 * This program is free software; you can redistribute it and/or modify | |
14 * it under the terms of the GNU General Public License as published by | |
15 * the Free Software Foundation; under version 2 of the License. | |
16 * | |
17 * This program is distributed in the hope that it will be useful, | |
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 * GNU General Public License for more details. | |
21 * | |
22 * You should have received a copy of the GNU General Public License | |
23 * along with this program; if not, write to the Free Software | |
24 * Foundation, Inc., 59 Tmple Place - Suite 330, Boston, MA 02110-1301, USA. | |
25 */ | |
26 | |
27 #ifdef HAVE_CONFIG_H | |
28 # include "config.h" | |
29 #endif | |
30 | |
31 #include "playlist.h" | |
32 | |
2633
c079e507869a
[svn] - use a managed heap for playlist entry node allocation.
nenolod
parents:
2614
diff
changeset
|
33 #include <mowgli.h> |
2313 | 34 #include <glib.h> |
35 #include <glib/gprintf.h> | |
36 #include <stdlib.h> | |
37 #include <string.h> | |
38 #include <time.h> | |
39 | |
40 #include <unistd.h> | |
41 #include <sys/types.h> | |
42 #include <sys/stat.h> | |
43 #include <sys/errno.h> | |
44 | |
45 #if defined(USE_REGEX_ONIGURUMA) | |
46 #include <onigposix.h> | |
47 #elif defined(USE_REGEX_PCRE) | |
48 #include <pcreposix.h> | |
49 #else | |
50 #include <regex.h> | |
51 #endif | |
52 | |
53 #include "input.h" | |
54 #include "main.h" | |
55 #include "ui_main.h" | |
2416
0fd7f4f969ad
[svn] integrated urldecode.* from libaudacious into audacious directory, made separate ui_fileopener.*
mf0102
parents:
2408
diff
changeset
|
56 #include "util.h" |
2717 | 57 #include "configdb.h" |
2313 | 58 #include "vfs.h" |
2416
0fd7f4f969ad
[svn] integrated urldecode.* from libaudacious into audacious directory, made separate ui_fileopener.*
mf0102
parents:
2408
diff
changeset
|
59 #include "urldecode.h" |
2313 | 60 #include "ui_equalizer.h" |
61 #include "playback.h" | |
62 #include "playlist.h" | |
63 #include "playlist_container.h" | |
2499
15a1f5ee4d1c
[svn] - playlist_manager -> ui_playlist_manager, since it's a UI component.
nenolod
parents:
2493
diff
changeset
|
64 #include "ui_playlist_manager.h" |
2313 | 65 #include "ui_playlist.h" |
2373
ad1d7687814c
[svn] made strings.h for existing strings.c, cleanups
mf0102
parents:
2328
diff
changeset
|
66 #include "strings.h" |
2313 | 67 #include "ui_fileinfo.h" |
68 | |
69 #include "debug.h" | |
70 | |
2407
1dc1d36d0347
[svn] - add hooks: playback begin, playback end, playlist reached end
nenolod
parents:
2380
diff
changeset
|
71 #include "hook.h" |
1dc1d36d0347
[svn] - add hooks: playback begin, playback end, playlist reached end
nenolod
parents:
2380
diff
changeset
|
72 |
2313 | 73 typedef gint (*PlaylistCompareFunc) (PlaylistEntry * a, PlaylistEntry * b); |
74 typedef void (*PlaylistSaveFunc) (FILE * file); | |
75 | |
76 /* If we manually change the song, p_p_b_j will show us where to go back to */ | |
77 PlaylistEntry *playlist_position_before_jump = NULL; | |
78 | |
79 static GList *playlists = NULL; | |
80 static GList *playlists_iter; | |
81 | |
82 /* If this is set to TRUE, we do not probe upon playlist add. | |
83 * | |
84 * Under Audacious 0.1.x, this was not a big deal because we used | |
85 * file extension introspection instead of looking for file format magic | |
86 * strings. | |
87 * | |
88 * Because we use file magic strings, we have to fstat a file being added | |
89 * to a playlist up to 1 * <number of input plugins installed> times. | |
90 * | |
91 * This can get really slow now that we're looking for files to add to a | |
92 * playlist. (Up to 5 minutes for 5000 songs, etcetera.) | |
93 * | |
94 * So, we obviously don't want to probe while opening a large playlist | |
95 * up. Hince the boolean below. | |
96 * | |
97 * January 7, 2006, William Pitcock <nenolod@nenolod.net> | |
98 */ | |
99 | |
100 G_LOCK_DEFINE(playlist_get_info_going); | |
101 | |
2328 | 102 //static gchar *playlist_current_name = NULL; |
2313 | 103 |
104 static gboolean playlist_get_info_scan_active = FALSE; | |
105 static gboolean playlist_get_info_going = FALSE; | |
106 static GThread *playlist_get_info_thread; | |
107 | |
108 static gint path_compare(const gchar * a, const gchar * b); | |
109 static gint playlist_compare_path(PlaylistEntry * a, PlaylistEntry * b); | |
110 static gint playlist_compare_filename(PlaylistEntry * a, PlaylistEntry * b); | |
111 static gint playlist_compare_title(PlaylistEntry * a, PlaylistEntry * b); | |
112 static gint playlist_compare_artist(PlaylistEntry * a, PlaylistEntry * b); | |
113 static time_t playlist_get_mtime(const gchar *filename); | |
114 static gint playlist_compare_date(PlaylistEntry * a, PlaylistEntry * b); | |
115 static gint playlist_compare_track(PlaylistEntry * a, PlaylistEntry * b); | |
116 static gint playlist_compare_playlist(PlaylistEntry * a, PlaylistEntry * b); | |
117 | |
118 static gint playlist_dupscmp_path(PlaylistEntry * a, PlaylistEntry * b); | |
119 static gint playlist_dupscmp_filename(PlaylistEntry * a, PlaylistEntry * b); | |
120 static gint playlist_dupscmp_title(PlaylistEntry * a, PlaylistEntry * b); | |
121 | |
122 static PlaylistCompareFunc playlist_compare_func_table[] = { | |
123 playlist_compare_path, | |
124 playlist_compare_filename, | |
125 playlist_compare_title, | |
126 playlist_compare_artist, | |
127 playlist_compare_date, | |
128 playlist_compare_track, | |
129 playlist_compare_playlist | |
130 }; | |
131 | |
132 static guint playlist_load_ins(Playlist * playlist, const gchar * filename, gint pos); | |
133 | |
134 static void playlist_generate_shuffle_list(Playlist *); | |
135 static void playlist_generate_shuffle_list_nolock(Playlist *); | |
136 | |
137 static void playlist_recalc_total_time_nolock(Playlist *); | |
138 static void playlist_recalc_total_time(Playlist *); | |
139 static gboolean playlist_entry_get_info(PlaylistEntry * entry); | |
140 | |
2633
c079e507869a
[svn] - use a managed heap for playlist entry node allocation.
nenolod
parents:
2614
diff
changeset
|
141 static mowgli_heap_t *playlist_entry_heap = NULL; |
c079e507869a
[svn] - use a managed heap for playlist entry node allocation.
nenolod
parents:
2614
diff
changeset
|
142 |
2313 | 143 /* *********************** playlist entry code ********************** */ |
144 | |
145 PlaylistEntry * | |
146 playlist_entry_new(const gchar * filename, | |
147 const gchar * title, | |
148 const gint length, | |
149 InputPlugin * dec) | |
150 { | |
151 PlaylistEntry *entry; | |
152 | |
2633
c079e507869a
[svn] - use a managed heap for playlist entry node allocation.
nenolod
parents:
2614
diff
changeset
|
153 entry = mowgli_heap_alloc(playlist_entry_heap); |
2313 | 154 entry->filename = g_strdup(filename); |
155 entry->title = str_to_utf8(title); | |
156 entry->length = length; | |
157 entry->selected = FALSE; | |
158 entry->decoder = dec; | |
159 | |
160 /* only do this if we have a decoder, otherwise it just takes too long */ | |
161 if (entry->decoder) | |
162 playlist_entry_get_info(entry); | |
163 | |
164 return entry; | |
165 } | |
166 | |
167 void | |
168 playlist_entry_free(PlaylistEntry * entry) | |
169 { | |
170 if (!entry) | |
171 return; | |
172 | |
173 if (entry->tuple != NULL) { | |
174 bmp_title_input_free(entry->tuple); | |
175 entry->tuple = NULL; | |
176 } | |
177 | |
178 if (entry->filename != NULL) | |
179 g_free(entry->filename); | |
180 | |
181 if (entry->title != NULL) | |
182 g_free(entry->title); | |
183 | |
2633
c079e507869a
[svn] - use a managed heap for playlist entry node allocation.
nenolod
parents:
2614
diff
changeset
|
184 mowgli_heap_free(playlist_entry_heap, entry); |
2313 | 185 } |
186 | |
187 static gboolean | |
188 playlist_entry_get_info(PlaylistEntry * entry) | |
189 { | |
190 TitleInput *tuple; | |
191 time_t modtime; | |
192 | |
193 g_return_val_if_fail(entry != NULL, FALSE); | |
194 | |
195 if (entry->tuple == NULL || entry->tuple->mtime > 0 || entry->tuple->mtime == -1) | |
2636 | 196 modtime = playlist_get_mtime(entry->filename); |
2313 | 197 else |
2636 | 198 modtime = 0; /* URI -nenolod */ |
2313 | 199 |
2668
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
200 if (str_has_prefix_nocase(entry->filename, "http:") && |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
201 g_thread_self() != playlist_get_info_thread) |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
202 { |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
203 g_print("attempting to retrieve remote info not in background thread!\n"); |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
204 return FALSE; |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
205 } |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
206 |
2313 | 207 if (entry->decoder == NULL) |
208 entry->decoder = input_check_file(entry->filename, FALSE); | |
209 | |
210 /* renew tuple if file mtime is newer than tuple mtime. */ | |
211 if(entry->tuple){ | |
212 if(entry->tuple->mtime == modtime) | |
213 return TRUE; | |
214 else { | |
215 bmp_title_input_free(entry->tuple); | |
216 entry->tuple = NULL; | |
217 } | |
218 } | |
219 | |
220 if (entry->decoder == NULL || entry->decoder->get_song_tuple == NULL) | |
221 tuple = input_get_song_tuple(entry->filename); | |
222 else | |
223 tuple = entry->decoder->get_song_tuple(entry->filename); | |
224 | |
225 if (tuple == NULL) | |
226 return FALSE; | |
227 | |
228 /* attach mtime */ | |
229 tuple->mtime = modtime; | |
230 | |
231 /* entry is still around */ | |
232 entry->title = xmms_get_titlestring(tuple->formatter != NULL ? tuple->formatter : xmms_get_gentitle_format(), tuple); | |
233 entry->length = tuple->length; | |
234 entry->tuple = tuple; | |
235 | |
236 return TRUE; | |
237 } | |
238 | |
239 /* *********************** playlist selector code ************************* */ | |
240 | |
241 void | |
242 playlist_init(void) | |
243 { | |
244 Playlist *initial_pl; | |
245 | |
2633
c079e507869a
[svn] - use a managed heap for playlist entry node allocation.
nenolod
parents:
2614
diff
changeset
|
246 /* create a heap with 1024 playlist entry nodes preallocated. --nenolod */ |
c079e507869a
[svn] - use a managed heap for playlist entry node allocation.
nenolod
parents:
2614
diff
changeset
|
247 playlist_entry_heap = mowgli_heap_create(sizeof(PlaylistEntry), 1024, |
c079e507869a
[svn] - use a managed heap for playlist entry node allocation.
nenolod
parents:
2614
diff
changeset
|
248 BH_NOW); |
c079e507869a
[svn] - use a managed heap for playlist entry node allocation.
nenolod
parents:
2614
diff
changeset
|
249 |
2313 | 250 /* FIXME: is this really necessary? REQUIRE_STATIC_LOCK(playlists); */ |
251 | |
252 initial_pl = playlist_new(); | |
253 | |
254 playlist_add_playlist(initial_pl); | |
255 } | |
256 | |
257 void | |
258 playlist_add_playlist(Playlist *playlist) | |
259 { | |
260 playlists = g_list_append(playlists, playlist); | |
261 | |
262 if (playlists_iter == NULL) | |
263 playlists_iter = playlists; | |
264 | |
265 playlist_manager_update(); | |
266 } | |
267 | |
268 void | |
269 playlist_remove_playlist(Playlist *playlist) | |
270 { | |
2820 | 271 /* users suppose playback will be stopped on removing playlist */ |
272 if (playback_get_playing()) { | |
273 ip_data.stop = TRUE; | |
274 playback_stop(); | |
275 ip_data.stop = FALSE; | |
276 mainwin_clear_song_info(); | |
277 } | |
278 | |
2313 | 279 /* trying to free the last playlist simply clears and resets it */ |
280 if (g_list_length(playlists) < 2) { | |
281 playlist_clear(playlist); | |
282 playlist_set_current_name(playlist, NULL); | |
283 return; | |
284 } | |
285 | |
286 if (playlist == playlist_get_active()) | |
287 playlist_select_next(); | |
288 | |
289 /* upon removal, a playlist should be cleared and freed */ | |
290 playlists = g_list_remove(playlists, playlist); | |
291 playlist_clear(playlist); | |
292 playlist_free(playlist); | |
293 | |
294 if (playlists_iter == NULL) | |
295 playlists_iter = playlists; | |
296 | |
297 playlist_manager_update(); | |
298 } | |
299 | |
300 GList * | |
301 playlist_get_playlists(void) | |
302 { | |
303 return playlists; | |
304 } | |
305 | |
306 void | |
307 playlist_select_next(void) | |
308 { | |
309 if (playlists_iter == NULL) | |
310 playlists_iter = playlists; | |
311 | |
312 playlists_iter = g_list_next(playlists_iter); | |
313 | |
314 if (playlists_iter == NULL) | |
315 playlists_iter = playlists; | |
316 | |
317 playlistwin_update_list(playlist_get_active()); | |
318 } | |
319 | |
320 void | |
321 playlist_select_prev(void) | |
322 { | |
323 if (playlists_iter == NULL) | |
324 playlists_iter = playlists; | |
325 | |
326 playlists_iter = g_list_previous(playlists_iter); | |
327 | |
328 if (playlists_iter == NULL) | |
329 playlists_iter = playlists; | |
330 | |
331 playlistwin_update_list(playlist_get_active()); | |
332 } | |
333 | |
334 void | |
335 playlist_select_playlist(Playlist *playlist) | |
336 { | |
337 if (playlists_iter == NULL) | |
338 playlists_iter = playlists; | |
339 | |
340 playlists_iter = g_list_find(playlists, playlist); | |
341 | |
342 if (playlists_iter == NULL) | |
343 playlists_iter = playlists; | |
344 | |
345 playlistwin_update_list(playlist); | |
346 } | |
347 | |
348 /* *********************** playlist code ********************** */ | |
349 | |
350 const gchar * | |
351 playlist_get_current_name(Playlist *playlist) | |
352 { | |
353 return playlist->title; | |
354 } | |
355 | |
356 gboolean | |
357 playlist_set_current_name(Playlist *playlist, const gchar * filename) | |
358 { | |
359 if (playlist->title) | |
360 g_free(playlist->title); | |
361 | |
362 if (!filename) { | |
363 playlist->title = NULL; | |
364 return FALSE; | |
365 } | |
366 | |
2504
fcf730269639
[svn] - convert filename string to utf8 for playlist->title (closes bug #789)
giacomo
parents:
2499
diff
changeset
|
367 playlist->title = filename_to_utf8(filename); |
2313 | 368 return TRUE; |
369 } | |
370 | |
371 static GList * | |
372 find_playlist_position_list(Playlist *playlist) | |
373 { | |
374 REQUIRE_LOCK(playlist->mutex); | |
375 | |
376 if (!playlist->position) { | |
377 if (cfg.shuffle) | |
378 return playlist->shuffle; | |
379 else | |
380 return playlist->entries; | |
381 } | |
382 | |
383 if (cfg.shuffle) | |
384 return g_list_find(playlist->shuffle, playlist->position); | |
385 else | |
386 return g_list_find(playlist->entries, playlist->position); | |
387 } | |
388 | |
389 static void | |
390 play_queued(Playlist *playlist) | |
391 { | |
392 GList *tmp = playlist->queue; | |
393 | |
394 REQUIRE_LOCK( playlist->mutex ); | |
395 | |
396 playlist->position = playlist->queue->data; | |
397 playlist->queue = g_list_remove_link(playlist->queue, playlist->queue); | |
398 g_list_free_1(tmp); | |
399 } | |
400 | |
401 void | |
402 playlist_clear(Playlist *playlist) | |
403 { | |
404 if (!playlist) | |
405 return; | |
406 | |
407 PLAYLIST_LOCK( playlist->mutex ); | |
408 | |
409 g_list_foreach(playlist->entries, (GFunc) playlist_entry_free, NULL); | |
410 g_list_free(playlist->entries); | |
411 playlist->position = NULL; | |
412 playlist->entries = NULL; | |
2549
ef59b072a5d2
[svn] - update playlist->tail when an entry has been removed.
yaz
parents:
2548
diff
changeset
|
413 playlist->tail = NULL; |
2637
420ce282920d
[svn] - clear playlist attribute when playlist_clear() is called.
yaz
parents:
2636
diff
changeset
|
414 playlist->attribute = PLAYLIST_PLAIN; |
2313 | 415 |
416 PLAYLIST_UNLOCK( playlist->mutex ); | |
417 | |
418 playlist_generate_shuffle_list(playlist); | |
419 playlistwin_update_list(playlist); | |
420 playlist_recalc_total_time(playlist); | |
421 playlist_manager_update(); | |
422 } | |
423 | |
424 static void | |
425 playlist_delete_node(Playlist * playlist, GList * node, gboolean * set_info_text, | |
426 gboolean * restart_playing) | |
427 { | |
428 PlaylistEntry *entry; | |
429 GList *playing_song = NULL; | |
430 | |
431 REQUIRE_LOCK(playlist->mutex); | |
432 | |
433 /* We call g_list_find manually here because we don't want an item | |
434 * in the shuffle_list */ | |
435 | |
436 if (playlist->position) | |
437 playing_song = g_list_find(playlist->entries, playlist->position); | |
438 | |
439 entry = PLAYLIST_ENTRY(node->data); | |
440 | |
441 if (playing_song == node) { | |
442 *set_info_text = TRUE; | |
443 | |
444 if (playback_get_playing()) { | |
445 PLAYLIST_UNLOCK(playlist->mutex); | |
446 ip_data.stop = TRUE; | |
447 playback_stop(); | |
448 ip_data.stop = FALSE; | |
449 PLAYLIST_LOCK(playlist->mutex); | |
450 *restart_playing = TRUE; | |
451 } | |
452 | |
453 playing_song = find_playlist_position_list(playlist); | |
454 | |
455 if (g_list_next(playing_song)) | |
456 playlist->position = g_list_next(playing_song)->data; | |
457 else if (g_list_previous(playing_song)) | |
458 playlist->position = g_list_previous(playing_song)->data; | |
459 else | |
460 playlist->position = NULL; | |
461 | |
462 /* Make sure the entry did not disappear under us */ | |
463 if (g_list_index(playlist->entries, entry) == -1) | |
464 return; | |
465 | |
466 } | |
467 else if (g_list_position(playlist->entries, playing_song) > | |
468 g_list_position(playlist->entries, node)) { | |
469 *set_info_text = TRUE; | |
470 } | |
471 | |
472 playlist->shuffle = g_list_remove(playlist->shuffle, entry); | |
473 playlist->queue = g_list_remove(playlist->queue, entry); | |
474 playlist->entries = g_list_remove_link(playlist->entries, node); | |
2549
ef59b072a5d2
[svn] - update playlist->tail when an entry has been removed.
yaz
parents:
2548
diff
changeset
|
475 playlist->tail = g_list_last(playlist->entries); |
2313 | 476 playlist_entry_free(entry); |
477 g_list_free_1(node); | |
478 | |
479 playlist_recalc_total_time_nolock(playlist); | |
480 } | |
481 | |
482 void | |
483 playlist_delete_index(Playlist *playlist, guint pos) | |
484 { | |
485 gboolean restart_playing = FALSE, set_info_text = FALSE; | |
486 GList *node; | |
487 | |
488 if (!playlist) | |
489 return; | |
490 | |
491 PLAYLIST_LOCK(playlist->mutex); | |
492 | |
493 node = g_list_nth(playlist->entries, pos); | |
494 | |
495 if (!node) { | |
496 PLAYLIST_UNLOCK(playlist->mutex); | |
497 return; | |
498 } | |
499 | |
500 playlist_delete_node(playlist, node, &set_info_text, &restart_playing); | |
501 | |
502 PLAYLIST_UNLOCK(playlist->mutex); | |
503 | |
504 playlist_recalc_total_time(playlist); | |
505 | |
506 playlistwin_update_list(playlist); | |
507 if (restart_playing) { | |
508 if (playlist->position) { | |
509 playback_initiate(); | |
510 } | |
511 else { | |
512 mainwin_clear_song_info(); | |
513 } | |
514 } | |
515 else if (set_info_text) { | |
516 mainwin_set_info_text(); | |
517 } | |
518 | |
519 playlist_manager_update(); | |
520 } | |
521 | |
522 void | |
523 playlist_delete_filenames(Playlist * playlist, GList * filenames) | |
524 { | |
525 GList *node, *fnode; | |
526 gboolean set_info_text = FALSE, restart_playing = FALSE; | |
527 | |
528 PLAYLIST_LOCK(playlist->mutex); | |
529 | |
530 for (fnode = filenames; fnode; fnode = g_list_next(fnode)) { | |
531 node = playlist->entries; | |
532 | |
533 while (node) { | |
534 GList *next = g_list_next(node); | |
535 PlaylistEntry *entry = node->data; | |
536 | |
537 if (!strcmp(entry->filename, fnode->data)) | |
538 playlist_delete_node(playlist, node, &set_info_text, &restart_playing); | |
539 | |
540 node = next; | |
541 } | |
542 } | |
543 | |
2792
790bb0954b93
[svn] - move playlist_recalc_total_time() out of the giant playlist lock in playlist_delete_filenames(). reported by dotzen, closes #908.
nenolod
parents:
2776
diff
changeset
|
544 PLAYLIST_UNLOCK(playlist->mutex); |
790bb0954b93
[svn] - move playlist_recalc_total_time() out of the giant playlist lock in playlist_delete_filenames(). reported by dotzen, closes #908.
nenolod
parents:
2776
diff
changeset
|
545 |
2313 | 546 playlist_recalc_total_time(playlist); |
547 playlistwin_update_list(playlist); | |
548 | |
549 if (restart_playing) { | |
550 if (playlist->position) { | |
551 playback_initiate(); | |
552 } | |
553 else { | |
554 mainwin_clear_song_info(); | |
555 } | |
556 } | |
557 else if (set_info_text) { | |
558 mainwin_set_info_text(); | |
559 } | |
560 | |
561 playlist_manager_update(); | |
562 } | |
563 | |
564 void | |
565 playlist_delete(Playlist * playlist, gboolean crop) | |
566 { | |
567 gboolean restart_playing = FALSE, set_info_text = FALSE; | |
568 GList *node, *next_node; | |
569 PlaylistEntry *entry; | |
570 | |
571 g_return_if_fail(playlist != NULL); | |
572 | |
573 PLAYLIST_LOCK(playlist->mutex); | |
574 | |
575 node = playlist->entries; | |
576 | |
577 while (node) { | |
578 entry = PLAYLIST_ENTRY(node->data); | |
579 | |
580 next_node = g_list_next(node); | |
581 | |
582 if ((entry->selected && !crop) || (!entry->selected && crop)) { | |
583 playlist_delete_node(playlist, node, &set_info_text, &restart_playing); | |
584 } | |
585 | |
586 node = next_node; | |
587 } | |
588 | |
589 PLAYLIST_UNLOCK(playlist->mutex); | |
590 | |
591 playlist_recalc_total_time(playlist); | |
592 | |
593 if (set_info_text) { | |
594 mainwin_set_info_text(); | |
595 } | |
596 | |
597 if (restart_playing) { | |
598 if (playlist->position) { | |
599 playback_initiate(); | |
600 } | |
601 else { | |
602 mainwin_clear_song_info(); | |
603 } | |
604 } | |
605 | |
606 playlistwin_update_list(playlist); | |
607 playlist_manager_update(); | |
608 } | |
609 | |
610 static void | |
611 __playlist_ins_with_info(Playlist * playlist, | |
2545
610d85b8a22b
[svn] - on loading a playlist with tuple, invoke the scanner thread only if mtime is -1.
yaz
parents:
2504
diff
changeset
|
612 const gchar * filename, |
2313 | 613 gint pos, |
614 const gchar * title, | |
615 gint len, | |
2545
610d85b8a22b
[svn] - on loading a playlist with tuple, invoke the scanner thread only if mtime is -1.
yaz
parents:
2504
diff
changeset
|
616 InputPlugin * dec) |
2313 | 617 { |
618 g_return_if_fail(filename != NULL); | |
619 | |
2668
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
620 PLAYLIST_LOCK( playlist->mutex ); |
2313 | 621 playlist->entries = g_list_insert(playlist->entries, |
622 playlist_entry_new(filename, title, len, dec), | |
623 pos); | |
2668
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
624 PLAYLIST_UNLOCK( playlist->mutex ); |
2313 | 625 |
626 g_mutex_lock(mutex_scan); | |
627 playlist_get_info_scan_active = TRUE; | |
628 g_mutex_unlock(mutex_scan); | |
629 g_cond_signal(cond_scan); | |
630 } | |
631 | |
632 static void | |
633 __playlist_ins_with_info_tuple(Playlist * playlist, | |
634 const gchar * filename, | |
635 gint pos, | |
636 TitleInput *tuple, | |
637 InputPlugin * dec) | |
638 { | |
639 PlaylistEntry *entry; | |
640 | |
641 g_return_if_fail(playlist != NULL); | |
642 g_return_if_fail(filename != NULL); | |
643 | |
2548
68d1e9761cc5
[svn] - highly experimental code to speed up loading huge playlist. let me know if it breaks something.
yaz
parents:
2545
diff
changeset
|
644 entry = playlist_entry_new(filename, tuple->track_name, tuple->length, dec); |
68d1e9761cc5
[svn] - highly experimental code to speed up loading huge playlist. let me know if it breaks something.
yaz
parents:
2545
diff
changeset
|
645 if(!playlist->tail) |
68d1e9761cc5
[svn] - highly experimental code to speed up loading huge playlist. let me know if it breaks something.
yaz
parents:
2545
diff
changeset
|
646 playlist->tail = g_list_last(playlist->entries); |
68d1e9761cc5
[svn] - highly experimental code to speed up loading huge playlist. let me know if it breaks something.
yaz
parents:
2545
diff
changeset
|
647 |
2668
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
648 PLAYLIST_LOCK( playlist->mutex ); |
2548
68d1e9761cc5
[svn] - highly experimental code to speed up loading huge playlist. let me know if it breaks something.
yaz
parents:
2545
diff
changeset
|
649 if(pos == -1) { // the common case |
68d1e9761cc5
[svn] - highly experimental code to speed up loading huge playlist. let me know if it breaks something.
yaz
parents:
2545
diff
changeset
|
650 GList *element; |
68d1e9761cc5
[svn] - highly experimental code to speed up loading huge playlist. let me know if it breaks something.
yaz
parents:
2545
diff
changeset
|
651 element = g_list_alloc(); |
68d1e9761cc5
[svn] - highly experimental code to speed up loading huge playlist. let me know if it breaks something.
yaz
parents:
2545
diff
changeset
|
652 element->data = entry; |
68d1e9761cc5
[svn] - highly experimental code to speed up loading huge playlist. let me know if it breaks something.
yaz
parents:
2545
diff
changeset
|
653 element->prev = playlist->tail; // NULL is allowed here. |
68d1e9761cc5
[svn] - highly experimental code to speed up loading huge playlist. let me know if it breaks something.
yaz
parents:
2545
diff
changeset
|
654 element->next = NULL; |
68d1e9761cc5
[svn] - highly experimental code to speed up loading huge playlist. let me know if it breaks something.
yaz
parents:
2545
diff
changeset
|
655 |
68d1e9761cc5
[svn] - highly experimental code to speed up loading huge playlist. let me know if it breaks something.
yaz
parents:
2545
diff
changeset
|
656 if(!playlist->entries) { // this is the first element |
68d1e9761cc5
[svn] - highly experimental code to speed up loading huge playlist. let me know if it breaks something.
yaz
parents:
2545
diff
changeset
|
657 playlist->entries = element; |
68d1e9761cc5
[svn] - highly experimental code to speed up loading huge playlist. let me know if it breaks something.
yaz
parents:
2545
diff
changeset
|
658 playlist->tail = element; |
68d1e9761cc5
[svn] - highly experimental code to speed up loading huge playlist. let me know if it breaks something.
yaz
parents:
2545
diff
changeset
|
659 } |
68d1e9761cc5
[svn] - highly experimental code to speed up loading huge playlist. let me know if it breaks something.
yaz
parents:
2545
diff
changeset
|
660 else { // the rests |
68d1e9761cc5
[svn] - highly experimental code to speed up loading huge playlist. let me know if it breaks something.
yaz
parents:
2545
diff
changeset
|
661 g_return_if_fail(playlist->tail != NULL); |
68d1e9761cc5
[svn] - highly experimental code to speed up loading huge playlist. let me know if it breaks something.
yaz
parents:
2545
diff
changeset
|
662 playlist->tail->next = element; |
68d1e9761cc5
[svn] - highly experimental code to speed up loading huge playlist. let me know if it breaks something.
yaz
parents:
2545
diff
changeset
|
663 playlist->tail = element; |
68d1e9761cc5
[svn] - highly experimental code to speed up loading huge playlist. let me know if it breaks something.
yaz
parents:
2545
diff
changeset
|
664 } |
68d1e9761cc5
[svn] - highly experimental code to speed up loading huge playlist. let me know if it breaks something.
yaz
parents:
2545
diff
changeset
|
665 } |
68d1e9761cc5
[svn] - highly experimental code to speed up loading huge playlist. let me know if it breaks something.
yaz
parents:
2545
diff
changeset
|
666 else { |
68d1e9761cc5
[svn] - highly experimental code to speed up loading huge playlist. let me know if it breaks something.
yaz
parents:
2545
diff
changeset
|
667 playlist->entries = g_list_insert(playlist->entries, entry, pos); |
68d1e9761cc5
[svn] - highly experimental code to speed up loading huge playlist. let me know if it breaks something.
yaz
parents:
2545
diff
changeset
|
668 } |
2313 | 669 |
2668
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
670 PLAYLIST_UNLOCK( playlist->mutex ); |
2313 | 671 if (tuple != NULL) { |
672 entry->title = xmms_get_titlestring(tuple->formatter != NULL ? tuple->formatter : xmms_get_gentitle_format(), tuple); | |
673 entry->length = tuple->length; | |
674 entry->tuple = tuple; | |
675 } | |
676 | |
2545
610d85b8a22b
[svn] - on loading a playlist with tuple, invoke the scanner thread only if mtime is -1.
yaz
parents:
2504
diff
changeset
|
677 if(tuple->mtime == -1) { // kick the scanner thread only if mtime = -1 (uninitialized). |
610d85b8a22b
[svn] - on loading a playlist with tuple, invoke the scanner thread only if mtime is -1.
yaz
parents:
2504
diff
changeset
|
678 g_mutex_lock(mutex_scan); |
610d85b8a22b
[svn] - on loading a playlist with tuple, invoke the scanner thread only if mtime is -1.
yaz
parents:
2504
diff
changeset
|
679 playlist_get_info_scan_active = TRUE; |
610d85b8a22b
[svn] - on loading a playlist with tuple, invoke the scanner thread only if mtime is -1.
yaz
parents:
2504
diff
changeset
|
680 g_mutex_unlock(mutex_scan); |
610d85b8a22b
[svn] - on loading a playlist with tuple, invoke the scanner thread only if mtime is -1.
yaz
parents:
2504
diff
changeset
|
681 g_cond_signal(cond_scan); |
610d85b8a22b
[svn] - on loading a playlist with tuple, invoke the scanner thread only if mtime is -1.
yaz
parents:
2504
diff
changeset
|
682 } |
2313 | 683 } |
684 | |
685 static void | |
686 __playlist_ins(Playlist * playlist, const gchar * filename, gint pos, InputPlugin *dec) | |
687 { | |
688 __playlist_ins_with_info(playlist, filename, pos, NULL, -1, dec); | |
689 playlist_recalc_total_time(playlist); | |
690 playlist_manager_update(); | |
691 } | |
692 | |
693 gboolean | |
694 playlist_ins(Playlist * playlist, const gchar * filename, gint pos) | |
695 { | |
696 gchar buf[64], *p; | |
697 gint r; | |
698 VFSFile *file; | |
2667
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
699 InputPlugin *dec = NULL; |
2313 | 700 |
701 g_return_val_if_fail(playlist != NULL, FALSE); | |
702 g_return_val_if_fail(filename != NULL, FALSE); | |
703 | |
704 if (is_playlist_name(filename)) { | |
705 playlist->loading_playlist = TRUE; | |
706 playlist_load_ins(playlist, filename, pos); | |
707 playlist->loading_playlist = FALSE; | |
708 return TRUE; | |
709 } | |
710 | |
711 if (playlist->loading_playlist == TRUE || cfg.playlist_detect == TRUE) | |
712 dec = NULL; | |
2667
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
713 else if (!str_has_prefix_nocase(filename, "http://") && |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
714 !str_has_prefix_nocase(filename, "https://")) |
2313 | 715 dec = input_check_file(filename, TRUE); |
716 | |
2701
906caaf4737d
[svn] - fix cuesheet bug in a better way (e.g. unbreak http:// streams)
nenolod
parents:
2690
diff
changeset
|
717 if (cfg.playlist_detect == TRUE || playlist->loading_playlist == TRUE || (playlist->loading_playlist == FALSE && dec != NULL) || (playlist->loading_playlist == FALSE && !is_playlist_name(filename) && str_has_prefix_nocase(filename, "http"))) |
2313 | 718 { |
2545
610d85b8a22b
[svn] - on loading a playlist with tuple, invoke the scanner thread only if mtime is -1.
yaz
parents:
2504
diff
changeset
|
719 __playlist_ins(playlist, filename, pos, dec); |
610d85b8a22b
[svn] - on loading a playlist with tuple, invoke the scanner thread only if mtime is -1.
yaz
parents:
2504
diff
changeset
|
720 playlist_generate_shuffle_list(playlist); |
610d85b8a22b
[svn] - on loading a playlist with tuple, invoke the scanner thread only if mtime is -1.
yaz
parents:
2504
diff
changeset
|
721 playlistwin_update_list(playlist); |
2313 | 722 return TRUE; |
723 } | |
724 | |
725 /* Some files (typically produced by some cgi-scripts) don't have | |
726 * the correct extension. Try to recognize these files by looking | |
727 * at their content. We only check for http entries since it does | |
728 * not make sense to have file entries in a playlist fetched from | |
729 * the net. */ | |
730 | |
731 /* Some strange people put fifo's with the .mp3 extension, so we | |
732 * need to make sure it's a real file (otherwise fread() may block | |
733 * and stall the entire program) */ | |
734 | |
735 /* FIXME: bah, FIFOs actually pass this regular file test */ | |
736 if (!vfs_file_test(filename, G_FILE_TEST_IS_REGULAR)) | |
737 return FALSE; | |
738 | |
739 if (!(file = vfs_fopen(filename, "rb"))) | |
740 return FALSE; | |
741 | |
742 r = vfs_fread(buf, 1, sizeof(buf), file); | |
743 vfs_fclose(file); | |
744 | |
745 for (p = buf; r-- > 0 && (*p == '\r' || *p == '\n'); p++); | |
746 | |
747 if (r > 5 && str_has_prefix_nocase(p, "http:")) { | |
748 playlist_load_ins(playlist, filename, pos); | |
749 return TRUE; | |
750 } | |
751 | |
2493
b7f48f00a342
[svn] - patch from Mark Glines to ad https:// URI support to playlists.
nenolod
parents:
2489
diff
changeset
|
752 if (r > 6 && str_has_prefix_nocase(p, "https:")) { |
b7f48f00a342
[svn] - patch from Mark Glines to ad https:// URI support to playlists.
nenolod
parents:
2489
diff
changeset
|
753 playlist_load_ins(playlist, filename, pos); |
b7f48f00a342
[svn] - patch from Mark Glines to ad https:// URI support to playlists.
nenolod
parents:
2489
diff
changeset
|
754 return TRUE; |
b7f48f00a342
[svn] - patch from Mark Glines to ad https:// URI support to playlists.
nenolod
parents:
2489
diff
changeset
|
755 } |
b7f48f00a342
[svn] - patch from Mark Glines to ad https:// URI support to playlists.
nenolod
parents:
2489
diff
changeset
|
756 |
2313 | 757 return FALSE; |
758 } | |
759 | |
760 /* FIXME: The next few functions are specific to Unix | |
761 * filesystems. Either abstract it away, or don't even bother checking | |
762 * at such low level */ | |
763 | |
764 typedef struct { | |
765 dev_t dev; | |
766 ino_t ino; | |
767 } DeviceInode; | |
768 | |
769 static DeviceInode * | |
770 devino_new(dev_t device, | |
771 ino_t inode) | |
772 { | |
773 DeviceInode *devino = g_new0(DeviceInode, 1); | |
774 | |
775 if (devino) | |
776 { | |
777 devino->dev = device; | |
778 devino->ino = inode; | |
779 } | |
780 | |
781 return devino; | |
782 } | |
783 | |
784 static guint | |
785 devino_hash(gconstpointer key) | |
786 { | |
787 const DeviceInode *d = key; | |
788 return d->ino; | |
789 } | |
790 | |
791 static gint | |
792 devino_compare(gconstpointer a, | |
793 gconstpointer b) | |
794 { | |
795 const DeviceInode *da = a, *db = b; | |
796 return (da->dev == db->dev && da->ino == db->ino); | |
797 } | |
798 | |
799 static gboolean | |
800 devino_destroy(gpointer key, | |
801 gpointer value, | |
802 gpointer data) | |
803 { | |
804 g_free(key); | |
805 return TRUE; | |
806 } | |
807 | |
808 static gboolean | |
809 file_is_hidden(const gchar * filename) | |
810 { | |
811 g_return_val_if_fail(filename != NULL, FALSE); | |
2570 | 812 return (g_basename(filename)[0] == '.'); |
2313 | 813 } |
814 | |
815 static GList * | |
816 playlist_dir_find_files(const gchar * path, | |
817 gboolean background, | |
818 GHashTable * htab) | |
819 { | |
820 GDir *dir; | |
821 GList *list = NULL, *ilist; | |
822 const gchar *dir_entry; | |
823 | |
824 struct stat statbuf; | |
825 DeviceInode *devino; | |
826 | |
827 if (!g_file_test(path, G_FILE_TEST_IS_DIR)) | |
828 return NULL; | |
829 | |
830 stat(path, &statbuf); | |
831 devino = devino_new(statbuf.st_dev, statbuf.st_ino); | |
832 | |
833 if (g_hash_table_lookup(htab, devino)) { | |
834 g_free(devino); | |
835 return NULL; | |
836 } | |
837 | |
838 g_hash_table_insert(htab, devino, GINT_TO_POINTER(1)); | |
839 | |
840 if ((ilist = input_scan_dir(path))) { | |
841 GList *node; | |
842 for (node = ilist; node; node = g_list_next(node)) { | |
843 gchar *name = g_build_filename(path, node->data, NULL); | |
844 list = g_list_prepend(list, name); | |
845 g_free(node->data); | |
846 } | |
847 g_list_free(ilist); | |
848 return list; | |
849 } | |
850 | |
851 if (!(dir = g_dir_open(path, 0, NULL))) | |
852 return NULL; | |
853 | |
854 while ((dir_entry = g_dir_read_name(dir))) { | |
855 gchar *filename; | |
856 | |
857 if (file_is_hidden(dir_entry)) | |
858 continue; | |
859 | |
860 filename = g_build_filename(path, dir_entry, NULL); | |
861 | |
862 if (g_file_test(filename, G_FILE_TEST_IS_DIR)) { | |
863 GList *sub; | |
864 sub = playlist_dir_find_files(filename, background, htab); | |
865 g_free(filename); | |
866 list = g_list_concat(list, sub); | |
867 } | |
868 else if (cfg.playlist_detect == TRUE) | |
869 list = g_list_prepend(list, filename); | |
870 else if (input_check_file(filename, TRUE)) | |
871 list = g_list_prepend(list, filename); | |
872 else | |
873 g_free(filename); | |
874 | |
875 while (background && gtk_events_pending()) | |
876 gtk_main_iteration(); | |
877 } | |
878 g_dir_close(dir); | |
879 | |
880 return list; | |
881 } | |
882 | |
883 gboolean | |
884 playlist_add(Playlist * playlist, const gchar * filename) | |
885 { | |
886 return playlist_ins(playlist, filename, -1); | |
887 } | |
888 | |
889 guint | |
890 playlist_add_dir(Playlist * playlist, const gchar * directory) | |
891 { | |
892 return playlist_ins_dir(playlist, directory, -1, TRUE); | |
893 } | |
894 | |
895 guint | |
896 playlist_add_url(Playlist * playlist, const gchar * url) | |
897 { | |
898 return playlist_ins_url(playlist, url, -1); | |
899 } | |
900 | |
2579 | 901 static gchar * |
902 _playlist_urldecode_basic_path(const gchar * encoded_path) | |
903 { | |
904 const gchar *cur, *ext; | |
905 gchar *path, *tmp; | |
906 gint realchar; | |
907 | |
908 if (!encoded_path) | |
909 return NULL; | |
910 | |
911 if (!str_has_prefix_nocase(encoded_path, "file:")) | |
912 return NULL; | |
913 | |
914 cur = encoded_path + 5; | |
915 | |
916 if (str_has_prefix_nocase(cur, "//localhost")) | |
917 cur += 11; | |
918 | |
919 if (*cur == '/') | |
920 while (cur[1] == '/') | |
921 cur++; | |
922 | |
923 tmp = g_malloc0(strlen(cur) + 1); | |
924 | |
925 while ((ext = strchr(cur, '%')) != NULL) { | |
926 strncat(tmp, cur, ext - cur); | |
927 ext++; | |
928 cur = ext + 2; | |
929 if (!sscanf(ext, "%2x", &realchar)) { | |
930 /* Assume it is a literal '%'. Several file | |
931 * managers send unencoded file: urls on drag | |
932 * and drop. */ | |
933 realchar = '%'; | |
934 cur -= 2; | |
935 } | |
936 tmp[strlen(tmp)] = realchar; | |
937 } | |
938 | |
939 path = g_strconcat(tmp, cur, NULL); | |
940 g_free(tmp); | |
941 return path; | |
942 } | |
943 | |
2313 | 944 guint |
945 playlist_ins_dir(Playlist * playlist, const gchar * path, | |
946 gint pos, | |
947 gboolean background) | |
948 { | |
949 guint entries = 0; | |
950 GList *list, *node; | |
951 GHashTable *htab; | |
2579 | 952 gchar *path2 = _playlist_urldecode_basic_path(path); |
2313 | 953 |
2580
48288757d7c7
[svn] - fix a regression introduced by the DnD fix.
nenolod
parents:
2579
diff
changeset
|
954 if (path2 == NULL) |
48288757d7c7
[svn] - fix a regression introduced by the DnD fix.
nenolod
parents:
2579
diff
changeset
|
955 path2 = g_strdup(path); |
48288757d7c7
[svn] - fix a regression introduced by the DnD fix.
nenolod
parents:
2579
diff
changeset
|
956 |
2313 | 957 htab = g_hash_table_new(devino_hash, devino_compare); |
958 | |
2579 | 959 list = playlist_dir_find_files(path2, background, htab); |
2313 | 960 list = g_list_sort(list, (GCompareFunc) path_compare); |
961 | |
962 g_hash_table_foreach_remove(htab, devino_destroy, NULL); | |
963 | |
964 for (node = list; node; node = g_list_next(node)) { | |
965 __playlist_ins(playlist, node->data, pos, NULL); | |
966 g_free(node->data); | |
967 entries++; | |
968 if (pos >= 0) | |
969 pos++; | |
970 } | |
971 | |
972 g_list_free(list); | |
2579 | 973 g_free(path2); |
2313 | 974 |
975 playlist_recalc_total_time(playlist); | |
976 playlist_generate_shuffle_list(playlist); | |
977 playlistwin_update_list(playlist); | |
978 playlist_manager_update(); | |
979 return entries; | |
980 } | |
981 | |
982 guint | |
983 playlist_ins_url(Playlist * playlist, const gchar * string, | |
984 gint pos) | |
985 { | |
986 gchar *tmp; | |
987 gint i = 1, entries = 0; | |
988 gboolean first = TRUE; | |
989 guint firstpos = 0; | |
990 gboolean success = FALSE; | |
991 gchar *decoded = NULL; | |
992 | |
993 g_return_val_if_fail(playlist != NULL, 0); | |
994 g_return_val_if_fail(string != NULL, 0); | |
995 | |
996 playlistwin_update_list(playlist); | |
997 | |
998 while (*string) { | |
999 GList *node; | |
1000 tmp = strchr(string, '\n'); | |
1001 if (tmp) { | |
1002 if (*(tmp - 1) == '\r') | |
1003 *(tmp - 1) = '\0'; | |
1004 *tmp = '\0'; | |
1005 } | |
1006 | |
1007 decoded = g_strdup(string); | |
1008 | |
2575 | 1009 if (vfs_file_test(decoded, G_FILE_TEST_IS_DIR)) { |
2313 | 1010 i = playlist_ins_dir(playlist, decoded, pos, FALSE); |
1011 } | |
1012 else { | |
1013 if (is_playlist_name(decoded)) { | |
1014 i = playlist_load_ins(playlist, decoded, pos); | |
1015 } | |
1016 else { | |
1017 success = playlist_ins(playlist, decoded, pos); | |
1018 i = 1; | |
1019 } | |
1020 } | |
1021 | |
1022 g_free(decoded); | |
1023 | |
1024 PLAYLIST_LOCK(playlist->mutex); | |
1025 node = g_list_nth(playlist->entries, pos); | |
1026 PLAYLIST_UNLOCK(playlist->mutex); | |
1027 | |
1028 entries += i; | |
1029 | |
1030 if (first) { | |
1031 first = FALSE; | |
1032 firstpos = pos; | |
1033 } | |
1034 | |
1035 if (pos >= 0) | |
1036 pos += i; | |
1037 if (!tmp) | |
1038 break; | |
1039 | |
1040 string = tmp + 1; | |
1041 } | |
1042 | |
1043 playlist_recalc_total_time(playlist); | |
1044 playlist_generate_shuffle_list(playlist); | |
1045 playlistwin_update_list(playlist); | |
1046 | |
1047 playlist_manager_update(); | |
1048 | |
1049 return entries; | |
1050 } | |
1051 | |
1052 void | |
1053 playlist_set_info_old_abi(const gchar * title, gint length, gint rate, | |
1054 gint freq, gint nch) | |
1055 { | |
1056 Playlist *playlist = playlist_get_active(); | |
1057 | |
1058 g_return_if_fail(playlist != NULL); | |
1059 | |
1060 if (playlist->position) { | |
1061 g_free(playlist->position->title); | |
1062 playlist->position->title = g_strdup(title); | |
1063 playlist->position->length = length; | |
2488
c5075a79f1aa
[svn] make input->set_info overwrite tuple->track_name. it allows input
yaz
parents:
2480
diff
changeset
|
1064 |
c5075a79f1aa
[svn] make input->set_info overwrite tuple->track_name. it allows input
yaz
parents:
2480
diff
changeset
|
1065 // overwrite tuple->track_name, mainly for streaming. it may incur side effects. --yaz |
2489
e56a55d07935
[svn] - revise r3986 to only override tuple->track_name on lengthless streams
nenolod
parents:
2488
diff
changeset
|
1066 if(playlist->position->tuple && length == -1){ |
2488
c5075a79f1aa
[svn] make input->set_info overwrite tuple->track_name. it allows input
yaz
parents:
2480
diff
changeset
|
1067 if(playlist->position->tuple->track_name){ |
c5075a79f1aa
[svn] make input->set_info overwrite tuple->track_name. it allows input
yaz
parents:
2480
diff
changeset
|
1068 g_free(playlist->position->tuple->track_name); |
c5075a79f1aa
[svn] make input->set_info overwrite tuple->track_name. it allows input
yaz
parents:
2480
diff
changeset
|
1069 } |
c5075a79f1aa
[svn] make input->set_info overwrite tuple->track_name. it allows input
yaz
parents:
2480
diff
changeset
|
1070 playlist->position->tuple->track_name = g_strdup(title); |
c5075a79f1aa
[svn] make input->set_info overwrite tuple->track_name. it allows input
yaz
parents:
2480
diff
changeset
|
1071 } |
2313 | 1072 } |
1073 | |
1074 playlist_recalc_total_time(playlist); | |
1075 | |
1076 mainwin_set_song_info(rate, freq, nch); | |
2460 | 1077 |
1078 if ( playlist->position ) | |
1079 hook_call( "playlist set info" , playlist->position ); | |
2313 | 1080 } |
1081 | |
1082 void | |
1083 playlist_set_info(Playlist * playlist, const gchar * title, gint length, gint rate, | |
1084 gint freq, gint nch) | |
1085 { | |
1086 g_return_if_fail(playlist != NULL); | |
1087 | |
1088 if (playlist->position) { | |
1089 g_free(playlist->position->title); | |
1090 playlist->position->title = g_strdup(title); | |
1091 playlist->position->length = length; | |
1092 } | |
1093 | |
1094 playlist_recalc_total_time(playlist); | |
1095 | |
1096 mainwin_set_song_info(rate, freq, nch); | |
2460 | 1097 |
1098 if ( playlist->position ) | |
1099 hook_call( "playlist set info" , playlist->position ); | |
2313 | 1100 } |
1101 | |
1102 void | |
1103 playlist_check_pos_current(Playlist *playlist) | |
1104 { | |
1105 gint pos, row, bottom; | |
1106 | |
1107 if (!playlist) | |
1108 return; | |
1109 | |
1110 PLAYLIST_LOCK(playlist->mutex); | |
1111 if (!playlist->position || !playlistwin_list) { | |
1112 PLAYLIST_UNLOCK(playlist->mutex); | |
1113 return; | |
1114 } | |
1115 | |
1116 pos = g_list_index(playlist->entries, playlist->position); | |
1117 | |
1118 if (playlistwin_item_visible(pos)) { | |
1119 PLAYLIST_UNLOCK(playlist->mutex); | |
1120 return; | |
1121 } | |
1122 | |
2671
e9b379528fbc
[svn] - playlist_get_length_nolock() -> playlist_get_length(), no need for
nenolod
parents:
2670
diff
changeset
|
1123 bottom = MAX(0, playlist_get_length(playlist) - |
2313 | 1124 playlistwin_list->pl_num_visible); |
1125 row = CLAMP(pos - playlistwin_list->pl_num_visible / 2, 0, bottom); | |
1126 PLAYLIST_UNLOCK(playlist->mutex); | |
1127 playlistwin_set_toprow(row); | |
1128 g_cond_signal(cond_scan); | |
1129 } | |
1130 | |
1131 void | |
1132 playlist_next(Playlist *playlist) | |
1133 { | |
1134 GList *plist_pos_list; | |
1135 gboolean restart_playing = FALSE; | |
2480
bb2f191895ce
[svn] - fixed crash when skipping to next song with an empty playlist
marvin
parents:
2460
diff
changeset
|
1136 if (!playlist_get_length(playlist)) |
2313 | 1137 return; |
1138 | |
1139 PLAYLIST_LOCK(playlist->mutex); | |
1140 | |
1141 if ((playlist_position_before_jump != NULL) && playlist->queue == NULL) | |
1142 { | |
1143 playlist->position = playlist_position_before_jump; | |
1144 playlist_position_before_jump = NULL; | |
1145 } | |
1146 | |
1147 plist_pos_list = find_playlist_position_list(playlist); | |
1148 | |
1149 if (!cfg.repeat && !g_list_next(plist_pos_list) && playlist->queue == NULL) { | |
1150 PLAYLIST_UNLOCK(playlist->mutex); | |
1151 return; | |
1152 } | |
1153 | |
1154 if (playback_get_playing()) { | |
1155 /* We need to stop before changing playlist_position */ | |
1156 PLAYLIST_UNLOCK(playlist->mutex); | |
1157 ip_data.stop = TRUE; | |
1158 playback_stop(); | |
1159 ip_data.stop = FALSE; | |
1160 PLAYLIST_LOCK(playlist->mutex); | |
1161 restart_playing = TRUE; | |
1162 } | |
1163 | |
1164 plist_pos_list = find_playlist_position_list(playlist); | |
1165 if (playlist->queue != NULL) | |
1166 play_queued(playlist); | |
1167 else if (g_list_next(plist_pos_list)) | |
1168 playlist->position = g_list_next(plist_pos_list)->data; | |
1169 else if (cfg.repeat) { | |
1170 playlist->position = NULL; | |
1171 playlist_generate_shuffle_list_nolock(playlist); | |
1172 if (cfg.shuffle) | |
1173 playlist->position = playlist->shuffle->data; | |
1174 else | |
1175 playlist->position = playlist->entries->data; | |
1176 } | |
1177 PLAYLIST_UNLOCK(playlist->mutex); | |
1178 playlist_check_pos_current(playlist); | |
1179 | |
1180 if (restart_playing) | |
1181 playback_initiate(); | |
1182 else { | |
1183 mainwin_set_info_text(); | |
1184 playlistwin_update_list(playlist); | |
1185 } | |
1186 } | |
1187 | |
1188 void | |
1189 playlist_prev(Playlist *playlist) | |
1190 { | |
1191 GList *plist_pos_list; | |
1192 gboolean restart_playing = FALSE; | |
1193 | |
2480
bb2f191895ce
[svn] - fixed crash when skipping to next song with an empty playlist
marvin
parents:
2460
diff
changeset
|
1194 if (!playlist_get_length(playlist)) |
2313 | 1195 return; |
1196 | |
1197 PLAYLIST_LOCK(playlist->mutex); | |
1198 | |
1199 if ((playlist_position_before_jump != NULL) && playlist->queue == NULL) | |
1200 { | |
1201 playlist->position = playlist_position_before_jump; | |
1202 playlist_position_before_jump = NULL; | |
1203 } | |
1204 | |
1205 plist_pos_list = find_playlist_position_list(playlist); | |
1206 | |
1207 if (!cfg.repeat && !g_list_previous(plist_pos_list)) { | |
1208 PLAYLIST_UNLOCK(playlist->mutex); | |
1209 return; | |
1210 } | |
1211 | |
1212 if (playback_get_playing()) { | |
1213 /* We need to stop before changing playlist_position */ | |
1214 PLAYLIST_UNLOCK(playlist->mutex); | |
1215 ip_data.stop = TRUE; | |
1216 playback_stop(); | |
1217 ip_data.stop = FALSE; | |
1218 PLAYLIST_LOCK(playlist->mutex); | |
1219 restart_playing = TRUE; | |
1220 } | |
1221 | |
1222 plist_pos_list = find_playlist_position_list(playlist); | |
1223 if (g_list_previous(plist_pos_list)) { | |
1224 playlist->position = g_list_previous(plist_pos_list)->data; | |
1225 } | |
1226 else if (cfg.repeat) { | |
1227 GList *node; | |
1228 playlist->position = NULL; | |
1229 playlist_generate_shuffle_list_nolock(playlist); | |
1230 if (cfg.shuffle) | |
1231 node = g_list_last(playlist->shuffle); | |
1232 else | |
1233 node = g_list_last(playlist->entries); | |
1234 if (node) | |
1235 playlist->position = node->data; | |
1236 } | |
1237 | |
1238 PLAYLIST_UNLOCK(playlist->mutex); | |
1239 | |
1240 playlist_check_pos_current(playlist); | |
1241 | |
1242 if (restart_playing) | |
1243 playback_initiate(); | |
1244 else { | |
1245 mainwin_set_info_text(); | |
1246 playlistwin_update_list(playlist); | |
1247 } | |
1248 } | |
1249 | |
1250 void | |
1251 playlist_queue(Playlist *playlist) | |
1252 { | |
1253 GList *list = playlist_get_selected(playlist); | |
1254 GList *it = list; | |
1255 | |
1256 PLAYLIST_LOCK(playlist->mutex); | |
1257 | |
1258 if ((cfg.shuffle) && (playlist_position_before_jump == NULL)) | |
1259 { | |
1260 /* Shuffling and this is our first manual jump. */ | |
1261 playlist_position_before_jump = playlist->position; | |
1262 } | |
1263 | |
1264 while (it) { | |
1265 GList *next = g_list_next(it); | |
1266 GList *tmp; | |
1267 | |
1268 /* XXX: WTF? --nenolod */ | |
1269 it->data = g_list_nth_data(playlist->entries, GPOINTER_TO_INT(it->data)); | |
1270 if ((tmp = g_list_find(playlist->queue, it->data))) { | |
1271 playlist->queue = g_list_remove_link(playlist->queue, tmp); | |
1272 g_list_free_1(tmp); | |
1273 list = g_list_remove_link(list, it); | |
1274 g_list_free_1(it); | |
1275 } | |
1276 | |
1277 it = next; | |
1278 } | |
1279 | |
1280 playlist->queue = g_list_concat(playlist->queue, list); | |
1281 | |
1282 PLAYLIST_UNLOCK(playlist->mutex); | |
1283 | |
1284 playlist_recalc_total_time(playlist); | |
1285 playlistwin_update_list(playlist); | |
1286 } | |
1287 | |
1288 void | |
1289 playlist_queue_position(Playlist *playlist, guint pos) | |
1290 { | |
1291 GList *tmp; | |
1292 PlaylistEntry *entry; | |
1293 | |
1294 PLAYLIST_LOCK(playlist->mutex); | |
1295 | |
1296 if ((cfg.shuffle) && (playlist_position_before_jump == NULL)) | |
1297 { | |
1298 /* Shuffling and this is our first manual jump. */ | |
1299 playlist_position_before_jump = playlist->position; | |
1300 } | |
1301 | |
1302 entry = g_list_nth_data(playlist->entries, pos); | |
1303 if ((tmp = g_list_find(playlist->queue, entry))) { | |
1304 playlist->queue = g_list_remove_link(playlist->queue, tmp); | |
1305 g_list_free_1(tmp); | |
1306 } | |
1307 else | |
1308 playlist->queue = g_list_append(playlist->queue, entry); | |
1309 PLAYLIST_UNLOCK(playlist->mutex); | |
1310 | |
1311 playlist_recalc_total_time(playlist); | |
1312 playlistwin_update_list(playlist); | |
1313 } | |
1314 | |
1315 gboolean | |
1316 playlist_is_position_queued(Playlist *playlist, guint pos) | |
1317 { | |
1318 PlaylistEntry *entry; | |
1319 GList *tmp; | |
1320 | |
1321 PLAYLIST_LOCK(playlist->mutex); | |
1322 entry = g_list_nth_data(playlist->entries, pos); | |
1323 tmp = g_list_find(playlist->queue, entry); | |
1324 PLAYLIST_UNLOCK(playlist->mutex); | |
1325 | |
1326 return tmp != NULL; | |
1327 } | |
1328 | |
1329 gint | |
1330 playlist_get_queue_position_number(Playlist *playlist, guint pos) | |
1331 { | |
1332 PlaylistEntry *entry; | |
1333 gint tmp; | |
1334 | |
1335 PLAYLIST_LOCK(playlist->mutex); | |
1336 entry = g_list_nth_data(playlist->entries, pos); | |
1337 tmp = g_list_index(playlist->queue, entry); | |
1338 PLAYLIST_UNLOCK(playlist->mutex); | |
1339 | |
1340 return tmp; | |
1341 } | |
1342 | |
1343 gint | |
1344 playlist_get_queue_qposition_number(Playlist *playlist, guint pos) | |
1345 { | |
1346 PlaylistEntry *entry; | |
1347 gint tmp; | |
1348 | |
1349 PLAYLIST_LOCK(playlist->mutex); | |
1350 entry = g_list_nth_data(playlist->queue, pos); | |
1351 tmp = g_list_index(playlist->entries, entry); | |
1352 PLAYLIST_UNLOCK(playlist->mutex); | |
1353 | |
1354 return tmp; | |
1355 } | |
1356 | |
1357 void | |
1358 playlist_clear_queue(Playlist *playlist) | |
1359 { | |
1360 PLAYLIST_LOCK(playlist->mutex); | |
1361 g_list_free(playlist->queue); | |
1362 playlist->queue = NULL; | |
1363 PLAYLIST_UNLOCK(playlist->mutex); | |
1364 | |
1365 playlist_recalc_total_time(playlist); | |
1366 playlistwin_update_list(playlist); | |
1367 } | |
1368 | |
1369 void | |
1370 playlist_queue_remove(Playlist *playlist, guint pos) | |
1371 { | |
1372 void *entry; | |
1373 | |
1374 PLAYLIST_LOCK(playlist->mutex); | |
1375 entry = g_list_nth_data(playlist->entries, pos); | |
1376 playlist->queue = g_list_remove(playlist->queue, entry); | |
1377 PLAYLIST_UNLOCK(playlist->mutex); | |
1378 | |
1379 playlistwin_update_list(playlist); | |
1380 } | |
1381 | |
1382 gint | |
1383 playlist_get_queue_position(Playlist *playlist, PlaylistEntry * entry) | |
1384 { | |
1385 return g_list_index(playlist->queue, entry); | |
1386 } | |
1387 | |
1388 void | |
1389 playlist_set_position(Playlist *playlist, guint pos) | |
1390 { | |
1391 GList *node; | |
1392 gboolean restart_playing = FALSE; | |
1393 | |
1394 if (!playlist) | |
1395 return; | |
1396 | |
1397 PLAYLIST_LOCK(playlist->mutex); | |
1398 | |
1399 node = g_list_nth(playlist->entries, pos); | |
1400 if (!node) { | |
1401 PLAYLIST_UNLOCK(playlist->mutex); | |
1402 return; | |
1403 } | |
1404 | |
1405 if (playback_get_playing()) { | |
1406 /* We need to stop before changing playlist_position */ | |
1407 PLAYLIST_UNLOCK(playlist->mutex); | |
1408 ip_data.stop = TRUE; | |
1409 playback_stop(); | |
1410 ip_data.stop = FALSE; | |
1411 PLAYLIST_LOCK(playlist->mutex); | |
1412 restart_playing = TRUE; | |
1413 } | |
1414 | |
1415 if ((cfg.shuffle) && (playlist_position_before_jump == NULL)) | |
1416 { | |
1417 /* Shuffling and this is our first manual jump. */ | |
1418 playlist_position_before_jump = playlist->position; | |
1419 } | |
1420 | |
1421 playlist->position = node->data; | |
1422 PLAYLIST_UNLOCK(playlist->mutex); | |
1423 playlist_check_pos_current(playlist); | |
1424 | |
1425 if (restart_playing) | |
1426 playback_initiate(); | |
1427 else { | |
1428 mainwin_set_info_text(); | |
1429 playlistwin_update_list(playlist); | |
1430 } | |
1431 } | |
1432 | |
1433 void | |
1434 playlist_eof_reached(Playlist *playlist) | |
1435 { | |
1436 GList *plist_pos_list; | |
1437 | |
1438 if ((cfg.no_playlist_advance && !cfg.repeat) || cfg.stopaftersong) | |
1439 ip_data.stop = TRUE; | |
1440 playback_stop(); | |
1441 if ((cfg.no_playlist_advance && !cfg.repeat) || cfg.stopaftersong) | |
1442 ip_data.stop = FALSE; | |
1443 | |
2408
b380e84148bb
[svn] - rename some hooks so that they are more logical
nenolod
parents:
2407
diff
changeset
|
1444 hook_call("playback end", playlist->position); |
2407
1dc1d36d0347
[svn] - add hooks: playback begin, playback end, playlist reached end
nenolod
parents:
2380
diff
changeset
|
1445 |
2313 | 1446 PLAYLIST_LOCK(playlist->mutex); |
1447 | |
1448 if ((playlist_position_before_jump != NULL) && playlist->queue == NULL) | |
1449 { | |
1450 playlist->position = playlist_position_before_jump; | |
1451 playlist_position_before_jump = NULL; | |
1452 } | |
1453 | |
1454 plist_pos_list = find_playlist_position_list(playlist); | |
1455 | |
1456 if (cfg.no_playlist_advance) { | |
1457 PLAYLIST_UNLOCK(playlist->mutex); | |
1458 mainwin_clear_song_info(); | |
1459 if (cfg.repeat) | |
1460 playback_initiate(); | |
1461 return; | |
1462 } | |
1463 | |
1464 if (cfg.stopaftersong) { | |
1465 PLAYLIST_UNLOCK(playlist->mutex); | |
1466 mainwin_clear_song_info(); | |
1467 mainwin_set_stopaftersong(FALSE); | |
1468 return; | |
1469 } | |
1470 | |
1471 if (playlist->queue != NULL) { | |
1472 play_queued(playlist); | |
1473 } | |
1474 else if (!g_list_next(plist_pos_list)) { | |
1475 if (cfg.shuffle) { | |
1476 playlist->position = NULL; | |
1477 playlist_generate_shuffle_list_nolock(playlist); | |
1478 } | |
2380 | 1479 else if (playlist->entries != NULL) |
2313 | 1480 playlist->position = playlist->entries->data; |
1481 | |
1482 if (!cfg.repeat) { | |
1483 PLAYLIST_UNLOCK(playlist->mutex); | |
2408
b380e84148bb
[svn] - rename some hooks so that they are more logical
nenolod
parents:
2407
diff
changeset
|
1484 hook_call("playlist end reached", playlist->position); |
2313 | 1485 mainwin_clear_song_info(); |
1486 mainwin_set_info_text(); | |
1487 return; | |
1488 } | |
1489 } | |
1490 else | |
1491 playlist->position = g_list_next(plist_pos_list)->data; | |
1492 | |
1493 PLAYLIST_UNLOCK(playlist->mutex); | |
1494 | |
1495 playlist_check_pos_current(playlist); | |
1496 playback_initiate(); | |
1497 mainwin_set_info_text(); | |
1498 playlistwin_update_list(playlist); | |
1499 } | |
1500 | |
1501 gint | |
1502 playlist_queue_get_length(Playlist *playlist) | |
1503 { | |
1504 gint length; | |
1505 | |
1506 PLAYLIST_LOCK(playlist->mutex); | |
1507 length = g_list_length(playlist->queue); | |
1508 PLAYLIST_UNLOCK(playlist->mutex); | |
1509 | |
1510 return length; | |
1511 } | |
1512 | |
1513 gint | |
2671
e9b379528fbc
[svn] - playlist_get_length_nolock() -> playlist_get_length(), no need for
nenolod
parents:
2670
diff
changeset
|
1514 playlist_get_length(Playlist *playlist) |
2313 | 1515 { |
1516 return g_list_length(playlist->entries); | |
1517 } | |
1518 | |
1519 gchar * | |
1520 playlist_get_info_text(Playlist *playlist) | |
1521 { | |
1522 gchar *text, *title, *numbers, *length; | |
1523 | |
1524 g_return_val_if_fail(playlist != NULL, NULL); | |
1525 | |
1526 PLAYLIST_LOCK(playlist->mutex); | |
1527 if (!playlist->position) { | |
1528 PLAYLIST_UNLOCK(playlist->mutex); | |
1529 return NULL; | |
1530 } | |
1531 | |
1532 /* FIXME: there should not be a need to do additional conversion, | |
1533 * if playlist is properly maintained */ | |
1534 if (playlist->position->title) { | |
1535 title = str_to_utf8(playlist->position->title); | |
1536 } | |
1537 else { | |
1538 gchar *basename = g_path_get_basename(playlist->position->filename); | |
1539 title = filename_to_utf8(basename); | |
1540 g_free(basename); | |
1541 } | |
1542 | |
1543 /* | |
1544 * If the user don't want numbers in the playlist, don't | |
1545 * display them in other parts of XMMS | |
1546 */ | |
1547 | |
1548 if (cfg.show_numbers_in_pl) | |
1549 numbers = g_strdup_printf("%d. ", playlist_get_position_nolock(playlist) + 1); | |
1550 else | |
1551 numbers = g_strdup(""); | |
1552 | |
1553 if (playlist->position->length != -1) | |
1554 length = g_strdup_printf(" (%d:%-2.2d)", | |
1555 playlist->position->length / 60000, | |
1556 (playlist->position->length / 1000) % 60); | |
1557 else | |
1558 length = g_strdup(""); | |
1559 | |
1560 PLAYLIST_UNLOCK(playlist->mutex); | |
1561 | |
1562 text = convert_title_text(g_strconcat(numbers, title, length, NULL)); | |
1563 | |
1564 g_free(numbers); | |
1565 g_free(title); | |
1566 g_free(length); | |
1567 | |
1568 return text; | |
1569 } | |
1570 | |
1571 gint | |
1572 playlist_get_current_length(Playlist * playlist) | |
1573 { | |
1574 gint len = 0; | |
1575 | |
1576 if (!playlist) | |
1577 return 0; | |
1578 | |
1579 if (playlist->position) | |
1580 len = playlist->position->length; | |
1581 | |
1582 return len; | |
1583 } | |
1584 | |
1585 gboolean | |
1586 playlist_save(Playlist * playlist, const gchar * filename) | |
1587 { | |
1588 PlaylistContainer *plc = NULL; | |
1589 gchar *ext; | |
1590 | |
1591 g_return_val_if_fail(playlist != NULL, FALSE); | |
1592 g_return_val_if_fail(filename != NULL, FALSE); | |
1593 | |
1594 ext = strrchr(filename, '.') + 1; | |
1595 | |
1596 playlist_set_current_name(playlist, filename); | |
1597 | |
1598 if ((plc = playlist_container_find(ext)) == NULL) | |
1599 return FALSE; | |
1600 | |
1601 if (plc->plc_write == NULL) | |
1602 return FALSE; | |
1603 | |
1604 plc->plc_write(filename, 0); | |
1605 | |
1606 return TRUE; | |
1607 } | |
1608 | |
1609 gboolean | |
1610 playlist_load(Playlist * playlist, const gchar * filename) | |
1611 { | |
1612 gboolean ret = FALSE; | |
1613 g_return_val_if_fail(playlist != NULL, FALSE); | |
1614 | |
1615 playlist->loading_playlist = TRUE; | |
1616 ret = playlist_load_ins(playlist, filename, -1); | |
1617 playlist->loading_playlist = FALSE; | |
1618 | |
1619 return ret; | |
1620 } | |
1621 | |
1622 void | |
1623 playlist_load_ins_file(Playlist *playlist, | |
1624 const gchar * filename_p, | |
1625 const gchar * playlist_name, gint pos, | |
1626 const gchar * title, gint len) | |
1627 { | |
1628 gchar *filename; | |
1629 gchar *tmp, *path; | |
2667
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1630 InputPlugin *dec = NULL; /* for decoder cache */ |
2313 | 1631 |
1632 g_return_if_fail(filename_p != NULL); | |
1633 g_return_if_fail(playlist != NULL); | |
1634 g_return_if_fail(playlist_name != NULL); | |
1635 | |
1636 filename = g_strchug(g_strdup(filename_p)); | |
1637 | |
1638 if(cfg.convert_slash) | |
1639 while ((tmp = strchr(filename, '\\')) != NULL) | |
1640 *tmp = '/'; | |
1641 | |
1642 if (filename[0] != '/' && !strstr(filename, "://")) { | |
1643 path = g_strdup(playlist_name); | |
1644 if ((tmp = strrchr(path, '/'))) | |
1645 *tmp = '\0'; | |
1646 else { | |
2667
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1647 if ((playlist->loading_playlist == TRUE || |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1648 cfg.playlist_detect == TRUE)) |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1649 dec = NULL; |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1650 else if (!str_has_prefix_nocase(filename, "http://") && |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1651 !str_has_prefix_nocase(filename, "https://")) |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1652 dec = input_check_file(filename, FALSE); |
2313 | 1653 |
1654 __playlist_ins_with_info(playlist, filename, pos, title, len, dec); | |
1655 return; | |
1656 } | |
1657 tmp = g_build_filename(path, filename, NULL); | |
1658 | |
2668
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
1659 if (playlist->loading_playlist == TRUE && cfg.playlist_detect == TRUE) |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
1660 dec = NULL; |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
1661 else if (!str_has_prefix_nocase(tmp, "http://") && |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
1662 !str_has_prefix_nocase(tmp, "https://")) |
2313 | 1663 dec = input_check_file(tmp, FALSE); |
1664 | |
1665 __playlist_ins_with_info(playlist, tmp, pos, title, len, dec); | |
1666 g_free(tmp); | |
1667 g_free(path); | |
1668 } | |
1669 else | |
1670 { | |
2667
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1671 if ((playlist->loading_playlist == TRUE || |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1672 cfg.playlist_detect == TRUE)) |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1673 dec = NULL; |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1674 else if (!str_has_prefix_nocase(filename, "http://") && |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1675 !str_has_prefix_nocase(filename, "https://")) |
2313 | 1676 dec = input_check_file(filename, FALSE); |
1677 | |
1678 __playlist_ins_with_info(playlist, filename, pos, title, len, dec); | |
1679 } | |
1680 | |
1681 g_free(filename); | |
1682 } | |
1683 | |
1684 void | |
1685 playlist_load_ins_file_tuple(Playlist * playlist, | |
1686 const gchar * filename_p, | |
1687 const gchar * playlist_name, | |
1688 gint pos, | |
1689 TitleInput *tuple) | |
1690 { | |
1691 gchar *filename; | |
1692 gchar *tmp, *path; | |
2667
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1693 InputPlugin *dec = NULL; /* for decoder cache */ |
2313 | 1694 |
1695 g_return_if_fail(filename_p != NULL); | |
1696 g_return_if_fail(playlist_name != NULL); | |
1697 g_return_if_fail(playlist != NULL); | |
1698 | |
1699 filename = g_strchug(g_strdup(filename_p)); | |
1700 | |
1701 while ((tmp = strchr(filename, '\\')) != NULL) | |
1702 *tmp = '/'; | |
1703 | |
1704 if (filename[0] != '/' && !strstr(filename, "://")) { | |
1705 path = g_strdup(playlist_name); | |
1706 if ((tmp = strrchr(path, '/'))) | |
1707 *tmp = '\0'; | |
1708 else { | |
2667
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1709 if ((playlist->loading_playlist == TRUE || |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1710 cfg.playlist_detect == TRUE)) |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1711 dec = NULL; |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1712 else if (!str_has_prefix_nocase(filename, "http://") && |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1713 !str_has_prefix_nocase(filename, "https://")) |
2313 | 1714 dec = input_check_file(filename, FALSE); |
1715 | |
1716 __playlist_ins_with_info_tuple(playlist, filename, pos, tuple, dec); | |
1717 return; | |
1718 } | |
1719 tmp = g_build_filename(path, filename, NULL); | |
1720 | |
2667
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1721 if ((playlist->loading_playlist == TRUE || |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1722 cfg.playlist_detect == TRUE)) |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1723 dec = NULL; |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1724 else if (!str_has_prefix_nocase(filename, "http://") && |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1725 !str_has_prefix_nocase(filename, "https://")) |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1726 dec = input_check_file(filename, FALSE); |
2313 | 1727 |
1728 __playlist_ins_with_info_tuple(playlist, tmp, pos, tuple, dec); | |
1729 g_free(tmp); | |
1730 g_free(path); | |
1731 } | |
1732 else | |
1733 { | |
2667
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1734 if ((playlist->loading_playlist == TRUE || |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1735 cfg.playlist_detect == TRUE)) |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1736 dec = NULL; |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1737 else if (!str_has_prefix_nocase(filename, "http://") && |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1738 !str_has_prefix_nocase(filename, "https://")) |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1739 dec = input_check_file(filename, FALSE); |
2313 | 1740 |
1741 __playlist_ins_with_info_tuple(playlist, filename, pos, tuple, dec); | |
1742 } | |
1743 | |
1744 g_free(filename); | |
1745 } | |
1746 | |
1747 static guint | |
1748 playlist_load_ins(Playlist * playlist, const gchar * filename, gint pos) | |
1749 { | |
1750 PlaylistContainer *plc; | |
1751 gchar *ext; | |
1752 | |
1753 g_return_val_if_fail(playlist != NULL, 0); | |
1754 g_return_val_if_fail(filename != NULL, 0); | |
1755 | |
1756 ext = strrchr(filename, '.') + 1; | |
1757 plc = playlist_container_find(ext); | |
1758 | |
1759 g_return_val_if_fail(plc != NULL, 0); | |
1760 g_return_val_if_fail(plc->plc_read != NULL, 0); | |
1761 | |
1762 plc->plc_read(filename, pos); | |
1763 | |
1764 playlist_generate_shuffle_list(playlist); | |
1765 playlistwin_update_list(playlist); | |
1766 | |
1767 return 1; | |
1768 } | |
1769 | |
1770 GList * | |
1771 get_playlist_nth(Playlist *playlist, guint nth) | |
1772 { | |
1773 g_warning("deprecated function get_playlist_nth() was called"); | |
1774 REQUIRE_LOCK(playlist->mutex); | |
1775 return g_list_nth(playlist->entries, nth); | |
1776 } | |
1777 | |
1778 gint | |
1779 playlist_get_position_nolock(Playlist *playlist) | |
1780 { | |
1781 if (playlist && playlist->position) | |
1782 return g_list_index(playlist->entries, playlist->position); | |
1783 return 0; | |
1784 } | |
1785 | |
1786 gint | |
1787 playlist_get_position(Playlist *playlist) | |
1788 { | |
1789 gint pos; | |
1790 | |
1791 PLAYLIST_LOCK(playlist->mutex); | |
1792 pos = playlist_get_position_nolock(playlist); | |
1793 PLAYLIST_UNLOCK(playlist->mutex); | |
1794 | |
1795 return pos; | |
1796 } | |
1797 | |
1798 gchar * | |
1799 playlist_get_filename(Playlist *playlist, guint pos) | |
1800 { | |
1801 gchar *filename; | |
1802 PlaylistEntry *entry; | |
1803 GList *node; | |
1804 | |
1805 if (!playlist) | |
1806 return NULL; | |
1807 | |
1808 PLAYLIST_LOCK(playlist->mutex); | |
1809 node = g_list_nth(playlist->entries, pos); | |
1810 if (!node) { | |
1811 PLAYLIST_UNLOCK(playlist->mutex); | |
1812 return NULL; | |
1813 } | |
1814 entry = node->data; | |
1815 | |
1816 filename = g_strdup(entry->filename); | |
1817 PLAYLIST_UNLOCK(playlist->mutex); | |
1818 | |
1819 return filename; | |
1820 } | |
1821 | |
1822 gchar * | |
1823 playlist_get_songtitle(Playlist *playlist, guint pos) | |
1824 { | |
1825 gchar *title = NULL; | |
1826 PlaylistEntry *entry; | |
1827 GList *node; | |
1828 | |
1829 if (!playlist) | |
1830 return NULL; | |
1831 | |
1832 PLAYLIST_LOCK(playlist->mutex); | |
1833 | |
1834 if (!(node = g_list_nth(playlist->entries, pos))) { | |
1835 PLAYLIST_UNLOCK(playlist->mutex); | |
1836 return NULL; | |
1837 } | |
1838 | |
1839 entry = node->data; | |
1840 | |
1841 /* FIXME: simplify this logic */ | |
1842 if ((entry->title == NULL && entry->length == -1) || | |
1843 (entry->tuple && entry->tuple->mtime != 0 && (entry->tuple->mtime == -1 || entry->tuple->mtime != playlist_get_mtime(entry->filename)))) | |
1844 { | |
1845 if (playlist_entry_get_info(entry)) | |
1846 title = entry->title; | |
1847 } | |
1848 else { | |
1849 title = entry->title; | |
1850 } | |
1851 | |
1852 PLAYLIST_UNLOCK(playlist->mutex); | |
1853 | |
1854 if (!title) { | |
1855 title = g_path_get_basename(entry->filename); | |
1856 return str_replace(title, filename_to_utf8(title)); | |
1857 } | |
1858 | |
1859 return str_to_utf8(title); | |
1860 } | |
1861 | |
1862 TitleInput * | |
1863 playlist_get_tuple(Playlist *playlist, guint pos) | |
1864 { | |
1865 PlaylistEntry *entry; | |
1866 TitleInput *tuple = NULL; | |
1867 GList *node; | |
1868 | |
1869 if (!playlist) | |
1870 return NULL; | |
1871 | |
1872 if (!(node = g_list_nth(playlist->entries, pos))) { | |
1873 return NULL; | |
1874 } | |
1875 | |
1876 entry = (PlaylistEntry *) node->data; | |
1877 | |
1878 tuple = entry->tuple; | |
1879 | |
1880 // if no tuple or tuple with old mtime, get new one. | |
1881 if (tuple == NULL || | |
1882 (entry->tuple && entry->tuple->mtime != 0 && (entry->tuple->mtime == -1 || entry->tuple->mtime != playlist_get_mtime(entry->filename)))) | |
1883 { | |
1884 playlist_entry_get_info(entry); | |
1885 tuple = entry->tuple; | |
1886 } | |
1887 | |
1888 return tuple; | |
1889 } | |
1890 | |
1891 gint | |
1892 playlist_get_songtime(Playlist *playlist, guint pos) | |
1893 { | |
1894 gint song_time = -1; | |
1895 PlaylistEntry *entry; | |
1896 GList *node; | |
1897 | |
1898 if (!playlist) | |
1899 return -1; | |
1900 | |
1901 if (!(node = g_list_nth(playlist->entries, pos))) { | |
1902 return -1; | |
1903 } | |
1904 | |
1905 entry = node->data; | |
1906 if (entry->tuple == NULL || | |
1907 (entry->tuple->mtime != 0 && (entry->tuple->mtime == -1 || entry->tuple->mtime != playlist_get_mtime(entry->filename)))) { | |
1908 | |
1909 if (playlist_entry_get_info(entry)) | |
1910 song_time = entry->length; | |
1911 } | |
1912 else { | |
1913 song_time = entry->length; | |
1914 } | |
1915 | |
1916 return song_time; | |
1917 } | |
1918 | |
1919 static gint | |
1920 playlist_compare_track(PlaylistEntry * a, | |
1921 PlaylistEntry * b) | |
1922 { | |
1923 g_return_val_if_fail(a != NULL, 0); | |
1924 g_return_val_if_fail(b != NULL, 0); | |
1925 | |
2614
deb09bfd716b
[svn] - now sort by track number and sort by title work even if on display metadata loading is specified.
yaz
parents:
2580
diff
changeset
|
1926 if(!a->tuple) |
deb09bfd716b
[svn] - now sort by track number and sort by title work even if on display metadata loading is specified.
yaz
parents:
2580
diff
changeset
|
1927 playlist_entry_get_info(a); |
deb09bfd716b
[svn] - now sort by track number and sort by title work even if on display metadata loading is specified.
yaz
parents:
2580
diff
changeset
|
1928 if(!b->tuple) |
deb09bfd716b
[svn] - now sort by track number and sort by title work even if on display metadata loading is specified.
yaz
parents:
2580
diff
changeset
|
1929 playlist_entry_get_info(b); |
deb09bfd716b
[svn] - now sort by track number and sort by title work even if on display metadata loading is specified.
yaz
parents:
2580
diff
changeset
|
1930 |
2313 | 1931 g_return_val_if_fail(a->tuple != NULL, 0); |
1932 g_return_val_if_fail(b->tuple != NULL, 0); | |
1933 | |
2614
deb09bfd716b
[svn] - now sort by track number and sort by title work even if on display metadata loading is specified.
yaz
parents:
2580
diff
changeset
|
1934 return (a->tuple->track_number && b->tuple->track_number ? |
deb09bfd716b
[svn] - now sort by track number and sort by title work even if on display metadata loading is specified.
yaz
parents:
2580
diff
changeset
|
1935 a->tuple->track_number - b->tuple->track_number : 0); |
2313 | 1936 } |
1937 | |
1938 static gint | |
1939 playlist_compare_playlist(PlaylistEntry * a, | |
1940 PlaylistEntry * b) | |
1941 { | |
1942 const gchar *a_title = NULL, *b_title = NULL; | |
1943 | |
1944 g_return_val_if_fail(a != NULL, 0); | |
1945 g_return_val_if_fail(b != NULL, 0); | |
1946 | |
1947 if (a->title != NULL) | |
1948 a_title = a->title; | |
1949 else { | |
1950 if (strrchr(a->filename, '/')) | |
1951 a_title = strrchr(a->filename, '/') + 1; | |
1952 else | |
1953 a_title = a->filename; | |
1954 } | |
1955 | |
1956 if (b->title != NULL) | |
1957 b_title = b->title; | |
1958 else { | |
1959 if (strrchr(a->filename, '/')) | |
1960 b_title = strrchr(b->filename, '/') + 1; | |
1961 else | |
1962 b_title = b->filename; | |
1963 } | |
1964 | |
1965 return strcasecmp(a_title, b_title); | |
1966 } | |
1967 | |
1968 static gint | |
1969 playlist_compare_title(PlaylistEntry * a, | |
1970 PlaylistEntry * b) | |
1971 { | |
1972 const gchar *a_title = NULL, *b_title = NULL; | |
1973 | |
1974 g_return_val_if_fail(a != NULL, 0); | |
1975 g_return_val_if_fail(b != NULL, 0); | |
1976 | |
2614
deb09bfd716b
[svn] - now sort by track number and sort by title work even if on display metadata loading is specified.
yaz
parents:
2580
diff
changeset
|
1977 if(!a->tuple) |
deb09bfd716b
[svn] - now sort by track number and sort by title work even if on display metadata loading is specified.
yaz
parents:
2580
diff
changeset
|
1978 playlist_entry_get_info(a); |
deb09bfd716b
[svn] - now sort by track number and sort by title work even if on display metadata loading is specified.
yaz
parents:
2580
diff
changeset
|
1979 if(!b->tuple) |
deb09bfd716b
[svn] - now sort by track number and sort by title work even if on display metadata loading is specified.
yaz
parents:
2580
diff
changeset
|
1980 playlist_entry_get_info(b); |
deb09bfd716b
[svn] - now sort by track number and sort by title work even if on display metadata loading is specified.
yaz
parents:
2580
diff
changeset
|
1981 |
2313 | 1982 if (a->tuple != NULL && a->tuple->track_name != NULL) |
1983 a_title = a->tuple->track_name; | |
1984 if (b->tuple != NULL && b->tuple->track_name != NULL) | |
1985 b_title = b->tuple->track_name; | |
1986 | |
1987 if (a_title != NULL && b_title != NULL) | |
1988 return strcasecmp(a_title, b_title); | |
1989 | |
1990 if (a->title != NULL) | |
1991 a_title = a->title; | |
1992 else { | |
1993 if (strrchr(a->filename, '/')) | |
1994 a_title = strrchr(a->filename, '/') + 1; | |
1995 else | |
1996 a_title = a->filename; | |
1997 } | |
1998 | |
1999 if (b->title != NULL) | |
2000 b_title = b->title; | |
2001 else { | |
2002 if (strrchr(a->filename, '/')) | |
2003 b_title = strrchr(b->filename, '/') + 1; | |
2004 else | |
2005 b_title = b->filename; | |
2006 } | |
2007 | |
2008 return strcasecmp(a_title, b_title); | |
2009 } | |
2010 | |
2011 static gint | |
2012 playlist_compare_artist(PlaylistEntry * a, | |
2013 PlaylistEntry * b) | |
2014 { | |
2015 const gchar *a_artist = NULL, *b_artist = NULL; | |
2016 | |
2017 g_return_val_if_fail(a != NULL, 0); | |
2018 g_return_val_if_fail(b != NULL, 0); | |
2019 | |
2020 if (a->tuple != NULL) | |
2021 playlist_entry_get_info(a); | |
2022 | |
2023 if (b->tuple != NULL) | |
2024 playlist_entry_get_info(b); | |
2025 | |
2683
d2ffabee3ced
[svn] Modified playlist_compare_artist so that a leading "the" in an artist's
magma
parents:
2671
diff
changeset
|
2026 if (a->tuple != NULL && a->tuple->performer != NULL) { |
2313 | 2027 a_artist = a->tuple->performer; |
2683
d2ffabee3ced
[svn] Modified playlist_compare_artist so that a leading "the" in an artist's
magma
parents:
2671
diff
changeset
|
2028 if (str_has_prefix_nocase(a_artist, "the ")) |
d2ffabee3ced
[svn] Modified playlist_compare_artist so that a leading "the" in an artist's
magma
parents:
2671
diff
changeset
|
2029 a_artist += 4; |
d2ffabee3ced
[svn] Modified playlist_compare_artist so that a leading "the" in an artist's
magma
parents:
2671
diff
changeset
|
2030 } |
d2ffabee3ced
[svn] Modified playlist_compare_artist so that a leading "the" in an artist's
magma
parents:
2671
diff
changeset
|
2031 |
d2ffabee3ced
[svn] Modified playlist_compare_artist so that a leading "the" in an artist's
magma
parents:
2671
diff
changeset
|
2032 if (b->tuple != NULL && b->tuple->performer != NULL) { |
2313 | 2033 b_artist = b->tuple->performer; |
2683
d2ffabee3ced
[svn] Modified playlist_compare_artist so that a leading "the" in an artist's
magma
parents:
2671
diff
changeset
|
2034 if (str_has_prefix_nocase(b_artist, "the ")) |
d2ffabee3ced
[svn] Modified playlist_compare_artist so that a leading "the" in an artist's
magma
parents:
2671
diff
changeset
|
2035 b_artist += 4; |
d2ffabee3ced
[svn] Modified playlist_compare_artist so that a leading "the" in an artist's
magma
parents:
2671
diff
changeset
|
2036 } |
2313 | 2037 |
2038 if (a_artist != NULL && b_artist != NULL) | |
2039 return strcasecmp(a_artist, b_artist); | |
2040 | |
2041 return 0; | |
2042 } | |
2043 | |
2044 static gint | |
2045 playlist_compare_filename(PlaylistEntry * a, | |
2046 PlaylistEntry * b) | |
2047 { | |
2048 gchar *a_filename, *b_filename; | |
2049 | |
2050 g_return_val_if_fail(a != NULL, 0); | |
2051 g_return_val_if_fail(b != NULL, 0); | |
2052 | |
2053 if (strrchr(a->filename, '/')) | |
2054 a_filename = strrchr(a->filename, '/') + 1; | |
2055 else | |
2056 a_filename = a->filename; | |
2057 | |
2058 if (strrchr(b->filename, '/')) | |
2059 b_filename = strrchr(b->filename, '/') + 1; | |
2060 else | |
2061 b_filename = b->filename; | |
2062 | |
2063 | |
2064 return strcasecmp(a_filename, b_filename); | |
2065 } | |
2066 | |
2067 static gint | |
2068 path_compare(const gchar * a, const gchar * b) | |
2069 { | |
2070 gchar *posa, *posb; | |
2071 gint len, ret; | |
2072 | |
2073 posa = strrchr(a, '/'); | |
2074 posb = strrchr(b, '/'); | |
2075 | |
2076 /* | |
2077 * Sort directories before files | |
2078 */ | |
2079 if (posa && posb && (posa - a != posb - b)) { | |
2080 if (posa - a > posb - b) { | |
2081 len = posb - b; | |
2082 ret = -1; | |
2083 } | |
2084 else { | |
2085 len = posa - a; | |
2086 ret = 1; | |
2087 } | |
2088 if (!strncasecmp(a, b, len)) | |
2089 return ret; | |
2090 } | |
2091 return strcasecmp(a, b); | |
2092 } | |
2093 | |
2094 static gint | |
2095 playlist_compare_path(PlaylistEntry * a, | |
2096 PlaylistEntry * b) | |
2097 { | |
2098 return path_compare(a->filename, b->filename); | |
2099 } | |
2100 | |
2101 | |
2102 static time_t | |
2103 playlist_get_mtime(const gchar *filename) | |
2104 { | |
2105 struct stat buf; | |
2106 gint rv; | |
2107 | |
2108 rv = stat(filename, &buf); | |
2109 | |
2110 if (rv == 0) { | |
2111 return buf.st_mtime; | |
2112 } else { | |
2113 return 0; //error | |
2114 } | |
2115 } | |
2116 | |
2117 | |
2118 static gint | |
2119 playlist_compare_date(PlaylistEntry * a, | |
2120 PlaylistEntry * b) | |
2121 { | |
2122 struct stat buf; | |
2123 time_t modtime; | |
2124 | |
2125 gint rv; | |
2126 | |
2127 | |
2128 rv = stat(a->filename, &buf); | |
2129 | |
2130 if (rv == 0) { | |
2131 modtime = buf.st_mtime; | |
2132 rv = stat(b->filename, &buf); | |
2133 | |
2134 if (stat(b->filename, &buf) == 0) { | |
2135 if (buf.st_mtime == modtime) | |
2136 return 0; | |
2137 else | |
2138 return (buf.st_mtime - modtime) > 0 ? -1 : 1; | |
2139 } | |
2140 else | |
2141 return -1; | |
2142 } | |
2143 else if (!lstat(b->filename, &buf)) | |
2144 return 1; | |
2145 else | |
2146 return playlist_compare_filename(a, b); | |
2147 } | |
2148 | |
2149 | |
2150 void | |
2151 playlist_sort(Playlist *playlist, PlaylistSortType type) | |
2152 { | |
2153 playlist_remove_dead_files(playlist); | |
2154 PLAYLIST_LOCK(playlist->mutex); | |
2155 playlist->entries = | |
2156 g_list_sort(playlist->entries, | |
2157 (GCompareFunc) playlist_compare_func_table[type]); | |
2158 PLAYLIST_UNLOCK(playlist->mutex); | |
2159 } | |
2160 | |
2161 static GList * | |
2162 playlist_sort_selected_generic(GList * list, GCompareFunc cmpfunc) | |
2163 { | |
2164 GList *list1, *list2; | |
2165 GList *tmp_list = NULL; | |
2166 GList *index_list = NULL; | |
2167 | |
2168 /* | |
2169 * We take all the selected entries out of the playlist, | |
2170 * sorts them, and then put them back in again. | |
2171 */ | |
2172 | |
2173 list1 = g_list_last(list); | |
2174 | |
2175 while (list1) { | |
2176 list2 = g_list_previous(list1); | |
2177 if (PLAYLIST_ENTRY(list1->data)->selected) { | |
2178 gpointer idx; | |
2179 idx = GINT_TO_POINTER(g_list_position(list, list1)); | |
2180 index_list = g_list_prepend(index_list, idx); | |
2181 list = g_list_remove_link(list, list1); | |
2182 tmp_list = g_list_concat(list1, tmp_list); | |
2183 } | |
2184 list1 = list2; | |
2185 } | |
2186 | |
2187 tmp_list = g_list_sort(tmp_list, cmpfunc); | |
2188 list1 = tmp_list; | |
2189 list2 = index_list; | |
2190 | |
2191 while (list2) { | |
2192 if (!list1) { | |
2193 g_critical(G_STRLOC ": Error during list sorting. " | |
2194 "Possibly dropped some playlist-entries."); | |
2195 break; | |
2196 } | |
2197 | |
2198 list = g_list_insert(list, list1->data, GPOINTER_TO_INT(list2->data)); | |
2199 | |
2200 list2 = g_list_next(list2); | |
2201 list1 = g_list_next(list1); | |
2202 } | |
2203 | |
2204 g_list_free(index_list); | |
2205 g_list_free(tmp_list); | |
2206 | |
2207 return list; | |
2208 } | |
2209 | |
2210 void | |
2211 playlist_sort_selected(Playlist *playlist, PlaylistSortType type) | |
2212 { | |
2213 PLAYLIST_LOCK(playlist->mutex); | |
2214 playlist->entries = playlist_sort_selected_generic(playlist->entries, (GCompareFunc) | |
2215 playlist_compare_func_table | |
2216 [type]); | |
2217 PLAYLIST_UNLOCK(playlist->mutex); | |
2218 } | |
2219 | |
2220 void | |
2221 playlist_reverse(Playlist *playlist) | |
2222 { | |
2223 PLAYLIST_LOCK(playlist->mutex); | |
2224 playlist->entries = g_list_reverse(playlist->entries); | |
2225 PLAYLIST_UNLOCK(playlist->mutex); | |
2226 } | |
2227 | |
2228 static GList * | |
2229 playlist_shuffle_list(Playlist *playlist, GList * list) | |
2230 { | |
2231 /* | |
2232 * Note that this doesn't make a copy of the original list. | |
2233 * The pointer to the original list is not valid after this | |
2234 * fuction is run. | |
2235 */ | |
2236 gint len = g_list_length(list); | |
2237 gint i, j; | |
2238 GList *node, **ptrs; | |
2239 | |
2240 if (!playlist) | |
2241 return NULL; | |
2242 | |
2243 REQUIRE_LOCK(playlist->mutex); | |
2244 | |
2245 if (!len) | |
2246 return NULL; | |
2247 | |
2248 ptrs = g_new(GList *, len); | |
2249 | |
2250 for (node = list, i = 0; i < len; node = g_list_next(node), i++) | |
2251 ptrs[i] = node; | |
2252 | |
2253 j = g_random_int_range(0, len); | |
2254 list = ptrs[j]; | |
2255 ptrs[j]->next = NULL; | |
2256 ptrs[j] = ptrs[0]; | |
2257 | |
2258 for (i = 1; i < len; i++) { | |
2259 j = g_random_int_range(0, len - i); | |
2260 list->prev = ptrs[i + j]; | |
2261 ptrs[i + j]->next = list; | |
2262 list = ptrs[i + j]; | |
2263 ptrs[i + j] = ptrs[i]; | |
2264 } | |
2265 list->prev = NULL; | |
2266 | |
2267 g_free(ptrs); | |
2268 | |
2269 return list; | |
2270 } | |
2271 | |
2272 void | |
2273 playlist_random(Playlist *playlist) | |
2274 { | |
2275 PLAYLIST_LOCK(playlist->mutex); | |
2276 playlist->entries = playlist_shuffle_list(playlist, playlist->entries); | |
2277 PLAYLIST_UNLOCK(playlist->mutex); | |
2278 } | |
2279 | |
2280 GList * | |
2281 playlist_get_selected(Playlist *playlist) | |
2282 { | |
2283 GList *node, *list = NULL; | |
2284 gint i = 0; | |
2285 | |
2286 PLAYLIST_LOCK(playlist->mutex); | |
2287 for (node = playlist->entries; node; node = g_list_next(node), i++) { | |
2288 PlaylistEntry *entry = node->data; | |
2289 if (entry->selected) | |
2290 list = g_list_prepend(list, GINT_TO_POINTER(i)); | |
2291 } | |
2292 PLAYLIST_UNLOCK(playlist->mutex); | |
2293 return g_list_reverse(list); | |
2294 } | |
2295 | |
2296 void | |
2297 playlist_clear_selected(Playlist *playlist) | |
2298 { | |
2299 GList *node = NULL; | |
2300 gint i = 0; | |
2301 | |
2302 PLAYLIST_LOCK(playlist->mutex); | |
2303 for (node = playlist->entries; node; node = g_list_next(node), i++) { | |
2304 PLAYLIST_ENTRY(node->data)->selected = FALSE; | |
2305 } | |
2306 PLAYLIST_UNLOCK(playlist->mutex); | |
2307 playlist_recalc_total_time(playlist); | |
2308 playlist_manager_update(); | |
2309 } | |
2310 | |
2311 gint | |
2312 playlist_get_num_selected(Playlist *playlist) | |
2313 { | |
2314 GList *node; | |
2315 gint num = 0; | |
2316 | |
2317 PLAYLIST_LOCK(playlist->mutex); | |
2318 for (node = playlist->entries; node; node = g_list_next(node)) { | |
2319 PlaylistEntry *entry = node->data; | |
2320 if (entry->selected) | |
2321 num++; | |
2322 } | |
2323 PLAYLIST_UNLOCK(playlist->mutex); | |
2324 return num; | |
2325 } | |
2326 | |
2327 | |
2328 static void | |
2329 playlist_generate_shuffle_list(Playlist *playlist) | |
2330 { | |
2331 PLAYLIST_LOCK(playlist->mutex); | |
2332 playlist_generate_shuffle_list_nolock(playlist); | |
2333 PLAYLIST_UNLOCK(playlist->mutex); | |
2334 } | |
2335 | |
2336 static void | |
2337 playlist_generate_shuffle_list_nolock(Playlist *playlist) | |
2338 { | |
2339 GList *node; | |
2340 gint numsongs; | |
2341 | |
2342 if (!cfg.shuffle || !playlist) | |
2343 return; | |
2344 | |
2345 REQUIRE_LOCK(playlist->mutex); | |
2346 | |
2347 if (playlist->shuffle) { | |
2348 g_list_free(playlist->shuffle); | |
2349 playlist->shuffle = NULL; | |
2350 } | |
2351 | |
2352 playlist->shuffle = playlist_shuffle_list(playlist, g_list_copy(playlist->entries)); | |
2353 numsongs = g_list_length(playlist->shuffle); | |
2354 | |
2355 if (playlist->position) { | |
2356 gint i = g_list_index(playlist->shuffle, playlist->position); | |
2357 node = g_list_nth(playlist->shuffle, i); | |
2358 playlist->shuffle = g_list_remove_link(playlist->shuffle, node); | |
2359 playlist->shuffle = g_list_prepend(playlist->shuffle, node->data); | |
2360 } | |
2361 } | |
2362 | |
2363 void | |
2364 playlist_fileinfo(Playlist *playlist, guint pos) | |
2365 { | |
2366 gchar *path = NULL; | |
2367 GList *node; | |
2368 PlaylistEntry *entry = NULL; | |
2369 TitleInput *tuple = NULL; | |
2370 | |
2371 PLAYLIST_LOCK(playlist->mutex); | |
2372 | |
2373 if ((node = g_list_nth(playlist->entries, pos))) | |
2374 { | |
2375 entry = node->data; | |
2376 tuple = entry->tuple; | |
2377 path = g_strdup(entry->filename); | |
2378 } | |
2379 | |
2380 PLAYLIST_UNLOCK(playlist->mutex); | |
2381 | |
2382 /* No tuple? Try to set this entry up properly. --nenolod */ | |
2383 if (entry->tuple == NULL || entry->tuple->mtime == -1 || | |
2384 entry->tuple->mtime == 0 || entry->tuple->mtime != playlist_get_mtime(entry->filename)) | |
2385 { | |
2386 playlist_entry_get_info(entry); | |
2387 tuple = entry->tuple; | |
2388 } | |
2389 | |
2390 if (tuple != NULL) | |
2391 { | |
2392 if (entry->decoder == NULL) | |
2393 entry->decoder = input_check_file(entry->filename, FALSE); /* try to find a decoder */ | |
2394 | |
2395 if (entry->decoder != NULL && entry->decoder->file_info_box == NULL) | |
2396 fileinfo_show_for_tuple(tuple); | |
2397 else if (entry->decoder != NULL && entry->decoder->file_info_box != NULL) | |
2398 entry->decoder->file_info_box(path); | |
2399 else | |
2400 fileinfo_show_for_path(path); | |
2401 g_free(path); | |
2402 } | |
2403 else if (path != NULL) | |
2404 { | |
2405 if (entry != NULL && entry->decoder != NULL && entry->decoder->file_info_box != NULL) | |
2406 entry->decoder->file_info_box(path); | |
2407 else | |
2408 fileinfo_show_for_path(path); | |
2409 g_free(path); | |
2410 } | |
2411 } | |
2412 | |
2413 void | |
2414 playlist_fileinfo_current(Playlist *playlist) | |
2415 { | |
2416 gchar *path = NULL; | |
2417 TitleInput *tuple = NULL; | |
2418 | |
2419 PLAYLIST_LOCK(playlist->mutex); | |
2420 | |
2421 if (playlist->entries && playlist->position) | |
2422 { | |
2423 path = g_strdup(playlist->position->filename); | |
2424 if (( playlist->position->tuple == NULL ) || ( playlist->position->decoder == NULL )) | |
2425 playlist_entry_get_info(playlist->position); | |
2426 tuple = playlist->position->tuple; | |
2427 } | |
2428 | |
2429 PLAYLIST_UNLOCK(playlist->mutex); | |
2430 | |
2431 if (tuple != NULL) | |
2432 { | |
2433 if (playlist->position->decoder != NULL && playlist->position->decoder->file_info_box == NULL) | |
2434 fileinfo_show_for_tuple(tuple); | |
2435 else if (playlist->position->decoder != NULL && playlist->position->decoder->file_info_box != NULL) | |
2436 playlist->position->decoder->file_info_box(path); | |
2437 else | |
2438 fileinfo_show_for_path(path); | |
2439 g_free(path); | |
2440 } | |
2441 else if (path != NULL) | |
2442 { | |
2443 if (playlist->position != NULL && playlist->position->decoder != NULL && playlist->position->decoder->file_info_box != NULL) | |
2444 playlist->position->decoder->file_info_box(path); | |
2445 else | |
2446 fileinfo_show_for_path(path); | |
2447 g_free(path); | |
2448 } | |
2449 } | |
2450 | |
2451 | |
2452 static gboolean | |
2453 playlist_get_info_is_going(void) | |
2454 { | |
2455 gboolean result; | |
2456 | |
2457 G_LOCK(playlist_get_info_going); | |
2458 result = playlist_get_info_going; | |
2459 G_UNLOCK(playlist_get_info_going); | |
2460 | |
2461 return result; | |
2462 } | |
2463 | |
2464 static gpointer | |
2465 playlist_get_info_func(gpointer arg) | |
2466 { | |
2467 GList *node; | |
2468 gboolean update_playlistwin = FALSE; | |
2469 gboolean update_mainwin = FALSE; | |
2470 | |
2471 while (playlist_get_info_is_going()) { | |
2472 PlaylistEntry *entry; | |
2473 Playlist *playlist = playlist_get_active(); | |
2474 | |
2475 // on_load | |
2476 if (cfg.use_pl_metadata && | |
2477 cfg.get_info_on_load && | |
2478 playlist_get_info_scan_active) { | |
2479 | |
2480 for (node = playlist->entries; node; node = g_list_next(node)) { | |
2481 entry = node->data; | |
2482 | |
2636 | 2483 if(playlist->attribute & PLAYLIST_STATIC || |
2484 (entry->tuple && entry->tuple->length > -1 && entry->tuple->mtime != -1)) { | |
2313 | 2485 update_playlistwin = TRUE; |
2486 continue; | |
2487 } | |
2488 | |
2489 if (!playlist_entry_get_info(entry)) { | |
2490 if (g_list_index(playlist->entries, entry) == -1) | |
2491 /* Entry disappeared while we looked it up. | |
2492 Restart. */ | |
2493 node = playlist->entries; | |
2494 } | |
2495 else if ((entry->tuple != NULL || entry->title != NULL) && entry->length != -1) { | |
2496 update_playlistwin = TRUE; | |
2497 if (entry == playlist->position) | |
2498 update_mainwin = TRUE; | |
2499 break; | |
2500 } | |
2501 } | |
2502 | |
2503 if (!node) { | |
2504 g_mutex_lock(mutex_scan); | |
2505 playlist_get_info_scan_active = FALSE; | |
2506 g_mutex_unlock(mutex_scan); | |
2507 } | |
2508 } // on_load | |
2509 | |
2510 // on_demand | |
2511 else if (!cfg.get_info_on_load && | |
2512 cfg.get_info_on_demand && | |
2513 cfg.playlist_visible && | |
2514 !cfg.playlist_shaded && | |
2515 cfg.use_pl_metadata) { | |
2516 | |
2517 g_mutex_lock(mutex_scan); | |
2518 playlist_get_info_scan_active = FALSE; | |
2519 g_mutex_unlock(mutex_scan); | |
2520 | |
2668
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2521 for (node = g_list_nth(playlist->entries, playlistwin_get_toprow()); |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2522 node && playlistwin_item_visible(g_list_position(playlist->entries, node)); |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2523 node = g_list_next(node)) { |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2524 |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2525 entry = node->data; |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2526 |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2527 if(playlist->attribute & PLAYLIST_STATIC || |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2528 (entry->tuple && entry->tuple->length > -1 && entry->tuple->mtime != -1)) { |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2529 update_playlistwin = TRUE; |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2530 continue; |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2531 } |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2532 |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2533 if (!playlist_entry_get_info(entry)) { |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2534 if (g_list_index(playlist->entries, entry) == -1) |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2535 /* Entry disapeared while we |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2536 looked it up. Restart. */ |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2537 node = g_list_nth(playlist->entries, |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2538 playlistwin_get_toprow()); |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2539 } |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2540 else if ((entry->tuple != NULL || entry->title != NULL) && entry->length != -1) { |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2541 update_playlistwin = TRUE; |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2542 if (entry == playlist->position) |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2543 update_mainwin = TRUE; |
2545
610d85b8a22b
[svn] - on loading a playlist with tuple, invoke the scanner thread only if mtime is -1.
yaz
parents:
2504
diff
changeset
|
2544 // no need for break here since this iteration is very short. |
2313 | 2545 } |
2546 } | |
2547 } // on_demand | |
2548 else if (cfg.get_info_on_demand && | |
2549 (!cfg.playlist_visible || cfg.playlist_shaded | |
2550 || !cfg.use_pl_metadata)) | |
2551 { | |
2552 g_mutex_lock(mutex_scan); | |
2553 playlist_get_info_scan_active = FALSE; | |
2554 g_mutex_unlock(mutex_scan); | |
2555 } | |
2556 else /* not on_demand and not on_load... | |
2557 NOTE: this shouldn't happen anymore, sanity check in bmp_config_load now */ | |
2558 { | |
2559 g_mutex_lock(mutex_scan); | |
2560 playlist_get_info_scan_active = FALSE; | |
2561 g_mutex_unlock(mutex_scan); | |
2562 } | |
2563 | |
2564 if (update_playlistwin) { | |
2565 playlistwin_update_list(playlist); | |
2566 update_playlistwin = FALSE; | |
2567 } | |
2568 | |
2569 if (update_mainwin) { | |
2570 mainwin_set_info_text(); | |
2571 update_mainwin = FALSE; | |
2572 } | |
2573 | |
2574 if (playlist_get_info_scan_active) { | |
2575 continue; | |
2576 } | |
2577 | |
2578 g_mutex_lock(mutex_scan); | |
2579 g_cond_wait(cond_scan, mutex_scan); | |
2580 g_mutex_unlock(mutex_scan); | |
2581 | |
2582 } // while | |
2583 | |
2584 g_thread_exit(NULL); | |
2585 return NULL; | |
2586 } | |
2587 | |
2588 void | |
2589 playlist_start_get_info_thread(void) | |
2590 { | |
2591 G_LOCK(playlist_get_info_going); | |
2592 playlist_get_info_going = TRUE; | |
2593 G_UNLOCK(playlist_get_info_going); | |
2594 | |
2595 playlist_get_info_thread = g_thread_create(playlist_get_info_func, | |
2596 NULL, TRUE, NULL); | |
2597 } | |
2598 | |
2599 void | |
2600 playlist_stop_get_info_thread(void) | |
2601 { | |
2602 G_LOCK(playlist_get_info_going); | |
2603 playlist_get_info_going = FALSE; | |
2604 G_UNLOCK(playlist_get_info_going); | |
2605 | |
2606 g_cond_broadcast(cond_scan); | |
2607 g_thread_join(playlist_get_info_thread); | |
2608 } | |
2609 | |
2610 void | |
2611 playlist_start_get_info_scan(void) | |
2612 { | |
2613 g_mutex_lock(mutex_scan); | |
2614 playlist_get_info_scan_active = TRUE; | |
2615 g_mutex_unlock(mutex_scan); | |
2616 g_cond_signal(cond_scan); | |
2617 } | |
2618 | |
2619 void | |
2620 playlist_remove_dead_files(Playlist *playlist) | |
2621 { | |
2622 GList *node, *next_node; | |
2623 | |
2624 PLAYLIST_LOCK(playlist->mutex); | |
2625 | |
2626 for (node = playlist->entries; node; node = next_node) { | |
2627 PlaylistEntry *entry = PLAYLIST_ENTRY(node->data); | |
2628 next_node = g_list_next(node); | |
2629 | |
2630 if (!entry || !entry->filename) { | |
2631 g_message(G_STRLOC ": Playlist entry is invalid!"); | |
2632 continue; | |
2633 } | |
2634 | |
2635 /* FIXME: What about 'file:///'? */ | |
2636 /* Don't kill URLs */ | |
2637 if (strstr(entry->filename, "://")) | |
2638 continue; | |
2639 | |
2640 /* FIXME: Should test for readability */ | |
2641 if (vfs_file_test(entry->filename, G_FILE_TEST_EXISTS)) | |
2642 continue; | |
2643 | |
2644 if (entry == playlist->position) { | |
2645 /* Don't remove the currently playing song */ | |
2646 if (playback_get_playing()) | |
2647 continue; | |
2648 | |
2649 if (next_node) | |
2650 playlist->position = PLAYLIST_ENTRY(next_node->data); | |
2651 else | |
2652 playlist->position = NULL; | |
2653 } | |
2654 | |
2655 playlist_entry_free(entry); | |
2656 playlist->entries = g_list_delete_link(playlist->entries, node); | |
2657 } | |
2658 | |
2659 PLAYLIST_UNLOCK(playlist->mutex); | |
2660 | |
2661 playlist_generate_shuffle_list(playlist); | |
2662 playlistwin_update_list(playlist); | |
2663 playlist_recalc_total_time(playlist); | |
2664 playlist_manager_update(); | |
2665 } | |
2666 | |
2667 | |
2668 static gint | |
2669 playlist_dupscmp_title(PlaylistEntry * a, | |
2670 PlaylistEntry * b) | |
2671 { | |
2672 const gchar *a_title, *b_title; | |
2673 | |
2674 g_return_val_if_fail(a != NULL, 0); | |
2675 g_return_val_if_fail(b != NULL, 0); | |
2676 | |
2677 if (a->title) | |
2678 a_title = a->title; | |
2679 else { | |
2680 if (strrchr(a->filename, '/')) | |
2681 a_title = strrchr(a->filename, '/') + 1; | |
2682 else | |
2683 a_title = a->filename; | |
2684 } | |
2685 | |
2686 if (b->title) | |
2687 b_title = b->title; | |
2688 else { | |
2689 if (strrchr(a->filename, '/')) | |
2690 b_title = strrchr(b->filename, '/') + 1; | |
2691 else | |
2692 b_title = b->filename; | |
2693 } | |
2694 | |
2695 return strcmp(a_title, b_title); | |
2696 } | |
2697 | |
2698 static gint | |
2699 playlist_dupscmp_filename(PlaylistEntry * a, | |
2700 PlaylistEntry * b ) | |
2701 { | |
2702 gchar *a_filename, *b_filename; | |
2703 | |
2704 g_return_val_if_fail(a != NULL, 0); | |
2705 g_return_val_if_fail(b != NULL, 0); | |
2706 | |
2707 if (strrchr(a->filename, '/')) | |
2708 a_filename = strrchr(a->filename, '/') + 1; | |
2709 else | |
2710 a_filename = a->filename; | |
2711 | |
2712 if (strrchr(b->filename, '/')) | |
2713 b_filename = strrchr(b->filename, '/') + 1; | |
2714 else | |
2715 b_filename = b->filename; | |
2716 | |
2717 return strcmp(a_filename, b_filename); | |
2718 } | |
2719 | |
2720 static gint | |
2721 playlist_dupscmp_path(PlaylistEntry * a, | |
2722 PlaylistEntry * b) | |
2723 { | |
2724 /* simply compare the entire filename string */ | |
2725 return strcmp(a->filename, b->filename); | |
2726 } | |
2727 | |
2728 void | |
2729 playlist_remove_duplicates(Playlist *playlist, PlaylistDupsType type) | |
2730 { | |
2731 GList *node, *next_node; | |
2732 GList *node_cmp, *next_node_cmp; | |
2733 gint (*dups_compare_func)(PlaylistEntry * , PlaylistEntry *); | |
2734 | |
2735 switch ( type ) | |
2736 { | |
2737 case PLAYLIST_DUPS_TITLE: | |
2738 dups_compare_func = playlist_dupscmp_title; | |
2739 break; | |
2740 case PLAYLIST_DUPS_PATH: | |
2741 dups_compare_func = playlist_dupscmp_path; | |
2742 break; | |
2743 case PLAYLIST_DUPS_FILENAME: | |
2744 default: | |
2745 dups_compare_func = playlist_dupscmp_filename; | |
2746 break; | |
2747 } | |
2748 | |
2749 PLAYLIST_LOCK(playlist->mutex); | |
2750 | |
2751 for (node = playlist->entries; node; node = next_node) { | |
2752 PlaylistEntry *entry = PLAYLIST_ENTRY(node->data); | |
2753 next_node = g_list_next(node); | |
2754 | |
2755 if (!entry || !entry->filename) { | |
2756 g_message(G_STRLOC ": Playlist entry is invalid!"); | |
2757 continue; | |
2758 } | |
2759 | |
2760 for (node_cmp = next_node; node_cmp; node_cmp = next_node_cmp) { | |
2761 PlaylistEntry *entry_cmp = PLAYLIST_ENTRY(node_cmp->data); | |
2762 next_node_cmp = g_list_next(node_cmp); | |
2763 | |
2764 if (!entry_cmp || !entry_cmp->filename) { | |
2765 g_message(G_STRLOC ": Playlist entry is invalid!"); | |
2766 continue; | |
2767 } | |
2768 | |
2769 /* compare using the chosen dups_compare_func */ | |
2770 if ( !dups_compare_func( entry , entry_cmp ) ) { | |
2771 | |
2772 if (entry_cmp == playlist->position) { | |
2773 /* Don't remove the currently playing song */ | |
2774 if (playback_get_playing()) | |
2775 continue; | |
2776 | |
2777 if (next_node_cmp) | |
2778 playlist->position = PLAYLIST_ENTRY(next_node_cmp->data); | |
2779 else | |
2780 playlist->position = NULL; | |
2781 } | |
2782 | |
2783 /* check if this was the next item of the external | |
2784 loop; if true, replace it with the next of the next*/ | |
2785 if ( node_cmp == next_node ) | |
2786 next_node = g_list_next(next_node); | |
2787 | |
2788 playlist_entry_free(entry_cmp); | |
2789 playlist->entries = g_list_delete_link(playlist->entries, node_cmp); | |
2790 } | |
2791 } | |
2792 } | |
2793 | |
2794 PLAYLIST_UNLOCK(playlist->mutex); | |
2795 | |
2796 playlistwin_update_list(playlist); | |
2797 playlist_recalc_total_time(playlist); | |
2798 | |
2799 playlist_manager_update(); | |
2800 } | |
2801 | |
2802 void | |
2803 playlist_get_total_time(Playlist * playlist, | |
2804 gulong * total_time, | |
2805 gulong * selection_time, | |
2806 gboolean * total_more, | |
2807 gboolean * selection_more) | |
2808 { | |
2809 PLAYLIST_LOCK(playlist->mutex); | |
2810 *total_time = playlist->pl_total_time; | |
2811 *selection_time = playlist->pl_selection_time; | |
2812 *total_more = playlist->pl_total_more; | |
2813 *selection_more = playlist->pl_selection_more; | |
2814 PLAYLIST_UNLOCK(playlist->mutex); | |
2815 } | |
2816 | |
2817 | |
2818 static void | |
2819 playlist_recalc_total_time_nolock(Playlist *playlist) | |
2820 { | |
2821 GList *list; | |
2822 PlaylistEntry *entry; | |
2823 | |
2824 REQUIRE_LOCK(playlist->mutex); | |
2825 | |
2826 playlist->pl_total_time = 0; | |
2827 playlist->pl_selection_time = 0; | |
2828 playlist->pl_total_more = FALSE; | |
2829 playlist->pl_selection_more = FALSE; | |
2830 | |
2831 for (list = playlist->entries; list; list = g_list_next(list)) { | |
2832 entry = list->data; | |
2833 | |
2834 if (entry->length != -1) | |
2835 playlist->pl_total_time += entry->length / 1000; | |
2836 else | |
2837 playlist->pl_total_more = TRUE; | |
2838 | |
2839 if (entry->selected) { | |
2840 if (entry->length != -1) | |
2841 playlist->pl_selection_time += entry->length / 1000; | |
2842 else | |
2843 playlist->pl_selection_more = TRUE; | |
2844 } | |
2845 } | |
2846 } | |
2847 | |
2848 static void | |
2849 playlist_recalc_total_time(Playlist *playlist) | |
2850 { | |
2851 PLAYLIST_LOCK(playlist->mutex); | |
2852 playlist_recalc_total_time_nolock(playlist); | |
2853 PLAYLIST_UNLOCK(playlist->mutex); | |
2854 } | |
2855 | |
2856 gint | |
2857 playlist_select_search( Playlist *playlist , TitleInput *tuple , gint action ) | |
2858 { | |
2859 GList *entry_list = NULL, *found_list = NULL, *sel_list = NULL; | |
2860 gboolean is_first_search = TRUE; | |
2861 gint num_of_entries_found = 0; | |
2862 | |
2863 #if defined(USE_REGEX_ONIGURUMA) | |
2864 /* set encoding for Oniguruma regex to UTF-8 */ | |
2865 reg_set_encoding( REG_POSIX_ENCODING_UTF8 ); | |
2866 onig_set_default_syntax( ONIG_SYNTAX_POSIX_BASIC ); | |
2867 #endif | |
2868 | |
2869 PLAYLIST_LOCK(playlist->mutex); | |
2870 | |
2871 if ( tuple->track_name != NULL ) | |
2872 { | |
2873 /* match by track_name */ | |
2874 const gchar *regex_pattern = tuple->track_name; | |
2875 regex_t regex; | |
2876 #if defined(USE_REGEX_PCRE) | |
2877 if ( regcomp( ®ex , regex_pattern , REG_NOSUB | REG_ICASE | REG_UTF8 ) == 0 ) | |
2878 #else | |
2879 if ( regcomp( ®ex , regex_pattern , REG_NOSUB | REG_ICASE ) == 0 ) | |
2880 #endif | |
2881 { | |
2882 GList *tfound_list = NULL; | |
2883 if ( is_first_search == TRUE ) entry_list = playlist->entries; | |
2884 else entry_list = found_list; /* use found_list */ | |
2885 for ( ; entry_list ; entry_list = g_list_next(entry_list) ) | |
2886 { | |
2887 PlaylistEntry *entry = entry_list->data; | |
2888 if ( ( entry->tuple != NULL ) && ( entry->tuple->track_name != NULL ) && | |
2889 ( regexec( ®ex , entry->tuple->track_name , 0 , NULL , 0 ) == 0 ) ) | |
2890 { | |
2891 tfound_list = g_list_append( tfound_list , entry ); | |
2892 } | |
2893 } | |
2894 g_list_free( found_list ); /* wipe old found_list */ | |
2895 found_list = tfound_list; /* move tfound_list in found_list */ | |
2896 regfree( ®ex ); | |
2897 } | |
2898 is_first_search = FALSE; | |
2899 } | |
2900 | |
2901 if ( tuple->album_name != NULL ) | |
2902 { | |
2903 /* match by album_name */ | |
2904 const gchar *regex_pattern = tuple->album_name; | |
2905 regex_t regex; | |
2906 #if defined(USE_REGEX_PCRE) | |
2907 if ( regcomp( ®ex , regex_pattern , REG_NOSUB | REG_ICASE | REG_UTF8 ) == 0 ) | |
2908 #else | |
2909 if ( regcomp( ®ex , regex_pattern , REG_NOSUB | REG_ICASE ) == 0 ) | |
2910 #endif | |
2911 { | |
2912 GList *tfound_list = NULL; | |
2913 if ( is_first_search == TRUE ) entry_list = playlist->entries; | |
2914 else entry_list = found_list; /* use found_list */ | |
2915 for ( ; entry_list ; entry_list = g_list_next(entry_list) ) | |
2916 { | |
2917 PlaylistEntry *entry = entry_list->data; | |
2918 if ( ( entry->tuple != NULL ) && ( entry->tuple->album_name != NULL ) && | |
2919 ( regexec( ®ex , entry->tuple->album_name , 0 , NULL , 0 ) == 0 ) ) | |
2920 { | |
2921 tfound_list = g_list_append( tfound_list , entry ); | |
2922 } | |
2923 } | |
2924 g_list_free( found_list ); /* wipe old found_list */ | |
2925 found_list = tfound_list; /* move tfound_list in found_list */ | |
2926 regfree( ®ex ); | |
2927 } | |
2928 is_first_search = FALSE; | |
2929 } | |
2930 | |
2931 if ( tuple->performer != NULL ) | |
2932 { | |
2933 /* match by performer */ | |
2934 const gchar *regex_pattern = tuple->performer; | |
2935 regex_t regex; | |
2936 #if defined(USE_REGEX_PCRE) | |
2937 if ( regcomp( ®ex , regex_pattern , REG_NOSUB | REG_ICASE | REG_UTF8 ) == 0 ) | |
2938 #else | |
2939 if ( regcomp( ®ex , regex_pattern , REG_NOSUB | REG_ICASE ) == 0 ) | |
2940 #endif | |
2941 { | |
2942 GList *tfound_list = NULL; | |
2943 if ( is_first_search == TRUE ) entry_list = playlist->entries; | |
2944 else entry_list = found_list; /* use found_list */ | |
2945 for ( ; entry_list ; entry_list = g_list_next(entry_list) ) | |
2946 { | |
2947 PlaylistEntry *entry = entry_list->data; | |
2948 if ( ( entry->tuple != NULL ) && ( entry->tuple->performer != NULL ) && | |
2949 ( regexec( ®ex , entry->tuple->performer , 0 , NULL , 0 ) == 0 ) ) | |
2950 { | |
2951 tfound_list = g_list_append( tfound_list , entry ); | |
2952 } | |
2953 } | |
2954 g_list_free( found_list ); /* wipe old found_list */ | |
2955 found_list = tfound_list; /* move tfound_list in found_list */ | |
2956 regfree( ®ex ); | |
2957 } | |
2958 is_first_search = FALSE; | |
2959 } | |
2960 | |
2961 if ( tuple->file_name != NULL ) | |
2962 { | |
2963 /* match by file_name */ | |
2964 const gchar *regex_pattern = tuple->file_name; | |
2965 regex_t regex; | |
2966 #if defined(USE_REGEX_PCRE) | |
2967 if ( regcomp( ®ex , regex_pattern , REG_NOSUB | REG_ICASE | REG_UTF8 ) == 0 ) | |
2968 #else | |
2969 if ( regcomp( ®ex , regex_pattern , REG_NOSUB | REG_ICASE ) == 0 ) | |
2970 #endif | |
2971 { | |
2972 GList *tfound_list = NULL; | |
2973 if ( is_first_search == TRUE ) entry_list = playlist->entries; | |
2974 else entry_list = found_list; /* use found_list */ | |
2975 for ( ; entry_list ; entry_list = g_list_next(entry_list) ) | |
2976 { | |
2977 PlaylistEntry *entry = entry_list->data; | |
2978 if ( ( entry->tuple != NULL ) && ( entry->tuple->file_name != NULL ) && | |
2979 ( regexec( ®ex , entry->tuple->file_name , 0 , NULL , 0 ) == 0 ) ) | |
2980 { | |
2981 tfound_list = g_list_append( tfound_list , entry ); | |
2982 } | |
2983 } | |
2984 g_list_free( found_list ); /* wipe old found_list */ | |
2985 found_list = tfound_list; /* move tfound_list in found_list */ | |
2986 regfree( ®ex ); | |
2987 } | |
2988 is_first_search = FALSE; | |
2989 } | |
2990 | |
2991 /* NOTE: action = 0 -> default behaviour, select all matching entries */ | |
2992 /* if some entries are still in found_list, those | |
2993 are what the user is searching for; select them */ | |
2994 for ( sel_list = found_list ; sel_list ; sel_list = g_list_next(sel_list) ) | |
2995 { | |
2996 PlaylistEntry *entry = sel_list->data; | |
2997 entry->selected = TRUE; | |
2998 num_of_entries_found++; | |
2999 } | |
3000 | |
3001 g_list_free( found_list ); | |
3002 | |
3003 PLAYLIST_UNLOCK(playlist->mutex); | |
3004 playlist_recalc_total_time(playlist); | |
3005 | |
3006 return num_of_entries_found; | |
3007 } | |
3008 | |
3009 void | |
3010 playlist_select_all(Playlist *playlist, gboolean set) | |
3011 { | |
3012 GList *list; | |
3013 | |
3014 PLAYLIST_LOCK(playlist->mutex); | |
3015 | |
3016 for (list = playlist->entries; list; list = g_list_next(list)) { | |
3017 PlaylistEntry *entry = list->data; | |
3018 entry->selected = set; | |
3019 } | |
3020 | |
3021 PLAYLIST_UNLOCK(playlist->mutex); | |
3022 playlist_recalc_total_time(playlist); | |
3023 } | |
3024 | |
3025 void | |
3026 playlist_select_invert_all(Playlist *playlist) | |
3027 { | |
3028 GList *list; | |
3029 | |
3030 PLAYLIST_LOCK(playlist->mutex); | |
3031 | |
3032 for (list = playlist->entries; list; list = g_list_next(list)) { | |
3033 PlaylistEntry *entry = list->data; | |
3034 entry->selected = !entry->selected; | |
3035 } | |
3036 | |
3037 PLAYLIST_UNLOCK(playlist->mutex); | |
3038 playlist_recalc_total_time(playlist); | |
3039 } | |
3040 | |
3041 gboolean | |
3042 playlist_select_invert(Playlist *playlist, guint pos) | |
3043 { | |
3044 GList *list; | |
3045 gboolean invert_ok = FALSE; | |
3046 | |
3047 PLAYLIST_LOCK(playlist->mutex); | |
3048 | |
3049 if ((list = g_list_nth(playlist->entries, pos))) { | |
3050 PlaylistEntry *entry = list->data; | |
3051 entry->selected = !entry->selected; | |
3052 invert_ok = TRUE; | |
3053 } | |
3054 | |
3055 PLAYLIST_UNLOCK(playlist->mutex); | |
3056 playlist_recalc_total_time(playlist); | |
3057 | |
3058 return invert_ok; | |
3059 } | |
3060 | |
3061 | |
3062 void | |
3063 playlist_select_range(Playlist *playlist, gint min_pos, gint max_pos, gboolean select) | |
3064 { | |
3065 GList *list; | |
3066 gint i; | |
3067 | |
3068 if (min_pos > max_pos) | |
3069 SWAP(min_pos, max_pos); | |
3070 | |
3071 PLAYLIST_LOCK(playlist->mutex); | |
3072 | |
3073 list = g_list_nth(playlist->entries, min_pos); | |
3074 for (i = min_pos; i <= max_pos && list; i++) { | |
3075 PlaylistEntry *entry = list->data; | |
3076 entry->selected = select; | |
3077 list = g_list_next(list); | |
3078 } | |
3079 | |
3080 PLAYLIST_UNLOCK(playlist->mutex); | |
3081 | |
3082 playlist_recalc_total_time(playlist); | |
3083 } | |
3084 | |
3085 gboolean | |
3086 playlist_read_info_selection(Playlist *playlist) | |
3087 { | |
3088 GList *node; | |
3089 gboolean retval = FALSE; | |
3090 | |
3091 PLAYLIST_LOCK(playlist->mutex); | |
3092 | |
3093 for (node = playlist->entries; node; node = g_list_next(node)) { | |
3094 PlaylistEntry *entry = node->data; | |
3095 if (!entry->selected) | |
3096 continue; | |
3097 | |
3098 retval = TRUE; | |
3099 | |
3100 str_replace_in(&entry->title, NULL); | |
3101 entry->length = -1; | |
3102 | |
3103 /* invalidate mtime to reread */ | |
3104 if (entry->tuple != NULL) | |
3105 entry->tuple->mtime = -1; /* -1 denotes "non-initialized". now 0 is for stream etc. yaz */ | |
3106 | |
3107 if (!playlist_entry_get_info(entry)) { | |
3108 if (g_list_index(playlist->entries, entry) == -1) | |
3109 /* Entry disappeared while we looked it up. Restart. */ | |
3110 node = playlist->entries; | |
3111 } | |
3112 } | |
3113 | |
3114 PLAYLIST_UNLOCK(playlist->mutex); | |
3115 | |
3116 playlistwin_update_list(playlist); | |
3117 playlist_recalc_total_time(playlist); | |
3118 | |
3119 return retval; | |
3120 } | |
3121 | |
3122 void | |
3123 playlist_read_info(Playlist *playlist, guint pos) | |
3124 { | |
3125 GList *node; | |
3126 | |
3127 PLAYLIST_LOCK(playlist->mutex); | |
3128 | |
3129 if ((node = g_list_nth(playlist->entries, pos))) { | |
3130 PlaylistEntry *entry = node->data; | |
3131 str_replace_in(&entry->title, NULL); | |
3132 entry->length = -1; | |
3133 playlist_entry_get_info(entry); | |
3134 } | |
3135 | |
3136 PLAYLIST_UNLOCK(playlist->mutex); | |
3137 | |
3138 playlistwin_update_list(playlist); | |
3139 playlist_recalc_total_time(playlist); | |
3140 } | |
3141 | |
3142 Playlist * | |
3143 playlist_get_active(void) | |
3144 { | |
3145 if (playlists_iter != NULL) | |
3146 return (Playlist *) playlists_iter->data; | |
3147 | |
2736 | 3148 if (playlists) |
3149 return (Playlist *) playlists->data; | |
3150 | |
3151 return NULL; | |
2313 | 3152 } |
3153 | |
3154 void | |
3155 playlist_set_shuffle(gboolean shuffle) | |
3156 { | |
3157 Playlist *playlist = playlist_get_active(); | |
3158 if (!playlist) | |
3159 return; | |
3160 | |
3161 PLAYLIST_LOCK(playlist->mutex); | |
3162 | |
3163 playlist_position_before_jump = NULL; | |
3164 | |
3165 cfg.shuffle = shuffle; | |
3166 playlist_generate_shuffle_list_nolock(playlist); | |
3167 | |
3168 PLAYLIST_UNLOCK(playlist->mutex); | |
3169 } | |
3170 | |
3171 Playlist * | |
3172 playlist_new(void) | |
3173 { | |
3174 Playlist *playlist = g_new0(Playlist, 1); | |
3175 playlist->mutex = g_mutex_new(); | |
3176 playlist->loading_playlist = FALSE; | |
3177 | |
3178 playlist_set_current_name(playlist, NULL); | |
3179 playlist_clear(playlist); | |
3180 | |
3181 return playlist; | |
3182 } | |
3183 | |
3184 void | |
3185 playlist_free(Playlist *playlist) | |
3186 { | |
3187 g_mutex_free( playlist->mutex ); | |
3188 g_free( playlist ); | |
3189 return; | |
3190 } | |
3191 | |
3192 Playlist * | |
3193 playlist_new_from_selected(void) | |
3194 { | |
3195 Playlist *newpl = playlist_new(); | |
3196 Playlist *playlist = playlist_get_active(); | |
3197 GList *list = playlist_get_selected(playlist); | |
3198 | |
3199 playlist_add_playlist( newpl ); | |
3200 | |
3201 PLAYLIST_LOCK(playlist->mutex); | |
3202 | |
3203 while ( list != NULL ) | |
3204 { | |
3205 PlaylistEntry *entry = g_list_nth_data(playlist->entries, GPOINTER_TO_INT(list->data)); | |
3206 if ( entry->filename != NULL ) /* paranoid? oh well... */ | |
3207 playlist_add( newpl , entry->filename ); | |
3208 list = g_list_next(list); | |
3209 } | |
3210 | |
3211 PLAYLIST_UNLOCK(playlist->mutex); | |
3212 | |
3213 playlist_recalc_total_time(newpl); | |
3214 playlistwin_update_list(playlist); | |
3215 | |
3216 return newpl; | |
3217 } | |
3218 | |
3219 const gchar * | |
3220 playlist_get_filename_to_play(Playlist *playlist) | |
3221 { | |
3222 const gchar *filename = NULL; | |
3223 | |
3224 if (!playlist) | |
3225 return NULL; | |
3226 | |
3227 PLAYLIST_LOCK(playlist->mutex); | |
3228 | |
3229 if (!playlist->position) { | |
3230 if (cfg.shuffle) | |
3231 playlist->position = playlist->shuffle->data; | |
3232 else | |
3233 playlist->position = playlist->entries->data; | |
3234 } | |
3235 | |
3236 filename = playlist->position->filename; | |
3237 | |
3238 PLAYLIST_UNLOCK(playlist->mutex); | |
3239 | |
3240 return filename; | |
3241 } | |
3242 | |
3243 PlaylistEntry * | |
3244 playlist_get_entry_to_play(Playlist *playlist) | |
3245 { | |
3246 if (!playlist) | |
3247 return NULL; | |
3248 | |
3249 PLAYLIST_LOCK(playlist->mutex); | |
3250 | |
3251 if (!playlist->position) { | |
3252 if (cfg.shuffle) | |
3253 playlist->position = playlist->shuffle->data; | |
3254 else | |
3255 playlist->position = playlist->entries->data; | |
3256 } | |
3257 | |
3258 PLAYLIST_UNLOCK(playlist->mutex); | |
3259 | |
3260 return playlist->position; | |
3261 } |