File Association in linux

Source: Internet
Author: User
Tags bmp image
File Association in linux-general Linux technology-Linux technology and application information. For details, refer to the following section. Source: http://blog.csdn.net/absurd/

We know that in Windows, double-click an executable file and the File Manager will automatically run the application. When you double-click a data file, the file manager opens it with the associated application. The association between data files and applications is achieved through the registry: the File Manager queries the registry, finds the application corresponding to the data file, and then runs the application, and pass the data file name as a command line parameter to it.

This file association method is very easy to use, saving the trouble of starting an application before opening the file. The desktop environment in Linux also has similar functions, and the implementation method is more reasonable. Recently, I was responsible for developing a resource manager. Originally, GNOME had a powerful resource manager Nautilus, but it was too large and not only had over 0.1 million lines of code, it also depends on libgnome, gnome-vfs, and CORBA, so it is not suitable for embedded applications. In the end, we decided to develop a simplified Resource Manager by ourselves, but we had to try to be compatible with existing applications as much as possible. In this case, we need to understand the relevant standards, and the file association method is one of them. Take notes on the knowledge learned over the past few days for your reference:

First, let's see what to do with file association.

1. Association of data files with applications. An application can only open certain data files, such as PNG, BMP, and JPEG. Opening a word has a broad meaning, including opening, playing, installing, editing, and printing.

2. file type information. When the resource manager lists data files, it usually uses an icon to identify such files and adds a short name, so that users can easily distinguish it from other types of files.

Next we will look at how it is implemented in linux.

1. Determine the file type. The number of files is infinite. We can only process the files by file type. How can I determine the file type to which a file belongs? It may be said that it is very easy to distinguish between extensions. Yes, it can be done with the extension, but this method has two drawbacks: on the one hand, it is not very precise, and the types of files with the same extension may be completely different, such as dat files, it may be a video file, it may also be a common data file. On the other hand, it is not very accurate, and the extension can be changed at any time. For some purpose, you can change the exe extension to the htm extension.

In addition, in Linux, the extension is only an option. Many files do not have an extension at all. Therefore, it is definitely not feasible to simply use the extension. To better determine the file type, two methods are used in linux: magic and file extension. The so-called magic method is to judge based on the content of the file. The vast majority of files contain some specific internal tags, which are called magic. For example, if a BMP image file starts with BM, BM is a magic. Even if the double insurance mechanism is adopted, there is a possibility of misjudgment, but the probability has been greatly reduced.

2. file type representation.

How is the file type represented? JPEG is an image file, txt is a text file, and WML is an XML file. This classification is intuitive, but there are also several problems: for JPEG files, the image files are too general. Some Image Browsing tools can open most image files, but they may not open all image files. They need more detailed file type information. For txt and WML, they are actually text files, and some editors may process them in the same way. To avoid too small or too rough classification, linux adopts the MIME (refer to the RFC) specification, which uses a hierarchical classification method, such:

JPEG file: image/jpeg

Text File: text/plain

XML file: text/xml

This classification method can take both width and width into consideration.

3. file type data information.

In linux, file types are usually stored in/usr/share/mime,/usr/local/share/mime, and User Directories. All applications can share this information. The following files are generally stored in this directory:

Aliases: the alias of the file type. For example, application/pdf is sometimes called application/x-pdf.

Magic: internal identifier of various files, used to determine the file type from the file content. For example, a BMP image file starts with BM.

Globs: the correspondence between the extension and the file type. For example, the *. cpp file is of the text/x-c ++ src type.

Packages Directory: used to install new file types.

Other subdirectories and their files: More detailed descriptions of various file types. For example, the jpeg. xml file in the image describes the jpeg file type. To facilitate internationalization, the descriptions are available in various language versions.

4. association between Icon files and data files.

In resource manager, different icons are usually used to differentiate different file types. At the same time, icons are also related to desktop themes. different themes have different sizes and appearances. Icon files are usually stored in the/usr/share/icons/topic/size/mimetypes directory.

The correspondence between file types and Icon files is achieved through file names. For example, the image file corresponding to the hosts file is gnome-mime-image-demo.png.

(This is not very definite, and further research is needed)

5. association between applications and data files.

The association between applications and data files is implemented through the. desktop file. The application must appear in the Start Menu or on the desktop. It must provide a desktop file. After the application is installed, the desktop file is usually installed under/usr/share/applications.

You can specify the file types that can be operated on in the desktop file. For example, the package installer can operate on rpm files, and its desktop file (system-install-packages.desktop) content is:

[Desktop Entry]

Name = Install Packages

GenericName = Install Packages

Comment = Install new packages on the system

MimeType = application/x-rpm;

Exec =/usr/bin/system-install-packages % F

Terminal = false

Type = Application

Iconw.system-config-packages.png

Encoding = UTF-8

NoDisplay = true

The MimeType item indicates that it can operate files of the rpm type.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.