slitaz-tools view tazbox/tazbox @ rev 713

tazbox: First attemp to fix Xorg config file creation
author Christophe Lincoln <pankso@slitaz.org>
date Tue Mar 20 14:32:17 2012 +0100 (2012-03-20)
parents e2c4bab7dc33
children 161c73d4c058
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")
50 EOT
51 }
53 # Su frontend GUI's
54 su_main() {
55 text=$(gettext "Slitaz admin password")
56 note=$(gettext "Please enter root password (default root) to execute:")
57 yad --title="Slitaz - su" --window-icon=$icon \
58 --width=520 --height=140 \
59 --text="<b>$text</b>\n$note\n$SU_CMD\n" \
60 --image="slitaz-icon" --image-on-top \
61 --center --on-top --form $PASSWD \
62 --field="`gettext "Password:"`:H" $CHECKED \
63 --field="`gettext "Autosave password"`:CHK"
64 }
66 su_error() {
67 text=$(gettext "Error: wrong password!")
68 yad --title="Slitaz - su error" --width=520 --height=60 \
69 --image="error" --image-on-top --center --on-top \
70 --text="\n\t<b>$text</b>" --button="gtk-close:1"
71 }
73 # User may press cancel on download.
74 cancel_dl() {
75 if [ "$?" == 1 ]; then
76 echo "CANCEL"
77 rm -f $DOWNLOADS/$(basename $url)
78 fi
79 }
81 # Output a command in a GTK window
82 output_command() {
83 yad --text-info --title="TazBox Output" --window-icon=$icon \
84 --geometry="560x210+0-24" --fore="#ffffff" --back="#000000"
85 }
87 # Logout GUI function
88 logout_main() {
89 text=$(gettext "<b>SliTaz Logout.</b> Please choose an action:")
90 yad --entry --title="SliTaz Logout" --window-icon=$icon \
91 --width=440 --height=140 --text="$text" \
92 --image="slitaz-icon" --image-on-top \
93 --center --on-top --entry-text \
94 "`gettext \"Close X session\"` : exit" \
95 "`gettext \"Reboot system\"` : reboot" \
96 "`gettext \"Shutdown system\"` : halt"
97 }
99 # Generate keymap list
100 gen_kmap_list() {
101 echo > $tmp
102 cd /usr/share/kbd/keymaps/i386
103 # We first need a list to sort and then use \n for Yad list.
104 for i in $(find *rty *rtz dvorak -name *.map.gz)
105 do
106 keymap=$(basename $i)
107 type=$(dirname $i)
108 echo -e "$keymap|$type" >> $tmp
109 done
110 }
112 # Initial Config functions
113 setup_main() {
114 gen_kmap_list
115 title=$(gettext "SliTaz Initial Setup")
116 message=$(gettext "\n<big>Here you can set your preferences\nfor <b>locale, keymap and timezone</b>.</big>\n\n")
117 locale=$(ls -1 $db | grep ^[a-z][a-z]_[A-Z][A-Z] | tr "\n" "!")
118 keymap=$(cat $tmp | cut -d. -f1 | sort | tr "\n" "!")
119 timezone=$(find $zi -type f | sed s,$zi,,g | tr "\n" "!")
120 yad --width=500 --height=380 \
121 --image=$icon --title="$title" --form --text="$message" \
122 --field "Locale:CB" \
123 --field "Keymap:CB" \
124 --field "Timezone:CB" \
125 $locale \ $keymap \ $timezone
126 }
128 setup() {
129 choices=$(setup_main)
130 locale=$(echo $choices | cut -d"|" -f1)
131 keymap=$(echo $choices | cut -d"|" -f2)
132 timezone=$(echo $choices | cut -d"|" -f3)
133 [ $locale ] && tazlocale init $locale
134 [ $keymap ] && tazkeymap init $keymap
135 [ $timezone ] && echo $timezone > /etc/TZ
136 }
138 # Locale functions
139 locale_main() {
140 text=$(gettext "Language configuration")
141 for locale in $(ls -1 $db | grep ^[a-z][a-z]_[A-Z][A-Z])
142 do
143 desc=$(grep ^title $db/$locale | cut -d '"' -f 2)
144 echo -e "$locale \n $desc"
145 done | \
146 yad --list --title="SliTaz locale" --window-icon=$icon \
147 --width=500 --height=380 --separator="" \
148 --center --sticky --on-top \
149 --image=preferences-desktop-locale --image-on-top \
150 --text="<b>$text</b>" --print-column=1 \
151 --column $(gettext "Name") --column $(gettext "Description")
152 }
154 locale() {
155 locale=$(locale_main)
156 # Deal with --button values
157 case $? in
158 1) exit 0 ;;
159 *) continue ;;
160 esac
161 # System language configuration.
162 [ "$locale" ] && tazlocale init $locale
163 }
165 # Keymap functions
166 keymap_main() {
167 gen_kmap_list
168 text=$(gettext "Keyboard configuration")
169 for i in $(sort $tmp)
170 do
171 keymap=$(echo $i | cut -d "|" -f 1)
172 type=$(echo $i | cut -d "|" -f 2)
173 echo -e "${keymap%.map.gz} \n $type"
174 done | \
175 yad --list $opts --title="SliTaz keymap" --window-icon=$icon \
176 --width=500 --height=380 --separator="" \
177 --center --sticky --on-top \
178 --image=input-keyboard --image-on-top \
179 --text="<b>$text</b>" --print-column=1 \
180 --column $(gettext "Keymap") --column $(gettext "Type")
181 rm -f $tmp
182 }
184 keymap() {
185 keymap=$(keymap_main)
186 # Deal with --button values
187 case $? in
188 1) exit 0 ;;
189 *) continue ;;
190 esac
191 # System keymap configuration
192 [ "$keymap" ] && tazkeymap init $keymap
193 }
195 # TZ functions
196 tz_main() {
197 text=$(gettext "TimeZone Configuration")
198 for t in $(find $zi -type f | sed s,$zi,,g)
199 do
200 echo -e "$t"
201 done | \
202 yad --list --title="SliTaz TZ" --window-icon=$icon \
203 --width=500 --height=380 --separator="" \
204 --center --sticky --on-top \
205 --image=preferences-desktop-locale --image-on-top \
206 --text="<b>$text</b>" --print-column=1 \
207 --column $(gettext "Name")
208 }
210 tz() {
211 timezone=$(tz_main)
212 case $? in
213 1) exit 0 ;;
214 *) continue ;;
215 esac
216 [ "$timezone" ] && echo $timezone > /etc/TZ
217 }
219 # New file functions
220 newfile_main() {
221 text=$(gettext "Create a new file or folder on your desktop")
222 yad --entry --title="$(gettext "New file")" --window-icon=$icon \
223 --text="<b>$text</b>" --width=460 --height=160 \
224 --center --on-top --image=slitaz-icon --image-on-top \
225 --entry-label="$(gettext "File name")" --always-print-result \
226 --button="$(gettext "SHell script"):4" \
227 --button="$(gettext "Folder"):3" \
228 --button="$(gettext "File"):2" \
229 --button="gtk-cancel:1"
230 }
232 newfile() {
233 file=$(newfile_main)
234 ret=$?
235 [ "$file" ] || exit 0
236 case $ret in
237 4)
238 cat > $HOME/Desktop/$file << EOT
239 #!/bin/sh
240 #
242 EOT
243 chmod +x $HOME/Desktop/$file ;;
244 3) mkdir -p $HOME/Desktop/$file ;;
245 2) touch $HOME/Desktop/$file ;;
246 1) exit 0 ;;
247 esac
248 }
250 # All applications
251 all_apps() {
252 yad --icons --title="$(gettext "All Applications")" \
253 --compact --read-dir=/usr/share/applications \
254 --window-icon=$icon --width=400 --height=400 \
255 --button="gtk-close:0"
256 }
258 #
259 # Commands
260 #
261 case "$1" in
262 su)
263 # Keep command in an exported variable to be used by Yad. 4 arguments
264 # should be enuff: appname --opt --opt -v /dev/sda. Nothing to do if
265 # we are root.
266 test $(id -u) = 0 && exec $2 $3 $4 $5
267 export SU_CMD="$2 $3 $4 $5"
268 # Check if a password has been saved before launching main dialog
269 if [ -s $HOME/.config/slitaz/subox.conf ]; then
270 PASSWD=$(cat $HOME/.config/slitaz/subox.conf)
271 CHECKED="TRUE"
272 fi
273 # Display the main dialog (ask for password)
274 main=$(su_main)
275 # Deal with --button values and exit if cancelled to avoid erasing
276 # saved password.
277 case $? in
278 1) exit 0 ;;
279 *) continue ;;
280 esac
281 # Save or erase Autosaved password
282 if [ $(echo $main | cut -f2 -d"|") == "TRUE" ]; then
283 echo $main | cut -f 1 -d "|" > $HOME/.config/slitaz/subox.conf
284 chmod 0600 $HOME/.config/slitaz/subox.conf
285 else
286 cat /dev/null > $HOME/.config/slitaz/subox.conf
287 fi
288 # Try to login & execute. If password is wrong execute error dialog
289 echo $main | cut -f 1 -d "|" | su -c "$SU_CMD &" || su_error ;;
290 logout)
291 # Logout window with actions
292 main=`logout_main`
293 # Deal with --button values
294 case $? in
295 1) exit 0 ;;
296 *) continue ;;
297 esac
298 # Deal with $main values
299 case "$main" in
300 *exit) openbox --exit || jwm -exit ;;
301 *reboot) reboot ;;
302 *halt) poweroff ;;
303 esac ;;
304 out)
305 # Pipe a command into a GTK window
306 output_command ;;
307 out-dl)
308 # A tiny GTK window for Busybox wget output
309 url=$2
310 [ -d $DOWNLOADS ] || mkdir -p $DOWNLOADS
311 busybox wget -c -P $DOWNLOADS $url 2>&1 | output_command
312 cancel_dl ;;
313 locale)
314 locale ;;
315 keymap)
316 keymap ;;
317 tz)
318 tz ;;
319 setup)
320 setup ;;
321 boot)
322 # This command is used at first boot to configure system.
323 Xorg -br -quiet -nolisten tcp :1 &
324 DISPLAY=:1 openbox &
325 locale
326 keymap
327 killall Xorg 2>/dev/null
328 rm -rf /tmp/.X-* ;;
329 new-file)
330 newfile ;;
331 all-apps)
332 all_apps ;;
333 notify|-n)
334 # On screen notification box.
335 time="$3"
336 [ "$time" ] || time=4
337 yad --text="<b>$2</b>" --width=520 --height=80 \
338 --center --on-top --image=info --image-on-top \
339 --timeout=$time --skip-taskbar --no-buttons \
340 --borders=12 --undecorated ;;
341 *)
342 usage ;;
343 esac
345 exit 0