OSDN Git Service

AppliStation-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                         appArgs["instsref"] = string.Empty;\r
28                         \r
29                         form = new PackageListViewForm();\r
30                 }\r
31                 \r
32                 void RunNormal()\r
33                 {\r
34                         form.Load += delegate(object sender, EventArgs e) {\r
35                                 hideSplashScreen();\r
36                                 form.updateActionInvoke(((bool)appArgs["noupdate"]) != true);\r
37                                 \r
38                                 form.UpdatePackageList();\r
39                         };\r
40                         Application.Run(form);\r
41                 }\r
42                 \r
43                 void RunInstall()\r
44                 {\r
45                         try {\r
46                                 NaGet.Packages.Install.Installation[] insts = NaGet.Utils.GetDeserializedObject<NaGet.Packages.Install.Installation[]>((string) appArgs["instsref"]);\r
47                                 \r
48                                 hideSplashScreen();\r
49                                 form.installActionInvoke(insts);\r
50                         } catch (UnauthorizedAccessException e) {\r
51                                 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
52                                                 Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);\r
53                         } catch (FileNotFoundException e) {\r
54                                 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
55                                                 Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);\r
56                         }\r
57                 }\r
58                 \r
59                 void RunUninstall()\r
60                 {\r
61                         try {\r
62                                 NaGet.Packages.Install.InstalledPackage[] pkgs = NaGet.Utils.GetDeserializedObject<NaGet.Packages.Install.InstalledPackage[]>((string) appArgs["pkgsref"]);\r
63                                 \r
64                                 hideSplashScreen();\r
65                                 form.uninstallActionInvoke(pkgs);\r
66                         } catch (UnauthorizedAccessException e) {\r
67                                 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
68                                                 Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);\r
69                         } catch (FileNotFoundException e) {\r
70                                 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
71                                                 Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);\r
72                         }\r
73                 }\r
74                 \r
75                 private void hideSplashScreen()\r
76                 {\r
77                         if (splashScreen != null && splashScreen.Visible) {\r
78                                 splashScreen.Hide();\r
79                                 splashScreen.Dispose();\r
80                         }\r
81                 }\r
82                 \r
83                 public void Run(string[] args)\r
84                 {\r
85                         try {\r
86                                 parseArgs(args);\r
87                         } catch (ApplicationException e) {\r
88                                 MessageBox.Show(e.Message, "AppliStation \8bN\93®\88ø\90\94\83G\83\89\81[", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
89                                 return;\r
90                         }\r
91                         \r
92                         switch ((string) appArgs["cmd"]) {\r
93                                 case "install":\r
94                                         RunInstall();\r
95                                         break;\r
96                                 case "uninstall":\r
97                                         RunUninstall();\r
98                                         break;\r
99                                 default:\r
100                                         RunNormal();\r
101                                         break;\r
102                         }\r
103                 }\r
104                 \r
105                 private void parseArgs(string[] args)\r
106                 {\r
107                         NaGet.ArgParser parser = new NaGet.ArgParser(appArgs);\r
108                         restAppArgs = parser.Parse(args);\r
109                 }\r
110                 \r
111                 [STAThread]\r
112                 public static void Main(string[] args)\r
113                 {\r
114                         Form splashScreen = null;\r
115                         \r
116                         try {\r
117                                 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
118                                         splashScreen = new Form();\r
119                                         splashScreen.FormBorderStyle = FormBorderStyle.None;\r
120                                         splashScreen.BackgroundImage = System.Drawing.Bitmap.FromFile(Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "SplashScreen.png"));\r
121                                         splashScreen.Size = splashScreen.BackgroundImage.Size;\r
122                                         splashScreen.ShowIcon = false;\r
123                                         splashScreen.ShowInTaskbar = false;\r
124                                         splashScreen.StartPosition = FormStartPosition.CenterScreen;\r
125                                         splashScreen.Show();\r
126                                 }\r
127                                 \r
128                                 ToolStripManager.VisualStylesEnabled = false; // ToolStrip\82ªLuna\82Å\90Â\82­\82È\82ç\82È\82¢\82æ\82¤\82É\r
129                                 Application.EnableVisualStyles(); // Luna\82âVista\82Ì\83f\83U\83C\83\93\82ð\97L\8cø\82É\r
130                                 \r
131                                 Application.ThreadException += AppliStation.Util.ExceptionDialogForm.Application_ThrowException;\r
132                                 System.Threading.Thread.GetDomain().UnhandledException += AppliStation.Util.ExceptionDialogForm.Application_ThrowException;\r
133                                 \r
134                                 Program prog = new Program();\r
135                                 prog.splashScreen = splashScreen;\r
136                                 prog.Run(args);\r
137                         } catch (Exception e) {\r
138                                 AppliStation.Util.ExceptionDialogForm.Application_ThrowException(null, new System.Threading.ThreadExceptionEventArgs(e));\r
139                         } finally {\r
140                                 if (splashScreen != null) {\r
141                                         splashScreen.Dispose();\r
142                                 }\r
143                         }\r
144                 }\r
145         }\r
146 }\r