Skip to content

Commit

Permalink
Bug Fix on selection & adding "diagnostic delivery"
Browse files Browse the repository at this point in the history
  • Loading branch information
BlythMeister committed Nov 28, 2012
1 parent f43a55c commit 4ace5a3
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 49 deletions.
2 changes: 2 additions & 0 deletions SecretSantaHelper/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@
<Button Content="Cancel" FontFamily="Calibri" Height="44" HorizontalAlignment="Left" IsEnabled="False" Margin="116,429,0,0" Name="btnCancel" VerticalAlignment="Top" Width="92" Grid.Column="1" Click="btnCancel_Click" Background="#FF1C892C" Foreground="White" />
<Button Content="Edit Person" FontFamily="Calibri" Height="44" HorizontalAlignment="Left" Margin="167,164,0,0" Name="btnEdit" VerticalAlignment="Top" Width="92" Grid.Column="1" Click="btnEdit_Click" Background="#FF1C892C" Foreground="White" BorderBrush="#FF005F20" />
<Button Background="#FF1C892C" BorderBrush="#FF005F20" Content="Send Details" FontFamily="Calibri" Foreground="White" Height="44" HorizontalAlignment="Left" Margin="167,64,0,0" Name="btnSendDetails" VerticalAlignment="Top" Width="92" Grid.Column="1" Click="btnSendDetails_Click" />
<Label Foreground="White" Height="28" HorizontalAlignment="Left" Margin="154,12,0,0" Name="lblVersion" VerticalAlignment="Top" Grid.Column="1" Width="108" Content="Version: 0.0.0.0" FontSize="14" />
<Image Height="51" HorizontalAlignment="Left" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="62" Source="/SecretSantaHelper;component/santa.ico" Margin="20,6,0,0" Grid.Column="1" />
</Grid>
</Window>
88 changes: 66 additions & 22 deletions SecretSantaHelper/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Text;
using System.Windows;
using System.Windows.Forms;
using Application = System.Windows.Forms.Application;
using MessageBox = System.Windows.Forms.MessageBox;

