~psybers/docky/bug518828

« back to all changes in this revision

Viewing changes to StandardPlugins/SessionManager/src/SessionManagerItem.cs

  • Committer: Robert Dyer
  • Date: 2010-03-28 19:25:35 UTC
  • mfrom: (1197.1.19 docky)
  • Revision ID: psybers@gmail.com-20100328192535-fdba9khlnrkr3g9p
merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  
 
2
//  Copyright (C) 2010 Claudio Melis, Rico Tzschichholz
 
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
 
 
18
using System;
 
19
using System.Collections.Generic;
 
20
using System.Diagnostics;
 
21
 
 
22
using NDesk.DBus;
 
23
using org.freedesktop.DBus;
 
24
 
 
25
using Gdk;
 
26
using GLib;
 
27
 
 
28
using Mono.Unix;
 
29
 
 
30
using Docky.Items;
 
31
using Docky.Menus;
 
32
using Docky.Services;
 
33
 
 
34
namespace SessionManager
 
35
{
 
36
        class SessionManagerEntry
 
37
        {
 
38
                public string icon, hover_text;
 
39
                public Action action;
 
40
 
 
41
                public SessionManagerEntry (string icon, string hover_text, Action action)
 
42
                {
 
43
                        this.icon = icon;
 
44
                        this.hover_text = hover_text;
 
45
                        this.action = action;
 
46
                }
 
47
        }
 
48
 
 
49
        public class SessionManagerItem : IconDockItem
 
50
        {
 
51
                int current_index = 0;
 
52
 
 
53
                SystemManager system_manager = SystemManager.GetInstance ();
 
54
 
 
55
                List<SessionManagerEntry> SessionMenuItems;
 
56
                List<SessionManagerEntry> SessionDockItems;
 
57
                
 
58
                SessionManagerEntry lockscreen;
 
59
                SessionManagerEntry logout;
 
60
                SessionManagerEntry suspend;
 
61
                SessionManagerEntry hibernate;
 
62
                SessionManagerEntry restart;
 
63
                SessionManagerEntry shutdown;
 
64
 
 
65
                public SessionManagerItem ()
 
66
                {
 
67
                        BuildMenuEntries ();
 
68
                        GenerateSessionItems ();
 
69
                        
 
70
                        system_manager.CapabilitiesChanged += HandlePowermanagerCapabilitiesChanged;
 
71
                        system_manager.RebootRequired += HandleRebootRequired;
 
72
                        
 
73
                        HoverText = SessionDockItems[current_index].hover_text;
 
74
                        Icon = SessionDockItems[current_index].icon;
 
75
                }
 
76
 
 
77
                void HandleRebootRequired (object sender, EventArgs e)
 
78
                {
 
79
                        if (system_manager.CanRestart ()) {
 
80
                                HoverText = restart.hover_text;
 
81
                                Icon = restart.icon;
 
82
                                QueueRedraw ();
 
83
                                State &= ~ItemState.Urgent;
 
84
                                State |= ItemState.Urgent;
 
85
                        }
 
86
                }
 
87
                
 
88
                void HandlePowermanagerCapabilitiesChanged (object sender, EventArgs e)
 
89
                {
 
90
                        GenerateSessionItems ();
 
91
                        
 
92
                        HoverText = SessionDockItems[current_index].hover_text;
 
93
                        Icon = SessionDockItems[current_index].icon;
 
94
 
 
95
                        QueueRedraw ();
 
96
                }
 
97
 
 
98
                void GenerateSessionItems () 
 
99
                {
 
100
                        SessionMenuItems = new List<SessionManagerEntry> ();
 
101
                        SessionDockItems = new List<SessionManagerEntry> ();
 
102
                        
 
103
                        current_index = 0;
 
104
                        
 
105
                        SessionMenuItems.Add (lockscreen);
 
106
                        SessionDockItems.Add (lockscreen);
 
107
 
 
108
                        SessionMenuItems.Add (logout);
 
109
                        SessionDockItems.Add (logout);
 
110
                        
 
111
                        if (system_manager.CanSuspend ()) {
 
112
                                SessionMenuItems.Add (suspend);
 
113
                        }
 
114
                        
 
115
                        if (system_manager.CanHibernate ()) {
 
116
                                SessionMenuItems.Add (hibernate);
 
117
                        }
 
118
                        
 
119
                        if (system_manager.CanRestart ()) {
 
120
                                SessionMenuItems.Add (restart);
 
121
                                SessionDockItems.Add (restart);
 
122
                        }
 
123
                        
 
124
                        if (system_manager.CanStop ()) {
 
125
                                SessionMenuItems.Add (shutdown);
 
126
                                SessionDockItems.Add (shutdown);
 
127
                        }
 
128
                        
 
129
                }
 
130
                
 
131
                void BuildMenuEntries () 
 
132
                {
 
133
                        
 
134
                        lockscreen = new SessionManagerEntry ("system-lock-screen", Catalog.GetString ("Lock Screen"), () => { 
 
135
                                
 
136
                                Console.WriteLine ("Lock Screen now...");
 
137
                                DockServices.System.Execute ("gnome-screensaver-command --lock");
 
138
                                
 
139
                        });
 
140
                        
 
141
                        logout = new SessionManagerEntry ("system-log-out", Catalog.GetString ("Log Out..."), () => { 
 
142
                                
 
143
                                ShowConfirmationDialog (Catalog.GetString ("Log Out"), 
 
144
                                                        Catalog.GetString ("Are you sure you want to close all programs and log out of the computer?"), 
 
145
                                                        "system-log-out", 
 
146
                                                        () => DockServices.System.Execute ("gnome-session-save --logout"));
 
147
                                
 
148
                        });
 
149
 
 
150
                        suspend = new SessionManagerEntry ("system-suspend", Catalog.GetString ("Suspend"), () => { 
 
151
                                
 
152
                                Console.WriteLine ("Suspend now...");
 
153
                                DockServices.System.Execute ("gnome-screensaver-command --lock");
 
154
                                system_manager.Suspend (); 
 
155
                                
 
156
                        });
 
157
                        
 
158
                        hibernate = new SessionManagerEntry ("system-hibernate", Catalog.GetString ("Hibernate"), () => { 
 
159
                                
 
160
                                Console.WriteLine ("Hibernate now...");
 
161
                                DockServices.System.Execute ("gnome-screensaver-command --lock");
 
162
                                system_manager.Hibernate (); 
 
163
                                
 
164
                        });
 
165
                        
 
166
 
 
167
                        restart = new SessionManagerEntry ("system-restart", Catalog.GetString ("Restart..."), () => { 
 
168
                
 
169
                                ShowConfirmationDialog (Catalog.GetString ("Restart"), 
 
170
                                                        Catalog.GetString ("Are you sure you want to close all programs and restart the computer?"), 
 
171
                                                        "system-restart", 
 
172
                                                        () => system_manager.Restart ());
 
173
                
 
174
                        });
 
175
                        
 
176
                        shutdown = new SessionManagerEntry ("system-shutdown", Catalog.GetString ("Shut Down..."), () => { 
 
177
                
 
178
                                ShowConfirmationDialog (Catalog.GetString ("Shut Down"), 
 
179
                                                        Catalog.GetString ("Are you sure you want to close all programs and shut down the computer?"), 
 
180
                                                        "system-shutdown", 
 
181
                                                        () => system_manager.Stop ());
 
182
                
 
183
                        });
 
184
                        
 
185
                }
 
186
 
 
187
                void ShowConfirmationDialog (string title, string text, string icon_name, Action action)
 
188
                {
 
189
                                Console.WriteLine ("Calling {0}", title);
 
190
                                
 
191
                                Gtk.MessageDialog md = new Gtk.MessageDialog (null, 0, Gtk.MessageType.Question, Gtk.ButtonsType.None, text);
 
192
                                
 
193
                                md.Title = title;
 
194
                                md.Image = Gtk.Image.NewFromIconName (icon_name, Gtk.IconSize.Dialog);
 
195
                                md.Image.Visible = true;
 
196
                                md.Image.Show ();
 
197
                                
 
198
                                md.AddButton (Gtk.Stock.Cancel, Gtk.ResponseType.Cancel);
 
199
                                md.AddButton (title, Gtk.ResponseType.Ok);
 
200
                                
 
201
                                md.Response += (o, args) => { 
 
202
                                        
 
203
                                        if (args.ResponseId == Gtk.ResponseType.Ok) {
 
204
                                                Console.WriteLine ("{0} now...", title);
 
205
                                                action.Invoke ();
 
206
                                        }
 
207
                                        md.Destroy ();
 
208
                                };
 
209
                                
 
210
                                md.Show ();
 
211
                }
 
212
                
 
213
                public override string UniqueID ()
 
214
                {
 
215
                        return "SessionManager";
 
216
                }
 
217
 
 
218
                protected override ClickAnimation OnClicked (uint button, Gdk.ModifierType mod, double xPercent, double yPercent)
 
219
                {
 
220
                        if (button == 1) {
 
221
                                SessionDockItems[current_index].action.Invoke ();
 
222
                                return ClickAnimation.Bounce;
 
223
                        }
 
224
                        
 
225
                        return ClickAnimation.None;
 
226
                }
 
227
 
 
228
                protected override MenuList OnGetMenuItems ()
 
229
                {
 
230
                        MenuList list = new MenuList ();
 
231
                        
 
232
                        foreach (SessionManagerEntry item in SessionMenuItems) {
 
233
                                SessionManagerEntry entry = item;
 
234
                                list[MenuListContainer.Actions].Add (new MenuItem (entry.hover_text, entry.icon, (o, a) => entry.action ()));
 
235
                        }
 
236
                        
 
237
                        return list;
 
238
                }
 
239
 
 
240
                protected override void OnScrolled (Gdk.ScrollDirection direction, Gdk.ModifierType mod)
 
241
                {
 
242
                        if (direction == Gdk.ScrollDirection.Up || direction == Gdk.ScrollDirection.Left) {
 
243
                                if (current_index == 0)
 
244
                                        current_index = SessionDockItems.Count;
 
245
                                current_index = (current_index - 1) % SessionDockItems.Count;
 
246
                        } else if (direction == Gdk.ScrollDirection.Down || direction == Gdk.ScrollDirection.Right) {
 
247
                                current_index = (current_index + 1) % SessionDockItems.Count;
 
248
                        } else
 
249
                                return;
 
250
                        
 
251
                        HoverText = SessionDockItems[current_index].hover_text;
 
252
                        Icon = SessionDockItems[current_index].icon;
 
253
                        
 
254
                        QueueRedraw ();
 
255
                }
 
256
 
 
257
                #region IDisposable implementation
 
258
                public override void Dispose ()
 
259
                {
 
260
                        system_manager.CapabilitiesChanged -= HandlePowermanagerCapabilitiesChanged;
 
261
                        system_manager.RebootRequired -= HandleRebootRequired;
 
262
                        
 
263
                        base.Dispose ();
 
264
                }
 
265
                
 
266
                #endregion
 
267
        }
 
268
}