Files
Chat/ChatController/LoginControl.xaml.cs

340 lines
11 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;
2017-10-06 12:08:05 +02:00
using System.Reflection;
using System.Runtime.CompilerServices;
2017-09-12 14:32:34 +02:00
using System.Text;
using System.Windows;
using System.Windows.Forms;
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 Cursors = System.Windows.Input.Cursors;
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
{
if (!Equals(_Benutzername, value))
{
_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))
{
_ChatCode = value;
OnPropertyChanged(nameof(ChatCode));
}
}
}
public string Tenant
{
get => _Kundennummer;
set
{
if(!Equals(_Kundennummer, value))
{
_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;
2021-10-19 11:09:00 +02:00
private readonly string _Version = "1.6";
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
{
if (_WaitLayer is null)
2017-10-20 07:34:26 +02:00
{
_WaitLayer = new ChatControlWaitLayer();
Panel.SetZIndex(_WaitLayer, int.MaxValue);
RootGrid.Children.Add(_WaitLayer);
_WaitLayer.RefreshUI();
2017-10-20 07:34:26 +02:00
}
}
private void EndWaiting()
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action) delegate
{
if(!(_WaitLayer is null))
{
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
{
2017-09-15 16:39:07 +02:00
try
{
if (!string.IsNullOrWhiteSpace(_Kundennummer) && !string.IsNullOrWhiteSpace(_ChatCode) && !string.IsNullOrWhiteSpace(_Benutzername) && Password.Length > 0)
2017-10-20 07:34:26 +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 =>
2017-09-12 14:32:34 +02:00
{
this.Dispatch(() =>
{
EndWaiting();
2019-04-04 13:00:10 +02:00
if(chatDatenUebergabe != null)
{
AutosetDaten();
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-12 14:32:34 +02:00
}
else
{
2017-10-06 12:08:05 +02:00
MessageBox.Show("Bitte alle Felder ausfüllen!", "Info", MessageBoxButton.OK);
2017-09-12 14:32:34 +02:00
}
2017-09-15 16:39:07 +02:00
}
catch (Exception exception)
{
2017-10-24 12:22:05 +02:00
MessageBox.Show("Fehler: " + exception.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
EndWaiting();
Cursor = Cursors.Arrow;
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
{
try
{
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 (File.Exists(debugFile))
{
using(var streamReader2 = new StreamReader(debugFile, true))
{
string line;
var counter = 0;
while(!((line = streamReader2.ReadLine()) is null))
{
if(counter == 0)
{
try
{
var encodedTextBytes = Convert.FromBase64String(line);
var plainText = Encoding.UTF8.GetString(encodedTextBytes);
UserName = plainText;
}
catch (Exception)
{
MessageBox.Show("Der Zeile muss im Base64-Format kodiert sein.", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
else if(counter == 1)
{
try
{
var encodedTextBytes = Convert.FromBase64String(line);
var plainText = Encoding.UTF8.GetString(encodedTextBytes);
Password = plainText;
}
catch (Exception)
{
MessageBox.Show("Der Zeile muss im Base64-Format kodiert sein.", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
counter++;
}
}
}
#endif
2017-09-12 14:32:34 +02:00
}
catch(Exception e)
2017-09-12 14:32:34 +02:00
{
MessageBox.Show("Fehler: \n" + e.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
2017-09-12 14:32:34 +02:00
}
}
private void LeseDateiFallsVorhanden()
{
LoadTenantAndChatCodeFromFile();
}
private void AutosetDaten()
2017-09-12 14:32:34 +02:00
{
try
{
if(File.Exists(Path.Combine(GetAndCreateUserAppDataPath(), Constants.TenantAndChatCodeFileName)))
{
File.SetAttributes(Path.Combine(GetAndCreateUserAppDataPath(), Constants.TenantAndChatCodeFileName), FileAttributes.Normal);
File.Delete(Path.Combine(GetAndCreateUserAppDataPath(), Constants.TenantAndChatCodeFileName));
2017-10-06 12:08:05 +02: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
var chatCodeBytes = utf8Encoding.GetBytes(_ChatCode);
var encodedChatCode = Convert.ToBase64String(chatCodeBytes);
2017-10-06 12:08:05 +02:00
streamWriter.WriteLine(encodedTenant);
streamWriter.WriteLine(encodedChatCode);
2017-10-06 12:08:05 +02:00
File.SetAttributes(Path.Combine(GetAndCreateUserAppDataPath(), Constants.TenantAndChatCodeFileName), FileAttributes.ReadOnly);
}
}
catch (Exception e)
{
MessageBox.Show("Fehler: \n" + e.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
2017-10-20 07:34:26 +02:00
private static string GetAndCreateUserAppDataPath()
{
try
{
var localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
var companyFilePath = Path.Combine(localAppData, "beyondSoft");
if(!Directory.Exists(companyFilePath))
{
2017-10-06 12:08:05 +02:00
Directory.CreateDirectory(companyFilePath);
}
var bewoFilePath = Path.Combine(companyFilePath, "OwnChat");
2017-09-14 12:36:48 +02:00
if(!Directory.Exists(bewoFilePath))
{
2017-10-06 12:08:05 +02:00
Directory.CreateDirectory(bewoFilePath);
}
2017-09-14 12:36:48 +02:00
2017-10-06 12:08:05 +02:00
return bewoFilePath;
}
catch (Exception ex)
{
MessageBox.Show(
"Fehler beim Anlegen des Anwendungsordners. Sie besitzen nicht die erforderlichen Rechte, bitte wenden Sie sich an Ihren Systemadministrator.\n" +
ex.Message, "BeWoPlaner", MessageBoxButton.OK, MessageBoxImage.Exclamation);
}
return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
}
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
}