33
32
/* GtkBuilder callbacks */
34
33
void sd_search_button_clicked_cb (GtkButton *self, AlmanahSearchDialog *search_dialog);
35
void sd_results_tree_view_row_activated_cb (GtkTreeView *self, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data);
34
void sd_cancel_button_clicked_cb (GtkButton *self, AlmanahSearchDialog *search_dialog);
35
void sd_results_tree_view_row_activated_cb (GtkTreeView *tree_view, GtkTreePath *path, GtkTreeViewColumn *column, AlmanahSearchDialog *self);
36
36
void sd_view_button_clicked_cb (GtkButton *self, AlmanahSearchDialog *search_dialog);
38
static void sd_search_progress_cb (AlmanahStorageManager *storage_manager, AlmanahEntry *entry, AlmanahSearchDialog **search_dialog_weak_pointer);
39
static void sd_search_ready_cb (AlmanahStorageManager *storage_manager, GAsyncResult *res, AlmanahSearchDialog **search_dialog_weak_pointer);
38
41
struct _AlmanahSearchDialogPrivate {
39
42
GtkEntry *sd_search_entry;
43
GtkWidget *sd_search_button;
44
GtkWidget *sd_cancel_button;
45
GtkSpinner *sd_search_spinner;
46
GtkLabel *sd_results_label;
47
GtkWidget *sd_results_alignment;
40
48
GtkListStore *sd_results_store;
41
49
GtkTreeSelection *sd_results_selection;
50
GCancellable *sd_cancellable;
44
53
G_DEFINE_TYPE (AlmanahSearchDialog, almanah_search_dialog, GTK_TYPE_DIALOG)
130
148
sd_response_cb (GtkDialog *dialog, gint response_id, AlmanahSearchDialog *search_dialog)
132
150
/* Ensure everything's tidy before we leave the room */
151
if (search_dialog->priv->sd_cancellable != NULL) {
152
g_cancellable_cancel (search_dialog->priv->sd_cancellable);
133
155
gtk_list_store_clear (search_dialog->priv->sd_results_store);
134
156
gtk_entry_set_text (search_dialog->priv->sd_search_entry, "");
136
158
gtk_widget_hide (GTK_WIDGET (dialog));
162
sd_search_progress_cb (AlmanahStorageManager *storage_manager, AlmanahEntry *entry, AlmanahSearchDialog **search_dialog_weak_pointer)
164
AlmanahSearchDialogPrivate *priv;
166
gchar formatted_date[100];
167
GtkTreeIter tree_iter;
169
if (*search_dialog_weak_pointer == NULL) {
170
/* The dialogue's been finalised already, so we'd better just return */
174
g_return_if_fail (ALMANAH_IS_STORAGE_MANAGER (storage_manager));
175
g_return_if_fail (ALMANAH_IS_ENTRY (entry));
176
g_return_if_fail (ALMANAH_IS_SEARCH_DIALOG (*search_dialog_weak_pointer));
178
priv = ALMANAH_SEARCH_DIALOG (*search_dialog_weak_pointer)->priv;
180
almanah_entry_get_date (entry, &date);
181
/* Translators: This is a strftime()-format string for the dates displayed in search results. */
182
g_date_strftime (formatted_date, sizeof (formatted_date), _("%A, %e %B %Y"), &date);
184
gtk_list_store_append (priv->sd_results_store, &tree_iter);
185
gtk_list_store_set (priv->sd_results_store, &tree_iter,
186
0, g_date_get_day (&date),
187
1, g_date_get_month (&date),
188
2, g_date_get_year (&date),
190
4, (almanah_entry_is_important (entry) == TRUE) ? "emblem-important" : NULL,
195
sd_search_ready_cb (AlmanahStorageManager *storage_manager, GAsyncResult *res, AlmanahSearchDialog **search_dialog_weak_pointer)
197
AlmanahSearchDialogPrivate *priv;
198
AlmanahSearchDialog *search_dialog;
200
GError *error = NULL;
202
/* Finish the operation */
203
counter = almanah_storage_manager_search_entries_async_finish (storage_manager, res, &error);
205
if (*search_dialog_weak_pointer == NULL) {
206
/* The dialogue's been finalised already, so we'd better just return */
207
g_clear_error (&error);
211
g_return_if_fail (ALMANAH_IS_SEARCH_DIALOG (*search_dialog_weak_pointer));
213
search_dialog = ALMANAH_SEARCH_DIALOG (*search_dialog_weak_pointer);
214
priv = search_dialog->priv;
216
/* Return the search result count to the user */
217
gtk_spinner_stop (priv->sd_search_spinner);
218
gtk_widget_hide (GTK_WIDGET (priv->sd_search_spinner));
219
gtk_widget_set_sensitive (priv->sd_cancel_button, FALSE);
220
gtk_widget_set_sensitive (priv->sd_search_button, TRUE);
222
if (error != NULL && g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED) == TRUE) {
223
gtk_label_set_text (priv->sd_results_label, _("Search canceled."));
224
} else if (error != NULL) {
225
/* Translators: This is an error message wrapper for when searches encounter an error. The placeholder is for an error message. */
226
gchar *error_message = g_strdup_printf (_("Error: %s"), error->message);
227
gtk_label_set_text (priv->sd_results_label, error_message);
228
g_free (error_message);
231
gchar *results_string = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE, "Found %d entry:", "Found %d entries:", counter), counter);
232
gtk_label_set_text (priv->sd_results_label, results_string);
233
g_free (results_string);
236
g_clear_error (&error);
239
g_object_remove_weak_pointer (G_OBJECT (search_dialog), (gpointer*) search_dialog_weak_pointer);
240
g_slice_free (AlmanahSearchDialog*, search_dialog_weak_pointer);
242
g_object_unref (priv->sd_cancellable);
243
search_dialog->priv->sd_cancellable = NULL;
247
sd_cancel_button_clicked_cb (GtkButton *self, AlmanahSearchDialog *search_dialog)
249
AlmanahSearchDialogPrivate *priv = search_dialog->priv;
251
if (priv->sd_cancellable != NULL) {
252
g_cancellable_cancel (priv->sd_cancellable);
140
257
sd_search_button_clicked_cb (GtkButton *self, AlmanahSearchDialog *search_dialog)
143
AlmanahStorageManagerIter iter;
259
AlmanahApplication *application;
260
AlmanahStorageManager *storage_manager;
144
261
AlmanahSearchDialogPrivate *priv = search_dialog->priv;
145
const gchar *search_string = gtk_entry_get_text (priv->sd_search_entry);
262
const gchar *search_string;
263
AlmanahSearchDialog **search_dialog_weak_pointer;
265
if (priv->sd_cancellable != NULL) {
267
g_assert_not_reached ();
147
271
/* Clear the results store of previous search results first */
148
gtk_list_store_clear (search_dialog->priv->sd_results_store);
150
/* Search over all entries */
151
almanah_storage_manager_iter_init (&iter);
152
while ((entry = almanah_storage_manager_search_entries (almanah->storage_manager, search_string, &iter)) != NULL) {
154
gchar formatted_date[100];
155
GtkTreeIter tree_iter;
157
almanah_entry_get_date (entry, &date);
159
/* Translators: This is a strftime()-format string for the dates displayed in search results. */
160
g_date_strftime (formatted_date, sizeof (formatted_date), _("%A, %e %B %Y"), &date);
162
gtk_list_store_append (priv->sd_results_store, &tree_iter);
163
gtk_list_store_set (priv->sd_results_store, &tree_iter,
164
0, g_date_get_day (&date),
165
1, g_date_get_month (&date),
166
2, g_date_get_year (&date),
168
4, (almanah_entry_is_important (entry) == TRUE) ? "emblem-important" : NULL,
171
g_object_unref (entry);
272
gtk_list_store_clear (priv->sd_results_store);
274
priv->sd_cancellable = g_cancellable_new ();
276
search_string = gtk_entry_get_text (priv->sd_search_entry);
278
/* Change UI to show the status */
279
gtk_widget_show (GTK_WIDGET (priv->sd_results_alignment));
280
gtk_label_set_text (priv->sd_results_label, _("Searching…"));
281
gtk_widget_show (GTK_WIDGET (priv->sd_search_spinner));
282
gtk_spinner_start (priv->sd_search_spinner);
284
/* Grab the storage manager */
285
application = ALMANAH_APPLICATION (gtk_window_get_application (GTK_WINDOW (search_dialog)));
286
storage_manager = almanah_application_dup_storage_manager (application);
288
/* Launch the search */
289
search_dialog_weak_pointer = g_slice_new (AlmanahSearchDialog*);
290
*search_dialog_weak_pointer = search_dialog;
291
g_object_add_weak_pointer (G_OBJECT (search_dialog), (gpointer*) search_dialog_weak_pointer);
293
almanah_storage_manager_search_entries_async (storage_manager, search_string, priv->sd_cancellable,
294
(AlmanahStorageManagerSearchCallback) sd_search_progress_cb,
295
(gpointer) search_dialog_weak_pointer, NULL,
296
(GAsyncReadyCallback) sd_search_ready_cb, (gpointer) search_dialog_weak_pointer);
298
/* Allow the user to cancel the search */
299
gtk_widget_set_sensitive (priv->sd_search_button, FALSE);
300
gtk_widget_set_sensitive (priv->sd_cancel_button, TRUE);
301
gtk_widget_grab_default (priv->sd_cancel_button);
303
g_object_unref (storage_manager);
176
select_date (GtkTreeModel *model, GtkTreeIter *iter)
307
select_date (AlmanahSearchDialog *self, GtkTreeModel *model, GtkTreeIter *iter)
309
AlmanahMainWindow *main_window;
178
310
guint day, month, year;