967a9fd023fa8cdd12f2c0e9ce69c13e8a65c196
[gentoo.git] / eclass / xdg.eclass
1 # Copyright 1999-2019 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: xdg.eclass
5 # @MAINTAINER:
6 # freedesktop-bugs@gentoo.org
7 # @AUTHOR:
8 # Original author: Gilles Dartiguelongue <eva@gentoo.org>
9 # @SUPPORTED_EAPIS: 4 5 6
10 # @BLURB: Provides phases for XDG compliant packages.
11 # @DESCRIPTION:
12 # Utility eclass to update the desktop, icon and shared mime info as laid
13 # out in the freedesktop specs & implementations
14
15 inherit xdg-utils
16
17 case "${EAPI:-0}" in
18         4|5|6)
19                 EXPORT_FUNCTIONS src_prepare pkg_preinst pkg_postinst pkg_postrm
20                 ;;
21         *) die "EAPI=${EAPI} is not supported" ;;
22 esac
23
24 # Avoid dependency loop as both depend on glib-2
25 if [[ ${CATEGORY}/${P} != dev-libs/glib-2.* ]] ; then
26 DEPEND="
27         dev-util/desktop-file-utils
28         x11-misc/shared-mime-info
29 "
30 fi
31
32 # @FUNCTION: xdg_src_prepare
33 # @DESCRIPTION:
34 # Prepare sources to work with XDG standards.
35 xdg_src_prepare() {
36         xdg_environment_reset
37
38         has ${EAPI:-0} 6 && default
39 }
40
41 # @FUNCTION: xdg_pkg_preinst
42 # @DESCRIPTION:
43 # Finds .desktop, icon and mime info files for later handling in pkg_postinst.
44 # Locations are stored in XDG_ECLASS_DESKTOPFILES, XDG_ECLASS_ICONFILES
45 # and XDG_ECLASS_MIMEINFOFILES respectively.
46 xdg_pkg_preinst() {
47         local f
48
49         XDG_ECLASS_DESKTOPFILES=()
50         while IFS= read -r -d '' f; do
51                 XDG_ECLASS_DESKTOPFILES+=( ${f} )
52         done < <(cd "${ED}" && find 'usr/share/applications' -type f -print0 2>/dev/null)
53
54         XDG_ECLASS_ICONFILES=()
55         while IFS= read -r -d '' f; do
56                 XDG_ECLASS_ICONFILES+=( ${f} )
57         done < <(cd "${ED}" && find 'usr/share/icons' -type f -print0 2>/dev/null)
58
59         XDG_ECLASS_MIMEINFOFILES=()
60         while IFS= read -r -d '' f; do
61                 XDG_ECLASS_MIMEINFOFILES+=( ${f} )
62         done < <(cd "${ED}" && find 'usr/share/mime' -type f -print0 2>/dev/null)
63 }
64
65 # @FUNCTION: xdg_pkg_postinst
66 # @DESCRIPTION:
67 # Handle desktop, icon and mime info database updates.
68 xdg_pkg_postinst() {
69         if [[ ${#XDG_ECLASS_DESKTOPFILES[@]} -gt 0 ]]; then
70                 xdg_desktop_database_update
71         else
72                 debug-print "No .desktop files to add to database"
73         fi
74
75         if [[ ${#XDG_ECLASS_ICONFILES[@]} -gt 0 ]]; then
76                 xdg_icon_cache_update
77         else
78                 debug-print "No icon files to add to cache"
79         fi
80
81         if [[ ${#XDG_ECLASS_MIMEINFOFILES[@]} -gt 0 ]]; then
82                 xdg_mimeinfo_database_update
83         else
84                 debug-print "No mime info files to add to database"
85         fi
86 }
87
88 # @FUNCTION: xdg_pkg_postrm
89 # @DESCRIPTION:
90 # Handle desktop, icon and mime info database updates.
91 xdg_pkg_postrm() {
92         if [[ ${#XDG_ECLASS_DESKTOPFILES[@]} -gt 0 ]]; then
93                 xdg_desktop_database_update
94         else
95                 debug-print "No .desktop files to add to database"
96         fi
97
98         if [[ ${#XDG_ECLASS_ICONFILES[@]} -gt 0 ]]; then
99                 xdg_icon_cache_update
100         else
101                 debug-print "No icon files to add to cache"
102         fi
103
104         if [[ ${#XDG_ECLASS_MIMEINFOFILES[@]} -gt 0 ]]; then
105                 xdg_mimeinfo_database_update
106         else
107                 debug-print "No mime info files to add to database"
108         fi
109 }
110