Mercurial > audlegacy
annotate audacious/playlist.c @ 1175:d03157b6b5dd trunk
[svn] prefswin_page_new():
This commit allows third party plugins to register directly into the preferences panel.
This concept originates from Winamp3/Wasabi.Player (I always thought it was cool, myself, so here it is.)
author | nenolod |
---|---|
date | Sun, 11 Jun 2006 20:11:40 -0700 |
parents | 6fe822f5a3c0 |
children | 56b57ed0a136 |
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 | |
1165 | 511 if (loading_playlist == TRUE || cfg.playlist_detect == TRUE) |
397
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 |
1165 | 516 if (cfg.playlist_detect == TRUE || loading_playlist == TRUE || (loading_playlist == FALSE && dec != NULL)) |
397
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 } | |
1165 | 663 else if (cfg.playlist_detect == TRUE) |
664 list = g_list_prepend(list, filename); | |
0 | 665 else if (input_check_file(filename, TRUE)) |
666 list = g_list_prepend(list, filename); | |
667 else | |
668 g_free(filename); | |
669 | |
670 while (background && gtk_events_pending()) | |
671 gtk_main_iteration(); | |
672 } | |
673 g_dir_close(dir); | |
674 | |
675 return list; | |
676 } | |
677 | |
678 gboolean | |
679 playlist_add(const gchar * filename) | |
680 { | |
681 return playlist_ins(filename, -1); | |
682 } | |
683 | |
684 guint | |
685 playlist_add_dir(const gchar * directory) | |
686 { | |
687 return playlist_ins_dir(directory, -1, TRUE); | |
688 } | |
689 | |
690 guint | |
691 playlist_add_url(const gchar * url) | |
692 { | |
693 return playlist_ins_url(url, -1); | |
694 } | |
695 | |
696 guint | |
697 playlist_ins_dir(const gchar * path, | |
698 gint pos, | |
699 gboolean background) | |
700 { | |
701 guint entries = 0; | |
702 GList *list, *node; | |
703 GHashTable *htab; | |
704 | |
705 htab = g_hash_table_new(devino_hash, devino_compare); | |
706 | |
707 list = playlist_dir_find_files(path, background, htab); | |
708 list = g_list_sort(list, (GCompareFunc) path_compare); | |
709 | |
710 g_hash_table_foreach_remove(htab, devino_destroy, NULL); | |
711 | |
712 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
|
713 __playlist_ins(node->data, pos, NULL); |
0 | 714 g_free(node->data); |
715 entries++; | |
716 if (pos >= 0) | |
717 pos++; | |
718 } | |
719 | |
720 g_list_free(list); | |
721 | |
722 playlist_recalc_total_time(); | |
723 playlist_generate_shuffle_list(); | |
724 playlistwin_update_list(); | |
725 return entries; | |
726 } | |
727 | |
728 guint | |
729 playlist_ins_url(const gchar * string, | |
730 gint pos) | |
731 { | |
732 gchar *tmp; | |
733 gint i = 1, entries = 0; | |
734 gboolean first = TRUE; | |
735 guint firstpos = 0; | |
736 gboolean success = FALSE; | |
737 gchar *decoded = NULL; | |
738 | |
739 g_return_val_if_fail(string != NULL, 0); | |
740 | |
741 playlistwin_update_list(); | |
742 | |
743 while (*string) { | |
744 GList *node; | |
745 tmp = strchr(string, '\n'); | |
746 if (tmp) { | |
747 if (*(tmp - 1) == '\r') | |
748 *(tmp - 1) = '\0'; | |
749 *tmp = '\0'; | |
750 } | |
751 | |
752 if (!(decoded = xmms_urldecode_path(string))) | |
753 decoded = g_strdup(string); | |
754 | |
755 if (g_file_test(decoded, G_FILE_TEST_IS_DIR)) { | |
756 i = playlist_ins_dir(decoded, pos, FALSE); | |
757 } | |
758 else { | |
759 if (is_playlist_name(decoded)) { | |
760 i = playlist_load_ins(decoded, pos); | |
761 } | |
762 else { | |
763 success = playlist_ins(decoded, pos); | |
764 i = 1; | |
765 } | |
766 } | |
767 | |
768 g_free(decoded); | |
769 | |
770 PLAYLIST_LOCK(); | |
771 node = g_list_nth(playlist_get(), pos); | |
772 PLAYLIST_UNLOCK(); | |
773 | |
774 entries += i; | |
775 | |
776 if (first) { | |
777 first = FALSE; | |
778 firstpos = pos; | |
779 } | |
780 | |
781 if (pos >= 0) | |
782 pos += i; | |
783 if (!tmp) | |
784 break; | |
785 | |
786 string = tmp + 1; | |
787 } | |
788 | |
789 playlist_recalc_total_time(); | |
790 playlist_generate_shuffle_list(); | |
791 playlistwin_update_list(); | |
792 | |
793 return entries; | |
794 } | |
795 | |
796 void | |
797 playlist_set_info(const gchar * title, gint length, gint rate, | |
798 gint freq, gint nch) | |
799 { | |
800 PLAYLIST_LOCK(); | |
801 | |
802 if (playlist_position) { | |
803 g_free(playlist_position->title); | |
804 playlist_position->title = g_strdup(title); | |
805 playlist_position->length = length; | |
806 } | |
807 | |
808 PLAYLIST_UNLOCK(); | |
809 | |
810 playlist_recalc_total_time(); | |
811 | |
812 mainwin_set_song_info(rate, freq, nch); | |
813 } | |
814 | |
815 void | |
816 playlist_check_pos_current(void) | |
817 { | |
818 gint pos, row, bottom; | |
819 | |
820 PLAYLIST_LOCK(); | |
821 if (!playlist || !playlist_position || !playlistwin_list) { | |
822 PLAYLIST_UNLOCK(); | |
823 return; | |
824 } | |
825 | |
826 pos = g_list_index(playlist, playlist_position); | |
827 | |
828 if (playlistwin_item_visible(pos)) { | |
829 PLAYLIST_UNLOCK(); | |
830 return; | |
831 } | |
832 | |
833 bottom = MAX(0, playlist_get_length_nolock() - | |
834 playlistwin_list->pl_num_visible); | |
835 row = CLAMP(pos - playlistwin_list->pl_num_visible / 2, 0, bottom); | |
836 PLAYLIST_UNLOCK(); | |
837 playlistwin_set_toprow(row); | |
838 } | |
839 | |
840 void | |
841 playlist_next(void) | |
842 { | |
843 GList *plist_pos_list; | |
844 gboolean restart_playing = FALSE; | |
845 | |
846 PLAYLIST_LOCK(); | |
847 if (!playlist) { | |
848 PLAYLIST_UNLOCK(); | |
849 return; | |
850 } | |
851 | |
852 plist_pos_list = find_playlist_position_list(); | |
853 | |
1026 | 854 if (!cfg.repeat && !g_list_next(plist_pos_list) && !queued_list) { |
0 | 855 PLAYLIST_UNLOCK(); |
856 return; | |
857 } | |
858 | |
859 if (bmp_playback_get_playing()) { | |
860 /* We need to stop before changing playlist_position */ | |
861 PLAYLIST_UNLOCK(); | |
895
1b919783797e
[svn] - should fix the ability to change songs while paused
nhjm449
parents:
853
diff
changeset
|
862 ip_data.stop = TRUE; |
0 | 863 bmp_playback_stop(); |
895
1b919783797e
[svn] - should fix the ability to change songs while paused
nhjm449
parents:
853
diff
changeset
|
864 ip_data.stop = FALSE; |
0 | 865 PLAYLIST_LOCK(); |
866 restart_playing = TRUE; | |
867 } | |
868 | |
869 plist_pos_list = find_playlist_position_list(); | |
870 if (queued_list) | |
871 play_queued(); | |
872 else if (g_list_next(plist_pos_list)) | |
873 playlist_position = g_list_next(plist_pos_list)->data; | |
874 else if (cfg.repeat) { | |
875 playlist_position = NULL; | |
876 playlist_generate_shuffle_list_nolock(); | |
877 if (cfg.shuffle) | |
878 playlist_position = shuffle_list->data; | |
879 else | |
880 playlist_position = playlist->data; | |
881 } | |
882 PLAYLIST_UNLOCK(); | |
883 playlist_check_pos_current(); | |
884 | |
885 if (restart_playing) | |
886 bmp_playback_initiate(); | |
887 else { | |
888 mainwin_set_info_text(); | |
889 playlistwin_update_list(); | |
890 } | |
891 } | |
892 | |
893 void | |
894 playlist_prev(void) | |
895 { | |
896 GList *plist_pos_list; | |
897 gboolean restart_playing = FALSE; | |
898 | |
899 PLAYLIST_LOCK(); | |
900 if (!playlist) { | |
901 PLAYLIST_UNLOCK(); | |
902 return; | |
903 } | |
904 | |
905 plist_pos_list = find_playlist_position_list(); | |
906 | |
907 if (!cfg.repeat && !g_list_previous(plist_pos_list)) { | |
908 PLAYLIST_UNLOCK(); | |
909 return; | |
910 } | |
911 | |
912 if (bmp_playback_get_playing()) { | |
913 /* We need to stop before changing playlist_position */ | |
914 PLAYLIST_UNLOCK(); | |
895
1b919783797e
[svn] - should fix the ability to change songs while paused
nhjm449
parents:
853
diff
changeset
|
915 ip_data.stop = TRUE; |
0 | 916 bmp_playback_stop(); |
895
1b919783797e
[svn] - should fix the ability to change songs while paused
nhjm449
parents:
853
diff
changeset
|
917 ip_data.stop = FALSE; |
0 | 918 PLAYLIST_LOCK(); |
919 restart_playing = TRUE; | |
920 } | |
921 | |
922 plist_pos_list = find_playlist_position_list(); | |
923 if (g_list_previous(plist_pos_list)) { | |
924 playlist_position = g_list_previous(plist_pos_list)->data; | |
925 } | |
926 else if (cfg.repeat) { | |
927 GList *node; | |
928 playlist_position = NULL; | |
929 playlist_generate_shuffle_list_nolock(); | |
930 if (cfg.shuffle) | |
931 node = g_list_last(shuffle_list); | |
932 else | |
933 node = g_list_last(playlist); | |
934 if (node) | |
935 playlist_position = node->data; | |
936 } | |
937 | |
938 PLAYLIST_UNLOCK(); | |
939 | |
940 playlist_check_pos_current(); | |
941 | |
942 if (restart_playing) | |
943 bmp_playback_initiate(); | |
944 else { | |
945 mainwin_set_info_text(); | |
946 playlistwin_update_list(); | |
947 } | |
948 } | |
949 | |
950 void | |
951 playlist_queue(void) | |
952 { | |
953 GList *list = playlist_get_selected(); | |
954 GList *it = list; | |
955 | |
956 PLAYLIST_LOCK(); | |
957 | |
958 while (it) { | |
959 GList *next = g_list_next(it); | |
960 GList *tmp; | |
961 | |
962 it->data = g_list_nth_data(playlist, GPOINTER_TO_INT(it->data)); | |
963 if ((tmp = g_list_find(queued_list, it->data))) { | |
964 queued_list = g_list_remove_link(queued_list, tmp); | |
965 g_list_free_1(tmp); | |
966 list = g_list_remove_link(list, it); | |
967 g_list_free_1(it); | |
968 } | |
969 | |
970 it = next; | |
971 } | |
972 | |
973 queued_list = g_list_concat(queued_list, list); | |
974 | |
975 PLAYLIST_UNLOCK(); | |
976 | |
977 playlist_recalc_total_time(); | |
978 playlistwin_update_list(); | |
979 } | |
980 | |
981 void | |
982 playlist_queue_position(guint pos) | |
983 { | |
984 GList *tmp; | |
985 PlaylistEntry *entry; | |
986 | |
987 PLAYLIST_LOCK(); | |
988 entry = g_list_nth_data(playlist, pos); | |
989 if ((tmp = g_list_find(queued_list, entry))) { | |
990 queued_list = g_list_remove_link(queued_list, tmp); | |
991 g_list_free_1(tmp); | |
992 } | |
993 else | |
994 queued_list = g_list_append(queued_list, entry); | |
995 PLAYLIST_UNLOCK(); | |
996 | |
997 playlist_recalc_total_time(); | |
998 playlistwin_update_list(); | |
999 } | |
1000 | |
1001 gboolean | |
1002 playlist_is_position_queued(guint pos) | |
1003 { | |
1004 PlaylistEntry *entry; | |
1005 GList *tmp; | |
1006 | |
1007 PLAYLIST_LOCK(); | |
1008 entry = g_list_nth_data(playlist, pos); | |
1009 tmp = g_list_find(queued_list, entry); | |
1010 PLAYLIST_UNLOCK(); | |
1011 | |
1012 return tmp != NULL; | |
1013 } | |
1014 | |
984 | 1015 gint |
1016 playlist_get_queue_position_number(guint pos) | |
1017 { | |
1018 PlaylistEntry *entry; | |
1019 gint tmp; | |
1020 | |
1021 PLAYLIST_LOCK(); | |
1022 entry = g_list_nth_data(playlist, pos); | |
1023 tmp = g_list_index(queued_list, entry); | |
1024 PLAYLIST_UNLOCK(); | |
1025 | |
1026 return tmp; | |
1027 } | |
1028 | |
1029 gint | |
1030 playlist_get_queue_qposition_number(guint pos) | |
1031 { | |
1032 PlaylistEntry *entry; | |
1033 gint tmp; | |
1034 | |
1035 PLAYLIST_LOCK(); | |
1036 entry = g_list_nth_data(queued_list, pos); | |
1037 tmp = g_list_index(playlist, entry); | |
1038 PLAYLIST_UNLOCK(); | |
1039 | |
1040 return tmp; | |
1041 } | |
1042 | |
0 | 1043 void |
1044 playlist_clear_queue(void) | |
1045 { | |
1046 PLAYLIST_LOCK(); | |
1047 g_list_free(queued_list); | |
1048 queued_list = NULL; | |
1049 PLAYLIST_UNLOCK(); | |
1050 | |
1051 playlist_recalc_total_time(); | |
1052 playlistwin_update_list(); | |
1053 } | |
1054 | |
1055 void | |
1056 playlist_queue_remove(guint pos) | |
1057 { | |
1058 void *entry; | |
1059 | |
1060 PLAYLIST_LOCK(); | |
1061 entry = g_list_nth_data(playlist, pos); | |
1062 queued_list = g_list_remove(queued_list, entry); | |
1063 PLAYLIST_UNLOCK(); | |
1064 | |
1065 playlistwin_update_list(); | |
1066 } | |
1067 | |
1068 gint | |
1069 playlist_get_queue_position(PlaylistEntry * entry) | |
1070 { | |
1071 return g_list_index(queued_list, entry); | |
1072 } | |
1073 | |
1074 void | |
1075 playlist_set_position(guint pos) | |
1076 { | |
1077 GList *node; | |
1078 gboolean restart_playing = FALSE; | |
1079 | |
1080 PLAYLIST_LOCK(); | |
1081 if (!playlist) { | |
1082 PLAYLIST_UNLOCK(); | |
1083 return; | |
1084 } | |
1085 | |
1086 node = g_list_nth(playlist, pos); | |
1087 if (!node) { | |
1088 PLAYLIST_UNLOCK(); | |
1089 return; | |
1090 } | |
1091 | |
1092 if (bmp_playback_get_playing()) { | |
1093 /* We need to stop before changing playlist_position */ | |
1094 PLAYLIST_UNLOCK(); | |
923 | 1095 ip_data.stop = TRUE; |
0 | 1096 bmp_playback_stop(); |
923 | 1097 ip_data.stop = FALSE; |
0 | 1098 PLAYLIST_LOCK(); |
1099 restart_playing = TRUE; | |
1100 } | |
1101 | |
1102 playlist_position = node->data; | |
1103 PLAYLIST_UNLOCK(); | |
1104 playlist_check_pos_current(); | |
1105 | |
1106 if (restart_playing) | |
1107 bmp_playback_initiate(); | |
1108 else { | |
1109 mainwin_set_info_text(); | |
1110 playlistwin_update_list(); | |
1111 } | |
1112 | |
1113 /* | |
1114 * Regenerate the shuffle list when the user set a position | |
1115 * manually | |
1116 */ | |
1117 playlist_generate_shuffle_list(); | |
1118 playlist_recalc_total_time(); | |
1119 } | |
1120 | |
1121 void | |
1122 playlist_eof_reached(void) | |
1123 { | |
1124 GList *plist_pos_list; | |
1125 | |
904
2cb51ff37e8d
[svn] - stop the psuedo output plugin when using 'no playlist advance' or 'stop after current song'
nhjm449
parents:
898
diff
changeset
|
1126 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
|
1127 ip_data.stop = TRUE; |
0 | 1128 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
|
1129 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
|
1130 ip_data.stop = FALSE; |
0 | 1131 |
1132 PLAYLIST_LOCK(); | |
1133 plist_pos_list = find_playlist_position_list(); | |
1134 | |
1135 if (cfg.no_playlist_advance) { | |
1136 PLAYLIST_UNLOCK(); | |
1137 mainwin_clear_song_info(); | |
1138 if (cfg.repeat) | |
1139 bmp_playback_initiate(); | |
1140 return; | |
1141 } | |
1142 | |
898 | 1143 if (cfg.stopaftersong) { |
1144 PLAYLIST_UNLOCK(); | |
1145 mainwin_clear_song_info(); | |
1146 mainwin_set_stopaftersong(FALSE); | |
1147 return; | |
1148 } | |
1149 | |
0 | 1150 if (queued_list) { |
1151 play_queued(); | |
1152 } | |
1153 else if (!g_list_next(plist_pos_list)) { | |
1154 if (cfg.shuffle) { | |
1155 playlist_position = NULL; | |
1156 playlist_generate_shuffle_list_nolock(); | |
1157 } | |
1158 else | |
1159 playlist_position = playlist->data; | |
1160 | |
1161 if (!cfg.repeat) { | |
1162 PLAYLIST_UNLOCK(); | |
1163 mainwin_clear_song_info(); | |
1164 mainwin_set_info_text(); | |
1165 return; | |
1166 } | |
1167 } | |
1168 else | |
1169 playlist_position = g_list_next(plist_pos_list)->data; | |
1170 | |
1171 PLAYLIST_UNLOCK(); | |
1172 | |
1173 playlist_check_pos_current(); | |
1174 bmp_playback_initiate(); | |
1175 mainwin_set_info_text(); | |
1176 playlistwin_update_list(); | |
1177 } | |
1178 | |
1179 gint | |
1180 playlist_get_length(void) | |
1181 { | |
1182 gint retval; | |
1183 | |
1184 PLAYLIST_LOCK(); | |
1185 retval = playlist_get_length_nolock(); | |
1186 PLAYLIST_UNLOCK(); | |
1187 | |
1188 return retval; | |
1189 } | |
1190 | |
1191 gint | |
1192 playlist_queue_get_length(void) | |
1193 { | |
1194 gint length; | |
1195 | |
1196 PLAYLIST_LOCK(); | |
1197 length = g_list_length(queued_list); | |
1198 PLAYLIST_UNLOCK(); | |
1199 | |
1200 return length; | |
1201 } | |
1202 | |
1203 gint | |
1204 playlist_get_length_nolock(void) | |
1205 { | |
1206 REQUIRE_STATIC_LOCK(playlist); | |
1207 return g_list_length(playlist); | |
1208 } | |
1209 | |
1210 gchar * | |
1211 playlist_get_info_text(void) | |
1212 { | |
1213 gchar *text, *title, *numbers, *length; | |
1214 | |
1215 PLAYLIST_LOCK(); | |
1216 if (!playlist_position) { | |
1217 PLAYLIST_UNLOCK(); | |
1218 return NULL; | |
1219 } | |
1220 | |
1221 /* FIXME: there should not be a need to do additional conversion, | |
1222 * if playlist is properly maintained */ | |
1223 if (playlist_position->title) { | |
1224 title = str_to_utf8(playlist_position->title); | |
1225 } | |
1226 else { | |
1227 gchar *basename = g_path_get_basename(playlist_position->filename); | |
1228 title = filename_to_utf8(basename); | |
1229 g_free(basename); | |
1230 } | |
1231 | |
1232 /* | |
1233 * If the user don't want numbers in the playlist, don't | |
1234 * display them in other parts of XMMS | |
1235 */ | |
1236 | |
1237 if (cfg.show_numbers_in_pl) | |
1238 numbers = g_strdup_printf("%d. ", playlist_get_position_nolock() + 1); | |
1239 else | |
1240 numbers = g_strdup(""); | |
1241 | |
1242 if (playlist_position->length != -1) | |
1243 length = g_strdup_printf(" (%d:%-2.2d)", | |
1244 playlist_position->length / 60000, | |
1245 (playlist_position->length / 1000) % 60); | |
1246 else | |
1247 length = g_strdup(""); | |
1248 | |
1249 PLAYLIST_UNLOCK(); | |
1250 | |
1251 text = convert_title_text(g_strconcat(numbers, title, length, NULL)); | |
1252 | |
1253 g_free(numbers); | |
1254 g_free(title); | |
1255 g_free(length); | |
1256 | |
1257 return text; | |
1258 } | |
1259 | |
1260 gint | |
1261 playlist_get_current_length(void) | |
1262 { | |
1263 gint len = 0; | |
1264 | |
1265 PLAYLIST_LOCK(); | |
1266 if (playlist && playlist_position) | |
1267 len = playlist_position->length; | |
1268 PLAYLIST_UNLOCK(); | |
1269 | |
1270 return len; | |
1271 } | |
1272 | |
1273 static void | |
1274 playlist_save_m3u(FILE * file) | |
1275 { | |
1276 GList *node; | |
1277 | |
1278 g_return_if_fail(file != NULL); | |
1279 | |
1280 if (cfg.use_pl_metadata) | |
1281 g_fprintf(file, "#EXTM3U\n"); | |
1282 | |
1283 PLAYLIST_LOCK(); | |
1284 | |
1285 for (node = playlist; node; node = g_list_next(node)) { | |
1286 PlaylistEntry *entry = PLAYLIST_ENTRY(node->data); | |
1287 | |
1288 if (entry->title && cfg.use_pl_metadata) { | |
1289 gint seconds; | |
1290 | |
1291 if (entry->length > 0) | |
1292 seconds = (entry->length) / 1000; | |
1293 else | |
1294 seconds = -1; | |
1295 | |
1296 g_fprintf(file, "#EXTINF:%d,%s\n", seconds, entry->title); | |
1297 } | |
1298 | |
1299 g_fprintf(file, "%s\n", entry->filename); | |
1300 } | |
1301 | |
1302 PLAYLIST_UNLOCK(); | |
1303 } | |
1304 | |
1305 static void | |
1306 playlist_save_pls(FILE * file) | |
1307 { | |
1308 GList *node; | |
1309 | |
1310 g_return_if_fail(file != NULL); | |
1311 | |
1312 g_fprintf(file, "[playlist]\n"); | |
1313 g_fprintf(file, "NumberOfEntries=%d\n", playlist_get_length()); | |
1314 | |
1315 PLAYLIST_LOCK(); | |
1316 | |
1317 for (node = playlist; node; node = g_list_next(node)) { | |
1318 PlaylistEntry *entry = PLAYLIST_ENTRY(node->data); | |
1319 | |
1320 g_fprintf(file, "File%d=%s\n", g_list_position(playlist, node) + 1, | |
1321 entry->filename); | |
1322 } | |
1323 | |
1324 PLAYLIST_UNLOCK(); | |
1325 } | |
1326 | |
1327 gboolean | |
1328 playlist_save(const gchar * filename, | |
1329 PlaylistFormat format) | |
1330 { | |
1331 FILE *file; | |
1332 | |
1333 g_return_val_if_fail(filename != NULL, FALSE); | |
1334 | |
1335 playlist_set_current_name(filename); | |
1336 | |
1337 if ((file = fopen(filename, "w")) == NULL) | |
1338 return FALSE; | |
1339 | |
1340 playlist_save_func_table[format](file); | |
1341 | |
1342 return (fclose(file) == 0); | |
1343 } | |
1344 | |
1345 gboolean | |
1346 playlist_load(const gchar * filename) | |
1347 { | |
397
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1348 gboolean ret = FALSE; |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1349 |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1350 loading_playlist = TRUE; |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1351 ret = playlist_load_ins(filename, -1); |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1352 loading_playlist = FALSE; |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1353 |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1354 return ret; |
0 | 1355 } |
1356 | |
1357 | |
1358 static void | |
1359 playlist_load_ins_file(const gchar * filename_p, | |
1360 const gchar * playlist_name, gint pos, | |
1361 const gchar * title, gint len) | |
1362 { | |
1363 gchar *filename; | |
1364 gchar *tmp, *path; | |
355
1c701dfe5098
[svn] Cache the decoder used for each PlaylistEntry. This reduces the amount
nenolod
parents:
284
diff
changeset
|
1365 InputPlugin *dec; /* for decoder cache */ |
0 | 1366 |
1367 g_return_if_fail(filename_p != NULL); | |
1368 g_return_if_fail(playlist_name != NULL); | |
1369 | |
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
|
1370 filename = g_strchug(g_strdup(filename_p)); |
0 | 1371 |
944
03d141f44b4d
[svn] Convert \ path separators to /. Support was already present, but was not enabled before. Closes bug #461.
chainsaw
parents:
923
diff
changeset
|
1372 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
|
1373 *tmp = '/'; |
0 | 1374 |
1375 if (filename[0] != '/' && !strstr(filename, "://")) { | |
1376 path = g_strdup(playlist_name); | |
1377 if ((tmp = strrchr(path, '/'))) | |
1378 *tmp = '\0'; | |
1379 else { | |
1163
ff71f891265b
[svn] - Allow to do format detection on demand; instead of immediately on add
nenolod
parents:
1026
diff
changeset
|
1380 if (loading_playlist != TRUE || cfg.playlist_detect == FALSE) |
397
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1381 dec = input_check_file(filename, FALSE); |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1382 else |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1383 dec = NULL; |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1384 |
355
1c701dfe5098
[svn] Cache the decoder used for each PlaylistEntry. This reduces the amount
nenolod
parents:
284
diff
changeset
|
1385 __playlist_ins_with_info(filename, pos, title, len, dec); |
0 | 1386 return; |
1387 } | |
1388 tmp = g_build_filename(path, filename, NULL); | |
397
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1389 |
1165 | 1390 if (loading_playlist != TRUE && cfg.playlist_detect != TRUE) |
397
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1391 dec = input_check_file(tmp, FALSE); |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1392 else |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1393 dec = NULL; |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1394 |
355
1c701dfe5098
[svn] Cache the decoder used for each PlaylistEntry. This reduces the amount
nenolod
parents:
284
diff
changeset
|
1395 __playlist_ins_with_info(tmp, pos, title, len, dec); |
0 | 1396 g_free(tmp); |
1397 g_free(path); | |
1398 } | |
1399 else | |
355
1c701dfe5098
[svn] Cache the decoder used for each PlaylistEntry. This reduces the amount
nenolod
parents:
284
diff
changeset
|
1400 { |
1165 | 1401 if (loading_playlist != TRUE && cfg.playlist_detect != TRUE) |
397
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1402 dec = input_check_file(filename, FALSE); |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1403 else |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1404 dec = NULL; |
4fa1244ad483
[svn] Do not generate a cache when loading a playlist.
nenolod
parents:
383
diff
changeset
|
1405 |
355
1c701dfe5098
[svn] Cache the decoder used for each PlaylistEntry. This reduces the amount
nenolod
parents:
284
diff
changeset
|
1406 __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
|
1407 } |
0 | 1408 |
1409 g_free(filename); | |
1410 } | |
1411 | |
1412 static void | |
1413 parse_extm3u_info(const gchar * info, gchar ** title, gint * length) | |
1414 { | |
1415 gchar *str; | |
1416 | |
1417 g_return_if_fail(info != NULL); | |
1418 g_return_if_fail(title != NULL); | |
1419 g_return_if_fail(length != NULL); | |
1420 | |
1421 *title = NULL; | |
1422 *length = -1; | |
1423 | |
1424 if (!str_has_prefix_nocase(info, "#EXTINF:")) { | |
1425 g_message("Invalid m3u metadata (%s)", info); | |
1426 return; | |
1427 } | |
1428 | |
1429 info += 8; | |
1430 | |
1431 *length = atoi(info); | |
1432 if (*length <= 0) | |
1433 *length = -1; | |
1434 else | |
1435 *length *= 1000; | |
1436 | |
1437 if ((str = strchr(info, ','))) { | |
1438 *title = g_strstrip(g_strdup(str + 1)); | |
1439 if (strlen(*title) < 1) { | |
1440 g_free(*title); | |
1441 *title = NULL; | |
1442 } | |
1443 } | |
1444 } | |
1445 | |
1446 static guint | |
1447 playlist_load_pls(const gchar * filename, gint pos) | |
1448 { | |
1449 guint i, count, added_count = 0; | |
1450 gchar key[10]; | |
1451 gchar *line; | |
1452 | |
1453 g_return_val_if_fail(filename != NULL, 0); | |
1454 | |
1455 if (!str_has_suffix_nocase(filename, ".pls")) | |
1456 return 0; | |
1457 | |
1458 if (!(line = read_ini_string(filename, "playlist", "NumberOfEntries"))) | |
1459 return 0; | |
1460 | |
1461 count = atoi(line); | |
1462 g_free(line); | |
1463 | |
1464 for (i = 1; i <= count; i++) { | |
1465 g_snprintf(key, sizeof(key), "File%d", i); | |
1466 if ((line = read_ini_string(filename, "playlist", key))) { | |
1467 playlist_load_ins_file(line, filename, pos, NULL, -1); | |
1468 added_count++; | |
1469 | |
1470 if (pos >= 0) | |
1471 pos++; | |
1472 | |
1473 g_free(line); | |
1474 } | |
1475 } | |
1476 | |
1477 playlist_generate_shuffle_list(); | |
1478 playlistwin_update_list(); | |
1479 | |
1480 return added_count; | |
1481 } | |
1482 | |
1483 static guint | |
1484 playlist_load_m3u(const gchar * filename, gint pos) | |
1485 { | |
1486 FILE *file; | |
1487 gchar *line; | |
1488 gchar *ext_info = NULL, *ext_title = NULL; | |
1489 gsize line_len = 1024; | |
1490 gint ext_len = -1; | |
1491 gboolean is_extm3u = FALSE; | |
1492 guint added_count = 0; | |
1493 | |
1494 if (!(file = fopen(filename, "r"))) | |
1495 return 0; | |
1496 | |
1497 line = g_malloc(line_len); | |
1498 while (fgets(line, line_len, file)) { | |
1499 while (strlen(line) == line_len - 1 && line[strlen(line) - 1] != '\n') { | |
1500 line_len += 1024; | |
1501 line = g_realloc(line, line_len); | |
1502 fgets(&line[strlen(line)], 1024, file); | |
1503 } | |
1504 | |
1505 while (line[strlen(line) - 1] == '\r' || | |
1506 line[strlen(line) - 1] == '\n') | |
1507 line[strlen(line) - 1] = '\0'; | |
1508 | |
1509 if (str_has_prefix_nocase(line, "#EXTM3U")) { | |
1510 is_extm3u = TRUE; | |
1511 continue; | |
1512 } | |
1513 | |
1514 if (is_extm3u && str_has_prefix_nocase(line, "#EXTINF:")) { | |
1515 str_replace_in(&ext_info, g_strdup(line)); | |
1516 continue; | |
1517 } | |
1518 | |
1519 if (line[0] == '#' || strlen(line) == 0) { | |
1520 if (ext_info) { | |
1521 g_free(ext_info); | |
1522 ext_info = NULL; | |
1523 } | |
1524 continue; | |
1525 } | |
1526 | |
1527 if (is_extm3u) { | |
1528 if (cfg.use_pl_metadata && ext_info) | |
1529 parse_extm3u_info(ext_info, &ext_title, &ext_len); | |
1530 g_free(ext_info); | |
1531 ext_info = NULL; | |
1532 } | |
1533 | |
1534 playlist_load_ins_file(line, filename, pos, ext_title, ext_len); | |
1535 | |
1536 str_replace_in(&ext_title, NULL); | |
1537 ext_len = -1; | |
1538 | |
1539 added_count++; | |
1540 if (pos >= 0) | |
1541 pos++; | |
1542 } | |
1543 | |
1544 fclose(file); | |
1545 g_free(line); | |
1546 | |
1547 playlist_generate_shuffle_list(); | |
1548 playlistwin_update_list(); | |
1549 | |
1550 if (g_ascii_strcasecmp(filename, BMP_PLAYLIST_BASENAME)) | |
1551 playlist_set_current_name(NULL); | |
1552 else | |
1553 playlist_set_current_name(filename); | |
1554 | |
1555 return added_count; | |
1556 } | |
1557 | |
1558 static guint | |
1559 playlist_load_ins(const gchar * filename, gint pos) | |
1560 { | |
1561 guint added_count; | |
1562 | |
1563 g_return_val_if_fail(filename != NULL, 0); | |
1564 | |
1565 /* .pls ? */ | |
1566 if ((added_count = playlist_load_pls(filename, pos)) > 0) | |
1567 return added_count; | |
1568 | |
1569 /* Assume .m3u */ | |
1570 return playlist_load_m3u(filename, pos); | |
1571 } | |
1572 | |
1573 GList * | |
1574 get_playlist_nth(guint nth) | |
1575 { | |
1576 REQUIRE_STATIC_LOCK(playlist); | |
1577 return g_list_nth(playlist, nth); | |
1578 } | |
1579 | |
1580 | |
1581 GList * | |
1582 playlist_get(void) | |
1583 { | |
1584 REQUIRE_STATIC_LOCK(playlist); | |
1585 return playlist; | |
1586 } | |
1587 | |
1588 gint | |
1589 playlist_get_position_nolock(void) | |
1590 { | |
1591 REQUIRE_STATIC_LOCK(playlist); | |
1592 | |
1593 if (playlist && playlist_position) | |
1594 return g_list_index(playlist, playlist_position); | |
1595 return 0; | |
1596 } | |
1597 | |
1598 gint | |
1599 playlist_get_position(void) | |
1600 { | |
1601 gint pos; | |
1602 | |
1603 PLAYLIST_LOCK(); | |
1604 pos = playlist_get_position_nolock(); | |
1605 PLAYLIST_UNLOCK(); | |
1606 | |
1607 return pos; | |
1608 } | |
1609 | |
1610 gchar * | |
1611 playlist_get_filename(guint pos) | |
1612 { | |
1613 gchar *filename; | |
1614 PlaylistEntry *entry; | |
1615 GList *node; | |
1616 | |
1617 PLAYLIST_LOCK(); | |
1618 if (!playlist) { | |
1619 PLAYLIST_UNLOCK(); | |
1620 return NULL; | |
1621 } | |
1622 node = g_list_nth(playlist, pos); | |
1623 if (!node) { | |
1624 PLAYLIST_UNLOCK(); | |
1625 return NULL; | |
1626 } | |
1627 entry = node->data; | |
1628 | |
1629 filename = g_strdup(entry->filename); | |
1630 PLAYLIST_UNLOCK(); | |
1631 | |
1632 return filename; | |
1633 } | |
1634 | |
1635 gchar * | |
1636 playlist_get_songtitle(guint pos) | |
1637 { | |
1638 gchar *title = NULL; | |
1639 PlaylistEntry *entry; | |
1640 GList *node; | |
1641 | |
1642 PLAYLIST_LOCK(); | |
1643 | |
1644 if (!playlist) { | |
1645 PLAYLIST_UNLOCK(); | |
1646 return NULL; | |
1647 } | |
1648 | |
1649 if (!(node = g_list_nth(playlist, pos))) { | |
1650 PLAYLIST_UNLOCK(); | |
1651 return NULL; | |
1652 } | |
1653 | |
1654 entry = node->data; | |
1655 | |
1656 /* FIXME: simplify this logic */ | |
1657 if (!entry->title && entry->length == -1) { | |
1658 if (playlist_entry_get_info(entry)) | |
1659 title = entry->title; | |
1660 } | |
1661 else { | |
1662 title = entry->title; | |
1663 } | |
1664 | |
1665 PLAYLIST_UNLOCK(); | |
1666 | |
1667 if (!title) { | |
1668 title = g_path_get_basename(entry->filename); | |
1669 return str_replace(title, filename_to_utf8(title)); | |
1670 } | |
1671 | |
1672 return str_to_utf8(title); | |
1673 } | |
1674 | |
1675 gint | |
1676 playlist_get_songtime(guint pos) | |
1677 { | |
1678 gint song_time = -1; | |
1679 PlaylistEntry *entry; | |
1680 GList *node; | |
1681 | |
1682 PLAYLIST_LOCK(); | |
1683 | |
1684 if (!playlist) { | |
1685 PLAYLIST_UNLOCK(); | |
1686 return -1; | |
1687 } | |
1688 | |
1689 if (!(node = g_list_nth(playlist, pos))) { | |
1690 PLAYLIST_UNLOCK(); | |
1691 return -1; | |
1692 } | |
1693 | |
1694 entry = node->data; | |
1695 | |
1696 if (!entry->title && entry->length == -1) { | |
1697 if (playlist_entry_get_info(entry)) | |
1698 song_time = entry->length; | |
1699 | |
1700 PLAYLIST_UNLOCK(); | |
1701 } | |
1702 else { | |
1703 song_time = entry->length; | |
1704 PLAYLIST_UNLOCK(); | |
1705 } | |
1706 | |
1707 return song_time; | |
1708 } | |
1709 | |
1710 static gint | |
1711 playlist_compare_title(const PlaylistEntry * a, | |
1712 const PlaylistEntry * b) | |
1713 { | |
1714 const gchar *a_title, *b_title; | |
1715 | |
1716 g_return_val_if_fail(a != NULL, 0); | |
1717 g_return_val_if_fail(b != NULL, 0); | |
1718 | |
1719 if (a->title) | |
1720 a_title = a->title; | |
1721 else { | |
1722 if (strrchr(a->filename, '/')) | |
1723 a_title = strrchr(a->filename, '/') + 1; | |
1724 else | |
1725 a_title = a->filename; | |
1726 } | |
1727 | |
1728 if (b->title) | |
1729 b_title = b->title; | |
1730 else { | |
1731 if (strrchr(a->filename, '/')) | |
1732 b_title = strrchr(b->filename, '/') + 1; | |
1733 else | |
1734 b_title = b->filename; | |
1735 } | |
1736 | |
1737 return strcasecmp(a_title, b_title); | |
1738 } | |
1739 | |
1740 static gint | |
1741 playlist_compare_filename(const PlaylistEntry * a, | |
1742 const PlaylistEntry * b) | |
1743 { | |
1744 gchar *a_filename, *b_filename; | |
1745 | |
1746 g_return_val_if_fail(a != NULL, 0); | |
1747 g_return_val_if_fail(b != NULL, 0); | |
1748 | |
1749 if (strrchr(a->filename, '/')) | |
1750 a_filename = strrchr(a->filename, '/') + 1; | |
1751 else | |
1752 a_filename = a->filename; | |
1753 | |
1754 if (strrchr(b->filename, '/')) | |
1755 b_filename = strrchr(b->filename, '/') + 1; | |
1756 else | |
1757 b_filename = b->filename; | |
1758 | |
1759 | |
1760 return strcasecmp(a_filename, b_filename); | |
1761 } | |
1762 | |
1763 static gint | |
1764 path_compare(const gchar * a, const gchar * b) | |
1765 { | |
1766 gchar *posa, *posb; | |
1767 gint len, ret; | |
1768 | |
1769 posa = strrchr(a, '/'); | |
1770 posb = strrchr(b, '/'); | |
1771 | |
1772 /* | |
1773 * Sort directories before files | |
1774 */ | |
1775 if (posa && posb && (posa - a != posb - b)) { | |
1776 if (posa - a > posb - b) { | |
1777 len = posb - b; | |
1778 ret = -1; | |
1779 } | |
1780 else { | |
1781 len = posa - a; | |
1782 ret = 1; | |
1783 } | |
1784 if (!strncasecmp(a, b, len)) | |
1785 return ret; | |
1786 } | |
1787 return strcasecmp(a, b); | |
1788 } | |
1789 | |
1790 static gint | |
1791 playlist_compare_path(const PlaylistEntry * a, | |
1792 const PlaylistEntry * b) | |
1793 { | |
1794 return path_compare(a->filename, b->filename); | |
1795 } | |
1796 | |
1797 static gint | |
1798 playlist_compare_date(const PlaylistEntry * a, | |
1799 const PlaylistEntry * b) | |
1800 { | |
1801 struct stat buf; | |
1802 time_t modtime; | |
1803 | |
1804 gint rv; | |
1805 | |
1806 | |
1807 rv = stat(a->filename, &buf); | |
1808 | |
1809 if (rv == 0) { | |
1810 modtime = buf.st_mtime; | |
1811 rv = stat(b->filename, &buf); | |
1812 | |
1813 if (stat(b->filename, &buf) == 0) { | |
1814 if (buf.st_mtime == modtime) | |
1815 return 0; | |
1816 else | |
1817 return (buf.st_mtime - modtime) > 0 ? -1 : 1; | |
1818 } | |
1819 else | |
1820 return -1; | |
1821 } | |
1822 else if (!lstat(b->filename, &buf)) | |
1823 return 1; | |
1824 else | |
1825 return playlist_compare_filename(a, b); | |
1826 } | |
1827 | |
1828 | |
1829 void | |
1830 playlist_sort(PlaylistSortType type) | |
1831 { | |
1832 playlist_remove_dead_files(); | |
1833 PLAYLIST_LOCK(); | |
1834 playlist = | |
1835 g_list_sort(playlist, | |
1836 (GCompareFunc) playlist_compare_func_table[type]); | |
1837 PLAYLIST_UNLOCK(); | |
1838 } | |
1839 | |
1840 static GList * | |
1841 playlist_sort_selected_generic(GList * list, GCompareFunc cmpfunc) | |
1842 { | |
1843 GList *list1, *list2; | |
1844 GList *tmp_list = NULL; | |
1845 GList *index_list = NULL; | |
1846 | |
1847 /* | |
1848 * We take all the selected entries out of the playlist, | |
1849 * sorts them, and then put them back in again. | |
1850 */ | |
1851 | |
1852 list1 = g_list_last(list); | |
1853 | |
1854 while (list1) { | |
1855 list2 = g_list_previous(list1); | |
1856 if (PLAYLIST_ENTRY(list1->data)->selected) { | |
1857 gpointer idx; | |
1858 idx = GINT_TO_POINTER(g_list_position(list, list1)); | |
1859 index_list = g_list_prepend(index_list, idx); | |
1860 list = g_list_remove_link(list, list1); | |
1861 tmp_list = g_list_concat(list1, tmp_list); | |
1862 } | |
1863 list1 = list2; | |
1864 } | |
1865 | |
1866 tmp_list = g_list_sort(tmp_list, cmpfunc); | |
1867 list1 = tmp_list; | |
1868 list2 = index_list; | |
1869 | |
1870 while (list2) { | |
1871 if (!list1) { | |
1872 g_critical(G_STRLOC ": Error during list sorting. " | |
1873 "Possibly dropped some playlist-entries."); | |
1874 break; | |
1875 } | |
1876 | |
1877 list = g_list_insert(list, list1->data, GPOINTER_TO_INT(list2->data)); | |
1878 | |
1879 list2 = g_list_next(list2); | |
1880 list1 = g_list_next(list1); | |
1881 } | |
1882 | |
1883 g_list_free(index_list); | |
1884 g_list_free(tmp_list); | |
1885 | |
1886 return list; | |
1887 } | |
1888 | |
1889 void | |
1890 playlist_sort_selected(PlaylistSortType type) | |
1891 { | |
1892 PLAYLIST_LOCK(); | |
1893 playlist = playlist_sort_selected_generic(playlist, (GCompareFunc) | |
1894 playlist_compare_func_table | |
1895 [type]); | |
1896 PLAYLIST_UNLOCK(); | |
1897 } | |
1898 | |
1899 void | |
1900 playlist_reverse(void) | |
1901 { | |
1902 PLAYLIST_LOCK(); | |
1903 playlist = g_list_reverse(playlist); | |
1904 PLAYLIST_UNLOCK(); | |
1905 } | |
1906 | |
1907 static GList * | |
1908 playlist_shuffle_list(GList * list) | |
1909 { | |
1910 /* | |
1911 * Note that this doesn't make a copy of the original list. | |
1912 * The pointer to the original list is not valid after this | |
1913 * fuction is run. | |
1914 */ | |
1915 gint len = g_list_length(list); | |
1916 gint i, j; | |
1917 GList *node, **ptrs; | |
1918 | |
1919 REQUIRE_STATIC_LOCK(playlist); | |
1920 | |
1921 if (!len) | |
1922 return NULL; | |
1923 | |
1924 ptrs = g_new(GList *, len); | |
1925 | |
1926 for (node = list, i = 0; i < len; node = g_list_next(node), i++) | |
1927 ptrs[i] = node; | |
1928 | |
1929 j = g_random_int_range(0, len); | |
1930 list = ptrs[j]; | |
1931 ptrs[j]->next = NULL; | |
1932 ptrs[j] = ptrs[0]; | |
1933 | |
1934 for (i = 1; i < len; i++) { | |
1935 j = g_random_int_range(0, len - i); | |
1936 list->prev = ptrs[i + j]; | |
1937 ptrs[i + j]->next = list; | |
1938 list = ptrs[i + j]; | |
1939 ptrs[i + j] = ptrs[i]; | |
1940 } | |
1941 list->prev = NULL; | |
1942 | |
1943 g_free(ptrs); | |
1944 | |
1945 return list; | |
1946 } | |
1947 | |
1948 void | |
1949 playlist_random(void) | |
1950 { | |
1951 PLAYLIST_LOCK(); | |
1952 playlist = playlist_shuffle_list(playlist); | |
1953 PLAYLIST_UNLOCK(); | |
1954 } | |
1955 | |
1956 GList * | |
1957 playlist_get_selected(void) | |
1958 { | |
1959 GList *node, *list = NULL; | |
1960 gint i = 0; | |
1961 | |
1962 PLAYLIST_LOCK(); | |
1963 for (node = playlist_get(); node; node = g_list_next(node), i++) { | |
1964 PlaylistEntry *entry = node->data; | |
1965 if (entry->selected) | |
1966 list = g_list_prepend(list, GINT_TO_POINTER(i)); | |
1967 } | |
1968 PLAYLIST_UNLOCK(); | |
1969 return g_list_reverse(list); | |
1970 } | |
1971 | |
1972 void | |
1973 playlist_clear_selected(void) | |
1974 { | |
1975 GList *node = NULL; | |
1976 gint i = 0; | |
1977 | |
1978 PLAYLIST_LOCK(); | |
1979 for (node = playlist_get(); node; node = g_list_next(node), i++) { | |
1980 PLAYLIST_ENTRY(node->data)->selected = FALSE; | |
1981 } | |
1982 PLAYLIST_UNLOCK(); | |
1983 playlist_recalc_total_time(); | |
1984 } | |
1985 | |
1986 gint | |
1987 playlist_get_num_selected(void) | |
1988 { | |
1989 GList *node; | |
1990 gint num = 0; | |
1991 | |
1992 PLAYLIST_LOCK(); | |
1993 for (node = playlist_get(); node; node = g_list_next(node)) { | |
1994 PlaylistEntry *entry = node->data; | |
1995 if (entry->selected) | |
1996 num++; | |
1997 } | |
1998 PLAYLIST_UNLOCK(); | |
1999 return num; | |
2000 } | |
2001 | |
2002 | |
2003 static void | |
2004 playlist_generate_shuffle_list(void) | |
2005 { | |
2006 PLAYLIST_LOCK(); | |
2007 playlist_generate_shuffle_list_nolock(); | |
2008 PLAYLIST_UNLOCK(); | |
2009 } | |
2010 | |
2011 static void | |
2012 playlist_generate_shuffle_list_nolock(void) | |
2013 { | |
2014 GList *node; | |
2015 gint numsongs; | |
2016 | |
2017 REQUIRE_STATIC_LOCK(playlist); | |
2018 | |
2019 if (shuffle_list) { | |
2020 g_list_free(shuffle_list); | |
2021 shuffle_list = NULL; | |
2022 } | |
2023 | |
2024 if (!cfg.shuffle || !playlist) | |
2025 return; | |
2026 | |
2027 shuffle_list = playlist_shuffle_list(g_list_copy(playlist)); | |
2028 numsongs = g_list_length(shuffle_list); | |
2029 | |
2030 if (playlist_position) { | |
2031 gint i = g_list_index(shuffle_list, playlist_position); | |
2032 node = g_list_nth(shuffle_list, i); | |
2033 shuffle_list = g_list_remove_link(shuffle_list, node); | |
2034 shuffle_list = g_list_prepend(shuffle_list, node->data); | |
2035 } | |
2036 } | |
2037 | |
2038 void | |
2039 playlist_fileinfo(guint pos) | |
2040 { | |
2041 gchar *path = NULL; | |
2042 GList *node; | |
2043 | |
2044 PLAYLIST_LOCK(); | |
2045 if ((node = g_list_nth(playlist_get(), pos))) { | |
2046 PlaylistEntry *entry = node->data; | |
2047 path = g_strdup(entry->filename); | |
2048 } | |
2049 PLAYLIST_UNLOCK(); | |
2050 if (path) { | |
2051 input_file_info_box(path); | |
2052 g_free(path); | |
2053 } | |
2054 } | |
2055 | |
2056 void | |
2057 playlist_fileinfo_current(void) | |
2058 { | |
2059 gchar *path = NULL; | |
2060 | |
2061 PLAYLIST_LOCK(); | |
2062 if (playlist_get() && playlist_position) | |
2063 path = g_strdup(playlist_position->filename); | |
2064 PLAYLIST_UNLOCK(); | |
2065 | |
2066 if (path) { | |
2067 input_file_info_box(path); | |
2068 g_free(path); | |
2069 } | |
2070 } | |
2071 | |
2072 | |
2073 static gboolean | |
2074 playlist_get_info_is_going(void) | |
2075 { | |
2076 gboolean result; | |
2077 | |
2078 G_LOCK(playlist_get_info_going); | |
2079 result = playlist_get_info_going; | |
2080 G_UNLOCK(playlist_get_info_going); | |
2081 | |
2082 return result; | |
2083 } | |
2084 | |
2085 static gpointer | |
2086 playlist_get_info_func(gpointer arg) | |
2087 { | |
2088 GList *node; | |
2089 gboolean update_playlistwin = FALSE; | |
2090 gboolean update_mainwin = FALSE; | |
2091 | |
2092 while (playlist_get_info_is_going()) { | |
2093 PlaylistEntry *entry; | |
2094 | |
2095 if (cfg.use_pl_metadata && | |
2096 cfg.get_info_on_load && | |
2097 playlist_get_info_scan_active) { | |
2098 | |
2099 PLAYLIST_LOCK(); | |
2100 for (node = playlist_get(); node; node = g_list_next(node)) { | |
2101 entry = node->data; | |
2102 | |
2103 if (entry->title || entry->length != -1) | |
2104 continue; | |
2105 | |
2106 if (!playlist_entry_get_info(entry)) { | |
2107 if (g_list_index(playlist_get(), entry) == -1) | |
2108 /* Entry disappeared while we looked it up. | |
2109 Restart. */ | |
2110 node = playlist_get(); | |
2111 } | |
2112 else if (entry->title || entry->length != -1) { | |
2113 update_playlistwin = TRUE; | |
2114 if (entry == playlist_position) | |
2115 update_mainwin = TRUE; | |
2116 break; | |
2117 } | |
2118 } | |
2119 PLAYLIST_UNLOCK(); | |
2120 | |
2121 if (!node) | |
2122 playlist_get_info_scan_active = FALSE; | |
2123 } | |
2124 else if (!cfg.get_info_on_load && | |
2125 cfg.get_info_on_demand && | |
2126 cfg.playlist_visible && | |
2127 !cfg.playlist_shaded && | |
2128 cfg.use_pl_metadata) { | |
2129 | |
2130 gboolean found = FALSE; | |
2131 | |
2132 PLAYLIST_LOCK(); | |
2133 | |
2134 if (!playlist_get()) { | |
2135 PLAYLIST_UNLOCK(); | |
2136 g_usleep(1000000); | |
2137 continue; | |
2138 } | |
2139 | |
2140 for (node = | |
2141 g_list_nth(playlist_get(), playlistwin_get_toprow()); | |
2142 node | |
2143 && | |
2144 playlistwin_item_visible(g_list_position | |
2145 (playlist_get(), node)); | |
2146 node = g_list_next(node)) { | |
2147 entry = node->data; | |
2148 if (entry->title || entry->length != -1) | |
2149 continue; | |
2150 | |
2151 if (!playlist_entry_get_info(entry)) { | |
2152 if (g_list_index(playlist_get(), entry) == -1) | |
2153 /* Entry disapeared while we | |
2154 looked it up. Restart. */ | |
2155 node = | |
2156 g_list_nth(playlist_get(), | |
2157 playlistwin_get_toprow()); | |
2158 } | |
2159 else if (entry->title || entry->length != -1) { | |
2160 update_playlistwin = TRUE; | |
2161 if (entry == playlist_position) | |
2162 update_mainwin = TRUE; | |
2163 found = TRUE; | |
2164 break; | |
2165 } | |
2166 } | |
2167 | |
2168 PLAYLIST_UNLOCK(); | |
2169 | |
2170 if (!found) { | |
2171 g_usleep(500000); | |
2172 continue; | |
2173 } | |
2174 } | |
2175 else | |
2176 g_usleep(500000); | |
2177 | |
2178 if (update_playlistwin) { | |
2179 playlistwin_update_list(); | |
2180 update_playlistwin = FALSE; | |
2181 } | |
2182 | |
2183 if (update_mainwin) { | |
2184 mainwin_set_info_text(); | |
2185 update_mainwin = FALSE; | |
2186 } | |
2187 } | |
2188 | |
2189 g_thread_exit(NULL); | |
2190 return NULL; | |
2191 } | |
2192 | |
2193 void | |
2194 playlist_start_get_info_thread(void) | |
2195 { | |
2196 playlist_get_info_going = TRUE; | |
2197 playlist_get_info_thread = g_thread_create(playlist_get_info_func, | |
2198 NULL, TRUE, NULL); | |
2199 } | |
2200 | |
2201 void | |
2202 playlist_stop_get_info_thread(void) | |
2203 { | |
2204 G_LOCK(playlist_get_info_going); | |
2205 playlist_get_info_going = FALSE; | |
2206 G_UNLOCK(playlist_get_info_going); | |
2207 g_thread_join(playlist_get_info_thread); | |
2208 } | |
2209 | |
2210 void | |
2211 playlist_start_get_info_scan(void) | |
2212 { | |
2213 playlist_get_info_scan_active = TRUE; | |
2214 } | |
2215 | |
2216 void | |
2217 playlist_remove_dead_files(void) | |
2218 { | |
2219 GList *node, *next_node; | |
2220 | |
2221 PLAYLIST_LOCK(); | |
2222 | |
2223 for (node = playlist; node; node = next_node) { | |
2224 PlaylistEntry *entry = PLAYLIST_ENTRY(node->data); | |
2225 next_node = g_list_next(node); | |
2226 | |
2227 if (!entry || !entry->filename) { | |
2228 g_message(G_STRLOC ": Playlist entry is invalid!"); | |
2229 continue; | |
2230 } | |
2231 | |
2232 /* FIXME: What about 'file:///'? */ | |
2233 /* Don't kill URLs */ | |
2234 if (strstr(entry->filename, "://")) | |
2235 continue; | |
2236 | |
2237 /* FIXME: Should test for readability */ | |
2238 if (vfs_file_test(entry->filename, G_FILE_TEST_EXISTS)) | |
2239 continue; | |
2240 | |
2241 if (entry == playlist_position) { | |
2242 /* Don't remove the currently playing song */ | |
2243 if (bmp_playback_get_playing()) | |
2244 continue; | |
2245 | |
2246 if (next_node) | |
2247 playlist_position = PLAYLIST_ENTRY(next_node->data); | |
2248 else | |
2249 playlist_position = NULL; | |
2250 } | |
2251 | |
2252 playlist_entry_free(entry); | |
2253 playlist = g_list_delete_link(playlist, node); | |
2254 } | |
2255 | |
2256 PLAYLIST_UNLOCK(); | |
2257 | |
2258 playlist_generate_shuffle_list(); | |
2259 playlistwin_update_list(); | |
2260 playlist_recalc_total_time(); | |
2261 } | |
2262 | |
852
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2263 |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2264 static gint |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2265 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
|
2266 { |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2267 const gchar *a_title, *b_title; |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2268 |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2269 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
|
2270 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
|
2271 |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2272 if (a->title) |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2273 a_title = a->title; |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2274 else { |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2275 if (strrchr(a->filename, '/')) |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2276 a_title = strrchr(a->filename, '/') + 1; |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2277 else |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2278 a_title = a->filename; |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2279 } |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2280 |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2281 if (b->title) |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2282 b_title = b->title; |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2283 else { |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2284 if (strrchr(a->filename, '/')) |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2285 b_title = strrchr(b->filename, '/') + 1; |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2286 else |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2287 b_title = b->filename; |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2288 } |
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 return strcmp(a_title, b_title); |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2291 } |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2292 |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2293 static gint |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2294 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
|
2295 { |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2296 gchar *a_filename, *b_filename; |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2297 |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2298 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
|
2299 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
|
2300 |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2301 if (strrchr(a->filename, '/')) |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2302 a_filename = strrchr(a->filename, '/') + 1; |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2303 else |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2304 a_filename = a->filename; |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2305 |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2306 if (strrchr(b->filename, '/')) |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2307 b_filename = strrchr(b->filename, '/') + 1; |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2308 else |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2309 b_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 return strcmp(a_filename, b_filename); |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2312 } |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2313 |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2314 static gint |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2315 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
|
2316 { |
853 | 2317 /* simply compare the entire filename string */ |
2318 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
|
2319 } |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2320 |
840
ffc5ab7b4b2c
[svn] added a 'remove duplicates' option to the playlist removal menu
giacomo
parents:
633
diff
changeset
|
2321 void |
852
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2322 playlist_remove_duplicates( PlaylistDupsType type ) |
840
ffc5ab7b4b2c
[svn] added a 'remove duplicates' option to the playlist removal menu
giacomo
parents:
633
diff
changeset
|
2323 { |
ffc5ab7b4b2c
[svn] added a 'remove duplicates' option to the playlist removal menu
giacomo
parents:
633
diff
changeset
|
2324 GList *node, *next_node; |
ffc5ab7b4b2c
[svn] added a 'remove duplicates' option to the playlist removal menu
giacomo
parents:
633
diff
changeset
|
2325 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
|
2326 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
|
2327 |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2328 switch ( type ) |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2329 { |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2330 case PLAYLIST_DUPS_TITLE: |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2331 dups_compare_func = playlist_dupscmp_title; |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2332 break; |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2333 case PLAYLIST_DUPS_PATH: |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2334 dups_compare_func = playlist_dupscmp_path; |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2335 break; |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2336 case PLAYLIST_DUPS_FILENAME: |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2337 default: |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2338 dups_compare_func = playlist_dupscmp_filename; |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2339 break; |
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2340 } |
840
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 PLAYLIST_LOCK(); |
ffc5ab7b4b2c
[svn] added a 'remove duplicates' option to the playlist removal menu
giacomo
parents:
633
diff
changeset
|
2343 |
ffc5ab7b4b2c
[svn] added a 'remove duplicates' option to the playlist removal menu
giacomo
parents:
633
diff
changeset
|
2344 for (node = playlist; node; node = next_node) { |
ffc5ab7b4b2c
[svn] added a 'remove duplicates' option to the playlist removal menu
giacomo
parents:
633
diff
changeset
|
2345 PlaylistEntry *entry = PLAYLIST_ENTRY(node->data); |
ffc5ab7b4b2c
[svn] added a 'remove duplicates' option to the playlist removal menu
giacomo
parents:
633
diff
changeset
|
2346 next_node = g_list_next(node); |
ffc5ab7b4b2c
[svn] added a 'remove duplicates' option to the playlist removal menu
giacomo
parents:
633
diff
changeset
|
2347 |
ffc5ab7b4b2c
[svn] added a 'remove duplicates' option to the playlist removal menu
giacomo
parents:
633
diff
changeset
|
2348 if (!entry || !entry->filename) { |
ffc5ab7b4b2c
[svn] added a 'remove duplicates' option to the playlist removal menu
giacomo
parents:
633
diff
changeset
|
2349 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
|
2350 continue; |
ffc5ab7b4b2c
[svn] added a 'remove duplicates' option to the playlist removal menu
giacomo
parents:
633
diff
changeset
|
2351 } |
ffc5ab7b4b2c
[svn] added a 'remove duplicates' option to the playlist removal menu
giacomo
parents:
633
diff
changeset
|
2352 |
ffc5ab7b4b2c
[svn] added a 'remove duplicates' option to the playlist removal menu
giacomo
parents:
633
diff
changeset
|
2353 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
|
2354 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
|
2355 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
|
2356 |
ffc5ab7b4b2c
[svn] added a 'remove duplicates' option to the playlist removal menu
giacomo
parents:
633
diff
changeset
|
2357 if (!entry_cmp || !entry_cmp->filename) { |
ffc5ab7b4b2c
[svn] added a 'remove duplicates' option to the playlist removal menu
giacomo
parents:
633
diff
changeset
|
2358 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
|
2359 continue; |
ffc5ab7b4b2c
[svn] added a 'remove duplicates' option to the playlist removal menu
giacomo
parents:
633
diff
changeset
|
2360 } |
ffc5ab7b4b2c
[svn] added a 'remove duplicates' option to the playlist removal menu
giacomo
parents:
633
diff
changeset
|
2361 |
852
bcff46a2558d
[svn] added multiple 'remove duplicates' (by title, by filename, by path+filename)
giacomo
parents:
840
diff
changeset
|
2362 /* 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
|
2363 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
|
2364 |
ffc5ab7b4b2c
[svn] added a 'remove duplicates' option to the playlist removal menu
giacomo
parents:
633
diff
changeset
|
2365 if (entry_cmp == playlist_position) { |
ffc5ab7b4b2c
[svn] added a 'remove duplicates' option to the playlist removal menu
giacomo
parents:
633
diff
changeset
|
2366 /* Don't remove the currently playing song */ |
ffc5ab7b4b2c
[svn] added a 'remove duplicates' option to the playlist removal menu
giacomo
parents:
633
diff
changeset
|
2367 if (bmp_playback_get_playing()) |
ffc5ab7b4b2c
[svn] added a 'remove duplicates' option to the playlist removal menu
giacomo
parents:
633
diff
changeset
|
2368 continue; |
ffc5ab7b4b2c
[svn] added a 'remove duplicates' option to the playlist removal menu
giacomo
parents:
633
diff
changeset
|
2369 |
ffc5ab7b4b2c
[svn] added a 'remove duplicates' option to the playlist removal menu
giacomo
parents:
633
diff
changeset
|
2370 if (next_node_cmp) |
ffc5ab7b4b2c
[svn] added a 'remove duplicates' option to the playlist removal menu
giacomo
parents:
633
diff
changeset
|
2371 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
|
2372 else |
ffc5ab7b4b2c
[svn] added a 'remove duplicates' option to the playlist removal menu
giacomo
parents:
633
diff
changeset
|
2373 playlist_position = NULL; |
ffc5ab7b4b2c
[svn] added a 'remove duplicates' option to the playlist removal menu
giacomo
parents:
633
diff
changeset
|
2374 } |
ffc5ab7b4b2c
[svn] added a 'remove duplicates' option to the playlist removal menu
giacomo
parents:
633
diff
changeset
|
2375 |
ffc5ab7b4b2c
[svn] added a 'remove duplicates' option to the playlist removal menu
giacomo
parents:
633
diff
changeset
|
2376 /* 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
|
2377 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
|
2378 if ( node_cmp == next_node ) |
ffc5ab7b4b2c
[svn] added a 'remove duplicates' option to the playlist removal menu
giacomo
parents:
633
diff
changeset
|
2379 next_node = g_list_next(next_node); |
ffc5ab7b4b2c
[svn] added a 'remove duplicates' option to the playlist removal menu
giacomo
parents:
633
diff
changeset
|
2380 |
ffc5ab7b4b2c
[svn] added a 'remove duplicates' option to the playlist removal menu
giacomo
parents:
633
diff
changeset
|
2381 playlist_entry_free(entry_cmp); |
ffc5ab7b4b2c
[svn] added a 'remove duplicates' option to the playlist removal menu
giacomo
parents:
633
diff
changeset
|
2382 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
|
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 } |
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 PLAYLIST_UNLOCK(); |
ffc5ab7b4b2c
[svn] added a 'remove duplicates' option to the playlist removal menu
giacomo
parents:
633
diff
changeset
|
2388 |
ffc5ab7b4b2c
[svn] added a 'remove duplicates' option to the playlist removal menu
giacomo
parents:
633
diff
changeset
|
2389 playlistwin_update_list(); |
ffc5ab7b4b2c
[svn] added a 'remove duplicates' option to the playlist removal menu
giacomo
parents:
633
diff
changeset
|
2390 playlist_recalc_total_time(); |
ffc5ab7b4b2c
[svn] added a 'remove duplicates' option to the playlist removal menu
giacomo
parents:
633
diff
changeset
|
2391 } |
ffc5ab7b4b2c
[svn] added a 'remove duplicates' option to the playlist removal menu
giacomo
parents:
633
diff
changeset
|
2392 |
0 | 2393 static gulong pl_total_time = 0, pl_selection_time = 0; |
2394 static gboolean pl_total_more = FALSE, pl_selection_more = FALSE; | |
2395 | |
2396 void | |
2397 playlist_get_total_time(gulong * total_time, | |
2398 gulong * selection_time, | |
2399 gboolean * total_more, | |
2400 gboolean * selection_more) | |
2401 { | |
2402 PLAYLIST_LOCK(); | |
2403 *total_time = pl_total_time; | |
2404 *selection_time = pl_selection_time; | |
2405 *total_more = pl_total_more; | |
2406 *selection_more = pl_selection_more; | |
2407 PLAYLIST_UNLOCK(); | |
2408 } | |
2409 | |
2410 | |
2411 static void | |
2412 playlist_recalc_total_time_nolock(void) | |
2413 { | |
2414 GList *list; | |
2415 PlaylistEntry *entry; | |
2416 | |
2417 REQUIRE_STATIC_LOCK(playlist); | |
2418 | |
2419 pl_total_time = 0; | |
2420 pl_selection_time = 0; | |
2421 pl_total_more = FALSE; | |
2422 pl_selection_more = FALSE; | |
2423 | |
2424 for (list = playlist_get(); list; list = g_list_next(list)) { | |
2425 entry = list->data; | |
2426 | |
2427 if (entry->length != -1) | |
2428 pl_total_time += entry->length / 1000; | |
2429 else | |
2430 pl_total_more = TRUE; | |
2431 | |
2432 if (entry->selected) { | |
2433 if (entry->length != -1) | |
2434 pl_selection_time += entry->length / 1000; | |
2435 else | |
2436 pl_selection_more = TRUE; | |
2437 } | |
2438 } | |
2439 } | |
2440 | |
2441 static void | |
2442 playlist_recalc_total_time(void) | |
2443 { | |
2444 PLAYLIST_LOCK(); | |
2445 playlist_recalc_total_time_nolock(); | |
2446 PLAYLIST_UNLOCK(); | |
2447 } | |
2448 | |
2449 | |
2450 void | |
2451 playlist_select_all(gboolean set) | |
2452 { | |
2453 GList *list; | |
2454 | |
2455 PLAYLIST_LOCK(); | |
2456 | |
2457 for (list = playlist_get(); list; list = g_list_next(list)) { | |
2458 PlaylistEntry *entry = list->data; | |
2459 entry->selected = set; | |
2460 } | |
2461 | |
2462 PLAYLIST_UNLOCK(); | |
2463 playlist_recalc_total_time(); | |
2464 } | |
2465 | |
2466 void | |
2467 playlist_select_invert_all(void) | |
2468 { | |
2469 GList *list; | |
2470 | |
2471 PLAYLIST_LOCK(); | |
2472 | |
2473 for (list = playlist_get(); list; list = g_list_next(list)) { | |
2474 PlaylistEntry *entry = list->data; | |
2475 entry->selected = !entry->selected; | |
2476 } | |
2477 | |
2478 PLAYLIST_UNLOCK(); | |
2479 playlist_recalc_total_time(); | |
2480 } | |
2481 | |
2482 gboolean | |
2483 playlist_select_invert(guint pos) | |
2484 { | |
2485 GList *list; | |
2486 gboolean invert_ok = FALSE; | |
2487 | |
2488 PLAYLIST_LOCK(); | |
2489 | |
2490 if ((list = g_list_nth(playlist_get(), pos))) { | |
2491 PlaylistEntry *entry = list->data; | |
2492 entry->selected = !entry->selected; | |
2493 invert_ok = TRUE; | |
2494 } | |
2495 | |
2496 PLAYLIST_UNLOCK(); | |
2497 playlist_recalc_total_time(); | |
2498 | |
2499 return invert_ok; | |
2500 } | |
2501 | |
2502 | |
2503 void | |
2504 playlist_select_range(gint min_pos, gint max_pos, gboolean select) | |
2505 { | |
2506 GList *list; | |
2507 gint i; | |
2508 | |
2509 if (min_pos > max_pos) | |
2510 SWAP(min_pos, max_pos); | |
2511 | |
2512 PLAYLIST_LOCK(); | |
2513 | |
2514 list = g_list_nth(playlist_get(), min_pos); | |
2515 for (i = min_pos; i <= max_pos && list; i++) { | |
2516 PlaylistEntry *entry = list->data; | |
2517 entry->selected = select; | |
2518 list = g_list_next(list); | |
2519 } | |
2520 | |
2521 PLAYLIST_UNLOCK(); | |
2522 | |
2523 playlist_recalc_total_time(); | |
2524 } | |
2525 | |
2526 gboolean | |
2527 playlist_read_info_selection(void) | |
2528 { | |
2529 GList *node; | |
2530 gboolean retval = FALSE; | |
2531 | |
2532 PLAYLIST_LOCK(); | |
2533 | |
2534 for (node = playlist_get(); node; node = g_list_next(node)) { | |
2535 PlaylistEntry *entry = node->data; | |
2536 if (!entry->selected) | |
2537 continue; | |
2538 | |
2539 retval = TRUE; | |
2540 | |
2541 str_replace_in(&entry->title, NULL); | |
2542 entry->length = -1; | |
2543 | |
2544 if (!playlist_entry_get_info(entry)) { | |
2545 if (g_list_index(playlist_get(), entry) == -1) | |
2546 /* Entry disappeared while we looked it up. Restart. */ | |
2547 node = playlist_get(); | |
2548 } | |
2549 } | |
2550 | |
2551 PLAYLIST_UNLOCK(); | |
2552 | |
2553 playlistwin_update_list(); | |
2554 playlist_recalc_total_time(); | |
2555 | |
2556 return retval; | |
2557 } | |
2558 | |
2559 void | |
2560 playlist_read_info(guint pos) | |
2561 { | |
2562 GList *node; | |
2563 | |
2564 PLAYLIST_LOCK(); | |
2565 | |
2566 if ((node = g_list_nth(playlist_get(), pos))) { | |
2567 PlaylistEntry *entry = node->data; | |
2568 str_replace_in(&entry->title, NULL); | |
2569 entry->length = -1; | |
2570 playlist_entry_get_info(entry); | |
2571 } | |
2572 | |
2573 PLAYLIST_UNLOCK(); | |
2574 | |
2575 playlistwin_update_list(); | |
2576 playlist_recalc_total_time(); | |
2577 } | |
2578 | |
2579 void | |
2580 playlist_set_shuffle(gboolean shuffle) | |
2581 { | |
2582 PLAYLIST_LOCK(); | |
2583 | |
2584 cfg.shuffle = shuffle; | |
2585 playlist_generate_shuffle_list_nolock(); | |
2586 | |
2587 PLAYLIST_UNLOCK(); | |
2588 } | |
2589 | |
2590 void | |
2591 playlist_new(void) | |
2592 { | |
2593 playlist_set_current_name(NULL); | |
2594 playlist_clear(); | |
2595 mainwin_clear_song_info(); | |
2596 mainwin_set_info_text(); | |
2597 } | |
2598 | |
2599 | |
2600 const gchar * | |
2601 playlist_get_filename_to_play(void) | |
2602 { | |
2603 const gchar *filename = NULL; | |
2604 | |
2605 PLAYLIST_LOCK(); | |
2606 | |
2607 if (playlist) { | |
2608 if (!playlist_position) { | |
2609 if (cfg.shuffle) | |
2610 playlist_position = shuffle_list->data; | |
2611 else | |
2612 playlist_position = playlist->data; | |
2613 } | |
2614 | |
2615 filename = playlist_position->filename; | |
2616 } | |
2617 | |
2618 PLAYLIST_UNLOCK(); | |
2619 | |
2620 return filename; | |
2621 } | |
356
99928e1275a1
[svn] This commit reduces the amount of times we probe a source down to ONE
nenolod
parents:
355
diff
changeset
|
2622 |
398
f908bcd87c3d
[svn] Generate cache content on demand if it was not previously there.
nenolod
parents:
397
diff
changeset
|
2623 PlaylistEntry * |
356
99928e1275a1
[svn] This commit reduces the amount of times we probe a source down to ONE
nenolod
parents:
355
diff
changeset
|
2624 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
|
2625 { |
99928e1275a1
[svn] This commit reduces the amount of times we probe a source down to ONE
nenolod
parents:
355
diff
changeset
|
2626 PLAYLIST_LOCK(); |
99928e1275a1
[svn] This commit reduces the amount of times we probe a source down to ONE
nenolod
parents:
355
diff
changeset
|
2627 |
99928e1275a1
[svn] This commit reduces the amount of times we probe a source down to ONE
nenolod
parents:
355
diff
changeset
|
2628 if (playlist) { |
99928e1275a1
[svn] This commit reduces the amount of times we probe a source down to ONE
nenolod
parents:
355
diff
changeset
|
2629 if (!playlist_position) { |
99928e1275a1
[svn] This commit reduces the amount of times we probe a source down to ONE
nenolod
parents:
355
diff
changeset
|
2630 if (cfg.shuffle) |
99928e1275a1
[svn] This commit reduces the amount of times we probe a source down to ONE
nenolod
parents:
355
diff
changeset
|
2631 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
|
2632 else |
99928e1275a1
[svn] This commit reduces the amount of times we probe a source down to ONE
nenolod
parents:
355
diff
changeset
|
2633 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
|
2634 } |
99928e1275a1
[svn] This commit reduces the amount of times we probe a source down to ONE
nenolod
parents:
355
diff
changeset
|
2635 } |
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 PLAYLIST_UNLOCK(); |
99928e1275a1
[svn] This commit reduces the amount of times we probe a source down to ONE
nenolod
parents:
355
diff
changeset
|
2638 |
99928e1275a1
[svn] This commit reduces the amount of times we probe a source down to ONE
nenolod
parents:
355
diff
changeset
|
2639 return playlist_position; |
99928e1275a1
[svn] This commit reduces the amount of times we probe a source down to ONE
nenolod
parents:
355
diff
changeset
|
2640 } |