slitaz-tools view tazbox/tazbox @ rev 878

tazbox: add icons to the buttons (Yad 0.26 supports custom icons)
author Aleksej Bobylev <al.bobylev@gmail.com>
date Fri May 02 20:02:16 2014 +0000 (2014-05-02)
parents 1d0373136e45
children 5a8adba12ac2
line source
1 #!/bin/sh
2 #
3 # SliTaz tiny GUI boxes for the desktop (su, logout, locale, etc)
4 # and as usual, please: KISS
5 #
6 # Copyright (C) 2011-2014 SliTaz GNU/Linux - GNU GPL v3
7 # - Christophe Lincoln <pankso@slitaz.org>
8 # - Aleksej Bobylev <al.bobylev@gmail.com>
9 #
10 . /lib/libtaz.sh
11 export TEXTDOMAIN='tazbox' # i18n
13 # download dir (may be in a config file)
14 DOWNLOADS=$HOME/Downloads
16 # some constants to be used inside functions
17 tmp=/tmp/keymap.list
18 db=/usr/share/i18n/locales
19 zi=/usr/share/zoneinfo/
20 ztab=${zi}zone-mini.tab
21 csv=/tmp/freegeoip.csv
23 #
24 # Functions
25 #
27 usage() {
28 newline; _ 'SliTaz tiny GUI boxes for the desktop'
30 newline; boldify $(_ 'Usage:')
31 echo " $(basename $0) [$(_n 'command')]"
33 newline; boldify $(_ 'Commands:')
34 optlist "\
35 usage $(_ 'Display this short help usage')
36 su $(_ 'Execute a command as super-user')
37 logout $(_ 'Desktop logout box with actions')
38 out $(_ 'Pipe a command output into a GTK window')
39 out-dl $(_ 'Pipe wget output into a GTK window')
40 locale $(_ 'Configure system language (root)')
41 keymap $(_ 'Configure system keymap (root)')
42 tz $(_ 'Configure system timezone (root)')
43 setup $(_ 'System initial setup (locale, keymap & timezone)')
44 new-file $(_ 'Create a new file or folder on the desktop')
45 all-apps $(_ 'Display icons of all installed applications')
46 notify $(_ 'Notify user with a desktop centered box')
47 tazapps $(_ 'Configure SliTaz default applications')"
48 newline
49 }
51 # try to find icon in .desktop files
52 find_icon() {
53 local desktop
54 desktop=$(find $HOME/.local/share/applications /usr/share/applications \
55 -name $1.desktop 2&>/dev/null | head -n1)
56 [ x$desktop != x ] && cat $desktop | sed '/\[Desktop\ Entry\]/,/^\[/!d' | \
57 sed '/^Icon=/!d' | head -n1 | cut -d= -f2
58 }
60 # su frontend GUIs
61 su_main() {
62 CMD1="$1"; shift; CMD2="$(echo $@ | sed 's|&|&amp;|g')"
63 : ${icon=$(find_icon $CMD1)}
64 [ x$icon == x ] && icon=gtk-dialog-authentication
66 yad --title="$(_n 'SliTaz admin password')" --window-icon=$icon \
67 --width=520 --on-top --center \
68 --image=$icon --image-on-top \
69 --text="$(_n 'Please enter root password (default root) to execute:')\n
70 <span foreground=\"red\"><tt><b>$CMD1</b> $CMD2</tt></span>\n" \
71 --form \
72 --field="$(_n 'Password:'):H" $PASSWD \
73 --field="$(_n 'Autosave password'):CHK" $CHECKED
74 }
76 su_error() {
77 icon=gtk-dialog-error
78 yad --title="$(_n 'Error')" --window-icon=$icon \
79 --width=320 --on-top --center \
80 --image=$icon --image-on-top \
81 --text="\n<b>$(_n 'Error: wrong password!')</b>\n" \
82 --button="gtk-close:1"
83 }
85 # user may press cancel on download.
86 cancel_dl() {
87 if [ "$?" == 1 ]; then
88 _ "CANCEL"
89 rm -f $DOWNLOADS/$(basename $url)
90 fi
91 }
93 # output a command in a GTK window
94 output_command() {
95 : ${title=$(_n 'TazBox Output')}
96 : ${icon=gtk-dialog-info}
98 yad --title="$title" --window-icon=$icon \
99 --geometry="600x220+0-24" --fore="#ffffff" --back="#000000" \
100 --text-info --fontname=monospace --wrap $opts \
101 --button="gtk-close:1"
102 }
104 # logout GUI function
105 logout_main() {
106 icon=/usr/share/pixmaps/slitaz-icon.png
107 yad --title="$(_n 'SliTaz Logout')" --window-icon=$icon \
108 --on-top --center --height=130 \
109 --image=$icon --image-on-top \
110 --text="<b>$(_n 'SliTaz Logout - Please choose an action:')</b>" \
111 --always-print-result \
112 --button "$(_n 'Close X session')!system-log-out:4" \
113 --button "$(_n 'Reboot system')!reload:3" \
114 --button "$(_n 'Shutdown system')!system-shutdown:2"
115 }
117 # generate keymap list
118 gen_kmap_list() {
119 echo > $tmp
120 cd /usr/share/kbd/keymaps/i386
121 # we first need a list to sort and then use \n for Yad list.
122 for i in $(find -type f | sed '/include/d; s|./||'); do
123 echo "$(basename $i .map.gz)|$(dirname $i)" >> $tmp
124 done
125 }
127 # Initial Config functions
128 setup_main() {
129 icon=locale
130 gen_kmap_list
131 locale=$(ls -1 $db | grep ^[a-z][a-z]_[A-Z][A-Z] | tr "\n" "!")
132 keymap=$(cat $tmp | sort | tr "\n" "!")
133 timezone=$(find $zi -type f | sed s,$zi,,g | tr "\n" "!")
134 yad --title="$(_n 'SliTaz Initial Setup')" --window-icon=$icon \
135 --width=500 \
136 --image=$icon --image-on-top \
137 --text="<big>$(_n 'Here you can set your preferences \n for <b>locale, keymap and timezone</b>.')</big>" \
138 --form \
139 --field "$(_n 'Locale'):CB" $locale \
140 --field "$(_n 'Keymap'):CB" $keymap \
141 --field "$(_n 'Timezone'):CB" $timezone
142 }
144 setup() {
145 choices=$(setup_main)
146 locale=$(echo $choices | cut -d"|" -f1)
147 keymap=$(echo $choices | cut -d"|" -f2)
148 timezone=$(echo $choices | cut -d"|" -f3)
149 [ $locale ] && tazlocale init $locale
150 [ $keymap ] && tazkeymap init $keymap
151 [ $timezone ] && echo $timezone > /etc/TZ
152 }
154 # Locale functions
155 locale_main() {
156 icon=preferences-desktop-locale
157 for locale in $(ls -1 $db | grep '[a-z]_[A-Z]'); do
158 desc=$(fgrep -m1 title $db/$locale | cut -d'"' -f2)
159 echo -e "$locale \n $desc"
160 done | \
161 yad --title="$(_n 'SliTaz locale')" --window-icon=$icon \
162 --width=600 --height=380 --sticky --on-top --center \
163 --image=$icon --image-on-top \
164 --text="<b>$(_n 'Language configuration')</b>" \
165 --list --column $(_n 'Name') --column $(_n 'Description') \
166 --print-column=1 --separator=''
167 }
169 locale() {
170 locale=$(locale_main)
171 # Deal with --button values
172 case $? in
173 1) exit 0 ;;
174 *) continue ;;
175 esac
176 # System language configuration.
177 [ "$locale" ] && tazlocale $locale
178 }
180 # Keymap functions
181 keymap_main() {
182 #icon=preferences-desktop-keyboard
183 icon=input-keyboard
184 gen_kmap_list
185 for i in $(sort $tmp); do
186 echo "$i" | tr '|' '\n'
187 done | \
188 yad --title="$(_ 'SliTaz keymap')" --window-icon=$icon \
189 --width=500 --height=380 --sticky --on-top --center \
190 --image=$icon --image-on-top \
191 --text="<b>$(_n 'Keyboard configuration')</b>" \
192 --list --column $(_n 'Keymap') --column $(_n 'Type') \
193 --print-column=1 --separator=''
194 rm -f $tmp
195 }
197 keymap() {
198 keymap=$(keymap_main)
199 # Deal with --button values
200 [ x$? == x1 ] && exit 0
201 # System keymap configuration
202 [ "$keymap" ] && tazkeymap $keymap
203 }
205 # Free GeoIP service
206 # Response: "IP","CountryCode","CountryName", ...
208 geoip() {
209 [ ! -e $csv ] && wget -q -T3 -O $csv http://freegeoip.net/csv/ 2&>/dev/null
210 [ -e $csv ] && cut -d'"' -f4 $csv
211 }
213 # TZ functions
214 tz_suggest() {
215 CountryCode=$(geoip)
216 if [ x$CountryCode != x ]; then
217 CountryName=$(cut -d'"' -f6 $csv)
218 yad --title="$(_ 'SliTaz TZ')" --window-icon=$icon \
219 --on-top --center \
220 --image=$CountryCode --image-on-top \
221 --text="$(_ 'Suggested location:') <b>$CountryName</b>\n
222 $(_ 'Are you agreed?')" \
223 --button="gtk-yes:0" --button="gtk-no:1" --always-print-result
224 [ $? == 0 ] && echo $CountryCode
225 fi
226 }
228 tz_select() {
229 case x$1 in
230 x)
231 # first pass - country
232 tmpcc=$(mktemp)
234 for tzfile in $(find $zi -type f -regex '.*info/[ABCEIMP].*'); do
235 grep -m1 $(basename $tzfile) $ztab
236 done | cut -d' ' -f1 | sort -u > $tmpcc
238 for CC in $(cat $tmpcc); do
239 cat << EOT
240 $CC
241 $CC
242 $(gettext iso_3166 "$(grep -m1 "$CC " ${zi}iso3166-1.tab | cut -d' ' -f2)")
243 EOT
244 done | \
245 yad --title="$(_ 'SliTaz TZ')" --window-icon=$icon \
246 --width=500 --height=380 --on-top --center \
247 --image=$icon --image-on-top \
248 --text="<b>$(_ 'TimeZone Configuration')</b>\n$(_ 'Select country')" \
249 --list \
250 --column="$(_n 'Flag'):IMG" --column=$(_n 'Code') --column=$(_n 'Country') \
251 --button="gtk-index:2" --button="gtk-go-forward:0" \
252 --button="gtk-cancel:1" --always-print-result \
253 --print-column=2 --separator=''
254 ;;
255 xindex)
256 # manual selection of file with timezone info
257 yad --title="$(_ 'SliTaz TZ')" --window-icon=$icon \
258 --width=500 --on-top --center \
259 --image=$icon --image-on-top \
260 --text="<b>$(_ 'TimeZone Configuration')</b>\n$(_ 'Select time zone')" \
261 --form --field=":FL" ${zi}UTC --separator='' | \
262 sed "s|$zi||"
263 ;;
264 *)
265 # second pass - city/place
266 list=$(cat $ztab | grep "^$1 " | cut -d' ' -f2 | tr ' ' "\n" | sort)
267 icon=$1
268 if [ $(echo "$list" | wc -l) != 1 ]; then
269 echo "$list" | \
270 yad --title="$(_ 'SliTaz TZ')" --window-icon=$icon \
271 --width=500 --height=380 --on-top --center \
272 --image=$icon --image-on-top \
273 --text="<b>$(_ 'TimeZone Configuration')</b>\n$(_ 'Select location')" \
274 --list --column $(_n 'Location/City') --separator=''
275 else
276 echo $list
277 fi
278 ;;
279 esac
280 }
282 tz() {
283 icon=config-date
284 arg=$(tz_suggest)
286 timezone=$(tz_select $arg)
287 case $? in
288 1) exit 0 ;;
289 0) [ x$timezone == x ] && exit 0
290 [ x$arg == x ] && timezone=$(tz_select "$timezone")
291 timezone=$(find $zi -name $timezone -regex '.*info/[ABCEIMP].*' | \
292 sed "s|$zi||") ;;
293 2) timezone=$(tz_select "index") ;;
294 esac
296 [ x$timezone == x ] && exit 0
297 echo $timezone > /etc/TZ
298 export TZ=$timezone
299 tazbox notify "$(_ 'TimeZone was set to $timezone')" config-date 3
300 rm -f $tmpcc
301 }
303 # New file functions
304 newfile_main() {
305 icon=gtk-new
306 yad --title="$(_n 'New file')" --window-icon=$icon \
307 --width=460 --height=160 --on-top --center \
308 --image=$icon --image-on-top \
309 --text="<b>$(_n 'Create a new file or folder on your desktop')</b>" \
310 --entry --entry-label="$(_n 'File name')" \
311 --ricon=editclear \
312 --always-print-result \
313 --button="$(_n 'SHell script')!application-x-shellscript:4" \
314 --button="$(_n 'Folder')!stock_folder:3" \
315 --icon="gtk-new" \
316 --button="$(_n 'File')!application-text:2" \
317 --button="gtk-cancel:1"
318 }
320 newfile() {
321 file=$(newfile_main)
322 ret=$?
323 [ "$file" ] || exit 0
324 case $ret in
325 4)
326 cat > $HOME/Desktop/$file << EOT
327 #!/bin/sh
328 #
330 EOT
331 chmod +x $HOME/Desktop/$file ;;
332 3) mkdir -p $HOME/Desktop/$file ;;
333 2) touch $HOME/Desktop/$file ;;
334 1) exit 0 ;;
335 esac
336 }
338 # All applications
339 all_apps() {
340 icon=stock_bookmark
341 yad --title="$(_n 'All Applications')" --window-icon=$icon \
342 --width=400 --height=400 \
343 --icons --compact \
344 --read-dir=/usr/share/applications \
345 --button="gtk-close:0"
346 }
348 # Generate alternatives list: first - current choice, following - existing apps
349 gen_alt() {
350 alts="$@"
351 current=$(echo $alts | cut -d' ' -f1)
352 alts=$(echo $alts | sed "s|$current||g")
353 for alt in $alts; do
354 [ ! $(busybox which $alt) ] && alts=$(echo $alts | sed "s|$alt||")
355 done
356 echo $current $alts | tr ' ' '!'
357 }
359 # Ask root permissions for system settings
360 ask_root() {
361 if [ $(id -u) != 0 ]; then
362 exec tazbox su $0 $@
363 exit 0
364 fi
365 }
367 #
368 # Commands
369 #
370 case "$1" in
371 su)
372 # Don't show dialog if we are root
373 test $(id -u) = 0 && exec ${@:2}
374 SU_CMD="${@:2}"
375 # Check if a password has been saved before launching main dialog
376 if [ -s $HOME/.config/slitaz/subox.conf ]; then
377 PASSWD=$(cat $HOME/.config/slitaz/subox.conf)
378 CHECKED="TRUE"
379 fi
380 # Display the main dialog (ask for password)
381 main=$(su_main $SU_CMD)
382 # Deal with --button values and exit if cancelled to avoid erasing
383 # saved password.
384 [ $? == 1 ] && exit 0
385 # Save or erase Autosaved password
386 if [ $(echo $main | cut -f2 -d"|") == "TRUE" ]; then
387 echo $main | cut -f 1 -d "|" > $HOME/.config/slitaz/subox.conf
388 chmod 0600 $HOME/.config/slitaz/subox.conf
389 else
390 cat /dev/null > $HOME/.config/slitaz/subox.conf
391 fi
392 # Try to login & execute. If password is wrong execute error dialog
393 SU_CMD_QUOTED="$(echo "$SU_CMD" | sed 's|&|\\&|g')"
394 echo $main | cut -f 1 -d "|" | su -c "$SU_CMD_QUOTED &" || su_error ;;
395 logout)
396 # Logout window with actions
397 if [ "$oldstyle" ]; then
398 answer=$(logout_oldstyle)
399 else
400 logout_main; answer=$?
401 fi
402 # Deal with --button values
403 # DE and WM started with a custom -session script should export
404 # XDG_CURRENT_DESKTOP
405 case $answer in
406 4|*exit)
407 case $XDG_CURRENT_DESKTOP in
408 LXDE)
409 [ "$_LXSESSION_PID" ] && kill $_LXSESSION_PID
410 [ "$DESKTOP_SESSION" == "compiz" ] && killall compiz
411 openbox --exit ;;
412 openbox) openbox --exit ;;
413 compiz) killall compiz ;;
414 *)
415 # Try to kill other WM that dont set XDG var.
416 jwm -exit 2>/dev/null ;;
417 esac ;;
418 3|*reboot)
419 reboot || reboot -f ;;
420 2|*halt)
421 poweroff ;;
422 esac ;;
423 out)
424 # Pipe a command into a GTK window
425 sed 's|\[.m||g; s|\[[0-9][0-9]*G| |g' | output_command ;;
426 out-dl)
427 # A tiny GTK window for Busybox wget output
428 url=$2; opts="--tail --button=gtk-cancel:1"
429 icon=folder-downloads; title="$(_ 'Downloading...')"
430 [ -d $DOWNLOADS ] || mkdir -p $DOWNLOADS
431 busybox wget -c -P $DOWNLOADS $url 2>&1 | output_command
432 cancel_dl ;;
433 locale)
434 ask_root $@ && locale ;;
435 keymap)
436 ask_root $@ && keymap ;;
437 tz)
438 ask_root $@ && tz ;;
439 setup)
440 ask_root $@ && setup ;;
441 new-file)
442 newfile ;;
443 all-apps)
444 all_apps ;;
445 notify|-n)
446 # On screen notification box.
447 icon="$3"
448 time="$4"
449 [ "$icon" ] || icon=info
450 [ "$time" ] || time=4
451 yad --width=520 --height=80 --timeout=$time --timeout-indicator=right \
452 --on-top --center --no-buttons --borders=12 --undecorated \
453 --skip-taskbar --image=$icon --image-on-top --text="<b>$2</b>" ;;
454 tazapps)
455 # Default applications configuration script. System wide config file
456 # is /etc/slitaz/applications.conf and each user can have personal
457 # settings. System wide for root and personal config for user.
458 export CONFIG="$HOME/.config/slitaz/applications.conf"
459 if [ ! -f $CONFIG ]; then
460 mkdir -p $HOME/.config/slitaz
461 cp /etc/slitaz/applications.conf $CONFIG
462 fi
463 . $CONFIG
465 FILE_MANAGERS=$(gen_alt $FILE_MANAGER \
466 clex emelfm2 mc pathfinder pcmanfm ranger spacefm thunar xfe ytree)
467 BROWSERS=$(gen_alt $BROWSER \
468 arora chrome cream dillo elinks firefox iron links lynx midori \
469 netsurf opera retawq seamonkey surf tazweb)
470 EDITORS=$(gen_alt $EDITOR \
471 adie beaver emacs geany jed joe le leafpad mousepad mp-5 nano \
472 SciTE vi vim xedit xfw zile)
473 TERMINALS=$(gen_alt $TERMINAL \
474 aterm evilvte lxterminal mrxvt sakura stjerm tilda urxvt vte xterm \
475 yeahconsole)
476 WINDOW_MANAGERS=$(gen_alt $WINDOW_MANAGER \
477 awesome blackbox dwm echinus enlightenment fluxbox icewm jwm \
478 karmen matchbox openbox-session pekwm ratpoison xfwm4)
480 icon=preferences-desktop-default-applications
481 eval $(yad --title="$(_n 'SliTaz default applications')" \
482 --window-icon=$icon --image=$icon --image-on-top \
483 --text="<b>$(_n 'SliTaz default applications configuration')</b>" \
484 --form \
485 --field="$(_n 'File manager:'):CBE" "$FILE_MANAGERS" \
486 --field="$(_n 'Web browser:'):CBE" "$BROWSERS" \
487 --field="$(_n 'Text editor:'):CBE" "$EDITORS" \
488 --field="$(_n 'Terminal:'):CBE" "$TERMINALS" \
489 --field="$(_n 'Window manager:'):CBE" "$WINDOW_MANAGERS" | \
490 awk -F'|' '{printf "FILE_MANAGER=\"%s\"\nBROWSER=\"%s\"\nEDITOR=\"%s\"\
491 TERMINAL=\"%s\"\nWINDOW_MANAGER=\"%s\"\n", $1, $2, $3, $4, $5}')
493 sed '/FILE_MANAGER=/s|"\([^"]*\)"|"'$FILE_MANAGER'"|; \
494 /BROWSER=/s|"\([^"]*\)"|"'$BROWSER'"|; \
495 /EDITOR=/s|"\([^"]*\)"|"'$EDITOR'"|; \
496 /TERMINAL=/s|"\([^"]*\)"|"'$TERMINAL'"|; \
497 /WINDOW_MANAGER=/s|"\([^"]*\)"|"'$WINDOW_MANAGER'"|' \
498 -i $CONFIG ;;
499 gpl)
500 yad --title="GNU General Public License" --window-icon=text-x-generic \
501 --geometry="650x500" \
502 --image=/usr/share/pixmaps/gpl3.png --image-on-top \
503 --center \
504 --text-info \
505 --fontname=monospace \
506 --button "OK" < /usr/share/licenses/gpl.txt ;;
507 *)
508 usage ;;
509 esac
511 exit 0