This commit is contained in:
Stephan Maier
2024-08-27 08:10:27 +02:00
parent eb5c2fa502
commit 647f938eee
617 changed files with 73086 additions and 7137 deletions

View File

@@ -0,0 +1,364 @@
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows;
using static FSI.BT.Tools.Global.Settings.Cmd;
using static FSI.BT.Tools.Global.Settings.Exe;
namespace FSI.BT.Tools.Global.Commands
{
/// <summary>
/// Shows the main window.
/// </summary>
public class CmdCommand : CommandBase<CmdCommand>
{
public override void Execute(object parameter)
{
if (parameter is not string)
{
Global.Vars.Log.Error("Parameter ist kein String");
return;
}
var cmds = Vars.GlobalSettings.Cmds.ToList();
ICmd selectedCmd = null;
switch ((string)parameter)
{
case "DarkMode":
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize", true);
if (key.GetValue("AppsUseLightTheme") != null)
{
if (Convert.ToInt32(key.GetValue("AppsUseLightTheme")) == 0)
key.SetValue("AppsUseLightTheme", 1); //sets 'someData' in 'someValue'
else
key.SetValue("AppsUseLightTheme", 0); //sets 'someData' in 'someValue'
key.Close();
}
return;
case "Epl.Prj":
Lib.Guis.Prj.Mgt.FrmMain frmMainEplPrj = new()
{
ShowPdf = false,
CloseAtLostFocus = true,
WindowStartupLocation = WindowStartupLocation.CenterScreen,
Path = Helpers.GetFolderByName.Get(Vars.GlobalSettings.Folders, "EplPrj").path,
EplExe = GetExeByCmdName("Epl").ExePath,
};
frmMainEplPrj.Show();
return;
case "Epl.Pdf":
Lib.Guis.Prj.Mgt.FrmMain frmMainEplPdf = new()
{
ShowPdf = true,
CloseAtLostFocus = true,
WindowStartupLocation = WindowStartupLocation.CenterScreen,
Path = Helpers.GetFolderByName.Get(Vars.GlobalSettings.Folders, "EplPdf").path
};
frmMainEplPdf.Show();
return;
case "Epl.PdfMgt":
Lib.Guis.Pdf.Mgt.FrmMain frmMainEplPdfMgt = new()
{
CloseAtLostFocus = true
};
frmMainEplPdfMgt.Show();
return;
case "DeEncrypt":
Lib.Guis.DeEncryptMessage.FrmMain frmMainDeEnCrypt = new()
{
Password = AppDomain.CurrentDomain.FriendlyName,
CloseAtLostFocus = true,
WindowStartupLocation = WindowStartupLocation.CenterScreen,
};
frmMainDeEnCrypt.Show();
return;
case "StarterCsvExporter":
Lib.Guis.SieStarterCsvExporter.FrmMain frmMain = new();
frmMain.Show();
return;
case "Folder":
Lib.Guis.Folder.Mgt.FrmMain frmFolderMgtMain = new()
{
CloseAtLostFocus = true,
Data = Global.Vars.GlobalSettings.Folders
};
frmFolderMgtMain.Show();
return;
//case "TxtToClip":
// Lib.Guis.TxtToClip.Mgt.FrmMain frmTxtToClipMain = new()
// {
// CloseAtLostFocus = true,
// InputData = Global.AppSettings.TxtToClip
// };
// frmTxtToClipMain.Show();
// return;
case "Rdp.Mgt":
Lib.Guis.Rdp.Mgt.FrmMain frmRdpMain = new()
{
CloseAtLostFocus = true,
InputData = Global.Vars.GlobalSettings.Rdps,
Exe = GetExeByCmdName("Rdp").ExePath,
FrmTitle = "Remotedesktopverbindungen",
};
frmRdpMain.Show();
break;
case "WebRadio":
//Lib.Guis.WebRadio.FrmMain frmWebRadio = new()
//{
// CloseAtLostFocus = false,
// InputData = Global.AppSettings.WebRadioUrls,
//};
//frmWebRadio.Show();
break;
default:
foreach (ICmd cmd in cmds)
{
if (String.Equals(parameter.ToString(), cmd.Cmd))
selectedCmd = cmd;
}
break;
}
if (selectedCmd == null)
return;
OpenExe(selectedCmd);
OpenUrl(selectedCmd);
}
public override bool CanExecute(object parameter)
{
if (parameter == null)
return false;
var cmds = Global.Vars.GlobalSettings.Cmds.ToList();
ICmd selectedCmd = null;
switch ((string)parameter)
{
case "DarkMode":
return Vars.AdminRights;
case "Epl.Prj":
return true;
case "Epl.Pdf":
return true;
case "Epl.PdfMgt":
return Vars.AdminRights;
case "DeEncrypt":
return Vars.AdminRights;
case "StarterCsvExporter":
return Vars.AdminRights;
case "Folder":
return Vars.GlobalSettings.Folders != null;
//case "TxtToClip":
// return Vars.GlobalSettings.TxtToClip != null;
case "Rdp.Mgt":
return Vars.GlobalSettings.Rdps != null;
case "WebRadio":
return false;// Global.AppSettings.WebRadioUrls != null;
default:
foreach (ICmd cmd in cmds)
{
if (String.Equals(parameter.ToString(), cmd.Cmd))
selectedCmd = cmd;
}
break;
}
if (selectedCmd == null)
return false;
foreach (var file in selectedCmd.Exe.ToList())
{
if (File.Exists(Environment.ExpandEnvironmentVariables(file.ExePath.Trim())))
return true;
else if (File.Exists(Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), file.ExePath.Trim())))
return true;
}
foreach (var url in selectedCmd.Urls)
{
if (url != String.Empty)
return true;
}
return false;
}
private static void OpenExe(ICmd selectedCmd)
{
IExe selectedFile = GetApp(selectedCmd.Exe);
if (selectedFile == null)
return;
if (selectedFile.ExePath == String.Empty)
return;
if (ProgramIsRunning(selectedFile.ExePath))
{
ProgramToFront(selectedFile.ExePath);
Vars.Log.Info("Anwendung \"{0}\" wurde in den Vordergrund gebracht", selectedFile.ExePath);
}
else
{
var selectedFileExePath = string.Empty;
if (File.Exists(Environment.ExpandEnvironmentVariables(selectedFile.ExePath.Trim())))
selectedFileExePath = Environment.ExpandEnvironmentVariables(selectedFile.ExePath.Trim());
if (File.Exists(Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), selectedFile.ExePath.Trim())))
selectedFileExePath = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), selectedFile.ExePath.Trim());
var abc = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), selectedFileExePath.Trim());
Process process = new();
process.StartInfo.FileName = selectedFileExePath;
process.StartInfo.WorkingDirectory = selectedFile.Path ?? Path.GetDirectoryName(selectedFileExePath);
process.StartInfo.Arguments = selectedFile.Arguments ?? string.Empty;
try
{
process.Start();
Vars.Log.Info("Anwendung \"{0}\" wurde gestartet", selectedFile.ExePath);
}
catch (System.ComponentModel.Win32Exception ex) when (ex.NativeErrorCode == 740)
{
try
{
process.StartInfo.UseShellExecute = true;
process.StartInfo.Verb = "runas";
process.Start();
Vars.Log.Info("Anwendung \"{0}\" wurde als Admin gestartet", selectedFile.ExePath);
}
catch (Exception ex2)
{
Vars.Log.Info("Anwendung konnte durch folgenden Fehler \"{0}\" nicht gestartet werden.", ex2.Message);
}
}
}
}
private static void OpenUrl(ICmd selectedCmd)
{
foreach (var url in selectedCmd.Urls)
{
if (url == String.Empty)
return;
Process.Start(new ProcessStartInfo(url.Replace("&", "^&")) { UseShellExecute = true });
Vars.Log.Info("Link \"{0}\" wurde geföffnet.", url.Replace("&", "^&"));
Thread.Sleep(100);
}
}
private static IExe GetExeByCmdName(string cmdName)
{
foreach (var cmd in Vars.GlobalSettings.Cmds)
{
if (string.Equals(cmd.Cmd, cmdName, StringComparison.InvariantCultureIgnoreCase))
return GetApp(cmd.Exe);
}
return null;
}
private static bool ProgramIsRunning(string FullPath)
{
string FilePath = Path.GetDirectoryName(FullPath);
string FileName = Path.GetFileNameWithoutExtension(FullPath).ToLower();
bool isRunning = false;
Process[] pList = Process.GetProcessesByName(FileName);
foreach (Process p in pList)
{
if (p.MainModule.FileName.StartsWith(FilePath, StringComparison.InvariantCultureIgnoreCase))
{
isRunning = true;
break;
}
}
return isRunning;
}
private static IExe GetApp(IEnumerable<IExe> files)
{
if (files.ToList().Count == 0)
return null;
var selectedFile = files.ToList()[0];
foreach (var file in files.ToList())
{
if (File.Exists(Environment.ExpandEnvironmentVariables(file.ExePath.Trim())))
selectedFile = (IExe)file;
else
continue;
}
return selectedFile;
}
[System.Runtime.InteropServices.DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr handle);
[System.Runtime.InteropServices.DllImport("User32.dll")]
private static extern bool ShowWindow(IntPtr handle, int nCmdShow);
[System.Runtime.InteropServices.DllImport("User32.dll")]
private static extern bool IsIconic(IntPtr handle);
private static void ProgramToFront(string FullPath)
{
string FilePath = Path.GetDirectoryName(FullPath);
string FileName = Path.GetFileNameWithoutExtension(FullPath).ToLower();
Process[] pList = Process.GetProcessesByName(FileName);
foreach (Process p in pList)
{
if (p.MainModule.FileName.StartsWith(FilePath, StringComparison.InvariantCultureIgnoreCase))
{
IntPtr handle = p.MainWindowHandle;
if (IsIconic(handle))
ShowWindow(handle, 9);
SetForegroundWindow(handle);
break;
}
}
}
}
}

