Files
2024-10-02 16:14:15 +02:00

288 lines
9.2 KiB
C#

using System.Windows.Forms;
using static EgwControlCenter.AccEnum;
using Microsoft.VisualBasic;
using EgwControlCenter.Core;
using System.Diagnostics;
using Newtonsoft.Json;
using System.Text;
using System.ComponentModel;
using EgwControlCenter.Core.Models;
using Newtonsoft.Json.Linq;
using Microsoft.VisualBasic.Logging;
using NLog;
namespace EgwControlCenter
{
public partial class ControlCenter : Form
{
#region Public Constructors
public ControlCenter()
{
InitializeComponent();
InitObjects();
ForceReload();
}
private void ForceReload()
{
Stopwatch sw = new Stopwatch();
sw.Start();
// rileggo conf x modifiche
CurrCheck = new ReleaseChecker(ConfDir, DataDir, AccUtils.ConfName);
// effettua un refresh del controllo status...
locCheckOk = CurrCheck.UpdateLocalStatus(true);
if (locCheckOk)
{
remCheckOk = CurrCheck.CheckRemoteReleases();
}
sw.Stop();
DisplayCheckResult(sw.Elapsed);
}
#endregion Public Constructors
#region Protected Fields
#endregion Protected Fields
#region Private Fields
/// <summary>
/// Classe logger
/// </summary>
private static Logger Log = LogManager.GetCurrentClassLogger();
private string ConfDir = "";
private string DataDir = "";
#endregion Private Fields
#region Private Properties
private System.Reflection.AssemblyName CurrAssembly { get; set; } = new System.Reflection.AssemblyName();
private ReleaseChecker CurrCheck { get; set; } = new ReleaseChecker("", "", "");
private bool locCheckOk { get; set; } = false;
private bool remCheckOk { get; set; } = false;
#endregion Private Properties
#region Private Methods
private void btnSetup_Click(object sender, EventArgs e)
{
CheckPwd tgtForm = new CheckPwd();
tgtForm.Show();
}
private void btnUpdate_Click(object sender, EventArgs e)
{
ForceReload();
}
/// <summary>
/// Verifica stato windows (minimized/normal) e visibilità con tray...
/// </summary>
private void CheckFormVisibility()
{
// controllo cosa devo mostrare...
if (WindowState == FormWindowState.Minimized)
{
notifyIcon1.Visible = false;
sendToTray();
}
else
{
notifyIcon1.Visible = false;
}
}
/// <summary>
/// evento chiusura
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ControlCenter_FormClosing(object sender, FormClosingEventArgs e)
{
timerCheck.Stop();
timerCheck.Dispose();
}
private void ControlCenter_Load(object sender, EventArgs e)
{
SetPosition();
}
private void SetPosition()
{
// posiziono in basso a sx...
Rectangle workArea = Screen.GetWorkingArea(this);
this.Location = new Point(workArea.Right - Size.Width, workArea.Bottom - Size.Height);
}
/// <summary>
/// evento resize
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ControlCenter_Resize(object sender, EventArgs e)
{
CheckFormVisibility();
}
/// <summary>
/// evento visualizzazione
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ControlCenter_Shown(object sender, EventArgs e)
{
// avvio minimizzato
sendToTray();
}
/// <summary>
/// crea menù tray x applicazione
/// </summary>
private void CreateTrayMenu()
{
// Fix testi menù tray...
trayMenu.Items.Clear();
// SE permessa massimizzazione...
trayMenu.Items.Add("Show EgalWare ACC");
// se è permesso chiudere
trayMenu.Items.Add("Close EgalWare ACC");
}
/// <summary>
/// Mostra i risultati dell'esito del controllo release
/// </summary>
/// <param name="elapsed"></param>
private void DisplayCheckResult(TimeSpan elapsed)
{
var list = new BindingList<VersStatusDTO>(CurrCheck.ListAppStatus);
dgView.DataSource = list;
dgView.Columns[0].HeaderCell.Value = "Applicazione";
dgView.Columns[1].HeaderCell.Value = "Installed";
dgView.Columns[2].HeaderCell.Value = "Available";
dgView.Columns[3].HeaderCell.Value = "Note";
tsLabelOut.Text = $"Last Check: {CurrCheck.LastChecked():yyyy-MM-dd HH:mm:ss}";
// notifica ballontip se ho aggiornamenti...
if (CurrCheck.HasUpdate() && WindowState != FormWindowState.Normal)
{
notifyIcon1.BalloonTipTitle = "EgalWare's App Control Center";
notifyIcon1.BalloonTipText = "Update found!";
notifyIcon1.BalloonTipIcon = ToolTipIcon.Warning;
notifyIcon1.BalloonTipClicked += NotifyIcon1_BalloonTipClicked;
notifyIcon1.ShowBalloonTip(1000);
}
}
private void InitObjects()
{
CurrAssembly = System.Reflection.Assembly.GetExecutingAssembly().GetName();
Log.Trace($"EgalWare's AppControlCenter Init, v.{CurrAssembly.Version}");
// in primis setup configurazione...
ConfDir = AccUtils.ConfDir;
DataDir = Environment.GetEnvironmentVariable("ClickOnce_DataDirectory") ?? AccUtils.dataDir;
CurrCheck = new ReleaseChecker(ConfDir, DataDir, "ConfPatrol.json");
Log.Trace($"Folder Setup | {ConfDir} | {DataDir}");
timerCheck.Interval = (CurrCheck.CurrPatrolCont.RefreshIntSec * 1000);
// sistemo grafica TRAY ICON
SetupTrayIcon();
CreateTrayMenu();
// sistemo timer
timerCheck.Start();
// notifica update...
tsLabelVers.Text = $"EACC v.{CurrAssembly.Version}";
tsLabelOut.Text = $"Last Check: {CurrCheck.LastChecked():yyyy-MM-dd HH:mm:ss}";
Log.Trace($"Started timer | {timerCheck.Interval} ms");
}
private void NotifyIcon1_BalloonTipClicked(object? sender, EventArgs e)
{
Show();
WindowState = FormWindowState.Normal;
}
/// <summary>
/// doppio click su tray icon
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
Show();
WindowState = FormWindowState.Normal;
}
private string AppName = "EgalWare ACC"; //= AccUtils.CRS("appName");
/// <summary>
/// Gestisce "andata nel tray" della form
/// </summary>
private void sendToTray()
{
if (!notifyIcon1.Visible)
{
notifyIcon1.BalloonTipTitle = AppName;
notifyIcon1.BalloonTipText = $"{AppName} running on tray";
notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
notifyIcon1.BalloonTipClicked += NotifyIcon1_BalloonTipClicked;
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(100);
}
WindowState = FormWindowState.Minimized;
Hide();
}
private void SetupTrayIcon()
{
// fix icon!
notifyIcon1.Text = $"EgalWare's AppControlCenter | {CurrAssembly.Version}";
}
private void timerCheck_Tick(object sender, EventArgs e)
{
Stopwatch sw = new Stopwatch();
sw.Start();
// esegue controllo locale ed eventualmente remoto se scaduto...
locCheckOk = CurrCheck.UpdateLocalStatus(false);
if (locCheckOk)
{
remCheckOk = CurrCheck.CheckRemoteReleases();
}
sw.Stop();
if (locCheckOk)
{
DisplayCheckResult(sw.Elapsed);
}
}
/// <summary>
/// click su menù contestuale in tray
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void trayMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (e != null && e.ClickedItem != null && !string.IsNullOrEmpty(e.ClickedItem.Text))
{
if (e.ClickedItem.Text.StartsWith("Close"))
{
// chiudo!
Close();
}
else if (e.ClickedItem.Text.StartsWith("Show"))
{
Show();
WindowState = FormWindowState.Normal;
}
}
}
#endregion Private Methods
}
}