~flimm/ooo-thumbnailer/releases

« back to all changes in this revision

Viewing changes to ooo-thumbnailer

  • Committer: David D Lowe
  • Date: 2011-11-12 01:11:57 UTC
  • mfrom: (1.1.33 trunk)
  • Revision ID: daviddlowe.flimm@gmail.com-20111112011157-hjtdnjg7riygpuot
0.5 release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/bin/bash
2
2
#
3
3
#  Copyright 2007 Erlend Davidson <Erlend.Davidson@gmail.com>
4
 
#  Copyright 2009-2010 David D Lowe <daviddlowe.flimm@gmail.com>
 
4
#  Copyright 2009-2011 David D Lowe <daviddlowe.flimm@gmail.com>
5
5
#
6
6
#  This program is free software; you can redistribute it and/or modify
7
7
#  it under the terms of the GNU General Public License as published by
19
19
#
20
20
# ooo-thumbnailer
21
21
# usage:
22
 
# ooo-thumbnailer [-m MAX_SIZE] file thumbnail-file size
 
22
# ooo-thumbnailer file thumbnail-file size
23
23
# example:
24
24
# ooo-thumbnailer document.odt thumbnail.png 128
25
25
 
26
26
set -e
27
27
 
28
 
MAX_SIZE=-1
29
 
while getopts 'm:' OPTION
30
 
do
31
 
  case $OPTION in
32
 
  m)    MAX_SIZE="$OPTARG"
33
 
        ;;
34
 
  ?)    echo "Usage: ooo-thumbnailer [-m unoconv_max_size] file thumbnail-file size" 1>&2
35
 
        exit 1
36
 
        ;;
37
 
  esac
38
 
done
39
 
shift $(($OPTIND - 1))
40
 
 
41
28
# command line parameters
42
29
ifile=$1
43
30
ofile=$2
45
32
 
46
33
if [ "$ofile" == "" ]; then
47
34
  echo "Some arguments are missing" 1>&2
48
 
  echo "Usage: ooo-thumbnailer [-m unoconv_max_size] file thumbnail-file size" 1>&2
 
35
  echo "Usage: ooo-thumbnailer file thumbnail-file size" 1>&2
49
36
  exit 1
50
37
fi
51
38
 
55
42
fi
56
43
 
57
44
# Files in the Templates directory are deliberately ignored
 
45
 
58
46
test -f ${XDG_CONFIG_HOME:-$HOME/.config}/user-dirs.dirs && source ${XDG_CONFIG_HOME:-$HOME/.config}/user-dirs.dirs
 
47
 
59
48
if [ "`dirname "$ifile"`" == "${XDG_TEMPLATES_DIR:-$HOME/Templates}" ]; then
60
49
  echo "Ignoring files in XDG templates directory"
61
50
  exit 0
62
51
fi
63
52
 
64
 
mime=`file "$ifile" --mime-type --brief` # mime-type of $ifile
65
 
 
66
 
# This function sets the $icon variable according to mime-type
 
53
# This function prints the filename of the appropriate icon according to mime-type
67
54
# Argument: file
68
 
function set_icon_variable {
 
55
function get_icon {
 
56
 
 
57
  local icon
69
58
  case "`file "$1" --mime-type --brief`" in
70
59
    application/vnd.oasis.opendocument.text | \
71
60
    application/vnd.sun.xml.writer | \
72
61
    application/vnd.sun.xml.writer.global | \
73
62
    application/msword | \
74
63
    application/vnd.openxmlformats-officedocument.wordprocessingml.document)
75
 
      icon=openofficeorg3-oasis-text.png
 
64
      icon=oasis-text.png
76
65
      ;;
77
66
    application/vnd.oasis.opendocument.presentation | \
78
67
    application/vnd.sun.xml.impress | \
79
68
    application/vnd.ms-powerpoint | \
80
69
    application/vnd.openxmlformats-officedocument.presentationml.presentation)
81
 
      icon=openofficeorg3-oasis-presentation.png
 
70
      icon=oasis-presentation.png
82
71
      ;;
83
72
    application/vnd.oasis.opendocument.graphics | \
84
73
    application/vnd.sun.xml.draw)
85
 
      icon=openofficeorg3-oasis-drawing.png
 
74
      icon=oasis-drawing.png
86
75
      ;;
87
76
    application/vnd.oasis.opendocument.spreadsheet | \
88
77
    application/vnd.sun.xml.calc | \
89
78
    application/vnd.ms-excel | \
90
79
    application/vnd.openxmlformats-officedocument.spreadsheetml.sheet)
91
 
      icon=openofficeorg3-oasis-spreadsheet.png
 
80
      icon=oasis-spreadsheet.png
92
81
      ;;
93
82
    application/vnd.oasis.opendocument.presentation-template | \
94
83
    application/vnd.sun.xml.impress.template)
95
 
      icon=openofficeorg3-oasis-presentation-template.png
 
84
      icon=oasis-presentation-template.png
