/// <summary> /// Executes the specified param. /// </summary> /// <param name="param">The param.</param> /// <param name="result">The result.</param> /// <returns></returns> public OpResult Execute(string param, NowPlayingList nowPlaying, MediaItem currentMedia) { OpResult opResult = new OpResult(); opResult.StatusCode = OpStatusCode.Json; if (currentMedia != null) { opResult.ContentText = JsonConvert.SerializeObject(currentMedia, Formatting.Indented); return opResult; } else if (param.IndexOf("-help") >= 0) { opResult.StatusCode = OpStatusCode.Ok; opResult = showHelp(opResult); return opResult; } debug_last_action = "Execute: Start"; bool should_enqueue = true; int size_x = 0; int size_y = 0; string create_playlist_name = null; string playlist_query = null; string template = ""; string cache_fn = make_cache_fn(String.Format("{0}-{1}.txt", which_command, param)); string cache_body = ""; try { IWMPMediaCollection2 collection = (IWMPMediaCollection2)Player.mediaCollection; IWMPPlaylistCollection playlistCollection = (IWMPPlaylistCollection)Player.playlistCollection; int ver = Player.mediaCollection.getByAttribute("MediaType", "Audio").count; string cache_ver = String.Format("{0}", ver); cache_body = get_cached(cache_fn, cache_ver); if (cache_body.Length != 0 && create_playlist_name == null && playlist_query == null && !m_stats_only) { opResult.ContentText = setCachedFlag(cache_body); return opResult; } IWMPQuery query = collection.createQuery(); IWMPPlaylistArray playlists = null; IWMPPlaylist mediaPlaylist = null; IWMPMedia media_item; ArrayList query_indexes = new ArrayList(); Library metadata = new Library(m_stats_only); bool has_query = false; bool has_exact_query = false; string query_text = ""; string query_type = ""; debug_last_action = "Execution: Parsing params"; request_params = HttpUtility.UrlEncode(param); if (param.Contains("create-playlist:")) { create_playlist_name = param.Substring(param.IndexOf("create-playlist:") + "create-playlist:".Length); create_playlist_name = trim_parameter(create_playlist_name); } if (param.Contains("exact-genre:")) { string genre = param.Substring(param.IndexOf("exact-genre:") + "exact-genre:".Length); genre = trim_parameter(genre); query.addCondition("WM/Genre", "Equals", genre); genre_filter = genre; query_text = genre; query_type = "Genre"; has_query = true; has_exact_query = true; } else if (param.Contains("genre:*")) { string genre = param.Substring(param.IndexOf("genre:*") + "genre:*".Length); genre = trim_parameter(genre); query.addCondition("WM/Genre", "BeginsWith", genre); query.beginNextGroup(); query.addCondition("WM/Genre", "Contains", " " + genre); genre_filter = genre; query_text = genre; query_type = "Genre"; has_query = true; } else if (param.Contains("genre:")) { string genre = param.Substring(param.IndexOf("genre:") + "genre:".Length); genre = trim_parameter(genre); query.addCondition("WM/Genre", "BeginsWith", genre); genre_filter = genre; query_text = genre; query_type = "Genre"; has_query = true; } if (param.Contains("exact-artist:")) { string artist = param.Substring(param.IndexOf("exact-artist:") + "exact-artist:".Length); artist = trim_parameter(artist); query.addCondition("Author", "Equals", artist); artist_filter = artist; if (query_text.Length > 0) { query_text += ": "; query_type += "/"; } query_text += artist; query_type += "Artist"; has_query = true; has_exact_query = true; } else if (param.Contains("exact-album-artist:")) { string album_artist = param.Substring(param.IndexOf("exact-album-artist:") + "exact-album-artist:".Length); album_artist = trim_parameter(album_artist); query.addCondition("WM/AlbumArtist", "Equals", album_artist); artist_filter = album_artist; if (query_text.Length > 0) { query_text += ": "; query_type += "/"; } query_text += album_artist; query_type += "Album Artist"; has_query = true; has_exact_query = true; } else if (param.Contains("album-artist:*")) { string album_artist = param.Substring(param.IndexOf("album-artist:*") + "album-artist:*".Length); album_artist = trim_parameter(album_artist); query.addCondition("WM/AlbumArtist", "BeginsWith", album_artist); query.beginNextGroup(); query.addCondition("WM/AlbumArtist", "Contains", " " + album_artist); artist_filter = album_artist; if (query_text.Length > 0) { query_text += ": "; query_type += "/"; } query_text += album_artist; query_type += "Album Artist"; has_query = true; } else if (param.Contains("album-artist:")) { string album_artist = param.Substring(param.IndexOf("album-artist:") + "album-artist:".Length); album_artist = trim_parameter(album_artist); query.addCondition("WM/AlbumArtist", "BeginsWith", album_artist); artist_filter = album_artist; if (query_text.Length > 0) { query_text += ": "; query_type += "/"; } query_text += album_artist; query_type += "Album Artist"; has_query = true; } else if (param.Contains("artist:*")) { string artist = param.Substring(param.IndexOf("artist:*") + "artist:*".Length); artist = trim_parameter(artist); query.addCondition("Author", "BeginsWith", artist); query.beginNextGroup(); query.addCondition("Author", "Contains", " " + artist); artist_filter = artist; if (query_text.Length > 0) { query_text += ": "; query_type += "/"; } query_text += artist; query_type += "Artist"; has_query = true; } else if (param.Contains("artist:")) { string artist = param.Substring(param.IndexOf("artist:") + "artist:".Length); artist = trim_parameter(artist); query.addCondition("Author", "BeginsWith", artist); artist_filter = artist; if (query_text.Length > 0) { query_text += ": "; query_type += "/"; } query_text += artist; query_type += "Artist"; has_query = true; } if (param.Contains("exact-album:")) { string album = param.Substring(param.IndexOf("exact-album:") + "exact-album:".Length); album = trim_parameter(album); query.addCondition("WM/AlbumTitle", "Equals", album); album_filter = album; if (query_text.Length > 0) { query_text += ": "; query_type += "/"; } query_text += album; query_type += "Album"; has_query = true; has_exact_query = true; } else if (param.Contains("album:")) { string album = param.Substring(param.IndexOf("album:") + "album:".Length); album = trim_parameter(album); query.addCondition("WM/AlbumTitle", "BeginsWith", album); album_filter = album; if (query_text.Length > 0) { query_text += ": "; query_type += "/"; } query_text += album; query_type += "Album"; has_query = true; } //This is not for a query but rather for playing/enqueing exact songs if (param.Contains("exact-song:")) { string song = param.Substring(param.IndexOf("exact-song:") + "exact-song:".Length); song = trim_parameter(song); song_filter = song; if (query_text.Length > 0) { query_text += ": "; query_type += "/"; } query_text += song; query_type += "Song"; has_exact_query = true; } if (param.Contains("exact-playlist:")) { playlist_query = param.Substring(param.IndexOf("exact-playlist:") + "exact-playlist:".Length); playlist_query = trim_parameter(playlist_query); } // Indexes specified? if (param.Contains("indexes:")) { string indexes = param.Substring(param.IndexOf("indexes:") + "indexes:".Length); if (indexes.IndexOf(" ") >= 0) indexes = indexes.Substring(0, indexes.IndexOf(" ")); string[] s_idx = indexes.Split(','); foreach (string s in s_idx) { if (s.Length > 0) query_indexes.Add(Int16.Parse(s)); } if (query_text.Length > 0) { query_text += ": "; query_type += "/"; } query_type += "Tracks"; has_query = true; } if (!has_query) query_type = query_text = "All"; // Cover size specified? if (param.Contains("size-x:")) { string tmp_size = param.Substring(param.IndexOf("size-x:") + "size-x:".Length); if (tmp_size.IndexOf(" ") >= 0) tmp_size = tmp_size.Substring(0, tmp_size.IndexOf(" ")); size_x = Convert.ToInt32(tmp_size); } if (param.Contains("size-y:")) { string tmp_size = param.Substring(param.IndexOf("size-y:") + "size-y:".Length); if (tmp_size.IndexOf(" ") >= 0) tmp_size = tmp_size.Substring(0, tmp_size.IndexOf(" ")); size_y = Convert.ToInt32(tmp_size); } // Use Custom Template? if (param.Contains("template:")) { template = param.Substring(param.IndexOf("template:") + "template:".Length); if (template.IndexOf(" ") >= 0) template = template.Substring(0, template.IndexOf(" ")); } if (which_command == PLAY) should_enqueue = false; switch (which_command) { case CLEAR_CACHE: opResult.StatusCode = OpStatusCode.Ok; clear_cache(); return opResult; case LIST_GENRES: if (create_playlist_name != null) { IWMPPlaylist genre_playlist = collection.getPlaylistByQuery(query, "Audio", "WM/Genre", true); add_to_playlist(opResult, has_query, genre_playlist, create_playlist_name); opResult.StatusCode = OpStatusCode.Ok; } else { IWMPStringCollection genres; if (has_query) { genres = collection.getStringCollectionByQuery("WM/Genre", query, "Audio", "WM/Genre", true); } else { genres = collection.getAttributeStringCollection("WM/Genre", "Audio"); } if (genres != null && genres.count > 0) { result_count = 0; for (int k = 0; k < genres.count; k++) { string item = genres.Item(k); if (item != null && !item.Equals("")) { if (!m_stats_only) metadata.addGenre(item); else result_count++; } } opResult.ResultCount = result_count; metadata.trimToSize(); opResult = serializeObject(opResult, metadata, cache_fn, cache_ver); } else { opResult.StatusCode = OpStatusCode.BadRequest; opResult.StatusText = "No genres found!"; } } return opResult; case LIST_ARTISTS: if (create_playlist_name != null) { IWMPPlaylist artists_playlist = collection.getPlaylistByQuery(query, "Audio", "Author", true); add_to_playlist(opResult, has_query, artists_playlist, create_playlist_name); opResult.StatusCode = OpStatusCode.Ok; } else { IWMPStringCollection artists; if (has_query) { artists = collection.getStringCollectionByQuery("Author", query, "Audio", "Author", true); } else { artists = collection.getAttributeStringCollection("Author", "Audio"); } if (artists != null && artists.count > 0) { result_count = 0; for (int k = 0; k < artists.count; k++) { string item = artists.Item(k); if (item != null && !item.Equals("")) { if (!m_stats_only) metadata.addArtist(item); else result_count++; } } opResult.ResultCount = result_count; metadata.trimToSize(); opResult = serializeObject(opResult, metadata, cache_fn, cache_ver); } else { opResult.StatusCode = OpStatusCode.BadRequest; opResult.StatusText = "No artists found!"; } } return opResult; case LIST_ALBUM_ARTISTS: if (create_playlist_name != null) { IWMPPlaylist album_artists_playlist = collection.getPlaylistByQuery(query, "Audio", "WM/AlbumArtist", true); add_to_playlist(opResult, has_query, album_artists_playlist, create_playlist_name); opResult.StatusCode = OpStatusCode.Ok; } else { IWMPStringCollection album_artists; if (has_query) { album_artists = collection.getStringCollectionByQuery("WM/AlbumArtist", query, "Audio", "WM/AlbumArtist", true); } else { album_artists = collection.getAttributeStringCollection("WM/AlbumArtist", "Audio"); } if (album_artists != null && album_artists.count > 0) { result_count = 0; for (int k = 0; k < album_artists.count; k++) { string item = album_artists.Item(k); if (item != null && !item.Equals("") && !metadata.containsAlbumArtist(item)) { if (!m_stats_only) metadata.addAlbumArtist(item); else result_count++; } } opResult.ResultCount = result_count; metadata.trimToSize(); opResult = serializeObject(opResult, metadata, cache_fn, cache_ver); } else { opResult.StatusCode = OpStatusCode.BadRequest; opResult.StatusText = "No album artists found!"; } } return opResult; case LIST_ALBUMS: if (create_playlist_name != null) { IWMPPlaylist albums_playlist = collection.getPlaylistByQuery(query, "Audio", "WM/AlbumTitle", true); add_to_playlist(opResult, has_query, albums_playlist, create_playlist_name); opResult.StatusCode = OpStatusCode.Ok; } else { IWMPStringCollection albums; if (has_query) { albums = collection.getStringCollectionByQuery("WM/AlbumTitle", query, "Audio", "WM/AlbumTitle", true); } else { albums = collection.getAttributeStringCollection("WM/AlbumTitle", "Audio"); } if (albums != null && albums.count > 0) { result_count = 0; for (int k = 0; k < albums.count; k++) { string item = albums.Item(k); if (item != null && !item.Equals("")) { if (!m_stats_only) metadata.addAlbum(new Album(item, collection.getByAlbum(item), m_stats_only)); else result_count++; } } opResult.ResultCount = result_count; metadata.trimToSize(); opResult = serializeObject(opResult, metadata, cache_fn, cache_ver); } else { opResult.StatusCode = OpStatusCode.BadRequest; opResult.StatusText = "No albums found!"; } } return opResult; case LIST_SONGS: IWMPStringCollection songs; if (has_query) { songs = collection.getStringCollectionByQuery("Title", query, "Audio", "Title", true); } else { songs = collection.getAttributeStringCollection("Title", "Audio"); } if (songs != null && songs.count > 0) { for (int k = 0; k < songs.count; k++) { IWMPPlaylist playlist = collection.getByName(songs.Item(k)); if (playlist != null && playlist.count > 0) { if (create_playlist_name != null) { add_to_playlist(opResult, has_query, playlist, create_playlist_name); opResult.StatusCode = OpStatusCode.Ok; } else { metadata.addSongs(playlist); } } } if (create_playlist_name == null) { metadata.trimToSize(); opResult = serializeObject(opResult, metadata, cache_fn, cache_ver); } } else { opResult.StatusCode = OpStatusCode.BadRequest; opResult.StatusText = "No songs found!"; } return opResult; case LIST_PLAYLISTS: result_count = 0; playlists = getAllUserPlaylists(playlistCollection); result_count = metadata.addPlaylists(playlistCollection, playlists); metadata.trimToSize(); opResult.ResultCount = result_count; opResult.ContentText = JsonConvert.SerializeObject(metadata, Formatting.Indented); return opResult; case LIST_RECENT: if (param.Contains("count:")) { string scount = param.Substring(param.IndexOf("count:") + "count:".Length); if (scount.IndexOf(" ") >= 0) scount = scount.Substring(0, scount.IndexOf(" ")); int count = Convert.ToInt32(scount); list_recent(opResult, template, count); } else list_recent(opResult, template); opResult.StatusCode = OpStatusCode.Ok; return opResult; case LIST_NOWPLAYING: if (nowPlaying != null) { opResult.ContentText = JsonConvert.SerializeObject(nowPlaying, Formatting.Indented); } else { opResult.StatusCode = OpStatusCode.BadRequest; opResult.StatusText = "Now playing is null!"; } return opResult; case DELETE_PLAYLIST: if (playlist_query != null) { playlists = getUserPlaylistsByName(playlist_query, playlistCollection); if (playlists.count > 0) { IWMPPlaylist mod_playlist = playlists.Item(0); if (query_indexes.Count > 0) { // Delete items indicated by indexes instead of deleting playlist for (int j = 0; j < query_indexes.Count; j++) { mod_playlist.removeItem(mod_playlist.get_Item((Int16)query_indexes[j])); } opResult.ContentText = "Items removed from playlist " + mod_playlist + "."; } else { ((IWMPPlaylistCollection)Player.playlistCollection).remove(mod_playlist); opResult.ContentText = "Playlist " + mod_playlist + " deleted."; } opResult.StatusCode = OpStatusCode.Success; } else { opResult.StatusCode = OpStatusCode.BadRequest; opResult.StatusText = "Playlist does not exist!"; } } else { opResult.StatusCode = OpStatusCode.BadRequest; opResult.StatusText = "Must specify the exact playlist!"; } return opResult; case LIST_DETAILS: // Get query as a playlist if (playlist_query != null) { //Return a specific playlist when music-list-details with exact-playlist is queried playlists = getUserPlaylistsByName(playlist_query, playlistCollection); if (playlists.count > 0) { Playlist aPlaylist = new Playlist(playlist_query); mediaPlaylist = playlists.Item(0); //Or return a playlist query if (mediaPlaylist != null) { aPlaylist.addItems(mediaPlaylist); } metadata.playlists.Add(aPlaylist); } } else if (has_exact_query) { mediaPlaylist = getPlaylistFromExactQuery(query_text, query_type, collection); } else if (has_query) { string type = getSortAttributeFromQueryType(query_type); mediaPlaylist = collection.getPlaylistByQuery(query, "Audio", type, true); } if (mediaPlaylist != null) { //Create playlist from query if supplied with playlist name if (create_playlist_name != null) { add_to_playlist(opResult, has_query, mediaPlaylist, create_playlist_name); return opResult; } else if (query_indexes.Count > 0) { for (int j = 0; j < query_indexes.Count; j++) { media_item = mediaPlaylist.get_Item((Int16)query_indexes[j]); if (media_item != null) { metadata.addSong(media_item); } } } else { if (query_type.Equals("Album")) { Album album = new Album(query_text, m_stats_only); album.addTracks(mediaPlaylist); metadata.addAlbum(album); } else { metadata.addSongs(mediaPlaylist); } } } else { if (logger != null) { logger.Write("Creating library metadata object"); } //No query supplied so entire detailed library requested //Parse all albums and return, no value album will be added as songs IWMPStringCollection album_collection = collection.getAttributeStringCollection("WM/AlbumTitle", "Audio"); if (album_collection.count > 0) { result_count = 0; for (int j = 0; j < album_collection.count; j++) { if (album_collection.Item(j) != null) { //The collection seems to represent the abcense of an album as an "" string value IWMPPlaylist album_playlist = collection.getByAlbum(album_collection.Item(j)); if (album_playlist != null) { if (!album_collection.Item(j).Equals("")) { Album album = new Album(album_collection.Item(j), m_stats_only); result_count += album.addTracks(album_playlist); metadata.addAlbum(album); } else { result_count += metadata.addSongs(album_playlist); } } } } metadata.trimToSize(); } } if (logger != null) { logger.Write("Starting serialization of metadata object."); } opResult.ResultCount = result_count; opResult = serializeObject(opResult, metadata, cache_fn, cache_ver); return opResult; case PLAY: case QUEUE: if (has_exact_query) { mediaPlaylist = getPlaylistFromExactQuery(query_text, query_type, collection); } else if (has_query) { string type = getSortAttributeFromQueryType(query_type); mediaPlaylist = collection.getPlaylistByQuery(query, "Audio", type, true); } else { mediaPlaylist = collection.getByAttribute("MediaType", "Audio"); } //Play or enqueue PlayMediaCmd pmc; if (query_indexes.Count > 0) { result_count = query_indexes.Count; for (int j = 0; j < query_indexes.Count; j++) { media_item = mediaPlaylist.get_Item(j); if (media_item != null) { query_text += ((j == 0) ? "" : ", ") + (Int16)query_indexes[j] + ". " + media_item.getItemInfo("Title"); } } pmc = new PlayMediaCmd(remotePlayer, mediaPlaylist, query_indexes, should_enqueue); } else { result_count = mediaPlaylist.count; pmc = new PlayMediaCmd(remotePlayer, mediaPlaylist, should_enqueue); } opResult = pmc.Execute(null); // Type, Artist, Album, Track, param, count add_to_mrp(query_type, query_text, param, result_count); //Add to recent played list return opResult; case SERV_COVER: if (has_exact_query) { mediaPlaylist = getPlaylistFromExactQuery(query_text, query_type, collection); } else if (has_query) { string type = getSortAttributeFromQueryType(query_type); mediaPlaylist = collection.getPlaylistByQuery(query, "Audio", type, true); } else { mediaPlaylist = collection.getByAttribute("MediaType", "Audio"); } try { if (query_indexes.Count > 0) { for (int j = 0; j < query_indexes.Count; j++) { media_item = mediaPlaylist.get_Item((Int16)query_indexes[j]); if (media_item != null) { string album_path = findAlbumPath(media_item.sourceURL); photoCmd pc = new photoCmd(photoCmd.SERV_PHOTO); if (album_path.Length == 0) return pc.getPhoto(DEFAULT_IMAGE, "jpeg", size_x, size_y); else return pc.getPhoto(album_path, "jpeg", size_x, size_y); } } } else { for (int j = 0; j < mediaPlaylist.count; j++) { media_item = mediaPlaylist.get_Item(j); if (media_item != null) { string album_path = findAlbumPath(media_item.sourceURL); photoCmd pc = new photoCmd(photoCmd.SERV_PHOTO); if (album_path.Length == 0) return pc.getPhoto(DEFAULT_IMAGE, "jpeg", size_x, size_y); else return pc.getPhoto(album_path, "jpeg", size_x, size_y); } } } } catch (Exception ex) { opResult.StatusCode = OpStatusCode.Exception; opResult.StatusText = ex.Message; } return opResult; } } catch (Exception ex) { opResult = new OpResult(); opResult.StatusCode = OpStatusCode.Exception; opResult.StatusText = ex.Message; opResult.AppendFormat("{0}", debug_last_action); opResult.AppendFormat("{0}", ex.Message); } debug_last_action = "Execute: End"; return opResult; }