Skip to content

Commit

Permalink
Add advanced mime type selection
Browse files Browse the repository at this point in the history
  • Loading branch information
joaopauloalbq committed Apr 18, 2024
1 parent b23b13f commit 6e82e92
Showing 1 changed file with 61 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,6 @@ run () {
coproc ( $1 > /dev/null 2>&1 )
}

xdg-all-apps() {
LOCAL="${XDG_DATA_HOME:-$HOME/.local/share}/applications/mimeinfo.cache"
GLOBAL="/usr/share/applications/mimeinfo.cache"

MATCHING="$(grep -h "$1" "$LOCAL" "$GLOBAL")"
if [ -z "$MATCHING" ]; then
echo "There are no application associated with $1"
return
fi
echo -en "$MATCHING" |cut -d = -f 2 |\
sed -z -e 's:\n::;s:;:\n:g' |\
sort |uniq
}

echo -en "\0message\x1f\n"
case $ROFI_INFO in
"" )
Expand All @@ -42,7 +28,7 @@ case $ROFI_INFO in
echo -en "$KEYBOARD_LABEL\0icon\x1fkeyboard\x1finfo\x1fkeyboardKeymapMenu\n"
echo -en "$LANGUAGES_LABEL\0icon\x1flanguages\x1finfo\x1flocaleMenu\n"
echo -en "$TIMEDATE_LABEL\0icon\x1fhistory\x1finfo\x1ftimeDateMenu\n"
echo -en "$DRIVERS_LABEL\0icon\x1fdevice_pci\x1fdata\x1ftestededata\x1finfo\x1fdriverMenu\n"
echo -en "$DRIVERS_LABEL\0icon\x1fdevice_pci\x1finfo\x1fdriverMenu\n"
echo -en "$USER_LABEL\0icon\x1favatar-default\x1finfo\x1fuserMenu\n"
echo -en "$ABOUT_LABEL\0icon\x1fdialog-information\x1finfo\x1faboutMenu\n"
;;
Expand Down Expand Up @@ -194,6 +180,7 @@ case $ROFI_INFO in
echo -en "$BACK_LABEL\0icon\x1fgo-previous\x1finfo\x1fkeyboardMenu\n"
;;
"keyboardKeymapMenu" )
echo -en "\0new-selection\x1f0"
echo -en "\0message\x1fSelect Keyboard Mapping\n"
echo -en "$CANCEL_LABEL\0icon\x1fedit-clear\x1finfo\x1f\n"

Expand Down Expand Up @@ -428,38 +415,81 @@ case $ROFI_INFO in
;;

"defaultApplicationsMenu" )
echo -en "Web\0icon\x1ffolder-network\x1finfo\x1ftext/html\n"
echo -en "Mail\0icon\x1fmail-unread\x1finfo\x1fdefaultApplicationsMenu\n"
echo -en "\0new-selection\x1f0\n"
# echo -en "\0active\x1f3\n"
echo -en "Web\0icon\x1ffolder-network\x1finfo\x1fx-scheme-handler/https\n"
echo -en "Mail\0icon\x1fmail-unread\x1finfo\x1fx-scheme-handler/mailto\n"
echo -en "Folder\0icon\x1ffolder\x1finfo\x1finode/directory\n"
echo -en "Audio\0icon\x1ffolder-music\x1finfo\x1faudio/*\n"
echo -en "Image\0icon\x1ffolder-pictures\x1finfo\x1fimage/*\n"
echo -en "Video\0icon\x1ffolder-videos\x1finfo\x1fvideo/*\n"
echo -en "PDF\0icon\x1ffolder-documents\x1finfo\x1fapplication/pdf\n"
echo -en "Advanced\0icon\x1fconfigure\x1finfo\x1fdefaultApplicationAdvancedMenu\n"
echo -en " \0nonselectable\x1ftrue\n"
echo -en "$BACK_LABEL\0icon\x1fgo-previous\x1finfo\x1f\n"
;;

