OSDN Git Service

AppliStation-GUI,無人インストールの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.GUIUtils.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 = "情報が提供されていません";\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("インストーラ その{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 = "フリーソフトウェア (無償)";\r
110                                                 linklabel.Enabled = false;\r
111                                         } else {\r
112                                                 linklabel.Text = "フリーソフトウェア (詳細は公式サイトなどで確認してください)";\r
113                                                 addLinkToLinkLabel(linklabel, "公式サイト", publicSiteUrl);\r
114                                         }\r
115                                         break;\r
116                                 #region オープンソースかつコピーレフト\r
117                                 case "GPL":\r
118                                         linklabel.Text = "GPL (オープンソース)";\r
119                                         addLinkToLinkLabel(linklabel, "GPL", @"http://www.gnu.org/licenses/gpl.ja.html");\r
120                                         addLinkToLinkLabel(linklabel, "オープンソース", @"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 (オープンソース)";\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 (オープンソース)";\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 (オープンソース)";\r
134                                         addLinkToLinkLabel(linklabel, "LGPL", @"http://www.gnu.org/licenses/lgpl.ja.html");\r
135                                         addLinkToLinkLabel(linklabel, "オープンソース", @"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 (オープンソース)";\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 (オープンソース)";\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 (オープンソース)";\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 (オープンソース)";\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 オープンソースだが、コピーレフトでない\r
157                                 case "MIT":\r
158                                         linklabel.Text = "MITライセンス (オープンソース)";\r
159                                         addLinkToLinkLabel(linklabel, "MITライセンス", @"http://www.opensource.org/licenses/mit-license.php");\r
160                                         break;\r
161                                 case "BSD":\r
162                                         linklabel.Text = "修正済BSDライセンス (オープンソース)";\r
163                                         addLinkToLinkLabel(linklabel, "BSDライセンス", @"http://www.opensource.org/licenses/bsd-license.php");\r
164                                         break;\r
165                                 case "Artistic":\r
166                                         linklabel.Text = "Artisticライセンス (オープンソース)";\r
167                                         addLinkToLinkLabel(linklabel, "Artisticライセンス", @"http://www.perlfoundation.org/artistic_license_2_0");\r
168                                         break;\r
169                                 #endregion\r
170                                 case "NYSL":\r
171                                         linklabel.Text = "煮るなり焼くなり好きにしろライセンス";\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 = "パブリックドメイン (著作権帰属なし)";\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 = "自己解凍書庫または書庫";\r
196                                         break;\r
197                                 case InstallerType.EXEC_INSTALLER:\r
198                                         typeValueLabel.Text = "インストーラ形式";\r
199                                         break;\r
200                                 case InstallerType.MSI_PACKAGE:\r
201                                         typeValueLabel.Text = "MSIファイル形式";\r
202                                         break;\r
203                                 default:\r
204                                         typeValueLabel.Text = "不明なインストール形式(インストール不能)";\r
205                                         break;\r
206                         }\r
207                 }\r
208                 \r
209                 /// <summary>\r
210                 /// リンクラベルのリンク挿入便利メソッド\r
211                 /// </summary>\r
212                 /// <param name="linkLabel">対象リンクラベル</param>\r
213                 /// <param name="word">リンク対象文字列</param>\r
214                 /// <param name="link">リンクオブジェクト</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