/// <summary> /// Updates a link object. /// </summary> /// <param name="link">The link object to be updated</param> /// <param name="connected">Whether the link is connected or not</param> /// <param name="json">A JSON object describing the link (cannot be null if connected = true)</param> /// <param name="forceDefaults">Whether or not the boolean properties should be set to default values if they do not appear in the JSON object</param> private static void UpdateLink(Link link, bool connected, JObject json = null, bool forceDefaults = true) { if (connected) { string[] props = new string[] { "share", "listen", "donate", "create_playlist" }; string[] pref = new string[] { "do", "can" }; Dictionary<string, bool> settings = new Dictionary<string, bool>(); foreach (string prop in props) { foreach (string prefix in pref) { string key = prefix + "_" + prop; try { settings[key] = json[key] != null && json[key].ToString().ToLower() == "true"; } catch { settings[key] = false; } } } if (json["display"] != null) link.Provider = (string)json["display"]; if (json["url"] != null) link.URL = (string)json["url"]; if (json["error"] != null) link.Error = (string)json["error"]; try { if (json["id"] != null) link.ID = Convert.ToUInt32(String.Format("{0}", json["id"])); } catch { } if (json["can_share"] != null || forceDefaults) link.CanShare = settings["can_share"]; if (json["do_share"] != null || forceDefaults) link.DoShare = settings["do_share"]; if (json["can_listen"] != null || forceDefaults) link.CanListen = settings["can_listen"]; if (json["do_listen"] != null || forceDefaults) link.DoListen = settings["do_listen"]; if (json["can_donate"] != null || forceDefaults) link.CanDonate = settings["can_donate"]; if (json["do_donate"] != null || forceDefaults) link.DoDonate = settings["do_donate"]; if (json["can_create_playlist"] != null || forceDefaults) link.CanCreatePlaylist = settings["can_create_playlist"]; if (json["do_create_playlist"] != null || forceDefaults) link.DoCreatePlaylist = settings["do_create_playlist"]; link.Connected = true; } else { link.Connected = false; } }
/// <summary> /// Deletes a given link to a third party service provider. /// </summary> /// <param name="link">The link to be deleted</param> public static void DeleteLink(Link link) { try { var response = SendRequest(link.URL + ".json", "DELETE"); if (response == null || response.StatusCode != HttpStatusCode.NoContent) { U.L(LogLevel.Error, "SERVICE", "There was a problem deleting link to " + link.Provider); U.L(LogLevel.Error, "SERVICE", response); } else { U.L(LogLevel.Information, "SERVICE", "Link to " + link.Provider + " deleted"); } if (response != null) response.Close(); } catch (Exception e) { U.L(LogLevel.Warning, "SERVICE", "Could not delete link to "+link.Provider+": " + e.Message); } }
/// <summary> /// Creates a link according to a JSON object. /// </summary> /// <param name="connected">Whether or not the link is connected or not</param> /// <param name="link">The JSON object describing the link</param> /// <returns>The Link object corresponding to the JSON object</returns> private static Link CreateLink(bool connected, JObject link) { try { if (connected) { Link l = new Link(); UpdateLink(l, true, link); l.ConnectURL = String.Format("{0}?origin=%2Fdashboard", link["connectURL"]); return l; } else { return new Link() { Provider = (string)link["display"], URL = (string)link["url"], ConnectURL = (string)link["url"], Connected = false }; } } catch (Exception e) { U.L(LogLevel.Warning, "SERVICE", "Could not create link object: " + e.Message); return null; } }
/// <summary> /// Updates the properties of a link object at the server. /// </summary> /// <param name="link">The link to update</param> public static void UpdateLink(Link link) { try { string url = String.Format("{0}.json", link.URL); string query = "?"; query += U.CreateParam("do_share", link.DoShare, "link") + "&"; query += U.CreateParam("do_listen", link.DoListen, "link") + "&"; query += U.CreateParam("do_donate", link.DoDonate, "link") + "&"; query += U.CreateParam("do_create_playlist", link.DoCreatePlaylist, "link"); var response = SendRequest(url, "PUT", query); if (response == null || response.StatusCode != HttpStatusCode.OK) { U.L(LogLevel.Error, "SERVICE", "There was a problem updating link to " + link.Provider); U.L(LogLevel.Error, "SERVICE", response); } else { U.L(LogLevel.Debug, "SERVICE", "Updated link to " + link.Provider); } if (response != null) response.Close(); } catch (Exception e) { U.L(LogLevel.Warning, "SERVICE", "Could not update link to "+link.Provider+": " + e.Message); } }
/// <summary> /// Creates a list of all checkboxes for a link. /// </summary> /// <param name="link">The link to the third party</param> /// <returns>A list of checkbox controls indexed by their name</returns> private Dictionary<string, CheckBox> GetLinkCheckboxes(Link link) { Dictionary<string, CheckBox> checkboxes = new Dictionary<string, CheckBox>(); Dispatcher.Invoke(DispatcherPriority.Background, new Action(delegate() { StackPanel sp = connectedLinks[link.Provider] as StackPanel; if (sp != null) foreach (FrameworkElement cb in sp.Children) if (cb is CheckBox) checkboxes[((string[])cb.Tag)[1]] = cb as CheckBox; })); return checkboxes; }
/// <summary> /// Creates a button for a given link which is not connected. /// </summary> /// <param name="link">The link object</param> /// <returns>A button which is used to represent an unconnected link and allows the user to connect it</returns> private Button CreateUnconnectedLink(Link link) { Button b = new Button(); b.Content = String.Format(U.T("ServicesLink", "Content"), link.Provider); b.HorizontalAlignment = HorizontalAlignment.Left; b.MinWidth = 150; b.Padding = new Thickness(15, 1, 15, 1); b.Margin = new Thickness(0,5,5,5); b.Tag = new string[] { link.Provider, link.ConnectURL }; b.Click += new RoutedEventHandler(Link_Click); return b; }
/// <summary> /// Builds the control for managing a link to a third party. /// </summary> /// <param name="link">The link to the third party service</param> private void BuildLink(Link link) { try { if (link.Connected) { link.PropertyChanged -= Link_PropertyChanged; link.PropertyChanged += Link_PropertyChanged; // remove from unconnected if it exists there if (notConnectedLinks.ContainsKey(link.Provider)) notConnectedLinks.Remove(link.Provider); if (connectedLinks.ContainsKey(link.Provider)) // adapt existing control { StackPanel sp = connectedLinks[link.Provider] as StackPanel; Dictionary<string, CheckBox> checkboxes = GetLinkCheckboxes(link); Thickness m = new Thickness(5); // update error visibility StackPanel spNotice = linkNotices[link.Provider]; if (spNotice != null) { if (String.IsNullOrWhiteSpace(link.Error)) spNotice.Visibility = Visibility.Collapsed; else { spNotice.Visibility = Visibility.Visible; spNotice.ToolTip = link.Error; relinkTimer.Start(); } } // add or modify capabilities on the link controls if (link.CanShare && !checkboxes.ContainsKey("Share")) sp.Children.Insert(0, CreateLinkCheckBox(link.Provider, "Share", link.DoShare)); else if (link.CanShare) checkboxes["Share"].IsChecked = link.DoShare; if (link.CanListen && !checkboxes.ContainsKey("Listen")) sp.Children.Insert(1, CreateLinkCheckBox(link.Provider, "Listen", link.DoListen)); else if (link.CanListen) checkboxes["Listen"].IsChecked = link.DoListen; if (link.CanCreatePlaylist && !checkboxes.ContainsKey("CreatePlaylist")) sp.Children.Insert(3, CreateLinkCheckBox(link.Provider, "CreatePlaylist", link.DoCreatePlaylist)); else if (link.CanCreatePlaylist) checkboxes["CreatePlaylist"].IsChecked = link.DoCreatePlaylist; if (link.CanDonate && !checkboxes.ContainsKey("Donate")) sp.Children.Insert(2, CreateLinkCheckBox(link.Provider, "Donate", link.DoDonate)); else if (link.CanDonate) checkboxes["Donate"].IsChecked = link.DoDonate; // remove checkboxes for capabilities the link doesn't have if (!link.CanShare && checkboxes.ContainsKey("Share")) sp.Children.Remove(checkboxes["Share"]); if (!link.CanListen && checkboxes.ContainsKey("Listen")) sp.Children.Remove(checkboxes["Listen"]); if (!link.CanDonate && checkboxes.ContainsKey("Donate")) sp.Children.Remove(checkboxes["Donate"]); if (!link.CanCreatePlaylist && checkboxes.ContainsKey("CreatePlaylist")) sp.Children.Remove(checkboxes["CreatePlaylist"]); } else // create new controls { StackPanel sp = new StackPanel() { Orientation = Orientation.Vertical }; Thickness m = new Thickness(5); DockPanel dp = new DockPanel(); dp.Children.Add(new TextBlock { Text = link.Provider, FontSize = 14, Margin = new Thickness(0, 5, 0, 0) }); StackPanel spNotice = new StackPanel() { HorizontalAlignment = HorizontalAlignment.Right, Orientation = Orientation.Horizontal, VerticalAlignment = VerticalAlignment.Bottom }; DockPanel.SetDock(spNotice, Dock.Left); spNotice.Children.Add(new Image() { Source = Utilities.GetIcoImage("pack://application:,,,/Platform/Windows 7/GUI/Images/Icons/Error.ico", 16, 16), Width = 16, Height = 16 }); spNotice.Children.Add(new TextBlock() { Text = String.Format(U.T("ServicesLinkDisconnected"), link.Provider), FontSize = 10, VerticalAlignment = VerticalAlignment.Center, Margin = new Thickness(5,0,5,0) }); Button relink = new Button() { Content = U.T("ServicesReconnect"), FontSize = 10, Padding = new Thickness(10, 0, 10, 1), Tag = link.ConnectURL }; relink.Click += new RoutedEventHandler(Relink_Click); spNotice.Children.Add(relink); if (String.IsNullOrWhiteSpace(link.Error)) spNotice.Visibility = Visibility.Collapsed; else { spNotice.ToolTip = link.Error; relinkTimer.Start(); } dp.Children.Add(spNotice); sp.Children.Add(dp); linkNotices[link.Provider] = spNotice; if (link.CanShare) sp.Children.Add(CreateLinkCheckBox(link.Provider, "Share", link.DoShare)); if (link.CanListen) sp.Children.Add(CreateLinkCheckBox(link.Provider, "Listen", link.DoListen)); if (link.CanCreatePlaylist) sp.Children.Add(CreateLinkCheckBox(link.Provider, "CreatePlaylist", link.DoCreatePlaylist)); if (link.CanDonate) sp.Children.Add(CreateLinkCheckBox(link.Provider, "Donate", link.DoDonate)); m = new Thickness(0, 5, 0, 15); if (link.CanShare || link.CanListen || link.CanDonate || link.CanCreatePlaylist) m.Top = 0; Button b = new Button(); b.MinWidth = 80; b.Padding = new Thickness(15, 1, 15, 1); b.Margin = m; b.Content = U.T("ServicesUnlink", "Content"); b.Tag = new string[] { link.Provider, link.URL, link.ConnectURL }; b.Click += new RoutedEventHandler(Unlink_Click); b.HorizontalAlignment = HorizontalAlignment.Left; sp.Tag = new string[] { link.Provider, link.URL }; sp.Children.Add(b); connectedLinks[link.Provider] = sp; } } else { // remove from connected if it exists there if (connectedLinks.ContainsKey(link.Provider)) connectedLinks.Remove(link.Provider); if (notConnectedLinks.ContainsKey(link.Provider)) { // adapt existing control Button b = notConnectedLinks[link.Provider] as Button; b.Content = String.Format(U.T("ServicesLink", "Content"), link.Provider); b.Tag = new string[] { link.Provider, link.ConnectURL }; } else // create new controls notConnectedLinks[link.Provider] = CreateUnconnectedLink(link); } // make sure all and only existing links are in GUI for (int i = 0; i < notConnectedLinks.Count; i++) { if (NotConnectedLinks.Children.Count > i) { Button b = (Button)NotConnectedLinks.Children[i]; string modl = notConnectedLinks.ElementAt(i).Key; string view = ((string[])b.Tag)[0]; int cmpr = String.Compare(modl, view); if (cmpr > 0) // view needs to be removed { NotConnectedLinks.Children.RemoveAt(i--); continue; } if (cmpr < 0) // model needs to be added to view NotConnectedLinks.Children.Insert(i, notConnectedLinks.ElementAt(i).Value); } else NotConnectedLinks.Children.Add(notConnectedLinks.ElementAt(i).Value); } while (NotConnectedLinks.Children.Count > notConnectedLinks.Count) NotConnectedLinks.Children.RemoveAt(notConnectedLinks.Count); for (int i = 0; i < connectedLinks.Count; i++) { if (ConnectedLinks.Children.Count > i) { StackPanel sp = (StackPanel)ConnectedLinks.Children[i]; string modl = connectedLinks.ElementAt(i).Key; string view = ((string[])sp.Tag)[0]; int cmpr = String.Compare(modl, view); while (cmpr > 0) // view needs to be removed { ConnectedLinks.Children.RemoveAt(i--); continue; } if (cmpr < 0) // model needs to be added to view ConnectedLinks.Children.Insert(i, connectedLinks.ElementAt(i).Value); } else ConnectedLinks.Children.Add(connectedLinks.ElementAt(i).Value); } while (ConnectedLinks.Children.Count > connectedLinks.Count) ConnectedLinks.Children.RemoveAt(connectedLinks.Count); } catch (Exception e) { U.L(LogLevel.Error, "CONTROL", "Could not build link controls for " + link.Provider + ": " + e.Message); } }