Skip to content

Commit

Permalink
FrmQuickSetup: reset user settings
Browse files Browse the repository at this point in the history
  • Loading branch information
d2phap committed Apr 27, 2024
1 parent f68bdc1 commit df3d542
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 68 deletions.
60 changes: 40 additions & 20 deletions Source/igcmd/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using ImageGlass;
using ImageGlass.Base;
using ImageGlass.Base.WinApi;
using ImageGlass.Settings;
Expand Down Expand Up @@ -89,23 +90,37 @@ private static int Main(string[] args)
#endregion


try
{
// load application configs
Config.Load();
}
catch (Exception)
if (CmdArgs.Length == 0)
{
Config.Language = [];
LoadAllConfigs();
return Config.ShowDefaultIgCommandError(nameof(igcmd));
}

var topCmd = CmdArgs[0].ToLowerInvariant().Trim();

if (CmdArgs.Length == 0)

#region QUICK_SETUP
if (topCmd == IgCommands.QUICK_SETUP)
{
return Config.ShowDefaultIgCommandError(nameof(igcmd));
// load the non-user settings
var nonUserConfig = Source.LoadNonUserConfigs();

// load language setting
var langPath = nonUserConfig.GetValueEx(nameof(Config.Language), "English");
Config.Language = new IgLang(langPath, App.StartUpDir(Dir.Language));

// load theme
Config.LoadThemePack(WinColorsApi.IsDarkMode, true, true, false);


Application.Run(new Tools.FrmQuickSetup());
return (int)IgExitCode.Done;
}
#endregion

var topCmd = CmdArgs[0].ToLowerInvariant().Trim();

// load all configs
LoadAllConfigs();


#region SET_WALLPAPER <string imgPath> [int style]
Expand Down Expand Up @@ -187,16 +202,6 @@ private static int Main(string[] args)
#endregion


#region QUICK_SETUP
if (topCmd == IgCommands.QUICK_SETUP)
{
Application.Run(new Tools.FrmQuickSetup());

return (int)IgExitCode.Done;
}
#endregion


#region INSTALL_LANGUAGES [string filePaths]
if (topCmd == IgCommands.INSTALL_LANGUAGES)
{
Expand Down Expand Up @@ -252,4 +257,19 @@ private static int Main(string[] args)
}


/// <summary>
/// Loads all user configs
/// </summary>
private static void LoadAllConfigs()
{
try
{
// load application configs
Config.Load();
}
catch (Exception)
{
Config.Language = [];
}
}
}
79 changes: 79 additions & 0 deletions Source/igcmd/Tools/CmdHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
ImageGlass Project - Image viewer for Windows
Copyright (C) 2010 - 2024 DUONG DIEU PHAP
Project homepage: https://imageglass.org
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using igcmd.Tools;
using ImageGlass.Base;
using ImageGlass.Settings;
using System.Diagnostics;

namespace igcmd;


public static class CmdHelper
{
/// <summary>
/// Kills ImageGlass processes.
/// Returns <c>true</c> if all processes are terminated.
/// </summary>
public static bool KillImageGlassProcessesAsync(Form? formOwner = null, bool showConfirm = true)
{
// get all IG processes
var igProcesses = Process.GetProcesses()
.Where(p =>
p.Id != Environment.ProcessId &&
p.ProcessName.Contains("ImageGlass")
).ToList();


// show user confirm
if (igProcesses.Count > 0)
{
var canProceed = true;
if (showConfirm)
{
var result = Config.ShowInfo(formOwner,
title: formOwner?.Text ?? string.Empty,
heading: Config.Language[$"{nameof(FrmQuickSetup)}._ConfirmCloseProcess"],
icon: ImageGlass.Base.WinApi.ShellStockIcon.SIID_HELP,
buttons: PopupButton.Yes_No);

canProceed = result.ExitResult == PopupExitResult.OK;
}

if (!canProceed) return false;

// Kill all processes
igProcesses.ForEach(p => p.Kill());
}

return true;
}



/// <summary>
/// Launch ImageGlass app
/// </summary>
public static void LaunchImageGlass()
{
using var p = new Process();
p.StartInfo.FileName = App.IGExePath;
p.Start();
}

}
63 changes: 15 additions & 48 deletions Source/igcmd/Tools/FrmQuickSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ You should have received a copy of the GNU General Public License
using ImageGlass;
using ImageGlass.Base;
using ImageGlass.Settings;
using System.Diagnostics;

