Camera import - warn of duplicate filenames
[geeqie.git] / plugins / camera-import / geeqie-camera-import
1 #!/bin/bash
2
3 # Import all images from camera
4 # Requires gphoto2
5
6 function finish 
7 {
8         if [ -f /tmp/geeqie-camera-import-files ]
9         then
10                 rm /tmp/geeqie-camera-import-files
11         fi
12
13         if [ -p $zen_pipe ]
14         then
15                 rm $zen_pipe
16         fi
17
18         if [ "$gphoto2_pid" != "" ]
19         then
20                 ps -p $gphoto2_pid > /dev/null
21                 if [ $? -eq 0 ]
22                 then
23                         kill $gphoto2_pid
24                 fi
25         fi
26
27         if [ "$zen_pid" != "" ]
28         then
29                 ps -p $zen_pid > /dev/null
30                 if [ $? -eq 0 ]
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 list=$(gphoto2 --auto-detect | tail +3)
56 readarray -t split_list <<<"$list"
57
58 camera_list=""
59 n=1
60 count=$(gphoto2 --auto-detect | tail +3 | wc -l)
61 if [[ $count -gt 1 ]]
62 then
63         for camera in "${split_list[@]}"
64         do
65                 if [[ $n -eq $count ]]
66                 then
67                         camera_list="$camera_list"$'TRUE\n'"$camera"$'\n'"$n"
68                 else
69                         camera_list="$camera_list"$'FALSE\n'"$camera"$'\n'"$n"$'\n'
70                 fi
71                 n=$((n+1))
72         done
73
74         camera_selected=$(echo "$camera_list" | zenity  --width=500 --height=250 --title="Geeqie camera import" --list  --text "Select camera" --radiolist  --column "Select" --column "Camera" --column "n" --hide-column=3 --print-column=2 2>/dev/null) 
75
76         if [[ $? == 1 ]]
77         then
78                 exit 0
79         fi
80 else
81         camera_selected=$(gphoto2 --auto-detect | tail +3)
82 fi
83
84 port_type=$(echo $camera_selected |awk -F ':' '{print $1}' | awk '{print $NF}')
85 camera=$(echo $camera_selected | awk -F $port_type '{print $1}')
86 port_address=$(echo $camera_selected | awk -F ':' '{print $2}')
87 port="$port_type:$port_address"
88
89 script_dir=$(dirname "$0")
90
91 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
92
93 if [[ $? == 1 ]]
94 then
95         exit 0
96 fi
97
98 src_files_sorted=$(mktemp --tmpdir geeqie_camera_import_camera_files_sorted_XXXXXX)
99 dest_files_sorted=$(mktemp --tmpdir geeqie_camera_import_computer_files_sorted_XXXXXX)
100
101 (
102 gphoto2 --port "$port" --list-files  2>/tmp/geeqie-camera-import.log | awk '/#/ {print $2}' | sort > $src_files_sorted
103 ) | 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 
104
105 error=$(grep -i error /tmp/geeqie-camera-import.log)
106
107 if [ ! -z "$error" ]
108 then
109         cat /tmp/geeqie-camera-import.log | zenity --text-info --title="Geeqie camera import" - --window-icon=error --width=250 2>/dev/null
110         exit 1
111 fi
112
113 ls -1 | sort > $dest_files_sorted
114 existing_file_count=$(comm -12 $src_files_sorted $dest_files_sorted | wc -l)
115
116 repeated=$(uniq --repeated $src_files_sorted)
117 if [ ! -z "$repeated" ]
118 then
119         repeated="Warning: The following source filenames are not unique.\nSome files may not be downloaded.\nSee the Help file.\n\n$repeated"
120         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         if [[ $? == 0 ]]
122         then
123                 exit 1
124         fi
125 fi
126
127 total=$(cat $src_files_sorted | wc -l)
128 files_to_load=$(( $total - $existing_file_count ))
129
130 rm $src_files_sorted
131 rm $dest_files_sorted
132
133 if [ "$files_to_load" -eq 0 ]
134 then
135         zenity --info --title="Geeqie camera download" --text="No photos to download" --width=250 --window-icon=usr/local/share/pixmaps/geeqie.png 2>/dev/null
136         exit 0
137 fi
138
139 if [ -f /tmp/geeqie-camera-import-files ]
140 then
141         rm /tmp/geeqie-camera-import-files
142 fi
143 touch /tmp/geeqie-camera-import-files
144
145 zen_pipe=$(mktemp --dry-run --tmpdir geeqie_camera_import_pipe_XXXXXX)
146 mkfifo $zen_pipe
147
148 gphoto2 --port "$port" --hook-script "$script_dir/"geeqie-camera-import-hook-script --get-all-files --skip-existing 2>/tmp/geeqie-camera-import.log &
149
150 gphoto2_pid=$!
151
152 (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 &
153 zen_pid=$!
154
155 n=0
156 while [ -f /tmp/geeqie-camera-import-files ] &&  [ "$n" -lt 100 ]
157 do
158         i=$(cat "/tmp/geeqie-camera-import-files" | wc -l)
159         n=$(( $((i * 100)) / $files_to_load))
160         echo "$n" >$zen_pipe
161
162         latest_file=$(tail -n 1 /tmp/geeqie-camera-import-files)
163         if [ -z "$latest_file" ]
164         then
165                 latest_file="Skipping existing files, if any..."
166         fi
167         echo "#Downloading: total: $files_to_load existing: $existing_file_count\n$latest_file" >$zen_pipe
168
169         sleep 1
170 done