Mercurial > audlegacy
annotate audacious/playlist.c @ 503:1cc9edf68ce9 trunk
[svn] Remove crap that is unneeded from about.c, update credits information.
author | nenolod |
---|---|
date | Wed, 25 Jan 2006 18:40:41 -0800 |
parents | f03932d43230 |
children | e4e897d20791 |
rev | line source |
---|---|
0 | 1 /* BMP (C) GPL 2003 $top_src_dir/AUTHORS |
2 * | |
3 * based on: | |
4 * | |
5 * XMMS - Cross-platform multimedia player | |
6 * Copyright (C) 1998-2003 Peter Alm, Mikael Alm, Olle Hallnas, | |
7 * Thomas Nilsson and 4Front Technologies | |
8 * Copyright (C) 1999-2003 Haavard Kvaalen | |
9 * | |
10 * | |
11 * This program is free software; you can redistribute it and/or modify | |
12 * it under the terms of the GNU General Public License as published by | |
13 * the Free Software Foundation; either version 2 of the License, or | |
14 * (at your option) any later version. | |
15 * | |
16 * This program is distributed in the hope that it will be useful, | |
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 * GNU General Public License for more details. | |
20 * | |
21 * You should have received a copy of the GNU General Public License | |
22 * along with this program; if not, write to the Free Software | |
23 * Foundation, Inc., 59 Tmple Place - Suite 330, Boston, MA 02111-1307, USA. | |
24 */ | |
25 | |
26 #ifdef HAVE_CONFIG_H | |
27 # include "config.h" | |
28 #endif | |
29 | |
30 #include "playlist.h" | |
31 | |
32 #include <glib.h> | |
33 #include <glib/gprintf.h> | |
34 #include <stdlib.h> | |
35 #include <string.h> | |
36 #include <time.h> | |
37 | |
38 #include <unistd.h> | |
39 #include <sys/types.h> | |
40 #include <sys/stat.h> | |
41 #include <sys/errno.h> | |
42 | |
43 #include "input.h" | |
44 #include "main.h" | |
45 #include "mainwin.h" | |
46 #include "libaudacious/util.h" | |
47 #include "libaudacious/configdb.h" | |
48 #include "libaudacious/vfs.h" | |
49 #include "equalizer.h" | |
284 | 50 #include "libaudcore/playback.h" |
0 | 51 #include "playlist.h" |
383 | 52 #include "ui_playlist.h" |
0 | 53 #include "playlist_list.h" |
54 #include "skin.h" | |
55 #include "urldecode.h" | |
56 #include "util.h" | |
57 | |
58 #include "debug.h" | |
59 | |
60 typedef gint (*PlaylistCompareFunc) (const PlaylistEntry * a, const PlaylistEntry * b); | |
61 typedef void (*PlaylistSaveFunc) (FILE * file); | |
62 | |
63 PlaylistEntry *playlist_position; | |
64 G_LOCK_DEFINE(playlist); | |
65 | |
66 /* NOTE: match the order listed in PlaylistFormat enum */ | |
67 static const gchar *playlist_format_suffixes[] = { | |
68 ".m3u", ".pls", NULL | |
69 }; | |
70 | |
71 static GList *playlist = NULL; | |
72 static GList *shuffle_list = NULL; | |
73 static GList *queued_list = NULL; | |
74 | |
397
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
75 /* If this is set to TRUE, we do not probe upon playlist add. |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
76 * |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
77 * Under Audacious 0.1.x, this was not a big deal because we used |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
78 * file extension introspection instead of looking for file format magic |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
79 * strings. |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
80 * |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
81 * Because we use file magic strings, we have to fstat a file being added |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
82 * to a playlist up to 1 * <number of input plugins installed> times. |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
83 * |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
84 * This can get really slow now that we're looking for files to add to a |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
85 * playlist. (Up to 5 minutes for 5000 songs, etcetera.) |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
86 * |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
87 * So, we obviously don't want to probe while opening a large playlist |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
88 * up. Hince the boolean below. |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
89 * |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
90 * January 7, 2006, William Pitcock <nenolod@nenolod.net> |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
91 */ |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
92 static gboolean loading_playlist = FALSE; |
0 | 93 |
94 G_LOCK_DEFINE(playlist_get_info_going); | |
95 | |
96 static gchar *playlist_current_name = NULL; | |
97 | |
98 static gboolean playlist_get_info_scan_active = FALSE; | |
99 static gboolean playlist_get_info_going = FALSE; | |
100 static GThread *playlist_get_info_thread; | |
101 | |
102 | |
103 static gint path_compare(const gchar * a, const gchar * b); | |
104 static gint playlist_compare_path(const PlaylistEntry * a, const PlaylistEntry * b); | |
105 static gint playlist_compare_filename(const PlaylistEntry * a, const PlaylistEntry * b); | |
106 static gint playlist_compare_title(const PlaylistEntry * a, const PlaylistEntry * b); | |
107 static gint playlist_compare_date(const PlaylistEntry * a, const PlaylistEntry * b); | |
108 | |
109 static PlaylistCompareFunc playlist_compare_func_table[] = { | |
110 playlist_compare_path, | |
111 playlist_compare_filename, | |
112 playlist_compare_title, | |
113 playlist_compare_date | |
114 }; | |
115 | |
116 static void playlist_save_m3u(FILE * file); | |
117 static void playlist_save_pls(FILE * file); | |
118 | |
119 static PlaylistSaveFunc playlist_save_func_table[] = { | |
120 playlist_save_m3u, | |
121 playlist_save_pls | |
122 }; | |
123 | |
124 | |
125 static guint playlist_load_ins(const gchar * filename, gint pos); | |
126 | |
127 static void playlist_load_ins_file(const gchar * filename, | |
128 const gchar * playlist_name, gint pos, | |
129 const gchar * title, gint len); | |
130 | |
131 static void playlist_generate_shuffle_list(void); | |
132 static void playlist_generate_shuffle_list_nolock(void); | |
133 | |
134 static void playlist_recalc_total_time_nolock(void); | |
135 static void playlist_recalc_total_time(void); | |
136 | |
137 | |
138 PlaylistEntry * | |
139 playlist_entry_new(const gchar * filename, | |
140 const gchar * title, | |
355
1c701dfe5098
[svn] Cache the decoder used for each PlaylistEntry. This reduces the amount
nenolod
parents:
284
diff
changeset
|
141 const gint length, |
1c701dfe5098
[svn] Cache the decoder used for each PlaylistEntry. This reduces the amount
nenolod
parents:
284
diff
changeset
|
142 InputPlugin * dec) |
0 | 143 { |
144 PlaylistEntry *entry; | |
145 | |
146 entry = g_new0(PlaylistEntry, 1); | |
147 entry->filename = g_strdup(filename); | |
148 entry->title = str_to_utf8(title); | |
149 entry->length = length; | |
150 entry->selected = FALSE; | |
355
1c701dfe5098
[svn] Cache the decoder used for each PlaylistEntry. This reduces the amount
nenolod
parents:
284
diff
changeset
|
151 entry->decoder = dec; |
0 | 152 |
153 return entry; | |
154 } | |
155 | |
156 void | |
157 playlist_entry_free(PlaylistEntry * entry) | |
158 { | |
159 if (!entry) | |
160 return; | |
161 | |
162 g_free(entry->filename); | |
163 g_free(entry->title); | |
164 g_free(entry); | |
165 } | |
166 | |
167 static gboolean | |
168 playlist_entry_get_info(PlaylistEntry * entry) | |
169 { | |
170 gchar *title = NULL; | |
171 gint length = -1; | |
172 | |
173 g_return_val_if_fail(entry != NULL, FALSE); | |
174 | |
355
1c701dfe5098
[svn] Cache the decoder used for each PlaylistEntry. This reduces the amount
nenolod
parents:
284
diff
changeset
|
175 if (entry->decoder == NULL) |
1c701dfe5098
[svn] Cache the decoder used for each PlaylistEntry. This reduces the amount
nenolod
parents:
284
diff
changeset
|
176 input_get_song_info(entry->filename, &title, &length); |
418
f03932d43230
[svn] Add a sanity check for crappily written plugins in the decoder cache.
nenolod
parents:
398
diff
changeset
|
177 else if (entry->decoder->get_song_info != NULL) |
355
1c701dfe5098
[svn] Cache the decoder used for each PlaylistEntry. This reduces the amount
nenolod
parents:
284
diff
changeset
|
178 entry->decoder->get_song_info(entry->filename, &title, &length); |
1c701dfe5098
[svn] Cache the decoder used for each PlaylistEntry. This reduces the amount
nenolod
parents:
284
diff
changeset
|
179 |
0 | 180 if (!title && length == -1) |
181 return FALSE; | |
182 | |
183 /* entry is still around */ | |
184 entry->title = title; | |
185 entry->length = length; | |
186 | |
187 return TRUE; | |
188 } | |
189 | |
190 | |
191 const gchar * | |
192 playlist_get_current_name(void) | |
193 { | |
194 return playlist_current_name; | |
195 } | |
196 | |
197 gboolean | |
198 playlist_set_current_name(const gchar * filename) | |
199 { | |
200 g_free(playlist_current_name); | |
201 | |
202 if (!filename) { | |
203 playlist_current_name = NULL; | |
204 return FALSE; | |
205 } | |
206 | |
207 playlist_current_name = g_strdup(filename); | |
208 return TRUE; | |
209 } | |
210 | |
211 static GList * | |
212 find_playlist_position_list(void) | |
213 { | |
214 REQUIRE_STATIC_LOCK(playlist); | |
215 | |
216 if (!playlist_position) { | |
217 if (cfg.shuffle) | |
218 return shuffle_list; | |
219 else | |
220 return playlist; | |
221 } | |
222 | |
223 if (cfg.shuffle) | |
224 return g_list_find(shuffle_list, playlist_position); | |
225 else | |
226 return g_list_find(playlist, playlist_position); | |
227 } | |
228 | |
229 static void | |
230 play_queued(void) | |
231 { | |
232 GList *tmp = queued_list; | |
233 | |
234 REQUIRE_STATIC_LOCK(playlist); | |
235 | |
236 playlist_position = queued_list->data; | |
237 queued_list = g_list_remove_link(queued_list, queued_list); | |
238 g_list_free_1(tmp); | |
239 } | |
240 | |
241 void | |
242 playlist_clear(void) | |
243 { | |
244 if (bmp_playback_get_playing()) | |
245 bmp_playback_stop(); | |
246 | |
247 PLAYLIST_LOCK(); | |
248 | |
249 if (playlist) { | |
250 g_list_foreach(playlist, (GFunc) playlist_entry_free, NULL); | |
251 g_list_free(playlist); | |
252 | |
253 playlist = NULL; | |
254 playlist_position = NULL; | |
255 } | |
256 | |
257 PLAYLIST_UNLOCK(); | |
258 | |
259 playlist_generate_shuffle_list(); | |
260 playlistwin_update_list(); | |
261 playlist_recalc_total_time(); | |
262 } | |
263 | |
264 void | |
265 playlist_delete_node(GList * node, gboolean * set_info_text, | |
266 gboolean * restart_playing) | |
267 { | |
268 PlaylistEntry *entry; | |
269 GList *playing_song = NULL; | |
270 | |
271 REQUIRE_STATIC_LOCK(playlist); | |
272 | |
273 /* We call g_list_find manually here because we don't want an item | |
274 * in the shuffle_list */ | |
275 | |
276 if (playlist_position) | |
277 playing_song = g_list_find(playlist, playlist_position); | |
278 | |
279 entry = PLAYLIST_ENTRY(node->data); | |
280 | |
281 if (playing_song == node) { | |
282 *set_info_text = TRUE; | |
283 | |
284 if (bmp_playback_get_playing()) { | |
285 PLAYLIST_UNLOCK(); | |
286 bmp_playback_stop(); | |
287 PLAYLIST_LOCK(); | |
288 *restart_playing = TRUE; | |
289 } | |
290 | |
291 playing_song = find_playlist_position_list(); | |
292 | |
293 if (g_list_next(playing_song)) | |
294 playlist_position = g_list_next(playing_song)->data; | |
295 else if (g_list_previous(playing_song)) | |
296 playlist_position = g_list_previous(playing_song)->data; | |
297 else | |
298 playlist_position = NULL; | |
299 | |
300 /* Make sure the entry did not disappear under us */ | |
301 if (g_list_index(playlist_get(), entry) == -1) | |
302 return; | |
303 | |
304 } | |
305 else if (g_list_position(playlist, playing_song) > | |
306 g_list_position(playlist, node)) { | |
307 *set_info_text = TRUE; | |
308 } | |
309 | |
310 shuffle_list = g_list_remove(shuffle_list, entry); | |
311 playlist = g_list_remove_link(playlist, node); | |
312 playlist_entry_free(entry); | |
313 g_list_free_1(node); | |
314 | |
315 playlist_recalc_total_time_nolock(); | |
316 } | |
317 | |
318 void | |
319 playlist_delete_index(guint pos) | |
320 { | |
321 gboolean restart_playing = FALSE, set_info_text = FALSE; | |
322 GList *node; | |
323 | |
324 PLAYLIST_LOCK(); | |
325 | |
326 if (!playlist) { | |
327 PLAYLIST_UNLOCK(); | |
328 return; | |
329 } | |
330 | |
331 node = g_list_nth(playlist, pos); | |
332 | |
333 if (!node) { | |
334 PLAYLIST_UNLOCK(); | |
335 return; | |
336 } | |
337 | |
338 playlist_delete_node(node, &set_info_text, &restart_playing); | |
339 | |
340 PLAYLIST_UNLOCK(); | |
341 | |
342 playlist_recalc_total_time(); | |
343 | |
344 playlistwin_update_list(); | |
345 if (restart_playing) { | |
346 if (playlist_position) { | |
347 bmp_playback_initiate(); | |
348 } | |
349 else { | |
350 mainwin_clear_song_info(); | |
351 } | |
352 } | |
353 else if (set_info_text) { | |
354 mainwin_set_info_text(); | |
355 } | |
356 } | |
357 | |
358 void | |
359 playlist_delete_filenames(GList * filenames) | |
360 { | |
361 GList *node, *fnode; | |
362 gboolean set_info_text = FALSE, restart_playing = FALSE; | |
363 | |
364 PLAYLIST_LOCK(); | |
365 | |
366 for (fnode = filenames; fnode; fnode = g_list_next(fnode)) { | |
367 node = playlist; | |
368 | |
369 while (node) { | |
370 GList *next = g_list_next(node); | |
371 PlaylistEntry *entry = node->data; | |
372 | |
373 if (!strcmp(entry->filename, fnode->data)) | |
374 playlist_delete_node(node, &set_info_text, &restart_playing); | |
375 | |
376 node = next; | |
377 } | |
378 } | |
379 | |
380 playlist_recalc_total_time(); | |
381 PLAYLIST_UNLOCK(); | |
382 | |
383 playlistwin_update_list(); | |
384 | |
385 if (restart_playing) { | |
386 if (playlist_position) { | |
387 bmp_playback_initiate(); | |
388 } | |
389 else { | |
390 mainwin_clear_song_info(); | |
391 } | |
392 } | |
393 else if (set_info_text) { | |
394 mainwin_set_info_text(); | |
395 } | |
396 | |
397 } | |
398 | |
399 void | |
400 playlist_delete(gboolean crop) | |
401 { | |
402 gboolean restart_playing = FALSE, set_info_text = FALSE; | |
403 GList *node, *next_node; | |
404 PlaylistEntry *entry; | |
405 | |
406 PLAYLIST_LOCK(); | |
407 | |
408 node = playlist; | |
409 | |
410 while (node) { | |
411 entry = PLAYLIST_ENTRY(node->data); | |
412 | |
413 next_node = g_list_next(node); | |
414 | |
415 if ((entry->selected && !crop) || (!entry->selected && crop)) { | |
416 playlist_delete_node(node, &set_info_text, &restart_playing); | |
417 } | |
418 | |
419 node = next_node; | |
420 } | |
421 | |
422 PLAYLIST_UNLOCK(); | |
423 | |
424 playlist_recalc_total_time(); | |
425 | |
426 if (set_info_text) { | |
427 mainwin_set_info_text(); | |
428 } | |
429 | |
430 if (restart_playing) { | |
431 if (playlist_position) { | |
432 bmp_playback_initiate(); | |
433 } | |
434 else { | |
435 mainwin_clear_song_info(); | |
436 } | |
437 } | |
438 | |
439 playlistwin_update_list(); | |
440 } | |
441 | |
442 static void | |
443 __playlist_ins_with_info(const gchar * filename, | |
444 gint pos, | |
445 const gchar * title, | |
355
1c701dfe5098
[svn] Cache the decoder used for each PlaylistEntry. This reduces the amount
nenolod
parents:
284
diff
changeset
|
446 gint len, |
1c701dfe5098
[svn] Cache the decoder used for each PlaylistEntry. This reduces the amount
nenolod
parents:
284
diff
changeset
|
447 InputPlugin * dec) |
0 | 448 { |
449 g_return_if_fail(filename != NULL); | |
450 | |
451 PLAYLIST_LOCK(); | |
452 playlist = g_list_insert(playlist, | |
355
1c701dfe5098
[svn] Cache the decoder used for each PlaylistEntry. This reduces the amount
nenolod
parents:
284
diff
changeset
|
453 playlist_entry_new(filename, title, len, dec), |
0 | 454 pos); |
455 PLAYLIST_UNLOCK(); | |
456 | |
457 playlist_get_info_scan_active = TRUE; | |
458 } | |
459 | |
460 static void | |
355
1c701dfe5098
[svn] Cache the decoder used for each PlaylistEntry. This reduces the amount
nenolod
parents:
284
diff
changeset
|
461 __playlist_ins(const gchar * filename, gint pos, InputPlugin *dec) |
0 | 462 { |
355
1c701dfe5098
[svn] Cache the decoder used for each PlaylistEntry. This reduces the amount
nenolod
parents:
284
diff
changeset
|
463 __playlist_ins_with_info(filename, pos, NULL, -1, dec); |
0 | 464 playlist_recalc_total_time(); |
465 } | |
466 | |
467 | |
468 PlaylistFormat | |
469 playlist_format_get_from_name(const gchar * filename) | |
470 { | |
471 int i; | |
472 | |
473 for (i = 0; i < PLAYLIST_FORMAT_COUNT; i++) | |
474 { | |
475 if (str_has_suffix_nocase(filename, playlist_format_suffixes[i])) | |
476 return i; | |
477 } | |
478 | |
479 return PLAYLIST_FORMAT_UNKNOWN; | |
480 } | |
481 | |
482 gboolean | |
483 is_playlist_name(const gchar * filename) | |
484 { | |
485 g_return_val_if_fail(filename != NULL, FALSE); | |
486 return playlist_format_get_from_name(filename) != PLAYLIST_FORMAT_UNKNOWN; | |
487 } | |
488 | |
489 gboolean | |
490 playlist_ins(const gchar * filename, gint pos) | |
491 { | |
492 gchar buf[64], *p; | |
493 gint r; | |
494 VFSFile *file; | |
355
1c701dfe5098
[svn] Cache the decoder used for each PlaylistEntry. This reduces the amount
nenolod
parents:
284
diff
changeset
|
495 InputPlugin *dec; |
0 | 496 |
497 if (is_playlist_name(filename)) { | |
498 playlist_load_ins(filename, pos); | |
499 return TRUE; | |
500 } | |
501 | |
397
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
502 if (loading_playlist == TRUE) |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
503 dec = NULL; |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
504 else |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
505 dec = input_check_file(filename, TRUE); |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
506 |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
507 if (loading_playlist == TRUE || (loading_playlist == FALSE && dec != NULL)) |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
508 { |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
509 __playlist_ins(filename, pos, dec); |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
510 playlist_generate_shuffle_list(); |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
511 playlistwin_update_list(); |
0 | 512 return TRUE; |
513 } | |
514 | |
515 /* Some files (typically produced by some cgi-scripts) don't have | |
516 * the correct extension. Try to recognize these files by looking | |
517 * at their content. We only check for http entries since it does | |
518 * not make sense to have file entries in a playlist fetched from | |
519 * the net. */ | |
520 | |
521 /* Some strange people put fifo's with the .mp3 extension, so we | |
522 * need to make sure it's a real file (otherwise fread() may block | |
523 * and stall the entire program) */ | |
524 | |
525 /* FIXME: bah, FIFOs actually pass this regular file test */ | |
526 if (!vfs_file_test(filename, G_FILE_TEST_IS_REGULAR)) | |
527 return FALSE; | |
528 | |
529 if (!(file = vfs_fopen(filename, "rb"))) | |
530 return FALSE; | |
531 | |
532 r = vfs_fread(buf, 1, sizeof(buf), file); | |
533 vfs_fclose(file); | |
534 | |
535 for (p = buf; r-- > 0 && (*p == '\r' || *p == '\n'); p++); | |
536 | |
537 if (r > 5 && str_has_prefix_nocase(p, "http:")) { | |
538 playlist_load_ins(filename, pos); | |
539 return TRUE; | |
540 } | |
541 | |
542 return FALSE; | |
543 } | |
544 | |
545 /* FIXME: The next few functions are specific to Unix | |
546 * filesystems. Either abstract it away, or don't even bother checking | |
547 * at such low level */ | |
548 | |
549 typedef struct { | |
550 dev_t dev; | |
551 ino_t ino; | |
552 } DeviceInode; | |
553 | |
554 static DeviceInode * | |
555 devino_new(dev_t device, | |
556 ino_t inode) | |
557 { | |
558 DeviceInode *devino = g_new0(DeviceInode, 1); | |
559 | |
560 if (devino) | |
561 { | |
562 devino->dev = device; | |
563 devino->ino = inode; | |
564 } | |
565 | |
566 return devino; | |
567 } | |
568 | |
569 static guint | |
570 devino_hash(gconstpointer key) | |
571 { | |
572 const DeviceInode *d = key; | |
573 return d->ino; | |
574 } | |
575 | |
576 static gint | |
577 devino_compare(gconstpointer a, | |
578 gconstpointer b) | |
579 { | |
580 const DeviceInode *da = a, *db = b; | |
581 return (da->dev == db->dev && da->ino == db->ino); | |
582 } | |
583 | |
584 static gboolean | |
585 devino_destroy(gpointer key, | |
586 gpointer value, | |
587 gpointer data) | |
588 { | |
589 g_free(key); | |
590 return TRUE; | |
591 } | |
592 | |
593 static gboolean | |
594 file_is_hidden(const gchar * filename) | |
595 { | |
596 // FIXME: remove the const cast | |
597 g_return_val_if_fail(filename != NULL, FALSE); | |
598 return (g_basename((gchar *) filename)[0] == '.'); | |
599 } | |
600 | |
601 static GList * | |
602 playlist_dir_find_files(const gchar * path, | |
603 gboolean background, | |
604 GHashTable * htab) | |
605 { | |
606 GDir *dir; | |
607 GList *list = NULL, *ilist; | |
608 const gchar *dir_entry; | |
609 | |
610 struct stat statbuf; | |
611 DeviceInode *devino; | |
612 | |
613 if (!g_file_test(path, G_FILE_TEST_IS_DIR)) | |
614 return NULL; | |
615 | |
616 stat(path, &statbuf); | |
617 devino = devino_new(statbuf.st_dev, statbuf.st_ino); | |
618 | |
619 if (g_hash_table_lookup(htab, devino)) { | |
620 g_free(devino); | |
621 return NULL; | |
622 } | |
623 | |
624 g_hash_table_insert(htab, devino, GINT_TO_POINTER(1)); | |
625 | |
626 if ((ilist = input_scan_dir(path))) { | |
627 GList *node; | |
628 for (node = ilist; node; node = g_list_next(node)) { | |
629 gchar *name = g_build_filename(path, node->data, NULL); | |
630 list = g_list_prepend(list, name); | |
631 g_free(node->data); | |
632 } | |
633 g_list_free(ilist); | |
634 return list; | |
635 } | |
636 | |
637 if (!(dir = g_dir_open(path, 0, NULL))) | |
638 return NULL; | |
639 | |
640 while ((dir_entry = g_dir_read_name(dir))) { | |
641 gchar *filename; | |
642 | |
643 if (file_is_hidden(dir_entry)) | |
644 continue; | |
645 | |
646 filename = g_build_filename(path, dir_entry, NULL); | |
647 | |
648 if (g_file_test(filename, G_FILE_TEST_IS_DIR)) { | |
649 GList *sub; | |
650 sub = playlist_dir_find_files(filename, background, htab); | |
651 g_free(filename); | |
652 list = g_list_concat(list, sub); | |
653 } | |
654 else if (input_check_file(filename, TRUE)) | |
655 list = g_list_prepend(list, filename); | |
656 else | |
657 g_free(filename); | |
658 | |
659 while (background && gtk_events_pending()) | |
660 gtk_main_iteration(); | |
661 } | |
662 g_dir_close(dir); | |
663 | |
664 return list; | |
665 } | |
666 | |
667 gboolean | |
668 playlist_add(const gchar * filename) | |
669 { | |
670 return playlist_ins(filename, -1); | |
671 } | |
672 | |
673 guint | |
674 playlist_add_dir(const gchar * directory) | |
675 { | |
676 return playlist_ins_dir(directory, -1, TRUE); | |
677 } | |
678 | |
679 guint | |
680 playlist_add_url(const gchar * url) | |
681 { | |
682 return playlist_ins_url(url, -1); | |
683 } | |
684 | |
685 guint | |
686 playlist_ins_dir(const gchar * path, | |
687 gint pos, | |
688 gboolean background) | |
689 { | |
690 guint entries = 0; | |
691 GList *list, *node; | |
692 GHashTable *htab; | |
693 | |
694 htab = g_hash_table_new(devino_hash, devino_compare); | |
695 | |
696 list = playlist_dir_find_files(path, background, htab); | |
697 list = g_list_sort(list, (GCompareFunc) path_compare); | |
698 | |
699 g_hash_table_foreach_remove(htab, devino_destroy, NULL); | |
700 | |
701 for (node = list; node; node = g_list_next(node)) { | |
355
1c701dfe5098
[svn] Cache the decoder used for each PlaylistEntry. This reduces the amount
nenolod
parents:
284
diff
changeset
|
702 __playlist_ins(node->data, pos, NULL); |
0 | 703 g_free(node->data); |
704 entries++; | |
705 if (pos >= 0) | |
706 pos++; | |
707 } | |
708 | |
709 g_list_free(list); | |
710 | |
711 playlist_recalc_total_time(); | |
712 playlist_generate_shuffle_list(); | |
713 playlistwin_update_list(); | |
714 return entries; | |
715 } | |
716 | |
717 guint | |
718 playlist_ins_url(const gchar * string, | |
719 gint pos) | |
720 { | |
721 gchar *tmp; | |
722 gint i = 1, entries = 0; | |
723 gboolean first = TRUE; | |
724 guint firstpos = 0; | |
725 gboolean success = FALSE; | |
726 gchar *decoded = NULL; | |
727 | |
728 g_return_val_if_fail(string != NULL, 0); | |
729 | |
730 playlistwin_update_list(); | |
731 | |
732 while (*string) { | |
733 GList *node; | |
734 tmp = strchr(string, '\n'); | |
735 if (tmp) { | |
736 if (*(tmp - 1) == '\r') | |
737 *(tmp - 1) = '\0'; | |
738 *tmp = '\0'; | |
739 } | |
740 | |
741 if (!(decoded = xmms_urldecode_path(string))) | |
742 decoded = g_strdup(string); | |
743 | |
744 if (g_file_test(decoded, G_FILE_TEST_IS_DIR)) { | |
745 i = playlist_ins_dir(decoded, pos, FALSE); | |
746 } | |
747 else { | |
748 if (is_playlist_name(decoded)) { | |
749 i = playlist_load_ins(decoded, pos); | |
750 } | |
751 else { | |
752 success = playlist_ins(decoded, pos); | |
753 i = 1; | |
754 } | |
755 } | |
756 | |
757 g_free(decoded); | |
758 | |
759 PLAYLIST_LOCK(); | |
760 node = g_list_nth(playlist_get(), pos); | |
761 PLAYLIST_UNLOCK(); | |
762 | |
763 entries += i; | |
764 | |
765 if (first) { | |
766 first = FALSE; | |
767 firstpos = pos; | |
768 } | |
769 | |
770 if (pos >= 0) | |
771 pos += i; | |
772 if (!tmp) | |
773 break; | |
774 | |
775 string = tmp + 1; | |
776 } | |
777 | |
778 playlist_recalc_total_time(); | |
779 playlist_generate_shuffle_list(); | |
780 playlistwin_update_list(); | |
781 | |
782 return entries; | |
783 } | |
784 | |
785 void | |
786 playlist_set_info(const gchar * title, gint length, gint rate, | |
787 gint freq, gint nch) | |
788 { | |
789 PLAYLIST_LOCK(); | |
790 | |
791 if (playlist_position) { | |
792 g_free(playlist_position->title); | |
793 playlist_position->title = g_strdup(title); | |
794 playlist_position->length = length; | |
795 } | |
796 | |
797 PLAYLIST_UNLOCK(); | |
798 | |
799 playlist_recalc_total_time(); | |
800 | |
801 mainwin_set_song_info(rate, freq, nch); | |
802 } | |
803 | |
804 void | |
805 playlist_check_pos_current(void) | |
806 { | |
807 gint pos, row, bottom; | |
808 | |
809 PLAYLIST_LOCK(); | |
810 if (!playlist || !playlist_position || !playlistwin_list) { | |
811 PLAYLIST_UNLOCK(); | |
812 return; | |
813 } | |
814 | |
815 pos = g_list_index(playlist, playlist_position); | |
816 | |
817 if (playlistwin_item_visible(pos)) { | |
818 PLAYLIST_UNLOCK(); | |
819 return; | |
820 } | |
821 | |
822 bottom = MAX(0, playlist_get_length_nolock() - | |
823 playlistwin_list->pl_num_visible); | |
824 row = CLAMP(pos - playlistwin_list->pl_num_visible / 2, 0, bottom); | |
825 PLAYLIST_UNLOCK(); | |
826 playlistwin_set_toprow(row); | |
827 } | |
828 | |
829 void | |
830 playlist_next(void) | |
831 { | |
832 GList *plist_pos_list; | |
833 gboolean restart_playing = FALSE; | |
834 | |
835 PLAYLIST_LOCK(); | |
836 if (!playlist) { | |
837 PLAYLIST_UNLOCK(); | |
838 return; | |
839 } | |
840 | |
841 plist_pos_list = find_playlist_position_list(); | |
842 | |
843 if (!cfg.repeat && !g_list_next(plist_pos_list)) { | |
844 PLAYLIST_UNLOCK(); | |
845 return; | |
846 } | |
847 | |
848 if (bmp_playback_get_playing()) { | |
849 /* We need to stop before changing playlist_position */ | |
850 PLAYLIST_UNLOCK(); | |
851 bmp_playback_stop(); | |
852 PLAYLIST_LOCK(); | |
853 restart_playing = TRUE; | |
854 } | |
855 | |
856 plist_pos_list = find_playlist_position_list(); | |
857 if (queued_list) | |
858 play_queued(); | |
859 else if (g_list_next(plist_pos_list)) | |
860 playlist_position = g_list_next(plist_pos_list)->data; | |
861 else if (cfg.repeat) { | |
862 playlist_position = NULL; | |
863 playlist_generate_shuffle_list_nolock(); | |
864 if (cfg.shuffle) | |
865 playlist_position = shuffle_list->data; | |
866 else | |
867 playlist_position = playlist->data; | |
868 } | |
869 PLAYLIST_UNLOCK(); | |
870 playlist_check_pos_current(); | |
871 | |
872 if (restart_playing) | |
873 bmp_playback_initiate(); | |
874 else { | |
875 mainwin_set_info_text(); | |
876 playlistwin_update_list(); | |
877 } | |
878 } | |
879 | |
880 void | |
881 playlist_prev(void) | |
882 { | |
883 GList *plist_pos_list; | |
884 gboolean restart_playing = FALSE; | |
885 | |
886 PLAYLIST_LOCK(); | |
887 if (!playlist) { | |
888 PLAYLIST_UNLOCK(); | |
889 return; | |
890 } | |
891 | |
892 plist_pos_list = find_playlist_position_list(); | |
893 | |
894 if (!cfg.repeat && !g_list_previous(plist_pos_list)) { | |
895 PLAYLIST_UNLOCK(); | |
896 return; | |
897 } | |
898 | |
899 if (bmp_playback_get_playing()) { | |
900 /* We need to stop before changing playlist_position */ | |
901 PLAYLIST_UNLOCK(); | |
902 bmp_playback_stop(); | |
903 PLAYLIST_LOCK(); | |
904 restart_playing = TRUE; | |
905 } | |
906 | |
907 plist_pos_list = find_playlist_position_list(); | |
908 if (g_list_previous(plist_pos_list)) { | |
909 playlist_position = g_list_previous(plist_pos_list)->data; | |
910 } | |
911 else if (cfg.repeat) { | |
912 GList *node; | |
913 playlist_position = NULL; | |
914 playlist_generate_shuffle_list_nolock(); | |
915 if (cfg.shuffle) | |
916 node = g_list_last(shuffle_list); | |
917 else | |
918 node = g_list_last(playlist); | |
919 if (node) | |
920 playlist_position = node->data; | |
921 } | |
922 | |
923 PLAYLIST_UNLOCK(); | |
924 | |
925 playlist_check_pos_current(); | |
926 | |
927 if (restart_playing) | |
928 bmp_playback_initiate(); | |
929 else { | |
930 mainwin_set_info_text(); | |
931 playlistwin_update_list(); | |
932 } | |
933 } | |
934 | |
935 void | |
936 playlist_queue(void) | |
937 { | |
938 GList *list = playlist_get_selected(); | |
939 GList *it = list; | |
940 | |
941 PLAYLIST_LOCK(); | |
942 | |
943 while (it) { | |
944 GList *next = g_list_next(it); | |
945 GList *tmp; | |
946 | |
947 it->data = g_list_nth_data(playlist, GPOINTER_TO_INT(it->data)); | |
948 if ((tmp = g_list_find(queued_list, it->data))) { | |
949 queued_list = g_list_remove_link(queued_list, tmp); | |
950 g_list_free_1(tmp); | |
951 list = g_list_remove_link(list, it); | |
952 g_list_free_1(it); | |
953 } | |
954 | |
955 it = next; | |
956 } | |
957 | |
958 queued_list = g_list_concat(queued_list, list); | |
959 | |
960 PLAYLIST_UNLOCK(); | |
961 | |
962 playlist_recalc_total_time(); | |
963 playlistwin_update_list(); | |
964 } | |
965 | |
966 void | |
967 playlist_queue_position(guint pos) | |
968 { | |
969 GList *tmp; | |
970 PlaylistEntry *entry; | |
971 | |
972 PLAYLIST_LOCK(); | |
973 entry = g_list_nth_data(playlist, pos); | |
974 if ((tmp = g_list_find(queued_list, entry))) { | |
975 queued_list = g_list_remove_link(queued_list, tmp); | |
976 g_list_free_1(tmp); | |
977 } | |
978 else | |
979 queued_list = g_list_append(queued_list, entry); | |
980 PLAYLIST_UNLOCK(); | |
981 | |
982 playlist_recalc_total_time(); | |
983 playlistwin_update_list(); | |
984 } | |
985 | |
986 gboolean | |
987 playlist_is_position_queued(guint pos) | |
988 { | |
989 PlaylistEntry *entry; | |
990 GList *tmp; | |
991 | |
992 PLAYLIST_LOCK(); | |
993 entry = g_list_nth_data(playlist, pos); | |
994 tmp = g_list_find(queued_list, entry); | |
995 PLAYLIST_UNLOCK(); | |
996 | |
997 return tmp != NULL; | |
998 } | |
999 | |
1000 void | |
1001 playlist_clear_queue(void) | |
1002 { | |
1003 PLAYLIST_LOCK(); | |
1004 g_list_free(queued_list); | |
1005 queued_list = NULL; | |
1006 PLAYLIST_UNLOCK(); | |
1007 | |
1008 playlist_recalc_total_time(); | |
1009 playlistwin_update_list(); | |
1010 } | |
1011 | |
1012 void | |
1013 playlist_queue_remove(guint pos) | |
1014 { | |
1015 void *entry; | |
1016 | |
1017 PLAYLIST_LOCK(); | |
1018 entry = g_list_nth_data(playlist, pos); | |
1019 queued_list = g_list_remove(queued_list, entry); | |
1020 PLAYLIST_UNLOCK(); | |
1021 | |
1022 playlistwin_update_list(); | |
1023 } | |
1024 | |
1025 gint | |
1026 playlist_get_queue_position(PlaylistEntry * entry) | |
1027 { | |
1028 return g_list_index(queued_list, entry); | |
1029 } | |
1030 | |
1031 void | |
1032 playlist_set_position(guint pos) | |
1033 { | |
1034 GList *node; | |
1035 gboolean restart_playing = FALSE; | |
1036 | |
1037 PLAYLIST_LOCK(); | |
1038 if (!playlist) { | |
1039 PLAYLIST_UNLOCK(); | |
1040 return; | |
1041 } | |
1042 | |
1043 node = g_list_nth(playlist, pos); | |
1044 if (!node) { | |
1045 PLAYLIST_UNLOCK(); | |
1046 return; | |
1047 } | |
1048 | |
1049 if (bmp_playback_get_playing()) { | |
1050 /* We need to stop before changing playlist_position */ | |
1051 PLAYLIST_UNLOCK(); | |
1052 bmp_playback_stop(); | |
1053 PLAYLIST_LOCK(); | |
1054 restart_playing = TRUE; | |
1055 } | |
1056 | |
1057 playlist_position = node->data; | |
1058 PLAYLIST_UNLOCK(); | |
1059 playlist_check_pos_current(); | |
1060 | |
1061 if (restart_playing) | |
1062 bmp_playback_initiate(); | |
1063 else { | |
1064 mainwin_set_info_text(); | |
1065 playlistwin_update_list(); | |
1066 } | |
1067 | |
1068 /* | |
1069 * Regenerate the shuffle list when the user set a position | |
1070 * manually | |
1071 */ | |
1072 playlist_generate_shuffle_list(); | |
1073 playlist_recalc_total_time(); | |
1074 } | |
1075 | |
1076 void | |
1077 playlist_eof_reached(void) | |
1078 { | |
1079 GList *plist_pos_list; | |
1080 | |
1081 bmp_playback_stop(); | |
1082 | |
1083 PLAYLIST_LOCK(); | |
1084 plist_pos_list = find_playlist_position_list(); | |
1085 | |
1086 if (cfg.no_playlist_advance) { | |
1087 PLAYLIST_UNLOCK(); | |
1088 mainwin_clear_song_info(); | |
1089 if (cfg.repeat) | |
1090 bmp_playback_initiate(); | |
1091 return; | |
1092 } | |
1093 | |
1094 if (queued_list) { | |
1095 play_queued(); | |
1096 } | |
1097 else if (!g_list_next(plist_pos_list)) { | |
1098 if (cfg.shuffle) { | |
1099 playlist_position = NULL; | |
1100 playlist_generate_shuffle_list_nolock(); | |
1101 } | |
1102 else | |
1103 playlist_position = playlist->data; | |
1104 | |
1105 if (!cfg.repeat) { | |
1106 PLAYLIST_UNLOCK(); | |
1107 mainwin_clear_song_info(); | |
1108 mainwin_set_info_text(); | |
1109 return; | |
1110 } | |
1111 } | |
1112 else | |
1113 playlist_position = g_list_next(plist_pos_list)->data; | |
1114 | |
1115 PLAYLIST_UNLOCK(); | |
1116 | |
1117 playlist_check_pos_current(); | |
1118 bmp_playback_initiate(); | |
1119 mainwin_set_info_text(); | |
1120 playlistwin_update_list(); | |
1121 } | |
1122 | |
1123 gint | |
1124 playlist_get_length(void) | |
1125 { | |
1126 gint retval; | |
1127 | |
1128 PLAYLIST_LOCK(); | |
1129 retval = playlist_get_length_nolock(); | |
1130 PLAYLIST_UNLOCK(); | |
1131 | |
1132 return retval; | |
1133 } | |
1134 | |
1135 gint | |
1136 playlist_queue_get_length(void) | |
1137 { | |
1138 gint length; | |
1139 | |
1140 PLAYLIST_LOCK(); | |
1141 length = g_list_length(queued_list); | |
1142 PLAYLIST_UNLOCK(); | |
1143 | |
1144 return length; | |
1145 } | |
1146 | |
1147 gint | |
1148 playlist_get_length_nolock(void) | |
1149 { | |
1150 REQUIRE_STATIC_LOCK(playlist); | |
1151 return g_list_length(playlist); | |
1152 } | |
1153 | |
1154 gchar * | |
1155 playlist_get_info_text(void) | |
1156 { | |
1157 gchar *text, *title, *numbers, *length; | |
1158 | |
1159 PLAYLIST_LOCK(); | |
1160 if (!playlist_position) { | |
1161 PLAYLIST_UNLOCK(); | |
1162 return NULL; | |
1163 } | |
1164 | |
1165 /* FIXME: there should not be a need to do additional conversion, | |
1166 * if playlist is properly maintained */ | |
1167 if (playlist_position->title) { | |
1168 title = str_to_utf8(playlist_position->title); | |
1169 } | |
1170 else { | |
1171 gchar *basename = g_path_get_basename(playlist_position->filename); | |
1172 title = filename_to_utf8(basename); | |
1173 g_free(basename); | |
1174 } | |
1175 | |
1176 /* | |
1177 * If the user don't want numbers in the playlist, don't | |
1178 * display them in other parts of XMMS | |
1179 */ | |
1180 | |
1181 if (cfg.show_numbers_in_pl) | |
1182 numbers = g_strdup_printf("%d. ", playlist_get_position_nolock() + 1); | |
1183 else | |
1184 numbers = g_strdup(""); | |
1185 | |
1186 if (playlist_position->length != -1) | |
1187 length = g_strdup_printf(" (%d:%-2.2d)", | |
1188 playlist_position->length / 60000, | |
1189 (playlist_position->length / 1000) % 60); | |
1190 else | |
1191 length = g_strdup(""); | |
1192 | |
1193 PLAYLIST_UNLOCK(); | |
1194 | |
1195 text = convert_title_text(g_strconcat(numbers, title, length, NULL)); | |
1196 | |
1197 g_free(numbers); | |
1198 g_free(title); | |
1199 g_free(length); | |
1200 | |
1201 return text; | |
1202 } | |
1203 | |
1204 gint | |
1205 playlist_get_current_length(void) | |
1206 { | |
1207 gint len = 0; | |
1208 | |
1209 PLAYLIST_LOCK(); | |
1210 if (playlist && playlist_position) | |
1211 len = playlist_position->length; | |
1212 PLAYLIST_UNLOCK(); | |
1213 | |
1214 return len; | |
1215 } | |
1216 | |
1217 static void | |
1218 playlist_save_m3u(FILE * file) | |
1219 { | |
1220 GList *node; | |
1221 | |
1222 g_return_if_fail(file != NULL); | |
1223 | |
1224 if (cfg.use_pl_metadata) | |
1225 g_fprintf(file, "#EXTM3U\n"); | |
1226 | |
1227 PLAYLIST_LOCK(); | |
1228 | |
1229 for (node = playlist; node; node = g_list_next(node)) { | |
1230 PlaylistEntry *entry = PLAYLIST_ENTRY(node->data); | |
1231 | |
1232 if (entry->title && cfg.use_pl_metadata) { | |
1233 gint seconds; | |
1234 | |
1235 if (entry->length > 0) | |
1236 seconds = (entry->length) / 1000; | |
1237 else | |
1238 seconds = -1; | |
1239 | |
1240 g_fprintf(file, "#EXTINF:%d,%s\n", seconds, entry->title); | |
1241 } | |
1242 | |
1243 g_fprintf(file, "%s\n", entry->filename); | |
1244 } | |
1245 | |
1246 PLAYLIST_UNLOCK(); | |
1247 } | |
1248 | |
1249 static void | |
1250 playlist_save_pls(FILE * file) | |
1251 { | |
1252 GList *node; | |
1253 | |
1254 g_return_if_fail(file != NULL); | |
1255 | |
1256 g_fprintf(file, "[playlist]\n"); | |
1257 g_fprintf(file, "NumberOfEntries=%d\n", playlist_get_length()); | |
1258 | |
1259 PLAYLIST_LOCK(); | |
1260 | |
1261 for (node = playlist; node; node = g_list_next(node)) { | |
1262 PlaylistEntry *entry = PLAYLIST_ENTRY(node->data); | |
1263 | |
1264 g_fprintf(file, "File%d=%s\n", g_list_position(playlist, node) + 1, | |
1265 entry->filename); | |
1266 } | |
1267 | |
1268 PLAYLIST_UNLOCK(); | |
1269 } | |
1270 | |
1271 gboolean | |
1272 playlist_save(const gchar * filename, | |
1273 PlaylistFormat format) | |
1274 { | |
1275 FILE *file; | |
1276 | |
1277 g_return_val_if_fail(filename != NULL, FALSE); | |
1278 | |
1279 playlist_set_current_name(filename); | |
1280 | |
1281 if ((file = fopen(filename, "w")) == NULL) | |
1282 return FALSE; | |
1283 | |
1284 playlist_save_func_table[format](file); | |
1285 | |
1286 return (fclose(file) == 0); | |
1287 } | |
1288 | |
1289 gboolean | |
1290 playlist_load(const gchar * filename) | |
1291 { | |
397
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1292 gboolean ret = FALSE; |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1293 |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1294 loading_playlist = TRUE; |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1295 ret = playlist_load_ins(filename, -1); |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1296 loading_playlist = FALSE; |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1297 |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1298 return ret; |
0 | 1299 } |
1300 | |
1301 | |
1302 static void | |
1303 playlist_load_ins_file(const gchar * filename_p, | |
1304 const gchar * playlist_name, gint pos, | |
1305 const gchar * title, gint len) | |
1306 { | |
1307 gchar *filename; | |
1308 gchar *tmp, *path; | |
355
1c701dfe5098
[svn] Cache the decoder used for each PlaylistEntry. This reduces the amount
nenolod
parents:
284
diff
changeset
|
1309 InputPlugin *dec; /* for decoder cache */ |
0 | 1310 |
1311 g_return_if_fail(filename_p != NULL); | |
1312 g_return_if_fail(playlist_name != NULL); | |
1313 | |
1314 filename = g_strstrip(g_strdup(filename_p)); | |
1315 | |
1316 if (cfg.use_backslash_as_dir_delimiter) { | |
1317 while ((tmp = strchr(filename, '\\')) != NULL) | |
1318 *tmp = '/'; | |
1319 } | |
1320 | |
1321 if (filename[0] != '/' && !strstr(filename, "://")) { | |
1322 path = g_strdup(playlist_name); | |
1323 if ((tmp = strrchr(path, '/'))) | |
1324 *tmp = '\0'; | |
1325 else { | |
397
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1326 if (loading_playlist != TRUE) |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1327 dec = input_check_file(filename, FALSE); |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1328 else |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1329 dec = NULL; |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1330 |
355
1c701dfe5098
[svn] Cache the decoder used for each PlaylistEntry. This reduces the amount
nenolod
parents:
284
diff
changeset
|
1331 __playlist_ins_with_info(filename, pos, title, len, dec); |
0 | 1332 return; |
1333 } | |
1334 tmp = g_build_filename(path, filename, NULL); | |
397
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1335 |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1336 if (loading_playlist != TRUE) |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1337 dec = input_check_file(tmp, FALSE); |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1338 else |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1339 dec = NULL; |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1340 |
355
1c701dfe5098
[svn] Cache the decoder used for each PlaylistEntry. This reduces the amount
nenolod
parents:
284
diff
changeset
|
1341 __playlist_ins_with_info(tmp, pos, title, len, dec); |
0 | 1342 g_free(tmp); |
1343 g_free(path); | |
1344 } | |
1345 else | |
355
1c701dfe5098
[svn] Cache the decoder used for each PlaylistEntry. This reduces the amount
nenolod
parents:
284
diff
changeset
|
1346 { |
397
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1347 if (loading_playlist != TRUE) |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1348 dec = input_check_file(filename, FALSE); |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1349 else |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1350 dec = NULL; |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1351 |
355
1c701dfe5098
[svn] Cache the decoder used for each PlaylistEntry. This reduces the amount
nenolod
parents:
284
diff
changeset
|
1352 __playlist_ins_with_info(filename, pos, title, len, dec); |
1c701dfe5098
[svn] Cache the decoder used for each PlaylistEntry. This reduces the amount
nenolod
parents:
284
diff
changeset
|
1353 } |
0 | 1354 |
1355 g_free(filename); | |
1356 } | |
1357 | |
1358 static void | |
1359 parse_extm3u_info(const gchar * info, gchar ** title, gint * length) | |
1360 { | |
1361 gchar *str; | |
1362 | |
1363 g_return_if_fail(info != NULL); | |
1364 g_return_if_fail(title != NULL); | |
1365 g_return_if_fail(length != NULL); | |
1366 | |
1367 *title = NULL; | |
1368 *length = -1; | |
1369 | |
1370 if (!str_has_prefix_nocase(info, "#EXTINF:")) { | |
1371 g_message("Invalid m3u metadata (%s)", info); | |
1372 return; | |
1373 } | |
1374 | |
1375 info += 8; | |
1376 | |
1377 *length = atoi(info); | |
1378 if (*length <= 0) | |
1379 *length = -1; | |
1380 else | |
1381 *length *= 1000; | |
1382 | |
1383 if ((str = strchr(info, ','))) { | |
1384 *title = g_strstrip(g_strdup(str + 1)); | |
1385 if (strlen(*title) < 1) { | |
1386 g_free(*title); | |
1387 *title = NULL; | |
1388 } | |
1389 } | |
1390 } | |
1391 | |
1392 static guint | |
1393 playlist_load_pls(const gchar * filename, gint pos) | |
1394 { | |
1395 guint i, count, added_count = 0; | |
1396 gchar key[10]; | |
1397 gchar *line; | |
1398 | |
1399 g_return_val_if_fail(filename != NULL, 0); | |
1400 | |
1401 if (!str_has_suffix_nocase(filename, ".pls")) | |
1402 return 0; | |
1403 | |
1404 if (!(line = read_ini_string(filename, "playlist", "NumberOfEntries"))) | |
1405 return 0; | |
1406 | |
1407 count = atoi(line); | |
1408 g_free(line); | |
1409 | |
1410 for (i = 1; i <= count; i++) { | |
1411 g_snprintf(key, sizeof(key), "File%d", i); | |
1412 if ((line = read_ini_string(filename, "playlist", key))) { | |
1413 playlist_load_ins_file(line, filename, pos, NULL, -1); | |
1414 added_count++; | |
1415 | |
1416 if (pos >= 0) | |
1417 pos++; | |
1418 | |
1419 g_free(line); | |
1420 } | |
1421 } | |
1422 | |
1423 playlist_generate_shuffle_list(); | |
1424 playlistwin_update_list(); | |
1425 | |
1426 return added_count; | |
1427 } | |
1428 | |
1429 static guint | |
1430 playlist_load_m3u(const gchar * filename, gint pos) | |
1431 { | |
1432 FILE *file; | |
1433 gchar *line; | |
1434 gchar *ext_info = NULL, *ext_title = NULL; | |
1435 gsize line_len = 1024; | |
1436 gint ext_len = -1; | |
1437 gboolean is_extm3u = FALSE; | |
1438 guint added_count = 0; | |
1439 | |
1440 if (!(file = fopen(filename, "r"))) | |
1441 return 0; | |
1442 | |
1443 line = g_malloc(line_len); | |
1444 while (fgets(line, line_len, file)) { | |
1445 while (strlen(line) == line_len - 1 && line[strlen(line) - 1] != '\n') { | |
1446 line_len += 1024; | |
1447 line = g_realloc(line, line_len); | |
1448 fgets(&line[strlen(line)], 1024, file); | |
1449 } | |
1450 | |
1451 while (line[strlen(line) - 1] == '\r' || | |
1452 line[strlen(line) - 1] == '\n') | |
1453 line[strlen(line) - 1] = '\0'; | |
1454 | |
1455 if (str_has_prefix_nocase(line, "#EXTM3U")) { | |
1456 is_extm3u = TRUE; | |
1457 continue; | |
1458 } | |
1459 | |
1460 if (is_extm3u && str_has_prefix_nocase(line, "#EXTINF:")) { | |
1461 str_replace_in(&ext_info, g_strdup(line)); | |
1462 continue; | |
1463 } | |
1464 | |
1465 if (line[0] == '#' || strlen(line) == 0) { | |
1466 if (ext_info) { | |
1467 g_free(ext_info); | |
1468 ext_info = NULL; | |
1469 } | |
1470 continue; | |
1471 } | |
1472 | |
1473 if (is_extm3u) { | |
1474 if (cfg.use_pl_metadata && ext_info) | |
1475 parse_extm3u_info(ext_info, &ext_title, &ext_len); | |
1476 g_free(ext_info); | |
1477 ext_info = NULL; | |
1478 } | |
1479 | |
1480 playlist_load_ins_file(line, filename, pos, ext_title, ext_len); | |
1481 | |
1482 str_replace_in(&ext_title, NULL); | |
1483 ext_len = -1; | |
1484 | |
1485 added_count++; | |
1486 if (pos >= 0) | |
1487 pos++; | |
1488 } | |
1489 | |
1490 fclose(file); | |
1491 g_free(line); | |
1492 | |
1493 playlist_generate_shuffle_list(); | |
1494 playlistwin_update_list(); | |
1495 | |
1496 if (g_ascii_strcasecmp(filename, BMP_PLAYLIST_BASENAME)) | |
1497 playlist_set_current_name(NULL); | |
1498 else | |
1499 playlist_set_current_name(filename); | |
1500 | |
1501 return added_count; | |
1502 } | |
1503 | |
1504 static guint | |
1505 playlist_load_ins(const gchar * filename, gint pos) | |
1506 { | |
1507 guint added_count; | |
1508 | |
1509 g_return_val_if_fail(filename != NULL, 0); | |
1510 | |
1511 /* .pls ? */ | |
1512 if ((added_count = playlist_load_pls(filename, pos)) > 0) | |
1513 return added_count; | |
1514 | |
1515 /* Assume .m3u */ | |
1516 return playlist_load_m3u(filename, pos); | |
1517 } | |
1518 | |
1519 GList * | |
1520 get_playlist_nth(guint nth) | |
1521 { | |
1522 REQUIRE_STATIC_LOCK(playlist); | |
1523 return g_list_nth(playlist, nth); | |
1524 } | |
1525 | |
1526 | |
1527 GList * | |
1528 playlist_get(void) | |
1529 { | |
1530 REQUIRE_STATIC_LOCK(playlist); | |
1531 return playlist; | |
1532 } | |
1533 | |
1534 gint | |
1535 playlist_get_position_nolock(void) | |
1536 { | |
1537 REQUIRE_STATIC_LOCK(playlist); | |
1538 | |
1539 if (playlist && playlist_position) | |
1540 return g_list_index(playlist, playlist_position); | |
1541 return 0; | |
1542 } | |
1543 | |
1544 gint | |
1545 playlist_get_position(void) | |
1546 { | |
1547 gint pos; | |
1548 | |
1549 PLAYLIST_LOCK(); | |
1550 pos = playlist_get_position_nolock(); | |
1551 PLAYLIST_UNLOCK(); | |
1552 | |
1553 return pos; | |
1554 } | |
1555 | |
1556 gchar * | |
1557 playlist_get_filename(guint pos) | |
1558 { | |
1559 gchar *filename; | |
1560 PlaylistEntry *entry; | |
1561 GList *node; | |
1562 | |
1563 PLAYLIST_LOCK(); | |
1564 if (!playlist) { | |
1565 PLAYLIST_UNLOCK(); | |
1566 return NULL; | |
1567 } | |
1568 node = g_list_nth(playlist, pos); | |
1569 if (!node) { | |
1570 PLAYLIST_UNLOCK(); | |
1571 return NULL; | |
1572 } | |
1573 entry = node->data; | |
1574 | |
1575 filename = g_strdup(entry->filename); | |
1576 PLAYLIST_UNLOCK(); | |
1577 | |
1578 return filename; | |
1579 } | |
1580 | |
1581 gchar * | |
1582 playlist_get_songtitle(guint pos) | |
1583 { | |
1584 gchar *title = NULL; | |
1585 PlaylistEntry *entry; | |
1586 GList *node; | |
1587 | |
1588 PLAYLIST_LOCK(); | |
1589 | |
1590 if (!playlist) { | |
1591 PLAYLIST_UNLOCK(); | |
1592 return NULL; | |
1593 } | |
1594 | |
1595 if (!(node = g_list_nth(playlist, pos))) { | |
1596 PLAYLIST_UNLOCK(); | |
1597 return NULL; | |
1598 } | |
1599 | |
1600 entry = node->data; | |
1601 | |
1602 /* FIXME: simplify this logic */ | |
1603 if (!entry->title && entry->length == -1) { | |
1604 if (playlist_entry_get_info(entry)) | |
1605 title = entry->title; | |
1606 } | |
1607 else { | |
1608 title = entry->title; | |
1609 } | |
1610 | |
1611 PLAYLIST_UNLOCK(); | |
1612 | |
1613 if (!title) { | |
1614 title = g_path_get_basename(entry->filename); | |
1615 return str_replace(title, filename_to_utf8(title)); | |
1616 } | |
1617 | |
1618 return str_to_utf8(title); | |
1619 } | |
1620 | |
1621 gint | |
1622 playlist_get_songtime(guint pos) | |
1623 { | |
1624 gint song_time = -1; | |
1625 PlaylistEntry *entry; | |
1626 GList *node; | |
1627 | |
1628 PLAYLIST_LOCK(); | |
1629 | |
1630 if (!playlist) { | |
1631 PLAYLIST_UNLOCK(); | |
1632 return -1; | |
1633 } | |
1634 | |
1635 if (!(node = g_list_nth(playlist, pos))) { | |
1636 PLAYLIST_UNLOCK(); | |
1637 return -1; | |
1638 } | |
1639 | |
1640 entry = node->data; | |
1641 | |
1642 if (!entry->title && entry->length == -1) { | |
1643 if (playlist_entry_get_info(entry)) | |
1644 song_time = entry->length; | |
1645 | |
1646 PLAYLIST_UNLOCK(); | |
1647 } | |
1648 else { | |
1649 song_time = entry->length; | |
1650 PLAYLIST_UNLOCK(); | |
1651 } | |
1652 | |
1653 return song_time; | |
1654 } | |
1655 | |
1656 static gint | |
1657 playlist_compare_title(const PlaylistEntry * a, | |
1658 const PlaylistEntry * b) | |
1659 { | |
1660 const gchar *a_title, *b_title; | |
1661 | |
1662 g_return_val_if_fail(a != NULL, 0); | |
1663 g_return_val_if_fail(b != NULL, 0); | |
1664 | |
1665 if (a->title) | |
1666 a_title = a->title; | |
1667 else { | |
1668 if (strrchr(a->filename, '/')) | |
1669 a_title = strrchr(a->filename, '/') + 1; | |
1670 else | |
1671 a_title = a->filename; | |
1672 } | |
1673 | |
1674 if (b->title) | |
1675 b_title = b->title; | |
1676 else { | |
1677 if (strrchr(a->filename, '/')) | |
1678 b_title = strrchr(b->filename, '/') + 1; | |
1679 else | |
1680 b_title = b->filename; | |
1681 } | |
1682 | |
1683 return strcasecmp(a_title, b_title); | |
1684 } | |
1685 | |
1686 static gint | |
1687 playlist_compare_filename(const PlaylistEntry * a, | |
1688 const PlaylistEntry * b) | |
1689 { | |
1690 gchar *a_filename, *b_filename; | |
1691 | |
1692 g_return_val_if_fail(a != NULL, 0); | |
1693 g_return_val_if_fail(b != NULL, 0); | |
1694 | |
1695 if (strrchr(a->filename, '/')) | |
1696 a_filename = strrchr(a->filename, '/') + 1; | |
1697 else | |
1698 a_filename = a->filename; | |
1699 | |
1700 if (strrchr(b->filename, '/')) | |
1701 b_filename = strrchr(b->filename, '/') + 1; | |
1702 else | |
1703 b_filename = b->filename; | |
1704 | |
1705 | |
1706 return strcasecmp(a_filename, b_filename); | |
1707 } | |
1708 | |
1709 static gint | |
1710 path_compare(const gchar * a, const gchar * b) | |
1711 { | |
1712 gchar *posa, *posb; | |
1713 gint len, ret; | |
1714 | |
1715 posa = strrchr(a, '/'); | |
1716 posb = strrchr(b, '/'); | |
1717 | |
1718 /* | |
1719 * Sort directories before files | |
1720 */ | |
1721 if (posa && posb && (posa - a != posb - b)) { | |
1722 if (posa - a > posb - b) { | |
1723 len = posb - b; | |
1724 ret = -1; | |
1725 } | |
1726 else { | |
1727 len = posa - a; | |
1728 ret = 1; | |
1729 } | |
1730 if (!strncasecmp(a, b, len)) | |
1731 return ret; | |
1732 } | |
1733 return strcasecmp(a, b); | |
1734 } | |
1735 | |
1736 static gint | |
1737 playlist_compare_path(const PlaylistEntry * a, | |
1738 const PlaylistEntry * b) | |
1739 { | |
1740 return path_compare(a->filename, b->filename); | |
1741 } | |
1742 | |
1743 static gint | |
1744 playlist_compare_date(const PlaylistEntry * a, | |
1745 const PlaylistEntry * b) | |
1746 { | |
1747 struct stat buf; | |
1748 time_t modtime; | |
1749 | |
1750 gint rv; | |
1751 | |
1752 | |
1753 rv = stat(a->filename, &buf); | |
1754 | |
1755 if (rv == 0) { | |
1756 modtime = buf.st_mtime; | |
1757 rv = stat(b->filename, &buf); | |
1758 | |
1759 if (stat(b->filename, &buf) == 0) { | |
1760 if (buf.st_mtime == modtime) | |
1761 return 0; | |
1762 else | |
1763 return (buf.st_mtime - modtime) > 0 ? -1 : 1; | |
1764 } | |
1765 else | |
1766 return -1; | |
1767 } | |
1768 else if (!lstat(b->filename, &buf)) | |
1769 return 1; | |
1770 else | |
1771 return playlist_compare_filename(a, b); | |
1772 } | |
1773 | |
1774 | |
1775 void | |
1776 playlist_sort(PlaylistSortType type) | |
1777 { | |
1778 playlist_remove_dead_files(); | |
1779 PLAYLIST_LOCK(); | |
1780 playlist = | |
1781 g_list_sort(playlist, | |
1782 (GCompareFunc) playlist_compare_func_table[type]); | |
1783 PLAYLIST_UNLOCK(); | |
1784 } | |
1785 | |
1786 static GList * | |
1787 playlist_sort_selected_generic(GList * list, GCompareFunc cmpfunc) | |
1788 { | |
1789 GList *list1, *list2; | |
1790 GList *tmp_list = NULL; | |
1791 GList *index_list = NULL; | |
1792 | |
1793 /* | |
1794 * We take all the selected entries out of the playlist, | |
1795 * sorts them, and then put them back in again. | |
1796 */ | |
1797 | |
1798 list1 = g_list_last(list); | |
1799 | |
1800 while (list1) { | |
1801 list2 = g_list_previous(list1); | |
1802 if (PLAYLIST_ENTRY(list1->data)->selected) { | |
1803 gpointer idx; | |
1804 idx = GINT_TO_POINTER(g_list_position(list, list1)); | |
1805 index_list = g_list_prepend(index_list, idx); | |
1806 list = g_list_remove_link(list, list1); | |
1807 tmp_list = g_list_concat(list1, tmp_list); | |
1808 } | |
1809 list1 = list2; | |
1810 } | |
1811 | |
1812 tmp_list = g_list_sort(tmp_list, cmpfunc); | |
1813 list1 = tmp_list; | |
1814 list2 = index_list; | |
1815 | |
1816 while (list2) { | |
1817 if (!list1) { | |
1818 g_critical(G_STRLOC ": Error during list sorting. " | |
1819 "Possibly dropped some playlist-entries."); | |
1820 break; | |
1821 } | |
1822 | |
1823 list = g_list_insert(list, list1->data, GPOINTER_TO_INT(list2->data)); | |
1824 | |
1825 list2 = g_list_next(list2); | |
1826 list1 = g_list_next(list1); | |
1827 } | |
1828 | |
1829 g_list_free(index_list); | |
1830 g_list_free(tmp_list); | |
1831 | |
1832 return list; | |
1833 } | |
1834 | |
1835 void | |
1836 playlist_sort_selected(PlaylistSortType type) | |
1837 { | |
1838 PLAYLIST_LOCK(); | |
1839 playlist = playlist_sort_selected_generic(playlist, (GCompareFunc) | |
1840 playlist_compare_func_table | |
1841 [type]); | |
1842 PLAYLIST_UNLOCK(); | |
1843 } | |
1844 | |
1845 void | |
1846 playlist_reverse(void) | |
1847 { | |
1848 PLAYLIST_LOCK(); | |
1849 playlist = g_list_reverse(playlist); | |
1850 PLAYLIST_UNLOCK(); | |
1851 } | |
1852 | |
1853 static GList * | |
1854 playlist_shuffle_list(GList * list) | |
1855 { | |
1856 /* | |
1857 * Note that this doesn't make a copy of the original list. | |
1858 * The pointer to the original list is not valid after this | |
1859 * fuction is run. | |
1860 */ | |
1861 gint len = g_list_length(list); | |
1862 gint i, j; | |
1863 GList *node, **ptrs; | |
1864 | |
1865 REQUIRE_STATIC_LOCK(playlist); | |
1866 | |
1867 if (!len) | |
1868 return NULL; | |
1869 | |
1870 ptrs = g_new(GList *, len); | |
1871 | |
1872 for (node = list, i = 0; i < len; node = g_list_next(node), i++) | |
1873 ptrs[i] = node; | |
1874 | |
1875 j = g_random_int_range(0, len); | |
1876 list = ptrs[j]; | |
1877 ptrs[j]->next = NULL; | |
1878 ptrs[j] = ptrs[0]; | |
1879 | |
1880 for (i = 1; i < len; i++) { | |
1881 j = g_random_int_range(0, len - i); | |
1882 list->prev = ptrs[i + j]; | |
1883 ptrs[i + j]->next = list; | |
1884 list = ptrs[i + j]; | |
1885 ptrs[i + j] = ptrs[i]; | |
1886 } | |
1887 list->prev = NULL; | |
1888 | |
1889 g_free(ptrs); | |
1890 | |
1891 return list; | |
1892 } | |
1893 | |
1894 void | |
1895 playlist_random(void) | |
1896 { | |
1897 PLAYLIST_LOCK(); | |
1898 playlist = playlist_shuffle_list(playlist); | |
1899 PLAYLIST_UNLOCK(); | |
1900 } | |
1901 | |
1902 GList * | |
1903 playlist_get_selected(void) | |
1904 { | |
1905 GList *node, *list = NULL; | |
1906 gint i = 0; | |
1907 | |
1908 PLAYLIST_LOCK(); | |
1909 for (node = playlist_get(); node; node = g_list_next(node), i++) { | |
1910 PlaylistEntry *entry = node->data; | |
1911 if (entry->selected) | |
1912 list = g_list_prepend(list, GINT_TO_POINTER(i)); | |
1913 } | |
1914 PLAYLIST_UNLOCK(); | |
1915 return g_list_reverse(list); | |
1916 } | |
1917 | |
1918 void | |
1919 playlist_clear_selected(void) | |
1920 { | |
1921 GList *node = NULL; | |
1922 gint i = 0; | |
1923 | |
1924 PLAYLIST_LOCK(); | |
1925 for (node = playlist_get(); node; node = g_list_next(node), i++) { | |
1926 PLAYLIST_ENTRY(node->data)->selected = FALSE; | |
1927 } | |
1928 PLAYLIST_UNLOCK(); | |
1929 playlist_recalc_total_time(); | |
1930 } | |
1931 | |
1932 gint | |
1933 playlist_get_num_selected(void) | |
1934 { | |
1935 GList *node; | |
1936 gint num = 0; | |
1937 | |
1938 PLAYLIST_LOCK(); | |
1939 for (node = playlist_get(); node; node = g_list_next(node)) { | |
1940 PlaylistEntry *entry = node->data; | |
1941 if (entry->selected) | |
1942 num++; | |
1943 } | |
1944 PLAYLIST_UNLOCK(); | |
1945 return num; | |
1946 } | |
1947 | |
1948 | |
1949 static void | |
1950 playlist_generate_shuffle_list(void) | |
1951 { | |
1952 PLAYLIST_LOCK(); | |
1953 playlist_generate_shuffle_list_nolock(); | |
1954 PLAYLIST_UNLOCK(); | |
1955 } | |
1956 | |
1957 static void | |
1958 playlist_generate_shuffle_list_nolock(void) | |
1959 { | |
1960 GList *node; | |
1961 gint numsongs; | |
1962 | |
1963 REQUIRE_STATIC_LOCK(playlist); | |
1964 | |
1965 if (shuffle_list) { | |
1966 g_list_free(shuffle_list); | |
1967 shuffle_list = NULL; | |
1968 } | |
1969 | |
1970 if (!cfg.shuffle || !playlist) | |
1971 return; | |
1972 | |
1973 shuffle_list = playlist_shuffle_list(g_list_copy(playlist)); | |
1974 numsongs = g_list_length(shuffle_list); | |
1975 | |
1976 if (playlist_position) { | |
1977 gint i = g_list_index(shuffle_list, playlist_position); | |
1978 node = g_list_nth(shuffle_list, i); | |
1979 shuffle_list = g_list_remove_link(shuffle_list, node); | |
1980 shuffle_list = g_list_prepend(shuffle_list, node->data); | |
1981 } | |
1982 } | |
1983 | |
1984 void | |
1985 playlist_fileinfo(guint pos) | |
1986 { | |
1987 gchar *path = NULL; | |
1988 GList *node; | |
1989 | |
1990 PLAYLIST_LOCK(); | |
1991 if ((node = g_list_nth(playlist_get(), pos))) { | |
1992 PlaylistEntry *entry = node->data; | |
1993 path = g_strdup(entry->filename); | |
1994 } | |
1995 PLAYLIST_UNLOCK(); | |
1996 if (path) { | |
1997 input_file_info_box(path); | |
1998 g_free(path); | |
1999 } | |
2000 } | |
2001 | |
2002 void | |
2003 playlist_fileinfo_current(void) | |
2004 { | |
2005 gchar *path = NULL; | |
2006 | |
2007 PLAYLIST_LOCK(); | |
2008 if (playlist_get() && playlist_position) | |
2009 path = g_strdup(playlist_position->filename); | |
2010 PLAYLIST_UNLOCK(); | |
2011 | |
2012 if (path) { | |
2013 input_file_info_box(path); | |
2014 g_free(path); | |
2015 } | |
2016 } | |
2017 | |
2018 | |
2019 static gboolean | |
2020 playlist_get_info_is_going(void) | |
2021 { | |
2022 gboolean result; | |
2023 | |
2024 G_LOCK(playlist_get_info_going); | |
2025 result = playlist_get_info_going; | |
2026 G_UNLOCK(playlist_get_info_going); | |
2027 | |
2028 return result; | |
2029 } | |
2030 | |
2031 static gpointer | |
2032 playlist_get_info_func(gpointer arg) | |
2033 { | |
2034 GList *node; | |
2035 gboolean update_playlistwin = FALSE; | |
2036 gboolean update_mainwin = FALSE; | |
2037 | |
2038 while (playlist_get_info_is_going()) { | |
2039 PlaylistEntry *entry; | |
2040 | |
2041 if (cfg.use_pl_metadata && | |
2042 cfg.get_info_on_load && | |
2043 playlist_get_info_scan_active) { | |
2044 | |
2045 PLAYLIST_LOCK(); | |
2046 for (node = playlist_get(); node; node = g_list_next(node)) { | |
2047 entry = node->data; | |
2048 | |
2049 if (entry->title || entry->length != -1) | |
2050 continue; | |
2051 | |
2052 if (!playlist_entry_get_info(entry)) { | |
2053 if (g_list_index(playlist_get(), entry) == -1) | |
2054 /* Entry disappeared while we looked it up. | |
2055 Restart. */ | |
2056 node = playlist_get(); | |
2057 } | |
2058 else if (entry->title || entry->length != -1) { | |
2059 update_playlistwin = TRUE; | |
2060 if (entry == playlist_position) | |
2061 update_mainwin = TRUE; | |
2062 break; | |
2063 } | |
2064 } | |
2065 PLAYLIST_UNLOCK(); | |
2066 | |
2067 if (!node) | |
2068 playlist_get_info_scan_active = FALSE; | |
2069 } | |
2070 else if (!cfg.get_info_on_load && | |
2071 cfg.get_info_on_demand && | |
2072 cfg.playlist_visible && | |
2073 !cfg.playlist_shaded && | |
2074 cfg.use_pl_metadata) { | |
2075 | |
2076 gboolean found = FALSE; | |
2077 | |
2078 PLAYLIST_LOCK(); | |
2079 | |
2080 if (!playlist_get()) { | |
2081 PLAYLIST_UNLOCK(); | |
2082 g_usleep(1000000); | |
2083 continue; | |
2084 } | |
2085 | |
2086 for (node = | |
2087 g_list_nth(playlist_get(), playlistwin_get_toprow()); | |
2088 node | |
2089 && | |
2090 playlistwin_item_visible(g_list_position | |
2091 (playlist_get(), node)); | |
2092 node = g_list_next(node)) { | |
2093 entry = node->data; | |
2094 if (entry->title || entry->length != -1) | |
2095 continue; | |
2096 | |
2097 if (!playlist_entry_get_info(entry)) { | |
2098 if (g_list_index(playlist_get(), entry) == -1) | |
2099 /* Entry disapeared while we | |
2100 looked it up. Restart. */ | |
2101 node = | |
2102 g_list_nth(playlist_get(), | |
2103 playlistwin_get_toprow()); | |
2104 } | |
2105 else if (entry->title || entry->length != -1) { | |
2106 update_playlistwin = TRUE; | |
2107 if (entry == playlist_position) | |
2108 update_mainwin = TRUE; | |
2109 found = TRUE; | |
2110 break; | |
2111 } | |
2112 } | |
2113 | |
2114 PLAYLIST_UNLOCK(); | |
2115 | |
2116 if (!found) { | |
2117 g_usleep(500000); | |
2118 continue; | |
2119 } | |
2120 } | |
2121 else | |
2122 g_usleep(500000); | |
2123 | |
2124 if (update_playlistwin) { | |
2125 playlistwin_update_list(); | |
2126 update_playlistwin = FALSE; | |
2127 } | |
2128 | |
2129 if (update_mainwin) { | |
2130 mainwin_set_info_text(); | |
2131 update_mainwin = FALSE; | |
2132 } | |
2133 } | |
2134 | |
2135 g_thread_exit(NULL); | |
2136 return NULL; | |
2137 } | |
2138 | |
2139 void | |
2140 playlist_start_get_info_thread(void) | |
2141 { | |
2142 playlist_get_info_going = TRUE; | |
2143 playlist_get_info_thread = g_thread_create(playlist_get_info_func, | |
2144 NULL, TRUE, NULL); | |
2145 } | |
2146 | |
2147 void | |
2148 playlist_stop_get_info_thread(void) | |
2149 { | |
2150 G_LOCK(playlist_get_info_going); | |
2151 playlist_get_info_going = FALSE; | |
2152 G_UNLOCK(playlist_get_info_going); | |
2153 g_thread_join(playlist_get_info_thread); | |
2154 } | |
2155 | |
2156 void | |
2157 playlist_start_get_info_scan(void) | |
2158 { | |
2159 playlist_get_info_scan_active = TRUE; | |
2160 } | |
2161 | |
2162 void | |
2163 playlist_remove_dead_files(void) | |
2164 { | |
2165 GList *node, *next_node; | |
2166 | |
2167 PLAYLIST_LOCK(); | |
2168 | |
2169 for (node = playlist; node; node = next_node) { | |
2170 PlaylistEntry *entry = PLAYLIST_ENTRY(node->data); | |
2171 next_node = g_list_next(node); | |
2172 | |
2173 if (!entry || !entry->filename) { | |
2174 g_message(G_STRLOC ": Playlist entry is invalid!"); | |
2175 continue; | |
2176 } | |
2177 | |
2178 /* FIXME: What about 'file:///'? */ | |
2179 /* Don't kill URLs */ | |
2180 if (strstr(entry->filename, "://")) | |
2181 continue; | |
2182 | |
2183 /* FIXME: Should test for readability */ | |
2184 if (vfs_file_test(entry->filename, G_FILE_TEST_EXISTS)) | |
2185 continue; | |
2186 | |
2187 if (entry == playlist_position) { | |
2188 /* Don't remove the currently playing song */ | |
2189 if (bmp_playback_get_playing()) | |
2190 continue; | |
2191 | |
2192 if (next_node) | |
2193 playlist_position = PLAYLIST_ENTRY(next_node->data); | |
2194 else | |
2195 playlist_position = NULL; | |
2196 } | |
2197 | |
2198 playlist_entry_free(entry); | |
2199 playlist = g_list_delete_link(playlist, node); | |
2200 } | |
2201 | |
2202 PLAYLIST_UNLOCK(); | |
2203 | |
2204 playlist_generate_shuffle_list(); | |
2205 playlistwin_update_list(); | |
2206 playlist_recalc_total_time(); | |
2207 } | |
2208 | |
2209 static gulong pl_total_time = 0, pl_selection_time = 0; | |
2210 static gboolean pl_total_more = FALSE, pl_selection_more = FALSE; | |
2211 | |
2212 void | |
2213 playlist_get_total_time(gulong * total_time, | |
2214 gulong * selection_time, | |
2215 gboolean * total_more, | |
2216 gboolean * selection_more) | |
2217 { | |
2218 PLAYLIST_LOCK(); | |
2219 *total_time = pl_total_time; | |
2220 *selection_time = pl_selection_time; | |
2221 *total_more = pl_total_more; | |
2222 *selection_more = pl_selection_more; | |
2223 PLAYLIST_UNLOCK(); | |
2224 } | |
2225 | |
2226 | |
2227 static void | |
2228 playlist_recalc_total_time_nolock(void) | |
2229 { | |
2230 GList *list; | |
2231 PlaylistEntry *entry; | |
2232 | |
2233 REQUIRE_STATIC_LOCK(playlist); | |
2234 | |
2235 pl_total_time = 0; | |
2236 pl_selection_time = 0; | |
2237 pl_total_more = FALSE; | |
2238 pl_selection_more = FALSE; | |
2239 | |
2240 for (list = playlist_get(); list; list = g_list_next(list)) { | |
2241 entry = list->data; | |
2242 | |
2243 if (entry->length != -1) | |
2244 pl_total_time += entry->length / 1000; | |
2245 else | |
2246 pl_total_more = TRUE; | |
2247 | |
2248 if (entry->selected) { | |
2249 if (entry->length != -1) | |
2250 pl_selection_time += entry->length / 1000; | |
2251 else | |
2252 pl_selection_more = TRUE; | |
2253 } | |
2254 } | |
2255 } | |
2256 | |
2257 static void | |
2258 playlist_recalc_total_time(void) | |
2259 { | |
2260 PLAYLIST_LOCK(); | |
2261 playlist_recalc_total_time_nolock(); | |
2262 PLAYLIST_UNLOCK(); | |
2263 } | |
2264 | |
2265 | |
2266 void | |
2267 playlist_select_all(gboolean set) | |
2268 { | |
2269 GList *list; | |
2270 | |
2271 PLAYLIST_LOCK(); | |
2272 | |
2273 for (list = playlist_get(); list; list = g_list_next(list)) { | |
2274 PlaylistEntry *entry = list->data; | |
2275 entry->selected = set; | |
2276 } | |
2277 | |
2278 PLAYLIST_UNLOCK(); | |
2279 playlist_recalc_total_time(); | |
2280 } | |
2281 | |
2282 void | |
2283 playlist_select_invert_all(void) | |
2284 { | |
2285 GList *list; | |
2286 | |
2287 PLAYLIST_LOCK(); | |
2288 | |
2289 for (list = playlist_get(); list; list = g_list_next(list)) { | |
2290 PlaylistEntry *entry = list->data; | |
2291 entry->selected = !entry->selected; | |
2292 } | |
2293 | |
2294 PLAYLIST_UNLOCK(); | |
2295 playlist_recalc_total_time(); | |
2296 } | |
2297 | |
2298 gboolean | |
2299 playlist_select_invert(guint pos) | |
2300 { | |
2301 GList *list; | |
2302 gboolean invert_ok = FALSE; | |
2303 | |
2304 PLAYLIST_LOCK(); | |
2305 | |
2306 if ((list = g_list_nth(playlist_get(), pos))) { | |
2307 PlaylistEntry *entry = list->data; | |
2308 entry->selected = !entry->selected; | |
2309 invert_ok = TRUE; | |
2310 } | |
2311 | |
2312 PLAYLIST_UNLOCK(); | |
2313 playlist_recalc_total_time(); | |
2314 | |
2315 return invert_ok; | |
2316 } | |
2317 | |
2318 | |
2319 void | |
2320 playlist_select_range(gint min_pos, gint max_pos, gboolean select) | |
2321 { | |
2322 GList *list; | |
2323 gint i; | |
2324 | |
2325 if (min_pos > max_pos) | |
2326 SWAP(min_pos, max_pos); | |
2327 | |
2328 PLAYLIST_LOCK(); | |
2329 | |
2330 list = g_list_nth(playlist_get(), min_pos); | |
2331 for (i = min_pos; i <= max_pos && list; i++) { | |
2332 PlaylistEntry *entry = list->data; | |
2333 entry->selected = select; | |
2334 list = g_list_next(list); | |
2335 } | |
2336 | |
2337 PLAYLIST_UNLOCK(); | |
2338 | |
2339 playlist_recalc_total_time(); | |
2340 } | |
2341 | |
2342 gboolean | |
2343 playlist_read_info_selection(void) | |
2344 { | |
2345 GList *node; | |
2346 gboolean retval = FALSE; | |
2347 | |
2348 PLAYLIST_LOCK(); | |
2349 | |
2350 for (node = playlist_get(); node; node = g_list_next(node)) { | |
2351 PlaylistEntry *entry = node->data; | |
2352 if (!entry->selected) | |
2353 continue; | |
2354 | |
2355 retval = TRUE; | |
2356 | |
2357 str_replace_in(&entry->title, NULL); | |
2358 entry->length = -1; | |
2359 | |
2360 if (!playlist_entry_get_info(entry)) { | |
2361 if (g_list_index(playlist_get(), entry) == -1) | |
2362 /* Entry disappeared while we looked it up. Restart. */ | |
2363 node = playlist_get(); | |
2364 } | |
2365 } | |
2366 | |
2367 PLAYLIST_UNLOCK(); | |
2368 | |
2369 playlistwin_update_list(); | |
2370 playlist_recalc_total_time(); | |
2371 | |
2372 return retval; | |
2373 } | |
2374 | |
2375 void | |
2376 playlist_read_info(guint pos) | |
2377 { | |
2378 GList *node; | |
2379 | |
2380 PLAYLIST_LOCK(); | |
2381 | |
2382 if ((node = g_list_nth(playlist_get(), pos))) { | |
2383 PlaylistEntry *entry = node->data; | |
2384 str_replace_in(&entry->title, NULL); | |
2385 entry->length = -1; | |
2386 playlist_entry_get_info(entry); | |
2387 } | |
2388 | |
2389 PLAYLIST_UNLOCK(); | |
2390 | |
2391 playlistwin_update_list(); | |
2392 playlist_recalc_total_time(); | |
2393 } | |
2394 | |
2395 void | |
2396 playlist_set_shuffle(gboolean shuffle) | |
2397 { | |
2398 PLAYLIST_LOCK(); | |
2399 | |
2400 cfg.shuffle = shuffle; | |
2401 playlist_generate_shuffle_list_nolock(); | |
2402 | |
2403 PLAYLIST_UNLOCK(); | |
2404 } | |
2405 | |
2406 void | |
2407 playlist_new(void) | |
2408 { | |
2409 playlist_set_current_name(NULL); | |
2410 playlist_clear(); | |
2411 mainwin_clear_song_info(); | |
2412 mainwin_set_info_text(); | |
2413 } | |
2414 | |
2415 | |
2416 const gchar * | |
2417 playlist_get_filename_to_play(void) | |
2418 { | |
2419 const gchar *filename = NULL; | |
2420 | |
2421 PLAYLIST_LOCK(); | |
2422 | |
2423 if (playlist) { | |
2424 if (!playlist_position) { | |
2425 if (cfg.shuffle) | |
2426 playlist_position = shuffle_list->data; | |
2427 else | |
2428 playlist_position = playlist->data; | |
2429 } | |
2430 | |
2431 filename = playlist_position->filename; | |
2432 } | |
2433 | |
2434 PLAYLIST_UNLOCK(); | |
2435 | |
2436 return filename; | |
2437 } | |
356
99928e1275a1
[svn] This commit reduces the amount of times we probe a source down to ONE
nenolod
parents:
355
diff
changeset
|
2438 |
398
f908bcd87c3d
[svn] Generate cache content on demand if it was not previously there.
nenolod
parents:
397
diff
changeset
|
2439 PlaylistEntry * |
356
99928e1275a1
[svn] This commit reduces the amount of times we probe a source down to ONE
nenolod
parents:
355
diff
changeset
|
2440 playlist_get_entry_to_play(void) |
99928e1275a1
[svn] This commit reduces the amount of times we probe a source down to ONE
nenolod
parents:
355
diff
changeset
|
2441 { |
99928e1275a1
[svn] This commit reduces the amount of times we probe a source down to ONE
nenolod
parents:
355
diff
changeset
|
2442 PLAYLIST_LOCK(); |
99928e1275a1
[svn] This commit reduces the amount of times we probe a source down to ONE
nenolod
parents:
355
diff
changeset
|
2443 |
99928e1275a1
[svn] This commit reduces the amount of times we probe a source down to ONE
nenolod
parents:
355
diff
changeset
|
2444 if (playlist) { |
99928e1275a1
[svn] This commit reduces the amount of times we probe a source down to ONE
nenolod
parents:
355
diff
changeset
|
2445 if (!playlist_position) { |
99928e1275a1
[svn] This commit reduces the amount of times we probe a source down to ONE
nenolod
parents:
355
diff
changeset
|
2446 if (cfg.shuffle) |
99928e1275a1
[svn] This commit reduces the amount of times we probe a source down to ONE
nenolod
parents:
355
diff
changeset
|
2447 playlist_position = shuffle_list->data; |
99928e1275a1
[svn] This commit reduces the amount of times we probe a source down to ONE
nenolod
parents:
355
diff
changeset
|
2448 else |
99928e1275a1
[svn] This commit reduces the amount of times we probe a source down to ONE
nenolod
parents:
355
diff
changeset
|
2449 playlist_position = playlist->data; |
99928e1275a1
[svn] This commit reduces the amount of times we probe a source down to ONE
nenolod
parents:
355
diff
changeset
|
2450 } |
99928e1275a1
[svn] This commit reduces the amount of times we probe a source down to ONE
nenolod
parents:
355
diff
changeset
|
2451 } |
99928e1275a1
[svn] This commit reduces the amount of times we probe a source down to ONE
nenolod
parents:
355
diff
changeset
|
2452 |
99928e1275a1
[svn] This commit reduces the amount of times we probe a source down to ONE
nenolod
parents:
355
diff
changeset
|
2453 PLAYLIST_UNLOCK(); |
99928e1275a1
[svn] This commit reduces the amount of times we probe a source down to ONE
nenolod
parents:
355
diff
changeset
|
2454 |
99928e1275a1
[svn] This commit reduces the amount of times we probe a source down to ONE
nenolod
parents:
355
diff
changeset
|
2455 return playlist_position; |
99928e1275a1
[svn] This commit reduces the amount of times we probe a source down to ONE
nenolod
parents:
355
diff
changeset
|
2456 } |