#include <gtk/gtk.h>
#include <glib/gi18n.h>
-#include "vikstatus.h"
#include "background.h"
+#include "settings.h"
static GThreadPool *thread_pool = NULL;
static gboolean stop_all_threads = FALSE;
static GtkWidget *bgtreeview = NULL;
static GtkListStore *bgstore = NULL;
-static GSList *statusbars_to_update = NULL;
+// Still only actually updating the statusbar though
+static GSList *windows_to_update = NULL;
static gint bgitemcount = 0;
+#define VIK_BG_NUM_ARGS 7
+
enum
{
TITLE_COLUMN = 0,
N_COLUMNS,
};
-void a_background_update_status ( VikStatusbar *vs, gchar *str )
+void a_background_update_status ( VikWindow *vw, gpointer data )
{
- gdk_threads_enter ();
- vik_statusbar_set_message ( vs, 1, str );
- gdk_threads_leave ();
+ static gchar buf[20];
+ g_snprintf(buf, sizeof(buf), _("%d items"), bgitemcount);
+ vik_window_statusbar_update ( vw, buf, VIK_STATUSBAR_ITEMS );
}
static void background_thread_update ()
{
- static gchar buf[20];
- g_snprintf(buf, sizeof(buf), _("%d items"), bgitemcount);
- g_slist_foreach ( statusbars_to_update, (GFunc) a_background_update_status, buf );
+ g_slist_foreach ( windows_to_update, (GFunc) a_background_update_status, NULL );
}
int a_background_thread_progress ( gpointer callbackdata, gdouble fraction )
return res;
}
-static void thread_die ( gpointer args[6] )
+static void thread_die ( gpointer args[VIK_BG_NUM_ARGS] )
{
vik_thr_free_func userdata_free_func = args[3];
- userdata_free_func ( args[2] );
+ if ( userdata_free_func != NULL )
+ userdata_free_func ( args[2] );
if ( GPOINTER_TO_INT(args[6]) )
{
}
return 0;
}
-void thread_helper ( gpointer args[6], gpointer user_data )
+
+static void thread_helper ( gpointer args[VIK_BG_NUM_ARGS], gpointer user_data )
{
/* unpack args */
vik_thr_func func = args[1];
thread_die ( args );
}
+/**
+ * a_background_thread:
+ * @parent:
+ * @message:
+ * @func: worker function
+ * @userdata:
+ * @userdata_free_func: free function for userdata
+ * @userdata_cancel_cleanup_func:
+ * @number_items:
+ *
+ * Function to enlist new background function.
+ */
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 )
{
GtkTreeIter *piter = g_malloc ( sizeof ( GtkTreeIter ) );
- gpointer *args = g_malloc ( sizeof(gpointer) * 7 );
+ gpointer *args = g_malloc ( sizeof(gpointer) * VIK_BG_NUM_ARGS );
g_debug(__FUNCTION__);
g_thread_pool_push( thread_pool, args, NULL );
}
+/**
+ * a_background_show_window:
+ *
+ * Display the background window.
+ */
void a_background_show_window ()
{
gtk_widget_show_all ( bgwindow );
{
gpointer *args;
- g_debug(__FUNCTION__);
+ g_debug(__FUNCTION__);
gtk_tree_model_get( GTK_TREE_MODEL(bgstore), piter, DATA_COLUMN, &args, -1 );
gtk_widget_hide ( bgwindow );
}
+#define VIK_SETTINGS_BACKGROUND_MAX_THREADS "background_max_threads"
+
+/**
+ * a_background_init:
+ *
+ * Initialize background feature.
+ */
void a_background_init()
{
/* initialize thread pool */
- /* TODO parametrize this via preference and/or command line arg */
gint max_threads = 10; /* limit maximum number of threads running at one time */
+ gint maxt;
+ if ( a_settings_get_integer ( VIK_SETTINGS_BACKGROUND_MAX_THREADS, &maxt ) )
+ max_threads = maxt;
+
thread_pool = g_thread_pool_new ( (GFunc) thread_helper, NULL, max_threads, FALSE, NULL );
GtkCellRenderer *renderer;
gtk_scrolled_window_set_policy ( GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
bgwindow = gtk_dialog_new_with_buttons ( "", NULL, 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_DELETE, 1, GTK_STOCK_CLEAR, 2, NULL );
- gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(bgwindow)->vbox), scrolled_window, TRUE, TRUE, 0 );
+ gtk_dialog_set_default_response ( GTK_DIALOG(bgwindow), GTK_RESPONSE_ACCEPT );
+ GtkWidget *response_w = NULL;
+#if GTK_CHECK_VERSION (2, 20, 0)
+ response_w = gtk_dialog_get_widget_for_response ( GTK_DIALOG(bgwindow), GTK_RESPONSE_ACCEPT );
+#endif
+ gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(bgwindow))), scrolled_window, TRUE, TRUE, 0 );
gtk_window_set_default_size ( GTK_WINDOW(bgwindow), 400, 400 );
gtk_window_set_title ( GTK_WINDOW(bgwindow), _("Viking Background Jobs") );
+ if ( response_w )
+ gtk_widget_grab_focus ( response_w );
/* don't destroy win */
g_signal_connect ( G_OBJECT(bgwindow), "delete-event", G_CALLBACK(gtk_widget_hide_on_delete), NULL );
}
+/**
+ * a_background_uninit:
+ *
+ * Uninitialize background feature.
+ */
void a_background_uninit()
{
/* wait until all running threads stop */
stop_all_threads = TRUE;
g_thread_pool_free ( thread_pool, TRUE, TRUE );
+
+ gtk_list_store_clear ( bgstore );
+ g_object_unref ( bgstore );
+
+ gtk_widget_destroy ( bgwindow );
}
-void a_background_add_status(VikStatusbar *vs)
+void a_background_add_window (VikWindow *vw)
{
- statusbars_to_update = g_slist_prepend(statusbars_to_update,vs);
+ windows_to_update = g_slist_prepend(windows_to_update,vw);
}
-void a_background_remove_status(VikStatusbar *vs)
+void a_background_remove_window (VikWindow *vw)
{
- statusbars_to_update = g_slist_remove(statusbars_to_update,vs);
+ windows_to_update = g_slist_remove(windows_to_update,vw);
}
-