network: Rewrite VPN section to be independent of NMConnectionBased
https://bugzilla.gnome.org/show_bug.cgi?id=701954
This commit is contained in:
parent
f4051e810e
commit
7ae7f046c2
@ -9,6 +9,7 @@ const NMGtk = imports.gi.NMGtk;
|
|||||||
const Signals = imports.signals;
|
const Signals = imports.signals;
|
||||||
const St = imports.gi.St;
|
const St = imports.gi.St;
|
||||||
|
|
||||||
|
const Hash = imports.misc.hash;
|
||||||
const Main = imports.ui.main;
|
const Main = imports.ui.main;
|
||||||
const PanelMenu = imports.ui.panelMenu;
|
const PanelMenu = imports.ui.panelMenu;
|
||||||
const PopupMenu = imports.ui.popupMenu;
|
const PopupMenu = imports.ui.popupMenu;
|
||||||
@ -1319,26 +1320,38 @@ const NMDeviceVirtual = new Lang.Class({
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const NMVPNSection = new Lang.Class({
|
const NMVPNConnectionItem = new Lang.Class({
|
||||||
Name: 'NMVPNSection',
|
Name: 'NMVPNConnectionItem',
|
||||||
Extends: NMConnectionBased,
|
|
||||||
category: NMConnectionCategory.VPN,
|
|
||||||
|
|
||||||
_init: function(client) {
|
_init: function(client, connection) {
|
||||||
this.parent([]);
|
|
||||||
this._client = client;
|
this._client = client;
|
||||||
|
this.connection = connection;
|
||||||
|
this._activeConnection = null;
|
||||||
|
this._activeConnectionChangedId = 0;
|
||||||
|
|
||||||
this.section = new PopupMenu.PopupMenuSection();
|
this.menuItem = new PopupMenu.PopupSwitchMenuItem(connection.get_id(), false,
|
||||||
this._deferredWorkId = Main.initializeDeferredWork(this.section.actor, Lang.bind(this, this._createSection));
|
{ style_class: 'popup-subtitle-menu-item' });
|
||||||
|
this.menuItem.connect('toggled', Lang.bind(this, this._toggle));
|
||||||
|
|
||||||
|
this._sync();
|
||||||
},
|
},
|
||||||
|
|
||||||
connectionValid: function(connection) {
|
destroy: function() {
|
||||||
// filtering is done by NMApplet code
|
this.menuItem.destroy();
|
||||||
return true;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
getStatusLabel: function(activeConnection) {
|
isActive: function() {
|
||||||
switch(activeConnection.vpn_state) {
|
if (this._activeConnection == null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return this._activeConnection.vpn_state == NetworkManager.VPNConnectionState.ACTIVATED;
|
||||||
|
},
|
||||||
|
|
||||||
|
_getStatus: function() {
|
||||||
|
if (this._activeConnection == null)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
switch(this._activeConnection.vpn_state) {
|
||||||
case NetworkManager.VPNConnectionState.DISCONNECTED:
|
case NetworkManager.VPNConnectionState.DISCONNECTED:
|
||||||
case NetworkManager.VPNConnectionState.ACTIVATED:
|
case NetworkManager.VPNConnectionState.ACTIVATED:
|
||||||
return null;
|
return null;
|
||||||
@ -1352,85 +1365,26 @@ const NMVPNSection = new Lang.Class({
|
|||||||
case NetworkManager.VPNConnectionState.FAILED:
|
case NetworkManager.VPNConnectionState.FAILED:
|
||||||
return _("connection failed");
|
return _("connection failed");
|
||||||
default:
|
default:
|
||||||
log('VPN connection state invalid, is %d'.format(this.device.state));
|
|
||||||
return 'invalid';
|
return 'invalid';
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
removeActiveConnection: function(activeConnection) {
|
_toggle: function() {
|
||||||
let pos = this._findConnection(activeConnection.uuid);
|
if (this._activeConnection == null)
|
||||||
if (pos < 0)
|
this._client.activate_connection(this.connection, null, null, null);
|
||||||
return;
|
else
|
||||||
|
this._client.deactivate_connection(this._activeConnection);
|
||||||
|
|
||||||
let obj = this._connections[pos];
|
this._sync();
|
||||||
obj.active.disconnect(obj.stateChangedId);
|
|
||||||
obj.active = null;
|
|
||||||
|
|
||||||
if (obj.item) {
|
|
||||||
obj.item.setToggleState(false);
|
|
||||||
obj.item.setStatus(null);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
addActiveConnection: function(activeConnection) {
|
_sync: function() {
|
||||||
let pos = this._findConnection(activeConnection.uuid);
|
this.menuItem.setToggleState(this.isActive());
|
||||||
if (pos < 0)
|
this.menuItem.setStatus(this._getStatus());
|
||||||
return;
|
this.emit('icon-changed');
|
||||||
|
|
||||||
let obj = this._connections[pos];
|
|
||||||
obj.active = activeConnection;
|
|
||||||
obj.stateChangedId = obj.active.connect('vpn-state-changed',
|
|
||||||
Lang.bind(this, this._connectionStateChanged));
|
|
||||||
|
|
||||||
if (obj.item) {
|
|
||||||
obj.item.setToggleState(obj.active.vpn_state ==
|
|
||||||
NetworkManager.VPNConnectionState.ACTIVATED);
|
|
||||||
obj.item.setStatus(this.getStatusLabel(obj.active));
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
_queueCreateSection: function() {
|
_connectionStateChanged: function(ac, newstate, reason) {
|
||||||
this.section.removeAll();
|
|
||||||
Main.queueDeferredWork(this._deferredWorkId);
|
|
||||||
},
|
|
||||||
|
|
||||||
_createConnectionItem: function(obj) {
|
|
||||||
let menuItem = new PopupMenu.PopupSwitchMenuItem(obj.name, false,
|
|
||||||
{ style_class: 'popup-subtitle-menu-item' });
|
|
||||||
menuItem.connect('toggled', Lang.bind(this, function(menuItem) {
|
|
||||||
if (menuItem.state) {
|
|
||||||
this._client.activate_connection(obj.connection, null, null, null);
|
|
||||||
// Immediately go back to disconnected, until NM tells us to change
|
|
||||||
menuItem.setToggleState(false);
|
|
||||||
} else if (obj.active) {
|
|
||||||
this._client.deactivate_connection(obj.active);
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
|
|
||||||
if (obj.active) {
|
|
||||||
menuItem.setToggleState(obj.active.vpn_state ==
|
|
||||||
NetworkManager.VPNConnectionState.ACTIVATED);
|
|
||||||
menuItem.setStatus(this.getStatusLabel(obj.active));
|
|
||||||
}
|
|
||||||
|
|
||||||
return menuItem;
|
|
||||||
},
|
|
||||||
|
|
||||||
_createSection: function() {
|
|
||||||
if (this._connections.length > 0) {
|
|
||||||
this.section.actor.show();
|
|
||||||
|
|
||||||
for(let j = 0; j < this._connections.length; ++j) {
|
|
||||||
let obj = this._connections[j];
|
|
||||||
obj.item = this._createConnectionItem(obj);
|
|
||||||
this.section.addMenuItem(obj.item);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
this.section.actor.hide()
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
_connectionStateChanged: function(vpnConnection, newstate, reason) {
|
|
||||||
if (newstate == NetworkManager.VPNConnectionState.FAILED &&
|
if (newstate == NetworkManager.VPNConnectionState.FAILED &&
|
||||||
reason != NetworkManager.VPNConnectionStateReason.NO_SECRETS) {
|
reason != NetworkManager.VPNConnectionStateReason.NO_SECRETS) {
|
||||||
// FIXME: if we ever want to show something based on reason,
|
// FIXME: if we ever want to show something based on reason,
|
||||||
@ -1439,24 +1393,27 @@ const NMVPNSection = new Lang.Class({
|
|||||||
this.emit('activation-failed', reason);
|
this.emit('activation-failed', reason);
|
||||||
}
|
}
|
||||||
|
|
||||||
let pos = this._findConnection(vpnConnection.uuid);
|
this._sync();
|
||||||
if (pos >= 0) {
|
|
||||||
let obj = this._connections[pos];
|
|
||||||
if (obj.item) {
|
|
||||||
obj.item.setToggleState(vpnConnection.vpn_state ==
|
|
||||||
NetworkManager.VPNConnectionState.ACTIVATED);
|
|
||||||
obj.item.setStatus(this.getStatusLabel(vpnConnection));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
log('Could not find connection for vpn-state-changed handler');
|
|
||||||
}
|
|
||||||
|
|
||||||
this.emit('icon-changed');
|
|
||||||
},
|
},
|
||||||
|
|
||||||
_getIconForConnection: function(vpnConnection) {
|
setActiveConnection: function(activeConnection) {
|
||||||
if (vpnConnection) {
|
if (this._activeConnectionChangedId > 0) {
|
||||||
if (vpnConnection.state == NetworkManager.ActiveConnectionState.ACTIVATING)
|
this._activeConnection.disconnect(this._activeConnectionChangedId);
|
||||||
|
this._activeConnectionChangedId = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
this._activeConnection = activeConnection;
|
||||||
|
|
||||||
|
if (this._activeConnection)
|
||||||
|
this._activeConnectionChangedId = this._activeConnection.connect('vpn-state-changed',
|
||||||
|
Lang.bind(this, this._connectionStateChanged));
|
||||||
|
|
||||||
|
this._sync();
|
||||||
|
},
|
||||||
|
|
||||||
|
getIndicatorIcon: function() {
|
||||||
|
if (this._activeConnection) {
|
||||||
|
if (this._activeConnection.state == NetworkManager.ActiveConnectionState.ACTIVATING)
|
||||||
return 'network-vpn-acquiring-symbolic';
|
return 'network-vpn-acquiring-symbolic';
|
||||||
else
|
else
|
||||||
return 'network-vpn-symbolic';
|
return 'network-vpn-symbolic';
|
||||||
@ -1464,17 +1421,64 @@ const NMVPNSection = new Lang.Class({
|
|||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
});
|
||||||
|
Signals.addSignalMethods(NMVPNConnectionItem.prototype);
|
||||||
|
|
||||||
|
const NMVPNSection = new Lang.Class({
|
||||||
|
Name: 'NMVPNSection',
|
||||||
|
category: NMConnectionCategory.VPN,
|
||||||
|
|
||||||
|
_init: function(client) {
|
||||||
|
this._client = client;
|
||||||
|
this._connectionItems = new Hash.Map();
|
||||||
|
|
||||||
|
this.section = new PopupMenu.PopupMenuSection();
|
||||||
|
},
|
||||||
|
|
||||||
|
checkConnection: function(connection) {
|
||||||
|
if (this._connectionItems.has(connection.get_uuid()))
|
||||||
|
return;
|
||||||
|
|
||||||
|
let item = new NMVPNConnectionItem(this._client, connection);
|
||||||
|
|
||||||
|
item.connect('icon-changed', Lang.bind(this, function() {
|
||||||
|
this.emit('icon-changed');
|
||||||
|
}));
|
||||||
|
item.connect('activation-failed', Lang.bind(this, function(item, reason) {
|
||||||
|
this.emit('activation-failed', reason);
|
||||||
|
}));
|
||||||
|
|
||||||
|
this.section.addMenuItem(item.menuItem);
|
||||||
|
this._connectionItems.set(connection.get_uuid(), item);
|
||||||
|
},
|
||||||
|
|
||||||
|
removeConnection: function(connection) {
|
||||||
|
this._connectionItems.get(connection.get_uuid()).destroy();
|
||||||
|
this._connectionItems.delete(connection.get_uuid());
|
||||||
|
},
|
||||||
|
|
||||||
|
addActiveConnection: function(activeConnection) {
|
||||||
|
let item = this._connectionItems.get(activeConnection._connection.get_uuid());
|
||||||
|
item.setActiveConnection(activeConnection);
|
||||||
|
},
|
||||||
|
|
||||||
|
removeActiveConnection: function(activeConnection) {
|
||||||
|
let item = this._connectionItems.get(activeConnection._connection.get_uuid());
|
||||||
|
item.setActiveConnection(null);
|
||||||
|
},
|
||||||
|
|
||||||
getIndicatorIcon: function() {
|
getIndicatorIcon: function() {
|
||||||
for (let i = 0; i < this._connections.length; i++) {
|
let items = this._connectionItems.values();
|
||||||
let obj = this._connections[i];
|
for (let i = 0; i < items.length; i++) {
|
||||||
let icon = this._getIconForConnection(obj.active);
|
let item = items[i];
|
||||||
|
let icon = item.getIndicatorIcon();
|
||||||
if (icon)
|
if (icon)
|
||||||
return icon;
|
return icon;
|
||||||
}
|
}
|
||||||
return '';
|
return '';
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
Signals.addSignalMethods(NMVPNSection.prototype);
|
||||||
|
|
||||||
const NMApplet = new Lang.Class({
|
const NMApplet = new Lang.Class({
|
||||||
Name: 'NMApplet',
|
Name: 'NMApplet',
|
||||||
|
Loading…
Reference in New Issue
Block a user