Geocode image by drag-drop
[geeqie.git] / src / search.c
index b273759..f48fef7 100644 (file)
@@ -32,6 +32,7 @@
 #include "image-load.h"
 #include "img-view.h"
 #include "layout.h"
+#include "math.h"
 #include "menu.h"
 #include "metadata.h"
 #include "misc.h"
@@ -60,7 +61,6 @@
 #define SEARCH_BUFFER_MATCH_MISS 1
 #define SEARCH_BUFFER_FLUSH_SIZE 99
 
-
 typedef enum {
        SEARCH_MATCH_NONE,
        SEARCH_MATCH_EQUAL,
@@ -160,6 +160,7 @@ struct _SearchData
        GList *search_keyword_list;
        gchar *search_comment;
        gboolean   search_comment_match_case;
+       gboolean   search_date_exif;
 
        MatchType search_type;
 
@@ -169,6 +170,7 @@ struct _SearchData
        MatchType match_dimensions;
        MatchType match_keywords;
        MatchType match_comment;
+       MatchType match_gps;
 
        gboolean match_name_enable;
        gboolean match_size_enable;
@@ -198,6 +200,18 @@ struct _SearchData
        ThumbLoader *thumb_loader;
        gboolean thumb_enable;
        FileData *thumb_fd;
+
+       /* Used for lat/long coordinate search
+       */
+       gint search_gps;
+       gdouble search_lat, search_lon;
+       GtkWidget *entry_gps_coord;
+       GtkWidget *check_gps;
+       GtkWidget *spin_gps;
+       GtkWidget *units_gps;
+       GtkWidget *menu_gps;
+       gboolean match_gps_enable;
+
 };
 
 typedef struct _MatchFileData MatchFileData;
@@ -252,6 +266,12 @@ static const MatchList text_search_menu_comment[] = {
        { N_("miss"),           SEARCH_MATCH_NONE }
 };
 
+static const MatchList text_search_menu_gps[] = {
+       { N_("not geocoded"),   SEARCH_MATCH_NONE },
+       { N_("less than"),      SEARCH_MATCH_UNDER },
+       { N_("greater than"),   SEARCH_MATCH_OVER }
+};
+
 static GList *search_window_list = NULL;
 
 
@@ -1305,6 +1325,9 @@ static gboolean search_result_keypress_cb(GtkWidget *widget, GdkEventKey *event,
                                               search_result_menu_pos_cb, sd, 0, GDK_CURRENT_TIME);
                                }
                                break;
+                       case GDK_KEY_F1:
+                               help_window_show("GuideReferenceKeyboardShortcuts.html#SearchKeyboardShortcuts");
+                               break;
                        default:
                                stop_signal = FALSE;
                                break;
@@ -1352,6 +1375,12 @@ static GtkTargetEntry result_drag_types[] = {
 };
 static gint n_result_drag_types = 2;
 
+static GtkTargetEntry result_drop_types[] = {
+       { "text/uri-list", 0, TARGET_URI_LIST },
+       { "text/plain", 0, TARGET_TEXT_PLAIN }
+};
+static gint n_result_drop_types = 2;
+
 static void search_dnd_data_set(GtkWidget *widget, GdkDragContext *context,
                                GtkSelectionData *selection_data, guint info,
                                guint time, gpointer data)
@@ -1398,6 +1427,45 @@ static void search_dnd_begin(GtkWidget *widget, GdkDragContext *context, gpointe
                }
 }
 
