load and save the 2548:1002 config for XBMC too. try to create the profile directory...
[deb_libcec.git] / src / LibCecTray / controller / applications / internal / XBMCController.cs
CommitLineData
f017f3c4
LOK
1/*
2 * This file is part of the libCEC(R) library.
3 *
4 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
6 *
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
8 *
9 * This program is dual-licensed; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 *
23 *
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
26 *
27 * For more information contact:
28 * Pulse-Eight Licensing <license@pulse-eight.com>
29 * http://www.pulse-eight.com/
30 * http://www.pulse-eight.net/
31 */
32
33using System;
34using System.Globalization;
35using System.IO;
36using System.Text;
37using System.Windows.Forms;
38using System.Xml;
39using CecSharp;
40using LibCECTray.Properties;
41using LibCECTray.settings;
42
43namespace LibCECTray.controller.applications.@internal
44{
45 internal class XBMCController : ApplicationController
46 {
47 public XBMCController(CECSettings settings) :
48 base(settings,
49 Resources.application_xbmc,
50 "XBMC",
51 "XBMC.exe",
52 Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + @"\XBMC")
53 {
54 IsInternal = true;
55 AutoStartApplication.Value = false;
56 ControlApplication.Value = false;
57
58 LoadXMLConfiguration();
59 }
60
61 public override ApplicationAction DefaultValue(CecKeypress key)
62 {
63 return null;
64 }
65
66 public override ControllerTabPage UiControl
67 {
68 get { return UIControlInternal ?? (UIControlInternal = new XBMCControllerUI(this)); }
69 }
70
71 public bool LoadXMLConfiguration()
72 {
49689754
LOK
73 var xbmcDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\XBMC\userdata\peripheral_data";
74 return LoadXMLConfiguration(xbmcDir + @"\usb_2548_1001.xml") || LoadXMLConfiguration(xbmcDir + @"\usb_2548_1002.xml");
75 }
f017f3c4 76
49689754
LOK
77 public bool LoadXMLConfiguration(string filename)
78 {
79 bool gotConfig = false;
80 if (File.Exists(filename))
f017f3c4 81 {
49689754 82 XmlTextReader reader = new XmlTextReader(filename);
f017f3c4
LOK
83 while (reader.Read())
84 {
85 gotConfig = true;
86 switch (reader.NodeType)
87 {
88 case XmlNodeType.Element:
89 if (reader.Name.ToLower() == "setting")
90 {
91 string name = string.Empty;
92 string value = string.Empty;
93
94 while (reader.MoveToNextAttribute())
95 {
96 if (reader.Name.ToLower().Equals("id"))
97 name = reader.Value.ToLower();
98 if (reader.Name.ToLower().Equals("value"))
99 value = reader.Value;
100 }
101
102 switch (name)
103 {
104 case "cec_hdmi_port":
105 {
106 byte iPort;
107 if (byte.TryParse(value, out iPort))
108 Settings.HDMIPort.Value = iPort;
109 }
110 break;
111 case "connected_device":
112 {
113 ushort iDevice;
114 if (ushort.TryParse(value, out iDevice))
115 Settings.ConnectedDevice.Value = (CecLogicalAddress)iDevice;
116 }
117 break;
118 case "cec_power_on_startup":
119 if (value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes"))
120 {
121 Settings.ActivateSource.Value = true;
122 Settings.WakeDevices.Value.Set(CecLogicalAddress.Tv);
123 }
124 break;
125 case "cec_power_off_shutdown":
126 if (value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes"))
127 Settings.PowerOffDevices.Value.Set(CecLogicalAddress.Broadcast);
128 break;
129 case "cec_standby_screensaver":
130 StandbyScreensaver.Value = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
131 break;
132 case "standby_pc_on_tv_standby":
133 PowerOffOnStandby.Value = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
134 break;
135 case "use_tv_menu_language":
136 UseTVLanguage.Value = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
137 break;
138 // 1.5.0+ settings
139 case "physical_address":
140 {
141 ushort physicalAddress;
142 if (ushort.TryParse(value, NumberStyles.AllowHexSpecifier, null, out physicalAddress))
143 Settings.PhysicalAddress.Value = physicalAddress;
144 }
145 break;
146 case "device_type":
147 {
148 ushort iType;
149 if (ushort.TryParse(value, out iType))
150 Settings.DeviceType.Value = (CecDeviceType)iType;
151 }
152 break;
153 case "tv_vendor":
154 {
155 UInt64 iVendor;
156 if (UInt64.TryParse(value, out iVendor))
157 Settings.TVVendor.Value = (CecVendorId)iVendor;
158 }
159 break;
160 case "wake_devices":
161 {
162 Settings.WakeDevices.Value.Clear();
163 string[] split = value.Split(new[] { ' ' });
164 foreach (string dev in split)
165 {
166 byte iLogicalAddress;
167 if (byte.TryParse(dev, out iLogicalAddress))
168 Settings.WakeDevices.Value.Set((CecLogicalAddress)iLogicalAddress);
169 }
170 }
171 break;
172 case "standby_devices":
173 {
174 Settings.PowerOffDevices.Value.Clear();
175 string[] split = value.Split(new[] { ' ' });
176 foreach (string dev in split)
177 {
178 byte iLogicalAddress;
179 if (byte.TryParse(dev, out iLogicalAddress))
180 Settings.PowerOffDevices.Value.Set((CecLogicalAddress)iLogicalAddress);
181 }
182 }
183 break;
184 case "enabled":
185 break;
186 case "port":
187 //TODO
188 break;
189 // 1.5.1 settings
190 case "send_inactive_source":
191 SendInactiveSource.Value = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
192 break;
193 }
194 }
195 break;
196 }
197 }
198 }
199 return gotConfig;
200 }
201
202 public void SaveXMLConfiguration()
203 {
204 Settings.Persist();
205
49689754
LOK
206 var xbmcDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\XBMC\userdata\peripheral_data";
207 if (!Directory.Exists(xbmcDir))
208 Directory.CreateDirectory(xbmcDir);
209
210 if (!Directory.Exists(xbmcDir))
211 {
212 // couldn't create directory
213 MessageBox.Show(string.Format(Resources.could_not_create_directory, xbmcDir), Resources.error,
214 MessageBoxButtons.OK, MessageBoxIcon.Error);
215 return;
216 }
f017f3c4
LOK
217
218 SaveFileDialog dialog = new SaveFileDialog
49689754 219 {
f017f3c4 220 Title = Resources.store_settings_where,
49689754
LOK
221 InitialDirectory = xbmcDir,
222 FileName = string.Format("usb_{0}.xml", Program.Instance.Controller.USBDescriptor.Replace(':', '_')),
f017f3c4
LOK
223 Filter = Resources.xml_file_filter,
224 FilterIndex = 1
225 };
226 if (dialog.ShowDialog() != DialogResult.OK) return;
227
228 FileStream fs = null;
229 string error = string.Empty;
230 try
231 {
232 fs = (FileStream)dialog.OpenFile();
233 }
234 catch (Exception ex)
235 {
236 error = ex.Message;
237 }
238 if (fs == null)
239 {
240 MessageBox.Show(string.Format(Resources.cannot_open_file, dialog.FileName) + (error.Length > 0 ? ": " + error : string.Empty), Resources.app_name, MessageBoxButtons.OK, MessageBoxIcon.Error);
241 }
242 else
243 {
244 StreamWriter writer = new StreamWriter(fs);
245 StringBuilder output = new StringBuilder();
246 output.AppendLine("<settings>");
247 output.AppendLine("<setting id=\"cec_hdmi_port\" value=\"" + Settings.HDMIPort.Value + "\" />");
248 output.AppendLine("<setting id=\"connected_device\" value=\"" + (Settings.ConnectedDevice.Value == CecLogicalAddress.AudioSystem ? 5 : 0) + "\" />");
249 output.AppendLine("<setting id=\"cec_power_on_startup\" value=\"" + (Settings.ActivateSource.Value ? 1 : 0) + "\" />");
250 output.AppendLine("<setting id=\"cec_power_off_shutdown\" value=\"" + (Settings.PowerOffDevices.Value.IsSet(CecLogicalAddress.Broadcast) ? 1 : 0) + "\" />");
251 output.AppendLine("<setting id=\"cec_standby_screensaver\" value=\"" + (StandbyScreensaver.Value ? 1 : 0) + "\" />");
252 output.AppendLine("<setting id=\"standby_pc_on_tv_standby\" value=\"" + (PowerOffOnStandby.Value ? 1 : 0) + "\" />");
253 output.AppendLine("<setting id=\"use_tv_menu_language\" value=\"" + (UseTVLanguage.Value ? 1 : 0) + "\" />");
254 output.AppendLine("<setting id=\"enabled\" value=\"1\" />");
255 output.AppendLine("<setting id=\"port\" value=\"\" />");
256
257 // only supported by 1.5.0+ clients
258 output.AppendLine("<!-- the following lines are only supported by v1.5.0+ clients -->");
259 output.AppendLine("<setting id=\"activate_source\" value=\"" + (Settings.ActivateSource.Value ? 1 : 0) + "\" />");
260 output.AppendLine("<setting id=\"physical_address\" value=\"" + string.Format("{0,4:X}", Settings.OverridePhysicalAddress.Value ? Settings.PhysicalAddress.Value : 0).Trim() + "\" />");
261 output.AppendLine("<setting id=\"device_type\" value=\"" + (int)Settings.DeviceType.Value + "\" />");
262 output.AppendLine("<setting id=\"tv_vendor\" value=\"" + string.Format("{0,6:X}", Settings.OverrideTVVendor.Value ? (int)Settings.TVVendor.Value : 0).Trim() + "\" />");
263
264 output.Append("<setting id=\"wake_devices\" value=\"");
265 StringBuilder strWakeDevices = new StringBuilder();
266 foreach (CecLogicalAddress addr in Settings.WakeDevices.Value.Addresses)
267 if (addr != CecLogicalAddress.Unknown)
268 strWakeDevices.Append(" " + (int)addr);
269 output.Append(strWakeDevices.ToString().Trim());
270 output.AppendLine("\" />");
271
272 output.Append("<setting id=\"standby_devices\" value=\"");
273 StringBuilder strSleepDevices = new StringBuilder();
274 foreach (CecLogicalAddress addr in Settings.PowerOffDevices.Value.Addresses)
275 if (addr != CecLogicalAddress.Unknown)
276 strSleepDevices.Append(" " + (int)addr);
277 output.Append(strSleepDevices.ToString().Trim());
278 output.AppendLine("\" />");
279
280 // only supported by 1.5.1+ clients
281 output.AppendLine("<!-- the following lines are only supported by v1.5.1+ clients -->");
282 //TODO
283 //output.AppendLine("<setting id=\"send_inactive_source\" value=\"" + (config.SendInactiveSource ? 1 : 0) + "\" />");
284
285 output.AppendLine("</settings>");
286 writer.Write(output.ToString());
287 writer.Close();
288 fs.Close();
289 fs.Dispose();
290 MessageBox.Show(Resources.settings_stored, Resources.app_name, MessageBoxButtons.OK, MessageBoxIcon.Information);
291 }
292 }
293
294 public CECSettingBool UseTVLanguage
295 {
296 get
297 {
298 if (!Settings.ContainsKey(ProcessName + "_use_tv_language"))
299 {
300 CECSettingBool setting = new CECSettingBool(ProcessName + "_use_tv_language", Resources.app_use_tv_language, true, null);
301 Settings.Load(setting);
302 Settings[ProcessName + "_use_tv_language"] = setting;
303 }
304 return Settings[ProcessName + "_use_tv_language"].AsSettingBool;
305 }
306 }
307
308 public CECSettingBool StandbyScreensaver
309 {
310 get
311 {
312 if (!Settings.ContainsKey(ProcessName + "_standby_screensaver"))
313 {
314 CECSettingBool setting = new CECSettingBool(ProcessName + "_standby_screensaver", Resources.app_standby_screensaver, true, null);
315 Settings.Load(setting);
316 Settings[ProcessName + "_standby_screensaver"] = setting;
317 }
318 return Settings[ProcessName + "_standby_screensaver"].AsSettingBool;
319 }
320 }
321
322 public CECSettingBool ActivateSource
323 {
324 get
325 {
326 if (!Settings.ContainsKey(ProcessName + "_activate_source"))
327 {
328 CECSettingBool setting = new CECSettingBool(ProcessName + "_activate_source", Resources.global_activate_source, true, null);
329 Settings.Load(setting);
330 Settings[ProcessName + "_activate_source"] = setting;
331 }
332 return Settings[ProcessName + "_activate_source"].AsSettingBool;
333 }
334 }
335
336 public CECSettingBool PowerOffOnStandby
337 {
338 get
339 {
340 if (!Settings.ContainsKey(ProcessName + "_standby_on_tv_standby"))
341 {
342 CECSettingBool setting = new CECSettingBool(ProcessName + "_standby_on_tv_standby", Resources.app_standby_on_tv_standby, true, null);
343 Settings.Load(setting);
344 Settings[ProcessName + "_standby_on_tv_standby"] = setting;
345 }
346 return Settings[ProcessName + "_standby_on_tv_standby"].AsSettingBool;
347 }
348 }
349
350 public CECSettingBool SendInactiveSource
351 {
352 get
353 {
354 if (!Settings.ContainsKey(ProcessName + "_send_inactive_source"))
355 {
356 CECSettingBool setting = new CECSettingBool(ProcessName + "_send_inactive_source", Resources.app_send_inactive_source, true, null);
357 Settings.Load(setting);
358 Settings[ProcessName + "_send_inactive_source"] = setting;
359 }
360 return Settings[ProcessName + "_send_inactive_source"].AsSettingBool;
361 }
362 }
363 }
364}