xref: /OK3568_Linux_fs/yocto/poky/meta/classes/mime-xdg.bbclass (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun#
2*4882a593Smuzhiyun# This class creates mime <-> application associations based on entry
3*4882a593Smuzhiyun# 'MimeType' in *.desktop files
4*4882a593Smuzhiyun#
5*4882a593Smuzhiyun
6*4882a593SmuzhiyunDEPENDS += "desktop-file-utils"
7*4882a593SmuzhiyunPACKAGE_WRITE_DEPS += "desktop-file-utils-native"
8*4882a593SmuzhiyunDESKTOPDIR = "${datadir}/applications"
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun# There are recipes out there installing their .desktop files as absolute
11*4882a593Smuzhiyun# symlinks. For us these are dangling and cannot be introspected for "MimeType"
12*4882a593Smuzhiyun# easily. By addding package-names to MIME_XDG_PACKAGES, packager can force
13*4882a593Smuzhiyun# proper update-desktop-database handling. Note that all introspection is
14*4882a593Smuzhiyun# skipped for MIME_XDG_PACKAGES not empty
15*4882a593SmuzhiyunMIME_XDG_PACKAGES ?= ""
16*4882a593Smuzhiyun
17*4882a593Smuzhiyunmime_xdg_postinst() {
18*4882a593Smuzhiyunif [ "x$D" != "x" ]; then
19*4882a593Smuzhiyun	$INTERCEPT_DIR/postinst_intercept update_desktop_database ${PKG} \
20*4882a593Smuzhiyun		mlprefix=${MLPREFIX} \
21*4882a593Smuzhiyun		desktop_dir=${DESKTOPDIR}
22*4882a593Smuzhiyunelse
23*4882a593Smuzhiyun	update-desktop-database $D${DESKTOPDIR}
24*4882a593Smuzhiyunfi
25*4882a593Smuzhiyun}
26*4882a593Smuzhiyun
27*4882a593Smuzhiyunmime_xdg_postrm() {
28*4882a593Smuzhiyunif [ "x$D" != "x" ]; then
29*4882a593Smuzhiyun	$INTERCEPT_DIR/postinst_intercept update_desktop_database ${PKG} \
30*4882a593Smuzhiyun		mlprefix=${MLPREFIX} \
31*4882a593Smuzhiyun		desktop_dir=${DESKTOPDIR}
32*4882a593Smuzhiyunelse
33*4882a593Smuzhiyun	update-desktop-database $D${DESKTOPDIR}
34*4882a593Smuzhiyunfi
35*4882a593Smuzhiyun}
36*4882a593Smuzhiyun
37*4882a593Smuzhiyunpython populate_packages:append () {
38*4882a593Smuzhiyun    packages = d.getVar('PACKAGES').split()
39*4882a593Smuzhiyun    pkgdest =  d.getVar('PKGDEST')
40*4882a593Smuzhiyun    desktop_base = d.getVar('DESKTOPDIR')
41*4882a593Smuzhiyun    forced_mime_xdg_pkgs = (d.getVar('MIME_XDG_PACKAGES') or '').split()
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun    for pkg in packages:
44*4882a593Smuzhiyun        desktops_with_mime_found = pkg in forced_mime_xdg_pkgs
45*4882a593Smuzhiyun        if d.getVar('MIME_XDG_PACKAGES') == '':
46*4882a593Smuzhiyun            desktop_dir = '%s/%s%s' % (pkgdest, pkg, desktop_base)
47*4882a593Smuzhiyun            if os.path.exists(desktop_dir):
48*4882a593Smuzhiyun                for df in os.listdir(desktop_dir):
49*4882a593Smuzhiyun                    if df.endswith('.desktop'):
50*4882a593Smuzhiyun                        try:
51*4882a593Smuzhiyun                            with open(desktop_dir + '/'+ df, 'r') as f:
52*4882a593Smuzhiyun                                for line in f.read().split('\n'):
53*4882a593Smuzhiyun                                    if 'MimeType' in line:
54*4882a593Smuzhiyun                                        desktops_with_mime_found = True
55*4882a593Smuzhiyun                                        break;
56*4882a593Smuzhiyun                        except:
57*4882a593Smuzhiyun                            bb.warn('Could not open %s. Set MIME_XDG_PACKAGES in recipe or add mime-xdg to INSANE_SKIP.' % desktop_dir + '/'+ df)
58*4882a593Smuzhiyun                    if desktops_with_mime_found:
59*4882a593Smuzhiyun                        break
60*4882a593Smuzhiyun        if desktops_with_mime_found:
61*4882a593Smuzhiyun            bb.note("adding mime-xdg postinst and postrm scripts to %s" % pkg)
62*4882a593Smuzhiyun            postinst = d.getVar('pkg_postinst:%s' % pkg)
63*4882a593Smuzhiyun            if not postinst:
64*4882a593Smuzhiyun                postinst = '#!/bin/sh\n'
65*4882a593Smuzhiyun            postinst += d.getVar('mime_xdg_postinst')
66*4882a593Smuzhiyun            d.setVar('pkg_postinst:%s' % pkg, postinst)
67*4882a593Smuzhiyun            postrm = d.getVar('pkg_postrm:%s' % pkg)
68*4882a593Smuzhiyun            if not postrm:
69*4882a593Smuzhiyun                postrm = '#!/bin/sh\n'
70*4882a593Smuzhiyun            postrm += d.getVar('mime_xdg_postrm')
71*4882a593Smuzhiyun            d.setVar('pkg_postrm:%s' % pkg, postrm)
72*4882a593Smuzhiyun            bb.note("adding desktop-file-utils dependency to %s" % pkg)
73*4882a593Smuzhiyun            d.appendVar('RDEPENDS:' + pkg, " " + d.getVar('MLPREFIX')+"desktop-file-utils")
74*4882a593Smuzhiyun}
75