gnome-shell/js/ui/status/network.js
Jasper St. Pierre 4305ebc5b0 network: Rework the VPN rewrite to work for modem and bluetooth as well
Replace NMNetworkMenuItem with NMConnectionItem, based on
NMVPNConnectionItem, and replace NMDevice with NMConnectionSection
and NMConnectionDevice.

Since this rips apart NMDevice, and since wi-fi should not be
connection-based, we'll temporarily remove NMDeviceWireless. We'll
add it back in a later commit, along with the new Wi-Fi dialog.

https://bugzilla.gnome.org/show_bug.cgi?id=704670
2013-07-28 15:40:23 -04:00

1064 lines
36 KiB
JavaScript

// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
const GLib = imports.gi.GLib;
const GObject = imports.gi.GObject;
const Gio = imports.gi.Gio;
const Lang = imports.lang;
const NetworkManager = imports.gi.NetworkManager;
const NMClient = imports.gi.NMClient;
const NMGtk = imports.gi.NMGtk;
const Signals = imports.signals;
const St = imports.gi.St;
const Hash = imports.misc.hash;
const Main = imports.ui.main;
const PanelMenu = imports.ui.panelMenu;
const PopupMenu = imports.ui.popupMenu;
const MessageTray = imports.ui.messageTray;
const NotificationDaemon = imports.ui.notificationDaemon;
const ModemManager = imports.misc.modemManager;
const Util = imports.misc.util;
const NMConnectionCategory = {
INVALID: 'invalid',
WIRELESS: 'wireless',
WWAN: 'wwan',
VPN: 'vpn'
};
const NMAccessPointSecurity = {
NONE: 1,
WEP: 2,
WPA_PSK: 3,
WPA2_PSK: 4,
WPA_ENT: 5,
WPA2_ENT: 6
};
// small optimization, to avoid using [] all the time
const NM80211Mode = NetworkManager['80211Mode'];
const NM80211ApFlags = NetworkManager['80211ApFlags'];
const NM80211ApSecurityFlags = NetworkManager['80211ApSecurityFlags'];
function ssidCompare(one, two) {
if (!one || !two)
return false;
if (one.length != two.length)
return false;
for (let i = 0; i < one.length; i++) {
if (one[i] != two[i])
return false;
}
return true;
}
function signalToIcon(value) {
if (value > 80)
return 'excellent';
if (value > 55)
return 'good';
if (value > 30)
return 'ok';
if (value > 5)
return 'weak';
return 'none';
}
function ssidToLabel(ssid) {
let label = NetworkManager.utils_ssid_to_utf8(ssid);
if (!label)
label = _("<unknown>");
return label;
}
const NMConnectionItem = new Lang.Class({
Name: 'NMConnectionItem',
_init: function(section, connection) {
this._section = section;
this._connection = connection;
this._activeConnection = null;
this._activeConnectionChangedId = 0;
this.menuItem = new PopupMenu.PopupSwitchMenuItem(connection.get_id(), false);
this.menuItem.connect('toggled', Lang.bind(this, this._toggle));
this._sync();
},
destroy: function() {
this.menuItem.destroy();
},
isActive: function() {
if (this._activeConnection == null)
return false;
return this._activeConnection.state == NetworkManager.ActiveConnectionState.ACTIVATED;
},
_sync: function() {
this.menuItem.setToggleState(this._getIsActive());
this.menuItem.setStatus(this._getStatus());
this.emit('icon-changed');
},
_toggle: function() {
if (this._activeConnection == null)
this._section.activateConnection(this._connection);
else
this._section.deactivateConnection(this._activeConnection);
this._sync();
},
_getStatus: function() {
return null;
},
_connectionStateChanged: function(ac, newstate, reason) {
this._sync();
},
setActiveConnection: function(activeConnection) {
if (this._activeConnectionChangedId > 0) {
this._activeConnection.disconnect(this._activeConnectionChangedId);
this._activeConnectionChangedId = 0;
}
this._activeConnection = activeConnection;
if (this._activeConnection)
this._activeConnectionChangedId = this._activeConnection.connect('state-changed',
Lang.bind(this, this._connectionStateChanged));
this._sync();
},
});
Signals.addSignalMethods(NMConnectionItem.prototype);
const NMConnectionSection = new Lang.Class({
Name: 'NMConnectionSection',
Abstract: true,
_init: function(client) {
this._client = client;
this._connectionItems = new Hash.Map();
this._connections = [];
this.section = new PopupMenu.PopupMenuSection();
this.statusItem = new PopupMenu.PopupSwitchMenuItem('', this.connected);
this._statusChanged = this.statusItem.connect('toggled', Lang.bind(this, function(item, state) {
let ok;
if (state)
ok = this.activate();
else
ok = this.deactivate();
if (!ok)
item.setToggleState(!state);
}));
this._updateStatusItem();
},
destroy: function() {
this.statusItem.destroy();
this.section.destroy();
},
get connected() {
return false;
},
_hasConnection: function(connection) {
return this._connectionItems.has(connection.get_uuid());
},
_connectionValid: function(connection) {
return true;
},
_connectionSortFunction: function(one, two) {
if (one._timestamp == two._timestamp)
return GLib.utf8_collate(one.get_id(), two.get_id());
return two._timestamp - one._timestamp;
},
_makeConnectionItem: function(connection) {
return new NMConnectionItem(this, connection);
},
checkConnection: function(connection) {
if (!this._connectionValid(connection))
return;
if (this._hasConnection(connection))
return;
this._addConnection(connection);
},
_addConnection: function(connection) {
let item = this._makeConnectionItem(connection);
if (!item)
return;
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);
}));
let pos = Util.insertSorted(this._connections, connection, this._connectionSortFunction);
this.section.addMenuItem(item.menuItem, pos);
this._connectionItems.set(connection.get_uuid(), item);
},
removeConnection: function(connection) {
this._connectionItems.get(connection.get_uuid()).destroy();
this._connectionItems.delete(connection.get_uuid());
let pos = this._connections.indexOf(connection);
this._connections.splice(pos, 1);
},
});
Signals.addSignalMethods(NMConnectionSection.prototype);
const NMConnectionDevice = new Lang.Class({
Name: 'NMConnectionDevice',
Extends: NMConnectionSection,
Abstract: true,
_init: function(client, device) {
this.parent(client);
this._device = device;
this._stateChangedId = this._device.connect('state-changed', Lang.bind(this, this._deviceStateChanged));
this._activeConnectionChangedId = this._device.connect('notify::active-connection', Lang.bind(this, this._activeConnectionChanged));
},
destroy: function() {
if (this._stateChangedId) {
GObject.Object.prototype.disconnect.call(this._device, this._stateChangedId);
this._stateChangedId = 0;
}
if (this._activeConnectionChangedId) {
GObject.Object.prototype.disconnect.call(this._device, this._activeConnectionChangedId);
this._stateChangedId = 0;
}
this.parent();
},
_deviceStateChanged: function(device, newstate, oldstate, reason) {
if (newstate == oldstate) {
log('device emitted state-changed without actually changing state');
return;
}
/* Emit a notification if activation fails, but don't do it
if the reason is no secrets, as that indicates the user
cancelled the agent dialog */
if (newstate == NetworkManager.DeviceState.FAILED &&
reason != NetworkManager.DeviceStateReason.NO_SECRETS) {
this.emit('activation-failed', reason);
}
this._updateStatusItem();
},
_connectionValid: function(connection) {
return this._device.connection_valid(connection);
},
activateConnection: function(connection) {
this._client.activate_connection(connection, this._device, null, null);
},
deactivateConnection: function(activeConnection) {
this._device.disconnect(null);
},
deactivate: function() {
this._device.disconnect(null);
return true;
},
activate: function() {
if (this._activeConnection)
// nothing to do
return true;
// If there is only one connection available, use that
// Otherwise, if no connection is currently configured,
// try automatic configuration (or summon the config dialog)
if (this._connections.length == 1) {
this._client.activate_connection(this._connections[0].connection, this._device || null, null, null);
return true;
} else if (this._connections.length == 0) {
return this._activateAutomaticConnection();
}
return false;
},
_activateAutomaticConnection: function() {
let connection = new NetworkManager.Connection();
this._client.add_and_activate_connection(connection, this._device, null, null);
return true;
},
get connected() {
return this._device && this._device.state == NetworkManager.DeviceState.ACTIVATED;
},
_activeConnectionChanged: function() {
let activeConnection = this._device.active_connection;
if (activeConnection == this._activeConnection)
// nothing to do
return;
this._activeConnection = activeConnection;
},
_getStatus: function() {
if (!this._device)
return null;
switch(this._device.state) {
case NetworkManager.DeviceState.DISCONNECTED:
case NetworkManager.DeviceState.ACTIVATED:
return null;
case NetworkManager.DeviceState.UNMANAGED:
/* Translators: this is for network devices that are physically present but are not
under NetworkManager's control (and thus cannot be used in the menu) */
return _("unmanaged");
case NetworkManager.DeviceState.DEACTIVATING:
return _("disconnecting...");
case NetworkManager.DeviceState.PREPARE:
case NetworkManager.DeviceState.CONFIG:
case NetworkManager.DeviceState.IP_CONFIG:
case NetworkManager.DeviceState.IP_CHECK:
case NetworkManager.DeviceState.SECONDARIES:
return _("connecting...");
case NetworkManager.DeviceState.NEED_AUTH:
/* Translators: this is for network connections that require some kind of key or password */
return _("authentication required");
case NetworkManager.DeviceState.UNAVAILABLE:
// This state is actually a compound of various states (generically unavailable,
// firmware missing), that are exposed by different properties (whose state may
// or may not updated when we receive state-changed).
if (this._device.firmware_missing) {
/* Translators: this is for devices that require some kind of firmware or kernel
module, which is missing */
return _("firmware missing");
}
/* Translators: this is for a network device that cannot be activated (for example it
is disabled by rfkill, or it has no coverage */
return _("unavailable");
case NetworkManager.DeviceState.FAILED:
return _("connection failed");
default:
log('Device state invalid, is %d'.format(this._device.state));
return 'invalid';
}
},
syncDescription: function() {
if (this._device && this._device._description)
this.statusItem.label.text = this._device._description;
},
_updateStatusItem: function() {
this.statusItem.setStatus(this.getStatusLabel());
this.statusItem.setToggleState(this.connected);
},
_substateChanged: function() {
this.statusItem.setStatus(this.getStatusLabel());
},
});
const NMDeviceModem = new Lang.Class({
Name: 'NMDeviceModem',
Extends: NMConnectionDevice,
category: NMConnectionCategory.WWAN,
_init: function(client, device) {
this.parent(client, device);
this._mobileDevice = null;
let capabilities = device.current_capabilities;
if (device.udi.indexOf('/org/freedesktop/ModemManager1/Modem') == 0)
this._mobileDevice = new ModemManager.BroadbandModem(device.udi, capabilities);
else if (capabilities & NetworkManager.DeviceModemCapabilities.GSM_UMTS)
this._mobileDevice = new ModemManager.ModemGsm(device.udi);
else if (capabilities & NetworkManager.DeviceModemCapabilities.CDMA_EVDO)
this._mobileDevice = new ModemManager.ModemCdma(device.udi);
else if (capabilities & NetworkManager.DeviceModemCapabilities.LTE)
this._mobileDevice = new ModemManager.ModemGsm(device.udi);
if (this._mobileDevice) {
this._operatorNameId = this._mobileDevice.connect('notify::operator-name', Lang.bind(this, function() {
if (this._operatorItem) {
let name = this._mobileDevice.operator_name;
if (name) {
this._operatorItem.label.text = name;
this._operatorItem.actor.show();
} else
this._operatorItem.actor.hide();
}
}));
this._signalQualityId = this._mobileDevice.connect('notify::signal-quality', Lang.bind(this, function() {
if (this._operatorItem) {
this._operatorItem.setIcon(this._getSignalIcon());
}
this.emit('icon-changed');
}));
}
},
destroy: function() {
if (this._operatorNameId) {
this._mobileDevice.disconnect(this._operatorNameId);
this._operatorNameId = 0;
}
if (this._signalQualityId) {
this._mobileDevice.disconnect(this._signalQualityId);
this._signalQualityId = 0;
}
this.parent();
},
_getSignalIcon: function() {
return 'network-cellular-signal-' + signalToIcon(this._mobileDevice.signal_quality) + '-symbolic';
},
_createSection: function() {
if (!this._shouldShowConnectionList())
return;
if (this._mobileDevice) {
// If operator_name is null, just pass the empty string, as the item is hidden anyway
this._operatorItem = new PopupMenu.PopupImageMenuItem(this._mobileDevice.operator_name || '',
this._getSignalIcon(),
{ reactive: false });
if (!this._mobileDevice.operator_name)
this._operatorItem.actor.hide();
this.section.addMenuItem(this._operatorItem);
}
this.parent();
},
_clearSection: function() {
this._operatorItem = null;
this.parent();
},
_activateAutomaticConnection: function() {
// Mobile wizard is too complex for the shell UI and
// is handled by the network panel
Util.spawn(['gnome-control-center', 'network',
'connect-3g', this._device.get_path()]);
return true;
},
getIndicatorIcon: function() {
if (this._device.active_connection.state == NetworkManager.ActiveConnectionState.ACTIVATING)
return 'network-cellular-acquiring-symbolic';
if (!this._mobileDevice) {
// this can happen for bluetooth in PAN mode
return 'network-cellular-connected-symbolic';
}
return this._getSignalIcon();
}
});
const NMDeviceBluetooth = new Lang.Class({
Name: 'NMDeviceBluetooth',
Extends: NMConnectionDevice,
category: NMConnectionCategory.WWAN,
_activateAutomaticConnection: function() {
// FIXME: DUN devices are configured like modems, so
// We need to spawn the mobile wizard
// but the network panel doesn't support bluetooth at the moment
// so we just create an empty connection and hope
// that this phone supports PAN
return this.parent();
},
getIndicatorIcon: function() {
if (this._device.active_connection.state == NetworkManager.ActiveConnectionState.ACTIVATING)
return 'network-wired-acquiring-symbolic';
else
return 'network-wired-symbolic';
},
});
const NMVPNConnectionItem = new Lang.Class({
Name: 'NMVPNConnectionItem',
Extends: NMConnectionItem,
isActive: function() {
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.ACTIVATED:
return null;
case NetworkManager.VPNConnectionState.PREPARE:
case NetworkManager.VPNConnectionState.CONNECT:
case NetworkManager.VPNConnectionState.IP_CONFIG_GET:
return _("connecting...");
case NetworkManager.VPNConnectionState.NEED_AUTH:
/* Translators: this is for network connections that require some kind of key or password */
return _("authentication required");
case NetworkManager.VPNConnectionState.FAILED:
return _("connection failed");
default:
return 'invalid';
}
},
_connectionStateChanged: function(ac, newstate, reason) {
if (newstate == NetworkManager.VPNConnectionState.FAILED &&
reason != NetworkManager.VPNConnectionStateReason.NO_SECRETS) {
// FIXME: if we ever want to show something based on reason,
// we need to convert from NetworkManager.VPNConnectionStateReason
// to NetworkManager.DeviceStateReason
this.emit('activation-failed', reason);
}
this.parent();
},
setActiveConnection: function(activeConnection) {
if (this._activeConnectionChangedId > 0) {
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';
else
return 'network-vpn-symbolic';
} else {
return '';
}
},
});
const NMVPNSection = new Lang.Class({
Name: 'NMVPNSection',
Extends: NMConnectionSection,
category: NMConnectionCategory.VPN,
activateConnection: function(connection) {
this._client.activate_connection(connection, null, null, null);
},
deactivateConnection: function(activeConnection) {
this._client.deactivate_connection(activeConnection);
},
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);
},
_makeConnectionItem: function(connection) {
return new NMVPNConnectionItem(this, connection);
},
getIndicatorIcon: function() {
let items = this._connectionItems.values();
for (let i = 0; i < items.length; i++) {
let item = items[i];
let icon = item.getIndicatorIcon();
if (icon)
return icon;
}
return '';
},
});
Signals.addSignalMethods(NMVPNSection.prototype);
const NMApplet = new Lang.Class({
Name: 'NMApplet',
Extends: PanelMenu.SystemStatusButton,
_init: function() {
this.parent('network-offline-symbolic', _('Network'));
this._vpnIcon = this.addIcon(null);
this._vpnIcon.hide();
// Device types
this._dtypes = { };
// this._dtypes[NetworkManager.DeviceType.WIFI] = NMDeviceWireless;
this._dtypes[NetworkManager.DeviceType.MODEM] = NMDeviceModem;
this._dtypes[NetworkManager.DeviceType.BT] = NMDeviceBluetooth;
// TODO: WiMax support
// Connection types
this._ctypes = { };
this._ctypes[NetworkManager.SETTING_WIRELESS_SETTING_NAME] = NMConnectionCategory.WIRELESS;
this._ctypes[NetworkManager.SETTING_BLUETOOTH_SETTING_NAME] = NMConnectionCategory.WWAN;
this._ctypes[NetworkManager.SETTING_CDMA_SETTING_NAME] = NMConnectionCategory.WWAN;
this._ctypes[NetworkManager.SETTING_GSM_SETTING_NAME] = NMConnectionCategory.WWAN;
this._ctypes[NetworkManager.SETTING_VPN_SETTING_NAME] = NMConnectionCategory.VPN;
NMClient.Client.new_async(null, Lang.bind(this, this._clientGot));
NMClient.RemoteSettings.new_async(null, null, Lang.bind(this, this._remoteSettingsGot));
},
_clientGot: function(obj, result) {
this._client = NMClient.Client.new_finish(result);
this._tryLateInit();
},
_remoteSettingsGot: function(obj, result) {
this._settings = NMClient.RemoteSettings.new_finish(result);
this._tryLateInit();
},
_tryLateInit: function() {
if (!this._client || !this._settings)
return;
this._activeConnections = [ ];
this._connections = [ ];
this._mainConnection = null;
this._mainConnectionIconChangedId = 0;
this._nmDevices = [];
this._devices = { };
this._section = new PopupMenu.PopupMenuSection();
this.menu.addMenuItem(this._section);
this._devices.wireless = {
section: new PopupMenu.PopupMenuSection(),
devices: [ ],
};
this._section.addMenuItem(this._devices.wireless.section);
this._devices.wwan = {
section: new PopupMenu.PopupMenuSection(),
devices: [ ],
};
this._section.addMenuItem(this._devices.wwan.section);
this._vpnSection = new NMVPNSection(this._client);
this._vpnSection.connect('activation-failed', Lang.bind(this, this._onActivationFailed));
this._vpnSection.connect('icon-changed', Lang.bind(this, this._updateIcon));
this._section.addMenuItem(this._vpnSection.section);
this._readConnections();
this._readDevices();
this._syncNMState();
this._client.connect('notify::manager-running', Lang.bind(this, this._syncNMState));
this._client.connect('notify::networking-enabled', Lang.bind(this, this._syncNMState));
this._client.connect('notify::state', Lang.bind(this, this._syncNMState));
this._client.connect('notify::active-connections', Lang.bind(this, this._syncActiveConnections));
this._client.connect('device-added', Lang.bind(this, this._deviceAdded));
this._client.connect('device-removed', Lang.bind(this, this._deviceRemoved));
this._settings.connect('new-connection', Lang.bind(this, this._newConnection));
},
_ensureSource: function() {
if (!this._source) {
this._source = new MessageTray.Source(_("Network Manager"),
'network-transmit-receive');
this._source.policy = new NotificationDaemon.NotificationApplicationPolicy('gnome-network-panel');
this._source.connect('destroy', Lang.bind(this, function() {
this._source = null;
}));
Main.messageTray.add(this._source);
}
},
_readDevices: function() {
let devices = this._client.get_devices() || [ ];
for (let i = 0; i < devices.length; ++i) {
this._deviceAdded(this._client, devices[i], true);
}
this._syncDeviceNames();
},
_notifyForDevice: function(device, iconName, title, text, urgency) {
if (device._notification)
device._notification.destroy();
/* must call after destroying previous notification,
or this._source will be cleared */
this._ensureSource();
let gicon = new Gio.ThemedIcon({ name: iconName });
device._notification = new MessageTray.Notification(this._source, title, text,
{ gicon: gicon });
device._notification.setUrgency(urgency);
device._notification.setTransient(true);
device._notification.connect('destroy', function() {
device._notification = null;
});
this._source.notify(device._notification);
},
_onActivationFailed: function(device, reason) {
// XXX: nm-applet has no special text depending on reason
// but I'm not sure of this generic message
this._notifyForDevice(device, 'network-error-symbolic',
_("Connection failed"),
_("Activation of network connection failed"),
MessageTray.Urgency.HIGH);
},
_syncDeviceNames: function() {
let names = NMGtk.utils_disambiguate_device_names(this._nmDevices);
for (let i = 0; i < this._nmDevices.length; i++) {
let device = this._nmDevices[i];
device._description = names[i];
if (device._delegate)
device._delegate.syncDescription();
}
},
_deviceAdded: function(client, device, skipSyncDeviceNames) {
if (device._delegate) {
// already seen, not adding again
return;
}
let wrapperClass = this._dtypes[device.get_device_type()];
if (wrapperClass) {
let wrapper = new wrapperClass(this._client, device);
device._delegate = wrapper;
this._addDeviceWrapper(wrapper);
this._nmDevices.push(device);
if (!skipSyncDeviceNames)
this._syncDeviceNames();
this._connections.forEach(function(connection) {
wrapper.checkConnection(connection);
});
}
},
_addDeviceWrapper: function(wrapper) {
wrapper._activationFailedId = wrapper.connect('activation-failed',
Lang.bind(this, this._onActivationFailed));
let section = this._devices[wrapper.category].section;
section.addMenuItem(wrapper.statusItem);
section.addMenuItem(wrapper.section);
let devices = this._devices[wrapper.category].devices;
devices.push(wrapper);
},
_deviceRemoved: function(client, device) {
let pos = this._nmDevices.indexOf(device);
if (pos != -1) {
this._nmDevices.splice(pos, 1);
this._syncDeviceNames();
}
let wrapper = device._delegate;
if (!wrapper) {
log('Removing a network device that was not added');
return;
}
this._removeDeviceWrapper(wrapper);
},
_removeDeviceWrapper: function(wrapper) {
wrapper.disconnect(wrapper._activationFailedId);
wrapper.destroy();
let devices = this._devices[wrapper.category].devices;
let pos = devices.indexOf(wrapper);
devices.splice(pos, 1);
},
_getSupportedActiveConnections: function() {
let activeConnections = this._client.get_active_connections() || [ ];
let supportedConnections = [];
for (let i = 0; i < activeConnections.length; i++) {
let devices = activeConnections[i].get_devices();
if (!devices || !devices[0])
continue;
// Ignore connections via unrecognized device types
if (!this._dtypes[devices[0].device_type])
continue;
// Ignore slave connections
let connectionPath = activeConnections[i].connection;
let connection = this._settings.get_connection_by_path(connectionPath);
// connection might be null, if libnm-glib fails to create
// the object due to version incompatibility, or if the
// connection is not visible to the current user
if (connection && this._ignoreConnection(connection))
continue;
supportedConnections.push(activeConnections[i]);
}
return supportedConnections;
},
_syncActiveConnections: function() {
let closedConnections = [ ];
let newActiveConnections = this._getSupportedActiveConnections();
for (let i = 0; i < this._activeConnections.length; i++) {
let a = this._activeConnections[i];
if (newActiveConnections.indexOf(a) == -1) // connection is removed
closedConnections.push(a);
}
for (let i = 0; i < closedConnections.length; i++) {
let a = closedConnections[i];
if (a._type == NetworkManager.SETTING_VPN_SETTING_NAME)
this._vpnSection.removeActiveConnection(a);
if (a._inited) {
a.disconnect(a._notifyStateId);
a.disconnect(a._notifyDefaultId);
a.disconnect(a._notifyDefault6Id);
a._inited = false;
}
}
if (this._mainConnectionIconChangedId > 0) {
this._mainConnection._primaryDevice.disconnect(this._mainConnectionIconChangedId);
this._mainConnectionIconChangedId = 0;
}
this._activeConnections = newActiveConnections;
this._mainConnection = null;
let activating = null;
let default_ip4 = null;
let default_ip6 = null;
let active_any = null;
for (let i = 0; i < this._activeConnections.length; i++) {
let a = this._activeConnections[i];
if (!a._inited) {
a._notifyDefaultId = a.connect('notify::default', Lang.bind(this, this._syncActiveConnections));
a._notifyDefault6Id = a.connect('notify::default6', Lang.bind(this, this._syncActiveConnections));
a._notifyStateId = a.connect('notify::state', Lang.bind(this, this._notifyActivated));
a._inited = true;
}
if (!a._connection) {
a._connection = this._settings.get_connection_by_path(a.connection);
if (a._connection) {
a._type = a._connection._type;
a._section = this._ctypes[a._type];
} else {
a._connection = null;
a._type = null;
a._section = null;
log('Cannot find connection for active (or connection cannot be read)');
}
}
if (a['default'])
default_ip4 = a;
if (a.default6)
default_ip6 = a;
if (a.state == NetworkManager.ActiveConnectionState.ACTIVATING)
activating = a;
else if (a.state == NetworkManager.ActiveConnectionState.ACTIVATED)
active_any = a;
if (!a._primaryDevice) {
if (a._type != NetworkManager.SETTING_VPN_SETTING_NAME) {
// This list is guaranteed to have one device in it.
a._primaryDevice = a.get_devices()[0]._delegate;
} else {
a._primaryDevice = this._vpnSection;
this._vpnSection.addActiveConnection(a);
}
if (a.state == NetworkManager.ActiveConnectionState.ACTIVATED
&& a._primaryDevice && a._primaryDevice._notification) {
a._primaryDevice._notification.destroy();
a._primaryDevice._notification = null;
}
}
}
this._mainConnection = activating || default_ip4 || default_ip6 || active_any || null;
if (this._mainConnection) {
let dev = this._mainConnection._primaryDevice;
this._mainConnectionIconChangedId = dev.connect('icon-changed', Lang.bind(this, this._updateIcon));
this._updateIcon();
}
},
_notifyActivated: function(activeConnection) {
if (activeConnection.state == NetworkManager.ActiveConnectionState.ACTIVATED
&& activeConnection._primaryDevice && activeConnection._primaryDevice._notification) {
activeConnection._primaryDevice._notification.destroy();
activeConnection._primaryDevice._notification = null;
}
this._syncActiveConnections();
},
_ignoreConnection: function(connection) {
let setting = connection.get_setting_connection();
if (!setting)
return true;
// Ignore slave connections
if (setting.get_master())
return true;
return false;
},
_addConnection: function(connection) {
if (this._ignoreConnection(connection))
return;
if (connection._updatedId) {
// connection was already seen
return;
}
connection._removedId = connection.connect('removed', Lang.bind(this, this._connectionRemoved));
connection._updatedId = connection.connect('updated', Lang.bind(this, this._updateConnection));
this._updateConnection(connection);
this._connections.push(connection);
},
_readConnections: function() {
let connections = this._settings.list_connections();
connections.forEach(Lang.bind(this, this._addConnection));
},
_newConnection: function(settings, connection) {
this._addConnection(connection);
this._syncActiveConnections();
},
_connectionRemoved: function(connection) {
let pos = this._connections.indexOf(connection);
if (pos != -1)
this._connections.splice(connection, 1);
let section = connection._section;
if (section == NMConnectionCategory.INVALID)
return;
if (section == NMConnectionCategory.VPN) {
this._vpnSection.removeConnection(connection);
} else {
let devices = this._devices[section].devices;
for (let i = 0; i < devices.length; i++)
devices[i].removeConnection(connection);
}
connection.disconnect(connection._removedId);
connection.disconnect(connection._updatedId);
connection._removedId = connection._updatedId = 0;
},
_updateConnection: function(connection) {
let connectionSettings = connection.get_setting_by_name(NetworkManager.SETTING_CONNECTION_SETTING_NAME);
connection._type = connectionSettings.type;
connection._section = this._ctypes[connection._type] || NMConnectionCategory.INVALID;
connection._timestamp = connectionSettings.timestamp;
let section = connection._section;
if (section == NMConnectionCategory.INVALID)
return;
if (section == NMConnectionCategory.VPN) {
this._vpnSection.checkConnection(connection);
} else {
let devices = this._devices[section].devices;
for (let i = 0; i < devices.length; i++) {
devices[i].checkConnection(connection);
}
}
},
_syncNMState: function() {
this.mainIcon.visible = this._client.manager_running;
this.actor.visible = this.mainIcon.visible;
this._syncActiveConnections();
this._section.actor.visible = this._client.networking_enabled;
},
_updateIcon: function() {
let hasApIcon = false;
let hasMobileIcon = false;
if (!this._client.networking_enabled || !this._mainConnection) {
this.setIcon('network-offline-symbolic');
} else {
let dev = this._mainConnection._primaryDevice;
if (!dev) {
log('Active connection with no primary device?');
return;
}
this.setIcon(dev.getIndicatorIcon());
}
this._vpnIcon.icon_name = this._vpnSection.getIndicatorIcon();
}
});