SelectDatabase.xaml.cs 8.6 KB

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