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