From: Stricted Date: Fri, 30 Oct 2015 15:23:52 +0000 (+0100) Subject: some stuff X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=a231311af5b81a73e190380c1e89c2a0f68ce86b;p=GitHub%2FStricted%2FSpeedportHybridControl.git some stuff --- diff --git a/SpeedportHybridControl.Implementations/PasswordHelper.cs b/SpeedportHybridControl.Implementations/PasswordHelper.cs index 8782491..7b5e0ae 100644 --- a/SpeedportHybridControl.Implementations/PasswordHelper.cs +++ b/SpeedportHybridControl.Implementations/PasswordHelper.cs @@ -5,79 +5,59 @@ using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; +using System.Windows.Data; namespace SpeedportHybridControl.Implementations { - public static class PasswordHelper { - public static readonly DependencyProperty PasswordProperty = - DependencyProperty.RegisterAttached("Password", - typeof(string), typeof(PasswordHelper), - new FrameworkPropertyMetadata(string.Empty, OnPasswordPropertyChanged)); - - public static readonly DependencyProperty AttachProperty = - DependencyProperty.RegisterAttached("Attach", - typeof(bool), typeof(PasswordHelper), new PropertyMetadata(false, Attach)); - - private static readonly DependencyProperty IsUpdatingProperty = - DependencyProperty.RegisterAttached("IsUpdating", typeof(bool), - typeof(PasswordHelper)); - - - public static void SetAttach (DependencyObject dp, bool value) { - dp.SetValue(AttachProperty, value); - } - - public static bool GetAttach (DependencyObject dp) { - return (bool)dp.GetValue(AttachProperty); - } - public static string GetPassword (DependencyObject dp) { - return (string)dp.GetValue(PasswordProperty); - } - - public static void SetPassword (DependencyObject dp, string value) { - dp.SetValue(PasswordProperty, value); - } + // see http://antonymale.co.uk/2015/08/binding-to-a-passwordbox-password-in-wpf + public static class PasswordHelper { + private static readonly DependencyProperty PasswordInitializedProperty = + DependencyProperty.RegisterAttached("PasswordInitialized", typeof(bool), typeof(PasswordHelper), new PropertyMetadata(false)); - private static bool GetIsUpdating (DependencyObject dp) { - return (bool)dp.GetValue(IsUpdatingProperty); - } + private static readonly DependencyProperty SettingPasswordProperty = + DependencyProperty.RegisterAttached("SettingPassword", typeof(bool), typeof(PasswordHelper), new PropertyMetadata(false)); - private static void SetIsUpdating (DependencyObject dp, bool value) { - dp.SetValue(IsUpdatingProperty, value); + public static string GetPassword (DependencyObject obj) { + return (string)obj.GetValue(PasswordProperty); } - - private static void OnPasswordPropertyChanged (DependencyObject sender, - DependencyPropertyChangedEventArgs e) { - PasswordBox passwordBox = sender as PasswordBox; - passwordBox.PasswordChanged -= PasswordChanged; - - if (!(bool)GetIsUpdating(passwordBox)) { - passwordBox.Password = (string)e.NewValue; - } - passwordBox.PasswordChanged += PasswordChanged; + public static void SetPassword (DependencyObject obj, string value) { + obj.SetValue(PasswordProperty, value); } - - private static void Attach (DependencyObject sender, - DependencyPropertyChangedEventArgs e) { - PasswordBox passwordBox = sender as PasswordBox; - + // We play a trick here. If we set the initial value to something, it'll be set to something else when the binding kicks in, + // and HandleBoundPasswordChanged will be called, which allows us to set up our event subscription. + // If the binding sets us to a value which we already are, then this doesn't happen. Therefore start with a value that's + // definitely unique. + public static readonly DependencyProperty PasswordProperty = + DependencyProperty.RegisterAttached("Password", typeof(string), typeof(PasswordHelper), + new FrameworkPropertyMetadata(Guid.NewGuid().ToString(), HandleBoundPasswordChanged) { + BindsTwoWayByDefault = true, + DefaultUpdateSourceTrigger = UpdateSourceTrigger.LostFocus // Match the default on Binding + }); + + private static void HandleBoundPasswordChanged (DependencyObject dp, DependencyPropertyChangedEventArgs e) { + var passwordBox = dp as PasswordBox; if (passwordBox == null) return; - if ((bool)e.OldValue) { - passwordBox.PasswordChanged -= PasswordChanged; - } + // If we're being called because we set the value of the property we're bound to (from inside + // HandlePasswordChanged, then do nothing - we already have the latest value). + if ((bool)passwordBox.GetValue(SettingPasswordProperty)) + return; - if ((bool)e.NewValue) { - passwordBox.PasswordChanged += PasswordChanged; + // If this is the initial set (see the comment on PasswordProperty), set ourselves up + if (!(bool)passwordBox.GetValue(PasswordInitializedProperty)) { + passwordBox.SetValue(PasswordInitializedProperty, true); + passwordBox.PasswordChanged += HandlePasswordChanged; } + + passwordBox.Password = e.NewValue as string; } - private static void PasswordChanged (object sender, RoutedEventArgs e) { - PasswordBox passwordBox = sender as PasswordBox; - SetIsUpdating(passwordBox, true); + private static void HandlePasswordChanged (object sender, RoutedEventArgs e) { + var passwordBox = (PasswordBox)sender; + passwordBox.SetValue(SettingPasswordProperty, true); SetPassword(passwordBox, passwordBox.Password); - SetIsUpdating(passwordBox, false); + passwordBox.SetValue(SettingPasswordProperty, false); } } } diff --git a/SpeedportHybridControl.Implementations/Settings.cs b/SpeedportHybridControl.Implementations/Settings.cs new file mode 100644 index 0000000..f98cf60 --- /dev/null +++ b/SpeedportHybridControl.Implementations/Settings.cs @@ -0,0 +1,78 @@ +using System; +using System.IO; +using System.Text; +using System.Xml; + +namespace SpeedportHybridControl.Implementations { + public class Settings { + public static SettingsModel load () { + SettingsModel result = new SettingsModel(); + try { + if (File.Exists("settings.xml").Equals(true)) { + XmlDocument xmlDocument = new XmlDocument(); + xmlDocument.Load("settings.xml"); + result = new SettingsModel { + ip = xmlDocument.DocumentElement["ip"].InnerText, + password = Cryptography.Decrypt(xmlDocument.DocumentElement["password"].InnerText) + }; + } + } + catch (Exception ex) { + LogManager.WriteToLog(ex.Message); + } + + return result; + } + + public static bool save(SettingsModel settings) { + bool result; + try { + using (XmlTextWriter xmlTextWriter = new XmlTextWriter("settings.xml", Encoding.UTF8)) { + xmlTextWriter.Formatting = Formatting.Indented; + xmlTextWriter.Indentation = 4; + xmlTextWriter.WriteStartDocument(); + xmlTextWriter.WriteStartElement("settings"); + xmlTextWriter.WriteElementString("ip", settings.ip); + xmlTextWriter.WriteElementString("password", Cryptography.Encrypt(settings.password)); + xmlTextWriter.WriteEndElement(); + xmlTextWriter.WriteEndDocument(); + } + result = true; + } + catch (Exception exception) { + LogManager.WriteToLog(exception.Message); + + result = false; + } + return result; + } + } + + public class SettingsModel { + private string _password; + private string _ip; + + public string password { + get { return _password; } + set { + if (_password == value) + return; + + _password = value; + } + } + + public string ip { + get { return _ip; } + set { + if (_ip == value) + return; + + _ip = value; + } + } + + public SettingsModel () { + } + } +} diff --git a/SpeedportHybridControl.Implementations/SpeedportHybridAPI.cs b/SpeedportHybridControl.Implementations/SpeedportHybridAPI.cs index c330b2f..8e88fbc 100644 --- a/SpeedportHybridControl.Implementations/SpeedportHybridAPI.cs +++ b/SpeedportHybridControl.Implementations/SpeedportHybridAPI.cs @@ -11,7 +11,7 @@ using System.Collections.Generic; namespace SpeedportHybridControl.Implementations { public class SpeedportHybridAPI : SingletonFactory { - public string ip = "speedport.ip"; + public string _ip = "speedport.ip"; private DateTime _lastReboot = DateTime.MinValue; private bool _checkIsActive = false; public string _password; @@ -19,7 +19,12 @@ namespace SpeedportHybridControl.Implementations { public string _hash; public string _derivedk; public CookieContainer _cookie = new CookieContainer(); - + + public string ip { + get { return _ip; } + set { _ip = value; } + } + /** * Requests the password-challenge from the router. * diff --git a/SpeedportHybridControl.Implementations/SpeedportHybridControl.Implementations.csproj b/SpeedportHybridControl.Implementations/SpeedportHybridControl.Implementations.csproj index d6969c6..5312a9a 100644 --- a/SpeedportHybridControl.Implementations/SpeedportHybridControl.Implementations.csproj +++ b/SpeedportHybridControl.Implementations/SpeedportHybridControl.Implementations.csproj @@ -56,6 +56,7 @@ + diff --git a/SpeedportHybridControl/Model/LoginPageModel.cs b/SpeedportHybridControl/Model/LoginPageModel.cs index 29cddcb..59f5fc0 100644 --- a/SpeedportHybridControl/Model/LoginPageModel.cs +++ b/SpeedportHybridControl/Model/LoginPageModel.cs @@ -12,6 +12,7 @@ namespace SpeedportHybridControl.Model { private bool _savePassword; private Visibility _passwordBoxVisibility = Visibility.Visible; private Visibility _passwordTextBoxVisibility = Visibility.Hidden; + private Visibility _loginFieldsVisibility = Visibility.Visible; private DelegateCommand _showPasswordCommand; private DelegateCommand _savePasswordCommand; @@ -52,6 +53,11 @@ namespace SpeedportHybridControl.Model { set { SetProperty(ref _passwordTextBoxVisibility, value); } } + public Visibility LoginFieldsVisibility { + get { return _loginFieldsVisibility; } + set { SetProperty(ref _loginFieldsVisibility, value); } + } + public DelegateCommand ShowPasswordCommand { get { return _showPasswordCommand; } set { SetProperty(ref _showPasswordCommand, value); } @@ -93,25 +99,34 @@ namespace SpeedportHybridControl.Model { bool login = SpeedportHybridAPI.getInstance().login(password); if (login.Equals(true)) { if (SavePassword.Equals(true)) { - /* + SettingsModel SettingsModel = new SettingsModel { password = password, ip = SpeedportHybridAPI.getInstance().ip }; Settings.save(SettingsModel); - */ + } else { - /* + SettingsModel SettingsModel = new SettingsModel { password = string.Empty, ip = SpeedportHybridAPI.getInstance().ip }; Settings.save(SettingsModel); - */ + } + LoginFieldsVisibility = Visibility.Hidden; + mwm.ButtonOverviewPageIsActive = true; + mwm.ButtonDSLPageIsActive = true; + mwm.ButtonLteInfoPageIsActive = true; + mwm.ButtonSyslogPageIsActive = true; + mwm.ButtonTR181PageIsActive = true; + mwm.ButtonPhonePageIsActive = true; + mwm.ButtonLanPageIsActive = true; + mwm.ButtonControlsPageIsActive = true; LoginButtonText = "Logout"; mwm.LoginButtonContent = "Logout"; @@ -124,6 +139,17 @@ namespace SpeedportHybridControl.Model { else { if (SpeedportHybridAPI.getInstance().logout().Equals(true)) { // TODO: util.logout(); + + LoginFieldsVisibility = Visibility.Visible; + mwm.ButtonOverviewPageIsActive = false; + mwm.ButtonDSLPageIsActive = false; + mwm.ButtonLteInfoPageIsActive = false; + mwm.ButtonSyslogPageIsActive = false; + mwm.ButtonTR181PageIsActive = false; + mwm.ButtonPhonePageIsActive = false; + mwm.ButtonLanPageIsActive = false; + mwm.ButtonControlsPageIsActive = false; + LoginButtonText = "Login"; mwm.LoginButtonContent = "Login"; } @@ -131,8 +157,18 @@ namespace SpeedportHybridControl.Model { } public LoginPageModel () { - // TODO: ip = Settings.... - // TODO: SpeedportHybridAPI.getInstance().ip = ip; + SettingsModel settings = Settings.load(); + if (settings.ip.IsNullOrEmpty().Equals(false)) { + ip = settings.ip; + } + + SpeedportHybridAPI.getInstance().ip = ip; + + if (settings.password.IsNullOrEmpty().Equals(false)) { + SavePassword = true; + password = settings.password; + } + ShowPasswordCommand = new DelegateCommand(new Action(OnShowPasswordCommandExecute)); SavePasswordCommand = new DelegateCommand(new Action(OnSavePasswordCommandExecute)); LoginCommand = new DelegateCommand(new Action(OnLoginCommandExecute)); diff --git a/SpeedportHybridControl/model/MainWindowModel.cs b/SpeedportHybridControl/model/MainWindowModel.cs index 51731e5..75049bd 100644 --- a/SpeedportHybridControl/model/MainWindowModel.cs +++ b/SpeedportHybridControl/model/MainWindowModel.cs @@ -254,14 +254,6 @@ namespace SpeedportHybridControl.Model { } private void OnSwitchToAboutPageExecute () { - ButtonOverviewPageIsActive = true; - ButtonDSLPageIsActive = true; - ButtonLteInfoPageIsActive = true; - ButtonSyslogPageIsActive = true; - ButtonTR181PageIsActive = true; - ButtonPhonePageIsActive = true; - ButtonLanPageIsActive = true; - ButtonControlsPageIsActive = true; FrameSource = new AboutPage(); changeColor("about"); } diff --git a/SpeedportHybridControl/page/LoginPage.xaml b/SpeedportHybridControl/page/LoginPage.xaml index 7fa58df..7a9768c 100644 --- a/SpeedportHybridControl/page/LoginPage.xaml +++ b/SpeedportHybridControl/page/LoginPage.xaml @@ -6,19 +6,20 @@ xmlns:local="clr-namespace:SpeedportHybridControl" xmlns:Implementations="clr-namespace:SpeedportHybridControl.Implementations;assembly=SpeedportHybridControl.Implementations" mc:Ignorable="d" - Loaded="loaded" Width="Auto" Height="Auto" Title="LoginPage"> - - + + + - - - - - -