Merge branch 'master' into release
[deb_libcec.git] / src / cec-config-gui / actions / ConnectToDevice.cs
index 9250ad09b73a7fc9c447abee32325f8e79dc4a58..8c198f4c60a20f86042aada349c0c0618ca81118 100644 (file)
@@ -1,35 +1,51 @@
-using System;
-using System.Collections.Generic;
-using System.Text;
-using CecSharp;
+using CecSharp;
 using System.Windows.Forms;
-using System.IO;
-using System.Xml;
-using System.Globalization;
 
 namespace CecConfigGui.actions
 {
   class ConnectToDevice : UpdateProcess
   {
-    public ConnectToDevice(ref LibCecSharp lib)
+    public ConnectToDevice(ref LibCecSharp lib, LibCECConfiguration config)
     {
       Lib = lib;
+      Config = config;
     }
 
     public override void Process()
     {
-      SendEvent(UpdateEventType.StatusText, "Connecting to the CEC adapter...");
-      SendEvent(UpdateEventType.ProgressBar, 0);
+      SendEvent(UpdateEventType.StatusText, "Opening connection...");
+      SendEvent(UpdateEventType.ProgressBar, 10);
 
+      //TODO read the com port setting from the configuration
       CecAdapter[] adapters = Lib.FindAdapters(string.Empty);
-      if (adapters.Length == 0 || !Lib.Open(adapters[0].ComPort, 10000))
+      if (adapters.Length == 0)
       {
-        MessageBox.Show("Could not connect to any CEC adapter. Please check your configuration.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK);
-        Application.Exit();
+        DialogResult result = MessageBox.Show("Could not detect to any CEC adapter. Please check your configuration. Do you want to try again?", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.YesNo);
+        if (result == DialogResult.No)
+        {
+          SendEvent(UpdateEventType.ExitApplication);
+          return;
+        }
+        else
+          adapters = Lib.FindAdapters(string.Empty);
       }
 
+      while (!Lib.Open(adapters[0].ComPort, 10000))
+      {
+        DialogResult result = MessageBox.Show("Could not connect to any CEC adapter. Please check your configuration. Do you want to try again?", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.YesNo);
+        if (result == DialogResult.No)
+        {
+          SendEvent(UpdateEventType.ExitApplication);
+          return;
+        }
+      }
+
+      SendEvent(UpdateEventType.ProgressBar, 20);
+      SendEvent(UpdateEventType.StatusText, "Sending power on commands...");
+      Lib.PowerOnDevices(CecLogicalAddress.Broadcast);
+
       SendEvent(UpdateEventType.StatusText, "Detecting TV vendor...");
-      SendEvent(UpdateEventType.ProgressBar, 25);
+      SendEvent(UpdateEventType.ProgressBar, 30);
       SendEvent(UpdateEventType.TVVendorId, (int)Lib.GetDeviceVendorId(CecLogicalAddress.Tv));
 
       SendEvent(UpdateEventType.StatusText, "Detecting menu language...");
@@ -44,127 +60,33 @@ namespace CecConfigGui.actions
 
       if (hasAVRDevice)
       {
-        SendEvent(UpdateEventType.ProgressBar, 75);
+        SendEvent(UpdateEventType.ProgressBar, 60);
         SendEvent(UpdateEventType.StatusText, "Detecting AVR vendor...");
         SendEvent(UpdateEventType.AVRVendorId, (int)Lib.GetDeviceVendorId(CecLogicalAddress.AudioSystem));
       }
 
       if (!Lib.GetDevicePowerStatus(CecLogicalAddress.Tv).Equals(CecPowerStatus.On))
       {
-        SendEvent(UpdateEventType.ProgressBar, 80);
-        SendEvent(UpdateEventType.StatusText, "Sending power on command...");
-        Lib.PowerOnDevices(CecLogicalAddress.Tv);
+        SendEvent(UpdateEventType.ProgressBar, 70);
+        SendEvent(UpdateEventType.StatusText, "Activating the source...");
+        Lib.SetActiveSource(CecDeviceType.Reserved);
       }
 
-      SendEvent(UpdateEventType.ProgressBar, 90);
+      SendEvent(UpdateEventType.ProgressBar, 80);
       SendEvent(UpdateEventType.StatusText, "Reading device configuration...");
 
-      LibCECConfiguration config = new LibCECConfiguration();
+      Lib.GetCurrentConfiguration(Config);
+      SendEvent(Config);
 
-      if (!Lib.CanPersistConfiguration())
-      {
-        bool gotConfig = false;
-        string xbmcDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\XBMC\userdata\peripheral_data";
-        string defaultDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
-        string file = defaultDir + @"\usb_2548_1001.xml";
-        if (File.Exists(xbmcDir + @"\usb_2548_1001.xml"))
-          file = xbmcDir + @"\usb_2548_1001.xml";
-
-        if (File.Exists(file))
-        {
-          XmlTextReader reader = new XmlTextReader(file);
-          while (reader.Read())
-          {
-            gotConfig = true;
-            switch (reader.NodeType)
-            {
-              case XmlNodeType.Element:
-                if (reader.Name.ToLower() == "setting")
-                {
-                  string name = string.Empty;
-                  string value = string.Empty;
-
-                  while (reader.MoveToNextAttribute())
-                  {
-                    if (reader.Name.ToLower().Equals("id"))
-                      name = reader.Value.ToLower();
-                    if (reader.Name.ToLower().Equals("value"))
-                      value = reader.Value;
-                  }
-
-                  switch (name)
-                  {
-                    case "cec_hdmi_port":
-                      {
-                        byte iPort;
-                        if (byte.TryParse(value, out iPort))
-                          config.HDMIPort = iPort;
-                      }
-                      break;
-                    case "connected_device":
-                      {
-                        ushort iDevice;
-                        if (ushort.TryParse(value, out iDevice))
-                          config.BaseDevice = (CecLogicalAddress)iDevice;
-                      }
-                      break;
-                    case "physical_address":
-                      {
-                        ushort physicalAddress = 0;
-                        if (ushort.TryParse(value, NumberStyles.AllowHexSpecifier, null, out physicalAddress))
-                          config.PhysicalAddress = physicalAddress;
-                      }
-                      break;
-                    case "device_type":
-                      {
-                        ushort iType;
-                        if (ushort.TryParse(value, out iType))
-                          config.DeviceTypes.Types[0] = (CecDeviceType)iType;
-                      }
-                      break;
-                    case "cec_power_on_startup":
-                      config.PowerOnStartup = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
-                      break;
-                    case "cec_power_off_shutdown":
-                      config.PowerOffShutdown = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
-                      break;
-                    case "cec_standby_screensaver":
-                      config.PowerOffScreensaver = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
-                      break;
-                    case "standby_pc_on_tv_standby":
-                      config.PowerOffOnStandby = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
-                      break;
-                    case "use_tv_menu_language":
-                      config.UseTVMenuLanguage = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
-                      break;
-                    case "enabled":
-                      break;
-                    case "port":
-                      break;
-                    default:
-                      break;
-                  }
-                }
-                break;
-              default:
-                break;
-            }
-          }
-        }
-
-        if (!gotConfig)
-          Lib.GetCurrentConfiguration(config);
-      }
-      else
-      {
-        Lib.GetCurrentConfiguration(config);
-      }
-      SendEvent(config);
+      SendEvent(UpdateEventType.ProgressBar, 90);
+      SendEvent(UpdateEventType.StatusText, "Polling active devices");
+      SendEvent(UpdateEventType.PollDevices);
 
       SendEvent(UpdateEventType.ProgressBar, 100);
       SendEvent(UpdateEventType.StatusText, "Ready.");
     }
 
     private LibCecSharp Lib;
+    private LibCECConfiguration Config;
   }
 }