~ubuntu-branches/ubuntu/raring/almanah/raring-proposed

« back to all changes in this revision

Viewing changes to src/search-dialog.c

  • Committer: Package Import Robot
  • Author(s): Angel Abad
  • Date: 2012-10-29 10:14:18 UTC
  • mfrom: (1.4.1) (10.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20121029101418-k2c27xb0aku22zeg
Tags: 0.10.0-1~exp1
* Imported Upstream version 0.10.0
* debian/control:
  - Bump evolution Build-Depends ( >=3.6.0)
  - debian/control: Depends on evolution-common (>= 3.6.0)
* Bump Standards-Version to 3.9.4 (no changes)

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 
25
25
#include "search-dialog.h"
26
26
#include "interface.h"
27
 
#include "main.h"
28
27
#include "main-window.h"
29
28
 
30
29
static void sd_response_cb (GtkDialog *dialog, gint response_id, AlmanahSearchDialog *search_dialog);
32
31
 
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);
37
37
 
 
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);
 
40
 
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;
42
51
};
43
52
 
44
53
G_DEFINE_TYPE (AlmanahSearchDialog, almanah_search_dialog, GTK_TYPE_DIALOG)
71
80
        const gchar *object_names[] = {
72
81
                "almanah_search_dialog",
73
82
                "almanah_sd_search_button_image",
 
83
                "almanah_sd_cancel_button_image",
74
84
                "almanah_sd_results_store",
 
85
                "almanah_ui_manager", /* HACK: work around bgo#672789 */
75
86
                NULL
76
87
        };
77
88
 
105
116
 
106
117
        priv = ALMANAH_SEARCH_DIALOG (search_dialog)->priv;
107
118
 
 
119
        priv->sd_cancellable = NULL;
 
120
 
108
121
        /* Grab our child widgets */
109
122
        priv->sd_search_entry = GTK_ENTRY (gtk_builder_get_object (builder, "almanah_sd_search_entry"));
110
123
        priv->sd_results_store = GTK_LIST_STORE (gtk_builder_get_object (builder, "almanah_sd_results_store"));
111
124
        priv->sd_results_selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (gtk_builder_get_object (builder, "almanah_sd_results_tree_view")));
 
125
        priv->sd_results_alignment = GTK_WIDGET (gtk_builder_get_object (builder, "almanah_sd_results_alignment"));
 
126
        priv->sd_search_spinner = GTK_SPINNER (gtk_builder_get_object (builder, "almanah_sd_search_spinner"));
 
127
        priv->sd_results_label = GTK_LABEL (gtk_builder_get_object (builder, "almanah_sd_results_label"));
 
128
        priv->sd_search_button = GTK_WIDGET (gtk_builder_get_object (builder, "almanah_sd_search_button"));
 
129
        priv->sd_cancel_button = GTK_WIDGET (gtk_builder_get_object (builder, "almanah_sd_cancel_button"));
112
130
 
113
131
        g_signal_connect (priv->sd_results_selection, "changed", G_CALLBACK (sd_results_selection_changed_cb),
114
132
                          gtk_builder_get_object (builder, "almanah_sd_view_button"));
115
133
 
116
 
        gtk_widget_grab_default (GTK_WIDGET (gtk_builder_get_object (builder, "almanah_sd_search_button")));
 
134
        gtk_widget_grab_default (priv->sd_search_button);
117
135
 
118
136
        g_object_unref (builder);
119
137
 
130
148
sd_response_cb (GtkDialog *dialog, gint response_id, AlmanahSearchDialog *search_dialog)
131
149
{
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);
 
153
        }
 
154
 
133
155
        gtk_list_store_clear (search_dialog->priv->sd_results_store);
134
156
        gtk_entry_set_text (search_dialog->priv->sd_search_entry, "");
135
157
 
136
158
        gtk_widget_hide (GTK_WIDGET (dialog));
137
159
}
138
160
 
 
161
static void
 
162
sd_search_progress_cb (AlmanahStorageManager *storage_manager, AlmanahEntry *entry, AlmanahSearchDialog **search_dialog_weak_pointer)
 
163
{
 
164
        AlmanahSearchDialogPrivate *priv;
 
165
        GDate date;
 
166
        gchar formatted_date[100];
 
167
        GtkTreeIter tree_iter;
 
168
 
 
169
        if (*search_dialog_weak_pointer == NULL) {
 
170
                /* The dialogue's been finalised already, so we'd better just return */
 
171
                return;
 
172
        }
 
173
 
 
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));
 
177
 
 
178
        priv = ALMANAH_SEARCH_DIALOG (*search_dialog_weak_pointer)->priv;
 
179
 
 
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);
 
183
 
 
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),
 
189
                            3, &formatted_date,
 
190
                            4, (almanah_entry_is_important (entry) == TRUE) ? "emblem-important" : NULL,
 
191
                            -1);
 
192
}
 
193
 
 
194
static void
 
195
sd_search_ready_cb (AlmanahStorageManager *storage_manager, GAsyncResult *res, AlmanahSearchDialog **search_dialog_weak_pointer)
 
