PageRenderTime 60ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/main/src/addins/Deployment/MonoDevelop.Deployment.Linux/MonoDevelop.Deployment.Linux/DotDesktopViewWidget.cs

https://github.com/jfcantin/monodevelop
C# | 501 lines | 412 code | 81 blank | 8 comment | 61 complexity | d8d5d3e213f74b6da4800a5b2558f5d0 MD5 | raw file
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Specialized;
  4. using System.Xml;
  5. using Gtk;
  6. using MonoDevelop.Ide;
  7. namespace MonoDevelop.Deployment.Linux
  8. {
  9. [System.ComponentModel.Category("widget")]
  10. [System.ComponentModel.ToolboxItem(true)]
  11. public partial class DotDesktopViewWidget : Gtk.Bin
  12. {
  13. public event EventHandler Changed;
  14. DesktopEntry entry;
  15. ListStore storeEnvs;
  16. ListStore storeCategs;
  17. ListStore storeMimeTypes;
  18. ListStore storeEntries;
  19. int loading;
  20. CellRendererText entryKeyCell;
  21. CellRendererText mimeTypeCell;
  22. XmlDocument desktopInfo = DesktopEntry.GetDesktopInfo ();
  23. public DotDesktopViewWidget()
  24. {
  25. this.Build();
  26. notebook.Page = 0;
  27. // Environments tree
  28. storeEnvs = new ListStore (typeof(bool), typeof(string), typeof(string));
  29. treeEnvs.Model = storeEnvs;
  30. treeEnvs.HeadersVisible = false;
  31. TreeViewColumn col = new TreeViewColumn ();
  32. Gtk.CellRendererToggle tog = new CellRendererToggle ();
  33. col.PackStart (tog, false);
  34. tog.Toggled += OnEnvironmentClicked;
  35. Gtk.CellRendererText crt = new CellRendererText ();
  36. col.PackStart (crt, true);
  37. col.AddAttribute (tog, "active", 0);
  38. col.AddAttribute (crt, "text", 1);
  39. treeEnvs.AppendColumn (col);
  40. // Selected categories tree
  41. storeCategs = new ListStore (typeof(string), typeof(string));
  42. treeCategories.Model = storeCategs;
  43. treeCategories.HeadersVisible = false;
  44. treeCategories.AppendColumn ("", new CellRendererText (), "text", 0);
  45. // Mime types
  46. storeMimeTypes = new ListStore (typeof(string), typeof(string));
  47. treeMimeTypes.Model = storeMimeTypes;
  48. mimeTypeCell = new CellRendererText ();
  49. mimeTypeCell.Edited += new Gtk.EditedHandler (HandleOnEditMimeType);
  50. mimeTypeCell.EditingCanceled += new EventHandler (HandleOnEditMimeTypeCancelled);
  51. mimeTypeCell.Editable = true;
  52. treeMimeTypes.AppendColumn (Mono.Unix.Catalog.GetString ("Mime Type"), mimeTypeCell, "text", 0);
  53. treeMimeTypes.AppendColumn (Mono.Unix.Catalog.GetString ("Description"), new CellRendererText (), "text", 1);
  54. // Other entries
  55. storeEntries = new ListStore (typeof(string), typeof(string), typeof(string));
  56. treeEntries.Model = storeEntries;
  57. entryKeyCell = new CellRendererText ();
  58. entryKeyCell.Edited += new Gtk.EditedHandler (HandleOnEditKey);
  59. entryKeyCell.EditingCanceled += new EventHandler (HandleOnEditKeyCancelled);
  60. entryKeyCell.Editable = false;
  61. treeEntries.AppendColumn (Mono.Unix.Catalog.GetString ("Key"), entryKeyCell, "markup", 0);
  62. crt = new CellRendererText ();
  63. crt.Edited += new Gtk.EditedHandler (HandleOnEditValue);
  64. crt.Editable = true;
  65. treeEntries.AppendColumn (Mono.Unix.Catalog.GetString ("Value"), crt, "text", 2);
  66. }
  67. void NotifyChanged ()
  68. {
  69. if (loading == 0 && Changed != null)
  70. Changed (this, EventArgs.Empty);
  71. }
  72. public DesktopEntry DesktopEntry {
  73. get { return entry; }
  74. set { entry = value; Fill (); }
  75. }
  76. void Fill ()
  77. {
  78. loading++;
  79. comboType.Active = (int) entry.Type;
  80. entryExec.Text = entry.Exec;
  81. entryTryExec.Text = entry.TryExec;
  82. entryPath.Text = entry.Path;
  83. entryUrl.Text = entry.Url;
  84. checkTerminal.Active = entry.Terminal;
  85. comboLocales.AppendText (Mono.Unix.Catalog.GetString ("<Default>"));
  86. comboLocales.Active = 0;
  87. foreach (string loc in entry.GetLocales ())
  88. comboLocales.AppendText (loc);
  89. // Environments list
  90. if (entry.OnlyShowIn.Count > 0) {
  91. radioOnlyShowIn.Active = true;
  92. } else if (entry.NotShowIn.Count > 0) {
  93. radioNotShowIn.Active = true;
  94. } else
  95. radioAlwaysShow.Active = true;
  96. FillEnvironments ();
  97. // Fill mime types
  98. ArrayList sortedmt = new ArrayList ();
  99. sortedmt.AddRange (entry.MimeTypes);
  100. sortedmt.Sort ();
  101. foreach (string mt in sortedmt) {
  102. string desc = DesktopService.GetMimeTypeDescription (mt);
  103. storeMimeTypes.AppendValues (mt, desc);
  104. }
  105. checkShowInMenu.Active = !entry.NoDisplay;
  106. foreach (string s in entry.GetUnknownEntries ()) {
  107. storeEntries.AppendValues ("<b>" + s + "</b>", s, entry.GetEntry (s));
  108. }
  109. FillNames ();
  110. FillCategs ();
  111. UpdateType ();
  112. UpdateShowInMenu ();
  113. loading--;
  114. }
  115. void UpdateType ()
  116. {
  117. tableCommand.Visible = entry.Type == DesktopEntryType.Application;
  118. boxUrl.Visible = entry.Type == DesktopEntryType.Link;
  119. tableMimeTypes.Visible = entry.Type == DesktopEntryType.Application;
  120. boxCategories.Visible = entry.Type == DesktopEntryType.Application;
  121. }
  122. void UpdateShowInMenu ()
  123. {
  124. foreach (Gtk.Widget w in boxMenu.Children) {
  125. if (w != checkShowInMenu)
  126. w.Sensitive = checkShowInMenu.Active;
  127. }
  128. }
  129. void FillEnvironments ()
  130. {
  131. StringCollection envCol = null;
  132. if (entry.OnlyShowIn.Count > 0) {
  133. envCol = entry.OnlyShowIn;
  134. } else if (entry.NotShowIn.Count > 0) {
  135. envCol = entry.NotShowIn;
  136. }
  137. treeEnvs.Sensitive = envCol != null;
  138. storeEnvs.Clear ();
  139. foreach (XmlElement elem in desktopInfo.DocumentElement.SelectNodes ("Environments/Environment")) {
  140. bool sel = envCol != null && envCol.Contains (elem.GetAttribute ("name"));
  141. storeEnvs.AppendValues (sel, elem.GetAttribute ("_label"), elem.GetAttribute ("name"));
  142. }
  143. }
  144. void FillNames ()
  145. {
  146. loading++;
  147. entryName.Text = entry.Name;
  148. entryGenericName.Text = entry.GenericName;
  149. entryComment.Text = entry.Comment;
  150. entryIcon.Text = entry.Icon;
  151. loading--;
  152. }
  153. void FillCategs ()
  154. {
  155. storeCategs.Clear ();
  156. XmlElement cats = desktopInfo.DocumentElement ["Categories"];
  157. foreach (string cat in entry.Categories) {
  158. XmlNode node = cats.SelectSingleNode ("Category[@name='" + cat + "']/@_label");
  159. string catName;
  160. if (node != null)
  161. catName = node.InnerText;
  162. else
  163. catName = cat;
  164. storeCategs.AppendValues (Mono.Unix.Catalog.GetString (catName), cat);
  165. }
  166. }
  167. void OnEnvironmentClicked (object s, Gtk.ToggledArgs args)
  168. {
  169. TreeIter iter;
  170. storeEnvs.GetIterFromString (out iter, args.Path);
  171. bool sel = (bool) storeEnvs.GetValue (iter, 0);
  172. string env = (string) storeEnvs.GetValue (iter, 2);
  173. StringCollection col = radioOnlyShowIn.Active ? entry.OnlyShowIn : entry.NotShowIn;
  174. if (sel)
  175. col.Remove (env);
  176. else
  177. col.Add (env);
  178. storeEnvs.SetValue (iter, 0, !sel);
  179. NotifyChanged ();
  180. }
  181. protected virtual void OnButtonAddCategoriesClicked(object sender, System.EventArgs e)
  182. {
  183. MenuCategorySelectorDialog dlg = new MenuCategorySelectorDialog ();
  184. if (MessageService.RunCustomDialog (dlg) == (int) Gtk.ResponseType.Ok) {
  185. foreach (string s in dlg.Selection)
  186. entry.Categories.Add (s);
  187. FillCategs ();
  188. NotifyChanged ();
  189. }
  190. dlg.Destroy ();
  191. }
  192. protected virtual void OnButtonRemoveCategoryClicked(object sender, System.EventArgs e)
  193. {
  194. TreeIter iter;
  195. if (treeCategories.Selection.GetSelected (out iter)) {
  196. string cat = (string) storeCategs.GetValue (iter, 1);
  197. entry.Categories.Remove (cat);
  198. storeCategs.Remove (ref iter);
  199. if (!iter.Equals (TreeIter.Zero))
  200. treeCategories.Selection.SelectIter (iter);
  201. NotifyChanged ();
  202. }
  203. }
  204. protected virtual void OnRadioAlwaysShowClicked(object sender, System.EventArgs e)
  205. {
  206. if (loading > 0) return;
  207. entry.NotShowIn.Clear ();
  208. entry.OnlyShowIn.Clear ();
  209. NotifyChanged ();
  210. FillEnvironments ();
  211. treeEnvs.Sensitive = false;
  212. }
  213. protected virtual void OnRadioOnlyShowInClicked(object sender, System.EventArgs e)
  214. {
  215. if (loading > 0) return;
  216. foreach (string s in entry.NotShowIn)
  217. entry.OnlyShowIn.Add (s);
  218. entry.NotShowIn.Clear ();
  219. treeEnvs.Sensitive = true;
  220. NotifyChanged ();
  221. }
  222. protected virtual void OnRadioNotShowInClicked(object sender, System.EventArgs e)
  223. {
  224. if (loading > 0) return;
  225. foreach (string s in entry.OnlyShowIn)
  226. entry.NotShowIn.Add (s);
  227. entry.OnlyShowIn.Clear ();
  228. treeEnvs.Sensitive = true;
  229. NotifyChanged ();
  230. }
  231. protected virtual void OnButtonAddMimeTypeClicked(object sender, System.EventArgs e)
  232. {
  233. TreeIter it = storeMimeTypes.AppendValues ("", "");
  234. treeMimeTypes.Selection.SelectIter (it);
  235. treeMimeTypes.SetCursor (storeMimeTypes.GetPath (it), treeMimeTypes.Columns [0], true);
  236. }
  237. void HandleOnEditMimeType (object o, Gtk.EditedArgs e)
  238. {
  239. Gtk.TreeIter iter;
  240. if (storeMimeTypes.GetIterFromString (out iter, e.Path)) {
  241. string mt = e.NewText;
  242. string oldmt = (string) storeMimeTypes.GetValue (iter, 0);
  243. if (mt.Length > 0) {
  244. if (!entry.MimeTypes.Contains (mt)) {
  245. entry.MimeTypes.Add (mt);
  246. storeMimeTypes.SetValue (iter, 0, mt);
  247. storeMimeTypes.SetValue (iter, 1, DesktopService.GetMimeTypeDescription (mt));
  248. if (!string.IsNullOrEmpty (oldmt))
  249. // It is a modification. Remove the old name.
  250. entry.MimeTypes.Remove (oldmt);
  251. else {
  252. // Add a new line, so the user can add several types at a time
  253. TreeIter newit = storeMimeTypes.AppendValues ("", "");
  254. treeMimeTypes.Selection.SelectIter (newit);
  255. treeMimeTypes.ScrollToCell (storeMimeTypes.GetPath (newit), treeMimeTypes.Columns [0], false, 0f, 0f);
  256. treeMimeTypes.SetCursor (storeMimeTypes.GetPath (newit), treeMimeTypes.Columns [0], true);
  257. NotifyChanged ();
  258. }
  259. }
  260. }
  261. else {
  262. storeMimeTypes.Remove (ref iter);
  263. if (!string.IsNullOrEmpty (oldmt))
  264. entry.MimeTypes.Remove (oldmt);
  265. }
  266. }
  267. }
  268. void HandleOnEditMimeTypeCancelled (object s, EventArgs args)
  269. {
  270. entryKeyCell.Editable = false;
  271. Gtk.TreeIter iter;
  272. if (treeEntries.Selection.GetSelected (out iter)) {
  273. string oldmt = (string) storeMimeTypes.GetValue (iter, 0);
  274. if (string.IsNullOrEmpty (oldmt))
  275. storeEntries.Remove (ref iter);
  276. }
  277. }
  278. protected virtual void OnButtonRemoveMimeTypeClicked(object sender, System.EventArgs e)
  279. {
  280. TreeIter it;
  281. if (treeMimeTypes.Selection.GetSelected (out it)) {
  282. string mt = (string) storeMimeTypes.GetValue (it, 0);
  283. entry.MimeTypes.Remove (mt);
  284. storeMimeTypes.Remove (ref it);
  285. if (!it.Equals (TreeIter.Zero))
  286. treeMimeTypes.Selection.SelectIter (it);
  287. NotifyChanged ();
  288. }
  289. }
  290. protected virtual void OnComboLocalesChanged(object sender, System.EventArgs e)
  291. {
  292. if (loading > 0) return;
  293. if (comboLocales.Active == 0)
  294. entry.CurrentLocale = null;
  295. else
  296. entry.CurrentLocale = comboLocales.ActiveText;
  297. FillNames ();
  298. }
  299. protected virtual void OnComboTypeChanged(object sender, System.EventArgs e)
  300. {
  301. if (loading > 0) return;
  302. NotifyChanged ();
  303. entry.Type = (DesktopEntryType) comboType.Active;
  304. UpdateType ();
  305. }
  306. protected virtual void OnCheckShowInMenuClicked(object sender, System.EventArgs e)
  307. {
  308. if (loading > 0) return;
  309. NotifyChanged ();
  310. UpdateShowInMenu ();
  311. }
  312. protected virtual void OnEntryNameChanged(object sender, System.EventArgs e)
  313. {
  314. if (loading > 0) return;
  315. NotifyChanged ();
  316. entry.Name = entryName.Text;
  317. }
  318. protected virtual void OnEntryUrlChanged(object sender, System.EventArgs e)
  319. {
  320. if (loading > 0) return;
  321. NotifyChanged ();
  322. entry.Url = entryUrl.Text;
  323. }
  324. protected virtual void OnEntryExecChanged(object sender, System.EventArgs e)
  325. {
  326. if (loading > 0) return;
  327. NotifyChanged ();
  328. entry.Exec = entryExec.Text;
  329. }
  330. protected virtual void OnEntryTryExecChanged(object sender, System.EventArgs e)
  331. {
  332. if (loading > 0) return;
  333. NotifyChanged ();
  334. entry.TryExec = entryTryExec.Text;
  335. }
  336. protected virtual void OnEntryPathChanged(object sender, System.EventArgs e)
  337. {
  338. if (loading > 0) return;
  339. NotifyChanged ();
  340. entry.Path = entryPath.Text;
  341. }
  342. protected virtual void OnEntryCommentChanged(object sender, System.EventArgs e)
  343. {
  344. if (loading > 0) return;
  345. NotifyChanged ();
  346. entry.Comment = entryComment.Text;
  347. }
  348. protected virtual void OnEntryGenericNameChanged(object sender, System.EventArgs e)
  349. {
  350. if (loading > 0) return;
  351. NotifyChanged ();
  352. entry.GenericName = entryGenericName.Text;
  353. }
  354. protected virtual void OnEntryIconChanged(object sender, System.EventArgs e)
  355. {
  356. if (loading > 0) return;
  357. NotifyChanged ();
  358. entry.Icon = entryIcon.Text;
  359. }
  360. protected virtual void OnCheckTerminalClicked(object sender, System.EventArgs e)
  361. {
  362. if (loading > 0) return;
  363. entry.Terminal = checkTerminal.Active;
  364. }
  365. protected virtual void OnButtonAddEntryClicked(object sender, System.EventArgs e)
  366. {
  367. TreeIter it = storeEntries.AppendValues ("", "", "");
  368. treeEntries.Selection.SelectIter (it);
  369. entryKeyCell.Editable = true;
  370. treeEntries.SetCursor (storeEntries.GetPath (it), treeEntries.Columns [0], true);
  371. }
  372. void HandleOnEditValue (object o, Gtk.EditedArgs e)
  373. {
  374. Gtk.TreeIter iter;
  375. if (storeEntries.GetIterFromString (out iter, e.Path)) {
  376. string key = (string) storeEntries.GetValue (iter, 1);
  377. entry.SetEntry (key, e.NewText);
  378. storeEntries.SetValue (iter, 2, e.NewText);
  379. NotifyChanged ();
  380. }
  381. }
  382. void HandleOnEditKey (object o, Gtk.EditedArgs e)
  383. {
  384. entryKeyCell.Editable = false;
  385. Gtk.TreeIter iter;
  386. if (storeEntries.GetIterFromString (out iter, e.Path)) {
  387. string key = e.NewText;
  388. if (key.Length > 0) {
  389. entry.SetEntry (key, "");
  390. storeEntries.SetValue (iter, 0, "<b>" + key + "</b>");
  391. storeEntries.SetValue (iter, 1, key);
  392. treeEntries.SetCursor (storeEntries.GetPath (iter), treeEntries.Columns [1], true);
  393. NotifyChanged ();
  394. }
  395. else {
  396. storeEntries.Remove (ref iter);
  397. }
  398. }
  399. }
  400. void HandleOnEditKeyCancelled (object s, EventArgs args)
  401. {
  402. entryKeyCell.Editable = false;
  403. Gtk.TreeIter iter;
  404. if (treeEntries.Selection.GetSelected (out iter)) {
  405. storeEntries.Remove (ref iter);
  406. }
  407. }
  408. protected virtual void OnButtonRemoveEntryClicked(object sender, System.EventArgs e)
  409. {
  410. Gtk.TreeIter iter;
  411. if (treeEntries.Selection.GetSelected (out iter)) {
  412. string key = (string) storeEntries.GetValue (iter, 1);
  413. entry.RemoveEntry (key);
  414. storeEntries.Remove (ref iter);
  415. if (!iter.Equals (TreeIter.Zero))
  416. treeEntries.Selection.SelectIter (iter);
  417. }
  418. }
  419. }
  420. }