[almanah] Add more AlmanahStorageManager signals



commit ec720ae05db81e0dfca11e37ce365b64a12be152
Author: Philip Withnall <philip tecnocode co uk>
Date:   Sun May 9 17:06:14 2010 +0100

    Add more AlmanahStorageManager signals

 src/main-window.c      |    6 -----
 src/storage-manager.c  |   53 ++++++++++++++++++++++++++++++++++-------------
 src/widgets/calendar.c |   34 ++++++++++++++++++++++++++++++
 3 files changed, 72 insertions(+), 21 deletions(-)
---
diff --git a/src/main-window.c b/src/main-window.c
index 310e8e9..404a1f5 100644
--- a/src/main-window.c
+++ b/src/main-window.c
@@ -446,15 +446,9 @@ save_current_entry (AlmanahMainWindow *self)
 	/* Store the entry! */
 	almanah_storage_manager_set_entry (almanah->storage_manager, priv->current_entry);
 
-	/* Mark the day on the calendar if the entry was non-empty (and deleted)
-	 * and update the state of the add definition action. */
 	if (entry_is_empty == TRUE) {
-		gtk_calendar_unmark_day (GTK_CALENDAR (priv->calendar), g_date_get_day (&date));
-
 		/* Since the entry is empty, remove all the events from the treeview */
 		gtk_list_store_clear (priv->event_store);
-	} else {
-		gtk_calendar_mark_day (GTK_CALENDAR (priv->calendar), g_date_get_day (&date));
 	}
 }
 
