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