summaryrefslogtreecommitdiffstats
path: root/gnome2-system-admin-guide/C/mimetypes.xml
blob: 15ca2a8b96c97da53cf33c856b60ce9f4c20585e (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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
<?xml version="1.0" encoding="UTF-8"?>
<chapter id="mimetypes-0">
  <title>MIME Types</title>
  <anchor id="mimetypes-TBL-3"/>
  <anchor id="mimetypes-TBL-6"/>
  <anchor id="mimetypes-TBL-8"/>

  <highlights>
    <para>This chapter describes how applications detect MIME types, how to
    register MIME types, and how to add applications to the GNOME
    Desktop.</para>
  </highlights>

  <para>The purpose of this chapter is to make it easy for an administrator to
  understand how to configure different parts of the MIME database and give an
  general overview of the MIME system. Therefore we will
  <emphasis>not</emphasis> attempt to go into details where it is not
  necessary. For the gory details, it is recommended that you refer to the
  <ulink url="http://www.freedesktop.org/Standards/shared-mime-info-spec">XDG
  shared mime info specification</ulink>.</para>

  <sect1 id="mimetypes-intro">
    <title>Introduction to MIME Types</title>
    <anchor id="mimetypes-1"/>

    <indexterm>
      <primary>MIME types</primary>

      <secondary>introduction</secondary>
    </indexterm>

    <para>A <firstterm>Multipurpose Internet Mail Extension</firstterm>
    [<acronym>MIME</acronym>] type identifies the format of a file.
    Applications such as Internet browsers and email applications use the MIME
    type of a file to decide which actions to perform on it. For example, an
    email application can use the MIME type to detect the format of an
    attachment and choose an appropriate viewer for the file, such as opening
    a text document with <application>gedit</application>.</para>

    <para>As another example, the <application>Nautilus</application> file
    manager needs to know the MIME type of a file to perform the following
    tasks:</para>

    <itemizedlist>
      <listitem>
        <para>Open the file in an appropriate application.</para>
      </listitem>

      <listitem>
        <para>Display a string that describes the type of file.</para>
      </listitem>

      <listitem>
        <para>Display an appropriate icon to represent the file.</para>
      </listitem>

      <listitem>
        <para>Display a list of other applications that can open the
        file.</para>
      </listitem>
    </itemizedlist>

    <para>MIME types were originally proposed as a standard for identifying
    the message body of an e-mail message. Now, many systems use MIME types to
    identify the format of arbitrary files on the file system. MIME types are
    composed of a top-level <emphasis>media type</emphasis> followed by a
    <emphasis>subtype identifier</emphasis>, separated by a forward slash
    character, <quote><literal>/</literal></quote>. One example of a MIME type
    is <literal>image/jpeg</literal>. The media type in this example is
    <quote><literal>image</literal></quote> and the subtype identifier is
    <quote><literal>jpeg</literal></quote>. The top-level media type is meant
    to be a general categorization about the content of the file, while the
    subtype identifer is meant to specifically identify the format of the
    file.</para>

    <para>There are eight media types currently blessed by the
    <acronym>IANA</acronym> [Internet Assigned Naming Authority]. These eight
    media types are <simplelist type="inline">
        <member>application</member>

        <member>audio</member>

        <member>image</member>

        <member>message</member>

        <member>model</member>

        <member>multipart</member>

        <member>text</member>

        <member>video</member>
      </simplelist>. Many subtypes exist for each media type; for more
    information, see <ulink
    url="http://www.iana.org/assignments/media-types/">MIME Media
    Types</ulink> at the <ulink url="http://www.iana.org/">IANA</ulink> web
    site.</para>

    <para>Implementation of MIME types in GNOME follows the <ulink
    url="http://www.freedesktop.org/Standards/shared-mime-info-spec">XDG
    shared mime info specification</ulink>. This specification provides the
    following advantages:</para>

    <itemizedlist>
      <listitem>
        <para>Standard locations for all MIME related files.</para>
      </listitem>

      <listitem>
        <para>A standard way for applications to register information about a
        new MIME type.</para>
      </listitem>

      <listitem>
        <para>A standard way to retrieve the MIME type for a file.</para>
      </listitem>

      <listitem>
        <para>A standard way to retrieve information about a MIME type.</para>
      </listitem>
    </itemizedlist>

    <para>The rest of the chapter describes the files and directories that
    make up the MIME database, details about source XML files, how to create
    or modify MIME types, how to register applications as handlers for certain
    MIME types and finally, how to add an application to the GNOME
    desktop.</para>
  </sect1>

  <sect1 id="mimetypes-database">
    <title>The MIME Database</title>
    <anchor id="mimetypes-4"/>
    <anchor id="mimetypes-5"/>

    <indexterm>
      <primary>MIME types</primary>

      <secondary>database</secondary>
    </indexterm>

    <para>The MIME database is a collection of files that make up:</para>

    <itemizedlist>
      <listitem>
        <para>The set of known MIME types</para>
      </listitem>

      <listitem>
        <para>The method for determing the MIME type of a file</para>
      </listitem>

      <listitem>
        <para>Meta information regarding a MIME type, such as a human readable
        description to use when displaying files of this type.</para>
      </listitem>
    </itemizedlist>

    <sect2 id="mimetypes-location">
      <title>Location</title>

      <para>As an administrator, the most important and basic step to
      understanding the MIME system is learning the locations where these
      files are stored. Since the <ulink
      url="http://www.freedesktop.org/Standards/shared-mime-info-spec">XDG
      shared mime info specification</ulink> was drafted by the X Desktop
      Group, it also makes use of the <ulink
      url="http://www.freedesktop.org/wiki/Standards/basedir-spec">XDG base
      directory specification</ulink>. It is highly recommended that you
      familiarize yourself with this specification, as it is also important
      for other system administration tasks such as editing menus. A brief
      summary of the directory locations as pertaining to the MIME
      specification is given below.</para>

      <para>The MIME database is created from the set of files located in the
      <filename>$XDG_DATA_HOME/mime</filename> and
      <filename>$XDG_DATA_DIRS/mime</filename> directories. If
      these environment variables are unset, then they default to the values
      <filename>~/.local/share</filename> and
      <filename>/usr/local/share:/usr/share</filename> respectively. As can be
      seen from the default value for <envar>$XDG_DATA_DIRS</envar> , each
      environment variable is actually a colon separated list of directories.
      The user's database at
      <filename>$XDG_DATA_HOME/mime</filename> has precedence
      over the system database at
      <filename>$XDG_DATA_DIRS/mime</filename> when conflicting
      definitions are encountered. Similar to the <citetitle>XDG shared mime
      specification</citetitle>, we will refer to this set of directories as
      <literal>&lt;MIME&gt;</literal> in the rest of this document.</para>

      <para>For example, assuming default paths for the environment variables,
      <quote>Load
      <filename>&lt;MIME&gt;/text/plain.xml</filename></quote>
      means to load the following files:</para>

      <itemizedlist>
        <listitem>
          <para><filename>~/.local/share/mime/text/plain.xml</filename></para>
        </listitem>

        <listitem>
          <para><filename>/usr/local/share/mime/text/plain.xml</filename></para>
        </listitem>

        <listitem>
          <para><filename>/usr/share/mime/text/plain.xml</filename></para>
        </listitem>
      </itemizedlist>
    </sect2>

    <sect2 id="mimetypes-contents">
      <title>Contents</title>

      <para>The following is a list of directories and files that are found
      inside the MIME database along with brief descriptions:</para>

      <variablelist>
        <varlistentry>
          <term><filename>&lt;MIME&gt;/packages/</filename></term>

          <listitem>
            <para>This directory contains any number of XML files, each of
            which describe a collection of MIME types. By default, the
            <filename>freedesktop.org.xml</filename> file is installed in the
            <filename>/usr/share/mime/packages</filename> directory. This file
            contains all the default MIME types that are widely used and
            recognized.</para>

            <para>Applications which provide information about new MIME types
            are to install a <emphasis>single</emphasis> new XML file here.
            Depending on the prefix where the application is installed, it
            will create the file in the <filename>/mime/package</filename>
            subdirectory of one of the directories in
            <envar>$XDG_DATA_HOME:$XDG_DATA_DIRS</envar>. For example, an
            application installed to <filename>/usr/bin</filename> should
            install a new source XML file to the
            <filename>/usr/share/mime/packages</filename> directory. For more
            information about the XML files in the
            <filename>packages</filename> directory, please see <xref linkend="mimetypes-source-xml"/>.</para>
          </listitem>
        </varlistentry>

        <varlistentry>
          <term><filename>&lt;MIME&gt;/<replaceable>MEDIA</replaceable>/<replaceable>SUBTYPE</replaceable>.xml</filename></term>

          <listitem>
            <para>These directories and files are automatically generated from
            the collection of source XML files in the
            <filename>&lt;MIME&gt;/packages/</filename> subdirectory by the
            <application>update-mime-database</application> application. For
            example, for each <literal>mime-type</literal> element in the
            <filename>/usr/share/mime/packages/freedesktop.org.xml</filename>
            file, a directory is created at
            <filename>/usr/share/mime/</filename> with the <emphasis>media
            type</emphasis> of the MIME type. An XML file is created in that
            directory with the <emphasis>subtype identifier</emphasis> of that
            MIME type as well. The contents of the created XML file include
            comments (and translations for them), subclasses designations and
            aliases.</para>

            <example>
              <title>Example:
              <filename>/usr/share/mime/text/plain.xml</filename> file</title>

              <para><programlisting>&lt;?xml version='1.0' encoding='utf-8'?&gt;
&lt;mime-type xmlns="http://www.freedesktop.org/standards/shared-mime-info" type="text/plain"&gt;
&lt;!--Created automatically by update-mime-database. DO NOT EDIT!--&gt;
  &lt;comment&gt;plain text document&lt;/comment&gt;
  &lt;!-- possibly more translations --&gt;
  &lt;comment xml:lang="es"&gt;documento de texto sencillo&lt;/comment&gt;
  &lt;comment xml:lang="eu"&gt;testu soileko dokumentua&lt;/comment&gt;
  &lt;comment xml:lang="fi"&gt;perustekstiasiakirja&lt;/comment&gt;
  &lt;comment xml:lang="fr"&gt;document plein texte&lt;/comment&gt;
  &lt;!-- possibly more translations --&gt;
&lt;/mime-type&gt;
</programlisting>This file is generated by the
              <application>update-mime-database</application> application,
              using the default source XML file
              <filename>freedesktop.org.xml</filename>.</para>
            </example>
          </listitem>
        </varlistentry>

        <varlistentry>
          <term><filename>&lt;MIME&gt;/globs</filename></term>

          <listitem>
            <para>Contains one line with a MIME type and a glob pattern,
            separated by a colon. Files which match the glob pattern are
            resolved to the MIME type specified before the colon. There are
            special rules about how filenames are matched by the glob pattern;
            for more details see the <citetitle>XDG shared mime
            specification</citetitle>.</para>

            <para>This file is also generated by the
            <application>update-mime-database</application> application, using
            the default source XML file
            <filename>freedesktop.org.xml</filename>.</para>
          </listitem>
        </varlistentry>

        <varlistentry>
          <term><filename>&lt;MIME&gt;/magic</filename></term>

          <listitem>
            <para>A binary file which contains information on how to resolve
            MIME types by <quote>sniffing</quote> the content of the file.
            This is generally a set of one or more rules such as <quote>check
            for the string <literal>%PDF- </literal>at byte offset 0 in the
            file; if found, assign it the MIME type
            <literal>application/pdf</literal></quote>.</para>

            <para>This file is also generated by the
            <application>update-mime-database</application>
            application.</para>
          </listitem>
        </varlistentry>

        <varlistentry>
          <term><filename>&lt;MIME&gt;/XMLnamespaces</filename></term>

          <listitem>
            <para>Contains a mapping of XML namespaces to MIME types. Each
            line contains three fields, the namespace, the localName and the
            MIME type. Each field is separated by a space. If the localName is
            empty, then there are two spaces between the namespace and the
            MIME type.</para>

            <para>This file is also generated by the
            <application>update-mime-database</application>
            application.</para>
          </listitem>
        </varlistentry>

        <varlistentry>
          <term><filename>&lt;MIME&gt;/aliases</filename></term>

          <listitem>
            <para>Contains a list of aliases for each MIME type. An alias is
            simply a MIME type that is sometimes known as another type. For
            each line in this file there are two fields: the first field is
            the alias name, and the second field is the MIME type. The fields
            are separated by a space.</para>

            <para>This file is also generated by the
            <application>update-mime-database</application>
            application.</para>
          </listitem>
        </varlistentry>

        <varlistentry>
          <term><filename>&lt;MIME&gt;/subclasses</filename></term>

          <listitem>
            <para>Contains a list of subclassed MIME types and their
            <quote>parent</quote> MIME type. From the <citetitle>XDG shared
            mime specification:</citetitle><blockquote>
                <para>A type is a subclass of another type if any instance of
                the first type is also an instance of the second. For example,
                all image/svg files are also text/xml, text/plain and
                application/octet-stream files. Subclassing is about the
                format, rather than the catagory of the data (for example,
                there is no 'generic spreadsheet' class that all spreadsheets
                inherit from).</para>
              </blockquote>The format of this file is similar to the
            <filename>aliases</filename> file. Each line contains two fields,
            where the first field is the subclassed MIME type and the second
            field is the parent MIME type. Each field is separated by a
            space.</para>

            <para>This file is also generated by the
            <application>update-mime-database</application>
            application.</para>
          </listitem>
        </varlistentry>
      </variablelist>
    </sect2>

    <sect2 id="mimetypes-refresh">
      <title>Refreshing the MIME Database</title>

      <para>Understanding how to refresh the MIME database is important for
      administrators who wish to add new MIME types to the system, or
      otherwise modify information about a MIME type. The application
      <application>update-mime-database</application> is intended for this
      purpose.</para>

      <para>For example, if an application installs information about a new
      MIME type to <filename>/usr/share/mime/packages/diff.xml</filename>,
      then <application>update-mime-database</application> must be called with
      the parameter <filename>/usr/share/mime</filename>.</para>

      <screen><prompt>#</prompt> <userinput>update-mime-database /usr/share/mime</userinput>
<computeroutput>***
* Updating MIME database in /usr/share/mime...
***
</computeroutput></screen>

      <para>The MIME database is refreshed by scanning all the source XML
      files in the directory
      <filename>&lt;MIME&gt;/packages</filename>.</para>
    </sect2>
  </sect1>

  <sect1 id="mimetypes-source-xml">
    <title>The source XML files</title>
    <anchor id="mimetypes-11" />
    <anchor id="mimetypes-12" />
    <anchor id="mimetypes-2" />

    <indexterm>
      <primary>MIME types</primary>

      <secondary>source XML files</secondary>
    </indexterm>

    <para>Located in the <filename>&lt;MIME&gt;/packages</filename> directory,
    these XML files provide all the information regarding MIME types that is
    installed into the database by the
    <application>update-mime-database</application> application. There are a
    few rules about the XML file itself:</para>

    <itemizedlist>
      <listitem>
        <para>It must specify the namespace as
        <quote><literal>http://www.freedesktop.org/standards/shared-mime-info</literal></quote></para>
      </listitem>

      <listitem>
        <para>The root element must be <literal>mime-info</literal></para>
      </listitem>

      <listitem>
        <para>Zero or more <literal>mime-type</literal> elements can be
        specified as children of the <literal>mime-info</literal> element. The
        <literal>type</literal> attribute is used to specify the MIME type
        that is being defined.</para>
      </listitem>
    </itemizedlist>

    <para>By default, the <filename>freedesktop.org.xml</filename> file is
    installed to the <filename>packages</filename> directory in one of the
    <filename>&lt;MIME&gt;</filename> paths (usually
    <filename>/usr/share/mime/packages</filename>).</para>

    <para><xref linkend="mimetypes-mimeinfo-elements" /> gives a brief
    description for each of the elements that can occur as children to the
    <literal>mime-type</literal> element.</para>

    <table frame="topbot" id="mimetypes-mimeinfo-elements">
      <title>Child elements of <literal>&lt;mime-info&gt;</literal></title>

      <tgroup cols="2" colsep="0" rowsep="0">
        <colspec colwidth="44.90*" />

        <colspec colwidth="59.10*" />

        <thead>
          <row rowsep="1">
            <entry>Element (and attributes)</entry>

            <entry>Description</entry>
          </row>
        </thead>

        <tbody>
          <row>
            <entry><literal>&lt;glob
            pattern="<replaceable>*.xyz</replaceable>"&gt;</literal></entry>

            <entry>This element specifies a glob pattern against filenames. If
            the filename matches, then it is assigned the MIME type of the
            parent <literal>mime-type</literal> element. The
            <literal>pattern</literal> attribute is mandatory.</entry>
          </row>

          <row>
            <entry><literal>&lt;magic
            priority="<replaceable>50</replaceable>"&gt;</literal></entry>

            <entry>This element contains a list of <literal>match</literal>
            elements as its children. The <literal>priority</literal>
            attribute is optional, and specifies a priority between 0 and 100,
            with 100 being the highest matching priority. Each child
            <literal>match</literal> element has three required attributes:
            <simplelist type="inline">
                <member><literal>type</literal></member>

                <member><literal>offset</literal></member>

                <member><literal>value</literal></member>
              </simplelist> and a fourth optional attribute,
            <literal>mask</literal>. For details on these attributes, see the
            <ulink
            url="http://www.freedesktop.org/Standards/shared-mime-info-spec">XDG
            shared mime info specification</ulink>.</entry>
          </row>

          <row>
            <entry><literal>&lt;alias
            type="<replaceable>media</replaceable>/<replaceable>subtype</replaceable>"&gt;</literal></entry>

            <entry>This element defines an alias for the parent
            <literal>mime-type</literal> element. An alias is simply a MIME
            type that is sometimes known as another type. For example,
            <literal>application/x-pdf</literal> is an alias for the MIME type
            <literal>application/pdf</literal>.</entry>
          </row>

          <row>
            <entry><literal>&lt;sub-class-of
            type="<replaceable>media</replaceable>/<replaceable>subtype</replaceable>"&gt;</literal></entry>

            <entry>This element defines the parent
            <literal>mime-type</literal> element as a subclass of the MIME
            type specified in the <literal>type</literal> attribute. For
            example, <literal>image/svg</literal> is a sub class of the MIME
            type <literal>text/xml</literal>, <literal>text/plain</literal>,
            and <literal>application/octet-stream</literal>.</entry>
          </row>

          <row>
            <entry><literal>&lt;comment
            xml:lang="<replaceable>locale</replaceable>"&gt;</literal></entry>

            <entry>This element provides a human readable description for the
            MIME type. There can be zero or more occurrences of this element
            as long as each one contains a unique value for the
            <literal>xml:lang</literal> attribute.</entry>
          </row>

          <row>
            <entry><literal>&lt;root-XML
            namespaceURI="<replaceable>namespace</replaceable>"
            localName=""&gt;</literal></entry>

            <entry>If a file is determined to an XML file, then this element
            helps to further classify it through the use of the
            <literal>namespaceURI</literal> and <literal>localName</literal>
            attributes, both of which are required. The attribute
            <literal>namespaceURI</literal> is the namespace of the document,
            and <literal>localName</literal> is the name of the root element
            for the document. If <literal>localName</literal> is present but
            its value is empty, then the root element may have any name, but
            the namespace must still match.</entry>
          </row>
        </tbody>
      </tgroup>
    </table>

    <para>The easiest way to understand these files is to take a look at an
    example. Borrowing from the <citetitle>XDG shared mime
    specification</citetitle>, <xref linkend="mimetypes-source-xml-example" />
    displays the contents of a source XML file called
    <filename>diff.xml</filename>. This example defines the MIME type
    <literal>text/x-diff</literal>. There are multiple
    <literal>comment</literal> elements which give a human readable name to
    the MIME type in a number of different languages. This MIME type has both
    rules for matching through glob patterns <emphasis>and</emphasis> through
    the use of content <quote>sniffing</quote> (better known as
    <filename>magic</filename> rules). Any file with the extension
    <literal>.diff</literal> or <filename>.patch</filename> will resolve to
    this MIME type. Additionally any file whose contents start with the
    strings specified in the <literal>value</literal> attributes of the
    <literal>match</literal> element, will resolve to the
    <literal>text/x-diff</literal> MIME type.</para>

    <para>The order in which glob patterns and magic rules apply is beyond the
    scope of this document. For details on this, see the <ulink
    url="http://www.freedesktop.org/Standards/shared-mime-info-spec">XDG
    shared mime info specification</ulink>.</para>

    <example id="mimetypes-source-xml-example">
      <title>Example of a source XML file:
      <filename>diff.xml</filename></title>

      <programlisting>&lt;?xml version='1.0'?&gt;
&lt;mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'&gt;
  &lt;mime-type type="text/x-diff"&gt;
    &lt;comment&gt;Differences between files&lt;/comment&gt;
    &lt;comment xml:lang="af"&gt;verskille tussen lêers&lt;/comment&gt;
    &lt;!-- more translated comment elements --&gt;
    &lt;magic priority="50"&gt;
      &lt;match type="string" offset="0" value="diff\t"/&gt;
      &lt;match type="string" offset="0" value="***\t"/&gt;
      &lt;match type="string" offset="0" value="Common subdirectories: "/&gt;
    &lt;/magic&gt;
    &lt;glob pattern="*.diff"/&gt;
    &lt;glob pattern="*.patch"/&gt;
  &lt;/mime-type&gt;
&lt;/mime-info&gt;</programlisting>
    </example>
  </sect1>

  <sect1 id="mimetypes-modifying">
    <title>Modifying MIME types</title>

    <indexterm>
      <primary>MIME types</primary>

      <secondary>verifying changes</secondary>
    </indexterm>

    <indexterm>
      <primary>MIME types</primary>

      <secondary>modifying</secondary>
    </indexterm>

    <para>You should never directly modify the source XML files that are
    installed to the <filename>&lt;MIME&gt;/packages</filename> directory by
    applications. Instead, modify the <filename>Overrides.xml</filename> file.
    This file has precedence over all other source XML files installed into
    the same <filename>packages</filename> directory. If you are an
    application author, then this rule does not apply. You should create a new
    source XML file and place it in the proper
    <filename>&lt;MIME&gt;/packages</filename> directory (your
    <filename>Makefile</filename> will take care of this, of course).</para>

    <para>You can modify the MIME database for all users on the system or for
    a particular user depending on the location of the file you change. To
    modify the database for all users, make changes to the file
    <filename>Overrides.xml</filename> in the
    <filename>$XDG_DATA_DIRS/mime/packages</filename>
    directory. To modify the database for a single user, make changes to the
    file <filename>Overrides.xml</filename> in the
    <filename>$XDG_DATA_HOME/mime/packages</filename>
    directory.</para>

    <para>After changes are made, you must always run the
    <application>update-mime-database</application> application, with the
    directory location of the MIME database as the first parameter.</para>

    <sect2 id="mimetypes-addmodify">
      <title>Adding or Modifying MIME types</title>

      <para>To add one or more MIME types for all users:</para>

      <orderedlist>
        <listitem>
          <para>Create or modify an existing
          <filename>Overrides.xml</filename> source XML file, containing the
          definitions for the MIME types. For more information, see <xref
          linkend="mimetypes-source-xml" />.</para>
        </listitem>

        <listitem>
          <para>Place the <filename>Overrides.xml</filename> file in the
          <filename>/usr/share/mime/packages</filename> directory.</para>
        </listitem>

        <listitem>
          <para>Update the MIME database by running
          <application>update-mime-database</application> using the system
          account.<screen><userinput>update-mime-database /usr/share/mime</userinput></screen></para>
        </listitem>
      </orderedlist>

      <para>To add one or more MIME types for a single user, follow the same
      steps, except place your <filename>Overrides.xml</filename> file in the
      <filename>~/.local/share/mime/packages</filename> directory.
      Additionally, call <application>update-mime-database</application> with
      <filename>~/.local/share/mime/packages</filename> as the first
      parameter.</para>
    </sect2>

    <sect2 id="mimetypes-verifying">
      <title>Verifying Changes</title>
      <anchor id="mimetypes-10"/>

      <para>After you have made a change to the MIME database and refreshed
      its contents, you can verify that the change has taken effect using the
      <application>gnomevfs-info</application> application. This application
      prints the MIME type and other useful information about a file.</para>

      <para>Running <application>gnomevfs-info</application> on a SVG file
      gives you the output shown below. You'll notice the default application
      for this MIME type is <filename>eog.desktop</filename>; We will discuss
      default applications in <xref linkend="mimetypes-registering" />.</para>

      <screen><prompt>$</prompt> <userinput>gnomevfs-info mime-diagram.svg</userinput>
<computeroutput>Name              : mime-diagram.svg
Type              : Regular
MIME type         : image/svg+xml
Default app       : eog.desktop
Size              : 14869
Blocks            : 32
I/O block size    : 4096
Local             : YES
SUID              : NO
SGID              : NO
Sticky            : NO
Permissions       : 600644
Link count        : 1
UID               : 1000
GID               : 100
Access time       : Wed Feb 22 18:24:47 2006
Modification time : Wed Feb 22 18:24:42 2006
Change time       : Wed Feb 22 18:24:42 2006
Device #          : 775
Inode #           : 297252
Readable          : YES
Writable          : YES
Executable        : NO</computeroutput>
<prompt>$</prompt>
</screen>

      <para><xref linkend="mimetypes-newtype-example" /> walks through the steps of
      creating a new MIME type and then verifying the changes using
      <application>gnomevfs-info</application>.</para>
    </sect2>

    <sect2 id="mimetypes-newtype-example">
      <title><literal>application/x-newtype</literal> Example</title>

      <para>To create (or override) a MIME type and verify the changes:</para>

      <orderedlist>
        <listitem>
          <para>Make a new, empty file in your home directory called
          <filename>testing.xyz</filename>.</para>
        </listitem>

        <listitem>
          <para>Use <application>gnomevfs-info</application> on the file to
          find out the MIME type.The MIME type for this file should be
          detected as <literal>text/plain</literal> because there are not any
          glob patterns or magic rules that match it<footnote>
              <para>When no glob patterns or magic rules match a file, then it
              is resolved to the MIME type <literal>text/plain</literal> if it
              contains textual data or
              <literal>application/octet-stream</literal> for binary data. If
              the file is empty, then it defaults to
              <literal>text/plain</literal>.</para>
            </footnote>.</para>
        </listitem>

        <listitem>
          <para>Create (or modify) the <filename>Overrides.xml</filename> file
          as described in <xref linkend="mimetypes-addmodify" /> with the
          contents given in <xref linkend="mimetypes-overrides-xml" />.</para>
        </listitem>

        <listitem>
          <para>Refresh the database using
          <application>update-mime-database</application>.</para>
        </listitem>

        <listitem>
          <para>Use <application>gnomevfs-info</application> to verify that
          your change has taken effect. You should see the MIME type for the
          <filename>testing.xyz</filename> file resolved as
          <literal>application/x-newtype</literal>.</para>

          <screen><prompt>$</prompt> <userinput>gnomevfs-info testing.xyz | grep MIME
</userinput><computeroutput>MIME type         : application/x-newtype
</computeroutput><prompt>$</prompt></screen>
        </listitem>
      </orderedlist>

      <example id="mimetypes-overrides-xml">
        <title><filename>Overrides.xml</filename> file</title>

        <programlisting>&lt;?xml version='1.0' encoding='utf-8'?&gt;
&lt;mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info"&gt;
  &lt;mime-type type="application/x-newtype"&gt;&lt;comment&gt;new mime type&lt;/comment&gt;&lt;glob pattern="*.xyz"/&gt;&lt;/mime-type&gt;
&lt;/mime-info&gt;</programlisting>
      </example>
    </sect2>
  </sect1>

  <sect1 id="mimetypes-registering">
    <title>Registering Applications for MIME Types</title>

    <anchor id="mimetypes-7" />

    <indexterm>
      <primary>MIME types</primary>

      <secondary>registering applications for</secondary>
    </indexterm>

    <indexterm>
      <primary>applications</primary>

      <secondary>registry</secondary>
    </indexterm>

    <para>Registering applications to handle MIME types is fairly
    straightforward. Applications are registered by creating a
    <literal>MimeType</literal> key in their <filename>.desktop</filename>
    entry file and listing each MIME type separated by a semicolon. The
    <literal>MimeType</literal> key should only be used in
    <filename>.desktop</filename> files whose <literal>Type</literal> key has
    the value <quote><literal>Application</literal></quote>. For more
    information on <filename>.desktop</filename> files, see <xref
    linkend="menustructure-desktopentry" />.</para>

    <para>After creating or modifying a <filename>.desktop</filename> entry
    file, you must update the application database using the
    <application>update-desktop-database</application> application (very
    similar to <application>update-mime-database</application>, except it does not take a
    parameter). This will create a
    <filename>mimeinfo.cache</filename> file in the
    <filename>applications</filename> subdirectory for each directory in
    <filename>$XDG_DATA_HOME:$XDG_DATA_DIRS</filename>.
    The cache file is necessary so that all the <filename>.desktop</filename>
    files do not need to be scanned for just the <literal>MimeType</literal>
    key, as this causes unnecessary disk I/O.</para>

    <para>Default applications to use for specific MIME types should be
    specified in a file called <filename>defaults.list</filename>. This file
    is located in the <filename>applications</filename> subdirectory for each
    directory in <envar>$XDG_DATA_HOME</envar> and
    <envar>$XDG_DATA_DIRS</envar>. The format for this file consists of the
    MIME type, the <literal>=</literal> symbol and the Desktop File ID (which
    is the filename for the desktop entry file). <xref
    linkend="mimetypes-registering-example" /> is a short example of a
    <filename>defaults.list</filename> file in a user's
    <filename>~/.local/share/applications</filename> directory.</para>

    <example id="mimetypes-registering-example">
      <title>A User's <filename>defaults.list</filename> file</title>

      <programlisting>[Default Applications]
application/pdf=evince.desktop
text/html=epiphany.desktop
text/plain=gedit.desktop
image/jpeg=eog.desktop
image/png=eog.desktop
text/xml=gedit.desktop
</programlisting>
    </example>

    <note>
      <title>XDG Desktop Entry Specification</title>

      <para>How to register MIME types for applications is part of the <ulink
      url="http://www.freedesktop.org/wiki/Standards/desktop-entry-spec">XDG desktop entry specification</ulink>, rather than the
      <ulink
      url="http://www.freedesktop.org/Standards/shared-mime-info-spec">XDG
      shared mime info specification</ulink>.</para>
    </note>
  </sect1>

  <sect1 id="mimetypes-9">
    <title>Adding an Application to the GNOME Desktop</title>

    <indexterm>
      <primary>MIME types</primary>

      <secondary>adding applications</secondary>
    </indexterm>

    <indexterm>
      <primary>applications</primary>

      <secondary>adding</secondary>
    </indexterm>

    <para>To add an application to the GNOME Desktop, perform the following
    steps:</para>

    <orderedlist>
      <listitem>
        <para>Add a menu item for the application. For more information on how
        to add an item to a menu, see <xref
        linkend="menustructure-2" />.</para>
      </listitem>

      <listitem>
        <para>Add an icon for the application to
        <filename>/usr/share/icons/<replaceable>theme-name</replaceable>/<replaceable>icon-size</replaceable>/apps</filename>.
        For more information on icons and themes, see <xref
        linkend="themes-11" />.</para>
      </listitem>

      <listitem>
        <para>If the application uses a new MIME type, add a source XML file
        to the MIME database. For more information, see <xref
        linkend="mimetypes-modifying" />.</para>
      </listitem>

      <listitem>
        <para>If the application uses a new MIME type, add an icon for the
        MIME type to
        <filename>/usr/share/icons/<replaceable>theme-name</replaceable>/<replaceable>icon-size</replaceable>/mimetypes</filename>.
        For more information on icons and themes, see <xref
        linkend="themes-0" />.</para>
      </listitem>

      <listitem>
        <para>To associate the application with a MIME type, include a
        <literal>MimeType</literal> key in your <filename>.desktop</filename>
        file. For more information, see <xref
        linkend="mimetypes-registering" />.</para>
      </listitem>
    </orderedlist>
  </sect1>
</chapter>