1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 
3 /*
4  *  GThumb
5  *
6  *  Copyright (C) 2008 Free Software Foundation, Inc.
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 
23 #include <config.h>
24 #include <glib/gi18n.h>
25 #include <gthumb.h>
26 #include <extensions/catalogs/gth-catalog.h>
27 #include "actions.h"
28 #include "gth-search-editor-dialog.h"
29 #include "gth-search-task.h"
30 
31 
32 static void
search_editor_dialog__response_cb(GtkDialog * dialog,int response,gpointer user_data)33 search_editor_dialog__response_cb (GtkDialog *dialog,
34 			           int        response,
35 			           gpointer   user_data)
36 {
37 	GthBrowser *browser = user_data;
38 	GthSearch  *search;
39 	GError     *error = NULL;
40 	GFile      *search_catalog;
41 	GthTask    *task;
42 
43 	if (response != GTK_RESPONSE_OK) {
44 		gtk_widget_destroy (GTK_WIDGET (dialog));
45 		return;
46 	}
47 
48 	search = gth_search_editor_dialog_get_search (GTH_SEARCH_EDITOR_DIALOG (dialog), &error);
49 	if (search == NULL) {
50 		_gtk_error_dialog_from_gerror_show (GTK_WINDOW (dialog), _("Could not perform the search"), error);
51 		g_clear_error (&error);
52 		return;
53 	}
54 
55         search_catalog = gth_catalog_file_from_relative_path (_("Search Result"), ".search");
56         task = gth_search_task_new (browser, search, search_catalog);
57 	gth_browser_exec_task (browser, task, GTH_TASK_FLAGS_FOREGROUND);
58 
59 	g_object_unref (task);
60 	g_object_unref (search_catalog);
61 	g_object_unref (search);
62 	gtk_widget_destroy (GTK_WIDGET (dialog));
63 }
64 
65 
66 void
gth_browser_activate_find(GSimpleAction * action,GVariant * parameter,gpointer user_data)67 gth_browser_activate_find (GSimpleAction *action,
68 			   GVariant      *parameter,
69 			   gpointer       user_data)
70 {
71 	GthBrowser *browser = GTH_BROWSER (user_data);
72 	GthSearch  *search;
73 	GtkWidget  *dialog;
74 
75 	search = gth_search_new ();
76 	gth_search_set_source (search, gth_browser_get_location (browser), TRUE);
77 
78 	dialog = gth_search_editor_dialog_new (_("Find"), search, GTK_WINDOW (browser));
79 	gtk_dialog_add_button (GTK_DIALOG (dialog), _GTK_LABEL_CANCEL, GTK_RESPONSE_CANCEL);
80 	gtk_dialog_add_button (GTK_DIALOG (dialog), _("_Find"), GTK_RESPONSE_OK);
81 
82 	_gtk_dialog_add_class_to_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK, GTK_STYLE_CLASS_SUGGESTED_ACTION);
83 
84 	g_signal_connect (dialog, "response",
85 			  G_CALLBACK (search_editor_dialog__response_cb),
86 			  browser);
87 
88 	gtk_window_set_modal (GTK_WINDOW (dialog), FALSE);
89 	gtk_window_present (GTK_WINDOW (dialog));
90 	gth_search_editor_dialog_focus_first_rule (GTH_SEARCH_EDITOR_DIALOG (dialog));
91 
92 	g_object_unref (search);
93 }
94 
95 
96 typedef struct {
97 	GthBrowser *browser;
98 	GFile      *file;
99 } SearchData;
100 
101 
102 static void
search_data_free(SearchData * search_data)103 search_data_free (SearchData *search_data)
104 {
105 	g_object_unref (search_data->file);
106 	g_free (search_data);
107 }
108 
109 
110 static void
search_update_buffer_ready_cb(void ** buffer,gsize count,GError * error,gpointer user_data)111 search_update_buffer_ready_cb (void     **buffer,
112 			       gsize      count,
113 			       GError    *error,
114 			       gpointer   user_data)
115 {
116 	SearchData *search_data = user_data;
117 	GError     *local_error = NULL;
118 	GthSearch  *search;
119 	GthTask    *task;
120 
121 	if (error != NULL) {
122 		_gtk_error_dialog_from_gerror_show (GTK_WINDOW (search_data->browser), _("Could not perform the search"), error);
123 		return;
124 	}
125 
126 	search = gth_search_new_from_data (*buffer, count, &local_error);
127 	if (search == NULL) {
128 		_gtk_error_dialog_from_gerror_show (GTK_WINDOW (search_data->browser), _("Could not perform the search"), local_error);
129 		g_clear_error (&local_error);
130 		return;
131 	}
132 
133 	task = gth_search_task_new (search_data->browser, search, search_data->file);
134 	gth_browser_exec_task (search_data->browser, task, GTH_TASK_FLAGS_FOREGROUND);
135 
136 	g_object_unref (task);
137 	g_object_unref (search);
138 	search_data_free (search_data);
139 }
140 
141 
142 void
gth_browser_update_search(GthBrowser * browser)143 gth_browser_update_search (GthBrowser *browser)
144 {
145 	GFile      *location;
146 	SearchData *search_data;
147 	GFile      *file;
148 
149 	location = gth_browser_get_location (browser);
150 
151 	search_data = g_new0 (SearchData, 1);
152 	search_data->browser = browser;
153 	search_data->file = g_file_dup (location);
154 
155 	file = gth_main_get_gio_file (location);
156 	_g_file_load_async (file,
157 			    G_PRIORITY_DEFAULT,
158 			    NULL,
159 			    search_update_buffer_ready_cb,
160 			    search_data);
161 
162 	g_object_unref (file);
163 }
164