+static void search_gps_dnd_received_cb(GtkWidget *pane, GdkDragContext *context,
+                                                                               gint x, gint y,
+                                                                               GtkSelectionData *selection_data, guint info,
+                                                                               guint time, gpointer data)
+{
+       SearchData *sd = data;
+       GList *list;
+       gdouble latitude, longitude;
+       FileData *fd;
+
+       if (info == TARGET_URI_LIST)
+               {
+               list = uri_filelist_from_gtk_selection_data(selection_data);
+
+               /* If more than one file, use only the first file in a list.
+               */
+               if (list != NULL)
+                       {
+                       fd = list->data;
+                       latitude = metadata_read_GPS_coord(fd, "Xmp.exif.GPSLatitude", 1000);
+                       longitude = metadata_read_GPS_coord(fd, "Xmp.exif.GPSLongitude", 1000);
+                       if (latitude != 1000 && longitude != 1000)
+                               {
+                               gtk_entry_set_text(GTK_ENTRY(sd->entry_gps_coord),
+                                                       g_strdup_printf("%lf %lf", latitude, longitude));
+                               }
+                       else
+                               {
+                               gtk_entry_set_text(GTK_ENTRY(sd->entry_gps_coord), "Image is not geocoded");
+                               }
+                       }
+               }
+
+       if (info == TARGET_TEXT_PLAIN)
+               {
+               gtk_entry_set_text(GTK_ENTRY(sd->entry_gps_coord),"");
+               }
+}
+
 static void search_dnd_init(SearchData *sd)
 {
        gtk_drag_source_set(sd->result_view, GDK_BUTTON1_MASK | GDK_BUTTON2_MASK,
@@ -1407,6 +1475,14 @@ static void search_dnd_init(SearchData *sd)
                         G_CALLBACK(search_dnd_data_set), sd);
        g_signal_connect(G_OBJECT(sd->result_view), "drag_begin",
                         G_CALLBACK(search_dnd_begin), sd);
+
+       gtk_drag_dest_set(GTK_WIDGET(sd->entry_gps_coord),
+                                        GTK_DEST_DEFAULT_ALL,
+                                         result_drop_types, n_result_drop_types,
+                                        GDK_ACTION_COPY);
+
+       g_signal_connect(G_OBJECT(sd->entry_gps_coord), "drag_data_received",
+                                       G_CALLBACK(search_gps_dnd_received_cb), sd);
 }
 
 /*
@@ -1650,6 +1726,7 @@ static gboolean search_file_next(SearchData *sd)
        gint width = 0;
        gint height = 0;
        gint sim = 0;
+       time_t file_date;
 
        if (!sd->search_file_list) return FALSE;
 
@@ -1726,11 +1803,21 @@ static gboolean search_file_next(SearchData *sd)
                tested = TRUE;
                match = FALSE;
 
+               if (sd->search_date_exif)
+                       {
+                       read_exif_time_data(fd);
+                       file_date = fd->exifdate;
+                       }
+               else
+                       {
+                       file_date = fd->date;
+                       }
+
                if (sd->match_date == SEARCH_MATCH_EQUAL)
                        {
                        struct tm *lt;
 
-                       lt = localtime(&fd->date);
+                       lt = localtime(&file_date);
                        match = (lt &&
                                 lt->tm_year == sd->search_date_y - 1900 &&
                                 lt->tm_mon == sd->search_date_m - 1 &&
@@ -1738,11 +1825,11 @@ static gboolean search_file_next(SearchData *sd)
                        }
                else if (sd->match_date == SEARCH_MATCH_UNDER)
                        {
-                       match = (fd->date < convert_dmy_to_time(sd->search_date_d, sd->search_date_m, sd->search_date_y));
+                       match = (file_date < convert_dmy_to_time(sd->search_date_d, sd->search_date_m, sd->search_date_y));
                        }
                else if (sd->match_date == SEARCH_MATCH_OVER)
                        {
-                       match = (fd->date > convert_dmy_to_time(sd->search_date_d, sd->search_date_m, sd->search_date_y) + 60 * 60 * 24 - 1);
+                       match = (file_date > convert_dmy_to_time(sd->search_date_d, sd->search_date_m, sd->search_date_y) + 60 * 60 * 24 - 1);
                        }
                else if (sd->match_date == SEARCH_MATCH_BETWEEN)
                        {
@@ -1757,7 +1844,7 @@ static gboolean search_file_next(SearchData *sd)
                                {
                                a += 60 * 60 * 24 - 1;
                                }
-                       match = MATCH_IS_BETWEEN(fd->date, a, b);
+                       match = MATCH_IS_BETWEEN(file_date, a, b);
                        }
                }
 
@@ -1875,8 +1962,63 @@ static gboolean search_file_next(SearchData *sd)
                        }
                }
 
-       if ((match || extra_only) &&
-           (sd->match_dimensions_enable || sd->match_similarity_enable))
+       if (match && sd->match_gps_enable)
+               {
+               /* Calculate the distance the image is from the specified origin.
+               * This is a standard algorithm. A simplified one may be faster.
+               */
+               #define RADIANS  0.0174532925
+               #define KM_EARTH_RADIUS 6371
+               #define MILES_EARTH_RADIUS 3959
+               #define NAUTICAL_MILES_EARTH_RADIUS 3440
+
+               gdouble latitude, longitude, range, conversion;
+
+               if (g_strcmp0(gtk_combo_box_text_get_active_text(
+                                               GTK_COMBO_BOX_TEXT(sd->units_gps)), _("km")) == 0)
+                       {
+                       conversion = KM_EARTH_RADIUS;
+                       }
+               else if (g_strcmp0(gtk_combo_box_text_get_active_text(
+                                               GTK_COMBO_BOX_TEXT(sd->units_gps)), _("miles")) == 0)
+                       {
+                       conversion = MILES_EARTH_RADIUS;
+                       }
+               else
+                       {
+                       conversion = NAUTICAL_MILES_EARTH_RADIUS;
+                       }
+
+               tested = TRUE;
+               match = FALSE;
+
+               latitude = metadata_read_GPS_coord(fd, "Xmp.exif.GPSLatitude", 1000);
+               longitude = metadata_read_GPS_coord(fd, "Xmp.exif.GPSLongitude", 1000);
+               if (latitude != 1000 && longitude != 1000)
+                       {
+                       range = conversion * acos(sin(latitude * RADIANS) *
+                                               sin(sd->search_lat * RADIANS) + cos(latitude * RADIANS) *
+                                               cos(sd->search_lat * RADIANS) * cos((sd->search_lon -
+                                               longitude) * RADIANS));
+                       if (sd->match_gps == SEARCH_MATCH_UNDER)
+                               {
+                               if (sd->search_gps >= range)
+                                       match = TRUE;
+                               }
+                       else if (sd->match_gps == SEARCH_MATCH_OVER)
+                               {
+                               if (sd->search_gps < range)
+                                       match = TRUE;
+                               }
+                       }
+               else if (sd->match_gps == SEARCH_MATCH_NONE)
+                       {
+                       match = TRUE;
+                       }
+               }
+
+       if ((match || extra_only) && (sd->match_dimensions_enable ||
+                                                               sd ->match_similarity_enable))
                {
                tested = TRUE;
 
@@ -2107,6 +2249,7 @@ static void search_start_cb(GtkWidget *widget, gpointer data)
        SearchData *sd = data;
        GtkTreeViewColumn *column;
        gchar *path;
+       gchar *entry_text;
 
        if (sd->search_folder_list)
                {
@@ -2137,6 +2280,32 @@ static void search_start_cb(GtkWidget *widget, gpointer data)
                tab_completion_append_to_history(sd->entry_similarity, sd->search_similarity_path);
                }
 
+       /* Check the coordinate entry.
+       * If the result is not sensible, it should get blocked.
+       */
+       if (sd->match_gps_enable)
+               {
+               if (sd->match_gps != SEARCH_MATCH_NONE)
+                       {
+                       entry_text = decode_geo_parameters(gtk_entry_get_text(
+                                                                               GTK_ENTRY(sd->entry_gps_coord)));
+
+                       sd->search_lat = 1000;
+                       sd->search_lon = 1000;
+                       sscanf(entry_text," %lf  %lf ", &sd->search_lat, &sd->search_lon );
+                       if (!(entry_text != NULL && !g_strstr_len(entry_text, -1, "Error") &&
+                                               sd->search_lat >= -90 && sd->search_lat <= 90 &&
+                                               sd->search_lon >= -180 && sd->search_lon <= 180))
+                               {
+                               file_util_warning_dialog(_(
+                                               "Entry does not contain a valid lat/long value"),
+                                                       entry_text, GTK_STOCK_DIALOG_WARNING, sd->window);
+                               return;
+                               }
+                       g_free(entry_text);
+                       }
+               }
+
        string_list_free(sd->search_keyword_list);
        sd->search_keyword_list = keyword_list_pull(sd->entry_keywords);
 
@@ -2410,6 +2579,16 @@ static void menu_choice_spin_cb(GtkAdjustment *adjustment, gpointer data)
        *value = (gint)gtk_adjustment_get_value(adjustment);
 }
 
