]> git.street.me.uk Git - andy/viking.git/blame - src/viktrwlayer_geotag.c
Remove compiler warning on 32bit systems.
[andy/viking.git] / src / viktrwlayer_geotag.c
CommitLineData
b3eb3b98
RN
1/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2/*
3 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 *
5 * Copyright (C) 2011, Rob Norris <rw_norris@hotmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22/*
23 * Similar to the track and trackpoint properties dialogs,
24 * this is made a separate file for ease of grouping related stuff together
25 */
26#ifdef HAVE_CONFIG_H
27#include "config.h"
28#endif
29#include <math.h>
30#include <time.h>
31#include <string.h>
32#include <stdlib.h>
33#include <gtk/gtk.h>
34#include <glib/gi18n.h>
35#include "viking.h"
36#include "vikfilelist.h"
37#include "geotag_exif.h"
38#include "thumbnails.h"
404328d1 39#include "background.h"
b3eb3b98 40
404328d1 41// Function taken from GPSCorrelate 1.6.1
b3eb3b98
RN
42// ConvertToUnixTime Copyright 2005 Daniel Foote. GPL2+
43
44#define EXIF_DATE_FORMAT "%d:%d:%d %d:%d:%d"
45
46time_t ConvertToUnixTime(char* StringTime, char* Format, int TZOffsetHours, int TZOffsetMinutes)
47{
48 /* Read the time using the specified format.
49 * The format and string being read from must
50 * have the most significant time on the left,
51 * and the least significant on the right:
52 * ie, Year on the left, seconds on the right. */
53
54 /* Sanity check... */
55 if (StringTime == NULL || Format == NULL)
56 {
57 return 0;
58 }
59
60 /* Define and set up our structure. */
61 struct tm Time;
62 Time.tm_wday = 0;
63 Time.tm_yday = 0;
64 Time.tm_isdst = -1;
65
66 /* Read out the time from the string using our format. */
67 sscanf(StringTime, Format, &Time.tm_year, &Time.tm_mon,
68 &Time.tm_mday, &Time.tm_hour,
69 &Time.tm_min, &Time.tm_sec);
70
71 /* Adjust the years for the mktime function to work. */
72 Time.tm_year -= 1900;
73 Time.tm_mon -= 1;
74
75 /* Add our timezone offset to the time.
76 * We don't check to see if it overflowed anything;
77 * mktime does this and fixes it for us. */
78 /* Note also that we SUBTRACT these times. We want the
79 * result to be in UTC. */
80
81 Time.tm_hour -= TZOffsetHours;
82 Time.tm_min -= TZOffsetMinutes;
83
84 /* Calculate and return the unix time. */
85 return mktime(&Time);
86}
87
88// GPSCorrelate END
89
90typedef struct {
91 GtkWidget *dialog;
92 VikFileList *files;
93 VikTrwLayer *vtl; // to pass on
94 VikTrack *track; // Use specified track or all tracks if NULL
95 GtkCheckButton *create_waypoints_b;
cbac0d22
RN
96 GtkLabel *overwrite_waypoints_l; // Referenced so the sensitivity can be changed
97 GtkCheckButton *overwrite_waypoints_b;
b3eb3b98
RN
98 GtkCheckButton *write_exif_b;
99 GtkLabel *overwrite_gps_exif_l; // Referenced so the sensitivity can be changed
100 GtkCheckButton *overwrite_gps_exif_b;
101 GtkLabel *no_change_mtime_l; // Referenced so the sensitivity can be changed
102 GtkCheckButton *no_change_mtime_b;
103 GtkCheckButton *interpolate_segments_b;
104 GtkEntry *time_zone_b; // TODO consider a more user friendly tz widget eg libtimezonemap or similar
105 GtkEntry *time_offset_b;
106} GeoTagWidgets;
107
108static GeoTagWidgets *geotag_widgets_new()
109{
110 GeoTagWidgets *widgets = g_malloc0(sizeof(GeoTagWidgets));
111 return widgets;
112}
113
114static void geotag_widgets_free ( GeoTagWidgets *widgets )
115{
116 // Need to free VikFileList??
117 g_free(widgets);
118}
119
120typedef struct {
121 gboolean create_waypoints;
cbac0d22 122 gboolean overwrite_waypoints;
b3eb3b98
RN
123 gboolean write_exif;
124 gboolean overwrite_gps_exif;
125 gboolean no_change_mtime;
126 gboolean interpolate_segments;
127 gint time_offset;
128 gint TimeZoneHours;
129 gint TimeZoneMins;
130} option_values_t;
131
132typedef struct {
133 VikTrwLayer *vtl;
134 gchar *image;
135 VikTrack *track; // Use specified track or all tracks if NULL
136 // User options...
137 option_values_t ov;
404328d1 138 GList *files;
b3eb3b98
RN
139 time_t PhotoTime;
140 // Store answer from interpolation for an image
141 gboolean found_match;
142 VikCoord coord;
143 gdouble altitude;
144 // If anything has changed
145 gboolean redraw;
146} geotag_options_t;
147
21f0a3ca
RN
148#define VIK_SETTINGS_GEOTAG_CREATE_WAYPOINT "geotag_create_waypoints"
149#define VIK_SETTINGS_GEOTAG_OVERWRITE_WAYPOINTS "geotag_overwrite_waypoints"
150#define VIK_SETTINGS_GEOTAG_WRITE_EXIF "geotag_write_exif"
151#define VIK_SETTINGS_GEOTAG_OVERWRITE_GPS_EXIF "geotag_overwrite_gps"
152#define VIK_SETTINGS_GEOTAG_NO_CHANGE_MTIME "geotag_no_change_mtime"
153#define VIK_SETTINGS_GEOTAG_INTERPOLATE_SEGMENTS "geotag_interpolate_segments"
154#define VIK_SETTINGS_GEOTAG_TIME_OFFSET "geotag_time_offset"
155#define VIK_SETTINGS_GEOTAG_TIME_OFFSET_HOURS "geotag_time_offset_hours"
156#define VIK_SETTINGS_GEOTAG_TIME_OFFSET_MINS "geotag_time_offset_mins"
157
158static void save_default_values ( option_values_t default_values )
159{
160 a_settings_set_boolean ( VIK_SETTINGS_GEOTAG_CREATE_WAYPOINT, default_values.create_waypoints );
161 a_settings_set_boolean ( VIK_SETTINGS_GEOTAG_OVERWRITE_WAYPOINTS, default_values.overwrite_waypoints );
162 a_settings_set_boolean ( VIK_SETTINGS_GEOTAG_WRITE_EXIF, default_values.write_exif );
163 a_settings_set_boolean ( VIK_SETTINGS_GEOTAG_OVERWRITE_GPS_EXIF, default_values.overwrite_gps_exif );
164 a_settings_set_boolean ( VIK_SETTINGS_GEOTAG_NO_CHANGE_MTIME, default_values.no_change_mtime );
165 a_settings_set_boolean ( VIK_SETTINGS_GEOTAG_INTERPOLATE_SEGMENTS, default_values.interpolate_segments );
166 a_settings_set_integer ( VIK_SETTINGS_GEOTAG_TIME_OFFSET, default_values.time_offset );
167 a_settings_set_integer ( VIK_SETTINGS_GEOTAG_TIME_OFFSET_HOURS, default_values.TimeZoneHours );
168 a_settings_set_integer ( VIK_SETTINGS_GEOTAG_TIME_OFFSET_MINS, default_values.TimeZoneMins );
169}
170
171static option_values_t get_default_values ( )
172{
173 option_values_t default_values;
174 if ( ! a_settings_get_boolean ( VIK_SETTINGS_GEOTAG_CREATE_WAYPOINT, &default_values.create_waypoints ) )
175 default_values.create_waypoints = TRUE;
176 if ( ! a_settings_get_boolean ( VIK_SETTINGS_GEOTAG_OVERWRITE_WAYPOINTS, &default_values.overwrite_waypoints ) )
177 default_values.overwrite_waypoints = TRUE;
178 if ( ! a_settings_get_boolean ( VIK_SETTINGS_GEOTAG_WRITE_EXIF, &default_values.write_exif ) )
179 default_values.write_exif = TRUE;
180 if ( ! a_settings_get_boolean ( VIK_SETTINGS_GEOTAG_OVERWRITE_GPS_EXIF, &default_values.overwrite_gps_exif ) )
181 default_values.overwrite_gps_exif = FALSE;
182 if ( ! a_settings_get_boolean ( VIK_SETTINGS_GEOTAG_NO_CHANGE_MTIME, &default_values.no_change_mtime ) )
183 default_values.no_change_mtime = TRUE;
184 if ( ! a_settings_get_boolean ( VIK_SETTINGS_GEOTAG_INTERPOLATE_SEGMENTS, &default_values.interpolate_segments ) )
185 default_values.interpolate_segments = TRUE;
186 if ( ! a_settings_get_integer ( VIK_SETTINGS_GEOTAG_TIME_OFFSET, &default_values.time_offset ) )
187 default_values.time_offset = 0;
188 if ( ! a_settings_get_integer ( VIK_SETTINGS_GEOTAG_TIME_OFFSET_HOURS, &default_values.TimeZoneHours ) )
189 default_values.TimeZoneHours = 0;
190 if ( ! a_settings_get_integer ( VIK_SETTINGS_GEOTAG_TIME_OFFSET_MINS, &default_values.TimeZoneMins ) )
191 default_values.TimeZoneMins = 0;
192 return default_values;
193}
b3eb3b98
RN
194
195/**
196 * Correlate the image against the specified track
197 */
89a068d8 198static void trw_layer_geotag_track ( const gpointer id, VikTrack *track, geotag_options_t *options )
b3eb3b98
RN
199{
200 // If already found match then don't need to check this track
201 if ( options->found_match )
202 return;
203
204 VikTrackpoint *trkpt;
205 VikTrackpoint *trkpt_next;
206
5e610fc3
RN
207 GList *mytrkpt;
208 for ( mytrkpt = track->trackpoints; mytrkpt; mytrkpt = mytrkpt->next ) {
b3eb3b98
RN
209
210 // Do something for this trackpoint...
211
212 trkpt = VIK_TRACKPOINT(mytrkpt->data);
213
214 // is it exactly this point?
215 if ( options->PhotoTime == trkpt->timestamp ) {
216 options->coord = trkpt->coord;
217 options->altitude = trkpt->altitude;
218 options->found_match = TRUE;
219 break;
220 }
221
222 // Now need two trackpoints, hence check next is available
223 if ( !mytrkpt->next ) break;
224 trkpt_next = VIK_TRACKPOINT(mytrkpt->next->data);
225
226 // TODO need to use 'has_timestamp' property
227 if ( trkpt->timestamp == trkpt_next->timestamp ) continue;
228 if ( trkpt->timestamp > trkpt_next->timestamp ) continue;
229
230 // When interpolating between segments, no need for any special segment handling
231 if ( !options->ov.interpolate_segments )
232 // Don't check between segments
233 if ( trkpt_next->newsegment )
234 // Simply move on to consider next point
235 continue;
236
237 // Too far
238 if ( trkpt->timestamp > options->PhotoTime ) break;
239
240 // Is is between this and the next point?
241 if ( (options->PhotoTime > trkpt->timestamp) && (options->PhotoTime < trkpt_next->timestamp) ) {
242 options->found_match = TRUE;
243 // Interpolate
244 /* Calculate the "scale": a decimal giving the relative distance
245 * in time between the two points. Ie, a number between 0 and 1 -
246 * 0 is the first point, 1 is the next point, and 0.5 would be
247 * half way. */
248 gdouble scale = (gdouble)trkpt_next->timestamp - (gdouble)trkpt->timestamp;
249 scale = ((gdouble)options->PhotoTime - (gdouble)trkpt->timestamp) / scale;
250
251 struct LatLon ll_result, ll1, ll2;
252
253 vik_coord_to_latlon ( &(trkpt->coord), &ll1 );
254 vik_coord_to_latlon ( &(trkpt_next->coord), &ll2 );
255
256 ll_result.lat = ll1.lat + ((ll2.lat - ll1.lat) * scale);
257
258 // NB This won't cope with going over the 180 degrees longitude boundary
259 ll_result.lon = ll1.lon + ((ll2.lon - ll1.lon) * scale);
260
261 // set coord
262 vik_coord_load_from_latlon ( &(options->coord), VIK_COORD_LATLON, &ll_result );
263
264 // Interpolate elevation
265 options->altitude = trkpt->altitude + ((trkpt_next->altitude - trkpt->altitude) * scale);
266 break;
267 }
268
269 }
270}
271
272/**
273 * Correlate the image to any track within the TrackWaypoint layer
274 */
275static void trw_layer_geotag_process ( geotag_options_t *options )
276{
404328d1 277 if ( !options->vtl || !IS_VIK_LAYER(options->vtl) )
b3eb3b98
RN
278 return;
279
280 if ( !options->image )
281 return;
282
283 gboolean has_gps_exif = FALSE;
284 gchar* datetime = a_geotag_get_exif_date_from_file ( options->image, &has_gps_exif );
285
286 if ( datetime ) {
287
288 // If image already has gps info - don't attempt to change it.
289 if ( !options->ov.overwrite_gps_exif && has_gps_exif ) {
290 if ( options->ov.create_waypoints ) {
291 // Create waypoint with file information
292 gchar *name = NULL;
293 VikWaypoint *wp = a_geotag_create_waypoint_from_file ( options->image, vik_trw_layer_get_coord_mode (options->vtl), &name );
0fb2c85d
RN
294 if ( !wp ) {
295 // Couldn't create Waypoint
296 g_free ( datetime );
297 return;
298 }
b3eb3b98
RN
299 if ( !name )
300 name = g_strdup ( a_file_basename ( options->image ) );
cbac0d22
RN
301
302 gboolean updated_waypoint = FALSE;
303
304 if ( options->ov.overwrite_waypoints ) {
305 VikWaypoint *current_wp = vik_trw_layer_get_waypoint ( options->vtl, name );
306 if ( current_wp ) {
307 // Existing wp found, so set new position, comment and image
308 current_wp = a_geotag_waypoint_positioned ( options->image, wp->coord, wp->altitude, &name, current_wp );
309 updated_waypoint = TRUE;
310 }
311 }
312
313 if ( !updated_waypoint ) {
314 vik_trw_layer_filein_add_waypoint ( options->vtl, name, wp );
315 }
316
b3eb3b98
RN
317 g_free ( name );
318
319 // Mark for redraw
320 options->redraw = TRUE;
321 }
0847b808 322 g_free ( datetime );
b3eb3b98
RN
323 return;
324 }
325
326 options->PhotoTime = ConvertToUnixTime ( datetime, EXIF_DATE_FORMAT, options->ov.TimeZoneHours, options->ov.TimeZoneMins);
327 g_free ( datetime );
328
329 // Apply any offset
330 options->PhotoTime = options->PhotoTime + options->ov.time_offset;
331
332 options->found_match = FALSE;
333
334 if ( options->track ) {
335 // Single specified track
89a068d8 336 // NB Doesn't care about track id
b3eb3b98
RN
337 trw_layer_geotag_track ( NULL, options->track, options );
338 }
339 else {
340 // Try all tracks
341 GHashTable *tracks = vik_trw_layer_get_tracks ( options->vtl );
342 if ( g_hash_table_size (tracks) > 0 ) {
343 g_hash_table_foreach ( tracks, (GHFunc) trw_layer_geotag_track, options );
344 }
345 }
346
347 // Match found ?
348 if ( options->found_match ) {
349
350 if ( options->ov.create_waypoints ) {
351
cbac0d22
RN
352 gboolean updated_waypoint = FALSE;
353
354 if ( options->ov.overwrite_waypoints ) {
b3eb3b98 355
cbac0d22
RN
356 // Update existing WP
357 // Find a WP with current name
358 gchar *name = NULL;
359 name = g_strdup ( a_file_basename ( options->image ) );
360 VikWaypoint *wp = vik_trw_layer_get_waypoint ( options->vtl, name );
361 if ( wp ) {
362 // Found, so set new position, comment and image
363 wp = a_geotag_waypoint_positioned ( options->image, options->coord, options->altitude, &name, wp );
364 updated_waypoint = TRUE;
365 }
366 g_free ( name );
367 }
368
369 if ( !updated_waypoint ) {
370 // Create waypoint with found position
371 gchar *name = NULL;
372 VikWaypoint *wp = a_geotag_waypoint_positioned ( options->image, options->coord, options->altitude, &name, NULL );
373 if ( !name )
374 name = g_strdup ( a_file_basename ( options->image ) );
375 vik_trw_layer_filein_add_waypoint ( options->vtl, name, wp );
376 g_free ( name );
377 }
378
b3eb3b98
RN
379 // Mark for redraw
380 options->redraw = TRUE;
381 }
382
383 // Write EXIF if specified
384 if ( options->ov.write_exif ) {
385 a_geotag_write_exif_gps ( options->image, options->coord, options->altitude, options->ov.no_change_mtime );
386 }
387 }
388 }
389}
390
404328d1
RN
391/*
392 * Tidy up
393 */
394static void trw_layer_geotag_thread_free ( geotag_options_t *gtd )
395{
396 if ( gtd->files )
397 g_list_free ( gtd->files );
398 g_free ( gtd );
399}
400
401/**
402 * Run geotagging process in a separate thread
403 */
404static int trw_layer_geotag_thread ( geotag_options_t *options, gpointer threaddata )
405{
406 guint total = g_list_length(options->files), done = 0;
407
408 // TODO decide how to report any issues to the user ...
409
410 // Foreach file attempt to geotag it
411 while ( options->files ) {
412 options->image = (gchar *) ( options->files->data );
413 trw_layer_geotag_process ( options );
414 options->files = options->files->next;
415
416 // Update thread progress and detect stop requests
417 int result = a_background_thread_progress ( threaddata, ((gdouble) ++done) / total );
418 if ( result != 0 )
419 return -1; /* Abort thread */
420 }
421
422 if ( options->redraw ) {
423 if ( IS_VIK_LAYER(options->vtl) ) {
424 // Ensure any new images get shown
425 trw_layer_verify_thumbnails ( options->vtl, NULL ); // NB second parameter not used ATM
426 // Force redraw as verify only redraws if there are new thumbnails (they may already exist)
da121f9b 427 vik_layer_emit_update ( VIK_LAYER(options->vtl) ); // NB Update from background
404328d1
RN
428 }
429 }
430
431 return 0;
432}
433
b3eb3b98
RN
434/**
435 * Parse user input from dialog response
436 */
437static void trw_layer_geotag_response_cb ( GtkDialog *dialog, gint resp, GeoTagWidgets *widgets )
438{
439 switch (resp) {
440 case GTK_RESPONSE_DELETE_EVENT: /* received delete event (not from buttons) */
441 case GTK_RESPONSE_REJECT:
442 break;
443 default: {
444 //GTK_RESPONSE_ACCEPT:
445 // Get options
404328d1
RN
446 geotag_options_t *options = g_malloc ( sizeof(geotag_options_t) );
447 options->vtl = widgets->vtl;
448 options->track = widgets->track;
b3eb3b98 449 // Values extracted from the widgets:
404328d1 450 options->ov.create_waypoints = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(widgets->create_waypoints_b) );
cbac0d22 451 options->ov.overwrite_waypoints = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(widgets->overwrite_waypoints_b) );
404328d1
RN
452 options->ov.write_exif = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(widgets->write_exif_b) );
453 options->ov.overwrite_gps_exif = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(widgets->overwrite_gps_exif_b) );
454 options->ov.no_change_mtime = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(widgets->no_change_mtime_b) );
455 options->ov.interpolate_segments = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(widgets->interpolate_segments_b) );
456 options->ov.TimeZoneHours = 0;
457 options->ov.TimeZoneMins = 0;
b3eb3b98
RN
458 const gchar* TZString = gtk_entry_get_text(GTK_ENTRY(widgets->time_zone_b));
459 /* Check the string. If there is a colon, then (hopefully) it's a time in xx:xx format.
460 * If not, it's probably just a +/-xx format. In all other cases,
461 * it will be interpreted as +/-xx, which, if given a string, returns 0. */
462 if (strstr(TZString, ":")) {
463 /* Found colon. Split into two. */
404328d1
RN
464 sscanf(TZString, "%d:%d", &options->ov.TimeZoneHours, &options->ov.TimeZoneMins);
465 if (options->ov.TimeZoneHours < 0)
466 options->ov.TimeZoneMins *= -1;
b3eb3b98
RN
467 } else {
468 /* No colon. Just parse. */
404328d1 469 options->ov.TimeZoneHours = atoi(TZString);
b3eb3b98 470 }
404328d1 471 options->ov.time_offset = atoi ( gtk_entry_get_text ( GTK_ENTRY(widgets->time_offset_b) ) );
b3eb3b98 472
404328d1 473 options->redraw = FALSE;
b3eb3b98
RN
474
475 // Save settings for reuse
21f0a3ca 476 save_default_values ( options->ov );
b3eb3b98 477
404328d1
RN
478 options->files = g_list_copy ( vik_file_list_get_files ( widgets->files ) );
479
480 gint len = g_list_length ( options->files );
481 gchar *tmp = g_strdup_printf ( _("Geotagging %d Images..."), len );
482
483 // Processing lots of files can take time - so run a background effort
484 a_background_thread ( VIK_GTK_WINDOW_FROM_LAYER(options->vtl),
485 tmp,
486 (vik_thr_func) trw_layer_geotag_thread,
487 options,
488 (vik_thr_free_func) trw_layer_geotag_thread_free,
489 NULL,
490 len );
491
492 g_free ( tmp );
b3eb3b98
RN
493
494 break;
495 }
496 }
497 geotag_widgets_free ( widgets );
498 gtk_widget_destroy ( GTK_WIDGET(dialog) );
499}
500
501/**
502 * Handle widget sensitivities
503 */
504static void write_exif_b_cb ( GtkWidget *gw, GeoTagWidgets *gtw )
505{
506 // Overwriting & file modification times are irrelevant if not going to write EXIF!
507 if ( gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(gtw->write_exif_b) ) ) {
508 gtk_widget_set_sensitive ( GTK_WIDGET(gtw->overwrite_gps_exif_b), TRUE );
509 gtk_widget_set_sensitive ( GTK_WIDGET(gtw->overwrite_gps_exif_l), TRUE );
510 gtk_widget_set_sensitive ( GTK_WIDGET(gtw->no_change_mtime_b), TRUE );
511 gtk_widget_set_sensitive ( GTK_WIDGET(gtw->no_change_mtime_l), TRUE );
512 }
513 else {
514 gtk_widget_set_sensitive ( GTK_WIDGET(gtw->overwrite_gps_exif_b), FALSE );
515 gtk_widget_set_sensitive ( GTK_WIDGET(gtw->overwrite_gps_exif_l), FALSE );
516 gtk_widget_set_sensitive ( GTK_WIDGET(gtw->no_change_mtime_b), FALSE );
517 gtk_widget_set_sensitive ( GTK_WIDGET(gtw->no_change_mtime_l), FALSE );
518 }
519}
520
cbac0d22
RN
521static void create_waypoints_b_cb ( GtkWidget *gw, GeoTagWidgets *gtw )
522{
523 // Overwriting waypoints are irrelevant if not going to create them!
524 if ( gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(gtw->create_waypoints_b) ) ) {
525 gtk_widget_set_sensitive ( GTK_WIDGET(gtw->overwrite_waypoints_b), TRUE );
526 gtk_widget_set_sensitive ( GTK_WIDGET(gtw->overwrite_waypoints_l), TRUE );
527 }
528 else {
529 gtk_widget_set_sensitive ( GTK_WIDGET(gtw->overwrite_waypoints_b), FALSE );
530 gtk_widget_set_sensitive ( GTK_WIDGET(gtw->overwrite_waypoints_l), FALSE );
531 }
532}
b3eb3b98
RN
533
534/**
535 * trw_layer_geotag_dialog:
536 * @parent: The Window of the calling process
537 * @vtl: The VikTrwLayer to use for correlating images to tracks
538 * @track: Optional - The particular track to use (if specified) for correlating images
539 * @track_name: Optional - The name of specified track to use
540 */
541void trw_layer_geotag_dialog ( GtkWindow *parent, VikTrwLayer *vtl, VikTrack *track, const gchar *track_name )
542{
543 GeoTagWidgets *widgets = geotag_widgets_new();
544
545 widgets->dialog = gtk_dialog_new_with_buttons ( _("Geotag Images"),
546 parent,
547 GTK_DIALOG_DESTROY_WITH_PARENT,
548 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
549 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
550 NULL );
551 widgets->files = VIK_FILE_LIST(vik_file_list_new ( _("Images") )); // TODO would be nice to be able to set a filefilter
552 widgets->vtl = vtl;
553 widgets->track = track;
554 widgets->create_waypoints_b = GTK_CHECK_BUTTON ( gtk_check_button_new () );
cbac0d22
RN
555 widgets->overwrite_waypoints_l = GTK_LABEL ( gtk_label_new ( _("Overwrite Existing Waypoints:") ) );
556 widgets->overwrite_waypoints_b = GTK_CHECK_BUTTON ( gtk_check_button_new () );
b3eb3b98
RN
557 widgets->write_exif_b = GTK_CHECK_BUTTON ( gtk_check_button_new () );
558 widgets->overwrite_gps_exif_l = GTK_LABEL ( gtk_label_new ( _("Overwrite Existing GPS Information:") ) );
559 widgets->overwrite_gps_exif_b = GTK_CHECK_BUTTON ( gtk_check_button_new () );
560 widgets->no_change_mtime_l = GTK_LABEL ( gtk_label_new ( _("Keep File Modification Timestamp:") ) );
561 widgets->no_change_mtime_b = GTK_CHECK_BUTTON ( gtk_check_button_new () );
562 widgets->interpolate_segments_b = GTK_CHECK_BUTTON ( gtk_check_button_new () );
563 widgets->time_zone_b = GTK_ENTRY ( gtk_entry_new () );
564 widgets->time_offset_b = GTK_ENTRY ( gtk_entry_new () );
565
566 gtk_entry_set_width_chars ( widgets->time_zone_b, 7);
567 gtk_entry_set_width_chars ( widgets->time_offset_b, 7);
568
21f0a3ca
RN
569 // Defaults
570 option_values_t default_values = get_default_values ();
571
b3eb3b98 572 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(widgets->create_waypoints_b), default_values.create_waypoints );
cbac0d22 573 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(widgets->overwrite_waypoints_b), default_values.overwrite_waypoints );
b3eb3b98
RN
574 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(widgets->write_exif_b), default_values.write_exif );
575 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(widgets->overwrite_gps_exif_b), default_values.overwrite_gps_exif );
576 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(widgets->no_change_mtime_b), default_values.no_change_mtime );
577 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(widgets->interpolate_segments_b), default_values.interpolate_segments );
578 gchar tmp_string[7];
579 snprintf (tmp_string, 7, "%+02d:%02d", default_values.TimeZoneHours, abs (default_values.TimeZoneMins) );
580 gtk_entry_set_text ( widgets->time_zone_b, tmp_string );
581 snprintf (tmp_string, 7, "%d", default_values.time_offset );
582 gtk_entry_set_text ( widgets->time_offset_b, tmp_string );
583
584 // Ensure sensitivities setup
585 write_exif_b_cb ( GTK_WIDGET(widgets->write_exif_b), widgets );
b3eb3b98
RN
586 g_signal_connect ( G_OBJECT(widgets->write_exif_b), "toggled", G_CALLBACK(write_exif_b_cb), widgets );
587
cbac0d22
RN
588 create_waypoints_b_cb ( GTK_WIDGET(widgets->create_waypoints_b), widgets );
589 g_signal_connect ( G_OBJECT(widgets->create_waypoints_b), "toggled", G_CALLBACK(create_waypoints_b_cb), widgets );
590
b3eb3b98
RN
591 GtkWidget *cw_hbox = gtk_hbox_new ( FALSE, 0 );
592 gtk_box_pack_start ( GTK_BOX(cw_hbox), gtk_label_new ( _("Create Waypoints:") ), FALSE, FALSE, 5 );
593 gtk_box_pack_start ( GTK_BOX(cw_hbox), GTK_WIDGET(widgets->create_waypoints_b), FALSE, FALSE, 5 );
594
cbac0d22
RN
595 GtkWidget *ow_hbox = gtk_hbox_new ( FALSE, 0 );
596 gtk_box_pack_start ( GTK_BOX(ow_hbox), GTK_WIDGET(widgets->overwrite_waypoints_l), FALSE, FALSE, 5 );
597 gtk_box_pack_start ( GTK_BOX(ow_hbox), GTK_WIDGET(widgets->overwrite_waypoints_b), FALSE, FALSE, 5 );
598
b3eb3b98
RN
599 GtkWidget *we_hbox = gtk_hbox_new ( FALSE, 0 );
600 gtk_box_pack_start ( GTK_BOX(we_hbox), gtk_label_new ( _("Write EXIF:") ), FALSE, FALSE, 5 );
601 gtk_box_pack_start ( GTK_BOX(we_hbox), GTK_WIDGET(widgets->write_exif_b), FALSE, FALSE, 5 );
602
603 GtkWidget *og_hbox = gtk_hbox_new ( FALSE, 0 );
604 gtk_box_pack_start ( GTK_BOX(og_hbox), GTK_WIDGET(widgets->overwrite_gps_exif_l), FALSE, FALSE, 5 );
605 gtk_box_pack_start ( GTK_BOX(og_hbox), GTK_WIDGET(widgets->overwrite_gps_exif_b), FALSE, FALSE, 5 );
606
607 GtkWidget *fm_hbox = gtk_hbox_new ( FALSE, 0 );
608 gtk_box_pack_start ( GTK_BOX(fm_hbox), GTK_WIDGET(widgets->no_change_mtime_l), FALSE, FALSE, 5 );
609 gtk_box_pack_start ( GTK_BOX(fm_hbox), GTK_WIDGET(widgets->no_change_mtime_b), FALSE, FALSE, 5 );
610
611 GtkWidget *is_hbox = gtk_hbox_new ( FALSE, 0 );
612 gtk_box_pack_start ( GTK_BOX(is_hbox), gtk_label_new ( _("Interpolate Between Track Segments:") ), FALSE, FALSE, 5 );
613 gtk_box_pack_start ( GTK_BOX(is_hbox), GTK_WIDGET(widgets->interpolate_segments_b), FALSE, FALSE, 5 );
614
615 GtkWidget *to_hbox = gtk_hbox_new ( FALSE, 0 );
616 gtk_box_pack_start ( GTK_BOX(to_hbox), gtk_label_new ( _("Image Time Offset (Seconds):") ), FALSE, FALSE, 5 );
617 gtk_box_pack_start ( GTK_BOX(to_hbox), GTK_WIDGET(widgets->time_offset_b), FALSE, FALSE, 5 );
618 gtk_widget_set_tooltip_text ( GTK_WIDGET(widgets->time_offset_b), _("The number of seconds to ADD to the photos time to make it match the GPS data. Calculate this with (GPS - Photo). Can be negative or positive. Useful to adjust times when a camera's timestamp was incorrect.") );
619
620 GtkWidget *tz_hbox = gtk_hbox_new ( FALSE, 0 );
621 gtk_box_pack_start ( GTK_BOX(tz_hbox), gtk_label_new ( _("Image Timezone:") ), FALSE, FALSE, 5 );
622 gtk_box_pack_start ( GTK_BOX(tz_hbox), GTK_WIDGET(widgets->time_zone_b), FALSE, FALSE, 5 );
623 gtk_widget_set_tooltip_text ( GTK_WIDGET(widgets->time_zone_b), _("The timezone that was used when the images were created. For example, if a camera is set to AWST or +8:00 hours. Enter +8:00 here so that the correct adjustment to the images' time can be made. GPS data is always in UTC.") );
624
625 gchar *track_string = NULL;
626 if ( widgets->track )
627 track_string = g_strdup_printf ( _("Using track: %s"), track_name );
628 else
629 track_string = g_strdup_printf ( _("Using all tracks in: %s"), VIK_LAYER(widgets->vtl)->name );
630
9b082b39
RN
631 gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(widgets->dialog))), gtk_label_new ( track_string ), FALSE, FALSE, 5 );
632
633 gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(widgets->dialog))), GTK_WIDGET(widgets->files), TRUE, TRUE, 0 );
634
635 gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(widgets->dialog))), cw_hbox, FALSE, FALSE, 0);
636 gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(widgets->dialog))), ow_hbox, FALSE, FALSE, 0);
637 gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(widgets->dialog))), we_hbox, FALSE, FALSE, 0);
638 gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(widgets->dialog))), og_hbox, FALSE, FALSE, 0);
639 gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(widgets->dialog))), fm_hbox, FALSE, FALSE, 0);
640 gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(widgets->dialog))), is_hbox, FALSE, FALSE, 0);
641 gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(widgets->dialog))), to_hbox, FALSE, FALSE, 0);
642 gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(widgets->dialog))), tz_hbox, FALSE, FALSE, 0);
b3eb3b98
RN
643
644 g_signal_connect ( widgets->dialog, "response", G_CALLBACK(trw_layer_geotag_response_cb), widgets );
645
646 gtk_dialog_set_default_response ( GTK_DIALOG(widgets->dialog), GTK_RESPONSE_REJECT );
647
648 gtk_widget_show_all ( widgets->dialog );
649
650 g_free ( track_string );
651}