View File

@@ -0,0 +1,153 @@
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
//using Hardcodet.Wpf.TaskbarNotification;
namespace FSI.BT.Tools.Global.Commands
{
/// <summary>
/// Basic implementation of the <see cref="ICommand"/>
/// interface, which is also accessible as a markup
/// extension.
/// </summary>
public abstract class CommandBase<T> : MarkupExtension, ICommand
where T : class, ICommand, new()
{
/// <summary>
/// A singleton instance.
/// </summary>
private static T command;
/// <summary>
/// Gets a shared command instance.
/// </summary>
public override object ProvideValue(IServiceProvider serviceProvider)
{
if (command == null) command = new T();
return command;
}
/// <summary>
/// Fires when changes occur that affect whether
/// or not the command should execute.
/// </summary>
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
/// <summary>
/// Defines the method to be called when the command is invoked.
/// </summary>
/// <param name="parameter">Data used by the command.
/// If the command does not require data to be passed,
/// this object can be set to null.
/// </param>
public abstract void Execute(object parameter);
/// <summary>
/// Defines the method that determines whether the command
/// can execute in its current state.
/// </summary>
/// <returns>
/// This default implementation always returns true.
/// </returns>
/// <param name="parameter">Data used by the command.
/// If the command does not require data to be passed,
/// this object can be set to null.
/// </param>
public virtual bool CanExecute(object parameter)
{
return !IsDesignMode;
}
public static bool IsDesignMode
{
get
{
return (bool)
DependencyPropertyDescriptor.FromProperty(DesignerProperties.IsInDesignModeProperty,
typeof(FrameworkElement))
.Metadata.DefaultValue;
}
}
///// <summary>
///// Resolves the window that owns the TaskbarIcon class.
///// </summary>
///// <param name="commandParameter"></param>
///// <returns>Window</returns>
//protected Window GetTaskbarWindow(object commandParameter)
//{
// if (IsDesignMode)
// return null;
// // get the showcase window off the taskbar icon
// var tb = commandParameter as TaskbarIcon;
// return tb == null ? null : TryFindParent<Window>(tb);
//}
#region TryFindParent helper
/// <summary>
/// Finds a parent of a given item on the visual tree.
/// </summary>
/// <typeparam name="TParent">The type of the queried item.</typeparam>
/// <param name="child">A direct or indirect child of the
/// queried item.</param>
/// <returns>The first parent item that matches the submitted
/// type parameter. If not matching item can be found, a null
/// reference is being returned.</returns>
public static TParent TryFindParent<TParent>(DependencyObject child) where TParent : DependencyObject
{
//get parent item
DependencyObject parentObject = GetParentObject(child);
//we've reached the end of the tree
if (parentObject == null) return null;
//check if the parent matches the type we're looking for
if (parentObject is TParent parent)
{
return parent;
}
//use recursion to proceed with next level
return TryFindParent<TParent>(parentObject);
}
/// <summary>
/// This method is an alternative to WPF's
/// <see cref="VisualTreeHelper.GetParent"/> method, which also
/// supports content elements. Keep in mind that for content element,
/// this method falls back to the logical tree of the element!
/// </summary>
/// <param name="child">The item to be processed.</param>
/// <returns>The submitted item's parent, if available. Otherwise
/// null.</returns>
public static DependencyObject GetParentObject(DependencyObject child)
{
if (child == null) return null;
if (child is ContentElement contentElement)
{
DependencyObject parent = ContentOperations.GetParent(contentElement);
if (parent != null) return parent;
FrameworkContentElement fce = contentElement as FrameworkContentElement;
return fce?.Parent;
}
//if it's not a ContentElement, rely on VisualTreeHelper
return VisualTreeHelper.GetParent(child);
}
#endregion
}
}

