summaryrefslogtreecommitdiffstats
path: root/win/CS/Program.cs
blob: 847c70bd6acb9e8bf181effeff476f690ee3ef4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*  Program.cs
    This file is part of the HandBrake source code.
    Homepage: <http://handbrake.fr>.
    It may be used under the terms of the GNU General Public License. */

namespace Handbrake
{
    using System;
    using System.Diagnostics;
    using System.IO;
    using System.Windows.Forms;

    using HandBrake.ApplicationServices;
    using HandBrake.ApplicationServices.Exceptions;
    using HandBrake.ApplicationServices.Services;
    using HandBrake.ApplicationServices.Services.Interfaces;

    using Handbrake.Properties;
    using Handbrake.ToolWindows;

    /// <summary>
    /// HandBrake Starts Here
    /// </summary>
    public static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        /// <param name="args">
        /// The args.
        /// </param>
        [STAThread]
        public static void Main(string[] args)
        {
            InstanceId = Process.GetProcessesByName("HandBrake").Length;

            // Handle any unhandled exceptions
            AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException;

            // Check that HandBrakeCLI is availabl.
            string failedInstall = "HandBrake is not installed properly. Please reinstall HandBrake. \n\n";
            string missingFiles = string.Empty;

            // Verify HandBrakeCLI.exe exists
            if (!File.Exists(Path.Combine(Application.StartupPath, "HandBrakeCLI.exe")))
            {
                missingFiles += "\"HandBrakeCLI.exe\" was not found.";
            }

            if (missingFiles != string.Empty)
            {
                MessageBox.Show(
                    failedInstall + missingFiles,
                    "Error",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
                return;
            }

            // Attempt to upgrade / keep the users settings between versions
            try
            {
                if (Settings.Default.UpdateRequired)
                {
                    // Upgrading user settings seems to be problematic from 0.9.5 -> Current, 
                    // Seems to be seeing user.config corrupation from time to time.
                    // So I'm going to only allow this for users using svn builds.
                    // Going from major to major will require the user to reset.
                    // Going from svn to svn will attempt the upgrade.
                    IUserSettingService service = ServiceManager.UserSettingService;
                    string version = service.GetUserSetting<string>(UserSettingConstants.HandBrakeVersion);
                    if (version.Contains("svn"))
                    {
                        Settings.Default.Upgrade();
                        Settings.Default.UpdateRequired = false;
                        Settings.Default.Save();
                    }

                    // Re-detect the CLI version data.
                    Functions.Main.SetCliVersionData();
                }
            }
            catch (Exception)
            {
                Functions.Main.RecoverFromCorruptedLocalApplicationConfig();
                return;
            }

            // Check were not running on a screen that's going to cause some funnies to happen.
            Screen scr = Screen.PrimaryScreen;
            if ((scr.Bounds.Width < 1024) || (scr.Bounds.Height < 620))
            {
                MessageBox.Show(
                    "Your system does not meet the minimum requirements for HandBrake. \n" +
                    "Your screen is running at: " + scr.Bounds.Width + "x" + scr.Bounds.Height +
                    " \nScreen resolution is too Low. Must be 1024x620 or greater.\n\n",
                    "Error",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
            } 
            else
            {
                string logDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"HandBrake\logs");
                if (!Directory.Exists(logDir))
                    Directory.CreateDirectory(logDir);

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new frmMain(args));
            }
        }

        /// <summary>
        /// Throw up an error message for any unhandled exceptions.
        /// </summary>
        /// <param name="sender">The sender</param>
        /// <param name="e">Unhandled Exception EventArgs </param>
        private static void CurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            try
            {   
                ExceptionWindow window = new ExceptionWindow();

                if (e.ExceptionObject.GetType() == typeof(GeneralApplicationException))
                {
                    GeneralApplicationException applicationException = e.ExceptionObject as GeneralApplicationException;
                    if (applicationException != null)
                    {
                        window.Setup(
                            applicationException.Error + Environment.NewLine + applicationException.Solution,
                            e.ExceptionObject + "\n\n ---- \n\n" + applicationException.ActualException);
                    }
                }
                else
                {
                    window.Setup("An Unknown Error has occured.", e.ExceptionObject.ToString());
                }
                window.ShowDialog();      
            }
            catch (Exception)
            {
                MessageBox.Show(
                    "An Unknown Error has occured. \n\n Exception:" + e.ExceptionObject,
                    "Unhandled Exception",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
            }
        }

        public static int InstanceId;
    }
}