app_containers: Support generation of shortcuts from ~/.local/share

This commit is contained in:
Peter Cai 2022-12-26 19:33:54 -05:00
parent ccde40e865
commit ea876c8a76
1 changed files with 31 additions and 4 deletions

View File

@ -12,19 +12,43 @@ container_root=/var/lib/machines/$container_name
app_name="$2"
[ -z "$app_name" ] && die "Please provide name of app"
is_user_app=false
desktop_file=$container_root/usr/share/applications/$app_name.desktop
desktop_file_content=$(sudo cat $desktop_file 2>/dev/null)
if ! [ $? == 0 ]; then
is_user_app=true
desktop_file=$container_root/home/user/.local/share/applications/$app_name.desktop
desktop_file_content=$(sudo cat $desktop_file 2>/dev/null)
fi
[ $? == 0 ] || die "App $app_name not found in container $container_name"
icon_name=$(echo "$desktop_file_content" | grep "Icon=" | head -1 | cut -d'=' -f 2)
[ -z "$icon_name" ] && die "No icon defined for app $app_name"
icon_root=$container_root/usr/share/icons/hicolor
icons=($(sudo find $icon_root -name "$icon_name.*"))
if $is_user_app; then
icon_root=$container_root/home/user/.local/share/icons/hicolor
else
icon_root=$container_root/usr/share/icons/hicolor
fi
if [ "${icon_name:0:1}" = "/" ]; then
icons=("$container_root$icon_name")
icon_name=$(basename "$icon_name")
icon_ext="${icon_name##*.}"
icon_name="$app_name" # If the icon path is absolute, we always use the app name as icon name (to prevent conflicts)
icon_absolute_path=true
else
icons=($(sudo find $icon_root -name "$icon_name.*"))
icon_absolute_path=false
fi
[ ${#icons[@]} == 0 ] && die "Cannot find any icon for app $app_name"
for icon in ${icons[@]}; do
icon_target=${icon//$icon_root/$HOME/.local/share/icons/hicolor}
if $icon_absolute_path; then
icon_target=$HOME/.local/share/icons/hicolor/256x256/apps/$icon_name.$icon_ext # See comment before; this is to avoid name conflicts
else
icon_target=${icon//$icon_root/$HOME/.local/share/icons/hicolor}
fi
icon_target_dir=$(dirname "$icon_target")
echo "Copying $icon to $icon_target in $icon_target_dir"
mkdir -p "$icon_target_dir"
@ -32,5 +56,8 @@ for icon in ${icons[@]}; do
done
mkdir -p $HOME/.local/share/applications
echo "$desktop_file_content" | sed -r "s@^Exec=(.*)\$@Exec=env CONTAINER_NAME=$container_name $HOME/.local/bin/run_app_container \\1@g" > $HOME/.local/share/applications/$app_name.desktop
echo "$desktop_file_content" | grep -v "^TryExec=.*\$" \
| sed -r "s@^Icon=.*\$@Icon=${icon_name}@g" \
| sed -r "s@^Exec=(.*)\$@Exec=env CONTAINER_NAME=$container_name $HOME/.local/bin/run_app_container \\1@g" \
> $HOME/.local/share/applications/$app_name.desktop
echo "Created $HOME/.local/share/applications/$app_name.desktop"