311 lines
9.3 KiB
C#
311 lines
9.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.IO;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Text;
|
|
using System.Windows;
|
|
using System.Windows.Threading;
|
|
using ChatController.Annotations;
|
|
using ChatController.HauptKlassen;
|
|
using ChatController.LoginKlassen;
|
|
using ChatController.Utilities;
|
|
using ChatController.Utilities.Extensions;
|
|
using MessageBox = System.Windows.MessageBox;
|
|
using Panel = System.Windows.Controls.Panel;
|
|
using Path = System.IO.Path;
|
|
|
|
namespace ChatController
|
|
{
|
|
public partial class LoginControl : INotifyPropertyChanged
|
|
{
|
|
private ChatControlWaitLayer _WaitLayer;
|
|
|
|
public string UserName
|
|
{
|
|
get => _Benutzername;
|
|
|
|
set
|
|
{
|
|
if(Equals(_Benutzername, value))
|
|
{
|
|
return;
|
|
}
|
|
|
|
_Benutzername = value;
|
|
OnPropertyChanged(nameof(UserName));
|
|
}
|
|
}
|
|
|
|
public string Password
|
|
{
|
|
get => PasswordBox.Password;
|
|
|
|
set => PasswordBox.Password = value;
|
|
}
|
|
|
|
public string ChatCode
|
|
{
|
|
get => _ChatCode;
|
|
|
|
set
|
|
{
|
|
if(Equals(_ChatCode, value))
|
|
{
|
|
return;
|
|
}
|
|
|
|
_ChatCode = value;
|
|
OnPropertyChanged(nameof(ChatCode));
|
|
}
|
|
}
|
|
|
|
public string Tenant
|
|
{
|
|
get => _Kundennummer;
|
|
|
|
set
|
|
{
|
|
if(Equals(_Kundennummer, value))
|
|
{
|
|
return;
|
|
}
|
|
|
|
_Kundennummer = value;
|
|
OnPropertyChanged(nameof(Tenant));
|
|
}
|
|
}
|
|
|
|
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.11";
|
|
|
|
public string Version => $"Version: {_Version}";
|
|
|
|
public LoginControl()
|
|
{
|
|
InitializeComponent();
|
|
|
|
DataContext = this;
|
|
|
|
LeseDateiFallsVorhanden();
|
|
}
|
|
|
|
private void AnmeldenButton_OnClick(object sender, RoutedEventArgs e)
|
|
{
|
|
Anmelden();
|
|
}
|
|
|
|
private void StartWaiting()
|
|
{
|
|
Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action) StartWaitingImmediately);
|
|
}
|
|
|
|
private void StartWaitingImmediately()
|
|
{
|
|
if(!(_WaitLayer is null))
|
|
{
|
|
return;
|
|
}
|
|
|
|
_WaitLayer = new ChatControlWaitLayer();
|
|
RootGrid.Children.Add(_WaitLayer);
|
|
Panel.SetZIndex(_WaitLayer, int.MaxValue);
|
|
_WaitLayer.RefreshUI();
|
|
}
|
|
|
|
private void EndWaiting()
|
|
{
|
|
Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action) delegate
|
|
{
|
|
if(_WaitLayer is null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
RootGrid.Children.Remove(_WaitLayer);
|
|
_WaitLayer = null;
|
|
});
|
|
}
|
|
|
|
private void Anmelden()
|
|
{
|
|
if(!string.IsNullOrWhiteSpace(_Kundennummer) && !string.IsNullOrWhiteSpace(_ChatCode) && !string.IsNullOrWhiteSpace(_Benutzername) && Password.Length > 0)
|
|
{
|
|
StartWaiting();
|
|
|
|
var login = new Login(_Kundennummer, _ChatCode, _Benutzername, PasswordBox.Password);
|
|
|
|
login.DoLoginAsync(chatDatenUebergabe =>
|
|
{
|
|
this.Dispatch(() =>
|
|
{
|
|
EndWaiting();
|
|
|
|
if(chatDatenUebergabe is null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
AutosetDaten();
|
|
|
|
OnLogin?.Invoke(chatDatenUebergabe);
|
|
});
|
|
},
|
|
errorMessage =>
|
|
{
|
|
this.Dispatch(() =>
|
|
{
|
|
EndWaiting();
|
|
|
|
MessageBox.Show(errorMessage, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
});
|
|
});
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Bitte alle Felder ausfüllen!", "Info", MessageBoxButton.OK);
|
|
}
|
|
}
|
|
|
|
private void LoadTenantAndChatCodeFromFile()
|
|
{
|
|
if(File.Exists(Path.Combine(GetAndCreateUserAppDataPath(), Constants.TenantAndChatCodeFileName)))
|
|
{
|
|
var lines = new List<string>();
|
|
using(var streamReader = new StreamReader(Path.Combine(GetAndCreateUserAppDataPath(), Constants.TenantAndChatCodeFileName), true))
|
|
{
|
|
string line;
|
|
|
|
while(!((line = streamReader.ReadLine()) is null))
|
|
{
|
|
var encodedTextBytes = Convert.FromBase64String(line);
|
|
|
|
var plainText = Encoding.UTF8.GetString(encodedTextBytes);
|
|
|
|
lines.Add(plainText);
|
|
}
|
|
}
|
|
|
|
if(lines.Count == 2)
|
|
{
|
|
_Kundennummer = lines[0];
|
|
_ChatCode = lines[1];
|
|
}
|
|
}
|
|
|
|
#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";
|
|
if (File.Exists(debugFile))
|
|
{
|
|
using(var streamReader2 = new StreamReader(debugFile, true))
|
|
{
|
|
string line;
|
|
|
|
var counter = 0;
|
|
|
|
while(!((line = streamReader2.ReadLine()) is null))
|
|
{
|
|
if(counter == 0)
|
|
{
|
|
var encodedTextBytes = Convert.FromBase64String(line);
|
|
|
|
var plainText = Encoding.UTF8.GetString(encodedTextBytes);
|
|
|
|
UserName = plainText;
|
|
}
|
|
else if(counter == 1)
|
|
{
|
|
var encodedTextBytes = Convert.FromBase64String(line);
|
|
|
|
var plainText = Encoding.UTF8.GetString(encodedTextBytes);
|
|
|
|
Password = plainText;
|
|
}
|
|
|
|
counter++;
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
}
|
|
|
|
private void LeseDateiFallsVorhanden()
|
|
{
|
|
LoadTenantAndChatCodeFromFile();
|
|
}
|
|
|
|
private void AutosetDaten()
|
|
{
|
|
if(File.Exists(Path.Combine(GetAndCreateUserAppDataPath(), Constants.TenantAndChatCodeFileName)))
|
|
{
|
|
File.SetAttributes(Path.Combine(GetAndCreateUserAppDataPath(), Constants.TenantAndChatCodeFileName), FileAttributes.Normal);
|
|
File.Delete(Path.Combine(GetAndCreateUserAppDataPath(), Constants.TenantAndChatCodeFileName));
|
|
}
|
|
|
|
using(var streamWriter = new StreamWriter(Path.Combine(GetAndCreateUserAppDataPath(), Constants.TenantAndChatCodeFileName)))
|
|
{
|
|
Encoding utf8Encoding = new UTF8Encoding();
|
|
var tenantBytes = utf8Encoding.GetBytes(_Kundennummer);
|
|
var encodedTenant = Convert.ToBase64String(tenantBytes);
|
|
|
|
var chatCodeBytes = utf8Encoding.GetBytes(_ChatCode);
|
|
var encodedChatCode = Convert.ToBase64String(chatCodeBytes);
|
|
|
|
streamWriter.WriteLine(encodedTenant);
|
|
streamWriter.WriteLine(encodedChatCode);
|
|
|
|
File.SetAttributes(Path.Combine(GetAndCreateUserAppDataPath(), Constants.TenantAndChatCodeFileName), FileAttributes.ReadOnly);
|
|
}
|
|
}
|
|
|
|
private static string GetAndCreateUserAppDataPath()
|
|
{
|
|
var localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
|
|
var companyFilePath = Path.Combine(localAppData, "ownSoft");
|
|
|
|
if(!Directory.Exists(companyFilePath))
|
|
{
|
|
Directory.CreateDirectory(companyFilePath);
|
|
}
|
|
|
|
var bewoFilePath = Path.Combine(companyFilePath, "OwnChat");
|
|
|
|
if(!Directory.Exists(bewoFilePath))
|
|
{
|
|
Directory.CreateDirectory(bewoFilePath);
|
|
}
|
|
|
|
return bewoFilePath;
|
|
}
|
|
|
|
public void Login()
|
|
{
|
|
Anmelden();
|
|
}
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
[NotifyPropertyChangedInvocator]
|
|
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
|
|
{
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
}
|
|
}
|
|
}
|