gcmd-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gcmd-dev] Simple mimetype editor


From: Magnus Stålnacke
Subject: [gcmd-dev] Simple mimetype editor
Date: Fri, 03 Aug 2007 15:36:36 +0200
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.4) Gecko/20061201 Firefox/2.0.0.4

Hello.

I have written a basic mimetype editor in a shellscript using zenity
to get a nice GTK inteface. I am attaching the script to this message.
Try it if you want. It can be used through the gcmd file properties
dialog (rightclick context menu -- properties...), where you can
use the "Change" button. Just place the script in you path (or
symlink to it from /usr/local/bin or something like that), it has to
be saved with the name "gnome-file-types-properties" to work
from gcmd.

It does so far only the basic tasks, like changing default "open with
application" or adding new applications not yet registered to be able
to handle the mimetype in question. The script works on the files
in the $HOME/.local/share/application folder, so back them up
before playing around...


#!/bin/bash
#
# gnome-file-types-properties - 
# A zenity- script for chosing prefered applications. 
# Written by Magnus Stålnacke (jemamo at telia.com) 
# Version 0.7 (3 Aug. 2007)
#
export LANG=en_US.utf8 
DATA_DIR=$HOME/.local/share/applications
G_CACHE=/usr/share/applications/mimeinfo.cache
U_CACHE=$DATA_DIR/mimeinfo.cache
U_DEF=$DATA_DIR/defaults.list
EXT=usercreated.desktop
if [ -e "$U_DEF" ]; then :; else echo > defaults.list;fi

#
# Update the users mime chache and set the defaults 
# first from the defaults.list file
#
function update (){
  update-desktop-database $DATA_DIR
  for i in $(cat $U_DEF); do
    MTYPE=$(echo "$i" | awk -F= '{print $1}')
    M_FIL=$(echo "$i" | awk -F= '{print $2}')
    if grep -q "$MTYPE" $U_CACHE
    then
     ROW=$(grep "$MTYPE" $U_CACHE | sed "s|$M_FIL||;s|$MTYPE=|$i;|;s|;;|;|")
     sed -i "s|$MTYPE=.*|$ROW|" $U_CACHE
    else
     echo "$i" >> $U_CACHE
    fi
  done
}

#
# Show a list with allredy registered apps for the 
# mimetype in question.
#
function deflist (){
  # Create the list in /tmp/avail to be used by the
  # zenity list-box
  echo "FALSE Add_New_App New" > /tmp/avail
  G_AVAIL=`grep -s "$MTPE" $G_CACHE | sed -e 's/.*=//g;s/;$//g;s/;/\n/g'`
  U_AVAIL=`grep -s "$MTPE" $U_CACHE | sed -e 's/.*=//g;s/;$//g;s/;/\n/g;' | 
grep "userc"`
  for i in $G_AVAIL; do
    NM=$(grep -s "^Name=" /usr/share/applications/$i | sed 's/Name=//;s/ /_/g')
    echo "FALSE $i $NM" >> /tmp/avail
  done
  for i in $U_AVAIL; do
    NM=`grep -s "^Name=" $DATA_DIR/$i | sed 's/Name=//g;s/ /_/g'`
    echo "FALSE $i $NM" >> /tmp/avail
  done
  # Change FALSE to TRUE for the default app. 
  if [ -n "$G_AVAIL" -o -n "$U_AVAIL" ]
  then
    if grep -q "$MTPE" $U_CACHE
    then
      DEF_APP=$(grep -s "$MTPE" $U_CACHE | sed "s|$MTPE=||g;s|\;.*||")
      sed -i "s|FALSE $DEF_APP|TRUE $DEF_APP|" /tmp/avail
    else
      DEF_APP=$(grep -s "$MTPE" $G_CACHE | sed "s|$MTPE=||g;s|\;.*||")
      sed -i "s|FALSE $DEF_APP|TRUE $DEF_APP|" /tmp/avail
    fi
  else
    echo "que?"
  fi
  #Show the list
  zenity --list --text "Please select" --radiolist \
  --column "Pick" --column ".desktop file" --column "Application" \
  $(cat /tmp/avail) > /tmp/chosen
  retval=$?
  choice=$(cat /tmp/chosen)
  if grep -q "$MTPE=$choice" $U_DEF
  then
  retval=1
  update
  fi
  case $retval in
  0)
    DEF_CH=$(grep -s "$choice" /tmp/avail | awk '{print $2}')
    if [ "$DEF_CH" = Add_New_App ]
    then
      newmime
    else
      if grep -q "$MTPE" $U_DEF
      then
        # Set the users chosen default app for the existing mime.
        sed -i "s|$MTPE=.*|$MTPE=$DEF_CH|" $U_DEF
      else
        # If mime is missing add it with the chosen app file.
        echo "$MTPE=$DEF_CH" >> $U_DEF
      fi
      update
      DEF_MESSAGE="Default application to handle $MTPE is set to $choice"
    fi;;
  1)
    echo "Cancel pressed.";;
  -1)
    echo "ESC pressed.";;
  esac
  rm -f /tmp/avail
  rm -f /tmp/chosen
}

