slitaz-tools view tazbox/tazbox @ rev 794

tazbox: verrify and ask root to set tz. Fix bug77
author border
date Tue Jan 08 11:09:03 2013 -0500 (2013-01-08)
parents 277277e6fb08
children f19a2346190f
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 # Note: $(gettext "text") doesn't work inside Yad so use `gettext \"text\"`
7 #
8 # Copyright (C) 2011 SliTaz GNU/linux - GNU gpl v3
9 # - Christophe Lincoln <pankso@slitaz.org>
10 #
12 # Download dir (may be in a config file)
13 DOWNLOADS=$HOME/Downloads
15 # Internationalization
16 . /usr/bin/gettext.sh
17 TEXTDOMAIN='tazbox'
18 export TEXTDOMAIN
20 # Icons for most windows
21 icon=/usr/share/pixmaps/slitaz-icon.png
23 # some constants to be used inside functions
24 tmp="/tmp/keymap.list"
25 db="/usr/share/i18n/locales"
26 zi="/usr/share/zoneinfo/"
28 #
29 # Functions
30 #
32 usage() {
33 cat << EOT
35 $(gettext "Usage:") $(basename $0) [command]
37 $(gettext "Commands:")
38 usage $(gettext "Display this short help usage")
39 su $(gettext "Execute a command as super-user")
40 logout $(gettext "Desktop logout box with actions")
41 out $(gettext "Pipe a command output into a GTK window")
42 out-dl $(gettext "Pipe Wget output into a GTK window")
43 locale $(gettext "Configure system language (root)")
44 keymap $(gettext "Configure system keymap (root)")
45 tz $(gettext "Configure system timezone (root)")
46 setup $(gettext "System initial setup (locale, keymap & timezone)")
47 new-file $(gettext "Create a new file or folder on the desktop")
48 all-apps $(gettext "Display icons of all installed applications")
49 notify $(gettext "Notify user with a desktop centered box")
51 EOT
52 }
54 # Su frontend GUI's
55 su_main() {
56 text=$(gettext "Slitaz admin password")
57 note=$(gettext "Please enter root password (default root) to execute:")
58 yad --title="Slitaz - su" --window-icon=$icon \
59 --width=520 --height=140 \
60 --text="<b>$text</b>\n$note\n$SU_CMD\n" \
61 --image="slitaz-icon" --image-on-top \
62 --center --on-top --form $PASSWD \
63 --field="`gettext "Password:"`:H" $CHECKED \
64 --field="`gettext "Autosave password"`:CHK"
65 }
67 su_error() {
68 text=$(gettext "Error: wrong password!")
69 yad --title="Slitaz - su error" --width=520 --height=60 \
70 --image="error" --image-on-top --center --on-top \
71 --text="\n\t<b>$text</b>" --button="gtk-close:1"
72 }
74 # User may press cancel on download.
75 cancel_dl() {
76 if [ "$?" == 1 ]; then
77 echo "CANCEL"
78 rm -f $DOWNLOADS/$(basename $url)
79 fi
80 }
82 # Output a command in a GTK window
83 output_command() {
84 yad --text-info --title="TazBox Output" --window-icon=$icon \
85 --geometry="560x210+0-24" --fore="#ffffff" --back="#000000"
86 }
88 # Logout GUI function
89 logout_main() {
90 text=$(gettext "<b>SliTaz Logout.</b> Please choose an action:")
91 yad --entry --title="SliTaz Logout" --window-icon=$icon \
92 --width=440 --height=140 --text="$text" \
93 --image="slitaz-icon" --image-on-top \
94 --center --on-top --entry-text \
95 "`gettext \"Close X session\"` : exit" \
96 "`gettext \"Reboot system\"` : reboot" \
97 "`gettext \"Shutdown system\"` : halt"
98 }
100 # Generate keymap list
101 gen_kmap_list() {
102 echo > $tmp
103 cd /usr/share/kbd/keymaps/i386
104 # We first need a list to sort and then use \n for Yad list.
105 for i in $(find *rty *rtz dvorak -name *.map.gz)
106 do
107 keymap=$(basename $i)
108 type=$(dirname $i)
109 echo -e "$keymap|$type" >> $tmp
110 done
111 }
113 # Initial Config functions
114 setup_main() {
115 gen_kmap_list
116 title=$(gettext "SliTaz Initial Setup")
117 message=$(gettext "\n<big>Here you can set your preferences\nfor <b>locale, keymap and timezone</b>.</big>\n\n")
118 locale=$(ls -1 $db | grep ^[a-z][a-z]_[A-Z][A-Z] | tr "\n" "!")
119 keymap=$(cat $tmp | cut -d. -f1 | sort | tr "\n" "!")
120 timezone=$(find $zi -type f | sed s,$zi,,g | tr "\n" "!")
121 yad --width=500 --height=380 \
122 --image=$icon --title="$title" --form --text="$message" \
123 --field "Locale:CB" \
124 --field "Keymap:CB" \
125 --field "Timezone:CB" \
126 $locale \ $keymap \ $timezone
127 }
129 setup() {
130 choices=$(setup_main)
131 locale=$(echo $choices | cut -d"|" -f1)
132 keymap=$(echo $choices | cut -d"|" -f2)
133 timezone=$(echo $choices | cut -d"|" -f3)
134 [ $locale ] && tazlocale init $locale
135 [ $keymap ] && tazkeymap init $keymap
136 [ $timezone ] && echo $timezone > /etc/TZ
137 }
139 # Locale functions
140 locale_main() {
141 text=$(gettext "Language configuration")
142 for locale in $(ls -1 $db | grep ^[a-z][a-z]_[A-Z][A-Z])
143 do
144 desc=$(grep ^title $db/$locale | cut -d '"' -f 2)
145 echo -e "$locale \n $desc"
146 done | \
147 yad --list --title="SliTaz locale" --window-icon=$icon \
148 --width=500 --height=380 --separator="" \
149 --center --sticky --on-top \
150 --image=preferences-desktop-locale --image-on-top \
151 --text="<b>$text</b>" --print-column=1 \
152 --column $(gettext "Name") --column $(gettext "Description")
153 }
155 locale() {
156 locale=$(locale_main)
157 # Deal with --button values
158 case $? in
159 1) exit 0 ;;
160 *) continue ;;
161 esac
162 # System language configuration.
163 [ "$locale" ] && tazlocale $locale
164 }
166 # Keymap functions
167 keymap_main() {
168 gen_kmap_list
169 text=$(gettext "Keyboard configuration")
170 for i in $(sort $tmp)
171 do
172 keymap=$(echo $i | cut -d "|" -f 1)
173 type=$(echo $i | cut -d "|" -f 2)
174 echo -e "${keymap%.map.gz} \n $type"
175 done | \
176 yad --list $opts --title="SliTaz keymap" --window-icon=$icon \
177 --width=500 --height=380 --separator="" \
178 --center --sticky --on-top \
179 --image=input-keyboard --image-on-top \
180 --text="<b>$text</b>" --print-column=1 \
181 --column $(gettext "Keymap") --column $(gettext "Type")
182 rm -f $tmp
183 }
185 keymap() {
186 keymap=$(keymap_main)
187 # Deal with --button values
188 case $? in
189 1) exit 0 ;;
190 *) continue ;;
191 esac
192 # System keymap configuration
193 [ "$keymap" ] && tazkeymap $keymap
194 }
196 # TZ functions
197 tz_main() {
198 text=$(gettext "TimeZone Configuration")
199 for t in $(find $zi -type f | sed s,$zi,,g)
200 do
201 echo -e "$t"
202 done | \
203 yad --list --title="SliTaz TZ" --window-icon=$icon \
204 --width=500 --height=380 --separator="" \
205 --center --sticky --on-top \
206 --image=preferences-desktop-locale --image-on-top \
207 --text="<b>$text</b>" --print-column=1 \
208 --column $(gettext "Name")
209 }
211 tz() {
212 timezone=$(tz_main)
213 case $? in
214 1) exit 0 ;;
215 *) continue ;;
216 esac
217 [ "$timezone" ] && echo $timezone > /etc/TZ
218 }
220 # New file functions
221 newfile_main() {
222 text=$(gettext "Create a new file or folder on your desktop")
223 yad --entry --title="$(gettext "New file")" --window-icon=$icon \
224 --text="<b>$text</b>" --width=460 --height=160 \
225 --center --on-top --image=slitaz-icon --image-on-top \
226 --entry-label="$(gettext "File name")" --always-print-result \
227 --button="$(gettext "SHell script"):4" \
228 --button="$(gettext "Folder"):3" \
229 --button="$(gettext "File"):2" \
230 --button="gtk-cancel:1"
231 }
233 newfile() {
234 file=$(newfile_main)
235 ret=$?
236 [ "$file" ] || exit 0
237 case $ret in
238 4)
239 cat > $HOME/Desktop/$file << EOT
240 #!/bin/sh
241 #
243 EOT
244 chmod +x $HOME/Desktop/$file ;;
245 3) mkdir -p $HOME/Desktop/$file ;;
246 2) touch $HOME/Desktop/$file ;;
247 1) exit 0 ;;
248 esac
249 }
251 # All applications
252 all_apps() {
253 yad --icons --title="$(gettext "All Applications")" \
254 --compact --read-dir=/usr/share/applications \
255 --window-icon=$icon --width=400 --height=400 \
256 --button="gtk-close:0"
257 }
259 #
260 # Commands
261 #
262 case "$1" in
263 su)
264 # Keep command in an exported variable to be used by Yad. 4 arguments
265 # should be enuff: appname --opt --opt -v /dev/sda. Nothing to do if
266 # we are root.
267 test $(id -u) = 0 && exec ${@:2}
268 export SU_CMD="${@:2}"
269 # Check if a password has been saved before launching main dialog
270 if [ -s $HOME/.config/slitaz/subox.conf ]; then
271 PASSWD=$(cat $HOME/.config/slitaz/subox.conf)
272 CHECKED="TRUE"
273 fi
274 # Display the main dialog (ask for password)
275 main=$(su_main)
276 # Deal with --button values and exit if cancelled to avoid erasing
277 # saved password.
278 case $? in
279 1) exit 0 ;;
280 *) continue ;;
281 esac
282 # Save or erase Autosaved password
283 if [ $(echo $main | cut -f2 -d"|") == "TRUE" ]; then
284 echo $main | cut -f 1 -d "|" > $HOME/.config/slitaz/subox.conf
285 chmod 0600 $HOME/.config/slitaz/subox.conf
286 else
287 cat /dev/null > $HOME/.config/slitaz/subox.conf
288 fi
289 # Try to login & execute. If password is wrong execute error dialog
290 echo $main | cut -f 1 -d "|" | su -c "$SU_CMD &" || su_error ;;
291 logout)
292 # Logout window with actions
293 main=$(logout_main)
294 # Deal with --button values
295 case $? in
296 1) exit 0 ;;
297 *) continue ;;
298 esac
299 # DE and WM started with a custom -session script should export
300 # XDG_CURRENT_DESKTOP
301 case "$main" in
302 *exit)
303 case $XDG_CURRENT_DESKTOP in
304 LXDE)
305 [ "$_LXSESSION_PID" ] && kill $_LXSESSION_PID
306 [ "$DESKTOP_SESSION" == "compiz" ] && \
307 killall compiz
308 openbox --exit ;;
309 openbox) openbox --exit ;;
310 compiz) killall compiz ;;
311 *)
312 # Try to kill other WM that dont set XDG var.
313 jwm -exit 2>/dev/null ;;
314 esac ;;
315 *reboot) reboot || reboot -f ;;
316 *halt) poweroff ;;
317 esac ;;
318 out)
319 # Pipe a command into a GTK window
320 output_command ;;
321 out-dl)
322 # A tiny GTK window for Busybox wget output
323 url=$2
324 [ -d $DOWNLOADS ] || mkdir -p $DOWNLOADS
325 busybox wget -c -P $DOWNLOADS $url 2>&1 | output_command
326 cancel_dl ;;
327 locale)
328 locale ;;
329 keymap)
330 keymap ;;
331 tz)
332 test $(id -u) != 0 && exec tazbox su tazbox $@ && exit 0 ;
333 tz ;;
334 setup)
335 setup ;;
336 new-file)
337 newfile ;;
338 all-apps)
339 all_apps ;;
340 notify|-n)
341 # On screen notification box.
342 icon="$3"
343 time="$4"
344 [ "$icon" ] || icon=info
345 [ "$time" ] || time=4
346 yad --text="<b>$2</b>" --width=520 --height=80 \
347 --center --on-top --image=$icon --image-on-top \
348 --timeout=$time --skip-taskbar --no-buttons \
349 --borders=12 --undecorated ;;
350 *)
351 usage ;;
352 esac
354 exit 0