123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- using Comal.Classes;
- using InABox.IPC;
- using InABox.Clients;
- using InABox.Configuration;
- using InABox.Core;
- using InABox.Database;
- using InABox.IPC;
- using InABox.Rpc;
- using InABox.Wpf;
- using InABox.WPF;
- using PRSServer;
- using Image = System.Windows.Controls.Image;
- using Size = System.Drawing.Size;
- namespace PRSDesktop;
- /// <summary>
- /// Interaction logic for SelectDatabase.xaml
- /// </summary>
- public partial class SelectDatabase : ThemableWindow
- {
- private CancellationTokenSource cancel = new CancellationTokenSource();
-
- public SelectDatabase(Dictionary<string, DatabaseSettings> databases)
- {
- InitializeComponent();
- var active = databases.Where(x => x.Value.IsActive).ToArray().Length;
- var cols = (int)Math.Ceiling(Math.Sqrt(active));
- for (var iCol = 0; iCol < cols; iCol++)
- Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) });
- var rows = (int)Math.Ceiling(active / (double)cols);
- for (var iRow = 0; iRow < cols; iRow++)
- Grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
- var i = 0;
- foreach (var key in databases.Keys)
- if (databases[key].IsActive)
- {
- var db = databases[key];
-
- var button = new Button
- {
- Background = new SolidColorBrush(Colors.Transparent),
- BorderThickness = new Thickness(0),
- Padding = new Thickness(0)
- };
- var color = Colors.Red;
- // try
- // {
- // color = (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(db.ColorScheme);
- // }
- // catch (Exception e)
- // {
- // color = Colors.Red;
- // }
-
- var border = new Border
- {
- CornerRadius = new CornerRadius(5),
- BorderBrush = new SolidColorBrush(color),
- BorderThickness = new Thickness(0.75),
- Background = db.DatabaseType == DatabaseType.Standalone ?
- new SolidColorBrush(Colors.WhiteSmoke) :
- new SolidColorBrush(Colors.White),
- Width = 200,
- Height = 200
- };
- var panel = new DockPanel { VerticalAlignment = VerticalAlignment.Stretch };
- border.Child = panel;
- StackPanel stack = new StackPanel() { Orientation = Orientation.Horizontal, HorizontalAlignment = HorizontalAlignment.Center };
- stack.SetValue(DockPanel.DockProperty, Dock.Top);
- Label dbtype = new Label()
- {
- Content = db.DatabaseType.ToString(),
- Margin = new Thickness(5,2,0,0),
- HorizontalContentAlignment = HorizontalAlignment.Left,
- FontSize = 8,
- Foreground = new SolidColorBrush(Colors.DimGray)
- };
- stack.Children.Add(dbtype);
- Label dbver = new Label()
- {
- Margin = new Thickness(0,2,5,0),
- HorizontalContentAlignment = HorizontalAlignment.Right,
- FontSize = 8,
- Foreground = new SolidColorBrush(Colors.DimGray)
- };
- stack.Children.Add(dbver);
- panel.Children.Add(stack);
-
- var label = new Label
- {
- Content = key,
- HorizontalContentAlignment = HorizontalAlignment.Center,
- VerticalContentAlignment = VerticalAlignment.Center,
- Width = 150,
- Height = 35
- };
- label.SetValue(DockPanel.DockProperty, Dock.Bottom);
- panel.Children.Add(label);
-
- var image = new Image();
- image.Height = 150;
- image.Width = 150;
- image.SetValue(DockPanel.DockProperty, Dock.Top);
- panel.Children.Add(image);
- button.Content = border;
- button.Margin = new Thickness(5, 5, 0, 0);
- button.Click += Button_Click;
- button.Height = 200;
- button.Width = 200;
- button.Tag = key;
- var col = i % cols;
- var row = i / cols;
- button.SetValue(Grid.ColumnProperty, col);
- button.SetValue(Grid.RowProperty, row);
- Grid.Children.Add(button);
- Task.Run(
- () =>
- {
- while (!cancel.IsCancellationRequested)
- {
- DatabaseInfo? info = null;
- if(db.DatabaseType == DatabaseType.Standalone)
- {
- info = new DatabaseInfo(db.ColorScheme, db.Logo, CoreUtils.GetVersion(), true, DbFactory.RestPort, DbFactory.RPCPort, DbFactory.ID);
- }
- if (db.DatabaseType == DatabaseType.Local)
- {
- info = new IPCClient<User>(DatabaseServerProperties.GetPipeName(db.LocalServerName, false)).Info();
- //info = new RpcClientPipeTransport(DatabaseServerProperties.GetPipeName(db.LocalServerName, true)).Info();
- }
- else if (db.DatabaseType == DatabaseType.Networked)
- {
- if (db.Protocol == SerializerProtocol.RPC)
- info = new RpcClientSocketTransport(db.URLs).Info();
- else
- {
- RestClient<User>.Ping(db.URLs, out DatabaseInfo i);
- info = i;
- }
- }
- UpdateInfo(key, db, info, border, image, dbver);
- if(info is not null)
- {
- break;
- }
- Task.Delay(1000).Wait();
- }
- }, cancel.Token);
- i++;
- }
- }
- private bool LogoEqual(byte[]? first, byte[]? second)
- {
- if ((first == null) && (second == null))
- return true;
- if ((first == null) || (second == null))
- return false;
- return first.SequenceEqual(second);
- }
- private void UpdateInfo(String key, DatabaseSettings db, DatabaseInfo? info, Border border, Image image, Label version)
- {
-
- Dispatcher.Invoke(() =>
- {
- if (info == null)
- {
- border.BorderBrush = new SolidColorBrush(Colors.Red);
- border.Background = db.DatabaseType == DatabaseType.Standalone
- ? new SolidColorBrush(Colors.WhiteSmoke)
- : new SolidColorBrush(Colors.White);
- image.Source = null;
- version.Content = "";
- return;
- }
- if (info?.ColorScheme is not null)
- {
- try
- {
- var color = (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(info.ColorScheme);
- border.BorderBrush = new SolidColorBrush(color);
- border.Background = new SolidColorBrush(color.AdjustLightness(90));
- }
- catch
- {
- }
- }
- Bitmap? bitmap = null; //PRSDesktop.Resources.splash_small;
- try
- {
- if (info?.Logo != null && info.Logo.Any())
- using (var ms = new MemoryStream(info.Logo))
- bitmap = new Bitmap(ms);
- }
- catch
- {
- }
- if (bitmap != null)
- {
- var size = new Size(bitmap.Width, bitmap.Height).Adjust(150, 150);
- image.Source = bitmap.AsBitmapImage(size.Height, size.Width);
- }
- try
- {
- version.Content = String.Equals(info?.Version,"???") ? "" : info?.Version;
- }
- catch
- {
- }
- if (!string.Equals(db.ColorScheme, info?.ColorScheme) || !LogoEqual(db.Logo, info?.Logo))
- {
- db.ColorScheme = info?.ColorScheme ?? DatabaseSettings.DefaultColorScheme;
- db.Logo = info?.Logo;
- new LocalConfiguration<DatabaseSettings>(key).Save(db);
- }
- });
- }
- public string Database { get; private set; }
- private void Button_Click(object sender, RoutedEventArgs e)
- {
- cancel.Cancel();
- Database = (sender as Button).Tag as string;
- DialogResult = true;
- }
- }
|