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