1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of the Nokia Corporation nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "modest-defs.h"
31 #include "modest-text-utils.h"
32 #include "modest-hildon2-sort-dialog.h"
33 #include "widgets/modest-sort-criterium-view.h"
36 static gint modest_hildon2_sort_dialog_add_sort_key (ModestSortCriteriumView *self, const gchar *sort_key);
37 static void modest_hildon2_sort_dialog_set_sort_key (ModestSortCriteriumView *self, gint key);
38 static void modest_hildon2_sort_dialog_set_sort_order (ModestSortCriteriumView *self, GtkSortType sort_type);
39 static gint modest_hildon2_sort_dialog_get_sort_key (ModestSortCriteriumView *self);
40 static GtkSortType modest_hildon2_sort_dialog_get_sort_order (ModestSortCriteriumView *self);
41 static void modest_sort_criterium_view_init (gpointer g_iface, gpointer iface_data);
43 G_DEFINE_TYPE_EXTENDED (ModestHildon2SortDialog,
44 modest_hildon2_sort_dialog,
45 HILDON_TYPE_PICKER_DIALOG,
47 G_IMPLEMENT_INTERFACE (MODEST_TYPE_SORT_CRITERIUM_VIEW, modest_sort_criterium_view_init));
49 typedef struct _ModestHildon2SortDialogPrivate ModestHildon2SortDialogPrivate;
50 struct _ModestHildon2SortDialogPrivate {
52 GtkSortType sort_type;
55 #define MODEST_HILDON2_SORT_DIALOG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
56 MODEST_TYPE_HILDON2_SORT_DIALOG, \
57 ModestHildon2SortDialogPrivate))
60 MODEST_HILDON2_SORT_DIALOG_RECIPIENT_AZ,
61 MODEST_HILDON2_SORT_DIALOG_RECIPIENT_ZA,
62 MODEST_HILDON2_SORT_DIALOG_DATE_NEWEST,
63 MODEST_HILDON2_SORT_DIALOG_DATE_OLDEST,
64 MODEST_HILDON2_SORT_DIALOG_SUBJECT_AZ,
65 MODEST_HILDON2_SORT_DIALOG_SUBJECT_ZA,
66 MODEST_HILDON2_SORT_DIALOG_ATTACHMENT,
67 MODEST_HILDON2_SORT_DIALOG_SIZE_LARGEST,
68 MODEST_HILDON2_SORT_DIALOG_SIZE_SMALLEST,
69 MODEST_HILDON2_SORT_DIALOG_PRIORITY,
70 MODEST_HILDON2_SORT_DIALOG_NUM_SORT_CRITERIUM
80 modest_hildon2_sort_dialog_finalize (GObject *object)
82 G_OBJECT_CLASS (modest_hildon2_sort_dialog_parent_class)->finalize (object);
86 modest_hildon2_sort_dialog_class_init (ModestHildon2SortDialogClass *klass)
88 GObjectClass *object_class = G_OBJECT_CLASS (klass);
90 object_class->finalize = modest_hildon2_sort_dialog_finalize;
92 g_type_class_add_private (object_class, sizeof(ModestHildon2SortDialogClass));
96 modest_hildon2_sort_dialog_init (ModestHildon2SortDialog *self)
98 ModestHildon2SortDialogPrivate *priv;
100 priv = MODEST_HILDON2_SORT_DIALOG_GET_PRIVATE(self);
102 priv->sort_type = GTK_SORT_ASCENDING;
106 modest_hildon2_sort_dialog_add_sort_key (ModestSortCriteriumView *self, const gchar *sort_key)
108 ModestHildon2SortDialogPrivate *priv;
110 priv = MODEST_HILDON2_SORT_DIALOG_GET_PRIVATE(self);
112 /* We return a different ID each time, it does not really
113 matter which one we return, we just want them to be
119 modest_hildon2_sort_dialog_set_sort_key (ModestSortCriteriumView *self, gint key)
121 HildonTouchSelector *selector;
122 ModestHildon2SortDialogPrivate *priv;
125 g_return_if_fail (MODEST_IS_HILDON2_SORT_DIALOG (self));
127 priv = MODEST_HILDON2_SORT_DIALOG_GET_PRIVATE(self);
128 selector = hildon_picker_dialog_get_selector (HILDON_PICKER_DIALOG (self));
130 /* Map to our selector */
133 if (priv->sort_type == GTK_SORT_ASCENDING)
134 active = MODEST_HILDON2_SORT_DIALOG_RECIPIENT_AZ;
136 active = MODEST_HILDON2_SORT_DIALOG_RECIPIENT_ZA;
139 if (priv->sort_type == GTK_SORT_ASCENDING)
140 active = MODEST_HILDON2_SORT_DIALOG_DATE_OLDEST;
142 active = MODEST_HILDON2_SORT_DIALOG_DATE_NEWEST;
145 if (priv->sort_type == GTK_SORT_ASCENDING)
146 active = MODEST_HILDON2_SORT_DIALOG_SUBJECT_AZ;
148 active = MODEST_HILDON2_SORT_DIALOG_SUBJECT_ZA;
151 active = MODEST_HILDON2_SORT_DIALOG_ATTACHMENT;
154 if (priv->sort_type == GTK_SORT_ASCENDING)
155 active = MODEST_HILDON2_SORT_DIALOG_SIZE_SMALLEST;
157 active = MODEST_HILDON2_SORT_DIALOG_SIZE_LARGEST;
160 active = MODEST_HILDON2_SORT_DIALOG_PRIORITY;
163 g_warning ("%s, no valid key found... falling back to default (0)", __FUNCTION__);
166 hildon_touch_selector_set_active (selector, 0, active);
170 modest_hildon2_sort_dialog_set_sort_order (ModestSortCriteriumView *self,
171 GtkSortType sort_type)
173 HildonTouchSelector *selector;
175 ModestHildon2SortDialogPrivate *priv;
177 g_return_if_fail (MODEST_IS_HILDON2_SORT_DIALOG (self));
179 priv = MODEST_HILDON2_SORT_DIALOG_GET_PRIVATE(self);
180 selector = hildon_picker_dialog_get_selector (HILDON_PICKER_DIALOG (self));
181 active = hildon_touch_selector_get_active (selector, 0);
183 /* Map to our selector, we need to check the current selected
184 key because this way we allow to select the order first and
187 case MODEST_HILDON2_SORT_DIALOG_RECIPIENT_AZ:
188 case MODEST_HILDON2_SORT_DIALOG_RECIPIENT_ZA:
189 if (sort_type == GTK_SORT_ASCENDING)
190 new = MODEST_HILDON2_SORT_DIALOG_RECIPIENT_ZA;
192 new = MODEST_HILDON2_SORT_DIALOG_RECIPIENT_AZ;
194 case MODEST_HILDON2_SORT_DIALOG_DATE_NEWEST:
195 case MODEST_HILDON2_SORT_DIALOG_DATE_OLDEST:
196 if (sort_type == GTK_SORT_ASCENDING)
197 new = MODEST_HILDON2_SORT_DIALOG_DATE_NEWEST;
199 new = MODEST_HILDON2_SORT_DIALOG_DATE_OLDEST;
201 case MODEST_HILDON2_SORT_DIALOG_SUBJECT_AZ:
202 case MODEST_HILDON2_SORT_DIALOG_SUBJECT_ZA:
203 if (sort_type == GTK_SORT_ASCENDING)
204 new = MODEST_HILDON2_SORT_DIALOG_SUBJECT_ZA;
206 new = MODEST_HILDON2_SORT_DIALOG_SUBJECT_AZ;
208 case MODEST_HILDON2_SORT_DIALOG_ATTACHMENT:
209 new = MODEST_HILDON2_SORT_DIALOG_ATTACHMENT;
211 case MODEST_HILDON2_SORT_DIALOG_SIZE_LARGEST:
212 case MODEST_HILDON2_SORT_DIALOG_SIZE_SMALLEST:
213 if (sort_type == GTK_SORT_ASCENDING)
214 new = MODEST_HILDON2_SORT_DIALOG_SIZE_SMALLEST;
216 new = MODEST_HILDON2_SORT_DIALOG_SIZE_LARGEST;
218 case MODEST_HILDON2_SORT_DIALOG_PRIORITY:
219 new = MODEST_HILDON2_SORT_DIALOG_PRIORITY;
222 g_warning ("%s, no valid key found... falling back to current (%d)", __FUNCTION__, active);
226 /* Activate the proper index */
227 hildon_touch_selector_set_active (selector, 0, new);
228 priv->sort_type = sort_type;
232 modest_hildon2_sort_dialog_get_sort_key (ModestSortCriteriumView *self)
234 HildonTouchSelector *selector;
237 g_return_val_if_fail (MODEST_IS_HILDON2_SORT_DIALOG (self), 0);
239 selector = hildon_picker_dialog_get_selector (HILDON_PICKER_DIALOG (self));
240 active = hildon_touch_selector_get_active (selector, 0);
243 case MODEST_HILDON2_SORT_DIALOG_RECIPIENT_AZ:
244 case MODEST_HILDON2_SORT_DIALOG_RECIPIENT_ZA:
246 case MODEST_HILDON2_SORT_DIALOG_DATE_NEWEST:
247 case MODEST_HILDON2_SORT_DIALOG_DATE_OLDEST:
249 case MODEST_HILDON2_SORT_DIALOG_SUBJECT_AZ:
250 case MODEST_HILDON2_SORT_DIALOG_SUBJECT_ZA:
252 case MODEST_HILDON2_SORT_DIALOG_ATTACHMENT:
254 case MODEST_HILDON2_SORT_DIALOG_SIZE_LARGEST:
255 case MODEST_HILDON2_SORT_DIALOG_SIZE_SMALLEST:
257 case MODEST_HILDON2_SORT_DIALOG_PRIORITY:
260 /* Default is order by ascending date */
261 g_return_val_if_reached (1);
267 modest_hildon2_sort_dialog_get_sort_order (ModestSortCriteriumView *self)
269 HildonTouchSelector *selector;
274 g_return_val_if_fail (MODEST_IS_HILDON2_SORT_DIALOG (self), GTK_SORT_ASCENDING);
276 selector = hildon_picker_dialog_get_selector (HILDON_PICKER_DIALOG (self));
277 hildon_touch_selector_get_selected (selector, 0, &iter);
279 model = hildon_touch_selector_get_model (selector, 0);
280 gtk_tree_model_get (model, &iter, ID_COLUMN, &id, -1);
283 case MODEST_HILDON2_SORT_DIALOG_RECIPIENT_AZ:
284 case MODEST_HILDON2_SORT_DIALOG_SUBJECT_AZ:
285 case MODEST_HILDON2_SORT_DIALOG_DATE_OLDEST:
286 case MODEST_HILDON2_SORT_DIALOG_SIZE_SMALLEST:
287 return GTK_SORT_ASCENDING;
288 case MODEST_HILDON2_SORT_DIALOG_RECIPIENT_ZA:
289 case MODEST_HILDON2_SORT_DIALOG_DATE_NEWEST:
290 case MODEST_HILDON2_SORT_DIALOG_SUBJECT_ZA:
291 case MODEST_HILDON2_SORT_DIALOG_SIZE_LARGEST:
292 case MODEST_HILDON2_SORT_DIALOG_ATTACHMENT:
293 case MODEST_HILDON2_SORT_DIALOG_PRIORITY:
294 return GTK_SORT_DESCENDING;
296 /* Default is order by ascending date */
297 g_return_val_if_reached (GTK_SORT_ASCENDING);
298 return GTK_SORT_ASCENDING;
303 modest_sort_criterium_view_init (gpointer g_iface,
306 ModestSortCriteriumViewIface *iface = (ModestSortCriteriumViewIface *) g_iface;
308 iface->add_sort_key_func = modest_hildon2_sort_dialog_add_sort_key;
309 iface->get_sort_key_func = modest_hildon2_sort_dialog_get_sort_key;
310 iface->set_sort_key_func = modest_hildon2_sort_dialog_set_sort_key;
311 iface->get_sort_order_func = modest_hildon2_sort_dialog_get_sort_order;
312 iface->set_sort_order_func = modest_hildon2_sort_dialog_set_sort_order;
316 modest_hildon2_sort_dialog_fill (ModestHildon2SortDialog *self)
318 GtkListStore *names_list;
321 HildonTouchSelectorColumn *column;
324 names_list = gtk_list_store_new (NUM_COLUMS, G_TYPE_STRING, G_TYPE_INT);
325 gtk_list_store_append (names_list, &iter);
326 gtk_list_store_set (names_list, &iter, NAME_COLUMN, _("mcen_li_sort_sender_recipient_az"),
327 ID_COLUMN, MODEST_HILDON2_SORT_DIALOG_RECIPIENT_AZ, -1);
328 gtk_list_store_append (names_list, &iter);
329 gtk_list_store_set (names_list, &iter, NAME_COLUMN, _("mcen_li_sort_sender_recipient_za"),
330 ID_COLUMN, MODEST_HILDON2_SORT_DIALOG_RECIPIENT_ZA, -1);
331 gtk_list_store_append (names_list, &iter);
332 gtk_list_store_set (names_list, &iter, NAME_COLUMN, _("mcen_li_sort_date_newest"),
333 ID_COLUMN, MODEST_HILDON2_SORT_DIALOG_DATE_NEWEST, -1);
334 gtk_list_store_append (names_list, &iter);
335 gtk_list_store_set (names_list, &iter, NAME_COLUMN, _("mcen_li_sort_date_oldest"),
336 ID_COLUMN, MODEST_HILDON2_SORT_DIALOG_DATE_OLDEST, -1);
337 gtk_list_store_append (names_list, &iter);
338 gtk_list_store_set (names_list, &iter, NAME_COLUMN, _("mcen_li_sort_subject_az"),
339 ID_COLUMN, MODEST_HILDON2_SORT_DIALOG_SUBJECT_AZ, -1);
340 gtk_list_store_append (names_list, &iter);
341 gtk_list_store_set (names_list, &iter, NAME_COLUMN, _("mcen_li_sort_subject_za"),
342 ID_COLUMN, MODEST_HILDON2_SORT_DIALOG_SUBJECT_ZA, -1);
343 gtk_list_store_append (names_list, &iter);
344 gtk_list_store_set (names_list, &iter, NAME_COLUMN, _("mcen_li_sort_attachment"),
345 ID_COLUMN, MODEST_HILDON2_SORT_DIALOG_ATTACHMENT, -1);
346 gtk_list_store_append (names_list, &iter);
347 gtk_list_store_set (names_list, &iter, NAME_COLUMN, _("mcen_li_sort_size_largest"),
348 ID_COLUMN, MODEST_HILDON2_SORT_DIALOG_SIZE_LARGEST, -1);
349 gtk_list_store_append (names_list, &iter);
350 gtk_list_store_set (names_list, &iter, NAME_COLUMN, _("mcen_li_sort_size_smallest"),
351 ID_COLUMN, MODEST_HILDON2_SORT_DIALOG_SIZE_SMALLEST, -1);
352 gtk_list_store_append (names_list, &iter);
353 gtk_list_store_set (names_list, &iter, NAME_COLUMN, _("mcen_li_sort_priority"),
354 ID_COLUMN, MODEST_HILDON2_SORT_DIALOG_PRIORITY, -1);
357 selector = hildon_touch_selector_new ();
358 column = hildon_touch_selector_append_text_column (HILDON_TOUCH_SELECTOR (selector),
359 GTK_TREE_MODEL (names_list), TRUE);
360 g_object_set (G_OBJECT (column), "text-column", NAME_COLUMN, NULL);
362 /* Set the selector */
363 hildon_picker_dialog_set_selector (HILDON_PICKER_DIALOG (self), HILDON_TOUCH_SELECTOR (selector));
367 modest_hildon2_sort_dialog_new (GtkWindow *parent)
369 GtkWidget *result = g_object_new (MODEST_TYPE_HILDON2_SORT_DIALOG, NULL);
371 modest_hildon2_sort_dialog_fill (MODEST_HILDON2_SORT_DIALOG (result));
374 gtk_window_set_transient_for(GTK_WINDOW(result), parent);
377 gtk_window_set_title (GTK_WINDOW (result), _HL("ckdg_ti_sort"));