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