+
+static gboolean match_all (TnyList *list, GObject *item, gpointer match_data)
+{
+ return TRUE;
+}
+
+gboolean
+modest_maemo_utils_select_attachments (GtkWindow *window, TnyList *att_list, gboolean include_msgs)
+{
+ GtkTreeModel *model;
+ TnyIterator *iterator;
+ GtkWidget *selector;
+ GtkCellRenderer *renderer;
+ GtkWidget *dialog;
+ gint response;
+ gboolean result = TRUE;
+ gint attachments_added = 0;
+
+ model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_OBJECT));
+ for (iterator = tny_list_create_iterator (att_list);
+ !tny_iterator_is_done (iterator);
+ tny_iterator_next (iterator)) {
+ GtkTreeIter iter;
+ TnyMimePart *part;
+ gchar *filename = NULL;
+
+ part = (TnyMimePart *) tny_iterator_get_current (iterator);
+
+ /* Ignore purged attachments and messages if ignore is
+ set to TRUE */
+ if (!(tny_mime_part_is_purged (part) ||
+ (TNY_IS_MSG (part) && !include_msgs))) {
+
+ if (TNY_IS_MSG (part)) {
+ TnyHeader *header = tny_msg_get_header (TNY_MSG (part));
+ filename = tny_header_dup_subject (header);
+ g_object_unref (header);
+ } else {
+ filename = g_strdup (tny_mime_part_get_filename (part));
+ }
+ if ((filename == NULL) || (filename[0] == '\0')) {
+ g_free (filename);
+ filename = g_strdup (_("mail_va_no_subject"));
+ }
+ gtk_list_store_append (GTK_LIST_STORE (model), &iter);
+ gtk_list_store_set (GTK_LIST_STORE (model), &iter, 0, filename, 1, part, -1);
+ attachments_added ++;
+ g_free (filename);
+ }
+ g_object_unref (part);
+ }
+ g_object_unref (iterator);
+
+ selector = GTK_WIDGET (hildon_touch_selector_new ());
+ renderer = gtk_cell_renderer_text_new ();
+ g_object_set((GObject *) renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
+ hildon_touch_selector_append_column ((HildonTouchSelector *) selector, model, renderer,
+ "text", 0, NULL);
+ hildon_touch_selector_set_column_selection_mode ((HildonTouchSelector *) selector,
+ HILDON_TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE);
+
+ dialog = hildon_picker_dialog_new (window);
+ gtk_window_set_title (GTK_WINDOW (dialog), (attachments_added > 1)?
+ _("mcen_ti_select_attachments_title"):_("mcen_ti_select_attachment_title"));
+ hildon_picker_dialog_set_selector (HILDON_PICKER_DIALOG (dialog), (HildonTouchSelector *) selector);
+ hildon_touch_selector_unselect_all ((HildonTouchSelector *) selector, 0);
+ hildon_picker_dialog_set_done_label (HILDON_PICKER_DIALOG (dialog), _HL("wdgt_bd_done"));
+
+ response = gtk_dialog_run (GTK_DIALOG (dialog));
+
+ if (response == GTK_RESPONSE_OK) {
+ GList *selected_rows, *node;
+
+ tny_list_remove_matches (att_list, match_all, NULL);
+ selected_rows = hildon_touch_selector_get_selected_rows ((HildonTouchSelector *) selector, 0);
+ for (node = selected_rows; node != NULL; node = g_list_next (node)) {
+ GtkTreePath *path;
+ GObject *selected;
+ GtkTreeIter iter;
+
+ path = (GtkTreePath *) node->data;
+ gtk_tree_model_get_iter (model, &iter, path);
+ gtk_tree_model_get (model, &iter, 1, &selected, -1);
+ tny_list_append (att_list, selected);
+ }
+ if (tny_list_get_length (att_list) == 0)
+ result = FALSE;
+
+ g_list_foreach (selected_rows, (GFunc) gtk_tree_path_free, NULL);
+ g_list_free (selected_rows);
+ } else {
+ result = FALSE;
+ }
+
+ gtk_widget_destroy (dialog);
+
+ g_object_unref (model);
+
+ return result;
+}
+
+#ifdef MODEST_PLATFORM_MAEMO
+gboolean
+modest_maemo_utils_in_usb_mode ()
+{
+ return modest_conf_get_bool (modest_runtime_get_conf (), INTERNAL_MMC_USB_MODE, NULL);
+}
+#endif
+
+void
+modest_maemo_utils_scroll_pannable (HildonPannableArea *pannable,
+ gint horizontal,
+ gint vertical)
+{
+ gint h_pos = -1;
+ gint v_pos = -1;
+
+ g_assert (pannable);
+ /* at atleast one of values have to be valid */
+ g_return_if_fail (h_pos == -1 && v_pos == -1);
+
+ if (horizontal != 0) {
+ GtkAdjustment *h_adj;
+
+ h_adj = hildon_pannable_area_get_hadjustment (pannable);
+ g_return_if_fail (h_adj);
+
+ h_pos = h_adj->value + h_adj->step_increment * horizontal;
+ if (horizontal > 0) {
+ h_pos += h_adj->page_size;
+ }
+ }
+
+ if (vertical != 0) {
+ GtkAdjustment *v_adj;
+
+ v_adj = hildon_pannable_area_get_vadjustment (pannable);
+ g_return_if_fail (v_adj);
+
+ v_pos = v_adj->value + v_adj->step_increment * vertical;
+ if (vertical > 0) {
+ v_pos += v_adj->page_size;
+ }
+ }
+
+ hildon_pannable_area_scroll_to (pannable, h_pos, v_pos);
+}
+
+#ifdef MODEST_USE_IPHB
+
+typedef struct _ModestHeartbeatSource {
+ GSource source;
+ iphb_t iphb;
+ GPollFD poll;
+ gint interval;
+} ModestHeartbeatSource;
+
+static gboolean modest_heartbeat_prepare (GSource* source, gint *timeout)
+{
+ *timeout = -1;
+ return FALSE;
+}
+
+static gboolean
+modest_heartbeat_check(GSource* source)
+{
+ return ((ModestHeartbeatSource *) source)->poll.revents != 0;
+}
+
+static gboolean modest_heartbeat_dispatch (GSource *source, GSourceFunc callback, gpointer userdata)
+{
+ if (callback(userdata))
+ {
+ ModestHeartbeatSource *hb_source = (ModestHeartbeatSource *) source;
+
+ g_source_remove_poll (source, &(hb_source->poll));
+
+ int min = MAX(hb_source->interval - 30, 5);
+ iphb_wait(hb_source->iphb, min, min + 60, 0);
+
+ hb_source->poll.fd = iphb_get_fd(hb_source->iphb);
+ hb_source->poll.events = G_IO_IN;
+ hb_source->poll.revents = 0;
+
+ g_source_add_poll(source, &(hb_source->poll));
+
+ return TRUE;
+ } else {
+ return FALSE;
+ }
+}
+
+static void
+modest_heartbeat_finalize (GSource* source)
+{
+ ModestHeartbeatSource* hb_source = (ModestHeartbeatSource *) source;
+ hb_source->iphb = iphb_close(hb_source->iphb);
+}
+
+GSourceFuncs modest_heartbeat_funcs =
+{
+ modest_heartbeat_prepare,
+ modest_heartbeat_check,
+ modest_heartbeat_dispatch,
+ modest_heartbeat_finalize,
+ NULL,
+ NULL
+};
+
+static GSource *
+modest_heartbeat_source_new (void)
+{
+ GSource *source;
+ ModestHeartbeatSource *hb_source;
+ iphb_t iphb;
+ int hb_interval;
+
+ source = NULL;
+ hb_interval = 0;
+
+ iphb = iphb_open (&hb_interval);
+
+ if (iphb != 0) {
+ int min;
+ source = g_source_new (&modest_heartbeat_funcs, sizeof (ModestHeartbeatSource));
+ hb_source = (ModestHeartbeatSource *) source;
+ g_source_set_priority (source, G_PRIORITY_DEFAULT_IDLE);
+ hb_source->iphb = iphb;
+ hb_source->interval = hb_interval;
+
+ min = MAX(hb_interval - 30, 5);
+ iphb_wait(hb_source->iphb, min, min + 60, 0);
+
+ hb_source->poll.fd = iphb_get_fd(hb_source->iphb);
+ hb_source->poll.events = G_IO_IN;
+ hb_source->poll.revents = 0;
+
+ g_source_add_poll(source, &(hb_source->poll));
+ } else {
+ source = g_idle_source_new ();
+ }
+
+ return source;
+}
+
+guint
+modest_heartbeat_add (GSourceFunc function,
+ gpointer userdata)
+{
+ GSource *source;
+ guint id;
+
+ source = modest_heartbeat_source_new ();
+ g_source_set_callback (source, function, userdata, NULL);
+ id = g_source_attach (source, NULL);
+ g_source_unref (source);
+
+ return id;
+}
+
+#else
+guint
+modest_heartbeat_add (GSourceFunc function,
+ gpointer userdata)
+{
+ return g_idle_add (function, userdata);
+}
+#endif