[Xfce4-commits] <postler:master> Implement and use Dock.Manager and Dock.Item

Christian Dywan noreply at xfce.org
Fri Mar 11 20:52:02 CET 2011


Updating branch refs/heads/master
         to efcd9ca3174614e1de2e62d76b915c45c0199e08 (commit)
       from 162f22dca3e3fc2451b4499d69c8ef57bf34100b (commit)

commit efcd9ca3174614e1de2e62d76b915c45c0199e08
Author: Christian Dywan <christian at twotoasts.de>
Date:   Fri Mar 11 20:44:27 2011 +0100

    Implement and use Dock.Manager and Dock.Item
    
    Fixes: https://bugs.launchpad.net/postler/+bug/728773

 postler/dockmanager.vala     |  145 ++++++++++++++++++++++++++++++++++++++++++
 postler/postler-service.vala |   12 +++-
 2 files changed, 155 insertions(+), 2 deletions(-)

diff --git a/postler/dockmanager.vala b/postler/dockmanager.vala
new file mode 100644
index 0000000..25d0a5e
--- /dev/null
+++ b/postler/dockmanager.vala
@@ -0,0 +1,145 @@
+/*
+ Copyright (C) 2011 Christian Dywan <christian at twotoasts.de>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ See the file COPYING for the full license text.
+*/
+
+namespace Dock {
+
+    [DBus (name = "org.freedesktop.DockManager")]
+    interface DockManagerService : Object {
+        public abstract ObjectPath[] get_items_by_desktop_file (string desktop_file) throws IOError;
+        public signal void item_added (ObjectPath object_path);
+        public signal void item_removed (ObjectPath object_path);
+    }
+    [DBus (name = "org.freedesktop.DockItem")]
+    interface DockItemService : GLib.DBusProxy {
+        public abstract void update_dock_item (HashTable<string, Variant> hints) throws IOError;
+        public abstract int32 add_menu_item (HashTable<string, Variant> hints) throws IOError;
+        public signal void menu_item_activated (int32 id);
+    }
+
+    public class Manager : Object {
+        DockManagerService? service = null;
+
+        public signal void item_added (ObjectPath object_path);
+        public signal void item_removed (ObjectPath object_path);
+
+        public Manager () {
+            try {
+                service = Bus.get_proxy_sync (BusType.SESSION,
+                                              "org.freedesktop.DockManager",
+                                              "/org/freedesktop/DockManager");
+                service.item_added.connect ((object_path) => {
+                    item_added (object_path);
+                });
+                service.item_removed.connect ((object_path) => {
+                    item_removed (object_path);
+                });
+            } catch (GLib.Error error) {
+                GLib.debug ("No DockManager running: %s", error.message);
+            }
+        }
+
+        public ObjectPath[] get_items_by_desktop_file (string desktop_file) {
+            try {
+                if (service == null)
+                    throw new GLib.IOError.FAILED ("Service unavailable");
+                ObjectPath[] items = service.get_items_by_desktop_file (desktop_file);
+                return items;
+            } catch (GLib.Error error) {
+                return {};
+            }
+        }
+    }
+
+    public class Item : Object {
+        Manager manager = new Manager ();
+        uint last_count = 0;
+        DockItemService? service = null;
+
+        public signal void menu_item_activated (int32 id);
+
+        public Item.for_desktop_file (string desktop_file) {
+            manager.item_added.connect ((oobject_path) => {
+                if (service != null)
+                    return;
+                var dockitems = manager.get_items_by_desktop_file (desktop_file);
+                foreach (ObjectPath object_path in dockitems) {
+                    update_service (object_path);
+                    set_badge (last_count);
+               }
+            });
+            manager.item_removed.connect ((object_path) => {
+                if (object_path.has_suffix (desktop_file))
+                    service = null;
+            });
+
+            var dockitems = manager.get_items_by_desktop_file (desktop_file);
+            foreach (ObjectPath object_path in dockitems) {
+                update_service (object_path);
+                break;
+            }
+        }
+
+        public void set_badge (uint count) {
+            if (service == null)
+                return;
+
+            var hints = new HashTable<string, Variant> (str_hash, str_equal);
+            if (count == 0)
+                hints.insert ("badge", "");
+            else
+                hints.insert ("badge", count.to_string ());
+            update_dock_item (hints);
+            last_count = count;
+        }
+
+        void update_service (ObjectPath object_path) {
+            try {
+                service = Bus.get_proxy_sync (BusType.SESSION,
+                                              "org.freedesktop.DockManager",
+                                              object_path);
+                service.menu_item_activated.connect ((id) => {
+                    menu_item_activated (id);
+                });
+            } catch (GLib.Error error) {
+                GLib.critical ("DockItem not running: %s", error.message);
+            }
+        }
+
+        public Item (ObjectPath object_path) {
+            update_service (object_path);
+        }
+
+        public void update_dock_item (HashTable<string, Variant> hints) {
+            try {
+                if (service == null)
+                    throw new GLib.IOError.FAILED ("Service unavailable");
+                service.update_dock_item (hints);
+            } catch (GLib.Error error) {
+                GLib.critical ("Failed to update DockItem: %s", error.message);
+            }
+        }
+
+        public int32 add_menu_item (string icon_name, string label) {
+            var hints = new HashTable<string, Variant> (str_hash, str_equal);
+            hints.insert ("icon-name", icon_name);
+            hints.insert ("label", label);
+            try {
+                if (service == null)
+                    throw new GLib.IOError.FAILED ("Service unavailable");
+                return service.add_menu_item (hints);
+            } catch (GLib.Error error) {
+                GLib.critical ("Failed to add menu item for DockItem: %s", error.message);
+            }
+            return 0;
+        }
+    }
+}
+
diff --git a/postler/postler-service.vala b/postler/postler-service.vala
index dc3e77d..9b53790 100644
--- a/postler/postler-service.vala
+++ b/postler/postler-service.vala
@@ -93,8 +93,10 @@ namespace Postler {
     class PostlerService : Object {
         double total = 0;
         int unread = 0;
-#if HAVE_INDICATE
         int interval = 600; /* 10 minutes */
+        Dock.Item dockitem;
+
+#if HAVE_INDICATE
         Indicate.Server indicator;
         List<Indicate.Indicator> items;
 
@@ -139,6 +141,7 @@ namespace Postler {
             items.append (item);
             update_inbox_indicator (item);
         }
+#endif
 
         bool new_message_timer () {
             receive ("");
@@ -147,6 +150,10 @@ namespace Postler {
 
         public PostlerService () {
             GLib.Timeout.add_seconds (interval, new_message_timer);
+
+            dockitem = new Dock.Item.for_desktop_file ("postler.desktop");
+
+#if HAVE_INDICATE
             indicator = Indicate.Server.ref_default ();
             indicator.set_type ("message.email");
             indicator.set_desktop_file (
@@ -177,8 +184,8 @@ namespace Postler {
             foreach (var info in accounts.get_infos ())
                 add_inbox_indicator (info);
             indicator.show ();
-        }
 #endif
+        }
 
         void display_status (HelperProcess helper, ref string line) {
             string? error_id = null;
@@ -279,6 +286,7 @@ namespace Postler {
                             return;
                         }
                         GLib.debug ("Done: %d new messages", unread);
+                        dockitem.set_badge (unread);
                         if (unread > 0) {
                             Postler.App.send_notification (
                                 ngettext ("You have %d message",



More information about the Xfce4-commits mailing list