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