~ubuntu-branches/ubuntu/precise/gnome-do/precise-proposed

« back to all changes in this revision

Viewing changes to Do/src/Do.UI/NotificationIcon.cs

  • Committer: Bazaar Package Importer
  • Author(s): Christopher James Halse Rogers
  • Date: 2008-09-14 10:09:40 UTC
  • mto: (0.1.8 sid)
  • mto: This revision was merged to the branch mainline in revision 7.
  • Revision ID: james.westby@ubuntu.com-20080914100940-kyghudg7py14bu2z
Tags: upstream-0.6.0.0
ImportĀ upstreamĀ versionĀ 0.6.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* NotificationIcon.cs
 
2
 *
 
3
 * GNOME Do is the legal property of its developers. Please refer to the
 
4
 * COPYRIGHT file distributed with this
 
5
 * source distribution.
 
6
 *
 
7
 * This program is free software: you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation, either version 3 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
 */
 
20
 
 
21
using System;
 
22
using Mono.Unix;
 
23
using Gtk;
 
24
using Gdk;
 
25
using Do.Core;
 
26
using Notifications;
 
27
 
 
28
namespace Do.UI
 
29
{
 
30
        /// <summary>
 
31
        /// Provides a notification area icon for GNOME Do allowing easy
 
32
        /// access to menus, and a way for GNOME Do to alert users of status changes.
 
33
        /// </summary>
 
34
        public class NotificationIcon : StatusIcon
 
35
        {
 
36
                private const int IconSize = 32;
 
37
                private bool updates_available;
 
38
                private Pixbuf normal_icon = IconProvider.PixbufFromIconName 
 
39
                        ("gnome-run", IconSize);
 
40
                private Pixbuf update_icon = IconProvider.PixbufFromIconName
 
41
                        ("software-update-available", IconSize);
 
42
                                
 
43
                public NotificationIcon()
 
44
                {
 
45
                        FromPixbuf = normal_icon;
 
46
                        Tooltip = Catalog.GetString ("Summon GNOME Do with " + 
 
47
                                Do.Preferences.SummonKeyBinding);
 
48
                        Activate += new EventHandler (OnActivateSummonDo);                      
 
49
                        PopupMenu += new PopupMenuHandler (OnTrayIconPopup);
 
50
                        
 
51
                        if (Do.Preferences.StatusIconVisible)
 
52
                                Show ();
 
53
                        else
 
54
                                Hide ();
 
55
                        
 
56
                        updates_available = false;
 
57
                        Addins.NotificationBridge.MessageRequested += SendNotification;
 
58
                }
 
59
                
 
60
                public void Show ()
 
61
                {
 
62
                        Visible = true;
 
63
                }
 
64
                
 
65
                public void Hide ()
 
66
                {
 
67
                        if (!updates_available)
 
68
                                Visible = false;
 
69
                }
 
70
                
 
71
                /// <summary>
 
72
                /// Sets some properties when new plugin updates are available
 
73
                /// </summary>
 
74
                public void NotifyUpdatesAvailable ()
 
75
                {
 
76
                        Show ();
 
77
                        Activate -= OnActivateSummonDo;
 
78
                        Activate += OnActivateStartUpdates;
 
79
                        FromPixbuf = update_icon;
 
80
                        if (!updates_available)
 
81
                                SendNotification ("GNOME Do",
 
82
                                        "Updated plugins are available. Click here to update.",
 
83
                                        "software-update-available");
 
84
                        updates_available = true;
 
85
                }
 
86
                
 
87
                public static void SendNotification (string message)
 
88
                {
 
89
                        SendNotification ("GNOME Do", message, null);
 
90
                }
 
91
                
 
92
                public static void SendNotification (string title, string message)
 
93
                {
 
94
                        SendNotification (title, message, null);
 
95
                }
 
96
                
 
97
                public static void SendNotification (string title, string message, string icon)
 
98
                {
 
99
                        Do.NotificationIcon.Show ();
 
100
                        GLib.Timeout.Add (1000, delegate {
 
101
                                Gtk.Application.Invoke (delegate {
 
102
                                        ShowNotification (title, message, icon);
 
103
                                });
 
104
                                return false;
 
105
                        });
 
106
                }
 
107
                
 
108
                private static void ShowNotification (string title, string message, string icon)
 
109
                {
 
110
                        int x, y;
 
111
                        Gdk.Screen screen;
 
112
 
 
113
                        Do.NotificationIcon.GetLocationOnScreen (
 
114
                                out screen, out x, out y);
 
115
 
 
116
                        Notification msg;
 
117
                        try {
 
118
                                msg = new Notification ();
 
119
                        } catch (Exception e) {
 
120
                                Log.Error ("Could not show notification: " + e.Message);
 
121
                                return;
 
122
                        }
 
123
                        msg.Closed += new EventHandler (OnNotificationClosed); 
 
124
                        msg.Summary = title;
 
125
                        msg.Body = message;
 
126
                        if (icon != null)
 
127
                                msg.Icon = IconProvider.PixbufFromIconName (icon,
 
128
                                        IconSize);
 
129
                        msg.Timeout = message.Length / 10 * 1000;
 
130
                        if (msg.Timeout > 10000) msg.Timeout = 10000;
 
131
                        if (msg.Timeout < 5000) msg.Timeout = 5000;
 
132
                        msg.SetGeometryHints (screen, x, y);
 
133
                        msg.Show ();
 
134
                }
 
135
                
 
136
                public void GetLocationOnScreen (out Gdk.Screen screen, out int x, out int y)
 
137
                {
 
138
                        Gdk.Rectangle area;
 
139
                        Gtk.Orientation orien;
 
140
                        
 
141
                        GetGeometry (out screen, out area, out orien);
 
142
                        x = area.X + area.Width / 2;
 
143
                        y = area.Y + area.Height - 5;
 
144
                }
 
145
                
 
146
                protected void OnActivateSummonDo (object sender, EventArgs args)
 
147
                {
 
148
                        if (!Do.Controller.IsSummoned)
 
149
                                Do.Controller.Summon ();
 
150
                }
 
151
                
 
152
                protected void OnActivateStartUpdates (object sender, EventArgs args)
 
153
                {
 
154
                        try {
 
155
                                PluginManager.InstallAvailableUpdates (true);
 
156
                                Activate -= OnActivateStartUpdates;
 
157
                                updates_available = false;
 
158
                                Pixbuf = normal_icon;
 
159
                                SendNotification ("Plugins successfully updated. " +
 
160
                                "Please restart GNOME Do.");
 
161
                        } catch (Exception e) {
 
162
                                Log.Error ("{0}: {1}", e.GetType (), e.Message);
 
163
                                //I removed these due to a bug in Mono.Addins, once that bug
 
164
                                //is fixed i will reinstate user error reporting.
 
165
                                //Log.Debug (e.StackTrace);
 
166
                                //SendNotification ("Plugin update failed.");
 
167
                        }
 
168
                        if (!Do.Preferences.StatusIconVisible)
 
169
                                Hide ();
 
170
                }
 
171
                                
 
172
                protected void OnTrayIconPopup (object o, EventArgs args) 
 
173
                {
 
174
                        int x, y;
 
175
                        bool push_in;
 
176
                        StatusIcon.PositionMenu (MainMenu.Instance, out x, out y, out push_in, Handle);
 
177
                        MainMenu.Instance.PopupAtPosition (x, y);
 
178
                }
 
179
                
 
180
                private static void OnNotificationClosed (object sender, EventArgs args)
 
181
                {
 
182
                        if (!Do.Preferences.StatusIconVisible)
 
183
                                Do.NotificationIcon.Hide ();
 
184
                }
 
185
        }
 
186
}