mate-base/mate-panel: stabilize 1.22.1 for amd64, x86
[gentoo.git] / eclass / xdg-utils.eclass
1 # Copyright 1999-2019 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: xdg-utils.eclass
5 # @MAINTAINER:
6 # gnome@gentoo.org
7 # @AUTHOR:
8 # Original author: Gilles Dartiguelongue <eva@gentoo.org>
9 # @SUPPORTED_EAPIS: 0 1 2 3 4 5 6 7
10 # @BLURB: Auxiliary functions commonly used by XDG compliant packages.
11 # @DESCRIPTION:
12 # This eclass provides a set of auxiliary functions needed by most XDG
13 # compliant packages.
14 # It provides XDG stack related functions such as:
15 #  * GTK/Qt5 icon theme cache management
16 #  * XDG .desktop files cache management
17 #  * XDG mime information database management
18
19 case "${EAPI:-0}" in
20         0|1|2|3|4|5|6|7) ;;
21         *) die "EAPI=${EAPI} is not supported" ;;
22 esac
23
24 # @ECLASS-VARIABLE: DESKTOP_DATABASE_DIR
25 # @INTERNAL
26 # @DESCRIPTION:
27 # Directory where .desktop files database is stored
28 : ${DESKTOP_DATABASE_DIR="/usr/share/applications"}
29
30 # @ECLASS-VARIABLE: MIMEINFO_DATABASE_DIR
31 # @INTERNAL
32 # @DESCRIPTION:
33 # Directory where .desktop files database is stored
34 : ${MIMEINFO_DATABASE_DIR:="/usr/share/mime"}
35
36 # @FUNCTION: xdg_environment_reset
37 # @DESCRIPTION:
38 # Clean up environment for clean builds.
39 xdg_environment_reset() {
40         # Prepare XDG base directories
41         export XDG_DATA_HOME="${HOME}/.local/share"
42         export XDG_CONFIG_HOME="${HOME}/.config"
43         export XDG_CACHE_HOME="${HOME}/.cache"
44         export XDG_RUNTIME_DIR="${T}/run"
45         mkdir -p "${XDG_DATA_HOME}" "${XDG_CONFIG_HOME}" "${XDG_CACHE_HOME}" \
46                 "${XDG_RUNTIME_DIR}" || die
47         # This directory needs to be owned by the user, and chmod 0700
48         # https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
49         chmod 0700 "${XDG_RUNTIME_DIR}" || die
50
51         unset DBUS_SESSION_BUS_ADDRESS
52 }
53
54 # @FUNCTION: xdg_desktop_database_update
55 # @DESCRIPTION:
56 # Updates the .desktop files database.
57 # Generates a list of mimetypes linked to applications that can handle them
58 xdg_desktop_database_update() {
59         if [[ ${EBUILD_PHASE} != post* ]] ; then
60                 die "xdg_desktop_database_update must be used in pkg_post* phases."
61         fi
62
63         if ! type update-desktop-database &>/dev/null; then
64                 debug-print "update-desktop-database is not found"
65                 return
66         fi
67
68         ebegin "Updating .desktop files database"
69         update-desktop-database -q "${EROOT%/}${DESKTOP_DATABASE_DIR}"
70         eend $?
71 }
72
73 # @FUNCTION: xdg_icon_cache_update
74 # @DESCRIPTION:
75 # Updates icon theme cache files under /usr/share/icons.
76 # This function should be called from pkg_postinst and pkg_postrm.
77 xdg_icon_cache_update() {
78         if [[ ${EBUILD_PHASE} != post* ]] ; then
79                 die "xdg_icon_cache_update must be used in pkg_post* phases."
80         fi
81
82         if ! type gtk-update-icon-cache &>/dev/null; then
83                 debug-print "gtk-update-icon-cache is not found"
84                 return
85         fi
86
87         ebegin "Updating icons cache"
88         local retval=0
89         local fails=( )
90         for dir in "${EROOT%/}"/usr/share/icons/*
91         do
92                 if [[ -f "${dir}/index.theme" ]] ; then
93                         local rv=0
94                         gtk-update-icon-cache -qf "${dir}"
95                         rv=$?
96                         if [[ ! $rv -eq 0 ]] ; then
97                                 debug-print "Updating cache failed on ${dir}"
98                                 # Add to the list of failures
99                                 fails+=( "${dir}" )
100                                 retval=2
101                         fi
102                 elif [[ $(ls "${dir}") = "icon-theme.cache" ]]; then
103                         # Clear stale cache files after theme uninstallation
104                         rm "${dir}/icon-theme.cache"
105                 fi
106                 if [[ -z $(ls "${dir}") ]]; then
107                         # Clear empty theme directories after theme uninstallation
108                         rmdir "${dir}"
109                 fi
110         done
111         eend ${retval}
112         for f in "${fails[@]}" ; do
113                 eerror "Failed to update cache with icon $f"
114         done
115 }
116
117 # @FUNCTION: xdg_mimeinfo_database_update
118 # @DESCRIPTION:
119 # Update the mime database.
120 # Creates a general list of mime types from several sources
121 xdg_mimeinfo_database_update() {
122         if [[ ${EBUILD_PHASE} != post* ]] ; then
123                 die "xdg_mimeinfo_database_update must be used in pkg_post* phases."
124         fi
125
126         if ! type update-mime-database &>/dev/null; then
127                 debug-print "update-mime-database is not found"
128                 return
129         fi
130
131         ebegin "Updating shared mime info database"
132         update-mime-database "${EROOT%/}${MIMEINFO_DATABASE_DIR}"
133         eend $?
134 }