|
|
34beb74 |
#!/bin/bash
|
|
|
34beb74 |
#
|
|
|
34beb74 |
# gnome-file-types-properties -
|
|
|
34beb74 |
# A zenity-script for chosing preferred applications.
|
|
|
34beb74 |
# Written by Magnus Stålnacke (jemamo at telia.com)
|
|
|
34beb74 |
# Version 0.8.4 Nov. 30 2007
|
|
|
34beb74 |
#
|
|
|
34beb74 |
export LANG=en_US.utf8
|
|
|
34beb74 |
|
|
|
34beb74 |
ARGU="$1"
|
|
|
34beb74 |
UTMP="/tmp/$(whoami)"
|
|
|
34beb74 |
DATA_DIR="$HOME/.local/share/applications"
|
|
|
34beb74 |
GDATA_DIR="/usr/share/applications"
|
|
|
34beb74 |
G_CACHE="$GDATA_DIR/mimeinfo.cache"
|
|
|
34beb74 |
U_CACHE="$DATA_DIR/mimeinfo.cache"
|
|
|
34beb74 |
SUBC="/usr/share/mime/subclasses"
|
|
|
34beb74 |
U_DEF="$DATA_DIR/defaults.list"
|
|
|
34beb74 |
EXT="usercreated.desktop"
|
|
|
34beb74 |
NONEW=1
|
|
|
34beb74 |
|
|
|
34beb74 |
#
|
|
|
34beb74 |
# Create dirs in users home if missing.
|
|
|
34beb74 |
#
|
|
|
34beb74 |
if [ -e "$DATA_DIR" ]; then :; else mkdir $DATA_DIR;fi
|
|
|
34beb74 |
if [ -e "$U_DEF" ]
|
|
|
34beb74 |
then :;
|
|
|
34beb74 |
else
|
|
|
34beb74 |
echo "[Default Applications]" > $DATA_DIR/defaults.list
|
|
|
34beb74 |
fi
|
|
|
34beb74 |
|
|
|
34beb74 |
#
|
|
|
34beb74 |
# Check how we got started.
|
|
|
34beb74 |
#
|
|
|
34beb74 |
function checkarg (){
|
|
|
34beb74 |
if [ -n "$ARGU" ]
|
|
|
34beb74 |
then
|
|
|
34beb74 |
if [ -e "$ARGU" ]
|
|
|
34beb74 |
then
|
|
|
34beb74 |
chkmtpe
|
|
|
34beb74 |
deflist
|
|
|
34beb74 |
else
|
|
|
34beb74 |
showhelp
|
|
|
34beb74 |
fi
|
|
|
34beb74 |
else
|
|
|
34beb74 |
selfile
|
|
|
34beb74 |
dtfedit
|
|
|
34beb74 |
if [ -n "$DT_FILE" ]
|
|
|
34beb74 |
then
|
|
|
34beb74 |
update
|
|
|
34beb74 |
fi
|
|
|
34beb74 |
fi
|
|
|
34beb74 |
}
|
|
|
34beb74 |
|
|
|
34beb74 |
#
|
|
|
34beb74 |
# Mimetype check.
|
|
|
34beb74 |
#
|
|
|
34beb74 |
function chkmtpe (){
|
|
|
34beb74 |
MTPE=$(gnomevfs-info -s "$ARGU" | grep "MIME type" | sed 's/.*: //')
|
|
|
34beb74 |
echo "The mimetype is: $MTPE"
|
|
|
34beb74 |
}
|
|
|
34beb74 |
|
|
|
34beb74 |
#
|
|
|
34beb74 |
# Build a list with registered apps for this mimetype.
|
|
|
34beb74 |
#
|
|
|
34beb74 |
function deflist (){
|
|
|
34beb74 |
if [ -e "$UTMP.avail" ]; then cleanup;fi
|
|
|
34beb74 |
# Create the list in "$UTMP.avail" to be used by the zenity list-box
|
|
|
34beb74 |
if [ "$NONEW" == 1 ]
|
|
|
34beb74 |
then
|
|
|
34beb74 |
echo "FALSE Add_New_App New" > "$UTMP.avail"
|
|
|
34beb74 |
fi
|
|
|
34beb74 |
# Get all sub-mimetypes, one per row.
|
|
|
34beb74 |
SUB="$(grep -hs "^$MTPE" $SUBC $G_CACHE $U_CACHE | awk -F= '{print $1}' | sed 's/ /\n/g')"
|
|
|
34beb74 |
# Get all .desktopfiles for the above mimetypes
|
|
|
34beb74 |
MTPEs=$(grep -hs "$SUB" $U_CACHE $G_CACHE | sed 's/.*=//g;s/;$//g;s/;/\n/g;')
|
|
|
34beb74 |
if [ -n "$SUB" ]
|
|
|
34beb74 |
then
|
|
|
34beb74 |
for i in $(echo "$MTPEs" | sort -u); do
|
|
|
34beb74 |
echo "FALSE $i $(grep -hs "^Name=" $GDATA_DIR/$i $DATA_DIR/$i | \
|
|
|
34beb74 |
sed 's/Name=//;s/ /_/g')" >> "$UTMP.avail"
|
|
|
34beb74 |
done
|
|
|
34beb74 |
fi
|
|
|
34beb74 |
# Change FALSE to TRUE for the default app.
|
|
|
34beb74 |
if grep -q "desktop" "$UTMP.avail"
|
|
|
34beb74 |
then
|
|
|
34beb74 |
DEF_APP=$(gnomevfs-info -s "$ARGU" | grep "Default app" | sed 's/.*: //')
|
|
|
34beb74 |
sed -i "s|FALSE $DEF_APP|TRUE $DEF_APP|" "$UTMP.avail"
|
|
|
34beb74 |
fi
|
|
|
34beb74 |
# Show the list
|
|
|
34beb74 |
zenity --height="350" --width="550" --list --radiolist --text \
|
|
|
34beb74 |
"The list below show applications that already have an entry for the mimetype:
|
|
|
34beb74 |
$MTPE on your system, meaning they are registered to be able
|
|
|
34beb74 |
to deal with this mimetype. You can choose your preferred default application
|
|
|
34beb74 |
by checking the radiobutton. The script will adjust the mimetype settings in
|
|
|
34beb74 |
your local home directory, thus not changing anything systemwide.
|
|
|
34beb74 |
|
|
|
34beb74 |
If you wish you can add an application that is not in the list by checking the first
|
|
|
34beb74 |
option in list named Add_New_App
|
|
|
34beb74 |
|
|
|
34beb74 |
Please select" \
|
|
|
34beb74 |
--column "Pick" --column ".desktop file" --column "Application" \
|
|
|
34beb74 |
$(cat "$UTMP.avail") > "$UTMP.chosen"
|
|
|
34beb74 |
retval=$?
|
|
|
34beb74 |
choice=$(cat "$UTMP.chosen")
|
|
|
34beb74 |
if grep -q "$MTPE=$choice" $U_DEF
|
|
|
34beb74 |
then
|
|
|
34beb74 |
retval=2
|
|
|
34beb74 |
fi
|
|
|
34beb74 |
case $retval in
|
|
|
34beb74 |
0)
|
|
|
34beb74 |
DEF_CH=$(grep -s "$choice" "$UTMP.avail" | awk '{print $2}')
|
|
|
34beb74 |
DEF_NME=$(grep -s "$choice" "$UTMP.avail" | awk '{print $3}')
|
|
|
34beb74 |
if [ "$DEF_CH" = Add_New_App ]
|
|
|
34beb74 |
then
|
|
|
34beb74 |
newmime
|
|
|
34beb74 |
fi
|
|
|
34beb74 |
if [ -z "$NOCH" ]
|
|
|
34beb74 |
then
|
|
|
34beb74 |
if grep -q "$MTPE" $U_DEF
|
|
|
34beb74 |
then
|
|
|
34beb74 |
# Set the users chosen default app for the existing mime.
|
|
|
34beb74 |
sed -i "s|$MTPE=.*|$MTPE=$DEF_CH|" $U_DEF
|
|
|
34beb74 |
else
|
|
|
34beb74 |
# If no user default for this mimetype, add it.
|
|
|
34beb74 |
echo "$MTPE=$DEF_CH" >> $U_DEF
|
|
|
34beb74 |
fi
|
|
|
34beb74 |
update
|
|
|
34beb74 |
zenity --info --text "Default application to handle $MTPE is set to $DEF_NME"
|
|
|
34beb74 |
fi;;
|
|
|
34beb74 |
1)
|
|
|
34beb74 |
echo "Cancel pressed.";;
|
|
|
34beb74 |
-1)
|
|
|
34beb74 |
echo "ESC pressed.";;
|
|
|
34beb74 |
2)
|
|
|
34beb74 |
zenity --info --text "Default was not changed.";;
|
|
|
34beb74 |
esac
|
|
|
34beb74 |
cleanup
|
|
|
34beb74 |
}
|
|
|
34beb74 |
|
|
|
34beb74 |
#
|
|
|
34beb74 |
# Show an input box for new app to handle the mimetype.
|
|
|
34beb74 |
#
|
|
|
34beb74 |
function newmime (){
|
|
|
34beb74 |
NEWAPP=$(zenity --entry --text \
|
|
|
34beb74 |
"Fill in the Name and command for the application you want to handle the
|
|
|
34beb74 |
mimetype: $MTPE separate them with a colon (:)
|
|
|
34beb74 |
|
|
|
34beb74 |
Separate Name and Command with a colon. If you omit the command, or
|
|
|
34beb74 |
type something that is not in your path, you will get the chance to
|
|
|
34beb74 |
provide the command with full path by a file chooser dialog.")
|
|
|
34beb74 |
|
|
|
34beb74 |
case $? in
|
|
|
34beb74 |
0)
|
|
|
34beb74 |
if echo "$NEWAPP" | grep -q ":"
|
|
|
34beb74 |
then
|
|
|
34beb74 |
NAME=$(echo "$NEWAPP" | awk -F: '{print $1}')
|
|
|
34beb74 |
CMD=$(echo "$NEWAPP" | awk -F: '{print $2}')
|
|
|
34beb74 |
if [ -n "$NAME" ]
|
|
|
34beb74 |
then
|
|
|
34beb74 |
check
|
|
|
34beb74 |
usercreate
|
|
|
34beb74 |
else
|
|
|
34beb74 |
zenity --error --text "Application name is missing"
|
|
|
34beb74 |
newmime
|
|
|
34beb74 |
fi
|
|
|
34beb74 |
else
|
|
|
34beb74 |
zenity --error --text "Separator missing"
|
|
|
34beb74 |
newmime
|
|
|
34beb74 |
fi
|
|
|
34beb74 |
;;
|
|
|
34beb74 |
1)
|
|
|
34beb74 |
deflist
|
|
|
34beb74 |
;;
|
|
|
34beb74 |
-1)
|
|
|
34beb74 |
deflist
|
|
|
34beb74 |
;;
|
|
|
34beb74 |
esac
|
|
|
34beb74 |
NOCH=yes
|
|
|
34beb74 |
}
|
|
|
34beb74 |
|
|
|
34beb74 |
#
|
|
|
34beb74 |
# Check if the command exists in path, if not give the
|
|
|
34beb74 |
# user a filechooser.
|
|
|
34beb74 |
#
|
|
|
34beb74 |
function check (){
|
|
|
34beb74 |
if which $CMD >&/dev/null
|
|
|
34beb74 |
then
|
|
|
34beb74 |
CMD=`which $CMD`
|
|
|
34beb74 |
else
|
|
|
34beb74 |
CMD=$(zenity --file-selection --filename=/ --title="Chose application")
|
|
|
34beb74 |
if [ -z $CMD ]
|
|
|
34beb74 |
then
|
|
|
34beb74 |
newmime
|
|
|
34beb74 |
fi
|
|
|
34beb74 |
fi
|
|
|
34beb74 |
}
|
|
|
34beb74 |
|
|
|
34beb74 |
#
|
|
|
34beb74 |
# Create a new *.desktop file for the added app that can handle
|
|
|
34beb74 |
# the mimetype in question.
|
|
|
34beb74 |
#
|
|
|
34beb74 |
function usercreate(){
|
|
|
34beb74 |
APP=$(basename "$CMD")
|
|
|
34beb74 |
USER_APP="$DATA_DIR/$APP-$EXT"
|
|
|
34beb74 |
if cat $G_CACHE $U_CACHE | grep "$MTPE" | grep -q "$APP"
|
|
|
34beb74 |
then
|
|
|
34beb74 |
zenity --error --text "The mime type: $MTPE is alredy assigned to: $APP"
|
|
|
34beb74 |
NOCH=yes
|
|
|
34beb74 |
else
|
|
|
34beb74 |
if [ -e "$USER_APP" ]
|
|
|
34beb74 |
then
|
|
|
34beb74 |
sed -i "s|MimeType=|MimeType=$MTPE;|g" $USER_APP
|
|
|
34beb74 |
update
|
|
|
34beb74 |
zenity --info --text \
|
|
|
34beb74 |
"The mime type: $MTPE is now registered to be handled by: $APP"
|
|
|
34beb74 |
else
|
|
|
34beb74 |
cat <<EOF >$USER_APP
|
|
|
34beb74 |
[Desktop Entry]
|
|
|
34beb74 |
Encoding=UTF-8
|
|
|
34beb74 |
Name=$NAME
|
|
|
34beb74 |
Exec=$CMD
|
|
|
34beb74 |
Type=Application
|
|
|
34beb74 |
Terminal=false
|
|
|
34beb74 |
NoDisplay=true
|
|
|
34beb74 |
MimeType=$MTPE;
|
|
|
34beb74 |
EOF
|
|
|
34beb74 |
fi
|
|
|
34beb74 |
fi
|
|
|
34beb74 |
if [ -z "$NOCH" ]
|
|
|
34beb74 |
then
|
|
|
34beb74 |
zenity --question --text \
|
|
|
34beb74 |
"The created $(basename "$USER_APP") can now be edited manually, to add, remove or change options, do you want to do this now?
|
|
|
34beb74 |
|
|
|
34beb74 |
This file should work as is, so you can safely cancel this operation, but advanced users may want to edit options to their liking. But due to a bug this editor cannot handle '%' charachters and replaces them with garbage."
|
|
|
34beb74 |
VAL=$?
|
|
|
34beb74 |
if [ "$VAL" == 0 ]
|
|
|
34beb74 |
then
|
|
|
34beb74 |
DT_FILE="$APP"
|
|
|
34beb74 |
dtfedit
|
|
|
34beb74 |
update
|
|
|
34beb74 |
else
|
|
|
34beb74 |
update
|
|
|
34beb74 |
fi
|
|
|
34beb74 |
zenity --info --text "$APP is now registered to handle: $MTPE"
|
|
|
34beb74 |
fi
|
|
|
34beb74 |
NONEW=2
|
|
|
34beb74 |
deflist
|
|
|
34beb74 |
}
|
|
|
34beb74 |
|
|
|
34beb74 |
#
|
|
|
34beb74 |
# Update the users mime chache and set the defaults
|
|
|
34beb74 |
# first from the defaults.list file
|
|
|
34beb74 |
#
|
|
|
34beb74 |
function update (){
|
|
|
34beb74 |
update-desktop-database $DATA_DIR
|
|
|
34beb74 |
for i in $(sed '/\[.*\]/d' $U_DEF); do
|
|
|
34beb74 |
MTYPE=$(echo "$i" | awk -F= '{print $1}')
|
|
|
34beb74 |
M_FIL=$(echo "$i" | awk -F= '{print $2}')
|
|
|
34beb74 |
if grep -q "$MTYPE" $U_CACHE
|
|
|
34beb74 |
then
|
|
|
34beb74 |
sed -i "\|$MTYPE|s|$M_FIL||;s|$MTYPE=|$i;|;s|;;|;|" $U_CACHE
|
|
|
34beb74 |
else
|
|
|
34beb74 |
echo "$i" >> $U_CACHE
|
|
|
34beb74 |
fi
|
|
|
34beb74 |
done
|
|
|
34beb74 |
# If there are rows with empty mimetypes, delete them.
|
|
|
34beb74 |
sed -i '/.*=$/d' $U_CACHE
|
|
|
34beb74 |
}
|
|
|
34beb74 |
|
|
|
34beb74 |
#
|
|
|
34beb74 |
# Give the user a helping hand.
|
|
|
34beb74 |
#
|
|
|
34beb74 |
function showhelp (){
|
|
|
34beb74 |
echo
|
|
|
34beb74 |
echo "Usage: $(basename $0) [ARGUMENT]"
|
|
|
34beb74 |
echo '''
|
|
|
34beb74 |
-h, -?, --help Show this help and exit.
|
|
|
34beb74 |
|
|
|
34beb74 |
DESCRIPTION
|
|
|
34beb74 |
This script is dependent on zenity for creating its GUI. It is
|
|
|
34beb74 |
to be used as a tool for choosing the default application or to
|
|
|
34beb74 |
run for different type of files (mimetypes). It modifies or add
|
|
|
34beb74 |
files in "$HOME/.local/share/applications"
|
|
|
34beb74 |
|
|
|
34beb74 |
When passed a valid filename it checks its mimetype as interpreted
|
|
|
34beb74 |
by gnomevfs, it will show a list of already registered applications
|
|
|
34beb74 |
able to handle the mimetype in question, the user can change default
|
|
|
34beb74 |
application in this list. The list also contains an option to add
|
|
|
34beb74 |
the Name and command of a program that is not registered to be able
|
|
|
34beb74 |
to open files of the mimetype. After any changes this script will
|
|
|
34beb74 |
run the update-desktop-database to update the users mime cache.
|
|
|
34beb74 |
|
|
|
34beb74 |
When executed without any argument, the script will list all
|
|
|
34beb74 |
applications associated with a mimetype. When selecting an item from
|
|
|
34beb74 |
this list, the user will be able to edit the options for the selected
|
|
|
34beb74 |
application. The edited file will be saved in the above path and the
|
|
|
34beb74 |
update-desktop-database is executed.
|
|
|
34beb74 |
|
|
|
34beb74 |
When given an invalid argument or any of the options -h -? --help,
|
|
|
34beb74 |
this text is shown.
|
|
|
34beb74 |
'''
|
|
|
34beb74 |
}
|
|
|
34beb74 |
|
|
|
34beb74 |
#
|
|
|
34beb74 |
# Remove tempfiles.
|
|
|
34beb74 |
#
|
|
|
34beb74 |
function cleanup (){
|
|
|
34beb74 |
rm -f "$UTMP.avail"
|
|
|
34beb74 |
rm -f "$UTMP.chosen"
|
|
|
34beb74 |
}
|
|
|
34beb74 |
|
|
|
34beb74 |
#
|
|
|
34beb74 |
# The .desktop file selector
|
|
|
34beb74 |
#
|
|
|
34beb74 |
function selfile (){
|
|
|
34beb74 |
DT_FILE=$(for i in $GDATA_DIR/*.desktop $DATA_DIR/*.desktop; do
|
|
|
34beb74 |
if grep -q "MimeType" $i
|
|
|
34beb74 |
then
|
|
|
34beb74 |
BNME=$(basename $i | sed 's/\.desktop//;s/-usercreated//')
|
|
|
34beb74 |
if [ -e $DATA_DIR/$BNME-$EXT ]
|
|
|
34beb74 |
then
|
|
|
34beb74 |
i="$DATA_DIR/$BNME-$EXT"
|
|
|
34beb74 |
fi
|
|
|
34beb74 |
CMD=$(grep -m 1 "^Exec" $i | sed "s/^Exec=//")
|
|
|
34beb74 |
NME=$(grep -m 1 "^Name=" $i | sed 's/^Name=//')
|
|
|
34beb74 |
echo "$BNME---$NME---\"$CMD\""
|
|
|
34beb74 |
fi
|
|
|
34beb74 |
done | sort -u | sed 's/---/\n/g' | zenity --list --width="550" --height="400" \
|
|
|
34beb74 |
--column="Base name" --column="Program name" --column="Program command" \
|
|
|
34beb74 |
--print-column="1" --text "Select item below.
|
|
|
34beb74 |
|
|
|
34beb74 |
By selecting an item in this list and klick OK, you will get another window where
|
|
|
34beb74 |
you can edit the file that contains the options of the selected application. The
|
|
|
34beb74 |
edited file will end up in your: $DATA_DIR
|
|
|
34beb74 |
|
|
|
34beb74 |
Pressing "Esc", click Cancel or use the window close (X) button will close without
|
|
|
34beb74 |
any changes in this or the following editor window. Be aware that the editor do
|
|
|
34beb74 |
have problems with the (%)percent charachter and may replace it by garbage.")
|
|
|
34beb74 |
}
|
|
|
34beb74 |
|
|
|
34beb74 |
#
|
|
|
34beb74 |
# The .desktop file editor
|
|
|
34beb74 |
#
|
|
|
34beb74 |
function dtfedit (){
|
|
|
34beb74 |
USER_DTF="$DT_FILE-$EXT"
|
|
|
34beb74 |
if [ -n "$DT_FILE" ]
|
|
|
34beb74 |
then
|
|
|
34beb74 |
# Chose to edit users own file if exist
|
|
|
34beb74 |
if [ -e $DATA_DIR/$USER_DTF ]
|
|
|
34beb74 |
then
|
|
|
34beb74 |
TO_EDIT="$DATA_DIR/$USER_DTF"
|
|
|
34beb74 |
else
|
|
|
34beb74 |
TO_EDIT="$GDATA_DIR/$DT_FILE.desktop"
|
|
|
34beb74 |
fi
|
|
|
34beb74 |
cat "$TO_EDIT" | zenity --height="350" --width="550" --text-info --editable \
|
|
|
34beb74 |
--title "Edititng the file: $USER_DTF" > $UTMP.$USER_DTF
|
|
|
34beb74 |
if [ -s $UTMP.$USER_DTF ]
|
|
|
34beb74 |
then
|
|
|
34beb74 |
mv $UTMP.$USER_DTF $DATA_DIR/$USER_DTF
|
|
|
34beb74 |
else
|
|
|
34beb74 |
rm -f $UTMP.$USER_DTF
|
|
|
34beb74 |
DT_FILE=""
|
|
|
34beb74 |
fi
|
|
|
34beb74 |
fi
|
|
|
34beb74 |
}
|
|
|
34beb74 |
|
|
|
34beb74 |
#
|
|
|
34beb74 |
# Check if we have zenity installed then start this mess.
|
|
|
34beb74 |
#
|
|
|
34beb74 |
if which zenity >&/dev/null
|
|
|
34beb74 |
then
|
|
|
34beb74 |
checkarg
|
|
|
34beb74 |
else
|
|
|
34beb74 |
echo "Error: zenity not found!"
|
|
|
34beb74 |
fi
|
|
|
34beb74 |
|