Files
Chat/ChatController/LoginControl.xaml.cs

311 lines
9.3 KiB
C#
Raw Normal View History

2017-09-12 14:32:34 +02:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
2017-09-12 14:32:34 +02:00
using System.IO;
using System.Runtime.CompilerServices;
2017-09-12 14:32:34 +02:00
using System.Text;
using System.Windows;
using System.Windows.Threading;
using ChatController.Annotations;
2017-09-12 14:32:34 +02:00
using ChatController.HauptKlassen;
2019-04-04 13:00:10 +02:00
using ChatController.LoginKlassen;
using ChatController.Utilities;
using ChatController.Utilities.Extensions;
2017-10-20 07:34:26 +02:00
using MessageBox = System.Windows.MessageBox;
using Panel = System.Windows.Controls.Panel;
2017-10-06 12:08:05 +02:00
using Path = System.IO.Path;
2017-09-12 14:32:34 +02:00
namespace ChatController
{
public partial class LoginControl : INotifyPropertyChanged
2017-09-12 14:32:34 +02:00
{
private ChatControlWaitLayer _WaitLayer;
public string UserName
{
get => _Benutzername;
set
{
2023-03-03 21:23:48 +01:00
if(Equals(_Benutzername, value))
{
2023-03-03 21:23:48 +01:00
return;
}
2023-03-03 21:23:48 +01:00
_Benutzername = value;
OnPropertyChanged(nameof(UserName));
}
}
public string Password
{
get => PasswordBox.Password;
set => PasswordBox.Password = value;
}
public string ChatCode
{
get => _ChatCode;
set
{
2023-03-03 21:23:48 +01:00
if(Equals(_ChatCode, value))
{
2023-03-03 21:23:48 +01:00
return;
}
2023-03-03 21:23:48 +01:00
_ChatCode = value;
OnPropertyChanged(nameof(ChatCode));
}
}
public string Tenant
{
get => _Kundennummer;
set
{
2023-03-03 21:23:48 +01:00
if(Equals(_Kundennummer, value))
{
2023-03-03 21:23:48 +01:00
return;
}
2023-03-03 21:23:48 +01:00
_Kundennummer = value;
OnPropertyChanged(nameof(Tenant));
}
}
2017-09-12 14:32:34 +02:00
public delegate void LoginDelegate(ChatDatenUebergabe cdu);
public event LoginDelegate OnLogin;
private string _Benutzername = string.Empty;
private string _ChatCode = string.Empty;
private string _Kundennummer = string.Empty;
private readonly string _Version = "1.13";
public string Version => $"Version: {_Version}";
2017-09-12 14:32:34 +02:00
public LoginControl()
{
InitializeComponent();
2017-10-06 12:08:05 +02:00
DataContext = this;
2017-11-13 17:09:21 +01:00
LeseDateiFallsVorhanden();
2017-09-12 14:32:34 +02:00
}
private void AnmeldenButton_OnClick(object sender, RoutedEventArgs e)
2017-10-20 07:34:26 +02:00
{
Anmelden();
}
private void StartWaiting()
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action) StartWaitingImmediately);
}
private void StartWaitingImmediately()
2017-10-20 07:34:26 +02:00
{
2023-03-03 21:23:48 +01:00
if(!(_WaitLayer is null))
2017-10-20 07:34:26 +02:00
{
2023-03-03 21:23:48 +01:00
return;
2017-10-20 07:34:26 +02:00
}
2023-03-03 21:23:48 +01:00
_WaitLayer = new ChatControlWaitLayer();
RootGrid.Children.Add(_WaitLayer);
Panel.SetZIndex(_WaitLayer, int.MaxValue);
2023-03-03 21:23:48 +01:00
_WaitLayer.RefreshUI();
2017-10-20 07:34:26 +02:00
}
private void EndWaiting()
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action) delegate
{
2023-03-03 21:23:48 +01:00
if(_WaitLayer is null)
{
2023-03-03 21:23:48 +01:00
return;
}
2023-03-03 21:23:48 +01:00
RootGrid.Children.Remove(_WaitLayer);
_WaitLayer = null;
});
}
2017-10-20 07:34:26 +02:00
private void Anmelden()
2017-09-12 14:32:34 +02:00
{
if(!string.IsNullOrWhiteSpace(_Kundennummer) && !string.IsNullOrWhiteSpace(_ChatCode) && !string.IsNullOrWhiteSpace(_Benutzername) && Password.Length > 0)
2017-09-15 16:39:07 +02:00
{
StartWaiting();
2017-09-12 14:32:34 +02:00
var login = new Login(_Kundennummer, _ChatCode, _Benutzername, PasswordBox.Password);
2017-09-12 14:32:34 +02:00
login.DoLoginAsync(chatDatenUebergabe =>
{
this.Dispatch(() =>
2017-09-12 14:32:34 +02:00
{
EndWaiting();
2019-04-04 13:00:10 +02:00
if(chatDatenUebergabe is null)
{
return;
}
2023-03-03 21:23:48 +01:00
AutosetDaten();
2023-03-03 21:23:48 +01:00
OnLogin?.Invoke(chatDatenUebergabe);
});
},
errorMessage =>
{
this.Dispatch(() =>
{
EndWaiting();
2017-09-12 14:32:34 +02:00
MessageBox.Show(errorMessage, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
});
});
2017-09-15 16:39:07 +02:00
}
else
2017-09-15 16:39:07 +02:00
{
MessageBox.Show("Bitte alle Felder ausfüllen!", "Info", MessageBoxButton.OK);
2017-09-15 16:39:07 +02:00
}
2017-10-06 12:08:05 +02:00
}
private void LoadTenantAndChatCodeFromFile()
2017-09-12 14:32:34 +02:00
{
2023-03-03 21:23:48 +01:00
if(File.Exists(Path.Combine(GetAndCreateUserAppDataPath(), Constants.TenantAndChatCodeFileName)))
2017-09-12 14:32:34 +02:00
{
2023-03-03 21:23:48 +01:00
var lines = new List<string>();
using(var streamReader = new StreamReader(Path.Combine(GetAndCreateUserAppDataPath(), Constants.TenantAndChatCodeFileName), true))
{
2023-03-03 21:23:48 +01:00
string line;
2023-03-03 21:23:48 +01:00
while(!((line = streamReader.ReadLine()) is null))
{
var encodedTextBytes = Convert.FromBase64String(line);
2023-03-03 21:23:48 +01:00
var plainText = Encoding.UTF8.GetString(encodedTextBytes);
2023-03-03 21:23:48 +01:00
lines.Add(plainText);
}
2023-03-03 21:23:48 +01:00
}
2023-03-03 21:23:48 +01:00
if(lines.Count == 2)
{
_Kundennummer = lines[0];
_ChatCode = lines[1];
}
2023-03-03 21:23:48 +01:00
}
2023-03-03 21:23:48 +01:00
#if DEBUG
//var debugFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "debug-info.txt");
if(Tenant == "1122334455" && Directory.Exists(@"C:\Users\Lyndon Jetten"))
{
ChatCode = "HprNe -1MwMB";
UserName = "MikeGahn";
Password = "TW2024!!!";
return;
}
var debugFile = @"C:\Users\Lyndon Jetten\Desktop\debug-info.txt";
2023-03-03 21:23:48 +01:00
if (File.Exists(debugFile))
{
using(var streamReader2 = new StreamReader(debugFile, true))
{
2023-03-03 21:23:48 +01:00
string line;
var counter = 0;
while(!((line = streamReader2.ReadLine()) is null))
{
2023-03-03 21:23:48 +01:00
if(counter == 0)
{
var encodedTextBytes = Convert.FromBase64String(line);
2023-03-03 21:23:48 +01:00
var plainText = Encoding.UTF8.GetString(encodedTextBytes);
2023-03-03 21:23:48 +01:00
UserName = plainText;
}
else if(counter == 1)
{
2023-03-03 21:23:48 +01:00
var encodedTextBytes = Convert.FromBase64String(line);
2023-03-03 21:23:48 +01:00
var plainText = Encoding.UTF8.GetString(encodedTextBytes);
Password = plainText;
}
2023-03-03 21:23:48 +01:00
counter++;
}
}
2017-09-12 14:32:34 +02:00
}
2023-03-03 21:23:48 +01:00
#endif
2017-09-12 14:32:34 +02:00
}
private void LeseDateiFallsVorhanden()
{
LoadTenantAndChatCodeFromFile();
}
private void AutosetDaten()
2017-09-12 14:32:34 +02:00
{
2023-03-03 21:23:48 +01:00
if(File.Exists(Path.Combine(GetAndCreateUserAppDataPath(), Constants.TenantAndChatCodeFileName)))
{
2023-03-03 21:23:48 +01:00
File.SetAttributes(Path.Combine(GetAndCreateUserAppDataPath(), Constants.TenantAndChatCodeFileName), FileAttributes.Normal);
File.Delete(Path.Combine(GetAndCreateUserAppDataPath(), Constants.TenantAndChatCodeFileName));
}
2017-10-06 12:08:05 +02:00
2023-03-03 21:23:48 +01:00
using(var streamWriter = new StreamWriter(Path.Combine(GetAndCreateUserAppDataPath(), Constants.TenantAndChatCodeFileName)))
{
Encoding utf8Encoding = new UTF8Encoding();
var tenantBytes = utf8Encoding.GetBytes(_Kundennummer);
var encodedTenant = Convert.ToBase64String(tenantBytes);
2017-10-06 12:08:05 +02:00
2023-03-03 21:23:48 +01:00
var chatCodeBytes = utf8Encoding.GetBytes(_ChatCode);
var encodedChatCode = Convert.ToBase64String(chatCodeBytes);
2017-10-06 12:08:05 +02:00
2023-03-03 21:23:48 +01:00
streamWriter.WriteLine(encodedTenant);
streamWriter.WriteLine(encodedChatCode);
2017-10-06 12:08:05 +02:00
2023-03-03 21:23:48 +01:00
File.SetAttributes(Path.Combine(GetAndCreateUserAppDataPath(), Constants.TenantAndChatCodeFileName), FileAttributes.ReadOnly);
}
}
2017-10-20 07:34:26 +02:00
private static string GetAndCreateUserAppDataPath()
{
2023-03-03 21:23:48 +01:00
var localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
var companyFilePath = Path.Combine(localAppData, "ownSoft");
2023-03-03 21:23:48 +01:00
if(!Directory.Exists(companyFilePath))
{
Directory.CreateDirectory(companyFilePath);
}
2017-09-14 12:36:48 +02:00
2023-03-03 21:23:48 +01:00
var bewoFilePath = Path.Combine(companyFilePath, "OwnChat");
2017-09-14 12:36:48 +02:00
2023-03-03 21:23:48 +01:00
if(!Directory.Exists(bewoFilePath))
2017-10-06 12:08:05 +02:00
{
2023-03-03 21:23:48 +01:00
Directory.CreateDirectory(bewoFilePath);
2017-10-06 12:08:05 +02:00
}
2023-03-03 21:23:48 +01:00
return bewoFilePath;
2017-10-06 12:08:05 +02:00
}
2017-09-14 12:36:48 +02:00
public void Login()
2017-09-12 14:32:34 +02:00
{
Anmelden();
2017-09-12 14:32:34 +02:00
}
public event PropertyChangedEventHandler PropertyChanged;
2017-09-12 14:32:34 +02:00
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
2017-09-12 14:32:34 +02:00
}