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

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

Issue 151098: Patch from mdm@google.com... (Closed) Base URL: svn://chrome-svn/chrome/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-mime ('k') | third_party/xdg-utils/scripts/xdg-open » ('j') | 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-mime
4 #
5 # Utility script to manipulate MIME related information
6 # on XDG compliant systems.
7 #
8 # Refer to the usage() function below for usage.
9 #
10 # Copyright 2006, Kevin Krammer <kevin.krammer@gmx.at>
11 # Copyright 2006, Jeremy White <jwhite@codeweavers.com>
12 #
13 # LICENSE:
14 #
15 #---------------------------------------------
16
17 manualpage()
18 {
19 cat << _MANUALPAGE
20 _MANUALPAGE
21 }
22
23 usage()
24 {
25 cat << _USAGE
26 _USAGE
27 }
28
29 #@xdg-utils-common@
30
31 update_mime_database()
32 {
33 if [ x"$mode" = x"user" -a -n "$DISPLAY" ] ; then
34 detectDE
35 if [ x"$DE" = x"kde" ] ; then
36 DEBUG 1 "Running kbuildsycoca"
37 eval 'kbuildsycoca'$xdg_redirect_output
38 fi
39 fi
40 for x in `echo "$PATH:/opt/gnome/bin" | sed 's/:/ /g'`; do
41 if [ -x $x/update-mime-database ] ; then
42 DEBUG 1 "Running $x/update-mime-database $1"
43 eval '$x/update-mime-database $1'$xdg_redirect_output
44 return
45 fi
46 done
47 }
48
49 info_kde()
50 {
51 if which "kmimetypefinder" > /dev/null 2>/dev/null; then
52 DEBUG 1 "Running kmimetypefinder \"$1\""
53 kmimetypefinder "$1" 2>/dev/null | head -n 1
54 else
55 DEBUG 1 "Running kfile \"$1\""
56 kfile "$1" 2> /dev/null | head -n 1 | cut -d "(" -f 2 | cut -d ")" -f 1
57 fi
58
59 exit_success
60 }
61
62 info_gnome()
63 {
64 file=`readlink -f "$1"` # Normalize path
65 DEBUG 1 "Running gnomevfs-info \"$file\""
66 gnomevfs-info "$file" 2> /dev/null | grep MIME | cut -d ":" -f 2 | sed s/"^ "//
67
68 exit_success
69 }
70
71 info_generic()
72 {
73 DEBUG 1 "Running /usr/bin/file -i \"$1\""
74 /usr/bin/file -i "$1" 2> /dev/null | cut -d ":" -f 2 | sed s/"^ "//
75
76 exit_success
77 }
78
79 make_default_kde()
80 {
81 # $1 is vendor-name.desktop
82 # $2 is mime/type
83 # Add to $KDE_HOME/share/config/profilerc:
84 # [$2 - 1]
85 # Application=$1
86 #
87 # Remove all [$2 - *] sections, or even better,
88 # renumber [$2 - *] sections and remove duplicate
89
90 default_file="$HOME/.kde/share/config/profilerc"
91 DEBUG 2 "make_default_kde $1 $2"
92 DEBUG 1 "Updating $default_file"
93 mkdir -p "$HOME/.kde/share/config"
94 [ -f $default_file ] || touch $default_file
95 awk -v application="$1" -v mimetype="$2" '
96 BEGIN {
97 header_start="[" mimetype " - "
98 supress=0
99 }
100 {
101 if (index($0, header_start) == 1 )
102 supress=1
103 else
104 if (/^\[/) { supress=0 }
105
106 if (!supress) {
107 print $0
108 }
109 }
110 END {
111 print ""
112 print "[" mimetype " - 1]"
113 print "Application=" application
114 print "AllowAsDefault=true"
115 print "GenericServiceType=Application"
116 print "Preference=1"
117 print "ServiceType=" mimetype
118 }
119 ' $default_file > ${default_file}.new && mv ${default_file}.new $default_file
120 }
121
122 make_default_generic()
123 {
124 # $1 is vendor-name.desktop
125 # $2 is mime/type
126 # Add $2=$1 to XDG_DATA_HOME/applications/defaults.list
127 xdg_user_dir="$XDG_DATA_HOME"
128 [ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share"
129 default_file="$xdg_user_dir/applications/defaults.list"
130 DEBUG 2 "make_default_generic $1 $2"
131 DEBUG 1 "Updating $default_file"
132 grep -v "$2=" $default_file > ${default_file}.new 2> /dev/null
133 if ! grep "[Default Applications]" ${default_file}.new > /dev/null; then
134 echo "[Default Applications]" >> ${default_file}.new
135 fi
136 echo $2=$1 >> ${default_file}.new
137 mv ${default_file}.new $default_file
138 }
139
140 defapp_generic()
141 {
142 MIME="$1"
143 xdg_user_dir="$XDG_DATA_HOME"
144 [ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share"
145 xdg_user_dir="$xdg_user_dir/$xdg_dir_name"
146 xdg_system_dirs="$XDG_DATA_DIRS"
147 [ -n "$xdg_system_dirs" ] || xdg_system_dirs=/usr/local/share/:/usr/share/
148
149 for x in `echo "$xdg_user_dir:$xdg_system_dirs" | sed 's/:/ /g'`; do
150 DEBUG 2 "Checking $x/applications/defaults.list"
151 trader_result=`grep "$MIME=" $x/applications/defaults.list 2> /dev/null | cut -d '=' -f 2 | cut -d ';' -f 1`
152 if [ -n "$trader_result" ] ; then
153 echo $trader_result
154 exit_success
155 fi
156 done
157 exit_success
158 }
159
160 defapp_kde()
161 {
162 MIME="$1"
163 ktradertest=`which ktradertest 2> /dev/null`
164 if [ -n "$ktradertest" ] ; then
165 DEBUG 1 "Running ktradertest \"$MIME\" Application"
166 trader_result=`ktradertest "$MIME" Application 2>/dev/null | grep Deskto pEntryPath \
167 | head -n 1 | cut -d ':' -f 2 | cut -d \' -f 2`
168 if [ -n "$trader_result" ] ; then
169 basename "$trader_result"
170 exit_success
171 else
172 exit_failure_operation_failed
173 fi
174 else
175 defapp_generic "$1"
176 fi
177 }
178
179 [ x"$1" != x"" ] || exit_failure_syntax
180
181 mode=
182 action=
183 filename=
184 mimetype=
185
186 case $1 in
187 install)
188 action=install
189 ;;
190
191 uninstall)
192 action=uninstall
193 ;;
194
195 query)
196 shift
197
198 if [ -z "$1" ] ; then
199 exit_failure_syntax "query type argument missing"
200 fi
201
202 case $1 in
203 filetype)
204 action=info
205
206 filename="$2"
207 if [ -z "$filename" ] ; then
208 exit_failure_syntax "FILE argument missing"
209 fi
210 case $filename in
211 -*)
212 exit_failure_syntax "unexpected option '$filename'"
213 ;;
214 esac
215 check_input_file "$filename"
216 ;;
217
218 default)
219 action=defapp
220 mimetype="$2"
221 if [ -z "$mimetype" ] ; then
222 exit_failure_syntax "mimetype argument missing"
223 fi
224 case $mimetype in
225 -*)
226 exit_failure_syntax "unexpected option '$mimetype'"
227 ;;
228
229 */*)
230 # Ok
231 ;;
232
233 *)
234 exit_failure_syntax "mimetype '$mimetype' is not in the form 'minor/ major'"
235 ;;
236 esac
237 ;;
238
239 *)
240 exit_failure_syntax "unknown query type '$1'"
241 ;;
242 esac
243 ;;
244
245 default)
246 action=makedefault
247 shift
248
249 if [ -z "$1" ] ; then
250 exit_failure_syntax "application argument missing"
251 fi
252 case $1 in
253 -*)
254 exit_failure_syntax "unexpected option '$1'"
255 ;;
256
257 *.desktop)
258 filename="$1"
259 ;;
260
261 *)
262 exit_failure_syntax "malformed argument '$1', expected *.desktop"
263 ;;
264 esac
265 ;;
266
267 *)
268 exit_failure_syntax "unknown command '$1'"
269 ;;
270 esac
271
272 shift
273
274
275 if [ "$action" = "makedefault" ]; then
276 if [ -z "$1" ] ; then
277 exit_failure_syntax "mimetype argument missing"
278 fi
279
280 while [ $# -gt 0 ] ; do
281 case $1 in
282 -*)
283 exit_failure_syntax "unexpected option '$1'"
284 ;;
285 esac
286 mimetype="$1"
287 shift
288
289 make_default_kde "$filename" "$mimetype"
290 make_default_generic "$filename" "$mimetype"
291 done
292 exit_success
293 fi
294
295 if [ "$action" = "info" ]; then
296 detectDE
297
298 if [ x"$DE" = x"" ]; then
299 if [ -x /usr/bin/file ]; then
300 DE=generic
301 fi
302 fi
303
304 case "$DE" in
305 kde)
306 info_kde "$filename"
307 ;;
308
309 gnome)
310 info_gnome "$filename"
311 ;;
312
313 *)
314 info_generic "$filename"
315 ;;
316 esac
317 exit_failure_operation_impossible "no method available for quering MIME type of '$filename'"
318 fi
319
320 if [ "$action" = "defapp" ]; then
321 detectDE
322
323 case "$DE" in
324 kde)
325 defapp_kde "$mimetype"
326 ;;
327
328 *)
329 defapp_generic "$mimetype"
330 ;;
331 esac
332 exit_failure_operation_impossible "no method available for quering default a pplication for '$mimetype'"
333 fi
334
335 vendor=true
336 while [ $# -gt 0 ] ; do
337 parm="$1"
338 shift
339
340 case $parm in
341 --mode)
342 if [ -z "$1" ] ; then
343 exit_failure_syntax "mode argument missing for --mode"
344 fi
345 case "$1" in
346 user)
347 mode="user"
348 ;;
349
350 system)
351 mode="system"
352 ;;
353
354 *)
355 exit_failure_syntax "unknown mode '$1'"
356 ;;
357 esac
358 shift
359 ;;
360
361 --novendor)
362 vendor=false
363 ;;
364
365 -*)
366 exit_failure_syntax "unexpected option '$parm'"
367 ;;
368
369 *)
370 if [ -n "$filename" ] ; then
371 exit_failure_syntax "unexpected argument '$parm'"
372 fi
373
374 filename="$parm"
375 check_input_file "$filename"
376 ;;
377 esac
378 done
379
380 if [ -z "$action" ] ; then
381 exit_failure_syntax "command argument missing"
382 fi
383
384 if [ -n "$XDG_UTILS_INSTALL_MODE" ] ; then
385 if [ "$XDG_UTILS_INSTALL_MODE" = "system" ] ; then
386 mode="system"
387 elif [ "$XDG_UTILS_INSTALL_MODE" = "user" ] ; then
388 mode="user"
389 fi
390 fi
391
392 if [ -z "$mode" ] ; then
393 if [ `whoami` = "root" ] ; then
394 mode="system"
395 else
396 mode="user"
397 fi
398 fi
399
400 if [ -z "$filename" ] ; then
401 exit_failure_syntax "mimetypes-file argument missing"
402 fi
403
404 if [ "$vendor" = "true" -a "$action" = "install" ] ; then
405 check_vendor_prefix "$filename"
406 fi
407
408 xdg_base_dir=
409 xdg_dir_name=mime/packages/
410
411 xdg_user_dir="$XDG_DATA_HOME"
412 [ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share"
413 [ x"$mode" = x"user" ] && xdg_base_dir="$xdg_user_dir/mime"
414 xdg_user_dir="$xdg_user_dir/$xdg_dir_name"
415
416 xdg_system_dirs="$XDG_DATA_DIRS"
417 [ -n "$xdg_system_dirs" ] || xdg_system_dirs=/usr/local/share/:/usr/share/
418 for x in `echo $xdg_system_dirs | sed 's/:/ /g'`; do
419 if [ -w $x/$xdg_dir_name ] ; then
420 [ x"$mode" = x"system" ] && xdg_base_dir="$x/mime"
421 xdg_global_dir="$x/$xdg_dir_name"
422 break
423 fi
424 done
425 [ -w $xdg_global_dir ] || xdg_global_dir=
426 DEBUG 3 "xdg_user_dir: $xdg_user_dir"
427 DEBUG 3 "xdg_global_dir: $xdg_global_dir"
428
429 # Find KDE3 mimelnk directory
430 kde_user_dir=
431 kde_global_dir=
432 kde_global_dirs=`kde${KDE_SESSION_VERSION}-config --path mime 2> /dev/null`
433 DEBUG 3 "kde_global_dirs: $kde_global_dirs"
434 first=
435 for x in `echo $kde_global_dirs | sed 's/:/ /g'` ; do
436 if [ -z "$first" ] ; then
437 first=false
438 kde_user_dir="$x"
439 elif [ -w $x ] ; then
440 kde_global_dir="$x"
441 fi
442 done
443 DEBUG 3 "kde_user_dir: $kde_user_dir"
444 DEBUG 3 "kde_global_dir: $kde_global_dir"
445
446 # TODO: Gnome legacy support
447 # See http://forums.fedoraforum.org/showthread.php?t=26875
448 gnome_user_dir="$HOME/.gnome/apps"
449 gnome_global_dir=/usr/share/gnome/apps
450 [ -w $gnome_global_dir ] || gnome_global_dir=
451 DEBUG 3 "gnome_user_dir: $gnome_user_dir"
452 DEBUG 3 "gnome_global_dir: $gnome_global_dir"
453
454 if [ x"$mode" = x"user" ] ; then
455 xdg_dir="$xdg_user_dir"
456 kde_dir="$kde_user_dir"
457 gnome_dir="$gnome_user_dir"
458 my_umask=077
459 else
460 xdg_dir="$xdg_global_dir"
461 kde_dir="$kde_global_dir"
462 gnome_dir="$gnome_global_dir"
463 my_umask=022
464 if [ -z "${xdg_dir}${kde_dir}${gnome_dir}" ] ; then
465 exit_failure_operation_impossible "No writable system mimetype directory found."
466 fi
467 fi
468
469 # echo "[xdg|$xdg_user_dir|$xdg_global_dir]"
470 # echo "[kde|$kde_user_dir|$kde_global_dir]"
471 # echo "[gnome|$gnome_user_dir|$gnome_global_dir]"
472 # echo "[using|$xdg_dir|$kde_dir|$gnome_dir]"
473
474 basefile=`basename "$filename"`
475 #[ -z $vendor ] || basefile="$vendor-$basefile"
476
477 mimetypes=
478 if [ -n "$kde_dir" ] ; then
479 DEBUG 2 "KDE3 mimelnk directory found, extracting mimetypes from XML file"
480
481 mimetypes=`awk < "$filename" '
482 # Strip XML comments
483 BEGIN {
484 supress=0
485 }
486 {
487 do
488 if (supress) {
489 if (match($0,/-->/)) {
490 $0=substr($0,RSTART+RLENGTH)
491 supress=0
492 }
493 else {
494 break
495 }
496 }
497 else {
498 if (match($0,/<!--/)) {
499 if (RSTART>1) print substr($0,0,RSTART)
500 $0=substr($0,RSTART+RLENGTH)
501 supress=1
502 }
503 else {
504 if ($0) print $0
505 break
506 }
507 }
508 while(1)
509 }
510 ' | awk '
511 # List MIME types listed in <mime-type> tags
512 BEGIN {
513 RS="<"
514 }
515 /^mime-info/, /^\/mime-info/ {
516 if (match($0,/^mime-type/)) {
517 if (match($0,/type="[^"]*/) || match($0,/type='"'"'[^'"'"']*/)) {
518 print substr($0,RSTART+6,RLENGTH-6)
519 }
520 }
521 }'`
522 fi
523
524 DEBUG 1 "$action mimetype in $xdg_dir"
525
526 case $action in
527 install)
528 save_umask=`umask`
529 umask $my_umask
530
531 for x in $xdg_dir ; do
532 mkdir -p $x
533 eval 'cp $filename $x/$basefile'$xdg_redirect_output
534 done
535
536 if [ -n "$mimetypes" ] ; then
537 # No quotes around $mimetypes
538 for x in $mimetypes ; do
539 DEBUG 1 "Installing $kde_dir/$x.desktop (KDE 3.x support)"
540 mkdir -p `dirname $kde_dir/$x.desktop`
541 awk < "$filename" '
542 # Strip XML comments
543 BEGIN {
544 supress=0
545 }
546 {
547 do
548 if (supress) {
549 if (match($0,/-->/)) {
550 $0=substr($0,RSTART+RLENGTH)
551 supress=0
552 }
553 else {
554 break
555 }
556 }
557 else {
558 if (match($0,/<!--/)) {
559 if (RSTART>1) print substr($0,0,RSTART)
560 $0=substr($0,RSTART+RLENGTH)
561 supress=1
562 }
563 else {
564 if ($0) print $0
565 break
566 }
567 }
568 while(1)
569 }
570 ' | awk > $kde_dir/$x.desktop '
571 # Extract mimetype $x from the XML file $filename
572 # Note that bash requires us to escape a single quote as '"'"'
573 BEGIN {
574 the_type=ARGV[1]
575 the_source=ARGV[2]
576 ARGC=1
577 RS="<"
578 found=0
579 glob_patterns=""
580 }
581 /^mime-info/, /^\/mime-info/ {
582 if (match($0,/^mime-type/)) {
583 if (match($0,/type="[^"]*/) || match($0,/type='"'"'[^'"'"']*/)) {
584 if (substr($0,RSTART+6,RLENGTH-6) == the_type) {
585 found=1
586 print "[Desktop Entry]"
587 print "# Installed by xdg-mime from " the_source
588 print "Type=MimeType"
589 print "MimeType=" the_type
590 the_icon=the_type
591 sub("/", "-", the_icon)
592 print "Icon=" the_icon
593 }
594 }
595 }
596 else if (found) {
597 if (match($0,/^\/mime-type/)) {
598 if (glob_patterns)
599 print "Patterns=" glob_patterns
600 exit 0
601 }
602
603 if (match($0,/^sub-class-of/)) {
604 if (match($0,/type="[^"]*/) || match($0,/type='"'"'[^'"'"']*/)) {
605 print "X-KDE-IsAlso=" substr($0,RSTART+6,RLENGTH-6)
606 }
607 else {
608 print "Error: '"'"'type'"'"' argument missing in " RS $0
609 exit 1
610 }
611 }
612 if (match($0,/^glob/)) {
613 if (match($0,/pattern="[^"]*/) || match($0,/pattern='"'"'[^'"'"']*/)) {
614 glob_patterns = glob_patterns substr($0,RSTART+9,RLENGTH-9) ";"
615 }
616 else {
617 print "Error: '"'"'pattern'"'"' argument missing in " RS $0
618 exit 1
619 }
620 }
621 if (match($0,/^comment/)) {
622 if (match($0,/xml:lang="[^"]*/) || match($0,/xml:lang='"'"'[^'"'"']*/)) {
623 lang=substr($0,RSTART+10,RLENGTH-10)
624 }
625 else {
626 lang=""
627 }
628 if (match($0,/>/)) {
629 comment=substr($0,RSTART+1)
630 sub("&lt;", "<", comment)
631 sub("&gt;", ">", comment)
632 sub("&amp;", "\\&", comment)
633 if (lang)
634 print "Comment[" lang "]=" comment
635 else
636 print "Comment=" comment
637 }
638 }
639 }
640 }
641 END {
642 if (!found) {
643 print "Error: Mimetype '"'"'" the_type "'"'"' not found"
644 exit 1
645 }
646 }
647 ' $x $basefile
648 if [ "$?" = "1" ] ; then
649 grep -A 10 "^Error:" $kde_dir/$x.desktop >&2
650 rm $kde_dir/$x.desktop
651 exit 1
652 fi
653 done
654 fi
655
656 umask $save_umask
657 ;;
658
659 uninstall)
660 for x in $xdg_dir ; do
661 rm -f $x/$basefile
662 done
663
664 # No quotes around $mimetypes
665 for x in $mimetypes ; do
666 if grep '^# Installed by xdg-mime' $kde_dir/$x.desktop &>/dev/null ; then
667 DEBUG 1 "Removing $kde_dir/$x.desktop (KDE 3.x support)"
668 rm -f $kde_dir/$x.desktop
669 fi
670 done
671 ;;
672 esac
673
674 update_mime_database $xdg_base_dir
675
676 exit_success
677
OLDNEW
« no previous file with comments | « third_party/xdg-utils/scripts/xdg-mime ('k') | third_party/xdg-utils/scripts/xdg-open » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698