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