qubes.GetAppmenus 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/bin/bash
  2. # shellcheck disable=SC2016
  3. # send .desktop files from directories persisting across VM restarts, specifically:
  4. # - any directory in case of "full" persistence
  5. # - directories stored on /rw in case of "rw-only" persistence
  6. # - nothing, otherwise
  7. if [ -z "$XDG_DATA_HOME" ]; then
  8. XDG_DATA_HOME="$HOME/.local/share"
  9. fi
  10. if [ -z "$XDG_DATA_DIRS" ]; then
  11. XDG_DATA_DIRS="/usr/local/share/:/usr/share/"
  12. fi
  13. # if read fails for some reason, default to full
  14. persistence=$(qubesdb-read /qubes-vm-persistence || echo full)
  15. rw_devno=$(stat -c %D /rw)
  16. apps_dirs_to_consider=( "$XDG_DATA_HOME" )
  17. old_IFS="$IFS"
  18. IFS=:
  19. # shellcheck disable=SC2206
  20. apps_dirs_to_consider+=( $XDG_DATA_DIRS )
  21. IFS="$old_IFS"
  22. apps_dirs=()
  23. for dir in "${apps_dirs_to_consider[@]}"; do
  24. if [ "$persistence" = "full" ]; then
  25. apps_dirs+=( "$dir/applications" )
  26. elif [ "$persistence" = "rw-only" ] && \
  27. [ "$(stat -c %D "$dir")" = "$rw_devno" ]; then
  28. apps_dirs+=( "$dir/applications" )
  29. fi
  30. done
  31. if [ "${#apps_dirs[@]}" -eq "0" ]; then
  32. # nothing to send, exit early to not let `find` browse the current
  33. # directory
  34. exit 0
  35. fi
  36. find "${apps_dirs[@]}" -name '*.desktop' -print0 2>/dev/null | \
  37. xargs -0 awk '
  38. BEGINFILE { entry="" }
  39. /^\[/ { if (tolower($0) != "\[desktop entry\]") nextfile }
  40. /^Exec=/ { entry = entry FILENAME ":Exec=qubes-desktop-run " FILENAME "\n"; next }
  41. /^NoDisplay *= *true$/ { entry=""; nextfile }
  42. /=/ { entry = entry FILENAME ":" $0 "\n" }
  43. ENDFILE { print entry }
  44. ' 2> /dev/null