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