Fix #1236: Window Layouts with vertical split are shifted to the left if they are...
[geeqie.git] / plugins / refresh-thumbnail / geeqie-refresh-thumbnail
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 ! [ -x "$(command -v gphoto2)" ]
39 then
40         zenity --title="Geeqie camera import" --info --width=200 --text="gphoto2 is not installed" 2> /dev/null
41         exit 0
42 fi
43
44 if [ -f /tmp/geeqie-camera-import.log ]
45 then
46         rm /tmp/geeqie-camera-import.log
47 fi
48
49 if [ "$(gphoto2 --auto-detect | wc -l)" -le 2 ]
50 then
51         zenity --error --title="Geeqie camera import" --text="No camera detected" --window-icon=/usr/local/share/pixmaps/geeqie.png --width=250 2> /dev/null
52         exit 0
53 fi
54
55 IFS='
56 '
57
58 list=$(gphoto2 --auto-detect | tail -n +3)
59 camera_list=""
60 n=1
61 count=$(gphoto2 --auto-detect | tail -n +3 | wc -l)
62 if [ "$count" -gt 1 ]
63 then
64         for camera in $list
65         do
66                 if [ "$n" -eq "$count" ]
67                 then
68                         camera_list="${camera_list:+${camera_list}}TRUE\n$camera\n$n"
69                 else
70                         camera_list="${camera_list:+${camera_list}}FALSE\n$camera\n$n\n"
71                 fi
72                 n=$((n + 1))
73         done
74
75         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)
76
77         if [ $? = 1 ]
78         then
79                 exit 0
80         fi
81 else
82         camera_selected=$(gphoto2 --auto-detect | tail +3)
83 fi
84
85 port_type=$(printf '%s\n' "$camera_selected" | awk -F ':' '{print $1}' | awk '{print $NF}')
86 camera=$(printf '%s\n' "$camera_selected" | awk -F "$port_type" '{print $1}')
87 port_address=$(printf '%s\n' "$camera_selected" | awk -F ':' '{print $2}')
88 port="$port_type:$port_address"
89
90 script_dir=$(dirname "$0")
91
92 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
93 then
94         exit 0
95 fi
96
97 src_files_sorted=$(mktemp "${TMPDIR:-/tmp}/geeqie.XXXXXXXXXX")
98 dest_files_sorted=$(mktemp "${TMPDIR:-/tmp}/geeqie.XXXXXXXXXX")
99
100 (
101         gphoto2 --port "$port" --list-files 2> /tmp/geeqie-camera-import.log | awk '/#/ {print $2}' | sort > "$src_files_sorted"
102 ) | 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
103
104 error=$(grep -i error /tmp/geeqie-camera-import.log)
105
106 if [ -n "$error" ]
107 then
108         zenity --text-info --title="Geeqie camera import" - --window-icon=error --width=250 < /tmp/geeqie-camera-import.log 2> /dev/null
109         exit 1
110 fi
111
112 find . -maxdepth 1 -type f -exec basename {} \; | sort > "$dest_files_sorted"
113 existing_file_count=$(comm -12 "$src_files_sorted" "$dest_files_sorted" | wc -l)
114
115 repeated=$(uniq --repeated "$src_files_sorted")
116 if [ -n "$repeated" ]
117 then
118         repeated="Warning: The following source filenames are not unique.\nSome files may not be downloaded.\nSee the Help file.\n\n$repeated"
119
120         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
121         then
122                 exit 1
123         fi
124 fi
125
126 total=$(wc -l < "$src_files_sorted")
127 files_to_load=$((total - existing_file_count))
128
129 rm "$src_files_sorted"
130 rm "$dest_files_sorted"
131
132 if [ "$files_to_load" -eq 0 ]
133 then
134         zenity --info --title="Geeqie camera download" --text="No photos to download" --width=250 --window-icon=usr/local/share/pixmaps/geeqie.png 2> /dev/null
135         exit 0
136 fi
137
138 if [ -f /tmp/geeqie-camera-import-files ]
139 then
140         rm /tmp/geeqie-camera-import-files
141 fi
142 touch /tmp/geeqie-camera-import-files
143
144 zen_pipe=$(mktemp -u "${TMPDIR:-/tmp}/geeqie.XXXXXXXXXX")
145 mkfifo "$zen_pipe"
146
147 gphoto2 --port "$port" --hook-script "$script_dir/"geeqie-camera-import-hook-script --get-all-files --skip-existing 2> /tmp/geeqie-camera-import.log &
148
149 gphoto2_pid=$!
150
151 (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 &
152 zen_pid=$!
153
154 n=0
155 while [ -f /tmp/geeqie-camera-import-files ] && [ "$n" -lt 100 ]
156 do
157         i=$(wc -l < "/tmp/geeqie-camera-import-files")
158         n=$(($((i * 100)) / files_to_load))
159         printf '%s\n' "$n" > "$zen_pipe"
160
161         latest_file=$(tail -n 1 /tmp/geeqie-camera-import-files)
162         if [ -z "$latest_file" ]
163         then
164                 latest_file="Skipping existing files, if any..."
165         fi
166         printf '#Downloading: total: %s existing: %s\n%s' "$files_to_load existing" "$existing_file_count" "$latest_file" > "$zen_pipe"
167
168         sleep 1
169 done