/// <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> /// 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> /// 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) { // 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); // 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); sp.Children.Add(new TextBlock { Text = link.Provider, FontSize = 14, Margin = new Thickness(0, 5, 0, 0) }); 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); } }