1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
#!/bin/bash -e
#
# Copyright 2007 Erlend Davidson <Erlend.Davidson@gmail.com>
# Copyright 2009-2010 David D Lowe <daviddlowe.flimm@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ooo-thumbnailer
# usage:
# ooo-thumbnailer file thumbnail-file size
# example:
# ooo-thumbnailer document.odt thumbnail.png 128
# Get user's XDG cache directory
test -f ${XDG_CONFIG_HOME:-$HOME/.config}/user-dirs.dirs && source ${XDG_CONFIG_HOME:-$HOME/.config}/user-dirs.dirs
cache=${XDG_CACHE_HOME:-$HOME/.cache}
mkdir -p "$cache"
# Set default options
OOOTHUMBNAILER_TIMEOUT=10
OOOTHUMBNAILER_UNOCONV_MAX_SIZE=8000000
# Create configuration file if it does not exist
if [ ! -f ${XDG_CONFIG_HOME:-$HOME/.config}/ooo-thumbnailer.conf ]; then
echo "make"
echo "# User options for ooo-thumbnailer file
OOOTHUMBNAILER_TIMEOUT=10 # in seconds
OOOTHUMBNAILER_UNOCONV_MAX_SIZE=8000000 # in bytes" > ${XDG_CONFIG_HOME:-$HOME/.config}/ooo-thumbnailer.conf
fi
# Get options
source ${XDG_CONFIG_HOME:-$HOME/.config}/ooo-thumbnailer.conf
# Override options with OVERRIDE environment variables if they have been specified
OOOTHUMBNAILER_TIMEOUT=${OVERRIDE_OOOTHUMBNAILER_TIMEOUT:-$OOOTHUMBNAILER_TIMEOUT}
OOOTHUMBNAILER_UNOCONV_MAX_SIZE=${OVERRIDE_OOOTHUMBNAILER_UNOCONV_MAX_SIZE:-$OOOTHUMBNAILER_UNOCONV_MAX_SIZE}
# command line parameters
ifile=$1
ofile=$2
size=$3
if [ "$ofile" == "" ]; then
echo "Some arguments are missing" 1>&2
echo "Usage: ooo-thumbnailer file thumbnail-file size" 1>&2
exit 1
fi
# Files in the Templates directory are deliberately ignored
if [ "`dirname "$ifile"`" == "${XDG_TEMPLATES_DIR:-$HOME/Templates}" ]; then
echo "Ignoring files in XDG templates directory"
exit 0
fi
{
mime=`file "$ifile" --mime-type --brief` # mime-type of $ifile
# This function sets the $icon variable according to mime-type
# Argument: file
function set_icon_variable {
case "`file "$1" --mime-type --brief`" in
application/vnd.oasis.opendocument.text | \
application/vnd.sun.xml.writer | \
application/vnd.sun.xml.writer.global | \
application/msword | \
application/vnd.openxmlformats-officedocument.wordprocessingml.document)
icon=openofficeorg3-oasis-text.png
;;
application/vnd.oasis.opendocument.presentation | \
application/vnd.sun.xml.impress | \
application/vnd.ms-powerpoint | \
application/vnd.openxmlformats-officedocument.presentationml.presentation)
icon=openofficeorg3-oasis-presentation.png
;;
application/vnd.oasis.opendocument.graphics | \
application/vnd.sun.xml.draw)
icon=openofficeorg3-oasis-drawing.png
;;
application/vnd.oasis.opendocument.spreadsheet | \
application/vnd.sun.xml.calc | \
application/vnd.ms-excel | \
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet)
icon=openofficeorg3-oasis-spreadsheet.png
;;
application/vnd.oasis.opendocument.presentation-template | \
application/vnd.sun.xml.impress.template)
icon=openofficeorg3-oasis-presentation-template.png
;;
application/vnd.oasis.opendocument.spreadsheet-template | \
application/vnd.sun.xml.calc.template)
icon=openofficeorg3-oasis-spreadsheet-template.png
;;
application/vnd.oasis.opendocument.text-template | \
application/vnd.sun.xml.writer.template)
icon=openofficeorg3-oasis-text-template.png
;;
application/vnd.oasis.opendocument.text-master)
icon=openofficeorg3-oasis-master-document.png
;;
application/vnd.oasis.opendocument.graphics-template | \
application/vnd.sun.xml.draw.template)
icon=openofficeorg3-oasis-drawing-template.png
;;
application/vnd.oasis.opendocument.chart)
icon=openofficeorg3-oasis-drawing.png
;;
application/vnd.oasis.opendocument.image)
icon=openofficeorg3-oasis-drawing.png
;;
application/vnd.oasis.opendocument.formula | \
application/vnd.sun.xml.math)
icon=openofficeorg3-oasis-formula.png
;;
*)
ext=${1/*./}
case "$ext" in
odg | sxd | otc)
icon=openofficeorg3-oasis-drawing.png
;;
docx | doc | sxw)
icon=openofficeorg3-oasis-text.png
;;
xlsx | ods | sxc)
icon=openofficeorg3-oasis-spreadsheet.png
;;
ppt | pptx | sxi | odp)
icon=openofficeorg3-oasis-presentation.png
;;
std | otg)
icon=openofficeorg3-oasis-drawing-template.png
;;
sti | otp)
icon=openofficeorg3-oasis-presentation-template.png
;;
stw | ott)
icon=openofficeorg3-oasis-text-template.png
;;
odm | sxg)
icon=openofficeorg3-oasis-master-document.png
;;
stc | ots)
icon=openofficeorg3-oasis-spreadsheet-template.png
;;
sxm | odf)
icon=openofficeorg3-oasis-formula.png
;;
*)
echo "Unsupported file type (file: $1 extension: $ext mime-type:`file "$1" --mime-type --brief`)" 1>&2
exit 5
esac
esac
icon=/usr/share/icons/hicolor/32x32/mimetypes/$icon
}
# This function tries to use gsf-office-thumbnailer, overlaying an
# OpenOffice.org icon on the thumbnail, and returns an appropriate
# exit code
# arguments: file thumbnail-file
function try_gsf_thumbnailer {
ext=${1/*./}
if [ "$ext" == "xls" -o "$ext" == "xlsx" ]; then
# gsf-office-thumbnailer is buggy when it comes to Excel files
return 1
fi
if gsf-office-thumbnailer -i "$1" -o "$2" -s "${size:-128}"; then
set_icon_variable "$1"
if [ -x /usr/bin/composite ] && [ -e "$icon" ]; then
convert "$2" -background white -flatten -scale ${size:-128}x${size:-128} "png:-" | composite -gravity SouthEast "$icon" - "$2"
fi
return 0
fi
return 1
}
try_gsf_thumbnailer "$ifile" "$ofile" && exit 0
# If script continues past this point then gsf-office-thumbnailer
# has been unsuccessful.
# This function launches unoconv --listener if it hasn't been launched
# already using ooo-thumbnailer-daemon
function set_up_unoconv {
if ! ps -e -o "cmd" | grep -e '^/bin/bash .*/ooo-thumbnailer-daemon' > /dev/null
then
echo "Starting ooo-thumbnailer-daemon"
if [ -x "`dirname "$0"`/ooo-thumbnailer-daemon" ]; then
echo "Using `dirname "$0"`/ooo-thumbnailer-daemon"
nohup "`dirname "$0"`/ooo-thumbnailer-daemon" 0<&- 1>/dev/null 2>&1 &
else
nohup /usr/share/ooo-thumbnailer/ooo-thumbnailer-daemon 0<&- 1>/dev/null 2>&1 &
fi
fi
wait=0
until ps -C soffice.bin -o "args" | grep -v -e '^grep' | grep -- '-nologo.*-nodefault.*-accept=' > /dev/null
do
sleep 0.1
wait=$((wait + 1))
if [ "$wait" -eq "500" ]; then
echo "Error: waited 50 seconds for unoconv to initialize" 1>&2
exit 2
fi
done
}
# If file is in Microsoft Office format, convert the
# file to OpenOffice.org format and use that new temporary file
iextension=$(expr "$ifile" : '.*\(....\)')
if [ "$mime" == application/vnd.ms-powerpoint -o "$mime" == application/vnd.openxmlformats-officedocument.presentationml.presentation \
-o "$mime" == application/vnd.ms-excel -o "$mime" == application/vnd.openxmlformats-officedocument.spreadsheetml.sheet \
-o "$mime" == application/msword -o "$mime" == application/vnd.ms-office \
-o "$mime" == application/vnd.openxmlformats-officedocument.wordprocessingml.document \
-o "$iextension" == xlsx \
-o "$iextension" == docx ]
then
if [ $OOOTHUMBNAILER_UNOCONV_MAX_SIZE -gt 0 -a "`stat -c%s "$ifile"`" -gt $OOOTHUMBNAILER_UNOCONV_MAX_SIZE ]; then
echo "Skipping conversion of Microsoft Office file bigger than $OOOTHUMBNAILER_UNOCONV_MAX_SIZE"
echo "To change this, modify ${XDG_CONFIG_HOME:-$HOME/.config}/ooo-thumbnailer.conf"
exit 9
fi
set_up_unoconv
if [ "$mime" == application/vnd.ms-powerpoint \
-o "$mime" == application/vnd.openxmlformats-officedocument.presentationml.presentation ]; then
extension=odp
elif [ "$mime" == application/vnd.ms-excel \
-o "$mime" == application/vnd.openxmlformats-officedocument.spreadsheetml.sheet \
-o "$iextension" == xlsx ]; then
extension=ods
elif [ "$mime" == application/msword \
-o "$mime" == application/vnd.openxmlformats-officedocument.wordprocessingml.document \
-o "$iextension" == docx -o "$iextension" == doc ]; then
extension=odt
else
echo "Unrecongised Microsoft Office file type" 1>&2
exit 6
fi
tmpfile=`mktemp --tmpdir="$cache"`.$extension
echo "Converting Microsoft Office file to OpenOffice.org format"
killall -s USR1 ooo-thumbnailer-daemon # reset idle time of daemon
unoconv -f "$extension" --stdout "$ifile" 1> "$tmpfile"
try_gsf_thumbnailer "$tmpfile" "$ofile" || {
echo "gsf-office-thumbnailer failed" 1>&2
rm "$tmpfile"
exit 1
}
rm "$tmpfile"
else
# Not a Microsoft Office file, and gsf-office-thumbnailer previously
# failed.
echo "ooo-thumbnailer: gsf-office-thumbnailer could not thumbnail this OpenOffice.org file" 1>&2
exit 3
fi
} &
pid=$!
if [ $OOOTHUMBNAILER_TIMEOUT -gt 0 ]; then
# If the script takes too long, kill it along with unoconv
echo "sleep $OOOTHUMBNAILER_TIMEOUT; pkill -P $pid unoconv; kill $pid" | at now 2> /dev/null
wait $pid &>/dev/null || {
if [ $? -eq 143 ]; then
echo "Timeout of $OOOTHUMBNAILER_TIMEOUT reached" 1>&2
exit 8
fi
}
else
wait $pid &>/dev/null || true
fi
|