From 66b7f0c58dd5fdd0f4f919feaa3a5b9d2657d01a Mon Sep 17 00:00:00 2001 From: Hylke Bons Date: Sun, 3 Jul 2011 18:43:22 +0100 Subject: [PATCH] Add a event log controller --- SparkleShare/Mac/SparkleEventLog.cs | 152 ++++++++---------- SparkleShare/Mac/SparkleEventLogController.cs | 109 +++++++++++++ SparkleShare/Mac/SparkleShare.csproj | 3 +- SparkleShare/Mac/SparkleUI.cs | 25 --- SparkleShare/SparkleController.cs | 1 - 5 files changed, 174 insertions(+), 116 deletions(-) create mode 100644 SparkleShare/Mac/SparkleEventLogController.cs diff --git a/SparkleShare/Mac/SparkleEventLog.cs b/SparkleShare/Mac/SparkleEventLog.cs index 9c8bd92a..074938dd 100644 --- a/SparkleShare/Mac/SparkleEventLog.cs +++ b/SparkleShare/Mac/SparkleEventLog.cs @@ -33,13 +33,12 @@ namespace SparkleShare { public class SparkleEventLog : NSWindow { + private SparkleEventLogController controller; + private WebView WebView; private NSBox Separator; - private string HTML; private NSPopUpButton popup_button; private NSProgressIndicator ProgressIndicator; - private List change_sets; - private string selected_log = null; public SparkleEventLog (IntPtr handle) : base (handle) { } @@ -61,16 +60,7 @@ namespace SparkleShare { HasShadow = true; BackingType = NSBackingStore.Buffered; - CreateEvents (); - UpdateEvents (false); - UpdateChooser (); - OrderFrontRegardless (); - } - - - private void CreateEvents () - { Separator = new NSBox (new RectangleF (0, 579, 480, 1)) { BorderColor = NSColor.LightGray, BoxType = NSBoxType.NSBoxCustom @@ -78,21 +68,42 @@ namespace SparkleShare { ContentView.AddSubview (Separator); + WebView = new WebView (new RectangleF (0, 0, 480, 579), "", "") { PolicyDelegate = new SparkleWebPolicyDelegate () }; + ProgressIndicator = new NSProgressIndicator () { Style = NSProgressIndicatorStyle.Spinning, Frame = new RectangleF (WebView.Frame.Width / 2 - 10, WebView.Frame.Height / 2 + 10, 20, 20) }; - ProgressIndicator.StartAnimation (this); - Update (); + + UpdateContent (null, false); + UpdateChooser (this.controller.Folders); + OrderFrontRegardless (); + + // Hook up the controller events + this.controller = new SparkleEventLogController (); + + this.controller.UpdateChooserEvent += delegate (string [] folders) { + InvokeOnMainThread (delegate { + UpdateChooser (folders); + }); + }; + + this.controller.UpdateContentEvent += delegate (string html, bool silently) { + InvokeOnMainThread (delegate { + UpdateContent (html, true); + }); + }; } - public void UpdateChooser () + + + public void UpdateChooser (string [] folders) { if (this.popup_button != null) this.popup_button.RemoveFromSuperview (); @@ -104,108 +115,71 @@ namespace SparkleShare { this.popup_button.Cell.ControlSize = NSControlSize.Small; this.popup_button.Font = NSFontManager.SharedFontManager.FontWithFamily - ("Lucida Grande", NSFontTraitMask.Condensed, 0, NSFont.SmallSystemFontSize); + ("Lucida Grande", NSFontTraitMask.Condensed, 0, NSFont.SmallSystemFontSize); this.popup_button.AddItem ("All Folders"); this.popup_button.Menu.AddItem (NSMenuItem.SeparatorItem); - this.popup_button.AddItems (SparkleShare.Controller.Folders.ToArray ()); - - if (this.selected_log != null && - !SparkleShare.Controller.Folders.Contains (this.selected_log)) { - - this.selected_log = null; - } + this.popup_button.AddItems (folders); this.popup_button.Activated += delegate { if (this.popup_button.IndexOfSelectedItem == 0) - this.selected_log = null; + this.controller.SelectedFolder = null; else - this.selected_log = this.popup_button.SelectedItem.Title; - - UpdateEvents (false); + this.controller.SelectedFolder = this.popup_button.SelectedItem.Title; }; ContentView.AddSubview (this.popup_button); } - public void UpdateEvents () + public void UpdateContent (string html, bool silently) { - UpdateEvents (true); - } + if (!silently) { + if (WebView.Superview == ContentView) + WebView.RemoveFromSuperview (); - - public void UpdateEvents (bool silent) - { - if (!silent) { - InvokeOnMainThread (delegate { - if (WebView.Superview == ContentView) - WebView.RemoveFromSuperview (); - - ContentView.AddSubview (ProgressIndicator); - }); + ContentView.AddSubview (ProgressIndicator); } Thread thread = new Thread (new ThreadStart (delegate { using (NSAutoreleasePool pool = new NSAutoreleasePool ()) { - Stopwatch watch = new Stopwatch (); - watch.Start (); - this.change_sets = SparkleShare.Controller.GetLog (this.selected_log); - GenerateHTML (); - watch.Stop (); + if (html == null) + html = this.controller.HTML; - // A short delay is less annoying than - // a flashing window - if (watch.ElapsedMilliseconds < 500 && !silent) - Thread.Sleep (500 - (int) watch.ElapsedMilliseconds); + html = html.Replace ("", "Lucida Grande"); + html = html.Replace ("", "13.6px"); + html = html.Replace ("", "13.4px"); + html = html.Replace ("", "#bbb"); + html = html.Replace ("", "#ddd"); + html = html.Replace ("", "#f5f5f5"); + html = html.Replace ("", "#0085cf"); + html = html.Replace ("", "#009ff8"); + html = html.Replace ("", + "file://" + Path.Combine (NSBundle.MainBundle.ResourcePath, "Pixmaps", "avatar-default.png")); + html = html.Replace ("", + "file://" + Path.Combine (NSBundle.MainBundle.ResourcePath, "Pixmaps", "document-added-12.png")); + html = html.Replace ("", + "file://" + Path.Combine (NSBundle.MainBundle.ResourcePath, "Pixmaps", "document-deleted-12.png")); + html = html.Replace ("", + "file://" + Path.Combine (NSBundle.MainBundle.ResourcePath, "Pixmaps", "document-edited-12.png")); + html = html.Replace ("", + "file://" + Path.Combine (NSBundle.MainBundle.ResourcePath, "Pixmaps", "document-moved-12.png")); - AddHTML (); + InvokeOnMainThread (delegate { + if (ProgressIndicator.Superview == ContentView) + ProgressIndicator.RemoveFromSuperview (); + + WebView.MainFrame.LoadHtmlString (html, new NSUrl ("")); + ContentView.AddSubview (WebView); + Update (); + }); } })); thread.Start (); } - - - private void GenerateHTML () - { - HTML = SparkleShare.Controller.GetHTMLLog (this.change_sets); - - HTML = HTML.Replace ("", "Lucida Grande"); - HTML = HTML.Replace ("", "13.6px"); - HTML = HTML.Replace ("", "13.4px"); - HTML = HTML.Replace ("", "#bbb"); - HTML = HTML.Replace ("", "#ddd"); - HTML = HTML.Replace ("", "#f5f5f5"); - HTML = HTML.Replace ("", "#0085cf"); - HTML = HTML.Replace ("", "#009ff8"); - HTML = HTML.Replace ("", - "file://" + Path.Combine (NSBundle.MainBundle.ResourcePath, "Pixmaps", "avatar-default.png")); - HTML = HTML.Replace ("", - "file://" + Path.Combine (NSBundle.MainBundle.ResourcePath, "Pixmaps", "document-added-12.png")); - HTML = HTML.Replace ("", - "file://" + Path.Combine (NSBundle.MainBundle.ResourcePath, "Pixmaps", "document-deleted-12.png")); - HTML = HTML.Replace ("", - "file://" + Path.Combine (NSBundle.MainBundle.ResourcePath, "Pixmaps", "document-edited-12.png")); - HTML = HTML.Replace ("", - "file://" + Path.Combine (NSBundle.MainBundle.ResourcePath, "Pixmaps", "document-moved-12.png")); - } - - - private void AddHTML () - { - InvokeOnMainThread (delegate { - if (ProgressIndicator.Superview == ContentView) - ProgressIndicator.RemoveFromSuperview (); - - WebView.MainFrame.LoadHtmlString (HTML, new NSUrl ("")); - ContentView.AddSubview (WebView); - Update (); - }); - } } - public class SparkleEventsDelegate : NSWindowDelegate { public override bool WindowShouldClose (NSObject sender) diff --git a/SparkleShare/Mac/SparkleEventLogController.cs b/SparkleShare/Mac/SparkleEventLogController.cs new file mode 100644 index 00000000..1dd9be43 --- /dev/null +++ b/SparkleShare/Mac/SparkleEventLogController.cs @@ -0,0 +1,109 @@ +// SparkleShare, a collaboration and sharing tool. +// Copyright (C) 2010 Hylke Bons +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + + +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Threading; + +using SparkleLib; + +namespace SparkleShare { + + public class SparkleEventLogController { + + public event UpdateContentEventEventHandler UpdateContentEvent; + public delegate void UpdateContentEventEventHandler (string html, bool silently); + + public event UpdateChooserEventHandler UpdateChooserEvent; + public delegate void UpdateChooserEventHandler (string [] folders); + + public string SelectedFolder { + get { + return this.selected_folder; + } + + set { + this.selected_folder = value; + + if (UpdateContentEvent != null) + UpdateContentEvent (HTML, true); + } + } + + public string HTML { + get { + Stopwatch watch = new Stopwatch (); + watch.Start (); + + List change_sets = SparkleShare.Controller.GetLog (SelectedFolder); + string html = SparkleShare.Controller.GetHTMLLog (change_sets); + + watch.Stop (); + + // A short delay is less annoying than + // a flashing window + if (watch.ElapsedMilliseconds < 500 /* && !silent */) + Thread.Sleep (500 - (int) watch.ElapsedMilliseconds); + + return html; + } + } + + public string [] Folders { + get { + return SparkleShare.Controller.Folders.ToArray (); + } + } + + + private string selected_folder; + + + public SparkleEventLogController () + { + SparkleShare.Controller.AvatarFetched += delegate { + if (UpdateContentEvent != null) + UpdateContentEvent (HTML, true); + }; + + SparkleShare.Controller.OnIdle += delegate { + if (UpdateContentEvent != null) + UpdateContentEvent (HTML, true); + }; + + SparkleShare.Controller.FolderListChanged += delegate { + if (this.selected_folder != null && + !SparkleShare.Controller.Folders.Contains (this.selected_folder)) { + + this.selected_folder = null; + } + + if (UpdateChooserEvent != null) + UpdateChooserEvent (Folders); + + if (UpdateContentEvent != null) + UpdateContentEvent (HTML, true); + }; + + SparkleShare.Controller.NotificationRaised += delegate { + if (UpdateContentEvent != null) + UpdateContentEvent (HTML, true); + }; + } + } +} diff --git a/SparkleShare/Mac/SparkleShare.csproj b/SparkleShare/Mac/SparkleShare.csproj index d5fc5b16..4da38ecf 100644 --- a/SparkleShare/Mac/SparkleShare.csproj +++ b/SparkleShare/Mac/SparkleShare.csproj @@ -62,7 +62,7 @@ False ..\..\bin\Meebey.SmartIrc4net.dll - + False ..\..\bin\SparkleLib.dll @@ -91,6 +91,7 @@ + diff --git a/SparkleShare/Mac/SparkleUI.cs b/SparkleShare/Mac/SparkleUI.cs index d4e24aeb..efd46f7f 100644 --- a/SparkleShare/Mac/SparkleUI.cs +++ b/SparkleShare/Mac/SparkleUI.cs @@ -96,8 +96,6 @@ namespace SparkleShare { SparkleShare.Controller.NotificationRaised += delegate (string user_name, string user_email, string message, string repository_path) { InvokeOnMainThread (delegate { - if (EventLog != null) - EventLog.UpdateEvents (); if (SparkleShare.Controller.NotificationsEnabled) { if (NSApplication.SharedApplication.DockTile.BadgeLabel == null) @@ -130,30 +128,7 @@ namespace SparkleShare { }; - SparkleShare.Controller.AvatarFetched += delegate { - InvokeOnMainThread (delegate { - if (EventLog != null) - EventLog.UpdateEvents (); - }); - }; - - SparkleShare.Controller.OnIdle += delegate { - InvokeOnMainThread (delegate { - if (EventLog != null) - EventLog.UpdateEvents (); - }); - }; - - - SparkleShare.Controller.FolderListChanged += delegate { - InvokeOnMainThread (delegate { - if (EventLog != null) { - EventLog.UpdateChooser (); - EventLog.UpdateEvents (); - } - }); - }; if (SparkleShare.Controller.FirstRun) { diff --git a/SparkleShare/SparkleController.cs b/SparkleShare/SparkleController.cs index 1ed7642e..1c4377b2 100644 --- a/SparkleShare/SparkleController.cs +++ b/SparkleShare/SparkleController.cs @@ -1006,7 +1006,6 @@ namespace SparkleShare { fetcher.Failed += delegate { - if (FolderFetchError != null) FolderFetchError ();