Files
limanapp/EgwControlCenter/ControlCenter.cs
T
2024-09-18 18:51:41 +02:00

241 lines
7.4 KiB
C#

using System.Windows.Forms;
using static EgwControlCenter.AccEnum;
using Microsoft.VisualBasic;
using EgwControlCenter.Core;
using System.Diagnostics;
using Newtonsoft.Json;
namespace EgwControlCenter
{
public partial class ControlCenter : Form
{
#region Public Constructors
public ControlCenter()
{
InitializeComponent();
SetupTrayIcon();
CreateTrayMenu();
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("", "");
#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 async void btnUpdate_Click(object sender, EventArgs e)
{
Stopwatch sw = new Stopwatch();
sw.Start();
// effettua un refresh del controllo status...
bool localOk = CurrCheck.UpdateLocalStatus(true);
await CurrCheck.CheckCurrReleases();
sw.Stop();
string rawConf = JsonConvert.SerializeObject(CurrCheck, Formatting.Indented);
lblOut.Text = rawConf;
lblOut.Text += $"{Environment.NewLine}---------------------{Environment.NewLine}";
lblOut.Text += $"Eseguito refresh in {sw.ElapsedMilliseconds:N0} ms";
}
/// <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;
}
#if false
// fix child!
this.LayoutMdi(MdiLayout.TileHorizontal);
#endif
}
/// <summary>
/// evento chiusura
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ControlCenter_FormClosing(object sender, FormClosingEventArgs e)
{
#if false
closeAllChild();
#endif
}
/// <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();
}
}
#if false
displayTaskAndLog("Main Form SHOWN (MDI)");
#endif
}
/// <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");
}
private void InitObjects()
{
// init obj gestione check...
CurrCheck = new ReleaseChecker(AccUtils.confDir, "ConfPatrol.json");
}
/// <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);
}
/// <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
}
}