Files
limanapp/EgwControlCenter/ControlCenter.cs
T
Samuele Locatelli 46345e3393 Update AppControlCenter
- aggiunto tooltip su update trovato
- posizionato form in basso a sx
2024-09-19 08:10:46 +02:00

298 lines
9.5 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;
namespace EgwControlCenter
{
public partial class ControlCenter : Form
{
#region Public Constructors
public ControlCenter()
{
InitializeComponent();
InitObjects();
}
#endregion Public Constructors
#region Protected Fields
/// <summary>
/// Modalità di avvio
/// </summary>
protected AccEnum.StartMode ModoAvvio = AccEnum.StartMode.STD;
#endregion Protected Fields
#region Private Properties
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)
{
string authPwd = Interaction.InputBox("Prego inserire pwd per abilitare lo sblocco", "Verifica Utente", "", 10, 10);
//verifico passphrase
if (authPwd == "24068Seriate")
{
TargetSetup tgtForm = new TargetSetup();
tgtForm.Show();
}
else
{
string message = "La passphrase inserita non è corretta. Impossibile procedere";
string caption = "Auth Error";
MessageBoxButtons buttons = MessageBoxButtons.OK;
DialogResult result;
result = MessageBox.Show(message, caption, buttons);
}
}
private void btnUpdate_Click(object sender, EventArgs e)
{
Stopwatch sw = new Stopwatch();
sw.Start();
// effettua un refresh del controllo status...
locCheckOk = CurrCheck.UpdateLocalStatus(true);
if (locCheckOk)
{
remCheckOk = CurrCheck.CheckCurrReleases();
}
sw.Stop();
DisplayCheckResult(sw.Elapsed);
}
/// <summary>
/// Verifica stato windows (minimized/normal) e visibilità con tray...
/// </summary>
private void CheckFormVisibility()
{
// se non può massimizzare imposto COMUNQUE a minimized...
if (!AccUtils.CRB("windowCanMax"))
{
WindowState = FormWindowState.Minimized;
}
// SOLO SE se sono in modo STD
if (ModoAvvio == AccEnum.StartMode.STD)
{
// controllo cosa devo mostrare...
if (WindowState == FormWindowState.Minimized)
{
notifyIcon1.Visible = false;
sendToTray();
}
else
{
notifyIcon1.Visible = false;
}
}
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)
{
// 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)
{
// SOLO SE se sono in modo STD
if (ModoAvvio == StartMode.STD)
{
// avvio minimizzato se richiesto
if (AccUtils.CRB("startMinimized"))
{
// controllo e mando a tray...
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)
{
#if false
StringBuilder sb = new StringBuilder();
sb.AppendLine("---------------------");
sb.AppendLine($"{DateTime.Now:HH:mm:ss}");
sb.AppendLine("---------------------");
sb.AppendLine();
if (locCheckOk && remCheckOk)
{
string rawConf = JsonConvert.SerializeObject(CurrCheck, Formatting.Indented);
sb.Append(rawConf);
}
sb.AppendLine();
sb.AppendLine("---------------------");
sb.AppendLine($"Eseguito refresh | Loc: {locCheckOk} | Rem: {remCheckOk} | {elapsed.TotalMilliseconds:N0} ms");
lblOut.Text = sb.ToString();
#endif
tsLabelOut.Text = $"Last Check: {CurrCheck.LastChecked():yyyy-MM-dd HH:mm:ss}";
// notifica ballontip se ho aggiornamenti...
if(CurrCheck.HasUpdate())
{
notifyIcon1.BalloonTipTitle = "EgalWare's App Control Center";
notifyIcon1.BalloonTipText = "Update found!";
notifyIcon1.BalloonTipIcon = ToolTipIcon.Warning;
notifyIcon1.ShowBalloonTip(1000);
}
}
private void InitObjects()
{
SetupTrayIcon();
CreateTrayMenu();
// init obj gestione check...
CurrCheck = new ReleaseChecker(AccUtils.confDir, "ConfPatrol.json");
tsLabelOut.Text = $"Last Check: {CurrCheck.LastChecked():yyyy-MM-dd HH:mm:ss}";
timerCheck.Start();
}
/// <summary>
/// doppio click su tray icon
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
// SOLO SE PERMESSO mostrare full...
if (AccUtils.CRB("windowCanMax"))
{
Show();
WindowState = FormWindowState.Normal;
}
}
/// <summary>
/// Gestisce "andata nel tray" della form
/// </summary>
private void sendToTray()
{
if (!notifyIcon1.Visible)
{
notifyIcon1.BalloonTipTitle = AccUtils.CRS("appName");
notifyIcon1.BalloonTipText = $"{AccUtils.CRS("appName")} running on tray";
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(100);
}
Hide();
}
private void SetupTrayIcon()
{
// fix icon!
var currObj = System.Reflection.Assembly.GetExecutingAssembly().GetName();
notifyIcon1.Text = $"EgalWare's AppControlCenter | {currObj.Version}";
//Icon = Icon.ExtractAssociatedIcon(AccUtils.defIconFilePath);
//notifyIcon1.Icon = Icon.ExtractAssociatedIcon(AccUtils.defIconFilePath);
}
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.CheckCurrReleases();
}
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"))
{
#if false
// stop child adapters...
closeAllChild();
#endif
// chiudo!
Close();
}
else if (e.ClickedItem.Text.StartsWith("Show"))
{
if (AccUtils.CRB("windowCanMax"))
{
Show();
WindowState = FormWindowState.Normal;
}
}
}
}
#endregion Private Methods
}
}