In-depth analysis of Linux Desktop entry files

Source: Internet
Author: User
Tags ibm developerworks

 

From http://www.ibm.com/developerworks/cn/linux/l-cn-dtef/

The desktop entry file is used to describe the startup configuration of a program in a Linux Desktop System. The desktop entry file provides shortcuts similar to those in windows. This article describes in detail the definition, programming and usage of the Linux Desktop entry file. Readers can further understand the desktop entry file application through instance operations at the end of the article.

On Windows, you can easily open the target application by clicking a shortcut on the desktop or menu. This function is also available in modern Linux desktop systems. Currently, both Linux KDE and Linux GNOME desktop systems use the desktop entry file standard to describe the program STARTUP configuration information. The desktop entry file standards are developed by freeyuntop.org (http://freedesktop.org/wiki/) and the latest version is "desktop entry Specification 1.0" [1].

 



Back to Top

 


 

The desktop entry file is suffixed with ". desktop. Take the Linux GNOME desktop system as an example. After you open the application browser (see figure 1), you will see many application shortcuts. In fact, each application shortcut corresponds to a desktop entry file. These desktop entry files are usually stored in/usr/share/applications/opt/gnome/share/applications/and other directories. Access these directories from the file browser and click the corresponding desktop entry file to start the corresponding application.

Assume that the current "/usr/share/applications/" directory contains a file "CBT. desktop, open "CBT. desktop, you will get the following content:

[Desktop Entry]Version = 1.0Encoding = UTF-8Name = Quick Start TourGenericName = User TutorialComment = Computer Based Training tutorial to guide and help you learn how to use the Desktop|-------10--------20--------30--------40--------50--------60--------70--------80--------9||-------- XML error:  The previous line is longer than the max of 90 characters ---------|Exec = gnome-open /usr/share/doc/manual/sled-gnome-cbt_en/index.htmlIcon = cbtStartupNotify = trueTerminal = falseType = ApplicationCategories = GNOME;Application;Documentation;OnlyShowIn = GNOME;X-SuSE-translate = trueName[cs] = Rychlá prohlídka systémuComment[cs] = V?ukov? program seznamující u?ivatele se základy pracovního prost?edíGenericName[cs] = U?ivatelsk? tutoriálName[hu] = RendszerbemutatóComment[hu] = A munkaállomés használatát bemutató segédletGenericName[hu] = Felhasználói segédlet

 

This article will focus on parsing the file structure of the desktop entry in the next section based on the above "CBT. desktop" file content. Readers can thoroughly understand the specific meanings of the above statements.

 



Back to Top

 

The desktop entry file usually starts with the string "[desktop entry. As you can see from List 1, the content of the desktop entry file is composed of several {keywords, values} paired entries. For example, "version" is a keyword, and the value corresponding to the keyword "version" is "1.0 ". The desktop entry file defines a series of standard keywords. Standard keywords are mandatory and optional: required standard keywords must be defined in the. desktop file, and Optional keywords are not required. The following describes key keywords.

  • Keyword "version": [Optional] This value specifies the standard version of the desktop entry file to be followed by the current desktop entry file.
  • Keyword "encoding": [not recommended for version 1.0] This value specifies the encoding method used by a specific string in the current desktop entry file. Although this keyword is not recommended in standard 1.0 of the desktop entry file, it is still widely used in existing desktop entry files due to historical reasons.
  • Keyword "name": [required]
    This value specifies the name of the application. For example, the value of the keyword "name" in Listing 1 is "Quick Start tour ". Open the file browser and go to the "/usr/share/Applications" directory to view the display style of the shortcut defined in the "CBT. desktop" file, as shown in figure 2. The display name of the shortcut is determined by the value of the keyword "name", and the icon used by the shortcut is determined by the value of the keyword "icon" to be introduced below. Of course, these definitions also apply in the application browser. See figure 3.


     

  • Keyword "genericname": [Optional]
    This value specifies the common name of the application. For example, the value of the keyword "genericname" in Listing 1 is "User tutorial ". Open the application browser and you will see that the string "User tutorial" is displayed on the right of the icon, as shown in 3:


     

  • Keyword "comment": [Optional]
    This value is a brief description of the current desktop entry.
  • Keyword "type": [required]
    The keyword "type" defines the type of the desktop entry file. Common "type" values are "application" and "Link ". "Type = Application" indicates that the current desktop entry file points to an application, and "type = link" indicates that the current desktop entry file points to a URL (Uniform Resource Locator ).
  • Keyword "EXEC": [Optional]
    The keyword "EXEC" is valid only when the "type" type is "application. The value of "EXEC" defines the command to be executed to start a specified application. This command can contain parameters. In this example, the value of the keyword "EXEC" is the string "gnome-Open/usr/share/doc/manual/sled-gnome-cbt_en/index.html ". Enter this string in shell and press the Enter key to start the specified application.
  • Keyword "url": [Optional]
    The keyword "url" makes sense only when the "type" type is "Link. The value of "url" defines the URL to which the desktop entry file points. For example:

    List 2 "type = link" type desktop entry file example 

    Type = LinkURL = http://www.ibm.com/developerworks

    Double-click the desktop entry file that contains the preceding content to start the web browser and open the specified web page "http://www.ibm.com/developerworks". See figure 4 for running results.

  • Keyword "icon": [Optional]
    This value defines the icon displayed in the current desktop entry file in the application browser or file browser. If the value of the keyword "icon" is given in absolute path format, the icon file specified by the value will be used. Otherwise, in Linux, "icon theme specification" [2] is used to locate the desired icon file under the icon directory specified by the system. For example, in this example, the value of the keyword "icon" is "CBT", which corresponds to the image file "cbt.png" in the directory specified by the system ". This image is used as the icon display result 2, as shown in figure 3.
  • Keyword "startupnotify": [Optional]
    The value of the keyword "startupnotify" is a Boolean value (true or false ). This keyword is valid only when the "type" type is "application. The numerical value is defined by the standard "Startup notification protocol specifications" [3] and will not be detailed here.
  • Keyword "terminal": [Optional]
    Like "startupnotify", the value of the keyword "startupnotify" is also a Boolean value, and this keyword is meaningful only when "type" is "application. The value indicates whether the application (that is, the value of the keyword "EXEC") needs to run in the terminal window. This article describes how to use the keyword "terminal" in the next section.
  • Keyword "categories": [Optional]
    The keyword "categories" makes sense only when the "type" type is "application. The value of "categories" indicates the category displayed by the relevant application in the menu. The specific menu category is defined by the specification "desktop specification menu" [4].
  • Keyword "onlyshowin" and "notshowin": [Optional]
    These two keywords define whether the current desktop entry is displayed in a specific Linux Desktop System (for example, Linux gnome or Linux KDE) (defined by "onlyshowin ), or not displayed (defined by "notshowin ). For specific definitions, see "desktop specification menu" [4].
  • Keyword "X-Suse-translate": [SuSE Linux-specific]
    The keyword "X-Suse-translate" is unique to SuSE Linux (http://www.novell.com/linux. "X-Suse-translate" meets the SuSE RPM package style. "X-Suse-translate" indicates whether to translate the keyword "name" and "genericname. For details, see "SuSE package conventions" [5].
  • Localized keyword "[locale]"
    According to the "desktop entry specification" Specification [1], you can add the string "[locale]" after the keyword to define the specific localization of this keyword. The valid value of "locale" is:
    LOCALE= lang_COUNTRY.ENCODING@MODIFIER

    Here, the domain "_ country", ". encoding" and "@ modifier" can be ignored. When the specified desktop entry file is parsed, the parser should correctly obtain the localized keyword value based on the current POSIX locale. For example, listing 1 defines the different values of the keywords "name", "comment", and "genericname" in the "CS" and "Hu" Language Environments.

  • Other keywords
    In addition to the preceding keywords in Listing 1, "desktop entry specification" also defines Optional keywords such as "hidden", "tryexec", and "mimetype. You can select as needed.

 



Back to Top

 

The desktop entry file is a common Linux File Format. Many Linux programs need to support this file. This article describes how to compile and analyze the desktop entry file.

The first step in operating the desktop entry file is to obtain the file content. Suppose there is a desktop entry file whose path information is stored in the variable ppath:

const char* pPath;

The following code reads the file content into the memory "buffer.

int file_size = 0;char *file_contents = NULL;char *buffer = NULL;if( eel_read_entire_file ( pPath, &file_size, &file_contents ) == GNOME_VFS_OK ){    buffer = (char *)g_realloc ( file_contents, file_size + 1 );    buffer[file_size] = '/0';}else{    return 1;}

 

After obtaining the content of the desktop entry file, you can further analyze the file content. Here, the analysis focuses on obtaining the keyword "type", "EXEC"/"url", and the value of "terminal. First, define the structure destopentrytype:

enum DestopEntryType{    Application, // Type = Application    Link,             // Type = Link    Unknown};

 

The following program extracts the values of the keyword "type", "EXEC"/"url", and "terminal", and stores these values in the variable "type ", "Uri" and "bterminal.

    DestopEntryType type = Unknown;char *uri = NULL;bool bTerminal = false;GnomeDesktopItem *desktop_file;desktop_file = gnome_desktop_item_new_from_string( NULL, buffer, file_size,(GnomeDesktopItemLoadFlags)0, NULL );if ( !desktop_file ){    g_free( buffer );    return 1;}const char *strType = gnome_desktop_item_get_string( desktop_file, "Type" );if ( !strType ){    g_free( buffer );    gnome_desktop_item_unref ( desktop_file );    return 1;}if ( 0 == strcmp( strType, "Application" ) )    //type = Application{    const char *exec_str = gnome_desktop_item_get_string( desktop_file, "Exec" );    if( !exec_str )    {        g_free( buffer );        gnome_desktop_item_unref( desktop_file );        return 1;    }   uri = g_strdup( exec_str );    type = Application;    const char *strTerminal = gnome_desktop_item_get_string( desktop_file, "Terminal" );    if ( strTerminal )    {        if ( 0 == strcmp( "true", strTerminal ) )            bTerminal = true;        else            bTerminal = false;    }}else if(strcmp(strType, "Link") == 0)    //type = Link{    uri = g_strdup( gnome_desktop_item_get_string( desktop_file, "URL" ) );    type = Link;}    g_free( buffer );    gnome_desktop_item_unref( desktop_file );    

 

With the values of the keyword "type", "EXEC", and "terminal", you can run the desktop entry file as follows.

if ( type == Application ){    if( bTerminal )        eel_gnome_open_terminal_on_screen( uri, NULL );    else        eel_gnome_shell_execute_on_screen( uri, NULL);    g_free( uri );    return 0;}

 

With the value of the keyword "type", "url", and "terminal", you can run the desktop entry file as follows.

if ( type == Link ){    gnome_url_show( uri, NULL );    g_free( uri );    return 0;}

 



Back to Top

 

In this section, we will provide two instances for creating the desktop entry file. Both instances aim to create shortcuts for automatically accessing the IBM developerworks website, as shown in Operation Result 4. The two instances will use different methods to achieve this goal. The first instance will create a file type of "application" desktop entry file "VisitDeveloperWorks-Application.desktop"; the second instance will create a file type of "Link" desktop entry file "VisitDeveloperWorks-Link.desktop ".


 

Assume that the image file "gaim.png" is stored in the icon directory specified by the system ". 5. edit the file "VisitDeveloperWorks-Application.desktop" and save the results under the "/usr/share/applications/" directory.


 

The core content of this file is to set the application icon to the "gaim.png" file, and set the type of the desktop entry file to "application ", and set the command to be executed by the application to "Firefox http://www.ibm.com/developerworks ". After editing, you can see the display style of the instance in the file browser and application browser (as shown in 6.


 

Modify the above "VisitDeveloperWorks-Application.desktop" file 7 and rename it "VisitDeveloperWorks-Link.desktop" to save it under the "/usr/share/applications/" directory.


 

The core content of this file is to set the type of the desktop entry file to "Link" and set the URL to which the desktop entry file points to "http://www.ibm.com/developerworks ". After editing, you can see the display style of the instance in the file browser (as shown in 8. It is worth noting that because this instance is not an application, you cannot see the corresponding shortcuts in the application browser.




Back to Top

 

The desktop entry file describes the standard program startup configurations in Linux KDE and Linux GNOME desktop systems. This article discusses the definition and application of the file format in depth. For more detailed usage and programming information, see references.

 

  • [1] "desktop entry specification ".
  • [2] "icon theme specification ".
  • [3] "Startup notification protocol specifications ".
  • [4] "desktop specification menu ".
  • [5] "SuSE package conventions ".

 

 

Yi Yiping, software engineer, wplc department, IBM China Software Development Center. He is mainly engaged in the research and development of notes Linux products. Research interests include cross-platform transplantation of Windows applications, GDI development, network device development, and scheduling algorithm research. Contact: gongyp@cn.ibm.com.

 

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.