some stuff
authorStricted <info@stricted.de>
Fri, 30 Oct 2015 15:23:52 +0000 (16:23 +0100)
committerStricted <info@stricted.de>
Fri, 30 Oct 2015 15:23:52 +0000 (16:23 +0100)
SpeedportHybridControl.Implementations/PasswordHelper.cs
SpeedportHybridControl.Implementations/Settings.cs [new file with mode: 0644]
SpeedportHybridControl.Implementations/SpeedportHybridAPI.cs
SpeedportHybridControl.Implementations/SpeedportHybridControl.Implementations.csproj
SpeedportHybridControl/Model/LoginPageModel.cs
SpeedportHybridControl/model/MainWindowModel.cs
SpeedportHybridControl/page/LoginPage.xaml
SpeedportHybridControl/page/LoginPage.xaml.cs

index 87824915acc958391117722f806aae9fe2be8e6b..7b5e0aef6330fb8e507e46e9dde1bf375ba60fa0 100644 (file)
@@ -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 (file)
index 0000000..f98cf60
--- /dev/null
@@ -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 () {
+               }
+    }
+}
index c330b2fd542335cbd58e79ddbb38ed64f8d90db8..8e88fbc0c99ab23df85bfd58af99d3e2e58edfa4 100644 (file)
@@ -11,7 +11,7 @@ using System.Collections.Generic;
 
 namespace SpeedportHybridControl.Implementations {
        public class SpeedportHybridAPI : SingletonFactory<SpeedportHybridAPI> {
-               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.
                 *
index d6969c6678e6f3b554188c1b12345509615d1077..5312a9a70903f08c882a7484a0f2080651814284 100644 (file)
@@ -56,6 +56,7 @@
     <Compile Include="LogManager.cs" />
     <Compile Include="PasswordHelper.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="Settings.cs" />
     <Compile Include="SingletonFactory.cs" />
     <Compile Include="sjcl.cs" />
   </ItemGroup>
index 29cddcb06d1aa8a53aec8d84247632579c1ad39a..59f5fc07b448a2c81bcda8e42bc10ba875a92bad 100644 (file)
@@ -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));
index 51731e56ded0b1a1da27ad48d0d13399279be060..75049bd50f988f5dc506ef5918fdba9c28b056f8 100644 (file)
@@ -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");
                }
index 7fa58df4f5d959c15c049f7e5dcfd670b86e7cab..7a9768c5a8784c8195e338852c7c5224dfc2f0c6 100644 (file)
@@ -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">
 
     <Grid DataContext="{StaticResource LoginPageModel}">
-        <TextBlock x:Name="diTextBlock" HorizontalAlignment="Left" Margin="51,21,0,0" TextWrapping="Wrap" Text="Domain/IP:" VerticalAlignment="Top"/>
-        <TextBox Text="{Binding Path=ip, Mode=TwoWay}" x:Name="tbip" HorizontalAlignment="Left" Height="23" Margin="115,14,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
+        <Grid Visibility="{Binding Path=LoginFieldsVisibility}">
+            <TextBlock HorizontalAlignment="Left" Margin="51,21,0,0" TextWrapping="Wrap" Text="Domain/IP:" VerticalAlignment="Top"/>
+            <TextBox Text="{Binding Path=ip, Mode=TwoWay}" HorizontalAlignment="Left" Height="23" Margin="115,14,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
 
-        <TextBlock x:Name="tbpw" Text="Passwort:" HorizontalAlignment="Left" Margin="55,49,0,0" VerticalAlignment="Top" />
-        <PasswordBox Implementations:PasswordHelper.Attach="True" Implementations:PasswordHelper.Password="{Binding Path=password, Mode=TwoWay}"  Visibility="{Binding Path=PasswordBoxVisibility}" x:Name="PasswordBox" HorizontalAlignment="Left" Height="23" Margin="115,42,0,0" VerticalAlignment="Top" Width="120" ForceCursor="True" />
-        <TextBox Text="{Binding Path=password, Mode=TwoWay}" x:Name="PasswordTextBox" HorizontalAlignment="Left" Height="23" Margin="115,42,0,0" VerticalAlignment="Top" Width="120" Visibility="{Binding Path=PasswordTextBoxVisibility}"/>
-        <CheckBox Command="{Binding Path=ShowPasswordCommand}" IsChecked="{Binding Path=ShowPassword, Mode=TwoWay}" x:Name="PasswordCheckBox" Content="Passwort anzeigen" Margin="240,50,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" />
-        <CheckBox Command="{Binding Path=SavePasswordCommand}" IsChecked="{Binding Path=SavePassword, Mode=TwoWay}"  x:Name="cbSave" Content="Passwort speichern" Margin="240,65,0,0" Checked="CheckBox" Unchecked="CheckBox" HorizontalAlignment="Left" VerticalAlignment="Top" />
-        <Button Command="{Binding Path=LoginCommand}" x:Name="button1" Content="{Binding Path=LoginButtonText}" HorizontalAlignment="Left" Margin="115,70,0,0" VerticalAlignment="Top" Width="75" IsDefault="True"/>
+            <TextBlock Text="Passwort:" HorizontalAlignment="Left" Margin="55,49,0,0" VerticalAlignment="Top" />
+            <PasswordBox Implementations:PasswordHelper.Password="{Binding Path=password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  Visibility="{Binding Path=PasswordBoxVisibility}" x:Name="passwordBox" HorizontalAlignment="Left" Height="23" Margin="115,42,0,0" VerticalAlignment="Top" Width="120" ForceCursor="True" />
+            <TextBox Text="{Binding Path=password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Height="23" Margin="115,42,0,0" VerticalAlignment="Top" Width="120" Visibility="{Binding Path=PasswordTextBoxVisibility}"/>
+            <CheckBox Command="{Binding Path=ShowPasswordCommand}" IsChecked="{Binding Path=ShowPassword, Mode=TwoWay}" Content="Passwort anzeigen" Margin="240,50,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" />
+            <CheckBox Command="{Binding Path=SavePasswordCommand}" IsChecked="{Binding Path=SavePassword, Mode=TwoWay}" Content="Passwort speichern" Margin="240,65,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" />
+        </Grid>
+        <Button Command="{Binding Path=LoginCommand}" Content="{Binding Path=LoginButtonText}" HorizontalAlignment="Left" Margin="115,70,0,0" VerticalAlignment="Top" Width="75" IsDefault="True"/>
     </Grid>
 </Page>
