Files
OutlookCaseHelper/OutlookCaseHelper/Form1.cs
T

216 lines
8.0 KiB
C#

using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
namespace OutlookCaseHelper
{
public partial class Form1 : Form
{
private NotifyIcon trayIcon = null!;
private ContextMenuStrip trayMenu = null!;
private OutlookHelper outlookHelper;
private System.Windows.Forms.Timer monitorTimer = null!;
public Form1()
{
InitializeComponent();
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
outlookHelper = new OutlookHelper();
InitializeTray();
InitializeTimer();
}
private void InitializeTray()
{
trayMenu = new ContextMenuStrip();
trayMenu.Items.Add("Create Rule (Selected Email)", null, ProcessEmail_Click);
trayMenu.Items.Add("Remove Rule (Selected Email)", null, RemoveRuleFromSelected_Click);
trayMenu.Items.Add("Remove Rule (Manual ID)", null, RemoveRule_Click);
trayMenu.Items.Add("-");
trayMenu.Items.Add("Exit", null, Exit_Click);
trayIcon = new NotifyIcon();
trayIcon.Icon = new Icon(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "casenew.ico"));
trayIcon.ContextMenuStrip = trayMenu;
trayIcon.Text = "Outlook Case Manager";
trayIcon.Visible = true;
trayIcon.MouseDoubleClick += TrayIcon_MouseDoubleClick;
}
private void InitializeTimer()
{
monitorTimer = new System.Windows.Forms.Timer();
monitorTimer.Interval = 60000; // verifica a cada 60 segundos
monitorTimer.Tick += MonitorTimer_Tick;
monitorTimer.Start();
}
private void MonitorTimer_Tick(object? sender, EventArgs e)
{
try
{
outlookHelper.ProcessActiveRules();
}
catch { }
}
private void ProcessEmail_Click(object? sender, EventArgs e)
{
try
{
var trackingId = outlookHelper.GetSelectedEmailTrackingId();
if (string.IsNullOrEmpty(trackingId))
{
MessageBox.Show(
"No email selected or ID not found in subject.\n\nExpected format: Title - TrackingID#1111111111111111",
"Warning",
MessageBoxButtons.OK,
MessageBoxIcon.Warning);
return;
}
bool success = outlookHelper.CreateFolderAndMoveEmails(trackingId);
if (success)
{
var activeCount = outlookHelper.GetActiveRules().Count;
MessageBox.Show(
$"Rule created! Emails moved and monitoring started.\n\nTrackingID: {trackingId}\nActive rules: {activeCount}",
"Success",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
else
{
MessageBox.Show(
"Error processing email. Make sure Outlook is open.",
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
MessageBox.Show($"Error: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void RemoveRule_Click(object? sender, EventArgs e)
{
try
{
var form = new InputForm("Enter TrackingID to remove:", "Remove Rule");
if (form.ShowDialog() == DialogResult.OK && !string.IsNullOrEmpty(form.TrackingId))
{
bool success = outlookHelper.RemoveRuleAndMoveToClosed(form.TrackingId);
if (success)
{
MessageBox.Show(
"Rule removed!\n\nFolder moved to: Inbox > Cases > Closed\nMonitoring stopped.",
"Success",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
else
{
MessageBox.Show(
"Error removing rule. Check if folder exists.",
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
catch (Exception ex)
{
MessageBox.Show($"Error: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void RemoveRuleFromSelected_Click(object? sender, EventArgs e)
{
try
{
var trackingId = outlookHelper.GetSelectedEmailTrackingId();
if (string.IsNullOrEmpty(trackingId))
{
MessageBox.Show(
"No email selected or ID not found in subject.\n\nExpected format: Title - TrackingID#1111111111111111",
"Warning",
MessageBoxButtons.OK,
MessageBoxIcon.Warning);
return;
}
bool success = outlookHelper.RemoveRuleAndMoveToClosed(trackingId);
if (success)
{
MessageBox.Show(
$"Rule removed!\n\nFolder moved to: Inbox > Cases > Closed\nTrackingID: {trackingId}\nMonitoring stopped.",
"Success",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
else
{
MessageBox.Show(
"Error removing rule. Check if folder exists and Outlook is open.",
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
MessageBox.Show($"Error: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void TrayIcon_MouseDoubleClick(object? sender, MouseEventArgs e)
{
ProcessEmail_Click(null, EventArgs.Empty);
}
private void Exit_Click(object? sender, EventArgs e)
{
monitorTimer.Stop();
trayIcon.Visible = false;
Application.Exit();
}
private class InputForm : Form
{
private TextBox txtInput;
public string TrackingId { get; private set; } = "";
public InputForm(string prompt, string title)
{
this.Text = title;
this.Width = 400;
this.Height = 150;
this.StartPosition = FormStartPosition.CenterScreen;
var label = new Label { Text = prompt, Left = 20, Top = 20, Width = 360, Height = 30 };
txtInput = new TextBox { Left = 20, Top = 60, Width = 340, Height = 30 };
var btnOk = new Button { Text = "OK", Left = 200, Top = 100, Width = 80, DialogResult = DialogResult.OK };
var btnCancel = new Button { Text = "Cancel", Left = 290, Top = 100, Width = 80, DialogResult = DialogResult.Cancel };
this.Controls.Add(label);
this.Controls.Add(txtInput);
this.Controls.Add(btnOk);
this.Controls.Add(btnCancel);
this.AcceptButton = btnOk;
this.CancelButton = btnCancel;
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
TrackingId = txtInput.Text;
base.OnFormClosing(e);
}
}
}
}