WinXGroupItem.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using BluePointLilac.Methods;
  2. using ContextMenuManager.Controls.Interfaces;
  3. using System.IO;
  4. using System.Windows.Forms;
  5. namespace ContextMenuManager.Controls
  6. {
  7. sealed class WinXGroupItem : GroupPathItem, IChkVisibleItem, ITsiDeleteItem, ITsiTextItem
  8. {
  9. public WinXGroupItem(string groupPath) : base(groupPath, ObjectPath.PathType.Directory)
  10. {
  11. InitializeComponents();
  12. this.TargetPath = groupPath;
  13. }
  14. public new string TargetPath
  15. {
  16. get => base.TargetPath;
  17. set
  18. {
  19. base.TargetPath = value;
  20. this.Text = Path.GetFileNameWithoutExtension(value);
  21. this.Image = ResourceIcon.GetFolderIcon(value).ToBitmap();
  22. ChkVisible.Checked = this.ItemVisible;
  23. }
  24. }
  25. public bool ItemVisible
  26. {
  27. get => (File.GetAttributes(TargetPath) & FileAttributes.Hidden) != FileAttributes.Hidden;
  28. set
  29. {
  30. FileAttributes attributes = File.GetAttributes(TargetPath);
  31. if(value) attributes &= ~FileAttributes.Hidden;
  32. else attributes |= FileAttributes.Hidden;
  33. File.SetAttributes(TargetPath, attributes);
  34. if(Directory.GetFiles(TargetPath).Length > 0) ExplorerRestarter.Show();
  35. }
  36. }
  37. public string ItemText
  38. {
  39. get => Path.GetFileNameWithoutExtension(TargetPath);
  40. set
  41. {
  42. string newPath = $@"{WinXList.WinXPath}\{ObjectPath.RemoveIllegalChars(value)}";
  43. Directory.Move(TargetPath, newPath);
  44. this.TargetPath = newPath;
  45. ExplorerRestarter.Show();
  46. }
  47. }
  48. public VisibleCheckBox ChkVisible { get; set; }
  49. public DeleteMeMenuItem TsiDeleteMe { get; set; }
  50. public ChangeTextMenuItem TsiChangeText { get; set; }
  51. readonly ToolStripMenuItem TsiRestoreDefault = new ToolStripMenuItem(AppString.Menu.RestoreDefault);
  52. private string DefaultGroupPath => $@"{WinXList.DefaultWinXPath}\{ItemText}";
  53. private void InitializeComponents()
  54. {
  55. ChkVisible = new VisibleCheckBox(this);
  56. this.SetCtrIndex(ChkVisible, 1);
  57. TsiDeleteMe = new DeleteMeMenuItem(this);
  58. TsiChangeText = new ChangeTextMenuItem(this);
  59. this.ContextMenuStrip = new ContextMenuStrip();
  60. this.ContextMenuStrip.Items.AddRange(new ToolStripItem[] { TsiChangeText,
  61. new ToolStripSeparator(), TsiRestoreDefault, new ToolStripSeparator(), TsiDeleteMe });
  62. this.ContextMenuStrip.Opening += (sender, e) => TsiRestoreDefault.Enabled = Directory.Exists(DefaultGroupPath);
  63. TsiRestoreDefault.Click += (sender, e) => RestoreDefault();
  64. }
  65. private void RestoreDefault()
  66. {
  67. if(MessageBoxEx.Show(AppString.MessageBox.RestoreDefault, MessageBoxButtons.OKCancel) == DialogResult.OK)
  68. {
  69. File.SetAttributes(TargetPath, File.GetAttributes(DefaultGroupPath));
  70. string[] paths = Directory.GetFiles(TargetPath);
  71. foreach(string path in paths)
  72. {
  73. File.Delete(path);
  74. }
  75. paths = Directory.GetFiles(DefaultGroupPath);
  76. foreach(string path in paths)
  77. {
  78. File.Copy(path, $@"{TargetPath}\{Path.GetFileName(path)}");
  79. }
  80. WinXList list = (WinXList)this.Parent;
  81. list.ClearItems();
  82. list.LoadItems();
  83. ExplorerRestarter.Show();
  84. }
  85. }
  86. public void DeleteMe()
  87. {
  88. bool flag = Directory.GetFiles(TargetPath, "*.lnk").Length > 0;
  89. if(flag && MessageBoxEx.Show(AppString.MessageBox.DeleteGroup, MessageBoxButtons.OKCancel) != DialogResult.OK) return;
  90. File.SetAttributes(TargetPath, FileAttributes.Normal);
  91. Directory.Delete(TargetPath, true);
  92. if(flag)
  93. {
  94. WinXList list = (WinXList)this.Parent;
  95. list.ClearItems();
  96. list.LoadItems();
  97. ExplorerRestarter.Show();
  98. }
  99. else this.Dispose();
  100. }
  101. }
  102. }