/// <summary> /// The dispatcher of the <see cref="PlaylistModified"/> event /// </summary> /// <param name="playlist">The playlist that was modified</param> /// <param name="type">The type of modification that occured</param> /// <param name="interactive">Whether the action was performed by the user directly</param> private static void DispatchPlaylistModified(PlaylistData playlist, ModifyType type, bool interactive = false) { if (PlaylistModified != null) PlaylistModified(playlist, new ModifiedEventArgs(type, null, interactive)); }
/// <summary> /// The dispatcher of the <see cref="PlaylistRenamed"/> event /// </summary> /// <param name="playlist">The playlist that was renamed</param> /// <param name="oldName">The name of the playlist before the change</param> /// <param name="newName">The name of the playlist after the change</param> private static void DispatchPlaylistRenamed(PlaylistData playlist, string oldName, string newName) { if (PlaylistRenamed != null) PlaylistRenamed(playlist, new RenamedEventArgs(WatcherChangeTypes.Renamed, "playlist", newName, oldName)); }
/// <summary> /// Checks if a playlist belongs to someone else. /// </summary> /// <param name="playlist">The playlist to check</param> /// <returns>true if someone else is the owner of the playlist, otherwise false</returns> public static bool IsSomeoneElses(PlaylistData playlist) { if (playlist == null) return false; var id = ServiceManager.Identity; return playlist.Owner > 0 && (id == null || id.UserID != playlist.Owner); }
/// <summary> /// Renames a playlist. If a playlist with the new name already exist or the new name is either "Create new" or "" it will do nothing. /// </summary> /// <param name="playlist">The playlist to be renamed</param> /// <param name="newName">The new name of the playlist</param> public static void RenamePlaylist(PlaylistData playlist, String newName) { if (playlist != null && playlist.Name != newName) { string oldName = playlist.Name; if (FindPlaylist(newName) != null) { int pExt = 1; while (FindPlaylist(newName + pExt) != null) pExt++; newName = newName + pExt; } if (playlist != null && newName != "" && newName.ToLower() != U.T("NavigationCreateNew").ToLower()) DispatchPlaylistRenamed(playlist, oldName, newName); } }
/// <summary> /// Checks whether a given playlist contains any of a given list of track. /// </summary> /// <param name="playlist">The playlist to search in</param> /// <param name="tracks">The tracks to search for</param> /// <returns>True of <paramref name="playlist"/> contains any of <paramref name="tracks"/>, otherwise false</returns> public static bool ContainsAny(PlaylistData playlist, List<TrackData> tracks) { foreach (TrackData t1 in playlist.Tracks) foreach (TrackData t2 in tracks) if (t1.Path == t2.Path) return true; return false; }
/// <summary> /// Creates a new playlist /// </summary> /// <param name="name">The name of the new playlist (this will be appended with a number if neccessary)</param> /// <param name="id">The ID of the playlist in the cloud</param> /// <param name="owner">The ID of the user who owns the playlist</param> /// <param name="interactive">Whether the action was performed by the user directly</param> /// <returns>The newly created PlaylistData for the playlist</returns> public static PlaylistData CreatePlaylist(String name, uint id = 0, uint owner = 0, bool interactive = false) { name = U.CleanXMLString(name); if (FindPlaylist(name) != null) { int pExt = 1; while (FindPlaylist(name + pExt) != null) pExt++; name = name + pExt; } PlaylistData playlist = new PlaylistData(); playlist.Name = name; playlist.ID = id; playlist.Owner = owner; playlist.Time = 0; playlist.Tracks = new ObservableCollection<TrackData>(); playlist.Tracks.CollectionChanged += TracksChanged; SettingsManager.Playlists.Add(playlist); DispatchPlaylistModified(playlist, ModifyType.Created, interactive); return playlist; }
/// <summary> /// Checks whether a given playlist contains a given track. /// </summary> /// <param name="playlist">The playlist to search in</param> /// <param name="track">The track to search for</param> /// <returns>True of <paramref name="playlist"/> contains <paramref name="track"/>, otherwise false</returns> public static bool Contains(PlaylistData playlist, TrackData track) { foreach (TrackData t in playlist.Tracks) if (t.Path == track.Path) return true; return false; }
/// <summary> /// Checks whether a given playlist contains all of a given list of track. /// </summary> /// <param name="playlist">The playlist to search in</param> /// <param name="tracks">The tracks to search for</param> /// <returns>True of <paramref name="playlist"/> contains all of <paramref name="tracks"/>, otherwise false</returns> public static bool ContainsAll(PlaylistData playlist, List<TrackData> tracks) { foreach (TrackData t1 in playlist.Tracks) foreach (TrackData t2 in tracks) if (t1.Path != t2.Path) return false; return playlist.Tracks.Count != 0; }
/// <summary> /// Uploads a playlist to the cloud. /// </summary> /// <param name="playlist">The playlist to upload</param> private static void UploadPlaylist(PlaylistData playlist) { if (playlist == null) return; JArray tracks = new JArray(); while (true) { try { tracks.Clear(); foreach (var track in playlist.Tracks) { var t = TrackToJSON(track); if (t != null) tracks.Add(t); } break; } catch { } } JObject para = new JObject(); para["name"] = playlist.Name; para["is_public"] = "0"; para["songs"] = tracks; lock (syncOutLocker) syncOutBuffer.Add(new SyncOperation("create", "playlists", para)); InitiateSyncTimer(); }
/// <summary> /// Shares a playlist. /// </summary> /// <param name="playlist">The playlist to share</param> public static void SharePlaylist(PlaylistData playlist) { ThreadStart shareThread = delegate() { string query = String.Format("?playlist={0}&object=playlist", OAuth.Manager.UrlEncode(playlist.Name)); var response = SendRequest("/shares.json", "POST", query); if (response == null || response.StatusCode != HttpStatusCode.Created) { U.L(LogLevel.Error, "SERVICE", "There was a problem sharing playlist " + playlist.Name); U.L(LogLevel.Error, "SERVICE", response); } else { U.L(LogLevel.Information, "SERVICE", "Shared playlist "+playlist.Name); } if (response != null) response.Close(); }; Thread sharethread = new Thread(shareThread); sharethread.Name = "Share thread"; sharethread.Priority = ThreadPriority.Lowest; sharethread.Start(); }
/// <summary> /// Retrieve the latest data on a playlist, owned by someone else, from the cloud and update it. /// </summary> /// <param name="playlist">The playlist to be udpated</param> public static void RefreshPlaylist(PlaylistData playlist) { if (playlist == null || !PlaylistManager.IsSomeoneElses(playlist)) return; ThreadStart thread = delegate() { try { U.L(LogLevel.Debug, "SERVICE", "Retrieving playlist: " + playlist.Name + " ("+playlist.ID.ToString()+")"); string url = String.Format("/playlists/"+playlist.ID.ToString()+".json"); var response = SendRequest(url, "GET"); if (response == null || response.StatusCode != HttpStatusCode.OK) { U.L(LogLevel.Error, "SERVICE", "There was a problem retrieving playlist"); U.L(LogLevel.Error, "SERVICE", response); //PlaylistManager.RemovePlaylist(playlist.ID); } else { MergePlaylistObject(ParseResponse(response) as JObject); U.L(LogLevel.Information, "SERVICE", "Synchronized playlist " + playlist.Name); } if (response != null) response.Close(); } catch (Exception e) { U.L(LogLevel.Error, "SERVICE", "There was a problem synchronizing playlist "+playlist.Name); U.L(LogLevel.Error, "SERVICE", e.Message); } }; Thread th = new Thread(thread); th.Name = "Playlist sync thread"; th.Priority = ThreadPriority.Lowest; th.Start(); }
/// <summary> /// Remove a playlist from the search box's context menu /// </summary> /// <param name="playlist">The playlist to be removed</param> public void RemovePlaylist(PlaylistData playlist) { // remove from "add to playlist" menu in list List<MenuItem> menu_items_to_remove = new List<MenuItem>(); foreach (MenuItem item in Menu_Add.Items) { if (item.Header.ToString() == playlist.Name) menu_items_to_remove.Add(item); } foreach (MenuItem item in menu_items_to_remove) Menu_Add.Items.Remove(item); // remove from "remove from playlist" menu in list menu_items_to_remove.Clear(); foreach (MenuItem item in Menu_Remove.Items) { if (item.Header.ToString() == playlist.Name) menu_items_to_remove.Add(item); } foreach (MenuItem item in menu_items_to_remove) Menu_Remove.Items.Remove(item); if (SettingsManager.Playlists.Count <= 0) Menu_Remove.IsEnabled = false; }
/// <summary> /// Add a new playlist to the search box's context menu /// </summary> /// <param name="playlist">The playlist to be added</param> public void AddPlaylist(PlaylistData playlist) { // create the menu item in "Add to Playlist" in list MenuItem ListAddMenu = new MenuItem(); ListAddMenu.Header = playlist.Name; ListAddMenu.Click += AddToPlaylist_Clicked; Menu_Add.Items.Insert(Menu_Add.Items.Count - 1, ListAddMenu); // create the menu item in "Remove from Playlist" in list MenuItem ListDelMenu = new MenuItem(); ListDelMenu.Header = playlist.Name; ListDelMenu.Click += RemoveFromPlaylist_Clicked; Menu_Remove.Items.Insert(Menu_Remove.Items.Count, ListDelMenu); Menu_Remove.IsEnabled = true; }