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