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