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