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);
}
}
}
--- /dev/null
+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 () {
+ }
+ }
+}
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;
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.
*
<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>
private bool _savePassword;
private Visibility _passwordBoxVisibility = Visibility.Visible;
private Visibility _passwordTextBoxVisibility = Visibility.Hidden;
+ private Visibility _loginFieldsVisibility = Visibility.Visible;
private DelegateCommand _showPasswordCommand;
private DelegateCommand _savePasswordCommand;
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); }
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";
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";
}
}
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));
}
private void OnSwitchToAboutPageExecute () {
- ButtonOverviewPageIsActive = true;
- ButtonDSLPageIsActive = true;
- ButtonLteInfoPageIsActive = true;
- ButtonSyslogPageIsActive = true;
- ButtonTR181PageIsActive = true;
- ButtonPhonePageIsActive = true;
- ButtonLanPageIsActive = true;
- ButtonControlsPageIsActive = true;
FrameSource = new AboutPage();
changeColor("about");
}
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>
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;
- }
- }
- */
- }
}
}