Download AzureContainerEcho.zip version 1.0.0.0, last updated 08/09/2013 (341.35 KB)

Download
  • md5: 08cb1fda4e8e679d8fec0608417e3910
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Cyotek.AzureContainerEcho.Client.Properties;
using Cyotek.TaskScheduler;

namespace Cyotek.AzureContainerEcho.Client
{
  internal class ApplicationContext : TrayIconApplicationContext
  {
    #region Instance Fields

    private readonly JobManager _jobManager;

    private AboutDialog _aboutDialog;

    private SettingsDialog _settingsDialog;

    #endregion

    #region Constructors

    public ApplicationContext()
    {
      this.ContextMenu.Items.Add("&Settings...", null, this.SettingsContextMenuClickHandler).Font = new Font(this.ContextMenu.Font, FontStyle.Bold);
      this.ContextMenu.Items.Add("-");
      this.ContextMenu.Items.Add("&About...", null, this.AboutContextMenuClickHandler);
      this.ContextMenu.Items.Add("-");
      this.ContextMenu.Items.Add("E&xit", null, this.ExitContextMenuClickHandler);

      _jobManager = new JobManager();
      _jobManager.TaskStarted += this.JobManagerTaskStartedHandler;
      _jobManager.TaskCompleted += this.JobManagerTaskCompletedHandler;
      _jobManager.TaskCancelled += this.JobManagerTaskCancelledHandler;
      _jobManager.TaskException += this.JobManagerTaskExceptionHandler;
      _jobManager.Load();

      this.SetIcon();

      _jobManager.Enabled = true;
    }

    #endregion

    #region Overridden Members

    protected override void OnApplicationExit(EventArgs e)
    {
      if (_jobManager != null)
        _jobManager.Dispose();

      base.OnApplicationExit(e);
    }

    protected override void OnTrayIconDoubleClick(MouseEventArgs e)
    {
      this.ShowSettings();

      base.OnTrayIconDoubleClick(e);
    }

    #endregion

    #region Members

    private void AboutContextMenuClickHandler(object sender, EventArgs eventArgs)
    {
      this.ShowDialog(ref _aboutDialog, null);
    }

    private void ExitContextMenuClickHandler(object sender, EventArgs eventArgs)
    {
      this.ExitThread();
    }

    private void SetIcon()
    {
      Icon icon;
      string suffix;

      if (_jobManager.Any())
      {
        bool areJobsRunning;
        bool didJobsFail;

        areJobsRunning = _jobManager.GetJobs().Any(j => j.InProgress);
        didJobsFail = _jobManager.GetJobs().Any(j => j.Failed);
        icon = areJobsRunning ? Resources.SmallDownloadIcon : didJobsFail ? Resources.SmallErrorIcon : Resources.SmallIcon;
        suffix = areJobsRunning ? " - Downloading changes" : _jobManager.GetJobs().Any(j => j.IsComplete) ? " - Up to date" : string.Empty;
      }
      else
      {
        icon = Resources.SmallQueryIcon;
        suffix = " - No jobs defined";
      }

      this.TrayIcon.Icon = icon;
      this.TrayIcon.Text = string.Concat(Application.ProductName, suffix);
    }

    private void SettingsContextMenuClickHandler(object sender, EventArgs eventArgs)
    {
      this.ShowSettings();
    }

    private void ShowDialog<T>(ref T localReference, Action<T> initialization) where T : Form, new()
    {
      if (localReference == null)
      {
        using (localReference = new T())
        {
          if (initialization != null)
            initialization(localReference);

          localReference.ShowDialog();
        }
        localReference = null;
      }
      else
        localReference.Activate();
    }

    private void ShowSettings()
    {
      this.ShowDialog(ref _settingsDialog, dialog => { dialog.Manager = _jobManager; });
    }

    #endregion

    #region Event Handlers

    private void JobManagerTaskCancelledHandler(object sender, ScheduledTaskEventArgs e)
    {
      this.SetIcon();
    }

    private void JobManagerTaskCompletedHandler(object sender, ScheduledTaskEventArgs e)
    {
      this.SetIcon();
    }

    private void JobManagerTaskExceptionHandler(object sender, ScheduledTaskExceptionEventArgs e)
    {
      this.TrayIcon.ShowBalloonTip(10000, e.Task.Name, e.Exception.GetBaseException().Message, ToolTipIcon.Error);
    }

    private void JobManagerTaskStartedHandler(object sender, ScheduledTaskEventArgs e)
    {
      this.SetIcon();
    }

    #endregion
  }
}

Donate

Donate