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