slitaz-tools view tazbox/tazbox @ rev 821

2014
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat Jan 04 14:32:42 2014 +0000 (2014-01-04)
parents c5e3fa73e5af
children b81a9d02e832
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 #
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="<b>$(_n 'Please choose an action:')</b>" \
111 --always-print-result \
112 --button "$(_n 'Close X session'):4" \
113 --button "$(_n 'Reboot system'):3" \
114 --button "$(_n 'Shutdown system'):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 # TZ functions
206 tz_main() {
207 icon=config-date
208 for t in $(find $zi -type f | sed s,$zi,,g); do
209 echo -e "$t"
210 done | \
211 yad --title="$(_ 'SliTaz TZ')" --window-icon=$icon \
212 --width=500 --height=380 --sticky --on-top --center \
213 --image=$icon --image-on-top \
214 --text="<b>$(_n 'TimeZone Configuration')</b>" \
215 --list \
216 --column $(_n 'Name')
217 }
219 tz_select() {
220 icon=config-date
221 ZONEINFO=/usr/share/zoneinfo
222 exit_code=0
224 case x$1 in
225 x)
226 # first pass - country
227 for CC in $(sed '/^#/d; s|^\([^ ]*\).*|\1|' $ZONEINFO/zone.tab | sort -u); do
228 cat << EOT
229 $CC
230 $CC
231 $(gettext iso_3166 "$(sed -n 's|^'$CC' \(.*\)$|\1|p' $ZONEINFO/iso3166-1.tab)")
232 EOT
233 done | \
234 yad --title="$(_ 'SliTaz TZ')" --window-icon=$icon \
235 --width=500 --height=380 --on-top --center \
236 --image=$icon --image-on-top \
237 --text="<b>$(_ 'TimeZone Configuration')</b>\n$(_ 'Select country')" \
238 --list \
239 --column="$(_n 'Flag'):IMG" --column=$(_n 'Code') --column=$(_n 'Country') \
240 --button="gtk-index:2" --button="gtk-go-forward:0" \
241 --button="gtk-cancel:1" --always-print-result \
242 --print-column=2 --separator=''
243 ;;
244 xindex)
245 # manual selection of file with timezone info
246 yad --title="$(_ 'SliTaz TZ')" --window-icon=$icon \
247 --width=500 --on-top --center \
248 --image=$icon --image-on-top \
249 --text="<b>$(_ 'TimeZone Configuration')</b>\n$(_ 'Select time zone')" \
250 --form --field=":FL" $ZONEINFO/UTC --separator='' | \
251 sed "s|$ZONEINFO/||"
252 ;;
253 *)
254 # second pass - city/place
255 list=$(cat $ZONEINFO/zone.tab | grep "^$1 " | cut -f3)
256 icon=$1
257 if [ $(echo "$list" | wc -l) != 1 ]; then
258 echo "$list" | \
259 yad --title="$(_ 'SliTaz TZ')" --window-icon=$icon \
260 --width=500 --height=380 --on-top --center \
261 --image=$icon --image-on-top \
262 --text="<b>$(_ 'TimeZone Configuration')</b>\n$(_ 'Select location')" \
263 --list --column $(_n 'Location/City') --separator=''
264 else
265 echo $list
266 fi
267 ;;
268 esac
269 }
271 tz() {
272 timezone=$(tz_select $1)
273 case $? in
274 1) exit 0 ;;
275 0) [ x$timezone == x ] && exit 0
276 timezone=$(tz_select "$timezone") ;;
277 2) timezone=$(tz_select "index") ;;
278 esac
279 [ x$timezone == x ] && exit 0
280 echo $timezone > /etc/TZ
281 export TZ=$timezone
282 }
284 # New file functions
285 newfile_main() {
286 icon=gtk-new
287 yad --title="$(_n 'New file')" --window-icon=$icon \
288 --width=460 --height=160 --on-top --center \
289 --image=$icon --image-on-top \
290 --text="<b>$(_n 'Create a new file or folder on your desktop')</b>" \
291 --entry --entry-label="$(_n 'File name')" \
292 --ricon=editclear \
293 --always-print-result \
294 --button="$(_n 'SHell script'):4" \
295 --button="$(_n 'Folder'):3" \
296 --icon="gtk-new" \
297 --button="$(_n 'File'):2" \
298 --button="gtk-cancel:1"
299 }
301 newfile() {
302 file=$(newfile_main)
303 ret=$?
304 [ "$file" ] || exit 0
305 case $ret in
306 4)
307 cat > $HOME/Desktop/$file << EOT
308 #!/bin/sh
309 #
311 EOT
312 chmod +x $HOME/Desktop/$file ;;
313 3) mkdir -p $HOME/Desktop/$file ;;
314 2) touch $HOME/Desktop/$file ;;
315 1) exit 0 ;;
316 esac
317 }
319 # All applications
320 all_apps() {
321 icon=stock_bookmark
322 yad --title="$(_n 'All Applications')" --window-icon=$icon \
323 --width=400 --height=400 \
324 --icons --compact \
325 --read-dir=/usr/share/applications \
326 --button="gtk-close:0"
327 }
329 # Generate alternatives list: first - current choice, following - existing apps
330 gen_alt() {
331 alts="$@"
332 current=$(echo $alts | cut -d' ' -f1)
333 alts=$(echo $alts | sed "s|$current||g")
334 for alt in $alts; do
335 [ ! $(busybox which $alt) ] && alts=$(echo $alts | sed "s|$alt||")
336 done
337 echo $current $alts | tr ' ' '!'
338 }
340 # Ask root permissions for system settings
341 ask_root() {
342 if [ $(id -u) != 0 ]; then
343 exec tazbox su $0 $@
344 exit 0
345 fi
346 }
348 #
349 # Commands
350 #
351 case "$1" in
352 su)
353 # Don't show dialog if we are root
354 test $(id -u) = 0 && exec ${@:2}
355 SU_CMD="${@:2}"
356 # Check if a password has been saved before launching main dialog
357 if [ -s $HOME/.config/slitaz/subox.conf ]; then
358 PASSWD=$(cat $HOME/.config/slitaz/subox.conf)
359 CHECKED="TRUE"
360 fi
361 # Display the main dialog (ask for password)
362 main=$(su_main $SU_CMD)
363 # Deal with --button values and exit if cancelled to avoid erasing
364 # saved password.
365 [ $? == 1 ] && exit 0
366 # Save or erase Autosaved password
367 if [ $(echo $main | cut -f2 -d"|") == "TRUE" ]; then
368 echo $main | cut -f 1 -d "|" > $HOME/.config/slitaz/subox.conf
369 chmod 0600 $HOME/.config/slitaz/subox.conf
370 else
371 cat /dev/null > $HOME/.config/slitaz/subox.conf
372 fi
373 # Try to login & execute. If password is wrong execute error dialog
374 SU_CMD_QUOTED="$(echo "$SU_CMD" | sed 's|&|\\&|g')"
375 echo "SU_CMD«$SU_CMD» SU_CMD_QUOTED«$SU_CMD_QUOTED»"
376 echo $main | cut -f 1 -d "|" | su -c "$SU_CMD_QUOTED &" || su_error ;;
377 logout)
378 # Logout window with actions
379 if [ "$oldstyle" ]; then
380 answer=$(logout_oldstyle)
381 else
382 logout_main; answer=$?
383 fi
384 # Deal with --button values
385 # DE and WM started with a custom -session script should export
386 # XDG_CURRENT_DESKTOP
387 case $answer in
388 4|*exit)
389 case $XDG_CURRENT_DESKTOP in
390 LXDE)
391 [ "$_LXSESSION_PID" ] && kill $_LXSESSION_PID
392 [ "$DESKTOP_SESSION" == "compiz" ] && killall compiz
393 openbox --exit ;;
394 openbox) openbox --exit ;;
395 compiz) killall compiz ;;
396 *)
397 # Try to kill other WM that dont set XDG var.
398 jwm -exit 2>/dev/null ;;
399 esac ;;
400 3|*reboot)
401 reboot || reboot -f ;;
402 2|*halt)
403 poweroff ;;
404 esac ;;
405 out)
406 # Pipe a command into a GTK window
407 sed 's|\[.m||g; s|\[[0-9][0-9]*G| |g' | output_command ;;
408 out-dl)
409 # A tiny GTK window for Busybox wget output
410 url=$2; opts="--tail --button=gtk-cancel:1"
411 icon=folder-downloads; title="$(_ 'Downloading...')"
412 [ -d $DOWNLOADS ] || mkdir -p $DOWNLOADS
413 busybox wget -c -P $DOWNLOADS $url 2>&1 | output_command
414 cancel_dl ;;
415 locale)
416 ask_root $@ && locale ;;
417 keymap)
418 ask_root $@ && keymap ;;
419 tz)
420 ask_root $@ && tz $2;;
421 setup)
422 ask_root $@ && setup ;;
423 new-file)
424 newfile ;;
425 all-apps)
426 all_apps ;;
427 notify|-n)
428 # On screen notification box.
429 icon="$3"
430 time="$4"
431 [ "$icon" ] || icon=info
432 [ "$time" ] || time=4
433 yad --width=520 --height=80 --timeout=$time --timeout-indicator=right \
434 --on-top --center --no-buttons --borders=12 --undecorated \
435 --skip-taskbar --image=$icon --image-on-top --text="<b>$2</b>" ;;
436 tazapps)
437 # Default applications configuration script. System wide config file
438 # is /etc/slitaz/applications.conf and each user can have personal
439 # settings. System wide for root and personal config for user.
440 export CONFIG="$HOME/.config/slitaz/applications.conf"
441 if [ ! -f $CONFIG ]; then
442 mkdir -p $HOME/.config/slitaz
443 cp /etc/slitaz/applications.conf $CONFIG
444 fi
445 . $CONFIG
447 FILE_MANAGERS=$(gen_alt $FILE_MANAGER \
448 clex emelfm2 mc pathfinder pcmanfm ranger spacefm thunar xfe ytree)
449 BROWSERS=$(gen_alt $BROWSER \
450 arora chrome cream dillo elinks firefox iron links lynx midori \
451 netsurf opera retawq seamonkey surf tazweb)
452 EDITORS=$(gen_alt $EDITOR \
453 adie beaver emacs geany jed joe le leafpad mousepad mp-5 nano \
454 SciTE vi vim xedit xfw zile)
455 TERMINALS=$(gen_alt $TERMINAL \
456 aterm evilvte lxterminal mrxvt sakura stjerm tilda urxvt vte xterm \
457 yeahconsole)
458 WINDOW_MANAGERS=$(gen_alt $WINDOW_MANAGER \
459 awesome blackbox dwm echinus enlightenment fluxbox icewm jwm \
460 karmen matchbox openbox-session pekwm ratpoison xfwm4)
462 icon=preferences-desktop-default-applications
463 eval $(yad --title="$(_n 'SliTaz default applications')" \
464 --window-icon=$icon --image=$icon --image-on-top \
465 --text="<b>$(_n 'SliTaz default applications configuration')</b>" \
466 --form \
467 --field="$(_n 'File manager:'):CBE" "$FILE_MANAGERS" \
468 --field="$(_n 'Web browser:'):CBE" "$BROWSERS" \
469 --field="$(_n 'Text editor:'):CBE" "$EDITORS" \
470 --field="$(_n 'Terminal:'):CBE" "$TERMINALS" \
471 --field="$(_n 'Window manager:'):CBE" "$WINDOW_MANAGERS" | \
472 awk -F'|' '{printf "FILE_MANAGER=\"%s\"\nBROWSER=\"%s\"\nEDITOR=\"%s\"\
473 TERMINAL=\"%s\"\nWINDOW_MANAGER=\"%s\"\n", $1, $2, $3, $4, $5}')
475 sed '/FILE_MANAGER=/s|"\([^"]*\)"|"'$FILE_MANAGER'"|; \
476 /BROWSER=/s|"\([^"]*\)"|"'$BROWSER'"|; \
477 /EDITOR=/s|"\([^"]*\)"|"'$EDITOR'"|; \
478 /TERMINAL=/s|"\([^"]*\)"|"'$TERMINAL'"|; \
479 /WINDOW_MANAGER=/s|"\([^"]*\)"|"'$WINDOW_MANAGER'"|' \
480 -i $CONFIG ;;
481 gpl)
482 yad --title="GNU General Public License" --window-icon=text-x-generic \
483 --geometry="650x500" \
484 --image=/usr/share/pixmaps/gpl3.png --image-on-top \
485 --center \
486 --text-info \
487 --fontname=monospace \
488 --button "OK" < /usr/share/licenses/gpl.txt ;;
489 *)
490 usage ;;
491 esac
493 exit 0