SelectDatabase.xaml.cs 10 KB

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