Common subdirectories: raeddit-04/debian and raeddit-05/debian diff -u raeddit-04/greddit2.c raeddit-05/greddit2.c --- raeddit-04/greddit2.c 2009-09-07 13:31:19.000000000 -0400 +++ raeddit-05/greddit2.c 2009-09-09 23:03:03.000000000 -0400 @@ -172,6 +172,17 @@ p->author = g_strdup (json_node_get_string (json_object_get_member (post, "author"))); p->subreddit = g_strdup (json_node_get_string (json_object_get_member (post, "subreddit"))); p->score = json_node_get_int (json_object_get_member (post, "score")); + p->comment_count = json_node_get_int (json_object_get_member (post, "num_comments")); + + p->suffix = index (p->id, '_'); + if (p->suffix) + { + p->suffix++; + } + else + { + p->suffix = p->id; + } result = g_slist_insert_sorted (result, p, greddit_post_compare); } diff -u raeddit-04/greddit2.h raeddit-05/greddit2.h --- raeddit-04/greddit2.h 2009-09-05 21:30:18.000000000 -0400 +++ raeddit-05/greddit2.h 2009-09-09 23:10:17.000000000 -0400 @@ -35,6 +35,12 @@ */ char *id; /** + * The final part of the ID. For example, an ID of "t3_9fvgj" + * will have a suffix of "9fvgj". This is often used to + * construct URLs. + */ + char *suffix; + /** * The title of the post. */ char* title; @@ -54,6 +60,10 @@ * The subsection of reddit.com which bears this post. */ char *subreddit; + /** + * The number of comments the post has received on Reddit. + */ + guint comment_count; /* ... more may be added here later. */ diff -u raeddit-04/raeddit.c raeddit-05/raeddit.c --- raeddit-04/raeddit.c 2009-09-05 22:52:28.000000000 -0400 +++ raeddit-05/raeddit.c 2009-09-09 23:35:35.000000000 -0400 @@ -62,10 +62,119 @@ } } +static gchar* +visit_site_cb (GRedditPost *post, gboolean prepare) +{ + if (prepare) + { + GRegex *hostname = g_regex_new ("//([^/]*)/", + 0, 0, NULL); + GMatchInfo *found_hostname = NULL; + gchar *result; + + g_regex_match (hostname, + post->url, + 0, + &found_hostname); + + result = g_strdup_printf ("Visit site (%s)", + g_match_info_fetch (found_hostname, 1)); + + g_match_info_free (found_hostname); + g_regex_unref (hostname); + + return result; + } + else + { + launch_browser (post->url); + return NULL; + } +} + +static gchar* +read_comments_cb (GRedditPost *post, gboolean prepare) +{ + if (prepare) + { + return g_strdup_printf ("Read comments (%d)", + post->comment_count); + } + else + { + gchar *comments_url = g_strdup_printf ("http://www.reddit.com/comments/%s/", + post->suffix); + + launch_browser (comments_url); + + g_free (comments_url); + + return NULL; + } +} + +typedef gchar* (*OptionCallback) (GRedditPost *, gboolean); + static void -visit_site (gpointer button, gpointer data) +options_selector (GtkButton *dummy, + GRedditPost *post) { - launch_browser ((char *) data); + OptionCallback callbacks[] = { + visit_site_cb, + read_comments_cb, + NULL + }; + OptionCallback *oc_cursor; + GtkWidget *dialog; + GtkWidget *selector; + GSList *options = NULL; + OptionCallback *target; + GList *rows; + GtkTreePath *path; + gint *indices; + + dialog = hildon_picker_dialog_new (GTK_WINDOW (window)); + selector = hildon_touch_selector_new_text (); + gtk_window_set_title (GTK_WINDOW (dialog), post->title); + + for (oc_cursor = callbacks; *oc_cursor; oc_cursor++) + { + char *title = (*oc_cursor) (post, TRUE); + + if (title) + { + hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector), + title); + options = g_slist_prepend (options, oc_cursor); + } + } + + hildon_picker_dialog_set_selector (HILDON_PICKER_DIALOG (dialog), + HILDON_TOUCH_SELECTOR (selector)); + + gtk_widget_show_all (GTK_WIDGET (GTK_DIALOG (dialog)->vbox)); + + if (gtk_dialog_run (GTK_DIALOG (dialog))!=GTK_RESPONSE_OK) + { + gtk_widget_destroy (GTK_WIDGET (dialog)); + g_slist_free (options); + return; + } + + options = g_slist_reverse (options); + + rows = hildon_touch_selector_get_selected_rows (HILDON_TOUCH_SELECTOR (selector), + 0); + path = (GtkTreePath*) rows->data; + indices = gtk_tree_path_get_indices (path); + + target = (OptionCallback*) g_slist_nth_data (options, *indices); + + (*target) (post, FALSE); + + g_slist_free (options); + + gtk_widget_destroy (GTK_WIDGET (dialog)); } static void @@ -116,7 +225,7 @@ HILDON_BUTTON_ARRANGEMENT_VERTICAL, title, subtitle); - g_signal_connect (button, "clicked", G_CALLBACK (visit_site), post->url); + g_signal_connect (button, "clicked", G_CALLBACK (options_selector), post); gtk_box_pack_end (GTK_BOX (vbox), button, FALSE, FALSE, 0);