namespace SecretSantaHelper
Expand All @@ -15,14 +16,15 @@ namespace SecretSantaHelper
/// </summary>
public partial class MainWindow : Window
{

private bool addClicked = false;
private Participant selectedParticipant;
private SantaSack santaSack;

public MainWindow()
{
InitializeComponent();
lblVersion.Content = string.Format("Version: {0}", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version);
santaSack = SantaSackSerializer.Deserialize();
lstParticipants.ItemsSource = from participant in santaSack.Participants select participant.DisplayValue();
}
Expand All @@ -38,6 +40,11 @@ private void btnImport_Click(object sender, RoutedEventArgs e)

if (filePicker.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if (MessageBox.Show("Do You Want To Clear Previous Items Before Importing?", "Clear Previous?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
{
santaSack.Participants.Clear();
}

try
{
var dataRead = File.ReadLines(filePicker.FileName);
Expand All @@ -60,6 +67,7 @@ private void btnImport_Click(object sender, RoutedEventArgs e)
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
SantaSackSerializer.Serialize(santaSack);
}

private void btnExport_Click(object sender, RoutedEventArgs e)
Expand All @@ -85,7 +93,7 @@ private void btnExport_Click(object sender, RoutedEventArgs e)
private void btnSave_Click(object sender, RoutedEventArgs e)
{
var emailHelper = new RegexUtilities();
if(!emailHelper.IsValidEmail(txtEmail.Text))
if (!emailHelper.IsValidEmail(txtEmail.Text))
{
MessageBox.Show("That is an invalid email address!");
return;
Expand Down Expand Up @@ -136,6 +144,7 @@ private void btnSave_Click(object sender, RoutedEventArgs e)

lstParticipants.ItemsSource = null;
lstParticipants.ItemsSource = from participant in santaSack.Participants select participant.DisplayValue();
SantaSackSerializer.Serialize(santaSack);
}

private void btnCancel_Click(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -206,16 +215,19 @@ private void btnRemove_Click(object sender, RoutedEventArgs e)
lstParticipants.ItemsSource = null;
lstParticipants.ItemsSource = from participant in santaSack.Participants select participant.DisplayValue();
}
SantaSackSerializer.Serialize(santaSack);
}

private void btnGo_Click(object sender, RoutedEventArgs e)
{
SantaSackSerializer.Serialize(santaSack);

if (santaSack.Participants.Count < 3)
{
MessageBox.Show("You Haven't Got Enough People!");
return;
}

if (string.IsNullOrWhiteSpace(santaSack.Template.FromAddress))
{
MessageBox.Show("You Haven't Entered A From Address!");
Expand Down Expand Up @@ -267,7 +279,7 @@ private void btnGo_Click(object sender, RoutedEventArgs e)
var participantsToPair = (from p in santaSack.Participants select p).ToList();
var participantsToAssign = (from p in santaSack.Participants select p).ToList();
var participantsToSend = new List<PairedParticipant>();
var randomGenerator = new Random();
var randomGenerator = new Random((int)DateTime.UtcNow.Ticks);

while (participantsToPair.Count > 0)
{
Expand Down Expand Up @@ -318,31 +330,58 @@ private void btnGo_Click(object sender, RoutedEventArgs e)

}

foreach (var pairedParticipant in participantsToSend)
while (participantsToSend.Any(x => x.Sent == false))
{
var message = new MailMessage();
message.From = new MailAddress(santaSack.Template.FromAddress);
message.To.Add(new MailAddress(pairedParticipant.EmailAddress));
message.Subject = santaSack.Template.Subject;
message.Body = santaSack.Template.Content
.Replace("{{GiftFromName}}", pairedParticipant.Name)
.Replace("{{GiftFromEmail}}", pairedParticipant.EmailAddress)
.Replace("{{GiftForName}}", pairedParticipant.PairedWith.Name)
.Replace("{{GiftForEmail}}", pairedParticipant.PairedWith.EmailAddress);
var client = new SmtpClient();
client.Host = santaSack.Template.Host;
int port;
if (!int.TryParse(santaSack.Template.Port, out port))
if(participantsToSend.Any(x=>x.SendAttempts > 5))
{
foreach (var tooManyAttemptParticipant in participantsToSend.Where(x=>x.SendAttempts>5))
{
MessageBox.Show(
string.Format("Couldn't Send Email To {0}, They Drew {1}",
tooManyAttemptParticipant.EmailAddress,
tooManyAttemptParticipant.PairedWith.EmailAddress), "Couldn't Send",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}

foreach (var pairedParticipant in participantsToSend.Where(x => x.Sent == false))
{
port = 25;
var message = new MailMessage();
message.From = new MailAddress(santaSack.Template.FromAddress);
message.To.Add(!string.IsNullOrWhiteSpace(santaSack.Template.DiagnosticDeliveryAddress)
? new MailAddress(santaSack.Template.DiagnosticDeliveryAddress)
: new MailAddress(pairedParticipant.EmailAddress));

message.Subject = santaSack.Template.Subject;
message.Body = santaSack.Template.Content
.Replace("{{GiftFromName}}", pairedParticipant.Name)
.Replace("{{GiftFromEmail}}", pairedParticipant.EmailAddress)
.Replace("{{GiftForName}}", pairedParticipant.PairedWith.Name)
.Replace("{{GiftForEmail}}", pairedParticipant.PairedWith.EmailAddress);
var client = new SmtpClient();
client.Host = santaSack.Template.Host;
int port;
if (!int.TryParse(santaSack.Template.Port, out port))
{
port = 25;
}
client.Port = port;
try
{
client.Send(message);
pairedParticipant.Sent = true;
}
catch (Exception)
{
pairedParticipant.Sent = false;
pairedParticipant.SendAttempts++;
}
}
client.Port = port;
client.Send(message);
}

MessageBox.Show("All Emails Have Been Sent!");
}

private void btnSendDetails_Click(object sender, RoutedEventArgs e)
{
var sendDetails = new SendDetails(ref santaSack);
Expand All @@ -353,5 +392,10 @@ private void Window_Closed(object sender, EventArgs e)
{
SantaSackSerializer.Serialize(santaSack);
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{

}
}
}
2 changes: 2 additions & 0 deletions SecretSantaHelper/PairedParticipant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ namespace SecretSantaHelper
public class PairedParticipant : Participant
{
public Participant PairedWith { get; set; }
public bool Sent { get; set; }
public int SendAttempts { get; set; }
}
}
4 changes: 2 additions & 2 deletions SecretSantaHelper/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.0.2.0")]
[assembly: AssemblyFileVersion("0.0.2.0")]
[assembly: AssemblyVersion("0.0.3.0")]
[assembly: AssemblyFileVersion("0.0.3.0")]
Binary file modified SecretSantaHelper/SecretSantaHelper.suo
Binary file not shown.
30 changes: 18 additions & 12 deletions SecretSantaHelper/SendDetails.xaml
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
<Window x:Class="SecretSantaHelper.SendDetails"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Secret Santa Helper Template Editor" Height="397" Width="409" Closed="Window_Closed" Icon="/SecretSantaHelper;component/santa.ico" WindowStyle="SingleBorderWindow">
<Grid Background="#FFA01717" Height="362" Width="392">
Title="Secret Santa Helper Template Editor" Height="425" Width="409" Closed="Window_Closed" Icon="/SecretSantaHelper;component/santa.ico" WindowStyle="SingleBorderWindow">
<Grid Background="#FFA01717" Height="390" Width="392">
<Grid.RowDefinitions>
<RowDefinition Height="340*" />
<RowDefinition Height="40*" />
</Grid.RowDefinitions>
<TextBox Height="28" HorizontalAlignment="Right" IsEnabled="True" Margin="0,10,123,0" Name="txtSMTP" VerticalAlignment="Top" Width="161" />
<Label Content="SMTP Server:" Foreground="White" Height="28" HorizontalAlignment="Left" Margin="18,12,0,0" Name="lblName" VerticalAlignment="Top" />
<TextBox Height="28" HorizontalAlignment="Right" IsEnabled="True" Margin="0,12,12,0" Name="txtPort" VerticalAlignment="Top" Width="64" />
<Label Content="Port:" Foreground="White" Height="28" HorizontalAlignment="Left" Margin="275,12,0,0" Name="label1" VerticalAlignment="Top" />
<Label Content="SMTP Server:" Foreground="White" Height="28" HorizontalAlignment="Left" Margin="27,12,0,0" Name="lblName" VerticalAlignment="Top" />
<TextBox Height="28" HorizontalAlignment="Right" IsEnabled="True" Margin="0,11,12,0" Name="txtPort" VerticalAlignment="Top" Width="64" />
<Label Content="Port:" Foreground="White" Height="28" HorizontalAlignment="Left" Margin="279,12,0,0" Name="label1" VerticalAlignment="Top" />
<TextBox Height="28" HorizontalAlignment="Right" IsEnabled="True" Margin="0,44,12,0" Name="txtFrom" VerticalAlignment="Top" Width="272" />
<Label Content="From Address:" Foreground="White" Height="28" HorizontalAlignment="Left" Margin="12,42,0,0" Name="label2" VerticalAlignment="Top" />
<TextBox Height="28" HorizontalAlignment="Right" IsEnabled="True" Margin="0,78,12,0" Name="txtSubject" VerticalAlignment="Top" Width="272" />
<Label Content="Subject:" Foreground="White" Height="28" HorizontalAlignment="Left" Margin="43,78,0,0" Name="label3" VerticalAlignment="Top" />
<TextBox Height="182" HorizontalAlignment="Right" IsEnabled="True" Margin="0,112,12,0" Name="txtContent" VerticalAlignment="Top" Width="272" TextWrapping="Wrap" AcceptsReturn="True" AcceptsTab="True" />
<Label Content="Email Body:" Foreground="White" Height="28" HorizontalAlignment="Left" Margin="24,112,0,0" Name="label4" VerticalAlignment="Top" />
<TextBlock Height="111" HorizontalAlignment="Left" Margin="10,146,0,0" Name="textBlock1" Text="Tags: {{GiftFromName}} {{GiftFromEmail}} {{GiftForName}} {{GiftForEmail}}" VerticalAlignment="Top" Foreground="White" TextWrapping="Wrap" Width="99" />
<Button Background="#FF1C892C" BorderBrush="#FF005F20" Content="Done" FontFamily="Calibri" Foreground="White" Height="44" HorizontalAlignment="Left" Margin="122,302,0,0" Name="btnDone" VerticalAlignment="Top" Width="92" Click="btnDone_Click" />
<Label Content="From Address:" Foreground="White" Height="28" HorizontalAlignment="Left" Margin="21,42,0,0" Name="label2" VerticalAlignment="Top" />
<TextBox Height="28" HorizontalAlignment="Right" IsEnabled="True" Margin="0,113,12,0" Name="txtSubject" VerticalAlignment="Top" Width="272" />
<Label Content="Subject:" Foreground="White" Height="28" HorizontalAlignment="Left" Margin="55,113,0,0" Name="label3" VerticalAlignment="Top" />
<TextBox Height="182" HorizontalAlignment="Right" IsEnabled="True" Margin="0,147,12,0" Name="txtContent" VerticalAlignment="Top" Width="272" TextWrapping="Wrap" AcceptsReturn="True" AcceptsTab="True" />
<Label Content="Email Body:" Foreground="White" Height="28" HorizontalAlignment="Left" Margin="36,147,0,0" Name="label4" VerticalAlignment="Top" />
<TextBlock Height="111" HorizontalAlignment="Left" Margin="10,181,0,0" Name="textBlock1" Text="Tags: {{GiftFromName}} {{GiftFromEmail}} {{GiftForName}} {{GiftForEmail}}" VerticalAlignment="Top" Foreground="White" TextWrapping="Wrap" Width="99" />
<Button Background="#FF1C892C" BorderBrush="#FF005F20" Content="Done" FontFamily="Calibri" Foreground="White" Height="44" HorizontalAlignment="Left" Margin="122,337,0,0" Name="btnDone" VerticalAlignment="Top" Width="92" Click="btnDone_Click" Grid.RowSpan="2" />
<TextBox Height="28" HorizontalAlignment="Right" IsEnabled="True" Margin="0,78,12,0" Name="txtDiagnostic" VerticalAlignment="Top" Width="272" />
<Label Content="Diagnostic Delivery:" Foreground="White" Height="28" HorizontalAlignment="Left" Margin="-4,76,0,0" Name="label5" VerticalAlignment="Top" />
</Grid>
</Window>
33 changes: 20 additions & 13 deletions SecretSantaHelper/SendDetails.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@ namespace SecretSantaHelper
/// </summary>
public partial class SendDetails : Window
{
private readonly SantaSack templateDetails;
private readonly SantaSack santaSack;

public SendDetails(ref SantaSack templateDetails)
public SendDetails(ref SantaSack santaSack)
{
this.templateDetails = templateDetails;
this.santaSack = santaSack;
InitializeComponent();
txtSMTP.Text = templateDetails.Template.Host;
txtPort.Text = templateDetails.Template.Port;
txtFrom.Text = templateDetails.Template.FromAddress;
txtSubject.Text = templateDetails.Template.Subject;
txtContent.Text = templateDetails.Template.Content;
txtSMTP.Text = santaSack.Template.Host;
txtPort.Text = santaSack.Template.Port;
txtFrom.Text = santaSack.Template.FromAddress;
txtSubject.Text = santaSack.Template.Subject;
txtContent.Text = santaSack.Template.Content;
txtDiagnostic.Text = santaSack.Template.DiagnosticDeliveryAddress;
btnDone.IsEnabled = true;
}

Expand All @@ -35,16 +36,22 @@ private void btnDone_Click(object sender, RoutedEventArgs e)
MessageBox.Show("That is an invalid from email address!");
return;
}
if (!string.IsNullOrWhiteSpace(txtDiagnostic.Text) && !emailHelper.IsValidEmail(txtDiagnostic.Text))
{
MessageBox.Show("That is an invalid diagnostic email address!");
return;
}
Close();
}

private void Window_Closed(object sender, System.EventArgs e)
{
templateDetails.Template.Host = txtSMTP.Text;
templateDetails.Template.Port = txtPort.Text;
templateDetails.Template.FromAddress = txtFrom.Text;
templateDetails.Template.Subject = txtSubject.Text;
templateDetails.Template.Content = txtContent.Text;
santaSack.Template.Host = txtSMTP.Text;
santaSack.Template.Port = txtPort.Text;
santaSack.Template.FromAddress = txtFrom.Text;
santaSack.Template.Subject = txtSubject.Text;
santaSack.Template.Content = txtContent.Text;
santaSack.Template.DiagnosticDeliveryAddress = txtDiagnostic.Text;
}
}
}
2 changes: 2 additions & 0 deletions SecretSantaHelper/Template.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class Template
public string Host { get; set; }
public string Port { get; set; }
public string FromAddress { get; set; }
public string DiagnosticDeliveryAddress { get; set; }
public string Subject { get; set; }
public string Content { get; set; }

Expand All @@ -21,6 +22,7 @@ public Template()
FromAddress = string.Empty;
Subject = string.Empty;
Content = string.Empty;
DiagnosticDeliveryAddress = string.Empty;
}
}
}

0 comments on commit 4ace5a3

Please sign in to comment.