diff --git a/src/storage-manager.c b/src/storage-manager.c
index c84d400..e728af9 100644
--- a/src/storage-manager.c
+++ b/src/storage-manager.c
@@ -54,6 +54,9 @@ enum {
 
 enum {
 	SIGNAL_DISCONNECTED,
+	SIGNAL_ENTRY_ADDED,
+	SIGNAL_ENTRY_MODIFIED,
+	SIGNAL_ENTRY_REMOVED,
 	SIGNAL_DEFINITION_ADDED,
 	SIGNAL_DEFINITION_MODIFIED,
 	SIGNAL_DEFINITION_REMOVED,
@@ -94,6 +97,24 @@ almanah_storage_manager_class_init (AlmanahStorageManagerClass *klass)
 	                                                             0, NULL, NULL,
 	                                                             almanah_marshal_VOID__STRING_STRING,
 	                                                             G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_STRING);
+	storage_manager_signals[SIGNAL_ENTRY_ADDED] = g_signal_new ("entry-added",
+	                                                            G_TYPE_FROM_CLASS (klass),
+	                                                            G_SIGNAL_RUN_LAST,
+	                                                            0, NULL, NULL,
+	                                                            g_cclosure_marshal_VOID__OBJECT,
+	                                                            G_TYPE_NONE, 1, ALMANAH_TYPE_ENTRY);
+	storage_manager_signals[SIGNAL_ENTRY_MODIFIED] = g_signal_new ("entry-modified",
+	                                                               G_TYPE_FROM_CLASS (klass),
+	                                                               G_SIGNAL_RUN_LAST,
+	                                                               0, NULL, NULL,
+	                                                               g_cclosure_marshal_VOID__OBJECT,
+	                                                               G_TYPE_NONE, 1, ALMANAH_TYPE_ENTRY);
+	storage_manager_signals[SIGNAL_ENTRY_REMOVED] = g_signal_new ("entry-removed",
+	                                                              G_TYPE_FROM_CLASS (klass),
+	                                                              G_SIGNAL_RUN_LAST,
+	                                                              0, NULL, NULL,
+	                                                              g_cclosure_marshal_VOID__BOXED,
+	                                                              G_TYPE_NONE, 1, G_TYPE_DATE);
 	storage_manager_signals[SIGNAL_DEFINITION_ADDED] = g_signal_new ("definition-added",
 	                                                                 G_TYPE_FROM_CLASS (klass),
 	                                                                 G_SIGNAL_RUN_LAST,
@@ -745,9 +766,6 @@ almanah_storage_manager_get_entry (AlmanahStorageManager *self, GDate *date)
 	AlmanahEntry *entry;
 	sqlite3_stmt *statement;
 
-	/* It's necessary to avoid our nice SQLite interface and use the sqlite3 API directly here as we can't otherwise reliably bind the data blob
-	 * to the query â?? if we pass it in as a string, it gets cut off at the first nul character, which could occur anywhere in the blob. */
-
 	/* Prepare the statement */
 	if (sqlite3_prepare_v2 (self->priv->connection,
 	                        "SELECT content, is_important, day, month, year, edited_day, edited_month, edited_year FROM entries "
@@ -795,19 +813,23 @@ almanah_storage_manager_set_entry (AlmanahStorageManager *self, AlmanahEntry *en
 
 	if (almanah_entry_is_empty (entry) == TRUE) {
 		/* Delete the entry */
-		return simple_query (self, "DELETE FROM entries WHERE year = %u AND month = %u AND day = %u", NULL,
-		                         g_date_get_year (&date),
-		                         g_date_get_month (&date),
-		                         g_date_get_day (&date));
+		gboolean success = simple_query (self, "DELETE FROM entries WHERE year = %u AND month = %u AND day = %u", NULL,
+		                                 g_date_get_year (&date),
+		                                 g_date_get_month (&date),
+		                                 g_date_get_day (&date));
+
+		/* Signal of the operation */
+		g_signal_emit (self, storage_manager_signals[SIGNAL_ENTRY_REMOVED], 0, &date);
+
+		return success;
 	} else {
 		const guint8 *data;
 		gsize length;
 		sqlite3_stmt *statement;
 		GDate last_edited;
+		gboolean existed_before;
 
-		/* It's necessary to avoid our nice SQLite interface and use the sqlite3 API directly here as we can't otherwise reliably bind the
-		 * data blob to the query â?? if we pass it in as a string, it gets cut off at the first nul character, which could occur anywhere in
-		 * the blob. */
+		existed_before = almanah_storage_manager_entry_exists (self, &date);
 
 		/* Prepare the statement */
 		if (sqlite3_prepare_v2 (self->priv->connection,
@@ -840,6 +862,12 @@ almanah_storage_manager_set_entry (AlmanahStorageManager *self, AlmanahEntry *en
 
 		sqlite3_finalize (statement);
 
+		/* Signal of the operation */
+		if (existed_before == TRUE)
+			g_signal_emit (self, storage_manager_signals[SIGNAL_ENTRY_MODIFIED], 0, entry);
+		else
+			g_signal_emit (self, storage_manager_signals[SIGNAL_ENTRY_ADDED], 0, entry);
+
 		return TRUE;
 	}
 }
@@ -994,9 +1022,6 @@ almanah_storage_manager_get_entries (AlmanahStorageManager *self, AlmanahStorage
 	g_return_val_if_fail (ALMANAH_IS_STORAGE_MANAGER (self), NULL);
 	g_return_val_if_fail (iter != NULL, NULL);
 
-	/* Just as with almanah_storage_manager_get_entry(), it's necessary to avoid our nice SQLite interface here. It's probably more efficient to
-	 * avoid it anyway. */
-
 	if (iter->finished == TRUE)
 		return NULL;
 
@@ -1143,8 +1168,6 @@ almanah_storage_manager_get_definitions (AlmanahStorageManager *self)
 	int result;
 	sqlite3_stmt *statement;
 
-	/* It's more efficient to avoid our nice SQLite interface and do things manually. */
-
 	/* Prepare the statement */
 	if (sqlite3_prepare_v2 (self->priv->connection,
 	                        "SELECT definition_type, definition_value, definition_value2, definition_text FROM definitions", -1,
diff --git a/src/widgets/calendar.c b/src/widgets/calendar.c
index 44a147d..d3d5636 100644
--- a/src/widgets/calendar.c
+++ b/src/widgets/calendar.c
@@ -27,6 +27,8 @@
 static void almanah_calendar_finalize (GObject *object);
 static void almanah_calendar_month_changed (GtkCalendar *calendar);
 static gchar *almanah_calendar_detail_func (GtkCalendar *calendar, guint year, guint month, guint day, gpointer user_data);
+static void entry_added_cb (AlmanahStorageManager *storage_manager, AlmanahEntry *entry, AlmanahCalendar *calendar);
+static void entry_removed_cb (AlmanahStorageManager *storage_manager, GDate *date, AlmanahCalendar *calendar);
 
 struct _AlmanahCalendarPrivate {
 	gboolean *important_days;
@@ -52,6 +54,10 @@ almanah_calendar_init (AlmanahCalendar *self)
 {
 	self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, ALMANAH_TYPE_CALENDAR, AlmanahCalendarPrivate);
 	gtk_calendar_set_detail_func (GTK_CALENDAR (self), almanah_calendar_detail_func, NULL, NULL);
+
+	/* Connect to signals from the storage manager so we can mark/unmark days as appropriate */
+	g_signal_connect (almanah->storage_manager, "entry-added", G_CALLBACK (entry_added_cb), self);
+	g_signal_connect (almanah->storage_manager, "entry-removed", G_CALLBACK (entry_removed_cb), self);
 }
 
 static void
@@ -112,6 +118,34 @@ almanah_calendar_detail_func (GtkCalendar *calendar, guint year, guint month, gu
 	return NULL;
 }
 
+static void
+entry_added_cb (AlmanahStorageManager *storage_manager, AlmanahEntry *entry, AlmanahCalendar *calendar)
+{
+	GDate date;
+	guint month;
+
+	almanah_entry_get_date (entry, &date);
+	gtk_calendar_get_date (GTK_CALENDAR (calendar), NULL, &month, NULL);
+
+	if (g_date_get_month (&date) == month + 1) {
+		/* Mark the entry on the calendar, since it's guaranteed to be non-empty */
+		gtk_calendar_mark_day (GTK_CALENDAR (calendar), g_date_get_day (&date));
+	}
+}
+
+static void
+entry_removed_cb (AlmanahStorageManager *storage_manager, GDate *date, AlmanahCalendar *calendar)
+{
+	guint month;
+
+	gtk_calendar_get_date (GTK_CALENDAR (calendar), NULL, &month, NULL);
+
+	if (g_date_get_month (date) == month + 1) {
+		/* Unmark the entry on the calendar */
+		gtk_calendar_unmark_day (GTK_CALENDAR (calendar), g_date_get_day (date));
+	}
+}
+
 GtkWidget *
 almanah_calendar_new (void)
 {



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]