IEItem.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using BluePointLilac.Controls;
  2. using BluePointLilac.Methods;
  3. using ContextMenuManager.Controls.Interfaces;
  4. using ContextMenuManager.Methods;
  5. using Microsoft.Win32;
  6. using System;
  7. using System.Drawing;
  8. using System.Windows.Forms;
  9. namespace ContextMenuManager.Controls
  10. {
  11. sealed class IEItem : MyListItem, ITsiRegPathItem, ITsiFilePathItem, ITsiRegDeleteItem, ITsiCommandItem,
  12. ITsiWebSearchItem, ITsiTextItem, ITsiRegExportItem, IBtnShowMenuItem, IChkVisibleItem
  13. {
  14. public static readonly string[] MeParts = { "MenuExt", "-MenuExt" };
  15. public IEItem(string regPath)
  16. {
  17. InitializeComponents();
  18. this.RegPath = regPath;
  19. }
  20. private string regPath;
  21. public string RegPath
  22. {
  23. get => regPath;
  24. set
  25. {
  26. regPath = value;
  27. this.Text = this.ItemText;
  28. this.Image = this.ItemImage;
  29. }
  30. }
  31. public string ValueName => null;
  32. private string KeyName => RegistryEx.GetKeyName(RegPath);
  33. private string BackupPath => $@"{IEList.IEPath}\{(ItemVisible ? MeParts[1] : MeParts[0])}\{KeyName}";
  34. private string MeKeyName => RegistryEx.GetKeyName(RegistryEx.GetParentPath(RegPath));
  35. public string ItemText
  36. {
  37. get => RegistryEx.GetKeyName(RegPath);
  38. set
  39. {
  40. string newPath = $@"{RegistryEx.GetParentPath(RegPath)}\{value.Replace("\\", "")}";
  41. string defaultValue = Registry.GetValue(newPath, "", null)?.ToString();
  42. if(!defaultValue.IsNullOrWhiteSpace())
  43. {
  44. AppMessageBox.Show(AppString.Message.HasBeenAdded);
  45. }
  46. else
  47. {
  48. RegistryEx.MoveTo(RegPath, newPath);
  49. RegPath = newPath;
  50. }
  51. }
  52. }
  53. public bool ItemVisible
  54. {
  55. get => MeKeyName.Equals(MeParts[0], StringComparison.OrdinalIgnoreCase);
  56. set
  57. {
  58. RegistryEx.MoveTo(RegPath, BackupPath);
  59. RegPath = BackupPath;
  60. }
  61. }
  62. public string ItemCommand
  63. {
  64. get => Registry.GetValue(RegPath, "", null)?.ToString();
  65. set
  66. {
  67. Registry.SetValue(RegPath, "", value);
  68. this.Image = this.ItemImage;
  69. }
  70. }
  71. public string SearchText => $@"{AppString.SideBar.IEMenu} {Text}";
  72. public string ItemFilePath => ObjectPath.ExtractFilePath(ItemCommand);
  73. private Icon ItemIcon => ResourceIcon.GetIcon(ItemFilePath) ?? ResourceIcon.GetExtensionIcon(ItemFilePath);
  74. private Image ItemImage => ItemIcon?.ToBitmap() ?? AppImage.NotFound;
  75. public MenuButton BtnShowMenu { get; set; }
  76. public VisibleCheckBox ChkVisible { get; set; }
  77. public WebSearchMenuItem TsiSearch { get; set; }
  78. public ChangeTextMenuItem TsiChangeText { get; set; }
  79. public ChangeCommandMenuItem TsiChangeCommand { get; set; }
  80. public FileLocationMenuItem TsiFileLocation { get; set; }
  81. public FilePropertiesMenuItem TsiFileProperties { get; set; }
  82. public RegLocationMenuItem TsiRegLocation { get; set; }
  83. public RegExportMenuItem TsiRegExport { get; set; }
  84. public DeleteMeMenuItem TsiDeleteMe { get; set; }
  85. readonly ToolStripMenuItem TsiDetails = new ToolStripMenuItem(AppString.Menu.Details);
  86. private void InitializeComponents()
  87. {
  88. BtnShowMenu = new MenuButton(this);
  89. ChkVisible = new VisibleCheckBox(this);
  90. TsiChangeText = new ChangeTextMenuItem(this);
  91. TsiChangeCommand = new ChangeCommandMenuItem(this);
  92. TsiSearch = new WebSearchMenuItem(this);
  93. TsiFileLocation = new FileLocationMenuItem(this);
  94. TsiFileProperties = new FilePropertiesMenuItem(this);
  95. TsiRegLocation = new RegLocationMenuItem(this);
  96. TsiRegExport = new RegExportMenuItem(this);
  97. TsiDeleteMe = new DeleteMeMenuItem(this);
  98. ContextMenuStrip.Items.AddRange(new ToolStripItem[] { TsiChangeText,
  99. new ToolStripSeparator(), TsiDetails, new ToolStripSeparator(), TsiDeleteMe });
  100. TsiDetails.DropDownItems.AddRange(new ToolStripItem[] { TsiSearch, new ToolStripSeparator(),
  101. TsiChangeCommand, TsiFileProperties, TsiFileLocation, TsiRegLocation, TsiRegExport});
  102. }
  103. public void DeleteMe()
  104. {
  105. RegistryEx.DeleteKeyTree(this.RegPath);
  106. RegistryEx.DeleteKeyTree(this.BackupPath);
  107. }
  108. }
  109. }