2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
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.
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.
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
23 #include <glib/gi18n.h>
25 #include "vikstatus.h"
26 #include "background.h"
28 static GThreadPool *thread_pool = NULL;
29 static gboolean stop_all_threads = FALSE;
31 static GtkWidget *bgwindow = NULL;
32 static GtkWidget *bgtreeview = NULL;
33 static GtkListStore *bgstore = NULL;
35 static GSList *statusbars_to_update = NULL;
37 static gint bgitemcount = 0;
47 void a_background_update_status ( VikStatusbar *vs, gchar *str )
50 vik_statusbar_set_message ( vs, 1, str );
54 static void background_thread_update ()
57 g_snprintf(buf, sizeof(buf), _("%d items"), bgitemcount);
58 g_slist_foreach ( statusbars_to_update, (GFunc) a_background_update_status, buf );
61 int a_background_thread_progress ( gpointer callbackdata, gdouble fraction )
63 gpointer *args = (gpointer *) callbackdata;
64 int res = a_background_testcancel ( callbackdata );
65 if (args[5] != NULL) {
67 gtk_list_store_set( GTK_LIST_STORE(bgstore), (GtkTreeIter *) args[5], PROGRESS_COLUMN, fraction*100, -1 );
71 args[6] = GINT_TO_POINTER(GPOINTER_TO_INT(args[6])-1);
73 background_thread_update();
77 static void thread_die ( gpointer args[6] )
79 vik_thr_free_func userdata_free_func = args[3];
81 userdata_free_func ( args[2] );
83 if ( GPOINTER_TO_INT(args[6]) )
85 bgitemcount -= GPOINTER_TO_INT(args[6]);
86 background_thread_update ();
89 g_free ( args[5] ); /* free iter */
93 int a_background_testcancel ( gpointer callbackdata )
95 gpointer *args = (gpointer *) callbackdata;
96 if ( stop_all_threads )
98 if ( args && args[0] )
100 vik_thr_free_func cleanup = args[4];
108 static void thread_helper ( gpointer args[6], gpointer user_data )
111 vik_thr_func func = args[1];
112 gpointer userdata = args[2];
114 g_debug(__FUNCTION__);
116 func ( userdata, args );
120 gtk_list_store_remove ( bgstore, (GtkTreeIter *) args[5] );
126 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 )
128 GtkTreeIter *piter = g_malloc ( sizeof ( GtkTreeIter ) );
129 gpointer *args = g_malloc ( sizeof(gpointer) * 7 );
131 g_debug(__FUNCTION__);
133 args[0] = GINT_TO_POINTER(0);
136 args[3] = userdata_free_func;
137 args[4] = userdata_cancel_cleanup_func;
139 args[6] = GINT_TO_POINTER(number_items);
141 bgitemcount += number_items;
143 gtk_list_store_append ( bgstore, piter );
144 gtk_list_store_set ( bgstore, piter,
145 TITLE_COLUMN, message,
146 PROGRESS_COLUMN, 0.0,
150 /* run the thread in the background */
151 g_thread_pool_push( thread_pool, args, NULL );
154 void a_background_show_window ()
156 gtk_widget_show_all ( bgwindow );
159 static void cancel_job_with_iter ( GtkTreeIter *piter )
163 g_debug(__FUNCTION__);
165 gtk_tree_model_get( GTK_TREE_MODEL(bgstore), piter, DATA_COLUMN, &args, -1 );
167 /* we know args still exists because it is free _after_ the list item is destroyed */
169 args[0] = GINT_TO_POINTER(1); /* set killswitch */
171 gtk_list_store_remove ( bgstore, piter );
175 static void bgwindow_response (GtkDialog *dialog, gint arg1 )
177 /* note this function is a signal handler called back from the GTK main loop,
178 * so GDK is already locked. We need to release the lock before calling
179 * thread-safe routines
181 if ( arg1 == 1 ) /* cancel */
184 if ( gtk_tree_selection_get_selected ( gtk_tree_view_get_selection ( GTK_TREE_VIEW(bgtreeview) ), NULL, &iter ) )
185 cancel_job_with_iter ( &iter );
187 background_thread_update();
190 else if ( arg1 == 2 ) /* clear */
193 while ( gtk_tree_model_get_iter_first ( GTK_TREE_MODEL(bgstore), &iter ) )
194 cancel_job_with_iter ( &iter );
196 background_thread_update();
200 gtk_widget_hide ( bgwindow );
203 void a_background_init()
205 /* initialize thread pool */
206 /* TODO parametrize this via preference and/or command line arg */
207 gint max_threads = 10; /* limit maximum number of threads running at one time */
208 thread_pool = g_thread_pool_new ( (GFunc) thread_helper, NULL, max_threads, FALSE, NULL );
210 GtkCellRenderer *renderer;
211 GtkTreeViewColumn *column;
212 GtkWidget *scrolled_window;
214 g_debug(__FUNCTION__);
216 /* store & treeview */
217 bgstore = gtk_list_store_new ( N_COLUMNS, G_TYPE_STRING, G_TYPE_DOUBLE, G_TYPE_POINTER );
218 bgtreeview = gtk_tree_view_new_with_model ( GTK_TREE_MODEL(bgstore) );
219 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (bgtreeview), TRUE);
220 gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW (bgtreeview)),
221 GTK_SELECTION_SINGLE);
224 renderer = gtk_cell_renderer_text_new ();
225 column = gtk_tree_view_column_new_with_attributes ( _("Job"), renderer, "text", TITLE_COLUMN, NULL );
226 gtk_tree_view_append_column ( GTK_TREE_VIEW(bgtreeview), column );
228 renderer = gtk_cell_renderer_progress_new ();
229 column = gtk_tree_view_column_new_with_attributes ( _("Progress"), renderer, "value", PROGRESS_COLUMN, NULL );
230 gtk_tree_view_append_column ( GTK_TREE_VIEW(bgtreeview), column );
233 scrolled_window = gtk_scrolled_window_new ( NULL, NULL );
234 gtk_container_add ( GTK_CONTAINER(scrolled_window), bgtreeview );
235 gtk_scrolled_window_set_policy ( GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
237 bgwindow = gtk_dialog_new_with_buttons ( "", NULL, 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_DELETE, 1, GTK_STOCK_CLEAR, 2, NULL );
238 gtk_dialog_set_default_response ( GTK_DIALOG(bgwindow), GTK_RESPONSE_ACCEPT );
239 GtkWidget *response_w = NULL;
240 #if GTK_CHECK_VERSION (2, 20, 0)
241 response_w = gtk_dialog_get_widget_for_response ( GTK_DIALOG(bgwindow), GTK_RESPONSE_ACCEPT );
243 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(bgwindow)->vbox), scrolled_window, TRUE, TRUE, 0 );
244 gtk_window_set_default_size ( GTK_WINDOW(bgwindow), 400, 400 );
245 gtk_window_set_title ( GTK_WINDOW(bgwindow), _("Viking Background Jobs") );
247 gtk_widget_grab_focus ( response_w );
248 /* don't destroy win */
249 g_signal_connect ( G_OBJECT(bgwindow), "delete-event", G_CALLBACK(gtk_widget_hide_on_delete), NULL );
251 g_signal_connect ( G_OBJECT(bgwindow), "response", G_CALLBACK(bgwindow_response), 0 );
255 void a_background_uninit()
257 /* wait until all running threads stop */
258 stop_all_threads = TRUE;
259 g_thread_pool_free ( thread_pool, TRUE, TRUE );
262 void a_background_add_status(VikStatusbar *vs)
264 statusbars_to_update = g_slist_prepend(statusbars_to_update,vs);
267 void a_background_remove_status(VikStatusbar *vs)
269 statusbars_to_update = g_slist_remove(statusbars_to_update,vs);