+static void menu_choice_gps_cb(GtkWidget *combo, gpointer data)
+{
+       SearchData *sd = data;
+
+       if (!menu_choice_get_match_type(combo, &sd->match_gps)) return;
+
+       menu_choice_set_visible(gtk_widget_get_parent(sd->spin_gps),
+                                       (sd->match_gps != SEARCH_MATCH_NONE));
+}
+
 static GtkWidget *menu_spin(GtkWidget *box, gdouble min, gdouble max, gint value,
                            GCallback func, gpointer data)
 {
@@ -2592,6 +2771,9 @@ void search_new(FileData *dir_fd, FileData *example_file)
 
        sd->search_similarity = 95;
 
+       sd->search_gps = 1;
+       sd->match_gps = SEARCH_MATCH_NONE;
+
        if (example_file)
                {
                sd->search_similarity_path = g_strdup(example_file->path);
@@ -2685,6 +2867,8 @@ void search_new(FileData *dir_fd, FileData *example_file)
        date_selection_time_set(sd->date_sel_end, time(NULL));
        gtk_box_pack_start(GTK_BOX(hbox2), sd->date_sel_end, FALSE, FALSE, 0);
        gtk_widget_show(sd->date_sel_end);
+       pref_checkbox_new_int(hbox, _("Exif date"),
+                               sd->search_date_exif, &sd->search_date_exif);
 
        /* Search for image dimensions */
        hbox = menu_choice(sd->box_search, &sd->check_dimensions, &sd->menu_dimensions,
@@ -2750,6 +2934,38 @@ void search_new(FileData *dir_fd, FileData *example_file)
        pref_checkbox_new_int(hbox, _("Match case"),
                              sd->search_comment_match_case, &sd->search_comment_match_case);
 
+       /* Search for images within a specified range of a lat/long coordinate
+       */
+       hbox = menu_choice(sd->box_search, &sd->check_gps, &sd->menu_gps,
+                          _("Image is"), &sd->match_gps_enable,
+                          text_search_menu_gps, sizeof(text_search_menu_gps) / sizeof(MatchList),
+                          G_CALLBACK(menu_choice_gps_cb), sd);
+
+       hbox2 = gtk_hbox_new(FALSE, PREF_PAD_SPACE);
+       gtk_box_pack_start(GTK_BOX(hbox), hbox2, FALSE, FALSE, 0);
+       sd->spin_gps = menu_spin(hbox2, 1, 9999, sd->search_gps,
+                                                                  G_CALLBACK(menu_choice_spin_cb), &sd->search_gps);
+
+       sd->units_gps = gtk_combo_box_text_new();
+       gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(sd->units_gps), _("km"));
+       gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(sd->units_gps), _("miles"));
+       gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(sd->units_gps), _("n.m."));
+       gtk_box_pack_start(GTK_BOX(hbox2), sd->units_gps, FALSE, FALSE, 0);
+       gtk_combo_box_set_active(GTK_COMBO_BOX(sd->units_gps), 0);
+       gtk_widget_set_tooltip_text(sd->units_gps, "kilometres, miles or nautical miles");
+       gtk_widget_show(sd->units_gps);
+
+       pref_label_new(hbox2, _("from"));
+
+       sd->entry_gps_coord = gtk_entry_new();
+       gtk_editable_set_editable(GTK_EDITABLE(sd->entry_gps_coord), TRUE);
+       gtk_widget_set_has_tooltip(sd->entry_gps_coord, TRUE);
+       gtk_widget_set_tooltip_text(sd->entry_gps_coord, _("Enter a coordinate in the form:\n89.123 179.456\nor drag-and-drop a geo-coded image\nor left-click on the map and paste\nor cut-and-paste or drag-and-drop\nan internet search URL\nSee the Help file"));
+       gtk_box_pack_start(GTK_BOX(hbox2), sd->entry_gps_coord, TRUE, TRUE, 0);
+       gtk_widget_set_sensitive(sd->entry_gps_coord, TRUE);
+
+       gtk_widget_show(sd->entry_gps_coord);
+
        /* Done the types of searches */
 
        scrolled = gtk_scrolled_window_new(NULL, NULL);
@@ -2831,6 +3047,10 @@ void search_new(FileData *dir_fd, FileData *example_file)
 
        sd->label_progress = gtk_progress_bar_new();
        gtk_widget_set_size_request(sd->label_progress, 50, -1);
+#if GTK_CHECK_VERSION(3,0,0)
+       gtk_progress_bar_set_text(GTK_PROGRESS_BAR(sd->label_progress), "");
+       gtk_progress_bar_set_show_text(GTK_PROGRESS_BAR(sd->label_progress), TRUE);
+#endif
        gtk_box_pack_start(GTK_BOX(hbox), sd->label_progress, TRUE, TRUE, 0);
        gtk_widget_show(sd->label_progress);