196
{
 
197
        AlmanahSearchDialogPrivate *priv;
 
198
        AlmanahSearchDialog *search_dialog;
 
199
        gint counter;
 
200
        GError *error = NULL;
 
201
 
 
202
        /* Finish the operation */
 
203
        counter = almanah_storage_manager_search_entries_async_finish (storage_manager, res, &error);
 
204
 
 
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);
 
208
                return;
 
209
        }
 
210
 
 
211
        g_return_if_fail (ALMANAH_IS_SEARCH_DIALOG (*search_dialog_weak_pointer));
 
212
 
 
213
        search_dialog = ALMANAH_SEARCH_DIALOG (*search_dialog_weak_pointer);
 
214
        priv = search_dialog->priv;
 
215
 
 
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);
 
221
 
 
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);
 
229
        } else {
 
230
                /* Success! */
 
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);
 
234
        }
 
235
 
 
236
        g_clear_error (&error);
 
237
 
 
238
        /* Tidy up */
 
239
        g_object_remove_weak_pointer (G_OBJECT (search_dialog), (gpointer*) search_dialog_weak_pointer);
 
240
        g_slice_free (AlmanahSearchDialog*, search_dialog_weak_pointer);
 
241
 
 
242
        g_object_unref (priv->sd_cancellable);
 
243
        search_dialog->priv->sd_cancellable = NULL;
 
244
}
 
245
 
 
246
void
 
247
sd_cancel_button_clicked_cb (GtkButton *self, AlmanahSearchDialog *search_dialog)
 
248
{
 
249
        AlmanahSearchDialogPrivate *priv = search_dialog->priv;
 
250
 
 
251
        if (priv->sd_cancellable != NULL) {
 
252
                g_cancellable_cancel (priv->sd_cancellable);
 
253
        }
 
254
}
 
255
 
139
256
void
140
257
sd_search_button_clicked_cb (GtkButton *self, AlmanahSearchDialog *search_dialog)
141
258
{
142
 
        AlmanahEntry *entry;
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;
 
264
 
 
265
        if (priv->sd_cancellable != NULL) {
 
266
                // Already searching
 
267
                g_assert_not_reached ();
 
268
                return;
 
269
        }
146
270
 
147
271
        /* Clear the results store of previous search results first */
148
 
        gtk_list_store_clear (search_dialog->priv->sd_results_store);
149
 
 
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) {
153
 
                GDate date;
154
 
                gchar formatted_date[100];
155
 
                GtkTreeIter tree_iter;
156
 
 
157
 
                almanah_entry_get_date (entry, &date);
158
 
 
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);
161
 
 
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),
167
 
                                    3, &formatted_date,
168
 
                                    4, (almanah_entry_is_important (entry) == TRUE) ? "emblem-important" : NULL,
169
 
                                    -1);
170
 
 
171
 
                g_object_unref (entry);
172
 
        }
 
272
        gtk_list_store_clear (priv->sd_results_store);
 
273
 
 
274
        priv->sd_cancellable = g_cancellable_new ();
 
275
 
 
276
        search_string = gtk_entry_get_text (priv->sd_search_entry);
 
277
 
 
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);
 
283
 
 
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);
 
287
 
 
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);
 
292
 
 
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);
 
297
 
 
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);
 
302
 
 
303
        g_object_unref (storage_manager);
173
304
}
174
305
 
175
306
static void
176
 
select_date (GtkTreeModel *model, GtkTreeIter *iter)
 
307
select_date (AlmanahSearchDialog *self, GtkTreeModel *model, GtkTreeIter *iter)
177
308
{
 
309
        AlmanahMainWindow *main_window;
178
310
        guint day, month, year;
179
311
        GDate date;
180
312
 
184
316
                            2, &year,
185
317
                            -1);
186
318
 
 
319
        main_window = ALMANAH_MAIN_WINDOW (gtk_window_get_transient_for (GTK_WINDOW (self)));
187
320
        g_date_set_dmy (&date, day, month, year);
188
 
        almanah_main_window_select_date (ALMANAH_MAIN_WINDOW (almanah->main_window), &date);
 
321
        almanah_main_window_select_date (main_window, &date);
189
322
}
190
323
 
191
324
void
192
 
sd_results_tree_view_row_activated_cb (GtkTreeView *self, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data)
 
325
sd_results_tree_view_row_activated_cb (GtkTreeView *tree_view, GtkTreePath *path, GtkTreeViewColumn *column, AlmanahSearchDialog *self)
193
326
{
194
327
        GtkTreeIter iter;
195
328
        GtkTreeModel *model;
196
329
 
197
 
        model = gtk_tree_view_get_model (self);
 
330
        model = gtk_tree_view_get_model (tree_view);
198
331
        gtk_tree_model_get_iter (model, &iter, path);
199
 
        select_date (model, &iter);
 
332
        select_date (self, model, &iter);
200
333
}
201
334
 
202
335
void
205
338
        GtkTreeIter iter;
206
339
        GtkTreeModel *model;
207
340
 
208
 
        if (gtk_tree_selection_get_selected (search_dialog->priv->sd_results_selection, &model, &iter) == TRUE)
209
 
                select_date (model, &iter);
 
341
        if (gtk_tree_selection_get_selected (search_dialog->priv->sd_results_selection, &model, &iter) == TRUE) {
 
342
                select_date (search_dialog, model, &iter);
 
343
        }
210
344
}