2
// Copyright (C) 2010 Claudio Melis, Rico Tzschichholz
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.
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.
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/>.
19
using System.Collections.Generic;
20
using System.Diagnostics;
23
using org.freedesktop.DBus;
34
namespace SessionManager
36
class SessionManagerEntry
38
public string icon, hover_text;
41
public SessionManagerEntry (string icon, string hover_text, Action action)
44
this.hover_text = hover_text;
49
public class SessionManagerItem : IconDockItem
51
int current_index = 0;
53
SystemManager system_manager = SystemManager.GetInstance ();
55
List<SessionManagerEntry> SessionMenuItems;
56
List<SessionManagerEntry> SessionDockItems;
58
SessionManagerEntry lockscreen;
59
SessionManagerEntry logout;
60
SessionManagerEntry suspend;
61
SessionManagerEntry hibernate;
62
SessionManagerEntry restart;
63
SessionManagerEntry shutdown;
65
public SessionManagerItem ()
68
GenerateSessionItems ();
70
system_manager.CapabilitiesChanged += HandlePowermanagerCapabilitiesChanged;
71
system_manager.RebootRequired += HandleRebootRequired;
73
HoverText = SessionDockItems[current_index].hover_text;
74
Icon = SessionDockItems[current_index].icon;
77
void HandleRebootRequired (object sender, EventArgs e)
79
if (system_manager.CanRestart ()) {
80
HoverText = restart.hover_text;
83
State &= ~ItemState.Urgent;
84
State |= ItemState.Urgent;
88
void HandlePowermanagerCapabilitiesChanged (object sender, EventArgs e)
90
GenerateSessionItems ();
92
HoverText = SessionDockItems[current_index].hover_text;
93
Icon = SessionDockItems[current_index].icon;
98
void GenerateSessionItems ()
100
SessionMenuItems = new List<SessionManagerEntry> ();
101
SessionDockItems = new List<SessionManagerEntry> ();
105
SessionMenuItems.Add (lockscreen);
106
SessionDockItems.Add (lockscreen);
108
SessionMenuItems.Add (logout);
109
SessionDockItems.Add (logout);
111
if (system_manager.CanSuspend ()) {
112
SessionMenuItems.Add (suspend);
115
if (system_manager.CanHibernate ()) {
116
SessionMenuItems.Add (hibernate);
119
if (system_manager.CanRestart ()) {
120
SessionMenuItems.Add (restart);
121
SessionDockItems.Add (restart);
124
if (system_manager.CanStop ()) {
125
SessionMenuItems.Add (shutdown);
126
SessionDockItems.Add (shutdown);
131
void BuildMenuEntries ()
134
lockscreen = new SessionManagerEntry ("system-lock-screen", Catalog.GetString ("Lock Screen"), () => {
136
Console.WriteLine ("Lock Screen now...");
137
DockServices.System.Execute ("gnome-screensaver-command --lock");
141
logout = new SessionManagerEntry ("system-log-out", Catalog.GetString ("Log Out..."), () => {
143
ShowConfirmationDialog (Catalog.GetString ("Log Out"),
144
Catalog.GetString ("Are you sure you want to close all programs and log out of the computer?"),
146
() => DockServices.System.Execute ("gnome-session-save --logout"));
150
suspend = new SessionManagerEntry ("system-suspend", Catalog.GetString ("Suspend"), () => {
152
Console.WriteLine ("Suspend now...");
153
DockServices.System.Execute ("gnome-screensaver-command --lock");
154
system_manager.Suspend ();
158
hibernate = new SessionManagerEntry ("system-hibernate", Catalog.GetString ("Hibernate"), () => {
160
Console.WriteLine ("Hibernate now...");
161
DockServices.System.Execute ("gnome-screensaver-command --lock");
162
system_manager.Hibernate ();
167
restart = new SessionManagerEntry ("system-restart", Catalog.GetString ("Restart..."), () => {
169
ShowConfirmationDialog (Catalog.GetString ("Restart"),
170
Catalog.GetString ("Are you sure you want to close all programs and restart the computer?"),
172
() => system_manager.Restart ());
176
shutdown = new SessionManagerEntry ("system-shutdown", Catalog.GetString ("Shut Down..."), () => {
178
ShowConfirmationDialog (Catalog.GetString ("Shut Down"),
179
Catalog.GetString ("Are you sure you want to close all programs and shut down the computer?"),
181
() => system_manager.Stop ());
187
void ShowConfirmationDialog (string title, string text, string icon_name, Action action)
189
Console.WriteLine ("Calling {0}", title);
191
Gtk.MessageDialog md = new Gtk.MessageDialog (null, 0, Gtk.MessageType.Question, Gtk.ButtonsType.None, text);
194
md.Image = Gtk.Image.NewFromIconName (icon_name, Gtk.IconSize.Dialog);
195
md.Image.Visible = true;
198
md.AddButton (Gtk.Stock.Cancel, Gtk.ResponseType.Cancel);
199
md.AddButton (title, Gtk.ResponseType.Ok);
201
md.Response += (o, args) => {
203
if (args.ResponseId == Gtk.ResponseType.Ok) {
204
Console.WriteLine ("{0} now...", title);
213
public override string UniqueID ()
215
return "SessionManager";
218
protected override ClickAnimation OnClicked (uint button, Gdk.ModifierType mod, double xPercent, double yPercent)
221
SessionDockItems[current_index].action.Invoke ();
222
return ClickAnimation.Bounce;
225
return ClickAnimation.None;
228
protected override MenuList OnGetMenuItems ()
230
MenuList list = new MenuList ();
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 ()));
240
protected override void OnScrolled (Gdk.ScrollDirection direction, Gdk.ModifierType mod)
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;
251
HoverText = SessionDockItems[current_index].hover_text;
252
Icon = SessionDockItems[current_index].icon;
257
#region IDisposable implementation
258
public override void Dispose ()
260
system_manager.CapabilitiesChanged -= HandlePowermanagerCapabilitiesChanged;
261
system_manager.RebootRequired -= HandleRebootRequired;