Refactor part of ModestWindow and ModestHildon2Window
[modest] / src / widgets / modest-toolkit-utils.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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.
16  *
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.
28  */
29
30 #include <glib.h>
31 #include <glib/gstdio.h>
32 #include <errno.h>
33 #include <string.h> /* for strlen */
34 #include <modest-runtime.h>
35
36 #include <modest-defs.h>
37 #include <modest-text-utils.h>
38 #include "modest-toolkit-utils.h"
39 #include "modest-ui-constants.h"
40 #ifdef MODEST_TOOLKIT_HILDON2
41 #include <hildon/hildon.h>
42 #endif
43
44 /* Label child of a captioned */
45 #define CAPTIONED_LABEL_CHILD "captioned-label"
46
47
48 GtkWidget *
49 modest_toolkit_utils_get_manager_menubar_as_menu (GtkUIManager *manager,
50                                                   const gchar *item_name)
51 {
52         GtkWidget *new_menu;
53         GtkWidget *menubar;
54         GList *children, *iter;
55
56         menubar = gtk_ui_manager_get_widget (manager, item_name);
57         new_menu = gtk_menu_new ();
58
59         children = gtk_container_get_children (GTK_CONTAINER (menubar));
60         for (iter = children; iter != NULL; iter = g_list_next (iter)) {
61                 GtkWidget *menu;
62
63                 menu = GTK_WIDGET (iter->data);
64                 gtk_widget_reparent (menu, new_menu);
65         }
66         
67         g_list_free (children);
68
69         return new_menu;
70 }
71
72 /**
73  * modest_toolkit_utils_create_captioned:
74  * @title_size_group: a #GtkSizeGroup
75  * @value_size_group: a #GtkSizeGroup
76  * @title: a string
77  * @control: a #GtkWidget
78  *
79  * this creates a widget (a #GtkHBox) with a control, and a label
80  * (@string) captioning it. It also uses the proper size groups for title
81  * and control.
82  *
83  * Returns: a widget containing the control and a proper label.
84  */
85 GtkWidget *
86 modest_toolkit_utils_create_captioned    (GtkSizeGroup *title_size_group,
87                                           GtkSizeGroup *value_size_group,
88                                           const gchar *title,
89                                           gboolean use_markup,
90                                           GtkWidget *control)
91 {
92         return modest_toolkit_utils_create_captioned_with_size_type (title_size_group,
93                                                                      value_size_group,
94                                                                      title,
95                                                                      use_markup,
96                                                                      control,
97                                                                      0);
98 }
99
100 GtkWidget *
101 modest_toolkit_utils_create_vcaptioned    (GtkSizeGroup *size_group,
102                                            const gchar *title,
103                                            gboolean use_markup,
104                                            GtkWidget *control)
105 {
106         return modest_toolkit_utils_create_vcaptioned_with_size_type (size_group,
107                                                                       title,
108                                                                       use_markup,
109                                                                       control,
110                                                                       0);
111 }
112
113 /**
114  * modest_toolkit_utils_create_captioned_with_size_type:
115  * @title_size_group: a #GtkSizeGroup
116  * @value_size_group: a #GtkSizeGroup
117  * @title: a string
118  * @control: a #GtkWidget
119  * @size_type: a #HildonSizeType
120  *
121  * this creates a widget (a #GtkHBox) with a control, and a label
122  * (@string) captioning it. It also uses the proper size groups for title
123  * and control.
124  *
125  * Returns: a widget containing the control and a proper label.
126  */
127 GtkWidget *
128 modest_toolkit_utils_create_captioned_with_size_type    (GtkSizeGroup *title_size_group,
129                                                          GtkSizeGroup *value_size_group,
130                                                          const gchar *title,
131                                                          gboolean use_markup,
132                                                          GtkWidget *control,
133                                                          ModestToolkitSizeType size_type)
134 {
135         GtkWidget *label;
136         GtkWidget *align;
137         GtkWidget *box;
138   
139         if (use_markup) {
140                 label = gtk_label_new (NULL);
141                 gtk_label_set_markup (GTK_LABEL (label), title);
142         } else {
143                 label = gtk_label_new (title);
144         }
145         align = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
146         gtk_alignment_set_padding (GTK_ALIGNMENT (align), 0, 0, MODEST_MARGIN_DOUBLE, MODEST_MARGIN_DOUBLE);
147
148         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
149 #ifdef MAEMO_CHANGES
150         hildon_gtk_widget_set_theme_size (label, HILDON_SIZE_FINGER_HEIGHT);
151 #endif
152         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
153         gtk_widget_show (label);
154         gtk_widget_show (align);
155         box = gtk_hbox_new (FALSE, 0);
156         gtk_container_add (GTK_CONTAINER (align), label);
157         gtk_box_pack_start (GTK_BOX (box), align, FALSE, FALSE, 0);
158         gtk_box_pack_start (GTK_BOX (box), control, TRUE, TRUE, 0);
159         if (title_size_group)
160                 gtk_size_group_add_widget (title_size_group, label);
161         if (value_size_group)
162                 gtk_size_group_add_widget (value_size_group, control);
163
164 #ifdef MAEMO_CHANGES
165         hildon_gtk_widget_set_theme_size (control, size_type);
166 #endif
167
168         g_object_set_data (G_OBJECT (box), CAPTIONED_LABEL_CHILD, label);
169
170         return box;
171 }
172
173 GtkWidget *
174 modest_toolkit_utils_create_vcaptioned_with_size_type    (GtkSizeGroup *size_group,
175                                                           const gchar *title,
176                                                           gboolean use_markup,
177                                                           GtkWidget *control,
178                                                           ModestToolkitSizeType size_type)
179 {
180         GtkWidget *label;
181         GtkWidget *align;
182         GtkWidget *box;
183   
184         if (use_markup) {
185                 label = gtk_label_new (NULL);
186                 gtk_label_set_markup (GTK_LABEL (label), title);
187         } else {
188                 label = gtk_label_new (title);
189         }
190         align = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
191         gtk_alignment_set_padding (GTK_ALIGNMENT (align), 0, 0, MODEST_MARGIN_DOUBLE, MODEST_MARGIN_DOUBLE);
192
193         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
194 #ifdef MAEMO_CHANGES
195         hildon_gtk_widget_set_theme_size (label, HILDON_SIZE_FINGER_HEIGHT);
196 #endif
197         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
198         gtk_widget_show (label);
199         gtk_widget_show (align);
200         box = gtk_vbox_new (FALSE, 0);
201         gtk_container_add (GTK_CONTAINER (align), label);
202         gtk_box_pack_start (GTK_BOX (box), align, FALSE, FALSE, 0);
203         gtk_box_pack_start (GTK_BOX (box), control, TRUE, TRUE, 0);
204         if (size_group) {
205                 gtk_size_group_add_widget (size_group, label);
206                 gtk_size_group_add_widget (size_group, control);
207         }
208
209 #ifdef MAEMO_CHANGES
210         hildon_gtk_widget_set_theme_size (control, size_type);
211 #endif
212
213         g_object_set_data (G_OBJECT (box), CAPTIONED_LABEL_CHILD, label);
214
215         return box;
216 }
217
218 /**
219  * modest_toolkit_utils_captioned_set_label:
220  * @captioned: a #GtkWidget built as captioned
221  * @new_label: a string
222  * @use_markup: a #gboolean
223  *
224  * set a new label for the captioned
225  */
226 void
227 modest_toolkit_utils_captioned_set_label (GtkWidget *captioned,
228                                           const gchar *new_label,
229                                           gboolean use_markup)
230 {
231         GtkWidget *label;
232
233         g_return_if_fail (GTK_IS_WIDGET (captioned));
234
235         label = g_object_get_data (G_OBJECT (captioned), CAPTIONED_LABEL_CHILD);
236         g_return_if_fail (GTK_IS_LABEL (label));
237
238         if (use_markup) {
239                 gtk_label_set_markup (GTK_LABEL (label), new_label);
240         } else {
241                 gtk_label_set_text (GTK_LABEL (label), new_label);
242         }
243 }
244
245 /**
246  * modest_toolkit_utils_captioned_get_label_widget:
247  * @captioned: a #GtkWidget built as captioned
248  *
249  * obtains the label widget for the captioned
250  *
251  * Returns: a #GtkLabel
252  */
253 GtkWidget *
254 modest_toolkit_utils_captioned_get_label_widget (GtkWidget *captioned)
255 {
256         GtkWidget *label;
257
258         g_return_val_if_fail (GTK_IS_WIDGET (captioned), NULL);
259
260         label = g_object_get_data (G_OBJECT (captioned), CAPTIONED_LABEL_CHILD);
261         g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
262
263         return label;
264
265 }
266
267 /**
268  * modest_toolkit_utils_set_hbutton_layout:
269  * @title_sizegroup: a #GtkSizeGroup, or %NULL
270  * @value_sizegroup: a #GtkSizeGroup, or %NULL
271  * @title: a string
272  * @button: a #HildonButton
273  *
274  * Configures the alignment and layout of @button. If @title_sizegroup is provided,
275  * the title will be aligned to the left using it. If @value_sizegroup is provided,
276  * the value will be aligned to the left using it. It also sets the title
277  * of the button.
278  *
279  * The alignment is left for the title and for the value.
280  */
281 void
282 modest_toolkit_utils_set_hbutton_layout (GtkSizeGroup *title_sizegroup, 
283                                        GtkSizeGroup *value_sizegroup,
284                                        const gchar *title, 
285                                        GtkWidget *button)
286 {
287 #ifdef MODEST_TOOLKIT_HILDON2
288         hildon_button_set_title (HILDON_BUTTON (button), title);
289         if (title_sizegroup)
290                 hildon_button_add_title_size_group (HILDON_BUTTON (button), title_sizegroup);
291         if (value_sizegroup)
292                 hildon_button_add_value_size_group (HILDON_BUTTON (button), value_sizegroup);
293         hildon_button_set_alignment (HILDON_BUTTON (button), 0.0, 0.5, 1.0, 0.0);
294         hildon_button_set_title_alignment (HILDON_BUTTON (button), 0.0, 0.5);
295         hildon_button_set_value_alignment (HILDON_BUTTON (button), 0.0, 0.5);
296 #endif
297 }
298
299 void
300 modest_toolkit_utils_set_vbutton_layout (GtkSizeGroup *sizegroup, 
301                                          const gchar *title, 
302                                          GtkWidget *button)
303 {
304 #ifdef MODEST_TOOLKIT_HILDON2
305         hildon_button_set_title (HILDON_BUTTON (button), title);
306         if (sizegroup) {
307                 hildon_button_add_title_size_group (HILDON_BUTTON (button), sizegroup);
308                 hildon_button_add_value_size_group (HILDON_BUTTON (button), sizegroup);
309         }
310         hildon_button_set_alignment (HILDON_BUTTON (button), 0.0, 0.5, 1.0, 0.0);
311         hildon_button_set_title_alignment (HILDON_BUTTON (button), 0.0, 0.5);
312         hildon_button_set_value_alignment (HILDON_BUTTON (button), 0.0, 0.5);
313 #endif
314 }
315
316 GtkWidget *
317 modest_toolkit_utils_create_group_box (const gchar *label_text, GtkWidget *contents)
318 {
319         GtkWidget *label;
320         GtkWidget *box;
321
322         label = gtk_label_new (label_text);
323         gtk_widget_show (label);
324
325         box = gtk_vbox_new (FALSE, MODEST_MARGIN_HALF);
326         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
327         gtk_box_pack_start (GTK_BOX (box), contents, TRUE, TRUE, 0);
328         gtk_widget_show (box);
329
330         return box;
331 }
332
333 static gboolean match_all (TnyList *list, GObject *item, gpointer match_data)
334 {
335         return TRUE;
336 }
337
338 gboolean
339 modest_toolkit_utils_select_attachments (GtkWindow *window, TnyList *att_list, gboolean include_msgs)
340 {
341 #ifdef MODEST_TOOLKIT_HILDON2
342         GtkTreeModel *model;
343         TnyIterator *iterator;
344         GtkWidget *selector;
345         GtkCellRenderer *renderer;
346         GtkWidget *dialog;
347         gint response;
348         gboolean result = TRUE;
349         gint attachments_added = 0;
350
351         model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_OBJECT));
352         for (iterator = tny_list_create_iterator (att_list);
353              !tny_iterator_is_done (iterator);
354              tny_iterator_next (iterator)) {
355                 GtkTreeIter iter;
356                 TnyMimePart *part;
357                 gchar *filename = NULL;
358
359                 part = (TnyMimePart *) tny_iterator_get_current (iterator);
360
361                 /* Ignore purged attachments and messages if ignore is
362                    set to TRUE */
363                 if (!(tny_mime_part_is_purged (part) ||
364                       (TNY_IS_MSG (part) && !include_msgs))) {
365
366                         if (TNY_IS_MSG (part)) {
367                                 TnyHeader *header = tny_msg_get_header (TNY_MSG (part));
368                                 filename = tny_header_dup_subject (header);
369                                 g_object_unref (header);
370                         } else {
371                                 filename = g_strdup (tny_mime_part_get_filename (part));
372                         }
373                         if ((filename == NULL) || (filename[0] == '\0')) {
374                                 g_free (filename);
375                                 filename = g_strdup (_("mail_va_no_subject"));
376                         }
377                         gtk_list_store_append (GTK_LIST_STORE (model), &iter);
378                         gtk_list_store_set (GTK_LIST_STORE (model), &iter, 0, filename, 1, part, -1);
379                         attachments_added ++;
380                         g_free (filename);
381                 }
382                 g_object_unref (part);
383         }
384         g_object_unref (iterator);
385
386         selector = GTK_WIDGET (hildon_touch_selector_new ());
387         renderer = gtk_cell_renderer_text_new ();
388         g_object_set((GObject *) renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
389         hildon_touch_selector_append_column ((HildonTouchSelector *) selector, model, renderer,
390                                              "text", 0, NULL);
391         hildon_touch_selector_set_column_selection_mode ((HildonTouchSelector *) selector, 
392                                                          HILDON_TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE);
393
394         dialog = hildon_picker_dialog_new (window);
395         gtk_window_set_title (GTK_WINDOW (dialog), (attachments_added > 1)?
396                               _("mcen_ti_select_attachments_title"):_("mcen_ti_select_attachment_title"));
397         hildon_picker_dialog_set_selector (HILDON_PICKER_DIALOG (dialog), (HildonTouchSelector *) selector);
398         hildon_touch_selector_unselect_all ((HildonTouchSelector *) selector, 0);
399         hildon_picker_dialog_set_done_label (HILDON_PICKER_DIALOG (dialog), _HL("wdgt_bd_done"));
400
401         response = gtk_dialog_run (GTK_DIALOG (dialog));
402
403         if (response == GTK_RESPONSE_OK) {
404                 GList *selected_rows, *node;
405
406                 tny_list_remove_matches (att_list, match_all, NULL);
407                 selected_rows = hildon_touch_selector_get_selected_rows ((HildonTouchSelector *) selector, 0);
408                 for (node = selected_rows; node != NULL; node = g_list_next (node)) {
409                         GtkTreePath *path;
410                         GObject *selected;
411                         GtkTreeIter iter;
412
413                         path = (GtkTreePath *) node->data;
414                         gtk_tree_model_get_iter (model, &iter, path);
415                         gtk_tree_model_get (model, &iter, 1, &selected, -1);
416                         tny_list_append (att_list, selected);
417                 }
418                 if (tny_list_get_length (att_list) == 0)
419                         result = FALSE;
420
421                 g_list_foreach (selected_rows, (GFunc) gtk_tree_path_free, NULL);
422                 g_list_free (selected_rows);
423         } else {
424                 result = FALSE;
425         }
426
427         gtk_widget_destroy (dialog);
428
429         g_object_unref (model);
430
431         return result;
432 #else
433         return FALSE;
434 #endif
435 }
436