game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
desktop_entry_utils.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// desktop_entry_utils.h
4// ==============================================================================
5// XDG Desktop Entry Specification helpers.
6//
7// References:
8// https://specifications.freedesktop.org/desktop-entry-spec/latest/
9// https://specifications.freedesktop.org/desktop-entry-spec/latest/exec-variables.html
10//
11// These helpers exist as a tiny standalone unit (rather than file-static
12// functions inside installation_executor.cpp) so they can be unit-tested
13// directly. Both are pure functions with no global state.
14// ==============================================================================
15
16#pragma once
17
18#include <string>
19
21{
22
23// Strip newlines and other control characters that would corrupt the INI-like
24// .desktop file format. Used for `Name=`, `Comment=`, `Path=`, `Categories=`,
25// `Keywords=` etc. The XDG spec says these fields are of type `string`,
26// `localestring`, or `iconstring` and disallow embedded `\n`/`\r`.
27//
28// Caller must apply this to any user-supplied (game name, prefix path, custom
29// comment) value before writing to a .desktop key=value line.
30std::string sanitizeField(const std::string& s);
31
32// Quote a single argument for a `.desktop` Exec= line per the XDG Desktop
33// Entry Specification.
34//
35// The Exec= value uses its own quoting rules — *NOT* shell. Per the spec:
36// - Quoting must be done by enclosing the argument between *double* quotes.
37// - The reserved characters `"`, `` ` ``, `$`, `\` MUST be escaped with a
38// leading backslash inside the quoted value.
39// - Single-quote is itself a reserved character; shell-style single-quote
40// wrapping (which `ShellUtils::escapeArg` produces) violates the spec
41// even though some parsers (GLib's `g_shell_parse_argv` historically)
42// tolerate it.
43// - Field codes (%f, %u, %F, %U, %i, %c, %k) start with `%`; literal `%`
44// in a value must be doubled to `%%` so the parser doesn't consume `%`
45// followed by an arbitrary byte as a field code.
46//
47// We always wrap in `"..."` for safety; an unconditional wrap costs at most
48// 2 bytes and prevents future edits from forgetting to quote a value that
49// suddenly contains a reserved character.
50std::string escapeExec(const std::string& s);
51
52} // namespace desktop_entry
std::string escapeExec(const std::string &s)
std::string sanitizeField(const std::string &s)