diary r96 - in trunk: . data src



Author: pwithnall
Date: Wed Oct 22 22:52:28 2008
New Revision: 96
URL: http://svn.gnome.org/viewvc/diary?rev=96&view=rev

Log:
2008-10-22  Philip Withnall  <philip tecnocode co uk>

	* data/almanah.ui: Fix one missing rename from the previous 
commit.
	* src/entry.c (almanah_entry_get_content):
	* src/search-dialog.c (sd_search_button_clicked_cb):
	* src/storage-manager.c 
(almanah_storage_manager_search_entries):
	* src/storage-manager.h: Rewrite the method to search for an 
entry
	so that it now works with the new serialised entry format.



Modified:
   trunk/ChangeLog
   trunk/data/almanah.ui
   trunk/src/entry.c
   trunk/src/search-dialog.c
   trunk/src/storage-manager.c
   trunk/src/storage-manager.h

Modified: trunk/data/almanah.ui
==============================================================================
--- trunk/data/almanah.ui	(original)
+++ trunk/data/almanah.ui	Wed Oct 22 22:52:28 2008
@@ -259,7 +259,7 @@
 										<property name="width-request">200</property>
 										<property name="can-focus">True</property>
 										<signal name="day-selected" handler="mw_calendar_day_selected_cb"/>
-										<signal name="month-changed" handler="diary_calendar_month_changed_cb"/>
+										<signal name="month-changed" handler="almanah_calendar_month_changed_cb"/>
 									</object>
 									<packing>
 										<property name="expand">False</property>

Modified: trunk/src/entry.c
==============================================================================
--- trunk/src/entry.c	(original)
+++ trunk/src/entry.c	Wed Oct 22 22:52:28 2008
@@ -179,6 +179,7 @@
 	GError *deserialise_error = NULL;
 
 	format_atom = gtk_text_buffer_register_deserialize_tagset (text_buffer, PACKAGE_NAME);
+	gtk_text_buffer_deserialize_set_can_create_tags (text_buffer, format_atom, TRUE);
 	gtk_text_buffer_get_start_iter (text_buffer, &start_iter);
 
 	/* Try deserializing the (hopefully) serialized data first */

Modified: trunk/src/search-dialog.c
==============================================================================
--- trunk/src/search-dialog.c	(original)
+++ trunk/src/search-dialog.c	Wed Oct 22 22:52:28 2008
@@ -140,7 +140,8 @@
 sd_search_button_clicked_cb (GtkButton *self, AlmanahSearchDialog *search_dialog)
 {
 	GDate *results;
-	guint result_count, i;
+	gint result_count;
+	guint i;
 	GtkTreeIter iter;
 	AlmanahSearchDialogPrivate *priv = search_dialog->priv;
 

Modified: trunk/src/storage-manager.c
==============================================================================
--- trunk/src/storage-manager.c	(original)
+++ trunk/src/storage-manager.c	Wed Oct 22 22:52:28 2008
@@ -785,41 +785,58 @@
  * If there are no results, @matches will be set to %NULL. It
  * must otherwise be freed with g_free().
  *
- * Return value: number of results
+ * Return value: number of results, or -1 on failure
  **/
-guint
+gint
 almanah_storage_manager_search_entries (AlmanahStorageManager *self, const gchar *search_string, GDate *matches[])
 {
-	AlmanahQueryResults *results;
-	guint i;
-
-	/* TODO: Won't really work with serialized data */
-	results = almanah_storage_manager_query (self, "SELECT day, month, year FROM entries WHERE content LIKE '%%%q%%'", NULL,
-						 search_string);
-
-	*matches = NULL;
-	if (results == NULL)
-		return 0;
-
-	/* No results? */
-	if (results->rows < 1) {
-		almanah_storage_manager_free_results (results);
-		return 0;
+	sqlite3_stmt *statement;
+	GtkTextBuffer *text_buffer;
+	guint result_count = 1; /* initialise to 1 to account for the working array element */
+
+	/* Prepare the statement */
+	if (sqlite3_prepare_v2 (self->priv->connection,
+				"SELECT content, day, month, year FROM entries", -1,
+				&statement, NULL) != SQLITE_OK) {
+		return -1;
 	}
 
-	/* Allocate and set the results */
-	*matches = g_new0 (GDate, results->rows);
+	text_buffer = gtk_text_buffer_new (NULL);
+	*matches = g_malloc (sizeof (GDate));
 
-	for (i = 0; i < results->rows; i++) {
-		g_date_set_dmy (&((*matches)[i]),
-				(GDateDay) atoi (results->data[(i + 1) * results->columns]),
-				(GDateMonth) atoi (results->data[(i + 1) * results->columns + 1]),
-				(GDateYear) atoi (results->data[(i + 1) * results->columns + 2]));
+	/* Execute the statement */
+	while (sqlite3_step (statement) == SQLITE_ROW) {
+		AlmanahEntry *entry;
+		GDate *date = &((*matches)[result_count - 1]);
+		GtkTextIter iter;
+
+		g_date_set_dmy (date, sqlite3_column_int (statement, 1), sqlite3_column_int (statement, 2), sqlite3_column_int (statement, 3));
+		entry = almanah_entry_new (date);
+		almanah_entry_set_data (entry, sqlite3_column_blob (statement, 0), sqlite3_column_bytes (statement, 0));
+
+		/* Deserialise the entry into our buffer */
+		gtk_text_buffer_set_text (text_buffer, "", 0);
+		if (almanah_entry_get_content (entry, text_buffer, NULL) == FALSE) {
+			g_object_unref (entry);
+			continue;
+		}
+
+		/* Perform the search */
+		gtk_text_buffer_get_start_iter (text_buffer, &iter);
+		if (gtk_text_iter_forward_search (&iter, search_string, GTK_TEXT_SEARCH_VISIBLE_ONLY | GTK_TEXT_SEARCH_TEXT_ONLY, NULL, NULL, NULL) == TRUE) {
+			/* A match was found, so move to the next working array element
+			 * (effectively add the date to the results list, by preventing it being overwritten). */
+			*matches = g_realloc (*matches, ++result_count * sizeof (GDate));
+		}
+
+		/* Free stuff up and continue */
+		g_object_unref (entry);
 	}
 
-	almanah_storage_manager_free_results (results);
+	sqlite3_finalize (statement);
+	g_object_unref (text_buffer);
 
-	return i;
+	return result_count - 1;
 }
 
 /* NOTE: Free results with g_slice_free */

Modified: trunk/src/storage-manager.h
==============================================================================
--- trunk/src/storage-manager.h	(original)
+++ trunk/src/storage-manager.h	Wed Oct 22 22:52:28 2008
@@ -81,7 +81,7 @@
 gboolean almanah_storage_manager_entry_exists (AlmanahStorageManager *self, GDate *date);
 AlmanahEntry *almanah_storage_manager_get_entry (AlmanahStorageManager *self, GDate *date);
 gboolean almanah_storage_manager_set_entry (AlmanahStorageManager *self, AlmanahEntry *entry);
-guint almanah_storage_manager_search_entries (AlmanahStorageManager *self, const gchar *search_string, GDate *matches[]);
+gint almanah_storage_manager_search_entries (AlmanahStorageManager *self, const gchar *search_string, GDate *matches[]);
 
 gboolean *almanah_storage_manager_get_month_marked_days (AlmanahStorageManager *self, GDateYear year, GDateMonth month);
 



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