182 lines
5.9 KiB
C#
182 lines
5.9 KiB
C#
using HandyControl.Controls;
|
|
using Microsoft.Win32;
|
|
using SafeBagDE70.API;
|
|
using Serilog;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
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;
|
|
using System.Windows.Threading;
|
|
|
|
namespace SafeBagDE70.Admin
|
|
{
|
|
/// <summary>
|
|
/// Interaction logic for AdminSettingsUC.xaml
|
|
/// </summary>
|
|
public partial class AdminSettingsUC : UserControl
|
|
{
|
|
string appName = "";
|
|
public AdminSettingsUC()
|
|
{
|
|
InitializeComponent();
|
|
LoadStartupSetting();
|
|
timePicker.SelectedTime = Properties.Settings.Default.RestartTime;
|
|
SetupAutoRestart();
|
|
}
|
|
|
|
private void LoadStartupSetting()
|
|
{
|
|
// Open the registry key for the current user's startup applications
|
|
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", false);
|
|
appName = Assembly.GetEntryAssembly()?.GetName().Name;
|
|
if (key != null)
|
|
{
|
|
// Check if the app is already set to start with Windows
|
|
string value = key.GetValue(appName) as string;
|
|
|
|
// If the app is listed in the startup items, set the checkbox to checked
|
|
cbStartWithWindows.IsChecked = value != null;
|
|
}
|
|
}
|
|
private void cbStartWithWindows_Checked(object sender, RoutedEventArgs e)
|
|
{
|
|
// Add the app to startup when the checkbox is checked
|
|
AddApplicationToStartup();
|
|
}
|
|
|
|
private void cbStartWithWindows_Unchecked(object sender, RoutedEventArgs e)
|
|
{
|
|
// Remove the app from startup when the checkbox is unchecked
|
|
RemoveApplicationFromStartup();
|
|
}
|
|
|
|
private void AddApplicationToStartup()
|
|
{
|
|
// Get the path to the executable
|
|
string exePath = Process.GetCurrentProcess().MainModule.FileName;
|
|
|
|
// Open the registry key for the current user's startup applications
|
|
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
|
|
|
|
if (key != null)
|
|
{
|
|
// Add the application to the startup list
|
|
key.SetValue(appName, exePath);
|
|
}
|
|
}
|
|
|
|
private void RemoveApplicationFromStartup()
|
|
{
|
|
// Open the registry key for the current user's startup applications
|
|
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
|
|
|
|
if (key != null)
|
|
{
|
|
// Remove the application from the startup list
|
|
key.DeleteValue(appName, false);
|
|
}
|
|
}
|
|
|
|
private void timePicker_SelectedTimeChanged(object sender, HandyControl.Data.FunctionEventArgs<DateTime?> e)
|
|
{
|
|
if(timePicker.SelectedTime!= Properties.Settings.Default.RestartTime)
|
|
{
|
|
Properties.Settings.Default.RestartTime = timePicker.SelectedTime == null ? DateTime.Today.AddHours(2) : (DateTime)timePicker.SelectedTime;
|
|
Properties.Settings.Default.Save();
|
|
SetupAutoRestart();
|
|
}
|
|
|
|
}
|
|
private DispatcherTimer timer;
|
|
private DispatcherTimer checkFlagTimer;
|
|
private void SetupAutoRestart()
|
|
{
|
|
DateTime now = DateTime.Now;
|
|
var dt = Properties.Settings.Default.RestartTime;
|
|
DateTime nextRestart = DateTime.Today.AddHours(dt.Hour).AddMinutes(dt.Minute).AddSeconds(dt.Second);
|
|
|
|
// If the restart time for today has already passed, set it for tomorrow
|
|
if (nextRestart <= now)
|
|
{
|
|
nextRestart = nextRestart.AddDays(1);
|
|
}
|
|
|
|
TimeSpan timeUntilRestart = nextRestart - now;
|
|
|
|
if (timer != null)
|
|
{
|
|
timer.Stop();
|
|
}
|
|
|
|
// Set up the timer to wait until the specified restart time
|
|
if (timer != null)
|
|
{
|
|
try
|
|
{
|
|
timer.Tick -= Timer_Tick;
|
|
timer.Stop();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
}
|
|
}
|
|
timer = new DispatcherTimer
|
|
{
|
|
Interval = timeUntilRestart
|
|
};
|
|
timer.Tick += Timer_Tick;
|
|
timer.Start();
|
|
}
|
|
private void Timer_Tick(object sender, EventArgs e)
|
|
{
|
|
timer.Stop();
|
|
|
|
if (checkFlagTimer != null)
|
|
{
|
|
checkFlagTimer.Stop();
|
|
}
|
|
|
|
checkFlagTimer = new DispatcherTimer
|
|
{
|
|
Interval = TimeSpan.FromSeconds(30)
|
|
};
|
|
checkFlagTimer.Tick += CheckFlagTimer_Tick;
|
|
checkFlagTimer.Start();
|
|
}
|
|
|
|
private void CheckFlagTimer_Tick(object? sender, EventArgs e)
|
|
{
|
|
checkFlagTimer.IsEnabled = false;
|
|
if (AppData.GetTransactionCode() == "")
|
|
{
|
|
Log.Information("Restart app");
|
|
AppData.DeviceStatus.Description = "Restart App";
|
|
APIModels.SendEvent(AppData.DeviceStatus);
|
|
RestartApplication();
|
|
}
|
|
checkFlagTimer.IsEnabled = true;
|
|
}
|
|
|
|
private void RestartApplication()
|
|
{
|
|
string exePath = Process.GetCurrentProcess().MainModule.FileName;
|
|
|
|
Process.Start(exePath);
|
|
|
|
System.Windows.Application.Current.Shutdown();
|
|
}
|
|
}
|
|
}
|