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