1
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
3
const GLib = imports.gi.GLib;
4
const Gio = imports.gi.Gio;
5
const Lang = imports.lang;
6
const Shell = imports.gi.Shell;
8
const SystemdLoginManagerIface = <interface name='org.freedesktop.login1.Manager'>
9
<method name='PowerOff'>
10
<arg type='b' direction='in'/>
12
<method name='Reboot'>
13
<arg type='b' direction='in'/>
15
<method name='CanPowerOff'>
16
<arg type='s' direction='out'/>
18
<method name='CanReboot'>
19
<arg type='s' direction='out'/>
23
const SystemdLoginSessionIface = <interface name='org.freedesktop.login1.Session'>
24
<signal name='Lock' />
25
<signal name='Unlock' />
28
const SystemdLoginManager = Gio.DBusProxy.makeProxyWrapper(SystemdLoginManagerIface);
29
const SystemdLoginSession = Gio.DBusProxy.makeProxyWrapper(SystemdLoginSessionIface);
31
const ConsoleKitManagerIface = <interface name='org.freedesktop.ConsoleKit.Manager'>
32
<method name='CanRestart'>
33
<arg type='b' direction='out'/>
35
<method name='CanStop'>
36
<arg type='b' direction='out'/>
38
<method name='Restart' />
39
<method name='Stop' />
40
<method name='GetCurrentSession'>
41
<arg type='o' direction='out' />
45
const ConsoleKitSessionIface = <interface name='org.freedesktop.ConsoleKit.Session'>
46
<method name='IsActive'>
47
<arg type='b' direction='out' />
49
<signal name='ActiveChanged'>
50
<arg type='b' direction='out' />
52
<signal name='Lock' />
53
<signal name='Unlock' />
56
const ConsoleKitSession = Gio.DBusProxy.makeProxyWrapper(ConsoleKitSessionIface);
57
const ConsoleKitManager = Gio.DBusProxy.makeProxyWrapper(ConsoleKitManagerIface);
59
function haveSystemd() {
60
return GLib.access("/sys/fs/cgroup/systemd", 0) >= 0;
63
let _loginManager = null;
67
* An abstraction over systemd/logind and ConsoleKit.
70
function getLoginManager() {
71
if (_loginManager == null) {
73
_loginManager = new LoginManagerSystemd();
75
_loginManager = new LoginManagerConsoleKit();
81
const LoginManagerSystemd = new Lang.Class({
82
Name: 'LoginManagerSystemd',
85
this._proxy = new SystemdLoginManager(Gio.DBus.system,
86
'org.freedesktop.login1',
87
'/org/freedesktop/login1');
90
// Having this function is a bit of a hack since the Systemd and ConsoleKit
91
// session objects have different interfaces - but in both cases there are
92
// Lock/Unlock signals, and that's all we count upon at the moment.
93
getCurrentSessionProxy: function() {
94
if (!this._currentSession) {
95
this._currentSession = new SystemdLoginSession(Gio.DBus.system,
96
'org.freedesktop.login1',
97
'/org/freedesktop/login1/session/' +
98
GLib.getenv('XDG_SESSION_ID'));
101
return this._currentSession;
104
get sessionActive() {
105
return Shell.session_is_active_for_systemd();
108
canPowerOff: function(asyncCallback) {
109
this._proxy.CanPowerOffRemote(function(result, error) {
111
asyncCallback(false);
113
asyncCallback(result[0] != 'no');
117
canReboot: function(asyncCallback) {
118
this._proxy.CanRebootRemote(function(result, error) {
120
asyncCallback(false);
122
asyncCallback(result[0] != 'no');
126
powerOff: function() {
127
this._proxy.PowerOffRemote(true);
131
this._proxy.RebootRemote(true);
135
const LoginManagerConsoleKit = new Lang.Class({
136
Name: 'LoginManagerConsoleKit',
139
this._proxy = new ConsoleKitManager(Gio.DBus.system,
140
'org.freedesktop.ConsoleKit',
141
'/org/freedesktop/ConsoleKit/Manager');
144
// Having this function is a bit of a hack since the Systemd and ConsoleKit
145
// session objects have different interfaces - but in both cases there are
146
// Lock/Unlock signals, and that's all we count upon at the moment.
147
getCurrentSessionProxy: function() {
148
if (!this._currentSession) {
149
let [currentSessionId] = this._proxy.GetCurrentSessionSync();
150
this._currentSession = new ConsoleKitSession(Gio.DBus.system,
151
'org.freedesktop.ConsoleKit',
155
return this._currentSession;
158
get sessionActive() {
159
if (this._sessionActive !== undefined)
160
return this._sessionActive;
162
let session = this.getCurrentSessionProxy();
163
session.connectSignal('ActiveChanged', Lang.bind(this, function(object, senderName, [isActive]) {
164
this._sessionActive = isActive;
166
[this._sessionActive] = session.IsActiveSync();
168
return this._sessionActive;
171
canPowerOff: function(asyncCallback) {
172
this._proxy.CanStopRemote(function(result, error) {
174
asyncCallback(false);
176
asyncCallback(result[0]);
180
canReboot: function(asyncCallback) {
181
this._proxy.CanRestartRemote(function(result, error) {
183
asyncCallback(false);
185
asyncCallback(result[0]);
189
powerOff: function() {
190
this._proxy.StopRemote();
194
this._proxy.RestartRemote();