systemActions: Store properties in a map

We want to be able to search for available system actions, so
rather than tracking each action in a separate property, store
them in a single map that can be searched in a generic and clean
way.

https://bugzilla.gnome.org/show_bug.cgi?id=691900
This commit is contained in:
Rares Visalom 2017-08-03 01:32:02 +03:00 committed by Florian Müllner
parent 9c3b3320f8
commit b4b1caf5a3

View File

@ -28,6 +28,13 @@ const SensorProxyInterface = '<node> \
</interface> \ </interface> \
</node>'; </node>';
const POWER_OFF_ACTION_ID = 'power-off';
const LOCK_SCREEN_ACTION_ID = 'lock-screen';
const LOGOUT_ACTION_ID = 'logout';
const SUSPEND_ACTION_ID = 'suspend';
const SWITCH_USER_ACTION_ID = 'switch-user';
const LOCK_ORIENTATION_ACTION_ID = 'lock-orientation';
const SensorProxy = Gio.DBusProxy.makeProxyWrapper(SensorProxyInterface); const SensorProxy = Gio.DBusProxy.makeProxyWrapper(SensorProxyInterface);
let _singleton = null; let _singleton = null;
@ -83,15 +90,23 @@ const SystemActions = new Lang.Class({
_init: function() { _init: function() {
this.parent(); this.parent();
this._canPowerOff = false;
this._canHavePowerOff = true; this._canHavePowerOff = true;
this._canSuspend = false;
this._canHaveSuspend = true; this._canHaveSuspend = true;
this._canLockScreen = false;
this._canSwitchUser = false; this._actions = new Map();
this._canLogout = false; this._actions.set(POWER_OFF_ACTION_ID,
this._canLockOrientation = false; { available: false });
this._orientationLockIcon = null; this._actions.set(LOCK_SCREEN_ACTION_ID,
{ available: false });
this._actions.set(LOGOUT_ACTION_ID,
{ available: false });
this._actions.set(SUSPEND_ACTION_ID,
{ available: false });
this._actions.set(SWITCH_USER_ACTION_ID,
{ available: false });
this._actions.set(LOCK_ORIENTATION_ACTION_ID,
{ iconName: '',
available: false });
this._loginScreenSettings = new Gio.Settings({ schema_id: LOGIN_SCREEN_SCHEMA }); this._loginScreenSettings = new Gio.Settings({ schema_id: LOGIN_SCREEN_SCHEMA });
this._lockdownSettings = new Gio.Settings({ schema_id: LOCKDOWN_SCHEMA }); this._lockdownSettings = new Gio.Settings({ schema_id: LOCKDOWN_SCHEMA });
@ -147,31 +162,31 @@ const SystemActions = new Lang.Class({
}, },
get can_power_off() { get can_power_off() {
return this._canPowerOff; return this._actions.get(POWER_OFF_ACTION_ID).available;
}, },
get can_suspend() { get can_suspend() {
return this._canSuspend; return this._actions.get(SUSPEND_ACTION_ID).available;
}, },
get can_lock_screen() { get can_lock_screen() {
return this._canLockScreen; return this._actions.get(LOCK_SCREEN_ACTION_ID).available;
}, },
get can_switch_user() { get can_switch_user() {
return this._canSwitchUser; return this._actions.get(SWITCH_USER_ACTION_ID).available;
}, },
get can_logout() { get can_logout() {
return this._canLogout; return this._actions.get(LOGOUT_ACTION_ID).available;
}, },
get can_lock_orientation() { get can_lock_orientation() {
return this._canLockOrientation; return this._actions.get(LOCK_ORIENTATION_ACTION_ID).available;
}, },
get orientation_lock_icon() { get orientation_lock_icon() {
return this._orientationLockIcon; return this._actions.get(LOCK_ORIENTATION_ACTION_ID).iconName;
}, },
_sensorProxyAppeared: function() { _sensorProxyAppeared: function() {
@ -188,11 +203,12 @@ const SystemActions = new Lang.Class({
}, },
_updateOrientationLock: function() { _updateOrientationLock: function() {
let available = false;
if (this._sensorProxy) if (this._sensorProxy)
this._canLockOrientation = this._sensorProxy.HasAccelerometer && available = this._sensorProxy.HasAccelerometer &&
this._monitorManager.get_is_builtin_display_on(); this._monitorManager.get_is_builtin_display_on();
else
this._canLockOrientation = false; this._actions.get(LOCK_ORIENTATION_ACTION_ID).available = available;
this.notify('can-lock-orientation'); this.notify('can-lock-orientation');
}, },
@ -201,7 +217,7 @@ const SystemActions = new Lang.Class({
let locked = this._orientationSettings.get_boolean('orientation-lock'); let locked = this._orientationSettings.get_boolean('orientation-lock');
let iconName = locked ? 'rotation-locked-symbolic' let iconName = locked ? 'rotation-locked-symbolic'
: 'rotation-allowed-symbolic'; : 'rotation-allowed-symbolic';
this._orientationLockIcon = iconName; this._actions.get(LOCK_ORIENTATION_ACTION_ID).iconName = iconName;
this.notify('orientation-lock-icon'); this.notify('orientation-lock-icon');
}, },
@ -224,7 +240,7 @@ const SystemActions = new Lang.Class({
_updateLockScreen() { _updateLockScreen() {
let showLock = !Main.sessionMode.isLocked && !Main.sessionMode.isGreeter; let showLock = !Main.sessionMode.isLocked && !Main.sessionMode.isGreeter;
let allowLockScreen = !this._lockdownSettings.get_boolean(DISABLE_LOCK_SCREEN_KEY); let allowLockScreen = !this._lockdownSettings.get_boolean(DISABLE_LOCK_SCREEN_KEY);
this._canLockScreen = showLock && allowLockScreen && LoginManager.canLock(); this._actions.get(LOCK_SCREEN_ACTION_ID).available = showLock && allowLockScreen && LoginManager.canLock();
this.notify('can-lock-screen'); this.notify('can-lock-screen');
}, },
@ -242,7 +258,7 @@ const SystemActions = new Lang.Class({
let disabled = Main.sessionMode.isLocked || let disabled = Main.sessionMode.isLocked ||
(Main.sessionMode.isGreeter && (Main.sessionMode.isGreeter &&
this._loginScreenSettings.get_boolean(DISABLE_RESTART_KEY)); this._loginScreenSettings.get_boolean(DISABLE_RESTART_KEY));
this._canPowerOff = this._canHavePowerOff && !disabled; this._actions.get(POWER_OFF_ACTION_ID).available = this._canHavePowerOff && !disabled;
this.notify('can-power-off'); this.notify('can-power-off');
}, },
@ -260,7 +276,7 @@ const SystemActions = new Lang.Class({
this._suspendNeedsAuth) || this._suspendNeedsAuth) ||
(Main.sessionMode.isGreeter && (Main.sessionMode.isGreeter &&
this._loginScreenSettings.get_boolean(DISABLE_RESTART_KEY)); this._loginScreenSettings.get_boolean(DISABLE_RESTART_KEY));
this._canSuspend = this._canHaveSuspend && !disabled; this._actions.get(SUSPEND_ACTION_ID).available = this._canHaveSuspend && !disabled;
this.notify('can-suspend'); this.notify('can-suspend');
}, },
@ -275,7 +291,7 @@ const SystemActions = new Lang.Class({
let shouldShowInMode = !Main.sessionMode.isLocked && !Main.sessionMode.isGreeter; let shouldShowInMode = !Main.sessionMode.isLocked && !Main.sessionMode.isGreeter;
let visible = allowSwitch && multiUser && shouldShowInMode; let visible = allowSwitch && multiUser && shouldShowInMode;
this._canSwitchUser = visible; this._actions.get(SWITCH_USER_ACTION_ID).available = visible;
this.notify('can-switch-user'); this.notify('can-switch-user');
return visible; return visible;
@ -293,14 +309,14 @@ const SystemActions = new Lang.Class({
let shouldShowInMode = !Main.sessionMode.isLocked && !Main.sessionMode.isGreeter; let shouldShowInMode = !Main.sessionMode.isLocked && !Main.sessionMode.isGreeter;
let visible = allowLogout && (alwaysShow || multiUser || multiSession || systemAccount || !localAccount) && shouldShowInMode; let visible = allowLogout && (alwaysShow || multiUser || multiSession || systemAccount || !localAccount) && shouldShowInMode;
this._canLogout = visible; this._actions.get(LOGOUT_ACTION_ID).available = visible;
this.notify('can-logout'); this.notify('can-logout');
return visible; return visible;
}, },
activateLockOrientation: function() { activateLockOrientation: function() {
if (!this._canLockOrientation) if (!this._actions.get(LOCK_ORIENTATION_ACTION_ID).available)
throw new Error('The lock-orientation action is not available!'); throw new Error('The lock-orientation action is not available!');
let locked = this._orientationSettings.get_boolean('orientation-lock'); let locked = this._orientationSettings.get_boolean('orientation-lock');
@ -308,14 +324,14 @@ const SystemActions = new Lang.Class({
}, },
activateLockScreen: function() { activateLockScreen: function() {
if (!this._canLockScreen) if (!this._actions.get(LOCK_SCREEN_ACTION_ID).available)
throw new Error('The lock-screen action is not available!'); throw new Error('The lock-screen action is not available!');
Main.screenShield.lock(true); Main.screenShield.lock(true);
}, },
activateSwitchUser: function() { activateSwitchUser: function() {
if (!this._canSwitchUser) if (!this._actions.get(SWITCH_USER_ACTION_ID).available)
throw new Error('The switch-user action is not available!'); throw new Error('The switch-user action is not available!');
if (Main.screenShield) if (Main.screenShield)
@ -328,7 +344,7 @@ const SystemActions = new Lang.Class({
}, },
activateLogout: function() { activateLogout: function() {
if (!this._canLogout) if (!this._actions.get(LOGOUT_ACTION_ID).available)
throw new Error('The logout action is not available!'); throw new Error('The logout action is not available!');
Main.overview.hide(); Main.overview.hide();
@ -336,14 +352,14 @@ const SystemActions = new Lang.Class({
}, },
activatePowerOff: function() { activatePowerOff: function() {
if (!this._canPowerOff) if (!this._actions.get(POWER_OFF_ACTION_ID).available)
throw new Error('The power-off action is not available!'); throw new Error('The power-off action is not available!');
this._session.ShutdownRemote(0); this._session.ShutdownRemote(0);
}, },
activateSuspend: function() { activateSuspend: function() {
if (!this._canSuspend) if (!this._actions.get(SUSPEND_ACTION_ID).available)
throw new Error('The suspend action is not available!'); throw new Error('The suspend action is not available!');
this._loginManager.suspend(); this._loginManager.suspend();