[almanah] Highlight important entries in search results



commit 422ab587d117c2ad75cc3061479fa4d96849438a
Author: Philip Withnall <philip tecnocode co uk>
Date:   Sun Apr 18 15:25:49 2010 +0100

    Highlight important entries in search results

 data/almanah.ui       |   17 ++++++++++++++---
 src/search-dialog.c   |   31 ++++++++++++++++++-------------
 src/storage-manager.c |   40 ++++++++++++++++++----------------------
 src/storage-manager.h |    4 ++--
 4 files changed, 52 insertions(+), 40 deletions(-)
---
diff --git a/data/almanah.ui b/data/almanah.ui
index 2e75900..5eb2388 100644
--- a/data/almanah.ui
+++ b/data/almanah.ui
@@ -152,7 +152,7 @@
 						<property name="name">format-important</property>
 						<property name="label" translatable="yes">I_mportant</property>
 						<property name="tooltip" translatable="yes">Toggle whether the current entry is marked as important.</property>
-						<property name="icon-name">gtk-info</property>
+						<property name="icon-name">emblem-important</property>
 						<property name="is-important">True</property>
 						<signal name="activate" handler="mw_important_activate_cb"/>
 					</object>
@@ -575,6 +575,7 @@
 			<column type="guint"/><!-- Month -->
 			<column type="guint"/><!-- Year -->
 			<column type="gchararray"/><!-- Formatted date -->
+			<column type="gchararray"/><!-- Icon stock ID (for important entries) -->
 		</columns>
 	</object>
 
@@ -659,9 +660,19 @@
 											</object>
 										</child>
 										<child>
-											<object class="GtkTreeViewColumn" id="column0">
+											<object class="GtkTreeViewColumn" id="almanah_sd_results_column_icon">
 												<child>
-													<object class="GtkCellRendererText" id="renderer0"/>
+													<object class="GtkCellRendererPixbuf" id="almanah_sd_results_column_icon_renderer"/>
+													<attributes>
+														<attribute name="icon-name">4</attribute>
+													</attributes>
+												</child>
+											</object>
+										</child>
+										<child>
+											<object class="GtkTreeViewColumn" id="almanah_sd_results_column_date">
+												<child>
+													<object class="GtkCellRendererText" id="almanah_sd_results_column_date_renderer"/>
 													<attributes>
 														<attribute name="text">3</attribute>
 													</attributes>
