510 lines
17 KiB
C#
510 lines
17 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.Drawing.Configuration;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using System.Xml.Linq;
|
|
using Eugen.ESystem.IO;
|
|
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
|
|
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
|
|
using static Eugen.ESystem.IO.LoadXMLFile;
|
|
using AppRes = Eugen.ESystem.Properties.AppResources;
|
|
|
|
namespace Eugen.ESystem
|
|
{
|
|
public class DynamicMenuItems
|
|
{
|
|
#region Version und Copyright
|
|
// Version und Copyright
|
|
static string dllName = Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase); //Den Programmnamen auslesen
|
|
static string dllVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
|
|
static object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
|
|
static string copyright = GenerateCopyright();
|
|
|
|
//Assembly Datum und Zeit
|
|
static DateTime value = AssemblyDateTime();
|
|
static string date = value.ToShortDateString();
|
|
static string time = value.ToLongTimeString();
|
|
|
|
private static DateTime AssemblyDateTime()
|
|
{
|
|
var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
|
|
var buildDateTime = new DateTime(2000, 1, 1).Add(new TimeSpan(TimeSpan.TicksPerDay * version.Build + TimeSpan.TicksPerSecond * 2 * version.Revision));
|
|
//Tage seit dem 1. Januar 2000 und Sekunden seit Mitternacht, (multiplizier mit 2 ergibt das Original)
|
|
return buildDateTime;
|
|
}
|
|
|
|
private static string CurrentYear()
|
|
{
|
|
DateTime dtn = DateTime.Now;
|
|
return dtn.Year.ToString();
|
|
}
|
|
|
|
private static string StartYear()
|
|
{
|
|
string startYear = "";
|
|
if (((AssemblyCopyrightAttribute)attributes[0]).Copyright.Contains("Copyright © "))
|
|
{
|
|
startYear = ((AssemblyCopyrightAttribute)attributes[0]).Copyright.Replace("Copyright © ", "");
|
|
while (startYear.StartsWith(" "))
|
|
{
|
|
startYear = startYear.Remove(0, 1);
|
|
}
|
|
|
|
if (startYear.Contains("-"))
|
|
{
|
|
startYear = startYear.Remove(startYear.IndexOf("-"));
|
|
}
|
|
else
|
|
{
|
|
startYear = startYear.Remove(startYear.IndexOf(" "));
|
|
}
|
|
return startYear;
|
|
}
|
|
else
|
|
{
|
|
DateTime dtn = DateTime.Now;
|
|
return dtn.Year.ToString();
|
|
}
|
|
}
|
|
|
|
private static string GenerateCopyright()
|
|
{
|
|
string startYear = StartYear();
|
|
string currentYear = CurrentYear();
|
|
|
|
if (startYear == currentYear)
|
|
{
|
|
return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
|
|
}
|
|
else
|
|
{
|
|
string temp = ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
|
|
string start = temp.Remove(temp.IndexOf(startYear) - 1);
|
|
string end = (temp.Remove(0, temp.IndexOf(" by")));
|
|
|
|
return String.Concat(start, " ", startYear, "-", currentYear, end);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region DLL-Info
|
|
/// <summary>
|
|
/// Contains the name of the dll file.
|
|
/// </summary>
|
|
public static string DllName { private set; get; }
|
|
|
|
/// <summary>
|
|
/// Contains the version of the dll file.
|
|
/// </summary>
|
|
public static string DllVersion { private set; get; }
|
|
|
|
/// <summary>
|
|
/// Contains the copyright notice.
|
|
/// </summary>
|
|
public static string Copyright { private set; get; }
|
|
|
|
/// <summary>
|
|
/// Contains the name-, version- and copyright-information of the dll file.
|
|
/// </summary>
|
|
public static string[] DllInfo { private set; get; }
|
|
|
|
private static void SetDllInfo()
|
|
{
|
|
//Name, Version und Copyright setzen
|
|
DllName = dllName;
|
|
DllVersion = dllVersion;
|
|
Copyright = copyright;
|
|
|
|
string[] dllInfo = new string[3];
|
|
dllInfo[0] = dllName;
|
|
dllInfo[1] = dllVersion;
|
|
dllInfo[2] = copyright;
|
|
DllInfo = dllInfo;
|
|
}
|
|
|
|
protected string ProgramName { private set; get; }
|
|
protected string ProgramVersion { private set; get; }
|
|
protected string ProgramCopyright { private set; get; }
|
|
#endregion
|
|
|
|
static DynamicMenuItems()
|
|
{
|
|
SetDllInfo();
|
|
}
|
|
|
|
public DynamicMenuItems()
|
|
{
|
|
SetDllInfo();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Log file name
|
|
/// </summary>
|
|
public static string LogFile { get; set; }
|
|
|
|
/// <summary>
|
|
/// Password for encryption
|
|
/// </summary>
|
|
public string Password { get; set; }
|
|
|
|
/// <summary>
|
|
/// File name of the configuration
|
|
/// </summary>
|
|
public string XmlFile { get; set; }
|
|
|
|
/// <summary>
|
|
/// The XML topic
|
|
/// </summary>
|
|
public string Topic { get; set; }
|
|
|
|
/// <summary>
|
|
/// The XML group
|
|
/// </summary>
|
|
public string Group { get; set; }
|
|
|
|
/// <summary>
|
|
/// The XML key
|
|
/// </summary>
|
|
public string Key { get; set; }
|
|
|
|
/// <summary>
|
|
/// The values stored in the XML file
|
|
/// </summary>
|
|
public string[,] Values { get; set; }
|
|
|
|
/// <summary>
|
|
/// List of menu entries to be created
|
|
/// </summary>
|
|
public string[] MenuItems { get; set; }
|
|
|
|
/// <summary>
|
|
/// The selected menu item
|
|
/// </summary>
|
|
public string Standard { get; set; }
|
|
|
|
/// <summary>
|
|
/// Menu into which the submenus are inserted
|
|
/// </summary>
|
|
public ToolStripMenuItem Menu { get; set; }
|
|
|
|
/// <summary>
|
|
/// The backcolor of a menu entry
|
|
/// </summary>
|
|
public Color BackColor { get; set; }
|
|
|
|
/// <summary>
|
|
/// The forcolor of a menu entry
|
|
/// </summary>
|
|
public Color ForeColor { get; set; }
|
|
|
|
/// <summary>
|
|
/// Insert a menu item
|
|
/// </summary>
|
|
/// <param name="menuItem">Name of the Menu item</param>
|
|
public void AddMenuItem(string menuItem)
|
|
{
|
|
LogWriter log = new LogWriter(Path.GetDirectoryName(LogFile), Path.GetFileName(LogFile));
|
|
|
|
ToolStripMenuItem menu = (ToolStripMenuItem)(Menu);
|
|
Menu.DropDownItems.Add(menuItem, null, new EventHandler(ToolStripMenuItem_Click));
|
|
|
|
//Wenn BackColor und ForeColor einen Wert enthalten, dann das Menü einfärben
|
|
if (BackColor != null & ForeColor != null)
|
|
{
|
|
foreach (ToolStripMenuItem item in menu.DropDownItems)
|
|
{
|
|
item.BackColor = BackColor;
|
|
item.ForeColor = ForeColor;
|
|
}
|
|
}
|
|
|
|
if (File.Exists(LogFile))
|
|
{
|
|
if (menu.ToString().Contains("&"))
|
|
{
|
|
log.WriteMessage(menu.ToString().Replace("&", "") + " menu item \'" + menuItem + "\' was generated", LogWriter.LogLevel.Information, LogWriter.Time.Write); //Logdatei schreiben
|
|
}
|
|
else
|
|
{
|
|
log.WriteMessage(menu.ToString() + " menu item \'" + menuItem + "\' was generated", LogWriter.LogLevel.Information, LogWriter.Time.Write); //Logdatei schreiben
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Insert menu entry from a list that is in an XML file
|
|
/// </summary>
|
|
public void AddMenuItemsFromXML()
|
|
{
|
|
LogWriter log = new LogWriter(Path.GetDirectoryName(LogFile), Path.GetFileName(LogFile));
|
|
|
|
if (File.Exists(LogFile))
|
|
{
|
|
log.WriteMessage("Creating dynamic menu items ...", LogWriter.LogLevel.Information, LogWriter.Time.Write); //Logdatei schreiben
|
|
}
|
|
|
|
LoadXmlFile(XmlFile, Topic, Group, Key); //Information aus der XML-Datei auslesen
|
|
ExtractMenuItem(); //Die Menueinträge aus den XML-Daten ermitteln
|
|
ExtractStandardItem(); //Das Standard Item ermitteln
|
|
|
|
AddMenuItems(MenuItems); //Item zum Menü hinzufügen
|
|
CheckMenuItem(Standard); //Standard Menü Item auswählen
|
|
|
|
if (File.Exists(LogFile))
|
|
{
|
|
log.WriteMessage("Dynamic menu items created successfully", LogWriter.LogLevel.Information, LogWriter.Time.Write); //Logdatei schreiben
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Insert menu entry from a list
|
|
/// </summary>
|
|
/// <param name="menuList">List of menu items</param>
|
|
public void AddMenuItems(string[] menuList)
|
|
{
|
|
LogWriter log = new LogWriter(Path.GetDirectoryName(LogFile), Path.GetFileName(LogFile));
|
|
|
|
//Menueinträge erzeugen
|
|
if (File.Exists(LogFile))
|
|
{
|
|
log.WriteMessage("Generating menu items ...", LogWriter.LogLevel.Information, LogWriter.Time.Write); //Logdatei schreiben
|
|
}
|
|
|
|
//Für jedes Element in der Liste einen Menüpunkt anlegen
|
|
foreach (var element in menuList)
|
|
{
|
|
AddMenuItem(element);
|
|
}
|
|
|
|
MenuItems = menuList;
|
|
|
|
if (File.Exists(LogFile))
|
|
{
|
|
log.WriteMessage("All menu items are generated successfully", LogWriter.LogLevel.Information, LogWriter.Time.Write); //Logdatei schreiben
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check a menu item
|
|
/// </summary>
|
|
/// <param name="selectedItem">Item name</param>
|
|
public void CheckMenuItem(string selectedItem)
|
|
{
|
|
LogWriter log = new LogWriter(Path.GetDirectoryName(LogFile), Path.GetFileName(LogFile));
|
|
|
|
//Hier soll der Menüeintrag, der dem Übergebenen Wert entspricht, auf 'Checked = true' gesetzt werden
|
|
foreach (ToolStripMenuItem item in Menu.DropDownItems)
|
|
{
|
|
if (item.Text == selectedItem)
|
|
{
|
|
item.Checked = true;
|
|
if (File.Exists(LogFile))
|
|
{
|
|
log.WriteMessage("The menu item \'" + item.Text + "\' was selected", LogWriter.LogLevel.Information, LogWriter.Time.Write); //Logdatei schreiben
|
|
}
|
|
}
|
|
else
|
|
{
|
|
item.Checked = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes a menu item
|
|
/// </summary>
|
|
/// <param name="item">Item name</param>
|
|
public void RemoveMenuItem(string item)
|
|
{
|
|
LogWriter log = new LogWriter(Path.GetDirectoryName(LogFile), Path.GetFileName(LogFile));
|
|
|
|
ToolStripMenuItem menu = (ToolStripMenuItem)(Menu);
|
|
|
|
foreach (ToolStripMenuItem xitem in menu.DropDownItems)
|
|
{
|
|
if (xitem.ToString() == item)
|
|
{
|
|
menu.DropDownItems.Remove(xitem); //Den Menüeinträge löschen
|
|
|
|
if (File.Exists(LogFile))
|
|
{
|
|
log.WriteMessage("Remove menu item" + item, LogWriter.LogLevel.Information, LogWriter.Time.Write); //Logdatei schreiben
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes the menu items specified in a list
|
|
/// </summary>
|
|
/// <param name="itemList">Item list</param>
|
|
public void RemoveMenuItems(string[] itemList)
|
|
{
|
|
foreach (string item in itemList)
|
|
{
|
|
RemoveMenuItem(item);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes all menu items
|
|
/// </summary>
|
|
public void RemoveAllMenuItems()
|
|
{
|
|
LogWriter log = new LogWriter(Path.GetDirectoryName(LogFile), Path.GetFileName(LogFile));
|
|
|
|
ToolStripMenuItem menu = (ToolStripMenuItem)(Menu);
|
|
|
|
menu.DropDownItems.Clear(); //Alle angelegten Menüeinträge löschen
|
|
|
|
if (File.Exists(LogFile))
|
|
{
|
|
log.WriteMessage("Removed all existing menu items", LogWriter.LogLevel.Information, LogWriter.Time.Write); //Logdatei schreiben
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a list of all submenus
|
|
/// </summary>
|
|
/// <returns>Item list</returns>
|
|
public string[] GetSubMenuItems()
|
|
{
|
|
ToolStripMenuItem menu = (ToolStripMenuItem)(Menu);
|
|
|
|
string[] items = new string[menu.DropDownItems.Count];
|
|
|
|
for (int i = 0; i < menu.DropDownItems.Count; i++)
|
|
{
|
|
items[i] = menu.DropDownItems[i].ToString();
|
|
}
|
|
|
|
return items;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Saves a license value to the roaming area
|
|
/// </summary>
|
|
/// <param name="value">A valid string</param>
|
|
///// <exception cref="UnableToSaveLicenseBundleException">Unable to save license value</exception>
|
|
//public void SaveValueToRoaming(string value, string key)
|
|
//{
|
|
// string password = Environment.GetEnvironmentVariable("USERNAME");
|
|
// var cryption = new Cryption();
|
|
|
|
// try
|
|
// {
|
|
// var roaming = new Roaming();
|
|
// roaming.MainKey = "NX-Portal";
|
|
// roaming.Key = key;
|
|
// roaming.Value = cryption.EncryptString(value, password);
|
|
// roaming.WriteKey();
|
|
// }
|
|
// catch (Exception)
|
|
// {
|
|
// throw new UnableToSaveLicenseBundleException();
|
|
// }
|
|
//}
|
|
|
|
private void ToolStripMenuItem_Click(object sender, EventArgs e)
|
|
{
|
|
LogWriter log = new LogWriter(Path.GetDirectoryName(LogFile), Path.GetFileName(LogFile));
|
|
|
|
ToolStripMenuItem MenuItem = (ToolStripMenuItem)sender;
|
|
|
|
Standard = MenuItem.Text; //Auswahl als Standard setzen
|
|
|
|
CheckMenuItem(MenuItem.Text); //Ausgewähltes Menu selektieren
|
|
|
|
OnDynamicMenuItemEvent(EventArgs.Empty); //Das eigene Event aufrufen
|
|
}
|
|
|
|
private void LoadXmlFile(string xmlFileName, string topic, string group, string key)
|
|
{
|
|
LogWriter log = new LogWriter(Path.GetDirectoryName(LogFile), Path.GetFileName(LogFile));
|
|
|
|
if (File.Exists(LogFile))
|
|
{
|
|
log.WriteMessage("Load menu items from XML-file", LogWriter.LogLevel.Information, LogWriter.Time.Write); //Logdatei schreiben
|
|
}
|
|
|
|
var loadXmlFile = new LoadXMLFile();
|
|
//LoadXML.Password = password;
|
|
loadXmlFile.FileName = xmlFileName;
|
|
loadXmlFile.Topic = topic;
|
|
loadXmlFile.Group = group;
|
|
loadXmlFile.Key = key;
|
|
loadXmlFile.LoadAllValues = LoadAll.Yes;
|
|
|
|
//XML laden
|
|
if (File.Exists(xmlFileName))
|
|
{
|
|
loadXmlFile.Load();
|
|
|
|
Values = loadXmlFile.Values;
|
|
}
|
|
else
|
|
{
|
|
//Datei nicht gefunden
|
|
MessageBox.Show(AppRes.msg001, AppRes.capt001, MessageBoxButtons.OK);
|
|
}
|
|
}
|
|
|
|
private void ExtractMenuItem()
|
|
{
|
|
string[] menuItems = new string[Values.Length / 5];
|
|
|
|
for (int i = 0; i < Values.Length / 5; i++)
|
|
{
|
|
menuItems[i] = Values[i, 3].ToString();
|
|
}
|
|
|
|
MenuItems = menuItems;
|
|
}
|
|
|
|
private void ExtractStandardItem()
|
|
{
|
|
for (int i = 0; i < Values.Length / 5; i++)
|
|
{
|
|
if (Values[i, 4].ToString() == "yes")
|
|
{
|
|
Standard = Values[i, 3].ToString();
|
|
}
|
|
}
|
|
}
|
|
|
|
//Ein eigenes Event erstellen
|
|
public event EventHandler DynamicMenuItemEvent;
|
|
|
|
//Eventhandler
|
|
protected virtual void OnDynamicMenuItemEvent(EventArgs e)
|
|
{
|
|
EventHandler dynamicMenuItemEvent = DynamicMenuItemEvent;
|
|
if (dynamicMenuItemEvent != null)
|
|
{
|
|
dynamicMenuItemEvent(this, e);
|
|
}
|
|
}
|
|
}
|
|
|
|
#region Exceptions
|
|
class UnableToSaveLicenseBundleException : ApplicationException
|
|
{
|
|
public UnableToSaveLicenseBundleException()
|
|
{ }
|
|
|
|
public UnableToSaveLicenseBundleException(string message) : base(message)
|
|
{ }
|
|
|
|
public UnableToSaveLicenseBundleException(string message, Exception inner) : base(message, inner)
|
|
{ }
|
|
}
|
|
#endregion
|
|
}
|