summaryrefslogtreecommitdiff
path: root/scripts/xdg-mime.in
blob: 6548720fde50290ea59814c0608b62494242c12e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
#!/bin/sh
#---------------------------------------------
#   xdg-mime
#
#   Utility script to manipulate MIME related information
#   on XDG compliant systems.
#
#   Refer to the usage() function below for usage.
#
#   Copyright 2006, Kevin Krammer <kevin.krammer@gmx.at>
#   Copyright 2006, Jeremy White <jwhite@codeweavers.com>
#
#   LICENSE:
#
#---------------------------------------------

manualpage()
{
cat << _MANUALPAGE
_MANUALPAGE
}

usage()
{
cat << _USAGE
_USAGE
}

#@xdg-utils-common@

update_mime_database()
{
   IFS_save=$IFS
   IFS=":"
   for x in $PATH:/opt/gnome/bin; do
      if [ -x $x/update-mime-database ] ; then
         DEBUG 1 "Running $x/update-mime-database $1"
         $x/update-mime-database $1
         return
      fi
   done
   IFS=$IFS_save
}

info_kde()
{
    DEBUG 1 "Running kfile \"$1\""
    kfile "$1" 2> /dev/null | head -n 1 | cut -d "(" -f 2 | cut -d ")" -f 1

    exit_success
}

info_gnome()
{
    DEBUG 1 "Running gnomevfs-info \"$1\""
    gnomevfs-info "$1" 2> /dev/null | grep MIME | cut -d ":" -f 2 | sed s/"^ "//

    exit_success
}

info_generic()
{
    DEBUG 1 "Running /usr/bin/file -i \"$1\"" 
    /usr/bin/file -i "$1" 2> /dev/null | cut -d ":" -f 2 | sed s/"^ "//

    exit_success
}

