OSDN Git Service

AppliStation-GUI,UserPrefForm.csへのコメント追加
[applistation/AppliStation.git] / AppliStation / PackageInfoForm.cs
1 using System;\r
2 using System.Drawing;\r
3 using System.Windows.Forms;\r
4 using System.Text;\r
5 using System.IO;\r
6 using NaGet.Packages.Install;\r
7 using NaGet.Packages;\r
8 \r
9 namespace AppliStation\r
10 {\r
11         /// <summary>\r
12         /// Description of PackageInfoForm.\r
13         /// </summary>\r
14         public partial class PackageInfoForm : Form\r
15         {\r
16                 private Package pkg;\r
17                 \r
18                 public PackageInfoForm()\r
19                 {\r
20                         InitializeComponent();\r
21                 }\r
22                 \r
23                 /// <summary>\r
24                 /// 現在選択中(=表示中)のパッケージ\r
25                 /// </summary>\r
26                 public Package SelectedObject {\r
27                         get { return pkg; }\r
28                         set {\r
29                                 pkg = value;\r
30                                 updateSelectedObject();\r
31                         }\r
32                 }\r
33                 \r
34                 private void updateSelectedObject()\r
35                 {\r
36                         bool isInstalledPackage = pkg is InstalledPackage;\r
37                         \r
38                         iconLabel.Image = null;\r
39                         iconLabel.Size = Size.Empty;\r
40                         if (isInstalledPackage) {\r
41                                 Icon icon = AppliStation.Util.GUIUtils.GetIconForPackage((InstalledPackage) pkg);\r
42                                 if (icon != null) {\r
43                                         iconLabel.Image = icon.ToBitmap();\r
44                                         iconLabel.Size = icon.Size;\r
45                                 }\r
46                         }\r
47                         nameLabel.Text = string.Format("{0} ({1})", pkg.Name, pkg.Version);\r
48                         nameLabel.Font = new Font(SystemFonts.CaptionFont.FontFamily, SystemFonts.CaptionFont.Size*1.25f);\r
49                         \r
50                         summaryTextBox.Text = pkg.Summary;\r
51                         \r
52                         if (isInstalledPackage) {\r
53                                 UninstallInformation uninstInfo = ((InstalledPackage) pkg).UninstallInfo;\r
54                                 \r
55                                 string instPath = null;\r
56                                 if (pkg.Type == InstallerType.ARCHIVE) {\r
57                                         instPath = Path.Combine(NaGet.Env.ArchiveProgramFiles, pkg.Name);\r
58                                 } else if (Directory.Exists(uninstInfo.InstallLocation)) {\r
59                                         instPath = uninstInfo.InstallLocation;\r
60                                 }\r
61                                 \r
62                                 statusLinkLabel.Links.Clear();\r
63                                 if (string.IsNullOrEmpty(instPath)) {\r
64                                         statusLinkLabel.Text = "情報が提供されていません";\r
65                                         statusLinkLabel.Enabled = false;\r
66                                 } else {\r
67                                         statusLinkLabel.Text = instPath;\r
68                                         statusLinkLabel.Enabled = true;\r
69                                         \r
70                                         addLinkToLinkLabel(statusLinkLabel, uninstInfo.InstallLocation, uninstInfo.InstallLocation);\r
71                                 }\r
72                         }\r
73                         urlLinkLabel.Links.Clear();\r
74                         if (pkg.Url != null) {\r
75                                 urlLinkLabel.Text = pkg.Url.Href;\r
76                                 urlLinkLabel.Links.Add(0, pkg.Url.Href.Length, pkg.Url.Href);\r
77                         } else {\r
78                                 urlLinkLabel.Text = string.Empty;\r
79                                 urlLinkLabel.Enabled = false;\r
80                                 urlLabel.Enabled = false;\r
81                         }\r
82                         tagsTextBox.Text = pkg.Tags;\r
83                         setLinkLabelFromLicense(licenseLinkLabel, pkg.License, (pkg.Url!=null)?pkg.Url.Href:string.Empty);\r
84                         setTypeValueLabel(typeValueLabel, pkg.Type);\r
85                         \r
86                         tabControl1.TabPages.Clear();\r
87                         if (pkg is InstalledPackage && string.IsNullOrEmpty(pkg.Version)) {\r
88                                 tabControl1.Visible = false;\r
89                         } else {\r
90                                 tabControl1.Visible = true;\r
91                                 foreach (Installer inst in pkg.Installer) {\r
92                                         TabPage page = new TabPage(string.Format("インストーラ その{0}", tabControl1.TabCount+1));\r
93                                         page.Dock = DockStyle.Fill;\r
94                                         \r
95                                         InstallerInfoForm instForm = new InstallerInfoForm();\r
96                                         instForm.SelectedObject = inst;\r
97                                         instForm.Dock = DockStyle.Fill;\r
98                                         page.Controls.Add(instForm);\r
99                                         \r
100                                         tabControl1.TabPages.Add(page);\r
101                                 }\r
102                         }\r
103                         \r
104                         this.Size = new Size(this.Size.Width, (tabControl1.TabCount>0)? 380 : 250);\r
105                 }\r
106                 \r
107                 /// <summary>\r
108                 /// インストーラの種類についてラベルにテキストを設定する\r
109                 /// </summary>\r
110                 /// <param name="typeValueLabel">設定先のラベル</param>\r
111                 /// <param name="instType">インストーラの種類</param>\r
112                 private static void setLinkLabelFromLicense(LinkLabel linklabel, string license, string publicSiteUrl)\r
113                 {\r
114                         linklabel.Enabled = true;\r
115                         switch (license ?? string.Empty) {\r
116                                 case "":\r
117                                         if (string.IsNullOrEmpty(publicSiteUrl)) {\r
118                                                 linklabel.Text = "フリーソフトウェア (無償)";\r
119                                                 linklabel.Enabled = false;\r
120                                         } else {\r
121                                                 linklabel.Text = "フリーソフトウェア (詳細は公式サイトなどで確認してください)";\r
122                                                 addLinkToLinkLabel(linklabel, "公式サイト", publicSiteUrl);\r
123                                         }\r
124                                         break;\r
125                                 #region オープンソースかつコピーレフト\r
126                                 case "GPL":\r
127                                         linklabel.Text = "GPL (オープンソース)";\r
128                                         addLinkToLinkLabel(linklabel, "GPL", @"http://www.opensource.jp/gpl/gpl.ja.html");\r
129                                         addLinkToLinkLabel(linklabel, "オープンソース", @"http://creativecommons.org/licenses/GPL/2.0/deed.ja");\r
130                                         break;\r
131                                 case "GPLv2":\r
132                                 case "GPLv2+":\r
133                                         linklabel.Text = "GPLv2 (オープンソース)";\r
134                                         addLinkToLinkLabel(linklabel, "GPLv2", @"http://www.gnu.org/licenses/gpl-2.0.txt");\r
135                                         break;\r
136                                 case "GPLv3":\r
137                                 case "GPLv3+":\r
138                                         linklabel.Text = "GPLv3 (オープンソース)";\r
139                                         addLinkToLinkLabel(linklabel, "GPLv3", @"http://www.gnu.org/licenses/gpl-3.0.txt");\r
140                                         break;\r
141                                 case "LGPL":\r
142                                         linklabel.Text = "LGPL (オープンソース)";\r
143                                         addLinkToLinkLabel(linklabel, "LGPL", @"http://www.opensource.jp/lesser/lgpl.ja.html");\r
144                                         addLinkToLinkLabel(linklabel, "オープンソース", @"http://creativecommons.org/licenses/LGPL/2.1/deed.ja");\r
145                                         break;\r
146                                 case "LGPLv2":\r
147                                 case "LGPLv2+":\r
148                                         linklabel.Text = "LGPLv2 (オープンソース)";\r
149                                         addLinkToLinkLabel(linklabel, "LGPLv2", @"http://www.gnu.org/licenses/lgpl-2.1.txt");\r
150                                         break;\r
151                                 case "LGPLv3":\r
152                                 case "LGPLv3+":\r
153                                         linklabel.Text = "LGPLv3 (オープンソース)";\r
154                                         addLinkToLinkLabel(linklabel, "LGPLv3", @"http://www.gnu.org/licenses/lgpl-3.0.txt");\r
155                                         break;\r
156                                 case "AGPLv3":\r
157                                 case "AGPLv3+":\r
158                                         linklabel.Text = "AGPLv3 (オープンソース)";\r
159                                         addLinkToLinkLabel(linklabel, "AGPLv3", @"http://www.gnu.org/licenses/agpl-3.0.txt");\r
160                                         break;\r
161                                 case "MPL":\r
162                                         linklabel.Text = "MPL (オープンソース)";\r
163                                         addLinkToLinkLabel(linklabel, "MPL", @"http://www.mozilla.org/MPL/MPL-1.1.html");\r
164                                         break;\r
165                                 case "CPL":\r
166                                         linklabel.Text = "CPL (オープンソース)";\r
167                                         linklabel.Links.Add(0, linklabel.Text.Length, @"http://sourceforge.jp/projects/opensource/wiki/licenses%2FCommon_Public_License");\r
168                                         break;\r
169                                 case "Artistic":\r
170                                         linklabel.Text = "Artisticライセンス (オープンソース)";\r
171                                         addLinkToLinkLabel(linklabel, "Artisticライセンス", @"http://www.perlfoundation.org/artistic_license_2_0");\r
172                                         break;\r
173                                 #endregion\r
174                                 #region オープンソースだが、コピーレフトでない著名なライセンス\r
175                                 case "MIT":\r
176                                         linklabel.Text = "MITライセンス (オープンソース)";\r
177                                         addLinkToLinkLabel(linklabel, "MITライセンス", @"http://www.opensource.org/licenses/mit-license.php");\r
178                                         break;\r
179                                 case "BSD":\r
180                                         linklabel.Text = "修正済BSDライセンス (オープンソース)";\r
181                                         addLinkToLinkLabel(linklabel, "BSDライセンス", @"http://www.opensource.org/licenses/bsd-license.php");\r
182                                         break;\r
183                                 case "Apache":\r
184                                 case "ASLv2":\r
185                                 case "ASLv2+":\r
186                                         linklabel.Text = "Apacheライセンス (オープンソース)";\r
187                                         addLinkToLinkLabel(linklabel, "Apacheライセンス", @"http://www.apache.org/licenses/LICENSE-2.0.txt");\r
188                                         break;\r
189                                 case "zlib":\r
190                                         linklabel.Text = "zlibライセンス";\r
191                                         linklabel.Links.Add(0, linklabel.Text.Length, @"http://opentechpress.jp/docs/licenses/zlib-license.shtml");\r
192                                         break;\r
193                                 #endregion\r
194                                 case "WTFPL":\r
195                                 case "WTFPLv2":\r
196                                         linklabel.Text = "WTFPL";\r
197                                         linklabel.Links.Add(0, linklabel.Text.Length, @"http://sam.zoy.org/wtfpl/");\r
198                                         break;\r
199                                 case "NYSL":\r
200                                         linklabel.Text = "煮るなり焼くなり好きにしろライセンス";\r
201                                         linklabel.Links.Add(0, linklabel.Text.Length, @"http://www.kmonos.net/nysl/NYSL_withfaq.TXT");\r
202                                         break;\r
203                                 case "Public Domain":\r
204                                         linklabel.Text = "パブリックドメイン (著作権帰属なし)";\r
205                                         linklabel.Links.Add(0, linklabel.Text.Length, @"http://e-words.jp/w/E38391E38396E383AAE38383E382AFE38389E383A1E382A4E383B3.html");\r
206                                         break;\r
207                                 default:\r
208                                         if (license.StartsWith("http://")) {\r
209                                                 linklabel.Text = license;\r
210                                                 linklabel.Links.Add(0, license.Length, license);\r
211                                         } else {\r
212                                                 linklabel.Text = license;\r
213                                                 linklabel.Links.Add(0, 0, string.Empty);\r
214                                         }\r
215                                         break;\r
216                         }\r
217                         \r
218                 }\r
219                 \r
220                 /// <summary>\r
221                 /// インストーラの種類についてラベルにテキストを設定する\r
222                 /// </summary>\r
223                 /// <param name="typeValueLabel">設定先のラベル</param>\r
224                 /// <param name="instType">インストーラの種類</param>\r
225                 private static void setTypeValueLabel(Label typeValueLabel, InstallerType instType)\r
226                 {\r
227                         switch (instType) {\r
228                                 case InstallerType.ARCHIVE:\r
229                                         typeValueLabel.Text = "自己解凍書庫または書庫";\r
230                                         break;\r
231                                 case InstallerType.EXEC_INSTALLER:\r
232                                         typeValueLabel.Text = "インストーラ形式";\r
233                                         break;\r
234                                 case InstallerType.MSI_PACKAGE:\r
235                                         typeValueLabel.Text = "MSIファイル形式";\r
236                                         break;\r
237                                 default:\r
238                                         typeValueLabel.Text = "不明なインストール形式(インストール不能)";\r
239                                         break;\r
240                         }\r
241                 }\r
242                 \r
243                 /// <summary>\r
244                 /// リンクラベルのリンク挿入便利メソッド\r
245                 /// </summary>\r
246                 /// <param name="linkLabel">対象リンクラベル</param>\r
247                 /// <param name="word">リンク対象文字列</param>\r
248                 /// <param name="link">リンクオブジェクト</param>\r
249                 private static void addLinkToLinkLabel(LinkLabel linkLabel, string word, string link)\r
250                 {\r
251                         int pos = linkLabel.Text.IndexOf(word, 0);\r
252                         if (pos >= 0) {\r
253                                 linkLabel.Links.Add(pos, word.Length, link);\r
254                         } else throw new ArgumentException(string.Format("label text \"{0}\" does not contain \"{1}\"!", linkLabel.Text, word));\r
255                 }\r
256                 \r
257                 private void linkLabelLinkClicked(object sender, LinkLabelLinkClickedEventArgs e)\r
258                 {\r
259                         string linkData = e.Link.LinkData.ToString();\r
260                         \r
261                         if (Directory.Exists(linkData)) {\r
262                                 e.Link.Visited = true;\r
263                                 System.Diagnostics.Process.Start(e.Link.LinkData.ToString());\r
264                         } else if (System.Text.RegularExpressions.Regex.IsMatch(linkData, @"^(https?|file)://")) {\r
265                                 e.Link.Visited = true;\r
266                                 try {\r
267                                         System.Diagnostics.Process.Start(e.Link.LinkData.ToString());\r
268                                 } catch (System.ComponentModel.Win32Exception) {\r
269                                         MessageBox.Show(string.Format("{0}を開くのに失敗しました。", e.Link.LinkData), "ブラウザ起動エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
270                                 }\r
271                         }\r
272                 }\r
273         }\r
274 }\r