OSDN Git Service

AppliStation-GUI,パッケージリストの列で「ライセンス」と「インストールした日」を追加(デフォルト非表示)。
[applistation/AppliStation.git] / AppliStation / AppliStation.PackageInfo / PackageDescripter.cs
1 using System;\r
2 using System.Windows.Forms;\r
3 using System.Collections.Generic;\r
4 using System.Text;\r
5 using System.Text.RegularExpressions;\r
6 using NaGet.Packages;\r
7 \r
8 namespace AppliStation.PackageInfo\r
9 {\r
10         /// <summary>\r
11         /// パッケージ情報アクセスのための便利メソッド群\r
12         /// </summary>\r
13         public sealed class PackageDescripter\r
14         {\r
15                 /// <summary>\r
16                 /// インストーラタイプに対応する文字列を生成して返す\r
17                 /// </summary>\r
18                 /// <param name="instType">インストーラタイプ</param>\r
19                 /// <returns>ラベル文字列</returns>\r
20                 public static string GetInstallerTypeLabel(InstallerType instType)\r
21                 {\r
22                         switch (instType) {\r
23                                 case InstallerType.ARCHIVE:\r
24                                         return "自己解凍書庫または書庫";\r
25                                 case InstallerType.EXEC_INSTALLER:\r
26                                         return "インストーラー形式";\r
27                                 case InstallerType.MSI_PACKAGE:\r
28                                         return "MSIファイル形式";\r
29                                 case InstallerType.ITSELF:\r
30                                         return "単体の実行ファイル";\r
31                                 default:\r
32                                         return "不明なインストール形式(インストール不能)";\r
33                         }\r
34                 }\r
35                 \r
36                 /// <summary>\r
37                 /// OSのラベル文字列を生成する\r
38                 /// </summary>\r
39                 /// <param name="platform">プラットフォーム</param>\r
40                 /// <returns>platformの示すOSバージョン文字列</returns>\r
41                 public static string GetOSValueLabel(Platform platform)\r
42                 {\r
43                         if (platform == null || string.IsNullOrEmpty(platform.Os)) {\r
44                                 return "任意のWindows";\r
45                         } else {\r
46                                 return platform.Os.Replace("WIN", string.Empty);\r
47                         }\r
48                 }\r
49                 \r
50                 /// <summary>\r
51                 /// アーキテクチャのラベル文字列を生成する\r
52                 /// </summary>\r
53                 /// <param name="platform">プラットフォーム</param>\r
54                 /// <returns>platformの示すアーキテクチャ文字列</returns>\r
55                 public static string GetArchValueLabel(Platform platform)\r
56                 {\r
57                         switch ((platform != null)? platform.Arch : System.Reflection.ProcessorArchitecture.X86) {\r
58                                 case System.Reflection.ProcessorArchitecture.Amd64:\r
59                                         return "x64 (64ビット環境)";\r
60                                 case System.Reflection.ProcessorArchitecture.IA64:\r
61                                         return "Itanium";\r
62                                 case System.Reflection.ProcessorArchitecture.None:\r
63                                 case System.Reflection.ProcessorArchitecture.MSIL:\r
64                                         return "アーキテクチャ依存なし";\r
65                                 case System.Reflection.ProcessorArchitecture.X86:\r
66                                 default:\r
67                                         return "x86 (32ビット環境)";\r
68                         }\r
69                 }\r
70                 \r
71                 /// <summary>\r
72                 /// ライセンスのラベル文字列とリンク情報を生成する\r
73                 /// </summary>\r
74                 /// <param name="pkg">パッケージ</param>\r
75                 /// <param name="text">ラベル文字列</param>\r
76                 /// <param name="links">リンク文字列</param>\r
77                 public static void GetLicenseValueLabel(Package pkg, out string text, out LinkLabel.Link[] links)\r
78                 {\r
79                         string license = pkg.License;\r
80                         string publicSiteUrl = (pkg.Url != null)? pkg.Url.Href : null;\r
81                         \r
82                         List<LinkLabel.Link> linkArray = new List<LinkLabel.Link>();\r
83                         \r
84                         switch (license ?? string.Empty) {\r
85                                 case "":\r
86                                         if (string.IsNullOrEmpty(publicSiteUrl)) {\r
87                                                 text = "フリーソフトウェア";\r
88                                         } else {\r
89                                                 text = "フリーソフトウェア (詳細は公式サイトなどで確認してください)";\r
90                                                 linkArray.Add(createLinkToLinkLabel(text, "公式サイト", publicSiteUrl));\r
91                                         }\r
92                                         break;\r
93                                 #region オープンソースかつコピーレフト\r
94                                 case "GPL":\r
95                                 case "GPLv2":\r
96                                 case "GPLv2+":\r
97                                 case "GPLv3":\r
98                                 case "GPLv3+":\r
99                                         text = string.Format("{0} (オープンソース)", license);\r
100                                         if (license.StartsWith("GPLv3")) {\r
101                                                 linkArray.Add(new LinkLabel.Link(0, license.Length, @"http://www.gnu.org/licenses/gpl-3.0.html"));\r
102                                         } else {\r
103                                                 linkArray.Add(new LinkLabel.Link(0, license.Length, @"http://www.gnu.org/licenses/gpl-2.0.html"));\r
104                                                 linkArray.Add(createLinkToLinkLabel(text, "オープンソース",\r
105                                                                                 @"http://creativecommons.org/licenses/GPL/2.0/deed.ja"));\r
106                                         }\r
107                                         break;\r
108                                 case "LGPL":\r
109                                 case "LGPLv2":\r
110                                 case "LGPLv2+":\r
111                                 case "LGPLv3":\r
112                                 case "LGPLv3+":\r
113                                         text = string.Format("{0} (オープンソース)", license);\r
114                                         if (license.StartsWith("GPLv3")) {\r
115                                                 linkArray.Add(new LinkLabel.Link(0, license.Length, @"http://www.gnu.org/licenses/lgpl-3.0.html"));\r
116                                         } else {\r
117                                                 linkArray.Add(new LinkLabel.Link(0, license.Length, @"http://www.gnu.org/licenses/lgpl-2.1.html"));\r
118                                                 linkArray.Add(createLinkToLinkLabel(text, "オープンソース",\r
119                                                                                 @"http://creativecommons.org/licenses/LGPL/2.1/deed.ja"));\r
120                                         }\r
121                                         break;\r
122                                 case "AGPLv3":\r
123                                 case "AGPLv3+":\r
124                                         text = string.Format("{0} (オープンソース)", license);\r
125                                         linkArray.Add(new LinkLabel.Link(0, license.Length, @"http://www.gnu.org/licenses/agpl-3.0.txt"));\r
126                                         break;\r
127                                 case "MPL":\r
128                                 case "MPLv1":\r
129                                 case "MPLv1+":\r
130                                         text = "MPL (オープンソース)";\r
131                                         linkArray.Add(new LinkLabel.Link(0, "MPL".Length, @"http://www.mozilla.org/MPL/MPL-1.1.html"));\r
132                                         break;\r
133                                 case "CPL":\r
134                                         text = "CPL (オープンソース)";\r
135                                         linkArray.Add(new LinkLabel.Link(0, "CPL".Length, @"http://opensource.org/licenses/cpl1.0.php"));\r
136                                         break;\r
137                                 case "Artistic":\r
138                                         text = "Artisticライセンス (オープンソース)";\r
139                                         linkArray.Add(new LinkLabel.Link(0, "Artisticライセンス".Length, @"http://www.perlfoundation.org/artistic_license_2_0"));\r
140                                         break;\r
141                                 #endregion\r
142                                 #region オープンソースだが、コピーレフトでない著名なライセンス\r
143                                 case "MIT":\r
144                                         text = "MITライセンス (オープンソース)";\r
145                                         linkArray.Add(new LinkLabel.Link(0, "MITライセンス".Length, @"http://www.opensource.org/licenses/mit-license.php"));\r
146                                         break;\r
147                                 case "BSD":\r
148                                         text = "修正済BSDライセンス (オープンソース)";\r
149                                         linkArray.Add(new LinkLabel.Link(0, "修正済BSDライセンス".Length, @"http://www.opensource.org/licenses/bsd-license.php"));\r
150                                         break;\r
151                                 case "Apache":\r
152                                 case "ASLv2":\r
153                                 case "ASLv2+":\r
154                                         text = "Apacheライセンス (オープンソース)";\r
155                                         linkArray.Add(new LinkLabel.Link(0, "Apacheライセンス".Length, @"http://www.apache.org/licenses/LICENSE-2.0.txt"));\r
156                                         break;\r
157                                 case "zlib":\r
158                                         text = "zlibライセンス (オープンソース)";\r
159                                         linkArray.Add(new LinkLabel.Link(0, "zlibライセンス".Length, @"http://sourceforge.jp/projects/opensource/wiki/licenses%2Fzlib_libpng_license"));\r
160                                         break;\r
161                                 #endregion\r
162                                 #region 実質Public Domainなライセンス\r
163                                 case "WTFPL":\r
164                                 case "WTFPLv2":\r
165                                         text = "WTFPL";\r
166                                         linkArray.Add(new LinkLabel.Link(0, text.Length, @"http://sam.zoy.org/wtfpl/"));\r
167                                         break;\r
168                                 case "NYSL":\r
169                                         text = "煮るなり焼くなり好きにしろライセンス";\r
170                                         linkArray.Add(new LinkLabel.Link(0, text.Length, @"http://www.kmonos.net/nysl/NYSL_withfaq.TXT"));\r
171                                         break;\r
172                                 case "Public Domain":\r
173                                         text = "パブリックドメイン (著作権帰属なし)";\r
174                                         break;\r
175                                 #endregion\r
176                                 default:\r
177                                         if (license.StartsWith("http://")) {\r
178                                                 text = license;\r
179                                                 linkArray.Add(new LinkLabel.Link(0, license.Length, license));\r
180                                         } else {\r
181                                                 Regex regex = new Regex(@"^(?<msg>.*)<(?<url>https?://.*)>$", RegexOptions.IgnoreCase);\r
182                                                 Match match = regex.Match(license);\r
183                                                 \r
184                                                 if (match.Success) {\r
185                                                         text = match.Groups["msg"].Value;\r
186                                                         linkArray.Add(new LinkLabel.Link(0, text.Length, match.Groups["url"].Value));\r
187                                                 } else {\r
188                                                         text = license;\r
189                                                 }\r
190                                         }\r
191                                         break;\r
192                         }\r
193                         \r
194                         links = linkArray.ToArray();\r
195                 }\r
196                 \r
197                 /// <summary>\r
198                 /// ライセンスのラベル文字列を生成する\r
199                 /// </summary>\r
200                 /// <param name="pkg">パッケージ</param>\r
201                 /// <returns>ラベル文字列</returns>\r
202                 public static string GetLicenseValueLabel(Package pkg)\r
203                 {\r
204                         string label;\r
205                         LinkLabel.Link[] links;\r
206                         \r
207                         GetLicenseValueLabel(pkg, out label, out links);\r
208                         \r
209                         return label;\r
210                 }\r
211                 \r
212                 /// <summary>\r
213                 /// リンクラベルのリンク生成便利メソッド\r
214                 /// </summary>\r
215                 /// <param name="text">ラベルテキスト</param>\r
216                 /// <param name="word">リンク対象文字列</param>\r
217                 /// <param name="link">リンクオブジェクト</param>\r
218                 private static LinkLabel.Link createLinkToLinkLabel(string text, string word, string link)\r
219                 {\r
220                         int pos = text.IndexOf(word, 0);\r
221                         if (pos >= 0) {\r
222                                 return new LinkLabel.Link(pos, word.Length, link);\r
223                         } else throw new ArgumentException(string.Format("label text \"{0}\" does not contain \"{1}\"!", text, word));\r
224                 }\r
225         }\r
226 }\r