make_default_kde()
{
    # $1 is vendor-name.desktop
    # $2 is mime/type 
    # Add to $KDE_HOME/share/config/profilerc:
    # [$2 - 1]
    # Application=$1
    #
    # Remove all [$2 - *] sections, or even better,
    # renumber [$2 - *] sections and remove duplicate
    
    default_file=$HOME/.kde/share/config/profilerc
    DEBUG 2 "make_default_kde $1 $2"
    DEBUG 1 "Updating $default_file"
    awk -v application="$1" -v mimetype="$2" '
    BEGIN {
        header_start="[" mimetype " - "
        supress=0
    }
    {  
        if (index($0, header_start) == 1 )
            supress=1
        else
            if (/^\[/) { supress=0 }

        if (!supress) {
            print $0
        }
    }
    END {
        print "" 
        print "[" mimetype " - 1]"
        print "Application=" application
        print "AllowAsDefault=true"
        print "GenericServiceType=Application"
        print "Preference=1"
        print "ServiceType=" mimetype
    }
' $default_file > ${default_file}.new && mv ${default_file}.new $default_file
}

make_default_generic()
{
    # $1 is vendor-name.desktop
    # $2 is mime/type 
    # Add $2=$1 to XDG_DATA_HOME/applications/defaults.list
    xdg_user_dir=$XDG_DATA_HOME
    [ -n "$xdg_user_dir" ] || xdg_user_dir=$HOME/.local/share
    default_file=$xdg_user_dir/applications/defaults.list
    DEBUG 2 "make_default_generic $1 $2"
    DEBUG 1 "Updating $default_file"
    grep -v "$2=" $default_file > ${default_file}.new 2> /dev/null
    if ! grep "[Default Applications]" ${default_file}.new > /dev/null; then
       echo "[Default Applications]" >> ${default_file}.new
    fi
    echo $2=$1 >> ${default_file}.new
    mv ${default_file}.new $default_file
}


defapp_kde()
{
    DEBUG 1 "Running kfile \"$1\""
    MIME=`kfile "$1" 2> /dev/null | head -n 1 | cut -d "(" -f 2 | cut -d ")" -f 1`
    DEBUG 2 "MIME = $MIME"

    if [ x"$MIME" != x"" ]; then
        DEBUG 1 "Running ktradertest \"$MIME\" Application"
        trader_result=`ktradertest "$MIME" Application 2>/dev/null | grep DesktopEntryPath \
            | head -n 1 | cut -d ':' -f 2 | cut -d \' -f 2`
        if [ -n "$trader_result" ] ; then
            basename "$trader_result"
        fi
        exit_success
    else
        exit_failure_operation_failed
    fi
}

defapp_gnome()
{
    DEBUG 1 "Running gnomevfs-info \"$1\""
    gnomevfs-info "$1" 2> /dev/null | grep "Default app" | cut -d ":" -f 2 \
        | cut -d " " -f 2

    exit_success
}


install_xsl_listmime()
{
# Dump script to file, can't pipe into xsltproc directly
xsl_file=`mktemp` || return
DEBUG 2 "Creating temporary XSL file to list mimetypes: $xsl_file"
cat > $xsl_file << _XSL
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:mime="http://www.freedesktop.org/standards/shared-mime-info">
<xsl:output method="text"
            encoding="UTF-8"
            indent="no"/>
<xsl:template match="/">
      <xsl:for-each select='mime:mime-info/mime:mime-type'>
        <xsl:value-of select="@type"/><xsl:text>&#10;</xsl:text>
      </xsl:for-each>
</xsl:template>
</xsl:stylesheet>
_XSL
}

install_xsl_shared2mimelnk()
{
# Dump script to file, can't pipe into xsltproc directly
xsl_file=`mktemp` || return
DEBUG 2 "Creating temporary XSL file to convert XML mimetype file: $xsl_file"
cat > $xsl_file << _XSL
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:mime="http://www.freedesktop.org/standards/shared-mime-info">
<xsl:output method="text"
            encoding="UTF-8"
            indent="no"/>
<xsl:template match="/">
      <xsl:for-each select='mime:mime-info/mime:mime-type[@type=\$type]'>
        <xsl:text>[Desktop Entry]&#10;</xsl:text>
        <xsl:text># Installed by xdg-mime from </xsl:text><xsl:value-of select="\$source"/><xsl:text>&#10;</xsl:text>
        <xsl:text>Type=MimeType&#10;</xsl:text>
        <xsl:text>MimeType=</xsl:text><xsl:value-of select="@type"/><xsl:text>&#10;</xsl:text>
        <xsl:text>Icon=</xsl:text><xsl:value-of select="translate(@type,'/','-')"/><xsl:text>&#10;</xsl:text>
        <xsl:if test="mime:sub-class-of">
           <xsl:text>X-KDE-IsAlso=</xsl:text><xsl:value-of select="mime:sub-class-of/@type"/><xsl:text>&#10;</xsl:text>
        </xsl:if>
        <xsl:if test="mime:glob">
          <xsl:text>Patterns=</xsl:text>
          <xsl:for-each select='mime:glob[@pattern]'>
            <xsl:value-of select="@pattern"/><xsl:text>;</xsl:text>
          </xsl:for-each>
          <xsl:text>&#10;</xsl:text>
        </xsl:if>
        <xsl:text>Comment=</xsl:text><xsl:value-of select="mime:comment[not(@xml:lang)]"/><xsl:text>&#10;</xsl:text>
        <xsl:for-each select='mime:comment[@xml:lang]'>
          <xsl:sort select='@xml:lang'/>
          <xsl:text>Comment[</xsl:text><xsl:value-of select="@xml:lang"/><xsl:text>]=</xsl:text><xsl:value-of select="."/><xsl:text>&#10;</xsl:text>
        </xsl:for-each>
     </xsl:for-each>
</xsl:template>
</xsl:stylesheet>
_XSL
}

uninstall_xsl()
{
  DEBUG 2 "Cleaning up temporary XSL file: $xsl_file"
  rm -f $xsl_file
  xsl_file=
}


[ x"$1" != x"" ] || exit_failure_syntax

mode=
action=
filename=

case $1 in
  install)
    action=install
    ;;

  uninstall)
    action=uninstall
    ;;

  query)
    shift

    if [ -z "$1" ] ; then 
        exit_failure_syntax "query type argument missing"
    fi
    
    case $1 in
      filetype)
        action=info
        ;;

      default)
        action=defapp
        ;;

      *)
      exit_failure_syntax "unknown query type '$1'"
      ;;
    esac
    ;;

  default)
    action=makedefault
    shift

    if [ -z "$1" ] ; then 
        exit_failure_syntax "application argument missing"
    fi
    case $1 in
      -*)
        exit_failure_syntax "unexpected option '$1'"
        ;;

      *.desktop)
        filename=$1
        ;;

      *)
        exit_failure_syntax "malformed argument '$1', expected *.desktop"
        ;;
    esac
    ;;

  *)
  exit_failure_syntax "unknown command '$1'"
  ;;
