SelectDatabase.xaml.cs 9.4 KB

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