12.3. 配置文件关联
12.3.1. 什么是 MIME 类型?
在 GNOME 中,MIME(多用途互联网邮件扩展)类型用来识别文件的格式。GNOME 桌面使用 MIME 类型来:
- 确定默认情况下,哪个应用应打开特定的文件格式。
- 注册那些也可以打开特定文件格式的其他应用程序。
- 提供描述文件类型的字符串,例如,在 Files 应用程序的文件属性对话框中。
- 提供代表特定文件格式的图标,例如,在 Files 应用程序的文件属性对话框中。
MIME 类型名称采用给定格式:
media-type/subtype-identifier
例 12.7. MIME 类型格式
image/jpeg
是 MIME 类型的示例,其中 image
是介质类型,jpeg
是子类型标识符。
GNOME 遵循 freedesktop.org Shared MIME Info 规范来决定:
- 机器范围和特定于用户的位置来保存所有 MIME 类型规范文件。
- 如何注册 MIME 类型,以便桌面环境知道可使用哪个应用程序来打开特定的文件格式。
- 用户如何更改哪些应用程序应打开哪些文件格式。
12.3.1.1. 什么是 MIME 数据库?
MIME 数据库是所有 MIME 类型规范文件的集合,GNOME 用来存储有关已知 MIME 类型的信息。
从系统管理员的角度来看,MIME 数据库最重要的部分是
/usr/share/mime/packages/
目录,其中存储了指定已知 MIME 类型信息的 MIME 类型相关文件。此类文件的一个示例是 /usr/share/mime/packages/freedesktop.org.xml
,指定系统上默认可用的标准 MIME 类型的信息。该文件由 shared-mime-info 软件包提供。
获取更多信息
有关描述 MIME 类型系统的详细信息,请参阅 freedesktop.org 网站上的 freedesktop.org 共享 MIME 信息规范 :
12.3.2. 为所有用户添加自定义 MIME 类型
要为系统上的所有用户添加自定义 MIME 类型,并为该 MIME 类型注册默认应用程序,您需要在
/usr/share/mime/packages/
目录中创建一个新的 MIME 类型规格文件,并在 /usr/share/applications/
目录中创建一个 .desktop
文件。
过程 12.3. 为所有用户添加自定义 application/x-newtype
MIME 类型
- 创建
/usr/share/mime/packages/application-x-newtype.xml
文件:<?xml version="1.0" encoding="UTF-8"?> <mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info"> <mime-type type="application/x-newtype"> <comment>new mime type</comment> <glob pattern="*.xyz"/> </mime-type> </mime-info>
上面的示例application-x-newtype.xml
文件定义了一个新的 MIME 类型application/x-newtype
,并将扩展名.xyz
的文件名分配给该 MIME 类型。 - 创建一个新的
.desktop
文件,例如myapplication1.desktop
,并将其放在/usr/share/applications/
目录中:[Desktop Entry] Type=Application MimeType=application/x-newtype Name=My Application 1 Exec=myapplication1 field_code
上面的示例myapplication1.desktop
文件将application/x-newtype
MIME 类型与名为 My Application 1 的应用程序相关联,后者由命令 myapplication1 运行。根据myapplication1
的启动方式,从 Desktop Entry Specification 中选择一个对应的字段代码。例如,对于能够打开多个文件的应用程序,请使用:Exec=myapplication1 %F
- 以 root 用户身份,更新 MIME 数据库以使更改生效:
#
update-mime-database /usr/share/mime
- 以 root 用户身份更新应用程序数据库:
#
update-desktop-database /usr/share/applications
- 要验证您是否已成功将 If
xyz 文件与
application/x-newtype
MIME 类型关联,请首先创建一个空文件,如test.xyz
:$
touch test.xyz
然后运行 gvfs-info 命令:$
gvfs-info test.xyz | grep "standard::content-type"
standard::content-type: application/x-newtype - 要验证
myapplication1.desktop
是否已正确设置为application/x-newtype
MIME 类型的默认注册的应用程序,请运行 gvfs-mime --query 命令:$
gvfs-mime --query application/x-newtype
Default application for 'application/x-newtype': myapplication1.desktop Registered applications: myapplication1.desktop Recommended applications: myapplication1.desktop
12.3.3. 为单个用户添加自定义 MIME 类型
要为单个用户添加自定义 MIME 类型,并为该 MIME 类型注册默认应用程序,您需要在
~/.local/share/mime/packages/
目录中创建一个新的 MIME 类型规格文件,并在 ~/.local/share/applications/
目录中创建一个 .desktop
文件。
过程 12.4. 为单个用户添加一个自定义 application/x-newtype
MIME 类型
- 创建
~/.local/share/mime/packages/application-x-newtype.xml
文件:<?xml version="1.0" encoding="UTF-8"?> <mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info"> <mime-type type="application/x-newtype"> <comment>new mime type</comment> <glob pattern="*.xyz"/> </mime-type> </mime-info>
上面的示例application-x-newtype.xml
文件定义了一个新的 MIME 类型application/x-newtype
,并将扩展名.xyz
的文件名分配给该 MIME 类型。 - 创建一个新的
.desktop
文件,例如myapplication1.desktop
,并将其放在~/.local/share/applications/
目录中:[Desktop Entry] Type=Application MimeType=application/x-newtype Name=My Application 1 Exec=myapplication1 field_code
上面的示例myapplication1.desktop
文件将application/x-newtype
MIME 类型与名为 My Application 1 的应用程序相关联,后者由命令 myapplication1 运行。根据myapplication1
的启动方式,从 Desktop Entry Specification 中选择一个对应的字段代码。例如,对于能够打开多个文件的应用程序,请使用:Exec=myapplication1 %F
- 更新 MIME 数据库以使您的更改生效:
$
update-mime-database ~/.local/share/mime
- 更新应用程序数据库:
$
update-desktop-database ~/.local/share/applications
- 要验证您是否已成功将 If
xyz 文件与
application/x-newtype
MIME 类型关联,请首先创建一个空文件,如test.xyz
:$
touch test.xyz
然后运行 gvfs-info 命令:$
gvfs-info test.xyz | grep "standard::content-type"
standard::content-type: application/x-newtype - 要验证
myapplication1.desktop
是否已正确设置为application/x-newtype
MIME 类型的默认注册的应用程序,请运行 gvfs-mime --query 命令:$
gvfs-mime --query application/x-newtype
Default application for 'application/x-newtype': myapplication1.desktop Registered applications: myapplication1.desktop Recommended applications: myapplication1.desktop
12.3.4. 覆盖所有用户的默认注册的应用程序
/usr/share/applications/mimeapps.list
和 /usr/share/applications/[desktop environment name]-mimeapps.list
文件是软件包安装的默认值,它指定默认注册哪个应用程序来打开特定的 MIME 类型。
要覆盖系统上所有用户的系统默认值,系统管理员需要创建
/etc/xdg/mimeapps.list
或 /etc/xdg/[desktop environment name]-mimeapps.list
文件,其中包含他们为其覆盖默认注册的应用程序的 MIME 类型列表。
应用配置的顺序如下:
/usr/share/applications/
/etc/xdg/
在特定的地方,按以下顺序应用配置:
mimeapps.list
[desktop environment name]-mimeapps.list
因此,系统管理员的配置优先于软件包配置。在每个配置中,特定于桌面的配置优先于不指定桌面环境的配置。
7.5 之前的 Red Hat Enterprise Linux 版本使用
defaults.list
文件,而不是 mimeapps.list
文件。
过程 12.5. 覆盖所有用户的默认注册的应用程序
- 查阅
/usr/share/applications/mimeapps.list
文件,以确定您要更改默认注册的应用程序的 MIME 类型。例如,以下mimeapps.list
文件示例指定了text/html
和application/xhtml+xml
MIME 类型的默认注册的应用程序:[Default Applications] text/html=firefox.desktop application/xhtml+xml=firefox.desktop
默认应用程序(Firefox)通过指定对应的 .desktop 文件(firefox
)来定义。其他应用程序的.desktop
.desktop
文件的默认位置为/usr/share/applications/
。 - 创建
/etc/xdg/mimeapps.list
文件。在文件中,指定 MIME 类型及其相应的默认注册的应用程序:[Default Applications] text/html=myapplication1.desktop application/xhtml+xml=myapplication2.desktop
这会将text/html
MIME 类型的默认注册的应用程序设为myapplication1.desktop
,将application/xhtml+xml
MIME 类型的默认注册的应用程序设为myapplication2.desktop
。要使这些设置正常工作,请确保myapplication1.desktop
和myapplication2.desktop
文件都放在/usr/share/applications/
目录中。 - 您可以使用 gvfs-mime query 命令来验证默认注册的应用程序是否已正确设置:
$
gvfs-mime query text/html
Default application for 'text/html': myapplication1.desktop Registered applications: myapplication1.desktop firefox.desktop Recommended applications: myapplication1.desktop firefox.desktop
12.3.5. 覆盖单个用户的默认注册的应用程序
/usr/share/applications/mimeapps.list
和 /usr/share/applications/[desktop environment name]-mimeapps.list
文件是软件包安装的默认值,它指定默认注册哪个应用程序来打开特定的 MIME 类型。
要覆盖单个用户的系统默认值,您需要创建
~/.local/share/applications/mimeapps.list
或 ~/.local/share/applications/[desktop environment id]-mimeapps.list
文件,其中包含您要为其覆盖默认注册的应用程序的 MIME 类型列表。
应用配置的顺序如下:
/usr/share/applications/
/etc/xdg/
~/.local/share/application/
在特定的地方,按以下顺序应用配置:
mimeapps.list
[desktop environment name]-mimeapps.list
因此,用户的配置优先于系统管理员的配置,系统管理员的配置优先于软件包配置。在每个配置中,特定于桌面的配置优先于不指定桌面环境的配置。
7.5 之前的 Red Hat Enterprise Linux 版本使用
defaults.list
文件,而不是 mimeapps.list
文件。
过程 12.6. 覆盖单个用户的默认注册的应用程序
- 查阅
/usr/share/applications/mimeapps.list
文件,以确定您要更改默认注册的应用程序的 MIME 类型。例如,以下mimeapps.list
文件示例指定了text/html
和application/xhtml+xml
MIME 类型的默认注册的应用程序:[Default Applications] text/html=firefox.desktop application/xhtml+xml=firefox.desktop
默认应用程序(Firefox)通过指定对应的 .desktop 文件(firefox
)来定义。其他应用程序的.desktop
.desktop
文件的系统默认位置为/usr/share/applications/
。单个用户的.desktop
文件可以保存在~/.local/share/applications/
中。 - 创建
~/.local/share/applications/mimeapps.list
文件。在文件中,指定 MIME 类型及其相应的默认注册的应用程序:[Default Applications] text/html=myapplication1.desktop application/xhtml+xml=myapplication2.desktop
这会将text/html
MIME 类型的默认注册的应用程序设为myapplication1.desktop
,将application/xhtml+xml
MIME 类型的默认注册的应用程序设为myapplication2.desktop
。要使这些设置正常工作,请确保myapplication1.desktop
和myapplication2.desktop
文件都放在/usr/share/applications/
目录中。 - 您可以使用 gvfs-mime --query 命令来验证默认注册的应用程序是否已正确设置:
$
gvfs-mime --query text/html
Default application for 'text/html': myapplication1.desktop Registered applications: myapplication1.desktop firefox.desktop Recommended applications: myapplication1.desktop firefox.desktop