2020-03-28 08:43:16 +13:00
|
|
|
|
using IWshRuntimeLibrary;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2020-03-30 06:26:45 +13:00
|
|
|
|
using System.Security;
|
2020-03-28 08:43:16 +13:00
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using SystemTrayMenu.Handler;
|
2020-03-28 12:42:32 +13:00
|
|
|
|
using SystemTrayMenu.Utilities;
|
2020-03-28 08:43:16 +13:00
|
|
|
|
using TAFactory.IconPack;
|
2020-03-28 12:42:32 +13:00
|
|
|
|
using Menu = SystemTrayMenu.UserInterface.Menu;
|
2020-03-28 08:43:16 +13:00
|
|
|
|
|
|
|
|
|
namespace SystemTrayMenu.DataClasses
|
|
|
|
|
{
|
2020-03-30 06:26:45 +13:00
|
|
|
|
internal class RowData : IDisposable
|
2020-03-28 08:43:16 +13:00
|
|
|
|
{
|
2020-04-01 07:42:49 +13:00
|
|
|
|
internal event EventHandler<RowData> OpenMenu;
|
2020-03-30 00:20:18 +13:00
|
|
|
|
internal BackgroundWorker Reading = new BackgroundWorker();
|
|
|
|
|
internal FileInfo FileInfo;
|
|
|
|
|
internal Menu SubMenu;
|
|
|
|
|
internal bool IsSelected;
|
|
|
|
|
internal bool IsSelectedByKeyboard;
|
|
|
|
|
internal bool ContainsMenu;
|
|
|
|
|
internal bool IsContextMenuOpen;
|
|
|
|
|
internal bool IsResolvedLnk;
|
|
|
|
|
internal bool IsLoading = false;
|
|
|
|
|
internal bool RestartLoading = false;
|
|
|
|
|
internal bool HiddenEntry;
|
|
|
|
|
internal string TargetFilePath;
|
|
|
|
|
internal string TargetFilePathOrig;
|
|
|
|
|
internal int RowIndex;
|
2020-04-01 07:42:49 +13:00
|
|
|
|
private string WorkingDirectory;
|
|
|
|
|
private string Arguments;
|
|
|
|
|
private string Text;
|
2020-03-28 08:43:16 +13:00
|
|
|
|
private readonly WaitMenuOpen waitMenuOpen = new WaitMenuOpen();
|
2020-03-30 12:19:11 +13:00
|
|
|
|
private Icon Icon = null;
|
|
|
|
|
private bool diposeIcon = true;
|
2020-03-28 08:43:16 +13:00
|
|
|
|
private bool isDisposed = false;
|
|
|
|
|
|
2020-03-30 06:26:45 +13:00
|
|
|
|
internal RowData()
|
2020-03-28 08:43:16 +13:00
|
|
|
|
{
|
|
|
|
|
Reading.WorkerSupportsCancellation = true;
|
|
|
|
|
waitMenuOpen.DoOpen += WaitMenuOpen_DoOpen;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-30 06:26:45 +13:00
|
|
|
|
internal void SetText(string text)
|
2020-03-28 08:43:16 +13:00
|
|
|
|
{
|
2020-03-30 06:26:45 +13:00
|
|
|
|
if (text.Length > MenuDefines.LengthMax)
|
2020-03-28 08:43:16 +13:00
|
|
|
|
{
|
|
|
|
|
text = $"{text.Substring(0, MenuDefines.LengthMax)}...";
|
|
|
|
|
}
|
|
|
|
|
Text = text;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-30 06:26:45 +13:00
|
|
|
|
internal void SetData(RowData data, DataGridView dgv)
|
|
|
|
|
{
|
|
|
|
|
data.RowIndex = dgv.Rows.Add();
|
|
|
|
|
DataGridViewRow row = dgv.Rows[data.RowIndex];
|
|
|
|
|
|
|
|
|
|
if (Icon == null)
|
|
|
|
|
{
|
2020-04-11 10:22:45 +12:00
|
|
|
|
Icon = Properties.Resources.WhiteTransparency;
|
2020-03-30 06:26:45 +13:00
|
|
|
|
}
|
|
|
|
|
DataGridViewImageCell cellIcon =
|
|
|
|
|
(DataGridViewImageCell)row.Cells[0];
|
|
|
|
|
|
|
|
|
|
if (HiddenEntry)
|
|
|
|
|
{
|
|
|
|
|
cellIcon.Value = IconReader.AddIconOverlay(data.Icon,
|
|
|
|
|
Properties.Resources.WhiteTransparency);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
cellIcon.Value = data.Icon;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DataGridViewTextBoxCell cellName =
|
|
|
|
|
(DataGridViewTextBoxCell)row.Cells[1];
|
|
|
|
|
cellName.Value = data.Text;
|
|
|
|
|
|
|
|
|
|
row.Tag = data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal bool ReadIcon(bool isDirectory, ref string resolvedLnkPath)
|
|
|
|
|
{
|
|
|
|
|
bool isLnkDirectory = false;
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(TargetFilePath))
|
|
|
|
|
{
|
|
|
|
|
Log.Info($"TargetFilePath from {resolvedLnkPath} empty");
|
|
|
|
|
}
|
|
|
|
|
else if (isDirectory)
|
|
|
|
|
{
|
|
|
|
|
Icon = IconReader.GetFolderIcon(TargetFilePath,
|
|
|
|
|
IconReader.FolderType.Closed, false);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
bool handled = false;
|
|
|
|
|
string fileExtension = Path.GetExtension(TargetFilePath);
|
|
|
|
|
|
|
|
|
|
if (fileExtension == ".lnk")
|
|
|
|
|
{
|
|
|
|
|
handled = SetLnk(ref isLnkDirectory,
|
|
|
|
|
ref resolvedLnkPath);
|
|
|
|
|
}
|
|
|
|
|
else if (fileExtension == ".url")
|
|
|
|
|
{
|
|
|
|
|
handled = SetUrl();
|
|
|
|
|
}
|
|
|
|
|
else if (fileExtension == ".sln")
|
|
|
|
|
{
|
|
|
|
|
handled = SetSln();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!handled)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Icon = IconReader.GetFileIconWithCache(TargetFilePath, false);
|
2020-03-30 12:19:11 +13:00
|
|
|
|
diposeIcon = false;
|
2020-03-30 06:26:45 +13:00
|
|
|
|
|
|
|
|
|
// other project -> fails sometimes
|
|
|
|
|
//icon = IconHelper.ExtractIcon(TargetFilePath, 0);
|
|
|
|
|
|
|
|
|
|
// standard way -> fails sometimes
|
|
|
|
|
//icon = Icon.ExtractAssociatedIcon(filePath);
|
|
|
|
|
|
|
|
|
|
// API Code Pack -> fails sometimes
|
|
|
|
|
//ShellFile shellFile = ShellFile.FromFilePath(filePath);
|
|
|
|
|
//Bitmap shellThumb = shellFile.Thumbnail.ExtraLargeBitmap;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
if (ex is SecurityException ||
|
|
|
|
|
ex is ArgumentException ||
|
|
|
|
|
ex is UnauthorizedAccessException ||
|
|
|
|
|
ex is PathTooLongException ||
|
|
|
|
|
ex is NotSupportedException)
|
|
|
|
|
{
|
|
|
|
|
Log.Warn($"path:'{TargetFilePath}'", ex);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return isLnkDirectory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-03-28 08:43:16 +13:00
|
|
|
|
private bool SetLnk(ref bool isLnkDirectory,
|
|
|
|
|
ref string resolvedLnkPath)
|
|
|
|
|
{
|
|
|
|
|
bool handled = false;
|
2020-04-30 23:22:47 +12:00
|
|
|
|
resolvedLnkPath = LnkHelper.GetResolvedFileName(TargetFilePath);
|
2020-03-28 08:43:16 +13:00
|
|
|
|
if (LnkHelper.IsDirectory(resolvedLnkPath))
|
|
|
|
|
{
|
|
|
|
|
Icon = IconReader.GetFolderIcon(TargetFilePath,
|
|
|
|
|
IconReader.FolderType.Open, true);
|
|
|
|
|
handled = true;
|
|
|
|
|
isLnkDirectory = true;
|
|
|
|
|
}
|
|
|
|
|
else if (string.IsNullOrEmpty(resolvedLnkPath))
|
|
|
|
|
{
|
|
|
|
|
Log.Info($"Resolve *.LNK '{TargetFilePath}' has no icon");
|
|
|
|
|
#warning [Feature] Resolve network root #48, start here
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
IWshShell shell = new WshShell();
|
|
|
|
|
IWshShortcut lnk = shell.CreateShortcut(TargetFilePath)
|
|
|
|
|
as IWshShortcut;
|
|
|
|
|
Arguments = lnk.Arguments;
|
|
|
|
|
WorkingDirectory = lnk.WorkingDirectory;
|
|
|
|
|
string iconLocation = lnk.IconLocation;
|
|
|
|
|
if (iconLocation.Length > 2)
|
|
|
|
|
{
|
|
|
|
|
iconLocation = iconLocation.Substring(0,
|
|
|
|
|
iconLocation.Length - 2);
|
|
|
|
|
if (System.IO.File.Exists(iconLocation))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Icon = Icon.ExtractAssociatedIcon(iconLocation);
|
|
|
|
|
handled = true;
|
|
|
|
|
}
|
2020-03-30 06:26:45 +13:00
|
|
|
|
catch (ArgumentException ex)
|
2020-03-28 08:43:16 +13:00
|
|
|
|
{
|
2020-04-16 03:23:42 +12:00
|
|
|
|
Log.Warn($"iconLocation:'{iconLocation}'", ex);
|
2020-03-28 08:43:16 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TargetFilePath = resolvedLnkPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SetText(Path.GetFileNameWithoutExtension(TargetFilePathOrig));
|
|
|
|
|
|
|
|
|
|
return handled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool SetUrl()
|
|
|
|
|
{
|
|
|
|
|
bool handled = false;
|
|
|
|
|
string iconFile = string.Empty;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
FileIni file = new FileIni(TargetFilePath);
|
|
|
|
|
iconFile = file.Value("IconFile", string.Empty);
|
|
|
|
|
if (string.IsNullOrEmpty(iconFile))
|
|
|
|
|
{
|
|
|
|
|
string browserPath = FileUrl.GetDefaultBrowserPath();
|
|
|
|
|
if (string.IsNullOrEmpty(browserPath))
|
|
|
|
|
{
|
|
|
|
|
Log.Info($"Resolve *.URL '{TargetFilePath}'" +
|
|
|
|
|
$"No default browser found!");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Icon = IconReader.GetFileIconWithCache(browserPath, false);
|
2020-03-30 12:19:11 +13:00
|
|
|
|
diposeIcon = false;
|
2020-03-28 08:43:16 +13:00
|
|
|
|
handled = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (System.IO.File.Exists(iconFile))
|
|
|
|
|
{
|
|
|
|
|
Icon = Icon.ExtractAssociatedIcon(iconFile);
|
|
|
|
|
handled = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Log.Info($"Resolve *.URL '{TargetFilePath}' has no icon");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2020-03-30 06:26:45 +13:00
|
|
|
|
if (ex is SecurityException ||
|
|
|
|
|
ex is ArgumentException ||
|
|
|
|
|
ex is UnauthorizedAccessException ||
|
|
|
|
|
ex is PathTooLongException ||
|
|
|
|
|
ex is NotSupportedException)
|
|
|
|
|
{
|
|
|
|
|
Log.Warn($"path:'{TargetFilePath}', " +
|
|
|
|
|
$"iconFile:'{iconFile}'", ex);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2020-03-28 08:43:16 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SetText($"{FileInfo.Name.Substring(0, FileInfo.Name.Length - 4)}");
|
|
|
|
|
|
|
|
|
|
return handled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool SetSln()
|
|
|
|
|
{
|
|
|
|
|
bool handled = false;
|
|
|
|
|
StringBuilder executable = new StringBuilder(1024);
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-03-30 06:26:45 +13:00
|
|
|
|
DllImports.NativeMethods.Shell32FindExecutable(TargetFilePath, string.Empty, executable);
|
2020-03-28 08:43:16 +13:00
|
|
|
|
// icon = IconReader.GetFileIcon(executable, false);
|
|
|
|
|
// e.g. VS 2019 icon, need another icom in imagelist
|
|
|
|
|
List<Icon> extractedIcons = IconHelper.ExtractAllIcons(
|
|
|
|
|
executable.ToString());
|
|
|
|
|
Icon = extractedIcons.Last();
|
|
|
|
|
handled = true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2020-03-30 06:26:45 +13:00
|
|
|
|
if (ex is SecurityException ||
|
|
|
|
|
ex is ArgumentException ||
|
|
|
|
|
ex is UnauthorizedAccessException ||
|
|
|
|
|
ex is PathTooLongException ||
|
|
|
|
|
ex is NotSupportedException)
|
2020-03-28 08:43:16 +13:00
|
|
|
|
{
|
2020-03-30 06:26:45 +13:00
|
|
|
|
Log.Warn($"path:'{TargetFilePath}', " +
|
|
|
|
|
$"executable:'{executable}'", ex);
|
2020-03-28 08:43:16 +13:00
|
|
|
|
}
|
2020-03-30 06:26:45 +13:00
|
|
|
|
else
|
2020-03-28 08:43:16 +13:00
|
|
|
|
{
|
2020-03-30 06:26:45 +13:00
|
|
|
|
throw;
|
2020-03-28 08:43:16 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-30 06:26:45 +13:00
|
|
|
|
return handled;
|
2020-03-28 08:43:16 +13:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-30 06:26:45 +13:00
|
|
|
|
internal void MouseDown(DataGridView dgv, MouseEventArgs e)
|
2020-03-28 08:43:16 +13:00
|
|
|
|
{
|
|
|
|
|
if (ContainsMenu)
|
|
|
|
|
{
|
|
|
|
|
TriggerMenuOpener(); // Touchscreen
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (IsLoading)
|
|
|
|
|
{
|
|
|
|
|
waitMenuOpen.Click();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (e == null ||
|
|
|
|
|
e.Button == MouseButtons.Left &&
|
|
|
|
|
!ContainsMenu)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-03-30 00:20:18 +13:00
|
|
|
|
using (Process p = new Process())
|
2020-03-28 08:43:16 +13:00
|
|
|
|
{
|
2020-03-30 00:20:18 +13:00
|
|
|
|
p.StartInfo = new ProcessStartInfo(TargetFilePath)
|
2020-03-28 08:43:16 +13:00
|
|
|
|
{
|
2020-03-30 01:30:52 +13:00
|
|
|
|
FileName = TargetFilePathOrig,
|
2020-03-28 08:43:16 +13:00
|
|
|
|
Arguments = Arguments,
|
|
|
|
|
WorkingDirectory = WorkingDirectory,
|
|
|
|
|
CreateNoWindow = true
|
2020-03-30 00:20:18 +13:00
|
|
|
|
};
|
|
|
|
|
p.Start();
|
2020-03-28 08:43:16 +13:00
|
|
|
|
};
|
|
|
|
|
}
|
2020-03-30 06:26:45 +13:00
|
|
|
|
catch (Win32Exception ex)
|
2020-03-28 08:43:16 +13:00
|
|
|
|
{
|
2020-03-30 06:26:45 +13:00
|
|
|
|
Log.Warn($"path:'{TargetFilePath}'", ex);
|
2020-03-28 08:43:16 +13:00
|
|
|
|
MessageBox.Show(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (e != null &&
|
|
|
|
|
e.Button == MouseButtons.Right &&
|
|
|
|
|
FileInfo != null &&
|
2020-03-30 06:26:45 +13:00
|
|
|
|
dgv != null &&
|
2020-03-28 08:43:16 +13:00
|
|
|
|
dgv.Rows.Count > RowIndex)
|
|
|
|
|
{
|
|
|
|
|
IsContextMenuOpen = true;
|
|
|
|
|
IsSelected = true;
|
|
|
|
|
dgv.Rows[RowIndex].Selected = true;
|
|
|
|
|
|
2020-03-30 06:26:45 +13:00
|
|
|
|
ShellContextMenu ctxMnu = new ShellContextMenu();
|
|
|
|
|
Point location = dgv.FindForm().Location;
|
|
|
|
|
Point point = new Point(
|
|
|
|
|
e.X + location.X + dgv.Location.X,
|
|
|
|
|
e.Y + location.Y + dgv.Location.Y);
|
|
|
|
|
if (ContainsMenu)
|
2020-03-28 08:43:16 +13:00
|
|
|
|
{
|
2020-03-30 06:26:45 +13:00
|
|
|
|
DirectoryInfo[] dir = new DirectoryInfo[1];
|
|
|
|
|
dir[0] = new DirectoryInfo(TargetFilePathOrig);
|
|
|
|
|
ctxMnu.ShowContextMenu(dir, point);
|
2020-03-28 08:43:16 +13:00
|
|
|
|
}
|
2020-03-30 06:26:45 +13:00
|
|
|
|
else
|
2020-03-28 08:43:16 +13:00
|
|
|
|
{
|
2020-03-30 06:26:45 +13:00
|
|
|
|
FileInfo[] arrFI = new FileInfo[1];
|
|
|
|
|
arrFI[0] = new FileInfo(TargetFilePathOrig);
|
|
|
|
|
ctxMnu.ShowContextMenu(arrFI, point);
|
2020-03-28 08:43:16 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!dgv.IsDisposed)
|
|
|
|
|
{
|
|
|
|
|
IsSelected = false;
|
|
|
|
|
dgv.Rows[RowIndex].Selected = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IsContextMenuOpen = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-30 06:26:45 +13:00
|
|
|
|
internal void DoubleClick()
|
2020-03-28 08:43:16 +13:00
|
|
|
|
{
|
|
|
|
|
if (ContainsMenu)
|
|
|
|
|
{
|
2020-03-30 06:26:45 +13:00
|
|
|
|
Log.ProcessStart("explorer.exe", TargetFilePath);
|
2020-03-28 08:43:16 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-30 06:26:45 +13:00
|
|
|
|
internal void MenuLoaded()
|
2020-03-28 08:43:16 +13:00
|
|
|
|
{
|
|
|
|
|
waitMenuOpen.MenuLoaded();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-30 06:26:45 +13:00
|
|
|
|
internal void StartMenuOpener()
|
2020-03-28 08:43:16 +13:00
|
|
|
|
{
|
|
|
|
|
if (ContainsMenu)
|
|
|
|
|
{
|
|
|
|
|
IsLoading = true;
|
|
|
|
|
waitMenuOpen.Start();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void TriggerMenuOpener()
|
|
|
|
|
{
|
|
|
|
|
if (ContainsMenu && IsLoading)
|
|
|
|
|
{
|
|
|
|
|
waitMenuOpen.Start();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-30 06:26:45 +13:00
|
|
|
|
internal void StopLoadMenuAndStartWaitToOpenIt()
|
2020-03-28 08:43:16 +13:00
|
|
|
|
{
|
|
|
|
|
if (ContainsMenu)
|
|
|
|
|
{
|
|
|
|
|
waitMenuOpen.Stop();
|
|
|
|
|
//IsLoading = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void WaitMenuOpen_DoOpen()
|
|
|
|
|
{
|
|
|
|
|
IsLoading = false;
|
2020-04-01 07:42:49 +13:00
|
|
|
|
OpenMenu?.Invoke(this, this);
|
2020-03-28 08:43:16 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Dispose(true);
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
if (!isDisposed)
|
|
|
|
|
{
|
|
|
|
|
waitMenuOpen.Dispose();
|
|
|
|
|
Reading.Dispose();
|
2020-03-30 12:19:11 +13:00
|
|
|
|
if (diposeIcon)
|
|
|
|
|
{
|
|
|
|
|
Icon?.Dispose();
|
|
|
|
|
}
|
2020-03-28 08:43:16 +13:00
|
|
|
|
}
|
|
|
|
|
isDisposed = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|