/SparkleShare/SparkleEventLog.cs

http://github.com/hbons/SparkleShare · C# · 288 lines · 195 code · 68 blank · 25 comment · 17 complexity · 1acc7ec782b8b95df7c19f9cea2c083e MD5 · raw file

  1. // SparkleShare, a collaboration and sharing tool.
  2. // Copyright (C) 2010 Hylke Bons <hylkebons@gmail.com>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Threading;
  19. using Gtk;
  20. using Mono.Unix;
  21. using WebKit;
  22. namespace SparkleShare {
  23. public class SparkleEventLog : Window {
  24. public SparkleEventLogController Controller = new SparkleEventLogController ();
  25. private Label size_label;
  26. private HBox layout_horizontal;
  27. private ComboBox combo_box;
  28. private EventBox content_wrapper;
  29. private ScrolledWindow scrolled_window;
  30. private WebView web_view;
  31. private SparkleSpinner spinner;
  32. private string link_status;
  33. // Short alias for the translations
  34. public static string _ (string s)
  35. {
  36. return Catalog.GetString (s);
  37. }
  38. public SparkleEventLog () : base ("")
  39. {
  40. SetSizeRequest (480, 640);
  41. SetPosition (WindowPosition.Center);
  42. Resizable = true;
  43. BorderWidth = 0;
  44. Title = _("Recent Events");
  45. IconName = "folder-sparkleshare";
  46. DeleteEvent += Close;
  47. this.size_label = new Label () {
  48. Markup = "<b>Size:</b> â&#x20AC;? <b>History:</b> â&#x20AC;?"
  49. };
  50. VBox layout_vertical = new VBox (false, 0);
  51. this.spinner = new SparkleSpinner (22);
  52. this.content_wrapper = new EventBox ();
  53. this.scrolled_window = new ScrolledWindow ();
  54. this.web_view = new WebView () {
  55. Editable = false
  56. };
  57. this.web_view.HoveringOverLink += delegate (object o, WebKit.HoveringOverLinkArgs args) {
  58. this.link_status = args.Link;
  59. };
  60. this.web_view.NavigationRequested += delegate (object o, WebKit.NavigationRequestedArgs args) {
  61. if (args.Request.Uri == this.link_status)
  62. SparkleEventLogController.LinkClicked (args.Request.Uri);
  63. // Don't follow HREFs (as this would cause a page refresh)
  64. if (!args.Request.Uri.Equals ("file:"))
  65. args.RetVal = 1;
  66. };
  67. this.scrolled_window.Add (this.web_view);
  68. this.content_wrapper.Add (this.spinner);
  69. this.spinner.Start ();
  70. this.layout_horizontal = new HBox (false, 0);
  71. this.layout_horizontal.PackStart (this.size_label, true, true, 0);
  72. this.layout_horizontal.PackStart (new Label (" "), false, false, 0);
  73. layout_vertical.PackStart (this.layout_horizontal, false, false, 0);
  74. layout_vertical.PackStart (CreateShortcutsBar (), false, false, 0);
  75. layout_vertical.PackStart (this.content_wrapper, true, true, 0);
  76. Add (layout_vertical);
  77. ShowAll ();
  78. UpdateChooser (null);
  79. UpdateContent (null);
  80. // Hook up the controller events
  81. Controller.UpdateChooserEvent += delegate (string [] folders) {
  82. Application.Invoke (delegate {
  83. UpdateChooser (folders);
  84. });
  85. };
  86. Controller.UpdateContentEvent += delegate (string html) {
  87. Application.Invoke (delegate {
  88. UpdateContent (html);
  89. });
  90. };
  91. Controller.ContentLoadingEvent += delegate {
  92. Application.Invoke (delegate {
  93. if (this.content_wrapper.Child != null)
  94. this.content_wrapper.Remove (this.content_wrapper.Child);
  95. this.content_wrapper.Add (this.spinner);
  96. this.spinner.Start ();
  97. this.content_wrapper.ShowAll ();
  98. });
  99. };
  100. Controller.UpdateSizeInfoEvent += delegate (string size, string history_size) {
  101. Application.Invoke (delegate {
  102. this.size_label.Markup = "<b>Size:</b> " + size + " " +
  103. "<b>History:</b> " + history_size;
  104. this.size_label.ShowAll ();
  105. });
  106. };
  107. }
  108. public void UpdateChooser (string [] folders)
  109. {
  110. if (folders == null)
  111. folders = Controller.Folders;
  112. if (this.combo_box != null && this.combo_box.Parent != null)
  113. this.layout_horizontal.Remove (this.combo_box);
  114. this.combo_box = new ComboBox ();
  115. CellRendererText cell = new CellRendererText();
  116. this.combo_box.PackStart (cell, false);
  117. this.combo_box.AddAttribute (cell, "text", 0);
  118. ListStore store = new ListStore (typeof (string));
  119. store.AppendValues (_("All Projects"));
  120. store.AppendValues ("---");
  121. foreach (string folder in folders)
  122. store.AppendValues (folder);
  123. this.combo_box.Model = store;
  124. this.combo_box.Active = 0;
  125. this.combo_box.RowSeparatorFunc = delegate (TreeModel model, TreeIter iter) {
  126. string item = (string) this.combo_box.Model.GetValue (iter, 0);
  127. return (item == "---");
  128. };
  129. this.combo_box.Changed += delegate {
  130. TreeIter iter;
  131. this.combo_box.GetActiveIter (out iter);
  132. string selection = (string) this.combo_box.Model.GetValue (iter, 0);
  133. if (selection.Equals (_("All Folders")))
  134. Controller.SelectedFolder = null;
  135. else
  136. Controller.SelectedFolder = selection;
  137. };
  138. this.layout_horizontal.BorderWidth = 9;
  139. this.layout_horizontal.PackStart (this.combo_box, true, true, 0);
  140. this.layout_horizontal.ShowAll ();
  141. }
  142. public void UpdateContent (string html)
  143. {
  144. Thread thread = new Thread (new ThreadStart (delegate {
  145. if (html == null)
  146. html = Controller.HTML;
  147. if (html == null)
  148. return;
  149. html = html.Replace ("<!-- $body-font-size -->", (double) (Style.FontDescription.Size / 1024 + 3) + "px");
  150. html = html.Replace ("<!-- $day-entry-header-font-size -->", (Style.FontDescription.Size / 1024 + 3) + "px");
  151. html = html.Replace ("<!-- $a-color -->", "#0085cf");
  152. html = html.Replace ("<!-- $a-hover-color -->", "#009ff8");
  153. html = html.Replace ("<!-- $body-font-family -->", "\"" + Style.FontDescription.Family + "\"");
  154. html = html.Replace ("<!-- $body-color -->", SparkleUIHelpers.GdkColorToHex (Style.Foreground (StateType.Normal)));
  155. html = html.Replace ("<!-- $body-background-color -->", SparkleUIHelpers.GdkColorToHex (new TreeView ().Style.Base (StateType.Normal)));
  156. html = html.Replace ("<!-- $day-entry-header-background-color -->", SparkleUIHelpers.GdkColorToHex (Style.Background (StateType.Normal)));
  157. html = html.Replace ("<!-- $secondary-font-color -->", SparkleUIHelpers.GdkColorToHex (Style.Foreground (StateType.Insensitive)));
  158. html = html.Replace ("<!-- $small-color -->", SparkleUIHelpers.GdkColorToHex (Style.Foreground (StateType.Insensitive)));
  159. html = html.Replace ("<!-- $no-buddy-icon-background-image -->", "file://" +
  160. new string [] {SparkleUI.AssetsPath, "icons",
  161. "hicolor", "32x32", "status", "avatar-default.png"}.Combine ());
  162. html = html.Replace ("<!-- $document-added-background-image -->", "file://" +
  163. new string [] {SparkleUI.AssetsPath, "icons",
  164. "hicolor", "12x12", "status", "document-added.png"}.Combine ());
  165. html = html.Replace ("<!-- $document-edited-background-image -->", "file://" +
  166. new string [] {SparkleUI.AssetsPath, "icons",
  167. "hicolor", "12x12", "status", "document-edited.png"}.Combine ());
  168. html = html.Replace ("<!-- $document-deleted-background-image -->", "file://" +
  169. new string [] {SparkleUI.AssetsPath, "icons",
  170. "hicolor", "12x12", "status", "document-deleted.png"}.Combine ());
  171. html = html.Replace ("<!-- $document-moved-background-image -->", "file://" +
  172. new string [] {SparkleUI.AssetsPath, "icons",
  173. "hicolor", "12x12", "status", "document-moved.png"}.Combine ());
  174. Application.Invoke (delegate {
  175. this.spinner.Stop ();
  176. this.web_view.LoadString (html, null, null, "file:///");
  177. this.content_wrapper.Remove (this.content_wrapper.Child);
  178. this.content_wrapper.Add (this.scrolled_window);
  179. this.content_wrapper.ShowAll ();
  180. });
  181. }));
  182. thread.Start ();
  183. }
  184. public void Close (object o, DeleteEventArgs args)
  185. {
  186. HideAll ();
  187. args.RetVal = true;
  188. }
  189. private MenuBar CreateShortcutsBar ()
  190. {
  191. // Adds a hidden menubar that contains to enable keyboard
  192. // shortcuts to close the log
  193. MenuBar menu_bar = new MenuBar ();
  194. MenuItem file_item = new MenuItem ("File");
  195. Menu file_menu = new Menu ();
  196. MenuItem close_1 = new MenuItem ("Close1");
  197. MenuItem close_2 = new MenuItem ("Close2");
  198. // adds specific Ctrl+W and Esc key accelerators to Log Window
  199. AccelGroup accel_group = new AccelGroup ();
  200. AddAccelGroup (accel_group);
  201. // Close on Esc
  202. close_1.AddAccelerator ("activate", accel_group, new AccelKey (Gdk.Key.W, Gdk.ModifierType.ControlMask,
  203. AccelFlags.Visible));
  204. close_1.Activated += delegate { HideAll (); };
  205. // Close on Ctrl+W
  206. close_2.AddAccelerator ("activate", accel_group, new AccelKey (Gdk.Key.Escape, Gdk.ModifierType.None,
  207. AccelFlags.Visible));
  208. close_2.Activated += delegate { HideAll (); };
  209. file_menu.Append (close_1);
  210. file_menu.Append (close_2);
  211. file_item.Submenu = file_menu;
  212. menu_bar.Append (file_item);
  213. // Hacky way to hide the menubar, but the accellerators
  214. // will simply be disabled when using Hide ()
  215. menu_bar.HeightRequest = 1;
  216. menu_bar.ModifyBg (StateType.Normal, Style.Background (StateType.Normal));
  217. return menu_bar;
  218. }
  219. }
  220. }