From: <che...@us...> - 2009-06-21 12:57:54
|
Revision: 2951 http://mp-plugins.svn.sourceforge.net/mp-plugins/?rev=2951&view=rev Author: chef_koch Date: 2009-06-21 12:31:09 +0000 (Sun, 21 Jun 2009) Log Message: ----------- refactored settings inot it's own class Modified Paths: -------------- trunk/plugins/MySleepTimer/MySleepTimer/MySleepTimer.cs trunk/plugins/MySleepTimer/MySleepTimer/MySleepTimer.csproj trunk/plugins/MySleepTimer/MySleepTimer/MySleepTimerConfig.cs Added Paths: ----------- trunk/plugins/MySleepTimer/MySleepTimer/Settings.cs Modified: trunk/plugins/MySleepTimer/MySleepTimer/MySleepTimer.cs =================================================================== --- trunk/plugins/MySleepTimer/MySleepTimer/MySleepTimer.cs 2009-06-20 02:41:16 UTC (rev 2950) +++ trunk/plugins/MySleepTimer/MySleepTimer/MySleepTimer.cs 2009-06-21 12:31:09 UTC (rev 2951) @@ -26,7 +26,6 @@ using MediaPortal.Configuration; using MediaPortal.Dialogs; using MediaPortal.GUI.Library; -using MediaPortal.Profile; using MediaPortal.Util; using Timer=System.Timers.Timer; @@ -50,53 +49,12 @@ private bool _timedOut = true; private bool _setByPlay = false; private bool _shutdowning = false; - //settings - private readonly Action.ActionType _actionType = Action.ActionType.ACTION_REMOTE_YELLOW_BUTTON; - private readonly string _sleepBehavior = "Shutdown"; - private readonly RestartOptions _restartOption = RestartOptions.ShutDown; - private readonly bool _shutdownForce = false; - private readonly int _sleepTimeMaxium = 120; //(1..n) - private readonly int _sleepTimeStep = 10; //(1..n) - private readonly int _actionTimeOutMs = 2*1000; //(1..n * 1000) - private readonly int _notifyBeforeSleep = 3; //(0..n) - private readonly int _notifyInterval = 1; //(1.._notifyBeforeSleep) - private readonly int _notifyTimeOutMs = 3*1000; //(1..65 * 1000) - public MySleepTimer() - { - try - { - //read settings from config - using (Settings xmlReader = new Settings(Config.GetFile(Config.Dir.Config, "MediaPortal.xml"))) - { - _actionType = - (Action.ActionType) - xmlReader.GetValueAsInt("MySleepTimer", "#MySleepTimer.ActionType", - (int) (Action.ActionType.ACTION_REMOTE_YELLOW_BUTTON)); - _sleepBehavior = xmlReader.GetValueAsString("MySleepTimer", "#MySleepTimer.SleepBehavior", "Shutdown"); - _restartOption = - (RestartOptions) - xmlReader.GetValueAsInt("MySleepTimer", "#MySleepTimer.ShutdownType", (int) RestartOptions.ShutDown); - _shutdownForce = xmlReader.GetValueAsBool("MySleepTimer", "#MySleepTimer.ShutdownForce", false); - _sleepTimeMaxium = xmlReader.GetValueAsInt("MySleepTimer", "#MySleepTimer.Maximum", 120); - _sleepTimeStep = xmlReader.GetValueAsInt("MySleepTimer", "#MySleepTimer.Step", 10); - _actionTimeOutMs = xmlReader.GetValueAsInt("MySleepTimer", "#MySleepTimer.ActionTimeOut", 2)*1000; - _notifyBeforeSleep = xmlReader.GetValueAsInt("MySleepTimer", "#MySleepTimer.NotifyBeforeSleep", 3); - _notifyInterval = xmlReader.GetValueAsInt("MySleepTimer", "#MySleepTimer.NotifyInterval", 1); - _notifyTimeOutMs = xmlReader.GetValueAsInt("MySleepTimer", "#MySleepTimer.NotifyTimeOut", 3)*1000; - } - } - catch (Exception ex) - { - Log.Error(ex); - } - } - private void GUIWindowManager_OnNewAction(Action action) { try { - if ((action.wID == _actionType) && (!_shutdowning)) //action and no shutdown in progress + if ((action.wID == Settings.ActionType) && (!_shutdowning)) //action and no shutdown in progress { _timerAction.Stop(); _timerNotify.Stop(); @@ -105,7 +63,7 @@ _timedOut = false; //action _timerAction.Start(); - ShowNotifyDialog(_actionTimeOutMs, string.Format("Time left: {0} min", _sleepTimeCurrent)); + ShowNotifyDialog(Settings.ActionTimeOutMs, string.Format("Time left: {0} min", _sleepTimeCurrent)); } else //additional presses { @@ -113,16 +71,16 @@ SetTime(); if (_sleepTimeCurrent > 0) _timerShutDown.Start(); - if ((_sleepTimeCurrent > 0) && (_sleepTimeCurrent <= _notifyBeforeSleep) && - ((_notifyBeforeSleep - _sleepTimeCurrent)%_notifyInterval == 0)) //notify + if ((_sleepTimeCurrent > 0) && (_sleepTimeCurrent <= Settings.NotifyBeforeSleep) && + ((Settings.NotifyBeforeSleep - _sleepTimeCurrent) % Settings.NotifyInterval == 0)) //notify { _timerNotify.Start(); - ShowNotifyDialog(_notifyTimeOutMs, null); + ShowNotifyDialog(Settings.NotifyTimeOutMs, null); } else //action { _timerAction.Start(); - ShowNotifyDialog(_actionTimeOutMs, null); + ShowNotifyDialog(Settings.NotifyTimeOutMs, null); } } //base.OnAction(action); @@ -180,10 +138,10 @@ _timedOut = true; _setByPlay = false; _shutdowning = false; - switch (_sleepBehavior) + switch (Settings.SleepBehavior) { case "Shutdown": - WindowsController.ExitWindows(_restartOption, _shutdownForce, null); + WindowsController.ExitWindows(Settings.ShutdownType, Settings.ShutdownForce, null); break; case "Exit MediaPortal": Application.Exit(); @@ -192,18 +150,18 @@ GUIWindowManager.ActivateWindow((int) GUIWindow.Window.WINDOW_HOME, true); break; default: - WindowsController.ExitWindows(_restartOption, _shutdownForce, null); + WindowsController.ExitWindows(Settings.ShutdownType, Settings.ShutdownForce, null); break; } } - else if ((_sleepTimeCurrent <= _notifyBeforeSleep) && ((_notifyBeforeSleep - _sleepTimeCurrent)%_notifyInterval == 0)) + else if ((_sleepTimeCurrent <= Settings.NotifyBeforeSleep) && ((Settings.NotifyBeforeSleep - _sleepTimeCurrent) % Settings.NotifyInterval == 0)) //notify { _timerAction.Stop(); _timerNotify.Stop(); _timedOut = false; //simulate 1st press - with reset by notify timer _timerNotify.Start(); - ShowNotifyDialog(_notifyTimeOutMs, null); + ShowNotifyDialog(Settings.NotifyTimeOutMs, null); } } } @@ -246,9 +204,9 @@ } _setByPlay = false; //reset play flag //standard increment - _sleepTimeCurrent += _sleepTimeStep; - _sleepTimeCurrent = (_sleepTimeCurrent/_sleepTimeStep)*_sleepTimeStep; //normalize - if (_sleepTimeCurrent > _sleepTimeMaxium) + _sleepTimeCurrent += Settings.SleepTimeStep; + _sleepTimeCurrent = (_sleepTimeCurrent / Settings.SleepTimeStep) * Settings.SleepTimeStep; //normalize + if (_sleepTimeCurrent > Settings.SleepTimeMaxium) _sleepTimeCurrent = 0; //overflowed; let timer be stopped } } @@ -402,6 +360,8 @@ /// </summary> public void Start() { + Settings.Load(); + _timerShutDown = new Timer(); _timerAction = new Timer(); _timerNotify = new Timer(); @@ -409,10 +369,10 @@ //inits _timerShutDown.Interval = SHUTDOWN_CYCLE_MS; _timerShutDown.Elapsed += new ElapsedEventHandler(timerShutDown_Tick); - _timerAction.Interval = _actionTimeOutMs; + _timerAction.Interval = Settings.ActionTimeOutMs; _timerAction.AutoReset = false; _timerAction.Elapsed += new ElapsedEventHandler(timerAction_Tick); - _timerNotify.Interval = _notifyTimeOutMs; + _timerNotify.Interval = Settings.NotifyTimeOutMs; _timerNotify.AutoReset = false; _timerNotify.Elapsed += new ElapsedEventHandler(timerNotify_Tick); Modified: trunk/plugins/MySleepTimer/MySleepTimer/MySleepTimer.csproj =================================================================== --- trunk/plugins/MySleepTimer/MySleepTimer/MySleepTimer.csproj 2009-06-20 02:41:16 UTC (rev 2950) +++ trunk/plugins/MySleepTimer/MySleepTimer/MySleepTimer.csproj 2009-06-21 12:31:09 UTC (rev 2951) @@ -69,6 +69,7 @@ </Reference> </ItemGroup> <ItemGroup> + <Compile Include="Settings.cs" /> <Compile Include="MySleepTimer.cs"> </Compile> <Compile Include="MySleepTimerConfig.cs"> Modified: trunk/plugins/MySleepTimer/MySleepTimer/MySleepTimerConfig.cs =================================================================== --- trunk/plugins/MySleepTimer/MySleepTimer/MySleepTimerConfig.cs 2009-06-20 02:41:16 UTC (rev 2950) +++ trunk/plugins/MySleepTimer/MySleepTimer/MySleepTimerConfig.cs 2009-06-21 12:31:09 UTC (rev 2951) @@ -56,25 +56,18 @@ comboBoxShutDownType.DisplayMember = "ShutdownName"; comboBoxShutDownType.ValueMember = "ShutdownID"; - using (Settings xmlReader = new Settings(Config.GetFile(Config.Dir.Config, "MediaPortal.xml"))) - { - comboBoxActionType.SelectedValue = xmlReader.GetValueAsInt("MySleepTimer", "#MySleepTimer.ActionType", - (int) - (Action.ActionType.ACTION_REMOTE_YELLOW_BUTTON)); - comboBoxSleepBehavior.Text = xmlReader.GetValueAsString("MySleepTimer", "#MySleepTimer.SleepBehavior", - "Shutdown"); - comboBoxShutDownType.SelectedValue = xmlReader.GetValueAsInt("MySleepTimer", "#MySleepTimer.ShutdownType", - (int) RestartOptions.ShutDown); - checkBoxShutDownForce.Checked = xmlReader.GetValueAsBool("MySleepTimer", "#MySleepTimer.ShutdownForce", false); - numericUpDownSleepTimeMax.Value = xmlReader.GetValueAsInt("MySleepTimer", "#MySleepTimer.Maximum", 120); - numericUpDownSleepTimeStep.Value = xmlReader.GetValueAsInt("MySleepTimer", "#MySleepTimer.Step", 10); - numericUpDownTimeOutB.Value = xmlReader.GetValueAsInt("MySleepTimer", "#MySleepTimer.ActionTimeOut", 2); - numericUpDownNotifyBeforeSleep.Value = xmlReader.GetValueAsInt("MySleepTimer", - "#MySleepTimer.NotifyBeforeSleep", 3); - numericUpDownNotifyInterval.Value = xmlReader.GetValueAsInt("MySleepTimer", "#MySleepTimer.NotifyInterval", 1); - numericUpDownTimeOutN.Value = xmlReader.GetValueAsInt("MySleepTimer", "#MySleepTimer.NotifyTimeOut", 3); - } - + Settings.Load(); + comboBoxActionType.SelectedValue = Settings.ActionType; + comboBoxSleepBehavior.Text = Settings.SleepBehavior; + comboBoxShutDownType.SelectedValue = Settings.ShutdownType; + checkBoxShutDownForce.Checked = Settings.ShutdownForce; + numericUpDownSleepTimeMax.Value = Settings.SleepTimeMaxium; + numericUpDownSleepTimeStep.Value = Settings.SleepTimeStep; + numericUpDownTimeOutB.Value = Settings.ActionTimeOutMs; + numericUpDownNotifyBeforeSleep.Value = Settings.NotifyBeforeSleep; + numericUpDownNotifyInterval.Value = Settings.NotifyInterval; + numericUpDownTimeOutN.Value = Settings.NotifyTimeOutMs; + SetUpShutDownTypeControls(); SetUpNotifyControls(); } @@ -124,22 +117,18 @@ { try { - // Write configuration settings - using (Settings xmlWriter = new Settings(Config.GetFile(Config.Dir.Config, "MediaPortal.xml"))) - { - xmlWriter.SetValue("MySleepTimer", "#MySleepTimer.ActionType", comboBoxActionType.SelectedValue); - xmlWriter.SetValue("MySleepTimer", "#MySleepTimer.SleepBehavior", comboBoxSleepBehavior.Text); - xmlWriter.SetValue("MySleepTimer", "#MySleepTimer.ShutdownType", comboBoxShutDownType.SelectedValue); - xmlWriter.SetValueAsBool("MySleepTimer", "#MySleepTimer.ShutdownForce", checkBoxShutDownForce.Checked); - xmlWriter.SetValue("MySleepTimer", "#MySleepTimer.Maximum", numericUpDownSleepTimeMax.Value.ToString()); - xmlWriter.SetValue("MySleepTimer", "#MySleepTimer.Step", numericUpDownSleepTimeStep.Value.ToString()); - xmlWriter.SetValue("MySleepTimer", "#MySleepTimer.ActionTimeOut", numericUpDownTimeOutB.Value.ToString()); - xmlWriter.SetValue("MySleepTimer", "#MySleepTimer.NotifyBeforeSleep", - numericUpDownNotifyBeforeSleep.Value.ToString()); - xmlWriter.SetValue("MySleepTimer", "#MySleepTimer.NotifyInterval", - numericUpDownNotifyInterval.Value.ToString()); - xmlWriter.SetValue("MySleepTimer", "#MySleepTimer.NotifyTimeOut", numericUpDownTimeOutN.Value.ToString()); - } + Settings.ActionType =(Action.ActionType) comboBoxActionType.SelectedValue; + Settings.SleepBehavior = comboBoxSleepBehavior.Text; + Settings.ShutdownType = (RestartOptions)comboBoxShutDownType.SelectedValue; + Settings.ShutdownForce = checkBoxShutDownForce.Checked; + Settings.SleepTimeMaxium = (int)numericUpDownSleepTimeMax.Value; + Settings.SleepTimeStep = (int)numericUpDownSleepTimeStep.Value; + Settings.ActionTimeOutMs = (int)numericUpDownTimeOutB.Value; + Settings.NotifyBeforeSleep = (int)numericUpDownNotifyBeforeSleep.Value; + Settings.NotifyInterval = (int)numericUpDownNotifyInterval.Value; + Settings.NotifyTimeOutMs = (int)numericUpDownTimeOutN.Value; + Settings.Save(); + Close(); } catch (Exception ex) Added: trunk/plugins/MySleepTimer/MySleepTimer/Settings.cs =================================================================== --- trunk/plugins/MySleepTimer/MySleepTimer/Settings.cs (rev 0) +++ trunk/plugins/MySleepTimer/MySleepTimer/Settings.cs 2009-06-21 12:31:09 UTC (rev 2951) @@ -0,0 +1,107 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Text; +using MediaPortal.Configuration; +using MediaPortal.GUI.Library; +using MediaPortal.Util; +using Log=MediaPortal.ServiceImplementations.Log; + +using System.Reflection; + +namespace MySleepTimer +{ + internal static class Settings + { + #region Properties + + private const string PLUGIN_NAME = "MySleepTimer"; + + public static Action.ActionType ActionType { get; set; } + public static string SleepBehavior { get; set; } + public static RestartOptions ShutdownType { get; set; } + public static bool ShutdownForce { get; set; } + public static int SleepTimeMaxium { get; set; } //(1..n) + public static int SleepTimeStep { get; set; } //(1..n) + public static int ActionTimeOutMs { get; set; } //(1..n * 1000) + public static int NotifyBeforeSleep { get; set; } //(0..n) + public static int NotifyInterval { get; set; } //(1..NotifyBeforeSleep) + public static int NotifyTimeOutMs { get; set; } //(1..65 * 1000) + + static Settings() + { + ActionType = Action.ActionType.ACTION_REMOTE_YELLOW_BUTTON; + SleepBehavior = "Shutdown"; + ShutdownType = RestartOptions.ShutDown; + ShutdownForce = false; + SleepTimeMaxium = 120; + SleepTimeStep = 10; + ActionTimeOutMs = 2 * 1000; + NotifyBeforeSleep = 3; + NotifyInterval = 1; + NotifyTimeOutMs = 3 * 1000; + } + + #endregion + + public static void Load() + { + Log.Info("{0}: Settings.Load()", PLUGIN_NAME); + + using ( + MediaPortal.Profile.Settings xmlReader = + new MediaPortal.Profile.Settings(Config.GetFile(Config.Dir.Config, "MediaPortal.xml"))) + { + ActionType = + (Action.ActionType) + xmlReader.GetValueAsInt(PLUGIN_NAME, "#MySleepTimer.ActionType", + (int)(Action.ActionType.ACTION_REMOTE_YELLOW_BUTTON)); + SleepBehavior = xmlReader.GetValueAsString(PLUGIN_NAME, "#MySleepTimer.SleepBehavior", "Shutdown"); + ShutdownType = + (RestartOptions) + xmlReader.GetValueAsInt(PLUGIN_NAME, "#MySleepTimer.ShutdownType", (int)RestartOptions.ShutDown); + ShutdownForce = xmlReader.GetValueAsBool(PLUGIN_NAME, "#MySleepTimer.ShutdownForce", false); + SleepTimeMaxium = xmlReader.GetValueAsInt(PLUGIN_NAME, "#MySleepTimer.Maximum", 120); + SleepTimeStep = xmlReader.GetValueAsInt(PLUGIN_NAME, "#MySleepTimer.Step", 10); + ActionTimeOutMs = xmlReader.GetValueAsInt(PLUGIN_NAME, "#MySleepTimer.ActionTimeOut", 2) * 1000; + NotifyBeforeSleep = xmlReader.GetValueAsInt(PLUGIN_NAME, "#MySleepTimer.NotifyBeforeSleep", 3); + NotifyInterval = xmlReader.GetValueAsInt(PLUGIN_NAME, "#MySleepTimer.NotifyInterval", 1); + NotifyTimeOutMs = xmlReader.GetValueAsInt(PLUGIN_NAME, "#MySleepTimer.NotifyTimeOut", 3) * 1000; + } + + //WriteToLog(); + } + + public static void Save() + { + Log.Info("{0}: Settings.Save()", PLUGIN_NAME); + + using ( + MediaPortal.Profile.Settings xmlWriter = + new MediaPortal.Profile.Settings(Config.GetFile(Config.Dir.Config, "MediaPortal.xml"))) + { + xmlWriter.SetValue(PLUGIN_NAME, "#MySleepTimer.ActionType", ActionType); + xmlWriter.SetValue(PLUGIN_NAME, "#MySleepTimer.SleepBehavior", SleepBehavior); + xmlWriter.SetValue(PLUGIN_NAME, "#MySleepTimer.ShutdownType", ShutdownType); + xmlWriter.SetValueAsBool(PLUGIN_NAME, "#MySleepTimer.ShutdownForce", ShutdownForce); + xmlWriter.SetValue(PLUGIN_NAME, "#MySleepTimer.Maximum", SleepTimeMaxium); + xmlWriter.SetValue(PLUGIN_NAME, "#MySleepTimer.Step", SleepTimeStep); + xmlWriter.SetValue(PLUGIN_NAME, "#MySleepTimer.ActionTimeOut", ActionTimeOutMs); + xmlWriter.SetValue(PLUGIN_NAME, "#MySleepTimer.NotifyBeforeSleep", NotifyBeforeSleep); + xmlWriter.SetValue(PLUGIN_NAME, "#MySleepTimer.NotifyInterval", NotifyInterval); + xmlWriter.SetValue(PLUGIN_NAME, "#MySleepTimer.NotifyTimeOut", NotifyTimeOutMs); + } + } + + public static void WriteToLog() + { + // get all properties + PropertyInfo[] propertyInfos = typeof(Settings).GetProperties(); + // write property names and values + foreach (PropertyInfo propertyInfo in propertyInfos) + { + Log.Info("{0}: {1} = {2}", PLUGIN_NAME, propertyInfo.Name, propertyInfo.GetValue(null, null).ToString()); + } + } + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |