moel@82: /*
moel@82:  
moel@344:   This Source Code Form is subject to the terms of the Mozilla Public
moel@344:   License, v. 2.0. If a copy of the MPL was not distributed with this
moel@344:   file, You can obtain one at http://mozilla.org/MPL/2.0/.
moel@82:  
moel@344:   Copyright (C) 2009-2010 Michael Möller <mmoeller@openhardwaremonitor.org>
moel@344: 	
moel@82: */
moel@82: 
moel@82: using System;
moel@82: using System.Collections.Generic;
moel@82: using System.IO;
moel@139: using System.Runtime.InteropServices;
moel@142: using System.Security;
moel@123: using System.Security.Principal;
moel@82: using System.Windows.Forms;
moel@82: using Microsoft.Win32;
moel@82: using OpenHardwareMonitor.TaskScheduler;
moel@82: 
moel@82: namespace OpenHardwareMonitor.GUI {
moel@82:   public class StartupManager {
moel@82: 
moel@82:     private TaskSchedulerClass scheduler;
moel@82:     private bool startup;
moel@142:     private bool isAvailable;
moel@82: 
moel@82:     private const string REGISTRY_RUN =
moel@82:       @"Software\Microsoft\Windows\CurrentVersion\Run";
moel@82: 
moel@123:     private bool IsAdministrator() {
moel@123:       try {
moel@123:         WindowsIdentity identity = WindowsIdentity.GetCurrent();
moel@123:         WindowsPrincipal principal = new WindowsPrincipal(identity);
moel@123:         return principal.IsInRole(WindowsBuiltInRole.Administrator);
moel@123:       } catch {
moel@123:         return false;
moel@123:       }
moel@123:     }
moel@123: 
moel@82:     public StartupManager() {
moel@142:       int p = (int)System.Environment.OSVersion.Platform;
moel@142:       if ((p == 4) || (p == 128)) {
moel@142:         scheduler = null;        
moel@142:         isAvailable = false;
moel@142:         return;
moel@142:       }
moel@142: 
moel@123:       if (IsAdministrator()) {
moel@123:         try {
moel@123:           scheduler = new TaskSchedulerClass();
moel@123:           scheduler.Connect(null, null, null, null);
moel@123:         } catch {
moel@123:           scheduler = null;
moel@123:         }
moel@123: 
moel@123:         if (scheduler != null) {
moel@123:           try {
moel@139:             // check if the taskscheduler is running
moel@139:             IRunningTaskCollection collection = scheduler.GetRunningTasks(0);            
moel@139: 
moel@123:             ITaskFolder folder = scheduler.GetFolder("\\Open Hardware Monitor");
moel@123:             IRegisteredTask task = folder.GetTask("Startup");
moel@129:             startup = (task != null) && 
moel@129:               (task.Definition.Triggers.Count > 0) &&
moel@129:               (task.Definition.Triggers[1].Type == 
moel@129:                 TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON) &&
moel@129:               (task.Definition.Actions.Count > 0) &&
moel@129:               (task.Definition.Actions[1].Type ==
moel@129:                 TASK_ACTION_TYPE.TASK_ACTION_EXEC) &&
moel@129:               (task.Definition.Actions[1] as IExecAction != null) &&
moel@129:               ((task.Definition.Actions[1] as IExecAction).Path ==
moel@129:                 Application.ExecutablePath);
moel@129:               
moel@123:           } catch (IOException) {
moel@123:             startup = false;
moel@123:           } catch (UnauthorizedAccessException) {
moel@123:             scheduler = null;
moel@139:           } catch (COMException) {
moel@139:             scheduler = null;
moel@123:           }
moel@123:         } 
moel@123:       } else {
moel@82:         scheduler = null;
moel@82:       }
moel@142: 
moel@120:       if (scheduler == null) {
moel@142:         try {
moel@142:           using (RegistryKey key =
moel@142:             Registry.CurrentUser.OpenSubKey(REGISTRY_RUN)) {
moel@142:             startup = false;
moel@142:             if (key != null) {
moel@142:               string value = (string)key.GetValue("OpenHardwareMonitor");
moel@142:               if (value != null)
moel@142:                 startup = value == Application.ExecutablePath;
moel@142:             }            
moel@142:           }
moel@142:           isAvailable = true;
moel@142:         } catch (SecurityException) {
moel@142:           isAvailable = false;
moel@82:         }
moel@142:       } else {
moel@142:         isAvailable = true;
moel@82:       }
moel@82:     }
moel@82: 
moel@82:     private void CreateSchedulerTask() {
moel@82:       ITaskDefinition definition = scheduler.NewTask(0);
moel@82:       definition.RegistrationInfo.Description =
moel@82:         "This task starts the Open Hardware Monitor on Windows startup.";
moel@82:       definition.Principal.RunLevel =
moel@82:         TASK_RUNLEVEL.TASK_RUNLEVEL_HIGHEST;
moel@82:       definition.Settings.DisallowStartIfOnBatteries = false;
moel@82:       definition.Settings.StopIfGoingOnBatteries = false;
moel@82:       definition.Settings.ExecutionTimeLimit = "PT0S";
moel@82: 
moel@82:       ILogonTrigger trigger = (ILogonTrigger)definition.Triggers.Create(
moel@82:         TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON);
moel@82: 
moel@82:       IExecAction action = (IExecAction)definition.Actions.Create(
moel@82:         TASK_ACTION_TYPE.TASK_ACTION_EXEC);
moel@82:       action.Path = Application.ExecutablePath;
moel@82:       action.WorkingDirectory =
moel@82:         Path.GetDirectoryName(Application.ExecutablePath);
moel@82: 
moel@82:       ITaskFolder root = scheduler.GetFolder("\\");
moel@82:       ITaskFolder folder;
moel@82:       try {
moel@82:         folder = root.GetFolder("Open Hardware Monitor");
moel@104:       } catch (IOException) {
moel@82:         folder = root.CreateFolder("Open Hardware Monitor", "");
moel@82:       }
moel@82:       folder.RegisterTaskDefinition("Startup", definition,
moel@82:         (int)TASK_CREATION.TASK_CREATE_OR_UPDATE, null, null,
moel@82:         TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, "");
moel@82:     }
moel@82: 
moel@82:     private void DeleteSchedulerTask() {
moel@82:       ITaskFolder root = scheduler.GetFolder("\\");
moel@82:       try {
moel@82:         ITaskFolder folder = root.GetFolder("Open Hardware Monitor");
moel@82:         folder.DeleteTask("Startup", 0);
moel@104:       } catch (IOException) { }
moel@82:       try {
moel@82:         root.DeleteFolder("Open Hardware Monitor", 0);
moel@104:       } catch (IOException) { }
moel@82:     }
moel@82: 
moel@82:     private void CreateRegistryRun() {
moel@185:       RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);     
moel@82:       key.SetValue("OpenHardwareMonitor", Application.ExecutablePath);
moel@82:     }
moel@82: 
moel@82:     private void DeleteRegistryRun() {
moel@82:       RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);
moel@82:       key.DeleteValue("OpenHardwareMonitor");
moel@82:     }
moel@82: 
moel@142:     public bool IsAvailable {
moel@142:       get { return isAvailable; }
moel@142:     }
moel@142: 
moel@82:     public bool Startup {
moel@82:       get {
moel@82:         return startup;
moel@82:       }
moel@82:       set {
moel@185:         if (startup != value) {          
moel@142:           if (isAvailable) {
moel@142:             if (scheduler != null) {
moel@185:               if (value)
moel@142:                 CreateSchedulerTask();
moel@142:               else
moel@142:                 DeleteSchedulerTask();
moel@185:               startup = value;
moel@142:             } else {
moel@185:               try {
moel@185:                 if (value)
moel@185:                   CreateRegistryRun();
moel@185:                 else
moel@185:                   DeleteRegistryRun();
moel@185:                 startup = value;
moel@185:               } catch (UnauthorizedAccessException) {
moel@185:                 throw new InvalidOperationException();
moel@185:               }
moel@142:             }
moel@185:           } else {
moel@185:             throw new InvalidOperationException();
moel@82:           }
moel@82:         }
moel@82:       }
moel@82:     }
moel@82:   }
moel@82: 
moel@82: }