123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Net.Sockets;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading;
- using System.Windows;
- using System.Windows.Data;
- using System.Windows.Media;
- using Comal.Classes;
- using InABox.Configuration;
- using InABox.Core;
- using InABox.Logging;
- using InABox.WPF;
- using InABox.WPF.Themes;
- using NDesk.Options;
- using PRSDesktop;
- 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 EmployeeEmail = "";
- public static string Profile = "";
- public static bool IsClosing = 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.v20_2));
- }
- private void AutoDiscover(Dictionary<string, DatabaseSettings> allsettings)
- {
- var confirm = MessageBox.Show("Try to configure Server Connection Automatically?", "Auto Discover", MessageBoxButton.YesNoCancel);
- if (confirm == MessageBoxResult.Yes)
- 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.URL = autodiscover.URL;
- settings.Port = autodiscover.Port;
- 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);
- MessageBox.Show($"Server found at {autodiscover.URL}:{autodiscover.Port}");
- }
- }
- catch
- {
- MessageBox.Show("No Server Found");
- }
- }
- 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);
- /*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);
- MessageBox.Show(sw.ToString(), "PRS Desktop Startup Options");
- }
- }
- var allsettings = new LocalConfiguration<DatabaseSettings>().LoadAll();
- if (!allsettings.Any())
- AutoDiscover(allsettings);
- if (!allsettings.Any())
- {
- var settings = new DatabaseSettings();
- new LocalConfiguration<DatabaseSettings>("Default").Save(settings);
- allsettings["Default"] = settings;
- }
- if (allsettings.ContainsKey(""))
- {
- var defaultSettings = allsettings[""];
- allsettings.Remove("");
- new LocalConfiguration<DatabaseSettings>().Delete();
- defaultSettings.IsActive = true;
- allsettings["Default"] = defaultSettings;
- new LocalConfiguration<DatabaseSettings>("Default").Save(defaultSettings);
- }
- DatabaseSettings = null;
- if (!string.IsNullOrWhiteSpace(Profile))
- {
- if (allsettings.ContainsKey(Profile))
- DatabaseSettings = allsettings[Profile];
- else
- MessageBox.Show(string.Format("Profile {0} does not exist!", Profile));
- }
- 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.Values.First();
- }
- }
- StartupUri = new Uri("MainWindow.xaml", UriKind.Relative);
- }
- protected override void OnExit(ExitEventArgs e)
- {
- base.OnExit(e);
- //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 void LogUnhandledException(Exception exception, string source)
- {
- var messages = new List<string>();
- var e2 = exception;
- while (e2 != null)
- {
- messages.InsertRange(0, new[] { e2.Message, e2.StackTrace, "============================================" });
- e2 = e2.InnerException;
- }
- MainLogger.Send(LogType.Error, "", string.Join("\n", messages));
- // 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);
- */
- }
- }
|