SelectDatabase.xaml.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. {
  128. info = new IPCClient<User>(DatabaseServerProperties.GetPipeName(db.LocalServerName, false)).Info();
  129. //info = new RpcClientPipeTransport(DatabaseServerProperties.GetPipeName(db.LocalServerName, true)).Info();
  130. }
  131. else if (db.DatabaseType == DatabaseType.Networked)
  132. {
  133. //info = new RpcClientSocketTransport(db.URLs).Info();
  134. RestClient<User>.Ping(db.URLs, out DatabaseInfo i);
  135. info = i;
  136. }
  137. UpdateInfo(key, db, info, border, image, dbver);
  138. Task.Delay(1000).Wait();
  139. }
  140. }
  141. ,cancel.Token);
  142. i++;
  143. }
  144. }
  145. private bool LogoEqual(byte[]? first, byte[]? second)
  146. {
  147. if ((first == null) && (second == null))
  148. return true;
  149. if ((first == null) || (second == null))
  150. return false;
  151. return first.SequenceEqual(second);
  152. }
  153. private void UpdateInfo(String key, DatabaseSettings db, DatabaseInfo? info, Border border, Image image, Label version)
  154. {
  155. Dispatcher.Invoke(() =>
  156. {
  157. if (info == null)
  158. {
  159. border.BorderBrush = new SolidColorBrush(Colors.Red);
  160. border.Background = db.DatabaseType == DatabaseType.Standalone
  161. ? new SolidColorBrush(Colors.WhiteSmoke)
  162. : new SolidColorBrush(Colors.White);
  163. image.Source = null;
  164. version.Content = "";
  165. return;
  166. }
  167. if (info?.ColorScheme is not null)
  168. {
  169. try
  170. {
  171. var color = (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(info.ColorScheme);
  172. border.BorderBrush = new SolidColorBrush(color);
  173. border.Background = new SolidColorBrush(color.AdjustLightness(90));
  174. }
  175. catch
  176. {
  177. }
  178. }
  179. Bitmap? bitmap = null; //PRSDesktop.Resources.splash_small;
  180. try
  181. {
  182. if (info?.Logo != null && info.Logo.Any())
  183. using (var ms = new MemoryStream(info.Logo))
  184. bitmap = new Bitmap(ms);
  185. }
  186. catch
  187. {
  188. }
  189. if (bitmap != null)
  190. {
  191. var size = new Size(bitmap.Width, bitmap.Height).Adjust(150, 150);
  192. image.Source = bitmap.AsBitmapImage(size.Height, size.Width);
  193. }
  194. try
  195. {
  196. version.Content = String.Equals(info?.Version,"???") ? "" : info?.Version;
  197. }
  198. catch
  199. {
  200. }
  201. if (!string.Equals(db.ColorScheme, info?.ColorScheme) || !LogoEqual(db.Logo, info?.Logo))
  202. {
  203. db.ColorScheme = info?.ColorScheme ?? DatabaseSettings.DefaultColorScheme;
  204. db.Logo = info?.Logo;
  205. new LocalConfiguration<DatabaseSettings>(key).Save(db);
  206. }
  207. });
  208. }
  209. public string Database { get; private set; }
  210. private void Button_Click(object sender, RoutedEventArgs e)
  211. {
  212. cancel.Cancel();
  213. Database = (sender as Button).Tag as string;
  214. DialogResult = true;
  215. }
  216. }
  217. }