SelectDatabase.xaml.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Media;
  11. using Comal.Classes;
  12. using InABox.IPC;
  13. using InABox.Clients;
  14. using InABox.Configuration;
  15. using InABox.Core;
  16. using InABox.IPC;
  17. using InABox.Rpc;
  18. using InABox.Wpf;
  19. using InABox.WPF;
  20. using PRSServer;
  21. using Image = System.Windows.Controls.Image;
  22. using Size = System.Drawing.Size;
  23. namespace PRSDesktop
  24. {
  25. /// <summary>
  26. /// Interaction logic for SelectDatabase.xaml
  27. /// </summary>
  28. public partial class SelectDatabase : ThemableWindow
  29. {
  30. private CancellationTokenSource cancel = new CancellationTokenSource();
  31. public SelectDatabase(Dictionary<string, DatabaseSettings> databases)
  32. {
  33. InitializeComponent();
  34. var active = databases.Where(x => x.Value.IsActive).ToArray().Length;
  35. var cols = (int)Math.Ceiling(Math.Sqrt(active));
  36. for (var iCol = 0; iCol < cols; iCol++)
  37. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) });
  38. var rows = (int)Math.Ceiling(active / (double)cols);
  39. for (var iRow = 0; iRow < cols; iRow++)
  40. Grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
  41. var i = 0;
  42. foreach (var key in databases.Keys)
  43. if (databases[key].IsActive)
  44. {
  45. var db = databases[key];
  46. var button = new Button
  47. {
  48. Background = new SolidColorBrush(Colors.Transparent),
  49. BorderThickness = new Thickness(0),
  50. Padding = new Thickness(0)
  51. };
  52. var color = Colors.Red;
  53. // try
  54. // {
  55. // color = (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(db.ColorScheme);
  56. // }
  57. // catch (Exception e)
  58. // {
  59. // color = Colors.Red;
  60. // }
  61. var border = new Border
  62. {
  63. CornerRadius = new CornerRadius(5),
  64. BorderBrush = new SolidColorBrush(color),
  65. BorderThickness = new Thickness(0.75),
  66. Background = db.DatabaseType == DatabaseType.Standalone ?
  67. new SolidColorBrush(Colors.WhiteSmoke) :
  68. new SolidColorBrush(Colors.White),
  69. Width = 200,
  70. Height = 200
  71. };
  72. var panel = new DockPanel { VerticalAlignment = VerticalAlignment.Stretch };
  73. border.Child = panel;
  74. StackPanel stack = new StackPanel() { Orientation = Orientation.Horizontal, HorizontalAlignment = HorizontalAlignment.Center };
  75. stack.SetValue(DockPanel.DockProperty, Dock.Top);
  76. Label dbtype = new Label()
  77. {
  78. Content = db.DatabaseType.ToString(),
  79. Margin = new Thickness(5,2,0,0),
  80. HorizontalContentAlignment = HorizontalAlignment.Left,
  81. FontSize = 8,
  82. Foreground = new SolidColorBrush(Colors.DimGray)
  83. };
  84. stack.Children.Add(dbtype);
  85. Label dbver = new Label()
  86. {
  87. Margin = new Thickness(0,2,5,0),
  88. HorizontalContentAlignment = HorizontalAlignment.Right,
  89. FontSize = 8,
  90. Foreground = new SolidColorBrush(Colors.DimGray)
  91. };
  92. stack.Children.Add(dbver);
  93. panel.Children.Add(stack);
  94. var label = new Label
  95. {
  96. Content = key,
  97. HorizontalContentAlignment = HorizontalAlignment.Center,
  98. VerticalContentAlignment = VerticalAlignment.Center,
  99. Width = 150,
  100. Height = 35
  101. };
  102. label.SetValue(DockPanel.DockProperty, Dock.Bottom);
  103. panel.Children.Add(label);
  104. var image = new Image();
  105. image.Height = 150;
  106. image.Width = 150;
  107. image.SetValue(DockPanel.DockProperty, Dock.Top);
  108. panel.Children.Add(image);
  109. button.Content = border;
  110. button.Margin = new Thickness(5, 5, 0, 0);
  111. button.Click += Button_Click;
  112. button.Height = 200;
  113. button.Width = 200;
  114. button.Tag = key;
  115. var col = i % cols;
  116. var row = i / cols;
  117. button.SetValue(Grid.ColumnProperty, col);
  118. button.SetValue(Grid.RowProperty, row);
  119. Grid.Children.Add(button);
  120. Task.Run(
  121. () =>
  122. {
  123. while (!cancel.IsCancellationRequested)
  124. {
  125. DatabaseInfo? info = new DatabaseInfo(db.ColorScheme, db.Logo, CoreUtils.GetVersion(), true);
  126. if (db.DatabaseType == DatabaseType.Local)
  127. info = new RpcClientPipeTransport(DatabaseServerProperties.GetPipeName(db.LocalServerName)).Info();
  128. else if (db.DatabaseType == DatabaseType.Networked)
  129. info = new RpcClientSocketTransport(db.URLs).Info();
  130. UpdateInfo(key, db, info, border, image, dbver);
  131. Task.Delay(1000).Wait();
  132. }
  133. }
  134. ,cancel.Token);
  135. i++;
  136. }
  137. }
  138. private bool LogoEqual(byte[]? first, byte[]? second)
  139. {
  140. if ((first == null) && (second == null))
  141. return true;
  142. if ((first == null) || (second == null))
  143. return false;
  144. return first.SequenceEqual(second);
  145. }
  146. private void UpdateInfo(String key, DatabaseSettings db, DatabaseInfo? info, Border border, Image image, Label version)
  147. {
  148. Dispatcher.Invoke(() =>
  149. {
  150. if (info == null)
  151. {
  152. border.BorderBrush = new SolidColorBrush(Colors.Red);
  153. border.Background = db.DatabaseType == DatabaseType.Standalone
  154. ? new SolidColorBrush(Colors.WhiteSmoke)
  155. : new SolidColorBrush(Colors.White);
  156. image.Source = null;
  157. version.Content = "";
  158. return;
  159. }
  160. if (info?.ColorScheme is not null)
  161. {
  162. try
  163. {
  164. var color = (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(info.ColorScheme);
  165. border.BorderBrush = new SolidColorBrush(color);
  166. border.Background = new SolidColorBrush(color.AdjustLightness(90));
  167. }
  168. catch
  169. {
  170. }
  171. }
  172. Bitmap? bitmap = null; //PRSDesktop.Resources.splash_small;
  173. try
  174. {
  175. if (info?.Logo != null && info.Logo.Any())
  176. using (var ms = new MemoryStream(info.Logo))
  177. bitmap = new Bitmap(ms);
  178. }
  179. catch
  180. {
  181. }
  182. if (bitmap != null)
  183. {
  184. var size = new Size(bitmap.Width, bitmap.Height).Adjust(150, 150);
  185. image.Source = bitmap.AsBitmapImage(size.Height, size.Width);
  186. }
  187. try
  188. {
  189. version.Content = String.Equals(info?.Version,"???") ? "" : info?.Version;
  190. }
  191. catch
  192. {
  193. }
  194. if (!string.Equals(db.ColorScheme, info?.ColorScheme) || !LogoEqual(db.Logo, info?.Logo))
  195. {
  196. db.ColorScheme = info?.ColorScheme ?? DatabaseSettings.DefaultColorScheme;
  197. db.Logo = info?.Logo;
  198. new LocalConfiguration<DatabaseSettings>(key).Save(db);
  199. }
  200. });
  201. }
  202. public string Database { get; private set; }
  203. private void Button_Click(object sender, RoutedEventArgs e)
  204. {
  205. cancel.Cancel();
  206. Database = (sender as Button).Tag as string;
  207. DialogResult = true;
  208. }
  209. }
  210. }