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