939a199a5ca024959db0d6fa373f4a620d1f089f
[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 #ifdef MODEST_USE_IPHB
57 #include <iphbd/libiphb.h>
58 #endif
59
60 /* Label child of a captioned */
61 #define CAPTIONED_LABEL_CHILD "captioned-label"
62
63 #ifdef MODEST_PLATFORM_MAEMO
64 #define INTERNAL_MMC_USB_MODE  "/system/osso/af/internal-mmc-used-over-usb"
65 #endif
66
67 static osso_context_t *__osso_context = NULL; /* urgh global */
68
69 osso_context_t *
70 modest_maemo_utils_get_osso_context (void)
71 {
72         if (!__osso_context) 
73                 __osso_context = osso_initialize(PACKAGE,PACKAGE_VERSION,
74                                                  FALSE, NULL);
75
76         return __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 (DBusConnection *conn,
145                         DBusMessage *message)
146 {
147         DBusMessageIter iter;
148         gchar* path = NULL;
149         DBusError error;
150         DBusMessage *msg, *adapterMsg;
151
152         dbus_message_iter_init (message, &iter);
153         dbus_message_iter_get_basic (&iter, &path);
154
155         if (!path)
156                 return;
157
158         adapterMsg = dbus_message_new_method_call ("org.bluez", path,
159                                                    "org.bluez.Adapter",
160                                                    "GetProperties");
161
162         dbus_error_init (&error);
163         msg = dbus_connection_send_with_reply_and_block (conn, adapterMsg, -1, &error);
164         if (msg) {
165                 g_debug ("Getting the properties");
166                 get_properties_cb (msg);
167                 dbus_message_unref (msg);
168         }
169         dbus_message_unref (adapterMsg);
170 }
171
172 void
173 modest_maemo_utils_get_device_name (void)
174 {
175         static DBusConnection *conn = NULL;
176         DBusMessage *request;
177         DBusError error;
178         DBusMessage *msg;
179
180         dbus_error_init (&error);
181         if (!conn) {
182                 conn = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
183                 if (!conn) {
184                         g_printerr ("modest: cannot get on the dbus: %s: %s\n",
185                                     error.name, error.message);
186                         dbus_error_free (&error);
187                         return;
188                 }
189         }
190
191         /* Get the default adapter */
192         request = dbus_message_new_method_call ("org.bluez", "/" ,
193                                                 "org.bluez.Manager",
194                                                 "DefaultAdapter");
195
196         msg = dbus_connection_send_with_reply_and_block (conn, request, -1, &error);
197         if (msg) {
198                 g_debug ("Getting the default adapter");
199                 get_default_adapter_cb (conn, msg);
200                 dbus_message_unref (msg);
201         }
202
203         dbus_message_unref (request);
204         if (dbus_error_is_set (&error))
205                 dbus_error_free (&error);
206 }
207
208 void
209 modest_maemo_utils_setup_images_filechooser (GtkFileChooser *chooser)
210 {
211         GtkFileFilter *file_filter;
212         GList *image_mimetypes_list;
213         GList *node;
214         gchar *conf_folder;
215
216         g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser));
217
218         conf_folder = modest_conf_get_string (modest_runtime_get_conf (),
219                                               MODEST_CONF_LATEST_INSERT_IMAGE_PATH, NULL);
220         if (conf_folder && conf_folder[0] != '\0') {
221                 gtk_file_chooser_set_current_folder_uri (chooser, conf_folder);
222         } else {
223                 gchar *images_folder;
224                 /* Set the default folder to images folder */
225                 images_folder = (gchar *) g_strdup(g_get_user_special_dir (G_USER_DIRECTORY_PICTURES));
226                 if (!images_folder) {
227                         /* fallback */
228                         images_folder = g_build_filename (g_getenv (MODEST_MAEMO_UTILS_MYDOCS_ENV),
229                                                           MODEST_MAEMO_UTILS_DEFAULT_IMAGE_FOLDER, NULL);
230                 }
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         image_mimetypes_list = hildon_mime_get_mime_types_for_category (HILDON_MIME_CATEGORY_IMAGES);
239         for (node = image_mimetypes_list; node != NULL; node = g_list_next (node)) {
240                 gtk_file_filter_add_mime_type (file_filter, node->data);
241         }
242         gtk_file_chooser_set_filter (chooser, file_filter);
243         hildon_mime_types_list_free (image_mimetypes_list);
244
245 }
246
247 GtkWidget *
248 modest_maemo_utils_get_manager_menubar_as_menu (GtkUIManager *manager,
249                                                 const gchar *item_name)
250 {
251         GtkWidget *new_menu;
252         GtkWidget *menubar;
253         GList *children, *iter;
254
255         menubar = gtk_ui_manager_get_widget (manager, item_name);
256         new_menu = gtk_menu_new ();
257
258         children = gtk_container_get_children (GTK_CONTAINER (menubar));
259         for (iter = children; iter != NULL; iter = g_list_next (iter)) {
260                 GtkWidget *menu;
261
262                 menu = GTK_WIDGET (iter->data);
263                 gtk_widget_reparent (menu, new_menu);
264         }
265         
266         g_list_free (children);
267
268         return new_menu;
269 }
270
271 /**
272  * modest_maemo_utils_create_captioned:
273  * @title_size_group: a #GtkSizeGroup
274  * @value_size_group: a #GtkSizeGroup
275  * @title: a string
276  * @control: a #GtkWidget
277  *
278  * this creates a widget (a #GtkHBox) with a control, and a label
279  * (@string) captioning it. It also uses the proper size groups for title
280  * and control.
281  *
282  * Returns: a widget containing the control and a proper label.
283  */
284 GtkWidget *
285 modest_maemo_utils_create_captioned    (GtkSizeGroup *title_size_group,
286                                         GtkSizeGroup *value_size_group,
287                                         const gchar *title,
288                                         gboolean use_markup,
289                                         GtkWidget *control)
290 {
291         return modest_maemo_utils_create_captioned_with_size_type (title_size_group,
292                                                                    value_size_group,
293                                                                    title,
294                                                                    use_markup,
295                                                                    control,
296                                                                    0);
297 }
298
299 GtkWidget *
300 modest_maemo_utils_create_vcaptioned    (GtkSizeGroup *size_group,
301                                         const gchar *title,
302                                         gboolean use_markup,
303                                         GtkWidget *control)
304 {
305         return modest_maemo_utils_create_vcaptioned_with_size_type (size_group,
306                                                                     title,
307                                                                     use_markup,
308                                                                     control,
309                                                                     0);
310 }
311
312 /**
313  * modest_maemo_utils_create_captioned_with_size_type:
314  * @title_size_group: a #GtkSizeGroup
315  * @value_size_group: a #GtkSizeGroup
316  * @title: a string
317  * @control: a #GtkWidget
318  * @size_type: a #HildonSizeType
319  *
320  * this creates a widget (a #GtkHBox) with a control, and a label
321  * (@string) captioning it. It also uses the proper size groups for title
322  * and control.
323  *
324  * Returns: a widget containing the control and a proper label.
325  */
326 GtkWidget *
327 modest_maemo_utils_create_captioned_with_size_type    (GtkSizeGroup *title_size_group,
328                                                        GtkSizeGroup *value_size_group,
329                                                        const gchar *title,
330                                                        gboolean use_markup,
331                                                        GtkWidget *control,
332                                                        HildonSizeType size_type)
333 {
334         GtkWidget *label;
335         GtkWidget *align;
336         GtkWidget *box;
337   
338         if (use_markup) {
339                 label = gtk_label_new (NULL);
340                 gtk_label_set_markup (GTK_LABEL (label), title);
341         } else {
342                 label = gtk_label_new (title);
343         }
344         align = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
345         gtk_alignment_set_padding (GTK_ALIGNMENT (align), 0, 0, MODEST_MARGIN_DOUBLE, MODEST_MARGIN_DOUBLE);
346
347         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
348         hildon_gtk_widget_set_theme_size (label, HILDON_SIZE_FINGER_HEIGHT);
349         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
350         gtk_widget_show (label);
351         gtk_widget_show (align);
352         box = gtk_hbox_new (FALSE, 0);
353         gtk_container_add (GTK_CONTAINER (align), label);
354         gtk_box_pack_start (GTK_BOX (box), align, FALSE, FALSE, 0);
355         gtk_box_pack_start (GTK_BOX (box), control, TRUE, TRUE, 0);
356         if (title_size_group)
357                 gtk_size_group_add_widget (title_size_group, label);
358         if (value_size_group)
359                 gtk_size_group_add_widget (value_size_group, control);
360
361         hildon_gtk_widget_set_theme_size (control, size_type);
362
363         g_object_set_data (G_OBJECT (box), CAPTIONED_LABEL_CHILD, label);
364
365         return box;
366 }
367
368 GtkWidget *
369 modest_maemo_utils_create_vcaptioned_with_size_type    (GtkSizeGroup *size_group,
370                                                         const gchar *title,
371                                                         gboolean use_markup,
372                                                         GtkWidget *control,
373                                                         HildonSizeType size_type)
374 {
375         GtkWidget *label;
376         GtkWidget *align;
377         GtkWidget *box;
378   
379         if (use_markup) {
380                 label = gtk_label_new (NULL);
381                 gtk_label_set_markup (GTK_LABEL (label), title);
382         } else {
383                 label = gtk_label_new (title);
384         }
385         align = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
386         gtk_alignment_set_padding (GTK_ALIGNMENT (align), 0, 0, MODEST_MARGIN_DOUBLE, MODEST_MARGIN_DOUBLE);
387
388         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
389         hildon_gtk_widget_set_theme_size (label, HILDON_SIZE_FINGER_HEIGHT);
390         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
391         gtk_widget_show (label);
392         gtk_widget_show (align);
393         box = gtk_vbox_new (FALSE, 0);
394         gtk_container_add (GTK_CONTAINER (align), label);
395         gtk_box_pack_start (GTK_BOX (box), align, FALSE, FALSE, 0);
396         gtk_box_pack_start (GTK_BOX (box), control, TRUE, TRUE, 0);
397         if (size_group) {
398                 gtk_size_group_add_widget (size_group, label);
399                 gtk_size_group_add_widget (size_group, control);
400         }
401
402         hildon_gtk_widget_set_theme_size (control, size_type);
403
404         g_object_set_data (G_OBJECT (box), CAPTIONED_LABEL_CHILD, label);
405
406         return box;
407 }
408
409 /**
410  * modest_maemo_utils_captioned_set_label:
411  * @captioned: a #GtkWidget built as captioned
412  * @new_label: a string
413  * @use_markup: a #gboolean
414  *
415  * set a new label for the captioned
416  */
417 void
418 modest_maemo_utils_captioned_set_label (GtkWidget *captioned,
419                                         const gchar *new_label,
420                                         gboolean use_markup)
421 {
422         GtkWidget *label;
423
424         g_return_if_fail (GTK_IS_WIDGET (captioned));
425
426         label = g_object_get_data (G_OBJECT (captioned), CAPTIONED_LABEL_CHILD);
427         g_return_if_fail (GTK_IS_LABEL (label));
428
429         if (use_markup) {
430                 gtk_label_set_markup (GTK_LABEL (label), new_label);
431         } else {
432                 gtk_label_set_text (GTK_LABEL (label), new_label);
433         }
434 }
435
436 /**
437  * modest_maemo_utils_captioned_get_label_widget:
438  * @captioned: a #GtkWidget built as captioned
439  *
440  * obtains the label widget for the captioned
441  *
442  * Returns: a #GtkLabel
443  */
444 GtkWidget *
445 modest_maemo_utils_captioned_get_label_widget (GtkWidget *captioned)
446 {
447         GtkWidget *label;
448
449         g_return_val_if_fail (GTK_IS_WIDGET (captioned), NULL);
450
451         label = g_object_get_data (G_OBJECT (captioned), CAPTIONED_LABEL_CHILD);
452         g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
453
454         return label;
455
456 }
457
458 /**
459  * modest_maemo_utils_set_hbutton_layout:
460  * @title_sizegroup: a #GtkSizeGroup, or %NULL
461  * @value_sizegroup: a #GtkSizeGroup, or %NULL
462  * @title: a string
463  * @button: a #HildonButton
464  *
465  * Configures the alignment and layout of @button. If @title_sizegroup is provided,
466  * the title will be aligned to the left using it. If @value_sizegroup is provided,
467  * the value will be aligned to the left using it. It also sets the title
468  * of the button.
469  *
470  * The alignment is left for the title and for the value.
471  */
472 void
473 modest_maemo_utils_set_hbutton_layout (GtkSizeGroup *title_sizegroup, 
474                                        GtkSizeGroup *value_sizegroup,
475                                        const gchar *title, 
476                                        GtkWidget *button)
477 {
478         hildon_button_set_title (HILDON_BUTTON (button), title);
479         if (title_sizegroup)
480                 hildon_button_add_title_size_group (HILDON_BUTTON (button), title_sizegroup);
481         if (value_sizegroup)
482                 hildon_button_add_value_size_group (HILDON_BUTTON (button), value_sizegroup);
483         hildon_button_set_alignment (HILDON_BUTTON (button), 0.0, 0.5, 1.0, 0.0);
484         hildon_button_set_title_alignment (HILDON_BUTTON (button), 0.0, 0.5);
485         hildon_button_set_value_alignment (HILDON_BUTTON (button), 0.0, 0.5);
486 }
487
488 void
489 modest_maemo_utils_set_vbutton_layout (GtkSizeGroup *sizegroup, 
490                                        const gchar *title, 
491                                        GtkWidget *button)
492 {
493         hildon_button_set_title (HILDON_BUTTON (button), title);
494         if (sizegroup) {
495                 hildon_button_add_title_size_group (HILDON_BUTTON (button), sizegroup);
496                 hildon_button_add_value_size_group (HILDON_BUTTON (button), sizegroup);
497         }
498         hildon_button_set_alignment (HILDON_BUTTON (button), 0.0, 0.5, 1.0, 0.0);
499         hildon_button_set_title_alignment (HILDON_BUTTON (button), 0.0, 0.5);
500         hildon_button_set_value_alignment (HILDON_BUTTON (button), 0.0, 0.5);
501 }
502
503 GtkWidget *
504 modest_maemo_utils_create_group_box (const gchar *label_text, GtkWidget *contents)
505 {
506         GtkWidget *label;
507         GtkWidget *box;
508
509         label = gtk_label_new (label_text);
510         gtk_widget_show (label);
511
512         box = gtk_vbox_new (FALSE, MODEST_MARGIN_HALF);
513         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
514         gtk_box_pack_start (GTK_BOX (box), contents, TRUE, TRUE, 0);
515         gtk_widget_show (box);
516
517         return box;
518 }
519
520 static gboolean match_all (TnyList *list, GObject *item, gpointer match_data)
521 {
522         return TRUE;
523 }
524
525 gboolean
526 modest_maemo_utils_select_attachments (GtkWindow *window, TnyList *att_list, gboolean include_msgs)
527 {
528         GtkTreeModel *model;
529         TnyIterator *iterator;
530         GtkWidget *selector;
531         GtkCellRenderer *renderer;
532         GtkWidget *dialog;
533         gint response;
534         gboolean result = TRUE;
535         gint attachments_added = 0;
536
537         model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_OBJECT));
538         for (iterator = tny_list_create_iterator (att_list);
539              !tny_iterator_is_done (iterator);
540              tny_iterator_next (iterator)) {
541                 GtkTreeIter iter;
542                 TnyMimePart *part;
543                 gchar *filename = NULL;
544
545                 part = (TnyMimePart *) tny_iterator_get_current (iterator);
546
547                 /* Ignore purged attachments and messages if ignore is
548                    set to TRUE */
549                 if (!(tny_mime_part_is_purged (part) ||
550                       (TNY_IS_MSG (part) && !include_msgs))) {
551
552                         if (TNY_IS_MSG (part)) {
553                                 TnyHeader *header = tny_msg_get_header (TNY_MSG (part));
554                                 filename = tny_header_dup_subject (header);
555                                 g_object_unref (header);
556                         } else {
557                                 filename = g_strdup (tny_mime_part_get_filename (part));
558                         }
559                         if ((filename == NULL) || (filename[0] == '\0')) {
560                                 g_free (filename);
561                                 filename = g_strdup (_("mail_va_no_subject"));
562                         }
563                         gtk_list_store_append (GTK_LIST_STORE (model), &iter);
564                         gtk_list_store_set (GTK_LIST_STORE (model), &iter, 0, filename, 1, part, -1);
565                         attachments_added ++;
566                         g_free (filename);
567                 }
568                 g_object_unref (part);
569         }
570         g_object_unref (iterator);
571
572         selector = GTK_WIDGET (hildon_touch_selector_new ());
573         renderer = gtk_cell_renderer_text_new ();
574         g_object_set((GObject *) renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
575         hildon_touch_selector_append_column ((HildonTouchSelector *) selector, model, renderer,
576                                              "text", 0, NULL);
577         hildon_touch_selector_set_column_selection_mode ((HildonTouchSelector *) selector, 
578                                                          HILDON_TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE);
579
580         dialog = hildon_picker_dialog_new (window);
581         gtk_window_set_title (GTK_WINDOW (dialog), (attachments_added > 1)?
582                               _("mcen_ti_select_attachments_title"):_("mcen_ti_select_attachment_title"));
583         hildon_picker_dialog_set_selector (HILDON_PICKER_DIALOG (dialog), (HildonTouchSelector *) selector);
584         hildon_touch_selector_unselect_all ((HildonTouchSelector *) selector, 0);
585         hildon_picker_dialog_set_done_label (HILDON_PICKER_DIALOG (dialog), _HL("wdgt_bd_done"));
586
587         response = gtk_dialog_run (GTK_DIALOG (dialog));
588
589         if (response == GTK_RESPONSE_OK) {
590                 GList *selected_rows, *node;
591
592                 tny_list_remove_matches (att_list, match_all, NULL);
593                 selected_rows = hildon_touch_selector_get_selected_rows ((HildonTouchSelector *) selector, 0);
594                 for (node = selected_rows; node != NULL; node = g_list_next (node)) {
595                         GtkTreePath *path;
596                         GObject *selected;
597                         GtkTreeIter iter;
598
599                         path = (GtkTreePath *) node->data;
600                         gtk_tree_model_get_iter (model, &iter, path);
601                         gtk_tree_model_get (model, &iter, 1, &selected, -1);
602                         tny_list_append (att_list, selected);
603                 }
604                 if (tny_list_get_length (att_list) == 0)
605                         result = FALSE;
606
607                 g_list_foreach (selected_rows, (GFunc) gtk_tree_path_free, NULL);
608                 g_list_free (selected_rows);
609         } else {
610                 result = FALSE;
611         }
612
613         gtk_widget_destroy (dialog);
614
615         g_object_unref (model);
616
617         return result;
618 }
619
620 #ifdef MODEST_PLATFORM_MAEMO
621 gboolean
622 modest_maemo_utils_in_usb_mode ()
623 {
624         return modest_conf_get_bool (modest_runtime_get_conf (), INTERNAL_MMC_USB_MODE, NULL);
625 }
626 #endif
627
628 #ifdef MODEST_USE_IPHB
629
630 typedef struct _ModestHeartbeatSource {
631         GSource source;
632         iphb_t iphb;
633         GPollFD poll;
634         gint interval;
635 } ModestHeartbeatSource;
636
637 static gboolean modest_heartbeat_prepare (GSource* source, gint *timeout)
638 {
639     *timeout = -1;
640     return FALSE;
641 }
642
643 static gboolean 
644 modest_heartbeat_check(GSource* source)
645 {
646         return ((ModestHeartbeatSource *) source)->poll.revents != 0;
647 }
648
649 static gboolean modest_heartbeat_dispatch (GSource *source, GSourceFunc callback, gpointer userdata)
650 {
651     if (callback(userdata))
652     {
653             ModestHeartbeatSource *hb_source = (ModestHeartbeatSource *) source;
654
655             g_source_remove_poll (source, &(hb_source->poll));
656
657             int min = MAX(hb_source->interval - 30, 5);
658             iphb_wait(hb_source->iphb, min, min + 60, 0);
659
660             hb_source->poll.fd = iphb_get_fd(hb_source->iphb);
661             hb_source->poll.events = G_IO_IN;
662             hb_source->poll.revents = 0;
663
664             g_source_add_poll(source, &(hb_source->poll));
665
666             return TRUE;
667     } else {
668             return FALSE;
669     }
670 }
671
672 static void 
673 modest_heartbeat_finalize (GSource* source)
674 {
675         ModestHeartbeatSource* hb_source = (ModestHeartbeatSource *) source;
676         hb_source->iphb = iphb_close(hb_source->iphb);
677 }
678
679 GSourceFuncs modest_heartbeat_funcs =
680 {
681   modest_heartbeat_prepare,
682   modest_heartbeat_check,
683   modest_heartbeat_dispatch,
684   modest_heartbeat_finalize,
685   NULL,
686   NULL
687 };
688
689 static GSource *
690 modest_heartbeat_source_new (void)
691 {
692         GSource *source;
693         ModestHeartbeatSource *hb_source;
694         iphb_t iphb;
695         int hb_interval;
696
697         source = NULL;
698         hb_interval = 0;
699
700         iphb = iphb_open (&hb_interval);
701
702         if (iphb != 0) {
703                 int min;
704                 source = g_source_new (&modest_heartbeat_funcs, sizeof (ModestHeartbeatSource));
705                 hb_source = (ModestHeartbeatSource *) source;
706                 g_source_set_priority (source, G_PRIORITY_DEFAULT_IDLE);
707                 hb_source->iphb = iphb;
708                 hb_source->interval = hb_interval;
709
710                 min = MAX(hb_interval - 30, 5);
711                 iphb_wait(hb_source->iphb, min, min + 60, 0);
712
713                 hb_source->poll.fd = iphb_get_fd(hb_source->iphb);
714                 hb_source->poll.events = G_IO_IN;
715                 hb_source->poll.revents = 0;
716
717                 g_source_add_poll(source, &(hb_source->poll));
718         } else {
719                 source = g_idle_source_new ();
720         }
721
722         return source;
723 }
724
725 guint
726 modest_heartbeat_add (GSourceFunc function,
727                       gpointer userdata)
728 {
729         GSource *source;
730         guint id;
731
732         source = modest_heartbeat_source_new ();
733         g_source_set_callback (source, function, userdata, NULL);
734         id = g_source_attach (source, NULL);
735         g_source_unref (source);
736
737         return id;
738 }
739
740 #else
741 guint
742 modest_heartbeat_add (GSourceFunc function,
743                       gpointer userdata)
744 {
745         return g_idle_add (function, userdata);
746 }
747 #endif