using System;
using System.Windows.Forms;
using System.Diagnostics;
//System.IO.Ports.SerialData
//System.Management.ManagementEventWatcher
//System.IO.FileSystemWatcher
namespace Trigger
{
///
/// The main
///
/// This is invisible
public partial class Main : Form
{
#region Properties
/// The logging
public Log Log;
/// The global EventManager
public Events.Manager EventMgr;
/// The global TaskManager
public Tasks.Manager TaskMgr;
private StatusView StatusView = null;
///
/// Checks whether this process has administrator privileges
///
///
public bool IsAdmin
{
get
{
bool isAdmin = false;
System.Security.Principal.WindowsIdentity winId = System.Security.Principal.WindowsIdentity.GetCurrent();
if (winId != null)
isAdmin = new System.Security.Principal.WindowsPrincipal(winId).IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator);
return isAdmin;
}
}
#endregion
#region Events
///
/// Is triggered when the application is already running and then gets called with command line arguments
///
private event Events.EventPlugin.EventValue OnCommandLineTrigger;
///
/// Is triggered when the application is already running and then gets called with command line arguments
///
public event Events.EventPlugin.EventValue CommandLineTrigger
{
add
{
OnCommandLineTrigger += value;
}
remove
{
OnCommandLineTrigger -= value;
}
}
#endregion
#region Constructor
///
public Main()
{
this.Visible = false;
InitializeComponent();
this.Log = new Log(this);
this.Log.Show();
if (this.IsAdmin)
this.Log.LogLine("Running with admin privileges", Log.Type.Other);
this.EventMgr = new Events.Manager(this);
this.TaskMgr = new Tasks.Manager(this);
this.Log.LogLine(new String('-', 32), Log.Type.Other);
this.RegisterEventForMessage(Classes.System.WindowsMessages.WM_USER);
MessageReceived += new Events.EventPlugin.EventValue(HandleNewInstance);
}
public Main(Options options) : this()
{
}
#endregion
#region Methods
///
/// Creates a new or refreshes the existing
///
public void RefreshStatus()
{
if (this.StatusView == null || this.StatusView.IsDisposed)
this.StatusView = new StatusView(this);
else
this.StatusView.Refresh();
this.StatusView.Show();
}
///
/// Check if the app was started as administrator or restart it with those permissions
/// This method has to be called during the task initialization process because otherwise the app will be restarted when trying to perform an action that requires administrator priviliges
///
///
/// Whether administrator privileges are possible and requested
/// When the user cancels UAC or some other error occurs this will return false.
///
///
public bool RequireAdministrator()
{
if (this.IsAdmin)
return true;
ProcessStartInfo procStart = new ProcessStartInfo(Process.GetCurrentProcess().MainModule.FileName, "-a");
procStart.UseShellExecute = true;
procStart.Verb = "runas"; // run as Admin
this.Log.LogLineDate("Requiring admin privileges...", Trigger.Log.Type.Other);
try
{
Process.Start(procStart);
}
catch (System.ComponentModel.Win32Exception e)
{
if (e.NativeErrorCode != 1223) // 1223 = user aborted UAC
throw;
}
return false;
}
///
/// Close the application
///
new public void Close()
{
this.Close(true);
}
///
/// Ask the user and close the application
///
/// Whether the user shall be asked first
public void Close(bool ask)
{
if (ask)
if (DialogResult.Yes != MessageBox.Show("Do you really want to exit Trigger4Win?", "Quit Trigger4Win", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
return;
base.Close();
}
#endregion
#region Event handler
///
/// Do not show this
///
///
///
private void Main_Shown(object sender, EventArgs e)
{
this.Visible = false;
}
///
/// Exit application
///
///
///
private void tsmNotifyIcon_Exit_Click(object sender, EventArgs e)
{
this.Close();
}
///
/// Show the Log
///
///
///
private void tmsNotifyIcon_Log_Click(object sender, EventArgs e)
{
this.Log.Show();
this.Log.BringToFront();
((ToolStripMenuItem)sender).Visible = false;
}
///
/// Show the log
///
///
///
private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Log.Show();
}
///
/// Show :
///
///
///
private void tsmNotifyIcon_Status_Click(object sender, EventArgs e)
{
this.RefreshStatus();
}
private void HandleNewInstance(object sender, Events.EventArgsValue e)
{
if (e.Value.Msg != (int)Classes.System.WindowsMessages.WM_USER)
return;
switch (e.Value.LParam.ToInt32())
{
case -1:
this.TaskMgr.Abort();
Application.Exit();
return;
case -2:
this.Show();
return;
}
switch (e.Value.WParam.ToInt32())
{
case 0:
return;
default:
if (OnCommandLineTrigger != null)
OnCommandLineTrigger(this, new Events.EventArgsValue(e.Value.WParam.ToInt32()));
return;
}
}
#endregion
}
}