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