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