namespace igcmd.Tools;

Expand Down Expand Up @@ -93,7 +92,7 @@ protected override async Task OnWeb2MessageReceivedAsync(Web2MessageReceivedEven
Config.QuickSetupVersion = Const.QUICK_SETUP_VERSION;
await Config.WriteAsync();

LaunchImageGlass();
CmdHelper.LaunchImageGlass();
Close();
}
else if (e.Name.Equals("LOAD_LANGUAGE", StringComparison.InvariantCultureIgnoreCase))
Expand All @@ -110,21 +109,28 @@ protected override async Task OnWeb2MessageReceivedAsync(Web2MessageReceivedEven
#endregion // Protected / override methods


// Private methods
#region Private methods
private async Task ApplySettingsAsync(string settingJson)
{
// try to kill all ImageGlass processes
if (!CmdHelper.KillImageGlassProcessesAsync(this, true)) return;


// don't auto-show Quick Setup again
Config.QuickSetupVersion = Const.QUICK_SETUP_VERSION;


// Parse settings JSON
#region Parse settings JSON
var dict = BHelper.ParseJson<Dictionary<string, object?>>(settingJson);

if (dict.TryGetValue("Language", out var lang))
if (dict.TryGetValue(nameof(Config.Language), out var lang))
{
Config.Language = new IgLang(lang?.ToString(), App.StartUpDir(Dir.Language));
}

if (dict.TryGetValue("ColorProfile", out var enableColorProfileObj))
if (dict.TryGetValue(nameof(Config.ColorProfile), out var enableColorProfileObj))
{
var enableColorProfile = enableColorProfileObj
?.ToString()
Expand All @@ -135,7 +141,7 @@ private async Task ApplySettingsAsync(string settingJson)
: nameof(ColorProfileOption.None);
}

if (dict.TryGetValue("UseEmbeddedThumbnailRawFormats", out var useThumbnailRawFormatsObj))
if (dict.TryGetValue(nameof(Config.UseEmbeddedThumbnailRawFormats), out var useThumbnailRawFormatsObj))
{
Config.UseEmbeddedThumbnailRawFormats = useThumbnailRawFormatsObj
?.ToString()
Expand All @@ -145,38 +151,8 @@ private async Task ApplySettingsAsync(string settingJson)
#endregion // Parse settings JSON


// kill all ImageGlass processes and relaunch
#region kill all ImageGlass processes and relaunch

var igProcesses = Process.GetProcesses()
.Where(p =>
p.Id != Environment.ProcessId &&
p.ProcessName.Contains("ImageGlass")
).ToList();

if (igProcesses.Count > 0)
{
var result = Config.ShowInfo(this,
title: Text,
heading: Config.Language[$"{nameof(FrmQuickSetup)}._ConfirmCloseProcess"],
icon: ImageGlass.Base.WinApi.ShellStockIcon.SIID_HELP,
buttons: PopupButton.Yes_No);

if (result.ExitResult == PopupExitResult.OK)
{
// Kill all processes
igProcesses.ForEach(p => p.Kill());

await ApplyAndCloseAsync();
}
}
else
{
await ApplyAndCloseAsync();
}

#endregion // kill all ImageGlass processes and relaunch

// apply and restart ImageGlass
await ApplyAndCloseAsync();
}


Expand All @@ -185,19 +161,10 @@ private async Task ApplyAndCloseAsync()
// write settings
await Config.WriteAsync();

LaunchImageGlass();
CmdHelper.LaunchImageGlass();
Close();
}


/// <summary>
/// Launch ImageGlass app
/// </summary>
private static void LaunchImageGlass()
{
using var p = new Process();
p.StartInfo.FileName = App.IGExePath;
p.Start();
}
#endregion // Private methods

}

0 comments on commit df3d542

Please sign in to comment.