Class DesktopFile


  • public class DesktopFile
    extends Object
    Parser and writer for Linux .desktop files.

    Instance-based API for reading and writing .desktop files. Supports all standard [Desktop Entry] section fields plus custom keys. This class provides a convenient way to manage desktop files for both application launching and URI scheme registration.

    Note: This class will eventually move to org.scijava.util.DesktopFile in scijava-common once the design is finalized.

    Author:
    Curtis Rueden
    • Constructor Detail

      • DesktopFile

        public DesktopFile​(Path path)
        Creates a DesktopFile instance for the given path.

        If the file exists, it will be loaded when load() is called. If it doesn't exist, the entries map will be empty until populated programmatically or load() is called.

        Parameters:
        path - the path to the .desktop file
    • Method Detail

      • path

        public Path path()
        Gets the file path for this desktop file.
        Returns:
        the path
      • exists

        public boolean exists()
        Checks if the file exists on disk.
        Returns:
        true if the file exists
      • load

        public void load()
                  throws IOException
        Loads the .desktop file from disk.

        Clears any existing entries and comments, then reads from the file. If the file doesn't exist, entries will be empty after this call.

        Throws:
        IOException - if reading fails
      • save

        public void save()
                  throws IOException
        Saves the .desktop file to disk.

        Creates parent directories if needed. Overwrites any existing file.

        Throws:
        IOException - if writing fails
      • delete

        public void delete()
                    throws IOException
        Deletes the file from disk.
        Throws:
        IOException - if deletion fails
      • parse

        public static DesktopFile parse​(Path path)
                                 throws IOException
        Parses a .desktop file from disk (static convenience method).
        Parameters:
        path - Path to the .desktop file
        Returns:
        Parsed DesktopFile
        Throws:
        IOException - if reading fails
      • writeTo

        public void writeTo​(Path path)
                     throws IOException
        Writes the .desktop file to disk (for backward compatibility).
        Parameters:
        path - Path to write to
        Throws:
        IOException - if writing fails
      • get

        public String get​(String key)
        Gets the value for a key.
        Parameters:
        key - The key
        Returns:
        The value, or null if not present
      • set

        public void set​(String key,
                        String value)
        Sets a key-value pair.
        Parameters:
        key - The key
        value - The value
      • getType

        public String getType()
      • setType

        public void setType​(String type)
      • getVersion

        public String getVersion()
      • setVersion

        public void setVersion​(String version)
      • getName

        public String getName()
      • setName

        public void setName​(String name)
      • getGenericName

        public String getGenericName()
      • setGenericName

        public void setGenericName​(String genericName)
      • getComment

        public String getComment()
      • setComment

        public void setComment​(String comment)
      • getExec

        public String getExec()
      • setExec

        public void setExec​(String exec)
      • getIcon

        public String getIcon()
      • setIcon

        public void setIcon​(String icon)
      • getPath

        public String getPath()
      • setPath

        public void setPath​(String path)
      • getTerminal

        public boolean getTerminal()
      • setTerminal

        public void setTerminal​(boolean terminal)
      • getCategories

        public String getCategories()
      • setCategories

        public void setCategories​(String categories)
      • getTryExec

        public String getTryExec()
      • setTryExec

        public void setTryExec​(String tryExec)
      • getStartupNotify

        public boolean getStartupNotify()
      • setStartupNotify

        public void setStartupNotify​(boolean startupNotify)
      • getStartupWMClass

        public String getStartupWMClass()
      • setStartupWMClass

        public void setStartupWMClass​(String startupWMClass)
      • hasMimeType

        public boolean hasMimeType​(String mimeType)
        Checks if a MimeType entry contains a specific MIME type.
        Parameters:
        mimeType - The MIME type to check (e.g., "x-scheme-handler/myapp")
        Returns:
        true if the MimeType field contains this type
      • addMimeType

        public void addMimeType​(String mimeType)
        Adds a MIME type to the MimeType field.

        The MimeType field is a semicolon-separated list. This method appends the new type if it's not already present.

        Parameters:
        mimeType - The MIME type to add (e.g., "x-scheme-handler/myapp")
      • removeMimeType

        public void removeMimeType​(String mimeType)
        Removes a MIME type from the MimeType field.
        Parameters:
        mimeType - The MIME type to remove