esac

shift


if [ "$action" = "makedefault" ]; then
    if [ -z "$1" ] ; then 
        exit_failure_syntax "mimetype argument missing"
    fi

    while [ $# -gt 0 ] ; do
        case $1 in
          -*)
            exit_failure_syntax "unexpected option '$1'"
            ;;
        esac
        mimetype=$1
        shift
        
        make_default_kde "$filename" "$mimetype"
        make_default_generic "$filename" "$mimetype"
    done
    exit_success
fi

while [ $# -gt 0 ] ; do
    parm=$1
    shift

    case $parm in
      --user)
        mode=user
        ;;

      --system)
        mode=system
        ;;

      -*)
        exit_failure_syntax "unexpected option '$parm'"
        ;;

      *)
        if [ -n "$filename" ] ; then
            exit_failure_syntax "unexpected argument '$parm'"
        fi
        if [ ! -f "$parm" ] ; then
            exit_failure_file_missing "file '$parm' does not exist"
        fi
        filename=$parm
        ;;
    esac
done

if [ -z "$action" ] ; then
    exit_failure_syntax "command argument missing"
fi

if [ "$action" = "info" ]; then
    if [ -n "$mode" ] ; then
        exit_failure_syntax "unexpected option '--$mode'"
    fi
    if [ -z "$filename" ] ; then
        exit_failure_syntax "FILE argument missing"
    fi

    detectDE

    if [ x"$DE" = x"" ]; then
        if [ -x /usr/bin/file ]; then
            DE=generic
        fi
    fi

    case "$DE" in
        kde)
        info_kde "$filename"
        ;;

        gnome)
        info_gnome "$filename"
        ;;

        generic)
        info_generic "$filename"
        ;;
    esac
    exit_failure_operation_impossible "no method available for quering MIME type of '$filename'"
fi

if [ "$action" = "defapp" ]; then
    if [ -n "$mode" ] ; then
        exit_failure_syntax "unexpected option '--$mode'"
    fi
    if [ -z "$filename" ] ; then
        exit_failure_syntax "FILE argument missing"
    fi

    detectDE

    case "$DE" in
        kde)
        defapp_kde "$filename"
        ;;

        gnome)
        defapp_gnome "$filename"
        ;;
    esac
    exit_failure_operation_impossible "no method available for quering default application for file '$filename'"
fi

if [ -z "$mode" ] ; then
    exit_failure_syntax "expected --user or --system option"
fi
if [ -z "$filename" ] ; then
    exit_failure_syntax "mimetypes-file argument missing"
fi

xdg_base_dir=
xdg_dir_name=mime/packages/

xdg_user_dir=$XDG_DATA_HOME
[ -n "$xdg_user_dir" ] || xdg_user_dir=$HOME/.local/share
[ x"$mode" = x"user" ] && xdg_base_dir=$xdg_user_dir/mime
xdg_user_dir=$xdg_user_dir/$xdg_dir_name

xdg_system_dirs=$XDG_DATA_DIRS
[ -n "$xdg_system_dirs" ] || xdg_system_dirs=/usr/local/share/:/usr/share/
IFS_save=$IFS
IFS=":"
for x in $xdg_system_dirs ; do
    if [ -w $x/$xdg_dir_name ] ; then
        [ x"$mode" = x"system" ] && xdg_base_dir=$x/mime
        xdg_global_dir=$x/$xdg_dir_name
        break
    fi
done
IFS=$IFS_save
[ -w $xdg_global_dir ] || xdg_global_dir=
DEBUG 3 "xdg_user_dir: $xdg_user_dir"
DEBUG 3 "xdg_global_dir: $xdg_global_dir"