96
85
      ;;
97
86
    application/vnd.oasis.opendocument.spreadsheet-template | \
98
87
    application/vnd.sun.xml.calc.template)
99
 
      icon=openofficeorg3-oasis-spreadsheet-template.png
 
88
      icon=oasis-spreadsheet-template.png
100
89
      ;;
101
90
    application/vnd.oasis.opendocument.text-template | \
102
91
    application/vnd.sun.xml.writer.template)
103
 
      icon=openofficeorg3-oasis-text-template.png
 
92
      icon=oasis-text-template.png
104
93
      ;;
105
94
    application/vnd.oasis.opendocument.text-master)
106
 
      icon=openofficeorg3-oasis-master-document.png
 
95
      icon=oasis-master-document.png
107
96
      ;;
108
97
    application/vnd.oasis.opendocument.graphics-template | \
109
98
    application/vnd.sun.xml.draw.template)
110
 
      icon=openofficeorg3-oasis-drawing-template.png
 
99
      icon=oasis-drawing-template.png
111
100
      ;;
112
101
    application/vnd.oasis.opendocument.chart)
113
 
      icon=openofficeorg3-oasis-drawing.png
 
102
      icon=oasis-drawing.png
114
103
      ;;
115
104
    application/vnd.oasis.opendocument.image)
116
 
      icon=openofficeorg3-oasis-drawing.png
 
105
      icon=oasis-drawing.png
117
106
      ;;
118
107
    application/vnd.oasis.opendocument.formula | \
119
108
    application/vnd.sun.xml.math)
120
 
      icon=openofficeorg3-oasis-formula.png
 
109
      icon=oasis-formula.png
121
110
      ;;
122
111
    *)
