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