SelectDatabase.xaml.cs 8.6 KB

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