X-Git-Url: http://git.maemo.org/git/?a=blobdiff_plain;f=src%2Fmaemo%2Fmodest-maemo-utils.c;h=2c38a9edcaa61172ebf0513b8a930c21c585c1bd;hb=66a30e4f7f6a0a4357265f36e6698f877ef141a6;hp=4870f7b5d9ec7c6b13bc31278a983e04f662da1a;hpb=98f5bc14623d0702fd78ad4b5cf96f8ad0b41fa7;p=modest diff --git a/src/maemo/modest-maemo-utils.c b/src/maemo/modest-maemo-utils.c index 4870f7b..2c38a9e 100644 --- a/src/maemo/modest-maemo-utils.c +++ b/src/maemo/modest-maemo-utils.c @@ -34,6 +34,9 @@ #include #include #include +#include +#include +#include /* for strlen */ #include #include #include @@ -45,6 +48,7 @@ #include "modest-hildon-includes.h" #include "modest-maemo-utils.h" +#include "modest-platform.h" /* * For getting and tracking the Bluetooth name @@ -61,6 +65,10 @@ #define BTNAME_MATCH_RULE "type='signal',interface='" BTNAME_SIGNAL_IF \ "',member='" BTNAME_SIG_CHANGED "'" +GQuark modest_maemo_utils_get_supported_secure_authentication_error_quark (void) +{ + return g_quark_from_static_string("modest-maemo-utils-get-supported-secure-authentication-error-quark"); +} GtkWidget* modest_maemo_utils_menubar_to_menu (GtkUIManager *ui_manager) @@ -199,6 +207,11 @@ modest_maemo_utils_get_device_name (void) gboolean modest_maemo_utils_folder_writable (const gchar *filename) { + g_return_val_if_fail (filename, FALSE); + + if (!filename) + return FALSE; + if (g_strncasecmp (filename, "obex", 4) != 0) { GnomeVFSFileInfo folder_info; gchar *folder; @@ -229,36 +242,61 @@ modest_maemo_utils_file_exists (const gchar *filename) } TnyFsStream * -modest_maemo_utils_create_temp_stream (const gchar *extension, gchar **path) +modest_maemo_utils_create_temp_stream (const gchar *orig_name, gchar **path) { - TnyStream *tmp_fs_stream = NULL; gint fd; gchar *filepath = NULL; - gchar *template = NULL; + gchar *tmpdir; - if (extension != NULL) - template = g_strdup_printf ("XXXXXX.%s", extension); + /* hmmm... maybe we need a modest_text_utils_validate_file_name? */ + g_return_val_if_fail (orig_name || strlen(orig_name) == 0, NULL); + if (strlen(orig_name) > 200) { + g_warning ("%s: filename too long ('%s')", + __FUNCTION__, orig_name); + return NULL; + } + + if (g_strstr_len (orig_name, strlen(orig_name), "/") != NULL) { + g_warning ("%s: filename contains '/' character(s) (%s)", + __FUNCTION__, orig_name); + return NULL; + } + + /* make a random subdir under /tmp or /var/tmp */ + tmpdir = g_strdup_printf ("%s/%d", g_get_tmp_dir (), (guint)random()); + if (g_mkdir (tmpdir, 0755) == -1) { + g_warning ("%s: failed to create dir '%s': %s", + __FUNCTION__, tmpdir, g_strerror(errno)); + g_free (tmpdir); + return NULL; + } - fd = g_file_open_tmp (template, &filepath, NULL); - g_free (template); - if (path != NULL) - *path = filepath; + /* try to write the file there */ + filepath = g_strconcat (tmpdir, "/", orig_name, NULL); + fd = g_open (filepath, O_CREAT|O_WRONLY|O_TRUNC, 0644); if (fd == -1) { - g_message ("TODO BANNER: Error saving stream"); + g_warning ("%s: failed to create '%s': %s", + __FUNCTION__, filepath, g_strerror(errno)); + g_free (tmpdir); + g_free (filepath); return NULL; } - tmp_fs_stream = tny_fs_stream_new (fd); - - return TNY_FS_STREAM (tmp_fs_stream); + + g_free (tmpdir); + + if (path) + *path = filepath; + + return TNY_FS_STREAM (tny_fs_stream_new (fd)); } typedef struct { - gboolean finished; gboolean cancel; GList *result; GtkWidget* dialog; - GtkWidget* progress; + GtkWidget* progress; + GError* error; } ModestGetSupportedAuthInfo; static void on_camel_account_get_supported_secure_authentication_status ( @@ -267,70 +305,118 @@ static void on_camel_account_get_supported_secure_authentication_status ( /*ModestGetSupportedAuthInfo* info = (ModestGetSupportedAuthInfo*) user_data;*/ } +static gboolean +on_idle_secure_auth_finished (gpointer user_data) +{ + ModestGetSupportedAuthInfo *info = (ModestGetSupportedAuthInfo*)user_data; + /* Operation has finished, close the dialog. Control continues after + * gtk_dialog_run in modest_maemo_utils_get_supported_secure_authentication_methods() */ + + /* This is a GDK lock because we are an idle callback and + * the code below is or does Gtk+ code */ + + gdk_threads_enter(); /* CHECKED */ + gtk_dialog_response (GTK_DIALOG (info->dialog), GTK_RESPONSE_ACCEPT); + gdk_threads_leave(); /* CHECKED */ + + return FALSE; +} + static void on_camel_account_get_supported_secure_authentication ( TnyCamelAccount *self, gboolean cancelled, - TnyList *auth_types, GError **err, + TnyList *auth_types, GError *err, gpointer user_data) { - ModestGetSupportedAuthInfo *info = (ModestGetSupportedAuthInfo*)user_data; g_return_if_fail (info); + /* Free everything if the actual action was canceled */ if (info->cancel) { + /* The operation was canceled and the ownership of the info given to us + * so that we could still check the cancel flag. */ g_slice_free (ModestGetSupportedAuthInfo, info); info = NULL; - return; } + else + { + if (err) + { + if (info->error) { + g_error_free (info->error); + info->error = NULL; + } + + info->error = g_error_copy (err); + } - if (!auth_types) { - printf ("DEBUG: %s: auth_types is NULL.\n", __FUNCTION__); - info->finished = TRUE; /* We are blocking, waiting for this. */ - return; - } - - ModestPairList* pairs = modest_protocol_info_get_auth_protocol_pair_list (); + if (!auth_types) { + g_warning ("DEBUG: %s: auth_types is NULL.\n", __FUNCTION__); + } + else if (tny_list_get_length(auth_types) == 0) { + g_warning ("DEBUG: %s: auth_types is an empty TnyList.\n", __FUNCTION__); + } else + { + ModestPairList* pairs = modest_protocol_info_get_auth_protocol_pair_list (); - /* Get the enum value for the strings: */ - GList *result = NULL; - TnyIterator* iter = tny_list_create_iterator(auth_types); - while (!tny_iterator_is_done(iter)) { - const gchar *auth_name = tny_pair_get_name(TNY_PAIR(tny_iterator_get_current(iter))); - printf("DEBUG: %s: auth_name=%s\n", __FUNCTION__, auth_name); - ModestPair *matching = modest_pair_list_find_by_first_as_string (pairs, - auth_name); - if (matching) - { - result = g_list_append (result, GINT_TO_POINTER((ModestConnectionProtocol)matching->first)); - } - tny_iterator_next(iter); - } + /* Get the enum value for the strings: */ + GList *result = NULL; + TnyIterator* iter = tny_list_create_iterator(auth_types); + while (!tny_iterator_is_done(iter)) { + TnyPair *pair = TNY_PAIR(tny_iterator_get_current(iter)); + const gchar *auth_name = NULL; + if (pair) { + auth_name = tny_pair_get_name(pair); + g_object_unref (pair); + pair = NULL; + } + + printf("DEBUG: %s: auth_name=%s\n", __FUNCTION__, auth_name); + + ModestAuthProtocol proto = modest_protocol_info_get_auth_protocol (auth_name); + if(proto != MODEST_PROTOCOL_AUTH_NONE) + result = g_list_prepend(result, GINT_TO_POINTER(proto)); + + tny_iterator_next(iter); + } + g_object_unref (iter); + + modest_pair_list_free (pairs); - g_object_unref(auth_types); - - modest_pair_list_free (pairs); - - info->result = result; - printf("DEBUG: finished\n"); - info->finished = TRUE; /* We are blocking, waiting for this. */ + info->result = result; + } + + printf("DEBUG: finished\n"); + + /* Close the dialog in a main thread */ + g_idle_add(on_idle_secure_auth_finished, info); + } } static void on_secure_auth_cancel(GtkWidget* dialog, int response, gpointer user_data) { - ModestGetSupportedAuthInfo *info = (ModestGetSupportedAuthInfo*)user_data; - g_return_if_fail(info); - /* We are blocking */ - info->result = NULL; - info->cancel = TRUE; + if(response == GTK_RESPONSE_REJECT || response == GTK_RESPONSE_DELETE_EVENT) + { + ModestGetSupportedAuthInfo *info = (ModestGetSupportedAuthInfo*)user_data; + g_return_if_fail(info); + /* This gives the ownership of the info to the worker thread. */ + info->result = NULL; + info->cancel = TRUE; + } } -GList* modest_maemo_utils_get_supported_secure_authentication_methods (ModestTransportStoreProtocol proto, - const gchar* hostname, gint port, const gchar* username, GtkWindow *parent_window) +GList* +modest_maemo_utils_get_supported_secure_authentication_methods (ModestTransportStoreProtocol proto, + const gchar* hostname, gint port, const gchar* username, GtkWindow *parent_window, GError** error) { g_return_val_if_fail (proto != MODEST_PROTOCOL_TRANSPORT_STORE_UNKNOWN, NULL); + /* We need a connection to get the capabilities; */ + if (!modest_platform_connect_and_wait (GTK_WINDOW (parent_window), NULL)) + return NULL; + /* result = g_list_append (result, GINT_TO_POINTER (MODEST_PROTOCOL_AUTH_CRAMMD5)); */ @@ -352,6 +438,7 @@ GList* modest_maemo_utils_get_supported_secure_authentication_methods (ModestTra default: tny_account = NULL; } + if (!tny_account) { g_printerr ("%s could not create tny account.", __FUNCTION__); @@ -364,9 +451,13 @@ GList* modest_maemo_utils_get_supported_secure_authentication_methods (ModestTra tny_account_set_proto (tny_account, modest_protocol_info_get_transport_store_protocol_name(proto)); + tny_account_set_hostname (tny_account, hostname); /* Required for POP, at least */ tny_account_set_user (tny_account, username); + if(port > 0) + tny_account_set_port (tny_account, port); + /* Set the session for the account, so we can use it: */ ModestTnyAccountStore *account_store = modest_runtime_get_account_store (); TnySessionCamel *session = @@ -374,32 +465,28 @@ GList* modest_maemo_utils_get_supported_secure_authentication_methods (ModestTra g_return_val_if_fail (session, NULL); tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), session); - tny_account_set_hostname (tny_account, hostname); - if(port > 0) - tny_account_set_port (tny_account, port); - - /* Ask camel to ask the server, asynchronously: */ ModestGetSupportedAuthInfo *info = g_slice_new (ModestGetSupportedAuthInfo); - info->finished = FALSE; info->result = NULL; info->cancel = FALSE; + info->error = NULL; info->progress = gtk_progress_bar_new(); - info->dialog = gtk_dialog_new_with_buttons(_("Authentication"), - parent_window, GTK_DIALOG_MODAL, - GTK_STOCK_CANCEL, - GTK_RESPONSE_REJECT, - NULL); + /* TODO: Need logical_ID for the title: */ + info->dialog = gtk_dialog_new_with_buttons(_("Authentication"), + parent_window, GTK_DIALOG_MODAL, + _("mcen_bd_dialog_cancel"), + GTK_RESPONSE_REJECT, + NULL); //gtk_window_set_default_size(GTK_WINDOW(info->dialog), 300, 100); g_signal_connect(G_OBJECT(info->dialog), "response", G_CALLBACK(on_secure_auth_cancel), info); gtk_container_add(GTK_CONTAINER(GTK_DIALOG(info->dialog)->vbox), - gtk_label_new("Checking for supported authentication types...")); + gtk_label_new("Checking for supported authentication types...")); gtk_container_add(GTK_CONTAINER(GTK_DIALOG(info->dialog)->vbox), info->progress); gtk_widget_show_all(info->dialog); - gtk_progress_bar_pulse(GTK_PROGRESS_BAR(info->progress)); + gtk_progress_bar_pulse(GTK_PROGRESS_BAR(info->progress)); printf ("DEBUG: %s: STARTING.\n", __FUNCTION__); tny_camel_account_get_supported_secure_authentication ( @@ -407,21 +494,164 @@ GList* modest_maemo_utils_get_supported_secure_authentication_methods (ModestTra on_camel_account_get_supported_secure_authentication, on_camel_account_get_supported_secure_authentication_status, info); - - /* Block until the callback has been called, - * driving the main context, so that the (idle handler) callback can be - * called, and so that our dialog is clickable: */ - while (!(info->finished) && !(info->cancel)) { - gtk_main_iteration_do(FALSE); - } + + gtk_dialog_run (GTK_DIALOG (info->dialog)); - gtk_widget_destroy(info->dialog); - + gtk_widget_destroy(info->dialog); + GList *result = info->result; if (!info->cancel) { + if (info->error) { + gchar * debug_url_string = tny_account_get_url_string (tny_account); + g_warning ("DEBUG: %s:\n error: %s\n account url: %s", __FUNCTION__, info->error->message, + debug_url_string); + g_free (debug_url_string); + + g_propagate_error(error, info->error); + info->error = NULL; + } + g_slice_free (ModestGetSupportedAuthInfo, info); info = NULL; } + else + { + // Tell the caller that the operation was canceled so it can + // make a difference + g_set_error(error, + modest_maemo_utils_get_supported_secure_authentication_error_quark(), + MODEST_MAEMO_UTILS_GET_SUPPORTED_SECURE_AUTHENTICATION_ERROR_CANCELED, + "User has canceled query"); + } + return result; } + +void +modest_maemo_utils_setup_images_filechooser (GtkFileChooser *chooser) +{ + gchar *images_folder; + GtkFileFilter *file_filter; + GList *image_mimetypes_list; + GList *node; + + g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser)); + + /* Set the default folder to images folder */ + images_folder = g_build_filename (g_get_home_dir (), + MODEST_MAEMO_UTILS_MYDOCS_FOLDER, + MODEST_MAEMO_UTILS_DEFAULT_IMAGE_FOLDER, NULL); + gtk_file_chooser_set_current_folder (chooser, images_folder); + g_free (images_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 + +} + +#if 0 +static void +on_hide (GtkDialog *dialog, gpointer user_data) +{ + /* Just destroy the dialog: */ + gtk_widget_destroy (GTK_WIDGET (dialog)); +} +#endif + +#if 0 /* Not used now. */ +/* user_data for the idle callback: */ +typedef struct +{ + GtkWindow *parent_window; + gchar *message; +} ModestIdleNoteInfo; + +static gboolean +on_idle_show_information(gpointer user_data) +{ + ModestIdleNoteInfo *info = (ModestIdleNoteInfo*)user_data; + + modest_maemo_show_information_note_and_forget (info->parent_window, info->message); + + g_free (info->message); + g_slice_free (ModestIdleNoteInfo, info); + + return FALSE; /* Don't call this again. */ +} + +void +modest_maemo_show_information_note_in_main_context_and_forget (GtkWindow *parent_window, + const gchar* message) +{ + ModestIdleNoteInfo *info = g_slice_new (ModestIdleNoteInfo); + info->parent_window = parent_window; + info->message = g_strdup (message); + + g_idle_add (on_idle_show_information, info); +} +#endif + +void +modest_maemo_show_dialog_and_forget (GtkWindow *parent_window, + GtkDialog *dialog) +{ + gtk_window_set_transient_for (GTK_WINDOW (dialog), parent_window); + + /* Destroy the dialog when it is closed: */ + g_signal_connect_swapped (dialog, + "response", + G_CALLBACK (gtk_widget_destroy), + dialog); + + gtk_widget_show (GTK_WIDGET (dialog)); +} + + + +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 */ +} + +void +modest_maemo_toggle_action_set_active_block_notify (GtkToggleAction *action, gboolean value) +{ + GSList *proxies = NULL; + + g_return_if_fail (GTK_IS_TOGGLE_ACTION (action)); + + for (proxies = gtk_action_get_proxies (GTK_ACTION (action)); + proxies != NULL; proxies = g_slist_next (proxies)) { + GtkWidget *widget = (GtkWidget *) proxies->data; + gtk_action_block_activate_from (GTK_ACTION (action), widget); + } + + gtk_toggle_action_set_active (action, value); + + for (proxies = gtk_action_get_proxies (GTK_ACTION (action)); + proxies != NULL; proxies = g_slist_next (proxies)) { + GtkWidget *widget = (GtkWidget *) proxies->data; + gtk_action_unblock_activate_from (GTK_ACTION (action), widget); + } + +}