| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116 | using System;using System.Collections.Generic;using System.Collections.ObjectModel;using System.ComponentModel;using System.Diagnostics;using System.Drawing;using System.Drawing.Drawing2D;using System.Drawing.Imaging;using System.IO;using System.Linq;using System.Net.Http;using System.Reflection;using System.Runtime.InteropServices;using System.Threading.Tasks;using System.Windows;using System.Windows.Controls;using System.Windows.Forms;using System.Windows.Input;using System.Windows.Interop;using System.Windows.Media;using System.Windows.Threading;using AvalonDock.Layout;using Comal.Classes;using Comal.Stores;using Comal.TaskScheduler.Shared;using H.Pipes;using InABox.Clients;using InABox.Configuration;using InABox.Core;using InABox.Database;using InABox.Database.SQLite;using InABox.DynamicGrid;using InABox.Mail;using InABox.Rpc;using InABox.Scripting;using InABox.Wpf;using InABox.WPF;using NAudio.Wave;using PRS.Shared;using InABox.WPF.Themes;using PRSDesktop.Configuration;using PRSDesktop.Forms;using PRSServer;using SharpAvi.Codecs;using SharpAvi.Output;using Syncfusion.Windows.Shared;using Syncfusion.Windows.Tools.Controls;using Application = System.Windows.Application;using ButtonBase = System.Windows.Controls.Primitives.ButtonBase;using Color = System.Windows.Media.Color;using ColorConverter = System.Windows.Media.ColorConverter;using Control = System.Windows.Controls.Control;using Image = System.Drawing.Image;using KeyEventArgs = System.Windows.Input.KeyEventArgs;using MessageBox = System.Windows.MessageBox;using Pen = System.Drawing.Pen;using PixelFormat = System.Drawing.Imaging.PixelFormat;using SortDirection = InABox.Core.SortDirection;using InABox.Wpf.Reports;using Comal.Classes.SecurityDescriptors;using System.Threading;using InABox.Formatters;using PRSDesktop.Forms.Issues;using Brushes = System.Windows.Media.Brushes;using System.Windows.Media.Imaging;namespace PRSDesktop;public enum PanelType{    InPlace,    NewWindow}public class SimpleCommand : ICommand{        public Action OnExecute { get; private set; }        public bool CanExecute(object? parameter) => true;     public event EventHandler? CanExecuteChanged    {        add => CommandManager.RequerySuggested += value;        remove => CommandManager.RequerySuggested -= value;    }    public void Execute(object? parameter)    {        OnExecute?.Invoke();    }    public SimpleCommand(Action onExecute)    {        OnExecute = onExecute;    }    }/// <summary>///     Interaction logic for Main.xaml/// </summary>public partial class MainWindow : IPanelHostControl{    //private const int WM_LBUTTONDOWN = 0x201;    private static PipeServer<string>? _client;    private IRpcClientTransport? _transport;    private WaveIn? _audio;    private bool _audioMuted;    private MemoryStream? _audioStream;    private readonly Dictionary<DateTime, Stream> _bitmaps = new();    private DesktopConsole? _console;    private Dictionary<DateTime, Tuple<Rectangle, string>> _notes = new();    private DispatcherTimer? _recorder;    private Process? _recordingnotes;    private int _screenheight = 720;    private int _screenleft;    private int _screentop;    private int _screenwidth = 1280;    private readonly Dictionary<Guid, SecondaryWindow> SecondaryWindows = new();    private CoreTable? _timesheets;    private DailyActivityHistory? ActivityHistory;    private readonly List<Control> CurrentModules = new();    private Fluent.RibbonTabItem? CurrentTab;    private Fluent.Button? CurrentButton;    private readonly int FRAMES_PER_SECOND = 10;    private DatabaseType DatabaseType;    private readonly Dictionary<int, int> messages = new();    private readonly DispatcherTimer NotificationsWatchDog;    private DateTime pausestarted = DateTime.MinValue;    private readonly Scheduler scheduler = new() { Interval = new TimeSpan(0, 5, 0) };    // We use a Guid for the StationID rather than an IP or Mac address    // because we want true single-instance restriction.  Using either of    // the above allows for two instances on the once machine, and thus    // double-counting in the Heartbeat() function    private Login station = new() { StationID = Guid.NewGuid().ToString() };    private TimeSpan totalpauses = new(0);    private readonly int VIDEO_HEIGHT = 1080;    private readonly int VIDEO_WIDTH = 1920;    private PanelHost PanelHost;    public MainWindow()    {        PanelHost = new PanelHost(this);        NotificationsWatchDog = new DispatcherTimer { IsEnabled = false };        NotificationsWatchDog.Tick += Notifications_Tick;        NotificationsWatchDog.Interval = new TimeSpan(0, 2, 0);        ClientFactory.PushHandlers.AddHandler<Notification>(ReceiveNotification);        ClientFactory.RegisterMailer(EmailType.IMAP, typeof(IMAPMailer));        ClientFactory.RegisterMailer(EmailType.Exchange, typeof(ExchangeMailer));        ClientFactory.OnLog += (type, userid, message, parameters) => Logger.Send(LogType.Information, ClientFactory.UserID, message, parameters);        ClientFactory.OnRequestError += ClientFactory_OnRequestError;        HotKeyManager.Initialize();        HotKeyManager.RegisterHotKey(Key.F1, ShowHelp);        //HotKeyManager.RegisterHotKey(Key.F5, ToggleRecording);        //HotKeyManager.RegisterHotKey(Key.F6, ShowRecordingNotes);        //HotKeyManager.RegisterHotKey(Key.F4, ToggleRecordingAudio);        Logger.Send(LogType.Information, "", "Connecting to server");        var settings = App.DatabaseSettings;        bool dbConnected;        DatabaseType = settings.DatabaseType;        switch (DatabaseType)        {            case DatabaseType.Standalone:                ClientFactory.SetClientType(typeof(LocalClient<>), Platform.Wpf, CoreUtils.GetVersion());                DbFactory.ColorScheme = App.DatabaseSettings.ColorScheme;                DbFactory.Logo = App.DatabaseSettings.Logo;                dbConnected = true;                break;            case DatabaseType.Networked:                if (App.DatabaseSettings.Protocol == SerializerProtocol.RPC)                {                    _transport = new RpcClientSocketTransport(App.DatabaseSettings.URLs);                    _transport.OnClose += TransportConnectionLost;                    _transport.OnException += Transport_OnException;                    _transport.OnOpen += Transport_OnOpen; ;                    dbConnected = _transport.Connect();                    ClientFactory.SetClientType(typeof(RpcClient<>), Platform.Wpf, CoreUtils.GetVersion(),                        _transport);                }                else                {                    var url = RestClient<User>.Ping(App.DatabaseSettings.URLs, out DatabaseInfo info);                    ClientFactory.SetClientType(typeof(RestClient<>), Platform.Wpf, CoreUtils.GetVersion(), url, true);                    dbConnected = true;                }                break;            case DatabaseType.Local:                //new RPC stuff (temporary disabled - for enabling when RPC is ready)                var pipename = DatabaseServerProperties.GetPipeName(App.DatabaseSettings.LocalServerName, true);                _transport = new RpcClientPipeTransport(pipename);                _transport.OnClose += TransportConnectionLost;                dbConnected = _transport.Connect();                ClientFactory.SetClientType(typeof(RpcClient<>), Platform.Wpf, CoreUtils.GetVersion(), _transport );                                //ClientFactory.SetClientType(typeof(IPCClient<>), Platform.Wpf, CoreUtils.GetVersion(),                //    DatabaseServerProperties.GetPipeName(App.DatabaseSettings.LocalServerName, false));                //dbConnected = true;                                break;            default:                throw new Exception($"Invalid database type {DatabaseType}");        }        InitializeComponent();        if (!dbConnected)        {            switch (DoConnectionFailed())            {                case ConnectionFailedResult.Quit:                    Close();                    return;                case ConnectionFailedResult.Restart:                    App.ShouldRestart = true;                    Close();                    return;                case ConnectionFailedResult.Ok:                    // Do nothing                    break;            }        }        ThemeManager.BaseColor = Colors.CornflowerBlue;        Progress.DisplayImage = PRSDesktop.Resources.splash_small.AsBitmapImage();        try        {            var dbInfo = new Client<User>().Info();            ClientFactory.DatabaseID = dbInfo.DatabaseID;            ThemeManager.BaseColor = (Color)ColorConverter.ConvertFromString(dbInfo.ColorScheme);            if (dbInfo.Logo?.Any() == true)                using (var ms = new MemoryStream(dbInfo.Logo))                {                    Progress.DisplayImage = new Bitmap(ms).AsBitmapImage();                }        }        catch        {        }        //VideoRecordingStatus.Source = PRSDesktop.Resources.videorecording.AsGrayScale().AsBitmapImage();        //AudioRecordingStatus.Source = PRSDesktop.Resources.audiorecording.AsGrayScale().AsBitmapImage();        //SecondaryWindowStatus.Source = PRSDesktop.Resources.target.AsGrayScale().AsBitmapImage();                ConsoleStatus.Source = PRSDesktop.Resources.view.AsGrayScale().AsBitmapImage();        SelectTask.Source = PRSDesktop.Resources.uparrow.Invert().AsBitmapImage();        Title = $"{(String.Equals(App.Profile?.ToUpper(), "DEFAULT") ? "PRS Desktop" : App.Profile)} (Release {CoreUtils.GetVersion()})";                Logger.Send(LogType.Information, "", "Checking for updates");        if (SupportUtils.CheckForUpdates())        {            Logger.Send(LogType.Information, "", "Update found, closing application.");            Close();            return;        }        Exception? startupException = null;        ValidationStatus? loginStatus = null;        Progress.ShowModal("Loading PRS", progress =>        {            DynamicGridUtils.PreviewReport = (t, m) => { ReportUtils.PreviewReport(t, m, false, Security.IsAllowed<CanDesignReports>()); };            DynamicGridUtils.PrintMenu = (e, s, m, p) => { ReportUtils.PrintMenu(e, s, m, Security.IsAllowed<CanDesignReports>(), p); };            ImportFactory.Register(typeof(ExcelImporter<>), "Excel File", "Excel Files (*.xls;*.xlsx;*.xlsm)|*.xls;*.xlsx;*.xlsm");            ImportFactory.Register(typeof(CustomImporter<>), "Custom", "All Files (*.*)|*.*");            FormUtils.Register();                        DigitalFormDocumentFactory.Init(                new WpfDigitalFormDocumentHandler(                    b => Dispatcher.BeginInvoke(() =>                        {                            BackgroundUploadStatus.Visibility = b                                ? Visibility.Visible                                : Visibility.Hidden;                        }                    ),                    () => _transport?.IsConnected() ?? false                )            );            DigitalFormDocumentFactory.Run();                        Logger.Send(LogType.Information, "", "Registering Classes");            progress.Report("Registering Classes");            var tasks = new List<Task>            {                Task.Run(() =>                {                    CoreUtils.RegisterClasses(typeof(TaskGrid).Assembly);                    CoreUtils.RegisterClasses();                    ComalUtils.RegisterClasses();                    StoreUtils.RegisterClasses();                    PRSSharedUtils.RegisterClasses();                    WPFUtils.RegisterClasses();                    ReportUtils.RegisterClasses();                    ConfigurationUtils.RegisterClasses();                }),                Task.Run(() =>                {                    ScriptDocument.DefaultAssemblies.AddRange(                        Assembly.Load("RoslynPad.Roslyn.Windows"),                        Assembly.Load("RoslynPad.Editor.Windows"),                        typeof(Control).Assembly,                        typeof(MessageBox).Assembly,                        typeof(SolidColorBrush).Assembly                    );                    ScriptDocument.Initialize();                }),                Task.Run(() => DatabaseUpdateScripts.RegisterScripts())            };            Task.WaitAll(tasks.ToArray());            Logger.Send(LogType.Information, "", "Configuring Application");            progress.Report("Configuring Application");            RegisterModules(progress);            if (DatabaseType == DatabaseType.Standalone)            {                progress.Report("Starting local database...");                try                {                    StartLocalDatabase(progress);                }                catch (Exception err)                {                    startupException = new Exception(                        string.Format(                            "Unable to open database ({0})\n\n{1}\n\n{2}",                            App.DatabaseSettings.FileName,                            err.Message,                            err.StackTrace                        )                    );                }            }        });        if (startupException is null && App.DatabaseSettings.Autologin)        {            try            {                Logger.Send(LogType.Information, "", "Logging in");                Dispatcher.Invoke(() =>                {                    loginStatus = TryAutoLogin();                });                if(loginStatus == ValidationStatus.VALID)                {                    // Do the AfterLogin() here so that we aren't opening and closing progress windows again and again.                    Progress.ShowModal("Loading PRS", progress =>                    {                        AfterLogin(progress);                    });                }            }            catch(Exception e)            {                startupException = e;            }        }        if (startupException != null)        {            MessageWindow.ShowError("Error during startup.", startupException);        }                // If the login status is valid, then we've already loaded everything, so we don't here.        if(loginStatus != ValidationStatus.VALID)        {            Logger.Send(LogType.Information, "", "Logging in");            if (DoLogin() == ValidationStatus.VALID)            {                AfterLogin(null);            }            else            {                ConfigureMainScreen(null);            }        }        ProfileName.Content = App.Profile;        URL.Content = GetDatabaseConnectionDescription();        if (loginStatus == ValidationStatus.VALID && DatabaseType == DatabaseType.Standalone)        {            Progress.ShowModal("Starting Scheduler", progress =>            {                scheduler.Start();            });        }    }    #region Connection Management    private string GetDatabaseConnectionDescription()    {        return DatabaseType switch        {#if RPC            DatabaseType.Networked => (ClientFactory.Parameters?.FirstOrDefault() as RpcClientSocketTransport)?.Host,#else            DatabaseType.Networked => ClientFactory.Parameters?.FirstOrDefault() as string,#endif            DatabaseType.Standalone => App.DatabaseSettings?.FileName,            DatabaseType.Local => App.DatabaseSettings?.LocalServerName,            _ => ""        } ?? "";    }    /// <summary>    /// Reconnect to the server.    /// </summary>    /// <returns><see langword="true"/> if connection was successful.</returns>    private bool Reconnect()    {        if (_transport != null)        {            return _transport.Connect();        }        Logger.Send(LogType.Error, ClientFactory.UserID, "Trying to reconnect without a transport set.");        return true; // Returning true so we don't get stuck in infinite loops in exceptional circumstances.    }    private enum ConnectionFailedResult    {        Quit,        Restart,        Ok    }    /// <summary>    /// To be called when initial connection to the server has failed; asks the user if they want to retry,    /// change the database settings, or simply quit PRS.    /// </summary>    /// <returns>The action to take next.</returns>    /// <exception cref="Exception"></exception>    private ConnectionFailedResult DoConnectionFailed()    {        bool connected = false;        while (!connected)        {            var connectionFailedWindow = new ConnectionFailed();            connectionFailedWindow.ShowDialog();            var reconnect = false;            switch (connectionFailedWindow.Result)            {                case ConnectionFailedWindowResult.OpenDatabaseConfiguration:                    var result = ShowDatabaseConfiguration();                    switch (result)                    {                        case DatabaseConfigurationResult.RestartRequired:                            var shouldRestart = MessageBox.Show(                                "A restart is required to apply these changes. Do you wish to restart now?",                                "Restart?",                                MessageBoxButton.YesNo);                            if (shouldRestart == MessageBoxResult.Yes)                            {                                return ConnectionFailedResult.Restart;                            }                            else                            {                                reconnect = true;                            }                            break;                        case DatabaseConfigurationResult.RestartNotRequired:                            reconnect = true;                            break;                        case DatabaseConfigurationResult.Cancel:                            reconnect = true;                            break;                        default:                            throw new Exception($"Invalid enum result {result}");                    }                    break;                case ConnectionFailedWindowResult.RetryConnection:                    reconnect = true;                    break;                case ConnectionFailedWindowResult.Quit:                    return ConnectionFailedResult.Quit;                default:                    throw new Exception($"Invalid enum result {connectionFailedWindow.Result}");            }            if (!reconnect)            {                return ConnectionFailedResult.Quit;            }            connected = Reconnect();        }        return ConnectionFailedResult.Ok;    }    private void Transport_OnOpen(IRpcTransport transport, RpcTransportOpenArgs e)    {        Logger.Send(LogType.Information, ClientFactory.UserID, "Connection opened");    }    private void Transport_OnException(IRpcTransport transport, RpcTransportExceptionArgs e)    {        Logger.Send(LogType.Error, ClientFactory.UserID, $"Error in connection: {CoreUtils.FormatException(e.Exception)}");    }    private void TransportConnectionLost(IRpcTransport transport, RpcTransportCloseArgs e)    {        Logger.Send(LogType.Information, ClientFactory.UserID, "Connection lost");        if (transport is IRpcClientTransport client)        {            Dispatcher.Invoke(() =>            {                var reconnection = new ReconnectionWindow();                var cancellationTokenSource = new CancellationTokenSource();                reconnection.OnCancelled = () => cancellationTokenSource.Cancel();                var ct = cancellationTokenSource.Token;                var work = () =>                {                    try                    {                        DateTime lost = DateTime.Now;                        while (!client.IsConnected() && !ct.IsCancellationRequested)                        {                            try                            {                                Logger.Send(LogType.Error, ClientFactory.UserID, $"Reconnecting - ({DateTime.Now - lost:hh\\:mm})");                                if (client.Connect(ct))                                {                                    break;                                }                            }                            catch (System.Exception e1)                            {                                Logger.Send(LogType.Error, ClientFactory.UserID, $"Reconnect Failed: {e1.Message}");                                // TODO: Remove this suppression                                if (e1.Message.StartsWith("The socket is connected, you needn't connect again!"))                                {                                    break;                                }                            }                        }                        if (client.IsConnected())                        {                            Logger.Send(LogType.Information, ClientFactory.UserID, "Reconnected");                            ClientFactory.Validate(ClientFactory.SessionID);                            Logger.Send(LogType.Information, ClientFactory.UserID, "Validated");                            return true;                        }                    }                    catch (Exception e)                    {                        Logger.Send(LogType.Error, ClientFactory.UserID, $"Reconnect Failed: {e.Message}");                    }                    return false;                };                var task = Task.Run(() =>                {                    var result = work();                    Dispatcher.Invoke(() =>                    {                        reconnection.Close();                    });                    return result;                }, ct);                reconnection.ShowDialog();                if (!task.Result)                {                    Close();                }            });        }    }    #endregion    private bool _loggingOut = false;    private void ClientFactory_OnRequestError(RequestException e)    {        if (e.Status == StatusCode.Unauthenticated)        {            switch (e.Method)            {                case RequestMethod.Query:                case RequestMethod.Save:                case RequestMethod.Delete:                case RequestMethod.MultiQuery:                case RequestMethod.MultiSave:                case RequestMethod.MultiDelete:                    if (!_loggingOut)                    {                        Dispatcher.InvokeAsync(() =>                        {                            _loggingOut = true;                            try                            {                                Logout(null, true);                            }                            finally                            {                                _loggingOut = false;                            }                        });                    }                    break;                default:                    break;            }        }    }    private void ApplyColorScheme()    {        Color baseColor;        try        {            baseColor = (Color)ColorConverter.ConvertFromString(App.DatabaseSettings.ColorScheme);        }        catch        {            baseColor = Colors.CornflowerBlue;        }        ThemeManager.BaseColor = baseColor;        DynamicGridUtils.SelectionBackground = ThemeManager.SelectionBackgroundBrush;        DynamicGridUtils.SelectionForeground = ThemeManager.SelectionForegroundBrush;        DynamicGridUtils.FilterBackground = ThemeManager.FilterBackgroundBrush;        DynamicGridUtils.FilterForeground = ThemeManager.FilterForegroundBrush;                //_ribbon.Background =  new SolidColorBrush(Colors.White);        //_ribbon.BackStageColor = ThemeConverter.GetBrush(ElementType.Ribbon, BrushType.Background);        ////_ribbon.BackStage.Background = ThemeConverter.GetBrush(ElementType.Ribbon, BrushType.Background);        ////_ribbon.BackStage.Foreground = ThemeConverter.GetBrush(ElementType.Ribbon, BrushType.Foreground);        UpdateRibbonColors();        PanelHost.Refresh();    }    #region Configuration    /*    protected override void OnSourceInitialized(EventArgs e)    {        base.OnSourceInitialized(e);        var source = PresentationSource.FromVisual(this) as HwndSource;        source?.AddHook(WndProc);    }    private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)    {        var message = (App.Message)msg;        switch (message)        {            case App.Message.Maximise:                WindowState = WindowState.Maximized;                break;        }        return IntPtr.Zero;    }*/    private void ConfigureMainScreen(IProgress<string>? progress)    {        var bMaps = Security.CanView<Equipment>()                    || Security.CanView<Job>()                    || Security.CanView<TimeSheet>()                    || Security.CanView<GPSTracker>();        var sections = new[]        {            new ProgressSection("Configuring Main Screen", SetupMainScreen),            new ProgressSection("Configuring Projects", () => SetupProjectsTab(bMaps)),            new ProgressSection("Configuring Manufacturing", () => SetupManufacturingTab(bMaps)),            new ProgressSection("Configuring Logistics", () => SetupLogisticsTab(bMaps)),            new ProgressSection("Configuring Products", () => SetupProductsTab(bMaps)),            new ProgressSection("Configuring Human Resources", () => SetupHumanResourcesTab(bMaps)),            new ProgressSection("Configuring Accounts", () => SetupAccountsTab(bMaps)),            new ProgressSection("Configuring Equipment", () => SetupEquipmentTab(bMaps)),            new ProgressSection("Configuring DigitalForms", () => SetupDigitalFormsTab(bMaps)),            new ProgressSection("Configuring Dashboards", () => SetupDashboardsTab(bMaps)),            new ProgressSection("Configuring System Modules", SetupSystemModules)        };        if(progress is not null)        {            Dispatcher.Invoke(SetupScreen);            foreach(var section in sections)            {                progress.Report(section.Message);                Dispatcher.Invoke(section.Action);            }        }        else        {            SetupScreen();            Progress.ShowModal(sections);        }    }    private void SetupScreen()    {        var button = _ribbon.FindVisualChildren<Fluent.DropDownButton>().FirstOrDefault();        if (button != null)            button.Visibility = Visibility.Collapsed;        if (ClientFactory.UserGuid == Guid.Empty)            _ribbonRow.Height = new GridLength(30, GridUnitType.Pixel);        else            _ribbonRow.Height = new GridLength(1, GridUnitType.Auto);    }    private void SetupMainScreen()    {        //DockManager.SidePanelSize = OutstandingDailyReports(false) ? 0.00F : 30.00F;        // Notifications Area        SetFrameworkItemVisibility(SendNotification, Security.CanView<Notification>());        SetFrameworkItemVisibility(Notifications, Security.CanView<Notification>());        SetFrameworkItemVisibility(TaskTracking, Security.IsAllowed<CanTrackTasksInDailyReport>());        UserID.Content = ClientFactory.UserID;        if (ClientFactory.PasswordExpiration != DateTime.MinValue)        {            var timeUntilExpiration = ClientFactory.PasswordExpiration - DateTime.Now;            if (timeUntilExpiration.Days < 14)            {                PasswordExpiryNotice.Content = $"Password will expire in {timeUntilExpiration.Days} days!";                PasswordExpiryNotice.Visibility = Visibility.Visible;            }            else            {                PasswordExpiryNotice.Visibility = Visibility.Collapsed;            }        }    }    private void SetupSystemModules()    {        SetFrameworkItemVisibility(CompanyInformation, Security.CanView<CompanyInformation>());        SetVisibleIfAny(BackstageSeparator0, CompanyInformation);        SetFrameworkItemVisibility(SecurityDefaultsButton,            ClientFactory.IsSupported<GlobalSecurityToken>() && Security.IsAllowed<CanCustomiseSecurityDefaults>());        SetVisibleIfAny(BackstageSeparator1, SecurityDefaultsButton);        BackstageSeparator1a.Visibility = Visibility.Visible;        SystemLogsButton.Visibility = Visibility.Visible;        SetFrameworkItemVisibility(DocumentTypeList, ClientFactory.IsSupported<DocumentType>() && Security.IsAllowed<CanViewDocumentTypes>());        SetFrameworkItemVisibility(EventList, Security.IsAllowed<CanManageEvents>());        SetVisibleIfAny(BackstageSeparator2, DocumentTypeList, EventList);        //SetModuleVisibility<>(VideoRecordingButton, Security.IsAllowed<CanRecordScreen>());        LogoutButton.Visibility = ClientFactory.UserGuid == Guid.Empty ? Visibility.Collapsed : Visibility.Visible;        LoginButton.Visibility = ClientFactory.UserGuid != Guid.Empty ? Visibility.Collapsed : Visibility.Visible;        EditDetailsButton.Visibility = ClientFactory.UserGuid == Guid.Empty ? Visibility.Collapsed : Visibility.Visible;        SetupDock<CanViewContactsDock>(ContactDock, Contacts);        SetupDock<CanViewJobDock>(JobDock, Jobs);        SetupDock<CanViewConsignmentDock>(ConsignmentDock, Consignments);        SetupDock<CanViewDeliveryDock>(DeliveryDock, Deliveries);        SetupDock<CanViewProductDock>(ProductLookupDock, ProductLookup);        SetupDock<CanViewDigitalFormsDock>(DigitalFormsDock, DigitalForms);        SetupDock<CanViewProblemsDock>(ProblemsDock, Problems);        SetupDock<CanViewRequisitionsDock>(RequisitionsDock, Requisitions);        _ribbon.InvalidateArrange();    }    private void SetupDashboardsTab(bool bMaps)    {        if (!Security.IsAllowed<ViewDesktopDashboardsTab>())            return;        SetModuleVisibility<UtilityDashboard>(DashboardsDashboardButton, Security.IsAllowed<CanViewUserDefinedDashboards>());        SetModuleVisibility<NotificationPanel>(DashboardMessagesButton, Security.CanView<Notification>());        SetModuleVisibility<TaskPanel>(DashboardsTaskButton, ClientFactory.IsSupported<Kanban>() && Security.IsAllowed<CanViewTasks>());        SetModuleVisibility<AttendancePanel>(DashboardsAttendanceButton, ClientFactory.IsSupported<TimeSheet>() && Security.IsAllowed<CanViewInOutBoard>());        SetModuleVisibility<LiveMapsPanel>(DashboardsMapButton, bMaps);        SetModuleVisibility<DailyReport>(DashboardsDailyReportButton,            ClientFactory.IsSupported<TimeSheet, Assignment>() && Security.IsAllowed<CanViewDailyReports>());        SetModuleVisibility<FactoryProductivityDashboard>(FactoryProductivityButton,            ClientFactory.IsSupported<ManufacturingHistory>()             && Security.IsAllowed<CanViewFactoryKPIs>()            && Security.IsAllowed<ViewDesktopFactoryKPIsDashboard>());        SetModuleVisibility<ManufacturingTemplateAnalysis>(TemplateAnalysisButton,            ClientFactory.IsSupported<ManufacturingTemplate, ManufacturingHistory>()             && Security.IsAllowed<CanViewTemplateAnalysis>()            && Security.IsAllowed<ViewDesktopTemplateAnalysisDashboard>());        SetModuleVisibility<FactoryFloorAnalysis>(FactoryAnalysisButton,            ClientFactory.IsSupported<ManufacturingTemplate, ManufacturingHistory>()             && Security.IsAllowed<CanViewFactoryAnalysis>()            && Security.IsAllowed<ViewDesktopFactoryAnalysisDashboard>());        SetModuleVisibility<DatabaseActivityDashboard>(DatabaseActivityButton,            ClientFactory.IsSupported<UserTracking>()             && Security.IsAllowed<CanViewDatabaseActivity>()            && Security.IsAllowed<ViewDesktopDatabaseActivityDashboard>());        SetModuleVisibility<UserActivity>(UserActivityButton, ClientFactory.IsSupported<ModuleTracking>()                                                         && Security.IsAllowed<CanViewUserActivity>()                                                        && Security.IsAllowed<ViewDesktopUserActivityDashboard>());        SetModuleVisibility<WidgetDashboard>(QuickStatusButton, Security.IsAllowed<CanViewQuickStatus>() && Security.IsAllowed<ViewDesktopQuickStatusDashboard>());        SetVisibleIfEither(DashboardsTaskSeparator,            new FrameworkElement[]            {                            DashboardsDashboardButton, DashboardMessagesButton, DashboardsTaskButton, DashboardsAttendanceButton,                            DashboardsMapButton,                            DashboardsDailyReportButton            },            new FrameworkElement[]            {                            FactoryProductivityButton, TemplateAnalysisButton, FactoryAnalysisButton, DatabaseActivityButton, UserActivityButton, QuickStatusButton            });        SetVisibleIfAny(DashboardsActions, DashboardsDashboardButton, DashboardMessagesButton, DashboardsTaskButton,            DashboardsAttendanceButton, DashboardsDailyReportButton, FactoryProductivityButton, TemplateAnalysisButton,            FactoryAnalysisButton, DatabaseActivityButton, UserActivityButton, QuickStatusButton);        //DashboardsActions.IsLauncherButtonVisible = Security.IsAllowed<CanCustomiseModules>();        //DashboardsReports.IsLauncherButtonVisible = Security.IsAllowed<CanDesignReports>();        SetVisibleIfAny(DashboardsTab, FactoryProductivityButton, TemplateAnalysisButton, FactoryAnalysisButton,            DatabaseActivityButton,            UserActivityButton, QuickStatusButton);    }        private void SetupDigitalFormsTab(bool bMaps)    {        if (!Security.IsAllowed<ViewDesktopDigitalFormsTab>())            return;        SetModuleVisibility<UtilityDashboard>(DigitalFormsDashboardButton, Security.IsAllowed<CanViewUserDefinedDashboards>());        SetModuleVisibility<NotificationPanel>(DigitalFormsMessagesButton, Security.CanView<Notification>());        SetModuleVisibility<TaskPanel>(DigitalFormsTaskButton, ClientFactory.IsSupported<Kanban>() && Security.IsAllowed<CanViewTasks>());        SetModuleVisibility<AttendancePanel>(DigitalFormsAttendanceButton, ClientFactory.IsSupported<TimeSheet>() && Security.IsAllowed<CanViewInOutBoard>());        SetModuleVisibility<LiveMapsPanel>(DigitalFormsMapButton, bMaps);        SetModuleVisibility<DailyReport>(DigitalFormsDailyReportButton,            ClientFactory.IsSupported<TimeSheet, Assignment>() && Security.IsAllowed<CanViewDailyReports>());        SetModuleVisibility<DigitalFormsLibrary>(DigitalFormsFormsLibraryButton, ClientFactory.IsSupported<DigitalForm>()                                                                           && Security.CanView<DigitalForm>()                                                                           && Security.IsAllowed<CanAdministerDigitalFormsLibrary>());        SetModuleVisibility<CompletedFormsPanel>(DigitalFormsCompletedFormsButton, Security.IsAllowed<CanViewDigitalFormsDashbaord>() && Security.IsAllowed<ViewDesktopDigitalFormsDashboard>());        SetVisibleIfEither(DigitalFormsTaskSeparator,            new FrameworkElement[]            {                DigitalFormsDashboardButton, DigitalFormsMessagesButton, DigitalFormsTaskButton, DigitalFormsAttendanceButton, DigitalFormsMapButton,                DigitalFormsDailyReportButton            }, new FrameworkElement[] { DigitalFormsFormsLibraryButton, DigitalFormsCompletedFormsButton });        SetVisibleIfAny(DigitalFormsActions, DigitalFormsDashboardButton, DigitalFormsMessagesButton, DigitalFormsTaskButton,            DigitalFormsAttendanceButton, DigitalFormsDailyReportButton, DigitalFormsFormsLibraryButton, DigitalFormsCompletedFormsButton);        SetTabVisibleIfAny(DigitalFormsTab, DigitalFormsFormsLibraryButton, DigitalFormsCompletedFormsButton);    }        private void SetupEquipmentTab(bool bMaps)    {        if (!Security.IsAllowed<ViewDesktopEquipmentTab>())            return;        SetModuleVisibility<UtilityDashboard>(EquipmentDashboardButton, Security.IsAllowed<CanViewUserDefinedDashboards>());        SetModuleVisibility<NotificationPanel>(EquipmentMessagesButton, Security.CanView<Notification>());        SetModuleVisibility<TaskPanel>(EquipmentTaskButton, ClientFactory.IsSupported<Kanban>() && Security.IsAllowed<CanViewTasks>());        SetModuleVisibility<AttendancePanel>(EquipmentAttendanceButton, ClientFactory.IsSupported<TimeSheet>() && Security.IsAllowed<CanViewInOutBoard>());        SetModuleVisibility<LiveMapsPanel>(EquipmentMapButton, bMaps);        SetModuleVisibility<DailyReport>(EquipmentDailyReportButton,            ClientFactory.IsSupported<TimeSheet, Assignment>() && Security.IsAllowed<CanViewDailyReports>());        SetModuleVisibility<EquipmentPanel>(EquipmentButton, ClientFactory.IsSupported<Equipment>()                                                        && Security.CanView<Equipment>()                                                       && Security.IsAllowed<ViewDesktopEquipmentListScreen>());                SetModuleVisibility<EquipmentMaintenancePanel>(EquipmentMaintenanceButton,            ClientFactory.IsSupported<Equipment>()             && Security.CanView<Equipment>()            && Security.IsAllowed<ViewDesktopEquipmentMaintenanceScreen>() );        SetModuleVisibility<EquipmentPlannerPanel>(EquipmentPlannerButton,             ClientFactory.IsSupported<Equipment>()             && Security.CanView<Equipment>()            && ClientFactory.IsSupported<Assignment>()             && Security.CanView<Assignment>()            && Security.IsAllowed<ViewDesktopEquipmentPlannerScreen>());        SetVisibleIfEither(EquipmentTaskSeparator,            new FrameworkElement[]            {                            EquipmentDashboardButton, EquipmentMessagesButton, EquipmentTaskButton, EquipmentAttendanceButton, EquipmentMapButton,                            EquipmentDailyReportButton            }, new FrameworkElement[] { EquipmentButton, EquipmentPlannerButton });                SetVisibleIfAny(EquipmentActions, EquipmentDashboardButton, EquipmentMessagesButton, EquipmentTaskButton,            EquipmentAttendanceButton, EquipmentDailyReportButton, EquipmentButton, EquipmentPlannerButton);        SetModuleVisibility<GPSTrackers>(TrackersMasterList, Security.CanView<GPSTracker>() && Security.IsAllowed<ViewDesktopGPSTrackersScreen>());        SetTabVisibleIfAny(EquipmentTab, EquipmentButton, TrackersMasterList);    }    private void SetupAccountsTab(bool bMaps)    {        if (!Security.IsAllowed<ViewDesktopAccountsTab>())            return;        SetModuleVisibility<UtilityDashboard>(AccountsDashboardButton, Security.IsAllowed<CanViewUserDefinedDashboards>());        SetModuleVisibility<NotificationPanel>(AccountsMessagesButton, Security.CanView<Notification>());        SetModuleVisibility<TaskPanel>(AccountsTaskButton, ClientFactory.IsSupported<Kanban>() && Security.IsAllowed<CanViewTasks>());        SetModuleVisibility<AttendancePanel>(AccountsAttendanceButton, ClientFactory.IsSupported<TimeSheet>() && Security.IsAllowed<CanViewInOutBoard>());        SetModuleVisibility<LiveMapsPanel>(AccountsMapButton, bMaps);        SetModuleVisibility<DailyReport>(AccountsDailyReportButton,            ClientFactory.IsSupported<TimeSheet, Assignment>() && Security.IsAllowed<CanViewDailyReports>());        SetModuleVisibility<CustomerPanel>(CustomerList, ClientFactory.IsSupported<Customer>()                                                    && Security.CanView<Customer>()                                                   && Security.IsAllowed<ViewDesktopCustomersScreen>());        SetModuleVisibility<InvoicePanel>(InvoiceList, ClientFactory.IsSupported<Invoice>()                                                  && Security.CanView<Invoice>()                                                 && Security.IsAllowed<ViewDesktopInvoicesScreen>());        SetModuleVisibility<CustomerReceipts>(ReceiptList, ClientFactory.IsSupported<Receipt>()                                                      && Security.CanView<Receipt>()                                                     && Security.IsAllowed<ViewDesktopReceiptsScreen>());        SetModuleVisibility<SupplierPanel>(SupplierList, ClientFactory.IsSupported<Supplier>()                                                    && Security.CanView<Supplier>()                                                   && Security.IsAllowed<ViewDesktopSuppliersScreen>());        SetModuleVisibility<DataEntryPanel>(AccountsDataButton, Security.IsAllowed<CanViewDataEntryPanel>());        SetModuleVisibility<SupplierPurchaseOrderPanel>(PurchasesList, ClientFactory.IsSupported<PurchaseOrder>()                                                                  && Security.CanView<PurchaseOrder>()                                                                 && Security.IsAllowed<ViewDesktopPurchaseOrdersScreen>());        SetModuleVisibility<SupplierBillPanel>(BillsList, ClientFactory.IsSupported<Bill>()                                                     && Security.CanView<Bill>()                                                    && Security.IsAllowed<ViewDesktopBillsScreen>());        SetModuleVisibility<SupplierPayments>(PaymentsList, ClientFactory.IsSupported<Payment>()                                                       && Security.CanView<Payment>()                                                      && Security.IsAllowed<ViewDesktopPaymentsScreen>());        SetVisibleIfEither(AccountsTaskSeparator1,            new FrameworkElement[]            {                            AccountsDashboardButton, AccountsMessagesButton, AccountsTaskButton, AccountsAttendanceButton, AccountsMapButton,                            AccountsDailyReportButton            }, new FrameworkElement[] { CustomerList, InvoiceList, ReceiptList });        SetVisibleIfEither(AccountsTaskSeparator2, new FrameworkElement[] { CustomerList, InvoiceList, ReceiptList },            new FrameworkElement[] { SupplierList, AccountsDataButton, PurchasesList, BillsList, PaymentsList });        SetVisibleIfAny(AccountsActions, AccountsDashboardButton, AccountsMessagesButton, AccountsTaskButton,            AccountsAttendanceButton,            AccountsDailyReportButton, CustomerList, InvoiceList, ReceiptList, SupplierList, PurchasesList, BillsList, PaymentsList);        SetTabVisibleIfAny(AccountsTab, CustomerList, InvoiceList, ReceiptList, SupplierList, AccountsDataButton, PurchasesList, BillsList, PaymentsList);    }    private void SetupHumanResourcesTab(bool bMaps)    {        if (!Security.IsAllowed<ViewDesktopHumanResourcesTab>())            return;        SetModuleVisibility<UtilityDashboard>(HumanResourcesDashboardButton, Security.IsAllowed<CanViewUserDefinedDashboards>());        SetModuleVisibility<NotificationPanel>(HumanResourcesMessagesButton, Security.CanView<Notification>());        SetModuleVisibility<TaskPanel>(HumanResourcesTaskButton, ClientFactory.IsSupported<Kanban>() && Security.IsAllowed<CanViewTasks>());        SetModuleVisibility<AttendancePanel>(HumanResourcesAttendanceButton,            ClientFactory.IsSupported<TimeSheet>() && Security.IsAllowed<CanViewInOutBoard>());        SetModuleVisibility<LiveMapsPanel>(HumanResourcesMapButton, bMaps);        SetModuleVisibility<DailyReport>(HumanResourcesDailyReportButton,            ClientFactory.IsSupported<TimeSheet, Assignment>() && Security.IsAllowed<CanViewDailyReports>());        SetModuleVisibility<CalendarPanel>(CalendarButton, Security.CanView<Assignment>() && Security.IsAllowed<ViewDesktopCalendarScreen>());        SetModuleVisibility<EmployeeResourcePlannerPanel>(EmployeePlannerButton, Security.CanView<Assignment>() && Security.IsAllowed<ViewDesktopEmployeePlannerScreen>());        SetModuleVisibility<TimesheetPanel>(TimesheetsButton, Security.CanView<TimeSheet>() && Security.IsAllowed<ViewDesktopStaffTimeSheetsScreen>());        SetModuleVisibility<LeaveRequestPanel>(LeaveRequestsButton, Security.CanView<LeaveRequest>() && Security.IsAllowed<ViewDesktopLeaveRequestsScreen>());        SetModuleVisibility<OrgChartPanel>(OrgChartButton,            ClientFactory.IsSupported<Employee>()            &&             Security.IsAllowed<ViewDesktopOrgChartScreen>()        );        SetModuleVisibility<MeetingPanel>(MeetingsButton, Security.IsAllowed<ViewDesktopMeetingsScreen>());        SetVisibleIfEither(HumanResourcesTaskSeparator,            new FrameworkElement[]            {                            HumanResourcesDashboardButton, HumanResourcesMessagesButton, HumanResourcesTaskButton, HumanResourcesAttendanceButton,                            HumanResourcesMapButton, HumanResourcesDailyReportButton            }, new FrameworkElement[] { CalendarButton, EmployeePlannerButton, TimesheetsButton, LeaveRequestsButton, OrgChartButton });        SetVisibleIfAny(HumanResourcesActions, HumanResourcesDashboardButton, HumanResourcesTaskButton,            HumanResourcesAttendanceButton,            HumanResourcesDailyReportButton, CalendarButton, EmployeePlannerButton, TimesheetsButton, LeaveRequestsButton, OrgChartButton);        SetModuleVisibility<UserPanel>(UsersButton, Security.CanView<User>() && Security.IsAllowed<ViewDesktopUserAccountsScreen>());        SetModuleVisibility<EmployeePanel>(EmployeesButton, Security.CanView<Employee>() && Security.IsAllowed<ViewDesktopEmployeeListScreen>());        SetVisibleIfEither(HumanResourcesSetupSeparator1,            new FrameworkElement[] { CalendarButton, TimesheetsButton, LeaveRequestsButton, OrgChartButton },            new FrameworkElement[] { UsersButton, EmployeesButton });        SetTabVisibleIfAny(HumanResourcesTab, CalendarButton, TimesheetsButton, LeaveRequestsButton, OrgChartButton, UsersButton, EmployeesButton);    }    private void SetupProductsTab(bool bMaps)    {        if (!Security.IsAllowed<ViewDesktopProductManagementTab>())            return;        SetModuleVisibility<UtilityDashboard>(ProductsDashboardButton, Security.IsAllowed<CanViewUserDefinedDashboards>());        SetModuleVisibility<NotificationPanel>(ProductsMessagesButton, Security.CanView<Notification>());        SetModuleVisibility<TaskPanel>(ProductsTaskButton, Security.CanView<Kanban>());        SetModuleVisibility<AttendancePanel>(ProductsAttendanceButton, Security.IsAllowed<CanViewInOutBoard>());        SetModuleVisibility<LiveMapsPanel>(ProductsMapButton, bMaps);        SetModuleVisibility<DailyReport>(ProductsDailyReportButton,            ClientFactory.IsSupported<TimeSheet, Assignment>() && Security.IsAllowed<CanViewDailyReports>());        SetModuleVisibility<ProductsPanel>(ProductsMasterList, Security.CanView<Product>() && Security.IsAllowed<ViewDesktopProductListScreen>());        SetModuleVisibility<StockLocationPanel>(StockLocationList, Security.CanView<StockLocation>() && Security.IsAllowed<ViewDesktopStockLocationsScreen>());        SetModuleVisibility<StockMovementPanel>(StockMovementList, Security.CanView<StockMovement>() && Security.IsAllowed<ViewDesktopStockMovementsScreen>());        SetModuleVisibility<StockForecastPanel>(StockForecastButton, Security.CanView<Product>()                                                                     && Security.CanView<JobMaterial>()                                                                     && Security.IsAllowed<ViewDesktopStockForecastScreen>());        SetModuleVisibility<ReservationManagementPanel>(ReservationManagementButton, Security.IsAllowed<ViewDesktopReservationManagementScreen>());        SetVisibleIfEither(ProductsTaskSeparator,            new FrameworkElement[]            {                            ProductsDashboardButton, ProductsMessagesButton, ProductsTaskButton, ProductsAttendanceButton, ProductsMapButton,                            ProductsDailyReportButton            }, new FrameworkElement[] { ProductsMasterList, StockLocationList, StockMovementList, StockForecastButton });        SetVisibleIfAny(ProductActions, ProductsMasterList, StockLocationList, StockMovementList, StockForecastButton);        SetTabVisibleIfAny(ProductTab, ProductActions);    }    private void SetupLogisticsTab(bool bMaps)    {        if (!Security.IsAllowed<ViewDesktopLogisticsTab>())            return;        SetModuleVisibility<UtilityDashboard>(LogisticsDashboardButton, Security.IsAllowed<CanViewUserDefinedDashboards>());        SetModuleVisibility<NotificationPanel>(LogisticsMessagesButton, Security.CanView<Notification>());        SetModuleVisibility<TaskPanel>(LogisticsTaskButton, ClientFactory.IsSupported<Kanban>() && Security.IsAllowed<CanViewTasks>());        SetModuleVisibility<AttendancePanel>(LogisticsAttendanceButton, ClientFactory.IsSupported<TimeSheet>() && Security.IsAllowed<CanViewInOutBoard>());        SetModuleVisibility<LiveMapsPanel>(LogisticsMapButton, bMaps);        SetModuleVisibility<DailyReport>(LogisticsDailyReportButton,            ClientFactory.IsSupported<TimeSheet, Assignment>() && Security.IsAllowed<CanViewDailyReports>());        SetModuleVisibility<ReadyToGoPanel>(ReadyToGoItemsButton,            ClientFactory.IsSupported<DeliveryItem>()            && Security.IsAllowed<CanViewLogisticsReadyToGo>()            && Security.IsAllowed<ViewDesktopReadyToGoScreen>());        SetModuleVisibility<DispatchPanel>(DispatchButton, Security.CanView<Shipment>()                                                           && Security.CanView<DeliveryItem>()                                                           && Security.IsAllowed<ViewDesktopRackListScreen>());        SetModuleVisibility<RequisitionPanel>(RequisitionsButton, Security.CanView<Requisition>() && Security.IsAllowed<ViewDesktopSiteRequisitionsScreen>());        SetModuleVisibility<DeliveryPanel>(DeliveriesButton, Security.IsAllowed<CanViewDeliveriesModule>() && Security.IsAllowed<ViewDesktopDeliveriesScren>());        SetModuleVisibility<DeliveredOnSitePanel>(DeliveredItemsButton,            ClientFactory.IsSupported<DeliveryItem>()            && Security.IsAllowed<CanViewDeliveredOnSite>()            && Security.IsAllowed<ViewDesktopDeliveredOnSiteScreen>());        SetModuleVisibility<ConsignmentsPanel>(ConsignmentButton, Security.CanView<Consignment>() && Security.IsAllowed<ViewDesktopIncomingConsignmentsScreen>());        SetVisibleIfEither(LogisticsTaskSeparator1,            new FrameworkElement[]            {                            LogisticsDashboardButton, LogisticsMessagesButton, LogisticsTaskButton, LogisticsAttendanceButton, LogisticsMapButton,                            LogisticsDailyReportButton            },            new FrameworkElement[]                { ReadyToGoItemsButton, DispatchButton, RequisitionsButton, DeliveriesButton, DeliveredItemsButton });        SetVisibleIfEither(LogisticsTaskSeparator2,            new FrameworkElement[]                { ReadyToGoItemsButton, DispatchButton, RequisitionsButton, DeliveriesButton, DeliveredItemsButton },            new FrameworkElement[] { ConsignmentButton });        SetTabVisibleIfAny(LogisticsTab, DispatchButton, RequisitionsButton, DeliveriesButton, ReadyToGoItemsButton,            DeliveredItemsButton,            ConsignmentButton);    }    private void SetupManufacturingTab(bool bMaps)    {        if (!Security.IsAllowed<ViewDesktopManufacturingTab>())            return;        SetModuleVisibility<UtilityDashboard>(ManufacturingDashboardButton, Security.IsAllowed<CanViewUserDefinedDashboards>());        SetModuleVisibility<NotificationPanel>(ManufacturingMessagesButton, Security.CanView<Notification>());        SetModuleVisibility<TaskPanel>(ManufacturingTaskButton, ClientFactory.IsSupported<Kanban>() && Security.IsAllowed<CanViewTasks>());        SetModuleVisibility<AttendancePanel>(ManufacturingAttendanceButton,            ClientFactory.IsSupported<TimeSheet>() && Security.IsAllowed<CanViewInOutBoard>());        SetModuleVisibility<LiveMapsPanel>(ManufacturingMapButton, bMaps);        SetModuleVisibility<DailyReport>(ManufacturingDailyReportButton,            ClientFactory.IsSupported<TimeSheet, Assignment>() && Security.IsAllowed<CanViewDailyReports>());                SetVisibleIfAny(ManufacturingTaskSeparator,            new FrameworkElement[]            {                ManufacturingDashboardButton, ManufacturingMessagesButton, ManufacturingTaskButton, ManufacturingAttendanceButton,                ManufacturingMapButton, ManufacturingDailyReportButton            });                SetModuleVisibility<StagingPanel>(DesignManagementButton, Security.CanView<Job>() && Security.IsAllowed<ViewDesktopDesignManagementScreen>());        SetFrameworkItemVisibility(ManufacturingDesignSeparator, Security.CanView<Job>() && Security.IsAllowed<ViewDesktopDesignManagementScreen>());                SetModuleVisibility<ManufacturingPanel>(FactoryStatusButton,            ClientFactory.IsSupported<ManufacturingFactory, ManufacturingPacket>()            && Security.IsAllowed<CanViewFactoryStatus>()            && Security.IsAllowed<ViewDesktopManufacturingStatusScreen>());        SetModuleVisibility<ManufacturingAllocationPanel>(FactoryAllocationButton,            ClientFactory.IsSupported<ManufacturingFactory, ManufacturingPacket, ManufacturingPacketStage>()            && Security.IsAllowed<CanViewFactoryAllocation>()            && Security.IsAllowed<ViewDesktopFactoryAllocationScreen>());        //SetModuleVisibility<>(FactoryScheduleButton, ClientFactory.IsSupported<Booking>() && ClientFactory.IsEnabled<>());        SetModuleVisibility<FactoryPanel>(FactoryFloorButton,            ClientFactory.IsSupported<ManufacturingPacket>()            && Security.IsAllowed<CanViewFactoryFloor>()            && Security.IsAllowed<ViewDesktopFactoryFloorScreen>());        //SetModuleVisibility<>(FactoryReadyButton, ClientFactory.IsSupported<ManufacturingPacket>() && Security.IsAllowed<CanViewFactoryReadyToGo>());                SetVisibleIfAny(ManufacturingActionSeparator,            new FrameworkElement[]            {                FactoryStatusButton, FactoryAllocationButton, FactoryFloorButton            });                        SetVisibleIfAny(ManufacturingActions, ManufacturingDashboardButton, ManufacturingMessagesButton, ManufacturingTaskButton,            ManufacturingAttendanceButton, ManufacturingDailyReportButton, FactoryStatusButton, FactoryAllocationButton,            FactoryFloorButton);        SetTabVisibleIfAny(ManufacturingTab, FactoryStatusButton, FactoryAllocationButton, FactoryFloorButton);    }    private void SetupProjectsTab(bool bMaps)    {        if (!Security.IsAllowed<ViewDesktopProjectsTab>())            return;        SetModuleVisibility<UtilityDashboard>(ProjectsDashboardButton, Security.IsAllowed<CanViewUserDefinedDashboards>());        SetModuleVisibility<NotificationPanel>(ProjectMessagesButton, Security.CanView<Notification>());        SetModuleVisibility<TaskPanel>(ProjectTaskButton, ClientFactory.IsSupported<Kanban>() && Security.IsAllowed<CanViewTasks>());        SetModuleVisibility<AttendancePanel>(ProjectAttendanceButton, ClientFactory.IsSupported<TimeSheet>() && Security.IsAllowed<CanViewInOutBoard>());        SetModuleVisibility<LiveMapsPanel>(ProjectsMapButton, bMaps);        SetModuleVisibility<DailyReport>(ProjectDailyReportButton,            ClientFactory.IsSupported<TimeSheet, Assignment>() && Security.IsAllowed<CanViewDailyReports>());        SetModuleVisibility<ProjectsPanel>(ProjectsButton, Security.CanView<Job>() && Security.IsAllowed<ViewDesktopProjectsScreen>());        //SetModuleVisibility<>(ServiceButton, Security.CanView<Job>() && Security.IsAllowed<ViewDesktopServiceScreen>());        SetModuleVisibility<JobResourcePlannerPanel>(ProjectPlannerButton, Security.CanView<Job>() && Security.IsAllowed<ViewDesktopProjectPlannerScreen>());                SetVisibleIfEither(ProjectTaskSeparator,            new FrameworkElement[]            {                            ProjectsDashboardButton, ProjectMessagesButton, ProjectTaskButton, ProjectAttendanceButton, ProjectsMapButton,                            ProjectDailyReportButton            }, new FrameworkElement[] { QuotesButton, ProjectsButton, ProjectPlannerButton });                SetModuleVisibility<QuotePanel>(QuotesButton, Security.CanView<Quote>() && Security.IsAllowed<ViewDesktopQuotesScreen>());        SetModuleVisibility<KitPanel>(KitsMasterList, Security.CanView<Kit>() && Security.IsAllowed<ViewDesktopProductKitsScreen>());        SetModuleVisibility<CostSheetPanel>(CostSheetsMasterList, Security.CanView<CostSheet>() && Security.IsAllowed<ViewDesktopCostSheetsScreen>());        SetVisibleIfAny(ProjectsSetup, KitsMasterList, CostSheetsMasterList);        //ProjectsActions.IsLauncherButtonVisible = Security.IsAllowed<CanCustomiseModules>();        //ProjectReports.IsLauncherButtonVisible = Security.IsAllowed<CanDesignReports>();        SetTabVisibleIfAny(ProjectsTab, QuotesButton, ProjectsButton, ProjectPlannerButton, CostSheetsMasterList, KitsMasterList);    }        private void SetupDock<TSecurityDescriptor>(LayoutAnchorable layout, IDockPanel dock)        where TSecurityDescriptor : ISecurityDescriptor, new()    {        if (Security.IsAllowed<TSecurityDescriptor>())        {            if (!DockGroup.Children.Any(x => x == layout))            {                DockGroup.Children.Add(layout);            }            if (layout.IsVisible && (ClientFactory.UserGuid != Guid.Empty))                dock.Setup();        }        else        {            DockGroup.RemoveChild(layout);        }    }    private void LoadApplicationState()    {        if (ClientFactory.UserGuid != Guid.Empty)        {            _ribbon.IsCollapsed = false;            if (OutstandingDailyReports(false))            {                MessageWindow.ShowMessage("There are outstanding Daily Reports that must be filled out before continuing!"                    + "\n\nAccess to PRS is restricted until this is corrected.",                    "Outstanding Reports");                var dailyReportPanel = LoadWindow<DailyReport>(ProjectDailyReportButton);                if(dailyReportPanel is not null)                {                    dailyReportPanel.OnTimeSheetConfirmed += e =>                    {                        if (!OutstandingDailyReports(true))                        {                            ConfigureMainScreen(null);                            LoadApplicationState();                        }                    };                }                return;            }            using (new WaitCursor())            {                _ribbon.IsCollapsed = false;                LoadInitialWindow();            }        }    }    private void LoadInitialWindow()    {        var app = new LocalConfiguration<AppSettings>().Load();        var module = app.Settings.ContainsKey("CurrentPanel")            ? !string.IsNullOrWhiteSpace(app.Settings["CurrentPanel"])                ? app.Settings["CurrentPanel"].Split([" / "], StringSplitOptions.None)                : [ "Human Resources", "Task List"]            : [ "Human Resources", "Task List"];                try        {            var bFound = false;            if (module.Length == 2)                foreach (Fluent.RibbonTabItem tab in _ribbon.Tabs)                {                    if (String.Equals(tab.Header, module.First()))                    {                        _ribbon.SelectedTabItem = tab;                        foreach (Fluent.RibbonGroupBox bar in tab.Groups)                        {                            foreach (var item in bar.Items)                            {                                var button = item as Fluent.Button;                                if (button != null && String.Equals(button.Header, module.Last()))                                {                                    bFound = true;                                    if (button.Command is SimpleCommand command)                                        command.Execute(null);                                    //button.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent));                                    break;                                }                            }                            if (bFound)                                break;                        }                    }                    if (bFound)                        break;                }        }        catch (Exception e)        {            MessageWindow.ShowError($"Unable to load {app.Settings["CurrentPanel"]}", e);        }    }    private void _ribbon_OnLoaded(object sender, RoutedEventArgs e)    {        _ribbon.SelectedTabItem = CurrentTab;    }    private void LoadSecondaryWindows()    {        if (ClientFactory.UserGuid != Guid.Empty)        {            var windows = App.DatabaseSettings.SecondaryWindows;            foreach (var key in windows.Keys.ToArray())            {                SecondaryWindows[key] = new SecondaryWindow(                    key,                    windows[key].Item1,                    windows[key].Item2,                    windows[key].Item3,                    windows[key].Item4,                    windows[key].Item5,                    windows[key].Item6                );                SecondaryWindows[key].Closed += (o, e) => { SecondaryWindows.Remove(key); };                SecondaryWindows[key].Show();            }        }        else        {            foreach (var key in SecondaryWindows.Keys.ToArray())            {                App.IsClosing = true;                SecondaryWindows[key].Close();                App.IsClosing = false;            }        }    }    private Fluent.RibbonTabItem GetTabItem(FrameworkElement? sender)    {        if (sender == null)            throw new Exception("No Tab Found!");        if (sender is Fluent.RibbonTabItem)            return (Fluent.RibbonTabItem)sender;        return GetTabItem(sender.Parent as FrameworkElement);    }    private IEnumerable<IBasePanel> Panels    {        get        {            if(PanelHost.CurrentPanel is not null)            {                yield return PanelHost.CurrentPanel;            }            foreach(var window in SecondaryWindows.Values)            {                yield return window.Panel;            }        }    }        #region LoadWindow / LoadSecondaryWindow    private T? LoadWindow<T>(Fluent.Button sender) where T : class, IBasePanel, new()    {        return LoadWindow<T>(sender, new CancelEventArgs());    }    public IBasePanel? LoadWindow(Type t, Fluent.Button sender, CancelEventArgs cancel)    {        using (new WaitCursor())        {            UnloadWindow(cancel);            if (cancel.Cancel)            {                return null;            }            CurrentTab = GetTabItem(sender);            CurrentButton = sender;            //CurrentButton.IsSelected = true;            UpdateRibbonColors();            var moduleName = sender.Header?.ToString() ?? "";            var panel = PanelHost.LoadPanel(t, moduleName);            ContentControl.Content = panel;            Title =                $"{moduleName} - {(String.Equals(App.Profile?.ToUpper(), "DEFAULT") ? "PRS Desktop" : App.Profile)} (Release {CoreUtils.GetVersion()})";            PanelHost.Refresh();            if (panel is NotificationPanel)            {                Logger.Send(LogType.Information, ClientFactory.UserID, "Disabling Heartbeat");                NotificationsWatchDog.IsEnabled = false;                Notifications.Visibility = Visibility.Collapsed;                DockingGrid.ColumnDefinitions[1].Width = new GridLength(0, GridUnitType.Pixel);                DockingGrid.ColumnDefinitions[2].Width = new GridLength(0, GridUnitType.Pixel);            }            else            {                ReloadNotifications();            }            if (sender != null)            {                var settings = new LocalConfiguration<AppSettings>().Load();                var module = string.Format("{0} / {1}", CurrentTab?.Header, sender.Header);                if (!settings.Settings.ContainsKey("CurrentPanel") || module != settings.Settings["CurrentPanel"])                {                    settings.Settings["CurrentPanel"] = module;                    try                    {                        new LocalConfiguration<AppSettings>().Save(settings);                    }                    catch (Exception e)                    {                        Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));                    }                }            }            return panel;        }    }        private T? LoadWindow<T>(Fluent.Button sender, CancelEventArgs cancel) where T : class, IBasePanel, new()    {        return LoadWindow(typeof(T), sender, cancel) as T;    }    private void LoadSecondaryWindow(Type t, Fluent.Button sender)    {        var id = Guid.NewGuid();        var window = new Tuple<string, string, double, double, double, double>(            t.EntityName(),            sender.Header?.ToString() ?? "",            Left + 100,            Top + 100,            Width - 200,            Height - 200        );        App.DatabaseSettings.SecondaryWindows[id] = window;        new LocalConfiguration<DatabaseSettings>(App.Profile).Save(App.DatabaseSettings);        SecondaryWindows[id] = new SecondaryWindow(            id,            window.Item1,            window.Item2,            window.Item3,            window.Item4,            window.Item5,            window.Item6        );        SecondaryWindows[id].Show();    }        private void LoadSecondaryWindow<T>(Fluent.Button sender) where T : class, IBasePanel, new()         => LoadSecondaryWindow(typeof(T), sender);        // private void SecondaryWindow_Click(object sender, RoutedEventArgs e)    // {    //     var panel = PanelHost.CurrentPanel;    //     if (panel is null) return;    //    //     var id = Guid.NewGuid();    //     var window = new Tuple<string, string, double, double, double, double>(    //         panel.GetType().EntityName(),    //         PanelHost.CurrentModuleName,    //         Left + 100,    //         Top + 100,    //         Width - 200,    //         Height - 200    //     );    //     App.DatabaseSettings.SecondaryWindows[id] = window;    //     new LocalConfiguration<DatabaseSettings>(App.Profile).Save(App.DatabaseSettings);    //     SecondaryWindows[id] = new SecondaryWindow(    //         id,    //         window.Item1,    //         window.Item2,    //         window.Item3,    //         window.Item4,    //         window.Item5,    //         window.Item6    //     );    //     SecondaryWindows[id].Show();    // }        #endregion    private void UpdateRibbonColors()    {        foreach (var tab in _ribbon.Tabs)        {            bool bFound = false;            foreach (var grp in tab.Groups)            {                foreach (var btn in grp.Items)                {                    if (btn is Fluent.Button fluentbutton)                    {                        bFound = bFound || (btn == CurrentButton);                        fluentbutton.Background = (btn == CurrentButton) ? ThemeManager.SelectedTabItemBackgroundBrush : new SolidColorBrush(Colors.White);                        fluentbutton.Foreground = (btn == CurrentButton) ? ThemeManager.SelectedTabItemForegroundBrush : new SolidColorBrush(Colors.Black);                    }                }                tab.Background = bFound ? ThemeManager.SelectedTabItemBackgroundBrush : new SolidColorBrush(Colors.White);                tab.Foreground = bFound ? ThemeManager.SelectedTabItemForegroundBrush : new SolidColorBrush(Colors.Black);            }        }    }    private static void StartLocalDatabase(IProgress<string> progress)    {        var dirName = Path.GetDirectoryName(App.DatabaseSettings.FileName);        if (!Directory.Exists(dirName) && dirName != null)            Directory.CreateDirectory(dirName);        var FileName = App.DatabaseSettings.FileName;        var Exists = File.Exists(FileName);        progress.Report("Configuring Stores");        DbFactory.Stores = CoreUtils.TypeList(            new[]            {                typeof(Store<>).Assembly,                typeof(EquipmentStore).Assembly            },            myType =>                myType.IsClass                && !myType.IsAbstract                && !myType.IsGenericType                && myType.GetInterfaces().Contains(typeof(IStore))        ).ToArray();        DbFactory.DefaultStore = typeof(BaseStore<>);        DbFactory.ProviderFactory = new SQLiteProviderFactory(App.DatabaseSettings.FileName);        DbFactory.ColorScheme = App.DatabaseSettings.ColorScheme;        DbFactory.Logo = App.DatabaseSettings.Logo;        progress.Report("Starting Local Database");        DbFactory.Start();        ClientFactory.DatabaseID = DbFactory.ID;        progress.Report("Checking Database");        var users = DbFactory.NewProvider(Logger.Main).Load<User>();        if (!users.Any())        {            var user = new User { UserID = "ADMIN", Password = "admin" };            DbFactory.NewProvider(Logger.Main).Save(user);            var employee = DbFactory.NewProvider(Logger.Main).Load(new Filter<Employee>(x => x.Code).IsEqualTo("ADMIN")).FirstOrDefault();            employee ??= new Employee { Code = "ADMIN", Name = "Administrator Account" };            employee.UserLink.ID = user.ID;            DbFactory.NewProvider(Logger.Main).Save(employee);        }        CoreUtils.GoogleAPIKey = App.DatabaseSettings.GoogleAPIKey;        Job.JobNumberPrefix = App.DatabaseSettings.JobPrefix;        PurchaseOrder.PONumberPrefix = App.DatabaseSettings.PurchaseOrderPrefix;    }    #endregion    #region Login Management    private ValidationStatus? TryAutoLogin()    {        ValidationStatus? result = null;        if (App.DatabaseSettings.LoginType == LoginType.UserID)        {            try            {                result = ClientFactory.Validate(App.DatabaseSettings.UserID, App.DatabaseSettings.Password);            }            catch (Exception e)            {                MessageWindow.ShowError("Error connecting to server.\nPlease check the server URL and port number.", e);                result = null;            }            if (result == ValidationStatus.INVALID)            {                MessageWindow.ShowMessage("Unable to Login with User ID: " + App.DatabaseSettings.UserID, "Login failed");            }        }        return result;    }    private ValidationStatus? DoLogin()    {        ValidationStatus? result = null;        if (result != ValidationStatus.VALID)        {            var login = new PinLogin(CoreUtils.GetVersion(), result ?? ValidationStatus.INVALID);            if (login.ShowDialog() == true)            {                result = ValidationStatus.VALID;            }        }        return result ?? ValidationStatus.INVALID;    }    /// <summary>    /// To be called after <see cref="DoLogin(bool)"/> and if a valid login session exists. Configures the main screen and loads the windows.    /// </summary>    /// <param name="progress">If not <see langword="null"/>, then rather than opening a new progress window, just uses that.</param>    private void AfterLogin(IProgress<string>? progress)    {        try        {            Logger.Send(LogType.Information, "", "Checking Support Ticket Status");            CheckSupportTicketStatus();            Logger.Send(LogType.Information, "", "Loading employee");            LoadCurrentEmployee();            if (CheckTimesheetBypass(true))            {                UpdateCurrentLogin();            }            else            {                Dispatcher.Invoke(() =>                {                    MessageWindow.ShowMessage("You must clock on before logging in to PRS!", "Not clocked in.");                });                ClientFactory.InvalidateUser();                App.EmployeeID = Guid.Empty;                App.EmployeeName = "";                App.EmployeeCode = "";                App.EmployeeEmail = "";            }            Logger.Send(LogType.Information, "", "Setting colours");            if (progress is null)            {                ApplyColorScheme();            }            else            {                Dispatcher.Invoke(ApplyColorScheme);            }            Logger.Send(LogType.Information, "", "Configuring main window");            ConfigureMainScreen(progress);            Logger.Send(LogType.Information, "", "Loading current window");            if (progress is null)            {                LoadApplicationState();            }            else            {                Dispatcher.Invoke(LoadApplicationState);            }            Logger.Send(LogType.Information, "", "Loading secondary window");            if (progress is null)            {                LoadSecondaryWindows();            }            else            {                Dispatcher.Invoke(LoadSecondaryWindows);            }            //if (_ribbon.Menu.IsVisible)            //{            //    _ribbon.;            //}        }        catch (Exception e)        {                    }    }    /// <summary>    /// Creates a new <see cref="Login"/> if one does not already exist. Otherwise, updates the <see cref="Login"/> entry in the database with new Station ID.    /// </summary>    private void UpdateCurrentLogin()    {        if (CoreUtils.GetVersion().Equals("???"))            return;        // Register this station with the Server        // Later on, the heartbeat will check to make sure         // that the StationID hasn't changed.  If it has,        // then we've logged in somewhere else and we'll        // drop out of this station        var curr = new Client<Login>().Query(            new Filter<Login>(x => x.User.ID).IsEqualTo(ClientFactory.UserGuid),            null        ).Rows.FirstOrDefault();        if (curr != null)        {            var c = curr.ToObject<Login>();            c.StationID = station.StationID;            station = c;        }        else        {            station.User.ID = ClientFactory.UserGuid;            station.TimeStamp = DateTime.Now;        }        new Client<Login>().Save(station, "", (o, e) => { });    }    private void LoadCurrentEmployee()    {        var me = new Client<Employee>().Query(            new Filter<Employee>(x => x.UserLink.ID).IsEqualTo(ClientFactory.UserGuid),            Columns.None<Employee>().Add(x => x.ID).Add(x=>x.Code).Add(x => x.Email).Add(x => x.Name)        );        App.EmployeeID = me.Rows.FirstOrDefault()?.Get<Employee, Guid>(x => x.ID) ?? Guid.Empty;        App.EmployeeCode = me.Rows.FirstOrDefault()?.Get<Employee, string>(x => x.Code) ?? "";        App.EmployeeName = me.Rows.FirstOrDefault()?.Get<Employee, String>(x => x.Name) ?? "";        App.EmployeeCode = me.Rows.FirstOrDefault()?.Get<Employee, String>(x => x.Code) ?? "";        App.EmployeeEmail = me.Rows.FirstOrDefault()?.Get<Employee, String>(x => x.Email) ?? "";    }    private void ExecuteLogout()    {        new Client<Login>().Delete(station, "");        station.ID = Guid.Empty;        App.EmployeeID = Guid.Empty;        App.EmployeeName = "";        App.EmployeeEmail = "";    }    /// <summary>    /// Logs the user out and unloads windows    /// </summary>    /// <param name="message">A message to display as the reason for logging out, or <c>null</c> for no message.</param>    private bool Logout(string? message = null, bool force = false)    {        // I really don't like all these try-catch blocks; unfortunately, if we are trying to log out and invalidate due to an unauthenticated user,        // all the queries that get called here will throw exceptions and thus break our system, failing to log out.        try        {            FinalizeAutoTimesheet();        }        catch        {            if (!force) throw;        }        // Try to unload the window;        try        {            UnloadWindow(null);            if (DatabaseType == DatabaseType.Standalone && !CoreUtils.GetVersion().Equals("???"))                scheduler.Stop();        }        catch        {            if (!force) throw;        }        // Next, try to set things to being empty        try        {            if (!CoreUtils.GetVersion().Equals("???"))                if (station.ID != Guid.Empty)                {                    ExecuteLogout();                }            ClearTrackingKanban();        }        catch        {            if (!force) throw;        }        ClientFactory.InvalidateUser();        ConfigureMainScreen(null);        LoadSecondaryWindows();        if (message != null)        {            MessageWindow.ShowMessage(message, "Logged out");        }        if (DoLogin() == ValidationStatus.VALID)        {            AfterLogin(null);        }        return true;    }    #endregion    #region Timesheets    private void RefreshTimeSheets()    {        if (App.EmployeeID == Guid.Empty)            return;        var filter = new Filter<TimeSheet>(x => x.EmployeeLink.ID).IsEqualTo(App.EmployeeID);        filter = filter.And(new Filter<TimeSheet>(x => x.Confirmed).IsEqualTo(DateTime.MinValue).Or(x => x.Date).IsEqualTo(DateTime.Today));        _timesheets = new Client<TimeSheet>().Query(            filter,            Columns.None<TimeSheet>().Add(                x => x.ID,                x => x.Date,                x => x.Finish            )        );    }    private CoreTable GetTimesheet()    {        return new Client<TimeSheet>().Query(            new Filter<TimeSheet>(x => x.Date).IsEqualTo(DateTime.Today)                .And(x => x.EmployeeLink.ID).IsEqualTo(App.EmployeeID)                .And(x => x.Finish).IsEqualTo(TimeSpan.Zero)        );    }    private bool CheckTimesheetBypass(bool message)    {        if (!ClientFactory.IsSupported<TimeSheet>())            return true;        var isClockedOn = IsClockedOn();        if (!Security.IsAllowed<CanBypassTimeBench>())        {            if (!isClockedOn)            {                return false;            }            return true;        }        if (Security.IsAllowed<AutoGenerateTimesheet>())            if (!isClockedOn)            {                var ts = new TimeSheet();                ts.Date = DateTime.Today;                ts.Start = DateTime.Now.TimeOfDay;                ts.EmployeeLink.ID = App.EmployeeID;                ts.Notes = "Automatic Login from PRS Desktop";                new Client<TimeSheet>().Save(ts, "AutoLogon because Timebench Bypass is enabled", (o, e) => { });            }        return true;    }    private bool IsClockedOn()    {        RefreshTimeSheets();        if (_timesheets == null)            return false;        return _timesheets.Rows.Any(r =>            r.Get<TimeSheet, DateTime>(c => c.Date).Date == DateTime.Today && r.Get<TimeSheet, TimeSpan>(c => c.Finish) == new TimeSpan());    }    private void FinalizeAutoTimesheet()    {        if (Security.IsAllowed<AutoGenerateTimesheet>())        {            var check = new Client<TimeSheet>().Query(                new Filter<TimeSheet>(x => x.Date).IsEqualTo(DateTime.Today).And(x => x.EmployeeLink.ID).IsEqualTo(App.EmployeeID).And(x => x.Finish)                    .IsEqualTo(TimeSpan.Zero)            );            if (check.Rows.Any())            {                var ts = check.Rows.First().ToObject<TimeSheet>();                if (DateTime.Now.TimeOfDay < ts.Start.Add(new TimeSpan(0, 2, 0)))                {                    new Client<TimeSheet>().Delete(ts, "Deleting Auto TimeSheet because TimeBench Bypass is enabled", (o, ex) => { });                }                else                {                    ts.Finish = DateTime.Now.TimeOfDay;                    new Client<TimeSheet>().Save(ts, "Clocking off Auto Timesheet because Timesheet Bypass is enabled", (o, ex) => { });                }            }        }    }    #endregion    private string CurrentPanelSlug()    {        var app = new LocalConfiguration<AppSettings>().Load();        var module = app.Settings["CurrentPanel"].Split(new[] { " / " }, StringSplitOptions.None);        return module.LastOrDefault()?.Replace(" ", "_").Replace("/", "") ?? "";    }    private bool ShowHelp()    {        Process.Start(new ProcessStartInfo("https://prsdigital.com.au/wiki/index.php/" + CurrentPanelSlug()) { UseShellExecute = true });        return true;    }    private void Wiki_Click(object sender, RoutedEventArgs e)    {        ShowHelp();    }    private void Window_Loaded(object sender, RoutedEventArgs e)    {    }    private void UnloadWindow(CancelEventArgs? cancel)    {        PanelHost.UnloadPanel(cancel);        if (cancel?.Cancel == true)        {            return;        }        Title =            $" - {(String.Equals(App.Profile?.ToUpper(), "DEFAULT") ? "PRS Desktop" : App.Profile)} (Release {CoreUtils.GetVersion()})";        if (CurrentTab is not null)        {            var border = VisualUtils.EnumChildrenOfType(CurrentTab, typeof(Border)).LastOrDefault();            if (border != null)            {                ((Border)border).Background = new SolidColorBrush(Colors.Transparent);                ((Border)border).BorderBrush = new SolidColorBrush(Colors.Transparent);            }            var ReportsBar = FindRibbonBar(CurrentTab, x => x.Header.Equals("Print"));            if (ReportsBar is not null)            {                ReportsBar.Items.Clear();                ReportsBar.Visibility = Visibility.Collapsed;                ReportsBar.IsLauncherVisible = false;            }            var ActionBar = FindRibbonBar(CurrentTab, x => x.Header.Equals("Actions"));            if (ActionBar is not null)            {                ActionBar.IsLauncherVisible = false;                foreach (var module in CurrentModules)                    ActionBar.Items.Remove(module);            }        }        CurrentTab = null;        CurrentButton = null;        ContentControl.Content = null;    }    private void RibbonWindow_Activated(object sender, EventArgs e)    {    }    private void RegisterModules(IProgress<string> progress)    {        foreach (Fluent.RibbonTabItem tab in _ribbon.Tabs)            foreach (Fluent.RibbonGroupBox bar in tab.Groups)                foreach (var item in bar.Items)                    Dispatcher.Invoke(() =>                    {                        var button = item as RibbonButton;                        if (button != null && button.Label != "Refresh")                            if (bar.Header.Equals("Actions"))                                Modules.Register(button.Label);                    });    }    #region Visibility    private void SetFrameworkItemVisibility(FrameworkElement button, bool visible)    {        var vResult = true;        var eResult = ClientFactory.UserGuid != Guid.Empty && visible;        button.Visibility = vResult && eResult ? Visibility.Visible : Visibility.Collapsed;        if (button is Fluent.Button rb)        {            CustomModules.Register(rb.Header?.ToString() ?? "");            rb.IsEnabled = !OutstandingDailyReports(false);        }    }        private void SetModuleVisibility<T>(Fluent.Button button, bool visible) where T : class, IBasePanel, new()    {        SetFrameworkItemVisibility(button,visible);                button.MinWidth = 60;                if (button.Command == null)        {            button.Command = new SimpleCommand(() => LoadWindow<T>(button));                   var menu = new ContextMenu();            menu.Items.Add(new MenuItem()            {                Header = "Open in New Window",                Icon =  new System.Windows.Controls.Image() { Source = PRSDesktop.Resources.target.AsBitmapImage() },                Command = new SimpleCommand(() => LoadSecondaryWindow<T>(button))            });            button.ContextMenu = menu;        }    }    private static void SetVisibleIfEither(FrameworkElement separator, FrameworkElement[] left, FrameworkElement[] right)    {        var bLeft = false;        foreach (var button in left)            bLeft = bLeft || button.Visibility == Visibility.Visible;        var bRight = false;        foreach (var button in right)            bRight = bRight || button.Visibility == Visibility.Visible;        separator.Visibility = bLeft && bRight ? Visibility.Visible : Visibility.Collapsed;    }    private static void SetVisibleIfAny(FrameworkElement separator, params FrameworkElement[] buttons)    {        var bVisible = false;        foreach (var button in buttons)            bVisible = bVisible || button.Visibility == Visibility.Visible;        separator.Visibility = bVisible ? Visibility.Visible : Visibility.Collapsed;    }    private static void SetTabVisibleIfAny(Fluent.RibbonTabItem tab, params FrameworkElement[] buttons)    {        var bVisible = false;        foreach (var button in buttons)            bVisible = bVisible || button.Visibility == Visibility.Visible;        tab.Visibility = bVisible ? Visibility.Visible : Visibility.Collapsed;    }    #endregion    private static Fluent.RibbonGroupBox? FindRibbonBar(Fluent.RibbonTabItem tab, Func<Fluent.RibbonGroupBox, bool> predicate)    {        foreach (var group in tab.Groups)        {            if (group != null)                if (predicate.Invoke(group))                    return group;        }        return null;    }    #region Button Event Handlers            #region Accounts    #endregion        #region Dashboards    #endregion    private void Console_Click(object sender, RoutedEventArgs a)    {        if (_console is null)        {            _console = new DesktopConsole("Console");            _console.Closing += (o, args) => _console = null;        }        _console.Show();    }        private void RefreshMenu_Click(object sender, RoutedEventArgs e)    {        PanelHost.Refresh();    }        //private void NotificationsList_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)    //{    //    if (NotificationsList.SelectedIndex < 0)    //        return;    //    var editors = NotificationsList.FindVisualChildren<InABox.DynamicGrid.ExtendedRichTextEditor>().ToArray();    //    var selected = editors[NotificationsList.SelectedIndex];    //    selected.Text = (String)selected.Tag; //NotificationsList.SelectedIndex.ToString();    //}    //private void ViewNotification_Click(object sender, System.Windows.Input.MouseButtonEventArgs e)    //{    //    Notification notification = (sender as Label).Tag as Notification;    //    NotificationDetails details = new NotificationDetails(notification);    //    details.ShowDialog();    //    ReloadNotifications();    //}        private void Library_Click(object sender, RoutedEventArgs e)    {        Process.Start(            new ProcessStartInfo            {                FileName = App.DatabaseSettings.LibraryLocation,                UseShellExecute = true,                Verb = "open"            }        );    }    private void SendNotificationClick(object sender, RoutedEventArgs e)    {        var form = new NotificationForm { Description = "" };        if (form.ShowDialog() == true)            ReloadNotifications();    }    private void CompanyInformation_Click(object sender, RoutedEventArgs e)    {        var info = new Client<CompanyInformation>().Load().FirstOrDefault();        if (info == null)            info = new CompanyInformation();        new DynamicDataGrid<CompanyInformation>().EditItems(new[] { info });    }        private void Setup_Click(object sender, RoutedEventArgs e)    {        var tab = _ribbon.SelectedTabItem;        if (tab is null)            return;        var menu = new ContextMenu();        PanelHost.InitialiseSetupMenu(menu);        menu.IsOpen = true;    }    private void Issues_Click(object sender, RoutedEventArgs e)    {        try        {            IssuesWindow.Execute();            CheckSupportTicketStatus();        }        catch(Exception err)        {            MessageWindow.ShowError("Could not load issues.", err);        }    }    private void StartForm<TEntityForm, TEntity, TEntityLink>(DigitalForm form)        where TEntityForm : BaseEntityForm<TEntity, TEntityLink, TEntityForm>, new()        where TEntity : Entity, new()        where TEntityLink : EntityLink<TEntity>, new()    {        var entityForm = new TEntityForm();        entityForm.Form.CopyFrom(form);        entityForm.Description = form.Description;        entityForm.FormStarted = DateTime.Now;        var entity = DFUtils.NewEntity<TEntityForm, TEntity, TEntityLink>(form);        if (DynamicFormEditWindow.EditDigitalForm(entityForm, out var dataModel, entity))        {            dataModel.Update(null);        }    }    private void Forms_Click(object sender, RoutedEventArgs e)    {        var select = new MultiSelectDialog<DigitalForm>(            Filter<DigitalForm>.And(                LookupFactory.DefineChildFilter<KanbanForm, DigitalForm>(Array.Empty<KanbanForm>()),                new Filter<DigitalForm>(x => x.ID).InQuery(                    new Filter<EmployeeDigitalForm>(x => x.Employee.ID).IsEqualTo(App.EmployeeID),                    x => x.Form.ID)),            LookupFactory.DefineChildColumns<KanbanForm, DigitalForm>()                .Add(x => x.Description),            false);        if (select.ShowDialog() == true)        {            var digitalForm = select.Data().Rows.FirstOrDefault()?.ToObject<DigitalForm>();            if (digitalForm is not null)            {                StartForm<KanbanForm, Kanban, KanbanLink>(digitalForm);            }        };    }    #endregion    private bool OutstandingDailyReports(bool refresh)    {        if (!Security.IsAllowed<CanViewDailyReports>() || Security.IsAllowed<BypassOutstandingDailyReports>())            return false;        if (refresh)            RefreshTimeSheets();        if (_timesheets == null)            return false;        return _timesheets.Rows.Any(r => r.Get<TimeSheet, DateTime>(c => c.Date).Date < DateTime.Today);    }    private void ShutDownTransport()    {        if (_transport != null)        {            _transport.OnClose -= TransportConnectionLost;            if (_transport.IsConnected())                _transport.Disconnect();            _transport = null;        }    }    private void Window_Unloaded(object sender, RoutedEventArgs e)    {        ShutDownTransport();    }    private void RibbonWindow_Closed(object sender, EventArgs e)    {        ShutDownTransport();        //DisconnectRecorderNotes();        Application.Current.Shutdown();    }    //private bool _closingFromSystemMenu = false;    private void Window_Closing(object sender, CancelEventArgs e)    {        /*if (!_closingFromSystemMenu && !CoreUtils.GetVersion().Equals("???"))        {            WindowState = WindowState.Minimized;            e.Cancel = true;            return;        }*/        PanelHost.UnloadPanel(e);        if (!e.Cancel)        {            ISubPanelHost.Global.ShutdownSubPanels(e);        }        if (e.Cancel)        {            return;        }        App.IsClosing = true;        FinalizeAutoTimesheet();        if (!CoreUtils.GetVersion().Equals("???"))            scheduler.Stop();        CurrentTab = null;        UpdateRibbonColors();        if (!CoreUtils.GetVersion().Equals("???"))            if (station.ID != Guid.Empty)                ExecuteLogout();        ShutDownTransport();    }    #region Notifications + Heartbeat    private void ReceiveNotification(Notification notification)    {        if (Security.CanView<Notification>())        {            Notifications.AddNotification(notification);        }        foreach(var panel in Panels.OfType<NotificationPanel>())        {            panel.AddNotification(notification);        }    }    private void Notifications_Tick(object? sender, EventArgs e)    {        if (ClientFactory.UserGuid != Guid.Empty)        {            try            {                ReloadNotifications();            }            catch (Exception err)            {                Logger.Send(LogType.Error, ClientFactory.UserID,                    string.Format("Exception in Notifications_Tick:ReloadNotifications() {0}\n{1}", err.Message, err.StackTrace));            }            Heartbeat();            try            {                CheckIsLoggedOn();            }            catch (Exception err2)            {                Logger.Send(LogType.Error, ClientFactory.UserID,                    string.Format("Exception in Notifications_Tick:CheckIsLoggedOn() {0}\n{1}", err2.Message, err2.StackTrace));            }        }        //else        //{        //    Logger.Send(LogType.Information, ClientFactory.UserID, "Notifications_Tick: ClientFactory.UserGuid is empty");        //}    }    private void CheckIsLoggedOn()    {        if (CoreUtils.GetVersion().Equals("???"))            return;        var bLogout = false;        if (!Security.IsAllowed<CanBypassTimeBench>())            if (!IsClockedOn())            {                Logger.Send(LogType.Information, ClientFactory.UserID, "User is no longer clocked in!");                bLogout = true;                Dispatcher.Invoke(() => { Logout(); });            }        if (!bLogout)            new Client<Login>().Query(                new Filter<Login>(x => x.User.ID).IsEqualTo(ClientFactory.UserGuid),                Columns.None<Login>().Add(x => x.StationID),                null,                CoreRange.All,                (o, e) =>                {                    if (e is RemoteException remote)                    {                        if (remote.Status == StatusCode.Unauthenticated)                        {                            Logger.Send(LogType.Information, ClientFactory.UserID, "User has been logged out");                            Dispatcher.Invoke(() =>                            {                                Logout("You have been logged out due to inactivity");                            });                        }                        else                        {                            Logger.Send(LogType.Information, ClientFactory.UserID, $"Error in CheckIsLoggedOn(): {CoreUtils.FormatException(remote)}");                        }                    }                    else if (e is not null)                    {                        Logger.Send(LogType.Information, ClientFactory.UserID, $"Error in CheckIsLoggedOn(): {CoreUtils.FormatException(e)}");                    }                    else if (o is not null)                    {                        var row = o.Rows.FirstOrDefault();                        if (row == null)                        {                            station.ID = Guid.Empty;                            new Client<Login>().Save(station, "", (o1, e1) => { });                        }                        else if (!row.Get<Login, string>(c => c.StationID).Equals(station.StationID))                        {                            Logger.Send(LogType.Information, ClientFactory.UserID, "User logged in somewhere else!");                            bLogout = true;                            Dispatcher.Invoke(() => { Logout(); });                        }                    }                }            );    }    private void Heartbeat()    {        //Task.Run(() =>        //{        try        {            CheckSupportTicketStatus();                        bool IsClockedOn = this.IsClockedOn();            if (IsClockedOn)            {                if ((_kanbantrackingassignment != null) && (_kanbantrackingassignment.Actual.Finish < DateTime.Now.TimeOfDay))                {                    _kanbantrackingassignment.Actual.Finish = DateTime.Now.TimeOfDay;                    new Client<Assignment>().Save(_kanbantrackingassignment, "");                }                if (Security.IsAllowed<MonitorApplicationWindows>())                {                    if (ActivityHistory == null)                        ActivityHistory = new LocalConfiguration<DailyActivityHistory>().Load();                    var appname = OpenWindowGetter.GetActiveWindowProcess();                    var title = OpenWindowGetter.GetActiveWindowTitle();                    ActivityHistory.Activities[DateTime.Now] = (!string.IsNullOrWhiteSpace(appname) ? appname.Trim() + " - " : "") + title;                    new LocalConfiguration<DailyActivityHistory>().Save(ActivityHistory);                }            }            PanelHost.Heartbeat();            foreach(var window in SecondaryWindows.Values)            {                window.Heartbeat();            }        }        catch (Exception err)        {            Logger.Send(LogType.Error, ClientFactory.UserID, string.Format("Exception in Heartbeat: {0}\n{1}", err.Message, err.StackTrace));        }        //});    }    private void CheckSupportTicketStatus()    {        Dispatcher.BeginInvoke(() =>        {            try            {                IssuesButton.Background = IssuesWindow.Check()                    ? new SolidColorBrush(Colors.Red) { Opacity = 0.5 }                    : Brushes.Transparent;            }            catch (Exception e)            {                            }        });    }    private void Notifications_Changed(object sender)    {        if (Notifications.IsActive)        {            Notifications.Visibility = Visibility.Visible;            DockingGrid.ColumnDefinitions[1].Width = new GridLength(4, GridUnitType.Pixel);            DockingGrid.ColumnDefinitions[2].Width = Equals(0.0, DockingGrid.ColumnDefinitions[2].Width.Value)                ? new GridLength(300, GridUnitType.Pixel)                : DockingGrid.ColumnDefinitions[2].Width;        }        else        {            Notifications.Visibility = Visibility.Collapsed;            DockingGrid.ColumnDefinitions[1].Width = new GridLength(0, GridUnitType.Pixel);            DockingGrid.ColumnDefinitions[2].Width = new GridLength(0, GridUnitType.Pixel);        }    }    private void ReloadNotifications()    {        if (Security.CanView<Notification>())            Notifications.Refresh();        if (!NotificationsWatchDog.IsEnabled)        {            //Logger.Send(LogType.Information, ClientFactory.UserID, "Enabling Heartbeat");            NotificationsWatchDog.IsEnabled = true;        }    }    #endregion    private void RibbonWindow_PreviewMouseUp(object sender, MouseButtonEventArgs e)    {        PanelHost.IncrementTrackingModuleClick();    }    private void RibbonWindow_PreviewKeyUp(object sender, KeyEventArgs e)    {        PanelHost.IncrementTrackingModuleKey();    }    public static void ActivateWindow(Window window)    {        var hwnd = new WindowInteropHelper(window).EnsureHandle();        var threadId1 = GetWindowThreadProcessId(GetForegroundWindow(), IntPtr.Zero);        var threadId2 = GetWindowThreadProcessId(hwnd, IntPtr.Zero);        if (threadId1 != threadId2)        {            AttachThreadInput(threadId1, threadId2, true);            SetForegroundWindow(hwnd);            AttachThreadInput(threadId1, threadId2, false);        }        else        {            SetForegroundWindow(hwnd);        }    }    private static IntPtr GetForegroundWindow()    {        var active = GetActiveWindow();        var window = Application.Current.Windows.OfType<Window>().SingleOrDefault(x => new WindowInteropHelper(x).Handle == active);        var hwnd = new WindowInteropHelper(window).EnsureHandle();        return hwnd;    }    [DllImport("user32.dll", SetLastError = true)]    private static extern IntPtr SetForegroundWindow(IntPtr hWnd);    [DllImport("user32.dll")]    private static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);    [DllImport("user32.dll")]    private static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);    [DllImport("user32.dll")]    private static extern IntPtr GetActiveWindow();    #region Modules + Reports    public void ClearActions()    {        foreach (var module in CurrentModules)        {            if (module.Parent is Fluent.RibbonGroupBox bar)                bar.Items.Remove(module);        }        CurrentModules.Clear();    }    public void ClearReports()    {        if (CurrentTab is not null)        {            var ReportsBar = FindRibbonBar(CurrentTab, x => x.Header.Equals("Print"));            if (ReportsBar is not null)            {                ReportsBar.Visibility = Visibility.Collapsed;                ReportsBar.Items.Clear();            }        }    }    public void CreateReport(PanelAction action)    {        if (CurrentTab is null)            return;        var ReportsBar = FindRibbonBar(CurrentTab, x => x.Header.Equals("Print"));        if (ReportsBar is not null)        {            var button = new Fluent.Button            {                Header = action.Caption,                LargeIcon = action.Image?.AsBitmapImage(),                MinWidth = 60            };            if (action.Menu is not null)            {                button.ContextMenu = action.Menu;            }            button.Click += (o, e) =>            {                action.Execute();            };            ReportsBar.Visibility = Security.IsAllowed<CanPrintReports>() ? Visibility.Visible : Visibility.Collapsed;            ReportsBar.Items.Add(button);        }    }    private NullConverter<Visibility>? _vis = null;        public void CreatePanelAction(PanelAction action)    {        if (CurrentTab is null)            return;        if (_vis == null)        {            _vis = new();            _vis.Converting += (o, e) =>            {            };        }                var Actions = FindRibbonBar(CurrentTab, x => x.Header.Equals("Actions"));        if (Actions is not null)        {            if (!CurrentModules.Any(x => x.GetType().Equals(typeof(RibbonSeparator))))            {                var sep = new RibbonSeparator();                CurrentModules.Add(sep);                Actions.Items.Add(sep);            }            if (action.OnPopulate != null)            {                Fluent.DropDownButton button;                if (action.OnExecute != null)                {                    button = new Fluent.SplitButton();                    ((Fluent.SplitButton)button).Click += (o, e) => action.Execute();                }                else                    button = new Fluent.DropDownButton();                                button.MinWidth = 60;                button.DataContext = action;                button.DropDownOpened += (sender, args) => { button.ItemsSource = CreateMenuItems(action); };                button.Bind<PanelAction, String>(Fluent.DropDownButton.HeaderProperty, x => x.Caption, null);                button.Bind<PanelAction, Bitmap?>(Fluent.DropDownButton.LargeIconProperty, x => x.Image,                    new BitmapToBitmapImageConverter());                button.Bind<PanelAction, ContextMenu?>(ContextMenuProperty, x => x.Menu);                button.Bind<PanelAction, bool>(IsEnabledProperty, x => x.IsEnabled);                button.Bind<PanelAction, Visibility>(Fluent.DropDownButton.VisibilityProperty, x => x.Visibility, _vis);                                    Actions.Items.Add(button);                CurrentModules.Add(button);            }                        else            {                var button = new Fluent.Button()                {                    MinWidth = 60,                    DataContext = action                };                button.Bind<PanelAction, String>(Fluent.Button.HeaderProperty, x => x.Caption, null);                button.Bind<PanelAction, Bitmap?>(Fluent.Button.LargeIconProperty, x => x.Image,                    new BitmapToBitmapImageConverter());                button.Bind<PanelAction, ContextMenu?>(Fluent.Button.ContextMenuProperty, x => x.Menu);                button.Bind<PanelAction, bool>(Fluent.Button.IsEnabledProperty, x => x.IsEnabled);                button.Bind<PanelAction, Visibility>(Fluent.Button.VisibilityProperty, x => x.Visibility, _vis);                button.Click += (o, e) => action.Execute();                Actions.Items.Add(button);                CurrentModules.Add(button);            }        }    }    private static ObservableCollection<MenuItem> CreateMenuItems(PanelAction action)    {        var items = new ObservableCollection<MenuItem>();        var entries = action.Populate();        if (entries != null)        {            foreach (var entry in entries)            {                var item = new Fluent.MenuItem()                {                    Header = entry.Caption,                    DataContext = entry.Data,                    Icon = entry.Image?.AsBitmapImage()                };                item.Click += (o, eventArgs) => entry.Execute();                items.Add(item);            }        }        return items;    }    #endregion    #region Tracking Kanban    private class TrackingKanbanFilterItemComponent : DynamicGridFilterComponent<Kanban>    {        public TrackingKanbanFilterItemComponent() : base(            new GlobalConfiguration<CoreFilterDefinitions>(nameof(Kanban)),            new UserConfiguration<CoreFilterDefinitions>(nameof(Kanban)))        {            ButtonText = "Filter";        }        public string Text { get; private set; } = "Filter";        public Bitmap? Image { get; private set; }        protected override void UpdateButtonText(Bitmap image, string text)        {            Text = text;            Image = image;        }    }    private TrackingKanbanFilterItemComponent? _trackingKanbanFilterComponent;    private TrackingKanbanFilterItemComponent TrackingKanbanFilterComponent    {        get        {            if(_trackingKanbanFilterComponent is null)            {                _trackingKanbanFilterComponent = new();                _trackingKanbanFilterComponent.OnFiltersSelected += _trackingKanbanFilterComponent_OnFiltersSelected;            }            return _trackingKanbanFilterComponent;        }    }    private ContextMenu? _trackingKanbanMenu;    private MenuItem? _trackingKanbanFilterMenu;    private void _trackingKanbanFilterComponent_OnFiltersSelected(DynamicGridSelectedFilterSettings filters)    {        if (_trackingKanbanMenu is null) return;        if (!filters.MultipleFilters)        {            _trackingKanbanMenu.IsOpen = false;        }        if(_trackingKanbanFilterMenu is not null)        {            _trackingKanbanFilterMenu.Header = TrackingKanbanFilterComponent.Text;            _trackingKanbanFilterMenu.Icon = new System.Windows.Controls.Image() { Source = TrackingKanbanFilterComponent.Image?.AsBitmapImage(24, 24) };        }    }    private void SelectTask_Click(object sender, RoutedEventArgs e)    {        var menu = new ContextMenu();        var others = new MenuItem() { Header = "Other Tasks" };        var waiting = new MenuItem() { Header = "Waiting Tasks" };        using (new WaitCursor())        {            var filter =                new Filter<KanbanSubscriber>(x => x.Employee.UserLink.ID).IsEqualTo(ClientFactory.UserGuid)                    .And(x => x.Kanban.Completed).IsEqualTo(DateTime.MinValue)                    .And(x => x.Kanban.Closed).IsEqualTo(DateTime.MinValue);            var kanbanFilter = TrackingKanbanFilterComponent.GetFilter();            if(kanbanFilter is not null)            {                filter = filter.And(x => x.Kanban.ID).InQuery(kanbanFilter, x => x.ID);            }            var kanbans = Client.Query(                filter,                Columns.None<KanbanSubscriber>()                    .Add(x => x.Kanban.ID)                    .Add(x => x.Kanban.Number)                    .Add(x => x.Kanban.Title)                    .Add(x => x.Kanban.Status)                    .Add(x => x.Assignee),                new SortOrder<KanbanSubscriber>(x => x.Kanban.Number, SortDirection.Ascending));            foreach (var subscriber in kanbans.ToObjects<KanbanSubscriber>())            {                CreateTaskMenu(subscriber.Assignee ? (subscriber.Kanban.Status != KanbanStatus.Waiting ? menu : waiting) : others,                    $"{subscriber.Kanban.Number} {subscriber.Kanban.Title}",                    subscriber.Kanban.ID);            }            menu.AddSeparatorIfNeeded();            if(others.Items.Count > 0)            {                menu.Items.Add(others);            }            if(waiting.Items.Count > 0)            {                menu.Items.Add(waiting);            }            menu.AddSeparatorIfNeeded();            CreateTaskMenu(menu, "(No Task Selected)", Guid.Empty);            if(_kanbantrackingassignment is not null && _kanbantrackingassignment.Task.ID != Guid.Empty)            {                menu.AddItem("View Task", null, _kanbantrackingassignment.Task.ID, ViewTrackingKanban_Click);            }            menu.AddSeparatorIfNeeded();            var filterItem = menu.AddItem(TrackingKanbanFilterComponent.Text, TrackingKanbanFilterComponent.Image, null);            TrackingKanbanFilterComponent.PopulateMenu(filterItem);            _trackingKanbanFilterMenu = filterItem;        }        menu.IsOpen = true;        _trackingKanbanMenu = menu;    }    private void ViewTrackingKanban_Click(Guid guid)    {        var item = Client.Query(            new Filter<Kanban>(x => x.ID).IsEqualTo(guid),            DynamicGridUtils.LoadEditorColumns<Kanban>())            .ToObjects<Kanban>().FirstOrDefault();        if (item is null) return;        var grid = DynamicGridUtils.CreateDynamicGrid<Kanban>(typeof(DynamicGrid<>));        grid.EditItemsNonModal(ISubPanelHost.Global, [item], (items, saved) => { });    }    private Assignment? _kanbantrackingassignment = null;    private CoreFilterDefinitions? _kanbanTrackingFilter = null;    private void SetTrackingKanban(Guid kanbanID, string header)    {        SelectedTaskName.Content = header;        var createNewAssignment = false;        if (_kanbantrackingassignment is not null)        {            if (_kanbantrackingassignment.Actual.Finish < DateTime.Now.TimeOfDay)            {                _kanbantrackingassignment.Actual.Finish = DateTime.Now.TimeOfDay;                Client.Save(_kanbantrackingassignment, "");            }            // Update Existing Kanban            if (kanbanID == Guid.Empty)                _kanbantrackingassignment = null;            else if (_kanbantrackingassignment.Task.ID != kanbanID)            {                createNewAssignment = true;            }        }        else if (kanbanID != Guid.Empty)        {            createNewAssignment = true;        }        if (createNewAssignment)        {            _kanbantrackingassignment = new Assignment();            _kanbantrackingassignment.Task.ID = kanbanID;            _kanbantrackingassignment.EmployeeLink.ID = App.EmployeeID;            _kanbantrackingassignment.Title = header;            _kanbantrackingassignment.Date = DateTime.Today;            _kanbantrackingassignment.Actual.Start = DateTime.Now.TimeOfDay;            _kanbantrackingassignment.Actual.Finish = DateTime.Now.TimeOfDay.Add(new TimeSpan(0, 2, 0));            Client.Save(_kanbantrackingassignment, "");        }    }    private void ClearTrackingKanban()        => SetTrackingKanban(Guid.Empty, "(No Task Selected)");    private void CreateTaskMenu(ItemsControl menu, string title, Guid id)    {        menu.AddCheckMenuItem(title, (title, id), TrackingKanbanMenuItem_Click, isChecked: _kanbantrackingassignment?.Task.ID == id);    }    private void TrackingKanbanMenuItem_Click((string title, Guid id) tuple, bool isChecked)    {        if (isChecked)        {            SetTrackingKanban(tuple.id, tuple.title);        }    }    #endregion    private void DockPanelBorder_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)    {        if (sender is not Border border || !border.IsVisible)            return;        var dock = border.Child is IDockPanel panel ? panel : border.Child?.FindVisualChildren<IDockPanel>().FirstOrDefault();        if (dock is null)            return;        dock.Refresh();    }    private void DockPanel_OnIsActiveChanged(object? sender, EventArgs e)    {        if (sender is not LayoutAnchorable layout)            return;        var content = layout.Content as DependencyObject;        var dock = content is IDockPanel panel ? panel : content?.FindVisualChildren<IDockPanel>().FirstOrDefault();        if (dock is null)            return;        if (layout.IsActive && layout.IsVisible)            dock.Refresh();    }    private void _ribbon_OnPreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)    {        e.Handled = true;    }    #region Backstage Functions    private enum DatabaseConfigurationResult    {        RestartRequired,        RestartNotRequired,        Cancel    }    private DatabaseConfigurationResult ShowDatabaseConfiguration()    {        DatabaseConfigurationResult result;        var config = new DataBaseConfiguration();        if (config.ShowDialog() == true)        {            var newsettings = new LocalConfiguration<DatabaseSettings>(App.Profile).Load();            if (newsettings.RestartRequired(App.DatabaseSettings))            {                result = DatabaseConfigurationResult.RestartRequired;            }            else            {                result = DatabaseConfigurationResult.RestartNotRequired;            }            if ((newsettings.DatabaseType == DatabaseType.Standalone) && (newsettings.ColorScheme != App.DatabaseSettings.ColorScheme))            {                App.DatabaseSettings.ColorScheme = newsettings.ColorScheme;                ApplyColorScheme();            }        }        else        {            result = DatabaseConfigurationResult.Cancel;        }        return result;    }    private void DatabaseSettings_OnClick(object sender, RoutedEventArgs e)    {        switch (ShowDatabaseConfiguration())        {            case DatabaseConfigurationResult.RestartRequired:                MessageBox.Show("Please restart the application to apply these changes!");                break;        }    }    private void CompanyInformation_OnClick(object sender, RoutedEventArgs e)    {        var info = new Client<CompanyInformation>().Load().FirstOrDefault();        if (info == null)            info = new CompanyInformation();        new DynamicDataGrid<CompanyInformation>().EditItems(new[] { info });    }    private void SecurityDefaultsButton_OnClick(object sender, RoutedEventArgs e)    {        var window = new GlobalTokenWindow();        window.ShowDialog();        Security.Reset();    }    private void SystemLogsButton_OnClick(object sender, RoutedEventArgs e)    {        var logfile = CoreUtils.GetLogFile();        if (File.Exists(logfile))        {            var startInfo = new ProcessStartInfo("notepad.exe", logfile);            startInfo.Verb = "open";            startInfo.UseShellExecute = true;            Process.Start(startInfo);        }        else        {            MessageBox.Show(logfile + " does not exist!");        }    }        private void CheckForUpdates_OnClick(object sender, RoutedEventArgs e)    {        if (SupportUtils.CheckForUpdates())        {            Close();        }        else        {            if (MessageWindow.ShowYesNo(                    "You appear to be using the latest version already!\n\nRun the installer anyway?", "Update"))            {                if (SupportUtils.DownloadAndRunInstaller())                {                    Close();                }            }        }    }        private void OpenSupportSession_OnClick(object sender, RoutedEventArgs e)    {        SupportUtils.OpenSupportSession();    }    private void DocumentTypeList_OnClick(object sender, RoutedEventArgs e)    {        var list = new MasterList(typeof(DocumentType));        list.ShowDialog();    }    private void EventList_Click(object sender, RoutedEventArgs e)    {        var list = new MasterList(typeof(Event));        list.ShowDialog();    }    private void EditDetailsButton_OnClick(object sender, RoutedEventArgs e)    {        var employee = new Client<Employee>().Query(                new Filter<Employee>(x => x.UserLink.ID).IsEqualTo(ClientFactory.UserGuid))            .Rows.FirstOrDefault()?.ToObject<Employee>();        var item = new MyDetailsConfiguration(employee);        item.User = ClientFactory.UserGuid;        var buttons = new DynamicEditorButtons();        buttons.Add("Change Password", null, item, (sender, item) =>        {            var details = (item as MyDetailsConfiguration)!;            var changePassword = new ChangePassword(details.User);            if (changePassword.ShowDialog() == true && changePassword.Password != null)            {                var newUser = new User();                newUser.SetID(details.User);                ChangePassword.ChangeUserPassword(newUser, changePassword.Password);                new Client<User>().Save(newUser, "Changed Password");                MessageBox.Show("Password changed!");            }        });        var editor = new DynamicEditorForm(typeof(MyDetailsConfiguration), buttons: buttons);        if (employee == null)        {            editor.OnFormCustomiseEditor += (sender, items, column, editor) =>            {                editor.Editable = Editable.Disabled;            };        }        editor.Items = new BaseObject[] { item };        if (editor.ShowDialog() == true)        {            if (employee != null)            {                item.SaveTo(employee);                new Client<Employee>().Save(employee, "Edited by user");            }        }    }    private void LogoutButton_OnClick(object sender, RoutedEventArgs e)    {        Logout();    }    private void LoginButton_OnClick(object sender, RoutedEventArgs e)    {        if (DoLogin() == ValidationStatus.VALID)            AfterLogin(null);    }    private void ExitButton_OnClick(object sender, RoutedEventArgs e)    {        //_closingFromSystemMenu = true;        Close();    }    #endregion}
 |