Plugin for camera download
[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 total=$(cat $src_files_sorted | wc -l)
117 files_to_load=$(( $total - $existing_file_count ))
118
119 rm $src_files_sorted
120 rm $dest_files_sorted
121
122 if [ "$files_to_load" -eq 0 ]
123 then
124         zenity --info --title="Geeqie camera download" --text="No photos to download" --width=250 --window-icon=usr/local/share/pixmaps/geeqie.png 2>/dev/null
125         exit 0
126 fi
127
128 if [ -f /tmp/geeqie-camera-import-files ]
129 then
130         rm /tmp/geeqie-camera-import-files
131 fi
132 touch /tmp/geeqie-camera-import-files
133
134 zen_pipe=$(mktemp --dry-run --tmpdir geeqie_camera_import_pipe_XXXXXX)
135 mkfifo $zen_pipe
136
137 gphoto2 --port "$port" --hook-script "$script_dir/"geeqie-camera-import-hook-script --get-all-files --skip-existing 2>/tmp/geeqie-camera-import.log &
138
139 gphoto2_pid=$!
140
141 (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 &
142 zen_pid=$!
143
144 n=0
145 while [ -f /tmp/geeqie-camera-import-files ] &&  [ "$n" -lt 100 ]
146 do
147         i=$(cat "/tmp/geeqie-camera-import-files" | wc -l)
148         n=$(( $((i * 100)) / $files_to_load))
149         echo "$n" >$zen_pipe
150
151         latest_file=$(tail -n 1 /tmp/geeqie-camera-import-files)
152         if [ -z "$latest_file" ]
153         then
154                 latest_file="Skipping existing files, if any..."
155         fi
156         echo "#Downloading: total: $files_to_load existing: $existing_file_count\n$latest_file" >$zen_pipe
157
158         sleep 1
159 done