index 41d52a75a8e7f63de5bd36917a78a96f20ee9589..a78eedfc8e6ed7f7bee4509351234ec42b5a0582 100644 (file)
@@ -3,109 +3,15 @@ using System.Windows.Controls;
 using System.Threading;
 using System;
 using SpeedportHybridControl.Model;
+using SpeedportHybridControl.Implementations;
 
 namespace SpeedportHybridControl.page {
        /// <summary>
        /// Interaction logic for LoginPage.xaml
        /// </summary>
        public partial class LoginPage : Page {
-               public bool savePW;
-
                public LoginPage() {
                        InitializeComponent();
                }
-
-               void loaded (object sender, RoutedEventArgs e) {
-                       /*
-                       SettingsModel settings = Settings.load();
-                       if (settings.ip.IsNullOrEmpty().Equals(false)) {
-                               SpeedportHybridAPI.getInstance().ip = settings.ip;
-                       }
-
-                       if (settings.password.IsNullOrEmpty().Equals(false)) {
-                               savePW = true;
-                               cbSave.IsChecked = true;
-                               PasswordBox.Password = settings.password;
-                       }
-
-                       PasswordBox.Focus();
-
-                       tbip.Text = SpeedportHybridAPI.getInstance().ip;
-                       */
-               }
-
-               private void button_click(object sender, RoutedEventArgs e) {
-                       /*
-                       if (sender.Equals(button1)) {
-                               if (button1.Content.Equals("Login")) {
-                                       if (SpeedportHybridAPI.getInstance().ip.Equals(tbip.Text).Equals(false)) {
-                                               SpeedportHybridAPI.getInstance().ip = tbip.Text;
-                                       }
-
-                                       if (PasswordCheckBox.IsChecked.Equals(true)) {
-                                               PasswordBox.Password = PasswordTextBox.Text;
-                                               PasswordCheckBox.IsChecked = false;
-                                       }
-
-                                       if (SpeedportHybridAPI.getInstance().login(PasswordBox.Password).Equals(true)) {
-                                               util.login();
-                                               SettingsModel SettingsModel = null;
-
-                                               if (savePW.Equals(true)) {
-                                                       SettingsModel = new SettingsModel {
-                                                               password = PasswordBox.Password,
-                                                               ip = SpeedportHybridAPI.getInstance().ip
-                                                       };
-                                               }
-                                               else {
-                                                       SettingsModel = new SettingsModel {
-                                                               password = string.Empty,
-                                                               ip = SpeedportHybridAPI.getInstance().ip
-                                                       };
-                                               }
-
-                                               Settings.save(SettingsModel);
-                                       }
-                                       else {
-                                               new Thread(() => { MessageBox.Show("Login fehlgeschlagen. Sie haben ein falsches Gerätepasswort eingegeben. Bitte versuchen Sie es erneut und achten Sie auf die korrekte Schreibweise.", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Error); }).Start();
-                                               LogManager.WriteToLog("Login Failed, wrong password");
-                                               PasswordBox.Focus();
-                                       }
-                               }
-                               else if (button1.Content.Equals("Logout")) {
-                                       if (SpeedportHybridAPI.getInstance().logout().Equals(true)) {
-                                               util.logout();
-                                       }
-                               }
-                       }
-                       */
-               }
-
-               private void CheckBox(object sender, RoutedEventArgs e) {
-                       /*
-                       if (sender.Equals(PasswordCheckBox)) {
-                               if (PasswordCheckBox.IsChecked.Equals(true)) {
-                                       PasswordTextBox.Text = PasswordBox.Password;
-                                       PasswordBox.Visibility = Visibility.Hidden;
-                                       PasswordTextBox.Visibility = Visibility.Visible;
-                                       PasswordTextBox.Focus();
-                               }
-                               else {
-                                       PasswordBox.Password = PasswordTextBox.Text;
-                                       PasswordBox.Visibility = Visibility.Visible;
-                                       PasswordTextBox.Visibility = Visibility.Hidden;
-                                       PasswordBox.Focus();
-                               }
-                       }
-                       else if (sender.Equals(cbSave)) {
-                               if (cbSave.IsChecked.Equals(true)) {
-                                       savePW = true;
-                               }
-                               else {
-                                       savePW = false;
-                               }
-                       }
-                       */
-               }
        }
 }