From: Jose Dapena Paz Date: Fri, 19 Oct 2007 09:10:40 +0000 (+0000) Subject: Work to make attachments be opened in a single instance (raise window X-Git-Tag: git_migration_finished~2246 X-Git-Url: http://git.maemo.org/git/?p=modest;a=commitdiff_plain;h=6fb1323bf79f5fe514da3bbb44130fdfdbd8e46f Work to make attachments be opened in a single instance (raise window if they're already openened) (fixes NB#65279). * src/widgets/modest-window-mgr.[ch]: * Now modest_window_mgr_register_header accepts an alt_uid parameter in case the header does not provide an uid. * src/maemo/modest-msg-view-window.c: * Now we pass a hash base to the create_temp_stream method used to create a temporary file to view an attachment. This hash is used to open always the file in the same directory. * Now the attachment messages are opened passing a "attachment id" that works similar to a message id. This identifies the message in the window manager properly. * src/modest-ui-actions.c, src/dbus_api/modest-dbus-callbacs.c: * Use new modest_window_mgr_register_header call. * src/maemo/modest-maemo-utils.[ch]: * Now ..._create_temp_stream accepts an additional parameter hash_base. This hash_base is used to generate the "random" directory the temporary file is created. This is used to force files created from attachments get the same path. pmo-trunk-r3529 --- diff --git a/src/dbus_api/modest-dbus-callbacks.c b/src/dbus_api/modest-dbus-callbacks.c index c70cfe2..d4ddeb8 100644 --- a/src/dbus_api/modest-dbus-callbacks.c +++ b/src/dbus_api/modest-dbus-callbacks.c @@ -652,7 +652,7 @@ on_idle_open_message (gpointer user_data) if (!already_opened) { /* g_debug ("creating new window for this msg"); */ - modest_window_mgr_register_header (win_mgr, header); + modest_window_mgr_register_header (win_mgr, header, NULL); const gchar *modest_account_name = modest_tny_account_get_parent_modest_account_name_for_server_account (account); diff --git a/src/maemo/modest-maemo-utils.c b/src/maemo/modest-maemo-utils.c index 8fe546c..6d50ee1 100644 --- a/src/maemo/modest-maemo-utils.c +++ b/src/maemo/modest-maemo-utils.c @@ -242,11 +242,12 @@ modest_maemo_utils_file_exists (const gchar *filename) } TnyFsStream * -modest_maemo_utils_create_temp_stream (const gchar *orig_name, gchar **path) +modest_maemo_utils_create_temp_stream (const gchar *orig_name, const gchar *hash_base, gchar **path) { gint fd; gchar *filepath = NULL; gchar *tmpdir; + guint hash_number; /* hmmm... maybe we need a modest_text_utils_validate_file_name? */ g_return_val_if_fail (orig_name || strlen(orig_name) == 0, NULL); @@ -263,8 +264,13 @@ modest_maemo_utils_create_temp_stream (const gchar *orig_name, gchar **path) } /* 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) { + if (hash_base != NULL) { + hash_number = g_str_hash (hash_base); + } else { + hash_number = (guint) random (); + } + tmpdir = g_strdup_printf ("%s/%u", g_get_tmp_dir (), hash_number); + if ((g_access (tmpdir, R_OK) == -1) && (g_mkdir (tmpdir, 0755) == -1)) { g_warning ("%s: failed to create dir '%s': %s", __FUNCTION__, tmpdir, g_strerror(errno)); g_free (tmpdir); diff --git a/src/maemo/modest-maemo-utils.h b/src/maemo/modest-maemo-utils.h index e8a1e9b..42cef66 100644 --- a/src/maemo/modest-maemo-utils.h +++ b/src/maemo/modest-maemo-utils.h @@ -90,13 +90,15 @@ gboolean modest_maemo_utils_file_exists (const gchar *filename); /** * modest_maemo_utils_create_temp_stream: * @orig_name: a string with the original name of the extension, or %NULL + * @hash_base: if %NULL, subdir will be random. If not, it will be a hash + * of this. * @path: a string with the created file path * * Creates a temporary fs stream, in a random subdir of /tmp or /var/tmp. * * Returns: a #TnyFsStream, or %NULL if operation failed. */ -TnyFsStream *modest_maemo_utils_create_temp_stream (const gchar *orig_name, gchar **path); +TnyFsStream *modest_maemo_utils_create_temp_stream (const gchar *orig_name, const gchar *hash_base, gchar **path); /** * modest_maemo_utils_get_supported_secure_authentication_methods: diff --git a/src/maemo/modest-msg-view-window.c b/src/maemo/modest-msg-view-window.c index ebf9b23..4d1cf23 100644 --- a/src/maemo/modest-msg-view-window.c +++ b/src/maemo/modest-msg-view-window.c @@ -2176,11 +2176,24 @@ void modest_msg_view_window_view_attachment (ModestMsgViewWindow *window, TnyMimePart *mime_part) { ModestMsgViewWindowPrivate *priv; + const gchar *msg_uid; + gchar *attachment_uid = NULL; + gint attachment_index = 0; + GList *attachments; + g_return_if_fail (MODEST_IS_MSG_VIEW_WINDOW (window)); g_return_if_fail (TNY_IS_MIME_PART (mime_part) || (mime_part == NULL)); - priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window); + msg_uid = modest_msg_view_window_get_message_uid (MODEST_MSG_VIEW_WINDOW (window)); + attachments = modest_msg_view_get_attachments (MODEST_MSG_VIEW (priv->msg_view)); + attachment_index = g_list_index (attachments, mime_part); + g_list_free (attachments); + + if (msg_uid && attachment_index >= 0) { + attachment_uid = g_strdup_printf ("%s/%d", msg_uid, attachment_index); + } + if (mime_part == NULL) { gboolean error = FALSE; GList *selected_attachments = modest_msg_view_get_selected_attachments (MODEST_MSG_VIEW (priv->msg_view)); @@ -2211,8 +2224,7 @@ modest_msg_view_window_view_attachment (ModestMsgViewWindow *window, TnyMimePart gchar *filepath = NULL; const gchar *att_filename = tny_mime_part_get_filename (mime_part); TnyFsStream *temp_stream = NULL; - - temp_stream = modest_maemo_utils_create_temp_stream (att_filename, &filepath); + temp_stream = modest_maemo_utils_create_temp_stream (att_filename, attachment_uid, &filepath); if (temp_stream) { const gchar *content_type; @@ -2245,11 +2257,11 @@ modest_msg_view_window_view_attachment (ModestMsgViewWindow *window, TnyMimePart g_warning ("window for is already being created"); } else { /* it's not found, so create a new window for it */ - modest_window_mgr_register_header (mgr, header); /* register the uid before building the window */ + modest_window_mgr_register_header (mgr, header, attachment_uid); /* register the uid before building the window */ gchar *account = g_strdup (modest_window_get_active_account (MODEST_WINDOW (window))); if (!account) account = modest_account_mgr_get_default_account (modest_runtime_get_account_mgr ()); - msg_win = modest_msg_view_window_new_for_attachment (TNY_MSG (mime_part), account, NULL); + msg_win = modest_msg_view_window_new_for_attachment (TNY_MSG (mime_part), account, attachment_uid); modest_window_set_zoom (MODEST_WINDOW (msg_win), modest_window_get_zoom (MODEST_WINDOW (window))); modest_window_mgr_register_window (mgr, msg_win); diff --git a/src/modest-ui-actions.c b/src/modest-ui-actions.c index 7708beb..bc58df1 100644 --- a/src/modest-ui-actions.c +++ b/src/modest-ui-actions.c @@ -1138,7 +1138,7 @@ _modest_ui_actions_open (TnyList *headers, ModestWindow *win) while (!tny_iterator_is_done (iter_not_opened)) { TnyHeader *header = TNY_HEADER (tny_iterator_get_current (iter_not_opened)); if (header) { - modest_window_mgr_register_header (mgr, header); + modest_window_mgr_register_header (mgr, header, NULL); g_object_unref (header); } tny_iterator_next (iter_not_opened); @@ -4189,7 +4189,7 @@ modest_ui_actions_on_main_window_remove_attachments (GtkAction *action, } } else { ModestMailOperation *mail_op = NULL; - modest_window_mgr_register_header (modest_runtime_get_window_mgr (), header); + modest_window_mgr_register_header (modest_runtime_get_window_mgr (), header, NULL); mail_op = modest_mail_operation_new_with_error_handling (G_OBJECT (win), modest_ui_actions_get_msgs_full_error_handler, NULL, NULL); diff --git a/src/widgets/modest-window-mgr.c b/src/widgets/modest-window-mgr.c index 33ec152..f6990de 100644 --- a/src/widgets/modest-window-mgr.c +++ b/src/widgets/modest-window-mgr.c @@ -261,7 +261,7 @@ append_uid (GSList *list, const gchar *uid) void -modest_window_mgr_register_header (ModestWindowMgr *self, TnyHeader *header) +modest_window_mgr_register_header (ModestWindowMgr *self, TnyHeader *header, const gchar *alt_uid) { ModestWindowMgrPrivate *priv; gchar* uid; @@ -272,13 +272,15 @@ modest_window_mgr_register_header (ModestWindowMgr *self, TnyHeader *header) priv = MODEST_WINDOW_MGR_GET_PRIVATE (self); uid = modest_tny_folder_get_header_unique_id (header); - + if (uid == NULL) + uid = g_strdup (alt_uid); + if (!has_uid (priv->preregistered_uids, uid)) { g_debug ("registering new uid %s", uid); priv->preregistered_uids = append_uid (priv->preregistered_uids, uid); } else g_debug ("already had uid %s", uid); - + g_free (uid); } @@ -293,13 +295,13 @@ modest_window_mgr_unregister_header (ModestWindowMgr *self, TnyHeader *header) priv = MODEST_WINDOW_MGR_GET_PRIVATE (self); uid = modest_tny_folder_get_header_unique_id (header); - + if (!has_uid (priv->preregistered_uids, uid)) { g_debug ("trying to unregister non-existing uid %s", uid); priv->preregistered_uids = append_uid (priv->preregistered_uids, uid); } else g_debug ("unregistering uid %s", uid); - + if (has_uid (priv->preregistered_uids, uid)) { priv->preregistered_uids = remove_uid (priv->preregistered_uids, uid); if (has_uid (priv->preregistered_uids, uid)) @@ -307,7 +309,7 @@ modest_window_mgr_unregister_header (ModestWindowMgr *self, TnyHeader *header) else g_debug ("uid %s removed", uid); } - + g_free (uid); } @@ -335,14 +337,13 @@ compare_msguids (ModestWindow *win, return 1; } - void modest_window_mgr_close_all_windows (ModestWindowMgr *self) { ModestWindowMgrPrivate *priv = NULL; GList *wins = NULL; gboolean ret_value = FALSE; - + g_return_if_fail (MODEST_IS_WINDOW_MGR (self)); priv = MODEST_WINDOW_MGR_GET_PRIVATE (self); @@ -375,9 +376,9 @@ modest_window_mgr_find_registered_header (ModestWindowMgr *self, TnyHeader *head if (win) *win = NULL; - + has_header = has_uid (priv->preregistered_uids, uid); - + item = g_list_find_custom (priv->window_list, uid, (GCompareFunc) compare_msguids); if (item) { has_window = TRUE; @@ -391,8 +392,8 @@ modest_window_mgr_find_registered_header (ModestWindowMgr *self, TnyHeader *head } } } - g_free (uid); + return has_header || has_window; } @@ -456,17 +457,16 @@ modest_window_mgr_register_window (ModestWindowMgr *self, if (MODEST_IS_MSG_VIEW_WINDOW(window)) { const gchar *uid = modest_msg_view_window_get_message_uid (MODEST_MSG_VIEW_WINDOW (window)); - + if (!has_uid (priv->preregistered_uids, uid)) g_debug ("weird: no uid for window (%s)", uid); g_debug ("registering window for %s", uid ? uid : ""); - + priv->preregistered_uids = remove_uid (priv->preregistered_uids, modest_msg_view_window_get_message_uid (MODEST_MSG_VIEW_WINDOW (window))); - } else if (MODEST_IS_MSG_EDIT_WINDOW(window)) { const gchar *uid = modest_msg_edit_window_get_message_uid (MODEST_MSG_EDIT_WINDOW (window)); diff --git a/src/widgets/modest-window-mgr.h b/src/widgets/modest-window-mgr.h index 4b1e697..1e65c49 100644 --- a/src/widgets/modest-window-mgr.h +++ b/src/widgets/modest-window-mgr.h @@ -201,13 +201,14 @@ void modest_window_mgr_close_all_windows (ModestWindowMgr *self); * modest_window_mgr_register_header * @self: a #ModestWindowMgr * @header: a valid #TnyHeader + * @alt_uid: alternative uid in case @header does not provide one * * register the uid, even before the window is created. thus, we know when * some window creation might already be underway. the uid will automatically be * removed when the window itself will registered * **/ -void modest_window_mgr_register_header (ModestWindowMgr *self, TnyHeader *header); +void modest_window_mgr_register_header (ModestWindowMgr *self, TnyHeader *header, const gchar *alt_uid); /**