diff --git a/src/search-dialog.c b/src/search-dialog.c
index 9c214a6..ddcd6db 100644
--- a/src/search-dialog.c
+++ b/src/search-dialog.c
@@ -140,32 +140,37 @@ sd_response_cb (GtkDialog *dialog, gint response_id, AlmanahSearchDialog *search
 void
 sd_search_button_clicked_cb (GtkButton *self, AlmanahSearchDialog *search_dialog)
 {
-	GDate *results;
-	gint result_count;
-	gint i;
+	GSList *results, *i;
 	GtkTreeIter iter;
 	AlmanahSearchDialogPrivate *priv = search_dialog->priv;
 
 	gtk_list_store_clear (search_dialog->priv->sd_results_store);
-	result_count = almanah_storage_manager_search_entries (almanah->storage_manager,
-							       gtk_entry_get_text (priv->sd_search_entry), &results);
+	results = almanah_storage_manager_search_entries (almanah->storage_manager, gtk_entry_get_text (priv->sd_search_entry));
 
-	for (i = 0; i < result_count; i++) {
+	for (i = results; i != NULL; i = i->next) {
+		AlmanahEntry *entry;
+		GDate date;
 		gchar formatted_date[100];
 
+		entry = ALMANAH_ENTRY (i->data);
+		almanah_entry_get_date (entry, &date);
+
 		/* Translators: This is a strftime()-format string for the dates displayed in search results. */
-		g_date_strftime (formatted_date, sizeof (formatted_date), _("%A, %e %B %Y"), &results[i]);
+		g_date_strftime (formatted_date, sizeof (formatted_date), _("%A, %e %B %Y"), &date);
 
 		gtk_list_store_append (priv->sd_results_store, &iter);
 		gtk_list_store_set (priv->sd_results_store, &iter,
-				    0, g_date_get_day (&results[i]),
-				    1, g_date_get_month (&results[i]),
-				    2, g_date_get_year (&results[i]),
-				    3, &formatted_date,
-				    -1);
+		                    0, g_date_get_day (&date),
+		                    1, g_date_get_month (&date),
+		                    2, g_date_get_year (&date),
+		                    3, &formatted_date,
+		                    4, (almanah_entry_is_important (entry) == TRUE) ? "emblem-important" : NULL,
+		                    -1);
+
+		g_object_unref (entry);
 	}
 
-	g_free (results);
+	g_slist_free (results);
 }
 
 static void
diff --git a/src/storage-manager.c b/src/storage-manager.c
index 8706786..f012394 100644
--- a/src/storage-manager.c
+++ b/src/storage-manager.c
@@ -859,42 +859,38 @@ almanah_storage_manager_set_entry (AlmanahStorageManager *self, AlmanahEntry *en
  * almanah_storage_manager_search_entries:
  * @self: an #AlmanahStorageManager
  * @search_string: string for which to search in entry content
- * @matches: return location for the results
  *
- * Searches for @search_string in the content in entries in the
- * database, and returns a the number of results. The results
- * themselves are returned in @matches as an array of #GDates.
+ * Searches for @search_string in the content in entries in the database, and returns the results. If there are no results, %NULL will be returned.
  *
- * If there are no results, @matches will be set to %NULL. It
- * must otherwise be freed with g_free().
+ * The results are returned in descending date order.
  *
- * Return value: number of results, or -1 on failure
+ * Return value: a #GSList of #AlmanahEntry<!-- -->s, or %NULL; unref elements with g_object_unref(); free list with g_slist_free()
  **/
-gint
-almanah_storage_manager_search_entries (AlmanahStorageManager *self, const gchar *search_string, GDate *matches[])
+GSList *
+almanah_storage_manager_search_entries (AlmanahStorageManager *self, const gchar *search_string)
 {
 	sqlite3_stmt *statement;
 	GtkTextBuffer *text_buffer;
-	guint result_count = 1; /* initialise to 1 to account for the working array element */
+	GSList *matches = NULL;
 
-	/* Prepare the statement */
+	/* Prepare the statement. Query in ascending date order, and then reverse the results by prepending them to the list as we build it,
+	 * rather than appending them. */
 	if (sqlite3_prepare_v2 (self->priv->connection,
-				"SELECT content, day, month, year, is_important FROM entries ORDER BY year DESC, month DESC, day DESC", -1,
+				"SELECT content, day, month, year, is_important FROM entries ORDER BY year ASC, month ASC, day ASC", -1,
 				&statement, NULL) != SQLITE_OK) {
-		return -1;
+		return NULL;
 	}
 
 	text_buffer = gtk_text_buffer_new (NULL);
-	*matches = g_malloc (sizeof (GDate));
 
 	/* Execute the statement */
 	while (sqlite3_step (statement) == SQLITE_ROW) {
 		AlmanahEntry *entry;
-		GDate *date = &((*matches)[result_count - 1]);
+		GDate date;
 		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);
+		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));
 		almanah_entry_set_is_important (entry, (sqlite3_column_int (statement, 4) == 1) ? TRUE : FALSE);
 
@@ -908,10 +904,10 @@ almanah_storage_manager_search_entries (AlmanahStorageManager *self, const gchar
 
 		/* 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));
+		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 */
+			matches = g_slist_prepend (matches, g_object_ref (entry));
 		}
 
 		/* Free stuff up and continue */
@@ -921,7 +917,7 @@ almanah_storage_manager_search_entries (AlmanahStorageManager *self, const gchar
 	sqlite3_finalize (statement);
 	g_object_unref (text_buffer);
 
-	return result_count - 1;
+	return matches;
 }
 
 /**
diff --git a/src/storage-manager.h b/src/storage-manager.h
index e572fa3..46a770d 100644
--- a/src/storage-manager.h
+++ b/src/storage-manager.h
@@ -82,8 +82,8 @@ gboolean almanah_storage_manager_get_statistics (AlmanahStorageManager *self, gu
 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);
-gint almanah_storage_manager_search_entries (AlmanahStorageManager *self, const gchar *search_string, GDate *matches[]);
-GSList *almanah_storage_manager_get_entries (AlmanahStorageManager *self) G_GNUC_WARN_UNUSED_RESULT;
+GSList *almanah_storage_manager_search_entries (AlmanahStorageManager *self, const gchar *search_string) G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
+GSList *almanah_storage_manager_get_entries (AlmanahStorageManager *self) G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
 
 gboolean *almanah_storage_manager_get_month_marked_days (AlmanahStorageManager *self, GDateYear year, GDateMonth month, guint *num_days);
 gboolean *almanah_storage_manager_get_month_important_days (AlmanahStorageManager *self, GDateYear year, GDateMonth month, guint *num_days);



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