summaryrefslogtreecommitdiff
blob: 35084fc8164dc0b3e96fa527418f5ef93ad7fffc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# Copyright 2004-2021 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

# @ECLASS: xdg-utils.eclass
# @MAINTAINER:
# gnome@gentoo.org
# freedesktop-bugs@gentoo.org
# @AUTHOR:
# Original author: Gilles Dartiguelongue <eva@gentoo.org>
# @SUPPORTED_EAPIS: 5 6 7 8
# @BLURB: Auxiliary functions commonly used by XDG compliant packages.
# @DESCRIPTION:
# This eclass provides a set of auxiliary functions needed by most XDG
# compliant packages.
# It provides XDG stack related functions such as:
#  * GTK/Qt5 icon theme cache management
#  * XDG .desktop files cache management
#  * XDG mime information database management

case ${EAPI} in
	5|6|7|8) ;;
	*) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;;
esac

# @ECLASS-VARIABLE: DESKTOP_DATABASE_DIR
# @INTERNAL
# @DESCRIPTION:
# Directory where .desktop files database is stored
: ${DESKTOP_DATABASE_DIR="/usr/share/applications"}

# @ECLASS-VARIABLE: MIMEINFO_DATABASE_DIR
# @INTERNAL
# @DESCRIPTION:
# Directory where .desktop files database is stored
: ${MIMEINFO_DATABASE_DIR:="/usr/share/mime"}

# @FUNCTION: xdg_environment_reset
# @DESCRIPTION:
# Clean up environment for clean builds.
xdg_environment_reset() {
	# Prepare XDG base directories
	export XDG_DATA_HOME="${HOME}/.local/share"
	export XDG_CONFIG_HOME="${HOME}/.config"
	export XDG_CACHE_HOME="${HOME}/.cache"
	export XDG_RUNTIME_DIR="${T}/run"
	mkdir -p "${XDG_DATA_HOME}" "${XDG_CONFIG_HOME}" "${XDG_CACHE_HOME}" \
		"${XDG_RUNTIME_DIR}" || die
	# This directory needs to be owned by the user, and chmod 0700
	# https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
	chmod 0700 "${XDG_RUNTIME_DIR}" || die

	unset DBUS_SESSION_BUS_ADDRESS
}

# @FUNCTION: xdg_desktop_database_update
# @DESCRIPTION:
# Updates the .desktop files database.
# Generates a list of mimetypes linked to applications that can handle them
xdg_desktop_database_update() {
	if [[ ${EBUILD_PHASE} != post* ]]; then
		die "xdg_desktop_database_update must be used in pkg_post* phases."
	fi

	if ! type update-desktop-database &>/dev/null; then
		debug-print "update-desktop-database is not found"
		return
	fi

	ebegin "Updating .desktop files database"
	update-desktop-database -q "${EROOT%/}${DESKTOP_DATABASE_DIR}"
	eend $?
}

# @FUNCTION: xdg_icon_cache_update
# @DESCRIPTION:
# Updates icon theme cache files under /usr/share/icons.
# This function should be called from pkg_postinst and pkg_postrm.
xdg_icon_cache_update() {
	if [[ ${EBUILD_PHASE} != post* ]]; then
		die "xdg_icon_cache_update must be used in pkg_post* phases."
	fi

	if ! type gtk-update-icon-cache &>/dev/null; then
		debug-print "gtk-update-icon-cache is not found"
		return
	fi

	ebegin "Updating icons cache"
	local dir retval=0
	local fails=()
	for dir in "${EROOT%/}"/usr/share/icons/*; do
		if [[ -f ${dir}/index.theme ]]; then
			if ! gtk-update-icon-cache -qf "${dir}"; then
				debug-print "Updating cache failed on ${dir}"
				# Add to the list of failures
				fails+=("${dir}")
				retval=2
			fi
		elif [[ $(ls "${dir}") = icon-theme.cache ]]; then
			# Clear stale cache files after theme uninstallation
			rm "${dir}/icon-theme.cache"
		fi
		if [[ -z $(ls "${dir}") ]]; then
			# Clear empty theme directories after theme uninstallation
			rmdir "${dir}"
		fi
	done
	eend ${retval}
	for dir in "${fails[@]}"; do
		eerror "Failed to update cache with icon ${dir}"
	done
}

# @FUNCTION: xdg_mimeinfo_database_update
# @DESCRIPTION:
# Update the mime database.
# Creates a general list of mime types from several sources
xdg_mimeinfo_database_update() {
	if [[ ${EBUILD_PHASE} != post* ]]; then
		die "xdg_mimeinfo_database_update must be used in pkg_post* phases."
	fi

	if ! type update-mime-database &>/dev/null; then
		debug-print "update-mime-database is not found"
		return
	fi

	ebegin "Updating shared mime info database"
	update-mime-database "${EROOT%/}${MIMEINFO_DATABASE_DIR}"
	eend $?
}