slitaz-tools view tazbox/tazbox @ rev 813

Current state, features stabilized and open for bugfixes and translations.
author Aleksej Bobylev <al.bobylev@gmail.com>
date Mon Sep 09 03:27:04 2013 +0300 (2013-09-09)
parents 8e208c31ad8e
children c5e3fa73e5af
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-2013 SliTaz GNU/Linux - GNU GPL v3
7 # - Christophe Lincoln <pankso@slitaz.org>
8 #
9 . /lib/libtaz.sh
10 export TEXTDOMAIN='tazbox' # i18n
12 # download dir (may be in a config file)
13 DOWNLOADS=$HOME/Downloads
15 # icons for most windows
16 #icon=/usr/share/pixmaps/slitaz-icon.png
18 # some constants to be used inside functions
19 tmp="/tmp/keymap.list"
20 db="/usr/share/i18n/locales"
21 zi="/usr/share/zoneinfo/"
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 | 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 GUI's
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=system-log-out
107 yad --title="$(_n 'SliTaz Logout')" --window-icon=$icon \
108 --on-top --center \
109 --image=$icon --image-on-top \
110 --text="$(_n 'Please choose an action:')" \
111 --always-print-result \
112 --button "$(_n 'Close X session'):4" \
113 --button "$(_n 'Reboot system'):3" \
114 --button "$(_n 'Shutdown system'):2"
115 }
116 logout_oldstyle() {
117 icon=/usr/share/pixmaps/slitaz-icon.png
118 yad --title="$(_n 'SliTaz Logout')" --window-icon=$icon \
119 --on-top --center --width=440 --height=140 \
120 --image=$icon --image-on-top \
121 --text="<b>$(_n 'SliTaz Logout').</b> $(_n 'Please choose an action:')" \
122 --entry --entry-text \
123 "$(_n 'Close X session'): exit" \
124 "$(_n 'Reboot system'): reboot" \
125 "$(_n 'Shutdown system'): halt"
126 }
128 # generate keymap list
129 gen_kmap_list() {
130 echo > $tmp
131 cd /usr/share/kbd/keymaps/i386
132 # we first need a list to sort and then use \n for Yad list.
133 for i in $(find -type f | sed '/include/d; s|./||'); do
134 echo "$(basename $i .map.gz)|$(dirname $i)" >> $tmp
135 done
136 }
138 # Initial Config functions
139 setup_main() {
140 icon=locale
141 gen_kmap_list
142 locale=$(ls -1 $db | grep ^[a-z][a-z]_[A-Z][A-Z] | tr "\n" "!")
143 keymap=$(cat $tmp | sort | tr "\n" "!")
144 timezone=$(find $zi -type f | sed s,$zi,,g | tr "\n" "!")
145 yad --title="$(_n 'SliTaz Initial Setup')" --window-icon=$icon \
146 --width=500 \
147 --image=$icon --image-on-top \
148 --text="<big>$(_n 'Here you can set your preferences \n for <b>locale, keymap and timezone</b>.')</big>" \
149 --form \
150 --field "$(_n 'Locale'):CB" $locale \
151 --field "$(_n 'Keymap'):CB" $keymap \
152 --field "$(_n 'Timezone'):CB" $timezone
153 }
155 setup() {
156 choices=$(setup_main)
157 locale=$(echo $choices | cut -d"|" -f1)
158 keymap=$(echo $choices | cut -d"|" -f2)
159 timezone=$(echo $choices | cut -d"|" -f3)
160 [ $locale ] && tazlocale init $locale
161 [ $keymap ] && tazkeymap init $keymap
162 [ $timezone ] && echo $timezone > /etc/TZ
163 }
165 # Locale functions
166 locale_main() {
167 icon=preferences-desktop-locale
168 for locale in $(ls -1 $db | grep '[a-z]_[A-Z]'); do
169 desc=$(fgrep -m1 title $db/$locale | cut -d'"' -f2)
170 echo -e "$locale \n $desc"
171 done | \
172 yad --title="$(_n 'SliTaz locale')" --window-icon=$icon \
173 --width=600 --height=380 --sticky --on-top --center \
174 --image=$icon --image-on-top \
175 --text="<b>$(_n 'Language configuration')</b>" \
176 --list --column $(_n 'Name') --column $(_n 'Description') \
177 --print-column=1 --separator=''
178 }
180 locale() {
181 locale=$(locale_main)
182 # Deal with --button values
183 case $? in
184 1) exit 0 ;;
185 *) continue ;;
186 esac
187 # System language configuration.
188 [ "$locale" ] && tazlocale $locale
189 }
191 # Keymap functions
192 keymap_main() {
193 #icon=preferences-desktop-keyboard
194 icon=input-keyboard
195 gen_kmap_list
196 for i in $(sort $tmp); do
197 echo "$i" | tr '|' '\n'
198 done | \
199 yad --title="$(_ 'SliTaz keymap')" --window-icon=$icon \
200 --width=500 --height=380 --sticky --on-top --center \
201 --image=$icon --image-on-top \
202 --text="<b>$(_n 'Keyboard configuration')</b>" \
203 --list --column $(_n 'Keymap') --column $(_n 'Type') \
204 --print-column=1 --separator=''
205 rm -f $tmp
206 }
208 keymap() {
209 keymap=$(keymap_main)
210 # Deal with --button values
211 [ x$? == x1 ] && exit 0
212 # System keymap configuration
213 [ "$keymap" ] && tazkeymap $keymap
214 }
216 # TZ functions
217 tz_main() {
218 icon=config-date
219 for t in $(find $zi -type f | sed s,$zi,,g); do
220 echo -e "$t"
221 done | \
222 yad --title="$(_ 'SliTaz TZ')" --window-icon=$icon \
223 --width=500 --height=380 --sticky --on-top --center \
224 --image=$icon --image-on-top \
225 --text="<b>$(_n 'TimeZone Configuration')</b>" \
226 --list \
227 --column $(_n 'Name')
228 }
230 tz_select() {
231 icon=config-date
232 ZONEINFO=/usr/share/zoneinfo
233 exit_code=0
235 case x$1 in
236 x)
237 # first pass - country
238 for CC in $(sed '/^#/d; s|^\([^ ]*\).*|\1|' $ZONEINFO/zone.tab | sort -u); do
239 cat << EOT
240 $CC
241 $CC
242 $(gettext iso_3166 "$(sed -n 's|^'$CC' \(.*\)$|\1|p' $ZONEINFO/iso3166-1.tab)")
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" $ZONEINFO/UTC --separator='' | \
262 sed "s|$ZONEINFO/||"
263 ;;
264 *)
265 # second pass - city/place
266 list=$(cat $ZONEINFO/zone.tab | grep "^$1 " | cut -f3)
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 timezone=$(tz_select $1)
284 case $? in
285 1) exit 0 ;;
286 0) [ x$timezone == x ] && exit 0
287 timezone=$(tz_select "$timezone") ;;
288 2) timezone=$(tz_select "index") ;;
289 esac
290 [ x$timezone == x ] && exit 0
291 echo $timezone > /etc/TZ
292 export TZ=$timezone
293 }
295 # New file functions
296 newfile_main() {
297 icon=gtk-new
298 yad --title="$(_n 'New file')" --window-icon=$icon \
299 --width=460 --height=160 --on-top --center \
300 --image=$icon --image-on-top \
301 --text="<b>$(_n 'Create a new file or folder on your desktop')</b>" \
302 --entry --entry-label="$(_n 'File name')" \
303 --ricon=editclear \
304 --always-print-result \
305 --button="$(_n 'SHell script'):4" \
306 --button="$(_n 'Folder'):3" \
307 --icon="gtk-new" \
308 --button="$(_n 'File'):2" \
309 --button="gtk-cancel:1"
310 }
312 newfile() {
313 file=$(newfile_main)
314 ret=$?
315 [ "$file" ] || exit 0
316 case $ret in
317 4)
318 cat > $HOME/Desktop/$file << EOT
319 #!/bin/sh
320 #
322 EOT
323 chmod +x $HOME/Desktop/$file ;;
324 3) mkdir -p $HOME/Desktop/$file ;;
325 2) touch $HOME/Desktop/$file ;;
326 1) exit 0 ;;
327 esac
328 }
330 # All applications
331 all_apps() {
332 icon=stock_bookmark
333 yad --title="$(_n 'All Applications')" --window-icon=$icon \
334 --width=400 --height=400 \
335 --icons --compact \
336 --read-dir=/usr/share/applications \
337 --button="gtk-close:0"
338 }
340 # Generate alternatives list: first - current choice, following - existing apps
341 gen_alt() {
342 alts="$@"
343 current=$(echo $alts | cut -d' ' -f1)
344 alts=$(echo $alts | sed "s|$current||g")
345 for alt in $alts; do
346 [ ! $(busybox which $alt) ] && alts=$(echo $alts | sed "s|$alt||")
347 done
348 echo $current $alts | tr ' ' '!'
349 }
351 # Ask root permissions for system settings
352 ask_root() {
353 if [ $(id -u) != 0 ]; then
354 exec tazbox su $0 $@
355 exit 0
356 fi
357 }
359 #
360 # Commands
361 #
362 case "$1" in
363 su)
364 # Don't show dialog if we are root
365 test $(id -u) = 0 && exec ${@:2}
366 SU_CMD="${@:2}"
367 # Check if a password has been saved before launching main dialog
368 if [ -s $HOME/.config/slitaz/subox.conf ]; then
369 PASSWD=$(cat $HOME/.config/slitaz/subox.conf)
370 CHECKED="TRUE"
371 fi
372 # Display the main dialog (ask for password)
373 main=$(su_main $SU_CMD)
374 # Deal with --button values and exit if cancelled to avoid erasing
375 # saved password.
376 [ $? == 1 ] && exit 0
377 # Save or erase Autosaved password
378 if [ $(echo $main | cut -f2 -d"|") == "TRUE" ]; then
379 echo $main | cut -f 1 -d "|" > $HOME/.config/slitaz/subox.conf
380 chmod 0600 $HOME/.config/slitaz/subox.conf
381 else
382 cat /dev/null > $HOME/.config/slitaz/subox.conf
383 fi
384 # Try to login & execute. If password is wrong execute error dialog
385 SU_CMD_QUOTED="$(echo "$SU_CMD" | sed 's|&|\\&|g')"
386 echo "SU_CMD«$SU_CMD» SU_CMD_QUOTED«$SU_CMD_QUOTED»"
387 echo $main | cut -f 1 -d "|" | su -c "$SU_CMD_QUOTED &" || su_error ;;
388 logout)
389 # Logout window with actions
390 if [ "$oldstyle" ]; then
391 answer=$(logout_oldstyle)
392 else
393 logout_main; answer=$?
394 fi
395 # Deal with --button values
396 # DE and WM started with a custom -session script should export
397 # XDG_CURRENT_DESKTOP
398 case $answer in
399 4|*exit)
400 case $XDG_CURRENT_DESKTOP in
401 LXDE)
402 [ "$_LXSESSION_PID" ] && kill $_LXSESSION_PID
403 [ "$DESKTOP_SESSION" == "compiz" ] && killall compiz
404 openbox --exit ;;
405 openbox) openbox --exit ;;
406 compiz) killall compiz ;;
407 *)
408 # Try to kill other WM that dont set XDG var.
409 jwm -exit 2>/dev/null ;;
410 esac ;;
411 3|*reboot)
412 reboot || reboot -f ;;
413 2|*halt)
414 poweroff ;;
415 esac ;;
416 out)
417 # Pipe a command into a GTK window
418 sed 's|\[.m||g; s|\[[0-9][0-9]*G| |g' | output_command ;;
419 out-dl)
420 # A tiny GTK window for Busybox wget output
421 url=$2; opts="--tail --button=gtk-cancel:1"
422 icon=folder-downloads; title="$(_ 'Downloading...')"
423 [ -d $DOWNLOADS ] || mkdir -p $DOWNLOADS
424 busybox wget -c -P $DOWNLOADS $url 2>&1 | output_command
425 cancel_dl ;;
426 locale)
427 ask_root $@ && locale ;;
428 keymap)
429 ask_root $@ && keymap ;;
430 tz)
431 ask_root $@ && tz $2;;
432 setup)
433 ask_root $@ && setup ;;
434 new-file)
435 newfile ;;
436 all-apps)
437 all_apps ;;
438 notify|-n)
439 # On screen notification box.
440 icon="$3"
441 time="$4"
442 [ "$icon" ] || icon=info
443 [ "$time" ] || time=4
444 yad --width=520 --height=80 --timeout=$time --timeout-indicator=right \
445 --on-top --center --no-buttons --borders=12 --undecorated \
446 --skip-taskbar --image=$icon --image-on-top --text="<b>$2</b>" ;;
447 tazapps)
448 # Default applications configuration script. System wide config file
449 # is /etc/slitaz/applications.conf and each user can have personal
450 # settings. System wide for root and personal config for user.
451 export CONFIG="$HOME/.config/slitaz/applications.conf"
452 if [ ! -f $CONFIG ]; then
453 mkdir -p $HOME/.config/slitaz
454 cp /etc/slitaz/applications.conf $CONFIG
455 fi
456 . $CONFIG
458 FILE_MANAGERS=$(gen_alt $FILE_MANAGER \
459 clex emelfm2 mc pathfinder pcmanfm ranger spacefm thunar xfe ytree)
460 BROWSERS=$(gen_alt $BROWSER \
461 arora chrome cream dillo elinks firefox iron links lynx midori \
462 netsurf opera retawq seamonkey surf tazweb)
463 EDITORS=$(gen_alt $EDITOR \
464 adie beaver emacs geany jed joe le leafpad mousepad mp-5 nano \
465 SciTE vi vim xedit xfw zile)
466 TERMINALS=$(gen_alt $TERMINAL \
467 aterm evilvte lxterminal mrxvt sakura stjerm tilda urxvt vte xterm \
468 yeahconsole)
469 WINDOW_MANAGERS=$(gen_alt $WINDOW_MANAGER \
470 awesome blackbox dwm echinus enlightenment fluxbox icewm jwm \
471 karmen matchbox openbox-session pekwm ratpoison xfwm4)
473 icon=preferences-desktop-default-applications
474 eval $(yad --title="$(_n 'SliTaz default applications')" \
475 --window-icon=$icon --image=$icon --image-on-top \
476 --text="<b>$(_n 'SliTaz default applications configuration')</b>" \
477 --form \
478 --field="$(_n 'File manager:'):CBE" "$FILE_MANAGERS" \
479 --field="$(_n 'Web browser:'):CBE" "$BROWSERS" \
480 --field="$(_n 'Text editor:'):CBE" "$EDITORS" \
481 --field="$(_n 'Terminal:'):CBE" "$TERMINALS" \
482 --field="$(_n 'Window manager:'):CBE" "$WINDOW_MANAGERS" | \
483 awk -F'|' '{printf "FILE_MANAGER=\"%s\"\nBROWSER=\"%s\"\nEDITOR=\"%s\"\
484 TERMINAL=\"%s\"\nWINDOW_MANAGER=\"%s\"\n", $1, $2, $3, $4, $5}')
486 sed '/FILE_MANAGER=/s|"\([^"]*\)"|"'$FILE_MANAGER'"|; \
487 /BROWSER=/s|"\([^"]*\)"|"'$BROWSER'"|; \
488 /EDITOR=/s|"\([^"]*\)"|"'$EDITOR'"|; \
489 /TERMINAL=/s|"\([^"]*\)"|"'$TERMINAL'"|; \
490 /WINDOW_MANAGER=/s|"\([^"]*\)"|"'$WINDOW_MANAGER'"|' \
491 -i $CONFIG ;;
492 gpl)
493 yad --title="GNU General Public License" --window-icon=text-x-generic \
494 --geometry="650x500" \
495 --image=/usr/share/pixmaps/gpl3.png --image-on-top \
496 --center \
497 --text-info \
498 --fontname=monospace \
499 --button "OK" < /usr/share/licenses/gpl.txt ;;
500 *)
501 usage ;;
502 esac
504 exit 0