PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/FilesystemFunction/FilesystemBookmarkFunction.cs

https://bitbucket.org/henderea/popupmultibox
C# | 318 lines | 284 code | 34 blank | 0 comment | 33 complexity | a304f0b4baeb33f37e95d0cccfcf0074 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows.Forms;
  5. using System.IO;
  6. using Multibox.Core.Functions;
  7. using Multibox.Core.UI;
  8. using Multibox.Plugin.Util;
  9. namespace Multibox.Plugin.FilesystemFunction
  10. {
  11. public class FilesystemBookmarkFunction : AbstractFunction
  12. {
  13. public override int SuggestedIndex()
  14. {
  15. return 2;
  16. }
  17. #region IMultiboxFunction Members
  18. public override bool Triggers(MultiboxFunctionParam args)
  19. {
  20. return args.MultiboxText.StartsWith(">>");
  21. }
  22. public override bool IsMulti(MultiboxFunctionParam args)
  23. {
  24. return true;
  25. }
  26. public override List<ResultItem> RunMulti(MultiboxFunctionParam args)
  27. {
  28. if (args.Key == Keys.Tab)
  29. {
  30. ResultItem tmp2 = args.MC.LabelManager.CurrentSelection;
  31. if (tmp2 != null)
  32. {
  33. try
  34. {
  35. args.MC.InputFieldText = ":" + tmp2.FullText;
  36. return new FilesystemFunction().RunMulti(args);
  37. }
  38. catch { }
  39. return null;
  40. }
  41. }
  42. if (args.Key == Keys.Delete && args.Shift)
  43. {
  44. ResultItem tmp2 = args.MC.LabelManager.CurrentSelection;
  45. if (tmp2 != null)
  46. BookmarkList.Delete(tmp2.DisplayText);
  47. }
  48. BookmarkItem[] itms = BookmarkList.Find(args.MultiboxText.Substring(2));
  49. if (itms == null || itms.Length <= 0)
  50. return null;
  51. List<ResultItem> ritms = new List<ResultItem>(0);
  52. foreach (BookmarkItem itm in itms)
  53. {
  54. try
  55. {
  56. ritms.Add(new ResultItem(itm.Name, itm.Path, itm.Path));
  57. }
  58. catch { }
  59. }
  60. return ritms.Count <= 0 ? null : ritms;
  61. }
  62. public override bool HasDetails(MultiboxFunctionParam args)
  63. {
  64. return true;
  65. }
  66. public override string GetDetails(MultiboxFunctionParam args)
  67. {
  68. try
  69. {
  70. ResultItem tmp2 = args.MC.LabelManager.CurrentSelection;
  71. if (tmp2 != null)
  72. return "Name: " + tmp2.DisplayText + "\nPath: " + tmp2.FullText;
  73. }
  74. catch { }
  75. return "";
  76. }
  77. public override bool SupressKeyPress(MultiboxFunctionParam args)
  78. {
  79. return (args.Key == Keys.Tab || (args.Key == Keys.Delete && args.Shift));
  80. }
  81. public override bool HasSpecialDisplayCopyHandling(MultiboxFunctionParam args)
  82. {
  83. return true;
  84. }
  85. public override string RunSpecialDisplayCopyHandling(MultiboxFunctionParam args)
  86. {
  87. ResultItem tmp2 = args.MC.LabelManager.CurrentSelection;
  88. if (tmp2 != null)
  89. {
  90. string tmpt = tmp2.FullText;
  91. if (tmpt.Length > 0 && tmpt[0] == '~')
  92. tmpt = Filesystem.UserProfile + tmpt.Substring(1);
  93. return tmpt;
  94. }
  95. return null;
  96. }
  97. #endregion
  98. }
  99. public class BookmarkItem : IComparable<BookmarkItem>
  100. {
  101. public string Name
  102. {
  103. get;
  104. set;
  105. }
  106. public string Path
  107. {
  108. get;
  109. set;
  110. }
  111. public BookmarkItem() : this("", "") { }
  112. public BookmarkItem(string n, string p)
  113. {
  114. Name = n;
  115. Path = p;
  116. }
  117. public string ToFileString()
  118. {
  119. return Name + ";;;" + Path;
  120. }
  121. public static BookmarkItem FromFileString(string data)
  122. {
  123. try
  124. {
  125. string[] parts = data.Split(new[] { ";;;" }, StringSplitOptions.None);
  126. return new BookmarkItem(parts[0], parts[1]);
  127. }
  128. catch { }
  129. return null;
  130. }
  131. #region IComparable<BookmarkItem> Members
  132. public int CompareTo(BookmarkItem other)
  133. {
  134. return Name.CompareTo(other.Name);
  135. }
  136. #endregion
  137. }
  138. public class BookmarkList
  139. {
  140. private static readonly List<BookmarkItem> items;
  141. public static BookmarkItem[] Items
  142. {
  143. get
  144. {
  145. return items.ToArray();
  146. }
  147. }
  148. public static int Count
  149. {
  150. get
  151. {
  152. return items.Count;
  153. }
  154. }
  155. static BookmarkList()
  156. {
  157. items = new List<BookmarkItem>(0);
  158. }
  159. public static BookmarkItem Get(int i)
  160. {
  161. try
  162. {
  163. return items[i];
  164. }
  165. catch { }
  166. return null;
  167. }
  168. public static void Set(int i, BookmarkItem itm)
  169. {
  170. try
  171. {
  172. items[i] = itm;
  173. Store();
  174. }
  175. catch { }
  176. }
  177. public static void Add(BookmarkItem i)
  178. {
  179. try
  180. {
  181. items.Add(i);
  182. Store();
  183. }
  184. catch { }
  185. }
  186. public static void Remove(BookmarkItem i)
  187. {
  188. try
  189. {
  190. items.Remove(i);
  191. Store();
  192. }
  193. catch { }
  194. }
  195. public static void RemoveAt(int i)
  196. {
  197. try
  198. {
  199. items.RemoveAt(i);
  200. Store();
  201. }
  202. catch { }
  203. }
  204. public static void Clear()
  205. {
  206. try
  207. {
  208. items.Clear();
  209. }
  210. catch { }
  211. }
  212. public static BookmarkItem[] Find(string fnd)
  213. {
  214. if (items == null || items.Count <= 0)
  215. return null;
  216. try
  217. {
  218. List<BookmarkItem> tmp = new List<BookmarkItem>(0);
  219. string fnd2 = fnd.ToLower();
  220. tmp.AddRange(items.Where(itm => itm.Name.ToLower().StartsWith(fnd2)));
  221. try
  222. {
  223. tmp.Sort();
  224. }
  225. catch { }
  226. return tmp.ToArray();
  227. }
  228. catch { }
  229. return null;
  230. }
  231. public static void Delete(string name)
  232. {
  233. if (items == null || items.Count <= 0)
  234. return;
  235. try
  236. {
  237. string name2 = name.ToLower();
  238. foreach (BookmarkItem itm in items)
  239. {
  240. if (itm.Name.ToLower().Equals(name2))
  241. {
  242. items.Remove(itm);
  243. break;
  244. }
  245. }
  246. Store();
  247. }
  248. catch { }
  249. }
  250. public static void Store()
  251. {
  252. try
  253. {
  254. try
  255. {
  256. items.Sort();
  257. }
  258. catch { }
  259. List<string> lines = new List<string>(0);
  260. lines.AddRange(items.Select(i => i.ToFileString()).Where(tmp => !tmp.Equals(";;;")));
  261. if (!Filesystem.DirectoryExists(Filesystem.UserProfile + "\\Popup Multibox"))
  262. Filesystem.CreateDirectory(Filesystem.UserProfile + "\\Popup Multibox");
  263. Filesystem.FileWriteAllLines(Filesystem.UserProfile + "\\Popup Multibox\\bookmarks.txt", lines.ToArray());
  264. }
  265. catch { }
  266. }
  267. public static void Load()
  268. {
  269. try
  270. {
  271. string[] lines = Filesystem.FileReadAllLines(Filesystem.UserProfile + "\\Popup Multibox\\bookmarks.txt");
  272. items.Clear();
  273. foreach (string line in lines)
  274. {
  275. BookmarkItem tmp = BookmarkItem.FromFileString(line);
  276. if (tmp != null)
  277. items.Add(tmp);
  278. }
  279. items.Sort();
  280. }
  281. catch { }
  282. }
  283. }
  284. }