#
# New mimetype input box
#
function newmime (){
  NEWAPP=$(zenity --entry --text \
"Fill in the name and command for the application you want to handle the 
mimetype: $MTPE 

Separate Name and Command with a colon. If you omit the command, or 
type something that is not in your path, you will get the chance to 
provide the command with full path by a file chooser dialog.")

  case $? in 
  0)
   if echo "$NEWAPP" | grep -q ":"
   then
     NAME=$(echo "$NEWAPP" | awk -F: '{print $1}')
     CMD=$(echo "$NEWAPP" | awk -F: '{print $2}')
     if [ -n "$NAME" ]
     then
       check
       usercreate
     else
       zenity --error --text "Application Name is missing"
       newmime
     fi
   else
     zenity --error --text "Separator missing"
     newmime
   fi
   ;;
  1)
   deflist;;
  -1)
   deflist;;
  esac
}

#
# Chech if the command exists in path, if not give the 
# user a filechooser.
#
function check (){
  if which $CMD >&/dev/null
  then
    CMD=`which $CMD`
  else
    CMD=$(zenity --file-selection --filename=/ --title="Chose application")
  fi
}

#
# Create a *.desktop file for the added app that can handle 
# the mimetype in question.
#
function usercreate(){
  APP=$(basename "$CMD")
  GLOB_USER=`cat $U_CACHE $G_CACHE`
  USER_APP="$DATA_DIR/$APP-$EXT"
  if echo "$GLOB_USER" | grep "$MTPE" | grep -q "$APP"
  then
    zenity --error --text "The mime type: $MTPE is alredy assigned to: $APP"
    deflist
  else
    if [ -e "$USER_APP" ]
    then
      sed -i "s|MimeType=|MimeType=$MTPE;|g" $USER_APP
      update
      zenity --info --text \
      "The mime type: $MTPE is now registered to be handled by: $APP"
    else
      cat <<EOF >$USER_APP 
[Desktop Entry]
Encoding=UTF-8
Name=$NAME
MimeType=$MTPE;
Exec='$CMD'
Type=Application
Terminal=false
NoDisplay=true
EOF
    fi
  fi
  update
  zenity --info --text \
  "The application: $APP is now registered to handle: $MTPE"
  deflist
}

#
# Check how we are started and set/check the mimetype 
# depending on what argument we are given. 
#
MTPE=$(if sed '/^#/d' /etc/file/magic.mime | grep -q "$1"
  then
    echo "$1"
  else 
    if [ -e "$1" ]
    then
      echo $(file -bi "$1" | awk '{print $1}' | sed 's/;//')
    fi
  fi)

case $MTPE in
  */*)
    echo "I was called with $1 as argument"
    echo "The mimetype is $MTPE"
    deflist
    ;;
  *)
    echo "No argument given, or unknown mimetype or file"
    ;;
esac

reply via email to

[Prev in Thread] Current Thread [Next in Thread]