]>
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 EB |
25 | #include "vikstatus.h" |
26 | #include "background.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 | ||
35 | static GSList *statusbars_to_update = NULL; | |
36 | ||
37 | static gint bgitemcount = 0; | |
38 | ||
fc1318fd RN |
39 | #define VIK_BG_NUM_ARGS 7 |
40 | ||
50a14534 EB |
41 | enum |
42 | { | |
5c01c6b7 | 43 | TITLE_COLUMN = 0, |
50a14534 EB |
44 | PROGRESS_COLUMN, |
45 | DATA_COLUMN, | |
46 | N_COLUMNS, | |
47 | }; | |
48 | ||
49 | void a_background_update_status ( VikStatusbar *vs, gchar *str ) | |
50 | { | |
51 | gdk_threads_enter (); | |
4efc10ca | 52 | vik_statusbar_set_message ( vs, VIK_STATUSBAR_ITEMS, str ); |
50a14534 EB |
53 | gdk_threads_leave (); |
54 | } | |
55 | ||
56 | static void background_thread_update () | |
57 | { | |
58 | static gchar buf[20]; | |
4c77d5e0 | 59 | g_snprintf(buf, sizeof(buf), _("%d items"), bgitemcount); |
50a14534 EB |
60 | g_slist_foreach ( statusbars_to_update, (GFunc) a_background_update_status, buf ); |
61 | } | |
62 | ||
634eca0a | 63 | int a_background_thread_progress ( gpointer callbackdata, gdouble fraction ) |
50a14534 EB |
64 | { |
65 | gpointer *args = (gpointer *) callbackdata; | |
7711383f | 66 | int res = a_background_testcancel ( callbackdata ); |
a08969b3 JN |
67 | if (args[5] != NULL) { |
68 | gdk_threads_enter(); | |
69 | gtk_list_store_set( GTK_LIST_STORE(bgstore), (GtkTreeIter *) args[5], PROGRESS_COLUMN, fraction*100, -1 ); | |
70 | gdk_threads_leave(); | |
71 | } | |
50a14534 EB |
72 | |
73 | args[6] = GINT_TO_POINTER(GPOINTER_TO_INT(args[6])-1); | |
74 | bgitemcount--; | |
7711383f JJ |
75 | background_thread_update(); |
76 | return res; | |
50a14534 EB |
77 | } |
78 | ||
fc1318fd | 79 | static void thread_die ( gpointer args[VIK_BG_NUM_ARGS] ) |
50a14534 EB |
80 | { |
81 | vik_thr_free_func userdata_free_func = args[3]; | |
82 | ||
eb6ae21e GB |
83 | if ( userdata_free_func != NULL ) |
84 | userdata_free_func ( args[2] ); | |
50a14534 EB |
85 | |
86 | if ( GPOINTER_TO_INT(args[6]) ) | |
87 | { | |
88 | bgitemcount -= GPOINTER_TO_INT(args[6]); | |
89 | background_thread_update (); | |
90 | } | |
91 | ||
92 | g_free ( args[5] ); /* free iter */ | |
93 | g_free ( args ); | |
50a14534 EB |
94 | } |
95 | ||
634eca0a | 96 | int a_background_testcancel ( gpointer callbackdata ) |
50a14534 EB |
97 | { |
98 | gpointer *args = (gpointer *) callbackdata; | |
7711383f JJ |
99 | if ( stop_all_threads ) |
100 | return -1; | |
101 | if ( args && args[0] ) | |
50a14534 EB |
102 | { |
103 | vik_thr_free_func cleanup = args[4]; | |
104 | if ( cleanup ) | |
105 | cleanup ( args[2] ); | |
634eca0a | 106 | return -1; |
50a14534 | 107 | } |
634eca0a | 108 | return 0; |
50a14534 | 109 | } |
818661ec | 110 | |
fc1318fd | 111 | static void thread_helper ( gpointer args[VIK_BG_NUM_ARGS], gpointer user_data ) |
50a14534 EB |
112 | { |
113 | /* unpack args */ | |
114 | vik_thr_func func = args[1]; | |
115 | gpointer userdata = args[2]; | |
116 | ||
5c01c6b7 GB |
117 | g_debug(__FUNCTION__); |
118 | ||
50a14534 EB |
119 | func ( userdata, args ); |
120 | ||
50a14534 EB |
121 | gdk_threads_enter(); |
122 | if ( ! args[0] ) | |
123 | gtk_list_store_remove ( bgstore, (GtkTreeIter *) args[5] ); | |
124 | gdk_threads_leave(); | |
125 | ||
126 | thread_die ( args ); | |
127 | } | |
128 | ||
43e8b799 GB |
129 | /** |
130 | * a_background_thread: | |
131 | * @parent: | |
132 | * @message: | |
133 | * @func: worker function | |
134 | * @userdata: | |
135 | * @userdata_free_func: free function for userdata | |
136 | * @userdata_cancel_cleanup_func: | |
137 | * @number_items: | |
138 | * | |
139 | * Function to enlist new background function. | |
140 | */ | |
50a14534 EB |
141 | 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 ) |
142 | { | |
143 | GtkTreeIter *piter = g_malloc ( sizeof ( GtkTreeIter ) ); | |
fc1318fd | 144 | gpointer *args = g_malloc ( sizeof(gpointer) * VIK_BG_NUM_ARGS ); |
50a14534 | 145 | |
5c01c6b7 GB |
146 | g_debug(__FUNCTION__); |
147 | ||
50a14534 EB |
148 | args[0] = GINT_TO_POINTER(0); |
149 | args[1] = func; | |
150 | args[2] = userdata; | |
151 | args[3] = userdata_free_func; | |
152 | args[4] = userdata_cancel_cleanup_func; | |
153 | args[5] = piter; | |
154 | args[6] = GINT_TO_POINTER(number_items); | |
155 | ||
156 | bgitemcount += number_items; | |
157 | ||
158 | gtk_list_store_append ( bgstore, piter ); | |
5c01c6b7 GB |
159 | gtk_list_store_set ( bgstore, piter, |
160 | TITLE_COLUMN, message, | |
161 | PROGRESS_COLUMN, 0.0, | |
162 | DATA_COLUMN, args, | |
163 | -1 ); | |
50a14534 EB |
164 | |
165 | /* run the thread in the background */ | |
634eca0a | 166 | g_thread_pool_push( thread_pool, args, NULL ); |
50a14534 EB |
167 | } |
168 | ||
43e8b799 GB |
169 | /** |
170 | * a_background_show_window: | |
171 | * | |
172 | * Display the background window. | |
173 | */ | |
50a14534 EB |
174 | void a_background_show_window () |
175 | { | |
176 | gtk_widget_show_all ( bgwindow ); | |
177 | } | |
178 | ||
179 | static void cancel_job_with_iter ( GtkTreeIter *piter ) | |
180 | { | |
181 | gpointer *args; | |
5c01c6b7 | 182 | |
43e8b799 | 183 | g_debug(__FUNCTION__); |
5c01c6b7 | 184 | |
50a14534 EB |
185 | gtk_tree_model_get( GTK_TREE_MODEL(bgstore), piter, DATA_COLUMN, &args, -1 ); |
186 | ||
187 | /* we know args still exists because it is free _after_ the list item is destroyed */ | |
188 | /* need MUTEX ? */ | |
189 | args[0] = GINT_TO_POINTER(1); /* set killswitch */ | |
190 | ||
50a14534 | 191 | gtk_list_store_remove ( bgstore, piter ); |
a08969b3 | 192 | args[5] = NULL; |
50a14534 EB |
193 | } |
194 | ||
195 | static void bgwindow_response (GtkDialog *dialog, gint arg1 ) | |
196 | { | |
439f34ec AF |
197 | /* note this function is a signal handler called back from the GTK main loop, |
198 | * so GDK is already locked. We need to release the lock before calling | |
199 | * thread-safe routines | |
200 | */ | |
201 | if ( arg1 == 1 ) /* cancel */ | |
202 | { | |
203 | GtkTreeIter iter; | |
204 | if ( gtk_tree_selection_get_selected ( gtk_tree_view_get_selection ( GTK_TREE_VIEW(bgtreeview) ), NULL, &iter ) ) | |
205 | cancel_job_with_iter ( &iter ); | |
206 | gdk_threads_leave(); | |
207 | background_thread_update(); | |
208 | gdk_threads_enter(); | |
209 | } | |
50a14534 | 210 | else if ( arg1 == 2 ) /* clear */ |
439f34ec AF |
211 | { |
212 | GtkTreeIter iter; | |
213 | while ( gtk_tree_model_get_iter_first ( GTK_TREE_MODEL(bgstore), &iter ) ) | |
214 | cancel_job_with_iter ( &iter ); | |
439f34ec AF |
215 | gdk_threads_leave(); |
216 | background_thread_update(); | |
217 | gdk_threads_enter(); | |
218 | } | |
50a14534 EB |
219 | else /* OK */ |
220 | gtk_widget_hide ( bgwindow ); | |
221 | } | |
222 | ||
43e8b799 GB |
223 | /** |
224 | * a_background_init: | |
225 | * | |
226 | * Initialize background feature. | |
227 | */ | |
50a14534 EB |
228 | void a_background_init() |
229 | { | |
634eca0a | 230 | /* initialize thread pool */ |
64a222e9 | 231 | /* TODO parametrize this via preference and/or command line arg */ |
634eca0a JJ |
232 | gint max_threads = 10; /* limit maximum number of threads running at one time */ |
233 | thread_pool = g_thread_pool_new ( (GFunc) thread_helper, NULL, max_threads, FALSE, NULL ); | |
234 | ||
50a14534 EB |
235 | GtkCellRenderer *renderer; |
236 | GtkTreeViewColumn *column; | |
237 | GtkWidget *scrolled_window; | |
238 | ||
5c01c6b7 GB |
239 | g_debug(__FUNCTION__); |
240 | ||
50a14534 EB |
241 | /* store & treeview */ |
242 | bgstore = gtk_list_store_new ( N_COLUMNS, G_TYPE_STRING, G_TYPE_DOUBLE, G_TYPE_POINTER ); | |
243 | bgtreeview = gtk_tree_view_new_with_model ( GTK_TREE_MODEL(bgstore) ); | |
244 | gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (bgtreeview), TRUE); | |
245 | gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW (bgtreeview)), | |
246 | GTK_SELECTION_SINGLE); | |
247 | ||
248 | /* add columns */ | |
249 | renderer = gtk_cell_renderer_text_new (); | |
4c77d5e0 | 250 | column = gtk_tree_view_column_new_with_attributes ( _("Job"), renderer, "text", TITLE_COLUMN, NULL ); |
50a14534 EB |
251 | gtk_tree_view_append_column ( GTK_TREE_VIEW(bgtreeview), column ); |
252 | ||
253 | renderer = gtk_cell_renderer_progress_new (); | |
5c01c6b7 | 254 | column = gtk_tree_view_column_new_with_attributes ( _("Progress"), renderer, "value", PROGRESS_COLUMN, NULL ); |
50a14534 EB |
255 | gtk_tree_view_append_column ( GTK_TREE_VIEW(bgtreeview), column ); |
256 | ||
257 | /* setup window */ | |
258 | scrolled_window = gtk_scrolled_window_new ( NULL, NULL ); | |
259 | gtk_container_add ( GTK_CONTAINER(scrolled_window), bgtreeview ); | |
260 | gtk_scrolled_window_set_policy ( GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC ); | |
261 | ||
262 | 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 |
263 | gtk_dialog_set_default_response ( GTK_DIALOG(bgwindow), GTK_RESPONSE_ACCEPT ); |
264 | GtkWidget *response_w = NULL; | |
265 | #if GTK_CHECK_VERSION (2, 20, 0) | |
266 | response_w = gtk_dialog_get_widget_for_response ( GTK_DIALOG(bgwindow), GTK_RESPONSE_ACCEPT ); | |
267 | #endif | |
50a14534 EB |
268 | gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(bgwindow)->vbox), scrolled_window, TRUE, TRUE, 0 ); |
269 | gtk_window_set_default_size ( GTK_WINDOW(bgwindow), 400, 400 ); | |
4c77d5e0 | 270 | gtk_window_set_title ( GTK_WINDOW(bgwindow), _("Viking Background Jobs") ); |
2bbbc9fb RN |
271 | if ( response_w ) |
272 | gtk_widget_grab_focus ( response_w ); | |
50a14534 EB |
273 | /* don't destroy win */ |
274 | g_signal_connect ( G_OBJECT(bgwindow), "delete-event", G_CALLBACK(gtk_widget_hide_on_delete), NULL ); | |
275 | ||
276 | g_signal_connect ( G_OBJECT(bgwindow), "response", G_CALLBACK(bgwindow_response), 0 ); | |
277 | ||
278 | } | |
279 | ||
43e8b799 GB |
280 | /** |
281 | * a_background_uninit: | |
282 | * | |
283 | * Uninitialize background feature. | |
284 | */ | |
634eca0a JJ |
285 | void a_background_uninit() |
286 | { | |
287 | /* wait until all running threads stop */ | |
7711383f | 288 | stop_all_threads = TRUE; |
634eca0a JJ |
289 | g_thread_pool_free ( thread_pool, TRUE, TRUE ); |
290 | } | |
291 | ||
50a14534 EB |
292 | void a_background_add_status(VikStatusbar *vs) |
293 | { | |
294 | statusbars_to_update = g_slist_prepend(statusbars_to_update,vs); | |
295 | } | |
296 | ||
297 | void a_background_remove_status(VikStatusbar *vs) | |
298 | { | |
299 | statusbars_to_update = g_slist_remove(statusbars_to_update,vs); | |
300 | } | |
301 |