PageRenderTime 13ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/HiddenFileManager/MainForm.cs

http://hiddenfilemanager.googlecode.com/
C# | 355 lines | 285 code | 53 blank | 17 comment | 47 complexity | b802c0ed5abcb4ba1a8f556331d20a5b MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10. using Microsoft.Win32;
  11. using HiddenFileManager.Properties;
  12. namespace HiddenFileManager
  13. {
  14. public partial class MainForm : Form
  15. {
  16. readonly Color TextBoxActiveColor = Color.FromArgb(255, 255, 255, 128);
  17. FileItemList _allItems;
  18. public MainForm()
  19. {
  20. InitializeComponent();
  21. try
  22. {
  23. _allItems = new FileItemList(ListFileName);
  24. }
  25. catch (Exception e)
  26. {
  27. // Delete the problematic file
  28. File.Delete(ListFileName);
  29. _allItems = new FileItemList(ListFileName);
  30. }
  31. // Initialize UI
  32. lbListOverlay.Bounds = lvItems.Bounds;
  33. lbListOverlay.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
  34. _privacyMode = true;
  35. PrivacyMode = false;
  36. // Read stored values
  37. PrivacyMode = Settings.Default.PrivacyMode;
  38. // Status bar
  39. lbStatusAction.Visible = ShowHiddenFilesStatus;
  40. lbStatus.Text = Resources.statusDragToAdd;
  41. }
  42. String ListFileName
  43. {
  44. get
  45. {
  46. return
  47. Path.Combine(Environment.GetFolderPath(
  48. Environment.SpecialFolder.ApplicationData),
  49. "HFM_TrackedItems.txt.enc");
  50. }
  51. }
  52. void FillItemList()
  53. {
  54. // Clear highlight if it was present
  55. tbFilter.BackColor = SystemColors.Window;
  56. this.lvItems.ItemChecked -= lvItems_ItemChecked;
  57. lvItems.Items.Clear();
  58. if (_allItems != null)
  59. {
  60. lbStatus.Text = String.Format(Resources.statusItemsTracked, _allItems.Count);
  61. }
  62. // No items
  63. String filter = Filter;
  64. if (_allItems != null &&
  65. filter != null) // null -- no items
  66. {
  67. // Filter
  68. lvItems.BeginUpdate();
  69. foreach (FileItem item in _allItems)
  70. {
  71. if (filter == String.Empty || // all items
  72. item.Matches(Filter))
  73. {
  74. lvItems.Items.Add(item);
  75. }
  76. }
  77. lvItems.EndUpdate();
  78. }
  79. UpdateOverlayLabel();
  80. this.lvItems.ItemChecked += lvItems_ItemChecked;
  81. }
  82. void UpdateOverlayLabel()
  83. {
  84. if (lvItems.Items.Count == 0)
  85. {
  86. if (_allItems == null || _allItems.Count == 0)
  87. {
  88. lbListOverlay.Text = Resources.overlayDragItems;
  89. }
  90. else
  91. {
  92. lbListOverlay.Text = PrivacyMode ?
  93. Resources.overlayPrivacyMode :
  94. Resources.overlayNoItemsFound;
  95. }
  96. lbListOverlay.Visible = true;
  97. }
  98. else
  99. {
  100. lbListOverlay.Visible = false;
  101. }
  102. }
  103. private void cbPrivacyMode_CheckedChanged(object sender, EventArgs e)
  104. {
  105. PrivacyMode = cbPrivacyMode.Checked;
  106. Settings.Default.PrivacyMode = PrivacyMode;
  107. Settings.Default.Save();
  108. }
  109. bool _privacyMode = false;
  110. bool PrivacyMode
  111. {
  112. get { return _privacyMode; }
  113. set
  114. {
  115. if (value == _privacyMode) { return; }
  116. _privacyMode = value;
  117. if (cbPrivacyMode.Checked != _privacyMode)
  118. {
  119. cbPrivacyMode.Checked = _privacyMode;
  120. }
  121. SuspendLayout();
  122. // TODO: preserve when going OUT of privacy mode
  123. if (_privacyMode)
  124. {
  125. Text = Resources.titlePrivacyMode;
  126. pTopBar.BackColor = Color.Silver;
  127. pbSpy.Visible = true;
  128. bShow.Visible = true;
  129. tbFilter.Width -= bShow.Width;
  130. // Clear the filter, for privacy
  131. tbFilter.Text = "";
  132. FillItemList();
  133. tbFilter.BackColor = TextBoxActiveColor;
  134. tbFilter.Select();
  135. }
  136. else
  137. {
  138. Text = Resources.titleNormal;
  139. pTopBar.BackColor = SystemColors.Control;
  140. pbSpy.Visible = false;
  141. bShow.Visible = false;
  142. tbFilter.BackColor = Color.White;
  143. tbFilter.Width += bShow.Width;
  144. FillItemList();
  145. }
  146. ResumeLayout();
  147. }
  148. }
  149. private void bShow_Click(object sender, EventArgs e)
  150. {
  151. FillItemList();
  152. }
  153. private void lvItems_MouseClick(object sender, MouseEventArgs e)
  154. {
  155. if (e.Button == MouseButtons.Left &&
  156. e.X > 16) // Prevents revert on checkbox itself
  157. {
  158. // Switch selected items depending on the first one
  159. if (lvItems.SelectedItems.Count > 0)
  160. {
  161. bool state = !lvItems.SelectedItems[0].Checked;
  162. foreach(FileItem item in lvItems.SelectedItems)
  163. {
  164. item.Checked = state;
  165. }
  166. }
  167. /*
  168. FileItem item = lvItems.GetItemAt(e.X, e.Y) as FileItem;
  169. if (item != null)
  170. {
  171. item.Checked = !item.Checked;
  172. }
  173. */
  174. }
  175. }
  176. private void lvItems_ItemChecked(object sender, ItemCheckedEventArgs e)
  177. {
  178. FileItem fsi = e.Item as FileItem;
  179. if (fsi == null) { return; }
  180. if (fsi.Status != ItemStatus.Visible && fsi.Status != ItemStatus.Hidden) { return; }
  181. fsi.SetHidden(e.Item.Checked);
  182. }
  183. private void tbFilter_TextChanged(object sender, EventArgs e)
  184. {
  185. if (PrivacyMode)
  186. {
  187. tbFilter.BackColor = TextBoxActiveColor;
  188. }
  189. else
  190. {
  191. FillItemList();
  192. }
  193. }
  194. private void tbFilter_KeyUp(object sender, KeyEventArgs e)
  195. {
  196. if (e.KeyCode == Keys.Enter)
  197. {
  198. FillItemList();
  199. }
  200. }
  201. public String Filter
  202. {
  203. get
  204. {
  205. if (String.IsNullOrEmpty(tbFilter.Text))
  206. {
  207. if (PrivacyMode) { return null; } // no items
  208. else { return String.Empty; } // all items
  209. }
  210. return tbFilter.Text;
  211. }
  212. }
  213. private void lvItems_DragDrop(object sender, DragEventArgs e)
  214. {
  215. if (!e.Data.GetDataPresent("FileDrop")) { return; }
  216. String[] files = e.Data.GetData("FileDrop", true) as String[];
  217. _allItems.AddItems(files);
  218. FillItemList();
  219. }
  220. private void lvItems_DragEnter(object sender, DragEventArgs e)
  221. {
  222. if (e.Data.GetDataPresent("FileDrop"))
  223. {
  224. e.Effect = DragDropEffects.Link;
  225. }
  226. else
  227. {
  228. e.Effect = DragDropEffects.None;
  229. }
  230. }
  231. private void lvItems_KeyDown(object sender, KeyEventArgs e)
  232. {
  233. if (e.KeyCode == Keys.Delete)
  234. {
  235. if (lvItems.SelectedItems.Count > 0)
  236. {
  237. int selectedIndex = lvItems.SelectedIndices[0];
  238. lvItems.BeginUpdate();
  239. foreach (FileItem item in lvItems.SelectedItems)
  240. {
  241. lvItems.Items.Remove(item);
  242. _allItems.Remove(item);
  243. }
  244. lvItems.EndUpdate();
  245. if (lvItems.Items.Count > 0)
  246. {
  247. if (selectedIndex >= lvItems.Items.Count) { selectedIndex = lvItems.Items.Count - 1; }
  248. lvItems.SelectedIndices.Add(selectedIndex);
  249. }
  250. _allItems.Save();
  251. UpdateOverlayLabel();
  252. }
  253. }
  254. }
  255. private void bHideAll_Click(object sender, EventArgs e)
  256. {
  257. lvItems.BeginUpdate();
  258. foreach (FileItem fi in _allItems)
  259. {
  260. fi.SetHidden(true);
  261. }
  262. lvItems.EndUpdate();
  263. }
  264. private void bUnhideAll_Click(object sender, EventArgs e)
  265. {
  266. lvItems.BeginUpdate();
  267. foreach (FileItem fi in _allItems)
  268. {
  269. fi.SetHidden(false);
  270. }
  271. lvItems.EndUpdate();
  272. }
  273. public static bool ShowHiddenFilesStatus
  274. {
  275. get
  276. {
  277. using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced"))
  278. {
  279. return (int)key.GetValue("Hidden") == 1;
  280. }
  281. }
  282. }
  283. private void lbStatusAction_Click(object sender, EventArgs e)
  284. {
  285. if (ShowHiddenFilesStatus)
  286. {
  287. MessageBox.Show(this,
  288. "Hidden items are shown in Windows Explorer. \r\n" +
  289. "\r\n" +
  290. "To make them invisible: \r\n" +
  291. "1) Open any Explorer window \r\n" +
  292. "2) Open the menus (press Alt if not visible) \r\n" +
  293. "3) Select Tools >> Folder Options \r\n" +
  294. "4) Switch to View tab and click \r\n" +
  295. " \"Don't show hidden files, folders, or drives\"\r\n" +
  296. "5) Click OK or Apply",
  297. "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  298. }
  299. lbStatusAction.Visible = ShowHiddenFilesStatus;
  300. }
  301. }
  302. }