"text/html" | "inode/directory" | "application/pdf" | "audio/*" | "video/*" | "image/*" )
# DEFAULTAPP=$(xdg-mime query default $1)
echo -en "\0message\x1f$1\n"
echo -en "$CANCEL_LABEL\0icon\x1fedit-clear\x1finfo\x1fdefaultApplicationsMenu\n"
"x-scheme-handler/https" | "x-scheme-handler/mailto" | "inode/directory" | "application/pdf" | "audio/*" | "video/*" | "image/*" )
echo -en "\0message\x1f$ROFI_INFO\n"

LOCAL="${XDG_DATA_HOME:-$HOME/.local/share}/applications/mimeinfo.cache"
GLOBAL="/usr/share/applications/mimeinfo.cache"
DEFAULT=$(xdg-mime query default $ROFI_INFO | sed 's/.desktop/ /g')
APPS=($(echo -e $( grep "$ROFI_INFO" "/usr/share/applications/mimeinfo.cache" | cut -d = -f 2 | sed 's/.desktop;/ /g' | tr ' ' '\n' | sort | uniq | tr '\n' ' ')))

for app in "${APPS[@]}"
do
if [ "$app" == $1 ]; then
xdg-mime default "$app.desktop" "$ROFI_INFO"
DEFAULT=$app
fi
done

MATCHING="$(grep -h "$ROFI_INFO" "$LOCAL" "$GLOBAL")"
if [ -z "$MATCHING" ]; then
echo "There are no application associated with $1"
return
echo -en "$CANCEL_LABEL\0icon\x1fedit-clear\x1finfo\x1fdefaultApplicationsMenu\n"
for app in "${APPS[@]}"
do
if [ "$app" == $DEFAULT ]; then
echo -en "$app\0icon\x1fstarred\x1finfo\x1f$ROFI_INFO\n"
else
echo -en "$app\0icon\x1fnon-starred\x1finfo\x1f$ROFI_INFO\n"
fi
done
;;

"defaultApplicationAdvancedMenu" | "setDefaultApplicationAdvancedMenu" )
if [ $ROFI_INFO == "setDefaultApplicationAdvancedMenu" ]; then
xdg-mime default "$1.desktop" "$ROFI_DATA"
fi
echo -en "$MATCHING" |cut -d = -f 2 |\
sed -z -e 's:\n::;s:;:\n:g' |\
sort |uniq

echo -en "$CANCEL_LABEL\0icon\x1fedit-clear\x1finfo\x1fdefaultApplicationsMenu\n"
MIMES=($(echo -e $( tail -n +2 "/usr/share/applications/mimeinfo.cache" | cut -d = -f 1 | sed 's/.desktop;/ /g' | tr ' ' '\n' | sort | uniq | tr '\n' ' ')))
for mime in "${MIMES[@]}"
do
echo -en "$mime\0icon\x1fdocument-edit-sign\x1finfo\x1fdefaultApplicationSelectedAdvancedMenu\n"
done

echo -en "\0new-selection\x1f0"
;;

"defaultApplicationSelectedAdvancedMenu" )
echo -en "\0message\x1f$1\n"
echo -en "\0data\x1f$1\n"

echo -en "$CANCEL_LABEL\0icon\x1fedit-clear\x1finfo\x1fdefaultApplicationAdvancedMenu\n"

DEFAULT=$(xdg-mime query default $1 | sed 's/.desktop//g')
APPS=($(echo -e $( grep "$1" "/usr/share/applications/mimeinfo.cache" | cut -d = -f 2 | sed 's/.desktop;/ /g' | tr ' ' '\n' | sort | uniq | tr '\n' ' ')))

for app in "${APPS[@]}"
do
if [ $app == $DEFAULT ]; then
echo -en "$app\0icon\x1fstarred\x1finfo\x1fsetDefaultApplicationAdvancedMenu\n"
else
echo -en "$app\0icon\x1fnon-starred\x1finfo\x1fsetDefaultApplicationAdvancedMenu\n"
fi
done
;;

*)
echo -en "\x00prompt\x1f\n"
echo -en "\0prompt\x1f\n"
echo -en "\0message\x1f$ROFI_INFO\n"
echo -en "ROFI_INFO - $ROFI_INFO\n"
echo -en "ROFI_DATA - $ROFI_DATA\n"
Expand Down

0 comments on commit 6e82e92

Please sign in to comment.