Example #1
0
        static public void Main(string[] Args)
        {
            //Upgrade settings
            Assembly a                = Assembly.GetExecutingAssembly();
            Version  appVersion       = a.GetName().Version;
            string   appVersionString = appVersion.ToString();

            log.DebugFormat("Application version of new settings {0}", appVersionString);

            try
            {
                if (Properties.Settings.Default.ApplicationVersion != appVersion.ToString())
                {
                    log.Debug("Upgrading settings...");
                    Properties.Settings.Default.Upgrade();

                    // if program's hash has changed (e.g. by upgrading to .NET 4.0), then Upgrade() doesn't import the previous application settings
                    // because it cannot locate a previous user.config file. In this case a new user.config file is created with the default settings.
                    // We will try and find a config file from a previous installation and update the settings from it
                    if (Properties.Settings.Default.ApplicationVersion == "" && Properties.Settings.Default.DoUpgrade)
                    {
                        SettingsUpdate.Update();
                    }
                    log.DebugFormat("Settings upgraded from '{0}' to '{1}'", Properties.Settings.Default.ApplicationVersion, appVersionString);
                    Properties.Settings.Default.ApplicationVersion = appVersionString;
                    Settings.TrySaveSettings();
                }
            }
            catch (ConfigurationErrorsException ex)
            {
                log.Error("Could not load settings.", ex);
                var msg = string.Format("{0}\n\n{1}", Messages.MESSAGEBOX_LOAD_CORRUPTED_TITLE,
                                        string.Format(Messages.MESSAGEBOX_LOAD_CORRUPTED, Settings.GetUserConfigPath()));
                using (var dlg = new ErrorDialog(msg)
                {
                    StartPosition = FormStartPosition.CenterScreen,
                    //For reasons I do not fully comprehend at the moment, the runtime
                    //overrides the above StartPosition with WindowsDefaultPosition if
                    //ShowInTaskbar is false. However it's a good idea anyway to show it
                    //in the taskbar since the main form is not launched at this point.
                    ShowInTaskbar = true
                })
                {
                    dlg.ShowDialog();
                }
                Application.Exit();
                return;
            }

            // Reset statics, because XenAdminTests likes to call Main() twice.
            TestExceptionString = null;
            Exiting             = false;
            // Clear XenConnections and History so static classes like OtherConfigAndTagsWatcher
            // listening to changes still work when Main is called more than once.
            ConnectionsManager.XenConnections.Clear();
            ConnectionsManager.History.Clear();

            Search.InitSearch(BrandManager.ExtensionSearch);
            TreeSearch.InitSearch();

            AppDomain.CurrentDomain.UnhandledException -= CurrentDomain_UnhandledException;
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            Application.ThreadException -= Application_ThreadException;
            Application.ThreadException += Application_ThreadException;
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            try
            {
                if (SystemInformation.FontSmoothingType == 2) // ClearType
                {
                    TransparentUsually = SystemColors.Window;
                }
            }
            catch (NotSupportedException)
            {
                // Leave TransparentUsually == Color.Transparent.  This is an old platform
                // without FontSmoothingType support.
            }

            switch (Environment.OSVersion.Version.Major)
            {
            case 6:     // Vista, 2K8, Win7.
                if (Application.RenderWithVisualStyles)
                {
                    // Vista, Win7 with styles.
                    TitleBarStartColor      = Color.FromArgb(242, 242, 242);
                    TitleBarEndColor        = Color.FromArgb(207, 207, 207);
                    TitleBarBorderColor     = Color.FromArgb(160, 160, 160);
                    TitleBarForeColor       = Color.FromArgb(60, 60, 60);
                    HeaderGradientForeColor = Color.White;
                    HeaderGradientFont      = new Font(DefaultFont.FontFamily, 11.25f);
                    TabbedDialogHeaderFont  = HeaderGradientFont;
                    TabPageRowBorder        = Color.Gainsboro;
                    TabPageRowHeader        = Color.WhiteSmoke;
                }
                else
                {
                    // 2K8 and Vista, Win7 without styles.
                    TitleBarStartColor      = ProfessionalColors.OverflowButtonGradientBegin;
                    TitleBarEndColor        = ProfessionalColors.OverflowButtonGradientEnd;
                    TitleBarBorderColor     = TitleBarEndColor;
                    TitleBarForeColor       = SystemColors.ControlText;
                    HeaderGradientForeColor = SystemColors.ControlText;
                    HeaderGradientFont      = new Font(DefaultFont.FontFamily, DefaultFont.Size + 1f, FontStyle.Bold);
                    TabbedDialogHeaderFont  = HeaderGradientFont;
                    TabPageRowBorder        = Color.DarkGray;
                    TabPageRowHeader        = Color.Silver;
                }
                break;

            default:
                TitleBarStartColor      = ProfessionalColors.OverflowButtonGradientBegin;
                TitleBarEndColor        = ProfessionalColors.OverflowButtonGradientEnd;
                TitleBarBorderColor     = TitleBarEndColor;
                TitleBarForeColor       = SystemColors.ControlText;
                HeaderGradientForeColor = Application.RenderWithVisualStyles ? Color.White : SystemColors.ControlText;
                HeaderGradientFont      = new Font(DefaultFont.FontFamily, DefaultFont.Size + 1f, FontStyle.Bold);
                TabbedDialogHeaderFont  = new Font(DefaultFont.FontFamily, DefaultFont.Size + 1.75f, FontStyle.Bold);
                TabPageRowBorder        = Color.DarkGray;
                TabPageRowHeader        = Color.Silver;
                break;
            }

            // Force the current culture, to make the layout the same whatever the culture of the underlying OS (CA-46983).
            Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture = new CultureInfo(InvisibleMessages.LOCALE, false);

            if (string.IsNullOrEmpty(Thread.CurrentThread.Name))
            {
                Thread.CurrentThread.Name = "Main program thread";
            }

            ServicePointManager.DefaultConnectionLimit = 20;
            ServicePointManager.ServerCertificateValidationCallback = SSL.ValidateServerCertificate;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
            Session.UserAgent = string.Format("XenCenter/{0}", ClientVersion());
            RememberProxyAuthenticationModules();
            ReconfigureConnectionSettings();

            log.Info("Application started");
            logSystemDetails();
            Settings.Log();

            // Remove the '--wait' argument, which may have been passed to the splash screen
            var sanitizedArgs = Args.Where(ar => ar != "--wait").ToArray();

            var firstArgType = ParseFileArgs(sanitizedArgs, out string[] tailArgs);

            if (firstArgType == ArgType.Passwords)
            {
                try
                {
                    PasswordsRequest.HandleRequest(tailArgs[0]);
                }
                catch (Exception exn)
                {
                    log.Fatal(exn, exn);
                }

                Application.Exit();
                return;
            }

            try
            {
                ConnectPipe();
            }
            catch (Win32Exception exn)
            {
                log.Error("Creating named pipe failed. Continuing to launch XenCenter.", exn);
            }

            Application.ApplicationExit -= Application_ApplicationExit;
            Application.ApplicationExit += Application_ApplicationExit;

            //set the help version before launching the main window;
            //the call starts a different thread so it won't delay the main window launch;
            //in most cases it is expected to have returned by the time the users request help;
            //if they do before it has returned, the thread requesting help will wait for it
            HelpManager.SetHelpVersion();

            MainWindow mainWindow = new MainWindow(firstArgType, tailArgs);

            Application.Run(mainWindow);

            log.Info("Application main thread exited");
        }