123
112
      ext=${1/*./}
124
113
      case "${ext,,}" in
125
114
        odg | sxd | otc)
126
 
          icon=openofficeorg3-oasis-drawing.png
127
 
          ;;
128
 
        docx | doc | sxw)
129
 
          icon=openofficeorg3-oasis-text.png
130
 
          ;;
131
 
        xlsx | ods | sxc)
132
 
          icon=openofficeorg3-oasis-spreadsheet.png
 
115
          icon=oasis-drawing.png
 
116
          ;;
 
117
        docx | doc | odt | sxw)
 
118
          icon=oasis-text.png
 
119
          ;;
 
120
        xls | xlsx | ods | sxc)
 
121
          icon=oasis-spreadsheet.png
133
122
          ;;
134
123
        ppt | pptx | sxi | odp)
135
 
          icon=openofficeorg3-oasis-presentation.png
 
124
          icon=oasis-presentation.png
136
125
          ;;
137
126
        std | otg)
138
 
          icon=openofficeorg3-oasis-drawing-template.png
 
127
          icon=oasis-drawing-template.png
139
128
          ;;
140
129
        sti | otp)
141
 
          icon=openofficeorg3-oasis-presentation-template.png
 
130
          icon=oasis-presentation-template.png
142
131
          ;;
143
132
        stw | ott)
144
 
          icon=openofficeorg3-oasis-text-template.png
 
133
          icon=oasis-text-template.png
145
134
          ;;
146
135
        odm | sxg)
147
 
          icon=openofficeorg3-oasis-master-document.png
 
136
          icon=oasis-master-document.png
148
137
          ;;
149
138
        stc | ots)
150
 
          icon=openofficeorg3-oasis-spreadsheet-template.png
 
139
          icon=oasis-spreadsheet-template.png
151
140
          ;;
152
141
        sxm | odf)
153
 
          icon=openofficeorg3-oasis-formula.png
 
142
          icon=oasis-formula.png
154
143
          ;;
155
144
        *)
156
145
          echo "Unsupported file type (file: $1 extension: $ext mime-type:`file "$1" --mime-type --brief`)" 1>&2
157
146
          exit 5
158
147
      esac
159
148
  esac
160
 
  icon=/usr/share/icons/hicolor/32x32/mimetypes/$icon
 
149
  local prefix
 
150
  prefix=/usr/share/icons/hicolor/32x32/mimetypes
 
151
  for match in "$prefix/openofficeorg3-$icon" "$prefix/libreoffice"*"-$icon" ; do
 
152
    if [ -e "$match" ] ; then
 
153
      printf '%s\n' "$match"
 
154
      return 0
 
155
    fi
 
156
  done
 
157
  return 1
161
158
}
162
159
 
163
 
# This function tries to use gsf-office-thumbnailer, overlaying an 
164
 
# OpenOffice.org icon on the thumbnail, and returns an appropriate
 
160
# This function tries to use gsf-office-thumbnailer, and returns an appropriate
165
161
# exit code
166
162
# arguments: file thumbnail-file
167
163
function try_gsf_thumbnailer {
171
167
    # gsf-office-thumbnailer is buggy when it comes to Excel files
172
168
    return 1
173
169
  fi
174
 
  if gsf-office-thumbnailer -i "$1" -o "$2" -s "${size:-128}"; then
175
 
    set_icon_variable "$1"
176
 
    if [ -x /usr/bin/composite ] && [ -e "$icon" ]; then
177
 
      convert "$2" -background white -flatten -scale ${size:-128}x${size:-128} "png:-" | composite -gravity SouthEast "$icon" - "$2"
178
 
    fi
179
 
    return 0
180
 
  fi
181
 
  return 1
182
 
}
183
 
 
184
 
try_gsf_thumbnailer "$ifile" "$ofile" && exit 0
185
 
 
186
 
# If script continues past this point then gsf-office-thumbnailer
187
 
# has been unsuccessful.
188
 
 
189
 
# If file is in Microsoft Office format, convert the 
190
 
# file to OpenOffice.org format and use that new temporary file
191
 
iextension=$(expr "$ifile" : '.*\(....\)')
192
 
iextension=${iextension,,}
193
 
if [ "$mime" == application/vnd.ms-powerpoint -o "$mime" == application/vnd.openxmlformats-officedocument.presentationml.presentation \
194
 
  -o "$mime" == application/vnd.ms-excel -o "$mime" == application/vnd.openxmlformats-officedocument.spreadsheetml.sheet \
195
 
  -o "$mime" == application/msword -o "$mime" == application/vnd.ms-office \
196
 
  -o "$mime" == application/vnd.openxmlformats-officedocument.wordprocessingml.document \
197
 
  -o "$iextension" == xlsx \
198
 
  -o "$iextension" == docx ]
199
 
then
200
 
    if [ "$MAX_SIZE" -gt 0 -a "`stat -c%s "$ifile"`" -gt $MAX_SIZE ]; then
201
 
      echo "Skipping conversion of Microsoft Office file bigger than $MAX_SIZE"
202
 
      exit 9
203
 
    fi
204
 
    
205
 
    if [ "$mime" == application/vnd.ms-powerpoint \
206
 
    -o "$mime" == application/vnd.openxmlformats-officedocument.presentationml.presentation ]; then
207
 
        extension=odp
208
 
    elif [ "$mime" == application/vnd.ms-excel \
209
 
    -o "$mime" == application/vnd.openxmlformats-officedocument.spreadsheetml.sheet \
210
 
    -o "$iextension" == xlsx ]; then
211
 
        extension=ods
212
 
    elif [ "$mime" == application/msword \
213
 
    -o "$mime" == application/vnd.openxmlformats-officedocument.wordprocessingml.document \
214
 
    -o "$iextension" == docx -o "$iextension" == doc ]; then
215
 
        extension=odt
216
 
    else
217
 
        echo "Unrecongised Microsoft Office file type" 1>&2
218
 
        exit 6
219
 
    fi
220
 
    tmpfile=`mktemp --tmpdir="$cache"`.$extension
221
 
    echo "Converting Microsoft Office file to OpenOffice.org format"
222
 
    unoconv -f "$extension" --stdout "$ifile" 1> "$tmpfile"
223
 
    try_gsf_thumbnailer "$tmpfile" "$ofile" || { 
224
 
                echo "gsf-office-thumbnailer failed" 1>&2
225
 
                rm "$tmpfile"
226
 
                exit 1
227
 
    }
228
 
    rm "$tmpfile"
229
 
else
230
 
        # Not a Microsoft Office file, and gsf-office-thumbnailer previously
231
 
        # failed.
232
 
    echo "ooo-thumbnailer: gsf-office-thumbnailer could not thumbnail this OpenOffice.org file" 1>&2
233
 
    exit 3
234
 
fi
 
170
  gsf-office-thumbnailer -i "$1" -o "$2" -s "${size:-128}"
 
171
  return $?
 
172
}
 
173
 
 
174
# This functions decorates a thumbnail with a mime-type icon
 
175
# arguments: document-filename thumbnail-filename
 
176
function decorate_with_icon {
 
177
  local icon
 
178
  icon=$(get_icon "$1")
 
179
  if [ ! -e "$icon" ] ; then
 
180
    echo "$icon does not exist" 1>&2
 
181
    return 1
 
182
  fi
 
183
  if [ -x /usr/bin/composite ] && [ -e "$icon" ]; then
 
184
    convert "$1" -background white -flatten -scale ${size:-128}x${size:-128} "png:-" | composite -gravity SouthEast "$icon" - "$2"
 
185
  fi
 
186
  return 0
 
187
}
 
188
 
 
189
# Prepare temporary intermediate file
 
190
 
 
191
tmpfile=$(mktemp --tmpdir="${XDG_CACHE_HOME:-$HOME/.cache}" --suffix="${ifile##*/}" )
 
192
function cleanup {
 
193
  rm -f "$tmpfile"
 
194
}
 
195
trap cleanup ERR
 
196
 
 
197
# Ready for action!
 
198
try_gsf_thumbnailer "$ifile" "$tmpfile"
 
199
decorate_with_icon "$tmpfile" "$ofile"
 
200