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