The tow most important Applets for this purpose are Button and Label
NOTICE:
The shown examples are thought as EXAMPLES to show how things work and allow you to create solutions to your own needs.
It's not inteded to have any kind of complete solutions here
Sharing scripts and definitions is however certainly not the worst of all things.
First of all you'll need an Applet definition in your config:
[netcfg] Icon=network-wireless Menu=netcfg MenuUpdater=/home/joe_user/.kde/share/apps/be.shell/scripts/netcfg-profiles Mode=0 Type=Button
This says it's a Icon only (Mode=0) Button with the network-wireless icon. So far so trivial.
The interesting part are the Menu and the MenuUpdater.
The latter is the path to (in this case) a script that does nothing but on the fly create a "netcfg" menu structure.
Let's have a look at this script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #!/bin/sh file="`kde4-config --path data`" BE_NETCFG="${file%%:*}be.shell/scripts/netcfg" BE_WIFI_SELECT="${file%%:*}be.shell/scripts/wifi-select" file="${file%%:*}be.shell/netcfg.xml" CURRENT="`/usr/bin/netcfg current 2>/dev/null`" echo '<menu>' > "$file" echo "<action label=\""Bestes"\" exec=\""$BE_NETCFG __best"\"/>" >> "$file" for network in `netcfg -l`; do if [ "$network" != "$CURRENT" ]; then echo "<action label=\""$network"\" exec=\""$BE_NETCFG $network"\"/>" >> "$file" fi done echo '<separator/>' >> "$file" echo "<action label=\"Scan Access Points\" exec=\"$BE_WIFI_SELECT\"/>" >> "$file" if [ ! -z "$CURRENT" ]; then echo '<separator/>' >> "$file" echo "<action label=\"Disconnect\" exec=\"sudo netcfg -a\"/>" >> "$file" fi echo '<separator/>' >> "$file" echo "<action label=\"Restart network\" exec=\"sudo /etc/rc.d/network restart\"/>" >> "$file" echo '</menu>' >> "$file" |
Looks complex?
Is not - just writes an xml structure as is used by all menus in BE::Shell that allows to call other scripts to select the current network.
be.shell/scripts/netcfg is basically a wrapper around netcfg & sudo (what means you need "netcfg" and "iwlist wlan0 scan" in your NOPASSWD list to use it - i use it on a touch device) with slight adjustments it's easily possible to run the script with kdesu and password entry instead
be.shell/scripts/wifi-select is a "KDE-fied" port of stock (ncurses) wifi-select from Archlinux (ie. it's functionally the same, but using "kdialog" instead of "dialog")
Now, you also want to know about your connection quality?
See the be-shell/scripts/netquality example script which checks the scanning result and updates the icon via dbus - this is really simple:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #!/bin/sh export DISPLAY=:0 QUALITY=`iwlist scan 2>/dev/null | sed -e '/Quality/!d; s/.*Quality=\([0-9]*\).*/\1/'` if ((QUALITY > 61)); then ICON=network-wireless-connected-100 elif ((QUALITY > 44)); then ICON=network-wireless-connected-75 elif ((QUALITY > 26)); then ICON=network-wireless-connected-50 elif ((QUALITY > 9)); then ICON=network-wireless-connected-25 else ICON=network-wireless-connected-00 fi qdbus org.kde.be.shell /netcfg setIcon "$ICON" |
You still have to invoke it. Either you run it in some while loop OR you do what unix users do since epoch - you use a cronjob:
$ crontab -e */5 * * * * /home/joe_user/.kde/share/apps/be.shell/scripts/netquality
Tada.
This runs the script every 5 mintues - for a complete explanation check "man crontab" - oh and of course "kcmshell4 cron" will launch you a GUI crontab editor ;-)
First off, the definition:
[Apt] Icon=utilties-file-archiver Exec=muon-updater Mode=2 Type=Button
This says it's a Icon and Text (Mode=2) Button with the utilties-file-archiver icon
The only interesting element is the script:
1 2 3 4 5 6 7 8 9 10 | #!/bin/sh export DISPLAY=:0 # qdbus needs a the X server UPDATES=$(aptitude search "~U") # query updates if [ -z "$UPDATES" ]; then COUNT=0 else COUNT=`echo "$UPDATES" | wc -l` fi qdbus org.kde.be.shell /Apt setText "$COUNT" qdbus org.kde.be.shell /Apt setToolTip "$UPDATES" |
This fetches the list of available packages using some aptitude magics (at least i was told so) and then sets the amount of updates as label on the icon and the packagelist itself as tooltip.
Done.
The script and a similar be.pacman for Archlinux is in the scripts subdir
Consider making this a cronjob by every 2 hours or so.
* */2 * * * /home/joe_user/.kde/share/apps/be.shell/scripts/be.apt
The Label applets can obtain their content by polling a file or the output of an executable, but the by far more interesting approach is to feed them through a FIFO. This allows you to you redirect the contents of other files or persistent commands.
"top" is the CLI tool to show processing usage and -nice enough- it also has a batch output for script invocation.
Unfortunately, if you run "top -bn1" (once, using batch output mode) you basically get junk.
The reason is, that a reasonable idea of actualy CPU load requires to monitor the process over some time, so what you would actually do would be "top -bd5" what runs "top" forever, with a 5 second delay.
So far so good, this would actually also work with
Exec=top -bd5 PollInterval=0
turning the label into continuosly printing the stdout of the process, but the output of that command is less then ideal.
We'd maybe prefer sth. like
top -bd5 | cut -c1-11,42-51,62- | grep --line-buffered --no-group-separator -A4 '%MEM'
Type that into konsole to see the result, ctrl+c exits execution.
Unfortunately this becomes a little complex for simple QProcess redirection and it's actually a rather simple case, chosen for explanation.
So what we do is to run this is a bash script and redirect it into a FIFO
The good part is that there's a generic framework in the scripts that you only need to invoke and then don't have to do more then just writing the actual output code:
1 2 3 4 5 6 | #!/bin/sh FIFO=resources daemon_func() { top -bd5 | cut -c1-11,42-51,62- | grep --line-buffered --no-group-separator -A4 '%MEM' > /tmp/`whoami`/be.shell/resources } source `dirname "$0"`/be.fifo.daemon |
The shebang is followed by the assignment of the FIFO you want to use (it should be unique, the FIFOs created this way all end up in /tmp/`whoami`/be.shell/)
The next thing is the function that actually writes into the FIFO.
It can be our permanent "top" as well as a while loop (with a "sleep" invocation, see next artile ;-)
Important is that it should run as long as you want the label to be updated (in doubt: forever) and writes the desired output into /tmp/`whoami`/be.shell/${FIFO}
The last statement includes the code that does the daemon thing. It ensures there's such a FIFO the Label then can read and provides parameters to start and stop the daemon, clean up things, ensure the daemon cannot be started twice, etc.
This is it. Suppose you saved that script as ~/.kde/share/apps/be.shell/scripts/topfeeder.sh and made it executable.
Now the only thing you need to do is to run "~/.kde/share/apps/be.shell/scripts/topfeeder.sh start" pre-startup in "kcmshell4 autostart"
NOTICE:
Running pre-startup is MANDATORY with the be.fifo.daemon framework because
a) it places the FIFOs into /tmp which is usually wiped on restarts (and eventually in tempfs)
b) BE::Shell Labels do not waste resources pinging the file every now and the to figure whether maybe a FIFO has appeared at the configured location. The file must be there before the Label is configured!