Skip to content
Snippets Groups Projects
AutoTxTray.cs 11.07 KiB
using System;
using System.Diagnostics;
using System.Timers;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using ATXCommon.Serializables;
using Microsoft.WindowsAPICodePack.Dialogs;
using Timer = System.Timers.Timer;

namespace ATXTray
{
    public class AutoTxTray : ApplicationContext
    {
        private const string AppTitle = "AutoTx Service Monitor";
        private static readonly Timer AppTimer = new Timer(1000);
        private static readonly string BaseDir = AppDomain.CurrentDomain.BaseDirectory;
        private static readonly string ConfigFile = Path.Combine(BaseDir, "configuration.xml");
        private static readonly string StatusFile = Path.Combine(BaseDir, "status.xml");
        private static DateTime _statusAge;
        private static ServiceConfig _config;
        private static ServiceStatus _status;

        private static bool _statusChanged = false;
        private static bool _svcRunning = false;
        private static bool _svcSuspended = true;
        private static string _svcSuspendReason;

        private static bool _txInProgress = false;
        private static long _txSize;
        
        private readonly NotifyIcon _notifyIcon = new NotifyIcon();
        private readonly Icon _tiDefault = new Icon("AutoTx.ico");
        private readonly Icon _tiStopped = new Icon("icon-stopped.ico");
        private readonly Icon _tiSuspended = new Icon("icon-suspended.ico");
        private readonly Icon _tiTx0 = new Icon("icon-tx-0.ico");
        private readonly Icon _tiTx1 = new Icon("icon-tx-1.ico");
        private readonly ContextMenuStrip _cmStrip = new ContextMenuStrip();
        private readonly ToolStripMenuItem _miExit = new ToolStripMenuItem();
        private readonly ToolStripMenuItem _miTitle = new ToolStripMenuItem();
        private readonly ToolStripMenuItem _miSvcRunning = new ToolStripMenuItem();
        private readonly ToolStripMenuItem _miSvcSuspended = new ToolStripMenuItem();
        private readonly ToolStripMenuItem _miTxProgress = new ToolStripMenuItem();
        private readonly ToolStripMenuItem _miTxEnqueue = new ToolStripMenuItem();

        public AutoTxTray() {

            _notifyIcon.Icon = _tiStopped;
            _notifyIcon.Visible = true;
            _notifyIcon.DoubleClick += StartNewTransfer;

            // this doesn't work properly, the menu will not close etc. so we disable it for now:
            // _notifyIcon.Click += ShowContextMenu;

            try {
                _config = ServiceConfig.Deserialize(ConfigFile);
                ReadStatus();
            }
            catch (Exception ex) {
                _notifyIcon.ShowBalloonTip(10000, AppTitle,
                    "Unable to read config / status: " + ex.Message, ToolTipIcon.Error);
                System.Threading.Thread.Sleep(10000);
                _notifyIcon.Visible = false;
                Application.Exit();
            }

            _miExit.Text = @"Exit";
            _miExit.Click += MiExitClick;

            _miTitle.Font = new Font(_cmStrip.Font, FontStyle.Bold);