Server/StartupManager.cs
changeset 92 787dee27fc0a
child 93 f0015e909f41
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Server/StartupManager.cs	Sun Jan 18 12:35:22 2015 +0100
     1.3 @@ -0,0 +1,246 @@
     1.4 +/*
     1.5 +
     1.6 +  This Source Code Form is subject to the terms of the Mozilla Public
     1.7 +  License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 +  file, You can obtain one at http://mozilla.org/MPL/2.0/.
     1.9 +
    1.10 +  Copyright (C) 2009-2010 Michael Möller <mmoeller@openhardwaremonitor.org>
    1.11 +
    1.12 +*/
    1.13 +
    1.14 +using Microsoft.Win32;
    1.15 +using SharpDisplayManager.TaskScheduler;
    1.16 +using System;
    1.17 +using System.IO;
    1.18 +using System.Runtime.InteropServices;
    1.19 +using System.Security;
    1.20 +using System.Security.Principal;
    1.21 +using System.Windows.Forms;
    1.22 +
    1.23 +namespace SharpDisplayManager
    1.24 +{
    1.25 +	public class StartupManager
    1.26 +	{
    1.27 +		private TaskSchedulerClass scheduler;
    1.28 +		private bool startup;
    1.29 +		private bool isAvailable;
    1.30 +
    1.31 +		private const string REGISTRY_RUN =
    1.32 +		  @"Software\Microsoft\Windows\CurrentVersion\Run";
    1.33 +
    1.34 +		private bool IsAdministrator()
    1.35 +		{
    1.36 +			try
    1.37 +			{
    1.38 +				WindowsIdentity identity = WindowsIdentity.GetCurrent();
    1.39 +				WindowsPrincipal principal = new WindowsPrincipal(identity);
    1.40 +				return principal.IsInRole(WindowsBuiltInRole.Administrator);
    1.41 +			}
    1.42 +			catch
    1.43 +			{
    1.44 +				return false;
    1.45 +			}
    1.46 +		}
    1.47 +
    1.48 +		public StartupManager()
    1.49 +		{
    1.50 +			int p = (int)System.Environment.OSVersion.Platform;
    1.51 +			if ((p == 4) || (p == 128))
    1.52 +			{
    1.53 +				scheduler = null;
    1.54 +				isAvailable = false;
    1.55 +				return;
    1.56 +			}
    1.57 +
    1.58 +			if (IsAdministrator())
    1.59 +			{
    1.60 +				try
    1.61 +				{
    1.62 +					scheduler = new TaskSchedulerClass();
    1.63 +					scheduler.Connect(null, null, null, null);
    1.64 +				}
    1.65 +				catch
    1.66 +				{
    1.67 +					scheduler = null;
    1.68 +				}
    1.69 +
    1.70 +				if (scheduler != null)
    1.71 +				{
    1.72 +					try
    1.73 +					{
    1.74 +						// check if the task scheduler is running
    1.75 +						IRunningTaskCollection collection = scheduler.GetRunningTasks(0);
    1.76 +
    1.77 +						ITaskFolder folder = scheduler.GetFolder("\\Sharp Display Manager");
    1.78 +						IRegisteredTask task = folder.GetTask("Startup");
    1.79 +						startup = (task != null) &&
    1.80 +						  (task.Definition.Triggers.Count > 0) &&
    1.81 +						  (task.Definition.Triggers[1].Type ==
    1.82 +							TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON) &&
    1.83 +						  (task.Definition.Actions.Count > 0) &&
    1.84 +						  (task.Definition.Actions[1].Type ==
    1.85 +							TASK_ACTION_TYPE.TASK_ACTION_EXEC) &&
    1.86 +						  (task.Definition.Actions[1] as IExecAction != null) &&
    1.87 +						  ((task.Definition.Actions[1] as IExecAction).Path ==
    1.88 +							Application.ExecutablePath);
    1.89 +					}
    1.90 +					catch (IOException)
    1.91 +					{
    1.92 +						startup = false;
    1.93 +					}
    1.94 +					catch (UnauthorizedAccessException)
    1.95 +					{
    1.96 +						scheduler = null;
    1.97 +					}
    1.98 +					catch (COMException)
    1.99 +					{
   1.100 +						scheduler = null;
   1.101 +					}
   1.102 +				}
   1.103 +			}
   1.104 +			else
   1.105 +			{
   1.106 +				scheduler = null;
   1.107 +			}
   1.108 +
   1.109 +			if (scheduler == null)
   1.110 +			{
   1.111 +				try
   1.112 +				{
   1.113 +					using (RegistryKey key =
   1.114 +					  Registry.CurrentUser.OpenSubKey(REGISTRY_RUN))
   1.115 +					{
   1.116 +						startup = false;
   1.117 +						if (key != null)
   1.118 +						{
   1.119 +							string value = (string)key.GetValue("SharpDisplayManager");
   1.120 +							if (value != null)
   1.121 +								startup = value == Application.ExecutablePath;
   1.122 +						}
   1.123 +					}
   1.124 +					isAvailable = true;
   1.125 +				}
   1.126 +				catch (SecurityException)
   1.127 +				{
   1.128 +					isAvailable = false;
   1.129 +				}
   1.130 +			}
   1.131 +			else
   1.132 +			{
   1.133 +				isAvailable = true;
   1.134 +			}
   1.135 +		}
   1.136 +
   1.137 +		private void CreateSchedulerTask()
   1.138 +		{
   1.139 +			ITaskDefinition definition = scheduler.NewTask(0);
   1.140 +			definition.RegistrationInfo.Description =
   1.141 +			  "This task starts the Sharp Display Manager on Windows startup.";
   1.142 +			definition.Principal.RunLevel =
   1.143 +			  TASK_RUNLEVEL.TASK_RUNLEVEL_HIGHEST;
   1.144 +			definition.Settings.DisallowStartIfOnBatteries = false;
   1.145 +			definition.Settings.StopIfGoingOnBatteries = false;
   1.146 +			definition.Settings.ExecutionTimeLimit = "PT0S";
   1.147 +
   1.148 +			ILogonTrigger trigger = (ILogonTrigger)definition.Triggers.Create(
   1.149 +			  TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON);
   1.150 +
   1.151 +			IExecAction action = (IExecAction)definition.Actions.Create(
   1.152 +			  TASK_ACTION_TYPE.TASK_ACTION_EXEC);
   1.153 +			action.Path = Application.ExecutablePath;
   1.154 +			action.WorkingDirectory =
   1.155 +			  Path.GetDirectoryName(Application.ExecutablePath);
   1.156 +
   1.157 +			ITaskFolder root = scheduler.GetFolder("\\");
   1.158 +			ITaskFolder folder;
   1.159 +			try
   1.160 +			{
   1.161 +				folder = root.GetFolder("Sharp Display Manager");
   1.162 +			}
   1.163 +			catch (IOException)
   1.164 +			{
   1.165 +				folder = root.CreateFolder("Sharp Display Manager", "");
   1.166 +			}
   1.167 +			folder.RegisterTaskDefinition("Startup", definition,
   1.168 +			  (int)TASK_CREATION.TASK_CREATE_OR_UPDATE, null, null,
   1.169 +			  TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, "");
   1.170 +		}
   1.171 +
   1.172 +		private void DeleteSchedulerTask()
   1.173 +		{
   1.174 +			ITaskFolder root = scheduler.GetFolder("\\");
   1.175 +			try
   1.176 +			{
   1.177 +				ITaskFolder folder = root.GetFolder("Sharp Display Manager");
   1.178 +				folder.DeleteTask("Startup", 0);
   1.179 +			}
   1.180 +			catch (IOException) { }
   1.181 +			try
   1.182 +			{
   1.183 +				root.DeleteFolder("Sharp Display Manager", 0);
   1.184 +			}
   1.185 +			catch (IOException) { }
   1.186 +		}
   1.187 +
   1.188 +		private void CreateRegistryRun()
   1.189 +		{
   1.190 +			RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);
   1.191 +			key.SetValue("SharpDisplayManager", Application.ExecutablePath);
   1.192 +		}
   1.193 +
   1.194 +		private void DeleteRegistryRun()
   1.195 +		{
   1.196 +			RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);
   1.197 +			key.DeleteValue("SharpDisplayManager");
   1.198 +		}
   1.199 +
   1.200 +		public bool IsAvailable
   1.201 +		{
   1.202 +			get { return isAvailable; }
   1.203 +		}
   1.204 +
   1.205 +		public bool Startup
   1.206 +		{
   1.207 +			get
   1.208 +			{
   1.209 +				return startup;
   1.210 +			}
   1.211 +			set
   1.212 +			{
   1.213 +				if (startup != value)
   1.214 +				{
   1.215 +					if (isAvailable)
   1.216 +					{
   1.217 +						if (scheduler != null)
   1.218 +						{
   1.219 +							if (value)
   1.220 +								CreateSchedulerTask();
   1.221 +							else
   1.222 +								DeleteSchedulerTask();
   1.223 +							startup = value;
   1.224 +						}
   1.225 +						else
   1.226 +						{
   1.227 +							try
   1.228 +							{
   1.229 +								if (value)
   1.230 +									CreateRegistryRun();
   1.231 +								else
   1.232 +									DeleteRegistryRun();
   1.233 +								startup = value;
   1.234 +							}
   1.235 +							catch (UnauthorizedAccessException)
   1.236 +							{
   1.237 +								throw new InvalidOperationException();
   1.238 +							}
   1.239 +						}
   1.240 +					}
   1.241 +					else
   1.242 +					{
   1.243 +						throw new InvalidOperationException();
   1.244 +					}
   1.245 +				}
   1.246 +			}
   1.247 +		}
   1.248 +	}
   1.249 +}
   1.250 \ No newline at end of file