Mercurial > audlegacy
annotate src/audacious/playlist.c @ 2670:84a10fce92a1 trunk
[svn] - playlist_set_info() does not change the playlist and shouldn't block
author | nenolod |
---|---|
date | Tue, 10 Apr 2007 11:25:39 -0700 |
parents | 0b4949cad842 |
children | e9b379528fbc |
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 g_return_if_fail(playlist != NULL); | |
1051 | |
1052 if (playlist->position) { | |
1053 g_free(playlist->position->title); | |
1054 playlist->position->title = g_strdup(title); | |
1055 playlist->position->length = length; | |
2488
c5075a79f1aa
[svn] make input->set_info overwrite tuple->track_name. it allows input
yaz
parents:
2480
diff
changeset
|
1056 |
c5075a79f1aa
[svn] make input->set_info overwrite tuple->track_name. it allows input
yaz
parents:
2480
diff
changeset
|
1057 // 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
|
1058 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
|
1059 if(playlist->position->tuple->track_name){ |
c5075a79f1aa
[svn] make input->set_info overwrite tuple->track_name. it allows input
yaz
parents:
2480
diff
changeset
|
1060 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
|
1061 } |
c5075a79f1aa
[svn] make input->set_info overwrite tuple->track_name. it allows input
yaz
parents:
2480
diff
changeset
|
1062 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
|
1063 } |
2313 | 1064 } |
1065 | |
1066 playlist_recalc_total_time(playlist); | |
1067 | |
1068 mainwin_set_song_info(rate, freq, nch); | |
2460 | 1069 |
1070 if ( playlist->position ) | |
1071 hook_call( "playlist set info" , playlist->position ); | |
2313 | 1072 } |
1073 | |
1074 void | |
1075 playlist_set_info(Playlist * playlist, const gchar * title, gint length, gint rate, | |
1076 gint freq, gint nch) | |
1077 { | |
1078 g_return_if_fail(playlist != NULL); | |
1079 | |
1080 if (playlist->position) { | |
1081 g_free(playlist->position->title); | |
1082 playlist->position->title = g_strdup(title); | |
1083 playlist->position->length = length; | |
1084 } | |
1085 | |
1086 playlist_recalc_total_time(playlist); | |
1087 | |
1088 mainwin_set_song_info(rate, freq, nch); | |
2460 | 1089 |
1090 if ( playlist->position ) | |
1091 hook_call( "playlist set info" , playlist->position ); | |
2313 | 1092 } |
1093 | |
1094 void | |
1095 playlist_check_pos_current(Playlist *playlist) | |
1096 { | |
1097 gint pos, row, bottom; | |
1098 | |
1099 if (!playlist) | |
1100 return; | |
1101 | |
1102 PLAYLIST_LOCK(playlist->mutex); | |
1103 if (!playlist->position || !playlistwin_list) { | |
1104 PLAYLIST_UNLOCK(playlist->mutex); | |
1105 return; | |
1106 } | |
1107 | |
1108 pos = g_list_index(playlist->entries, playlist->position); | |
1109 | |
1110 if (playlistwin_item_visible(pos)) { | |
1111 PLAYLIST_UNLOCK(playlist->mutex); | |
1112 return; | |
1113 } | |
1114 | |
1115 bottom = MAX(0, playlist_get_length_nolock(playlist) - | |
1116 playlistwin_list->pl_num_visible); | |
1117 row = CLAMP(pos - playlistwin_list->pl_num_visible / 2, 0, bottom); | |
1118 PLAYLIST_UNLOCK(playlist->mutex); | |
1119 playlistwin_set_toprow(row); | |
1120 g_cond_signal(cond_scan); | |
1121 } | |
1122 | |
1123 void | |
1124 playlist_next(Playlist *playlist) | |
1125 { | |
1126 GList *plist_pos_list; | |
1127 gboolean restart_playing = FALSE; | |
2480
bb2f191895ce
[svn] - fixed crash when skipping to next song with an empty playlist
marvin
parents:
2460
diff
changeset
|
1128 if (!playlist_get_length(playlist)) |
2313 | 1129 return; |
1130 | |
1131 PLAYLIST_LOCK(playlist->mutex); | |
1132 | |
1133 if ((playlist_position_before_jump != NULL) && playlist->queue == NULL) | |
1134 { | |
1135 playlist->position = playlist_position_before_jump; | |
1136 playlist_position_before_jump = NULL; | |
1137 } | |
1138 | |
1139 plist_pos_list = find_playlist_position_list(playlist); | |
1140 | |
1141 if (!cfg.repeat && !g_list_next(plist_pos_list) && playlist->queue == NULL) { | |
1142 PLAYLIST_UNLOCK(playlist->mutex); | |
1143 return; | |
1144 } | |
1145 | |
1146 if (playback_get_playing()) { | |
1147 /* We need to stop before changing playlist_position */ | |
1148 PLAYLIST_UNLOCK(playlist->mutex); | |
1149 ip_data.stop = TRUE; | |
1150 playback_stop(); | |
1151 ip_data.stop = FALSE; | |
1152 PLAYLIST_LOCK(playlist->mutex); | |
1153 restart_playing = TRUE; | |
1154 } | |
1155 | |
1156 plist_pos_list = find_playlist_position_list(playlist); | |
1157 if (playlist->queue != NULL) | |
1158 play_queued(playlist); | |
1159 else if (g_list_next(plist_pos_list)) | |
1160 playlist->position = g_list_next(plist_pos_list)->data; | |
1161 else if (cfg.repeat) { | |
1162 playlist->position = NULL; | |
1163 playlist_generate_shuffle_list_nolock(playlist); | |
1164 if (cfg.shuffle) | |
1165 playlist->position = playlist->shuffle->data; | |
1166 else | |
1167 playlist->position = playlist->entries->data; | |
1168 } | |
1169 PLAYLIST_UNLOCK(playlist->mutex); | |
1170 playlist_check_pos_current(playlist); | |
1171 | |
1172 if (restart_playing) | |
1173 playback_initiate(); | |
1174 else { | |
1175 mainwin_set_info_text(); | |
1176 playlistwin_update_list(playlist); | |
1177 } | |
1178 } | |
1179 | |
1180 void | |
1181 playlist_prev(Playlist *playlist) | |
1182 { | |
1183 GList *plist_pos_list; | |
1184 gboolean restart_playing = FALSE; | |
1185 | |
2480
bb2f191895ce
[svn] - fixed crash when skipping to next song with an empty playlist
marvin
parents:
2460
diff
changeset
|
1186 if (!playlist_get_length(playlist)) |
2313 | 1187 return; |
1188 | |
1189 PLAYLIST_LOCK(playlist->mutex); | |
1190 | |
1191 if ((playlist_position_before_jump != NULL) && playlist->queue == NULL) | |
1192 { | |
1193 playlist->position = playlist_position_before_jump; | |
1194 playlist_position_before_jump = NULL; | |
1195 } | |
1196 | |
1197 plist_pos_list = find_playlist_position_list(playlist); | |
1198 | |
1199 if (!cfg.repeat && !g_list_previous(plist_pos_list)) { | |
1200 PLAYLIST_UNLOCK(playlist->mutex); | |
1201 return; | |
1202 } | |
1203 | |
1204 if (playback_get_playing()) { | |
1205 /* We need to stop before changing playlist_position */ | |
1206 PLAYLIST_UNLOCK(playlist->mutex); | |
1207 ip_data.stop = TRUE; | |
1208 playback_stop(); | |
1209 ip_data.stop = FALSE; | |
1210 PLAYLIST_LOCK(playlist->mutex); | |
1211 restart_playing = TRUE; | |
1212 } | |
1213 | |
1214 plist_pos_list = find_playlist_position_list(playlist); | |
1215 if (g_list_previous(plist_pos_list)) { | |
1216 playlist->position = g_list_previous(plist_pos_list)->data; | |
1217 } | |
1218 else if (cfg.repeat) { | |
1219 GList *node; | |
1220 playlist->position = NULL; | |
1221 playlist_generate_shuffle_list_nolock(playlist); | |
1222 if (cfg.shuffle) | |
1223 node = g_list_last(playlist->shuffle); | |
1224 else | |
1225 node = g_list_last(playlist->entries); | |
1226 if (node) | |
1227 playlist->position = node->data; | |
1228 } | |
1229 | |
1230 PLAYLIST_UNLOCK(playlist->mutex); | |
1231 | |
1232 playlist_check_pos_current(playlist); | |
1233 | |
1234 if (restart_playing) | |
1235 playback_initiate(); | |
1236 else { | |
1237 mainwin_set_info_text(); | |
1238 playlistwin_update_list(playlist); | |
1239 } | |
1240 } | |
1241 | |
1242 void | |
1243 playlist_queue(Playlist *playlist) | |
1244 { | |
1245 GList *list = playlist_get_selected(playlist); | |
1246 GList *it = list; | |
1247 | |
1248 PLAYLIST_LOCK(playlist->mutex); | |
1249 | |
1250 if ((cfg.shuffle) && (playlist_position_before_jump == NULL)) | |
1251 { | |
1252 /* Shuffling and this is our first manual jump. */ | |
1253 playlist_position_before_jump = playlist->position; | |
1254 } | |
1255 | |
1256 while (it) { | |
1257 GList *next = g_list_next(it); | |
1258 GList *tmp; | |
1259 | |
1260 /* XXX: WTF? --nenolod */ | |
1261 it->data = g_list_nth_data(playlist->entries, GPOINTER_TO_INT(it->data)); | |
1262 if ((tmp = g_list_find(playlist->queue, it->data))) { | |
1263 playlist->queue = g_list_remove_link(playlist->queue, tmp); | |
1264 g_list_free_1(tmp); | |
1265 list = g_list_remove_link(list, it); | |
1266 g_list_free_1(it); | |
1267 } | |
1268 | |
1269 it = next; | |
1270 } | |
1271 | |
1272 playlist->queue = g_list_concat(playlist->queue, list); | |
1273 | |
1274 PLAYLIST_UNLOCK(playlist->mutex); | |
1275 | |
1276 playlist_recalc_total_time(playlist); | |
1277 playlistwin_update_list(playlist); | |
1278 } | |
1279 | |
1280 void | |
1281 playlist_queue_position(Playlist *playlist, guint pos) | |
1282 { | |
1283 GList *tmp; | |
1284 PlaylistEntry *entry; | |
1285 | |
1286 PLAYLIST_LOCK(playlist->mutex); | |
1287 | |
1288 if ((cfg.shuffle) && (playlist_position_before_jump == NULL)) | |
1289 { | |
1290 /* Shuffling and this is our first manual jump. */ | |
1291 playlist_position_before_jump = playlist->position; | |
1292 } | |
1293 | |
1294 entry = g_list_nth_data(playlist->entries, pos); | |
1295 if ((tmp = g_list_find(playlist->queue, entry))) { | |
1296 playlist->queue = g_list_remove_link(playlist->queue, tmp); | |
1297 g_list_free_1(tmp); | |
1298 } | |
1299 else | |
1300 playlist->queue = g_list_append(playlist->queue, entry); | |
1301 PLAYLIST_UNLOCK(playlist->mutex); | |
1302 | |
1303 playlist_recalc_total_time(playlist); | |
1304 playlistwin_update_list(playlist); | |
1305 } | |
1306 | |
1307 gboolean | |
1308 playlist_is_position_queued(Playlist *playlist, guint pos) | |
1309 { | |
1310 PlaylistEntry *entry; | |
1311 GList *tmp; | |
1312 | |
1313 PLAYLIST_LOCK(playlist->mutex); | |
1314 entry = g_list_nth_data(playlist->entries, pos); | |
1315 tmp = g_list_find(playlist->queue, entry); | |
1316 PLAYLIST_UNLOCK(playlist->mutex); | |
1317 | |
1318 return tmp != NULL; | |
1319 } | |
1320 | |
1321 gint | |
1322 playlist_get_queue_position_number(Playlist *playlist, guint pos) | |
1323 { | |
1324 PlaylistEntry *entry; | |
1325 gint tmp; | |
1326 | |
1327 PLAYLIST_LOCK(playlist->mutex); | |
1328 entry = g_list_nth_data(playlist->entries, pos); | |
1329 tmp = g_list_index(playlist->queue, entry); | |
1330 PLAYLIST_UNLOCK(playlist->mutex); | |
1331 | |
1332 return tmp; | |
1333 } | |
1334 | |
1335 gint | |
1336 playlist_get_queue_qposition_number(Playlist *playlist, guint pos) | |
1337 { | |
1338 PlaylistEntry *entry; | |
1339 gint tmp; | |
1340 | |
1341 PLAYLIST_LOCK(playlist->mutex); | |
1342 entry = g_list_nth_data(playlist->queue, pos); | |
1343 tmp = g_list_index(playlist->entries, entry); | |
1344 PLAYLIST_UNLOCK(playlist->mutex); | |
1345 | |
1346 return tmp; | |
1347 } | |
1348 | |
1349 void | |
1350 playlist_clear_queue(Playlist *playlist) | |
1351 { | |
1352 PLAYLIST_LOCK(playlist->mutex); | |
1353 g_list_free(playlist->queue); | |
1354 playlist->queue = NULL; | |
1355 PLAYLIST_UNLOCK(playlist->mutex); | |
1356 | |
1357 playlist_recalc_total_time(playlist); | |
1358 playlistwin_update_list(playlist); | |
1359 } | |
1360 | |
1361 void | |
1362 playlist_queue_remove(Playlist *playlist, guint pos) | |
1363 { | |
1364 void *entry; | |
1365 | |
1366 PLAYLIST_LOCK(playlist->mutex); | |
1367 entry = g_list_nth_data(playlist->entries, pos); | |
1368 playlist->queue = g_list_remove(playlist->queue, entry); | |
1369 PLAYLIST_UNLOCK(playlist->mutex); | |
1370 | |
1371 playlistwin_update_list(playlist); | |
1372 } | |
1373 | |
1374 gint | |
1375 playlist_get_queue_position(Playlist *playlist, PlaylistEntry * entry) | |
1376 { | |
1377 return g_list_index(playlist->queue, entry); | |
1378 } | |
1379 | |
1380 void | |
1381 playlist_set_position(Playlist *playlist, guint pos) | |
1382 { | |
1383 GList *node; | |
1384 gboolean restart_playing = FALSE; | |
1385 | |
1386 if (!playlist) | |
1387 return; | |
1388 | |
1389 PLAYLIST_LOCK(playlist->mutex); | |
1390 | |
1391 node = g_list_nth(playlist->entries, pos); | |
1392 if (!node) { | |
1393 PLAYLIST_UNLOCK(playlist->mutex); | |
1394 return; | |
1395 } | |
1396 | |
1397 if (playback_get_playing()) { | |
1398 /* We need to stop before changing playlist_position */ | |
1399 PLAYLIST_UNLOCK(playlist->mutex); | |
1400 ip_data.stop = TRUE; | |
1401 playback_stop(); | |
1402 ip_data.stop = FALSE; | |
1403 PLAYLIST_LOCK(playlist->mutex); | |
1404 restart_playing = TRUE; | |
1405 } | |
1406 | |
1407 if ((cfg.shuffle) && (playlist_position_before_jump == NULL)) | |
1408 { | |
1409 /* Shuffling and this is our first manual jump. */ | |
1410 playlist_position_before_jump = playlist->position; | |
1411 } | |
1412 | |
1413 playlist->position = node->data; | |
1414 PLAYLIST_UNLOCK(playlist->mutex); | |
1415 playlist_check_pos_current(playlist); | |
1416 | |
1417 if (restart_playing) | |
1418 playback_initiate(); | |
1419 else { | |
1420 mainwin_set_info_text(); | |
1421 playlistwin_update_list(playlist); | |
1422 } | |
1423 } | |
1424 | |
1425 void | |
1426 playlist_eof_reached(Playlist *playlist) | |
1427 { | |
1428 GList *plist_pos_list; | |
1429 | |
1430 if ((cfg.no_playlist_advance && !cfg.repeat) || cfg.stopaftersong) | |
1431 ip_data.stop = TRUE; | |
1432 playback_stop(); | |
1433 if ((cfg.no_playlist_advance && !cfg.repeat) || cfg.stopaftersong) | |
1434 ip_data.stop = FALSE; | |
1435 | |
2408
b380e84148bb
[svn] - rename some hooks so that they are more logical
nenolod
parents:
2407
diff
changeset
|
1436 hook_call("playback end", playlist->position); |
2407
1dc1d36d0347
[svn] - add hooks: playback begin, playback end, playlist reached end
nenolod
parents:
2380
diff
changeset
|
1437 |
2313 | 1438 PLAYLIST_LOCK(playlist->mutex); |
1439 | |
1440 if ((playlist_position_before_jump != NULL) && playlist->queue == NULL) | |
1441 { | |
1442 playlist->position = playlist_position_before_jump; | |
1443 playlist_position_before_jump = NULL; | |
1444 } | |
1445 | |
1446 plist_pos_list = find_playlist_position_list(playlist); | |
1447 | |
1448 if (cfg.no_playlist_advance) { | |
1449 PLAYLIST_UNLOCK(playlist->mutex); | |
1450 mainwin_clear_song_info(); | |
1451 if (cfg.repeat) | |
1452 playback_initiate(); | |
1453 return; | |
1454 } | |
1455 | |
1456 if (cfg.stopaftersong) { | |
1457 PLAYLIST_UNLOCK(playlist->mutex); | |
1458 mainwin_clear_song_info(); | |
1459 mainwin_set_stopaftersong(FALSE); | |
1460 return; | |
1461 } | |
1462 | |
1463 if (playlist->queue != NULL) { | |
1464 play_queued(playlist); | |
1465 } | |
1466 else if (!g_list_next(plist_pos_list)) { | |
1467 if (cfg.shuffle) { | |
1468 playlist->position = NULL; | |
1469 playlist_generate_shuffle_list_nolock(playlist); | |
1470 } | |
2380 | 1471 else if (playlist->entries != NULL) |
2313 | 1472 playlist->position = playlist->entries->data; |
1473 | |
1474 if (!cfg.repeat) { | |
1475 PLAYLIST_UNLOCK(playlist->mutex); | |
2408
b380e84148bb
[svn] - rename some hooks so that they are more logical
nenolod
parents:
2407
diff
changeset
|
1476 hook_call("playlist end reached", playlist->position); |
2313 | 1477 mainwin_clear_song_info(); |
1478 mainwin_set_info_text(); | |
1479 return; | |
1480 } | |
1481 } | |
1482 else | |
1483 playlist->position = g_list_next(plist_pos_list)->data; | |
1484 | |
1485 PLAYLIST_UNLOCK(playlist->mutex); | |
1486 | |
1487 playlist_check_pos_current(playlist); | |
1488 playback_initiate(); | |
1489 mainwin_set_info_text(); | |
1490 playlistwin_update_list(playlist); | |
1491 } | |
1492 | |
1493 gint | |
1494 playlist_get_length(Playlist *playlist) | |
1495 { | |
1496 gint retval; | |
1497 | |
1498 PLAYLIST_LOCK(playlist->mutex); | |
1499 retval = playlist_get_length_nolock(playlist); | |
1500 PLAYLIST_UNLOCK(playlist->mutex); | |
1501 | |
1502 return retval; | |
1503 } | |
1504 | |
1505 gint | |
1506 playlist_queue_get_length(Playlist *playlist) | |
1507 { | |
1508 gint length; | |
1509 | |
1510 PLAYLIST_LOCK(playlist->mutex); | |
1511 length = g_list_length(playlist->queue); | |
1512 PLAYLIST_UNLOCK(playlist->mutex); | |
1513 | |
1514 return length; | |
1515 } | |
1516 | |
1517 gint | |
1518 playlist_get_length_nolock(Playlist *playlist) | |
1519 { | |
1520 return g_list_length(playlist->entries); | |
1521 } | |
1522 | |
1523 gchar * | |
1524 playlist_get_info_text(Playlist *playlist) | |
1525 { | |
1526 gchar *text, *title, *numbers, *length; | |
1527 | |
1528 g_return_val_if_fail(playlist != NULL, NULL); | |
1529 | |
1530 PLAYLIST_LOCK(playlist->mutex); | |
1531 if (!playlist->position) { | |
1532 PLAYLIST_UNLOCK(playlist->mutex); | |
1533 return NULL; | |
1534 } | |
1535 | |
1536 /* FIXME: there should not be a need to do additional conversion, | |
1537 * if playlist is properly maintained */ | |
1538 if (playlist->position->title) { | |
1539 title = str_to_utf8(playlist->position->title); | |
1540 } | |
1541 else { | |
1542 gchar *basename = g_path_get_basename(playlist->position->filename); | |
1543 title = filename_to_utf8(basename); | |
1544 g_free(basename); | |
1545 } | |
1546 | |
1547 /* | |
1548 * If the user don't want numbers in the playlist, don't | |
1549 * display them in other parts of XMMS | |
1550 */ | |
1551 | |
1552 if (cfg.show_numbers_in_pl) | |
1553 numbers = g_strdup_printf("%d. ", playlist_get_position_nolock(playlist) + 1); | |
1554 else | |
1555 numbers = g_strdup(""); | |
1556 | |
1557 if (playlist->position->length != -1) | |
1558 length = g_strdup_printf(" (%d:%-2.2d)", | |
1559 playlist->position->length / 60000, | |
1560 (playlist->position->length / 1000) % 60); | |
1561 else | |
1562 length = g_strdup(""); | |
1563 | |
1564 PLAYLIST_UNLOCK(playlist->mutex); | |
1565 | |
1566 text = convert_title_text(g_strconcat(numbers, title, length, NULL)); | |
1567 | |
1568 g_free(numbers); | |
1569 g_free(title); | |
1570 g_free(length); | |
1571 | |
1572 return text; | |
1573 } | |
1574 | |
1575 gint | |
1576 playlist_get_current_length(Playlist * playlist) | |
1577 { | |
1578 gint len = 0; | |
1579 | |
1580 if (!playlist) | |
1581 return 0; | |
1582 | |
1583 if (playlist->position) | |
1584 len = playlist->position->length; | |
1585 | |
1586 return len; | |
1587 } | |
1588 | |
1589 gboolean | |
1590 playlist_save(Playlist * playlist, const gchar * filename) | |
1591 { | |
1592 PlaylistContainer *plc = NULL; | |
1593 gchar *ext; | |
1594 | |
1595 g_return_val_if_fail(playlist != NULL, FALSE); | |
1596 g_return_val_if_fail(filename != NULL, FALSE); | |
1597 | |
1598 ext = strrchr(filename, '.') + 1; | |
1599 | |
1600 playlist_set_current_name(playlist, filename); | |
1601 | |
1602 if ((plc = playlist_container_find(ext)) == NULL) | |
1603 return FALSE; | |
1604 | |
1605 if (plc->plc_write == NULL) | |
1606 return FALSE; | |
1607 | |
1608 plc->plc_write(filename, 0); | |
1609 | |
1610 return TRUE; | |
1611 } | |
1612 | |
1613 gboolean | |
1614 playlist_load(Playlist * playlist, const gchar * filename) | |
1615 { | |
1616 gboolean ret = FALSE; | |
1617 g_return_val_if_fail(playlist != NULL, FALSE); | |
1618 | |
1619 playlist->loading_playlist = TRUE; | |
1620 ret = playlist_load_ins(playlist, filename, -1); | |
1621 playlist->loading_playlist = FALSE; | |
1622 | |
1623 return ret; | |
1624 } | |
1625 | |
1626 void | |
1627 playlist_load_ins_file(Playlist *playlist, | |
1628 const gchar * filename_p, | |
1629 const gchar * playlist_name, gint pos, | |
1630 const gchar * title, gint len) | |
1631 { | |
1632 gchar *filename; | |
1633 gchar *tmp, *path; | |
2667
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1634 InputPlugin *dec = NULL; /* for decoder cache */ |
2313 | 1635 |
1636 g_return_if_fail(filename_p != NULL); | |
1637 g_return_if_fail(playlist != NULL); | |
1638 g_return_if_fail(playlist_name != NULL); | |
1639 | |
1640 filename = g_strchug(g_strdup(filename_p)); | |
1641 | |
1642 if(cfg.convert_slash) | |
1643 while ((tmp = strchr(filename, '\\')) != NULL) | |
1644 *tmp = '/'; | |
1645 | |
1646 if (filename[0] != '/' && !strstr(filename, "://")) { | |
1647 path = g_strdup(playlist_name); | |
1648 if ((tmp = strrchr(path, '/'))) | |
1649 *tmp = '\0'; | |
1650 else { | |
2667
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1651 if ((playlist->loading_playlist == TRUE || |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1652 cfg.playlist_detect == TRUE)) |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1653 dec = NULL; |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1654 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
|
1655 !str_has_prefix_nocase(filename, "https://")) |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1656 dec = input_check_file(filename, FALSE); |
2313 | 1657 |
1658 __playlist_ins_with_info(playlist, filename, pos, title, len, dec); | |
1659 return; | |
1660 } | |
1661 tmp = g_build_filename(path, filename, NULL); | |
1662 | |
2668
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
1663 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
|
1664 dec = NULL; |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
1665 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
|
1666 !str_has_prefix_nocase(tmp, "https://")) |
2313 | 1667 dec = input_check_file(tmp, FALSE); |
1668 | |
1669 __playlist_ins_with_info(playlist, tmp, pos, title, len, dec); | |
1670 g_free(tmp); | |
1671 g_free(path); | |
1672 } | |
1673 else | |
1674 { | |
2667
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1675 if ((playlist->loading_playlist == TRUE || |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1676 cfg.playlist_detect == TRUE)) |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1677 dec = NULL; |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1678 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
|
1679 !str_has_prefix_nocase(filename, "https://")) |
2313 | 1680 dec = input_check_file(filename, FALSE); |
1681 | |
1682 __playlist_ins_with_info(playlist, filename, pos, title, len, dec); | |
1683 } | |
1684 | |
1685 g_free(filename); | |
1686 } | |
1687 | |
1688 void | |
1689 playlist_load_ins_file_tuple(Playlist * playlist, | |
1690 const gchar * filename_p, | |
1691 const gchar * playlist_name, | |
1692 gint pos, | |
1693 TitleInput *tuple) | |
1694 { | |
1695 gchar *filename; | |
1696 gchar *tmp, *path; | |
2667
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1697 InputPlugin *dec = NULL; /* for decoder cache */ |
2313 | 1698 |
1699 g_return_if_fail(filename_p != NULL); | |
1700 g_return_if_fail(playlist_name != NULL); | |
1701 g_return_if_fail(playlist != NULL); | |
1702 | |
1703 filename = g_strchug(g_strdup(filename_p)); | |
1704 | |
1705 while ((tmp = strchr(filename, '\\')) != NULL) | |
1706 *tmp = '/'; | |
1707 | |
1708 if (filename[0] != '/' && !strstr(filename, "://")) { | |
1709 path = g_strdup(playlist_name); | |
1710 if ((tmp = strrchr(path, '/'))) | |
1711 *tmp = '\0'; | |
1712 else { | |
2667
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1713 if ((playlist->loading_playlist == TRUE || |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1714 cfg.playlist_detect == TRUE)) |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1715 dec = NULL; |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1716 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
|
1717 !str_has_prefix_nocase(filename, "https://")) |
2313 | 1718 dec = input_check_file(filename, FALSE); |
1719 | |
1720 __playlist_ins_with_info_tuple(playlist, filename, pos, tuple, dec); | |
1721 return; | |
1722 } | |
1723 tmp = g_build_filename(path, filename, NULL); | |
1724 | |
2667
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1725 if ((playlist->loading_playlist == TRUE || |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1726 cfg.playlist_detect == TRUE)) |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1727 dec = NULL; |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1728 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
|
1729 !str_has_prefix_nocase(filename, "https://")) |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1730 dec = input_check_file(filename, FALSE); |
2313 | 1731 |
1732 __playlist_ins_with_info_tuple(playlist, tmp, pos, tuple, dec); | |
1733 g_free(tmp); | |
1734 g_free(path); | |
1735 } | |
1736 else | |
1737 { | |
2667
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1738 if ((playlist->loading_playlist == TRUE || |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1739 cfg.playlist_detect == TRUE)) |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1740 dec = NULL; |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1741 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
|
1742 !str_has_prefix_nocase(filename, "https://")) |
8c56926de2ad
[svn] - probe remote sources (hardcoded to http://, https://) in the background.
nenolod
parents:
2637
diff
changeset
|
1743 dec = input_check_file(filename, FALSE); |
2313 | 1744 |
1745 __playlist_ins_with_info_tuple(playlist, filename, pos, tuple, dec); | |
1746 } | |
1747 | |
1748 g_free(filename); | |
1749 } | |
1750 | |
1751 static guint | |
1752 playlist_load_ins(Playlist * playlist, const gchar * filename, gint pos) | |
1753 { | |
1754 PlaylistContainer *plc; | |
1755 gchar *ext; | |
1756 | |
1757 g_return_val_if_fail(playlist != NULL, 0); | |
1758 g_return_val_if_fail(filename != NULL, 0); | |
1759 | |
1760 ext = strrchr(filename, '.') + 1; | |
1761 plc = playlist_container_find(ext); | |
1762 | |
1763 g_return_val_if_fail(plc != NULL, 0); | |
1764 g_return_val_if_fail(plc->plc_read != NULL, 0); | |
1765 | |
1766 plc->plc_read(filename, pos); | |
1767 | |
1768 playlist_generate_shuffle_list(playlist); | |
1769 playlistwin_update_list(playlist); | |
1770 | |
1771 return 1; | |
1772 } | |
1773 | |
1774 GList * | |
1775 get_playlist_nth(Playlist *playlist, guint nth) | |
1776 { | |
1777 g_warning("deprecated function get_playlist_nth() was called"); | |
1778 REQUIRE_LOCK(playlist->mutex); | |
1779 return g_list_nth(playlist->entries, nth); | |
1780 } | |
1781 | |
1782 gint | |
1783 playlist_get_position_nolock(Playlist *playlist) | |
1784 { | |
1785 if (playlist && playlist->position) | |
1786 return g_list_index(playlist->entries, playlist->position); | |
1787 return 0; | |
1788 } | |
1789 | |
1790 gint | |
1791 playlist_get_position(Playlist *playlist) | |
1792 { | |
1793 gint pos; | |
1794 | |
1795 PLAYLIST_LOCK(playlist->mutex); | |
1796 pos = playlist_get_position_nolock(playlist); | |
1797 PLAYLIST_UNLOCK(playlist->mutex); | |
1798 | |
1799 return pos; | |
1800 } | |
1801 | |
1802 gchar * | |
1803 playlist_get_filename(Playlist *playlist, guint pos) | |
1804 { | |
1805 gchar *filename; | |
1806 PlaylistEntry *entry; | |
1807 GList *node; | |
1808 | |
1809 if (!playlist) | |
1810 return NULL; | |
1811 | |
1812 PLAYLIST_LOCK(playlist->mutex); | |
1813 node = g_list_nth(playlist->entries, pos); | |
1814 if (!node) { | |
1815 PLAYLIST_UNLOCK(playlist->mutex); | |
1816 return NULL; | |
1817 } | |
1818 entry = node->data; | |
1819 | |
1820 filename = g_strdup(entry->filename); | |
1821 PLAYLIST_UNLOCK(playlist->mutex); | |
1822 | |
1823 return filename; | |
1824 } | |
1825 | |
1826 gchar * | |
1827 playlist_get_songtitle(Playlist *playlist, guint pos) | |
1828 { | |
1829 gchar *title = NULL; | |
1830 PlaylistEntry *entry; | |
1831 GList *node; | |
1832 | |
1833 if (!playlist) | |
1834 return NULL; | |
1835 | |
1836 PLAYLIST_LOCK(playlist->mutex); | |
1837 | |
1838 if (!(node = g_list_nth(playlist->entries, pos))) { | |
1839 PLAYLIST_UNLOCK(playlist->mutex); | |
1840 return NULL; | |
1841 } | |
1842 | |
1843 entry = node->data; | |
1844 | |
1845 /* FIXME: simplify this logic */ | |
1846 if ((entry->title == NULL && entry->length == -1) || | |
1847 (entry->tuple && entry->tuple->mtime != 0 && (entry->tuple->mtime == -1 || entry->tuple->mtime != playlist_get_mtime(entry->filename)))) | |
1848 { | |
1849 if (playlist_entry_get_info(entry)) | |
1850 title = entry->title; | |
1851 } | |
1852 else { | |
1853 title = entry->title; | |
1854 } | |
1855 | |
1856 PLAYLIST_UNLOCK(playlist->mutex); | |
1857 | |
1858 if (!title) { | |
1859 title = g_path_get_basename(entry->filename); | |
1860 return str_replace(title, filename_to_utf8(title)); | |
1861 } | |
1862 | |
1863 return str_to_utf8(title); | |
1864 } | |
1865 | |
1866 TitleInput * | |
1867 playlist_get_tuple(Playlist *playlist, guint pos) | |
1868 { | |
1869 PlaylistEntry *entry; | |
1870 TitleInput *tuple = NULL; | |
1871 GList *node; | |
1872 | |
1873 if (!playlist) | |
1874 return NULL; | |
1875 | |
1876 if (!(node = g_list_nth(playlist->entries, pos))) { | |
1877 return NULL; | |
1878 } | |
1879 | |
1880 entry = (PlaylistEntry *) node->data; | |
1881 | |
1882 tuple = entry->tuple; | |
1883 | |
1884 // if no tuple or tuple with old mtime, get new one. | |
1885 if (tuple == NULL || | |
1886 (entry->tuple && entry->tuple->mtime != 0 && (entry->tuple->mtime == -1 || entry->tuple->mtime != playlist_get_mtime(entry->filename)))) | |
1887 { | |
1888 playlist_entry_get_info(entry); | |
1889 tuple = entry->tuple; | |
1890 } | |
1891 | |
1892 return tuple; | |
1893 } | |
1894 | |
1895 gint | |
1896 playlist_get_songtime(Playlist *playlist, guint pos) | |
1897 { | |
1898 gint song_time = -1; | |
1899 PlaylistEntry *entry; | |
1900 GList *node; | |
1901 | |
1902 if (!playlist) | |
1903 return -1; | |
1904 | |
1905 if (!(node = g_list_nth(playlist->entries, pos))) { | |
1906 return -1; | |
1907 } | |
1908 | |
1909 entry = node->data; | |
1910 if (entry->tuple == NULL || | |
1911 (entry->tuple->mtime != 0 && (entry->tuple->mtime == -1 || entry->tuple->mtime != playlist_get_mtime(entry->filename)))) { | |
1912 | |
1913 if (playlist_entry_get_info(entry)) | |
1914 song_time = entry->length; | |
1915 } | |
1916 else { | |
1917 song_time = entry->length; | |
1918 } | |
1919 | |
1920 return song_time; | |
1921 } | |
1922 | |
1923 static gint | |
1924 playlist_compare_track(PlaylistEntry * a, | |
1925 PlaylistEntry * b) | |
1926 { | |
1927 g_return_val_if_fail(a != NULL, 0); | |
1928 g_return_val_if_fail(b != NULL, 0); | |
1929 | |
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
|
1930 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
|
1931 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
|
1932 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
|
1933 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
|
1934 |
2313 | 1935 g_return_val_if_fail(a->tuple != NULL, 0); |
1936 g_return_val_if_fail(b->tuple != NULL, 0); | |
1937 | |
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
|
1938 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
|
1939 a->tuple->track_number - b->tuple->track_number : 0); |
2313 | 1940 } |
1941 | |
1942 static gint | |
1943 playlist_compare_playlist(PlaylistEntry * a, | |
1944 PlaylistEntry * b) | |
1945 { | |
1946 const gchar *a_title = NULL, *b_title = NULL; | |
1947 | |
1948 g_return_val_if_fail(a != NULL, 0); | |
1949 g_return_val_if_fail(b != NULL, 0); | |
1950 | |
1951 if (a->title != NULL) | |
1952 a_title = a->title; | |
1953 else { | |
1954 if (strrchr(a->filename, '/')) | |
1955 a_title = strrchr(a->filename, '/') + 1; | |
1956 else | |
1957 a_title = a->filename; | |
1958 } | |
1959 | |
1960 if (b->title != NULL) | |
1961 b_title = b->title; | |
1962 else { | |
1963 if (strrchr(a->filename, '/')) | |
1964 b_title = strrchr(b->filename, '/') + 1; | |
1965 else | |
1966 b_title = b->filename; | |
1967 } | |
1968 | |
1969 return strcasecmp(a_title, b_title); | |
1970 } | |
1971 | |
1972 static gint | |
1973 playlist_compare_title(PlaylistEntry * a, | |
1974 PlaylistEntry * b) | |
1975 { | |
1976 const gchar *a_title = NULL, *b_title = NULL; | |
1977 | |
1978 g_return_val_if_fail(a != NULL, 0); | |
1979 g_return_val_if_fail(b != NULL, 0); | |
1980 | |
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
|
1981 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
|
1982 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
|
1983 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
|
1984 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
|
1985 |
2313 | 1986 if (a->tuple != NULL && a->tuple->track_name != NULL) |
1987 a_title = a->tuple->track_name; | |
1988 if (b->tuple != NULL && b->tuple->track_name != NULL) | |
1989 b_title = b->tuple->track_name; | |
1990 | |
1991 if (a_title != NULL && b_title != NULL) | |
1992 return strcasecmp(a_title, b_title); | |
1993 | |
1994 if (a->title != NULL) | |
1995 a_title = a->title; | |
1996 else { | |
1997 if (strrchr(a->filename, '/')) | |
1998 a_title = strrchr(a->filename, '/') + 1; | |
1999 else | |
2000 a_title = a->filename; | |
2001 } | |
2002 | |
2003 if (b->title != NULL) | |
2004 b_title = b->title; | |
2005 else { | |
2006 if (strrchr(a->filename, '/')) | |
2007 b_title = strrchr(b->filename, '/') + 1; | |
2008 else | |
2009 b_title = b->filename; | |
2010 } | |
2011 | |
2012 return strcasecmp(a_title, b_title); | |
2013 } | |
2014 | |
2015 static gint | |
2016 playlist_compare_artist(PlaylistEntry * a, | |
2017 PlaylistEntry * b) | |
2018 { | |
2019 const gchar *a_artist = NULL, *b_artist = NULL; | |
2020 | |
2021 g_return_val_if_fail(a != NULL, 0); | |
2022 g_return_val_if_fail(b != NULL, 0); | |
2023 | |
2024 if (a->tuple != NULL) | |
2025 playlist_entry_get_info(a); | |
2026 | |
2027 if (b->tuple != NULL) | |
2028 playlist_entry_get_info(b); | |
2029 | |
2030 if (a->tuple != NULL && a->tuple->performer != NULL) | |
2031 a_artist = a->tuple->performer; | |
2032 if (b->tuple != NULL && b->tuple->performer != NULL) | |
2033 b_artist = b->tuple->performer; | |
2034 | |
2035 if (a_artist != NULL && b_artist != NULL) | |
2036 return strcasecmp(a_artist, b_artist); | |
2037 | |
2038 return 0; | |
2039 } | |
2040 | |
2041 static gint | |
2042 playlist_compare_filename(PlaylistEntry * a, | |
2043 PlaylistEntry * b) | |
2044 { | |
2045 gchar *a_filename, *b_filename; | |
2046 | |
2047 g_return_val_if_fail(a != NULL, 0); | |
2048 g_return_val_if_fail(b != NULL, 0); | |
2049 | |
2050 if (strrchr(a->filename, '/')) | |
2051 a_filename = strrchr(a->filename, '/') + 1; | |
2052 else | |
2053 a_filename = a->filename; | |
2054 | |
2055 if (strrchr(b->filename, '/')) | |
2056 b_filename = strrchr(b->filename, '/') + 1; | |
2057 else | |
2058 b_filename = b->filename; | |
2059 | |
2060 | |
2061 return strcasecmp(a_filename, b_filename); | |
2062 } | |
2063 | |
2064 static gint | |
2065 path_compare(const gchar * a, const gchar * b) | |
2066 { | |
2067 gchar *posa, *posb; | |
2068 gint len, ret; | |
2069 | |
2070 posa = strrchr(a, '/'); | |
2071 posb = strrchr(b, '/'); | |
2072 | |
2073 /* | |
2074 * Sort directories before files | |
2075 */ | |
2076 if (posa && posb && (posa - a != posb - b)) { | |
2077 if (posa - a > posb - b) { | |
2078 len = posb - b; | |
2079 ret = -1; | |
2080 } | |
2081 else { | |
2082 len = posa - a; | |
2083 ret = 1; | |
2084 } | |
2085 if (!strncasecmp(a, b, len)) | |
2086 return ret; | |
2087 } | |
2088 return strcasecmp(a, b); | |
2089 } | |
2090 | |
2091 static gint | |
2092 playlist_compare_path(PlaylistEntry * a, | |
2093 PlaylistEntry * b) | |
2094 { | |
2095 return path_compare(a->filename, b->filename); | |
2096 } | |
2097 | |
2098 | |
2099 static time_t | |
2100 playlist_get_mtime(const gchar *filename) | |
2101 { | |
2102 struct stat buf; | |
2103 gint rv; | |
2104 | |
2105 rv = stat(filename, &buf); | |
2106 | |
2107 if (rv == 0) { | |
2108 return buf.st_mtime; | |
2109 } else { | |
2110 return 0; //error | |
2111 } | |
2112 } | |
2113 | |
2114 | |
2115 static gint | |
2116 playlist_compare_date(PlaylistEntry * a, | |
2117 PlaylistEntry * b) | |
2118 { | |
2119 struct stat buf; | |
2120 time_t modtime; | |
2121 | |
2122 gint rv; | |
2123 | |
2124 | |
2125 rv = stat(a->filename, &buf); | |
2126 | |
2127 if (rv == 0) { | |
2128 modtime = buf.st_mtime; | |
2129 rv = stat(b->filename, &buf); | |
2130 | |
2131 if (stat(b->filename, &buf) == 0) { | |
2132 if (buf.st_mtime == modtime) | |
2133 return 0; | |
2134 else | |
2135 return (buf.st_mtime - modtime) > 0 ? -1 : 1; | |
2136 } | |
2137 else | |
2138 return -1; | |
2139 } | |
2140 else if (!lstat(b->filename, &buf)) | |
2141 return 1; | |
2142 else | |
2143 return playlist_compare_filename(a, b); | |
2144 } | |
2145 | |
2146 | |
2147 void | |
2148 playlist_sort(Playlist *playlist, PlaylistSortType type) | |
2149 { | |
2150 playlist_remove_dead_files(playlist); | |
2151 PLAYLIST_LOCK(playlist->mutex); | |
2152 playlist->entries = | |
2153 g_list_sort(playlist->entries, | |
2154 (GCompareFunc) playlist_compare_func_table[type]); | |
2155 PLAYLIST_UNLOCK(playlist->mutex); | |
2156 } | |
2157 | |
2158 static GList * | |
2159 playlist_sort_selected_generic(GList * list, GCompareFunc cmpfunc) | |
2160 { | |
2161 GList *list1, *list2; | |
2162 GList *tmp_list = NULL; | |
2163 GList *index_list = NULL; | |
2164 | |
2165 /* | |
2166 * We take all the selected entries out of the playlist, | |
2167 * sorts them, and then put them back in again. | |
2168 */ | |
2169 | |
2170 list1 = g_list_last(list); | |
2171 | |
2172 while (list1) { | |
2173 list2 = g_list_previous(list1); | |
2174 if (PLAYLIST_ENTRY(list1->data)->selected) { | |
2175 gpointer idx; | |
2176 idx = GINT_TO_POINTER(g_list_position(list, list1)); | |
2177 index_list = g_list_prepend(index_list, idx); | |
2178 list = g_list_remove_link(list, list1); | |
2179 tmp_list = g_list_concat(list1, tmp_list); | |
2180 } | |
2181 list1 = list2; | |
2182 } | |
2183 | |
2184 tmp_list = g_list_sort(tmp_list, cmpfunc); | |
2185 list1 = tmp_list; | |
2186 list2 = index_list; | |
2187 | |
2188 while (list2) { | |
2189 if (!list1) { | |
2190 g_critical(G_STRLOC ": Error during list sorting. " | |
2191 "Possibly dropped some playlist-entries."); | |
2192 break; | |
2193 } | |
2194 | |
2195 list = g_list_insert(list, list1->data, GPOINTER_TO_INT(list2->data)); | |
2196 | |
2197 list2 = g_list_next(list2); | |
2198 list1 = g_list_next(list1); | |
2199 } | |
2200 | |
2201 g_list_free(index_list); | |
2202 g_list_free(tmp_list); | |
2203 | |
2204 return list; | |
2205 } | |
2206 | |
2207 void | |
2208 playlist_sort_selected(Playlist *playlist, PlaylistSortType type) | |
2209 { | |
2210 PLAYLIST_LOCK(playlist->mutex); | |
2211 playlist->entries = playlist_sort_selected_generic(playlist->entries, (GCompareFunc) | |
2212 playlist_compare_func_table | |
2213 [type]); | |
2214 PLAYLIST_UNLOCK(playlist->mutex); | |
2215 } | |
2216 | |
2217 void | |
2218 playlist_reverse(Playlist *playlist) | |
2219 { | |
2220 PLAYLIST_LOCK(playlist->mutex); | |
2221 playlist->entries = g_list_reverse(playlist->entries); | |
2222 PLAYLIST_UNLOCK(playlist->mutex); | |
2223 } | |
2224 | |
2225 static GList * | |
2226 playlist_shuffle_list(Playlist *playlist, GList * list) | |
2227 { | |
2228 /* | |
2229 * Note that this doesn't make a copy of the original list. | |
2230 * The pointer to the original list is not valid after this | |
2231 * fuction is run. | |
2232 */ | |
2233 gint len = g_list_length(list); | |
2234 gint i, j; | |
2235 GList *node, **ptrs; | |
2236 | |
2237 if (!playlist) | |
2238 return NULL; | |
2239 | |
2240 REQUIRE_LOCK(playlist->mutex); | |
2241 | |
2242 if (!len) | |
2243 return NULL; | |
2244 | |
2245 ptrs = g_new(GList *, len); | |
2246 | |
2247 for (node = list, i = 0; i < len; node = g_list_next(node), i++) | |
2248 ptrs[i] = node; | |
2249 | |
2250 j = g_random_int_range(0, len); | |
2251 list = ptrs[j]; | |
2252 ptrs[j]->next = NULL; | |
2253 ptrs[j] = ptrs[0]; | |
2254 | |
2255 for (i = 1; i < len; i++) { | |
2256 j = g_random_int_range(0, len - i); | |
2257 list->prev = ptrs[i + j]; | |
2258 ptrs[i + j]->next = list; | |
2259 list = ptrs[i + j]; | |
2260 ptrs[i + j] = ptrs[i]; | |
2261 } | |
2262 list->prev = NULL; | |
2263 | |
2264 g_free(ptrs); | |
2265 | |
2266 return list; | |
2267 } | |
2268 | |
2269 void | |
2270 playlist_random(Playlist *playlist) | |
2271 { | |
2272 PLAYLIST_LOCK(playlist->mutex); | |
2273 playlist->entries = playlist_shuffle_list(playlist, playlist->entries); | |
2274 PLAYLIST_UNLOCK(playlist->mutex); | |
2275 } | |
2276 | |
2277 GList * | |
2278 playlist_get_selected(Playlist *playlist) | |
2279 { | |
2280 GList *node, *list = NULL; | |
2281 gint i = 0; | |
2282 | |
2283 PLAYLIST_LOCK(playlist->mutex); | |
2284 for (node = playlist->entries; node; node = g_list_next(node), i++) { | |
2285 PlaylistEntry *entry = node->data; | |
2286 if (entry->selected) | |
2287 list = g_list_prepend(list, GINT_TO_POINTER(i)); | |
2288 } | |
2289 PLAYLIST_UNLOCK(playlist->mutex); | |
2290 return g_list_reverse(list); | |
2291 } | |
2292 | |
2293 void | |
2294 playlist_clear_selected(Playlist *playlist) | |
2295 { | |
2296 GList *node = NULL; | |
2297 gint i = 0; | |
2298 | |
2299 PLAYLIST_LOCK(playlist->mutex); | |
2300 for (node = playlist->entries; node; node = g_list_next(node), i++) { | |
2301 PLAYLIST_ENTRY(node->data)->selected = FALSE; | |
2302 } | |
2303 PLAYLIST_UNLOCK(playlist->mutex); | |
2304 playlist_recalc_total_time(playlist); | |
2305 playlist_manager_update(); | |
2306 } | |
2307 | |
2308 gint | |
2309 playlist_get_num_selected(Playlist *playlist) | |
2310 { | |
2311 GList *node; | |
2312 gint num = 0; | |
2313 | |
2314 PLAYLIST_LOCK(playlist->mutex); | |
2315 for (node = playlist->entries; node; node = g_list_next(node)) { | |
2316 PlaylistEntry *entry = node->data; | |
2317 if (entry->selected) | |
2318 num++; | |
2319 } | |
2320 PLAYLIST_UNLOCK(playlist->mutex); | |
2321 return num; | |
2322 } | |
2323 | |
2324 | |
2325 static void | |
2326 playlist_generate_shuffle_list(Playlist *playlist) | |
2327 { | |
2328 PLAYLIST_LOCK(playlist->mutex); | |
2329 playlist_generate_shuffle_list_nolock(playlist); | |
2330 PLAYLIST_UNLOCK(playlist->mutex); | |
2331 } | |
2332 | |
2333 static void | |
2334 playlist_generate_shuffle_list_nolock(Playlist *playlist) | |
2335 { | |
2336 GList *node; | |
2337 gint numsongs; | |
2338 | |
2339 if (!cfg.shuffle || !playlist) | |
2340 return; | |
2341 | |
2342 REQUIRE_LOCK(playlist->mutex); | |
2343 | |
2344 if (playlist->shuffle) { | |
2345 g_list_free(playlist->shuffle); | |
2346 playlist->shuffle = NULL; | |
2347 } | |
2348 | |
2349 playlist->shuffle = playlist_shuffle_list(playlist, g_list_copy(playlist->entries)); | |
2350 numsongs = g_list_length(playlist->shuffle); | |
2351 | |
2352 if (playlist->position) { | |
2353 gint i = g_list_index(playlist->shuffle, playlist->position); | |
2354 node = g_list_nth(playlist->shuffle, i); | |
2355 playlist->shuffle = g_list_remove_link(playlist->shuffle, node); | |
2356 playlist->shuffle = g_list_prepend(playlist->shuffle, node->data); | |
2357 } | |
2358 } | |
2359 | |
2360 void | |
2361 playlist_fileinfo(Playlist *playlist, guint pos) | |
2362 { | |
2363 gchar *path = NULL; | |
2364 GList *node; | |
2365 PlaylistEntry *entry = NULL; | |
2366 TitleInput *tuple = NULL; | |
2367 | |
2368 PLAYLIST_LOCK(playlist->mutex); | |
2369 | |
2370 if ((node = g_list_nth(playlist->entries, pos))) | |
2371 { | |
2372 entry = node->data; | |
2373 tuple = entry->tuple; | |
2374 path = g_strdup(entry->filename); | |
2375 } | |
2376 | |
2377 PLAYLIST_UNLOCK(playlist->mutex); | |
2378 | |
2379 /* No tuple? Try to set this entry up properly. --nenolod */ | |
2380 if (entry->tuple == NULL || entry->tuple->mtime == -1 || | |
2381 entry->tuple->mtime == 0 || entry->tuple->mtime != playlist_get_mtime(entry->filename)) | |
2382 { | |
2383 playlist_entry_get_info(entry); | |
2384 tuple = entry->tuple; | |
2385 } | |
2386 | |
2387 if (tuple != NULL) | |
2388 { | |
2389 if (entry->decoder == NULL) | |
2390 entry->decoder = input_check_file(entry->filename, FALSE); /* try to find a decoder */ | |
2391 | |
2392 if (entry->decoder != NULL && entry->decoder->file_info_box == NULL) | |
2393 fileinfo_show_for_tuple(tuple); | |
2394 else if (entry->decoder != NULL && entry->decoder->file_info_box != NULL) | |
2395 entry->decoder->file_info_box(path); | |
2396 else | |
2397 fileinfo_show_for_path(path); | |
2398 g_free(path); | |
2399 } | |
2400 else if (path != NULL) | |
2401 { | |
2402 if (entry != NULL && entry->decoder != NULL && entry->decoder->file_info_box != NULL) | |
2403 entry->decoder->file_info_box(path); | |
2404 else | |
2405 fileinfo_show_for_path(path); | |
2406 g_free(path); | |
2407 } | |
2408 } | |
2409 | |
2410 void | |
2411 playlist_fileinfo_current(Playlist *playlist) | |
2412 { | |
2413 gchar *path = NULL; | |
2414 TitleInput *tuple = NULL; | |
2415 | |
2416 PLAYLIST_LOCK(playlist->mutex); | |
2417 | |
2418 if (playlist->entries && playlist->position) | |
2419 { | |
2420 path = g_strdup(playlist->position->filename); | |
2421 if (( playlist->position->tuple == NULL ) || ( playlist->position->decoder == NULL )) | |
2422 playlist_entry_get_info(playlist->position); | |
2423 tuple = playlist->position->tuple; | |
2424 } | |
2425 | |
2426 PLAYLIST_UNLOCK(playlist->mutex); | |
2427 | |
2428 if (tuple != NULL) | |
2429 { | |
2430 if (playlist->position->decoder != NULL && playlist->position->decoder->file_info_box == NULL) | |
2431 fileinfo_show_for_tuple(tuple); | |
2432 else if (playlist->position->decoder != NULL && playlist->position->decoder->file_info_box != NULL) | |
2433 playlist->position->decoder->file_info_box(path); | |
2434 else | |
2435 fileinfo_show_for_path(path); | |
2436 g_free(path); | |
2437 } | |
2438 else if (path != NULL) | |
2439 { | |
2440 if (playlist->position != NULL && playlist->position->decoder != NULL && playlist->position->decoder->file_info_box != NULL) | |
2441 playlist->position->decoder->file_info_box(path); | |
2442 else | |
2443 fileinfo_show_for_path(path); | |
2444 g_free(path); | |
2445 } | |
2446 } | |
2447 | |
2448 | |
2449 static gboolean | |
2450 playlist_get_info_is_going(void) | |
2451 { | |
2452 gboolean result; | |
2453 | |
2454 G_LOCK(playlist_get_info_going); | |
2455 result = playlist_get_info_going; | |
2456 G_UNLOCK(playlist_get_info_going); | |
2457 | |
2458 return result; | |
2459 } | |
2460 | |
2461 static gpointer | |
2462 playlist_get_info_func(gpointer arg) | |
2463 { | |
2464 GList *node; | |
2465 gboolean update_playlistwin = FALSE; | |
2466 gboolean update_mainwin = FALSE; | |
2467 | |
2468 while (playlist_get_info_is_going()) { | |
2469 PlaylistEntry *entry; | |
2470 Playlist *playlist = playlist_get_active(); | |
2471 | |
2472 // on_load | |
2473 if (cfg.use_pl_metadata && | |
2474 cfg.get_info_on_load && | |
2475 playlist_get_info_scan_active) { | |
2476 | |
2477 for (node = playlist->entries; node; node = g_list_next(node)) { | |
2478 entry = node->data; | |
2479 | |
2636 | 2480 if(playlist->attribute & PLAYLIST_STATIC || |
2481 (entry->tuple && entry->tuple->length > -1 && entry->tuple->mtime != -1)) { | |
2313 | 2482 update_playlistwin = TRUE; |
2483 continue; | |
2484 } | |
2485 | |
2486 if (!playlist_entry_get_info(entry)) { | |
2487 if (g_list_index(playlist->entries, entry) == -1) | |
2488 /* Entry disappeared while we looked it up. | |
2489 Restart. */ | |
2490 node = playlist->entries; | |
2491 } | |
2492 else if ((entry->tuple != NULL || entry->title != NULL) && entry->length != -1) { | |
2493 update_playlistwin = TRUE; | |
2494 if (entry == playlist->position) | |
2495 update_mainwin = TRUE; | |
2496 break; | |
2497 } | |
2498 } | |
2499 | |
2500 if (!node) { | |
2501 g_mutex_lock(mutex_scan); | |
2502 playlist_get_info_scan_active = FALSE; | |
2503 g_mutex_unlock(mutex_scan); | |
2504 } | |
2505 } // on_load | |
2506 | |
2507 // on_demand | |
2508 else if (!cfg.get_info_on_load && | |
2509 cfg.get_info_on_demand && | |
2510 cfg.playlist_visible && | |
2511 !cfg.playlist_shaded && | |
2512 cfg.use_pl_metadata) { | |
2513 | |
2514 g_mutex_lock(mutex_scan); | |
2515 playlist_get_info_scan_active = FALSE; | |
2516 g_mutex_unlock(mutex_scan); | |
2517 | |
2668
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2518 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
|
2519 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
|
2520 node = g_list_next(node)) { |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2521 |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2522 entry = node->data; |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2523 |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2524 if(playlist->attribute & PLAYLIST_STATIC || |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2525 (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
|
2526 update_playlistwin = TRUE; |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2527 continue; |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2528 } |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2529 |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2530 if (!playlist_entry_get_info(entry)) { |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2531 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
|
2532 /* Entry disapeared while we |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2533 looked it up. Restart. */ |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2534 node = g_list_nth(playlist->entries, |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2535 playlistwin_get_toprow()); |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2536 } |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2537 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
|
2538 update_playlistwin = TRUE; |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2539 if (entry == playlist->position) |
d5da5d37ec8b
[svn] - unlock many playlist operations that do not really need locking
nenolod
parents:
2667
diff
changeset
|
2540 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
|
2541 // no need for break here since this iteration is very short. |
2313 | 2542 } |
2543 } | |
2544 } // on_demand | |
2545 else if (cfg.get_info_on_demand && | |
2546 (!cfg.playlist_visible || cfg.playlist_shaded | |
2547 || !cfg.use_pl_metadata)) | |
2548 { | |
2549 g_mutex_lock(mutex_scan); | |
2550 playlist_get_info_scan_active = FALSE; | |
2551 g_mutex_unlock(mutex_scan); | |
2552 } | |
2553 else /* not on_demand and not on_load... | |
2554 NOTE: this shouldn't happen anymore, sanity check in bmp_config_load now */ | |
2555 { | |
2556 g_mutex_lock(mutex_scan); | |
2557 playlist_get_info_scan_active = FALSE; | |
2558 g_mutex_unlock(mutex_scan); | |
2559 } | |
2560 | |
2561 if (update_playlistwin) { | |
2562 playlistwin_update_list(playlist); | |
2563 update_playlistwin = FALSE; | |
2564 } | |
2565 | |
2566 if (update_mainwin) { | |
2567 mainwin_set_info_text(); | |
2568 update_mainwin = FALSE; | |
2569 } | |
2570 | |
2571 if (playlist_get_info_scan_active) { | |
2572 continue; | |
2573 } | |
2574 | |
2575 g_mutex_lock(mutex_scan); | |
2576 g_cond_wait(cond_scan, mutex_scan); | |
2577 g_mutex_unlock(mutex_scan); | |
2578 | |
2579 } // while | |
2580 | |
2581 g_thread_exit(NULL); | |
2582 return NULL; | |
2583 } | |
2584 | |
2585 void | |
2586 playlist_start_get_info_thread(void) | |
2587 { | |
2588 G_LOCK(playlist_get_info_going); | |
2589 playlist_get_info_going = TRUE; | |
2590 G_UNLOCK(playlist_get_info_going); | |
2591 | |
2592 playlist_get_info_thread = g_thread_create(playlist_get_info_func, | |
2593 NULL, TRUE, NULL); | |
2594 } | |
2595 | |
2596 void | |
2597 playlist_stop_get_info_thread(void) | |
2598 { | |
2599 G_LOCK(playlist_get_info_going); | |
2600 playlist_get_info_going = FALSE; | |
2601 G_UNLOCK(playlist_get_info_going); | |
2602 | |
2603 g_cond_broadcast(cond_scan); | |
2604 g_thread_join(playlist_get_info_thread); | |
2605 } | |
2606 | |
2607 void | |
2608 playlist_start_get_info_scan(void) | |
2609 { | |
2610 g_mutex_lock(mutex_scan); | |
2611 playlist_get_info_scan_active = TRUE; | |
2612 g_mutex_unlock(mutex_scan); | |
2613 g_cond_signal(cond_scan); | |
2614 } | |
2615 | |
2616 void | |
2617 playlist_remove_dead_files(Playlist *playlist) | |
2618 { | |
2619 GList *node, *next_node; | |
2620 | |
2621 PLAYLIST_LOCK(playlist->mutex); | |
2622 | |
2623 for (node = playlist->entries; node; node = next_node) { | |
2624 PlaylistEntry *entry = PLAYLIST_ENTRY(node->data); | |
2625 next_node = g_list_next(node); | |
2626 | |
2627 if (!entry || !entry->filename) { | |
2628 g_message(G_STRLOC ": Playlist entry is invalid!"); | |
2629 continue; | |
2630 } | |
2631 | |
2632 /* FIXME: What about 'file:///'? */ | |
2633 /* Don't kill URLs */ | |
2634 if (strstr(entry->filename, "://")) | |
2635 continue; | |
2636 | |
2637 /* FIXME: Should test for readability */ | |
2638 if (vfs_file_test(entry->filename, G_FILE_TEST_EXISTS)) | |
2639 continue; | |
2640 | |
2641 if (entry == playlist->position) { | |
2642 /* Don't remove the currently playing song */ | |
2643 if (playback_get_playing()) | |
2644 continue; | |
2645 | |
2646 if (next_node) | |
2647 playlist->position = PLAYLIST_ENTRY(next_node->data); | |
2648 else | |
2649 playlist->position = NULL; | |
2650 } | |
2651 | |
2652 playlist_entry_free(entry); | |
2653 playlist->entries = g_list_delete_link(playlist->entries, node); | |
2654 } | |
2655 | |
2656 PLAYLIST_UNLOCK(playlist->mutex); | |
2657 | |
2658 playlist_generate_shuffle_list(playlist); | |
2659 playlistwin_update_list(playlist); | |
2660 playlist_recalc_total_time(playlist); | |
2661 playlist_manager_update(); | |
2662 } | |
2663 | |
2664 | |
2665 static gint | |
2666 playlist_dupscmp_title(PlaylistEntry * a, | |
2667 PlaylistEntry * b) | |
2668 { | |
2669 const gchar *a_title, *b_title; | |
2670 | |
2671 g_return_val_if_fail(a != NULL, 0); | |
2672 g_return_val_if_fail(b != NULL, 0); | |
2673 | |
2674 if (a->title) | |
2675 a_title = a->title; | |
2676 else { | |
2677 if (strrchr(a->filename, '/')) | |
2678 a_title = strrchr(a->filename, '/') + 1; | |
2679 else | |
2680 a_title = a->filename; | |
2681 } | |
2682 | |
2683 if (b->title) | |
2684 b_title = b->title; | |
2685 else { | |
2686 if (strrchr(a->filename, '/')) | |
2687 b_title = strrchr(b->filename, '/') + 1; | |
2688 else | |
2689 b_title = b->filename; | |
2690 } | |
2691 | |
2692 return strcmp(a_title, b_title); | |
2693 } | |
2694 | |
2695 static gint | |
2696 playlist_dupscmp_filename(PlaylistEntry * a, | |
2697 PlaylistEntry * b ) | |
2698 { | |
2699 gchar *a_filename, *b_filename; | |
2700 | |
2701 g_return_val_if_fail(a != NULL, 0); | |
2702 g_return_val_if_fail(b != NULL, 0); | |
2703 | |
2704 if (strrchr(a->filename, '/')) | |
2705 a_filename = strrchr(a->filename, '/') + 1; | |
2706 else | |
2707 a_filename = a->filename; | |
2708 | |
2709 if (strrchr(b->filename, '/')) | |
2710 b_filename = strrchr(b->filename, '/') + 1; | |
2711 else | |
2712 b_filename = b->filename; | |
2713 | |
2714 return strcmp(a_filename, b_filename); | |
2715 } | |
2716 | |
2717 static gint | |
2718 playlist_dupscmp_path(PlaylistEntry * a, | |
2719 PlaylistEntry * b) | |
2720 { | |
2721 /* simply compare the entire filename string */ | |
2722 return strcmp(a->filename, b->filename); | |
2723 } | |
2724 | |
2725 void | |
2726 playlist_remove_duplicates(Playlist *playlist, PlaylistDupsType type) | |
2727 { | |
2728 GList *node, *next_node; | |
2729 GList *node_cmp, *next_node_cmp; | |
2730 gint (*dups_compare_func)(PlaylistEntry * , PlaylistEntry *); | |
2731 | |
2732 switch ( type ) | |
2733 { | |
2734 case PLAYLIST_DUPS_TITLE: | |
2735 dups_compare_func = playlist_dupscmp_title; | |
2736 break; | |
2737 case PLAYLIST_DUPS_PATH: | |
2738 dups_compare_func = playlist_dupscmp_path; | |
2739 break; | |
2740 case PLAYLIST_DUPS_FILENAME: | |
2741 default: | |
2742 dups_compare_func = playlist_dupscmp_filename; | |
2743 break; | |
2744 } | |
2745 | |
2746 PLAYLIST_LOCK(playlist->mutex); | |
2747 | |
2748 for (node = playlist->entries; node; node = next_node) { | |
2749 PlaylistEntry *entry = PLAYLIST_ENTRY(node->data); | |
2750 next_node = g_list_next(node); | |
2751 | |
2752 if (!entry || !entry->filename) { | |
2753 g_message(G_STRLOC ": Playlist entry is invalid!"); | |
2754 continue; | |
2755 } | |
2756 | |
2757 for (node_cmp = next_node; node_cmp; node_cmp = next_node_cmp) { | |
2758 PlaylistEntry *entry_cmp = PLAYLIST_ENTRY(node_cmp->data); | |
2759 next_node_cmp = g_list_next(node_cmp); | |
2760 | |
2761 if (!entry_cmp || !entry_cmp->filename) { | |
2762 g_message(G_STRLOC ": Playlist entry is invalid!"); | |
2763 continue; | |
2764 } | |
2765 | |
2766 /* compare using the chosen dups_compare_func */ | |
2767 if ( !dups_compare_func( entry , entry_cmp ) ) { | |
2768 | |
2769 if (entry_cmp == playlist->position) { | |
2770 /* Don't remove the currently playing song */ | |
2771 if (playback_get_playing()) | |
2772 continue; | |
2773 | |
2774 if (next_node_cmp) | |
2775 playlist->position = PLAYLIST_ENTRY(next_node_cmp->data); | |
2776 else | |
2777 playlist->position = NULL; | |
2778 } | |
2779 | |
2780 /* check if this was the next item of the external | |
2781 loop; if true, replace it with the next of the next*/ | |
2782 if ( node_cmp == next_node ) | |
2783 next_node = g_list_next(next_node); | |
2784 | |
2785 playlist_entry_free(entry_cmp); | |
2786 playlist->entries = g_list_delete_link(playlist->entries, node_cmp); | |
2787 } | |
2788 } | |
2789 } | |
2790 | |
2791 PLAYLIST_UNLOCK(playlist->mutex); | |
2792 | |
2793 playlistwin_update_list(playlist); | |
2794 playlist_recalc_total_time(playlist); | |
2795 | |
2796 playlist_manager_update(); | |
2797 } | |
2798 | |
2799 void | |
2800 playlist_get_total_time(Playlist * playlist, | |
2801 gulong * total_time, | |
2802 gulong * selection_time, | |
2803 gboolean * total_more, | |
2804 gboolean * selection_more) | |
2805 { | |
2806 PLAYLIST_LOCK(playlist->mutex); | |
2807 *total_time = playlist->pl_total_time; | |
2808 *selection_time = playlist->pl_selection_time; | |
2809 *total_more = playlist->pl_total_more; | |
2810 *selection_more = playlist->pl_selection_more; | |
2811 PLAYLIST_UNLOCK(playlist->mutex); | |
2812 } | |
2813 | |
2814 | |
2815 static void | |
2816 playlist_recalc_total_time_nolock(Playlist *playlist) | |
2817 { | |
2818 GList *list; | |
2819 PlaylistEntry *entry; | |
2820 | |
2821 REQUIRE_LOCK(playlist->mutex); | |
2822 | |
2823 playlist->pl_total_time = 0; | |
2824 playlist->pl_selection_time = 0; | |
2825 playlist->pl_total_more = FALSE; | |
2826 playlist->pl_selection_more = FALSE; | |
2827 | |
2828 for (list = playlist->entries; list; list = g_list_next(list)) { | |
2829 entry = list->data; | |
2830 | |
2831 if (entry->length != -1) | |
2832 playlist->pl_total_time += entry->length / 1000; | |
2833 else | |
2834 playlist->pl_total_more = TRUE; | |
2835 | |
2836 if (entry->selected) { | |
2837 if (entry->length != -1) | |
2838 playlist->pl_selection_time += entry->length / 1000; | |
2839 else | |
2840 playlist->pl_selection_more = TRUE; | |
2841 } | |
2842 } | |
2843 } | |
2844 | |
2845 static void | |
2846 playlist_recalc_total_time(Playlist *playlist) | |
2847 { | |
2848 PLAYLIST_LOCK(playlist->mutex); | |
2849 playlist_recalc_total_time_nolock(playlist); | |
2850 PLAYLIST_UNLOCK(playlist->mutex); | |
2851 } | |
2852 | |
2853 gint | |
2854 playlist_select_search( Playlist *playlist , TitleInput *tuple , gint action ) | |
2855 { | |
2856 GList *entry_list = NULL, *found_list = NULL, *sel_list = NULL; | |
2857 gboolean is_first_search = TRUE; | |
2858 gint num_of_entries_found = 0; | |
2859 | |
2860 #if defined(USE_REGEX_ONIGURUMA) | |
2861 /* set encoding for Oniguruma regex to UTF-8 */ | |
2862 reg_set_encoding( REG_POSIX_ENCODING_UTF8 ); | |
2863 onig_set_default_syntax( ONIG_SYNTAX_POSIX_BASIC ); | |
2864 #endif | |
2865 | |
2866 PLAYLIST_LOCK(playlist->mutex); | |
2867 | |
2868 if ( tuple->track_name != NULL ) | |
2869 { | |
2870 /* match by track_name */ | |
2871 const gchar *regex_pattern = tuple->track_name; | |
2872 regex_t regex; | |
2873 #if defined(USE_REGEX_PCRE) | |
2874 if ( regcomp( ®ex , regex_pattern , REG_NOSUB | REG_ICASE | REG_UTF8 ) == 0 ) | |
2875 #else | |
2876 if ( regcomp( ®ex , regex_pattern , REG_NOSUB | REG_ICASE ) == 0 ) | |
2877 #endif | |
2878 { | |
2879 GList *tfound_list = NULL; | |
2880 if ( is_first_search == TRUE ) entry_list = playlist->entries; | |
2881 else entry_list = found_list; /* use found_list */ | |
2882 for ( ; entry_list ; entry_list = g_list_next(entry_list) ) | |
2883 { | |
2884 PlaylistEntry *entry = entry_list->data; | |
2885 if ( ( entry->tuple != NULL ) && ( entry->tuple->track_name != NULL ) && | |
2886 ( regexec( ®ex , entry->tuple->track_name , 0 , NULL , 0 ) == 0 ) ) | |
2887 { | |
2888 tfound_list = g_list_append( tfound_list , entry ); | |
2889 } | |
2890 } | |
2891 g_list_free( found_list ); /* wipe old found_list */ | |
2892 found_list = tfound_list; /* move tfound_list in found_list */ | |
2893 regfree( ®ex ); | |
2894 } | |
2895 is_first_search = FALSE; | |
2896 } | |
2897 | |
2898 if ( tuple->album_name != NULL ) | |
2899 { | |
2900 /* match by album_name */ | |
2901 const gchar *regex_pattern = tuple->album_name; | |
2902 regex_t regex; | |
2903 #if defined(USE_REGEX_PCRE) | |
2904 if ( regcomp( ®ex , regex_pattern , REG_NOSUB | REG_ICASE | REG_UTF8 ) == 0 ) | |
2905 #else | |
2906 if ( regcomp( ®ex , regex_pattern , REG_NOSUB | REG_ICASE ) == 0 ) | |
2907 #endif | |
2908 { | |
2909 GList *tfound_list = NULL; | |
2910 if ( is_first_search == TRUE ) entry_list = playlist->entries; | |
2911 else entry_list = found_list; /* use found_list */ | |
2912 for ( ; entry_list ; entry_list = g_list_next(entry_list) ) | |
2913 { | |
2914 PlaylistEntry *entry = entry_list->data; | |
2915 if ( ( entry->tuple != NULL ) && ( entry->tuple->album_name != NULL ) && | |
2916 ( regexec( ®ex , entry->tuple->album_name , 0 , NULL , 0 ) == 0 ) ) | |
2917 { | |
2918 tfound_list = g_list_append( tfound_list , entry ); | |
2919 } | |
2920 } | |
2921 g_list_free( found_list ); /* wipe old found_list */ | |
2922 found_list = tfound_list; /* move tfound_list in found_list */ | |
2923 regfree( ®ex ); | |
2924 } | |
2925 is_first_search = FALSE; | |
2926 } | |
2927 | |
2928 if ( tuple->performer != NULL ) | |
2929 { | |
2930 /* match by performer */ | |
2931 const gchar *regex_pattern = tuple->performer; | |
2932 regex_t regex; | |
2933 #if defined(USE_REGEX_PCRE) | |
2934 if ( regcomp( ®ex , regex_pattern , REG_NOSUB | REG_ICASE | REG_UTF8 ) == 0 ) | |
2935 #else | |
2936 if ( regcomp( ®ex , regex_pattern , REG_NOSUB | REG_ICASE ) == 0 ) | |
2937 #endif | |
2938 { | |
2939 GList *tfound_list = NULL; | |
2940 if ( is_first_search == TRUE ) entry_list = playlist->entries; | |
2941 else entry_list = found_list; /* use found_list */ | |
2942 for ( ; entry_list ; entry_list = g_list_next(entry_list) ) | |
2943 { | |
2944 PlaylistEntry *entry = entry_list->data; | |
2945 if ( ( entry->tuple != NULL ) && ( entry->tuple->performer != NULL ) && | |
2946 ( regexec( ®ex , entry->tuple->performer , 0 , NULL , 0 ) == 0 ) ) | |
2947 { | |
2948 tfound_list = g_list_append( tfound_list , entry ); | |
2949 } | |
2950 } | |
2951 g_list_free( found_list ); /* wipe old found_list */ | |
2952 found_list = tfound_list; /* move tfound_list in found_list */ | |
2953 regfree( ®ex ); | |
2954 } | |
2955 is_first_search = FALSE; | |
2956 } | |
2957 | |
2958 if ( tuple->file_name != NULL ) | |
2959 { | |
2960 /* match by file_name */ | |
2961 const gchar *regex_pattern = tuple->file_name; | |
2962 regex_t regex; | |
2963 #if defined(USE_REGEX_PCRE) | |
2964 if ( regcomp( ®ex , regex_pattern , REG_NOSUB | REG_ICASE | REG_UTF8 ) == 0 ) | |
2965 #else | |
2966 if ( regcomp( ®ex , regex_pattern , REG_NOSUB | REG_ICASE ) == 0 ) | |
2967 #endif | |
2968 { | |
2969 GList *tfound_list = NULL; | |
2970 if ( is_first_search == TRUE ) entry_list = playlist->entries; | |
2971 else entry_list = found_list; /* use found_list */ | |
2972 for ( ; entry_list ; entry_list = g_list_next(entry_list) ) | |
2973 { | |
2974 PlaylistEntry *entry = entry_list->data; | |
2975 if ( ( entry->tuple != NULL ) && ( entry->tuple->file_name != NULL ) && | |
2976 ( regexec( ®ex , entry->tuple->file_name , 0 , NULL , 0 ) == 0 ) ) | |
2977 { | |
2978 tfound_list = g_list_append( tfound_list , entry ); | |
2979 } | |
2980 } | |
2981 g_list_free( found_list ); /* wipe old found_list */ | |
2982 found_list = tfound_list; /* move tfound_list in found_list */ | |
2983 regfree( ®ex ); | |
2984 } | |
2985 is_first_search = FALSE; | |
2986 } | |
2987 | |
2988 /* NOTE: action = 0 -> default behaviour, select all matching entries */ | |
2989 /* if some entries are still in found_list, those | |
2990 are what the user is searching for; select them */ | |
2991 for ( sel_list = found_list ; sel_list ; sel_list = g_list_next(sel_list) ) | |
2992 { | |
2993 PlaylistEntry *entry = sel_list->data; | |
2994 entry->selected = TRUE; | |
2995 num_of_entries_found++; | |
2996 } | |
2997 | |
2998 g_list_free( found_list ); | |
2999 | |
3000 PLAYLIST_UNLOCK(playlist->mutex); | |
3001 playlist_recalc_total_time(playlist); | |
3002 | |
3003 return num_of_entries_found; | |
3004 } | |
3005 | |
3006 void | |
3007 playlist_select_all(Playlist *playlist, gboolean set) | |
3008 { | |
3009 GList *list; | |
3010 | |
3011 PLAYLIST_LOCK(playlist->mutex); | |
3012 | |
3013 for (list = playlist->entries; list; list = g_list_next(list)) { | |
3014 PlaylistEntry *entry = list->data; | |
3015 entry->selected = set; | |
3016 } | |
3017 | |
3018 PLAYLIST_UNLOCK(playlist->mutex); | |
3019 playlist_recalc_total_time(playlist); | |
3020 } | |
3021 | |
3022 void | |
3023 playlist_select_invert_all(Playlist *playlist) | |
3024 { | |
3025 GList *list; | |
3026 | |
3027 PLAYLIST_LOCK(playlist->mutex); | |
3028 | |
3029 for (list = playlist->entries; list; list = g_list_next(list)) { | |
3030 PlaylistEntry *entry = list->data; | |
3031 entry->selected = !entry->selected; | |
3032 } | |
3033 | |
3034 PLAYLIST_UNLOCK(playlist->mutex); | |
3035 playlist_recalc_total_time(playlist); | |
3036 } | |
3037 | |
3038 gboolean | |
3039 playlist_select_invert(Playlist *playlist, guint pos) | |
3040 { | |
3041 GList *list; | |
3042 gboolean invert_ok = FALSE; | |
3043 | |
3044 PLAYLIST_LOCK(playlist->mutex); | |
3045 | |
3046 if ((list = g_list_nth(playlist->entries, pos))) { | |
3047 PlaylistEntry *entry = list->data; | |
3048 entry->selected = !entry->selected; | |
3049 invert_ok = TRUE; | |
3050 } | |
3051 | |
3052 PLAYLIST_UNLOCK(playlist->mutex); | |
3053 playlist_recalc_total_time(playlist); | |
3054 | |
3055 return invert_ok; | |
3056 } | |
3057 | |
3058 | |
3059 void | |
3060 playlist_select_range(Playlist *playlist, gint min_pos, gint max_pos, gboolean select) | |
3061 { | |
3062 GList *list; | |
3063 gint i; | |
3064 | |
3065 if (min_pos > max_pos) | |
3066 SWAP(min_pos, max_pos); | |
3067 | |
3068 PLAYLIST_LOCK(playlist->mutex); | |
3069 | |
3070 list = g_list_nth(playlist->entries, min_pos); | |
3071 for (i = min_pos; i <= max_pos && list; i++) { | |
3072 PlaylistEntry *entry = list->data; | |
3073 entry->selected = select; | |
3074 list = g_list_next(list); | |
3075 } | |
3076 | |
3077 PLAYLIST_UNLOCK(playlist->mutex); | |
3078 | |
3079 playlist_recalc_total_time(playlist); | |
3080 } | |
3081 | |
3082 gboolean | |
3083 playlist_read_info_selection(Playlist *playlist) | |
3084 { | |
3085 GList *node; | |
3086 gboolean retval = FALSE; | |
3087 | |
3088 PLAYLIST_LOCK(playlist->mutex); | |
3089 | |
3090 for (node = playlist->entries; node; node = g_list_next(node)) { | |
3091 PlaylistEntry *entry = node->data; | |
3092 if (!entry->selected) | |
3093 continue; | |
3094 | |
3095 retval = TRUE; | |
3096 | |
3097 str_replace_in(&entry->title, NULL); | |
3098 entry->length = -1; | |
3099 | |
3100 /* invalidate mtime to reread */ | |
3101 if (entry->tuple != NULL) | |
3102 entry->tuple->mtime = -1; /* -1 denotes "non-initialized". now 0 is for stream etc. yaz */ | |
3103 | |
3104 if (!playlist_entry_get_info(entry)) { | |
3105 if (g_list_index(playlist->entries, entry) == -1) | |
3106 /* Entry disappeared while we looked it up. Restart. */ | |
3107 node = playlist->entries; | |
3108 } | |
3109 } | |
3110 | |
3111 PLAYLIST_UNLOCK(playlist->mutex); | |
3112 | |
3113 playlistwin_update_list(playlist); | |
3114 playlist_recalc_total_time(playlist); | |
3115 | |
3116 return retval; | |
3117 } | |
3118 | |
3119 void | |
3120 playlist_read_info(Playlist *playlist, guint pos) | |
3121 { | |
3122 GList *node; | |
3123 | |
3124 PLAYLIST_LOCK(playlist->mutex); | |
3125 | |
3126 if ((node = g_list_nth(playlist->entries, pos))) { | |
3127 PlaylistEntry *entry = node->data; | |
3128 str_replace_in(&entry->title, NULL); | |
3129 entry->length = -1; | |
3130 playlist_entry_get_info(entry); | |
3131 } | |
3132 | |
3133 PLAYLIST_UNLOCK(playlist->mutex); | |
3134 | |
3135 playlistwin_update_list(playlist); | |
3136 playlist_recalc_total_time(playlist); | |
3137 } | |
3138 | |
3139 Playlist * | |
3140 playlist_get_active(void) | |
3141 { | |
3142 if (playlists_iter != NULL) | |
3143 return (Playlist *) playlists_iter->data; | |
3144 | |
3145 return (Playlist *) playlists->data; | |
3146 } | |
3147 | |
3148 void | |
3149 playlist_set_shuffle(gboolean shuffle) | |
3150 { | |
3151 Playlist *playlist = playlist_get_active(); | |
3152 if (!playlist) | |
3153 return; | |
3154 | |
3155 PLAYLIST_LOCK(playlist->mutex); | |
3156 | |
3157 playlist_position_before_jump = NULL; | |
3158 | |
3159 cfg.shuffle = shuffle; | |
3160 playlist_generate_shuffle_list_nolock(playlist); | |
3161 | |
3162 PLAYLIST_UNLOCK(playlist->mutex); | |
3163 } | |
3164 | |
3165 Playlist * | |
3166 playlist_new(void) | |
3167 { | |
3168 Playlist *playlist = g_new0(Playlist, 1); | |
3169 playlist->mutex = g_mutex_new(); | |
3170 playlist->loading_playlist = FALSE; | |
3171 | |
3172 playlist_set_current_name(playlist, NULL); | |
3173 playlist_clear(playlist); | |
3174 | |
3175 return playlist; | |
3176 } | |
3177 | |
3178 void | |
3179 playlist_free(Playlist *playlist) | |
3180 { | |
3181 g_mutex_free( playlist->mutex ); | |
3182 g_free( playlist ); | |
3183 return; | |
3184 } | |
3185 | |
3186 Playlist * | |
3187 playlist_new_from_selected(void) | |
3188 { | |
3189 Playlist *newpl = playlist_new(); | |
3190 Playlist *playlist = playlist_get_active(); | |
3191 GList *list = playlist_get_selected(playlist); | |
3192 | |
3193 playlist_add_playlist( newpl ); | |
3194 | |
3195 PLAYLIST_LOCK(playlist->mutex); | |
3196 | |
3197 while ( list != NULL ) | |
3198 { | |
3199 PlaylistEntry *entry = g_list_nth_data(playlist->entries, GPOINTER_TO_INT(list->data)); | |
3200 if ( entry->filename != NULL ) /* paranoid? oh well... */ | |
3201 playlist_add( newpl , entry->filename ); | |
3202 list = g_list_next(list); | |
3203 } | |
3204 | |
3205 PLAYLIST_UNLOCK(playlist->mutex); | |
3206 | |
3207 playlist_recalc_total_time(newpl); | |
3208 playlistwin_update_list(playlist); | |
3209 | |
3210 return newpl; | |
3211 } | |
3212 | |
3213 const gchar * | |
3214 playlist_get_filename_to_play(Playlist *playlist) | |
3215 { | |
3216 const gchar *filename = NULL; | |
3217 | |
3218 if (!playlist) | |
3219 return NULL; | |
3220 | |
3221 PLAYLIST_LOCK(playlist->mutex); | |
3222 | |
3223 if (!playlist->position) { | |
3224 if (cfg.shuffle) | |
3225 playlist->position = playlist->shuffle->data; | |
3226 else | |
3227 playlist->position = playlist->entries->data; | |
3228 } | |
3229 | |
3230 filename = playlist->position->filename; | |
3231 | |
3232 PLAYLIST_UNLOCK(playlist->mutex); | |
3233 | |
3234 return filename; | |
3235 } | |
3236 | |
3237 PlaylistEntry * | |
3238 playlist_get_entry_to_play(Playlist *playlist) | |
3239 { | |
3240 if (!playlist) | |
3241 return NULL; | |
3242 | |
3243 PLAYLIST_LOCK(playlist->mutex); | |
3244 | |
3245 if (!playlist->position) { | |
3246 if (cfg.shuffle) | |
3247 playlist->position = playlist->shuffle->data; | |
3248 else | |
3249 playlist->position = playlist->entries->data; | |
3250 } | |
3251 | |
3252 PLAYLIST_UNLOCK(playlist->mutex); | |
3253 | |
3254 return playlist->position; | |
3255 } |