Files
Mapo-IOB-WIN/IOB-WIN/MainForm.cs
T
Samuele E. Locatelli bdec9c3110 Minor graphic fix
2017-12-20 12:56:44 +01:00

360 lines
9.0 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AutoUpdaterDotNET;
using IOB_UT;
using NLog;
using NLog.Targets;
using NLog.Config;
using System.Configuration;
namespace IOB_WIN
{
public partial class MainForm : Form
{
#region utils ed helpers
/// <summary>
/// oggetto logging
/// </summary>
public static Logger lg;
/// <summary>
/// Data Avvio form
/// </summary>
DateTime formStartTime;
/// <summary>
/// mostra un testo sulla status bar + LOG
/// </summary>
/// <param name="txt2show"></param>
public void displayTaskAndLog(string txt2show)
{
lblStatus.Text = txt2show;
lblStatus.Invalidate();
lg.Info(txt2show);
}
#endregion
/// <summary>
/// URL stringa di UPDATE...
/// </summary>
protected string updateUrl
{
get
{
//string branchName = utils.CRS("appVers");
return string.Format("http://seriate.steamware.net:8083/SWS/MAPO/IOB-WIN/{0}/manifest.xml", branchName);
}
}
/// <summary>
/// Ramo applicazione (x update)
/// </summary>
protected string branchName = "develop";
public MainForm()
{
InitializeComponent();
myInit();
}
protected void myInit()
{
formStartTime = DateTime.Now;
lblStatus.Text = "Loading";
// fix icon!
notifyIcon1.Text = string.Format("IOB-WIN | {0}", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version);
Icon = Icon.ExtractAssociatedIcon(utils.defIconFilePath);
notifyIcon1.Icon = Icon.ExtractAssociatedIcon(utils.defIconFilePath);
// fix versione!
lblApp.Text = string.Format("{0}", ConfigurationManager.AppSettings.Get("appName"));
lblVers.Text = string.Format(" v.{0}", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version);
#if DEBUG
// Setup the logging view for Sentinel - http://sentinel.codeplex.com
var sentinelTarget = new NLogViewerTarget()
{
Name = "sentinel",
Address = "udp://127.0.0.1:9999",
IncludeNLogData = false
};
var sentinelRule = new LoggingRule("*", LogLevel.Trace, sentinelTarget);
LogManager.Configuration.AddTarget("sentinel", sentinelTarget);
LogManager.Configuration.LoggingRules.Add(sentinelRule);
#endif
LogManager.ReconfigExistingLoggers();
lg = LogManager.GetCurrentClassLogger();
displayTaskAndLog("MainForm Starting");
// se abilitato autoload conf leggo file corretto...
if (utils.CRB("autoLoadConf"))
{
loadIniFile(utils.defConfFilePath);
lg.Info("INI LOADED");
}
else
{
displayTaskAndLog("Waiting for config file selection");
}
displayTaskAndLog("Program Running");
createTrayMenu();
displayTaskAndLog("Tray Menu OK");
// avvio minimizzato se richiesto
if (utils.CRB("startMinimized"))
{
// imposto minimized se necessario!
if (WindowState != FormWindowState.Minimized)
{
WindowState = FormWindowState.Minimized;
}
displayTaskAndLog("Minimized");
}
#if false
try
{
// segnalo reboot (programma)...
iobObj.callUrl(iobObj.urlReboot);
}
catch (Exception exc)
{
lg.Error(string.Format("EXCEPTION in fase di chaimata URL di reboot:{0}{1}", Environment.NewLine, exc));
}
#endif
displayTaskAndLog("Main Form OK");
}
/// <summary>
/// Carica file ini della configurazione richiesta
/// </summary>
/// <param name="iniConfFile"></param>
private void loadIniFile(string iniConfFile)
{
displayTaskAndLog(string.Format("Loading iniConfFile: {0}", iniConfFile));
IniFile fIni = new IniFile(iniConfFile);
// salvo branchName...
branchName = fIni.ReadString("BRANCH", "NAME", "develop");
// se NON sono in DEBUG faccio check update...
#if !(DEBUG)
// avvio autoupdater...
AutoUpdater.Start(updateUrl);
#endif
}
private void mCheckUpdates_Click(object sender, EventArgs e)
{
AutoUpdater.ShowSkipButton = false;
AutoUpdater.ShowRemindLaterButton = false;
AutoUpdater.Start(updateUrl);
}
protected void closeActiveChild()
{
if (this.HasChildren)
{
try
{
this.ActiveMdiChild.Close();
this.LayoutMdi(MdiLayout.TileHorizontal);
}
catch
{ }
}
}
protected void openChild()
{
TestForm child = new TestForm();
child.MdiParent = this;
child.Show();
this.LayoutMdi(MdiLayout.TileHorizontal);
}
private void closeChildToolStripMenuItem_Click(object sender, EventArgs e)
{
closeActiveChild();
}
private void openChildToolStripMenuItem_Click(object sender, EventArgs e)
{
openChild();
}
#region gestione FORM principale + tray
/// <summary>
/// timer principale
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MainTimer_Tick(object sender, EventArgs e)
{
// inizio a riportare che sto funzionando..
advProgBar();
}
/// <summary>
/// Avanza la barra di stato...
/// </summary>
public void advProgBar()
{
try
{
MainProgrBar.PerformStep();
// se è arrivato a MAX resetto...
if (MainProgrBar.Value >= MainProgrBar.Maximum)
{
MainProgrBar.Value = 0;
}
MainProgrBar.Invalidate();
// aggiorno runtime...
TimeSpan uptime = DateTime.Now.Subtract(formStartTime);
tslRunTime.Text = string.Format("Running: {0}gg {1:00}:{2:00}:{3:00}", uptime.Days, uptime.Hours, uptime.Minutes, uptime.Seconds);
}
catch
{ }
}
/// <summary>
/// crea menù tray x applicazione
/// </summary>
private void createTrayMenu()
{
// Fix testi menù tray...
trayMenu.Items.Clear();
// SE permessa massimizzazione...
if (utils.CRB("windowCanMax"))
{
trayMenu.Items.Add("Show IOB-WIN");
}
// se è permesso tray close...
if (utils.CRB("trayClose"))
{
trayMenu.Items.Add("Close IOB-WIN");
}
}
/// <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 (utils.CRB("windowCanMax"))
{
Show();
WindowState = FormWindowState.Normal;
}
}
/// <summary>
/// Verifica stato windows (minimized/normal) e visibilità con tray...
/// </summary>
private void checkFormVisibility()
{
// se non può massimizzare imposto COMUNQUE a minimized...
if (!utils.CRB("windowCanMax"))
{
WindowState = FormWindowState.Minimized;
}
// controllo cosa devo mostrare...
if (WindowState == FormWindowState.Minimized)
{
notifyIcon1.Visible = false;
sendToTray();
}
else
{
notifyIcon1.Visible = false;
}
}
/// <summary>
/// Gestisce "andata nel tray" della form
/// </summary>
private void sendToTray()
{
if (!notifyIcon1.Visible)
{
notifyIcon1.BalloonTipTitle = utils.CRS("appName");
notifyIcon1.BalloonTipText = string.Format("{0} running on tray", utils.CRS("appName"));
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(100);
}
Hide();
}
/// <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.ClickedItem.Text.StartsWith("Close"))
{
// stop child adapters...
closeAllChild();
// chiudo!
Close();
}
else if (e.ClickedItem.Text.StartsWith("Show"))
{
if (utils.CRB("windowCanMax"))
{
Show();
WindowState = FormWindowState.Normal;
}
}
}
/// <summary>
/// Chiusura applicazione
/// </summary>
private void closeAllChild()
{
// ferma tutti i child form...
foreach (var ChildForm in this.MdiChildren)
{
ChildForm.Close();
}
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
closeAllChild();
}
private void MainForm_Shown(object sender, EventArgs e)
{
// avvio minimizzato se richiesto
if (utils.CRB("startMinimized"))
{
// controllo e mando a tray...
sendToTray();
}
displayTaskAndLog("Main Form SHOWN");
}
private void MainForm_Resize(object sender, EventArgs e)
{
checkFormVisibility();
}
#endregion
}
}