Beispiel #1
0
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            try
            {
                Logging.LogManager.Initialize("Tray");
            }
            catch (Exception ex)
            {
#if DEBUG
                Debug.WriteLine("Failed to initialize logging");
                Debug.WriteLine(ex);
#endif
                MessageBox.Show($"Failed to initialize logging: {ex}");

                Environment.Exit(-1);
            }

            _logger.Info($"{Namespace} v{Version} initializing...");
            _logger.Debug($"ComamndLine: {Environment.CommandLine}");

            var ownProcess    = Process.GetCurrentProcess();
            var openProcesses = Process.GetProcessesByName(ownProcess.ProcessName).Where(p => p.SessionId == ownProcess.SessionId);
            if (openProcesses.Count() > 1)
            {
                // There is already another process in this session. Just exit
                _logger.Info($"Another instance of {ownProcess.ProcessName} is already running. Exiting...");
                Environment.Exit(0);
            }

            _logger.Trace("Reading settings ...");
            Settings = ToolkitEnvironment.Settings.GetTrayAppSettings();

            _logger.Trace("Creating LanguageManager...");
            try
            {
                LanguageManager = new LanguageManager();
            }
            catch (Exception ex)
            {
                _logger.Fatal(ex, "Failed to create LanguageManager. Exiting...");
                MessageBox.Show($"Failed to initialize LanguageManager: {ex}");
                Environment.Exit(-1);
            }

            _logger.Trace("Creating AppList...");
            FormAppList = new AppList();

            _logger.Trace("Creating TrayIcon...");
            try
            {
                TrayIcon = new NotifyIcon
                {
                    Icon = new Icon("Tray.ico"),
                    Text = "DeploymentToolkit TrayApp"
                };
            }
            catch (Exception ex)
            {
                _logger.Fatal(ex, "Failed to create tray icon");
                Environment.Exit(-2);
            }

            _logger.Trace("Creating ContextMenu...");
            var contextMenu = new ContextMenu();

            if (Settings.EnableAppList)
            {
                _logger.Trace("Creating Show MenuItem...");
                MenuItemToggleVisibility = new MenuItem
                {
                    Index = 0,
                    Text  = "Show"
                };
                MenuItemToggleVisibility.Click += ToggleTrayAppClicked;
                contextMenu.MenuItems.Add(MenuItemToggleVisibility);
            }
            else
            {
                _logger.Debug("AppList not enabled. Not creating 'Show' MenuItem");
            }

            _logger.Trace("Creating Close MenuItem...");
            MenuItemExit = new MenuItem
            {
                Index = 1,
                Text  = "Close"
            };
            MenuItemExit.Click += CloseTrayAppClicked;
            contextMenu.MenuItems.Add(MenuItemExit);

#if DEBUG
            _logger.Trace("Creating debug MenuItems...");

            {
                var item = new MenuItem()
                {
                    Index = 2,
                    Text  = "DEBUG: View CloseApplication (cmd.exe)"
                };
                item.Click += delegate(object sender, EventArgs e)
                {
                    FormCloseApplication?.Dispose();
                    FormCloseApplication = new CloseApplication(new[] { "cmd.exe" }, 0);
                    ShowForm(FormCloseApplication);
                };
                contextMenu.MenuItems.Add(item);
            }

            {
                var item = new MenuItem()
                {
                    Index = 3,
                    Text  = "DEBUG: View DeploymentDeferal"
                };
                item.Click += delegate(object sender, EventArgs e)
                {
                    FormDeploymentDeferal?.Dispose();
                    FormDeploymentDeferal = new DeploymentDeferal(1, DateTime.Now.AddDays(7));
                    ShowForm(FormDeploymentDeferal);
                };
                contextMenu.MenuItems.Add(item);

                // Make sure this is never null as we may test dialogs
                DeploymentInformation = new DeploymentInformationMessage()
                {
                    DeploymentName  = "DEBUG",
                    SequenceType    = SequenceType.Installation,
                    DisplaySettings = new DisplaySettings()
                    {
                        BlockScreensDuringInstallation = false,
                        PersistentPrompt = false
                    }
                };
            }

            {
                var item = new MenuItem()
                {
                    Index = 4,
                    Text  = "DEBUG: Logoff"
                };
                item.Click += delegate(object sender, EventArgs e)
                {
                    Util.PowerUtil.Logoff();
                };
                contextMenu.MenuItems.Add(item);
            }

            {
                var item = new MenuItem()
                {
                    Index = 5,
                    Text  = "DEBUG: Restart"
                };
                item.Click += delegate(object sender, EventArgs e)
                {
                    Util.PowerUtil.Restart();
                };
                contextMenu.MenuItems.Add(item);
            }
#endif

            _logger.Trace("Finishing TrayIcon...");
            TrayIcon.ContextMenu = contextMenu;
            TrayIcon.Visible     = true;

            _logger.Trace("Creating PipeServer...");
            _pipeServer = new PipeServer(TrayIcon);
            _pipeServer.OnNewMessage += OnNewMessage;

            _logger.Trace("Checking commandline arguments...");
            StartUpParameter = args.Any(a => a.ToLower() == "--requested");
            if (args.Any(a => a.ToLower() == "--startup") || !Settings.EnableAppList)
            {
                _logger.Info($"Detected '--startup' commandline or EnableAppList is set to false. Starting application in Tray only ({args.Any(a => a.ToLower() == "--startup")}/{StartUpParameter}/{Settings.EnableAppList})");
                StartUp = true;
                // create handles
                var handle = FormAppList.Handle;
                Application.Run(FormAppList);
            }
            else
            {
                _logger.Info("No valid commandline detected. Executing normal start");
                Application.Run(FormAppList);
            }
        }
