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