private MemoryStream SaveToExcel(string outputDir, bool getMemoryStream) { // grab the invoice title string title = GetInvoiceTitle(); // finally, write the data and save the spreadsheet SetStatusBar("Exporting Spreadsheet", StatusBarState.InProgress); MemoryStream ms = null; try { ExcelWriter excelWriter = new ExcelWriter(outputDir, title, Configuration.SenderEmailAddress, Configuration.RecipientEmailAddress); excelWriter.AddItems(this._view.ItemsListEntries.ToList()); if (getMemoryStream) { ms = excelWriter.CloseAndGetMemoryStream(); } else { excelWriter.CloseAndSave(); } } catch (Exception) { // it failed // tell the user via a dialog and reset the status bar // TODO: log error this._view.ShowErrorDialogOk("Error exporting spreadsheet"); SetStatusBar("Ready", StatusBarState.Ready); return(null); } // it succeeded // tell the user via a dialog and reset the status bar this._view.ShowSuccessDialog("Exported spreadsheet"); SetStatusBar("Ready", StatusBarState.Ready); return(ms); }
public void SaveAndEmailButtonClicked(object sender, EventArgs args) { // disable controls the user shouldn't play with at this point DisableControlsDuringOperation(); // check if an invoice with this title already exists // if not, grab the title string title = GetInvoiceTitle(); if (this._view.CreatingNewInvoice) { bool exists = this._repo.InvoiceWithTitleExists(title); if (exists) { // invoice with this title already exists // tell the user via a dialog this._view.ShowErrorDialogOk("Invoice with title: " + title + " already exists. Please choose a different title."); EnableControlsAfterOperation(); return; } } // show send email dialog EmailWindowPresenter emailWindowPresenter = new EmailWindowPresenter(new EmailWindow(title, Configuration.INVALID_INPUT_COLOUR, Configuration.SenderEmailAddress, Configuration.RecipientEmailAddress), new EmailModel(Configuration.INVALID_INPUT_COLOUR)); emailWindowPresenter.View.Subject = "Invoice: " + title; // set default email subject if (this._view.CreatingNewInvoice) { emailWindowPresenter.View.SendButtonText = "Save and Send"; emailWindowPresenter.View.CancelButtonText = "Cancel Save and Send"; } else { emailWindowPresenter.View.SendButtonText = "Send"; emailWindowPresenter.View.CancelButtonText = "Cancel Send"; } DialogResult emailDialogResult = emailWindowPresenter.ShowDialog(); // send email, or cancel if (emailDialogResult == DialogResult.OK) { SetStatusBar("Sending Email", StatusBarState.InProgress); ExcelWriter excelWriter = new ExcelWriter(null, "Invoice: " + title, Configuration.SenderEmailAddress, Configuration.RecipientEmailAddress); excelWriter.AddItems(this._view.ItemsListEntries.ToList()); SecureString password = emailWindowPresenter.View.Password; string from = emailWindowPresenter.View.From; string to = emailWindowPresenter.View.To; string cc = emailWindowPresenter.View.Cc; string bcc = emailWindowPresenter.View.Bcc; string subject = emailWindowPresenter.View.Subject; string body = emailWindowPresenter.View.Body; // do it on a background thread to avoid blocking the UI BackgroundWorker sendEmailWorker = new BackgroundWorker(); sendEmailWorker.DoWork += BeginSendEmail; sendEmailWorker.RunWorkerCompleted += EndSendEmail; // new invoice will be saved to records upon successful sending of email sendEmailWorker.RunWorkerAsync(new object[] { title, excelWriter.CloseAndGetMemoryStream(), password, from, to, cc, bcc, subject, body }); } else { if (this._view.CreatingNewInvoice) { EnableControlsAfterOperation(); } else { this._view.SaveAndEmailButtonEnabled = true; this._view.SaveAndExportXLButtonEnabled = true; this._view.CancelButtonEnabled = true; SetStatusBar("Ready", StatusBarState.Ready); } } // dispose send email dialog emailWindowPresenter.DisposeDialog(); }