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