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