View File

@@ -0,0 +1,21 @@
using System.Windows;
namespace FSI.BT.Tools.Global.Commands
{
/// <summary>
/// Shows the main window.
/// </summary>
public class ExitCommand : CommandBase<ExitCommand>
{
public override void Execute(object parameter)
{
Vars.Log.Info("Anwendung wurde beendet!");
Application.Current.Shutdown();
}
public override bool CanExecute(object parameter)
{
return true;
}
}
}

View File

@@ -0,0 +1,37 @@
using System.Windows;
namespace FSI.BT.Tools.Global.Commands
{
/// <summary>
/// Shows the main window.
/// </summary>
public class LoginCommand : CommandBase<LoginCommand>
{
public override void Execute(object parameter)
{
Lib.Guis.AutoPw.FrmMain frmMain = new Lib.Guis.AutoPw.FrmMain()
{
CloseAtLostFocus = false,
WindowStartupLocation = WindowStartupLocation.CenterScreen,
};
frmMain.ShowDialog();
Global.Vars.UserRights =
Vars.AdminRights = frmMain.PwOk;
if (frmMain.PwOk)
{
Vars.Log.Info("Admin-Passowrt wurde korrekt eingegben.");
}
else
{
Vars.Log.Info("Anmeldung wurde vom Benutzer abgebrochen.");
}
}
public override bool CanExecute(object parameter)
{
return true;
}
}
}

View File

@@ -0,0 +1,24 @@
namespace FSI.BT.Tools.Global.Commands
{
/// <summary>
/// Shows the main window.
/// </summary>
public class ProcessCommand : CommandBase<ProcessCommand>
{
public override void Execute(object parameter)
{
RadialMenu.UserInterface.FrmProcesses frm = new RadialMenu.UserInterface.FrmProcesses()
{
WinCC = Vars.WinCC
};
frm.Iba = Vars.Iba;
frm.ShowDialog();
}
public override bool CanExecute(object parameter)
{
return Vars.AdminRights;
}
}
}