NetworkMenu: only show 5 connections per device

If we have more than 5 (which can happen with VPN connections), place
them into a More... submenu, which also becomes scrollable if needed.
To protect from race conditions and ordering issues while reading
connections, sort them in alphabetic order when the timestamp is equal.

https://bugzilla.gnome.org/show_bug.cgi?id=651602
This commit is contained in:
Giovanni Campagna 2011-06-02 16:22:36 +02:00
parent ea1e5a5210
commit 82648bc86c

View File

@ -373,12 +373,11 @@ NMDevice.prototype = {
}; };
this._connections.push(obj); this._connections.push(obj);
} }
this._connections.sort(function(one, two) { this._connections.sort(this._connectionSortFunction);
return two.timestamp - one.timestamp;
});
this._activeConnection = null; this._activeConnection = null;
this._activeConnectionItem = null; this._activeConnectionItem = null;
this._autoConnectionItem = null; this._autoConnectionItem = null;
this._overflowItem = null;
if (this.device) { if (this.device) {
this.statusItem = new NMDeviceTitleMenuItem(this._getDescription()); this.statusItem = new NMDeviceTitleMenuItem(this._getDescription());
@ -482,9 +481,7 @@ NMDevice.prototype = {
timestamp: connection._timestamp, timestamp: connection._timestamp,
}; };
this._connections.push(obj); this._connections.push(obj);
this._connections.sort(function(one, two) { this._connections.sort(this._connectionSortFunction);
return two.timestamp - one.timestamp;
});
this._clearSection(); this._clearSection();
this._createSection(); this._createSection();
@ -519,6 +516,13 @@ NMDevice.prototype = {
return this.device.connection_valid(connection); return this.device.connection_valid(connection);
}, },
_connectionSortFunction: function(one, two) {
if (one.timestamp == two.timestamp)
return GLib.utf8_collate(one.name, two.name);
return two.timestamp - one.timestamp;
},
setEnabled: function(enabled) { setEnabled: function(enabled) {
// do nothing by default, we want to keep the conneciton list visible // do nothing by default, we want to keep the conneciton list visible
// in the majority of cases (wired, wwan, vpn) // in the majority of cases (wired, wwan, vpn)
@ -593,6 +597,7 @@ NMDevice.prototype = {
this.section.removeAll(); this.section.removeAll();
this._autoConnectionItem = null; this._autoConnectionItem = null;
this._activeConnectionItem = null; this._activeConnectionItem = null;
this._overflowItem = null;
for (let i = 0; i < this._connections.length; i++) { for (let i = 0; i < this._connections.length; i++) {
this._connections[i].item = null; this._connections[i].item = null;
} }
@ -611,13 +616,23 @@ NMDevice.prototype = {
this.section.addMenuItem(this._activeConnectionItem); this.section.addMenuItem(this._activeConnectionItem);
} }
if (this._connections.length > 0) { if (this._connections.length > 0) {
let activeOffset = this._activeConnectionItem ? 1 : 0;
for(let j = 0; j < this._connections.length; ++j) { for(let j = 0; j < this._connections.length; ++j) {
let obj = this._connections[j]; let obj = this._connections[j];
if (this._activeConnection && if (this._activeConnection &&
obj.connection == this._activeConnection._connection) obj.connection == this._activeConnection._connection)
continue; continue;
obj.item = this._createConnectionItem(obj); obj.item = this._createConnectionItem(obj);
this.section.addMenuItem(obj.item);
if (j + activeOffset >= NUM_VISIBLE_NETWORKS) {
if (!this._overflowItem) {
this._overflowItem = new PopupMenu.PopupSubMenuMenuItem(_("More..."));
this.section.addMenuItem(this._overflowItem);
}
this._overflowItem.menu.addMenuItem(obj.item);
} else
this.section.addMenuItem(obj.item);
} }
} else if (this._autoConnectionName) { } else if (this._autoConnectionName) {
this._autoConnectionItem = new PopupMenu.PopupMenuItem(this._autoConnectionName); this._autoConnectionItem = new PopupMenu.PopupMenuItem(this._autoConnectionName);
@ -825,7 +840,7 @@ NMDeviceModem.prototype = {
})); }));
} }
NMDevice.prototype._init.call(this, client, device, connections, 1); NMDevice.prototype._init.call(this, client, device, connections);
}, },
setEnabled: function(enabled) { setEnabled: function(enabled) {