/soc/2015/igor.gajowiak/chatlog: 6333c6948398: Implemented showi...

Igor Gajowiak igor.gajowiak at gmail.com
Thu Jul 30 12:11:22 EDT 2015


Changeset: 6333c694839876466ac91d63a91eb6febf552389
Author:	 Igor Gajowiak <igor.gajowiak at gmail.com>
Date:	 2015-07-30 18:11 +0200
Branch:	 default
URL: https://hg.pidgin.im/soc/2015/igor.gajowiak/chatlog/rev/6333c6948398

Description:

Implemented showing days in the log.

diffstat:

 pidgin/gtkgenericlog.c |  178 +++++++++++++++++++++++++++++++++++++-----------
 pidgin/gtkgenericlog.h |    8 +-
 2 files changed, 142 insertions(+), 44 deletions(-)

diffs (truncated from 321 to 300 lines):

diff --git a/pidgin/gtkgenericlog.c b/pidgin/gtkgenericlog.c
--- a/pidgin/gtkgenericlog.c
+++ b/pidgin/gtkgenericlog.c
@@ -19,7 +19,8 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
  */
 
-#include "blistnodetypes.h"
+#include <sys/time.h>
+
 #include "buddylist.h"
 #include "genericlog.h"
 #include "gtkgenericlog.h"
@@ -227,9 +228,40 @@ on_buddylist_row_activated(GtkTreeView *
 }
 
 static void
+populate_calendar_store(PurpleBuddy *buddy, GtkTreeStore *store);
+
+static void
 on_buddylist_row_changed(GtkTreeSelection *sel, gpointer data)
 {
-	printf("on_buddylist_row_changed\n");
+	g_assert(data);
+
+	PidginGenericLogViewer *viewer = (PidginGenericLogViewer*) data;
+
+	g_object_unref(G_OBJECT(viewer->current_buddy));
+	viewer->current_buddy = NULL;
+
+	GtkTreeModel *model;
+	GtkTreeIter row;
+
+	/* No buddy selected */
+	if (!gtk_tree_selection_get_selected(sel, &model, &row)) {
+		gtk_tree_store_clear(viewer->calendar_tree_store);
+		return;
+	}
+
+	gpointer buddy = NULL;
+	gtk_tree_model_get(model, &row, BLIST_TREE_STORE_COL_BUDDY, &buddy, -1);
+
+	/* Group row activated */
+	if (!buddy) {
+		gtk_tree_store_clear(viewer->calendar_tree_store);
+		return;
+	}
+
+	g_object_ref(buddy);
+	viewer->current_buddy = buddy;
+
+	populate_calendar_store((PurpleBuddy*) buddy, viewer->calendar_tree_store);
 }
 
 static void
@@ -259,20 +291,28 @@ append_buddy(GtkTreeStore *store, GHashT
 		BLIST_TREE_STORE_COL_BUDDY, buddy, -1);
 }
 
-static GtkTreeModel *
-create_buddylist_model(void)
+static GtkTreeStore *
+create_buddylist_store(void)
 {
+	// Stores (group_name, NULL) or (buddy_name, buddy)
+	return gtk_tree_store_new(BLIST_TREE_STORE_COLUMNS,
+		G_TYPE_STRING, G_TYPE_OBJECT);
+}
+
+static void
+populate_buddylist_store(GtkTreeStore *store)
+{
+	g_assert(store);
+
+	gtk_tree_store_clear(store);
+
 	GList *buddies;
 	if (!purple_genericlog_get_all_buddies(&buddies, NULL)) {
 		purple_debug_error(GTKGENERICLOG_DEBUG_CATEGORY,
 			"Getting buddies failed");
-		buddies = NULL;
+		return;
 	}
 
-	// Stores (group_name, NULL) or (buddy_name, buddy)
-	GtkTreeStore *store = gtk_tree_store_new(BLIST_TREE_STORE_COLUMNS,
-		G_TYPE_STRING, G_TYPE_OBJECT);
-
 	// Maps PurpleGroup* -> GtkTreeIter*
 	GHashTable *branches = g_hash_table_new_full(g_direct_hash, g_direct_equal,
 		g_object_unref, g_free);
@@ -283,17 +323,15 @@ create_buddylist_model(void)
 
 	g_list_free_full(buddies, g_object_unref);
 	g_hash_table_destroy(branches);
-
-	return GTK_TREE_MODEL(store);
 }
 
 static GtkWidget *
