OSDN Git Service

AppliStation-GUI,インストール除外リストの設定を設定ダイアログに追加
[applistation/AppliStation.git] / AppliStation / AppliStation.Util / ToolStripPetitLauncherMenuItem.cs
index b196ae8..f8c5097 100644 (file)
@@ -6,6 +6,8 @@ using System.Windows.Forms;
 using System.Diagnostics;\r
 using System.Text;\r
 \r
+using System.ComponentModel;\r
+\r
 namespace AppliStation.Util\r
 {\r
        public class ToolStripPetitLauncherMenuItem : ToolStripMenuItem\r
@@ -14,9 +16,12 @@ namespace AppliStation.Util
                \r
                private string baseFolderPath;\r
                \r
+               private Form invokerForm;\r
+               \r
                /// <summary>\r
-               /// 親フォルダ\r
+               /// 親フォルダ\r
                /// </summary>\r
+               [ReadOnly(true)]\r
                public string BaseFolderPath {\r
                        get { return baseFolderPath; }\r
                        set {\r
@@ -26,18 +31,25 @@ namespace AppliStation.Util
                }\r
                \r
                /// <summary>\r
+               /// 別スレッドからInvokeするときのオブジェクト\r
+               /// </summary>\r
+               public Form InvokerForm {\r
+                       get { return invokerForm; }\r
+                       set { invokerForm = value; }\r
+               }\r
+               \r
+               /// <summary>\r
                /// ドロップアイテムの(再)生成を行う\r
                /// </summary>\r
                public void BuildItems()\r
                {\r
                        if (thread != null) {\r
-                               if (! thread.Join(1000)) {\r
+                               if (! thread.Join(500)) {\r
                                        thread.Interrupt();\r
                                }\r
                                thread = null;\r
                        }\r
                        \r
-                       DropDownItems.Clear();\r
                        if (Directory.Exists(baseFolderPath)) {\r
                                thread = new Thread(new ThreadStart(buildItems));\r
                                thread.Start();\r
@@ -49,8 +61,8 @@ namespace AppliStation.Util
                private delegate int ToolStripItemCollection_AddDelegate(ToolStripItem item);\r
                private void _addToItemsInv(ToolStripItem item)\r
                {\r
-                       if (Parent.InvokeRequired) {\r
-                               Parent.Invoke(new ToolStripItemCollection_AddDelegate(DropDownItems.Add), item);\r
+                       if (InvokerForm.InvokeRequired) {\r
+                               InvokerForm.Invoke(new ToolStripItemCollection_AddDelegate(DropDownItems.Add), item);\r
                        } else {\r
                                DropDownItems.Add(item);\r
                        }\r
@@ -58,38 +70,67 @@ namespace AppliStation.Util
                private delegate void ToolStripItemCollection_InsertDelegate(int index, ToolStripItem item);\r
                private void _insertToItemsInv(int index, ToolStripItem item)\r
                {\r
-                       if (Parent.InvokeRequired) {\r
-                               Parent.Invoke(new ToolStripItemCollection_InsertDelegate(DropDownItems.Insert), index, item);\r
+                       if (InvokerForm.InvokeRequired) {\r
+                               InvokerForm.Invoke(new ToolStripItemCollection_InsertDelegate(DropDownItems.Insert), index, item);\r
                        } else {\r
                                DropDownItems.Insert(index, item);\r
                        }\r
                }\r
                \r
+               private void _insertItemFor(string filepath, ref bool cmdIsAdded, string basedir)\r
+               {\r
+                       string extension = Path.GetExtension(filepath).ToLower();\r
+                       \r
+                       string dirname = Path.GetDirectoryName(filepath);\r
+                       string itemtext = NaGet.Utils.GetRelativePath(basedir, filepath);\r
+#if DEBUG\r
+Debug.Assert(! itemtext.Contains(".."), string.Format("{2} -- base:{0}, dirname:{1}", basedir, dirname, itemtext));\r
+#endif\r
+                       \r
+                       if (extension == ".exe") {\r
+                               switch (NaGet.InteropServices.PEFileInfoUtils.GetPEFileType(filepath)) {\r
+                                       case NaGet.InteropServices.PEFileType.WinGUI:\r
+                                               _addToItemsInv(CreateMenuItemForFile(filepath, itemtext));\r
+                                               break;\r
+                                       case NaGet.InteropServices.PEFileType.WinConsole:\r
+                                       case NaGet.InteropServices.PEFileType.MSDosCom:\r
+                                               if (! cmdIsAdded) {\r
+                                                       _insertToItemsInv(0, CreateMenuItemForCmdAt(basedir));\r
+                                                       cmdIsAdded = true;\r
+                                               }\r
+                                               break;\r
+                               }\r
+                       } else if ((extension == ".bat") || (extension == ".lnk")) {\r
+                               _addToItemsInv(CreateMenuItemForFile(filepath, itemtext));\r
+                       }\r
+               }\r
+               \r
                        \r
                private void buildItems()\r
                {\r
+                       DropDownItems.Clear();\r
+                       \r
                        try {\r
-                               string folderPath = baseFolderPath;\r
+                               string folderPath = Path.GetFullPath(baseFolderPath);\r
                                ToolStripSeparator sep = new ToolStripSeparator();\r
                                bool cmdIsAdded = false;\r
                                \r
                                _addToItemsInv(CreateMenuItemForFolder(folderPath));\r
                                _addToItemsInv(sep);\r
                                \r
-                               if (Directory.Exists(folderPath)) {\r
-                                       foreach (string exeFile in Directory.GetFiles(folderPath, "*.exe")) {\r
+                               if (Directory.Exists(folderPath)) {                                     \r
+                                       foreach (string filepath in Directory.GetFiles(folderPath)) {\r
                                                if (baseFolderPath != folderPath) return; // 途中でなんか操作されているならば終了\r
                                                \r
-                                               switch (NaGet.InteropServices.PEFileInfoUtils.GetPEFileType(exeFile)) {\r
-                                                       case NaGet.InteropServices.PEFileInfoUtils.PEFileType.WinGUI:\r
-                                                               _addToItemsInv(CreateMenuItemForFile(exeFile));\r
-                                                               break;\r
-                                                       case NaGet.InteropServices.PEFileInfoUtils.PEFileType.WinConsole:\r
-                                                               if (! cmdIsAdded) {\r
-                                                                       _insertToItemsInv(0, CreateMenuItemForCmdAt(folderPath));\r
-                                                                       cmdIsAdded = true;\r
-                                                               }\r
-                                                               break;\r
+                                               _insertItemFor(filepath, ref cmdIsAdded, folderPath);\r
+                                       }\r
+                                       \r
+                                       string bindir = Path.Combine(folderPath, "bin");\r
+                                       if (Directory.Exists(bindir)) {\r
+                                               foreach (string filepath in Directory.GetFiles(bindir)) {\r
+                                                       if (baseFolderPath != folderPath) return; // 途中でなんか操作されているならば終了\r
+                                                       \r
+                                                       _insertItemFor(filepath, ref cmdIsAdded, folderPath);\r
                                                }\r
                                        }\r
                                }\r
@@ -99,14 +140,15 @@ namespace AppliStation.Util
                #endregion\r
                \r
                /// <summary>\r
-               /// 与えられたフォルダを開くメニューアイテムを生成する\r
+               /// ä¸\8eã\81\88ã\82\89ã\82\8cã\81\9fã\83\95ã\82©ã\83«ã\83\80ã\83¼ã\82\92é\96\8bã\81\8fã\83¡ã\83\8bã\83¥ã\83¼ã\82¢ã\82¤ã\83\86ã\83 ã\82\92ç\94\9fæ\88\90ã\81\99ã\82\8b\r
                /// </summary>\r
                /// <param name="folderPath"></param>\r
                /// <returns></returns>\r
-               public static ToolStripMenuItem CreateMenuItemForFolder(string folderPath)\r
+               private static ToolStripMenuItem CreateMenuItemForFolder(string folderPath)\r
                {\r
-                       ToolStripMenuItem item = CreateMenuItemForFile(folderPath);\r
-                       item.Text = "フォルダを開く(&O)";\r
+                       ToolStripMenuItem item = CreateMenuItemForFile(folderPath, "フォルダーを開く(&O)");\r
+                       item.Image = GUIUtils.ShellIconForFolder.ToBitmap();\r
+                       item.ShowShortcutKeys = true;\r
                        return item;\r
                }\r
 \r
@@ -115,12 +157,11 @@ namespace AppliStation.Util
                /// </summary>\r
                /// <param name="filePath">ファイルパス</param>\r
                /// <returns>生成されたメニューアイテム</returns>\r
-               public static ToolStripMenuItem CreateMenuItemForFile(string filePath)\r
+               private static ToolStripMenuItem CreateMenuItemForFile(string filePath, string text)\r
                {\r
                        ToolStripMenuItem item = new ToolStripMenuItem();\r
-                       string fileName = Path.GetFileName(filePath);\r
                        \r
-                       item.Text = fileName;\r
+                       item.Text = text;\r
                        item.ShowShortcutKeys = false;\r
                        item.Tag = filePath;\r
                        item.Click += new System.EventHandler(menuItemForFileClicked);\r
@@ -130,7 +171,7 @@ namespace AppliStation.Util
                                \r
                                StringBuilder sb = new StringBuilder();\r
                                sb.AppendFormat("場所: {0}", Path.GetDirectoryName(filePath));\r
-                               try {\r
+                               if (File.Exists(filePath)) {\r
                                        FileVersionInfo vInfo = FileVersionInfo.GetVersionInfo(filePath);\r
                                        \r
                                        if (! string.IsNullOrEmpty(vInfo.FileDescription))\r
@@ -139,7 +180,7 @@ namespace AppliStation.Util
                                                sb.AppendFormat("\r\n会社: {0}", vInfo.CompanyName);\r
                                        if (! string.IsNullOrEmpty(vInfo.FileVersion))\r
                                                sb.AppendFormat("\r\nファイルバージョン: {0}", vInfo.FileVersion);\r
-                               } catch (Exception) {}\r
+                               }\r
                                item.ToolTipText = sb.ToString();\r
                        } else {\r
                                item.ToolTipText = filePath;\r
@@ -159,15 +200,19 @@ namespace AppliStation.Util
                        \r
                        ProcessStartInfo procInfo = new ProcessStartInfo(item.Tag.ToString());\r
                        procInfo.WorkingDirectory = Path.GetDirectoryName(procInfo.FileName);\r
-                       Process.Start(procInfo);\r
+                       try {\r
+                               Process.Start(procInfo);\r
+                       } catch (Win32Exception ex) {\r
+                               MessageBox.Show(ex.Message, procInfo.FileName, MessageBoxButtons.OK, MessageBoxIcon.Error);\r
+                       }\r
                }\r
                \r
                /// <summary>\r
-               /// 与えられたフォルダのところでコマンドプロンプトを開く。\r
+               /// ä¸\8eã\81\88ã\82\89ã\82\8cã\81\9fã\83\95ã\82©ã\83«ã\83\80ã\83¼ã\81®ã\81¨ã\81\93ã\82\8dã\81§ã\82³ã\83\9eã\83³ã\83\89ã\83\97ã\83­ã\83³ã\83\97ã\83\88ã\82\92é\96\8bã\81\8fã\80\82\r
                /// </summary>\r
-               /// <param name="folderPath">カレントフォルダ及びパスに追加されるフォルダ</param>\r
+               /// <param name="folderPath">カレントフォルダー及びパスに追加されるフォルダー</param>\r
                /// <returns>生成されたメニューアイテム</returns>\r
-               public static ToolStripMenuItem CreateMenuItemForCmdAt(string folderPath)\r
+               private static ToolStripMenuItem CreateMenuItemForCmdAt(string folderPath)\r
                {\r
                        string cmdPath = Environment.GetEnvironmentVariable("comspec");\r
                        if (string.IsNullOrEmpty(cmdPath) || (!File.Exists(cmdPath)))\r