X-Git-Url: http://git.maemo.org/git/?p=modest;a=blobdiff_plain;f=src%2Fhildon2%2Fmodest-maemo-utils.c;h=40269d6765ad30b3b598a74cbe83a45c89d94de0;hp=b8078f47c787a33b53aa2b86443d2cc636005e4f;hb=2dea0f12d724484854dd1bf75c0bad05fa151ab2;hpb=cca502af5db5549021fdf2aca1a57a8333a07fac diff --git a/src/hildon2/modest-maemo-utils.c b/src/hildon2/modest-maemo-utils.c index b8078f4..40269d6 100644 --- a/src/hildon2/modest-maemo-utils.c +++ b/src/hildon2/modest-maemo-utils.c @@ -31,7 +31,6 @@ #define DBUS_API_SUBJECT_TO_CHANGE #endif /*DBUS_API_SUBJECT_TO_CHANGE*/ -#include #include #include #include @@ -54,25 +53,16 @@ #include "modest-platform.h" #include "modest-ui-constants.h" #include - -/* - * For getting and tracking the Bluetooth name - */ -#define BTNAME_SERVICE "org.bluez" -#define BTNAME_REQUEST_IF "org.bluez.Adapter" -#define BTNAME_SIGNAL_IF "org.bluez.Adapter" -#define BTNAME_REQUEST_PATH "/org/bluez/hci0" -#define BTNAME_SIGNAL_PATH "/org/bluez/hci0" - -#define BTNAME_REQ_GET "GetName" -#define BTNAME_SIG_CHANGED "NameChanged" - -#define BTNAME_MATCH_RULE "type='signal',interface='" BTNAME_SIGNAL_IF \ - "',member='" BTNAME_SIG_CHANGED "'" +#ifdef MODEST_USE_IPHB +#include +#endif /* Label child of a captioned */ #define CAPTIONED_LABEL_CHILD "captioned-label" +#ifdef MODEST_PLATFORM_MAEMO +#define INTERNAL_MMC_USB_MODE "/system/osso/af/internal-mmc-used-over-usb" +#endif static osso_context_t *__osso_context = NULL; /* urgh global */ @@ -80,109 +70,178 @@ osso_context_t * modest_maemo_utils_get_osso_context (void) { if (!__osso_context) - g_warning ("%s: __osso_context == NULL", __FUNCTION__); + __osso_context = osso_initialize(PACKAGE,PACKAGE_VERSION, + FALSE, NULL); return __osso_context; } -void -modest_maemo_utils_set_osso_context (osso_context_t *osso_context) +static void +get_properties_cb (DBusMessage *message) +{ + DBusMessageIter iter; + DBusMessageIter dict_iter; + DBusMessageIter dict_entry_iter; + DBusError err; + gchar *bt_name = NULL; + int key_type, array_type, msg_type; + + dbus_error_init(&err); + if (dbus_set_error_from_message (&err, message)) { + g_warning ("%s: %s", __FUNCTION__, err.message); + } + + /* Get msg type */ + dbus_message_iter_init (message, &iter); + msg_type = dbus_message_iter_get_arg_type (&iter); + dbus_message_iter_recurse (&iter, &dict_iter); + + while ((array_type = dbus_message_iter_get_arg_type (&dict_iter)) == DBUS_TYPE_DICT_ENTRY) { + + dbus_message_iter_recurse (&dict_iter, &dict_entry_iter); + + while ((key_type = dbus_message_iter_get_arg_type (&dict_entry_iter)) == DBUS_TYPE_STRING) { + DBusMessageIter dict_entry_content_iter; + char *key; + char *value; + int dict_entry_type; + int dict_entry_content_type; + + dbus_message_iter_get_basic (&dict_entry_iter, &key); + dbus_message_iter_next (&dict_entry_iter); + dict_entry_type = dbus_message_iter_get_arg_type (&dict_entry_iter); + dbus_message_iter_recurse (&dict_entry_iter, &dict_entry_content_iter); + dict_entry_content_type = dbus_message_iter_get_arg_type (&dict_entry_content_iter); + + if (dict_entry_content_type == DBUS_TYPE_STRING) { + dbus_message_iter_get_basic ( &dict_entry_content_iter, &value ); + + if (strcmp (key, "Name") == 0 ) { + g_debug ("-------------Name %s", value); + bt_name = value; + break; + } + } + dbus_message_iter_next (&dict_entry_iter); + } + + if (key_type != DBUS_TYPE_INVALID) + break; + + dbus_message_iter_next (&dict_iter); + } + + /* Save device name */ + if (bt_name) { + g_debug ("Setting the device name %s", bt_name); + modest_conf_set_string (modest_runtime_get_conf(), + MODEST_CONF_DEVICE_NAME, bt_name, + NULL); + } +} + +static void +get_default_adapter_cb (DBusConnection *conn, + DBusMessage *message) { - g_return_if_fail (osso_context); - __osso_context = osso_context; + DBusMessageIter iter; + gchar* path = NULL; + DBusError error; + DBusMessage *msg, *adapterMsg; + + dbus_message_iter_init (message, &iter); + dbus_message_iter_get_basic (&iter, &path); + + if (!path) + return; + + adapterMsg = dbus_message_new_method_call ("org.bluez", path, + "org.bluez.Adapter", + "GetProperties"); + + dbus_error_init (&error); + msg = dbus_connection_send_with_reply_and_block (conn, adapterMsg, -1, &error); + if (msg) { + g_debug ("Getting the properties"); + get_properties_cb (msg); + dbus_message_unref (msg); + } + dbus_message_unref (adapterMsg); } void modest_maemo_utils_get_device_name (void) { - struct utsname name; + static DBusConnection *conn = NULL; + DBusMessage *request; + DBusError error; + DBusMessage *msg; + + dbus_error_init (&error); + if (!conn) { + conn = dbus_bus_get (DBUS_BUS_SYSTEM, &error); + if (!conn) { + g_printerr ("modest: cannot get on the dbus: %s: %s\n", + error.name, error.message); + dbus_error_free (&error); + return; + } + } - if (uname (&name) == 0) { - modest_conf_set_string (modest_runtime_get_conf(), - MODEST_CONF_DEVICE_NAME, name.nodename, - NULL); + /* Get the default adapter */ + request = dbus_message_new_method_call ("org.bluez", "/" , + "org.bluez.Manager", + "DefaultAdapter"); + + msg = dbus_connection_send_with_reply_and_block (conn, request, -1, &error); + if (msg) { + g_debug ("Getting the default adapter"); + get_default_adapter_cb (conn, msg); + dbus_message_unref (msg); } + + dbus_message_unref (request); + if (dbus_error_is_set (&error)) + dbus_error_free (&error); } void modest_maemo_utils_setup_images_filechooser (GtkFileChooser *chooser) { - gchar *images_folder; GtkFileFilter *file_filter; GList *image_mimetypes_list; GList *node; + gchar *conf_folder; g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser)); - /* Set the default folder to images folder */ - images_folder = g_build_filename (g_getenv (MODEST_MAEMO_UTILS_MYDOCS_ENV), - MODEST_MAEMO_UTILS_DEFAULT_IMAGE_FOLDER, NULL); - gtk_file_chooser_set_current_folder (chooser, images_folder); - g_free (images_folder); + conf_folder = modest_conf_get_string (modest_runtime_get_conf (), + MODEST_CONF_LATEST_INSERT_IMAGE_PATH, NULL); + if (conf_folder && conf_folder[0] != '\0') { + gtk_file_chooser_set_current_folder_uri (chooser, conf_folder); + } else { + gchar *images_folder; + /* Set the default folder to images folder */ + images_folder = (gchar *) g_strdup(g_get_user_special_dir (G_USER_DIRECTORY_PICTURES)); + if (!images_folder) { + /* fallback */ + images_folder = g_build_filename (g_getenv (MODEST_MAEMO_UTILS_MYDOCS_ENV), + MODEST_MAEMO_UTILS_DEFAULT_IMAGE_FOLDER, NULL); + } + gtk_file_chooser_set_current_folder (chooser, images_folder); + g_free (images_folder); + } + g_free (conf_folder); /* Set the images mime filter */ file_filter = gtk_file_filter_new (); -#ifdef MODEST_HAVE_HILDON0_WIDGETS - image_mimetypes_list = osso_mime_get_mime_types_for_category (OSSO_MIME_CATEGORY_IMAGES); -#else image_mimetypes_list = hildon_mime_get_mime_types_for_category (HILDON_MIME_CATEGORY_IMAGES); -#endif for (node = image_mimetypes_list; node != NULL; node = g_list_next (node)) { gtk_file_filter_add_mime_type (file_filter, node->data); } gtk_file_chooser_set_filter (chooser, file_filter); -#ifdef MODEST_HAVE_HILDON0_WIDGETS - osso_mime_types_list_free (image_mimetypes_list); -#else hildon_mime_types_list_free (image_mimetypes_list); -#endif - -} -void -modest_maemo_set_thumbable_scrollbar (GtkScrolledWindow *win, - gboolean thumbable) -{ - g_return_if_fail (GTK_IS_SCROLLED_WINDOW(win)); -#ifdef MODEST_HAVE_HILDON1_WIDGETS - hildon_helper_set_thumb_scrollbar (win, thumbable); -#endif /* MODEST_HAVE_HILDON1_WIDGETS */ -} - -FILE* -modest_maemo_open_mcc_mapping_file (gboolean *translated) -{ - FILE* result = NULL; - const gchar* path; - gchar *path1 = g_strdup_printf ("%s.%s", MODEST_OPERATOR_WIZARD_MCC_MAPPING, getenv("LANG")); - const gchar* path2 = MODEST_MCC_MAPPING; - - if (translated) - *translated = TRUE; - - if (access (path1, R_OK) == 0) { - path = path1; - } else if (access (MODEST_OPERATOR_WIZARD_MCC_MAPPING, R_OK) == 0) { - path = MODEST_OPERATOR_WIZARD_MCC_MAPPING; - if (translated) - *translated = FALSE; - } else if (access (path2, R_OK) == 0) { - path = path2; - } else { - g_warning ("%s: neither '%s' nor '%s' is a readable mapping file", - __FUNCTION__, path1, path2); - goto end; - } - - result = fopen (path, "r"); - if (!result) { - g_warning ("%s: error opening mapping file '%s': %s", - __FUNCTION__, path, strerror(errno)); - goto end; - } - end: - g_free (path1); - return result; } GtkWidget * @@ -234,7 +293,7 @@ modest_maemo_utils_create_captioned (GtkSizeGroup *title_size_group, title, use_markup, control, - HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH); + 0); } /** @@ -270,7 +329,7 @@ modest_maemo_utils_create_captioned_with_size_type (GtkSizeGroup *title_size_ label = gtk_label_new (title); } align = gtk_alignment_new (0.0, 0.0, 1.0, 1.0); - gtk_alignment_set_padding (GTK_ALIGNMENT (align), 0, 0, MODEST_MARGIN_DOUBLE, MODEST_MARGIN_TRIPLE); + gtk_alignment_set_padding (GTK_ALIGNMENT (align), 0, 0, MODEST_MARGIN_DOUBLE, MODEST_MARGIN_DOUBLE); gtk_label_set_line_wrap (GTK_LABEL (label), TRUE); hildon_gtk_widget_set_theme_size (label, HILDON_SIZE_FINGER_HEIGHT); @@ -321,6 +380,28 @@ modest_maemo_utils_captioned_set_label (GtkWidget *captioned, } /** + * modest_maemo_utils_captioned_get_label_widget: + * @captioned: a #GtkWidget built as captioned + * + * obtains the label widget for the captioned + * + * Returns: a #GtkLabel + */ +GtkWidget * +modest_maemo_utils_captioned_get_label_widget (GtkWidget *captioned) +{ + GtkWidget *label; + + g_return_val_if_fail (GTK_IS_WIDGET (captioned), NULL); + + label = g_object_get_data (G_OBJECT (captioned), CAPTIONED_LABEL_CHILD); + g_return_val_if_fail (GTK_IS_LABEL (label), NULL); + + return label; + +} + +/** * modest_maemo_utils_set_hbutton_layout: * @title_sizegroup: a #GtkSizeGroup, or %NULL * @value_sizegroup: a #GtkSizeGroup, or %NULL @@ -388,7 +469,7 @@ static gboolean match_all (TnyList *list, GObject *item, gpointer match_data) } gboolean -modest_maemo_utils_select_attachments (GtkWindow *window, TnyList *att_list) +modest_maemo_utils_select_attachments (GtkWindow *window, TnyList *att_list, gboolean include_msgs) { GtkTreeModel *model; TnyIterator *iterator; @@ -397,6 +478,7 @@ modest_maemo_utils_select_attachments (GtkWindow *window, TnyList *att_list) GtkWidget *dialog; gint response; gboolean result = TRUE; + gint attachments_added = 0; model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_OBJECT)); for (iterator = tny_list_create_iterator (att_list); @@ -404,33 +486,46 @@ modest_maemo_utils_select_attachments (GtkWindow *window, TnyList *att_list) tny_iterator_next (iterator)) { GtkTreeIter iter; TnyMimePart *part; + gchar *filename = NULL; part = (TnyMimePart *) tny_iterator_get_current (iterator); - /* Embbeded messages are not offered to be - saved. Purged attachments are ignored as well */ - if (!TNY_IS_MSG (part) && !tny_mime_part_is_purged (part)) { - gchar *label; - gchar *filename = NULL; - - filename = g_strdup (tny_mime_part_get_filename (part)); - label = g_strconcat (filename, NULL); + /* Ignore purged attachments and messages if ignore is + set to TRUE */ + if (!(tny_mime_part_is_purged (part) || + (TNY_IS_MSG (part) && !include_msgs))) { + + if (TNY_IS_MSG (part)) { + TnyHeader *header = tny_msg_get_header (TNY_MSG (part)); + filename = tny_header_dup_subject (header); + g_object_unref (header); + } else { + filename = g_strdup (tny_mime_part_get_filename (part)); + } + if ((filename == NULL) || (filename[0] == '\0')) { + g_free (filename); + filename = g_strdup (_("mail_va_no_subject")); + } gtk_list_store_append (GTK_LIST_STORE (model), &iter); - gtk_list_store_set (GTK_LIST_STORE (model), &iter, 0, label, 1, part, -1); - g_free (label); - g_object_unref (part); + gtk_list_store_set (GTK_LIST_STORE (model), &iter, 0, filename, 1, part, -1); + attachments_added ++; + g_free (filename); } + g_object_unref (part); } + g_object_unref (iterator); selector = GTK_WIDGET (hildon_touch_selector_new ()); renderer = gtk_cell_renderer_text_new (); + g_object_set((GObject *) renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL); hildon_touch_selector_append_column (HILDON_TOUCH_SELECTOR (selector), model, renderer, "text", 0, NULL); hildon_touch_selector_set_column_selection_mode (HILDON_TOUCH_SELECTOR (selector), HILDON_TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE); dialog = hildon_picker_dialog_new (window); - gtk_window_set_title (GTK_WINDOW (dialog), _("mcen_ti_select_attachment_title")); + gtk_window_set_title (GTK_WINDOW (dialog), (attachments_added > 1)? + _("mcen_ti_select_attachments_title"):_("mcen_ti_select_attachment_title")); hildon_picker_dialog_set_selector (HILDON_PICKER_DIALOG (dialog), HILDON_TOUCH_SELECTOR (selector)); hildon_picker_dialog_set_done_label (HILDON_PICKER_DIALOG (dialog), _HL("wdgt_bd_done")); @@ -453,6 +548,9 @@ modest_maemo_utils_select_attachments (GtkWindow *window, TnyList *att_list) } if (tny_list_get_length (att_list) == 0) result = FALSE; + + g_list_foreach (selected_rows, (GFunc) gtk_tree_path_free, NULL); + g_list_free (selected_rows); } else { result = FALSE; } @@ -463,3 +561,132 @@ modest_maemo_utils_select_attachments (GtkWindow *window, TnyList *att_list) return result; } + +#ifdef MODEST_PLATFORM_MAEMO +gboolean +modest_maemo_utils_in_usb_mode () +{ + return modest_conf_get_bool (modest_runtime_get_conf (), INTERNAL_MMC_USB_MODE, NULL); +} +#endif + +#ifdef MODEST_USE_IPHB + +typedef struct _ModestHeartbeatSource { + GSource source; + iphb_t iphb; + GPollFD poll; + gint interval; +} ModestHeartbeatSource; + +static gboolean modest_heartbeat_prepare (GSource* source, gint *timeout) +{ + *timeout = -1; + return FALSE; +} + +static gboolean +modest_heartbeat_check(GSource* source) +{ + return ((ModestHeartbeatSource *) source)->poll.revents != 0; +} + +static gboolean modest_heartbeat_dispatch (GSource *source, GSourceFunc callback, gpointer userdata) +{ + if (callback(userdata)) + { + ModestHeartbeatSource *hb_source = (ModestHeartbeatSource *) source; + + g_source_remove_poll (source, &(hb_source->poll)); + + int min = MAX(hb_source->interval - 30, 5); + iphb_wait(hb_source->iphb, min, min + 60, 0); + + hb_source->poll.fd = iphb_get_fd(hb_source->iphb); + hb_source->poll.events = G_IO_IN; + hb_source->poll.revents = 0; + + g_source_add_poll(source, &(hb_source->poll)); + + return TRUE; + } else { + return FALSE; + } +} + +static void +modest_heartbeat_finalize (GSource* source) +{ + ModestHeartbeatSource* hb_source = (ModestHeartbeatSource *) source; + hb_source->iphb = iphb_close(hb_source->iphb); +} + +GSourceFuncs modest_heartbeat_funcs = +{ + modest_heartbeat_prepare, + modest_heartbeat_check, + modest_heartbeat_dispatch, + modest_heartbeat_finalize, + NULL, + NULL +}; + +static GSource * +modest_heartbeat_source_new (void) +{ + GSource *source; + ModestHeartbeatSource *hb_source; + iphb_t iphb; + int hb_interval; + + source = NULL; + hb_interval = 0; + + iphb = iphb_open (&hb_interval); + + if (iphb != 0) { + int min; + source = g_source_new (&modest_heartbeat_funcs, sizeof (ModestHeartbeatSource)); + hb_source = (ModestHeartbeatSource *) source; + g_source_set_priority (source, G_PRIORITY_DEFAULT_IDLE); + hb_source->iphb = iphb; + hb_source->interval = hb_interval; + + min = MAX(hb_interval - 30, 5); + iphb_wait(hb_source->iphb, min, min + 60, 0); + + hb_source->poll.fd = iphb_get_fd(hb_source->iphb); + hb_source->poll.events = G_IO_IN; + hb_source->poll.revents = 0; + + g_source_add_poll(source, &(hb_source->poll)); + } else { + source = g_idle_source_new (); + } + + return source; +} + +guint +modest_heartbeat_add (GSourceFunc function, + gpointer userdata) +{ + GSource *source; + guint id; + + source = modest_heartbeat_source_new (); + g_source_set_callback (source, function, userdata, NULL); + id = g_source_attach (source, NULL); + g_source_unref (source); + + return id; +} + +#else +guint +modest_heartbeat_add (GSourceFunc function, + gpointer userdata) +{ + return g_idle_add (function, userdata); +} +#endif