Beispiel #2
0
        private static void OnNewMessage(object sender, NewMessageEventArgs e)
        {
            // NOT ON GUI THREAD !!
            try
            {
                switch (e.MessageId)
                {
                case MessageId.DeploymentInformationMessage:
                {
                    _logger.Trace("Received DeploymentInformationMessage");
                    DeploymentInformation = e.Message as DeploymentInformationMessage;

                    _logger.Trace($"Deploymentmethod: {DeploymentInformation.SequenceType}");
                    _logger.Trace($"Deploymentname: {DeploymentInformation.DeploymentName}");
                }
                break;

                case MessageId.DeploymentStarted:
                case MessageId.DeploymentSuccess:
                case MessageId.DeploymentError:
                {
                    if (e.MessageId == MessageId.DeploymentStarted && DeploymentInformation.DisplaySettings.BlockScreensDuringInstallation)
                    {
                        _logger.Trace("BlockScreensDuringInstallation specified. Blocking ...");
                        BlockScreen();
                    }
                    else
                    {
                        UnblockScreen();
                    }

                    var language = LanguageManager.Language;
                    var text     = DeploymentInformation.SequenceType == SequenceType.Installation
                                ? language.DeploymentType_Install
                                : language.DeploymentType_UnInstall;
                    text += " ";
                    var icon = ToolTipIcon.Info;

                    if (e.MessageId == MessageId.DeploymentStarted)
                    {
                        text += language.BalloonText_Start;
                    }
                    else if (e.MessageId == MessageId.DeploymentSuccess)
                    {
                        text += language.BalloonText_Complete;
                    }
                    else if (e.MessageId == MessageId.DeploymentError)
                    {
                        icon  = ToolTipIcon.Error;
                        text += language.BalloonText_Error;
                    }

                    _logger.Trace($"Icon: {icon}");
                    _logger.Trace($"Final balloon tip text: {text}");

                    FormAppList.Invoke((Action) delegate()
                        {
                            TrayIcon.BalloonTipIcon  = icon;
                            TrayIcon.BalloonTipTitle = DeploymentInformation.DeploymentName;
                            TrayIcon.BalloonTipText  = text;
                            TrayIcon.ShowBalloonTip(10000);

                            if (e.MessageId == MessageId.DeploymentSuccess || e.MessageId == MessageId.DeploymentError)
                            {
                                // Enable exit again
                                MenuItemExit.Enabled = true;
                            }
                        });
                }
                break;

                case MessageId.DeploymentRestart:
                {
                    var message = e.Message as DeploymentRestartMessage;
                    FormAppList.Invoke((Action) delegate()
                        {
#if !DEBUG
                            // Disable exit of the program
                            MenuItemExit.Enabled = false;
#endif

                            if (FormRestart != null && !FormRestart.IsDisposed)
                            {
                                FormRestart.Dispose();
                            }

                            FormRestart = new RestartDialog(message.TimeUntilForceRestart);
                            ShowForm(FormRestart);
                        });
                }
                break;

                case MessageId.DeploymentLogoff:
                {
                    var message = e.Message as DeploymentLogoffMessage;

                    FormAppList.Invoke((Action) delegate()
                        {
                            TrayIcon.BalloonTipIcon  = ToolTipIcon.Warning;
                            TrayIcon.BalloonTipTitle = DeploymentInformation.DeploymentName;
                            TrayIcon.BalloonTipText  = $"You will be logged off in {message.TimeUntilForceLogoff} seconds";
                            TrayIcon.ShowBalloonTip(10000);

                            System.Threading.Tasks.Task.Factory.StartNew(async() =>
                            {
                                await System.Threading.Tasks.Task.Delay(message.TimeUntilForceLogoff * 1000);
                                Util.PowerUtil.Logoff();
                            });
                        });
                }
                break;

                case MessageId.CloseApplications:
                {
                    var message = e.Message as CloseApplicationsMessage;
#if !DEBUG
                    // Disable exit of the program
                    MenuItemExit.Enabled = false;
#endif
                    FormAppList.Invoke((Action) delegate()
                        {
                            if (FormCloseApplication != null && !FormCloseApplication.IsDisposed)
                            {
                                FormCloseApplication.Dispose();
                            }

                            FormCloseApplication = new CloseApplication(message.ApplicationNames, message.TimeUntilForceClose);
                            ShowForm(FormCloseApplication);
                        });
                }
                break;

                case MessageId.DeferDeployment:
                {
                    var message = e.Message as DeferMessage;
#if !DEBUG
                    // Disable exit of the program
                    MenuItemExit.Enabled = false;
#endif
                    FormAppList.Invoke((Action) delegate()
                        {
                            if (FormDeploymentDeferal != null && !FormDeploymentDeferal.IsDisposed)
                            {
                                FormDeploymentDeferal.Dispose();
                            }

                            FormDeploymentDeferal = new DeploymentDeferal(message.RemainingDays, message.DeadLine);
                            ShowForm(FormDeploymentDeferal);
                        });
                }
                break;
                }
            }
            catch (Exception ex)
            {
                _logger.Fatal(ex, "Failed to process message");
            }
        }