2020-07-07 07:15:45 +12:00
|
|
|
|
// <copyright file="Config.cs" company="PlaceholderCompany">
|
|
|
|
|
// Copyright (c) PlaceholderCompany. All rights reserved.
|
|
|
|
|
// </copyright>
|
2020-03-28 08:43:16 +13:00
|
|
|
|
|
|
|
|
|
namespace SystemTrayMenu
|
|
|
|
|
{
|
2021-04-17 04:59:35 +12:00
|
|
|
|
using System;
|
2020-07-07 07:15:45 +12:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Windows.Forms;
|
2020-08-08 01:24:25 +12:00
|
|
|
|
using Microsoft.Win32;
|
2020-07-08 03:05:19 +12:00
|
|
|
|
using SystemTrayMenu.UserInterface.FolderBrowseDialog;
|
2020-07-07 07:15:45 +12:00
|
|
|
|
using SystemTrayMenu.Utilities;
|
|
|
|
|
|
2020-03-28 08:43:16 +13:00
|
|
|
|
public static class Config
|
|
|
|
|
{
|
2021-04-17 12:39:48 +12:00
|
|
|
|
private static bool readDarkModeDone;
|
|
|
|
|
private static bool isDarkMode;
|
|
|
|
|
private static bool readHideFileExtdone;
|
|
|
|
|
private static bool isHideFileExtension;
|
2021-04-17 07:30:45 +12:00
|
|
|
|
|
|
|
|
|
public static bool IsHideFileExtdone => IsHideFileExtension();
|
2020-08-08 01:24:25 +12:00
|
|
|
|
|
2020-03-28 08:43:16 +13:00
|
|
|
|
public static string Path => Properties.Settings.Default.PathDirectory;
|
|
|
|
|
|
|
|
|
|
public static void UpgradeIfNotUpgraded()
|
|
|
|
|
{
|
|
|
|
|
if (!Properties.Settings.Default.IsUpgraded)
|
|
|
|
|
{
|
|
|
|
|
Properties.Settings.Default.Upgrade();
|
|
|
|
|
Properties.Settings.Default.IsUpgraded = true;
|
|
|
|
|
Properties.Settings.Default.Save();
|
|
|
|
|
|
|
|
|
|
FileVersionInfo versionInfo = FileVersionInfo.
|
|
|
|
|
GetVersionInfo(Assembly.GetEntryAssembly().Location);
|
2021-04-17 04:59:35 +12:00
|
|
|
|
string upgradedFromPath = $"%localappdata%\\{versionInfo.CompanyName}\\";
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
upgradedFromPath = System.IO.Path.GetFullPath(upgradedFromPath);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
if (ex is ArgumentException ||
|
|
|
|
|
ex is System.Security.SecurityException ||
|
|
|
|
|
ex is NotSupportedException ||
|
|
|
|
|
ex is PathTooLongException)
|
|
|
|
|
{
|
|
|
|
|
Log.Warn($"Resolve path {upgradedFromPath} failed", ex);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Log.Info($"Settings upgraded from {upgradedFromPath}");
|
2020-03-28 08:43:16 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool LoadOrSetByUser()
|
|
|
|
|
{
|
2020-10-25 03:07:40 +13:00
|
|
|
|
bool pathOK = Directory.Exists(Path);
|
2020-03-28 08:43:16 +13:00
|
|
|
|
|
|
|
|
|
if (!pathOK)
|
|
|
|
|
{
|
2020-05-31 23:43:12 +12:00
|
|
|
|
string textFirstStart = Translator.GetText("TextFirstStart");
|
2020-07-07 07:15:45 +12:00
|
|
|
|
MessageBox.Show(
|
|
|
|
|
textFirstStart,
|
|
|
|
|
Translator.GetText("SystemTrayMenu"),
|
2020-05-31 23:43:12 +12:00
|
|
|
|
MessageBoxButtons.OK);
|
|
|
|
|
ShowHelpFAQ();
|
2020-03-28 08:43:16 +13:00
|
|
|
|
pathOK = SetFolderByUser();
|
|
|
|
|
}
|
2020-07-07 07:15:45 +12:00
|
|
|
|
|
2020-03-28 08:43:16 +13:00
|
|
|
|
return pathOK;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-05 05:43:47 +12:00
|
|
|
|
public static bool SetFolderByUser(bool save = true)
|
2020-03-28 08:43:16 +13:00
|
|
|
|
{
|
|
|
|
|
bool pathOK = false;
|
|
|
|
|
bool userAborted = false;
|
2020-06-06 02:42:12 +12:00
|
|
|
|
using (FolderDialog dialog = new FolderDialog())
|
2020-03-28 08:43:16 +13:00
|
|
|
|
{
|
2020-06-06 02:42:12 +12:00
|
|
|
|
dialog.InitialFolder = Path;
|
2020-03-28 08:43:16 +13:00
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
{
|
2020-06-06 02:42:12 +12:00
|
|
|
|
if (dialog.ShowDialog() == DialogResult.OK)
|
2020-03-28 08:43:16 +13:00
|
|
|
|
{
|
2020-06-06 02:42:12 +12:00
|
|
|
|
if (Directory.Exists(dialog.Folder))
|
2020-03-28 08:43:16 +13:00
|
|
|
|
{
|
|
|
|
|
pathOK = true;
|
|
|
|
|
Properties.Settings.Default.PathDirectory =
|
2020-06-06 02:42:12 +12:00
|
|
|
|
dialog.Folder;
|
2020-05-05 05:43:47 +12:00
|
|
|
|
if (save)
|
|
|
|
|
{
|
|
|
|
|
Properties.Settings.Default.Save();
|
|
|
|
|
}
|
2020-03-28 08:43:16 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
userAborted = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
while (!pathOK && !userAborted);
|
2020-07-07 07:15:45 +12:00
|
|
|
|
}
|
2020-03-28 08:43:16 +13:00
|
|
|
|
|
|
|
|
|
return pathOK;
|
|
|
|
|
}
|
2020-05-31 23:43:12 +12:00
|
|
|
|
|
|
|
|
|
internal static void ShowHelpFAQ()
|
|
|
|
|
{
|
2020-06-06 02:42:12 +12:00
|
|
|
|
string browserPath = FileUrl.GetDefaultBrowserPath();
|
2020-06-06 23:46:12 +12:00
|
|
|
|
if (!string.IsNullOrEmpty(browserPath))
|
2020-06-06 02:42:12 +12:00
|
|
|
|
{
|
|
|
|
|
Process.Start(browserPath, "https://github.com/Hofknecht/SystemTrayMenu#FAQ");
|
|
|
|
|
}
|
2020-05-31 23:43:12 +12:00
|
|
|
|
}
|
2020-08-08 01:24:25 +12:00
|
|
|
|
|
2021-04-17 07:30:45 +12:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Read the OS setting whether dark mode is enabled.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>true = Dark mode; false = Light mode.</returns>
|
2020-08-08 01:24:25 +12:00
|
|
|
|
internal static bool IsDarkMode()
|
|
|
|
|
{
|
2021-04-17 07:30:45 +12:00
|
|
|
|
if (!readDarkModeDone)
|
2020-08-08 01:24:25 +12:00
|
|
|
|
{
|
2021-04-17 07:30:45 +12:00
|
|
|
|
// 0 = Dark mode, 1 = Light mode
|
|
|
|
|
if (Properties.Settings.Default.IsDarkModeAlwaysOn ||
|
|
|
|
|
IsRegistryValueThisValue(
|
|
|
|
|
@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize",
|
|
|
|
|
"AppsUseLightTheme",
|
|
|
|
|
"0"))
|
2020-08-08 01:24:25 +12:00
|
|
|
|
{
|
|
|
|
|
isDarkMode = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
readDarkModeDone = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return isDarkMode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-04-17 07:30:45 +12:00
|
|
|
|
/// Read the OS setting whether HideFileExt enabled.
|
2020-08-08 01:24:25 +12:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>true = Dark mode; false = Light mode.</returns>
|
2021-04-17 07:30:45 +12:00
|
|
|
|
internal static bool IsHideFileExtension()
|
|
|
|
|
{
|
|
|
|
|
if (!readHideFileExtdone)
|
|
|
|
|
{
|
|
|
|
|
// 0 = To show extensions, 1 = To hide extensions
|
|
|
|
|
if (IsRegistryValueThisValue(
|
|
|
|
|
@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced",
|
|
|
|
|
"HideFileExt",
|
|
|
|
|
"1"))
|
|
|
|
|
{
|
|
|
|
|
isHideFileExtension = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
readHideFileExtdone = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return isHideFileExtension;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static bool IsRegistryValueThisValue(string keyName, string valueName, string value)
|
2020-08-08 01:24:25 +12:00
|
|
|
|
{
|
2021-04-17 07:30:45 +12:00
|
|
|
|
bool isRegistryValueThisValue = false;
|
|
|
|
|
|
|
|
|
|
try
|
2021-04-14 06:02:54 +12:00
|
|
|
|
{
|
2021-04-17 07:30:45 +12:00
|
|
|
|
object registryHideFileExt = Registry.GetValue(keyName, valueName, 1);
|
|
|
|
|
|
|
|
|
|
if (registryHideFileExt == null)
|
|
|
|
|
{
|
|
|
|
|
Log.Info($"Could not read registry keyName:{keyName} valueName:{valueName}");
|
|
|
|
|
}
|
|
|
|
|
else if (registryHideFileExt.ToString() == value)
|
|
|
|
|
{
|
|
|
|
|
isRegistryValueThisValue = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
if (ex is System.Security.SecurityException ||
|
|
|
|
|
ex is IOException)
|
|
|
|
|
{
|
|
|
|
|
Log.Warn($"Could not read registry keyName:{keyName} valueName:{valueName}", ex);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2021-04-14 06:02:54 +12:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-17 07:30:45 +12:00
|
|
|
|
return isRegistryValueThisValue;
|
2020-08-08 01:24:25 +12:00
|
|
|
|
}
|
2020-03-28 08:43:16 +13:00
|
|
|
|
}
|
|
|
|
|
}
|