Files
CMS-MTConn/MTC_Adapter/MTC-ADB/MainForm.cs
T

282 lines
8.3 KiB
C#

/*
* Copyright Copyright 2017, Steamware s.r.l. & CMS/SCM s.p.a.
*
* Based on data, code and example by MTC consortium & System Insights, Inc.
*
* */
using System.Windows.Forms;
using MTC;
using NLog;
using NLog.Config;
using NLog.Targets;
using System;
using System.Drawing;
using System.Threading;
using System.Configuration;
namespace MTC_ADB
{
public partial class MainForm : Form
{
#region variabili ed oggetti globali
/// <summary>
/// timer base in avvio
/// </summary>
protected int startTimerMs = 250;
/// <summary>
/// contatore normale
/// </summary>
protected int normCount = 5000; // di norma 5 sec x refresh...
/// <summary>
/// ultimo tentativo riavvio...
/// </summary>
protected DateTime lastStartTry;
/// <summary>
/// oggetto logging
/// </summary>
public static Logger lg;
#endregion
public MainForm()
{
InitializeComponent();
myInit();
}
private void myInit()
{
lblStatus.Text = "Loading";
lastStartTry = DateTime.Now;
// fix icon!
Icon = Icon.ExtractAssociatedIcon(defIconFilePath);
notifyIcon1.Icon = Icon.ExtractAssociatedIcon(defIconFilePath);
// fix versione!
lblApp.Text = string.Format("{0}", ConfigurationManager.AppSettings.Get("appName"));
lblVers.Text = string.Format(" v.{0}", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version);
startTimerMs = utils.CRI("startTimerMs");
#if DEBUG
// Setup the logging view for Sentinel - http://sentinel.codeplex.com
var sentinalTarget = new NLogViewerTarget()
{
Name = "sentinal",
Address = "udp://127.0.0.1:9999",
IncludeNLogData = false
};
var sentinalRule = new LoggingRule("*", LogLevel.Trace, sentinalTarget);
LogManager.Configuration.AddTarget("sentinal", sentinalTarget);
LogManager.Configuration.LoggingRules.Add(sentinalRule);
#endif
LogManager.ReconfigExistingLoggers();
lg = LogManager.GetCurrentClassLogger();
displayTaskAndWait("Starting MainForm");
// se abilitato autoload conf leggo file corretto...
if (utils.CRB("autoLoadConf"))
{
//loadXmlFile(defConfFilePath);
lg.Info("XML LOADED");
//loadPersistLayer(defPersLayerFile);
lg.Info("PersLayerFile READ");
//agObj.loadPersData();
lg.Info("PersLayerFile LOADED");
}
else
{
//// definisco e avvio tipo adapter generico
//tipoScelto = tipoAdapter.ND;
//adpConf = new AdapterConf();
//loadAdapterType();
displayTaskAndWait("Waiting for config file selection");
}
// Start timer periodico
gather.Interval = utils.CRI("timerIntMs");
gather.Enabled = true;
displayTaskAndWait("Running");
createTrayMenu();
displayTaskAndWait("Tray Menu OK");
// avvio minimizzato se richiesto
if (utils.CRB("startMinimized"))
{
// imposto minimized se necessario!
if (WindowState != FormWindowState.Minimized)
{
WindowState = FormWindowState.Minimized;
}
displayTaskAndWait("Minimized");
}
displayTaskAndWait("Main Form OK");
}
/// <summary>
/// Verifica finale a fine show...
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MainForm_Shown(object sender, EventArgs e)
{
// avvio minimizzato se richiesto
if (utils.CRB("startMinimized"))
{
// controllo e mando a tray...
sendToTray();
}
displayTaskAndWait("Main Form SHOWN");
}
/// <summary>
/// mostra un testo sulla status bar ed attende startTimerMs
/// </summary>
/// <param name="txt2show"></param>
public void displayTaskAndWait(string txt2show)
{
lblStatus.Text = txt2show;
lg.Info(txt2show);
Thread.Sleep(startTimerMs);
}
/// <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 MTC-ADB");
}
// se è permesso tray close...
if (utils.CRB("trayClose"))
{
trayMenu.Items.Add("Close MTC-ADB");
}
}
/// <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>
/// evento resize
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MainForm_Resize(object sender, EventArgs e)
{
checkFormVisibility();
}
/// <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)
{
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"))
{
#if false
// stop adapter...
closeAdapter();
#endif
// chiudo!
Close();
}
else if (e.ClickedItem.Text.StartsWith("Show"))
{
if (utils.CRB("windowCanMax"))
{
Show();
WindowState = FormWindowState.Normal;
}
}
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
closeAdapter();
}
private void closeAdapter()
{
#if false
fermaTutto(true, false);
#endif
}
/// <summary>
/// File persistenza generale
/// </summary>
protected string defPersLayerFile
{
get
{
return string.Format(@"{0}\{1}", utils.dataDatDir, utils.CRS("defaultPersLayerFile"));
}
}
protected string defIconFilePath
{
get
{
return string.Format(@"{0}\MTCADB.ico", utils.resxDir);
}
}
}
}