Archive / / / / / Echo.Tools.URIHandler.vala
2008-12-01 16:11:23 UTC
previous next
/* vim: set noexpandtab tabstop=4 shiftwidth=4 nowrap textwidth=100 * * Echo Media Player * Copyright (C) 2008 Shane O'Connell * * [ The original file includes a copyright header in this location describing * the file as being released under the terms of the GNU General Public * License. It has been removed in order to display the file as part of the * archive. ] */ using GLib; using Gee; using Echo.Core; using Echo.Interfaces; [Compact] public class Echo.Tools.URIHandler { /* TODO change to this interface: public URIHandler.from_uri(string uri) { } public URIHandler.from_uris(string[] uris) { } public Track get_next_track() { }*/ public delegate void TrackHandler(StubTrack track); public static void handle_uris(string[] uris, TrackHandler handler) throws GLib.Error { foreach(string uri in uris) { handle_uri(uri, handler); } } public static void handle_uri(string uri, TrackHandler handler, bool playlists_ok = true, bool directories_ok = true) throws GLib.Error { var file = File.new_for_uri(uri); var file_info = file.query_info("standard::type,standard::fast-content-type", FileQueryInfoFlags.NONE, null); if (file_info.get_file_type() == FileType.DIRECTORY) { if (directories_ok) { var children = file.enumerate_children("standard::display-name", FileQueryInfoFlags.NONE, null); FileInfo child; var child_names = new ArrayList<string>(); while((child = children.next_file(null)) != null) child_names.add(child.get_display_name()); // TODO use g_utf8_collate_key_for_filename when vala's library supports it // TODO sort filenames alphabetically when vala gets some kind of sort function... //child_names.sort((a, b) => { // return ((string)a).casefold().collate(((string)b).casefold()); //}); foreach(string child_name in child_names) { if (child_name.length >= 1 && child_name.get_char() != '.') { // TODO add some kind of protection against infinitely recursive directories handle_uri(file.get_child_for_display_name(child_name).get_uri(), handler, false, true); } } } } else { weak string content_type = file_info.get_attribute_string("standard::fast-content-type"); if (playlists_ok && content_type == "audio/x-mpegurl") { handle_m3u_playlist(file, handler); } else if (file.is_native() && content_type == "audio/mpeg") { var file_tags = new TagLib.File(file.get_path()); handler(new StubTrack( file_tags.tag().title(), file_tags.tag().artist(), file_tags.tag().album(), (int)file_tags.tag().track(), (int)file_tags.tag().year(), 0, 0, uri)); } } } private static void handle_m3u_playlist(File file, TrackHandler handler) { } }