]>
Commit | Line | Data |
---|---|---|
50a14534 EB |
1 | /* |
2 | * viking -- GPS Data and Topo Analyzer, Explorer, and Manager | |
3 | * | |
4 | * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 | * | |
20 | */ | |
21 | ||
22 | #include <gtk/gtk.h> | |
4c77d5e0 GB |
23 | #include <glib/gi18n.h> |
24 | ||
50a14534 | 25 | #include "background.h" |
062790f9 | 26 | #include "settings.h" |
50a14534 | 27 | |
634eca0a | 28 | static GThreadPool *thread_pool = NULL; |
7711383f | 29 | static gboolean stop_all_threads = FALSE; |
634eca0a | 30 | |
50a14534 EB |
31 | static GtkWidget *bgwindow = NULL; |
32 | static GtkWidget *bgtreeview = NULL; | |
33 | static GtkListStore *bgstore = NULL; | |
34 | ||
90142302 RN |
35 | // Still only actually updating the statusbar though |
36 | static GSList *windows_to_update = NULL; | |
50a14534 EB |
37 | |
38 | static gint bgitemcount = 0; | |
39 | ||
fc1318fd RN |
40 | #define VIK_BG_NUM_ARGS 7 |
41 | ||
50a14534 EB |
42 | enum |
43 | { | |
5c01c6b7 | 44 | TITLE_COLUMN = 0, |
50a14534 EB |
45 | PROGRESS_COLUMN, |
46 | DATA_COLUMN, | |
47 | N_COLUMNS, | |
48 | }; | |
49 | ||
90142302 | 50 | void a_background_update_status ( VikWindow *vw, gpointer data ) |
50a14534 | 51 | { |
90142302 RN |
52 | static gchar buf[20]; |
53 | g_snprintf(buf, sizeof(buf), _("%d items"), bgitemcount); | |
39ae014f | 54 | vik_window_statusbar_update ( vw, buf, VIK_STATUSBAR_ITEMS ); |
50a14534 EB |
55 | } |
56 | ||
57 | static void background_thread_update () | |
58 | { | |
90142302 | 59 | g_slist_foreach ( windows_to_update, (GFunc) a_background_update_status, NULL ); |
50a14534 EB |
60 | } |
61 | ||
634eca0a | 62 | int a_background_thread_progress ( gpointer callbackdata, gdouble fraction ) |
50a14534 EB |
63 | { |
64 | gpointer *args = (gpointer *) callbackdata; | |
7711383f | 65 | int res = a_background_testcancel ( callbackdata ); |
a08969b3 JN |
66 | if (args[5] != NULL) { |
67 | gdk_threads_enter(); | |
68 | gtk_list_store_set( GTK_LIST_STORE(bgstore), (GtkTreeIter *) args[5], PROGRESS_COLUMN, fraction*100, -1 ); | |
69 | gdk_threads_leave(); | |
70 | } | |
50a14534 EB |
71 | |
72 | args[6] = GINT_TO_POINTER(GPOINTER_TO_INT(args[6])-1); | |
73 | bgitemcount--; | |
7711383f JJ |
74 | background_thread_update(); |
75 | return res; | |
50a14534 EB |
76 | } |
77 | ||
fc1318fd | 78 | static void thread_die ( gpointer args[VIK_BG_NUM_ARGS] ) |
50a14534 EB |
79 | { |
80 | vik_thr_free_func userdata_free_func = args[3]; | |
81 | ||
eb6ae21e GB |
82 | if ( userdata_free_func != NULL ) |
83 | userdata_free_func ( args[2] ); | |
50a14534 EB |
84 | |
85 | if ( GPOINTER_TO_INT(args[6]) ) | |
86 | { | |
87 | bgitemcount -= GPOINTER_TO_INT(args[6]); | |
88 | background_thread_update (); | |
89 | } | |
90 | ||
91 | g_free ( args[5] ); /* free iter */ | |
92 | g_free ( args ); | |
50a14534 EB |
93 | } |
94 | ||
634eca0a | 95 | int a_background_testcancel ( gpointer callbackdata ) |
50a14534 EB |
96 | { |
97 | gpointer *args = (gpointer *) callbackdata; | |
7711383f JJ |
98 | if ( stop_all_threads ) |
99 | return -1; | |
100 | if ( args && args[0] ) | |
50a14534 EB |
101 | { |
102 | vik_thr_free_func cleanup = args[4]; | |
103 | if ( cleanup ) | |
104 | cleanup ( args[2] ); | |
634eca0a | 105 | return -1; |
50a14534 | 106 | } |
634eca0a | 107 | return 0; |
50a14534 | 108 | } |
818661ec | 109 | |
fc1318fd | 110 | static void thread_helper ( gpointer args[VIK_BG_NUM_ARGS], gpointer user_data ) |
50a14534 EB |
111 | { |
112 | /* unpack args */ | |
113 | vik_thr_func func = args[1]; | |
114 | gpointer userdata = args[2]; | |
115 | ||
5c01c6b7 GB |
116 | g_debug(__FUNCTION__); |
117 | ||
50a14534 EB |
118 | func ( userdata, args ); |
119 | ||
50a14534 EB |
120 | gdk_threads_enter(); |
121 | if ( ! args[0] ) | |
122 | gtk_list_store_remove ( bgstore, (GtkTreeIter *) args[5] ); | |
123 | gdk_threads_leave(); | |
124 | ||
125 | thread_die ( args ); | |
126 | } | |
127 | ||
43e8b799 GB |
128 | /** |
129 | * a_background_thread: | |
130 | * @parent: | |
131 | * @message: | |
132 | * @func: worker function | |
133 | * @userdata: | |
134 | * @userdata_free_func: free function for userdata | |
135 | * @userdata_cancel_cleanup_func: | |
136 | * @number_items: | |
137 | * | |
138 | * Function to enlist new background function. | |
139 | */ | |
50a14534 EB |
140 | void a_background_thread ( GtkWindow *parent, const gchar *message, vik_thr_func func, gpointer userdata, vik_thr_free_func userdata_free_func, vik_thr_free_func userdata_cancel_cleanup_func, gint number_items ) |
141 | { | |
142 | GtkTreeIter *piter = g_malloc ( sizeof ( GtkTreeIter ) ); | |
fc1318fd | 143 | gpointer *args = g_malloc ( sizeof(gpointer) * VIK_BG_NUM_ARGS ); |
50a14534 | 144 | |
5c01c6b7 GB |
145 | g_debug(__FUNCTION__); |
146 | ||
50a14534 EB |
147 | args[0] = GINT_TO_POINTER(0); |
148 | args[1] = func; | |
149 | args[2] = userdata; | |
150 | args[3] = userdata_free_func; | |
151 | args[4] = userdata_cancel_cleanup_func; | |
152 | args[5] = piter; | |
153 | args[6] = GINT_TO_POINTER(number_items); | |
154 | ||
155 | bgitemcount += number_items; | |
156 | ||
157 | gtk_list_store_append ( bgstore, piter ); | |
5c01c6b7 GB |
158 | gtk_list_store_set ( bgstore, piter, |
159 | TITLE_COLUMN, message, | |
160 | PROGRESS_COLUMN, 0.0, | |
161 | DATA_COLUMN, args, | |
162 | -1 ); | |
50a14534 EB |
163 | |
164 | /* run the thread in the background */ | |
634eca0a | 165 | g_thread_pool_push( thread_pool, args, NULL ); |
50a14534 EB |
166 | } |
167 | ||
43e8b799 GB |
168 | /** |
169 | * a_background_show_window: | |
170 | * | |
171 | * Display the background window. | |
172 | */ | |
50a14534 EB |
173 | void a_background_show_window () |
174 | { | |
175 | gtk_widget_show_all ( bgwindow ); | |
176 | } | |
177 | ||
178 | static void cancel_job_with_iter ( GtkTreeIter *piter ) | |
179 | { | |
180 | gpointer *args; | |
5c01c6b7 | 181 | |
43e8b799 | 182 | g_debug(__FUNCTION__); |
5c01c6b7 | 183 | |
50a14534 EB |
184 | gtk_tree_model_get( GTK_TREE_MODEL(bgstore), piter, DATA_COLUMN, &args, -1 ); |
185 | ||
186 | /* we know args still exists because it is free _after_ the list item is destroyed */ | |
187 | /* need MUTEX ? */ | |
188 | args[0] = GINT_TO_POINTER(1); /* set killswitch */ | |
189 | ||
50a14534 | 190 | gtk_list_store_remove ( bgstore, piter ); |
a08969b3 | 191 | args[5] = NULL; |
50a14534 EB |
192 | } |
193 | ||
194 | static void bgwindow_response (GtkDialog *dialog, gint arg1 ) | |
195 | { | |
439f34ec AF |
196 | /* note this function is a signal handler called back from the GTK main loop, |
197 | * so GDK is already locked. We need to release the lock before calling | |
198 | * thread-safe routines | |
199 | */ | |
200 | if ( arg1 == 1 ) /* cancel */ | |
201 | { | |
202 | GtkTreeIter iter; | |
203 | if ( gtk_tree_selection_get_selected ( gtk_tree_view_get_selection ( GTK_TREE_VIEW(bgtreeview) ), NULL, &iter ) ) | |
204 | cancel_job_with_iter ( &iter ); | |
205 | gdk_threads_leave(); | |
206 | background_thread_update(); | |
207 | gdk_threads_enter(); | |
208 | } | |
50a14534 | 209 | else if ( arg1 == 2 ) /* clear */ |
439f34ec AF |
210 | { |
211 | GtkTreeIter iter; | |
212 | while ( gtk_tree_model_get_iter_first ( GTK_TREE_MODEL(bgstore), &iter ) ) | |
213 | cancel_job_with_iter ( &iter ); | |
439f34ec AF |
214 | gdk_threads_leave(); |
215 | background_thread_update(); | |
216 | gdk_threads_enter(); | |
217 | } | |
50a14534 EB |
218 | else /* OK */ |
219 | gtk_widget_hide ( bgwindow ); | |
220 | } | |
221 | ||
062790f9 RN |
222 | #define VIK_SETTINGS_BACKGROUND_MAX_THREADS "background_max_threads" |
223 | ||
43e8b799 GB |
224 | /** |
225 | * a_background_init: | |
226 | * | |
227 | * Initialize background feature. | |
228 | */ | |
50a14534 EB |
229 | void a_background_init() |
230 | { | |
634eca0a JJ |
231 | /* initialize thread pool */ |
232 | gint max_threads = 10; /* limit maximum number of threads running at one time */ | |
062790f9 RN |
233 | gint maxt; |
234 | if ( a_settings_get_integer ( VIK_SETTINGS_BACKGROUND_MAX_THREADS, &maxt ) ) | |
235 | max_threads = maxt; | |
236 | ||
634eca0a JJ |
237 | thread_pool = g_thread_pool_new ( (GFunc) thread_helper, NULL, max_threads, FALSE, NULL ); |
238 | ||
50a14534 EB |
239 | GtkCellRenderer *renderer; |
240 | GtkTreeViewColumn *column; | |
241 | GtkWidget *scrolled_window; | |
242 | ||
5c01c6b7 GB |
243 | g_debug(__FUNCTION__); |
244 | ||
50a14534 EB |
245 | /* store & treeview */ |
246 | bgstore = gtk_list_store_new ( N_COLUMNS, G_TYPE_STRING, G_TYPE_DOUBLE, G_TYPE_POINTER ); | |
247 | bgtreeview = gtk_tree_view_new_with_model ( GTK_TREE_MODEL(bgstore) ); | |
248 | gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (bgtreeview), TRUE); | |
249 | gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW (bgtreeview)), | |
250 | GTK_SELECTION_SINGLE); | |
251 | ||
252 | /* add columns */ | |
253 | renderer = gtk_cell_renderer_text_new (); | |
4c77d5e0 | 254 | column = gtk_tree_view_column_new_with_attributes ( _("Job"), renderer, "text", TITLE_COLUMN, NULL ); |
50a14534 EB |
255 | gtk_tree_view_append_column ( GTK_TREE_VIEW(bgtreeview), column ); |
256 | ||
257 | renderer = gtk_cell_renderer_progress_new (); | |
5c01c6b7 | 258 | column = gtk_tree_view_column_new_with_attributes ( _("Progress"), renderer, "value", PROGRESS_COLUMN, NULL ); |
50a14534 EB |
259 | gtk_tree_view_append_column ( GTK_TREE_VIEW(bgtreeview), column ); |
260 | ||
261 | /* setup window */ | |
262 | scrolled_window = gtk_scrolled_window_new ( NULL, NULL ); | |
263 | gtk_container_add ( GTK_CONTAINER(scrolled_window), bgtreeview ); | |
264 | gtk_scrolled_window_set_policy ( GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC ); | |
265 | ||
266 | bgwindow = gtk_dialog_new_with_buttons ( "", NULL, 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_DELETE, 1, GTK_STOCK_CLEAR, 2, NULL ); | |
2bbbc9fb RN |
267 | gtk_dialog_set_default_response ( GTK_DIALOG(bgwindow), GTK_RESPONSE_ACCEPT ); |
268 | GtkWidget *response_w = NULL; | |
269 | #if GTK_CHECK_VERSION (2, 20, 0) | |
270 | response_w = gtk_dialog_get_widget_for_response ( GTK_DIALOG(bgwindow), GTK_RESPONSE_ACCEPT ); | |
271 | #endif | |
9b082b39 | 272 | gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(bgwindow))), scrolled_window, TRUE, TRUE, 0 ); |
50a14534 | 273 | gtk_window_set_default_size ( GTK_WINDOW(bgwindow), 400, 400 ); |
4c77d5e0 | 274 | gtk_window_set_title ( GTK_WINDOW(bgwindow), _("Viking Background Jobs") ); |
2bbbc9fb RN |
275 | if ( response_w ) |
276 | gtk_widget_grab_focus ( response_w ); | |
50a14534 EB |
277 | /* don't destroy win */ |
278 | g_signal_connect ( G_OBJECT(bgwindow), "delete-event", G_CALLBACK(gtk_widget_hide_on_delete), NULL ); | |
279 | ||
280 | g_signal_connect ( G_OBJECT(bgwindow), "response", G_CALLBACK(bgwindow_response), 0 ); | |
281 | ||
282 | } | |
283 | ||
43e8b799 GB |
284 | /** |
285 | * a_background_uninit: | |
286 | * | |
287 | * Uninitialize background feature. | |
288 | */ | |
634eca0a JJ |
289 | void a_background_uninit() |
290 | { | |
291 | /* wait until all running threads stop */ | |
7711383f | 292 | stop_all_threads = TRUE; |
634eca0a | 293 | g_thread_pool_free ( thread_pool, TRUE, TRUE ); |
919ed63e RN |
294 | |
295 | gtk_list_store_clear ( bgstore ); | |
296 | g_object_unref ( bgstore ); | |
297 | ||
298 | gtk_widget_destroy ( bgwindow ); | |
634eca0a JJ |
299 | } |
300 | ||
90142302 | 301 | void a_background_add_window (VikWindow *vw) |
50a14534 | 302 | { |
90142302 | 303 | windows_to_update = g_slist_prepend(windows_to_update,vw); |
50a14534 EB |
304 | } |
305 | ||
90142302 | 306 | void a_background_remove_window (VikWindow *vw) |
50a14534 | 307 | { |
90142302 | 308 | windows_to_update = g_slist_remove(windows_to_update,vw); |
50a14534 | 309 | } |