/SparkleShare/SparkleEventLog.cs
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 17 18using System; 19using System.Collections.Generic; 20using System.Threading; 21 22using Gtk; 23using Mono.Unix; 24using WebKit; 25 26namespace SparkleShare { 27 28 public class SparkleEventLog : Window { 29 30 public SparkleEventLogController Controller = new SparkleEventLogController (); 31 32 private Label size_label; 33 private HBox layout_horizontal; 34 private ComboBox combo_box; 35 private EventBox content_wrapper; 36 private ScrolledWindow scrolled_window; 37 private WebView web_view; 38 private SparkleSpinner spinner; 39 private string link_status; 40 41 42 // Short alias for the translations 43 public static string _ (string s) 44 { 45 return Catalog.GetString (s); 46 } 47 48 49 public SparkleEventLog () : base ("") 50 { 51 SetSizeRequest (480, 640); 52 SetPosition (WindowPosition.Center); 53 54 Resizable = true; 55 BorderWidth = 0; 56 57 Title = _("Recent Events"); 58 IconName = "folder-sparkleshare"; 59 60 DeleteEvent += Close; 61 62 this.size_label = new Label () { 63 Markup = "<b>Size:</b> � <b>History:</b> �" 64 }; 65 66 VBox layout_vertical = new VBox (false, 0); 67 this.spinner = new SparkleSpinner (22); 68 this.content_wrapper = new EventBox (); 69 this.scrolled_window = new ScrolledWindow (); 70 71 this.web_view = new WebView () { 72 Editable = false 73 }; 74 75 this.web_view.HoveringOverLink += delegate (object o, WebKit.HoveringOverLinkArgs args) { 76 this.link_status = args.Link; 77 }; 78 79 this.web_view.NavigationRequested += delegate (object o, WebKit.NavigationRequestedArgs args) { 80 if (args.Request.Uri == this.link_status) 81 SparkleEventLogController.LinkClicked (args.Request.Uri); 82 83 // Don't follow HREFs (as this would cause a page refresh) 84 if (!args.Request.Uri.Equals ("file:")) 85 args.RetVal = 1; 86 }; 87 88 this.scrolled_window.Add (this.web_view); 89 this.content_wrapper.Add (this.spinner); 90 91 this.spinner.Start (); 92 93 this.layout_horizontal = new HBox (false, 0); 94 this.layout_horizontal.PackStart (this.size_label, true, true, 0); 95 this.layout_horizontal.PackStart (new Label (" "), false, false, 0); 96 97 layout_vertical.PackStart (this.layout_horizontal, false, false, 0); 98 layout_vertical.PackStart (CreateShortcutsBar (), false, false, 0); 99 layout_vertical.PackStart (this.content_wrapper, true, true, 0); 100 101 Add (layout_vertical); 102 ShowAll (); 103 104 UpdateChooser (null); 105 UpdateContent (null); 106 107 108 // Hook up the controller events 109 Controller.UpdateChooserEvent += delegate (string [] folders) { 110 Application.Invoke (delegate { 111 UpdateChooser (folders); 112 }); 113 }; 114 115 Controller.UpdateContentEvent += delegate (string html) { 116 Application.Invoke (delegate { 117 UpdateContent (html); 118 }); 119 }; 120 121 Controller.ContentLoadingEvent += delegate { 122 Application.Invoke (delegate { 123 if (this.content_wrapper.Child != null) 124 this.content_wrapper.Remove (this.content_wrapper.Child); 125 126 this.content_wrapper.Add (this.spinner); 127 this.spinner.Start (); 128 this.content_wrapper.ShowAll (); 129 }); 130 }; 131 132 Controller.UpdateSizeInfoEvent += delegate (string size, string history_size) { 133 Application.Invoke (delegate { 134 this.size_label.Markup = "<b>Size:</b> " + size + " " + 135 "<b>History:</b> " + history_size; 136 137 this.size_label.ShowAll (); 138 }); 139 }; 140 } 141 142 143 public void UpdateChooser (string [] folders) 144 { 145 if (folders == null) 146 folders = Controller.Folders; 147 148 if (this.combo_box != null && this.combo_box.Parent != null) 149 this.layout_horizontal.Remove (this.combo_box); 150 151 this.combo_box = new ComboBox (); 152 153 CellRendererText cell = new CellRendererText(); 154 this.combo_box.PackStart (cell, false); 155 this.combo_box.AddAttribute (cell, "text", 0); 156 157 ListStore store = new ListStore (typeof (string)); 158 159 store.AppendValues (_("All Projects")); 160 store.AppendValues ("---"); 161 162 foreach (string folder in folders) 163 store.AppendValues (folder); 164 165 this.combo_box.Model = store; 166 this.combo_box.Active = 0; 167 168 this.combo_box.RowSeparatorFunc = delegate (TreeModel model, TreeIter iter) { 169 string item = (string) this.combo_box.Model.GetValue (iter, 0); 170 return (item == "---"); 171 }; 172 173 this.combo_box.Changed += delegate { 174 TreeIter iter; 175 this.combo_box.GetActiveIter (out iter); 176 string selection = (string) this.combo_box.Model.GetValue (iter, 0); 177 178 if (selection.Equals (_("All Folders"))) 179 Controller.SelectedFolder = null; 180 else 181 Controller.SelectedFolder = selection; 182 }; 183 184 this.layout_horizontal.BorderWidth = 9; 185 this.layout_horizontal.PackStart (this.combo_box, true, true, 0); 186 this.layout_horizontal.ShowAll (); 187 } 188 189 190 public void UpdateContent (string html) 191 { 192 Thread thread = new Thread (new ThreadStart (delegate { 193 if (html == null) 194 html = Controller.HTML; 195 196 if (html == null) 197 return; 198 199 html = html.Replace ("<!-- $body-font-size -->", (double) (Style.FontDescription.Size / 1024 + 3) + "px"); 200 html = html.Replace ("<!-- $day-entry-header-font-size -->", (Style.FontDescription.Size / 1024 + 3) + "px"); 201 html = html.Replace ("<!-- $a-color -->", "#0085cf"); 202 html = html.Replace ("<!-- $a-hover-color -->", "#009ff8"); 203 html = html.Replace ("<!-- $body-font-family -->", "\"" + Style.FontDescription.Family + "\""); 204 html = html.Replace ("<!-- $body-color -->", SparkleUIHelpers.GdkColorToHex (Style.Foreground (StateType.Normal))); 205 html = html.Replace ("<!-- $body-background-color -->", SparkleUIHelpers.GdkColorToHex (new TreeView ().Style.Base (StateType.Normal))); 206 html = html.Replace ("<!-- $day-entry-header-background-color -->", SparkleUIHelpers.GdkColorToHex (Style.Background (StateType.Normal))); 207 html = html.Replace ("<!-- $secondary-font-color -->", SparkleUIHelpers.GdkColorToHex (Style.Foreground (StateType.Insensitive))); 208 html = html.Replace ("<!-- $small-color -->", SparkleUIHelpers.GdkColorToHex (Style.Foreground (StateType.Insensitive))); 209 html = html.Replace ("<!-- $no-buddy-icon-background-image -->", "file://" + 210 new string [] {SparkleUI.AssetsPath, "icons", 211 "hicolor", "32x32", "status", "avatar-default.png"}.Combine ()); 212 html = html.Replace ("<!-- $document-added-background-image -->", "file://" + 213 new string [] {SparkleUI.AssetsPath, "icons", 214 "hicolor", "12x12", "status", "document-added.png"}.Combine ()); 215 html = html.Replace ("<!-- $document-edited-background-image -->", "file://" + 216 new string [] {SparkleUI.AssetsPath, "icons", 217 "hicolor", "12x12", "status", "document-edited.png"}.Combine ()); 218 html = html.Replace ("<!-- $document-deleted-background-image -->", "file://" + 219 new string [] {SparkleUI.AssetsPath, "icons", 220 "hicolor", "12x12", "status", "document-deleted.png"}.Combine ()); 221 html = html.Replace ("<!-- $document-moved-background-image -->", "file://" + 222 new string [] {SparkleUI.AssetsPath, "icons", 223 "hicolor", "12x12", "status", "document-moved.png"}.Combine ()); 224 225 Application.Invoke (delegate { 226 this.spinner.Stop (); 227 this.web_view.LoadString (html, null, null, "file:///"); 228 this.content_wrapper.Remove (this.content_wrapper.Child); 229 this.content_wrapper.Add (this.scrolled_window); 230 this.content_wrapper.ShowAll (); 231 }); 232 })); 233 234 thread.Start (); 235 } 236 237 238 public void Close (object o, DeleteEventArgs args) 239 { 240 HideAll (); 241 args.RetVal = true; 242 } 243 244 245 private MenuBar CreateShortcutsBar () 246 { 247 // Adds a hidden menubar that contains to enable keyboard 248 // shortcuts to close the log 249 MenuBar menu_bar = new MenuBar (); 250 251 MenuItem file_item = new MenuItem ("File"); 252 253 Menu file_menu = new Menu (); 254 255 MenuItem close_1 = new MenuItem ("Close1"); 256 MenuItem close_2 = new MenuItem ("Close2"); 257 258 // adds specific Ctrl+W and Esc key accelerators to Log Window 259 AccelGroup accel_group = new AccelGroup (); 260 AddAccelGroup (accel_group); 261 262 // Close on Esc 263 close_1.AddAccelerator ("activate", accel_group, new AccelKey (Gdk.Key.W, Gdk.ModifierType.ControlMask, 264 AccelFlags.Visible)); 265 266 close_1.Activated += delegate { HideAll (); }; 267 268 // Close on Ctrl+W 269 close_2.AddAccelerator ("activate", accel_group, new AccelKey (Gdk.Key.Escape, Gdk.ModifierType.None, 270 AccelFlags.Visible)); 271 close_2.Activated += delegate { HideAll (); }; 272 273 file_menu.Append (close_1); 274 file_menu.Append (close_2); 275 276 file_item.Submenu = file_menu; 277 278 menu_bar.Append (file_item); 279 280 // Hacky way to hide the menubar, but the accellerators 281 // will simply be disabled when using Hide () 282 menu_bar.HeightRequest = 1; 283 menu_bar.ModifyBg (StateType.Normal, Style.Background (StateType.Normal)); 284 285 return menu_bar; 286 } 287 } 288}