using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using BulkArchiveMailer.AddIn.UI;
using BulkArchiveMailer.Core;
using Office = Microsoft.Office.Core;
namespace BulkArchiveMailer.AddIn
{
///
/// Backs the single "Массовая рассылка" ribbon button declared in
/// Ribbon.xml (raw Ribbon XML + callbacks approach, not the Ribbon
/// Designer visual item - keeps everything in one small readable
/// file). Wired up via ThisAddIn.CreateRibbonExtensibilityObject().
///
[System.Runtime.InteropServices.ComVisible(true)]
public class RibbonController : Office.IRibbonExtensibility
{
private Office.IRibbonUI ribbon;
public string GetCustomUI(string ribbonId)
{
return GetEmbeddedResourceText("BulkArchiveMailer.AddIn.Ribbon.xml");
}
public void Ribbon_Load(Office.IRibbonUI ribbonUi)
{
ribbon = ribbonUi;
}
///
/// The button's onAction callback. Opens SettingsForm first if the
/// archive folder is not configured yet (or does not exist);
/// otherwise jumps straight to PreviewForm. Kept deliberately
/// simple - see README.md for the reasoning.
///
public void OnOpenBulkSenderButtonClick(Office.IRibbonControl control)
{
try
{
var settings = AppSettings.Load();
var outlookApplication = Globals.ThisAddIn.OutlookApplication;
if (string.IsNullOrWhiteSpace(settings.ArchiveFolder) || !Directory.Exists(settings.ArchiveFolder))
{
using (var settingsForm = new SettingsForm(outlookApplication))
{
settingsForm.ShowDialog();
}
return;
}
using (var previewForm = new PreviewForm(outlookApplication))
{
previewForm.ShowDialog();
}
}
catch (Exception ex)
{
MessageBox.Show(
"Не удалось открыть окно массовой рассылки: " + ex.Message,
"BulkArchiveMailer",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private static string GetEmbeddedResourceText(string resourceName)
{
var asm = Assembly.GetExecutingAssembly();
foreach (var name in asm.GetManifestResourceNames())
{
if (string.Equals(name, resourceName, StringComparison.OrdinalIgnoreCase))
{
using (var stream = asm.GetManifestResourceStream(name))
{
if (stream == null)
{
return null;
}
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
}
return null;
}
}
}