1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
30 #ifndef DBUS_API_SUBJECT_TO_CHANGE
31 #define DBUS_API_SUBJECT_TO_CHANGE
32 #endif /*DBUS_API_SUBJECT_TO_CHANGE*/
34 #include <dbus/dbus.h>
35 #include <dbus/dbus-glib-lowlevel.h>
37 #include <glib/gstdio.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"
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>
60 /* Label child of a captioned */
61 #define CAPTIONED_LABEL_CHILD "captioned-label"
63 #ifdef MODEST_PLATFORM_MAEMO
64 #define INTERNAL_MMC_USB_MODE "/system/osso/af/internal-mmc-used-over-usb"
67 static osso_context_t *__osso_context = NULL; /* urgh global */
70 modest_maemo_utils_get_osso_context (void)
73 __osso_context = osso_initialize(PACKAGE,PACKAGE_VERSION,
76 return __osso_context;
80 get_properties_cb (DBusMessage *message)
83 DBusMessageIter dict_iter;
84 DBusMessageIter dict_entry_iter;
86 gchar *bt_name = NULL;
87 int key_type, array_type, msg_type;
89 dbus_error_init(&err);
90 if (dbus_set_error_from_message (&err, message)) {
91 g_warning ("%s: %s", __FUNCTION__, err.message);
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);
99 while ((array_type = dbus_message_iter_get_arg_type (&dict_iter)) == DBUS_TYPE_DICT_ENTRY) {
101 dbus_message_iter_recurse (&dict_iter, &dict_entry_iter);
103 while ((key_type = dbus_message_iter_get_arg_type (&dict_entry_iter)) == DBUS_TYPE_STRING) {
104 DBusMessageIter dict_entry_content_iter;
108 int dict_entry_content_type;
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);
116 if (dict_entry_content_type == DBUS_TYPE_STRING) {
117 dbus_message_iter_get_basic ( &dict_entry_content_iter, &value );
119 if (strcmp (key, "Name") == 0 ) {
120 g_debug ("-------------Name %s", value);
125 dbus_message_iter_next (&dict_entry_iter);
128 if (key_type != DBUS_TYPE_INVALID)
131 dbus_message_iter_next (&dict_iter);
134 /* Save device 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,
144 get_default_adapter_cb (DBusConnection *conn,
145 DBusMessage *message)
147 DBusMessageIter iter;
150 DBusMessage *msg, *adapterMsg;
152 dbus_message_iter_init (message, &iter);
153 dbus_message_iter_get_basic (&iter, &path);
158 adapterMsg = dbus_message_new_method_call ("org.bluez", path,
162 dbus_error_init (&error);
163 msg = dbus_connection_send_with_reply_and_block (conn, adapterMsg, -1, &error);
165 g_debug ("Getting the properties");
166 get_properties_cb (msg);
167 dbus_message_unref (msg);
169 dbus_message_unref (adapterMsg);
173 modest_maemo_utils_get_device_name (void)
175 static DBusConnection *conn = NULL;
176 DBusMessage *request;
180 dbus_error_init (&error);
182 conn = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
184 g_printerr ("modest: cannot get on the dbus: %s: %s\n",
185 error.name, error.message);
186 dbus_error_free (&error);
191 /* Get the default adapter */
192 request = dbus_message_new_method_call ("org.bluez", "/" ,
196 msg = dbus_connection_send_with_reply_and_block (conn, request, -1, &error);
198 g_debug ("Getting the default adapter");
199 get_default_adapter_cb (conn, msg);
200 dbus_message_unref (msg);
203 dbus_message_unref (request);
204 if (dbus_error_is_set (&error))
205 dbus_error_free (&error);
209 modest_maemo_utils_setup_images_filechooser (GtkFileChooser *chooser)
211 GtkFileFilter *file_filter;
212 GList *image_mimetypes_list;
216 g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser));
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);
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) {
228 images_folder = g_build_filename (g_getenv (MODEST_MAEMO_UTILS_MYDOCS_ENV),
229 MODEST_MAEMO_UTILS_DEFAULT_IMAGE_FOLDER, NULL);
231 gtk_file_chooser_set_current_folder (chooser, images_folder);
232 g_free (images_folder);
234 g_free (conf_folder);
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);
242 gtk_file_chooser_set_filter (chooser, file_filter);
243 hildon_mime_types_list_free (image_mimetypes_list);
248 modest_maemo_utils_get_manager_menubar_as_menu (GtkUIManager *manager,
249 const gchar *item_name)
253 GList *children, *iter;
255 menubar = gtk_ui_manager_get_widget (manager, item_name);
256 new_menu = gtk_menu_new ();
258 children = gtk_container_get_children (GTK_CONTAINER (menubar));
259 for (iter = children; iter != NULL; iter = g_list_next (iter)) {
262 menu = GTK_WIDGET (iter->data);
263 gtk_widget_reparent (menu, new_menu);
266 g_list_free (children);
272 * modest_maemo_utils_create_captioned:
273 * @title_size_group: a #GtkSizeGroup
274 * @value_size_group: a #GtkSizeGroup
276 * @control: a #GtkWidget
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
282 * Returns: a widget containing the control and a proper label.
285 modest_maemo_utils_create_captioned (GtkSizeGroup *title_size_group,
286 GtkSizeGroup *value_size_group,
291 return modest_maemo_utils_create_captioned_with_size_type (title_size_group,
300 * modest_maemo_utils_create_captioned_with_size_type:
301 * @title_size_group: a #GtkSizeGroup
302 * @value_size_group: a #GtkSizeGroup
304 * @control: a #GtkWidget
305 * @size_type: a #HildonSizeType
307 * this creates a widget (a #GtkHBox) with a control, and a label
308 * (@string) captioning it. It also uses the proper size groups for title
311 * Returns: a widget containing the control and a proper label.
314 modest_maemo_utils_create_captioned_with_size_type (GtkSizeGroup *title_size_group,
315 GtkSizeGroup *value_size_group,
319 HildonSizeType size_type)
326 label = gtk_label_new (NULL);
327 gtk_label_set_markup (GTK_LABEL (label), title);
329 label = gtk_label_new (title);
331 align = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
332 gtk_alignment_set_padding (GTK_ALIGNMENT (align), 0, 0, MODEST_MARGIN_DOUBLE, MODEST_MARGIN_DOUBLE);
334 gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
335 hildon_gtk_widget_set_theme_size (label, HILDON_SIZE_FINGER_HEIGHT);
336 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
337 gtk_widget_show (label);
338 gtk_widget_show (align);
339 box = gtk_hbox_new (FALSE, 0);
340 gtk_container_add (GTK_CONTAINER (align), label);
341 gtk_box_pack_start (GTK_BOX (box), align, FALSE, FALSE, 0);
342 gtk_box_pack_start (GTK_BOX (box), control, TRUE, TRUE, 0);
343 if (title_size_group)
344 gtk_size_group_add_widget (title_size_group, label);
345 if (value_size_group)
346 gtk_size_group_add_widget (value_size_group, control);
348 hildon_gtk_widget_set_theme_size (control, size_type);
350 g_object_set_data (G_OBJECT (box), CAPTIONED_LABEL_CHILD, label);
356 * modest_maemo_utils_captioned_set_label:
357 * @captioned: a #GtkWidget built as captioned
358 * @new_label: a string
359 * @use_markup: a #gboolean
361 * set a new label for the captioned
364 modest_maemo_utils_captioned_set_label (GtkWidget *captioned,
365 const gchar *new_label,
370 g_return_if_fail (GTK_IS_WIDGET (captioned));
372 label = g_object_get_data (G_OBJECT (captioned), CAPTIONED_LABEL_CHILD);
373 g_return_if_fail (GTK_IS_LABEL (label));
376 gtk_label_set_markup (GTK_LABEL (label), new_label);
378 gtk_label_set_text (GTK_LABEL (label), new_label);
383 * modest_maemo_utils_captioned_get_label_widget:
384 * @captioned: a #GtkWidget built as captioned
386 * obtains the label widget for the captioned
388 * Returns: a #GtkLabel
391 modest_maemo_utils_captioned_get_label_widget (GtkWidget *captioned)
395 g_return_val_if_fail (GTK_IS_WIDGET (captioned), NULL);
397 label = g_object_get_data (G_OBJECT (captioned), CAPTIONED_LABEL_CHILD);
398 g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
405 * modest_maemo_utils_set_hbutton_layout:
406 * @title_sizegroup: a #GtkSizeGroup, or %NULL
407 * @value_sizegroup: a #GtkSizeGroup, or %NULL
409 * @button: a #HildonButton
411 * Configures the alignment and layout of @button. If @title_sizegroup is provided,
412 * the title will be aligned to the left using it. If @value_sizegroup is provided,
413 * the value will be aligned to the left using it. It also sets the title
416 * The alignment is left for the title and for the value.
419 modest_maemo_utils_set_hbutton_layout (GtkSizeGroup *title_sizegroup,
420 GtkSizeGroup *value_sizegroup,
424 hildon_button_set_title (HILDON_BUTTON (button), title);
426 hildon_button_add_title_size_group (HILDON_BUTTON (button), title_sizegroup);
428 hildon_button_add_value_size_group (HILDON_BUTTON (button), value_sizegroup);
429 hildon_button_set_alignment (HILDON_BUTTON (button), 0.0, 0.5, 1.0, 0.0);
430 hildon_button_set_title_alignment (HILDON_BUTTON (button), 0.0, 0.5);
431 hildon_button_set_value_alignment (HILDON_BUTTON (button), 0.0, 0.5);
435 modest_maemo_utils_set_vbutton_layout (GtkSizeGroup *sizegroup,
439 hildon_button_set_title (HILDON_BUTTON (button), title);
441 hildon_button_add_title_size_group (HILDON_BUTTON (button), sizegroup);
442 hildon_button_add_value_size_group (HILDON_BUTTON (button), sizegroup);
444 hildon_button_set_alignment (HILDON_BUTTON (button), 0.0, 0.5, 1.0, 0.0);
445 hildon_button_set_title_alignment (HILDON_BUTTON (button), 0.0, 0.5);
446 hildon_button_set_value_alignment (HILDON_BUTTON (button), 0.0, 0.5);
450 modest_maemo_utils_create_group_box (const gchar *label_text, GtkWidget *contents)
455 label = gtk_label_new (label_text);
456 gtk_widget_show (label);
458 box = gtk_vbox_new (FALSE, MODEST_MARGIN_HALF);
459 gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
460 gtk_box_pack_start (GTK_BOX (box), contents, TRUE, TRUE, 0);
461 gtk_widget_show (box);
466 static gboolean match_all (TnyList *list, GObject *item, gpointer match_data)
472 modest_maemo_utils_select_attachments (GtkWindow *window, TnyList *att_list, gboolean include_msgs)
475 TnyIterator *iterator;
477 GtkCellRenderer *renderer;
480 gboolean result = TRUE;
481 gint attachments_added = 0;
483 model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_OBJECT));
484 for (iterator = tny_list_create_iterator (att_list);
485 !tny_iterator_is_done (iterator);
486 tny_iterator_next (iterator)) {
489 gchar *filename = NULL;
491 part = (TnyMimePart *) tny_iterator_get_current (iterator);
493 /* Ignore purged attachments and messages if ignore is
495 if (!(tny_mime_part_is_purged (part) ||
496 (TNY_IS_MSG (part) && !include_msgs))) {
498 if (TNY_IS_MSG (part)) {
499 TnyHeader *header = tny_msg_get_header (TNY_MSG (part));
500 filename = tny_header_dup_subject (header);
501 g_object_unref (header);
503 filename = g_strdup (tny_mime_part_get_filename (part));
505 if ((filename == NULL) || (filename[0] == '\0')) {
507 filename = g_strdup (_("mail_va_no_subject"));
509 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
510 gtk_list_store_set (GTK_LIST_STORE (model), &iter, 0, filename, 1, part, -1);
511 attachments_added ++;
514 g_object_unref (part);
516 g_object_unref (iterator);
518 selector = GTK_WIDGET (hildon_touch_selector_new ());
519 renderer = gtk_cell_renderer_text_new ();
520 g_object_set((GObject *) renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
521 hildon_touch_selector_append_column ((HildonTouchSelector *) selector, model, renderer,
523 hildon_touch_selector_set_column_selection_mode ((HildonTouchSelector *) selector,
524 HILDON_TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE);
526 dialog = hildon_picker_dialog_new (window);
527 gtk_window_set_title (GTK_WINDOW (dialog), (attachments_added > 1)?
528 _("mcen_ti_select_attachments_title"):_("mcen_ti_select_attachment_title"));
529 hildon_picker_dialog_set_selector (HILDON_PICKER_DIALOG (dialog), (HildonTouchSelector *) selector);
530 hildon_touch_selector_unselect_all ((HildonTouchSelector *) selector, 0);
531 hildon_picker_dialog_set_done_label (HILDON_PICKER_DIALOG (dialog), _HL("wdgt_bd_done"));
533 response = gtk_dialog_run (GTK_DIALOG (dialog));
535 if (response == GTK_RESPONSE_OK) {
536 GList *selected_rows, *node;
538 tny_list_remove_matches (att_list, match_all, NULL);
539 selected_rows = hildon_touch_selector_get_selected_rows ((HildonTouchSelector *) selector, 0);
540 for (node = selected_rows; node != NULL; node = g_list_next (node)) {
545 path = (GtkTreePath *) node->data;
546 gtk_tree_model_get_iter (model, &iter, path);
547 gtk_tree_model_get (model, &iter, 1, &selected, -1);
548 tny_list_append (att_list, selected);
550 if (tny_list_get_length (att_list) == 0)
553 g_list_foreach (selected_rows, (GFunc) gtk_tree_path_free, NULL);
554 g_list_free (selected_rows);
559 gtk_widget_destroy (dialog);
561 g_object_unref (model);
566 #ifdef MODEST_PLATFORM_MAEMO
568 modest_maemo_utils_in_usb_mode ()
570 return modest_conf_get_bool (modest_runtime_get_conf (), INTERNAL_MMC_USB_MODE, NULL);
575 modest_maemo_utils_scroll_pannable (HildonPannableArea *pannable,
583 /* at atleast one of values have to be valid */
584 g_return_if_fail (h_pos == -1 && v_pos == -1);
586 if (horizontal != 0) {
587 GtkAdjustment *h_adj;
589 h_adj = hildon_pannable_area_get_hadjustment (pannable);
590 g_return_if_fail (h_adj);
592 h_pos = h_adj->value + h_adj->step_increment * horizontal;
593 if (horizontal > 0) {
594 h_pos += h_adj->page_size;
599 GtkAdjustment *v_adj;
601 v_adj = hildon_pannable_area_get_vadjustment (pannable);
602 g_return_if_fail (v_adj);
604 v_pos = v_adj->value + v_adj->step_increment * vertical;
606 v_pos += v_adj->page_size;
610 hildon_pannable_area_scroll_to (pannable, h_pos, v_pos);
613 #ifdef MODEST_USE_IPHB
615 typedef struct _ModestHeartbeatSource {
620 } ModestHeartbeatSource;
622 static gboolean modest_heartbeat_prepare (GSource* source, gint *timeout)
629 modest_heartbeat_check(GSource* source)
631 return ((ModestHeartbeatSource *) source)->poll.revents != 0;
634 static gboolean modest_heartbeat_dispatch (GSource *source, GSourceFunc callback, gpointer userdata)
636 if (callback(userdata))
638 ModestHeartbeatSource *hb_source = (ModestHeartbeatSource *) source;
640 g_source_remove_poll (source, &(hb_source->poll));
642 int min = MAX(hb_source->interval - 30, 5);
643 iphb_wait(hb_source->iphb, min, min + 60, 0);
645 hb_source->poll.fd = iphb_get_fd(hb_source->iphb);
646 hb_source->poll.events = G_IO_IN;
647 hb_source->poll.revents = 0;
649 g_source_add_poll(source, &(hb_source->poll));
658 modest_heartbeat_finalize (GSource* source)
660 ModestHeartbeatSource* hb_source = (ModestHeartbeatSource *) source;
661 hb_source->iphb = iphb_close(hb_source->iphb);
664 GSourceFuncs modest_heartbeat_funcs =
666 modest_heartbeat_prepare,
667 modest_heartbeat_check,
668 modest_heartbeat_dispatch,
669 modest_heartbeat_finalize,
675 modest_heartbeat_source_new (void)
678 ModestHeartbeatSource *hb_source;
685 iphb = iphb_open (&hb_interval);
689 source = g_source_new (&modest_heartbeat_funcs, sizeof (ModestHeartbeatSource));
690 hb_source = (ModestHeartbeatSource *) source;
691 g_source_set_priority (source, G_PRIORITY_DEFAULT_IDLE);
692 hb_source->iphb = iphb;
693 hb_source->interval = hb_interval;
695 min = MAX(hb_interval - 30, 5);
696 iphb_wait(hb_source->iphb, min, min + 60, 0);
698 hb_source->poll.fd = iphb_get_fd(hb_source->iphb);
699 hb_source->poll.events = G_IO_IN;
700 hb_source->poll.revents = 0;
702 g_source_add_poll(source, &(hb_source->poll));
704 source = g_idle_source_new ();
711 modest_heartbeat_add (GSourceFunc function,
717 source = modest_heartbeat_source_new ();
718 g_source_set_callback (source, function, userdata, NULL);
719 id = g_source_attach (source, NULL);
720 g_source_unref (source);
727 modest_heartbeat_add (GSourceFunc function,
730 return g_idle_add (function, userdata);