| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494 | 
							- using System;
 
- using System.Collections.Generic;
 
- using System.Diagnostics;
 
- using System.IO;
 
- using System.Linq;
 
- using System.Net;
 
- using System.Net.Sockets;
 
- using System.Text;
 
- using System.Threading;
 
- using System.Windows;
 
- using Comal.Classes;
 
- using InABox.Configuration;
 
- using InABox.Core;
 
- using InABox.Logging;
 
- using InABox.Wpf;
 
- using InABox.WPF;
 
- using Microsoft.Win32;
 
- using NDesk.Options;
 
- using Syncfusion.Licensing;
 
- using Path = System.IO.Path;
 
- namespace PRSDesktop
 
- {
 
-     
 
-     /// <summary>
 
-     ///     Interaction logic for App.xaml
 
-     /// </summary>
 
-     public partial class App : Application
 
-     {
 
-         public static DatabaseSettings DatabaseSettings;
 
-         public static AutoUpdateSettings AutoUpdateSettings;
 
-         public static Guid EmployeeID = Guid.Empty;
 
-         public static String EmployeeCode = "";
 
-         public static String EmployeeName = "";
 
-         public static String EmployeeEmail = "";
 
-         public static string Profile { get; set; } = "";
 
-         public static bool IsClosing { get; set; } = false;
 
-         public static bool ShouldRestart { get; set; } = false;
 
-         /*/ Guid to ensure that only one instance of PRS is running
 
-         public static Guid AppGuid = Guid.Parse("237E8828-7F5A-4298-B311-CF0FC27882EC");
 
-         private static Mutex AppMutex;*/
 
-         private readonly OptionSet _commandLineParameters = new()
 
-         {
 
-             {
 
-                 "profile=",
 
-                 "",
 
-                 p => { Profile = p; }
 
-             }
 
-         };
 
-         public App()
 
-         {
 
-             SyncfusionLicenseProvider.RegisterLicense(CoreUtils.SyncfusionLicense(SyncfusionVersion.v29_x));
 
-             //CoreUtils.GoogleAPIKey = "AIzaSyAVReknl_sP2VLz5SUD8R-sZh1KDVCcWis";
 
-         }
 
-         private void AutoDiscover(Dictionary<string, DatabaseSettings> allsettings)
 
-         {
 
-             var mw = new MessageWindow();
 
-             mw.Title = "Configure Database";
 
-             mw.Message =
 
-                 "No PRS Databases found!.\n\n" +
 
-                 "The following options are available:\n" +
 
-                 "- [Scan] Search the local network for PRS databases\n" +
 
-                 "- [Import] bring in a pre-configured connection profile, or\n" +
 
-                 "- [Demo Mode] connect to the Demonstration Database\n\n"+
 
-                 "What would you like to do?";  
 
-             mw.Buttons.Add(new MessageWindowButton("Scan",(win,args) => ScanForDatabases(args, allsettings), MessageWindowButtonPosition.Left));
 
-             mw.Buttons.Add(new MessageWindowButton("Import",(win,args) => ImportDatabase(args, allsettings), MessageWindowButtonPosition.Left));
 
-             mw.Buttons.Add(new MessageWindowButton("Demo Mode",(win,args) => args.Close = true, MessageWindowButtonPosition.Right));
 
-             mw.ShowDialog();
 
-         }
 
-         
 
-         private void ImportDatabase(MessageWindowButtonDelegateArgs args, Dictionary<string, DatabaseSettings> allsettings)
 
-         {
 
-             var ofd = new OpenFileDialog();
 
-             ofd.Filter = "PRS Connection Files (*.prsconnection)|*.prsconnection";
 
-             if (ofd.ShowDialog() == true)
 
-             {
 
-                 var profilename = Path.GetFileNameWithoutExtension(ofd.FileName);
 
-                 var text = File.ReadAllText(ofd.FileName);
 
-                 var settings = Serialization.Deserialize<DatabaseSettings>(text);
 
-                 if (settings != null)
 
-                 {
 
-                     allsettings[profilename] = settings;
 
-                     new LocalConfiguration<DatabaseSettings>().SaveAll(allsettings);
 
-                     args.Close = true;
 
-                 }
 
-                 else
 
-                 {
 
-                     MessageBox.Show("Invalid Connection Profile!");
 
-                 }
 
-             }
 
-         }
 
-         private void ScanForDatabases(MessageWindowButtonDelegateArgs args, Dictionary<string, DatabaseSettings> allsettings)
 
-         {
 
-             try
 
-             {
 
-                 using (new WaitCursor())
 
-                 {
 
-                     AutoDiscoverySettings autodiscover;
 
-                     using (var client = new UdpClient())
 
-                     {
 
-                         client.Client.SendTimeout = 10000;
 
-                         client.Client.ReceiveTimeout = 20000;
 
-                         var requestData = Encoding.ASCII.GetBytes("");
 
-                         var serverEndPoint = new IPEndPoint(IPAddress.Any, 0);
 
-                         client.EnableBroadcast = true;
 
-                         client.Send(requestData, requestData.Length, new IPEndPoint(IPAddress.Broadcast, 8888));
 
-                         var serverResponseData = client.Receive(ref serverEndPoint);
 
-                         var serverResponse = Encoding.ASCII.GetString(serverResponseData);
 
-                         autodiscover = Serialization.Deserialize<AutoDiscoverySettings>(serverResponse);
 
-                         client.Close();
 
-                     }
 
-                     var settings = new DatabaseSettings();
 
-                     settings.IsActive = true;
 
-                     settings.Logo = autodiscover.Logo;
 
-                     settings.Protocol = autodiscover.Protocol;
 
-                     settings.DatabaseType = DatabaseType.Networked;
 
-                     settings.URLs = autodiscover.URLs;
 
-                     settings.LibraryLocation = autodiscover.LibraryLocation;
 
-                     settings.GoogleAPIKey = autodiscover.GoogleAPIKey;
 
-                     allsettings[autodiscover.Name] = settings;
 
-                     new LocalConfiguration<DatabaseSettings>(autodiscover.Name).Save(settings);
 
-                     AutoUpdateSettings = new AutoUpdateSettings
 
-                     {
 
-                         Channel = autodiscover.UpdateChannel,
 
-                         Type = autodiscover.UpdateType,
 
-                         Location = autodiscover.UpdateLocation,
 
-                         Elevated = autodiscover.UpdateAdmin
 
-                     };
 
-                     new LocalConfiguration<AutoUpdateSettings>().Save(AutoUpdateSettings);
 
-                     MessageWindow.ShowMessage($"Server found at {String.Join(";",autodiscover.URLs)}", "Success");
 
-                     args.Close = true;
 
-                 }
 
-             }
 
-             catch
 
-             {
 
-                 MessageWindow.ShowMessage("No Server Found", "Not found", image: MessageWindow.WarningImage);
 
-             }
 
-         }
 
-         private void MoveDirectory(string[] source, string target)
 
-         {
 
-             var stack = new Stack<Folders>();
 
-             stack.Push(new Folders(source[0], target));
 
-             while (stack.Count > 0)
 
-             {
 
-                 var folders = stack.Pop();
 
-                 Directory.CreateDirectory(folders.Target);
 
-                 foreach (var file in Directory.GetFiles(folders.Source, "*.*"))
 
-                 {
 
-                     var targetFile = Path.Combine(folders.Target, Path.GetFileName(file));
 
-                     if (!File.Exists(targetFile))
 
-                         File.Move(file, targetFile);
 
-                     else
 
-                         File.Delete(file);
 
-                 }
 
-                 foreach (var folder in Directory.GetDirectories(folders.Source))
 
-                     stack.Push(new Folders(folder, Path.Combine(folders.Target, Path.GetFileName(folder))));
 
-             }
 
-             Directory.Delete(source[0], true);
 
-         }
 
-         public static void SaveDatabaseSettings()
 
-         {
 
-             new LocalConfiguration<DatabaseSettings>(Profile).Save(DatabaseSettings);
 
-         }
 
-         protected override void OnStartup(StartupEventArgs e)
 
-         {
 
-             base.OnStartup(e);
 
-             Inflector.Inflector.SetDefaultCultureFunc = () => Thread.CurrentThread.CurrentUICulture;
 
-             
 
-             /*AppMutex = new Mutex(false, $"Global\\{AppGuid}");
 
-             // Don't open if PRS is already running.
 
-             if(!AppMutex.WaitOne(0, false))
 
-             {
 
-                 Shutdown(0);
 
-                 SendToProcesses(Message.Maximise);
 
-                 return;
 
-             }*/
 
-             AutoUpdateSettings = new LocalConfiguration<AutoUpdateSettings>().Load();
 
-             var oldPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Comal.Desktop");
 
-             if (Directory.Exists(oldPath))
 
-                 MoveDirectory(new[] { oldPath }, CoreUtils.GetPath());
 
-             ShutdownMode = ShutdownMode.OnExplicitShutdown;
 
-             SetupExceptionHandling();
 
-             MainLogger.AddLogger(new LogFileLogger(CoreUtils.GetPath()));
 
-             Logger.OnLog += MainLogger.Send;
 
-             if (e.Args.Any())
 
-             {
 
-                 DatabaseSettings = new DatabaseSettings();
 
-                 var extras = _commandLineParameters.Parse(e.Args);
 
-                 if (extras.Any())
 
-                 {
 
-                     var sw = new StringWriter();
 
-                     sw.WriteLine("Unknown Parameter(s) found:");
 
-                     foreach (var extra in extras)
 
-                         sw.WriteLine("    " + extra);
 
-                     sw.WriteLine();
 
-                     sw.WriteLine("The following parameters are valid:");
 
-                     _commandLineParameters.WriteOptionDescriptions(sw);
 
-                     MessageWindow.ShowMessage(sw.ToString(), "PRS Desktop Startup Options");
 
-                 }
 
-             }
 
-             
 
-             bool bSaveSettings = false;
 
-             var allsettings = new LocalConfiguration<DatabaseSettings>().LoadAll();
 
-             
 
-             // Try AutoDiscovery
 
-             if (!allsettings.Any())
 
-                 AutoDiscover(allsettings);
 
-             
 
-             // Create Default Database Entry
 
-             if (!allsettings.Any())
 
-             {
 
-                 var settings = new DatabaseSettings();
 
-                 settings.UserID = "GUEST";
 
-                 settings.Password = "guest";
 
-                 settings.Autologin = false;
 
-                 allsettings["Default"] = settings;
 
-                 bSaveSettings = true;
 
-             }
 
-             // Convert Blank Key to "Default"
 
-             if (allsettings.ContainsKey(""))
 
-             {
 
-                 var defaultSettings = allsettings[""];
 
-                 allsettings.Remove("");
 
-                 defaultSettings.IsActive = true;
 
-                 allsettings["Default"] = defaultSettings;
 
-                 bSaveSettings = true;
 
-             }
 
-             
 
-             // Check and Convert from URL + Port => URLs
 
-             foreach (var key in allsettings.Keys)
 
-             {
 
-                 var settings = allsettings[key];
 
-                 var oldurl = CoreUtils.GetPropertyValue(settings, "URL") as String;
 
-                 var oldport = CoreUtils.GetPropertyValue(settings, "Port");
 
-                 if (!String.IsNullOrWhiteSpace(oldurl))
 
-                 {
 
-                     bSaveSettings = true;
 
-                     settings.URLs = new String[] { $"{oldurl}:{oldport}" };
 
-                     CoreUtils.SetPropertyValue(settings, "URL", "");
 
-                     CoreUtils.SetPropertyValue(settings, "Port", 0);
 
-                 }
 
-                 if (settings.URLs != null)
 
-                 {
 
-                     var urls = new List<String>();
 
-                     foreach (var url in settings.URLs)
 
-                     {
 
-                         var comps = url.Split(new[] { "://" }, StringSplitOptions.RemoveEmptyEntries);
 
-                         if (comps.Length > 1)
 
-                         {
 
-                             bSaveSettings = true;
 
-                             urls.Add(comps.Last());
 
-                         }
 
-                         else
 
-                             urls.Add(url);
 
-                     }
 
-                     settings.URLs = urls.ToArray();
 
-                 }
 
-             }
 
-             
 
-             if (bSaveSettings)
 
-                 new LocalConfiguration<DatabaseSettings>().SaveAll(allsettings);
 
-             DatabaseSettings = null;
 
-             if (!string.IsNullOrWhiteSpace(Profile))
 
-             {
 
-                 if (allsettings.ContainsKey(Profile))
 
-                     DatabaseSettings = allsettings[Profile];
 
-                 else
 
-                     MessageWindow.ShowMessage($"Profile {Profile} does not exist!", "Error");
 
-             }
 
-             if (DatabaseSettings == null)
 
-             {
 
-                 var keys = allsettings.Keys.Where(x => allsettings[x].IsActive).ToArray();
 
-                 if (keys.Length > 1)
 
-                 {
 
-                     var form = new SelectDatabase(allsettings);
 
-                     if (form.ShowDialog() == true)
 
-                     {
 
-                         Profile = form.Database;
 
-                         DatabaseSettings = allsettings[form.Database];
 
-                     }
 
-                     else
 
-                     {
 
-                         Shutdown(1);
 
-                         return;
 
-                     }
 
-                     form = null;
 
-                 }
 
-                 else
 
-                 {
 
-                     Profile = keys.First();
 
-                     DatabaseSettings = allsettings[Profile];
 
-                 }
 
-             }
 
-             CoreUtils.GoogleAPIKey = DatabaseSettings.GoogleAPIKey;
 
-             StartupUri = new Uri("MainWindow.xaml", UriKind.Relative);
 
-         }
 
-         
 
-         private static IEnumerable<string> GetRestartArguments()
 
-         {
 
-             // Skip the first one, because that is the location.
 
-             return Environment.GetCommandLineArgs().Skip(1);
 
-         }
 
-         protected override void OnExit(ExitEventArgs e)
 
-         {
 
-             base.OnExit(e);
 
-             if (ShouldRestart)
 
-             {
 
-                 Process.Start(Path.ChangeExtension(ResourceAssembly.Location, ".exe"), GetRestartArguments());
 
-             }
 
-             //AppMutex.ReleaseMutex();
 
-         }
 
-         private void SetupExceptionHandling()
 
-         {
 
-             AppDomain.CurrentDomain.UnhandledException += (s, e) =>
 
-                 LogUnhandledException((Exception)e.ExceptionObject, "AppDomain.CurrentDomain.UnhandledException");
 
-             DispatcherUnhandledException += (s, e) =>
 
-             {
 
-                 LogUnhandledException(e.Exception, "Application.Current.DispatcherUnhandledException");
 
-                 e.Handled = true;
 
-             };
 
-         }
 
-         private class UnhandledException
 
-         {
 
-             private string _message;
 
-             public string Message
 
-             {
 
-                 get => _message;
 
-                 set
 
-                 {
 
-                     _message = value;
 
-                     Hash = GenerateExceptionHash(value);
 
-                 }
 
-             }
 
-             public DateTime OriginalTime { get; set; }
 
-             public DateTime LastTime { get; set; }
 
-             public DateTime LastPrinted { get; set; }
 
-             public int Occurences { get; set; }
 
-             public int Hash { get; private set; }
 
-         }
 
-         private static TimeSpan RepeatedExceptionThreshold = TimeSpan.FromSeconds(10);
 
-         private static Queue<UnhandledException> PreviousUnhandledExceptions { get; set; } = new Queue<UnhandledException>();
 
-         private static int GenerateExceptionHash(string message)
 
-         {
 
-             return message.GetHashCode();
 
-         }
 
-         private static void ClearUnhandledExceptions()
 
-         {
 
-             while (PreviousUnhandledExceptions.TryPeek(out var e) && DateTime.Now - e.LastTime > RepeatedExceptionThreshold)
 
-             {
 
-                 PreviousUnhandledExceptions.Dequeue();
 
-             }
 
-         }
 
-         private void LogUnhandledException(Exception exception, string source)
 
-         {
 
-             ClearUnhandledExceptions();
 
-             
 
-             var messages = new List<string>();
 
-             var e2 = exception;
 
-             while (e2 != null)
 
-             {
 
-                 messages.InsertRange(0, new[] { e2.Message, e2.StackTrace, "============================================" });
 
-                 e2 = e2.InnerException;
 
-             }
 
-             var unhandled = new UnhandledException
 
-             {
 
-                 Message = string.Join("\n", messages),
 
-                 OriginalTime = DateTime.Now,
 
-                 Occurences = 1
 
-             };
 
-             unhandled.LastTime = unhandled.OriginalTime;
 
-             unhandled.LastPrinted = unhandled.OriginalTime;
 
-             UnhandledException? found = null;
 
-             foreach(var e in PreviousUnhandledExceptions)
 
-             {
 
-                 if(e.Hash == unhandled.Hash)
 
-                 {
 
-                     e.LastTime = unhandled.LastTime;
 
-                     e.Occurences++;
 
-                     found = e;
 
-                     break;
 
-                 }
 
-             }
 
-             if (found is null)
 
-             {
 
-                 PreviousUnhandledExceptions.Enqueue(unhandled);
 
-                 MainLogger.Send(LogType.Error, "", string.Join("\n", messages), Guid.Empty);
 
-             }
 
-             else
 
-             {
 
-                 if((DateTime.Now - found.LastPrinted).TotalSeconds >= 1)
 
-                 {
 
-                     MainLogger.Send(LogType.Error, "", $"Recurrent Error occurred {found.Occurences} times; See {found.OriginalTime}", Guid.Empty);
 
-                     found.LastPrinted = DateTime.Now;
 
-                 }
 
-             }
 
-             // if (exception.Message.StartsWith("Dispatcher processing has been suspended"))
 
-             //     try
 
-             //     {
 
-             //         SendKeys.Send("{ESC}");
 
-             //     }
 
-             //     catch (Exception e)
 
-             //     {
 
-             //     }
 
-         }
 
-         private class Folders
 
-         {
 
-             public Folders(string source, string target)
 
-             {
 
-                 Source = source;
 
-                 Target = target;
 
-             }
 
-             public string Source { get; }
 
-             public string Target { get; }
 
-         }
 
-         /*
 
-         public enum Message
 
-         {
 
-             Maximise = 0x2A76
 
-         }
 
-         private void SendToProcesses(Message message)
 
-         {
 
-             var process = Process.GetCurrentProcess();
 
-             var processes = Process.GetProcessesByName(process.ProcessName);
 
-             if(processes.Length > 1)
 
-             {
 
-                 foreach(var p in processes)
 
-                 {
 
-                     if(p.Id != process.Id)
 
-                     {
 
-                         SendMessage(p.MainWindowHandle, (uint)message, IntPtr.Zero, IntPtr.Zero);
 
-                     }
 
-                 }
 
-             }
 
-         }
 
-         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
 
-         private static extern IntPtr SendMessage(IntPtr hwnd, uint Msg, IntPtr wParam, IntPtr lParam);
 
-         */
 
-     }
 
- }
 
 
  |