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