GUI/StartupManager.cs
author moel.mich
Sat, 22 May 2010 15:51:59 +0000
changeset 125 b1278888d9a9
parent 120 41dc766e28b8
child 129 efb1b414d33e
permissions -rw-r--r--
A few bug fixes to avoid crashes on Linux systems.
     1 /*
     2   
     3   Version: MPL 1.1/GPL 2.0/LGPL 2.1
     4 
     5   The contents of this file are subject to the Mozilla Public License Version
     6   1.1 (the "License"); you may not use this file except in compliance with
     7   the License. You may obtain a copy of the License at
     8  
     9   http://www.mozilla.org/MPL/
    10 
    11   Software distributed under the License is distributed on an "AS IS" basis,
    12   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    13   for the specific language governing rights and limitations under the License.
    14 
    15   The Original Code is the Open Hardware Monitor code.
    16 
    17   The Initial Developer of the Original Code is 
    18   Michael Möller <m.moeller@gmx.ch>.
    19   Portions created by the Initial Developer are Copyright (C) 2009-2010
    20   the Initial Developer. All Rights Reserved.
    21 
    22   Contributor(s):
    23 
    24   Alternatively, the contents of this file may be used under the terms of
    25   either the GNU General Public License Version 2 or later (the "GPL"), or
    26   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    27   in which case the provisions of the GPL or the LGPL are applicable instead
    28   of those above. If you wish to allow use of your version of this file only
    29   under the terms of either the GPL or the LGPL, and not to allow others to
    30   use your version of this file under the terms of the MPL, indicate your
    31   decision by deleting the provisions above and replace them with the notice
    32   and other provisions required by the GPL or the LGPL. If you do not delete
    33   the provisions above, a recipient may use your version of this file under
    34   the terms of any one of the MPL, the GPL or the LGPL.
    35  
    36 */
    37 
    38 using System;
    39 using System.Collections.Generic;
    40 using System.IO;
    41 using System.Security.Principal;
    42 using System.Windows.Forms;
    43 using Microsoft.Win32;
    44 using OpenHardwareMonitor.TaskScheduler;
    45 
    46 namespace OpenHardwareMonitor.GUI {
    47   public class StartupManager {
    48 
    49     private TaskSchedulerClass scheduler;
    50     private bool startup;
    51 
    52     private const string REGISTRY_RUN =
    53       @"Software\Microsoft\Windows\CurrentVersion\Run";
    54 
    55     private bool IsAdministrator() {
    56       try {
    57         WindowsIdentity identity = WindowsIdentity.GetCurrent();
    58         WindowsPrincipal principal = new WindowsPrincipal(identity);
    59         return principal.IsInRole(WindowsBuiltInRole.Administrator);
    60       } catch {
    61         return false;
    62       }
    63     }
    64 
    65     public StartupManager() {
    66       if (IsAdministrator()) {
    67         try {
    68           scheduler = new TaskSchedulerClass();
    69           scheduler.Connect(null, null, null, null);
    70         } catch {
    71           scheduler = null;
    72         }
    73 
    74         if (scheduler != null) {
    75           try {
    76             ITaskFolder folder = scheduler.GetFolder("\\Open Hardware Monitor");
    77             IRegisteredTask task = folder.GetTask("Startup");
    78             startup = task != null;
    79           } catch (IOException) {
    80             startup = false;
    81           } catch (UnauthorizedAccessException) {
    82             scheduler = null;
    83           }
    84         } 
    85       } else {
    86         scheduler = null;
    87       }
    88             
    89       if (scheduler == null) {
    90         RegistryKey key = Registry.CurrentUser.OpenSubKey(REGISTRY_RUN);
    91         startup = false;
    92         if (key != null) {
    93           string value = (string)key.GetValue("OpenHardwareMonitor");
    94           if (value != null)
    95             startup = value == Application.ExecutablePath;
    96         }
    97       }
    98     }
    99 
   100     private void CreateSchedulerTask() {
   101       ITaskDefinition definition = scheduler.NewTask(0);
   102       definition.RegistrationInfo.Description =
   103         "This task starts the Open Hardware Monitor on Windows startup.";
   104       definition.Principal.RunLevel =
   105         TASK_RUNLEVEL.TASK_RUNLEVEL_HIGHEST;
   106       definition.Settings.DisallowStartIfOnBatteries = false;
   107       definition.Settings.StopIfGoingOnBatteries = false;
   108       definition.Settings.ExecutionTimeLimit = "PT0S";
   109 
   110       ILogonTrigger trigger = (ILogonTrigger)definition.Triggers.Create(
   111         TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON);
   112 
   113       IExecAction action = (IExecAction)definition.Actions.Create(
   114         TASK_ACTION_TYPE.TASK_ACTION_EXEC);
   115       action.Path = Application.ExecutablePath;
   116       action.WorkingDirectory =
   117         Path.GetDirectoryName(Application.ExecutablePath);
   118 
   119       ITaskFolder root = scheduler.GetFolder("\\");
   120       ITaskFolder folder;
   121       try {
   122         folder = root.GetFolder("Open Hardware Monitor");
   123       } catch (IOException) {
   124         folder = root.CreateFolder("Open Hardware Monitor", "");
   125       }
   126       folder.RegisterTaskDefinition("Startup", definition,
   127         (int)TASK_CREATION.TASK_CREATE_OR_UPDATE, null, null,
   128         TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, "");
   129     }
   130 
   131     private void DeleteSchedulerTask() {
   132       ITaskFolder root = scheduler.GetFolder("\\");
   133       try {
   134         ITaskFolder folder = root.GetFolder("Open Hardware Monitor");
   135         folder.DeleteTask("Startup", 0);
   136       } catch (IOException) { }
   137       try {
   138         root.DeleteFolder("Open Hardware Monitor", 0);
   139       } catch (IOException) { }
   140     }
   141 
   142     private void CreateRegistryRun() {
   143       RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);
   144       key.SetValue("OpenHardwareMonitor", Application.ExecutablePath);
   145     }
   146 
   147     private void DeleteRegistryRun() {
   148       RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);
   149       key.DeleteValue("OpenHardwareMonitor");
   150     }
   151 
   152     public bool Startup {
   153       get {
   154         return startup;
   155       }
   156       set {
   157         if (startup != value) {
   158           startup = value;
   159           if (scheduler != null) {
   160             if (startup)
   161               CreateSchedulerTask();
   162             else
   163               DeleteSchedulerTask();
   164           } else {
   165             if (startup)
   166               CreateRegistryRun();
   167             else
   168               DeleteRegistryRun();
   169           }
   170         }
   171       }
   172     }
   173   }
   174 
   175  
   176 
   177  
   178 
   179 }