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