1#!/bin/sh
2#---------------------------------------------
3#   xdg-file-dialog
4#
5#   Utility script to file selection dialogs
6#   on XDG compliant systems.
7#
8#   Refer to the usage() function below for usage.
9#
10#   Copyright 2009-2010, Fathi Boudra <fabo@freedesktop.org>
11#   Copyright 2009-2010, Rex Dieter <rdieter@fedoraproject.org>
12#   Copyright 2006, Kevin Krammer <kevin.krammer@gmx.at>
13#
14#   LICENSE:
15#
16#---------------------------------------------
17
18manualpage()
19{
20cat << _MANUALPAGE
21_MANUALPAGE
22}
23
24usage()
25{
26cat << _USAGE
27_USAGE
28}
29
30#@xdg-utils-common@
31
32open_kde()
33{
34    DIALOG=`which kdialog`
35    if [ $? -eq 0 ]; then
36        if [ x"$TITLE" != x"" ]; then
37            $DIALOG --title "$TITLE" --getopenfilename "$1" ""
38        else
39            $DIALOG --getopenfilename "$1" ""
40        fi
41
42        if [ $? -eq 0 ]; then
43            exit_success
44        else
45            exit_failure_operation_failed
46        fi
47    else
48        exit_failure_operation_impossible
49    fi
50}
51
52open_zenity()
53{
54    DIALOG=`which zenity`
55    if [ $? -eq 0 ]; then
56        if [ x"$1" != x"" ]; then
57            cd `dirname "$1"` 2>/dev/null
58            FILENAME=`basename "$1"`
59            if [ x"$FILENAME" != x"" ]; then
60                FILENAME="--filename=""$FILENAME"
61            fi
62        fi
63
64        if [ x"$FILENAME" != x"" ]; then
65            if [ x"$TITLE" != x"" ]; then
66                $DIALOG --title "$TITLE" --file-selection "$FILENAME"
67            else
68                $DIALOG --file-selection "$FILENAME"
69            fi
70        else
71            if [ x"$TITLE" != x"" ]; then
72                $DIALOG --title "$TITLE" --file-selection
73            else
74                $DIALOG --file-selection
75            fi
76        fi
77
78        if [ $? -eq 0 ]; then
79            exit_success
80        else
81            exit_failure_operation_failed
82        fi
83    else
84        exit_failure_operation_impossible
85    fi
86}
87
88open_multi_kde()
89{
90    DIALOG=`which kdialog`
91    if [ $? -eq 0 ]; then
92        if [ x"$TITLE" != x"" ]; then
93            $DIALOG --title "$TITLE" --multiple --separate-output \
94                    --getopenfilename "$1" ""
95        else
96            $DIALOG --multiple --separate-output --getopenfilename "$1" ""
97        fi
98
99        if [ $? -eq 0 ]; then
100            exit_success
101        else
102            exit_failure_operation_failed
103        fi
104    else
105        exit_failure_operation_impossible
106    fi
107}
108
109open_multi_zenity()
110{
111    DIALOG=`which zenity`
112    if [ $? -eq 0 ]; then
113        if [ x"$1" != x"" ]; then
114            cd `dirname "$1"` 2>/dev/null
115            FILENAME=`basename "$1"`
116            if [ x"$FILENAME" != x"" ]; then
117                FILENAME="--filename=""$FILENAME"
118            fi
119        fi
120
121        if [ x"$FILENAME" != x"" ]; then
122            if [ x"$TITLE" != x"" ]; then
123                LIST=`$DIALOG --title "$TITLE" --multiple --file-selection "$FILENAME"`
124            else
125                LIST=`$DIALOG --multiple --file-selection "$FILENAME"`
126            fi
127        else
128            if [ x"$TITLE" != x"" ]; then
129                LIST=`$DIALOG --title "$TITLE" --multiple --file-selection`
130            else
131                LIST=`$DIALOG --multiple --file-selection`
132            fi
133        fi
134
135        if [ $? -eq 0 ]; then
136            echo "$LIST" | sed s#'|'#\\n#g
137            exit_success
138        else
139            exit_failure_operation_failed
140        fi
141    else
142        exit_failure_operation_impossible
143    fi
144}
145
146save_kde()
147{
148    DIALOG=`which kdialog`
149    if [ $? -eq 0 ]; then
150        if [ x"$TITLE" != x"" ]; then
151            $DIALOG --title "$TITLE" --getsavefilename "$1" ""
152        else
153            $DIALOG --getsavefilename "$1" ""
154        fi
155
156        if [ $? -eq 0 ]; then
157            exit_success
158        else
159            exit_failure_operation_failed
160        fi
161    else
162        exit_failure_operation_impossible
163    fi
164}
165
166save_zenity()
167{
168    DIALOG=`which zenity`
169    if [ $? -eq 0 ]; then
170        if [ x"$1" != x"" ]; then
171            cd `dirname "$1"` 2>/dev/null
172            FILENAME=`basename "$1"`
173            if [ x"$FILENAME" != x"" ]; then
174                FILENAME="--filename=""$FILENAME"
175            fi
176        fi
177
178        if [ x"$FILENAME" != x"" ]; then
179            if [ x"$TITLE" != x"" ]; then
180                $DIALOG --title "$TITLE" --save --file-selection "$FILENAME"
181            else
182                $DIALOG --save --file-selection "$FILENAME"
183            fi
184        else
185            if [ x"$TITLE" != x"" ]; then
186                $DIALOG --title "$TITLE" --save --file-selection
187            else
188                $DIALOG --save --file-selection
189            fi
190        fi
191
192        if [ $? -eq 0 ]; then
193            exit_success
194        else
195            exit_failure_operation_failed
196        fi
197    else
198        exit_failure_operation_impossible
199    fi
200}
201
202directory_kde()
203{
204    DIALOG=`which kdialog`
205    if [ $? -eq 0 ]; then
206        if [ x"$TITLE" != x"" ]; then
207            $DIALOG --title "$TITLE" --getexistingdirectory "$1" ""
208        else
209            $DIALOG --getexistingdirectory "$1" ""
210        fi
211
212        if [ $? -eq 0 ]; then
213            exit_success
214        else
215            exit_failure_operation_failed
216        fi
217    else
218        exit_failure_operation_impossible
219    fi
220}
221
222directory_zenity()
223{
224    DIALOG=`which zenity`
225    if [ $? -eq 0 ]; then
226        if [ x"$1" != x"" ]; then
227            cd "$1" 2>/dev/null
228        fi
229
230        if [ x"$TITLE" != x"" ]; then
231            $DIALOG --title "$TITLE" --directory --file-selection
232        else
233            $DIALOG --directory --file-selection
234        fi
235
236        if [ $? -eq 0 ]; then
237            exit_success
238        else
239            exit_failure_operation_failed
240        fi
241    else
242        exit_failure_operation_impossible
243    fi
244}
245
246[ x"$1" != x"" ] || exit_failure_syntax
247
248TITLE=
249action=
250filename=
251
252case $1 in
253  openfilename)
254    action=openfilename
255    ;;
256
257  openfilenamelist)
258    action=openfilenamelist
259    ;;
260
261  savefilename)
262    action=savefilename
263    ;;
264
265  directory)
266    action=directory
267    ;;
268
269  *)
270  exit_failure_syntax "unknown command '$1'"
271  ;;
272esac
273
274shift
275
276while [ $# -gt 0 ] ; do
277    parm="$1"
278    shift
279
280    case "$parm" in
281      --title)
282        if [ -z "$1" ] ; then
283            exit_failure_syntax "TITLE argument missing for --title"
284        fi
285        TITLE="$1"
286        shift
287        ;;
288
289      -*)
290        exit_failure_syntax "unexpected option '$parm'"
291        ;;
292
293      *)
294        if [ -n "$filename" ] ; then
295            exit_failure_syntax "unexpected argument '$parm'"
296        fi
297        filename="$parm"
298        ;;
299    esac
300done
301
302# Shouldn't happen
303if [ -z "$action" ] ; then
304    exit_failure_syntax "command argument missing"
305fi
306
307detectDE
308
309if [ "$action" = "openfilename" ]; then
310    case "$DE" in
311        kde)
312        open_kde "$filename"
313        ;;
314
315        gnome*|cinnamon|lxde|mate|xfce)
316        open_zenity "$filename"
317        ;;
318
319        *)
320        exit_failure_operation_impossible "no method available for opening a filename dialog"
321        ;;
322    esac
323elif [ "$action" = "openfilenamelist" ]; then
324    case "$DE" in
325        kde)
326        open_multi_kde "$filename"
327        ;;
328
329        gnome*|cinnamon|lxde|mate|xfce)
330        open_multi_zenity "$filename"
331        ;;
332
333        *)
334        exit_failure_operation_impossible "no method available for opening a filename dialog"
335        ;;
336    esac
337elif [ "$action" = "savefilename" ]; then
338    case "$DE" in
339        kde)
340        save_kde "$filename"
341        ;;
342
343        gnome*|cinnamon|lxde|mate|xfce)
344        save_zenity "$filename"
345        ;;
346
347        *)
348        exit_failure_operation_impossible "no method available for opening a filename dialog"
349        ;;
350    esac
351elif [ "$action" = "directory" ]; then
352    case "$DE" in
353        kde)
354        directory_kde "$filename"
355        ;;
356
357        gnome*|cinnamon|lxde|mate|xfce)
358        directory_zenity "$filename"
359        ;;
360
361        *)
362        exit_failure_operation_impossible "no method available for opening a directory dialog"
363        ;;
364    esac
365fi
366