GUI/SoundGraphDisplay.cs
author StephaneLenclud
Mon, 02 Feb 2015 13:28:41 +0100
branchMiniDisplay
changeset 437 38e7b78cf732
parent 436 e9aefd454d1e
child 438 f590956d3234
permissions -rw-r--r--
Reverting client/server communication around our pipes to fix access denied err.
Now simply opening files for pipes created by SoundGraphAccess server.
     1 /*
     2  
     3   This Source Code Form is subject to the terms of the Mozilla Public
     4   License, v. 2.0. If a copy of the MPL was not distributed with this
     5   file, You can obtain one at http://mozilla.org/MPL/2.0/.
     6  
     7   Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
     8 	
     9 */
    10 
    11 using System;
    12 using System.Collections.Generic;
    13 using System.Drawing;
    14 using System.Text;
    15 using System.Diagnostics;
    16 using System.Windows.Forms;
    17 using System.Windows;
    18 using OpenHardwareMonitor.Hardware;
    19 using OpenHardwareMonitor.Utilities;
    20 using System.Runtime.InteropServices;
    21 using UacHelpers;
    22 
    23 
    24 
    25 
    26 
    27 
    28 
    29 namespace OpenHardwareMonitor.GUI
    30 {
    31     public class SoundGraphDisplay : IDisposable
    32     {
    33         private IComputer computer;
    34         private PersistentSettings settings;
    35         private UnitManager unitManager;
    36         private List<SensorFrontView> list = new List<SensorFrontView>();
    37         private SoundGraph.Server iServer;
    38 
    39 
    40         public SoundGraphDisplay(IComputer computer, PersistentSettings settings,
    41           UnitManager unitManager)
    42         {
    43             this.computer = computer;
    44             this.settings = settings;
    45             this.unitManager = unitManager;
    46             computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
    47             computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
    48 
    49             //Start our client if needed
    50             Process[] processes = Process.GetProcessesByName("SoundGraphAccess");
    51             if (!(processes.Length > 0))
    52             {
    53 
    54                 //Process client = UserAccountControl.CreateProcessAsStandardUser(@"D:\Dev\SoundGraphAccess\Debug\SoundGraphAccess.exe","");
    55                     /*
    56                 Process client = new Process();
    57                 client.StartInfo.FileName = @"D:\Dev\SoundGraphAccess\Debug\SoundGraphAccess.exe";
    58                 client.StartInfo.WorkingDirectory = @"D:\Dev\SoundGraphAccess";
    59                 client.Start();*/
    60             }
    61 
    62             //Start our SoundGraph server
    63             iServer = new SoundGraph.Server(@"\\.\pipe\sga-inbound", @"\\.\pipe\sga-outbound");
    64             iServer.Start();
    65             //iServer.SendMessage("init:");
    66         }
    67 
    68         private void HardwareRemoved(IHardware hardware)
    69         {
    70             hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
    71             hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
    72             foreach (ISensor sensor in hardware.Sensors)
    73                 SensorRemoved(sensor);
    74             foreach (IHardware subHardware in hardware.SubHardware)
    75                 HardwareRemoved(subHardware);
    76         }
    77 
    78         private void HardwareAdded(IHardware hardware)
    79         {
    80             foreach (ISensor sensor in hardware.Sensors)
    81                 SensorAdded(sensor);
    82             hardware.SensorAdded += new SensorEventHandler(SensorAdded);
    83             hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
    84             foreach (IHardware subHardware in hardware.SubHardware)
    85                 HardwareAdded(subHardware);
    86         }
    87 
    88         private void SensorAdded(ISensor sensor)
    89         {
    90             if (settings.GetValue(new Identifier(sensor.Identifier,
    91               "tray").ToString(), false))
    92                 Add(sensor, false);
    93         }
    94 
    95         private void SensorRemoved(ISensor sensor)
    96         {
    97             if (Contains(sensor))
    98                 Remove(sensor, false);
    99         }
   100 
   101         public void Dispose()
   102         {
   103             foreach (SensorFrontView icon in list)
   104                 icon.Dispose();
   105 
   106             Quit();
   107             iServer.Stop();
   108 
   109         }
   110 
   111         public void Redraw()
   112         {
   113             foreach (SensorFrontView icon in list)
   114                 icon.Update();
   115         }
   116 
   117         public bool Contains(ISensor sensor)
   118         {
   119             foreach (SensorFrontView icon in list)
   120                 if (icon.Sensor == sensor)
   121                     return true;
   122             return false;
   123         }
   124 
   125         public void Add(ISensor sensor, bool balloonTip)
   126         {
   127             if (Contains(sensor))
   128             {
   129                 return;
   130             }
   131             else
   132             {
   133                 //SL:
   134                 //list.Add(new SensorFrontView(this, sensor, balloonTip, settings, unitManager));
   135                 //UpdateMainIconVisibilty();
   136                 //settings.SetValue(new Identifier(sensor.Identifier, "tray").ToString(), true);
   137             }
   138         }
   139 
   140         public void Remove(ISensor sensor)
   141         {
   142             Remove(sensor, true);
   143         }
   144 
   145         private void Remove(ISensor sensor, bool deleteConfig)
   146         {
   147             if (deleteConfig)
   148             {
   149                 settings.Remove(
   150                   new Identifier(sensor.Identifier, "tray").ToString());
   151                 settings.Remove(
   152                   new Identifier(sensor.Identifier, "traycolor").ToString());
   153             }
   154             SensorFrontView instance = null;
   155             foreach (SensorFrontView icon in list)
   156                 if (icon.Sensor == sensor)
   157                     instance = icon;
   158             if (instance != null)
   159             {
   160                 list.Remove(instance);
   161                 UpdateMainIconVisibilty();
   162                 instance.Dispose();
   163             }
   164         }
   165 
   166 
   167 
   168         private void UpdateMainIconVisibilty()
   169         {
   170             /*
   171             if (mainIconEnabled)
   172             {
   173                 mainIcon.Visible = list.Count == 0;
   174             }
   175             else
   176             {
   177                 mainIcon.Visible = false;
   178             }
   179              */
   180         }
   181 
   182         public void Init()
   183         {
   184             iServer.SendMessage("init:");
   185         }
   186 
   187         public void Uninit()
   188         {
   189             iServer.SendMessage("uninit:");
   190         }
   191 
   192         public void SetText(string aUpperLine, string aLowerLine)
   193         {
   194             iServer.SendMessage("set-vfd-text:" + aUpperLine);
   195         }
   196 
   197         public void Quit()
   198         {
   199             iServer.SendMessage("quit:");
   200         }
   201 
   202         /*
   203         public bool IsMainIconEnabled
   204         {
   205             get { return mainIconEnabled; }
   206             set
   207             {
   208                 if (mainIconEnabled != value)
   209                 {
   210                     mainIconEnabled = value;
   211                     UpdateMainIconVisibilty();
   212                 }
   213             }
   214         }*/
   215 
   216 
   217     }
   218 }