OSDN Git Service

AppliAppliStation-GUI,例外表示ダイアログ部分で例外が発生したときの対策。
[applistation/AppliStation.git] / AppliStation / Program.cs
1 using System;\r
2 using System.Collections.Generic;\r
3 using System.Windows.Forms;\r
4 using System.IO;\r
5 \r
6 namespace AppliStation\r
7 {\r
8         class Program\r
9         {\r
10                 Form splashScreen;\r
11                 \r
12                 PackageListViewForm form;\r
13                 \r
14                 /// <summary>\r
15                 /// \83A\83v\83\8a\83P\81[\83V\83\87\83\93\82Ì\83I\83v\83V\83\87\83\93\r
16                 /// </summary>\r
17                 Dictionary<string, object> appArgs;\r
18                 \r
19                 string[] restAppArgs;\r
20                 \r
21                 public Program()\r
22                 {\r
23                         appArgs = new Dictionary<string, object>();\r
24                         appArgs["noupdate"] = false;\r
25                         appArgs["cmd"] = string.Empty;\r
26                         appArgs["pkgsref"] = string.Empty;\r
27                         \r
28                         form = new PackageListViewForm();\r
29                 }\r
30                 \r
31                 void RunNormal()\r
32                 {\r
33                         form.Load += delegate(object sender, EventArgs e) {\r
34                                 hideSplashScreen();\r
35                                 form.updateActionInvoke(((bool)appArgs["noupdate"]) != true);\r
36                                 \r
37                                 form.UpdatePackageList();\r
38                         };\r
39                         Application.Run(form);\r
40                 }\r
41                 \r
42                 void RunInstall()\r
43                 {\r
44                         try {\r
45                                 NaGet.Packages.Package[] pkgs = NaGet.Utils.GetDeserializedObject<NaGet.Packages.Package[]>((string) appArgs["pkgsref"]);\r
46                                 \r
47                                 hideSplashScreen();\r
48                                 form.installActionInvoke(pkgs);\r
49                         } catch (UnauthorizedAccessException e) {\r
50                                 MessageBox.Show(string.Format("\8aÇ\97\9d\8eÒ\8c \8cÀ\82É\8f¸\8ai\82µ\82Ä\82¢\82È\82¢\82©\81A\8eÀ\8ds\8c \8cÀ\82É\96â\91è\82ª\82 \82è\82Ü\82·\81B\n(\8fÚ\8d×:{0})", e.Message),\r
51                                                 Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);\r
52                         } catch (FileNotFoundException e) {\r
53                                 MessageBox.Show(string.Format("\83\\83t\83g\8ew\92è\83t\83@\83C\83\8b{0}\82ª\8c©\82Â\82©\82è\82Ü\82¹\82ñ", e.FileName),\r
54                                                 Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);\r
55                         }\r
56                 }\r
57                 \r
58                 void RunUninstall()\r
59                 {\r
60                         try {\r
61                                 NaGet.Packages.Install.InstalledPackage[] pkgs = NaGet.Utils.GetDeserializedObject<NaGet.Packages.Install.InstalledPackage[]>((string) appArgs["pkgsref"]);\r
62                                 \r
63                                 hideSplashScreen();\r
64                                 form.uninstallActionInvoke(pkgs);\r
65                         } catch (UnauthorizedAccessException e) {\r
66                                 MessageBox.Show(string.Format("\8aÇ\97\9d\8eÒ\8c \8cÀ\82É\8f¸\8ai\82µ\82Ä\82¢\82È\82¢\82©\81A\8eÀ\8ds\8c \8cÀ\82É\96â\91è\82ª\82 \82è\82Ü\82·\81B\n(\8fÚ\8d×:{0})", e.Message),\r
67                                                 Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);\r
68                         } catch (FileNotFoundException e) {\r
69                                 MessageBox.Show(string.Format("\83\\83t\83g\8ew\92è\83t\83@\83C\83\8b{0}\82ª\8c©\82Â\82©\82è\82Ü\82¹\82ñ", e.FileName),\r
70                                                 Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);\r
71                         }\r
72                 }\r
73                 \r
74                 private void hideSplashScreen()\r
75                 {\r
76                         if (splashScreen != null && splashScreen.Visible) {\r
77                                 splashScreen.Hide();\r
78                                 splashScreen.Dispose();\r
79                         }\r
80                 }\r
81                 \r
82                 public void Run(string[] args)\r
83                 {\r
84                         parseArgs(args);\r
85                         \r
86                         switch ((string) appArgs["cmd"]) {\r
87                                 case "install":\r
88                                         RunInstall();\r
89                                         break;\r
90                                 case "uninstall":\r
91                                         RunUninstall();\r
92                                         break;\r
93                                 default:\r
94                                         RunNormal();\r
95                                         break;\r
96                         }\r
97                 }\r
98                 \r
99                 private void parseArgs(string[] args)\r
100                 {\r
101                         ArgParse parser = new ArgParse(appArgs);\r
102                         restAppArgs = parser.Parse(args);\r
103                 }\r
104                 \r
105                 [STAThread]\r
106                 public static void Main(string[] args)\r
107                 {\r
108                         Form splashScreen = null;\r
109                         \r
110                         try {\r
111                                 if (args.Length <= 0) { // HACK \88ø\90\94\83p\81[\83X\82Ì\8e\9e\8aÔ\82³\82¦\91Ò\82Ä\82È\82¢\82Ì\82Å\88ø\90\94\82Ì\97L\96³\82Å\95\\8e¦\82ð\94»\92f\r
112                                         splashScreen = new Form();\r
113                                         splashScreen.FormBorderStyle = FormBorderStyle.None;\r
114                                         splashScreen.BackgroundImage = System.Drawing.Bitmap.FromFile(Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "SplashScreen.png"));\r
115                                         splashScreen.Size = splashScreen.BackgroundImage.Size;\r
116                                         splashScreen.ShowIcon = false;\r
117                                         splashScreen.ShowInTaskbar = false;\r
118                                         splashScreen.StartPosition = FormStartPosition.CenterScreen;\r
119                                         splashScreen.Show();\r
120                                 }\r
121                                 \r
122                                 ToolStripManager.VisualStylesEnabled = false; // ToolStrip\82ªLuna\82Å\90Â\82­\82È\82ç\82È\82¢\82æ\82¤\82É\r
123                                 Application.EnableVisualStyles(); // Luna\82âVista\82Ì\83f\83U\83C\83\93\82ð\97L\8cø\82É\r
124                                 \r
125                                 Application.ThreadException += AppliStation.Util.ExceptionDialogForm.Application_ThrowException;\r
126                                 System.Threading.Thread.GetDomain().UnhandledException += AppliStation.Util.ExceptionDialogForm.Application_ThrowException;\r
127                                 \r
128                                 Program prog = new Program();\r
129                                 prog.splashScreen = splashScreen;\r
130                                 prog.Run(args);\r
131                         } catch (Exception e) {\r
132                                 AppliStation.Util.ExceptionDialogForm.Application_ThrowException(null, new System.Threading.ThreadExceptionEventArgs(e));\r
133                         } finally {\r
134                                 if (splashScreen != null) {\r
135                                         splashScreen.Dispose();\r
136                                 }\r
137                         }\r
138                 }\r
139         }\r
140 }\r