Fix #1236: Window Layouts with vertical split are shifted to the left if they are...
[geeqie.git] / plugins / refresh-thumbnail / geeqie-refresh-thumbnail.sh
1 #!/bin/sh
2
3 ## @file
4 ## @brief Import all images from camera
5 ##
6 ## Requires gphoto2
7 ##
8 finish()
9 {
10         if [ -f /tmp/geeqie-camera-import-files ]
11         then
12                 rm /tmp/geeqie-camera-import-files
13         fi
14
15         if [ -p "$zen_pipe" ]
16         then
17                 rm "$zen_pipe"
18         fi
19
20         if [ "$gphoto2_pid" != "" ]
21         then
22                 if ps -p "$gphoto2_pid" > /dev/null
23                 then
24                         kill "$gphoto2_pid"
25                 fi
26         fi
27
28         if [ "$zen_pid" != "" ]
29         then
30                 if ps -p "$zen_pid" > /dev/null
31                 then
32                         kill "$zen_pid"
33                 fi
34         fi
35 }
36 trap finish EXIT
37
38 if [ $XDG_CACHE_HOME ]
39 then
40         
41         thumb_dir="$XDG_CACHE_HOME"
42 else
43         thumb_dir="$HOME/.cache/geeqie/thumbnails"
44 fi
45 echo $thumb_dir
46
47 exit
48
49
50 if ! [ -x "$(command -v gphoto2)" ]
51 then
52         zenity --title="Geeqie camera import" --info --width=200 --text="gphoto2 is not installed" 2> /dev/null
53         exit 0
54 fi
55
56 if [ -f /tmp/geeqie-camera-import.log ]
57 then
58         rm /tmp/geeqie-camera-import.log
59 fi
60
61 if [ "$(gphoto2 --auto-detect | wc -l)" -le 2 ]
62 then
63         zenity --error --title="Geeqie camera import" --text="No camera detected" --window-icon=/usr/local/share/pixmaps/geeqie.png --width=250 2> /dev/null
64         exit 0
65 fi
66
67 IFS='
68 '
69
70 list=$(gphoto2 --auto-detect | tail -n +3)
71 camera_list=""
72 n=1
73 count=$(gphoto2 --auto-detect | tail -n +3 | wc -l)
74 if [ "$count" -gt 1 ]
75 then
76         for camera in $list
77         do
78                 if [ "$n" -eq "$count" ]
79                 then
80                         camera_list="${camera_list:+${camera_list}}TRUE\n$camera\n$n"
81                 else
82                         camera_list="${camera_list:+${camera_list}}FALSE\n$camera\n$n\n"
83                 fi
84                 n=$((n + 1))
85         done
86
87         camera_selected=$(printf '%b' "$camera_list" | zenity --width=500 --height=250 --title="Geeqie camera import" --list --text "Select camera" --radiolist --column "Select" --column "Camera" --column "n" --print-column=2 2> /dev/null)
88
89         if [ $? = 1 ]
90         then
91                 exit 0
92         fi
93 else
94         camera_selected=$(gphoto2 --auto-detect | tail +3)
95 fi
96
97 port_type=$(printf '%s\n' "$camera_selected" | awk -F ':' '{print $1}' | awk '{print $NF}')
98 camera=$(printf '%s\n' "$camera_selected" | awk -F "$port_type" '{print $1}')
99 port_address=$(printf '%s\n' "$camera_selected" | awk -F ':' '{print $2}')
100 port="$port_type:$port_address"
101
102 script_dir=$(dirname "$0")
103
104 if ! zenity --question --title="Geeqie camera import" --text="Camera: $camera\n\nDownloading to folder:\n<b>$PWD</b>" --ok-label="OK" --cancel-label="Cancel" --window-icon=/usr/local/share/pixmaps/geeqie.png --width=350 2> /dev/null
105 then
106         exit 0
107 fi
108
109 src_files_sorted=$(mktemp "${TMPDIR:-/tmp}/geeqie.XXXXXXXXXX")
110 dest_files_sorted=$(mktemp "${TMPDIR:-/tmp}/geeqie.XXXXXXXXXX")
111
112 (
113         gphoto2 --port "$port" --list-files 2> /tmp/geeqie-camera-import.log | awk '/#/ {print $2}' | sort > "$src_files_sorted"
114 ) | zenity --progress --auto-close --auto-kill --title="Geeqie camera import" --text="Searching for files to download..." --pulsate --window-icon=/usr/local/share/pixmaps/geeqie.png --width=250
115
116 error=$(grep -i error /tmp/geeqie-camera-import.log)
117
118 if [ -n "$error" ]
119 then
120         zenity --text-info --title="Geeqie camera import" - --window-icon=error --width=250 < /tmp/geeqie-camera-import.log 2> /dev/null
121         exit 1
122 fi
123
124 find . -maxdepth 1 -type f -exec basename {} \; | sort > "$dest_files_sorted"
125 existing_file_count=$(comm -12 "$src_files_sorted" "$dest_files_sorted" | wc -l)
126
127 repeated=$(uniq --repeated "$src_files_sorted")
128 if [ -n "$repeated" ]
129 then
130         repeated="Warning: The following source filenames are not unique.\nSome files may not be downloaded.\nSee the Help file.\n\n$repeated"
131
132         if zenity --question --text="$repeated" --title="Geeqie camera import" --cancel-label="OK" --ok-label="Cancel" --width=400 --window-icon=/usr/local/share/pixmaps/geeqie.png 2> /dev/null
133         then
134                 exit 1
135         fi
136 fi
137
138 total=$(wc -l < "$src_files_sorted")
139 files_to_load=$((total - existing_file_count))
140
141 rm "$src_files_sorted"
142 rm "$dest_files_sorted"
143
144 if [ "$files_to_load" -eq 0 ]
145 then
146         zenity --info --title="Geeqie camera download" --text="No photos to download" --width=250 --window-icon=usr/local/share/pixmaps/geeqie.png 2> /dev/null
147         exit 0
148 fi
149
150 if [ -f /tmp/geeqie-camera-import-files ]
151 then
152         rm /tmp/geeqie-camera-import-files
153 fi
154 touch /tmp/geeqie-camera-import-files
155
156 zen_pipe=$(mktemp -u "${TMPDIR:-/tmp}/geeqie.XXXXXXXXXX")
157 mkfifo "$zen_pipe"
158
159 gphoto2 --port "$port" --hook-script "$script_dir/"geeqie-camera-import-hook-script --get-all-files --skip-existing 2> /tmp/geeqie-camera-import.log &
160
161 gphoto2_pid=$!
162
163 (tail -f "$zen_pipe" 2> /dev/null) | zenity --progress --title="Geeqie camera import" --width=370 --text="Downloading: total: $files_to_load existing: $existing_file_count\n" --auto-close --auto-kill --percentage=0 window-icon=/usr/local/share/pixmaps/geeqie.png 2> /dev/null &
164 zen_pid=$!
165
166 n=0
167 while [ -f /tmp/geeqie-camera-import-files ] && [ "$n" -lt 100 ]
168 do
169         i=$(wc -l < "/tmp/geeqie-camera-import-files")
170         n=$(($((i * 100)) / files_to_load))
171         printf '%s\n' "$n" > "$zen_pipe"
172
173         latest_file=$(tail -n 1 /tmp/geeqie-camera-import-files)
174         if [ -z "$latest_file" ]
175         then
176                 latest_file="Skipping existing files, if any..."
177         fi
178         printf '#Downloading: total: %s existing: %s\n%s' "$files_to_load existing" "$existing_file_count" "$latest_file" > "$zen_pipe"
179
180         sleep 1
181 done