SelectDatabase.xaml.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 InABox.IPC;
  12. using InABox.Clients;
  13. using InABox.Configuration;
  14. using InABox.Core;
  15. using InABox.IPC;
  16. using InABox.Rpc;
  17. using InABox.Wpf;
  18. using InABox.WPF;
  19. using PRSServer;
  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(db.ColorScheme, db.Logo, CoreUtils.GetVersion(), true);
  121. if (db.DatabaseType == DatabaseType.Local)
  122. info = new RpcClientPipeTransport(DatabaseServerProperties.GetPipeName(db.LocalServerName)).Info();
  123. else if (db.DatabaseType == DatabaseType.Networked)
  124. {
  125. List<Task<DatabaseInfo?>> infos = new List<Task<DatabaseInfo?>>();
  126. foreach (var url in db.URLs)
  127. infos.Add(Task.Run<DatabaseInfo?>(() => new RpcClientSocketTransport(url).Info()));
  128. var index = Task.WaitAny(infos.ToArray<Task>());
  129. info = infos[index].Result;
  130. }
  131. UpdateInfo(key, db, info, border, image, dbver);
  132. });
  133. i++;
  134. }
  135. }
  136. private bool LogoEqual(byte[]? first, byte[]? second)
  137. {
  138. if ((first == null) && (second == null))
  139. return true;
  140. if ((first == null) || (second == null))
  141. return false;
  142. return first.SequenceEqual(second);
  143. }
  144. private void UpdateInfo(String key, DatabaseSettings db, DatabaseInfo? info, Border border, Image image, Label version)
  145. {
  146. if (info == null)
  147. return;
  148. Dispatcher.Invoke(() =>
  149. {
  150. if(info?.ColorScheme is not null)
  151. {
  152. try
  153. {
  154. var color = (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(info.ColorScheme);
  155. border.BorderBrush = new SolidColorBrush(color);
  156. border.Background = new SolidColorBrush(color.AdjustLightness(90));
  157. }
  158. catch
  159. {
  160. }
  161. }
  162. Bitmap? bitmap = null; //PRSDesktop.Resources.splash_small;
  163. try
  164. {
  165. if (info?.Logo != null && info.Logo.Any())
  166. using (var ms = new MemoryStream(info.Logo))
  167. bitmap = new Bitmap(ms);
  168. }
  169. catch
  170. {
  171. }
  172. if (bitmap != null)
  173. {
  174. var size = new Size(bitmap.Width, bitmap.Height).Adjust(150, 150);
  175. image.Source = bitmap.AsBitmapImage(size.Height, size.Width);
  176. }
  177. try
  178. {
  179. version.Content = String.Equals(info?.Version,"???") ? "" : info?.Version;
  180. }
  181. catch
  182. {
  183. }
  184. if (!string.Equals(db.ColorScheme, info?.ColorScheme) || !LogoEqual(db.Logo, info?.Logo))
  185. {
  186. db.ColorScheme = info?.ColorScheme ?? DatabaseSettings.DefaultColorScheme;
  187. db.Logo = info?.Logo;
  188. new LocalConfiguration<DatabaseSettings>(key).Save(db);
  189. }
  190. });
  191. }
  192. public string Database { get; private set; }
  193. private void Button_Click(object sender, RoutedEventArgs e)
  194. {
  195. Database = (sender as Button).Tag as string;
  196. DialogResult = true;
  197. }
  198. //private void OK_Click(object sender, RoutedEventArgs e)
  199. //{
  200. // DialogResult = true;
  201. //}
  202. //private void Cancel_Click(object sender, RoutedEventArgs e)
  203. //{
  204. // DialogResult = false;
  205. //}
  206. }
  207. }