OSDN Git Service

AppliStation-GUI,名前に"&"を含むソフトの場合アクセスキーの&と競合してしまい表示が変になる問題を対処
[applistation/AppliStation.git] / AppliStation / PackageInfoForm.cs
1 using System;\r
2 using System.Drawing;\r
3 using System.Windows.Forms;\r
4 using NaGet.Packages.Install;\r
5 using NaGet.Packages;\r
6 using System.Text;\r
7 \r
8 namespace AppliStation\r
9 {\r
10         /// <summary>\r
11         /// Description of PackageInfoForm.\r
12         /// </summary>\r
13         public partial class PackageInfoForm : Form\r
14         {\r
15                 private Package pkg;\r
16                 \r
17                 public PackageInfoForm()\r
18                 {\r
19                         InitializeComponent();\r
20                 }\r
21                 \r
22                 public Package SelectedObject {\r
23                         get { return pkg; }\r
24                         set {\r
25                                 pkg = value;\r
26                                 updateSelectedObject();\r
27                         }\r
28                 }\r
29                 \r
30                 private void updateSelectedObject()\r
31                 {\r
32                         bool isInstalledPackage = pkg is InstalledPackage;\r
33                         \r
34                         iconLabel.Image = null;\r
35                         iconLabel.Size = Size.Empty;\r
36                         if (isInstalledPackage) {\r
37                                 Icon icon = AppliStation.Util.PackageUtils.GetIconForPackage((InstalledPackage) pkg);\r
38                                 if (icon != null) {\r
39                                         iconLabel.Image = icon.ToBitmap();\r
40                                         iconLabel.Size = icon.Size;\r
41                                 }\r
42                         }\r
43                         nameLabel.Text = string.Format("{0} ({1})", pkg.Name, pkg.Version);\r
44                         nameLabel.Font = new Font(SystemFonts.DefaultFont.FontFamily, SystemFonts.DefaultFont.Size*1.5f);\r
45                         \r
46                         summaryTextBox.Text = pkg.Summary;\r
47                         \r
48                         if (isInstalledPackage) {\r
49                                 UninstallInformation uninstInfo = ((InstalledPackage) pkg).UninstallInfo;\r
50                                 \r
51                                 string instPath = null;\r
52                                 if (pkg.Type == InstallerType.ARCHIVE) {\r
53                                         instPath = System.IO.Path.Combine(NaGet.Env.ArchiveProgramFiles, pkg.Name);\r
54                                 } else if (System.IO.Directory.Exists(uninstInfo.InstallLocation)) {\r
55                                         instPath = uninstInfo.InstallLocation;\r
56                                 }\r
57                                 \r
58                                 statusLinkLabel.Links.Clear();\r
59                                 if (string.IsNullOrEmpty(instPath)) {\r
60                                         statusLinkLabel.Text = "\8fî\95ñ\82ª\92ñ\8b\9f\82³\82ê\82Ä\82¢\82Ü\82¹\82ñ";\r
61                                         statusLinkLabel.Enabled = false;\r
62                                 } else {\r
63                                         statusLinkLabel.Text = instPath;\r
64                                         statusLinkLabel.Enabled = true;\r
65                                         \r
66                                         addLinkToLinkLabel(statusLinkLabel, uninstInfo.InstallLocation, uninstInfo.InstallLocation);\r
67                                 }\r
68                         }\r
69                         urlLinkLabel.Links.Clear();\r
70                         if (pkg.Url != null) {\r
71                                 urlLinkLabel.Text = pkg.Url.Href;\r
72                                 urlLinkLabel.Links.Add(0, pkg.Url.Href.Length, pkg.Url.Href);\r
73                         } else {\r
74                                 urlLinkLabel.Text = string.Empty;\r
75                                 urlLinkLabel.Enabled = false;\r
76                                 urlLabel.Enabled = false;\r
77                         }\r
78                         tagsTextBox.Text = pkg.Tags;\r
79                         setLinkLabelFromLicense(licenseLinkLabel, pkg.License, pkg.Url.Href);\r
80                         setTypeValueLabel(typeValueLabel, pkg.Type);\r
81                         \r
82                         tabControl1.TabPages.Clear();\r
83                         if (pkg is InstalledPackage && string.IsNullOrEmpty(pkg.Version)) {\r
84                                 tabControl1.Visible = false;\r
85                         } else {\r
86                                 tabControl1.Visible = true;\r
87                                 foreach (Installer inst in pkg.Installer) {\r
88                                         TabPage page = new TabPage(string.Format("\83C\83\93\83X\83g\81[\83\89 \82»\82Ì{0}", tabControl1.TabCount+1));\r
89                                         page.Dock = DockStyle.Fill;\r
90                                         \r
91                                         InstallerInfoForm instForm = new InstallerInfoForm();\r
92                                         instForm.SelectedObject = inst;\r
93                                         instForm.Dock = DockStyle.Fill;\r
94                                         page.Controls.Add(instForm);\r
95                                         \r
96                                         tabControl1.TabPages.Add(page);\r
97                                 }\r
98                         }\r
99                         \r
100                         this.Size = new Size(this.Size.Width, (tabControl1.TabCount>0)? 380 : 250);\r
101                 }\r
102                 \r
103                 private static void setLinkLabelFromLicense(LinkLabel linklabel, string license, string publicSiteUrl)\r
104                 {\r
105                         linklabel.Enabled = true;\r
106                         switch (license ?? string.Empty) {\r
107                                 case "":\r
108                                         if (string.IsNullOrEmpty(publicSiteUrl)) {\r
109                                                 linklabel.Text = "\83t\83\8a\81[\83\\83t\83g\83E\83F\83A (\96³\8f\9e)";\r
110                                                 linklabel.Enabled = false;\r
111                                         } else {\r
112                                                 linklabel.Text = "\83t\83\8a\81[\83\\83t\83g\83E\83F\83A (\8fÚ\8d×\82Í\8cö\8e®\83T\83C\83g\82È\82Ç\82Å\8am\94F\82µ\82Ä\82­\82¾\82³\82¢)";\r
113                                                 addLinkToLinkLabel(linklabel, "\8cö\8e®\83T\83C\83g", publicSiteUrl);\r
114                                         }\r
115                                         break;\r
116                                 #region \83I\81[\83v\83\93\83\\81[\83X\82©\82Â\83R\83s\81[\83\8c\83t\83g\r
117                                 case "GPL":\r
118                                         linklabel.Text = "GPL (\83I\81[\83v\83\93\83\\81[\83X)";\r
119                                         addLinkToLinkLabel(linklabel, "GPL", @"http://www.gnu.org/licenses/gpl.ja.html");\r
120                                         addLinkToLinkLabel(linklabel, "\83I\81[\83v\83\93\83\\81[\83X", @"http://creativecommons.org/licenses/GPL/2.0/deed.ja");\r
121                                         break;\r
122                                 case "GPLv2":\r
123                                 case "GPLv2+":\r
124                                         linklabel.Text = "GPLv2 (\83I\81[\83v\83\93\83\\81[\83X)";\r
125                                         addLinkToLinkLabel(linklabel, "GPLv2", @"http://www.gnu.org/licenses/gpl-2.0.html");\r
126                                         break;\r
127                                 case "GPLv3":\r
128                                 case "GPLv3+":\r
129                                         linklabel.Text = "GPLv3 (\83I\81[\83v\83\93\83\\81[\83X)";\r
130                                         addLinkToLinkLabel(linklabel, "GPLv3", @"http://www.gnu.org/licenses/gpl-3.0.html");\r
131                                         break;\r
132                                 case "LGPL":\r
133                                         linklabel.Text = "LGPL (\83I\81[\83v\83\93\83\\81[\83X)";\r
134                                         addLinkToLinkLabel(linklabel, "LGPL", @"http://www.gnu.org/licenses/lgpl.ja.html");\r
135                                         addLinkToLinkLabel(linklabel, "\83I\81[\83v\83\93\83\\81[\83X", @"http://creativecommons.org/licenses/LGPL/2.1/deed.ja");\r
136                                         break;\r
137                                 case "LGPLv2":\r
138                                 case "LGPLv2+":\r
139                                         linklabel.Text = "LGPLv2 (\83I\81[\83v\83\93\83\\81[\83X)";\r
140                                         addLinkToLinkLabel(linklabel, "LGPLv2", @"http://www.gnu.org/licenses/lgpl-2.1.html");\r
141                                         break;\r
142                                 case "LGPLv3":\r
143                                 case "LGPLv3+":\r
144                                         linklabel.Text = "LGPLv3 (\83I\81[\83v\83\93\83\\81[\83X)";\r
145                                         addLinkToLinkLabel(linklabel, "LGPLv3", @"http://www.gnu.org/licenses/lgpl-3.0.html");\r
146                                         break;\r
147                                 case "MPL":\r
148                                         linklabel.Text = "MPL (\83I\81[\83v\83\93\83\\81[\83X)";\r
149                                         addLinkToLinkLabel(linklabel, "MPL", @"http://www.mozilla.org/MPL/MPL-1.1.html");\r
150                                         break;\r
151                                 case "CPL":\r
152                                         linklabel.Text = "CPL (\83I\81[\83v\83\93\83\\81[\83X)";\r
153                                         linklabel.Links.Add(0, linklabel.Text.Length, @"http://sourceforge.jp/projects/opensource/wiki/licenses%2FCommon_Public_License");\r
154                                         break;\r
155                                 #endregion\r
156                                 #region \83I\81[\83v\83\93\83\\81[\83X\82¾\82ª\81A\83R\83s\81[\83\8c\83t\83g\82Å\82È\82¢\r
157                                 case "MIT":\r
158                                         linklabel.Text = "MIT\83\89\83C\83Z\83\93\83X (\83I\81[\83v\83\93\83\\81[\83X)";\r
159                                         addLinkToLinkLabel(linklabel, "MIT\83\89\83C\83Z\83\93\83X", @"http://www.opensource.org/licenses/mit-license.php");\r
160                                         break;\r
161                                 case "BSD":\r
162                                         linklabel.Text = "\8fC\90³\8dÏBSD\83\89\83C\83Z\83\93\83X (\83I\81[\83v\83\93\83\\81[\83X)";\r
163                                         addLinkToLinkLabel(linklabel, "BSD\83\89\83C\83Z\83\93\83X", @"http://www.opensource.org/licenses/bsd-license.php");\r
164                                         break;\r
165                                 case "Artistic":\r
166                                         linklabel.Text = "Artistic\83\89\83C\83Z\83\93\83X (\83I\81[\83v\83\93\83\\81[\83X)";\r
167                                         addLinkToLinkLabel(linklabel, "Artistic\83\89\83C\83Z\83\93\83X", @"http://www.perlfoundation.org/artistic_license_2_0");\r
168                                         break;\r
169                                 #endregion\r
170                                 case "NYSL":\r
171                                         linklabel.Text = "\8eÏ\82é\82È\82è\8fÄ\82­\82È\82è\8dD\82«\82É\82µ\82ë\83\89\83C\83Z\83\93\83X";\r
172                                         linklabel.Links.Add(0, linklabel.Text.Length, @"http://www.kmonos.net/nysl/");\r
173                                         break;\r
174                                 case "Public Domain":\r
175                                         linklabel.Text = "\83p\83u\83\8a\83b\83N\83h\83\81\83C\83\93 (\92\98\8dì\8c \8bA\91®\82È\82µ)";\r
176                                         linklabel.Links.Add(0, linklabel.Text.Length, @"http://e-words.jp/w/E38391E38396E383AAE38383E382AFE38389E383A1E382A4E383B3.html");\r
177                                         break;\r
178                                 default:\r
179                                         if (license.StartsWith("http://")) {\r
180                                                 linklabel.Text = license;\r
181                                                 linklabel.Links.Add(0, license.Length, license);\r
182                                         } else {\r
183                                                 linklabel.Text = license;\r
184                                                 linklabel.Links.Add(0, 0, string.Empty);\r
185                                         }\r
186                                         break;\r
187                         }\r
188                         \r
189                 }\r
190                 \r
191                 private static void setTypeValueLabel(Label typeValueLabel, InstallerType instType)\r
192                 {\r
193                         switch (instType) {\r
194                                 case InstallerType.ARCHIVE:\r
195                                         typeValueLabel.Text = "\8e©\8cÈ\89ð\93\80\8f\91\8cÉ\82Ü\82½\82Í\8f\91\8cÉ";\r
196                                         break;\r
197                                 case InstallerType.EXEC_INSTALLER:\r
198                                         typeValueLabel.Text = "\83C\83\93\83X\83g\81[\83\89\8c`\8e®";\r
199                                         break;\r
200                                 case InstallerType.MSI_PACKAGE:\r
201                                         typeValueLabel.Text = "MSI\83t\83@\83C\83\8b\8c`\8e®";\r
202                                         break;\r
203                                 default:\r
204                                         typeValueLabel.Text = "\95s\96¾\82È\83C\83\93\83X\83g\81[\83\8b\8c`\8e®(\83C\83\93\83X\83g\81[\83\8b\95s\94\)";\r
205                                         break;\r
206                         }\r
207                 }\r
208                 \r
209                 /// <summary>\r
210                 /// \83\8a\83\93\83N\83\89\83x\83\8b\82Ì\83\8a\83\93\83N\91}\93ü\95Ö\97\98\83\81\83\\83b\83h\r
211                 /// </summary>\r
212                 /// <param name="linkLabel">\91Î\8fÛ\83\8a\83\93\83N\83\89\83x\83\8b</param>\r
213                 /// <param name="word">\83\8a\83\93\83N\91Î\8fÛ\95\8e\9a\97ñ</param>\r
214                 /// <param name="link">\83\8a\83\93\83N\83I\83u\83W\83F\83N\83g</param>\r
215                 private static void addLinkToLinkLabel(LinkLabel linkLabel, string word, string link)\r
216                 {\r
217                         int pos = linkLabel.Text.IndexOf(word, 0);\r
218                         if (pos >= 0) {\r
219                                 linkLabel.Links.Add(pos, word.Length, link);\r
220                         } else throw new ArgumentException(string.Format("label text \"{0}\" does not contain \"{1}\"!", linkLabel.Text, word));\r
221                 }\r
222                 \r
223                 private void linkLabelLinkClicked(object sender, LinkLabelLinkClickedEventArgs e)\r
224                 {\r
225                         string linkData = e.Link.LinkData.ToString();\r
226                         \r
227                         if (System.IO.Directory.Exists(linkData)) {\r
228                                 e.Link.Visited = true;\r
229                                 System.Diagnostics.Process.Start(e.Link.LinkData.ToString());\r
230                         } else if (System.Text.RegularExpressions.Regex.IsMatch(linkData, @"(https?|file)://")) {\r
231                                 e.Link.Visited = true;\r
232                                 System.Diagnostics.Process.Start(linkData);\r
233                         }\r
234                 }\r
235         }\r
236 }\r