X-Git-Url: http://git.maemo.org/git/?a=blobdiff_plain;f=src%2Fhildon2%2Fmodest-maemo-utils.c;h=dd71fcf4ea7bb3c70ab7f970ddbf045769a7f367;hb=02f0668bb12c4fccb22f0fef53548b8e0f39698f;hp=5b02b207aa6d8dba3db543c55d18d273d9a1b80a;hpb=d35a1ca8a2e4e905cdb7d5925c73c078ba7da3e3;p=modest diff --git a/src/hildon2/modest-maemo-utils.c b/src/hildon2/modest-maemo-utils.c index 5b02b20..dd71fcf 100644 --- a/src/hildon2/modest-maemo-utils.c +++ b/src/hildon2/modest-maemo-utils.c @@ -53,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_SIGNAL_PATH "/org/bluez/hci0" - -#define BTNAME_REQ_GET_PROPERTIES "GetProperties" -#define BTNAME_REQ_DEFAULT_ADAPTER "DefaultAdapter" -#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 */ @@ -79,97 +70,27 @@ 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) -{ - g_return_if_fail (osso_context); - __osso_context = osso_context; -} - -/* static void */ -/* update_device_name_from_msg (DBusMessage *message) */ -/* { */ -/* DBusError error; */ -/* DBusMessageIter iter; */ - -/* dbus_error_init (&error); */ - -/* if (dbus_set_error_from_message (&error, message)) { */ -/* g_printerr ("modest: failed to get bluetooth name: %s\n", error.message); */ -/* dbus_error_free (&error); */ -/* } else { */ -/* const gchar *device_name; */ -/* if (!dbus_message_iter_init (message, &iter)) { */ -/* g_printerr ("modest: message did not have argument\n"); */ -/* return; */ -/* } */ -/* dbus_message_iter_get_basic (&iter, &device_name); */ -/* modest_conf_set_string (modest_runtime_get_conf(), */ -/* MODEST_CONF_DEVICE_NAME, device_name, */ -/* NULL); */ -/* } */ -/* } */ - - -/* static void */ -/* on_device_name_received (DBusPendingCall *call, void *user_data) */ -/* { */ -/* DBusMessage *message; */ - -/* g_return_if_fail (dbus_pending_call_get_completed (call)); */ - -/* message = dbus_pending_call_steal_reply (call); */ -/* if (!message) { */ -/* g_printerr ("modest: no reply on device name query\n"); */ -/* return; */ -/* } */ - -/* update_device_name_from_msg (message); */ -/* dbus_message_unref (message); */ -/* } */ - - -/* static DBusHandlerResult */ -/* handle_dbus_signal (DBusConnection *conn, DBusMessage *msg, gpointer data) */ -/* { */ -/* if (dbus_message_is_signal(msg, BTNAME_SIGNAL_IF, BTNAME_SIG_CHANGED)) */ -/* update_device_name_from_msg (msg); */ - -/* return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; */ -/* } */ - static void -get_properties_cb (DBusPendingCall *call, - void *user_data) +get_properties_cb (DBusMessage *message) { DBusMessageIter iter; DBusMessageIter dict_iter; DBusMessageIter dict_entry_iter; DBusError err; gchar *bt_name = NULL; - DBusMessage *message; int key_type, array_type, msg_type; - if ( !dbus_pending_call_get_completed ( call ) ) - g_warning ("%s: Call not completed!", __FUNCTION__); - - message = dbus_pending_call_steal_reply ( call ); - - if (message == NULL) - g_warning ("%s: Message is NULL", __FUNCTION__); - - 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); @@ -195,8 +116,9 @@ get_properties_cb (DBusPendingCall *call, if (dict_entry_content_type == DBUS_TYPE_STRING) { dbus_message_iter_get_basic ( &dict_entry_content_iter, &value ); - if ( strcmp ( key, "Name" ) == 0 ) { + if (strcmp (key, "Name") == 0 ) { g_debug ("-------------Name %s", value); + bt_name = value; break; } } @@ -211,6 +133,7 @@ get_properties_cb (DBusPendingCall *call, /* 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); @@ -218,36 +141,32 @@ get_properties_cb (DBusPendingCall *call, } static void -get_default_adapter_cb (DBusPendingCall *call, - void *user_data) +get_default_adapter_cb (DBusConnection *conn, + DBusMessage *message) { - DBusMessage *message; DBusMessageIter iter; gchar* path = NULL; + DBusError error; + DBusMessage *msg, *adapterMsg; - message = dbus_pending_call_steal_reply(call); + dbus_message_iter_init (message, &iter); + dbus_message_iter_get_basic (&iter, &path); - // Todo extract msg info from here - dbus_message_iter_init ( message, &iter ); + if (!path) + return; - dbus_message_iter_get_basic (&iter, &path); - if (path != NULL) { - DBusConnection *conn; - DBusMessage *adapterMsg = dbus_message_new_method_call(BTNAME_SERVICE, path, - BTNAME_REQUEST_IF, - BTNAME_REQ_GET_PROPERTIES); - DBusPendingCall *call = NULL; - - conn = dbus_bus_get (DBUS_BUS_SYSTEM, NULL); - - if (conn && dbus_connection_send_with_reply(conn, adapterMsg, &call, -1) ) { - dbus_pending_call_set_notify(call, get_properties_cb, NULL, NULL); - dbus_pending_call_block(call); - dbus_pending_call_unref(call); - } - } else { - g_warning ("Failed to get the default bluetooth adapter"); + 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 @@ -256,7 +175,7 @@ modest_maemo_utils_get_device_name (void) static DBusConnection *conn = NULL; DBusMessage *request; DBusError error; - DBusPendingCall *call = NULL; + DBusMessage *msg; dbus_error_init (&error); if (!conn) { @@ -270,40 +189,20 @@ modest_maemo_utils_get_device_name (void) } /* Get the default adapter */ - request = dbus_message_new_method_call(BTNAME_SERVICE, "/" , - BTNAME_REQUEST_IF, - BTNAME_REQ_DEFAULT_ADAPTER); - - if (dbus_connection_send_with_reply(conn, request, &call, -1)) { - dbus_pending_call_set_notify(call, get_default_adapter_cb, NULL, NULL); - dbus_pending_call_block(call); - dbus_pending_call_unref(call); + 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); } -/* request = dbus_message_new_method_call (BTNAME_SERVICE, BTNAME_REQUEST_PATH, */ -/* BTNAME_REQUEST_IF, BTNAME_REQ_GET); */ -/* if (!request) { */ -/* /\* should we free the connection? *\/ */ -/* g_printerr ("modest: dbus_message_new_method_call failed\n"); */ -/* return; */ -/* } */ -/* dbus_message_set_auto_start (request, TRUE); */ -/* if (dbus_connection_send_with_reply (conn, request, &call, -1)) { */ -/* dbus_pending_call_set_notify (call, on_device_name_received, */ -/* NULL, NULL); */ -/* dbus_pending_call_unref (call); */ -/* } */ -/* dbus_message_unref (request); */ - -/* dbus_connection_setup_with_g_main (conn, NULL); */ -/* dbus_bus_add_match (conn, BTNAME_MATCH_RULE, &error); */ -/* if (dbus_error_is_set(&error)) { */ -/* g_printerr ("modest: dbus_bus_add_match failed: %s\n", error.message); */ -/* dbus_error_free (&error); */ -/* } */ - -/* if (!dbus_connection_add_filter(conn, handle_dbus_signal, NULL, NULL)) */ -/* g_printerr ("modest: dbus_connection_add_filter failed\n"); */ + dbus_message_unref (request); + if (dbus_error_is_set (&error)) + dbus_error_free (&error); } void @@ -316,14 +215,19 @@ modest_maemo_utils_setup_images_filechooser (GtkFileChooser *chooser) g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser)); - conf_folder = modest_conf_get_string (modest_runtime_get_conf (), MODEST_CONF_LATEST_INSERT_IMAGE_PATH, NULL); + 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 = g_build_filename (g_getenv (MODEST_MAEMO_UTILS_MYDOCS_ENV), - MODEST_MAEMO_UTILS_DEFAULT_IMAGE_FOLDER, NULL); + 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); } @@ -443,7 +347,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); @@ -494,6 +398,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 @@ -590,24 +516,26 @@ modest_maemo_utils_select_attachments (GtkWindow *window, TnyList *att_list, gbo if (TNY_IS_MSG (part)) { TnyHeader *header = tny_msg_get_header (TNY_MSG (part)); filename = tny_header_dup_subject (header); - if ((filename == NULL) || (filename[0] == '\0')) { - g_free (filename); - filename = g_strdup (_("mail_va_no_subject")); - } 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, filename, 1, part, -1); attachments_added ++; g_free (filename); - g_object_unref (part); } + 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), @@ -638,6 +566,9 @@ modest_maemo_utils_select_attachments (GtkWindow *window, TnyList *att_list, gbo } 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; } @@ -648,3 +579,171 @@ modest_maemo_utils_select_attachments (GtkWindow *window, TnyList *att_list, gbo 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 + +void +modest_maemo_utils_scroll_pannable (HildonPannableArea *pannable, + gint horizontal, + gint vertical) +{ + gint h_pos = -1; + gint v_pos = -1; + + g_assert (pannable); + /* at atleast one of values have to be valid */ + g_return_if_fail (h_pos == -1 && v_pos == -1); + + if (horizontal != 0) { + GtkAdjustment *h_adj; + + h_adj = hildon_pannable_area_get_hadjustment (pannable); + g_return_if_fail (h_adj); + + h_pos = h_adj->value + h_adj->step_increment * horizontal; + if (horizontal > 0) { + h_pos += h_adj->page_size; + } + } + + if (vertical != 0) { + GtkAdjustment *v_adj; + + v_adj = hildon_pannable_area_get_vadjustment (pannable); + g_return_if_fail (v_adj); + + v_pos = v_adj->value + v_adj->step_increment * vertical; + if (vertical > 0) { + v_pos += v_adj->page_size; + } + } + + hildon_pannable_area_scroll_to (pannable, h_pos, v_pos); +} + +#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