From 8739d9d9ffbf707c1af0df86cd61cfed04ff07a5 Mon Sep 17 00:00:00 2001 From: Arne Zellentin Date: Fri, 9 Jun 2006 18:09:19 +0000 Subject: [PATCH] * sending of attachments works but has lots of bugs pmo-trunk-r220 --- src/gtk-glade/modest-ui.c | 79 ++++++++++++++++++++++++++++++++---- src/modest-editor-window.c | 40 ++++++++++++++++++ src/modest-editor-window.h | 6 +++ src/modest-tny-transport-actions.c | 44 ++++++++++++++++++-- src/modest-tny-transport-actions.h | 3 +- src/modest-ui.h | 1 + 6 files changed, 161 insertions(+), 12 deletions(-) diff --git a/src/gtk-glade/modest-ui.c b/src/gtk-glade/modest-ui.c index c313f1b..7d0a2c8 100644 --- a/src/gtk-glade/modest-ui.c +++ b/src/gtk-glade/modest-ui.c @@ -70,6 +70,8 @@ static void on_delete_clicked (GtkWidget *widget, ModestUI *modest_ui); static void on_view_attachments_toggled(GtkWidget *widget, ModestUI *modest_ui); +static void on_attach_button_clicked (GtkWidget *widget, ModestEditorWindow *modest_editwin); + #if 1 static void on_send_button_clicked (GtkWidget *widget, ModestEditorWindow *modest_editwin); #else @@ -93,6 +95,7 @@ typedef struct { ModestUI *modest_ui; ModestEditorWindow *edit_win; GladeXML *glade_xml; + GList *attachments; } EditWinData; @@ -539,6 +542,8 @@ GtkContainer g_object_unref(G_OBJECT(glade_xml)); return NULL; } + + win_data->attachments = NULL; /* redundant */ return GTK_CONTAINER(top_container); } @@ -630,6 +635,18 @@ modest_ui_editor_window_set_body(const gchar *body, gpointer window_data) return TRUE; } + +gboolean +modest_ui_editor_window_update_attachments(gpointer window_data) +{ + GladeXML *glade_xml; + + glade_xml = ((EditWinData *) window_data)->glade_xml; + + //body_view = glade_xml_get_widget(glade_xml, "body_view"); + + return TRUE; +} #if 0 gboolean modest_ui_new_edit_window (ModestUI *modest_ui, const gchar* to, @@ -1009,11 +1026,8 @@ static void on_editor_buffer_changed (GtkTextBuffer *textbuffer, gpointer user_data) { GtkWidget *edit_win; - EditWinData *windata; - + edit_win = (GtkWidget *)user_data; - windata = (EditWinData *)modest_editor_window_get_data(MODEST_EDITOR_WINDOW(edit_win)); - modest_editor_window_set_modified(MODEST_EDITOR_WINDOW(edit_win), TRUE); } @@ -1040,6 +1054,9 @@ on_new_mail_clicked (GtkWidget *widget, ModestUI *modest_ui) btn = glade_xml_get_widget (glade_xml, "toolb_send"); g_signal_connect (btn, "clicked", G_CALLBACK(on_send_button_clicked), edit_win); + btn = glade_xml_get_widget (glade_xml, "toolb_attach"); + g_signal_connect (btn, "clicked", G_CALLBACK(on_attach_button_clicked), + edit_win); w = glade_xml_get_widget (glade_xml, "to_entry"); g_signal_connect(w, "changed", G_CALLBACK(on_editor_entry_changed), edit_win); @@ -1093,6 +1110,9 @@ new_editor_with_presets (ModestUI *modest_ui, const gchar *to_header, btn = glade_xml_get_widget (glade_xml, "toolb_send"); g_signal_connect (btn, "clicked", G_CALLBACK(on_send_button_clicked), edit_win); + btn = glade_xml_get_widget (glade_xml, "toolb_attach"); + g_signal_connect (btn, "clicked", G_CALLBACK(on_attach_button_clicked), + edit_win); w = glade_xml_get_widget (glade_xml, "to_entry"); g_signal_connect(w, "changed", G_CALLBACK(on_editor_entry_changed), edit_win); @@ -1240,6 +1260,46 @@ on_forward_clicked (GtkWidget *widget, ModestUI *modest_ui) static void +on_attach_button_clicked (GtkWidget *widget, ModestEditorWindow *modest_editwin) +{ + /* open file selector */ + GtkWidget *dialog; + gchar *mime_type; + gchar *filename = NULL; + + dialog = gtk_file_chooser_dialog_new ("Open File", + GTK_WINDOW(modest_editwin), + GTK_FILE_CHOOSER_ACTION_OPEN, + GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, + GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, + NULL); + + if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) + { + + filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog)); + printf ("file:%s\n", filename); + } + gtk_widget_destroy (dialog); + + /* check file */ + if (!filename) + return; + + g_return_if_fail(g_str_has_suffix(filename, ".jpg")); /* for now... */ + + /* get mime type */ + mime_type = "image/jpeg"; + + /* attach file */ + + modest_editor_window_attach_file(modest_editwin, filename); + + g_free (filename); +} + + +static void on_send_button_clicked (GtkWidget *widget, ModestEditorWindow *modest_editwin) { ModestTnyTransportActions *actions; @@ -1253,9 +1313,9 @@ on_send_button_clicked (GtkWidget *widget, ModestEditorWindow *modest_editwin) TnyAccountStoreIface *account_store; const GList *transport_accounts; TnyTransportAccountIface *transport_account; - ModestConf *conf; ModestIdentityMgr *id_mgr; EditWinData *win_data; + GList * attachments; win_data = modest_editor_window_get_data(modest_editwin); @@ -1293,7 +1353,11 @@ on_send_button_clicked (GtkWidget *widget, ModestEditorWindow *modest_editwin) email_from = modest_identity_mgr_get_identity_string(id_mgr, MODEST_IDENTITY_DEFAULT_IDENTITY, MODEST_IDENTITY_EMAIL, NULL); - + attachments = modest_editor_window_get_attachments(modest_editwin); + /* while (attachments) { + printf("att: %s\n", (gchar *) attachments->data); + attachments = attachments->next; + } */ if (!email_from) email_from = ""; @@ -1303,7 +1367,8 @@ on_send_button_clicked (GtkWidget *widget, ModestEditorWindow *modest_editwin) transport_account, email_from, to, "", "", subject, - body); + body, + attachments); g_free (body); g_object_unref (G_OBJECT(actions)); diff --git a/src/modest-editor-window.c b/src/modest-editor-window.c index 18b8f16..72ef31b 100644 --- a/src/modest-editor-window.c +++ b/src/modest-editor-window.c @@ -21,6 +21,7 @@ typedef struct _ModestEditorWindowPrivate ModestEditorWindowPrivate; struct _ModestEditorWindowPrivate { gpointer user_data; gboolean modified; + GList *attachments; }; #define MODEST_EDITOR_WINDOW_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \ MODEST_TYPE_EDITOR_WINDOW, \ @@ -80,6 +81,7 @@ modest_editor_window_init (ModestEditorWindow *obj) priv->user_data = NULL; priv->modified = FALSE; + priv->attachments = NULL; } static void @@ -229,3 +231,41 @@ gboolean modest_editor_window_set_body(ModestEditorWindow *edit_win, const gchar return modest_ui_editor_window_set_body(body, priv->user_data); } + + +gboolean modest_editor_window_attach_file(ModestEditorWindow *edit_win, const gchar *filename) +{ + ModestEditorWindowPrivate *priv; + + if (!edit_win) + return FALSE; + priv = MODEST_EDITOR_WINDOW_GET_PRIVATE(edit_win); + + priv->attachments = g_list_append( + priv->attachments, + g_strdup(filename)); + + return modest_ui_editor_window_update_attachments(priv->user_data); +} + +GList * modest_editor_window_set_attachments(ModestEditorWindow *edit_win, GList* attachments) +{ + ModestEditorWindowPrivate *priv; + + g_return_val_if_fail(edit_win, NULL); + + priv = MODEST_EDITOR_WINDOW_GET_PRIVATE(edit_win); + + priv->attachments = attachments; + return priv->attachments; +} + +GList * modest_editor_window_get_attachments(ModestEditorWindow *edit_win) +{ + ModestEditorWindowPrivate *priv; + + g_return_val_if_fail(edit_win, NULL); + + priv = MODEST_EDITOR_WINDOW_GET_PRIVATE(edit_win); + return priv->attachments; +} diff --git a/src/modest-editor-window.h b/src/modest-editor-window.h index 8174fd7..9c9befc 100644 --- a/src/modest-editor-window.h +++ b/src/modest-editor-window.h @@ -60,6 +60,12 @@ gboolean modest_editor_window_set_subject_header(ModestEditorWindow *edit_win, c gboolean modest_editor_window_set_body(ModestEditorWindow *edit_win, const gchar *body); +gboolean modest_editor_window_attach_file(ModestEditorWindow *edit_win, const gchar *filename); + +GList * modest_editor_window_set_attachments(ModestEditorWindow *edit_win, GList* attachments); + +GList * modest_editor_window_get_attachments(ModestEditorWindow *edit_win); + G_END_DECLS #endif /* __MODEST_EDITOR_WINDOW_H__ */ diff --git a/src/modest-tny-transport-actions.c b/src/modest-tny-transport-actions.c index f45caa3..42badc6 100644 --- a/src/modest-tny-transport-actions.c +++ b/src/modest-tny-transport-actions.c @@ -153,13 +153,15 @@ modest_tny_transport_actions_send_message (ModestTnyTransportActions *self, const gchar *cc, const gchar *bcc, const gchar *subject, - const gchar *body) + const gchar *body, + const GList *attachments_list) { TnyMsgIface *new_msg; - TnyMsgMimePartIface *body_part; + TnyMsgMimePartIface *body_part, *attachment_part; TnyMsgHeaderIface *headers; - TnyStreamIface *body_stream; - gchar *content_type; + TnyStreamIface *body_stream, *attachment_stream; + gchar *content_type, *attachment_content_type; + GList *attachment; new_msg = TNY_MSG_IFACE(tny_msg_new ()); headers = TNY_MSG_HEADER_IFACE(tny_msg_header_new ()); @@ -188,6 +190,40 @@ modest_tny_transport_actions_send_message (ModestTnyTransportActions *self, tny_msg_mime_part_iface_construct_from_stream (TNY_MSG_MIME_PART_IFACE(new_msg), body_stream, content_type); + + attachment = (GList *)attachments_list; + while (attachment) { + gchar * att_buf; + struct stat stat_data; + int file; + + printf("att: %s\n", (gchar *) attachment->data); + /* leaks galore! */ + /* of course, the attachment should _not_ be read into mem... */ + file = open(attachment->data, O_RDONLY); + fstat(file, &stat_data); + att_buf = g_malloc0(stat_data.st_size + 1); + read(file, att_buf, stat_data.st_size); + close(file); + + attachment_stream = TNY_STREAM_IFACE (tny_stream_camel_new + (camel_stream_mem_new_with_buffer + (att_buf, stat_data.st_size))); + + attachment_part = TNY_MSG_MIME_PART_IFACE (tny_msg_mime_part_new + (camel_mime_part_new())); + + attachment_content_type = "image/jpeg"; /* later... */ + + tny_msg_mime_part_iface_construct_from_stream (attachment_part, attachment_stream, + content_type); + + tny_stream_iface_reset (attachment_stream); + tny_msg_mime_part_iface_set_content_type(attachment_part, attachment_content_type); + tny_msg_iface_add_part (new_msg, attachment_part); + + attachment = attachment->next; + } tny_transport_account_iface_send (transport_account, new_msg); diff --git a/src/modest-tny-transport-actions.h b/src/modest-tny-transport-actions.h index 0bc2801..4e32062 100644 --- a/src/modest-tny-transport-actions.h +++ b/src/modest-tny-transport-actions.h @@ -62,7 +62,8 @@ gboolean modest_tny_transport_actions_send_message (ModestTnyTransportActions *s const gchar *cc, const gchar *bcc, const gchar *subject, - const gchar *body); + const gchar *body, + const GList *attachments_list); G_END_DECLS diff --git a/src/modest-ui.h b/src/modest-ui.h index acb7719..9ef6cb1 100644 --- a/src/modest-ui.h +++ b/src/modest-ui.h @@ -93,6 +93,7 @@ gboolean modest_ui_editor_window_set_cc_header(const gchar *cc, gpointer window_ gboolean modest_ui_editor_window_set_bcc_header(const gchar *bcc, gpointer window_data); gboolean modest_ui_editor_window_set_subject_header(const gchar *subject, gpointer window_data); gboolean modest_ui_editor_window_set_body(const gchar *body, gpointer window_data); +gboolean modest_ui_editor_window_update_attachments(gpointer window_data); G_END_DECLS -- 1.7.9.5