From 99f758997c2a504c8ed3c49d3eeb253552107e6f Mon Sep 17 00:00:00 2001 From: Peter Keresztes Schmidt Date: Sun, 3 Sep 2017 20:01:10 +0200 Subject: [PATCH] formatting: Notifications, apply codestyle We tidy up some things and add some sugar: - tab => space conversion; - brace placement and; - single line get/set => lambda. --- .../Notifications/Notification.cs | 724 +++++++++--------- .../Notifications/Notifications.cs | 128 ++-- 2 files changed, 426 insertions(+), 426 deletions(-) diff --git a/src/Libraries/Notifications/Notifications/Notification.cs b/src/Libraries/Notifications/Notifications/Notification.cs index 7aef8006b..24a85df26 100644 --- a/src/Libraries/Notifications/Notifications/Notification.cs +++ b/src/Libraries/Notifications/Notifications/Notification.cs @@ -21,373 +21,373 @@ */ using System; -using System.Reflection; using System.Collections.Generic; +using System.Reflection; +using System.Runtime.InteropServices; +using DBus; using Gdk; using Gtk; -using DBus; -using EventHandler = System.EventHandler; -using EventArgs = System.EventArgs; - -namespace Notifications { - public enum Urgency : byte { - Low = 0, - Normal, - Critical - } - - public class ActionArgs : EventArgs { - private string action; - public string Action { - get { return action; } - } - - public ActionArgs (string action) { - this.action = action; - } - } - - public class CloseArgs : EventArgs { - private CloseReason reason; - public CloseReason Reason { - get { return reason; } - } - - public CloseArgs (CloseReason reason) { - this.reason = reason; - } - } - - public delegate void ActionHandler (object o, ActionArgs args); - public delegate void CloseHandler (object o, CloseArgs args); - - public class Notification { - private struct IconData { - public int Width; - public int Height; - public int Rowstride; - public bool HasAlpha; - public int BitsPerSample; - public int NChannels; - public byte[] Pixels; - } - - private struct ActionTuple { - public string Label; - public ActionHandler Handler; - - public ActionTuple (string label, ActionHandler handler) { - Label = label; - Handler = handler; - } - } - - private INotifications nf; - - private bool updates_pending; - private bool shown; - - private string app_name; - private uint id; - private int timeout = -1; - private string summary = String.Empty, body = String.Empty; - private string icon = String.Empty; - private Widget attach_widget; - private StatusIcon status_icon; - private IDictionary action_map = new Dictionary (); - private IDictionary hints = new Dictionary (); - - public event EventHandler Closed; - - static Notification () { - BusG.Init (); - } - - public Notification () { - nf = Global.DBusObject; - - nf.NotificationClosed += OnClosed; - nf.ActionInvoked += OnActionInvoked; - - Assembly app_asm = Assembly.GetEntryAssembly (); - - if (app_asm == null) { - app_asm = Assembly.GetCallingAssembly (); - } - - app_name = app_asm.GetName ().Name; - } - - public Notification (string summary, string body) : this () { - this.summary = summary; - this.body = body; - } - - public Notification (string summary, string body, string icon) : this (summary, body) { - this.icon = icon; - } - - public Notification (string summary, string body, Pixbuf icon) : this (summary, body) { - SetPixbufHint (icon); - } - - public Notification (string summary, string body, Pixbuf icon, Widget widget) : this (summary, body, icon) { - AttachToWidget (widget); - } - - public Notification (string summary, string body, string icon, Widget widget) : this (summary, body, icon) { - AttachToWidget (widget); - } - - public Notification (string summary, string body, Pixbuf icon, StatusIcon status_icon) : this (summary, body, icon) { - AttachToStatusIcon (status_icon); - } - - public Notification (string summary, string body, string icon, StatusIcon status_icon) : this (summary, body, icon) { - AttachToStatusIcon (status_icon); - } - - - public string Summary { - set { - summary = value; - Update (); - } - get { - return summary; - } - } - - public string Body { - set { - body = value; - Update (); - } - get { - return body; - } - } - - public int Timeout { - set { - timeout = value; - Update (); - } - get { - return timeout; - } - } - - public Urgency Urgency { - set { - hints["urgency"] = (byte) value; - Update (); - } - get { - return hints.ContainsKey ("urgency") ? (Urgency) hints["urgency"] : Urgency.Normal; - } - } - - public string Category { - set { - hints["category"] = value; - Update (); - } - get { - return hints.ContainsKey ("category") ? (string) hints["category"] : String.Empty; - } - - } - - public Pixbuf Icon { - set { - SetPixbufHint (value); - icon = String.Empty; - Update (); - } - } - - public string IconName { - set { - icon = value; - hints.Remove ("icon_data"); - Update (); - } - } - - public uint Id { - get { - return id; - } - } - - public Widget AttachWidget { - get { - return attach_widget; - } - set { - AttachToWidget (value); - } - } - - public StatusIcon StatusIcon { - get { - return status_icon; - } - set { - AttachToStatusIcon (value); - } - } - - private void SetPixbufHint (Pixbuf pixbuf) { - IconData icon_data = new IconData (); - icon_data.Width = pixbuf.Width; - icon_data.Height = pixbuf.Height; - icon_data.Rowstride = pixbuf.Rowstride; - icon_data.HasAlpha = pixbuf.HasAlpha; - icon_data.BitsPerSample = pixbuf.BitsPerSample; - icon_data.NChannels = pixbuf.NChannels; - - int len = (icon_data.Height - 1) * icon_data.Rowstride + icon_data.Width * - ((icon_data.NChannels * icon_data.BitsPerSample + 7) / 8); - icon_data.Pixels = new byte[len]; - System.Runtime.InteropServices.Marshal.Copy (pixbuf.Pixels, icon_data.Pixels, 0, len); - - hints["icon_data"] = icon_data; - } - - public void AttachToWidget (Widget widget) { - if (widget == null) { - throw new ArgumentNullException (nameof(widget)); +using Window = Gtk.Window; + +namespace Notifications +{ + public enum Urgency : byte + { + Low = 0, + Normal, + Critical + } + + public class ActionArgs : EventArgs + { + public ActionArgs (string action) + { + Action = action; + } + + public string Action { get; } + } + + public class CloseArgs : EventArgs + { + public CloseArgs (CloseReason reason) + { + Reason = reason; + } + + public CloseReason Reason { get; } + } + + public delegate void ActionHandler (object o, ActionArgs args); + + public delegate void CloseHandler (object o, CloseArgs args); + + public class Notification + { + private readonly IDictionary action_map = new Dictionary (); + + private readonly string app_name; + private Widget attach_widget; + private readonly IDictionary hints = new Dictionary (); + private string icon = string.Empty; + + private readonly INotifications nf; + private bool shown; + private StatusIcon status_icon; + private string summary = string.Empty, body = string.Empty; + private int timeout = -1; + + private bool updates_pending; + + static Notification () + { + BusG.Init (); + } + + public Notification () + { + nf = Global.DBusObject; + + nf.NotificationClosed += OnClosed; + nf.ActionInvoked += OnActionInvoked; + + var app_asm = Assembly.GetEntryAssembly () ?? Assembly.GetCallingAssembly (); + + app_name = app_asm.GetName ().Name; + } + + public Notification (string summary, string body) : this () + { + this.summary = summary; + this.body = body; + } + + public Notification (string summary, string body, string icon) : this (summary, body) + { + this.icon = icon; + } + + public Notification (string summary, string body, Pixbuf icon) : this (summary, body) + { + SetPixbufHint (icon); + } + + public Notification (string summary, string body, Pixbuf icon, Widget widget) : this (summary, body, icon) + { + AttachToWidget (widget); + } + + public Notification (string summary, string body, string icon, Widget widget) : this (summary, body, icon) + { + AttachToWidget (widget); + } + + public Notification (string summary, string body, Pixbuf icon, StatusIcon status_icon) : this (summary, body, + icon) + { + AttachToStatusIcon (status_icon); + } + + public Notification (string summary, string body, string icon, StatusIcon status_icon) : this (summary, body, + icon) + { + AttachToStatusIcon (status_icon); + } + + + public string Summary { + set { + summary = value; + Update (); + } + get => summary; + } + + public string Body { + set { + body = value; + Update (); + } + get => body; + } + + public int Timeout { + set { + timeout = value; + Update (); + } + get => timeout; + } + + public Urgency Urgency { + set { + hints["urgency"] = (byte) value; + Update (); } + get => hints.ContainsKey ("urgency") ? (Urgency) hints["urgency"] : Urgency.Normal; + } + + public string Category { + set { + hints["category"] = value; + Update (); + } + get => hints.ContainsKey ("category") ? (string) hints["category"] : string.Empty; + } + + public Pixbuf Icon { + set { + SetPixbufHint (value); + icon = string.Empty; + Update (); + } + } + + public string IconName { + set { + icon = value; + hints.Remove ("icon_data"); + Update (); + } + } + + public uint Id { get; private set; } + + public Widget AttachWidget { + get => attach_widget; + set => AttachToWidget (value); + } + + public StatusIcon StatusIcon { + get => status_icon; + set => AttachToStatusIcon (value); + } + + public event EventHandler Closed; - int x, y; - - widget.Window.GetOrigin (out x, out y); - - if (widget.GetType() != typeof (Gtk.Window) || ! widget.GetType().IsSubclassOf(typeof (Gtk.Window))) { - x += widget.Allocation.X; - y += widget.Allocation.Y; - } - - x += widget.Allocation.Width / 2; - y += widget.Allocation.Height / 2; - - SetGeometryHints (widget.Screen, x, y); - attach_widget = widget; - status_icon = null; - } - - public void AttachToStatusIcon (StatusIcon status_icon) { - Screen screen; - Rectangle rect; - Orientation orientation; - int x, y; - - if (!status_icon.GetGeometry (out screen, out rect, out orientation)) { - return; - } - - x = rect.X + rect.Width / 2; - y = rect.Y + rect.Height / 2; - - SetGeometryHints (screen, x, y); - - this.status_icon = status_icon; - attach_widget = null; - } - - public void SetGeometryHints (Screen screen, int x, int y) { - hints["x"] = x; - hints["y"] = y; - hints["xdisplay"] = screen.MakeDisplayName (); - Update (); - } - - private void Update () { - if (shown && !updates_pending) { - updates_pending = true; - GLib.Timeout.Add (100, delegate { - if (updates_pending) { - Show (); - updates_pending = false; - } - return false; - }); - } - } - - public void Show () { - string[] actions; - lock (action_map) { - actions = new string[action_map.Keys.Count * 2]; - int i = 0; - foreach (KeyValuePair pair in action_map) { - actions[i++] = pair.Key; - actions[i++] = pair.Value.Label; - } - } - id = nf.Notify (app_name, id, icon, summary, body, actions, hints, timeout); - shown = true; - } - - public void Close () { - nf.CloseNotification (id); - id = 0; - shown = false; - } - - private void OnClosed (uint id, uint reason) { - if (this.id == id) { - this.id = 0; - shown = false; - if (Closed != null) { - Closed (this, new CloseArgs ((CloseReason) reason)); - } - } - } - - public void AddAction (string action, string label, ActionHandler handler) { - if (Global.Capabilities != null && - Array.IndexOf (Global.Capabilities, "actions") > -1) { - lock (action_map) { - action_map[action] = new ActionTuple (label, handler); - } - Update (); - } - } - - public void RemoveAction (string action) { - lock (action_map) { - action_map.Remove (action); - } - Update (); - } - - public void ClearActions () { - lock (action_map) { - action_map.Clear (); - } - Update (); - } - - private void OnActionInvoked (uint id, string action) { - lock (action_map) { - if (this.id == id && action_map.ContainsKey (action)) - action_map[action].Handler (this, new ActionArgs (action)); - } - } - - public void AddHint (string name, object value) { - hints[name] = value; - Update (); - } - - public void RemoveHint (string name) { - hints.Remove (name); - Update (); - } - } + private void SetPixbufHint (Pixbuf pixbuf) + { + var icon_data = new IconData + { + Width = pixbuf.Width, + Height = pixbuf.Height, + Rowstride = pixbuf.Rowstride, + HasAlpha = pixbuf.HasAlpha, + BitsPerSample = pixbuf.BitsPerSample, + NChannels = pixbuf.NChannels + }; + + var len = (icon_data.Height - 1) * icon_data.Rowstride + icon_data.Width * + ((icon_data.NChannels * icon_data.BitsPerSample + 7) / 8); + icon_data.Pixels = new byte[len]; + Marshal.Copy (pixbuf.Pixels, icon_data.Pixels, 0, len); + + hints["icon_data"] = icon_data; + } + + public void AttachToWidget (Widget widget) + { + if (widget == null) throw new ArgumentNullException (nameof(widget)); + + int x, y; + + widget.Window.GetOrigin (out x, out y); + + if (widget.GetType () != typeof(Window) || !widget.GetType ().IsSubclassOf (typeof(Window))) { + x += widget.Allocation.X; + y += widget.Allocation.Y; + } + + x += widget.Allocation.Width / 2; + y += widget.Allocation.Height / 2; + + SetGeometryHints (widget.Screen, x, y); + attach_widget = widget; + status_icon = null; + } + + public void AttachToStatusIcon (StatusIcon status_icon) + { + Screen screen; + Rectangle rect; + Orientation orientation; + int x, y; + + if (!status_icon.GetGeometry (out screen, out rect, out orientation)) return; + + x = rect.X + rect.Width / 2; + y = rect.Y + rect.Height / 2; + + SetGeometryHints (screen, x, y); + + this.status_icon = status_icon; + attach_widget = null; + } + + public void SetGeometryHints (Screen screen, int x, int y) + { + hints["x"] = x; + hints["y"] = y; + hints["xdisplay"] = screen.MakeDisplayName (); + Update (); + } + + private void Update () + { + if (shown && !updates_pending) { + updates_pending = true; + GLib.Timeout.Add (100, delegate + { + if (updates_pending) { + Show (); + updates_pending = false; + } + return false; + }); + } + } + + public void Show () + { + string[] actions; + lock (action_map) { + actions = new string[action_map.Keys.Count * 2]; + var i = 0; + foreach (var pair in action_map) { + actions[i++] = pair.Key; + actions[i++] = pair.Value.Label; + } + } + Id = nf.Notify (app_name, Id, icon, summary, body, actions, hints, timeout); + shown = true; + } + + public void Close () + { + nf.CloseNotification (Id); + Id = 0; + shown = false; + } + + private void OnClosed (uint id, uint reason) + { + if (Id == id) { + Id = 0; + shown = false; + Closed?.Invoke (this, new CloseArgs ((CloseReason) reason)); + } + } + + public void AddAction (string action, string label, ActionHandler handler) + { + if (Global.Capabilities != null && + Array.IndexOf (Global.Capabilities, "actions") > -1) { + lock (action_map) { + action_map[action] = new ActionTuple (label, handler); + } + Update (); + } + } + + public void RemoveAction (string action) + { + lock (action_map) { + action_map.Remove (action); + } + Update (); + } + + public void ClearActions () + { + lock (action_map) { + action_map.Clear (); + } + Update (); + } + + private void OnActionInvoked (uint id, string action) + { + lock (action_map) { + if (Id == id && action_map.ContainsKey (action)) + action_map[action].Handler (this, new ActionArgs (action)); + } + } + + public void AddHint (string name, object value) + { + hints[name] = value; + Update (); + } + + public void RemoveHint (string name) + { + hints.Remove (name); + Update (); + } + + private struct IconData + { + public int Width; + public int Height; + public int Rowstride; + public bool HasAlpha; + public int BitsPerSample; + public int NChannels; + public byte[] Pixels; + } + + private struct ActionTuple + { + public readonly string Label; + public readonly ActionHandler Handler; + + public ActionTuple (string label, ActionHandler handler) + { + Label = label; + Handler = handler; + } + } + } } diff --git a/src/Libraries/Notifications/Notifications/Notifications.cs b/src/Libraries/Notifications/Notifications/Notifications.cs index 4eb4cb13d..504ddbe6b 100644 --- a/src/Libraries/Notifications/Notifications/Notifications.cs +++ b/src/Libraries/Notifications/Notifications/Notifications.cs @@ -25,68 +25,68 @@ using DBus; using org.freedesktop.DBus; -namespace Notifications { - [Interface ("org.freedesktop.Notifications")] - internal interface INotifications : Introspectable, Properties { - ServerInformation GetServerInformation (); - string[] GetCapabilities (); - void CloseNotification (uint id); - uint Notify (string app_name, uint id, string icon, string summary, string body, - string[] actions, IDictionary hints, int timeout); - event NotificationClosedHandler NotificationClosed; - event ActionInvokedHandler ActionInvoked; - } - - public enum CloseReason : uint { - Expired = 1, - User = 2, - API = 3, - Reserved = 4 - } - - internal delegate void NotificationClosedHandler (uint id, uint reason); - internal delegate void ActionInvokedHandler (uint id, string action); - - public struct ServerInformation { - public string Name; - public string Vendor; - public string Version; - public string SpecVersion; - } - - public static class Global { - private const string interface_name = "org.freedesktop.Notifications"; - private const string object_path = "/org/freedesktop/Notifications"; - - private static INotifications dbus_object; - private static object dbus_object_lock = new object (); - - internal static INotifications DBusObject { - get { - if (dbus_object != null) - return dbus_object; - - lock (dbus_object_lock) { - if (! Bus.Session.NameHasOwner (interface_name)) - Bus.Session.StartServiceByName (interface_name); - - dbus_object = Bus.Session.GetObject - (interface_name, new ObjectPath (object_path)); - return dbus_object; - } - } - } - - public static string[] Capabilities { - get { - return DBusObject.GetCapabilities (); - } - } - - public static ServerInformation ServerInformation { - get { - return DBusObject.GetServerInformation (); - } - } - } +namespace Notifications +{ + [Interface ("org.freedesktop.Notifications")] + internal interface INotifications : Introspectable, Properties + { + ServerInformation GetServerInformation (); + string[] GetCapabilities (); + void CloseNotification (uint id); + + uint Notify (string app_name, uint id, string icon, string summary, string body, + string[] actions, IDictionary hints, int timeout); + + event NotificationClosedHandler NotificationClosed; + event ActionInvokedHandler ActionInvoked; + } + + public enum CloseReason : uint + { + Expired = 1, + User = 2, + API = 3, + Reserved = 4 + } + + internal delegate void NotificationClosedHandler (uint id, uint reason); + + internal delegate void ActionInvokedHandler (uint id, string action); + + public struct ServerInformation + { + public string Name; + public string Vendor; + public string Version; + public string SpecVersion; + } + + public static class Global + { + private const string InterfaceName = "org.freedesktop.Notifications"; + private const string ObjectPath = "/org/freedesktop/Notifications"; + + private static INotifications dbus_object; + private static readonly object DbusObjectLock = new object (); + + internal static INotifications DBusObject { + get { + if (dbus_object != null) + return dbus_object; + + lock (DbusObjectLock) { + if (!Bus.Session.NameHasOwner (InterfaceName)) + Bus.Session.StartServiceByName (InterfaceName); + + dbus_object = Bus.Session.GetObject + (InterfaceName, new ObjectPath (ObjectPath)); + return dbus_object; + } + } + } + + public static string[] Capabilities => DBusObject.GetCapabilities (); + + public static ServerInformation ServerInformation => DBusObject.GetServerInformation (); + } }