| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448 | using System;using System.Collections.Generic;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.Core.Reports;using InABox.IPC;using InABox.Rpc;using InABox.Scripting;using InABox.Wpf.Console;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;using Syncfusion.Windows.Tools.Controls;using Activity = Comal.Classes.Activity;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 Role = Comal.Classes.Role;using SortDirection = InABox.Core.SortDirection;using PRSDesktop.Components.Spreadsheet;using InABox.Wpf.Reports;using Comal.Classes.SecurityDescriptors;namespace PRSDesktop;/// <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);        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()})";        CheckForUpdates();        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)),                Task.Run(() => CoreUtils.RegisterClasses()),                Task.Run(() => ComalUtils.RegisterClasses()),                Task.Run(() => PRSSharedUtils.RegisterClasses()),                Task.Run(() => ReportUtils.RegisterClasses()),                Task.Run(() => ConfigurationUtils.RegisterClasses()),                Task.Run(() => DynamicGridUtils.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());            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 != null)            MessageBox.Show(startupException.Message);        if (DoLogin(App.DatabaseSettings.Autologin) == ValidationStatus.VALID)        {            AfterLogin();        }        ProfileName.Content = App.Profile;        URL.Content = GetDatabaseConnectionDescription();        Progress.ShowModal("Starting Up", progress =>        {            if (loginStatus == ValidationStatus.VALID && DatabaseType == DatabaseType.Standalone)            {                progress.Report("Starting Scheduler");                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 cancelled = false;                var success = Progress.ShowModal("Reconnecting", "Exit PRS", (progress, ct) =>                {                    try                    {                        DateTime lost = DateTime.Now;                        while (!client.IsConnected() && !ct.IsCancellationRequested)                        {                            progress.Report($"Connection lost - ({(DateTime.Now - lost):hh\\:mm})");                            try                            {                                Logger.Send(LogType.Error, ClientFactory.UserID, "Reconnecting - ({0:hh\\:mm})", DateTime.Now - lost);                                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!"))                                {                                    cancelled = true;                                    break;                                }                            }                        }                        if (client.IsConnected())                        {                            Logger.Send(LogType.Information, ClientFactory.UserID, "Reconnected");                            ClientFactory.Validate(ClientFactory.SessionID);                            Logger.Send(LogType.Information, ClientFactory.UserID, "Validated");                        }                    }                    catch (Exception e)                    {                        Logger.Send(LogType.Error, ClientFactory.UserID, $"Reconnect Failed: {e.Message}");                    }                });                if (!success || cancelled)                {                    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;        BaseDynamicGrid.SelectionBackground = ThemeManager.SelectionBackgroundBrush;        BaseDynamicGrid.SelectionForeground = ThemeManager.SelectionForegroundBrush;        BaseDynamicGrid.FilterBackground = ThemeManager.FilterBackgroundBrush;        //_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()    {        SetupScreen();        var bMaps = Security.CanView<Equipment>()                    || Security.CanView<Job>()                    || Security.CanView<TimeSheet>()                    || Security.CanView<GPSTracker>();        Progress.ShowModal(            new ProgressSection("Configuring Main Screen", SetupMainScreen),            new ProgressSection("Configuring Quotes Screen", () => SetupQuotesTab(bMaps)),            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)        );    }    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        SetVisibility(SendNotification, Security.CanView<Notification>());        SetVisibility(Notifications, Security.CanView<Notification>());        SetVisibility(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()    {        SetVisibility(CompanyInformation, Security.CanView<CompanyInformation>());        SetVisibleIfAny(BackstageSeparator0, CompanyInformation);        SetVisibility(SecurityDefaultsButton,            ClientFactory.IsSupported<GlobalSecurityToken>() && Security.IsAllowed<CanCustomiseSecurityDefaults>());        SetVisibleIfAny(BackstageSeparator1, SecurityDefaultsButton);        BackstageSeparator1a.Visibility = Visibility.Visible;        SystemLogsButton.Visibility = Visibility.Visible;        SetVisibility(DocumentTypeList, ClientFactory.IsSupported<DocumentType>() && Security.IsAllowed<CanViewDocumentTypes>());        SetVisibleIfAny(BackstageSeparator2, DocumentTypeList);        SetVisibility(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);        _ribbon.InvalidateArrange();    }    private void SetupDashboardsTab(bool bMaps)    {        if (!Security.IsAllowed<ViewDesktopDashboardsTab>())            return;        SetVisibility(DashboardsDashboardButton, Security.IsAllowed<CanViewUserDefinedDashboards>());        SetVisibility(DashboardMessagesButton, Security.CanView<Notification>());        SetVisibility(DashboardsTaskButton, ClientFactory.IsSupported<Kanban>() && Security.IsAllowed<CanViewTasks>());        SetVisibility(DashboardsAttendanceButton, ClientFactory.IsSupported<TimeSheet>() && Security.IsAllowed<CanViewInOutBoard>());        SetVisibility(DashboardsMapButton, bMaps);        SetVisibility(DashboardsDailyReportButton,            ClientFactory.IsSupported<TimeSheet, Assignment>() && Security.IsAllowed<CanViewDailyReports>());        SetVisibility(FactoryProductivityButton,            ClientFactory.IsSupported<ManufacturingHistory>()             && Security.IsAllowed<CanViewFactoryKPIs>()            && Security.IsAllowed<ViewDesktopFactoryKPIsDashboard>());        SetVisibility(TemplateAnalysisButton,            ClientFactory.IsSupported<ManufacturingTemplate, ManufacturingHistory>()             && Security.IsAllowed<CanViewTemplateAnalysis>()            && Security.IsAllowed<ViewDesktopTemplateAnalysisDashboard>());        SetVisibility(FactoryAnalysisButton,            ClientFactory.IsSupported<ManufacturingTemplate, ManufacturingHistory>()             && Security.IsAllowed<CanViewFactoryAnalysis>()            && Security.IsAllowed<ViewDesktopFactoryAnalysisDashboard>());        SetVisibility(DatabaseActivityButton,            ClientFactory.IsSupported<UserTracking>()             && Security.IsAllowed<CanViewDatabaseActivity>()            && Security.IsAllowed<ViewDesktopDatabaseActivityDashboard>());        SetVisibility(UserActivityButton, ClientFactory.IsSupported<ModuleTracking>()             && Security.IsAllowed<CanViewUserActivity>()            && Security.IsAllowed<ViewDesktopUserActivityDashboard>());        SetVisibility(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;        SetVisibility(DigitalFormsDashboardButton, Security.IsAllowed<CanViewUserDefinedDashboards>());        SetVisibility(DigitalFormsMessagesButton, Security.CanView<Notification>());        SetVisibility(DigitalFormsTaskButton, ClientFactory.IsSupported<Kanban>() && Security.IsAllowed<CanViewTasks>());        SetVisibility(DigitalFormsAttendanceButton, ClientFactory.IsSupported<TimeSheet>() && Security.IsAllowed<CanViewInOutBoard>());        SetVisibility(DigitalFormsMapButton, bMaps);        SetVisibility(DigitalFormsDailyReportButton,            ClientFactory.IsSupported<TimeSheet, Assignment>() && Security.IsAllowed<CanViewDailyReports>());        SetVisibility(DigitalFormsFormsLibraryButton, ClientFactory.IsSupported<DigitalForm>()            && Security.CanView<DigitalForm>()            && Security.IsAllowed<CanAdministerDigitalFormsLibrary>());        SetVisibility(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;        SetVisibility(EquipmentDashboardButton, Security.IsAllowed<CanViewUserDefinedDashboards>());        SetVisibility(EquipmentMessagesButton, Security.CanView<Notification>());        SetVisibility(EquipmentTaskButton, ClientFactory.IsSupported<Kanban>() && Security.IsAllowed<CanViewTasks>());        SetVisibility(EquipmentAttendanceButton, ClientFactory.IsSupported<TimeSheet>() && Security.IsAllowed<CanViewInOutBoard>());        SetVisibility(EquipmentMapButton, bMaps);        SetVisibility(EquipmentDailyReportButton,            ClientFactory.IsSupported<TimeSheet, Assignment>() && Security.IsAllowed<CanViewDailyReports>());        SetVisibility(EquipmentButton, ClientFactory.IsSupported<Equipment>()             && Security.CanView<Equipment>()            && Security.IsAllowed<ViewDesktopEquipmentListScreen>());        SetVisibility(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);        SetVisibility(TrackersMasterList, Security.CanView<GPSTracker>() && Security.IsAllowed<ViewDesktopGPSTrackersScreen>());        SetTabVisibleIfAny(EquipmentTab, EquipmentButton, TrackersMasterList);    }    private void SetupAccountsTab(bool bMaps)    {        if (!Security.IsAllowed<ViewDesktopAccountsTab>())            return;        SetVisibility(AccountsDashboardButton, Security.IsAllowed<CanViewUserDefinedDashboards>());        SetVisibility(AccountsMessagesButton, Security.CanView<Notification>());        SetVisibility(AccountsTaskButton, ClientFactory.IsSupported<Kanban>() && Security.IsAllowed<CanViewTasks>());        SetVisibility(AccountsAttendanceButton, ClientFactory.IsSupported<TimeSheet>() && Security.IsAllowed<CanViewInOutBoard>());        SetVisibility(AccountsMapButton, bMaps);        SetVisibility(AccountsDailyReportButton,            ClientFactory.IsSupported<TimeSheet, Assignment>() && Security.IsAllowed<CanViewDailyReports>());        SetVisibility(CustomerList, ClientFactory.IsSupported<Customer>()             && Security.CanView<Customer>()            && Security.IsAllowed<ViewDesktopCustomersScreen>());        SetVisibility(InvoiceList, ClientFactory.IsSupported<Invoice>()             && Security.CanView<Invoice>()            && Security.IsAllowed<ViewDesktopInvoicesScreen>());        SetVisibility(ReceiptList, ClientFactory.IsSupported<Receipt>()             && Security.CanView<Receipt>()            && Security.IsAllowed<ViewDesktopReceiptsScreen>());        SetVisibility(SupplierList, ClientFactory.IsSupported<Supplier>()             && Security.CanView<Supplier>()            && Security.IsAllowed<ViewDesktopSuppliersScreen>());        SetVisibility(AccountsDataButton, Security.IsAllowed<CanViewDataEntryPanel>());        SetVisibility(PurchasesList, ClientFactory.IsSupported<PurchaseOrder>()             && Security.CanView<PurchaseOrder>()            && Security.IsAllowed<ViewDesktopPurchaseOrdersScreen>());        SetVisibility(BillsList, ClientFactory.IsSupported<Bill>()             && Security.CanView<Bill>()            && Security.IsAllowed<ViewDesktopBillsScreen>());        SetVisibility(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;        SetVisibility(HumanResourcesDashboardButton, Security.IsAllowed<CanViewUserDefinedDashboards>());        SetVisibility(HumanResourcesMessagesButton, Security.CanView<Notification>());        SetVisibility(HumanResourcesTaskButton, ClientFactory.IsSupported<Kanban>() && Security.IsAllowed<CanViewTasks>());        SetVisibility(HumanResourcesAttendanceButton,            ClientFactory.IsSupported<TimeSheet>() && Security.IsAllowed<CanViewInOutBoard>());        SetVisibility(HumanResourcesMapButton, bMaps);        SetVisibility(HumanResourcesDailyReportButton,            ClientFactory.IsSupported<TimeSheet, Assignment>() && Security.IsAllowed<CanViewDailyReports>());        SetVisibility(CalendarButton, Security.CanView<Assignment>() && Security.IsAllowed<ViewDesktopCalendarScreen>());        SetVisibility(EmployeePlannerButton, Security.CanView<Assignment>() && Security.IsAllowed<ViewDesktopEmployeePlannerScreen>());        SetVisibility(TimesheetsButton, Security.CanView<TimeSheet>() && Security.IsAllowed<ViewDesktopStaffTimeSheetsScreen>());        SetVisibility(LeaveRequestsButton, Security.CanView<LeaveRequest>() && Security.IsAllowed<ViewDesktopLeaveRequestsScreen>());        SetVisibility(OrgChartButton,            ClientFactory.IsSupported<Employee>()            &&             Security.IsAllowed<ViewDesktopOrgChartScreen>()        );        SetVisibility(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);        SetVisibility(UsersButton, Security.CanView<User>() && Security.IsAllowed<ViewDesktopUserAccountsScreen>());        SetVisibility(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;        SetVisibility(ProductsDashboardButton, Security.IsAllowed<CanViewUserDefinedDashboards>());        SetVisibility(ProductsMessagesButton, Security.CanView<Notification>());        SetVisibility(ProductsTaskButton, Security.CanView<Kanban>());        SetVisibility(ProductsAttendanceButton, Security.IsAllowed<CanViewInOutBoard>());        SetVisibility(ProductsMapButton, bMaps);        SetVisibility(ProductsDailyReportButton,            ClientFactory.IsSupported<TimeSheet, Assignment>() && Security.IsAllowed<CanViewDailyReports>());        SetVisibility(ProductsMasterList, Security.CanView<Product>() && Security.IsAllowed<ViewDesktopProductListScreen>());        SetVisibility(StockLocationList, Security.CanView<StockLocation>() && Security.IsAllowed<ViewDesktopStockLocationsScreen>());        SetVisibility(StockMovementList, Security.CanView<StockMovement>() && Security.IsAllowed<ViewDesktopStockMovementsScreen>());        SetVisibility(StockSummaryButton, Security.CanView<Product>()            && Security.CanView<JobMaterial>()            && Security.IsAllowed<ViewDesktopStockForecastScreen>());        SetVisibility(ReservationManagementButton, Security.IsAllowed<ViewDesktopReservationManagementScreen>());        SetVisibleIfEither(ProductsTaskSeparator,            new FrameworkElement[]            {                            ProductsDashboardButton, ProductsMessagesButton, ProductsTaskButton, ProductsAttendanceButton, ProductsMapButton,                            ProductsDailyReportButton            }, new FrameworkElement[] { ProductsMasterList, StockLocationList, StockMovementList, StockSummaryButton });        SetVisibleIfAny(ProductActions, ProductsMasterList, StockLocationList, StockMovementList, StockSummaryButton);        SetTabVisibleIfAny(ProductTab, ProductActions);    }    private void SetupLogisticsTab(bool bMaps)    {        if (!Security.IsAllowed<ViewDesktopLogisticsTab>())            return;        SetVisibility(LogisticsDashboardButton, Security.IsAllowed<CanViewUserDefinedDashboards>());        SetVisibility(LogisticsMessagesButton, Security.CanView<Notification>());        SetVisibility(LogisticsTaskButton, ClientFactory.IsSupported<Kanban>() && Security.IsAllowed<CanViewTasks>());        SetVisibility(LogisticsAttendanceButton, ClientFactory.IsSupported<TimeSheet>() && Security.IsAllowed<CanViewInOutBoard>());        SetVisibility(LogisticsMapButton, bMaps);        SetVisibility(LogisticsDailyReportButton,            ClientFactory.IsSupported<TimeSheet, Assignment>() && Security.IsAllowed<CanViewDailyReports>());        SetVisibility(ReadyToGoItemsButton,            ClientFactory.IsSupported<DeliveryItem>()            && Security.IsAllowed<CanViewLogisticsReadyToGo>()            && Security.IsAllowed<ViewDesktopReadyToGoScreen>());        SetVisibility(DispatchButton, Security.CanView<Shipment>()            && Security.CanView<DeliveryItem>()            && Security.IsAllowed<ViewDesktopRackListScreen>());        SetVisibility(RequisitionsButton, Security.CanView<Requisition>() && Security.IsAllowed<ViewDesktopSiteRequisitionsScreen>());        SetVisibility(DeliveriesButton, Security.IsAllowed<CanViewDeliveriesModule>() && Security.IsAllowed<ViewDesktopDeliveriesScren>());        SetVisibility(DeliveredItemsButton,            ClientFactory.IsSupported<DeliveryItem>()            && Security.IsAllowed<CanViewDeliveredOnSite>()            && Security.IsAllowed<ViewDesktopDeliveredOnSiteScreen>());        SetVisibility(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;        SetVisibility(ManufacturingDashboardButton, Security.IsAllowed<CanViewUserDefinedDashboards>());        SetVisibility(ManufacturingMessagesButton, Security.CanView<Notification>());        SetVisibility(ManufacturingTaskButton, ClientFactory.IsSupported<Kanban>() && Security.IsAllowed<CanViewTasks>());        SetVisibility(ManufacturingAttendanceButton,            ClientFactory.IsSupported<TimeSheet>() && Security.IsAllowed<CanViewInOutBoard>());        SetVisibility(ManufacturingMapButton, bMaps);        SetVisibility(ManufacturingDailyReportButton,            ClientFactory.IsSupported<TimeSheet, Assignment>() && Security.IsAllowed<CanViewDailyReports>());        SetVisibility(FactoryStatusButton,            ClientFactory.IsSupported<ManufacturingFactory, ManufacturingPacket>()            && Security.IsAllowed<CanViewFactoryStatus>()            && Security.IsAllowed<ViewDesktopManufacturingStatusScreen>());        SetVisibility(FactoryAllocationButton,            ClientFactory.IsSupported<ManufacturingFactory, ManufacturingPacket, ManufacturingPacketStage>()            && Security.IsAllowed<CanViewFactoryAllocation>()            && Security.IsAllowed<ViewDesktopFactoryAllocationScreen>());        //SetVisibility(FactoryScheduleButton, ClientFactory.IsSupported<Booking>() && ClientFactory.IsEnabled<>());        SetVisibility(FactoryFloorButton,            ClientFactory.IsSupported<ManufacturingPacket>()            && Security.IsAllowed<CanViewFactoryFloor>()            && Security.IsAllowed<ViewDesktopFactoryFloorScreen>());        //SetVisibility(FactoryReadyButton, ClientFactory.IsSupported<ManufacturingPacket>() && Security.IsAllowed<CanViewFactoryReadyToGo>());        SetVisibleIfEither(ManufacturingTaskSeparator,            new FrameworkElement[]            {                            ManufacturingDashboardButton, ManufacturingMessagesButton, ManufacturingTaskButton, ManufacturingAttendanceButton,                            ManufacturingMapButton, ManufacturingDailyReportButton            }, new FrameworkElement[] { FactoryStatusButton, FactoryAllocationButton, FactoryFloorButton /* , FactoryReadyButton */ });        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;        SetVisibility(ProjectsDashboardButton, Security.IsAllowed<CanViewUserDefinedDashboards>());        SetVisibility(ProjectMessagesButton, Security.CanView<Notification>());        SetVisibility(ProjectTaskButton, ClientFactory.IsSupported<Kanban>() && Security.IsAllowed<CanViewTasks>());        SetVisibility(ProjectAttendanceButton, ClientFactory.IsSupported<TimeSheet>() && Security.IsAllowed<CanViewInOutBoard>());        SetVisibility(ProjectsMapButton, bMaps);        SetVisibility(ProjectDailyReportButton,            ClientFactory.IsSupported<TimeSheet, Assignment>() && Security.IsAllowed<CanViewDailyReports>());        SetVisibility(ProjectsButton, Security.CanView<Job>() && Security.IsAllowed<ViewDesktopProjectsScreen>());        SetVisibility(ServiceButton, false);        SetVisibility(ProjectPlannerButton, Security.CanView<Job>() && Security.IsAllowed<ViewDesktopProjectPlannerScreen>());        SetVisibility(DesignManagementButton, Security.CanView<Job>() && Security.IsAllowed<ViewDesktopDesignManagementScreen>());        SetVisibleIfEither(ProjectTaskSeparator,            new FrameworkElement[]            {                            ProjectsDashboardButton, ProjectMessagesButton, ProjectTaskButton, ProjectAttendanceButton, ProjectsMapButton,                            ProjectDailyReportButton            }, new FrameworkElement[] { QuotesButton, ProjectsButton, ServiceButton, ProjectPlannerButton });        //ProjectsActions.IsLauncherButtonVisible = Security.IsAllowed<CanCustomiseModules>();        //ProjectReports.IsLauncherButtonVisible = Security.IsAllowed<CanDesignReports>();        SetTabVisibleIfAny(ProjectsTab, ProjectsButton, ServiceButton, ProjectPlannerButton);    }    private void SetupQuotesTab(bool bMaps)    {        if (!Security.IsAllowed<ViewDesktopQuotesTab>())            return;        SetVisibility(QuotesDashboardButton, Security.IsAllowed<CanViewUserDefinedDashboards>());        SetVisibility(QuotesMessagesButton, Security.CanView<Notification>());        SetVisibility(QuotesTaskButton, Security.IsAllowed<CanViewTasks>());        SetVisibility(QuotesAttendanceButton, Security.IsAllowed<CanViewInOutBoard>());        SetVisibility(QuotesMapButton, bMaps);        SetVisibility(QuotesDailyReportButton,            ClientFactory.IsSupported<TimeSheet, Assignment>() && Security.IsAllowed<CanViewDailyReports>());        SetVisibility(QuotesButton, Security.CanView<Quote>() && Security.IsAllowed<ViewDesktopQuotesScreen>());        SetVisibility(KitsMasterList, Security.CanView<Kit>() && Security.IsAllowed<ViewDesktopProductKitsScreen>());        SetVisibility(CostSheetsMasterList, Security.CanView<CostSheet>() && Security.IsAllowed<ViewDesktopCostSheetsScreen>());        SetVisibleIfEither(QuotesTaskSeparator,            new FrameworkElement[]            {                            QuotesDashboardButton, QuotesMessagesButton, QuotesTaskButton, QuotesAttendanceButton, QuotesMapButton,                            QuotesDailyReportButton            }, new FrameworkElement[] { QuotesButton });        SetVisibleIfEither(QuotesActionSeparator, new FrameworkElement[] { QuotesButton },            new FrameworkElement[] { KitsMasterList, CostSheetsMasterList });        SetVisibleIfAny(QuotesActions, QuotesButton, KitsMasterList, CostSheetsMasterList);        SetTabVisibleIfAny(QuotesTab, QuotesActions);    }    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))            {                MessageBox.Show("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();                            LoadApplicationState();                        }                    };                }                return;            }            using (new WaitCursor())            {                _ribbon.IsCollapsed = false;                LoadInitialWindow();            }        }    }    private void LoadInitialWindow()    {        var app = new LocalConfiguration<AppSettings>().Load();        if (app.Settings.ContainsKey("CurrentPanel"))        {            try            {                var bFound = false;                var module = app.Settings["CurrentPanel"].Split(new[] { " / " }, StringSplitOptions.None);                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;                                        button.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent));                                        break;                                    }                                }                                if (bFound)                                    break;                            }                        }                        if (bFound)                            break;                    }            }            catch (Exception e)            {                CoreUtils.LogException(ClientFactory.UserID, e);                MessageBox.Show(string.Format("Unable to Load {0}!\n\n{1}\n{2}", app.Settings["CurrentPanel"], e.Message, e.StackTrace));            }        }    }    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;            }        }    }    private T? LoadWindow<T>(Fluent.Button sender) where T : class, IBasePanel, new()    {        return LoadWindow<T>(sender, new CancelEventArgs());    }    private T? LoadWindow<T>(Fluent.Button sender, CancelEventArgs cancel) where T : class, IBasePanel, new()    {        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 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(DocumentStore).Assembly,                typeof(EquipmentStore).Assembly            },            myType =>                myType.IsClass                && !myType.IsAbstract                && !myType.IsGenericType                && myType.GetInterfaces().Contains(typeof(IStore))        ).ToArray();        DbFactory.Provider = new SQLiteProvider(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.Provider.Load<User>();        if (!users.Any())        {            var user = new User { UserID = "ADMIN", Password = "admin" };            DbFactory.Provider.Save(user);            var employee = DbFactory.Provider.Load(new Filter<Employee>(x => x.Code).IsEqualTo("ADMIN")).FirstOrDefault();            employee ??= new Employee { Code = "ADMIN", Name = "Administrator Account" };            employee.UserLink.ID = user.ID;            DbFactory.Provider.Save(employee);        }        StoreUtils.GoogleAPIKey = App.DatabaseSettings.GoogleAPIKey;        Job.JobNumberPrefix = App.DatabaseSettings.JobPrefix;        PurchaseOrder.PONumberPrefix = App.DatabaseSettings.PurchaseOrderPrefix;    }    #endregion    #region Login Management    private ValidationStatus? DoLogin(bool autoLogin)    {        ValidationStatus? result = null;        if (autoLogin)        {            if (App.DatabaseSettings.LoginType == LoginType.UserID)            {                try                {                    result = ClientFactory.Validate(App.DatabaseSettings.UserID, App.DatabaseSettings.Password);                }                catch (Exception e)                {                    Logger.Send(LogType.Error, ClientFactory.UserID, $"Error connecting to server: {CoreUtils.FormatException(e)}");                    MessageBox.Show("Error connecting to server.\nPlease check the server URL and port number.");                    result = null;                }                if (result == ValidationStatus.INVALID)                {                    MessageBox.Show("Unable to Login with User ID: " + App.DatabaseSettings.UserID);                }            }        }        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>    private void AfterLogin()    {        LoadCurrentEmployee();        if (CheckTimesheetBypass(true))        {            UpdateCurrentLogin();        }        else        {            MessageBox.Show("You must clock on before logging in to PRS!");            ClientFactory.InvalidateUser();            App.EmployeeID = Guid.Empty;            App.EmployeeName = "";            App.EmployeeCode = "";            App.EmployeeEmail = "";        }        ApplyColorScheme();        ConfigureMainScreen();        LoadApplicationState();        LoadSecondaryWindows();        //if (_ribbon.Menu.IsVisible)        //{        //    _ribbon.;        //}    }    /// <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),            new Columns<Employee>(x => x.ID).Add(x => x.Email).Add(x => x.Name)        );        App.EmployeeID = me.Rows.FirstOrDefault()?.Get<Employee, Guid>(x => x.ID) ?? Guid.Empty;        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();        LoadSecondaryWindows();        if (message != null)        {            MessageBox.Show(message);        }        if (DoLogin(false) == ValidationStatus.VALID)        {            AfterLogin();        }        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,            new Columns<TimeSheet>(                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)            {                if (message)                    MessageBox.Show("You must clock on before opening this screen");                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 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();    }    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 SetVisibility(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 RibbonButton rb)        {            CustomModules.Register(rb.Label);            rb.IsEnabled = !OutstandingDailyReports(false);        }    }    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 Common    private void Tasks_Checked(object sender, RoutedEventArgs e)    {        //LoadWindow<KanbanPanel>((RibbonButton)sender);        LoadWindow<TaskPanel>((Fluent.Button)sender);    }    private void DataEntry_Click(object sender, RoutedEventArgs e)    {        LoadWindow<DataEntryPanel>((Fluent.Button)sender);    }    private void Attendance_Checked(object sender, RoutedEventArgs e)    {        LoadWindow<AttendancePanel>((Fluent.Button)sender);    }    #endregion    #region Quotes    private void Quotes_Checked(object sender, RoutedEventArgs e)    {        LoadWindow<QuotePanel>((Fluent.Button)sender);    }    #endregion    #region Projects    private void Jobs_Checked(object sender, RoutedEventArgs e)    {        LoadWindow<JobPanel>((Fluent.Button)sender);    }    private void ProjectPlanner_Checked(object sender, RoutedEventArgs e)    {        LoadWindow<JobResourcePlannerPanel>((Fluent.Button)sender);    }    private void DesignManagement_Checked(object sender, RoutedEventArgs e)    {        LoadWindow<StagingPanel>((Fluent.Button)sender);    }    private void Service_Checked(object sender, RoutedEventArgs e)    {        MessageBox.Show("Not Implemented");        //LoadWindow<JobPanel>((RibbonButton)sender);    }    #endregion    #region Manufacturing    private void ManufacturingMenu_Checked(object sender, RoutedEventArgs e)    {        LoadWindow<ManufacturingPanel>((Fluent.Button)sender);    }    private void FactoryFloorButton_Click(object sender, RoutedEventArgs e)    {        LoadWindow<FactoryPanel>((Fluent.Button)sender);    }    #endregion    #region Logistics    private void ReadyToGoMenu_Checked(object sender, RoutedEventArgs e)    {        LoadWindow<ReadyToGoPanel>((Fluent.Button)sender);    }    private void DispatchMenu_Checked(object sender, RoutedEventArgs e)    {        LoadWindow<DispatchPanel>((Fluent.Button)sender);    }    private void Requisitions_Checked(object sender, RoutedEventArgs e)    {        LoadWindow<RequisitionPanel>((Fluent.Button)sender);    }    #endregion    #region Products    #endregion    #region Human Resources    private void Timesheets_Checked(object sender, RoutedEventArgs e)    {        LoadWindow<TimesheetPanel>((Fluent.Button)sender);    }    #endregion    #region Accounts    #endregion    #region Equipment    private void Equipment_Checked(object sender, RoutedEventArgs e)    {        LoadWindow<EquipmentPanel>((Fluent.Button)sender);    }    #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 Purchases_Checked(object sender, RoutedEventArgs e)    //{    //    LoadWindow(PurchasesMenu, new PurchasesPanel());    //}    private void Maps_Checked(object sender, RoutedEventArgs e)    {        LoadWindow<MapsPanel>((Fluent.Button)sender);    }    private void RefreshMenu_Click(object sender, RoutedEventArgs e)    {        PanelHost.Refresh();    }    private void UserSetup_Click(object sender, RoutedEventArgs e)    {        LoadWindow<UserPanel>((Fluent.Button)sender);    }    private void Employees_Click(object sender, RoutedEventArgs e)    {        LoadWindow<EmployeePanel>((Fluent.Button)sender);    }    private void Trackers_Click(object sender, RoutedEventArgs e)    {        LoadWindow<GPSTrackers>((Fluent.Button)sender);    }    private void DeliveredOnSiteMenu_Checked(object sender, RoutedEventArgs e)    {        LoadWindow<DeliveredOnSitePanel>((Fluent.Button)sender);    }    private void Products_Checked(object sender, RoutedEventArgs e)    {        LoadWindow<ProductsPanel>((Fluent.Button)sender);    }    private void StockLocations_Checked(object sender, RoutedEventArgs e)    {        LoadWindow<StockLocationPanel>((Fluent.Button)sender);    }    private void StockMovements_Checked(object sender, RoutedEventArgs e)    {        LoadWindow<StockMovementPanel>((Fluent.Button)sender);    }    private void StockSummaryButton_Clicked(object sender, RoutedEventArgs e)    {        LoadWindow<StockSummaryPanel>((Fluent.Button)sender);    }    private void ReservationManagementButton_Clicked(object sender, RoutedEventArgs e)    {        LoadWindow<JobRequisitionsPanel>((Fluent.Button)sender);    }    private void KitsMasterList_Click(object sender, RoutedEventArgs e)    {        LoadWindow<KitPanel>((Fluent.Button)sender);    }    private void CostSheetsMasterList_Click(object sender, RoutedEventArgs e)    {        LoadWindow<CostSheetPanel>((Fluent.Button)sender);    }    private void LeaveRequestsButton_Click(object sender, RoutedEventArgs e)    {        LoadWindow<LeaveRequestPanel>((Fluent.Button)sender);    }    private void MeetingsButton_Click(object sender, RoutedEventArgs e)    {        LoadWindow<MeetingPanel>((Fluent.Button)sender);    }    private void OrgChartButton_Click(object sender, RoutedEventArgs e)    {        LoadWindow<OrgChartPanel>((Fluent.Button)sender);    }    private void SupplierList_Click(object sender, RoutedEventArgs e)    {        LoadWindow<SupplierPanel>((Fluent.Button)sender);    }    private void CustomerList_Click(object sender, RoutedEventArgs e)    {        LoadWindow<CustomerPanel>((Fluent.Button)sender);    }    private void CalendarButton_Click(object sender, RoutedEventArgs e)    {        LoadWindow<CalendarPanel>((Fluent.Button)sender);    }    private void EmployeePlannerButton_Click(object sender, RoutedEventArgs e)    {        LoadWindow<EmployeeResourcePlannerPanel>((Fluent.Button)sender);    }    private void EquipmentPlannerButton_Click(object sender, RoutedEventArgs e)    {        LoadWindow<EquipmentPlannerPanel>((Fluent.Button)sender);    }    private void InvoiceList_Click(object sender, RoutedEventArgs e)    {        LoadWindow<InvoicePanel>((Fluent.Button)sender);    }    private void ReceiptList_Click(object sender, RoutedEventArgs e)    {        LoadWindow<CustomerReceipts>((Fluent.Button)sender);    }    private void PaymentsList_Click(object sender, RoutedEventArgs e)    {        LoadWindow<SupplierPayments>((Fluent.Button)sender);    }    private void BillsList_Click(object sender, RoutedEventArgs e)    {        LoadWindow<SupplierBillPanel>((Fluent.Button)sender);    }    private void PurchasesList_Click(object sender, RoutedEventArgs e)    {        LoadWindow<SupplierPurchaseOrderPanel>((Fluent.Button)sender);    }    private void DeliveriesButton_Click(object sender, RoutedEventArgs e)    {        LoadWindow<DeliveryPanel>((Fluent.Button)sender);    }    private void FactoryAllocationButton_Click(object sender, RoutedEventArgs e)    {        LoadWindow<ManufacturingAllocationPanel>((Fluent.Button)sender);    }    private void Messages_Checked(object sender, RoutedEventArgs e)    {        LoadWindow<NotificationPanel>((Fluent.Button)sender);    }    private void DatabaseActivityButton_Click(object sender, RoutedEventArgs e)    {        LoadWindow<DatabaseActivityDashboard>((Fluent.Button)sender);    }    private void FactoryProductivityButton_Click(object sender, RoutedEventArgs e)    {        LoadWindow<FactoryProductivityDashboard>((Fluent.Button)sender);    }    private void TemplateAnalysisButton_Click(object sender, RoutedEventArgs e)    {        LoadWindow<ManufacturingTemplateAnalysis>((Fluent.Button)sender);    }    private void FactoryAnalysisButton_Click(object sender, RoutedEventArgs e)    {        LoadWindow<FactoryFloorAnalysis>((Fluent.Button)sender);    }    private void UserActivityButton_Click(object sender, RoutedEventArgs e)    {        LoadWindow<UserActivity>((Fluent.Button)sender);    }    private void QuickStatus_Click(object sender, RoutedEventArgs e)    {        LoadWindow<WidgetDashboard>((Fluent.Button)sender);    }    private void ConsignmentButton_Click(object sender, RoutedEventArgs e)    {        LoadWindow<ConsignmentsPanel>((Fluent.Button)sender);    }    //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 DailyReport_Checked(object sender, RoutedEventArgs e)    {        LoadWindow<DailyReport>((Fluent.Button)sender);    }    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 });    }    #region Digital Forms    private void DigitalFormsFormsLibraryButton_Click(object sender, RoutedEventArgs e)    {        LoadWindow<DigitalFormsLibrary>((Fluent.Button)sender);    }    private void DigitalFormsCompletedFormsButton_Click(object sender, RoutedEventArgs e)    {        LoadWindow<CompletedFormsPanel>((Fluent.Button)sender);    }    #endregion    private void Dashboards_Checked(object sender, RoutedEventArgs e)    {        LoadWindow<UtilityDashboard>((Fluent.Button)sender);    }    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 StartForm<TEntityForm, TEntity, TEntityLink>(DigitalForm form)        where TEntityForm : EntityForm<TEntity, TEntityLink, TEntityForm>, new()        where TEntity : Entity, new()        where TEntityLink : EntityLink<TEntity>, new()    {        var entityForm = new TEntityForm();        entityForm.Form.ID = form.ID;        entityForm.Description = form.Description;        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.DefineFilter<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.DefineColumns<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)        {            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),                new Columns<Login>(x => x.StationID),                null,                (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        {            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 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();    }    #region Recording    //private IntPtr myHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)    //{    //    switch (msg)    //    {    //        case 0x100:    //            if (_subject != null)    //            {    //                _recorder.Stop();    //                pausestarted = DateTime.Now;    //                _subject.OnNext(new WampEvent() { Arguments = new[] { "EDIT" } });    //            }    //            break;    //        default:    //            messages.TryGetValue(msg, out int value);    //            messages[msg] = value + 1;    //            break;                      //    }    //    return IntPtr.Zero;    //}    private void RecordScreenButton_Click(object sender, RoutedEventArgs e)    {        ToggleRecording();    }    private bool ToggleRecording()    {        if (!Security.IsAllowed<CanRecordScreen>())            return true;        if (_recorder is null)        {            var windowInteropHelper = new WindowInteropHelper(this);            var screen = Screen.FromHandle(windowInteropHelper.Handle);            _screenleft = screen.WorkingArea.Left;            _screentop = screen.WorkingArea.Top;            _screenwidth = screen.WorkingArea.Width;            _screenheight = screen.WorkingArea.Height;            //VIDEO_WIDTH = _screenwidth;            //VIDEO_HEIGHT = _screenheight;            //SetupJPEGEncoder();            messages.Clear();            _bitmaps.Clear();            ConnectToRecordNotes();            StartAudio();            _recorder = new DispatcherTimer();            _recorder.Tick += (timer, args) => { TakeScreenShot(); };            _recorder.Interval = TimeSpan.FromMilliseconds(1000 / FRAMES_PER_SECOND);            _recorder.Start();            VideoRecordingStatus.Source = PRSDesktop.Resources.videorecording.AsBitmapImage();            RecordingNotesStatus.Source = PRSDesktop.Resources.speechbubble.AsGrayScale().AsBitmapImage();            RecordingNotesButton.Visibility = Visibility.Visible;            AudioRecordingStatus.Source = PRSDesktop.Resources.audiorecording.AsGrayScale().AsBitmapImage();            AudioRecordingButton.Visibility = Visibility.Visible;        }        else        {            using (new WaitCursor())            {                StopRecording();                _recorder.Stop();                _recorder = null;                DisconnectRecorderNotes();                VideoRecordingStatus.Source = PRSDesktop.Resources.videorecording.AsGrayScale().AsBitmapImage();                RecordingNotesStatus.Source = PRSDesktop.Resources.speechbubble.AsGrayScale().AsBitmapImage();                RecordingNotesButton.Visibility = Visibility.Hidden;                AudioRecordingStatus.Source = PRSDesktop.Resources.audiorecording.AsGrayScale().AsBitmapImage();                AudioRecordingButton.Visibility = Visibility.Hidden;                var filename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyVideos),                    string.Format("PRS Screen Recording {0:yyyy-MM-dd hh-mm-ss-ff}.avi", DateTime.Now));                ProcessScreenShots(filename);            }        }        return true;    }    private void StopRecording()    {        try        {            _audio?.StopRecording();            AudioRecordingStatus.Source = PRSDesktop.Resources.audiorecording.AsGrayScale().AsBitmapImage();        }        catch (Exception e)        {            Logger.Send(LogType.Error, "", string.Format("Unable to stop Audio Recording: {0}\n{1}", e.Message, e.StackTrace));        }    }    private void StartAudio()    {        _audioMuted = true;        _audioStream = new MemoryStream();        _audio = new WaveIn        {            DeviceNumber = 0,            WaveFormat = new WaveFormat(44100, 16, 2)        };        _audio.DataAvailable += (o, e) =>        {            var buf = _audioMuted ? new byte[e.BytesRecorded] : e.Buffer;            _audioStream.Write(buf, 0, e.BytesRecorded);        };        try        {            _audio.StartRecording();            AudioRecordingStatus.Source = PRSDesktop.Resources.audiorecording.AsBitmapImage();        }        catch (Exception e)        {            Logger.Send(LogType.Error, "", string.Format("Unable to start Audio Recording: {0}\n{1}", e.Message, e.StackTrace));            AudioRecordingStatus.Source = PRSDesktop.Resources.audiorecording.AsGrayScale().AsBitmapImage();        }    }    private bool ToggleRecordingAudio()    {        if (!Security.IsAllowed<CanRecordScreen>())            return true;        if (_recorder != null)        {            _audioMuted = !_audioMuted;            AudioRecordingStatus.Source = _audioMuted                ? PRSDesktop.Resources.audiorecording.AsGrayScale().AsBitmapImage()                : PRSDesktop.Resources.audiorecording.AsBitmapImage();        }        return true;    }    private void DisconnectRecorderNotes()    {        if (_client != null)        {            _client.WriteAsync("QUIT");            _client.StopAsync();            _client = null;        }    }    private void ConnectToRecordNotes()    {        var filename = Debugger.IsAttached            ? "c:\\development\\comal\\prsrecordingnotes\\bin\\debug\\prsrecordingnotes.exe"            : Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "recordingnotes", "prsrecordingnotes.exe");        Logger.Send(LogType.Information, "", "Recording Notes: " + filename);        _recordingnotes = Process.Start(filename);        //PipeSecurity sec = new System.IO.Pipes.PipeSecurity();        //sec.SetAccessRule(new PipeAccessRule("Everyone", PipeAccessRights.ReadWrite, AccessControlType.Allow));        _client = new PipeServer<string>("PRSScreenRecorder"); //, sec);        _client.ClientConnected += (o, args) => args.Connection.WriteAsync("Connected to PRSScreenRecorder");        _client.MessageReceived += (o, args) =>        {            if (string.Equals(args.Message, "RESUME"))                Dispatcher.Invoke(() =>                {                    _audio?.StartRecording();                    totalpauses += DateTime.Now - pausestarted;                    _recorder?.Start();                    Activate();                    Focus();                });        };        _client.StartAsync();    }    private bool ShowRecordingNotes()    {        if (_recorder != null)        {            _audio?.StopRecording();            _recorder.Stop();            pausestarted = DateTime.Now;            _client?.WriteAsync("EDIT");            //_subject.OnNext(new WampEvent() { Arguments = new[] { "EDIT" } });        }        return true;    }    private void TakeScreenShot()    {        using (var bmp = new ScreenCapture().CaptureScreen(_screenleft, _screentop, _screenwidth, _screenheight))        {            var cursor = PointToScreen(Mouse.GetPosition(this));            using (var g = Graphics.FromImage(bmp))            {                var brush = new SolidBrush(System.Drawing.Color.FromArgb(128, 255, 255, 0));                g.FillEllipse(brush, new RectangleF((float)cursor.X - 25F, (float)cursor.Y - 25F, 50F, 50F));                var pen = new Pen(System.Drawing.Color.Black, 0.75F);                g.DrawLine(pen, (float)cursor.X - 10F, (float)cursor.Y, (float)cursor.X + 10F, (float)cursor.Y);                g.DrawLine(pen, (float)cursor.X, (float)cursor.Y - 10F, (float)cursor.X, (float)cursor.Y + 10F);            }            var key = DateTime.Now - totalpauses;            using (var reduced = ReduceBitmap(bmp, VIDEO_WIDTH, VIDEO_HEIGHT))            {                var ms = new MemoryStream();                reduced.Save(ms, ImageFormat.Png);                ms.Position = 0;                _bitmaps[key] = ms;            }        }    }    public Bitmap ReduceBitmap(Bitmap original, int reducedWidth, int reducedHeight)    {        var reduced = new Bitmap(reducedWidth, reducedHeight);        using (var dc = Graphics.FromImage(reduced))        {            // Figure out the ratio            var ratioX = (double)reducedWidth / original.Width;            var ratioY = (double)reducedHeight / original.Height;            // use whichever multiplier is smaller            var ratio = ratioX < ratioY ? ratioX : ratioY;            // now we can get the new height and width            var newHeight = Convert.ToInt32(original.Height * ratio);            var newWidth = Convert.ToInt32(original.Width * ratio);            // Now calculate the X,Y position of the upper-left corner             // (one of these will always be zero)            var posX = Convert.ToInt32((reducedWidth - original.Width * ratio) / 2);            var posY = Convert.ToInt32((reducedHeight - original.Height * ratio) / 2);            dc.InterpolationMode = InterpolationMode.HighQualityBicubic;            dc.Clear(System.Drawing.Color.Black); // white padding            //dc.DrawImage(original, posX, posY, newWidth, newHeight);            dc.DrawImage(original, new Rectangle(posX, posY, newWidth, newHeight), new Rectangle(0, 0, original.Width, original.Height),                GraphicsUnit.Pixel);        }        return reduced;    }    public static byte[] BitmapToByteArray(Bitmap bitmap, int width, int height)    {        // and buffer of appropriate size for storing its bits        var buffer = new byte[width * height * 4];        var pixelFormat = PixelFormat.Format32bppRgb;        // Now copy bits from bitmap to buffer        var bits = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, pixelFormat);        //Marshal.Copy(bits.Scan0, buffer, 0, buffer.Length);        Marshal.Copy(bits.Scan0, buffer, 0, buffer.Length);        bitmap.UnlockBits(bits);        return buffer;    }    private void ProcessScreenShots(string filename)    {        if (!_bitmaps.Any() || _audioStream is null)            return;        Progress.Show("Saving Video");        var framRate = 10;        using (var writer = new AviWriter(filename))        {            writer.FramesPerSecond = framRate;            writer.EmitIndex1 = true;            _audioStream.Position = 0;            var audio = writer.AddAudioStream(2);            //audio.WriteBlock(_audioStream.GetBuffer(), 0, (int)_audioStream.Length);            //File.WriteAllBytes(Path.ChangeExtension(filename, "wav"), _audioStream.GetBuffer());            var audioByteRate = audio.BitsPerSample / 8 * audio.ChannelCount * audio.SamplesPerSecond;            var audioBlockSize = (int)(audioByteRate / writer.FramesPerSecond);            var audioBuffer = new byte[audioBlockSize];            _audioStream.Position = 0;            var encoder = new MJpegWpfVideoEncoder(VIDEO_WIDTH, VIDEO_HEIGHT, 50);            var stream = writer.AddEncodingVideoStream(encoder, true, VIDEO_WIDTH, VIDEO_HEIGHT);            //var encoder = new SharpAvi.Codecs.UncompressedVideoEncoder(VIDEO_WIDTH, VIDEO_HEIGHT);            //var stream = writer.AddEncodingVideoStream(encoder,true,VIDEO_WIDTH, VIDEO_HEIGHT);            //stream.Codec = CodecIds.Uncompressed;            //stream.BitsPerPixel = BitsPerPixel.Bpp16;            IEnumerable<DateTime> keys = _bitmaps.Keys.OrderBy(x => x);            var start = keys.First();            var end = keys.Last();            for (var cur = start; cur <= end; cur = cur + TimeSpan.FromMilliseconds(100))            {                Progress.SetMessage(string.Format("Processing ({0:F2} complete)",                    (cur - start).TotalSeconds * 100.0F / (end - start).TotalSeconds));                var key = keys.LastOrDefault(x => x <= cur);                if (_bitmaps.ContainsKey(key))                {                    var frame = (Image.FromStream(_bitmaps[key]) as Bitmap)!;                    //frame.RotateFlip(RotateFlipType.RotateNoneFlipY);                    var buf = BitmapToByteArray(frame, stream.Width, stream.Height);                    stream.WriteFrame(true, buf, 0, buf.Length);                    if (_audioStream.Position < _audioStream.Length)                    {                        var bytes = _audioStream.Read(audioBuffer, 0, audioBlockSize);                        audio.WriteBlock(audioBuffer, 0, bytes);                    }                }            }            writer.Close();            //foreach (var key in _bitmaps.Keys)            //    File.Delete(_bitmaps[key]);        }        Progress.SetMessage("Transcoding");        //var inputFile = new MediaFile { Filename = filename };        //var outputFile = new MediaFile { Filename = Path.ChangeExtension(filename, "mp4") };        //using (var engine = new Engine())        //{        //    //engine.ConvertProgressEvent += ConvertProgressEvent;        //    engine.Convert(inputFile, outputFile);        //}        Progress.Close();        var dirName = Path.GetDirectoryName(filename);        if (dirName is not null)        {            var startInfo = new ProcessStartInfo(dirName);            startInfo.Verb = "open";            startInfo.UseShellExecute = true;            Process.Start(startInfo);            //MessageBox.Show("Recording Saved");        }    }    private void VideoRecordingButton_Click(object sender, RoutedEventArgs e)    {        ToggleRecording();    }    private void AudioRecordingButton_Click(object sender, RoutedEventArgs e)    {        ToggleRecordingAudio();    }    private void RecordingNotesButton_Click(object sender, RoutedEventArgs e)    {        ShowRecordingNotes();    }    #endregion    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 Check For Updates    private string GetUpdateLocation()    {        if (App.DatabaseSettings.DatabaseType == DatabaseType.Networked)        {            string url = "";            //var domain = App.DatabaseSettings.URL.Split(new[] { "://" }, StringSplitOptions.RemoveEmptyEntries).Last();            //var port = App.DatabaseSettings.Port;            var domain = ClientFactory.Parameters?.FirstOrDefault()?.ToString() ?? "";            domain = domain.Split(new[] { "://" }, StringSplitOptions.RemoveEmptyEntries).LastOrDefault() ?? "";            if (!String.IsNullOrWhiteSpace(domain))            {                                try                {                    var client = new HttpClient { BaseAddress = new Uri($"https://{domain}") };                    client.GetAsync("operations").Wait();                    url = $"https://{domain}";                }                catch (Exception)                {                    url = $"http://{domain}";                }            }            return url;        }        else            return Path.Combine(CoreUtils.GetCommonAppData("PRSServer"), "update");    }    private string GetLatestVersion(string location)    {        return Client.Version();    }    private string GetReleaseNotes(string location)    {        return Client.ReleaseNotes();    }    private byte[]? GetInstaller(string location)    {        return Client.Installer();    }    private void CheckForUpdates()    {        Update.CheckForUpdates(            GetUpdateLocation, GetLatestVersion, GetReleaseNotes, GetInstaller, null, App.AutoUpdateSettings.Elevated, "PRSDesktopSetup.exe");    }    #endregion    #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 = Security.IsAllowed<CanPrintReports>() ? Visibility.Visible : 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.Items.Add(button);        }    }    public void CreatePanelAction(PanelAction action)    {        if (CurrentTab is null)            return;        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);            }            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();            };            CurrentModules.Add(button);            Actions.Items.Add(button);        }    }    #endregion    #region Tracking Kanban    private void SelectTask_Click(object sender, RoutedEventArgs e)    {        ContextMenu menu = new ContextMenu();        MenuItem others = new MenuItem() { Header = "Other Tasks" };        using (new WaitCursor())        {            var kanbans = new Client<KanbanSubscriber>().Query(                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),                new Columns<KanbanSubscriber>(x => x.Kanban.ID)                    .Add(x => x.Kanban.Number)                    .Add(x => x.Kanban.Title)                    .Add(x => x.Assignee),                new SortOrder<KanbanSubscriber>(x => x.Kanban.Number, SortDirection.Ascending)            );            foreach (var row in kanbans.Rows)            {                CreateTaskMenu(row.Get<KanbanSubscriber, bool>(c => c.Assignee) ? menu.Items : others.Items,                    String.Format("{0} {1}",                        row.Get<KanbanSubscriber, int>(c => c.Kanban.Number),                        row.Get<KanbanSubscriber, String>(c => c.Kanban.Title)),                    row.Get<KanbanSubscriber, Guid>(c => c.Kanban.ID)                );            }            menu.Items.Add(new Separator());            menu.Items.Add(others);            menu.Items.Add(new Separator());            CreateTaskMenu(menu.Items, "(No Task Selected)", Guid.Empty);        }        menu.IsOpen = true;    }    private Assignment? _kanbantrackingassignment = 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;                new Client<Assignment>().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));            new Client<Assignment>().Save(_kanbantrackingassignment, "");        }    }    private void ClearTrackingKanban()        => SetTrackingKanban(Guid.Empty, "(No Task Selected)");    private void CreateTaskMenu(ItemCollection items, String title, Guid id)    {        var item = new MenuItem()        {            Header = title,            Tag = id        };        item.Click += (o, args) =>        {            if (o is not MenuItem menu) return;            SetTrackingKanban((Guid)item.Tag, (menu.Header as string) ?? "");        };        items.Add(item);    }    #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 = Path.Combine(CoreUtils.GetPath(), string.Format("{0:yyyy-MM-dd}.log", DateTime.Today));        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 DocumentTypeList_OnClick(object sender, RoutedEventArgs e)    {        var list = new MasterList(typeof(DocumentType));        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.ID = 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(false) == ValidationStatus.VALID)            AfterLogin();    }    private void ExitButton_OnClick(object sender, RoutedEventArgs e)    {        //_closingFromSystemMenu = true;        Close();    }    #endregion}
 |