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