Files
Massmailer/Massmailer.UI/Views/MainView.xaml.cs

208 lines
7.3 KiB
C#

using Massmailer.Shared.Events;
using Massmailer.Shared.Logic;
using Massmailer.Shared.Model;
using Microsoft.Win32;
using PubSub;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
namespace Massmailer.UI
{
public partial class MainView : Window
{
private readonly Hub hub = new Hub();
private SmtpSettings smtpSettings;
private MassmailerProject project = new MassmailerProject();
private string selectedFilePath = string.Empty;
private bool isDirty = false;
public MainView()
{
this.InitializeComponent();
this.hub.Subscribe<MailSentEvent>(this.MailSentEventHandler);
}
private async void Window_Loaded(object sender, RoutedEventArgs e)
{
this.smtpSettings = await SmtpSettingsLogic.Load(Path.Combine(Environment.CurrentDirectory, Constants.SmtpSettingsFileName));
}
private void MailSentEventHandler(MailSentEvent mailSentEvent)
{
//this.BtnSend.Content = $"mail sent: {mailSentEvent.Recipient} at {mailSentEvent.Timestamp}";
}
private void MnuFileNew_Click(object sender, RoutedEventArgs e)
{
if (this.isDirty)
{
var result = MessageBox.Show("Aktuelle Änderungen speichern?", "Massmailer", MessageBoxButton.YesNoCancel);
if (result == MessageBoxResult.Cancel)
{
return;
}
if (result == MessageBoxResult.Yes)
{
MassmailerProjectLogic.Save(this.project, this.GetSaveFilePath(false));
}
// TODO untested
this.project = new MassmailerProject();
this.DataContext = this.project;
this.Title = "Massmailer";
}
}
private async void MnuFileOpen_Click(object sender, RoutedEventArgs e)
{
if (this.isDirty)
{
var result = MessageBox.Show("Aktuelle Änderungen speichern?", "Massmailer", MessageBoxButton.YesNoCancel);
if (result == MessageBoxResult.Cancel)
{
return;
}
if (result == MessageBoxResult.Yes)
{
MassmailerProjectLogic.Save(this.project, this.GetSaveFilePath(false));
}
}
// TODO untested
var openFileDialog = new OpenFileDialog();
openFileDialog.DefaultExt = $".{Constants.ProjectFileExtension}";
openFileDialog.Filter = $"Massmailer Projekte|*.{Constants.ProjectFileExtension}|Alle Dateien|*.*";
if (openFileDialog.ShowDialog() == true)
{
this.project = await MassmailerProjectLogic.Load(openFileDialog.FileName);
this.DataContext = this.project;
this.selectedFilePath = openFileDialog.FileName;
this.Title = $"Massmailer - {openFileDialog.FileName}";
}
}
private void MnuFileSave_Click(object sender, RoutedEventArgs e)
{
MassmailerProjectLogic.Save(this.project, this.GetSaveFilePath(false));
}
private void MnuFileSaveAs_Click(object sender, RoutedEventArgs e)
{
MassmailerProjectLogic.Save(this.project, this.GetSaveFilePath(true));
}
private void MnuFileQuit_Click(object sender, RoutedEventArgs e)
{
if (this.isDirty)
{
var result = MessageBox.Show("Aktuelle Änderungen speichern?", "Massmailer", MessageBoxButton.YesNoCancel);
if (result == MessageBoxResult.Cancel)
{
return;
}
if (result == MessageBoxResult.Yes)
{
MassmailerProjectLogic.Save(this.project, this.GetSaveFilePath(false));
}
}
Application.Current.Shutdown();
}
private void MnuHelpAbout_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("todo");
}
private async void BtnSend_Click(object sender, RoutedEventArgs e)
{
var smtpSettings = new SmtpSettings
{
Server = "localhost",
Port = 25,
Username = "test@test.local",
Password = "asdf33asdf",
UseSsl = false,
SenderEmail = "test@test.local",
SenderName = "Massmailer Test",
MaxSendRate = 60
};
var mailer = new Mailer(smtpSettings, this.hub);
var recipients = new List<string>
{
"1@test.local",
"2@test.local",
"3@test.local",
"4@test.local",
"5@test.local",
"6@test.local",
"7@test.local",
"8@test.local",
"9@test.local",
"10@test.local"
};
var project = new MassmailerProject();
project.Recipients.Add(new MassmailerRecipient { Address = "1@test.local" });
project.Recipients.Add(new MassmailerRecipient { Address = "2@test.local" });
project.Recipients.Add(new MassmailerRecipient { Address = "3@test.local" });
project.Recipients.Add(new MassmailerRecipient { Address = "4@test.local" });
project.Recipients.Add(new MassmailerRecipient { Address = "5@test.local" });
project.Recipients.Add(new MassmailerRecipient { Address = "6@test.local" });
project.Recipients.Add(new MassmailerRecipient { Address = "7@test.local" });
project.Recipients.Add(new MassmailerRecipient { Address = "8@test.local" });
project.Recipients.Add(new MassmailerRecipient { Address = "9@test.local" });
project.Recipients.Add(new MassmailerRecipient { Address = "10@test.local" });
project.Subject = "Hello World Subject";
project.Body = "Hello World Body";
await mailer.SendMailsAsync(project);
var result = "";
foreach (var recipient in project.Recipients)
{
result += $"{recipient.Address} - {recipient.IsSent} - {recipient.SentDate}\r\n";
}
MassmailerProjectLogic.Save(project, "project.json");
MessageBox.Show(result);
}
private string GetSaveFilePath(bool force)
{
if (string.IsNullOrEmpty(this.selectedFilePath) || force)
{
var savefileDialog = new SaveFileDialog();
savefileDialog.DefaultExt = $".{Constants.ProjectFileExtension}";
savefileDialog.Filter = $"Massmailer Projekte|*.{Constants.ProjectFileExtension}|Alle Dateien|*.*";
if (savefileDialog.ShowDialog() == true)
{
this.selectedFilePath = savefileDialog.FileName;
}
}
return this.selectedFilePath;
}
}
}