Setting a hotkey for connecting and disconnecting a network connection in KDE environment
Control of the NetworkManger
The NetworkManger can be controlled from the commandline:
man nmcli:
NAME
nmcli – command‐line tool for controlling NetworkManager
SYNOPSIS
nmcli [ OPTIONS ] OBJECT { COMMAND | help }
...
The man page on the net: http://manpages.ubuntu.com/manpages/saucy/man1/nmcli.1.html
With the dbus:
- Configuration of the Network Manager via DBus: how to set the ad hoc mode
- http://cheesehead-techblog.blogspot.fi/2012/09/dbus-tutorial-fun-with-network-manager.html
Example - working at here:
Disconnecting from the current network connection and staying disconnected:
qdbus --system --literal org.freedesktop.NetworkManager /org/freedesktop/NetworkManager/Devices/0 org.freedesktop.NetworkManager.Device.Disconnect
Connecting the network with th UUID:
1) Reading the UUID:
nmcli -p c
At here the UUID is 81540c01-2377-4d79-9763-d0f3104c81f3
2) Activating the connection:
nmcli c up uuid 81540c01-2377-4d79-9763-d0f3104c81f3
Hotkeys
One way to add the shortcuts/hotkeys is to use the KDE menu editor.
The command can be added to the KDE menu with a shortcut to run the command.
The KDE document can be found: K-Menu > Applications > Help ...
The KDE menu editor handbook on the net: http://docs.kde.org/development/en/kde-workspace/kmenuedit/index.html
Thanks to user26687 these are the commands I use for reconnecting:
$ nmcli -p c
$ nmcli c up uuid 6e6c6430-ffae-4dc3-aeb8-60a89fa63796
the first one to get uuid. If there's one connection, one use
nmcli c up uuid "$(nmcli -t -f uuid c)"
instead.
And these are what I use to disconnect:
$ nmcli dev status
$ nmcli dev disconnect iface eth1
For me, qdbus
is useless because of upgrading to Qt5.
To give it a toggling nature which is needed to set a single hotkey for both enabling and disabling, probably a C++ code is appropriate for combining and automating these commands (time-consuming for me). Two hotkeys amy be better for me. I use this method for setting a hotkey for a command:
https://askubuntu.com/a/393343/126106
Still I worry about uuid
(to be volatile).