Fixes NB#113242, use dbus_connection_send_with_reply_and_block to prevent crashes...
[modest] / src / hildon2 / modest-maemo-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 #ifndef DBUS_API_SUBJECT_TO_CHANGE
31 #define DBUS_API_SUBJECT_TO_CHANGE
32 #endif /*DBUS_API_SUBJECT_TO_CHANGE*/
33
34 #include <dbus/dbus.h>
35 #include <dbus/dbus-glib-lowlevel.h>
36 #include <glib.h>
37 #include <glib/gstdio.h>
38 #include <errno.h>
39 #include <string.h> /* for strlen */
40 #include <modest-runtime.h>
41 #include <libgnomevfs/gnome-vfs.h>
42 #include <tny-fs-stream.h>
43 #include <tny-camel-account.h>
44 #include <tny-status.h>
45 #include <tny-camel-transport-account.h>
46 #include <tny-camel-imap-store-account.h>
47 #include <tny-camel-pop-store-account.h>
48 #include "modest-hildon-includes.h"
49
50 #include <modest-defs.h>
51 #include "modest-maemo-utils.h"
52 #include "modest-text-utils.h"
53 #include "modest-platform.h"
54 #include "modest-ui-constants.h"
55 #include <hildon/hildon-picker-dialog.h>
56
57 /* Label child of a captioned */
58 #define CAPTIONED_LABEL_CHILD "captioned-label"
59
60
61 static osso_context_t *__osso_context = NULL; /* urgh global */
62
63 osso_context_t *
64 modest_maemo_utils_get_osso_context (void)
65 {
66         if (!__osso_context) 
67                 g_warning ("%s: __osso_context == NULL", __FUNCTION__);
68
69         return __osso_context;
70 }
71
72 void
73 modest_maemo_utils_set_osso_context (osso_context_t *osso_context)
74 {
75         g_return_if_fail (osso_context);
76         __osso_context = osso_context;
77 }
78
79 static void
80 get_properties_cb (DBusMessage *message)
81 {
82         DBusMessageIter iter;
83         DBusMessageIter dict_iter;
84         DBusMessageIter dict_entry_iter;
85         DBusError err;
86         gchar *bt_name = NULL;
87         int key_type, array_type, msg_type;
88
89         dbus_error_init(&err);
90         if (dbus_set_error_from_message (&err, message)) {
91                 g_warning ("%s: %s", __FUNCTION__, err.message);
92         }
93
94         /* Get msg type */
95         dbus_message_iter_init (message, &iter);
96         msg_type = dbus_message_iter_get_arg_type (&iter);
97         dbus_message_iter_recurse (&iter, &dict_iter);
98
99         while ((array_type = dbus_message_iter_get_arg_type (&dict_iter)) == DBUS_TYPE_DICT_ENTRY) {
100
101                 dbus_message_iter_recurse (&dict_iter, &dict_entry_iter);
102
103                 while ((key_type = dbus_message_iter_get_arg_type (&dict_entry_iter)) == DBUS_TYPE_STRING) {
104                         DBusMessageIter dict_entry_content_iter;
105                         char *key;
106                         char *value;
107                         int dict_entry_type;
108                         int dict_entry_content_type;
109
110                         dbus_message_iter_get_basic (&dict_entry_iter, &key);
111                         dbus_message_iter_next (&dict_entry_iter);
112                         dict_entry_type = dbus_message_iter_get_arg_type (&dict_entry_iter);
113                         dbus_message_iter_recurse (&dict_entry_iter, &dict_entry_content_iter);
114                         dict_entry_content_type = dbus_message_iter_get_arg_type (&dict_entry_content_iter);
115
116                         if (dict_entry_content_type == DBUS_TYPE_STRING) {
117                                 dbus_message_iter_get_basic ( &dict_entry_content_iter, &value );
118
119                                 if (strcmp (key, "Name") == 0 ) {
120                                         g_debug ("-------------Name %s", value);
121                                         bt_name = value;
122                                         break;
123                                 }
124                         }
125                         dbus_message_iter_next (&dict_entry_iter);
126                 }
127
128                 if (key_type != DBUS_TYPE_INVALID)
129                         break;
130
131                 dbus_message_iter_next (&dict_iter);
132         }
133
134         /* Save device name */
135         if (bt_name) {
136                 g_debug ("Setting the device name %s", bt_name);
137                 modest_conf_set_string (modest_runtime_get_conf(),
138                                         MODEST_CONF_DEVICE_NAME, bt_name,
139                                         NULL);
140         }
141 }
142
143 static void
144 get_default_adapter_cb (DBusMessage *message)
145 {
146         DBusMessageIter iter;
147         gchar* path = NULL;
148
149         dbus_message_iter_init (message, &iter);
150
151         dbus_message_iter_get_basic (&iter, &path);
152         if (path != NULL) {
153                 DBusError error;
154                 DBusConnection *conn;
155                 DBusMessage *adapterMsg = dbus_message_new_method_call ("org.bluez", path,
156                                                                         "org.bluez.Adapter",
157                                                                         "GetProperties");
158
159                 conn = dbus_bus_get (DBUS_BUS_SYSTEM, NULL);
160
161                 if (conn) {
162                         DBusMessage *msg;
163
164                         dbus_error_init (&error);
165                         msg = dbus_connection_send_with_reply_and_block (conn, adapterMsg, -1, &error);
166                         if (msg) {
167                                 g_debug ("Getting the properties");
168                                 get_properties_cb (msg);
169                                 dbus_message_unref (msg);
170                         }
171                         dbus_connection_unref (conn);
172                 }
173                 dbus_message_unref (adapterMsg);
174         } else {
175                 g_warning ("Failed to get the default bluetooth adapter");
176         }
177 }
178
179 void
180 modest_maemo_utils_get_device_name (void)
181 {
182         static DBusConnection *conn = NULL;
183         DBusMessage *request;
184         DBusError error;
185         DBusMessage *msg;
186
187         dbus_error_init (&error);
188         if (!conn) {
189                 conn = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
190                 if (!conn) {
191                         g_printerr ("modest: cannot get on the dbus: %s: %s\n",
192                                     error.name, error.message);
193                         dbus_error_free (&error);
194                         return;
195                 }
196         }
197
198         /* Get the default adapter */
199         request = dbus_message_new_method_call ("org.bluez", "/" ,
200                                                 "org.bluez.Manager",
201                                                 "DefaultAdapter");
202
203         msg = dbus_connection_send_with_reply_and_block (conn, request, -1, &error);
204         if (msg) {
205                 g_debug ("Getting the default adapter");
206                 get_default_adapter_cb (msg);
207                 dbus_message_unref (msg);
208         }
209         dbus_message_unref (request);
210         dbus_connection_unref (conn);
211 }
212
213 void
214 modest_maemo_utils_setup_images_filechooser (GtkFileChooser *chooser)
215 {
216         GtkFileFilter *file_filter;
217         GList *image_mimetypes_list;
218         GList *node;
219         gchar *conf_folder;
220
221         g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser));
222
223         conf_folder = modest_conf_get_string (modest_runtime_get_conf (), MODEST_CONF_LATEST_INSERT_IMAGE_PATH, NULL);
224         if (conf_folder && conf_folder[0] != '\0') {
225                 gtk_file_chooser_set_current_folder_uri (chooser, conf_folder);
226         } else {
227                 gchar *images_folder;
228                 /* Set the default folder to images folder */
229                 images_folder = g_build_filename (g_getenv (MODEST_MAEMO_UTILS_MYDOCS_ENV),
230                                                   MODEST_MAEMO_UTILS_DEFAULT_IMAGE_FOLDER, NULL);
231                 gtk_file_chooser_set_current_folder (chooser, images_folder);
232                 g_free (images_folder);
233         }
234         g_free (conf_folder);
235
236         /* Set the images mime filter */
237         file_filter = gtk_file_filter_new ();
238 #ifdef MODEST_HAVE_HILDON0_WIDGETS
239         image_mimetypes_list = osso_mime_get_mime_types_for_category (OSSO_MIME_CATEGORY_IMAGES);
240 #else
241         image_mimetypes_list = hildon_mime_get_mime_types_for_category (HILDON_MIME_CATEGORY_IMAGES);
242 #endif
243         for (node = image_mimetypes_list; node != NULL; node = g_list_next (node)) {
244                 gtk_file_filter_add_mime_type (file_filter, node->data);
245         }
246         gtk_file_chooser_set_filter (chooser, file_filter);
247 #ifdef MODEST_HAVE_HILDON0_WIDGETS
248         osso_mime_types_list_free (image_mimetypes_list);
249 #else
250         hildon_mime_types_list_free (image_mimetypes_list);
251 #endif
252
253 }
254
255 void
256 modest_maemo_set_thumbable_scrollbar (GtkScrolledWindow *win, 
257                                       gboolean thumbable)
258 {
259         g_return_if_fail (GTK_IS_SCROLLED_WINDOW(win));
260 #ifdef MODEST_HAVE_HILDON1_WIDGETS              
261         hildon_helper_set_thumb_scrollbar (win, thumbable);
262 #endif /* MODEST_HAVE_HILDON1_WIDGETS */
263 }
264
265 GtkWidget *
266 modest_maemo_utils_get_manager_menubar_as_menu (GtkUIManager *manager,
267                                                 const gchar *item_name)
268 {
269         GtkWidget *new_menu;
270         GtkWidget *menubar;
271         GList *children, *iter;
272
273         menubar = gtk_ui_manager_get_widget (manager, item_name);
274         new_menu = gtk_menu_new ();
275
276         children = gtk_container_get_children (GTK_CONTAINER (menubar));
277         for (iter = children; iter != NULL; iter = g_list_next (iter)) {
278                 GtkWidget *menu;
279
280                 menu = GTK_WIDGET (iter->data);
281                 gtk_widget_reparent (menu, new_menu);
282         }
283         
284         g_list_free (children);
285
286         return new_menu;
287 }
288
289 /**
290  * modest_maemo_utils_create_captioned:
291  * @title_size_group: a #GtkSizeGroup
292  * @value_size_group: a #GtkSizeGroup
293  * @title: a string
294  * @control: a #GtkWidget
295  *
296  * this creates a widget (a #GtkHBox) with a control, and a label
297  * (@string) captioning it. It also uses the proper size groups for title
298  * and control.
299  *
300  * Returns: a widget containing the control and a proper label.
301  */
302 GtkWidget *
303 modest_maemo_utils_create_captioned    (GtkSizeGroup *title_size_group,
304                                         GtkSizeGroup *value_size_group,
305                                         const gchar *title,
306                                         gboolean use_markup,
307                                         GtkWidget *control)
308 {
309         return modest_maemo_utils_create_captioned_with_size_type (title_size_group,
310                                                                    value_size_group,
311                                                                    title,
312                                                                    use_markup,
313                                                                    control,
314                                                                    0);
315 }
316
317 /**
318  * modest_maemo_utils_create_captioned_with_size_type:
319  * @title_size_group: a #GtkSizeGroup
320  * @value_size_group: a #GtkSizeGroup
321  * @title: a string
322  * @control: a #GtkWidget
323  * @size_type: a #HildonSizeType
324  *
325  * this creates a widget (a #GtkHBox) with a control, and a label
326  * (@string) captioning it. It also uses the proper size groups for title
327  * and control.
328  *
329  * Returns: a widget containing the control and a proper label.
330  */
331 GtkWidget *
332 modest_maemo_utils_create_captioned_with_size_type    (GtkSizeGroup *title_size_group,
333                                                        GtkSizeGroup *value_size_group,
334                                                        const gchar *title,
335                                                        gboolean use_markup,
336                                                        GtkWidget *control,
337                                                        HildonSizeType size_type)
338 {
339         GtkWidget *label;
340         GtkWidget *align;
341         GtkWidget *box;
342   
343         if (use_markup) {
344                 label = gtk_label_new (NULL);
345                 gtk_label_set_markup (GTK_LABEL (label), title);
346         } else {
347                 label = gtk_label_new (title);
348         }
349         align = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
350         gtk_alignment_set_padding (GTK_ALIGNMENT (align), 0, 0, MODEST_MARGIN_DOUBLE, MODEST_MARGIN_DOUBLE);
351
352         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
353         hildon_gtk_widget_set_theme_size (label, HILDON_SIZE_FINGER_HEIGHT);
354         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
355         gtk_widget_show (label);
356         gtk_widget_show (align);
357         box = gtk_hbox_new (FALSE, 0);
358         gtk_container_add (GTK_CONTAINER (align), label);
359         gtk_box_pack_start (GTK_BOX (box), align, FALSE, FALSE, 0);
360         gtk_box_pack_start (GTK_BOX (box), control, TRUE, TRUE, 0);
361         if (title_size_group)
362                 gtk_size_group_add_widget (title_size_group, label);
363         if (value_size_group)
364                 gtk_size_group_add_widget (value_size_group, control);
365
366         hildon_gtk_widget_set_theme_size (control, size_type);
367
368         g_object_set_data (G_OBJECT (box), CAPTIONED_LABEL_CHILD, label);
369
370         return box;
371 }
372
373 /**
374  * modest_maemo_utils_captioned_set_label:
375  * @captioned: a #GtkWidget built as captioned
376  * @new_label: a string
377  * @use_markup: a #gboolean
378  *
379  * set a new label for the captioned
380  */
381 void
382 modest_maemo_utils_captioned_set_label (GtkWidget *captioned,
383                                         const gchar *new_label,
384                                         gboolean use_markup)
385 {
386         GtkWidget *label;
387
388         g_return_if_fail (GTK_IS_WIDGET (captioned));
389
390         label = g_object_get_data (G_OBJECT (captioned), CAPTIONED_LABEL_CHILD);
391         g_return_if_fail (GTK_IS_LABEL (label));
392
393         if (use_markup) {
394                 gtk_label_set_markup (GTK_LABEL (label), new_label);
395         } else {
396                 gtk_label_set_text (GTK_LABEL (label), new_label);
397         }
398 }
399
400 /**
401  * modest_maemo_utils_captioned_get_label_widget:
402  * @captioned: a #GtkWidget built as captioned
403  *
404  * obtains the label widget for the captioned
405  *
406  * Returns: a #GtkLabel
407  */
408 GtkWidget *
409 modest_maemo_utils_captioned_get_label_widget (GtkWidget *captioned)
410 {
411         GtkWidget *label;
412
413         g_return_val_if_fail (GTK_IS_WIDGET (captioned), NULL);
414
415         label = g_object_get_data (G_OBJECT (captioned), CAPTIONED_LABEL_CHILD);
416         g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
417
418         return label;
419
420 }
421
422 /**
423  * modest_maemo_utils_set_hbutton_layout:
424  * @title_sizegroup: a #GtkSizeGroup, or %NULL
425  * @value_sizegroup: a #GtkSizeGroup, or %NULL
426  * @title: a string
427  * @button: a #HildonButton
428  *
429  * Configures the alignment and layout of @button. If @title_sizegroup is provided,
430  * the title will be aligned to the left using it. If @value_sizegroup is provided,
431  * the value will be aligned to the left using it. It also sets the title
432  * of the button.
433  *
434  * The alignment is left for the title and for the value.
435  */
436 void
437 modest_maemo_utils_set_hbutton_layout (GtkSizeGroup *title_sizegroup, 
438                                        GtkSizeGroup *value_sizegroup,
439                                        const gchar *title, 
440                                        GtkWidget *button)
441 {
442         hildon_button_set_title (HILDON_BUTTON (button), title);
443         if (title_sizegroup)
444                 hildon_button_add_title_size_group (HILDON_BUTTON (button), title_sizegroup);
445         if (value_sizegroup)
446                 hildon_button_add_value_size_group (HILDON_BUTTON (button), value_sizegroup);
447         hildon_button_set_alignment (HILDON_BUTTON (button), 0.0, 0.5, 1.0, 0.0);
448         hildon_button_set_title_alignment (HILDON_BUTTON (button), 0.0, 0.5);
449         hildon_button_set_value_alignment (HILDON_BUTTON (button), 0.0, 0.5);
450 }
451
452 void
453 modest_maemo_utils_set_vbutton_layout (GtkSizeGroup *sizegroup, 
454                                        const gchar *title, 
455                                        GtkWidget *button)
456 {
457         hildon_button_set_title (HILDON_BUTTON (button), title);
458         if (sizegroup) {
459                 hildon_button_add_title_size_group (HILDON_BUTTON (button), sizegroup);
460                 hildon_button_add_value_size_group (HILDON_BUTTON (button), sizegroup);
461         }
462         hildon_button_set_alignment (HILDON_BUTTON (button), 0.0, 0.5, 1.0, 0.0);
463         hildon_button_set_title_alignment (HILDON_BUTTON (button), 0.0, 0.5);
464         hildon_button_set_value_alignment (HILDON_BUTTON (button), 0.0, 0.5);
465 }
466
467 GtkWidget *
468 modest_maemo_utils_create_group_box (const gchar *label_text, GtkWidget *contents)
469 {
470         GtkWidget *label;
471         GtkWidget *box;
472
473         label = gtk_label_new (label_text);
474         gtk_widget_show (label);
475
476         box = gtk_vbox_new (FALSE, MODEST_MARGIN_HALF);
477         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
478         gtk_box_pack_start (GTK_BOX (box), contents, TRUE, TRUE, 0);
479         gtk_widget_show (box);
480
481         return box;
482 }
483
484 static gboolean match_all (TnyList *list, GObject *item, gpointer match_data)
485 {
486         return TRUE;
487 }
488
489 gboolean
490 modest_maemo_utils_select_attachments (GtkWindow *window, TnyList *att_list, gboolean include_msgs)
491 {
492         GtkTreeModel *model;
493         TnyIterator *iterator;
494         GtkWidget *selector;
495         GtkCellRenderer *renderer;
496         GtkWidget *dialog;
497         gint response;
498         gboolean result = TRUE;
499         gint attachments_added = 0;
500
501         model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_OBJECT));
502         for (iterator = tny_list_create_iterator (att_list);
503              !tny_iterator_is_done (iterator);
504              tny_iterator_next (iterator)) {
505                 GtkTreeIter iter;
506                 TnyMimePart *part;
507                 gchar *filename = NULL;
508
509                 part = (TnyMimePart *) tny_iterator_get_current (iterator);
510
511                 /* Ignore purged attachments and messages if ignore is
512                    set to TRUE */
513                 if (!(tny_mime_part_is_purged (part) ||
514                       (TNY_IS_MSG (part) && !include_msgs))) {
515
516                         if (TNY_IS_MSG (part)) {
517                                 TnyHeader *header = tny_msg_get_header (TNY_MSG (part));
518                                 filename = tny_header_dup_subject (header);
519                                 if ((filename == NULL) || (filename[0] == '\0')) {
520                                         g_free (filename);
521                                         filename = g_strdup (_("mail_va_no_subject"));
522                                 }
523                                 g_object_unref (header);
524                         } else {
525                                 filename = g_strdup (tny_mime_part_get_filename (part));
526                         }
527                         gtk_list_store_append (GTK_LIST_STORE (model), &iter);
528                         gtk_list_store_set (GTK_LIST_STORE (model), &iter, 0, filename, 1, part, -1);
529                         attachments_added ++;
530                         g_free (filename);
531                 }
532                 g_object_unref (part);
533         }
534         g_object_unref (iterator);
535
536         selector = GTK_WIDGET (hildon_touch_selector_new ());
537         renderer = gtk_cell_renderer_text_new ();
538         hildon_touch_selector_append_column (HILDON_TOUCH_SELECTOR (selector), model, renderer,
539                                              "text", 0, NULL);
540         hildon_touch_selector_set_column_selection_mode (HILDON_TOUCH_SELECTOR (selector), 
541                                                          HILDON_TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE);
542
543         dialog = hildon_picker_dialog_new (window);
544         gtk_window_set_title (GTK_WINDOW (dialog), (attachments_added > 1)?
545                               _("mcen_ti_select_attachments_title"):_("mcen_ti_select_attachment_title"));
546         hildon_picker_dialog_set_selector (HILDON_PICKER_DIALOG (dialog), HILDON_TOUCH_SELECTOR (selector));
547         hildon_picker_dialog_set_done_label (HILDON_PICKER_DIALOG (dialog), _HL("wdgt_bd_done"));
548
549         response = gtk_dialog_run (GTK_DIALOG (dialog));
550
551         if (response == GTK_RESPONSE_OK) {
552                 GList *selected_rows, *node;
553
554                 tny_list_remove_matches (att_list, match_all, NULL);
555                 selected_rows = hildon_touch_selector_get_selected_rows (HILDON_TOUCH_SELECTOR (selector), 0);
556                 for (node = selected_rows; node != NULL; node = g_list_next (node)) {
557                         GtkTreePath *path;
558                         GObject *selected;
559                         GtkTreeIter iter;
560
561                         path = (GtkTreePath *) node->data;
562                         gtk_tree_model_get_iter (model, &iter, path);
563                         gtk_tree_model_get (model, &iter, 1, &selected, -1);
564                         tny_list_append (att_list, selected);
565                 }
566                 if (tny_list_get_length (att_list) == 0)
567                         result = FALSE;
568
569                 g_list_foreach (selected_rows, (GFunc) gtk_tree_path_free, NULL);
570                 g_list_free (selected_rows);
571         } else {
572                 result = FALSE;
573         }
574
575         gtk_widget_destroy (dialog);
576
577         g_object_unref (model);
578
579         return result;
580 }