2020-07-07 07:15:45 +12:00
|
|
|
|
// <copyright file="RowData.cs" company="PlaceholderCompany">
|
|
|
|
|
// Copyright (c) PlaceholderCompany. All rights reserved.
|
|
|
|
|
// </copyright>
|
2020-03-28 08:43:16 +13:00
|
|
|
|
|
|
|
|
|
namespace SystemTrayMenu.DataClasses
|
|
|
|
|
{
|
2020-07-07 07:15:45 +12:00
|
|
|
|
using System;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using SystemTrayMenu.Utilities;
|
2021-06-27 21:35:07 +12:00
|
|
|
|
using Menu = SystemTrayMenu.UserInterface.Menu;
|
2020-07-07 07:15:45 +12:00
|
|
|
|
|
2021-10-14 04:13:11 +13:00
|
|
|
|
internal class RowData
|
2020-03-28 08:43:16 +13:00
|
|
|
|
{
|
2021-04-27 06:06:27 +12:00
|
|
|
|
private static readonly Icon White50PercentageIcon = Properties.Resources.White50Percentage;
|
2021-09-24 08:53:46 +12:00
|
|
|
|
private static readonly Icon NotFoundIcon = Properties.Resources.NotFound;
|
2020-07-07 09:37:55 +12:00
|
|
|
|
private static DateTime contextMenuClosed;
|
|
|
|
|
private string text;
|
2021-04-17 12:39:48 +12:00
|
|
|
|
private Icon icon;
|
2020-03-28 08:43:16 +13:00
|
|
|
|
|
2020-03-30 06:26:45 +13:00
|
|
|
|
internal RowData()
|
2020-03-28 08:43:16 +13:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-08 03:05:19 +12:00
|
|
|
|
internal FileInfo FileInfo { get; set; }
|
|
|
|
|
|
|
|
|
|
internal Menu SubMenu { get; set; }
|
|
|
|
|
|
|
|
|
|
internal bool IsMenuOpen { get; set; }
|
|
|
|
|
|
|
|
|
|
internal bool IsSelected { get; set; }
|
|
|
|
|
|
|
|
|
|
internal bool ContainsMenu { get; set; }
|
|
|
|
|
|
|
|
|
|
internal bool IsContextMenuOpen { get; set; }
|
|
|
|
|
|
|
|
|
|
internal bool IsResolvedLnk { get; set; }
|
|
|
|
|
|
|
|
|
|
internal bool HiddenEntry { get; set; }
|
|
|
|
|
|
|
|
|
|
internal string TargetFilePath { get; set; }
|
|
|
|
|
|
|
|
|
|
internal string TargetFilePathOrig { get; set; }
|
|
|
|
|
|
|
|
|
|
internal int RowIndex { get; set; }
|
|
|
|
|
|
|
|
|
|
internal int MenuLevel { get; set; }
|
|
|
|
|
|
2021-09-24 08:53:46 +12:00
|
|
|
|
internal bool IconLoading { get; set; }
|
|
|
|
|
|
2021-10-14 04:13:11 +13:00
|
|
|
|
internal string FilePathIcon { get; set; }
|
2020-07-08 03:05:19 +12:00
|
|
|
|
|
2021-10-26 08:45:49 +13:00
|
|
|
|
internal bool ProcessStarted { get; set; }
|
|
|
|
|
|
2020-03-30 06:26:45 +13:00
|
|
|
|
internal void SetText(string text)
|
2020-03-28 08:43:16 +13:00
|
|
|
|
{
|
2020-07-07 09:37:55 +12:00
|
|
|
|
this.text = text;
|
2020-03-28 08:43:16 +13:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-14 05:14:51 +12:00
|
|
|
|
internal void SetData(RowData data, DataTable dataTable)
|
2020-03-30 06:26:45 +13:00
|
|
|
|
{
|
2020-05-14 05:14:51 +12:00
|
|
|
|
DataRow row = dataTable.Rows.Add();
|
|
|
|
|
data.RowIndex = dataTable.Rows.IndexOf(row);
|
2020-03-30 06:26:45 +13:00
|
|
|
|
|
|
|
|
|
if (HiddenEntry)
|
|
|
|
|
{
|
2021-10-14 04:13:11 +13:00
|
|
|
|
row[0] = IconReader.AddIconOverlay(data.icon, White50PercentageIcon);
|
2020-03-30 06:26:45 +13:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-07-07 09:37:55 +12:00
|
|
|
|
row[0] = data.icon;
|
2020-03-30 06:26:45 +13:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-17 07:30:45 +12:00
|
|
|
|
if (!ContainsMenu &&
|
|
|
|
|
Config.IsHideFileExtension())
|
|
|
|
|
{
|
|
|
|
|
row[1] = Path.GetFileNameWithoutExtension(data.text);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
row[1] = data.text;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-14 05:14:51 +12:00
|
|
|
|
row[2] = data;
|
2020-03-30 06:26:45 +13:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-19 09:12:03 +13:00
|
|
|
|
internal bool ReadIcon(bool isDirectory, ref string resolvedLnkPath, int level)
|
2020-03-30 06:26:45 +13:00
|
|
|
|
{
|
|
|
|
|
bool isLnkDirectory = false;
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(TargetFilePath))
|
|
|
|
|
{
|
|
|
|
|
Log.Info($"TargetFilePath from {resolvedLnkPath} empty");
|
|
|
|
|
}
|
|
|
|
|
else if (isDirectory)
|
|
|
|
|
{
|
2021-11-19 09:12:03 +13:00
|
|
|
|
icon = IconReader.GetFolderIconWithCache(TargetFilePathOrig, IconReader.FolderType.Closed, false, true, level == 0, out bool loading);
|
2021-10-14 04:13:11 +13:00
|
|
|
|
IconLoading = loading;
|
2020-03-30 06:26:45 +13:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
bool handled = false;
|
2021-05-02 20:05:33 +12:00
|
|
|
|
bool showOverlay = false;
|
2020-03-30 06:26:45 +13:00
|
|
|
|
string fileExtension = Path.GetExtension(TargetFilePath);
|
|
|
|
|
|
|
|
|
|
if (fileExtension == ".lnk")
|
|
|
|
|
{
|
2021-11-19 09:12:03 +13:00
|
|
|
|
handled = SetLnk(level, ref isLnkDirectory, ref resolvedLnkPath);
|
2021-05-02 20:05:33 +12:00
|
|
|
|
showOverlay = true;
|
2020-03-30 06:26:45 +13:00
|
|
|
|
}
|
|
|
|
|
else if (fileExtension == ".url")
|
|
|
|
|
{
|
2021-11-19 09:12:03 +13:00
|
|
|
|
handled = SetUrl(level);
|
2021-05-02 20:05:33 +12:00
|
|
|
|
showOverlay = true;
|
2020-03-30 06:26:45 +13:00
|
|
|
|
}
|
|
|
|
|
else if (fileExtension == ".sln")
|
|
|
|
|
{
|
2021-11-19 09:12:03 +13:00
|
|
|
|
handled = SetSln(level);
|
2020-03-30 06:26:45 +13:00
|
|
|
|
}
|
2021-10-14 04:13:11 +13:00
|
|
|
|
else if (fileExtension == ".appref-ms")
|
|
|
|
|
{
|
|
|
|
|
showOverlay = true;
|
|
|
|
|
}
|
2020-03-30 06:26:45 +13:00
|
|
|
|
|
|
|
|
|
if (!handled)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-10-14 04:13:11 +13:00
|
|
|
|
FilePathIcon = TargetFilePathOrig;
|
2021-11-19 09:12:03 +13:00
|
|
|
|
icon = IconReader.GetFileIconWithCache(FilePathIcon, showOverlay, true, level == 0, out bool loading);
|
2021-09-24 08:53:46 +12:00
|
|
|
|
IconLoading = loading;
|
2020-03-30 06:26:45 +13:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2021-10-11 04:33:54 +13:00
|
|
|
|
Log.Warn($"path:'{TargetFilePathOrig}'", ex);
|
2020-03-30 06:26:45 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-25 01:37:58 +12:00
|
|
|
|
if (icon == null)
|
|
|
|
|
{
|
|
|
|
|
icon = NotFoundIcon;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-30 06:26:45 +13:00
|
|
|
|
return isLnkDirectory;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-02 23:12:18 +12:00
|
|
|
|
internal void MouseDown(DataGridView dgv, MouseEventArgs e, out bool toCloseByDoubleClick)
|
2020-07-07 07:15:45 +12:00
|
|
|
|
{
|
2021-05-02 23:12:18 +12:00
|
|
|
|
toCloseByDoubleClick = false;
|
|
|
|
|
|
2020-07-07 07:15:45 +12:00
|
|
|
|
if (e != null &&
|
|
|
|
|
e.Button == MouseButtons.Right &&
|
|
|
|
|
FileInfo != null &&
|
|
|
|
|
dgv != null &&
|
|
|
|
|
dgv.Rows.Count > RowIndex &&
|
2020-07-07 09:37:55 +12:00
|
|
|
|
(DateTime.Now - contextMenuClosed).TotalMilliseconds > 200)
|
2020-07-07 07:15:45 +12:00
|
|
|
|
{
|
|
|
|
|
IsContextMenuOpen = true;
|
|
|
|
|
|
2021-11-17 12:13:46 +13:00
|
|
|
|
ShellContextMenu ctxMnu = new();
|
2020-07-07 07:15:45 +12:00
|
|
|
|
Point location = dgv.FindForm().Location;
|
2021-11-17 12:13:46 +13:00
|
|
|
|
Point point = new(
|
2020-07-07 07:15:45 +12:00
|
|
|
|
e.X + location.X + dgv.Location.X,
|
|
|
|
|
e.Y + location.Y + dgv.Location.Y);
|
|
|
|
|
if (ContainsMenu)
|
|
|
|
|
{
|
|
|
|
|
DirectoryInfo[] dir = new DirectoryInfo[1];
|
|
|
|
|
dir[0] = new DirectoryInfo(TargetFilePathOrig);
|
|
|
|
|
ctxMnu.ShowContextMenu(dir, point);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
FileInfo[] arrFI = new FileInfo[1];
|
|
|
|
|
arrFI[0] = new FileInfo(TargetFilePathOrig);
|
|
|
|
|
ctxMnu.ShowContextMenu(arrFI, point);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IsContextMenuOpen = false;
|
2020-07-07 09:37:55 +12:00
|
|
|
|
contextMenuClosed = DateTime.Now;
|
2020-07-07 07:15:45 +12:00
|
|
|
|
}
|
2020-08-06 20:42:27 +12:00
|
|
|
|
|
|
|
|
|
if (Properties.Settings.Default.OpenItemWithOneClick)
|
|
|
|
|
{
|
2021-05-02 23:12:18 +12:00
|
|
|
|
OpenItem(e, ref toCloseByDoubleClick);
|
2020-08-06 20:42:27 +12:00
|
|
|
|
}
|
2020-07-07 07:15:45 +12:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-02 23:12:18 +12:00
|
|
|
|
internal void DoubleClick(MouseEventArgs e, out bool toCloseByDoubleClick)
|
2020-08-06 20:42:27 +12:00
|
|
|
|
{
|
2021-05-02 23:12:18 +12:00
|
|
|
|
toCloseByDoubleClick = false;
|
2020-08-06 20:42:27 +12:00
|
|
|
|
if (!Properties.Settings.Default.OpenItemWithOneClick)
|
|
|
|
|
{
|
2021-05-02 23:12:18 +12:00
|
|
|
|
OpenItem(e, ref toCloseByDoubleClick);
|
2020-08-06 20:42:27 +12:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-14 04:13:11 +13:00
|
|
|
|
if (ContainsMenu && (e == null || e.Button == MouseButtons.Left))
|
2020-08-06 20:42:27 +12:00
|
|
|
|
{
|
2021-05-03 01:01:31 +12:00
|
|
|
|
Log.ProcessStart(TargetFilePath);
|
2021-05-02 23:12:18 +12:00
|
|
|
|
if (!Properties.Settings.Default.StaysOpenWhenItemClicked)
|
|
|
|
|
{
|
|
|
|
|
toCloseByDoubleClick = true;
|
|
|
|
|
}
|
2020-08-06 20:42:27 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-25 01:37:58 +12:00
|
|
|
|
internal Icon ReadLoadedIcon()
|
|
|
|
|
{
|
2021-10-14 04:13:11 +13:00
|
|
|
|
if (ContainsMenu)
|
2021-09-25 01:37:58 +12:00
|
|
|
|
{
|
2021-11-14 01:55:01 +13:00
|
|
|
|
icon = IconReader.GetFolderIconWithCache(TargetFilePathOrig, IconReader.FolderType.Closed, false, false, MenuLevel == 0, out bool loading);
|
2021-10-14 04:13:11 +13:00
|
|
|
|
IconLoading = loading;
|
2021-09-25 01:37:58 +12:00
|
|
|
|
}
|
2021-10-14 04:13:11 +13:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
bool showOverlay = false;
|
|
|
|
|
string fileExtension = Path.GetExtension(TargetFilePath);
|
|
|
|
|
if (fileExtension == ".lnk" || fileExtension == ".url" || fileExtension == ".appref-ms")
|
|
|
|
|
{
|
|
|
|
|
showOverlay = true;
|
|
|
|
|
}
|
2021-09-25 01:37:58 +12:00
|
|
|
|
|
2021-10-14 04:13:11 +13:00
|
|
|
|
string filePath = FilePathIcon;
|
|
|
|
|
if (string.IsNullOrEmpty(filePath))
|
|
|
|
|
{
|
|
|
|
|
filePath = TargetFilePathOrig;
|
|
|
|
|
}
|
2021-09-25 01:37:58 +12:00
|
|
|
|
|
2021-11-14 01:55:01 +13:00
|
|
|
|
icon = IconReader.GetFileIconWithCache(filePath, showOverlay, false, MenuLevel == 0, out bool loading);
|
2021-10-14 04:13:11 +13:00
|
|
|
|
IconLoading = loading;
|
2021-09-25 01:37:58 +12:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-14 04:13:11 +13:00
|
|
|
|
if (!IconLoading && icon == null)
|
2020-08-06 20:42:27 +12:00
|
|
|
|
{
|
2021-10-14 04:13:11 +13:00
|
|
|
|
icon = NotFoundIcon;
|
2020-08-06 20:42:27 +12:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-14 04:13:11 +13:00
|
|
|
|
return icon;
|
2020-08-06 20:42:27 +12:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-02 23:12:18 +12:00
|
|
|
|
private void OpenItem(MouseEventArgs e, ref bool toCloseByOpenItem)
|
2020-07-07 07:15:45 +12:00
|
|
|
|
{
|
2020-07-07 09:37:55 +12:00
|
|
|
|
if (!ContainsMenu &&
|
|
|
|
|
(e == null || e.Button == MouseButtons.Left))
|
2020-07-07 07:15:45 +12:00
|
|
|
|
{
|
2021-10-26 08:45:49 +13:00
|
|
|
|
ProcessStarted = true;
|
2021-10-24 20:46:38 +13:00
|
|
|
|
Log.ProcessStart(TargetFilePathOrig, string.Empty, false, string.Empty, true);
|
2021-05-03 01:01:31 +12:00
|
|
|
|
if (!Properties.Settings.Default.StaysOpenWhenItemClicked)
|
2020-07-07 07:15:45 +12:00
|
|
|
|
{
|
2021-05-03 01:01:31 +12:00
|
|
|
|
toCloseByOpenItem = true;
|
2020-07-07 07:15:45 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-30 06:26:45 +13:00
|
|
|
|
|
2021-11-19 09:12:03 +13:00
|
|
|
|
private bool SetLnk(int level, ref bool isLnkDirectory, ref string resolvedLnkPath)
|
2020-03-28 08:43:16 +13:00
|
|
|
|
{
|
|
|
|
|
bool handled = false;
|
2020-07-07 07:15:45 +12:00
|
|
|
|
resolvedLnkPath = FileLnk.GetResolvedFileName(TargetFilePath);
|
2021-10-06 09:47:47 +13:00
|
|
|
|
|
2021-11-11 11:39:52 +13:00
|
|
|
|
if (string.IsNullOrEmpty(resolvedLnkPath))
|
|
|
|
|
{
|
|
|
|
|
// do nothing
|
|
|
|
|
}
|
|
|
|
|
else if (string.IsNullOrEmpty(Path.GetExtension(resolvedLnkPath)))
|
2020-03-28 08:43:16 +13:00
|
|
|
|
{
|
2021-11-19 09:12:03 +13:00
|
|
|
|
icon = IconReader.GetFolderIconWithCache(TargetFilePathOrig, IconReader.FolderType.Open, true, true, level == 0, out bool loading);
|
2021-10-14 04:13:11 +13:00
|
|
|
|
IconLoading = loading;
|
2020-03-28 08:43:16 +13:00
|
|
|
|
handled = true;
|
|
|
|
|
isLnkDirectory = true;
|
|
|
|
|
}
|
2020-07-07 07:15:45 +12:00
|
|
|
|
else if (FileLnk.IsNetworkRoot(resolvedLnkPath))
|
2020-07-05 07:20:56 +12:00
|
|
|
|
{
|
|
|
|
|
isLnkDirectory = true;
|
|
|
|
|
}
|
2020-03-28 08:43:16 +13:00
|
|
|
|
else if (string.IsNullOrEmpty(resolvedLnkPath))
|
|
|
|
|
{
|
|
|
|
|
Log.Info($"Resolve *.LNK '{TargetFilePath}' has no icon");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
TargetFilePath = resolvedLnkPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SetText(Path.GetFileNameWithoutExtension(TargetFilePathOrig));
|
|
|
|
|
|
|
|
|
|
return handled;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-19 09:12:03 +13:00
|
|
|
|
private bool SetUrl(int level)
|
2020-03-28 08:43:16 +13:00
|
|
|
|
{
|
|
|
|
|
bool handled = false;
|
|
|
|
|
string iconFile = string.Empty;
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-11-17 12:13:46 +13:00
|
|
|
|
FileIni file = new(TargetFilePath);
|
2020-03-28 08:43:16 +13:00
|
|
|
|
iconFile = file.Value("IconFile", string.Empty);
|
|
|
|
|
if (string.IsNullOrEmpty(iconFile))
|
|
|
|
|
{
|
2021-05-03 01:01:31 +12:00
|
|
|
|
if (FileUrl.GetDefaultBrowserPath(out string browserPath))
|
2020-03-28 08:43:16 +13:00
|
|
|
|
{
|
2021-10-14 04:13:11 +13:00
|
|
|
|
FilePathIcon = browserPath;
|
2021-11-19 09:12:03 +13:00
|
|
|
|
icon = IconReader.GetFileIconWithCache(FilePathIcon, true, true, level == 0, out bool loading);
|
2021-09-24 08:53:46 +12:00
|
|
|
|
IconLoading = loading;
|
2020-03-28 08:43:16 +13:00
|
|
|
|
handled = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-02 01:32:06 +13:00
|
|
|
|
else if (File.Exists(iconFile))
|
2020-03-28 08:43:16 +13:00
|
|
|
|
{
|
2021-10-14 04:13:11 +13:00
|
|
|
|
FilePathIcon = iconFile;
|
2021-11-19 09:12:03 +13:00
|
|
|
|
icon = IconReader.GetFileIconWithCache(FilePathIcon, true, true, level == 0, out bool loading);
|
2021-10-14 04:13:11 +13:00
|
|
|
|
IconLoading = loading;
|
2020-03-28 08:43:16 +13:00
|
|
|
|
handled = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Log.Info($"Resolve *.URL '{TargetFilePath}' has no icon");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2021-10-14 04:13:11 +13:00
|
|
|
|
Log.Warn($"path:'{TargetFilePath}', iconFile:'{iconFile}'", ex);
|
2020-03-28 08:43:16 +13:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-07 02:46:47 +12:00
|
|
|
|
SetText($"{FileInfo.Name[0..^4]}");
|
2020-03-28 08:43:16 +13:00
|
|
|
|
|
|
|
|
|
return handled;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-19 09:12:03 +13:00
|
|
|
|
private bool SetSln(int level)
|
2020-03-28 08:43:16 +13:00
|
|
|
|
{
|
|
|
|
|
bool handled = false;
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-11-19 09:12:03 +13:00
|
|
|
|
icon = IconReader.GetExtractAllIconsLastWithCache(TargetFilePathOrig, true, level == 0, out bool loading);
|
2021-10-14 04:13:11 +13:00
|
|
|
|
IconLoading = loading;
|
2020-03-28 08:43:16 +13:00
|
|
|
|
handled = true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2021-10-14 04:13:11 +13:00
|
|
|
|
Log.Warn($"path:'{TargetFilePath}'", ex);
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|