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