Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(179)

Side by Side Diff: third_party/xdg-utils/scripts/xdg-settings.in

Issue 151109: Add initial xdg-settings patch to xdg-utils. (Closed) Base URL: http://src.chromium.org/svn/trunk/deps/
Patch Set: '' Created 11 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « third_party/xdg-utils/scripts/xdg-settings ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/bin/sh
2 #---------------------------------------------
3 # xdg-settings
4 #
5 # Utility script to get various settings from the desktop environment.
6 #
7 # Refer to the usage() function below for usage.
8 #
9 # Copyright 2009, Google Inc.
10 #
11 # LICENSE:
12 #
13 #---------------------------------------------
14
15 manualpage()
16 {
17 cat << _MANUALPAGE
18 _MANUALPAGE
19 }
20
21 usage()
22 {
23 cat << _USAGE
24 _USAGE
25 }
26
27 #@xdg-utils-common@
28
29 check_desktop_filename()
30 {
31 case "$1" in
32 */*)
33 exit_failure_syntax "invalid application name"
34 ;;
35 *.desktop)
36 return
37 ;;
38 *)
39 exit_failure_syntax "invalid application name"
40 ;;
41 esac
42 }
43
44 # {{{ default browser
45
46 # In order to remove an application from the automatically-generated list of
47 # applications for handling a given MIME type, the desktop environment may copy
48 # the global .desktop file into the user's .local directory, and remove that
49 # MIME type from its list. In that case, we must restore the MIME type to the
50 # application's list of MIME types before we can set it as the default for that
51 # MIME type. (We can't just delete the local version, since the user may have
52 # made other changes to it as well. So, tweak the existing file.)
53 # This function is hard-coded for text/html but it could be adapted if needed.
54 fix_local_desktop_file()
55 {
56 apps="${XDG_DATA_HOME:-$HOME/.local/share}/applications"
57 # No local desktop file?
58 [ ! -f "$apps/$1" ] && return
59 MIME="`grep ^MimeType= "$apps/$1" | cut -d= -f 2-`"
60 case "$MIME" in
61 text/html\;*|*\;text/html\;*|*\;text/html\;|*\;text/html)
62 # Already has text/html? Great!
63 return 0
64 ;;
65 esac
66
67 # Add text/html to the list
68 temp="`mktemp $apps/$1.XXXXXX`" || return
69 grep -v ^MimeType= "$apps/$1" >> "$temp"
70 echo "MimeType=text/html;$MIME" >> "$temp"
71
72 oldlines="`wc -l < "$apps/$1"`"
73 newlines="`wc -l < "$temp"`"
74 # The new file should have at least as many lines as the old
75 if [ $oldlines -le $newlines ]; then
76 mv "$temp" "$apps/$1"
77 # This can take a little bit to get noticed
78 sleep 4
79 else
80 rm -f "$temp"
81 return 1
82 fi
83 }
84
85 get_browser_mime()
86 {
87 xdg-mime query default text/html
88 }
89
90 set_browser_mime()
91 {
92 orig="`get_browser_mime`"
93 # Fixing the local desktop file can actually change the default browser all
94 # by itself, so we fix it only after querying to find the current default
95 fix_local_desktop_file "$1" || return
96 mkdir -p "${XDG_DATA_HOME:-$HOME/.local/share/applications}"
97 xdg-mime default "$1" text/html || return
98 if [ x"`get_browser_mime`" != x"$1" ]; then
99 # Put back the original value
100 xdg-mime default "$orig" text/html
101 exit_failure_operation_failed
102 fi
103 }
104
105 get_browser_kde()
106 {
107 browser="`kreadconfig --file kdeglobals --group General --key BrowserApplica tion`"
108 if [ x"$browser" = x ]; then
109 # No default browser; KDE will probably use the MIME type text/html
110 get_browser_mime
111 else
112 echo "$browser"
113 fi
114 }
115
116 set_browser_kde()
117 {
118 set_browser_mime "$1" || return
119 kwriteconfig --file kdeglobals --group General --key BrowserApplication "$1"
120 }
121
122 # This handles backslashes but not quote marks
123 first_word()
124 {
125 read first rest
126 echo "$first"
127 }
128
129 binary_to_desktop_file()
130 {
131 search="${XDG_DATA_HOME:-$HOME/.local/share}:${XDG_DATA_DIRS:-/usr/local/sha re:/usr/share}"
132 binary="`which "$1"`"
133 base="`basename "$1"`"
134 IFS=:
135 for dir in $search; do
136 unset IFS
137 [ "$dir" -a -d "$dir/applications" ] || continue
138 for file in "$dir"/applications/*.desktop; do
139 [ -r "$file" ] || continue
140 # Check to make sure it's worth the processing
141 grep -q "^Exec.*$base" "$file" || continue
142 exec="`grep "^Exec\(\[[^]=]*]\)\?=" "$file" | cut -d= -f 2- | first_ word`"
143 full="`which "$exec"`"
144 if [ x"$full" = x"$binary" ]; then
145 # Fix any double slashes that got added path composition
146 echo "$file" | sed -e 's,//*,/,g'
147 return
148 fi
149 done
150 done
151 }
152
153 desktop_file_to_command()
154 {
155 search="${XDG_DATA_HOME:-$HOME/.local/share}:${XDG_DATA_DIRS:-/usr/local/sha re:/usr/share}"
156 desktop="`basename "$1"`"
157 IFS=:
158 for dir in $search; do
159 [ "$dir" -a -d "$dir/applications" ] || continue
160 file="$dir/applications/$desktop"
161 [ -r "$file" ] || continue
162 # Change %F, %f, %U, and %u to %s
163 grep "^Exec\(\[[^]=]*]\)\?=" "$file" | cut -d= -f 2- | sed -e 's/%[FfUu] /%s/g'
164 return
165 done
166 }
167
168 get_browser_gnome()
169 {
170 binary="`gconftool-2 --get /desktop/gnome/applications/browser/exec`"
171 if [ x"$binary" = x ]; then
172 # No default browser; GNOME might use the MIME type text/html
173 get_browser_mime
174 else
175 # gconftool gives the binary (maybe with %s etc. afterward),
176 # but we want the desktop file name, not the binary. So, we
177 # have to find the desktop file to which it corresponds.
178 binary="`echo "$binary" | first_word`"
179 desktop="`binary_to_desktop_file "$binary"`"
180 if [ "$desktop" ]; then
181 basename "$desktop"
182 exit_success
183 fi
184 binary="`which "$binary"`"
185 if [ "$binary" ]; then
186 echo "$binary"
187 exit_success
188 fi
189 exit_failure_operation_failed
190 fi
191 }
192
193 set_browser_gnome()
194 {
195 command="`desktop_file_to_command "$1"`"
196 [ "$command" ] || exit_failure_file_missing
197 binary="`echo "$command" | first_word`"
198 set_browser_mime "$1" || return
199
200 # Set the default browser
201 gconftool-2 --type string --set /desktop/gnome/applications/browser/exec "$b inary"
202 gconftool-2 --type bool --set /desktop/gnome/applications/browser/needs_term false
203 gconftool-2 --type bool --set /desktop/gnome/applications/browser/nremote tr ue
204 # Set the handler for HTTP and HTTPS
205 for protocol in http https; do
206 gconftool-2 --type string --set /desktop/gnome/url-handlers/$protocol/co mmand "$command"
207 gconftool-2 --type bool --set /desktop/gnome/url-handlers/$protocol/need s_terminal false
208 gconftool-2 --type bool --set /desktop/gnome/url-handlers/$protocol/enab led true
209 done
210 # Set the handler for about: and unknown URL types
211 for protocol in about unknown; do
212 gconftool-2 --type string --set /desktop/gnome/url-handlers/$protocol/co mmand "$command"
213 done
214 }
215
216 get_browser_xfce()
217 {
218 # Not supported yet
219 exit_failure_operation_impossible
220 }
221
222 set_browser_xfce()
223 {
224 # Not supported yet
225 exit_failure_operation_impossible
226 }
227
228 # }}} default browser
229
230 dispatch_specific()
231 {
232 # The PARM comments in this function are used to generate the output of
233 # the --list option. The formatting is important. Make sure to line up the
234 # property descriptions with spaces so that it will look nice.
235 if [ x"$op" = x"get" ]; then
236 case "$parm" in
237 default-web-browser) # PROP: Default web browser
238 get_browser_$DE
239 ;;
240
241 *)
242 exit_failure_syntax
243 ;;
244 esac
245 else # set
246 [ $# -eq 1 ] || exit_failure_syntax "unexpected/missing argument"
247 case "$parm" in
248 default-web-browser)
249 check_desktop_filename "$1"
250 set_browser_$DE "$1"
251 ;;
252
253 *)
254 exit_failure_syntax
255 ;;
256 esac
257 fi
258
259 if [ $? -eq 0 ]; then
260 exit_success
261 else
262 exit_failure_operation_failed
263 fi
264 }
265
266 dispatch_generic()
267 {
268 # We only know how to get the default web browser
269 [ x"$op" != x"get" ] && exit_failure_operation_impossible
270 [ x"$parm" != x"default-web-browser" ] && exit_failure_operation_impossible
271
272 # FIXME: look up the desktop file name
273
274 # First look in $BROWSER
275 if [ x"$BROWSER" != x ]; then
276 echo "${BROWSER%%:*}"
277 exit_success
278 fi
279
280 # Debian and Ubuntu (and others?) have x-www-browser
281 browser="`which x-www-browser`"
282 [ "$browser" ] || exit_failure_operation_failed
283 browser="`readlink -f "$browser"`"
284
285 echo "$browser"
286 exit_success
287 }
288
289 if [ x"$1" = x"--list" ]; then
290 echo "Known properties:"
291 # Extract the property names from dispatch_specific() above
292 grep "^[ ]*[^)]*) # PROP:" "$0" | sed -e 's/^[ ]*\([^)]*\)) # PROP: \(. *\)$/ \1 \2/' | sort
293 exit_success
294 fi
295
296 [ x"$1" != x ] || exit_failure_syntax "no operation given"
297 [ x"$2" != x ] || exit_failure_syntax "no parameter name given"
298
299 op="$1"
300 parm="$2"
301 shift 2
302
303 [ x"$op" = x"get" -o x"$op" = x"set" ] || exit_failure_syntax "invalid operation "
304
305 detectDE
306
307 case "$DE" in
308 kde|gnome|xfce)
309 dispatch_specific "$@"
310 ;;
311
312 generic)
313 dispatch_generic "$@"
314 ;;
315
316 *)
317 exit_failure_operation_impossible "unknown desktop environment"
318 ;;
319 esac
OLDNEW
« no previous file with comments | « third_party/xdg-utils/scripts/xdg-settings ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698