-create_buddylist_view(GtkTreeModel *model, PidginGenericLogViewer *viewer)
+create_buddylist_view(GtkTreeStore *store, PidginGenericLogViewer *viewer)
 {
-	g_assert(model);
+	g_assert(store);
 	g_assert(viewer);
 
-	GtkWidget *view = gtk_tree_view_new_with_model(model);
+	GtkWidget *view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
 	GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
 
 	gtk_tree_view_insert_column_with_attributes(
@@ -317,6 +355,12 @@ create_buddylist_view(GtkTreeModel *mode
 /* Calendar view                                                              */
 /******************************************************************************/
 
+typedef enum
+{
+	CALENDAR_TREE_STORE_COL_DATESTRING = 0,
+	CALENDAR_TREE_STORE_COL_DATE       = 1,
+} CalendarTreeStoreColumn;
+
 static void
 on_calendar_row_activated(GtkTreeView *tv, GtkTreePath *path,
 	GtkTreeViewColumn *col, gpointer data)
@@ -341,36 +385,85 @@ popup_calendar_button_pressed(GtkWidget 
 	return FALSE;
 }
 
-static GtkTreeModel *
-create_calendar_model(void)
+static GtkTreeStore *
+create_calendar_store(void)
 {
-	// TODO: Fill the store with real values
+	return gtk_tree_store_new(2, G_TYPE_STRING, G_TYPE_UINT64);
+}
 
-	GtkTreeStore *store = gtk_tree_store_new(1, G_TYPE_STRING);
-	GtkTreeIter iter;
+static void
+append_new_calendar_group(GtkTreeStore *store, GtkTreeIter *group,
+	const struct tm *time)
+{
+	g_assert(store);
+	g_assert(group);
+	g_assert(time);
 
-	gtk_tree_store_append(store, &iter, NULL);
-	gtk_tree_store_set(store, &iter, 0, "Entry 1", -1);
+	gtk_tree_store_append(store, group, NULL);
 
-	gtk_tree_store_append(store, &iter, &iter);
-	gtk_tree_store_set(store, &iter, 0, "Entry 2", -1);
+	gtk_tree_store_set(store, group,
+		CALENDAR_TREE_STORE_COL_DATESTRING, purple_utf8_strftime(
+			"%Y %b", time),
+		CALENDAR_TREE_STORE_COL_DATE, 0, -1);
+}
 
-	gtk_tree_store_append(store, &iter, NULL);
-	gtk_tree_store_set(store, &iter, 0, "Entry 3", -1);
+static void
+populate_calendar_store(PurpleBuddy *buddy, GtkTreeStore *store)
+{
+	g_assert(buddy);
+	g_assert(store);
 
-	GtkTreeModel *model = GTK_TREE_MODEL(store);
+	gtk_tree_store_clear(store);
 
-	return model;
+	GArray *days;
+	if (!purple_genericlog_get_all_days(buddy, &days, NULL)) {
+		purple_debug_error(GTKGENERICLOG_DEBUG_CATEGORY,
+			"Getting days failed");
+		return;
+	}
+
+	if (days->len == 0) {
+		g_array_unref(days);
+		return;
+	}
+
+	time_t timestamp = (time_t) g_array_index(days, guint64, 0);
+
+	GtkTreeIter curr_group;
+	struct tm curr_group_time = *localtime(&timestamp);
+	append_new_calendar_group(store, &curr_group, &curr_group_time);
+
+	for (guint i = 0; i < days->len; ++i) {
+		timestamp = (time_t) g_array_index(days, guint64, i);
+
+		/* Start a new group if year or month changed */
+		struct tm *curr_time = localtime(&timestamp);
+		if (curr_time->tm_year != curr_group_time.tm_year ||
+			curr_time->tm_mon != curr_group_time.tm_mon) {
+			curr_group_time = *curr_time;
+			append_new_calendar_group(store, &curr_group, &curr_group_time);
+		}
+
+		/* Append a new day under current group */
+		GtkTreeIter day_iter;
+		gtk_tree_store_append(store, &day_iter, &curr_group);
+		gtk_tree_store_set(store, &day_iter,
+			CALENDAR_TREE_STORE_COL_DATESTRING, purple_utf8_strftime(
+				"%a %d", curr_time),
+			CALENDAR_TREE_STORE_COL_DATE, (guint64) timestamp, -1);
+	}
+
+	g_array_unref(days);
 }
 
 static GtkWidget *
-create_calendar_view(GtkTreeModel *model, PidginGenericLogViewer *viewer)
+create_calendar_view(GtkTreeStore *store, PidginGenericLogViewer *viewer)
 {
-	g_assert(model);
+	g_assert(store);
 	g_assert(viewer);
 
 	GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
-	GtkWidget *view = gtk_tree_view_new_with_model(model);
+	GtkWidget *view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
 
 	gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW (view),
 		-1, "Calendar", renderer, "text", 0, NULL);
@@ -416,14 +509,14 @@ on_close_response(GtkWidget *widget, gin
 
 static GtkWidget *
 create_window(PidginGenericLogViewer *viewer, GtkWidget **buddylist_view_res,
-	GtkTreeModel **buddylist_model_res, GtkWidget **calendar_view_res,
-	GtkTreeModel **calendar_model_res, GtkWidget **webview_res)
+	GtkTreeStore **buddylist_store_res, GtkWidget **calendar_view_res,
+	GtkTreeStore **calendar_store_res, GtkWidget **webview_res)
 {
 	g_assert(viewer);
 	g_assert(buddylist_view_res);
-	g_assert(buddylist_model_res);
+	g_assert(buddylist_store_res);
 	g_assert(calendar_view_res);
-	g_assert(calendar_model_res);
+	g_assert(calendar_store_res);
 	g_assert(webview_res);
 
 	GtkWidget *window = gtk_dialog_new_with_buttons(
@@ -447,8 +540,9 @@ create_window(PidginGenericLogViewer *vi
 	gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(window))),
 		horizontal_pane, TRUE, TRUE, PIDGIN_HIG_BOX_SPACE);
 
-	GtkTreeModel *buddylist_model = create_buddylist_model();
-	GtkWidget *buddylist_view = create_buddylist_view(buddylist_model, viewer);
+	GtkTreeStore *buddylist_store = create_buddylist_store();
+	populate_buddylist_store(buddylist_store);
+	GtkWidget *buddylist_view = create_buddylist_view(buddylist_store, viewer);
 	gtk_paned_add1(GTK_PANED(horizontal_pane), pidgin_make_scrollable(
 		buddylist_view, GTK_POLICY_NEVER, GTK_POLICY_ALWAYS, GTK_SHADOW_IN, -1,
 		-1));
@@ -456,8 +550,8 @@ create_window(PidginGenericLogViewer *vi
 	GtkWidget *vertical_pane = gtk_paned_new(GTK_ORIENTATION_VERTICAL);
 	gtk_paned_add2(GTK_PANED(horizontal_pane), vertical_pane);
 
-	GtkTreeModel *calendar_model = create_calendar_model();
-	GtkWidget *calendar_view = create_calendar_view(calendar_model, viewer);
+	GtkTreeStore *calendar_store = create_calendar_store();
+	GtkWidget *calendar_view = create_calendar_view(calendar_store, viewer);
 	gtk_paned_add1(GTK_PANED(vertical_pane), pidgin_make_scrollable(
 		calendar_view, GTK_POLICY_NEVER, GTK_POLICY_ALWAYS, GTK_SHADOW_IN, -1,
 		-1));
@@ -467,10 +561,10 @@ create_window(PidginGenericLogViewer *vi
 	gtk_paned_add2(GTK_PANED(vertical_pane), frame);
 
 	*buddylist_view_res = buddylist_view;
-	*buddylist_model_res = buddylist_model;
+	*buddylist_store_res = buddylist_store;
 
 	*calendar_view_res = calendar_view;
-	*calendar_model_res = calendar_model;
+	*calendar_store_res = calendar_store;
 
 	*webview_res = webview;
 
@@ -483,8 +577,8 @@ create_genericlog_viewer()
 	PidginGenericLogViewer *viewer = g_new0(PidginGenericLogViewer, 1);
 
 	viewer->window = create_window(viewer,
-		&viewer->blist_tree_view, &viewer->blist_tree_model,
-		&viewer->calendar_tree_view, &viewer->calendar_tree_model,
+		&viewer->blist_tree_view, &viewer->blist_tree_store,
+		&viewer->calendar_tree_view, &viewer->calendar_tree_store,
 		&viewer->webview);
 
 	return viewer;
diff --git a/pidgin/gtkgenericlog.h b/pidgin/gtkgenericlog.h
--- a/pidgin/gtkgenericlog.h
+++ b/pidgin/gtkgenericlog.h
@@ -24,18 +24,22 @@
 
 #include <gtk/gtk.h>
 



More information about the Commits mailing list