--- raeddit-03.c 2009-09-01 20:22:00.000000000 -0400 +++ raeddit-04/raeddit.c 2009-09-05 22:52:28.000000000 -0400 @@ -1,9 +1,5 @@ /* raeddit: a simple reddit client for maemo. - - compile with: - - gcc `pkg-config --cflags --libs glib-2.0 hildon-1 json-glib-1.0 libcurl dbus-glib-1` raeddit-03.c greddit.c -o raeddit-03 */ #include #include @@ -11,12 +7,16 @@ #include #include #include -#include "greddit.h" +#include +#include "greddit2.h" /* "r[ae]ddit" */ #define APP_NAME "r" "\xc3\xa6" "ddit" GtkWidget *window; +GtkWidget *progress_bar; +GtkWidget *pan, *vbox; +GSList *posts = NULL; static void show_message (const char *message) @@ -68,25 +68,37 @@ launch_browser ((char *) data); } -int -main(int argc, char **argv) +static void +update_progress_bar (int so_far, int total) { - GtkWidget *pan, *vbox; - GSList *posts = NULL; - GSList *cursor; + gdk_threads_enter (); - gtk_init (&argc, &argv); - g_set_application_name (APP_NAME); - curl_global_init (CURL_GLOBAL_ALL); + if (total==0) + { + gtk_progress_bar_pulse (GTK_PROGRESS_BAR (progress_bar)); + } + else + { + gdouble fraction = ((gdouble)so_far) / ((gdouble)total); + gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (progress_bar), + fraction); + } - posts = greddit_get_posts (); + gdk_threads_leave (); +} - window = GTK_WIDGET (hildon_window_new ()); - gtk_window_set_title (GTK_WINDOW (window), APP_NAME); - gtk_widget_show_all (GTK_WIDGET (window)); +static void* +download_thread (void* dummy) +{ + GSList *cursor; - pan = GTK_WIDGET (hildon_pannable_area_new ()); - vbox = GTK_WIDGET (gtk_vbox_new (FALSE, 0)); + posts = greddit_get_posts_with_callback (update_progress_bar); + + /* Got it. */ + gdk_threads_enter (); + + gtk_container_remove (GTK_CONTAINER (vbox), + progress_bar); for (cursor=posts; cursor; cursor = cursor->next) { @@ -114,17 +126,54 @@ g_free (subtitle); } + gtk_widget_show_all (GTK_WIDGET (window)); + + if (!posts) + { + show_message ("Nothing found."); + } + + gdk_threads_leave (); + + return NULL; +} + +int +main(int argc, char **argv) +{ + gtk_init (&argc, &argv); + g_thread_init (NULL); + gdk_threads_init (); + g_set_application_name (APP_NAME); + curl_global_init (CURL_GLOBAL_ALL); + + window = GTK_WIDGET (hildon_window_new ()); + gtk_window_set_title (GTK_WINDOW (window), APP_NAME); + gtk_widget_show_all (GTK_WIDGET (window)); + + pan = GTK_WIDGET (hildon_pannable_area_new ()); + vbox = GTK_WIDGET (gtk_vbox_new (FALSE, 0)); + + progress_bar = gtk_progress_bar_new (); + gtk_progress_bar_set_text (GTK_PROGRESS_BAR (progress_bar), + "Synchronising with Reddit"); + gtk_box_pack_end (GTK_BOX (vbox), progress_bar, TRUE, FALSE, 0); + hildon_pannable_area_add_with_viewport (HILDON_PANNABLE_AREA (pan), vbox); gtk_container_add (GTK_CONTAINER (window), pan); + gtk_widget_show_all (GTK_WIDGET (window)); - if (!posts) + if (!g_thread_create(&download_thread, NULL, FALSE, NULL) != 0) { - show_message ("Nothing found."); + show_message ("Couldn't create the download thread. Must exit."); + gdk_threads_leave (); + return EXIT_FAILURE; } gtk_main (); + gdk_threads_leave (); greddit_free_posts (posts); curl_global_cleanup ();