From f76e3968f8061ea1bc04890cc28800ef71e5a3b7 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Fri, 21 Jan 2022 19:35:48 -0500 Subject: [PATCH] refactor: Clean up App.xaml.cs * Sort members * Fix naming violations * Remove unneccesary using directives * Run formatter --- src/client/spark/App.xaml.cs | 294 ++++++++++++++++------------------- 1 file changed, 136 insertions(+), 158 deletions(-) diff --git a/src/client/spark/App.xaml.cs b/src/client/spark/App.xaml.cs index 84fc23a20..dc7331ab7 100644 --- a/src/client/spark/App.xaml.cs +++ b/src/client/spark/App.xaml.cs @@ -1,158 +1,136 @@ -using bts; - -using mbts.ui.native; - -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Diagnostics; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading; -using System.Windows; -using System.Windows.Controls; -using System.Windows.Data; -using System.Windows.Documents; -using System.Windows.Input; -using System.Windows.Media; -using System.Windows.Media.Imaging; -using System.Windows.Navigation; -using System.Windows.Shapes; - -namespace mbts.ui.shell { - public partial class App : Application { - ShellBootstrapper _runtime; - static Mutex _mutex; - - static App() { - // Don't let the designer take the mutex. - if (!IsInDesigner()) { - _mutex = new Mutex(true, "5BA5BA1C-9AAC-4844-B7E5-094691CD5105"); - } - } - - private static bool IsInDesigner() { -#if DEBUG - var prop = DesignerProperties.IsInDesignModeProperty; - return (bool)DependencyPropertyDescriptor.FromProperty(prop, typeof(FrameworkElement)).Metadata.DefaultValue; -#else - return false; -#endif - } - - protected override void OnStartup(StartupEventArgs e) { - // force US culture until we can sort out internationalization issues - var us_culture = new CultureInfo("en-US"); - Thread.CurrentThread.CurrentCulture = us_culture; - Thread.CurrentThread.CurrentUICulture = us_culture; - CultureInfo.DefaultThreadCurrentCulture = us_culture; - CultureInfo.DefaultThreadCurrentUICulture = us_culture; - base.OnStartup(e); - _runtime = new ShellBootstrapper(); - //BTSUINativeMethods.enable_limits(); - _runtime.Run(); - - // Select text on tab in - EventManager.RegisterClassHandler(typeof(TextBox), UIElement.GotFocusEvent, new RoutedEventHandler(textbox_focus)); - } - - private static void textbox_focus(object sender, RoutedEventArgs e) { - TextBox text = sender as TextBox; - if (text != null) { - text.SelectAll(); - } - } - - protected override void OnExit(ExitEventArgs e) { - Log.Gen.info("Application.OnExit"); - if (_runtime != null) _runtime.Dispose(); - base.OnExit(e); - Environment.Exit(0); - } - - void start_stunnel(string path, uint job_error) { - string config_error; - if (!mbts.config.Configuration.try_initialize(out config_error)) { - _stunnel_process = Process.Start(path + "bin\\stunnel.exe"); - if (_stunnel_process != null) { - for (int i = 0; i < 10; i++) { - if (mbts.config.Configuration.try_initialize(out config_error)) - break; - System.Threading.Thread.Sleep(500); - } - } else - stunnel_job_error = "Stunnel Job Error code: " + job_error + - " could not start stunnel " + (config_error ?? string.Empty); - } - } - - void kill_stunnel() { - try { - Process[] ps = Process.GetProcessesByName("stunnel"); - ps.ForEach(p => { - try { p.Kill(); } catch { } - }); - } catch { } - } - - Process _stunnel_process; - static internal string stunnel_job_error { get; private set; } - void init_stunnel(string[] argv) { - try { - string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\BTS\\stunnel\\"; - if (File.Exists(path + "config\\stunnel.conf")) { - uint job_error = BTSUINativeMethods.setup_process_job_object(); - //if we can't put this process and stunnel into a job, try to stop stunnel then restart - if (job_error != 0) - kill_stunnel(); - start_stunnel(path, job_error); - } - path = AppDomain.CurrentDomain.BaseDirectory + "\\..\\stunnel\\"; - if (File.Exists(path + "config\\stunnel.conf")) { - uint job_error = BTSUINativeMethods.setup_process_job_object(); - //if we can't put this process and stunnel into a job, try to stop stunnel then restart - if (job_error != 0) - kill_stunnel(); - start_stunnel(path, job_error); - } - } catch (Exception ex) { - stunnel_job_error = "Error starting stunnel: " + ex.Message; - } - } - - public new int Run() { - var argv = Environment.GetCommandLineArgs(); - try { - if (argv.Contains("-multiple") || _mutex == null || _mutex.WaitOne(TimeSpan.Zero, true)) { - if (_mutex == null || _mutex.WaitOne(TimeSpan.Zero, true)) { - if (!argv.Contains("-nostunnel")) { - init_stunnel(argv); - } - } - return base.Run(); - } else { - int index = Array.IndexOf(argv, "-url"); - if (index > 0 && argv.Length > index + 1) { - // todo: replace with a service - //using (DesktopBus d = new DesktopBus()) { - // d.publish("open_url", argv[index + 1]); - //} - } else { - // Second instance. Activate the main window and return - NativeMethods.PostMessage(NativeMethods.HWND_BROADCAST, (WM)NativeMethods.WM_SHOWME, IntPtr.Zero, IntPtr.Zero); - Log.Ui.info("Starting a second shell is not allowed. Attempting to bring the original shell to the foreground."); - } - return 0; - } - } catch (AbandonedMutexException ame) { - Log.Ui.warn("Shell mutex was abandoned. Releasing the mutex and trying again. \n" + ame.ToString()); - _mutex.ReleaseMutex(); - // Try agai until stack overflow - return Run(); - } - } - - } -} +namespace mbts.ui.shell { + using bts; + + using mbts.ui.native; + + using System; + using System.ComponentModel; + using System.Diagnostics; + using System.Globalization; + using System.IO; + using System.Linq; + using System.Threading; + using System.Windows; + using System.Windows.Controls; + + public partial class App : Application { + static readonly Mutex _mutex; + + ShellBootstrapper _runtime; + Process _stunnel_process; + + static App() { + // Don't let the designer take the mutex. + if (_is_in_designer) { + _mutex = new Mutex(true, "5BA5BA1C-9AAC-4844-B7E5-094691CD5105"); + } + } + + internal static string stunnel_job_error { get; private set; } + + static bool _is_in_designer => +#if DEBUG + (bool)DependencyPropertyDescriptor.FromProperty( + DesignerProperties.IsInDesignModeProperty, + typeof(FrameworkElement)).Metadata.DefaultValue; +#else + false; +#endif + + public new int Run() { + var argv = Environment.GetCommandLineArgs(); + try { + if (_mutex is null || _mutex.WaitOne(TimeSpan.Zero, true)) { + _init_stunnel(); + } else if (!argv.Contains("-multiple")) { + // Second instance. Activate the main window and return + NativeMethods.PostMessage(NativeMethods.HWND_BROADCAST, NativeMethods.WM_SHOWME, IntPtr.Zero, IntPtr.Zero); + Log.Ui.info("Bringing running shell to the foreground..."); + return 0; + } + return base.Run(); + } catch (AbandonedMutexException ame) { + Log.Ui.warn($"Shell mutex was abandoned. Releasing the mutex and trying again. \n{ame}"); + _mutex.ReleaseMutex(); + // Keep trying until stack overflow + return Run(); + } + } + + protected override void OnStartup(StartupEventArgs e) { + // force US culture until we can sort out internationalization issues + var us_culture = new CultureInfo("en-US"); + Thread.CurrentThread.CurrentCulture = us_culture; + Thread.CurrentThread.CurrentUICulture = us_culture; + CultureInfo.DefaultThreadCurrentCulture = us_culture; + CultureInfo.DefaultThreadCurrentUICulture = us_culture; + + base.OnStartup(e); + _runtime = new ShellBootstrapper(); + _runtime.Run(); + + // Select text on tab in + EventManager.RegisterClassHandler(typeof(TextBox), UIElement.GotFocusEvent, new RoutedEventHandler(_on_text_box_focus)); + } + + protected override void OnExit(ExitEventArgs e) { + Log.Gen.info("Application.OnExit"); + if (_runtime != null) _runtime.Dispose(); + base.OnExit(e); + Environment.Exit(0); + } + + static void _on_text_box_focus(object sender, RoutedEventArgs e) => + (sender as TextBox)?.SelectAll(); + + void _start_stunnel(string path, uint job_error) { + if (config.Configuration.try_initialize(out string config_error)) { + return; + } + _stunnel_process = Process.Start(path + "bin\\stunnel.exe"); + if (_stunnel_process is null) { + stunnel_job_error = $"Stunnel Job Error code: {job_error} could not start stunnel {config_error ?? string.Empty}"; + return; + } + for (int i = 0; i < 10; i++) { + if (config.Configuration.try_initialize(out _)) { + break; + } + Thread.Sleep(500); + } + } + + static void _kill_stunnel() { + try { + Process[] ps = Process.GetProcessesByName("stunnel"); + ps.ForEach(p => { + try { p.Kill(); } catch { } + }); + } catch { } + } + + void _init_stunnel() { + try { + string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\BTS\\stunnel\\"; + if (File.Exists(path + "config\\stunnel.conf")) { + uint job_error = BTSUINativeMethods.setup_process_job_object(); + //if we can't put this process and stunnel into a job, try to stop stunnel then restart + if (job_error != 0) + _kill_stunnel(); + _start_stunnel(path, job_error); + } + path = AppDomain.CurrentDomain.BaseDirectory + "\\..\\stunnel\\"; + if (File.Exists(path + "config\\stunnel.conf")) { + uint job_error = BTSUINativeMethods.setup_process_job_object(); + //if we can't put this process and stunnel into a job, try to stop stunnel then restart + if (job_error != 0) + _kill_stunnel(); + _start_stunnel(path, job_error); + } + } catch (Exception ex) { + stunnel_job_error = "Error starting stunnel: " + ex.Message; + } + } + + } +}