Exemplo n.º 1
0
        static public void Main(string[] Args)
        {
            //Upgrade settings
            System.Reflection.Assembly a = System.Reflection.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()));
                var dlog = new ThreeButtonDialog(new ThreeButtonDialog.Details(SystemIcons.Error, msg, Messages.XENCENTER))
                {
                    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 launcched at this point.
                    ShowInTaskbar = true
                };
                dlog.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(Branding.Search);
            TreeSearch.InitSearch();

            ArgType argType = ArgType.None;

            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);
                    HeaderGradientFontSmall = DefaultFont;
                    TabbedDialogHeaderFont  = HeaderGradientFont;
                    TabPageRowBorder        = Color.Gainsboro;
                    TabPageRowHeader        = Color.WhiteSmoke;
                }
                else
                {
                    // 2K8, and Vista, Win7 without styles.
                    TitleBarForeColor       = SystemColors.ControlText;
                    HeaderGradientForeColor = SystemColors.ControlText;
                    HeaderGradientFont      = new Font(DefaultFont.FontFamily, DefaultFont.Size + 1f, FontStyle.Bold);
                    HeaderGradientFontSmall = DefaultFontBold;
                    TabbedDialogHeaderFont  = HeaderGradientFont;
                    TabPageRowBorder        = Color.DarkGray;
                    TabPageRowHeader        = Color.Silver;
                }
                break;

            default:
                TitleBarStartColor      = ProfessionalColors.OverflowButtonGradientBegin;
                TitleBarEndColor        = ProfessionalColors.OverflowButtonGradientEnd;
                TitleBarBorderColor     = TitleBarEndColor;
                TitleBarForeColor       = Application.RenderWithVisualStyles ? Color.White : SystemColors.ControlText;
                HeaderGradientForeColor = TitleBarForeColor;
                HeaderGradientFont      = new Font(DefaultFont.FontFamily, DefaultFont.Size + 1f, FontStyle.Bold);
                HeaderGradientFontSmall = DefaultFontBold;
                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;
            XenAPI.Session.UserAgent             = string.Format("XenCenter/{0}", ClientVersion());
            ReconfigureConnectionSettings();

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

            if (Args.Length > 0)
            {
                log.InfoFormat("Args[0]: {0}", Args[0]);
            }

            List <string> sanitizedArgs = new List <string>(Args);

            // Remove the '--wait' argument, which may have been passed to the splash screen
            sanitizedArgs.Remove("--wait");
            string[] args = null;
            if (sanitizedArgs.Count > 1)
            {
                argType = ParseFileArgs(sanitizedArgs, out args);

                if (argType == ArgType.Passwords)
                {
                    log.DebugFormat("Handling password request using '{0}'", args[0]);
                    try
                    {
                        PasswordsRequest.HandleRequest(args[0]);
                    }
                    catch (Exception exn)
                    {
                        log.Fatal(exn, exn);
                    }
                    Application.Exit();
                    return;
                }
            }
            else if (sanitizedArgs.Count == 1 && sanitizedArgs[0] == "messageboxtest")
            {
                new Dialogs.MessageBoxTest().ShowDialog();
                Application.Exit();
                return;
            }
            else if (sanitizedArgs.Count > 0)
            {
                log.Warn("Unrecognised command line options");
            }

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

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

            MainWindow mainWindow = new MainWindow(argType, args);

            Application.Run(mainWindow);

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