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