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

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

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
Property Changes:
Name: svn:executable
+ *
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 # Permission is hereby granted, free of charge, to any person obtaining a
16 # copy of this software and associated documentation files (the "Software"),
17 # to deal in the Software without restriction, including without limitation
18 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
19 # and/or sell copies of the Software, and to permit persons to whom the
20 # Software is furnished to do so, subject to the following conditions:
21 #
22 # The above copyright notice and this permission notice shall be included
23 # in all copies or substantial portions of the Software.
24 #
25 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
26 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29 # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30 # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
31 # OTHER DEALINGS IN THE SOFTWARE.
32 #
33 #---------------------------------------------
34
35 manualpage()
36 {
37 cat << _MANUALPAGE
38 Name
39
40 xdg-mime - command line tool for querying information about file type handling
41 and adding descriptions for new file types
42
43 Synopsis
44
45 xdg-mime query { filetype | default } ...
46
47 xdg-mime default application mimetype(s)
48
49 xdg-mime install [--mode mode] [--novendor] mimetypes-file
50
51 xdg-mime uninstall [--mode mode] mimetypes-file
52
53 xdg-mime { --help | --manual | --version }
54
55 Description
56
57 The xdg-mime program can be used to query information about file types and to
58 add descriptions for new file types.
59
60 Commands
61
62 query
63
64 Returns information related to file types.
65
66 The query option is for use inside a desktop session only. It is not
67 recommended to use xdg-mime query as root.
68
69 The following queries are supported:
70
71 query filetype FILE: Returns the file type of FILE in the form of a MIME
72 type.
73
74 query default mimetype: Returns the default application that the desktop
75 environment uses for opening files of type mimetype. The default
76 application is identified by its *.desktop file.
77
78 default
79
80 Ask the desktop environment to make application the default application for
81 opening files of type mimetype. An application can be made the default for
82 several file types by specifying multiple mimetypes.
83
84 application is the desktop file id of the application and has the form
85 vendor-name.desktop application must already be installed in the desktop
86 menu before it can be made the default handler. The aplication's desktop
87 file must list support for all the MIME types that it wishes to be the
88 default handler for.
89
90 Requests to make an application a default handler may be subject to system
91 policy or approval by the end-user. xdg-mime query can be used to verify
92 whether an application is the actual default handler for a specific file
93 type.
94
95 The default option is for use inside a desktop session only. It is not
96 recommended to use xdg-mime default as root.
97
98 install
99 Adds the file type descriptions provided in mimetypes-file to the desktop
100 environment. mimetypes-file must be a XML file that follows the
101 freedesktop.org Shared MIME-info Database specification and that has a
102 mime-info element as its document root. For each new file type one or more
103 icons with name major-minor must be installed with the xdg-icon-resource
104 command in the mimetypes context. For example the application/
105 vnd.oasis.opendocument.text filetype requires an icon by the name of
106 application-vnd.oasis.opendocument.text to be installed.
107 uninstall
108 Removes the file type descriptions provided in mimetypes-file and
109 previously added with xdg-mime install from the desktop environment.
110 mimetypes-file must be a XML file that follows the freedesktop.org Shared
111 MIME-info Database specification and that has a mime-info element as its
112 document root.
113
114 Options
115
116 --mode mode
117
118 mode can be user or system. In user mode the file is (un)installed for the
119 current user only. In system mode the file is (un)installed for all users
120 on the system. Usually only root is allowed to install in system mode.
121
122 The default is to use system mode when called by root and to use user mode
123 when called by a non-root user.
124
125 --novendor
126
127 Normally, xdg-mime checks to ensure that the mimetypes-file to be installed
128 has a proper vendor prefix. This option can be used to disable that check.
129
130 A vendor prefix consists of alpha characters ([a-zA-Z]) and is terminated
131 with a dash ("-"). Companies and organizations are encouraged to use a word
132 or phrase, preferably the organizations name, for which they hold a
133 trademark as their vendor prefix. The purpose of the vendor prefix is to
134 prevent name conflicts.
135
136 --help
137 Show command synopsis.
138 --manual
139 Show this manualpage.
140 --version
141 Show the xdg-utils version information.
142
143 Environment Variables
144
145 xdg-mime honours the following environment variables:
146
147 XDG_UTILS_DEBUG_LEVEL
148 Setting this environment variable to a non-zero numerical value makes
149 xdg-mime do more verbose reporting on stderr. Setting a higher value
150 increases the verbosity.
151 XDG_UTILS_INSTALL_MODE
152 This environment variable can be used by the user or administrator to
153 override the installation mode. Valid values are user and system.
154
155 Exit Codes
156
157 An exit code of 0 indicates success while a non-zero exit code indicates
158 failure. The following failure codes can be returned:
159
160 1
161 Error in command line syntax.
162 2
163 One of the files passed on the command line did not exist.
164 3
165 A required tool could not be found.
166 4
167 The action failed.
168 5
169 No permission to read one of the files passed on the command line.
170
171 See Also
172
173 xdg-icon-resource(1), xdg-desktop-menu(1)
174
175 Examples
176
177 xdg-mime query filetype /tmp/foobar.png
178
179 Prints the MIME type of the file /tmp/foobar.png, in this case image/png
180
181 xdg-mime query default image/png
182
183 Prints the .desktop filename of the application which is registered to open PNG
184 files.
185
186 xdg-mime install shinythings-shiny.xml
187
188 Adds a file type description for "shiny"-files. "shinythings-" is used as the
189 vendor prefix. The file type description could look as folows.
190
191 shinythings-shiny.xml:
192
193 <?xml version="1.0"?>
194 <mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
195 <mime-type type="text/x-shiny">
196 <comment>Shiny new file type</comment>
197 <glob pattern="*.shiny"/>
198 <glob pattern="*.shi"/>
199 </mime-type>
200 </mime-info>
201
202 An icon for this new file type must also be installed, for example with:
203
204 xdg-icon-resource install --context mimetypes --size 64 shiny-file-icon.png text -x-shiny
205
206 _MANUALPAGE
207 }
208
209 usage()
210 {
211 cat << _USAGE
212 xdg-mime - command line tool for querying information about file type handling
213 and adding descriptions for new file types
214
215 Synopsis
216
217 xdg-mime query { filetype | default } ...
218
219 xdg-mime default application mimetype(s)
220
221 xdg-mime install [--mode mode] [--novendor] mimetypes-file
222
223 xdg-mime uninstall [--mode mode] mimetypes-file
224
225 xdg-mime { --help | --manual | --version }
226
227 _USAGE
228 }
229
230 #@xdg-utils-common@
231
232 #----------------------------------------------------------------------------
233 # Common utility functions included in all XDG wrapper scripts
234 #----------------------------------------------------------------------------
235
236 DEBUG()
237 {
238 [ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && return 0;
239 [ ${XDG_UTILS_DEBUG_LEVEL} -lt $1 ] && return 0;
240 shift
241 echo "$@" >&2
242 }
243
244 #-------------------------------------------------------------
245 # Exit script on successfully completing the desired operation
246
247 exit_success()
248 {
249 if [ $# -gt 0 ]; then
250 echo "$@"
251 echo
252 fi
253
254 exit 0
255 }
256
257
258 #-----------------------------------------
259 # Exit script on malformed arguments, not enough arguments
260 # or missing required option.
261 # prints usage information
262
263 exit_failure_syntax()
264 {
265 if [ $# -gt 0 ]; then
266 echo "xdg-mime: $@" >&2
267 echo "Try 'xdg-mime --help' for more information." >&2
268 else
269 usage
270 echo "Use 'man xdg-mime' or 'xdg-mime --manual' for additional info."
271 fi
272
273 exit 1
274 }
275
276 #-------------------------------------------------------------
277 # Exit script on missing file specified on command line
278
279 exit_failure_file_missing()
280 {
281 if [ $# -gt 0 ]; then
282 echo "xdg-mime: $@" >&2
283 fi
284
285 exit 2
286 }
287
288 #-------------------------------------------------------------
289 # Exit script on failure to locate necessary tool applications
290
291 exit_failure_operation_impossible()
292 {
293 if [ $# -gt 0 ]; then
294 echo "xdg-mime: $@" >&2
295 fi
296
297 exit 3
298 }
299
300 #-------------------------------------------------------------
301 # Exit script on failure returned by a tool application
302
303 exit_failure_operation_failed()
304 {
305 if [ $# -gt 0 ]; then
306 echo "xdg-mime: $@" >&2
307 fi
308
309 exit 4
310 }
311
312 #------------------------------------------------------------
313 # Exit script on insufficient permission to read a specified file
314
315 exit_failure_file_permission_read()
316 {
317 if [ $# -gt 0 ]; then
318 echo "xdg-mime: $@" >&2
319 fi
320
321 exit 5
322 }
323
324 #------------------------------------------------------------
325 # Exit script on insufficient permission to read a specified file
326
327 exit_failure_file_permission_write()
328 {
329 if [ $# -gt 0 ]; then
330 echo "xdg-mime: $@" >&2
331 fi
332
333 exit 6
334 }
335
336 check_input_file()
337 {
338 if [ ! -e "$1" ]; then
339 exit_failure_file_missing "file '$1' does not exist"
340 fi
341 if [ ! -r "$1" ]; then
342 exit_failure_file_permission_read "no permission to read file '$1'"
343 fi
344 }
345
346 check_vendor_prefix()
347 {
348 file_label="$2"
349 [ -n "$file_label" ] || file_label="filename"
350 file=`basename "$1"`
351 case "$file" in
352 [a-zA-Z]*-*)
353 return
354 ;;
355 esac
356
357 echo "xdg-mime: $file_label '$file' does not have a proper vendor prefix" >& 2
358 echo 'A vendor prefix consists of alpha characters ([a-zA-Z]) and is termina ted' >&2
359 echo 'with a dash ("-"). An example '"$file_label"' is '"'example-$file'" >& 2
360 echo "Use --novendor to override or 'xdg-mime --manual' for additional info. " >&2
361 exit 1
362 }
363
364 check_output_file()
365 {
366 # if the file exists, check if it is writeable
367 # if it does not exists, check if we are allowed to write on the directory
368 if [ -e "$1" ]; then
369 if [ ! -w "$1" ]; then
370 exit_failure_file_permission_write "no permission to write to file ' $1'"
371 fi
372 else
373 DIR=`dirname "$1"`
374 if [ ! -w "$DIR" -o ! -x "$DIR" ]; then
375 exit_failure_file_permission_write "no permission to create file '$1 '"
376 fi
377 fi
378 }
379
380 #----------------------------------------
381 # Checks for shared commands, e.g. --help
382
383 check_common_commands()
384 {
385 while [ $# -gt 0 ] ; do
386 parm="$1"
387 shift
388
389 case "$parm" in
390 --help)
391 usage
392 echo "Use 'man xdg-mime' or 'xdg-mime --manual' for additional info. "
393 exit_success
394 ;;
395
396 --manual)
397 manualpage
398 exit_success
399 ;;
400
401 --version)
402 echo "xdg-mime 1.0.1"
403 exit_success
404 ;;
405 esac
406 done
407 }
408
409 check_common_commands "$@"
410
411 [ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && unset XDG_UTILS_DEBUG_LEVEL;
412 if [ ${XDG_UTILS_DEBUG_LEVEL-0} -lt 1 ]; then
413 # Be silent
414 xdg_redirect_output=" > /dev/null 2> /dev/null"
415 else
416 # All output to stderr
417 xdg_redirect_output=" >&2"
418 fi
419
420 #--------------------------------------
421 # Checks for known desktop environments
422 # set variable DE to the desktop environments name, lowercase
423
424 detectDE()
425 {
426 if [ x"$KDE_FULL_SESSION" = x"true" ]; then DE=kde;
427 elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then DE=gnome;
428 elif xprop -root _DT_SAVE_MODE | grep ' = \"xfce4\"$' >/dev/null 2>&1; then DE=xfce;
429 fi
430 }
431
432 #----------------------------------------------------------------------------
433 # kfmclient exec/openURL can give bogus exit value in KDE <= 3.5.4
434 # It also always returns 1 in KDE 3.4 and earlier
435 # Simply return 0 in such case
436
437 kfmclient_fix_exit_code()
438 {
439 version=`kde${KDE_SESSION_VERSION}-config --version 2>/dev/null | grep KDE`
440 major=`echo $version | sed 's/KDE: \([0-9]\).*/\1/'`
441 minor=`echo $version | sed 's/KDE: [0-9]*\.\([0-9]\).*/\1/'`
442 release=`echo $version | sed 's/KDE: [0-9]*\.[0-9]*\.\([0-9]\).*/\1/'`
443 test "$major" -gt 3 && return $1
444 test "$minor" -gt 5 && return $1
445 test "$release" -gt 4 && return $1
446 return 0
447 }
448
449 update_mime_database()
450 {
451 if [ x"$mode" = x"user" -a -n "$DISPLAY" ] ; then
452 detectDE
453 if [ x"$DE" = x"kde" ] ; then
454 DEBUG 1 "Running kbuildsycoca"
455 eval 'kbuildsycoca'$xdg_redirect_output
456 fi
457 fi
458 for x in `echo "$PATH:/opt/gnome/bin" | sed 's/:/ /g'`; do
459 if [ -x $x/update-mime-database ] ; then
460 DEBUG 1 "Running $x/update-mime-database $1"
461 eval '$x/update-mime-database $1'$xdg_redirect_output
462 return
463 fi
464 done
465 }
466
467 info_kde()
468 {
469 if which "kmimetypefinder" > /dev/null 2>/dev/null; then
470 DEBUG 1 "Running kmimetypefinder \"$1\""
471 kmimetypefinder "$1" 2>/dev/null | head -n 1
472 else
473 DEBUG 1 "Running kfile \"$1\""
474 kfile "$1" 2> /dev/null | head -n 1 | cut -d "(" -f 2 | cut -d ")" -f 1
475 fi
476
477 exit_success
478 }
479
480 info_gnome()
481 {
482 file=`readlink -f "$1"` # Normalize path
483 DEBUG 1 "Running gnomevfs-info \"$file\""
484 gnomevfs-info "$file" 2> /dev/null | grep MIME | cut -d ":" -f 2 | sed s/"^ "//
485
486 exit_success
487 }
488
489 info_generic()
490 {
491 DEBUG 1 "Running /usr/bin/file -i \"$1\""
492 /usr/bin/file -i "$1" 2> /dev/null | cut -d ":" -f 2 | sed s/"^ "//
493
494 exit_success
495 }
496
497 make_default_kde()
498 {
499 # $1 is vendor-name.desktop
500 # $2 is mime/type
501 # Add to $KDE_HOME/share/config/profilerc:
502 # [$2 - 1]
503 # Application=$1
504 #
505 # Remove all [$2 - *] sections, or even better,
506 # renumber [$2 - *] sections and remove duplicate
507
508 default_file="$HOME/.kde/share/config/profilerc"
509 DEBUG 2 "make_default_kde $1 $2"
510 DEBUG 1 "Updating $default_file"
511 mkdir -p "$HOME/.kde/share/config"
512 [ -f $default_file ] || touch $default_file
513 awk -v application="$1" -v mimetype="$2" '
514 BEGIN {
515 header_start="[" mimetype " - "
516 supress=0
517 }
518 {
519 if (index($0, header_start) == 1 )
520 supress=1
521 else
522 if (/^\[/) { supress=0 }
523
524 if (!supress) {
525 print $0
526 }
527 }
528 END {
529 print ""
530 print "[" mimetype " - 1]"
531 print "Application=" application
532 print "AllowAsDefault=true"
533 print "GenericServiceType=Application"
534 print "Preference=1"
535 print "ServiceType=" mimetype
536 }
537 ' $default_file > ${default_file}.new && mv ${default_file}.new $default_file
538 }
539
540 make_default_generic()
541 {
542 # $1 is vendor-name.desktop
543 # $2 is mime/type
544 # Add $2=$1 to XDG_DATA_HOME/applications/defaults.list
545 xdg_user_dir="$XDG_DATA_HOME"
546 [ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share"
547 default_file="$xdg_user_dir/applications/defaults.list"
548 DEBUG 2 "make_default_generic $1 $2"
549 DEBUG 1 "Updating $default_file"
550 grep -v "$2=" $default_file > ${default_file}.new 2> /dev/null
551 if ! grep "[Default Applications]" ${default_file}.new > /dev/null; then
552 echo "[Default Applications]" >> ${default_file}.new
553 fi
554 echo $2=$1 >> ${default_file}.new
555 mv ${default_file}.new $default_file
556 }
557
558 defapp_generic()
559 {
560 MIME="$1"
561 xdg_user_dir="$XDG_DATA_HOME"
562 [ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share"
563 xdg_user_dir="$xdg_user_dir/$xdg_dir_name"
564 xdg_system_dirs="$XDG_DATA_DIRS"
565 [ -n "$xdg_system_dirs" ] || xdg_system_dirs=/usr/local/share/:/usr/share/
566
567 for x in `echo "$xdg_user_dir:$xdg_system_dirs" | sed 's/:/ /g'`; do
568 DEBUG 2 "Checking $x/applications/defaults.list"
569 trader_result=`grep "$MIME=" $x/applications/defaults.list 2> /dev/null | cut -d '=' -f 2 | cut -d ';' -f 1`
570 if [ -n "$trader_result" ] ; then
571 echo $trader_result
572 exit_success
573 fi
574 done
575 exit_success
576 }
577
578 defapp_kde()
579 {
580 MIME="$1"
581 ktradertest=`which ktradertest 2> /dev/null`
582 if [ -n "$ktradertest" ] ; then
583 DEBUG 1 "Running ktradertest \"$MIME\" Application"
584 trader_result=`ktradertest "$MIME" Application 2>/dev/null | grep Deskto pEntryPath \
585 | head -n 1 | cut -d ':' -f 2 | cut -d \' -f 2`
586 if [ -n "$trader_result" ] ; then
587 basename "$trader_result"
588 exit_success
589 else
590 exit_failure_operation_failed
591 fi
592 else
593 defapp_generic "$1"
594 fi
595 }
596
597 [ x"$1" != x"" ] || exit_failure_syntax
598
599 mode=
600 action=
601 filename=
602 mimetype=
603
604 case $1 in
605 install)
606 action=install
607 ;;
608
609 uninstall)
610 action=uninstall
611 ;;
612
613 query)
614 shift
615
616 if [ -z "$1" ] ; then
617 exit_failure_syntax "query type argument missing"
618 fi
619
620 case $1 in
621 filetype)
622 action=info
623
624 filename="$2"
625 if [ -z "$filename" ] ; then
626 exit_failure_syntax "FILE argument missing"
627 fi
628 case $filename in
629 -*)
630 exit_failure_syntax "unexpected option '$filename'"
631 ;;
632 esac
633 check_input_file "$filename"
634 ;;
635
636 default)
637 action=defapp
638 mimetype="$2"
639 if [ -z "$mimetype" ] ; then
640 exit_failure_syntax "mimetype argument missing"
641 fi
642 case $mimetype in
643 -*)
644 exit_failure_syntax "unexpected option '$mimetype'"
645 ;;
646
647 */*)
648 # Ok
649 ;;
650
651 *)
652 exit_failure_syntax "mimetype '$mimetype' is not in the form 'minor/ major'"
653 ;;
654 esac
655 ;;
656
657 *)
658 exit_failure_syntax "unknown query type '$1'"
659 ;;
660 esac
661 ;;
662
663 default)
664 action=makedefault
665 shift
666
667 if [ -z "$1" ] ; then
668 exit_failure_syntax "application argument missing"
669 fi
670 case $1 in
671 -*)
672 exit_failure_syntax "unexpected option '$1'"
673 ;;
674
675 *.desktop)
676 filename="$1"
677 ;;
678
679 *)
680 exit_failure_syntax "malformed argument '$1', expected *.desktop"
681 ;;
682 esac
683 ;;
684
685 *)
686 exit_failure_syntax "unknown command '$1'"
687 ;;
688 esac
689
690 shift
691
692
693 if [ "$action" = "makedefault" ]; then
694 if [ -z "$1" ] ; then
695 exit_failure_syntax "mimetype argument missing"
696 fi
697
698 while [ $# -gt 0 ] ; do
699 case $1 in
700 -*)
701 exit_failure_syntax "unexpected option '$1'"
702 ;;
703 esac
704 mimetype="$1"
705 shift
706
707 make_default_kde "$filename" "$mimetype"
708 make_default_generic "$filename" "$mimetype"
709 done
710 exit_success
711 fi
712
713 if [ "$action" = "info" ]; then
714 detectDE
715
716 if [ x"$DE" = x"" ]; then
717 if [ -x /usr/bin/file ]; then
718 DE=generic
719 fi
720 fi
721
722 case "$DE" in
723 kde)
724 info_kde "$filename"
725 ;;
726
727 gnome)
728 info_gnome "$filename"
729 ;;
730
731 *)
732 info_generic "$filename"
733 ;;
734 esac
735 exit_failure_operation_impossible "no method available for quering MIME type of '$filename'"
736 fi
737
738 if [ "$action" = "defapp" ]; then
739 detectDE
740
741 case "$DE" in
742 kde)
743 defapp_kde "$mimetype"
744 ;;
745
746 *)
747 defapp_generic "$mimetype"
748 ;;
749 esac
750 exit_failure_operation_impossible "no method available for quering default a pplication for '$mimetype'"
751 fi
752
753 vendor=true
754 while [ $# -gt 0 ] ; do
755 parm="$1"
756 shift
757
758 case $parm in
759 --mode)
760 if [ -z "$1" ] ; then
761 exit_failure_syntax "mode argument missing for --mode"
762 fi
763 case "$1" in
764 user)
765 mode="user"
766 ;;
767
768 system)
769 mode="system"
770 ;;
771
772 *)
773 exit_failure_syntax "unknown mode '$1'"
774 ;;
775 esac
776 shift
777 ;;
778
779 --novendor)
780 vendor=false
781 ;;
782
783 -*)
784 exit_failure_syntax "unexpected option '$parm'"
785 ;;
786
787 *)
788 if [ -n "$filename" ] ; then
789 exit_failure_syntax "unexpected argument '$parm'"
790 fi
791
792 filename="$parm"
793 check_input_file "$filename"
794 ;;
795 esac
796 done
797
798 if [ -z "$action" ] ; then
799 exit_failure_syntax "command argument missing"
800 fi
801
802 if [ -n "$XDG_UTILS_INSTALL_MODE" ] ; then
803 if [ "$XDG_UTILS_INSTALL_MODE" = "system" ] ; then
804 mode="system"
805 elif [ "$XDG_UTILS_INSTALL_MODE" = "user" ] ; then
806 mode="user"
807 fi
808 fi
809
810 if [ -z "$mode" ] ; then
811 if [ `whoami` = "root" ] ; then
812 mode="system"
813 else
814 mode="user"
815 fi
816 fi
817
818 if [ -z "$filename" ] ; then
819 exit_failure_syntax "mimetypes-file argument missing"
820 fi
821
822 if [ "$vendor" = "true" -a "$action" = "install" ] ; then
823 check_vendor_prefix "$filename"
824 fi
825
826 xdg_base_dir=
827 xdg_dir_name=mime/packages/
828
829 xdg_user_dir="$XDG_DATA_HOME"
830 [ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share"
831 [ x"$mode" = x"user" ] && xdg_base_dir="$xdg_user_dir/mime"
832 xdg_user_dir="$xdg_user_dir/$xdg_dir_name"
833
834 xdg_system_dirs="$XDG_DATA_DIRS"
835 [ -n "$xdg_system_dirs" ] || xdg_system_dirs=/usr/local/share/:/usr/share/
836 for x in `echo $xdg_system_dirs | sed 's/:/ /g'`; do
837 if [ -w $x/$xdg_dir_name ] ; then
838 [ x"$mode" = x"system" ] && xdg_base_dir="$x/mime"
839 xdg_global_dir="$x/$xdg_dir_name"
840 break
841 fi
842 done
843 [ -w $xdg_global_dir ] || xdg_global_dir=
844 DEBUG 3 "xdg_user_dir: $xdg_user_dir"
845 DEBUG 3 "xdg_global_dir: $xdg_global_dir"
846
847 # Find KDE3 mimelnk directory
848 kde_user_dir=
849 kde_global_dir=
850 kde_global_dirs=`kde${KDE_SESSION_VERSION}-config --path mime 2> /dev/null`
851 DEBUG 3 "kde_global_dirs: $kde_global_dirs"
852 first=
853 for x in `echo $kde_global_dirs | sed 's/:/ /g'` ; do
854 if [ -z "$first" ] ; then
855 first=false
856 kde_user_dir="$x"
857 elif [ -w $x ] ; then
858 kde_global_dir="$x"
859 fi
860 done
861 DEBUG 3 "kde_user_dir: $kde_user_dir"
862 DEBUG 3 "kde_global_dir: $kde_global_dir"
863
864 # TODO: Gnome legacy support
865 # See http://forums.fedoraforum.org/showthread.php?t=26875
866 gnome_user_dir="$HOME/.gnome/apps"
867 gnome_global_dir=/usr/share/gnome/apps
868 [ -w $gnome_global_dir ] || gnome_global_dir=
869 DEBUG 3 "gnome_user_dir: $gnome_user_dir"
870 DEBUG 3 "gnome_global_dir: $gnome_global_dir"
871
872 if [ x"$mode" = x"user" ] ; then
873 xdg_dir="$xdg_user_dir"
874 kde_dir="$kde_user_dir"
875 gnome_dir="$gnome_user_dir"
876 my_umask=077
877 else
878 xdg_dir="$xdg_global_dir"
879 kde_dir="$kde_global_dir"
880 gnome_dir="$gnome_global_dir"
881 my_umask=022
882 if [ -z "${xdg_dir}${kde_dir}${gnome_dir}" ] ; then
883 exit_failure_operation_impossible "No writable system mimetype directory found."
884 fi
885 fi
886
887 # echo "[xdg|$xdg_user_dir|$xdg_global_dir]"
888 # echo "[kde|$kde_user_dir|$kde_global_dir]"
889 # echo "[gnome|$gnome_user_dir|$gnome_global_dir]"
890 # echo "[using|$xdg_dir|$kde_dir|$gnome_dir]"
891
892 basefile=`basename "$filename"`
893 #[ -z $vendor ] || basefile="$vendor-$basefile"
894
895 mimetypes=
896 if [ -n "$kde_dir" ] ; then
897 DEBUG 2 "KDE3 mimelnk directory found, extracting mimetypes from XML file"
898
899 mimetypes=`awk < "$filename" '
900 # Strip XML comments
901 BEGIN {
902 supress=0
903 }
904 {
905 do
906 if (supress) {
907 if (match($0,/-->/)) {
908 $0=substr($0,RSTART+RLENGTH)
909 supress=0
910 }
911 else {
912 break
913 }
914 }
915 else {
916 if (match($0,/<!--/)) {
917 if (RSTART>1) print substr($0,0,RSTART)
918 $0=substr($0,RSTART+RLENGTH)
919 supress=1
920 }
921 else {
922 if ($0) print $0
923 break
924 }
925 }
926 while(1)
927 }
928 ' | awk '
929 # List MIME types listed in <mime-type> tags
930 BEGIN {
931 RS="<"
932 }
933 /^mime-info/, /^\/mime-info/ {
934 if (match($0,/^mime-type/)) {
935 if (match($0,/type="[^"]*/) || match($0,/type='"'"'[^'"'"']*/)) {
936 print substr($0,RSTART+6,RLENGTH-6)
937 }
938 }
939 }'`
940 fi
941
942 DEBUG 1 "$action mimetype in $xdg_dir"
943
944 case $action in
945 install)
946 save_umask=`umask`
947 umask $my_umask
948
949 for x in $xdg_dir ; do
950 mkdir -p $x
951 eval 'cp $filename $x/$basefile'$xdg_redirect_output
952 done
953
954 if [ -n "$mimetypes" ] ; then
955 # No quotes around $mimetypes
956 for x in $mimetypes ; do
957 DEBUG 1 "Installing $kde_dir/$x.desktop (KDE 3.x support)"
958 mkdir -p `dirname $kde_dir/$x.desktop`
959 awk < "$filename" '
960 # Strip XML comments
961 BEGIN {
962 supress=0
963 }
964 {
965 do
966 if (supress) {
967 if (match($0,/-->/)) {
968 $0=substr($0,RSTART+RLENGTH)
969 supress=0
970 }
971 else {
972 break
973 }
974 }
975 else {
976 if (match($0,/<!--/)) {
977 if (RSTART>1) print substr($0,0,RSTART)
978 $0=substr($0,RSTART+RLENGTH)
979 supress=1
980 }
981 else {
982 if ($0) print $0
983 break
984 }
985 }
986 while(1)
987 }
988 ' | awk > $kde_dir/$x.desktop '
989 # Extract mimetype $x from the XML file $filename
990 # Note that bash requires us to escape a single quote as '"'"'
991 BEGIN {
992 the_type=ARGV[1]
993 the_source=ARGV[2]
994 ARGC=1
995 RS="<"
996 found=0
997 glob_patterns=""
998 }
999 /^mime-info/, /^\/mime-info/ {
1000 if (match($0,/^mime-type/)) {
1001 if (match($0,/type="[^"]*/) || match($0,/type='"'"'[^'"'"']*/)) {
1002 if (substr($0,RSTART+6,RLENGTH-6) == the_type) {
1003 found=1
1004 print "[Desktop Entry]"
1005 print "# Installed by xdg-mime from " the_source
1006 print "Type=MimeType"
1007 print "MimeType=" the_type
1008 the_icon=the_type
1009 sub("/", "-", the_icon)
1010 print "Icon=" the_icon
1011 }
1012 }
1013 }
1014 else if (found) {
1015 if (match($0,/^\/mime-type/)) {
1016 if (glob_patterns)
1017 print "Patterns=" glob_patterns
1018 exit 0
1019 }
1020
1021 if (match($0,/^sub-class-of/)) {
1022 if (match($0,/type="[^"]*/) || match($0,/type='"'"'[^'"'"']*/)) {
1023 print "X-KDE-IsAlso=" substr($0,RSTART+6,RLENGTH-6)
1024 }
1025 else {
1026 print "Error: '"'"'type'"'"' argument missing in " RS $0
1027 exit 1
1028 }
1029 }
1030 if (match($0,/^glob/)) {
1031 if (match($0,/pattern="[^"]*/) || match($0,/pattern='"'"'[^'"'"']*/)) {
1032 glob_patterns = glob_patterns substr($0,RSTART+9,RLENGTH-9) ";"
1033 }
1034 else {
1035 print "Error: '"'"'pattern'"'"' argument missing in " RS $0
1036 exit 1
1037 }
1038 }
1039 if (match($0,/^comment/)) {
1040 if (match($0,/xml:lang="[^"]*/) || match($0,/xml:lang='"'"'[^'"'"']*/)) {
1041 lang=substr($0,RSTART+10,RLENGTH-10)
1042 }
1043 else {
1044 lang=""
1045 }
1046 if (match($0,/>/)) {
1047 comment=substr($0,RSTART+1)
1048 sub("&lt;", "<", comment)
1049 sub("&gt;", ">", comment)
1050 sub("&amp;", "\\&", comment)
1051 if (lang)
1052 print "Comment[" lang "]=" comment
1053 else
1054 print "Comment=" comment
1055 }
1056 }
1057 }
1058 }
1059 END {
1060 if (!found) {
1061 print "Error: Mimetype '"'"'" the_type "'"'"' not found"
1062 exit 1
1063 }
1064 }
1065 ' $x $basefile
1066 if [ "$?" = "1" ] ; then
1067 grep -A 10 "^Error:" $kde_dir/$x.desktop >&2
1068 rm $kde_dir/$x.desktop
1069 exit 1
1070 fi
1071 done
1072 fi
1073
1074 umask $save_umask
1075 ;;
1076
1077 uninstall)
1078 for x in $xdg_dir ; do
1079 rm -f $x/$basefile
1080 done
1081
1082 # No quotes around $mimetypes
1083 for x in $mimetypes ; do
1084 if grep '^# Installed by xdg-mime' $kde_dir/$x.desktop &>/dev/null ; then
1085 DEBUG 1 "Removing $kde_dir/$x.desktop (KDE 3.x support)"
1086 rm -f $kde_dir/$x.desktop
1087 fi
1088 done
1089 ;;
1090 esac
1091
1092 update_mime_database $xdg_base_dir
1093
1094 exit_success
1095
OLDNEW
« no previous file with comments | « third_party/xdg-utils/scripts/xdg-icon-resource.in ('k') | third_party/xdg-utils/scripts/xdg-mime.in » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698