# Find KDE3 mimelnk directory
kde_user_dir=$HOME/.kde/share/mimelnk
kde_global_dir=
kde_global_dirs=`kde-config --path mime`
DEBUG 3 "kde_global_dirs: $kde_global_dirs"
[ -n $kde_global_dirs ] || kde_global_dirs=/opt/kde3/share/mimelnk:/opt/kde/share/mimelnk:/usr/share/mimelnk
IFS_save=$IFS
IFS=":"
first=
for x in $kde_global_dirs ; do
    if [ -z "$first" ] ; then
        first=false
        #TODO: Mode check seems redundant
        [ x"$mode" = x"user" ] && kde_user_dir=$x
    elif [ -w $x ] ; then
        [ x"$mode" = x"system" ] && kde_global_dir=$x
    fi
done
IFS=$IFS_save
DEBUG 3 "kde_user_dir: $kde_user_dir"
DEBUG 3 "kde_global_dir: $kde_global_dir"

# TODO: Gnome legacy support
# See http://forums.fedoraforum.org/showthread.php?t=26875
gnome_user_dir=$HOME/.gnome/apps
gnome_global_dir=/usr/share/gnome/apps
[ -w $gnome_global_dir ] || gnome_global_dir=
DEBUG 3 "gnome_user_dir: $gnome_user_dir"
DEBUG 3 "gnome_global_dir: $gnome_global_dir"

if [ x"$mode" = x"user" ] ; then
    xdg_dir=$xdg_user_dir
    kde_dir=$kde_user_dir
    gnome_dir=$gnome_user_dir
    my_umask=077
else
    xdg_dir=$xdg_global_dir
    kde_dir=$kde_global_dir
    gnome_dir=$gnome_global_dir
    my_umask=022
    if [ -z "${xdg_dir}${kde_dir}${gnome_dir}" ] ; then
        [ `whoami` = "root" ] || rootmsg="Try as root or use --user."
        exit_failure_operation_impossible "No writable system mimetype directory found. $rootmsg"
    fi
fi

# echo "[xdg|$xdg_user_dir|$xdg_global_dir]"
# echo "[kde|$kde_user_dir|$kde_global_dir]"
# echo "[gnome|$gnome_user_dir|$gnome_global_dir]"
# echo "[using|$xdg_dir|$kde_dir|$gnome_dir]"

basefile=`basename "$filename"`
#[ -z $vendor ] || basefile=$vendor-$basefile

mimetypes=
if [ -n "$kde_dir" ] ; then
    DEBUG 2 "KDE3 mimelnk directory found, extracting mimetypes from XML file"
    install_xsl_listmime
    DEBUG 1 "Running xsltproc \"$xsl_file\" \"$filename\""
    mimetypes=`xsltproc "$xsl_file" "$filename"`
    uninstall_xsl
fi  

DEBUG 1 "$action mimetype in $xdg_dir"

case $action in
    install)
        save_umask=`umask`
        umask $my_umask

        for x in $xdg_dir ; do
            mkdir -p $x
            cp $filename $x/$basefile
        done

        if [ -n "$mimetypes" ] ; then
            install_xsl_shared2mimelnk
        
            IFS_save=$IFS
            IFS='
'
            for x in $mimetypes ; do 
                DEBUG 1 "Installing $kde_dir/$x.desktop (KDE 3.x support)"
                xsltproc --stringparam type $x --stringparam source $basefile $xsl_file $filename > $kde_dir/$x.desktop
            done
            IFS=$IFS_save

            uninstall_xsl
        fi

        umask $save_umask
        ;;

    uninstall)
        for x in $xdg_dir ; do
            rm -f $x/$basefile
        done

        IFS_save=$IFS
        IFS='
'
        for x in $mimetypes ; do 
            if grep '^# Installed by xdg-mime' $kde_dir/$x.desktop &>/dev/null ; then
                DEBUG 1 "Removing $kde_dir/$x.desktop (KDE 3.x support)"
                rm -f $kde_dir/$x.desktop
            fi
        done
        IFS=$IFS_save

        ;;
esac

update_mime_database $xdg_base_dir

exit_success