OLD | NEW |
(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 2009-2010, Fathi Boudra <fabo@freedesktop.org> |
| 11 # Copyright 2009-2010, Rex Dieter <rdieter@fedoraproject.org> |
| 12 # Copyright 2006, Kevin Krammer <kevin.krammer@gmx.at> |
| 13 # Copyright 2006, Jeremy White <jwhite@codeweavers.com> |
| 14 # |
| 15 # LICENSE: |
| 16 # |
| 17 # Permission is hereby granted, free of charge, to any person obtaining a |
| 18 # copy of this software and associated documentation files (the "Software"), |
| 19 # to deal in the Software without restriction, including without limitation |
| 20 # the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 21 # and/or sell copies of the Software, and to permit persons to whom the |
| 22 # Software is furnished to do so, subject to the following conditions: |
| 23 # |
| 24 # The above copyright notice and this permission notice shall be included |
| 25 # in all copies or substantial portions of the Software. |
| 26 # |
| 27 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 28 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 29 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 30 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 31 # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 32 # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 33 # OTHER DEALINGS IN THE SOFTWARE. |
| 34 # |
| 35 #--------------------------------------------- |
| 36 |
| 37 manualpage() |
| 38 { |
| 39 cat << _MANUALPAGE |
| 40 Name |
| 41 |
| 42 xdg-mime - command line tool for querying information about file type handling |
| 43 and adding descriptions for new file types |
| 44 |
| 45 Synopsis |
| 46 |
| 47 xdg-mime query { filetype | default } ... |
| 48 |
| 49 xdg-mime default application mimetype(s) |
| 50 |
| 51 xdg-mime install [--mode mode] [--novendor] mimetypes-file |
| 52 |
| 53 xdg-mime uninstall [--mode mode] mimetypes-file |
| 54 |
| 55 xdg-mime { --help | --manual | --version } |
| 56 |
| 57 Description |
| 58 |
| 59 The xdg-mime program can be used to query information about file types and to |
| 60 add descriptions for new file types. |
| 61 |
| 62 Commands |
| 63 |
| 64 query |
| 65 |
| 66 Returns information related to file types. |
| 67 |
| 68 The query option is for use inside a desktop session only. It is not |
| 69 recommended to use xdg-mime query as root. |
| 70 |
| 71 The following queries are supported: |
| 72 |
| 73 query filetype FILE: Returns the file type of FILE in the form of a MIME |
| 74 type. |
| 75 |
| 76 query default mimetype: Returns the default application that the desktop |
| 77 environment uses for opening files of type mimetype. The default |
| 78 application is identified by its *.desktop file. |
| 79 |
| 80 default |
| 81 |
| 82 Ask the desktop environment to make application the default application for |
| 83 opening files of type mimetype. An application can be made the default for |
| 84 several file types by specifying multiple mimetypes. |
| 85 |
| 86 application is the desktop file id of the application and has the form |
| 87 vendor-name.desktop application must already be installed in the desktop |
| 88 menu before it can be made the default handler. The aplication's desktop |
| 89 file must list support for all the MIME types that it wishes to be the |
| 90 default handler for. |
| 91 |
| 92 Requests to make an application a default handler may be subject to system |
| 93 policy or approval by the end-user. xdg-mime query can be used to verify |
| 94 whether an application is the actual default handler for a specific file |
| 95 type. |
| 96 |
| 97 The default option is for use inside a desktop session only. It is not |
| 98 recommended to use xdg-mime default as root. |
| 99 |
| 100 install |
| 101 Adds the file type descriptions provided in mimetypes-file to the desktop |
| 102 environment. mimetypes-file must be a XML file that follows the |
| 103 freedesktop.org Shared MIME-info Database specification and that has a |
| 104 mime-info element as its document root. For each new file type one or more |
| 105 icons with name type-subtype must be installed with the xdg-icon-resource |
| 106 command in the mimetypes context. For example the filetype application/ |
| 107 vnd.oasis.opendocument.text requires an icon named |
| 108 application-vnd.oasis.opendocument.text to be installed (unless the file |
| 109 type recommends another icon name). |
| 110 uninstall |
| 111 Removes the file type descriptions provided in mimetypes-file and |
| 112 previously added with xdg-mime install from the desktop environment. |
| 113 mimetypes-file must be a XML file that follows the freedesktop.org Shared |
| 114 MIME-info Database specification and that has a mime-info element as its |
| 115 document root. |
| 116 |
| 117 Options |
| 118 |
| 119 --mode mode |
| 120 |
| 121 mode can be user or system. In user mode the file is (un)installed for the |
| 122 current user only. In system mode the file is (un)installed for all users |
| 123 on the system. Usually only root is allowed to install in system mode. |
| 124 |
| 125 The default is to use system mode when called by root and to use user mode |
| 126 when called by a non-root user. |
| 127 |
| 128 --novendor |
| 129 |
| 130 Normally, xdg-mime checks to ensure that the mimetypes-file to be installed |
| 131 has a proper vendor prefix. This option can be used to disable that check. |
| 132 |
| 133 A vendor prefix consists of alpha characters ([a-zA-Z]) and is terminated |
| 134 with a dash ("-"). Companies and organizations are encouraged to use a word |
| 135 or phrase, preferably the organizations name, for which they hold a |
| 136 trademark as their vendor prefix. The purpose of the vendor prefix is to |
| 137 prevent name conflicts. |
| 138 |
| 139 --help |
| 140 Show command synopsis. |
| 141 --manual |
| 142 Show this manualpage. |
| 143 --version |
| 144 Show the xdg-utils version information. |
| 145 |
| 146 Environment Variables |
| 147 |
| 148 xdg-mime honours the following environment variables: |
| 149 |
| 150 XDG_UTILS_DEBUG_LEVEL |
| 151 Setting this environment variable to a non-zero numerical value makes |
| 152 xdg-mime do more verbose reporting on stderr. Setting a higher value |
| 153 increases the verbosity. |
| 154 XDG_UTILS_INSTALL_MODE |
| 155 This environment variable can be used by the user or administrator to |
| 156 override the installation mode. Valid values are user and system. |
| 157 |
| 158 Exit Codes |
| 159 |
| 160 An exit code of 0 indicates success while a non-zero exit code indicates |
| 161 failure. The following failure codes can be returned: |
| 162 |
| 163 1 |
| 164 Error in command line syntax. |
| 165 2 |
| 166 One of the files passed on the command line did not exist. |
| 167 3 |
| 168 A required tool could not be found. |
| 169 4 |
| 170 The action failed. |
| 171 5 |
| 172 No permission to read one of the files passed on the command line. |
| 173 |
| 174 See Also |
| 175 |
| 176 xdg-icon-resource(1), xdg-desktop-menu(1) |
| 177 |
| 178 Examples |
| 179 |
| 180 xdg-mime query filetype /tmp/foobar.png |
| 181 |
| 182 Prints the MIME type of the file /tmp/foobar.png, in this case image/png |
| 183 |
| 184 xdg-mime query default image/png |
| 185 |
| 186 Prints the .desktop filename of the application which is registered to open PNG |
| 187 files. |
| 188 |
| 189 xdg-mime install shinythings-shiny.xml |
| 190 |
| 191 Adds a file type description for "shiny"-files. "shinythings-" is used as the |
| 192 vendor prefix. The file type description could look as folows. |
| 193 |
| 194 shinythings-shiny.xml: |
| 195 |
| 196 <?xml version="1.0"?> |
| 197 <mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'> |
| 198 <mime-type type="text/x-shiny"> |
| 199 <comment>Shiny new file type</comment> |
| 200 <glob pattern="*.shiny"/> |
| 201 <glob pattern="*.shi"/> |
| 202 </mime-type> |
| 203 </mime-info> |
| 204 |
| 205 An icon for this new file type must also be installed, for example with: |
| 206 |
| 207 xdg-icon-resource install --context mimetypes --size 64 shiny-file-icon.png text
-x-shiny |
| 208 |
| 209 _MANUALPAGE |
| 210 } |
| 211 |
| 212 usage() |
| 213 { |
| 214 cat << _USAGE |
| 215 xdg-mime - command line tool for querying information about file type handling |
| 216 and adding descriptions for new file types |
| 217 |
| 218 Synopsis |
| 219 |
| 220 xdg-mime query { filetype | default } ... |
| 221 |
| 222 xdg-mime default application mimetype(s) |
| 223 |
| 224 xdg-mime install [--mode mode] [--novendor] mimetypes-file |
| 225 |
| 226 xdg-mime uninstall [--mode mode] mimetypes-file |
| 227 |
| 228 xdg-mime { --help | --manual | --version } |
| 229 |
| 230 _USAGE |
| 231 } |
| 232 |
| 233 #@xdg-utils-common@ |
| 234 |
| 235 #---------------------------------------------------------------------------- |
| 236 # Common utility functions included in all XDG wrapper scripts |
| 237 #---------------------------------------------------------------------------- |
| 238 |
| 239 DEBUG() |
| 240 { |
| 241 [ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && return 0; |
| 242 [ ${XDG_UTILS_DEBUG_LEVEL} -lt $1 ] && return 0; |
| 243 shift |
| 244 echo "$@" >&2 |
| 245 } |
| 246 |
| 247 # This handles backslashes but not quote marks. |
| 248 first_word() |
| 249 { |
| 250 read first rest |
| 251 echo "$first" |
| 252 } |
| 253 |
| 254 #------------------------------------------------------------- |
| 255 # map a binary to a .desktop file |
| 256 binary_to_desktop_file() |
| 257 { |
| 258 search="${XDG_DATA_HOME:-$HOME/.local/share}:${XDG_DATA_DIRS:-/usr/local/sha
re:/usr/share}" |
| 259 binary="`which "$1"`" |
| 260 binary="`readlink -f "$binary"`" |
| 261 base="`basename "$binary"`" |
| 262 IFS=: |
| 263 for dir in $search; do |
| 264 unset IFS |
| 265 [ "$dir" ] || continue |
| 266 [ -d "$dir/applications" -o -d "$dir/applnk" ] || continue |
| 267 for file in "$dir"/applications/*.desktop "$dir"/applications/*/*.deskto
p "$dir"/applnk/*.desktop "$dir"/applnk/*/*.desktop; do |
| 268 [ -r "$file" ] || continue |
| 269 # Check to make sure it's worth the processing. |
| 270 grep -q "^Exec.*$base" "$file" || continue |
| 271 # Make sure it's a visible desktop file (e.g. not "preferred-web-bro
wser.desktop"). |
| 272 grep -Eq "^(NoDisplay|Hidden)=true" "$file" && continue |
| 273 command="`grep -E "^Exec(\[[^]=]*])?=" "$file" | cut -d= -f 2- | fir
st_word`" |
| 274 command="`which "$command"`" |
| 275 if [ x"`readlink -f "$command"`" = x"$binary" ]; then |
| 276 # Fix any double slashes that got added path composition |
| 277 echo "$file" | sed -e 's,//*,/,g' |
| 278 return |
| 279 fi |
| 280 done |
| 281 done |
| 282 } |
| 283 |
| 284 #------------------------------------------------------------- |
| 285 # map a .desktop file to a binary |
| 286 ## FIXME: handle vendor dir case |
| 287 desktop_file_to_binary() |
| 288 { |
| 289 search="${XDG_DATA_HOME:-$HOME/.local/share}:${XDG_DATA_DIRS:-/usr/local/sha
re:/usr/share}" |
| 290 desktop="`basename "$1"`" |
| 291 IFS=: |
| 292 for dir in $search; do |
| 293 unset IFS |
| 294 [ "$dir" -a -d "$dir/applications" ] || continue |
| 295 file="$dir/applications/$desktop" |
| 296 [ -r "$file" ] || continue |
| 297 # Remove any arguments (%F, %f, %U, %u, etc.). |
| 298 command="`grep -E "^Exec(\[[^]=]*])?=" "$file" | cut -d= -f 2- | first_w
ord`" |
| 299 command="`which "$command"`" |
| 300 readlink -f "$command" |
| 301 return |
| 302 done |
| 303 } |
| 304 |
| 305 #------------------------------------------------------------- |
| 306 # Exit script on successfully completing the desired operation |
| 307 |
| 308 exit_success() |
| 309 { |
| 310 if [ $# -gt 0 ]; then |
| 311 echo "$@" |
| 312 echo |
| 313 fi |
| 314 |
| 315 exit 0 |
| 316 } |
| 317 |
| 318 |
| 319 #----------------------------------------- |
| 320 # Exit script on malformed arguments, not enough arguments |
| 321 # or missing required option. |
| 322 # prints usage information |
| 323 |
| 324 exit_failure_syntax() |
| 325 { |
| 326 if [ $# -gt 0 ]; then |
| 327 echo "xdg-mime: $@" >&2 |
| 328 echo "Try 'xdg-mime --help' for more information." >&2 |
| 329 else |
| 330 usage |
| 331 echo "Use 'man xdg-mime' or 'xdg-mime --manual' for additional info." |
| 332 fi |
| 333 |
| 334 exit 1 |
| 335 } |
| 336 |
| 337 #------------------------------------------------------------- |
| 338 # Exit script on missing file specified on command line |
| 339 |
| 340 exit_failure_file_missing() |
| 341 { |
| 342 if [ $# -gt 0 ]; then |
| 343 echo "xdg-mime: $@" >&2 |
| 344 fi |
| 345 |
| 346 exit 2 |
| 347 } |
| 348 |
| 349 #------------------------------------------------------------- |
| 350 # Exit script on failure to locate necessary tool applications |
| 351 |
| 352 exit_failure_operation_impossible() |
| 353 { |
| 354 if [ $# -gt 0 ]; then |
| 355 echo "xdg-mime: $@" >&2 |
| 356 fi |
| 357 |
| 358 exit 3 |
| 359 } |
| 360 |
| 361 #------------------------------------------------------------- |
| 362 # Exit script on failure returned by a tool application |
| 363 |
| 364 exit_failure_operation_failed() |
| 365 { |
| 366 if [ $# -gt 0 ]; then |
| 367 echo "xdg-mime: $@" >&2 |
| 368 fi |
| 369 |
| 370 exit 4 |
| 371 } |
| 372 |
| 373 #------------------------------------------------------------ |
| 374 # Exit script on insufficient permission to read a specified file |
| 375 |
| 376 exit_failure_file_permission_read() |
| 377 { |
| 378 if [ $# -gt 0 ]; then |
| 379 echo "xdg-mime: $@" >&2 |
| 380 fi |
| 381 |
| 382 exit 5 |
| 383 } |
| 384 |
| 385 #------------------------------------------------------------ |
| 386 # Exit script on insufficient permission to write a specified file |
| 387 |
| 388 exit_failure_file_permission_write() |
| 389 { |
| 390 if [ $# -gt 0 ]; then |
| 391 echo "xdg-mime: $@" >&2 |
| 392 fi |
| 393 |
| 394 exit 6 |
| 395 } |
| 396 |
| 397 check_input_file() |
| 398 { |
| 399 if [ ! -e "$1" ]; then |
| 400 exit_failure_file_missing "file '$1' does not exist" |
| 401 fi |
| 402 if [ ! -r "$1" ]; then |
| 403 exit_failure_file_permission_read "no permission to read file '$1'" |
| 404 fi |
| 405 } |
| 406 |
| 407 check_vendor_prefix() |
| 408 { |
| 409 file_label="$2" |
| 410 [ -n "$file_label" ] || file_label="filename" |
| 411 file=`basename "$1"` |
| 412 case "$file" in |
| 413 [a-zA-Z]*-*) |
| 414 return |
| 415 ;; |
| 416 esac |
| 417 |
| 418 echo "xdg-mime: $file_label '$file' does not have a proper vendor prefix" >&
2 |
| 419 echo 'A vendor prefix consists of alpha characters ([a-zA-Z]) and is termina
ted' >&2 |
| 420 echo 'with a dash ("-"). An example '"$file_label"' is '"'example-$file'" >&
2 |
| 421 echo "Use --novendor to override or 'xdg-mime --manual' for additional info.
" >&2 |
| 422 exit 1 |
| 423 } |
| 424 |
| 425 check_output_file() |
| 426 { |
| 427 # if the file exists, check if it is writeable |
| 428 # if it does not exists, check if we are allowed to write on the directory |
| 429 if [ -e "$1" ]; then |
| 430 if [ ! -w "$1" ]; then |
| 431 exit_failure_file_permission_write "no permission to write to file '
$1'" |
| 432 fi |
| 433 else |
| 434 DIR=`dirname "$1"` |
| 435 if [ ! -w "$DIR" -o ! -x "$DIR" ]; then |
| 436 exit_failure_file_permission_write "no permission to create file '$1
'" |
| 437 fi |
| 438 fi |
| 439 } |
| 440 |
| 441 #---------------------------------------- |
| 442 # Checks for shared commands, e.g. --help |
| 443 |
| 444 check_common_commands() |
| 445 { |
| 446 while [ $# -gt 0 ] ; do |
| 447 parm="$1" |
| 448 shift |
| 449 |
| 450 case "$parm" in |
| 451 --help) |
| 452 usage |
| 453 echo "Use 'man xdg-mime' or 'xdg-mime --manual' for additional info.
" |
| 454 exit_success |
| 455 ;; |
| 456 |
| 457 --manual) |
| 458 manualpage |
| 459 exit_success |
| 460 ;; |
| 461 |
| 462 --version) |
| 463 echo "xdg-mime 1.1.0 rc1" |
| 464 exit_success |
| 465 ;; |
| 466 esac |
| 467 done |
| 468 } |
| 469 |
| 470 check_common_commands "$@" |
| 471 |
| 472 [ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && unset XDG_UTILS_DEBUG_LEVEL; |
| 473 if [ ${XDG_UTILS_DEBUG_LEVEL-0} -lt 1 ]; then |
| 474 # Be silent |
| 475 xdg_redirect_output=" > /dev/null 2> /dev/null" |
| 476 else |
| 477 # All output to stderr |
| 478 xdg_redirect_output=" >&2" |
| 479 fi |
| 480 |
| 481 #-------------------------------------- |
| 482 # Checks for known desktop environments |
| 483 # set variable DE to the desktop environments name, lowercase |
| 484 |
| 485 detectDE() |
| 486 { |
| 487 # see https://bugs.freedesktop.org/show_bug.cgi?id=34164 |
| 488 unset GREP_OPTIONS |
| 489 |
| 490 if [ x"$KDE_FULL_SESSION" = x"true" ]; then DE=kde; |
| 491 elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then DE=gnome; |
| 492 elif `dbus-send --print-reply --dest=org.freedesktop.DBus /org/freedesktop/D
Bus org.freedesktop.DBus.GetNameOwner string:org.gnome.SessionManager > /dev/nul
l 2>&1` ; then DE=gnome; |
| 493 elif xprop -root _DT_SAVE_MODE 2> /dev/null | grep ' = \"xfce4\"$' >/dev/nul
l 2>&1; then DE=xfce; |
| 494 elif xprop -root 2> /dev/null | grep -i '^xfce_desktop_window' >/dev/null 2>
&1; then DE=xfce |
| 495 fi |
| 496 |
| 497 if [ x"$DE" = x"" ]; then |
| 498 # fallback to checking $DESKTOP_SESSION |
| 499 case "$DESKTOP_SESSION" in |
| 500 gnome) |
| 501 DE=gnome; |
| 502 ;; |
| 503 LXDE) |
| 504 DE=lxde; |
| 505 ;; |
| 506 xfce|xfce4) |
| 507 DE=xfce; |
| 508 ;; |
| 509 esac |
| 510 fi |
| 511 |
| 512 if [ x"$DE" = x"" ]; then |
| 513 # fallback to uname output for other platforms |
| 514 case "$(uname 2>/dev/null)" in |
| 515 Darwin) |
| 516 DE=darwin; |
| 517 ;; |
| 518 esac |
| 519 fi |
| 520 |
| 521 if [ x"$DE" = x"gnome" ]; then |
| 522 # gnome-default-applications-properties is only available in GNOME 2.x |
| 523 # but not in GNOME 3.x |
| 524 which gnome-default-applications-properties > /dev/null 2>&1 || DE="gnome
3" |
| 525 fi |
| 526 } |
| 527 |
| 528 #---------------------------------------------------------------------------- |
| 529 # kfmclient exec/openURL can give bogus exit value in KDE <= 3.5.4 |
| 530 # It also always returns 1 in KDE 3.4 and earlier |
| 531 # Simply return 0 in such case |
| 532 |
| 533 kfmclient_fix_exit_code() |
| 534 { |
| 535 version=`LC_ALL=C.UTF-8 kde-config --version 2>/dev/null | grep '^KDE'` |
| 536 major=`echo $version | sed 's/KDE.*: \([0-9]\).*/\1/'` |
| 537 minor=`echo $version | sed 's/KDE.*: [0-9]*\.\([0-9]\).*/\1/'` |
| 538 release=`echo $version | sed 's/KDE.*: [0-9]*\.[0-9]*\.\([0-9]\).*/\1/'` |
| 539 test "$major" -gt 3 && return $1 |
| 540 test "$minor" -gt 5 && return $1 |
| 541 test "$release" -gt 4 && return $1 |
| 542 return 0 |
| 543 } |
| 544 |
| 545 update_mime_database() |
| 546 { |
| 547 if [ x"$mode" = x"user" -a -n "$DISPLAY" ] ; then |
| 548 detectDE |
| 549 if [ x"$DE" = x"kde" ] ; then |
| 550 DEBUG 1 "Running kbuildsycoca" |
| 551 if [ x"$KDE_SESSION_VERSION" = x"4" ]; then |
| 552 eval 'kbuildsycoca4'$xdg_redirect_output |
| 553 else |
| 554 eval 'kbuildsycoca'$xdg_redirect_output |
| 555 fi |
| 556 fi |
| 557 fi |
| 558 for x in `echo "$PATH:/opt/gnome/bin" | sed 's/:/ /g'`; do |
| 559 if [ -x $x/update-mime-database ] ; then |
| 560 DEBUG 1 "Running $x/update-mime-database $1" |
| 561 eval '$x/update-mime-database $1'$xdg_redirect_output |
| 562 return |
| 563 fi |
| 564 done |
| 565 } |
| 566 |
| 567 info_kde() |
| 568 { |
| 569 if [ x"$KDE_SESSION_VERSION" = x"4" ]; then |
| 570 DEBUG 1 "Running kmimetypefinder \"$1\"" |
| 571 kmimetypefinder "$1" 2>/dev/null | head -n 1 |
| 572 else |
| 573 DEBUG 1 "Running kfile \"$1\"" |
| 574 kfile "$1" 2> /dev/null | head -n 1 | cut -d "(" -f 2 | cut -d ")" -f 1 |
| 575 fi |
| 576 |
| 577 if [ $? -eq 0 ]; then |
| 578 exit_success |
| 579 else |
| 580 exit_failure_operation_failed |
| 581 fi |
| 582 } |
| 583 |
| 584 info_gnome() |
| 585 { |
| 586 if gvfs-info --help 2>/dev/null 1>&2; then |
| 587 DEBUG 1 "Running gvfs-info \"$1\"" |
| 588 gvfs-info "$1" 2> /dev/null | grep standard::content-type | cut -d' ' -f
4 |
| 589 elif gnomevfs-info --help 2>/dev/null 1>&2; then |
| 590 DEBUG 1 "Running gnomevfs-info \"$1\"" |
| 591 gnomevfs-info --slow-mime "$1" 2> /dev/null | grep "^MIME" | cut -d ":" -
f 2 | sed s/"^ "// |
| 592 else |
| 593 # according to https://bugs.freedesktop.org/show_bug.cgi?id=33094#c5 |
| 594 # neither gvfs-info or gnomevfs-info are present in a default Ubuntu Natt
y |
| 595 # install, so fallback to info_generic |
| 596 info_generic "$1" |
| 597 fi |
| 598 |
| 599 if [ $? -eq 0 ]; then |
| 600 exit_success |
| 601 else |
| 602 exit_failure_operation_failed |
| 603 fi |
| 604 } |
| 605 |
| 606 info_generic() |
| 607 { |
| 608 if mimetype --version >/dev/null 2>&1; then |
| 609 DEBUG 1 "Running mimetype -b \"$1\"" |
| 610 mimetype -b "$1" |
| 611 else |
| 612 DEBUG 1 "Running file -i \"$1\"" |
| 613 /usr/bin/file -i "$1" 2> /dev/null | cut -d ":" -f 2 | sed s/"^ "// |
| 614 fi |
| 615 |
| 616 if [ $? -eq 0 ]; then |
| 617 exit_success |
| 618 else |
| 619 exit_failure_operation_failed |
| 620 fi |
| 621 } |
| 622 |
| 623 make_default_kde() |
| 624 { |
| 625 # $1 is vendor-name.desktop |
| 626 # $2 is mime/type |
| 627 # |
| 628 # On KDE 3, add to $KDE_CONFIG_PATH/profilerc: |
| 629 # [$2 - 1] |
| 630 # Application=$1 |
| 631 # |
| 632 # Remove all [$2 - *] sections, or even better, |
| 633 # renumber [$2 - *] sections and remove duplicate |
| 634 # |
| 635 # On KDE 4, add $2=$1 to $XDG_DATA_APPS/mimeapps.list |
| 636 # |
| 637 # Example file: |
| 638 # |
| 639 # [Added Associations] |
| 640 # text/plain=kde4-kate.desktop;kde4-kwrite.desktop; |
| 641 # |
| 642 # [Removed Associations] |
| 643 # text/plain=gnome-gedit.desktop;gnu-emacs.desktop; |
| 644 vendor="$1" |
| 645 mimetype="$2" |
| 646 if [ x"$KDE_SESSION_VERSION" = x"4" ]; then |
| 647 default_dir=`kde4-config --path xdgdata-apps 2> /dev/null | cut -d ':' -
f 1` |
| 648 default_file="$default_dir/mimeapps.list" |
| 649 else |
| 650 default_dir=`kde-config --path config 2> /dev/null | cut -d ':' -f 1` |
| 651 default_file="$default_dir/profilerc" |
| 652 fi |
| 653 if [ -z "$default_dir" ]; then |
| 654 DEBUG 2 "make_default_kde: No kde runtime detected" |
| 655 return |
| 656 fi |
| 657 DEBUG 2 "make_default_kde $vendor $mimetype" |
| 658 DEBUG 1 "Updating $default_file" |
| 659 mkdir -p "$default_dir" |
| 660 [ -f "$default_file" ] || touch "$default_file" |
| 661 if [ x"$KDE_SESSION_VERSION" = x"4" ]; then |
| 662 [ -f "$default_file" ] || touch "$default_file" |
| 663 awk -v application="$vendor" -v mimetype="$mimetype" ' |
| 664 BEGIN { |
| 665 prefix=mimetype "=" |
| 666 associations=0 |
| 667 found=0 |
| 668 blanks=0 |
| 669 } |
| 670 { |
| 671 suppress=0 |
| 672 if (index($0, "[Added Associations]") == 1) { |
| 673 associations=1 |
| 674 } else if (index($0, "[") == 1) { |
| 675 if (associations && !found) { |
| 676 print prefix application |
| 677 found=1 |
| 678 } |
| 679 associations=0 |
| 680 } else if ($0 == "") { |
| 681 blanks++ |
| 682 suppress=1 |
| 683 } else if (associations && index($0, prefix) == 1) { |
| 684 value=substr($0, length(prefix) + 1, length) |
| 685 split(value, apps, ";") |
| 686 value=application ";" |
| 687 count=0 |
| 688 for (i in apps) { |
| 689 count++ |
| 690 } |
| 691 for (i=0; i < count; i++) { |
| 692 if (apps[i] != application && apps[i] != "") { |
| 693 value=value apps[i] ";" |
| 694 } |
| 695 } |
| 696 $0=prefix value |
| 697 found=1 |
| 698 } |
| 699 if (!suppress) { |
| 700 while (blanks > 0) { |
| 701 print "" |
| 702 blanks-- |
| 703 } |
| 704 print $0 |
| 705 } |
| 706 } |
| 707 END { |
| 708 if (!found) { |
| 709 if (!associations) { |
| 710 print "[Added Associations]" |
| 711 } |
| 712 print prefix application |
| 713 } |
| 714 while (blanks > 0) { |
| 715 print "" |
| 716 blanks-- |
| 717 } |
| 718 } |
| 719 ' "$default_file" > "${default_file}.new" && mv "${default_file}.new" "$default_
file" |
| 720 eval 'kbuildsycoca4'$xdg_redirect_output |
| 721 else |
| 722 awk -v application="$vendor" -v mimetype="$mimetype" ' |
| 723 BEGIN { |
| 724 header_start="[" mimetype " - " |
| 725 suppress=0 |
| 726 } |
| 727 { |
| 728 if (index($0, header_start) == 1 ) |
| 729 suppress=1 |
| 730 else |
| 731 if (/^\[/) { suppress=0 } |
| 732 |
| 733 if (!suppress) { |
| 734 print $0 |
| 735 } |
| 736 } |
| 737 END { |
| 738 print "" |
| 739 print "[" mimetype " - 1]" |
| 740 print "Application=" application |
| 741 print "AllowAsDefault=true" |
| 742 print "GenericServiceType=Application" |
| 743 print "Preference=1" |
| 744 print "ServiceType=" mimetype |
| 745 } |
| 746 ' "$default_file" > "${default_file}.new" && mv "${default_file}.new" "$default_
file" |
| 747 fi |
| 748 } |
| 749 |
| 750 make_default_generic() |
| 751 { |
| 752 # $1 is vendor-name.desktop |
| 753 # $2 is mime/type |
| 754 # Add $2=$1 to XDG_DATA_HOME/applications/mimeapps.list |
| 755 xdg_user_dir="$XDG_DATA_HOME" |
| 756 [ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share" |
| 757 default_file="$xdg_user_dir/applications/mimeapps.list" |
| 758 DEBUG 2 "make_default_generic $1 $2" |
| 759 DEBUG 1 "Updating $default_file" |
| 760 [ -f "$default_file" ] || touch "$default_file" |
| 761 awk -v mimetype="$2" -v application="$1" ' |
| 762 BEGIN { |
| 763 prefix=mimetype "=" |
| 764 indefault=0 |
| 765 added=0 |
| 766 blanks=0 |
| 767 found=0 |
| 768 } |
| 769 { |
| 770 suppress=0 |
| 771 if (index($0, "[Default Applications]") == 1) { |
| 772 indefault=1 |
| 773 found=1 |
| 774 } else if (index($0, "[") == 1) { |
| 775 if (!added && indefault) { |
| 776 print prefix application |
| 777 added=1 |
| 778 } |
| 779 indefault=0 |
| 780 } else if ($0 == "") { |
| 781 suppress=1 |
| 782 blanks++ |
| 783 } else if (indefault && !added && index($0, prefix) == 1) { |
| 784 $0=prefix application |
| 785 added=1 |
| 786 } |
| 787 if (!suppress) { |
| 788 while (blanks > 0) { |
| 789 print "" |
| 790 blanks-- |
| 791 } |
| 792 print $0 |
| 793 } |
| 794 } |
| 795 END { |
| 796 if (!added) { |
| 797 if (!found) { |
| 798 print "" |
| 799 print "[Default Applications]" |
| 800 } |
| 801 print prefix application |
| 802 } |
| 803 while (blanks > 0) { |
| 804 print "" |
| 805 blanks-- |
| 806 } |
| 807 } |
| 808 ' "$default_file" > "${default_file}.new" && mv "${default_file}.new" "$default_
file" |
| 809 } |
| 810 |
| 811 defapp_generic() |
| 812 { |
| 813 MIME="$1" |
| 814 xdg_user_dir="$XDG_DATA_HOME" |
| 815 [ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share" |
| 816 xdg_user_dir="$xdg_user_dir/$xdg_dir_name" |
| 817 xdg_system_dirs="$XDG_DATA_DIRS" |
| 818 [ -n "$xdg_system_dirs" ] || xdg_system_dirs=/usr/local/share/:/usr/share/ |
| 819 |
| 820 for x in `echo "$xdg_user_dir" | sed 's/:/ /g'`; do |
| 821 mimeapps_list="$x/applications/mimeapps.list" |
| 822 if [ -f "$mimeapps_list" ] ; then |
| 823 DEBUG 2 "Checking $mimeapps_list" |
| 824 trader_result=`awk -v mimetype="$MIME" ' |
| 825 BEGIN { |
| 826 prefix=mimetype "=" |
| 827 indefault=0 |
| 828 found=0 |
| 829 } |
| 830 { |
| 831 if (index($0, "[Default Applications]") == 1) { |
| 832 indefault=1 |
| 833 } else if (index($0, "[") == 1) { |
| 834 indefault=0 |
| 835 } else if (!found && indefault && index($0, prefix) == 1) { |
| 836 print substr($0, length(prefix) +1, length) |
| 837 found=1 |
| 838 } |
| 839 } |
| 840 ' $mimeapps_list` |
| 841 if [ -n "$trader_result" ] ; then |
| 842 echo $trader_result |
| 843 exit_success |
| 844 fi |
| 845 fi |
| 846 done |
| 847 |
| 848 for x in `echo "$xdg_system_dirs" | sed 's/:/ /g'`; do |
| 849 DEBUG 2 "Checking $x/applications/defaults.list" |
| 850 trader_result=`grep "$MIME=" $x/applications/defaults.list 2> /dev/null |
cut -d '=' -f 2 | cut -d ';' -f 1` |
| 851 if [ -n "$trader_result" ] ; then |
| 852 echo $trader_result |
| 853 exit_success |
| 854 fi |
| 855 done |
| 856 exit_success |
| 857 } |
| 858 |
| 859 defapp_kde() |
| 860 { |
| 861 MIME="$1" |
| 862 if [ x"$KDE_SESSION_VERSION" = x"4" ]; then |
| 863 KTRADER=`which ktraderclient 2> /dev/null` |
| 864 MIMETYPE="--mimetype" |
| 865 SERVICETYPE="--servicetype" |
| 866 else |
| 867 KTRADER=`which ktradertest 2> /dev/null` |
| 868 fi |
| 869 if [ -n "$KTRADER" ] ; then |
| 870 DEBUG 1 "Running KDE trader query \"$MIME\" mimetype and \"Application\"
servicetype" |
| 871 trader_result=`$KTRADER $MIMETYPE "$MIME" $SERVICETYPE Application 2>/de
v/null \ |
| 872 | grep DesktopEntryPath | head -n 1 | cut -d ':' -f 2 | cut -d \' -f
2` |
| 873 if [ -n "$trader_result" ] ; then |
| 874 basename "$trader_result" |
| 875 exit_success |
| 876 else |
| 877 exit_failure_operation_failed |
| 878 fi |
| 879 else |
| 880 defapp_generic "$1" |
| 881 fi |
| 882 } |
| 883 |
| 884 [ x"$1" != x"" ] || exit_failure_syntax |
| 885 |
| 886 mode= |
| 887 action= |
| 888 filename= |
| 889 mimetype= |
| 890 |
| 891 case $1 in |
| 892 install) |
| 893 action=install |
| 894 ;; |
| 895 |
| 896 uninstall) |
| 897 action=uninstall |
| 898 ;; |
| 899 |
| 900 query) |
| 901 shift |
| 902 |
| 903 if [ -z "$1" ] ; then |
| 904 exit_failure_syntax "query type argument missing" |
| 905 fi |
| 906 |
| 907 case $1 in |
| 908 filetype) |
| 909 action=info |
| 910 |
| 911 filename="$2" |
| 912 if [ -z "$filename" ] ; then |
| 913 exit_failure_syntax "FILE argument missing" |
| 914 fi |
| 915 case $filename in |
| 916 -*) |
| 917 exit_failure_syntax "unexpected option '$filename'" |
| 918 ;; |
| 919 esac |
| 920 check_input_file "$filename" |
| 921 filename=`readlink -f -- "$filename"` |
| 922 ;; |
| 923 |
| 924 default) |
| 925 action=defapp |
| 926 mimetype="$2" |
| 927 if [ -z "$mimetype" ] ; then |
| 928 exit_failure_syntax "mimetype argument missing" |
| 929 fi |
| 930 case $mimetype in |
| 931 -*) |
| 932 exit_failure_syntax "unexpected option '$mimetype'" |
| 933 ;; |
| 934 |
| 935 */*) |
| 936 # Ok |
| 937 ;; |
| 938 |
| 939 *) |
| 940 exit_failure_syntax "mimetype '$mimetype' is not in the form 'minor/
major'" |
| 941 ;; |
| 942 esac |
| 943 ;; |
| 944 |
| 945 *) |
| 946 exit_failure_syntax "unknown query type '$1'" |
| 947 ;; |
| 948 esac |
| 949 ;; |
| 950 |
| 951 default) |
| 952 action=makedefault |
| 953 shift |
| 954 |
| 955 if [ -z "$1" ] ; then |
| 956 exit_failure_syntax "application argument missing" |
| 957 fi |
| 958 case $1 in |
| 959 -*) |
| 960 exit_failure_syntax "unexpected option '$1'" |
| 961 ;; |
| 962 |
| 963 *.desktop) |
| 964 filename="$1" |
| 965 ;; |
| 966 |
| 967 *) |
| 968 exit_failure_syntax "malformed argument '$1', expected *.desktop" |
| 969 ;; |
| 970 esac |
| 971 ;; |
| 972 |
| 973 *) |
| 974 exit_failure_syntax "unknown command '$1'" |
| 975 ;; |
| 976 esac |
| 977 |
| 978 shift |
| 979 |
| 980 |
| 981 if [ "$action" = "makedefault" ]; then |
| 982 if [ -z "$1" ] ; then |
| 983 exit_failure_syntax "mimetype argument missing" |
| 984 fi |
| 985 |
| 986 while [ $# -gt 0 ] ; do |
| 987 case $1 in |
| 988 -*) |
| 989 exit_failure_syntax "unexpected option '$1'" |
| 990 ;; |
| 991 esac |
| 992 mimetype="$1" |
| 993 shift |
| 994 |
| 995 make_default_kde "$filename" "$mimetype" |
| 996 make_default_generic "$filename" "$mimetype" |
| 997 done |
| 998 exit_success |
| 999 fi |
| 1000 |
| 1001 if [ "$action" = "info" ]; then |
| 1002 detectDE |
| 1003 |
| 1004 if [ x"$DE" = x"" ]; then |
| 1005 if [ -x /usr/bin/file ]; then |
| 1006 DE=generic |
| 1007 fi |
| 1008 fi |
| 1009 |
| 1010 case "$DE" in |
| 1011 kde) |
| 1012 info_kde "$filename" |
| 1013 ;; |
| 1014 |
| 1015 gnome*) |
| 1016 info_gnome "$filename" |
| 1017 ;; |
| 1018 |
| 1019 *) |
| 1020 info_generic "$filename" |
| 1021 ;; |
| 1022 esac |
| 1023 exit_failure_operation_impossible "no method available for quering MIME type
of '$filename'" |
| 1024 fi |
| 1025 |
| 1026 if [ "$action" = "defapp" ]; then |
| 1027 detectDE |
| 1028 |
| 1029 case "$DE" in |
| 1030 kde) |
| 1031 defapp_kde "$mimetype" |
| 1032 ;; |
| 1033 |
| 1034 *) |
| 1035 defapp_generic "$mimetype" |
| 1036 ;; |
| 1037 esac |
| 1038 exit_failure_operation_impossible "no method available for quering default a
pplication for '$mimetype'" |
| 1039 fi |
| 1040 |
| 1041 vendor=true |
| 1042 while [ $# -gt 0 ] ; do |
| 1043 parm="$1" |
| 1044 shift |
| 1045 |
| 1046 case $parm in |
| 1047 --mode) |
| 1048 if [ -z "$1" ] ; then |
| 1049 exit_failure_syntax "mode argument missing for --mode" |
| 1050 fi |
| 1051 case "$1" in |
| 1052 user) |
| 1053 mode="user" |
| 1054 ;; |
| 1055 |
| 1056 system) |
| 1057 mode="system" |
| 1058 ;; |
| 1059 |
| 1060 *) |
| 1061 exit_failure_syntax "unknown mode '$1'" |
| 1062 ;; |
| 1063 esac |
| 1064 shift |
| 1065 ;; |
| 1066 |
| 1067 --novendor) |
| 1068 vendor=false |
| 1069 ;; |
| 1070 |
| 1071 -*) |
| 1072 exit_failure_syntax "unexpected option '$parm'" |
| 1073 ;; |
| 1074 |
| 1075 *) |
| 1076 if [ -n "$filename" ] ; then |
| 1077 exit_failure_syntax "unexpected argument '$parm'" |
| 1078 fi |
| 1079 |
| 1080 filename="$parm" |
| 1081 check_input_file "$filename" |
| 1082 ;; |
| 1083 esac |
| 1084 done |
| 1085 |
| 1086 if [ -z "$action" ] ; then |
| 1087 exit_failure_syntax "command argument missing" |
| 1088 fi |
| 1089 |
| 1090 if [ -n "$XDG_UTILS_INSTALL_MODE" ] ; then |
| 1091 if [ "$XDG_UTILS_INSTALL_MODE" = "system" ] ; then |
| 1092 mode="system" |
| 1093 elif [ "$XDG_UTILS_INSTALL_MODE" = "user" ] ; then |
| 1094 mode="user" |
| 1095 fi |
| 1096 fi |
| 1097 |
| 1098 if [ -z "$mode" ] ; then |
| 1099 if [ `whoami` = "root" ] ; then |
| 1100 mode="system" |
| 1101 else |
| 1102 mode="user" |
| 1103 fi |
| 1104 fi |
| 1105 |
| 1106 if [ -z "$filename" ] ; then |
| 1107 exit_failure_syntax "mimetypes-file argument missing" |
| 1108 fi |
| 1109 |
| 1110 if [ "$vendor" = "true" -a "$action" = "install" ] ; then |
| 1111 check_vendor_prefix "$filename" |
| 1112 fi |
| 1113 |
| 1114 xdg_base_dir= |
| 1115 xdg_dir_name=mime/packages/ |
| 1116 |
| 1117 xdg_user_dir="$XDG_DATA_HOME" |
| 1118 [ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share" |
| 1119 [ x"$mode" = x"user" ] && xdg_base_dir="$xdg_user_dir/mime" |
| 1120 xdg_user_dir="$xdg_user_dir/$xdg_dir_name" |
| 1121 |
| 1122 xdg_system_dirs="$XDG_DATA_DIRS" |
| 1123 [ -n "$xdg_system_dirs" ] || xdg_system_dirs=/usr/local/share/:/usr/share/ |
| 1124 for x in `echo $xdg_system_dirs | sed 's/:/ /g'`; do |
| 1125 if [ -w $x/$xdg_dir_name ] ; then |
| 1126 [ x"$mode" = x"system" ] && xdg_base_dir="$x/mime" |
| 1127 xdg_global_dir="$x/$xdg_dir_name" |
| 1128 break |
| 1129 fi |
| 1130 done |
| 1131 [ -w $xdg_global_dir ] || xdg_global_dir= |
| 1132 DEBUG 3 "xdg_user_dir: $xdg_user_dir" |
| 1133 DEBUG 3 "xdg_global_dir: $xdg_global_dir" |
| 1134 |
| 1135 # Find KDE3 mimelnk directory |
| 1136 kde_user_dir= |
| 1137 kde_global_dir= |
| 1138 kde_global_dirs=`kde${KDE_SESSION_VERSION}-config --path mime 2> /dev/null` |
| 1139 DEBUG 3 "kde_global_dirs: $kde_global_dirs" |
| 1140 first= |
| 1141 for x in `echo $kde_global_dirs | sed 's/:/ /g'` ; do |
| 1142 if [ -z "$first" ] ; then |
| 1143 first=false |
| 1144 kde_user_dir="$x" |
| 1145 elif [ -w $x ] ; then |
| 1146 kde_global_dir="$x" |
| 1147 fi |
| 1148 done |
| 1149 DEBUG 3 "kde_user_dir: $kde_user_dir" |
| 1150 DEBUG 3 "kde_global_dir: $kde_global_dir" |
| 1151 |
| 1152 # TODO: Gnome legacy support |
| 1153 # See http://forums.fedoraforum.org/showthread.php?t=26875 |
| 1154 gnome_user_dir="$HOME/.gnome/apps" |
| 1155 gnome_global_dir=/usr/share/gnome/apps |
| 1156 [ -w $gnome_global_dir ] || gnome_global_dir= |
| 1157 DEBUG 3 "gnome_user_dir: $gnome_user_dir" |
| 1158 DEBUG 3 "gnome_global_dir: $gnome_global_dir" |
| 1159 |
| 1160 if [ x"$mode" = x"user" ] ; then |
| 1161 xdg_dir="$xdg_user_dir" |
| 1162 kde_dir="$kde_user_dir" |
| 1163 gnome_dir="$gnome_user_dir" |
| 1164 my_umask=077 |
| 1165 else |
| 1166 xdg_dir="$xdg_global_dir" |
| 1167 kde_dir="$kde_global_dir" |
| 1168 gnome_dir="$gnome_global_dir" |
| 1169 my_umask=022 |
| 1170 if [ -z "${xdg_dir}${kde_dir}${gnome_dir}" ] ; then |
| 1171 exit_failure_operation_impossible "No writable system mimetype directory
found." |
| 1172 fi |
| 1173 fi |
| 1174 |
| 1175 # echo "[xdg|$xdg_user_dir|$xdg_global_dir]" |
| 1176 # echo "[kde|$kde_user_dir|$kde_global_dir]" |
| 1177 # echo "[gnome|$gnome_user_dir|$gnome_global_dir]" |
| 1178 # echo "[using|$xdg_dir|$kde_dir|$gnome_dir]" |
| 1179 |
| 1180 basefile=`basename "$filename"` |
| 1181 #[ -z $vendor ] || basefile="$vendor-$basefile" |
| 1182 |
| 1183 mimetypes= |
| 1184 if [ -n "$kde_dir" ] ; then |
| 1185 DEBUG 2 "KDE3 mimelnk directory found, extracting mimetypes from XML file" |
| 1186 |
| 1187 mimetypes=`awk < "$filename" ' |
| 1188 # Strip XML comments |
| 1189 BEGIN { |
| 1190 suppress=0 |
| 1191 } |
| 1192 { |
| 1193 do |
| 1194 if (suppress) { |
| 1195 if (match($0,/-->/)) { |
| 1196 $0=substr($0,RSTART+RLENGTH) |
| 1197 suppress=0 |
| 1198 } |
| 1199 else { |
| 1200 break |
| 1201 } |
| 1202 } |
| 1203 else { |
| 1204 if (match($0,/<!--/)) { |
| 1205 if (RSTART>1) print substr($0,0,RSTART) |
| 1206 $0=substr($0,RSTART+RLENGTH) |
| 1207 suppress=1 |
| 1208 } |
| 1209 else { |
| 1210 if ($0) print $0 |
| 1211 break |
| 1212 } |
| 1213 } |
| 1214 while(1) |
| 1215 } |
| 1216 ' | awk ' |
| 1217 # List MIME types listed in <mime-type> tags |
| 1218 BEGIN { |
| 1219 RS="<" |
| 1220 } |
| 1221 /^mime-info/, /^\/mime-info/ { |
| 1222 if (match($0,/^mime-type/)) { |
| 1223 if (match($0,/type="[^"]*/) || match($0,/type='"'"'[^'"'"']*/)) { |
| 1224 print substr($0,RSTART+6,RLENGTH-6) |
| 1225 } |
| 1226 } |
| 1227 }'` |
| 1228 fi |
| 1229 |
| 1230 DEBUG 1 "$action mimetype in $xdg_dir" |
| 1231 |
| 1232 case $action in |
| 1233 install) |
| 1234 save_umask=`umask` |
| 1235 umask $my_umask |
| 1236 |
| 1237 for x in $xdg_dir ; do |
| 1238 mkdir -p $x |
| 1239 eval 'cp $filename $x/$basefile'$xdg_redirect_output |
| 1240 done |
| 1241 |
| 1242 if [ -n "$mimetypes" ] ; then |
| 1243 # No quotes around $mimetypes |
| 1244 for x in $mimetypes ; do |
| 1245 DEBUG 1 "Installing $kde_dir/$x.desktop (KDE 3.x support)" |
| 1246 mkdir -p `dirname $kde_dir/$x.desktop` |
| 1247 awk < "$filename" ' |
| 1248 # Strip XML comments |
| 1249 BEGIN { |
| 1250 suppress=0 |
| 1251 } |
| 1252 { |
| 1253 do |
| 1254 if (suppress) { |
| 1255 if (match($0,/-->/)) { |
| 1256 $0=substr($0,RSTART+RLENGTH) |
| 1257 suppress=0 |
| 1258 } |
| 1259 else { |
| 1260 break |
| 1261 } |
| 1262 } |
| 1263 else { |
| 1264 if (match($0,/<!--/)) { |
| 1265 if (RSTART>1) print substr($0,0,RSTART) |
| 1266 $0=substr($0,RSTART+RLENGTH) |
| 1267 suppress=1 |
| 1268 } |
| 1269 else { |
| 1270 if ($0) print $0 |
| 1271 break |
| 1272 } |
| 1273 } |
| 1274 while(1) |
| 1275 } |
| 1276 ' | awk > $kde_dir/$x.desktop ' |
| 1277 # Extract mimetype $x from the XML file $filename |
| 1278 # Note that bash requires us to escape a single quote as '"'"' |
| 1279 BEGIN { |
| 1280 the_type=ARGV[1] |
| 1281 the_source=ARGV[2] |
| 1282 ARGC=1 |
| 1283 RS="<" |
| 1284 found=0 |
| 1285 glob_patterns="" |
| 1286 } |
| 1287 /^mime-info/, /^\/mime-info/ { |
| 1288 if (match($0,/^mime-type/)) { |
| 1289 if (match($0,/type="[^"]*/) || match($0,/type='"'"'[^'"'"']*/)) { |
| 1290 if (substr($0,RSTART+6,RLENGTH-6) == the_type) { |
| 1291 found=1 |
| 1292 print "[Desktop Entry]" |
| 1293 print "# Installed by xdg-mime from " the_source |
| 1294 print "Type=MimeType" |
| 1295 print "MimeType=" the_type |
| 1296 the_icon=the_type |
| 1297 sub("/", "-", the_icon) |
| 1298 print "Icon=" the_icon |
| 1299 } |
| 1300 } |
| 1301 } |
| 1302 else if (found) { |
| 1303 if (match($0,/^\/mime-type/)) { |
| 1304 if (glob_patterns) |
| 1305 print "Patterns=" glob_patterns |
| 1306 exit 0 |
| 1307 } |
| 1308 |
| 1309 if (match($0,/^sub-class-of/)) { |
| 1310 if (match($0,/type="[^"]*/) || match($0,/type='"'"'[^'"'"']*/)) { |
| 1311 print "X-KDE-IsAlso=" substr($0,RSTART+6,RLENGTH-6) |
| 1312 } |
| 1313 else { |
| 1314 print "Error: '"'"'type'"'"' argument missing in " RS $0 |
| 1315 exit 1 |
| 1316 } |
| 1317 } |
| 1318 if (match($0,/^glob/)) { |
| 1319 if (match($0,/pattern="[^"]*/) || match($0,/pattern='"'"'[^'"'"']*/)) { |
| 1320 glob_patterns = glob_patterns substr($0,RSTART+9,RLENGTH-9) ";" |
| 1321 } |
| 1322 else { |
| 1323 print "Error: '"'"'pattern'"'"' argument missing in " RS $0 |
| 1324 exit 1 |
| 1325 } |
| 1326 } |
| 1327 if (match($0,/^comment/)) { |
| 1328 if (match($0,/xml:lang="[^"]*/) || match($0,/xml:lang='"'"'[^'"'"']*/)) { |
| 1329 lang=substr($0,RSTART+10,RLENGTH-10) |
| 1330 } |
| 1331 else { |
| 1332 lang="" |
| 1333 } |
| 1334 if (match($0,/>/)) { |
| 1335 comment=substr($0,RSTART+1) |
| 1336 sub("<", "<", comment) |
| 1337 sub(">", ">", comment) |
| 1338 sub("&", "\\&", comment) |
| 1339 if (lang) |
| 1340 print "Comment[" lang "]=" comment |
| 1341 else |
| 1342 print "Comment=" comment |
| 1343 } |
| 1344 } |
| 1345 } |
| 1346 } |
| 1347 END { |
| 1348 if (!found) { |
| 1349 print "Error: Mimetype '"'"'" the_type "'"'"' not found" |
| 1350 exit 1 |
| 1351 } |
| 1352 } |
| 1353 ' $x $basefile |
| 1354 if [ "$?" = "1" ] ; then |
| 1355 grep -A 10 "^Error:" $kde_dir/$x.desktop >&2 |
| 1356 rm $kde_dir/$x.desktop |
| 1357 exit 1 |
| 1358 fi |
| 1359 done |
| 1360 fi |
| 1361 |
| 1362 umask $save_umask |
| 1363 ;; |
| 1364 |
| 1365 uninstall) |
| 1366 for x in $xdg_dir ; do |
| 1367 rm -f $x/$basefile |
| 1368 done |
| 1369 |
| 1370 # No quotes around $mimetypes |
| 1371 for x in $mimetypes ; do |
| 1372 if grep '^# Installed by xdg-mime' $kde_dir/$x.desktop >/dev/null 2>
&1; then |
| 1373 DEBUG 1 "Removing $kde_dir/$x.desktop (KDE 3.x support)" |
| 1374 rm -f $kde_dir/$x.desktop |
| 1375 fi |
| 1376 done |
| 1377 ;; |
| 1378 esac |
| 1379 |
| 1380 update_mime_database $xdg_base_dir |
| 1381 |
| 1382 exit_success |
| 1383 |
OLD | NEW |