From 674685c46e0ffd6a061f21930a1a33e5f579fa91 Mon Sep 17 00:00:00 2001 From: Arne Zellentin Date: Tue, 30 May 2006 14:38:57 +0000 Subject: [PATCH] * moded find_body_part from modest-tny-msg-view to modest-tny-msg-actions * modest_tny_msg_actions_quote & co now pass and take char* instead of GtkTextBuffers (but there's still one layer of wrapping left -> TODO) pmo-trunk-r126 --- src/gtk-glade/modest-ui.c | 8 +-- src/modest-text-utils.c | 19 +++++- src/modest-text-utils.h | 19 +++++- src/modest-tny-msg-actions.c | 149 ++++++++++++++++++++++-------------------- src/modest-tny-msg-actions.h | 14 ++-- src/modest-tny-msg-view.c | 40 +++--------- src/modest-tny-msg-view.h | 2 +- 7 files changed, 134 insertions(+), 117 deletions(-) diff --git a/src/gtk-glade/modest-ui.c b/src/gtk-glade/modest-ui.c index f50eae3..ba5a004 100644 --- a/src/gtk-glade/modest-ui.c +++ b/src/gtk-glade/modest-ui.c @@ -85,7 +85,7 @@ struct _ModestUIPrivate { ModestWindowMgr *modest_window_mgr; TnyAccountStoreIface *account_store; - GtkWindow *main_window; + GtkWindow *main_window; GladeXML *glade_xml; @@ -655,8 +655,7 @@ reply_to_msg (ModestUI *modest_ui, TnyMsgHeaderIface *header, const TnyMsgFolderIface *folder; GString *re_sub; const gchar *subject, *from; - GtkTextBuffer *unquoted; - gchar *quoted; + gchar *unquoted, *quoted; time_t sent_date; gint line_limit = 76; @@ -687,8 +686,7 @@ reply_to_msg (ModestUI *modest_ui, TnyMsgHeaderIface *header, modest_ui_show_edit_window (modest_ui, from, /* cc */ "", /* bcc */ "", re_sub->str, quoted, NULL); g_free(quoted); - if (unquoted != NULL) - g_object_unref(unquoted); + g_free(unquoted); g_string_free(re_sub, TRUE); } diff --git a/src/modest-text-utils.c b/src/modest-text-utils.c index c43475e..0dd7229 100644 --- a/src/modest-text-utils.c +++ b/src/modest-text-utils.c @@ -5,6 +5,8 @@ #include #include +#include "modest-text-utils.h" + #ifdef HAVE_CONFIG_H #include @@ -182,15 +184,26 @@ get_breakpoint(const gchar *s, const gint indent, const gint limit) { } } + gchar * -modest_text_utils_quote(GtkTextBuffer *buf, const gchar *from, const time_t sent_date, const int limit) +modest_text_utils_quote(const gchar *to_quote, const gchar *from, const time_t sent_date, const int limit) { + GtkTextBuffer *buf; + + buf = gtk_text_buffer_new (NULL); + gtk_text_buffer_set_text(buf, to_quote, -1); + return modest_text_utils_quote_text_buffer(buf, from, sent_date, limit); +} + +gchar * +modest_text_utils_quote_text_buffer(GtkTextBuffer *buf, const gchar *from, const time_t sent_date, const int limit) +{ + GtkTextIter iter; gint indent, breakpoint, rem_indent; gchar sent_str[101]; GString *q, *l, *remaining; /* quoted msg, line */ - /* format sent_date */ strftime(sent_str, 100, "%c", localtime(&sent_date)); q = g_string_new(""); @@ -198,6 +211,8 @@ modest_text_utils_quote(GtkTextBuffer *buf, const gchar *from, const time_t sent /* remaining will store the rest of the line if we have to break it */ remaining = g_string_new(""); + + gtk_text_buffer_get_iter_at_line(buf, &iter, 0); do { l = get_next_line(buf, &iter); diff --git a/src/modest-text-utils.h b/src/modest-text-utils.h index 28723f0..9205e0f 100644 --- a/src/modest-text-utils.h +++ b/src/modest-text-utils.h @@ -8,18 +8,35 @@ /** * modest_text_utils_quote: + * @buf: a string which contains the message to quote + * @from: the sender of the original message + * @sent_date: sent date/time of the original message + * @limit: specifies the maximum characters per line in the quoted text + * + * Returns: a string containing the quoted message + */ +gchar * +modest_text_utils_quote(const gchar *buf, + const gchar *from, + const time_t sent_date, + const int limit); + +/** + * modest_text_utils_quote_text_buffer: * @buf: a GtkTextBuffer which contains the message to quote * @from: the sender of the original message * @sent_date: sent date/time of the original message * @limit: specifies the maximum characters per line in the quoted text * * Returns: a string containing the quoted message + * Please do not use this function, it may be removed */ gchar * -modest_text_utils_quote(GtkTextBuffer *buf, +modest_text_utils_quote_text_buffer(GtkTextBuffer *buf, const gchar *from, const time_t sent_date, const int limit); + #endif diff --git a/src/modest-tny-msg-actions.c b/src/modest-tny-msg-actions.c index cf40598..8b0e68c 100644 --- a/src/modest-tny-msg-actions.c +++ b/src/modest-tny-msg-actions.c @@ -11,123 +11,132 @@ #ifdef HAVE_CONFIG_H #include -#endif /*HAVE_CONFIG_H*/ +#endif /*HAVE_CONFIG_H */ +#include "modest-tny-msg-actions.h" #include "modest-text-utils.h" /* private */ -static gchar *quote_msg(const TnyMsgIface *src, const gchar *from, time_t sent_date, gint limit, gboolean textorhtml); -static GtkTextBuffer *htmltotext(TnyMsgMimePartIface * body); - - +static gchar *quote_msg (const TnyMsgIface * src, const gchar * from, + time_t sent_date, gint limit, gboolean textorhtml); +static GtkTextBuffer *htmltotext (TnyMsgMimePartIface * body); static GtkTextBuffer * -htmltotext(TnyMsgMimePartIface * body) +htmltotext (TnyMsgMimePartIface * body) { GtkTextBuffer *buf; + #ifdef ACTIVATE_HACKS GtkWidget *html, *win; - TnyStreamIface *stream; + TnyStreamIface *stream; GtkClipboard *clip; gchar *text; - - win = gtk_window_new(GTK_WINDOW_TOPLEVEL); - html = gtk_html_new(); - gtk_container_add(GTK_CONTAINER(win), html); - - gtk_html_set_editable (GTK_HTML(html), FALSE); - gtk_html_allow_selection (GTK_HTML(html), TRUE); - gtk_html_set_caret_mode (GTK_HTML(html), FALSE); - gtk_html_set_blocking (GTK_HTML(html), FALSE); - gtk_html_set_images_blocking (GTK_HTML(html), FALSE); - - stream = TNY_STREAM_IFACE(modest_tny_stream_gtkhtml_new( - gtk_html_begin(GTK_HTML(html)))); - + + win = gtk_window_new (GTK_WINDOW_TOPLEVEL); + html = gtk_html_new (); + gtk_container_add (GTK_CONTAINER (win), html); + + gtk_html_set_editable (GTK_HTML (html), FALSE); + gtk_html_allow_selection (GTK_HTML (html), TRUE); + stream = TNY_STREAM_IFACE (modest_tny_stream_gtkhtml_new + (gtk_html_begin (GTK_HTML (html)))); + tny_stream_iface_reset (stream); tny_msg_mime_part_iface_decode_to_stream (body, stream); tny_stream_iface_reset (stream); - g_object_unref (G_OBJECT(stream)); - - gtk_widget_show_all(win); - gtk_html_select_all(GTK_HTML(html)); - clip = gtk_widget_get_clipboard(html, GDK_SELECTION_PRIMARY); + g_object_unref (G_OBJECT (stream)); + + gtk_widget_show_all (win); + gtk_html_select_all (GTK_HTML (html)); + clip = gtk_widget_get_clipboard (html, GDK_SELECTION_PRIMARY); //clip = gtk_widget_get_clipboard(html, GDK_SELECTION_CLIPBOARD); - text = gtk_clipboard_wait_for_text(clip); - - buf = gtk_text_buffer_new(NULL); - gtk_text_buffer_set_text(buf, text, -1); - g_free(text); + text = gtk_clipboard_wait_for_text (clip); + + buf = gtk_text_buffer_new (NULL); + gtk_text_buffer_set_text (buf, text, -1); + g_free (text); /* destroy win & html */ #else - buf = gtk_text_buffer_new(NULL); + buf = gtk_text_buffer_new (NULL); #endif return buf; } gchar * -modest_tny_msg_actions_quote(const TnyMsgIface *self, const gchar *from, time_t sent_date, gint limit, GtkTextBuffer *to_quote) +modest_tny_msg_actions_quote (const TnyMsgIface * self, const gchar * from, + time_t sent_date, gint limit, + char * to_quote) { gchar *quoted; /* 3 cases: */ - + /* a) quote text from selection */ if (to_quote != NULL) { - return modest_text_utils_quote(to_quote, from, sent_date, limit); + return modest_text_utils_quote (to_quote, from, sent_date, + limit); } - + /* b) try to find a text/plain part in the msg and quote it */ - quoted = quote_msg(self, from, sent_date, limit, FALSE); + quoted = quote_msg (self, from, sent_date, limit, FALSE); if (quoted != NULL) return quoted; - + /* c) if that fails, try text/html */ - return quote_msg(self, from, sent_date, limit, TRUE); + return quote_msg (self, from, sent_date, limit, TRUE); } - - + + static gchar * -quote_msg(const TnyMsgIface *src, const gchar *from, time_t sent_date, gint limit, gboolean textorhtml) +quote_msg (const TnyMsgIface * src, const gchar * from, time_t sent_date, + gint limit, gboolean textorhtml) { - - GList *parts; /* LEAK? */ - TnyStreamIface* stream; - TnyTextBufferStream *dest; - TnyMsgMimePartIface *body = NULL; - TnyMsgMimePartIface *part; + TnyStreamIface *stream; + TnyMsgMimePartIface *body; GtkTextBuffer *buf; gchar *quoted; - - /* is the warning in this line due to a bug in tinymail? */ - parts = (GList*) tny_msg_iface_get_parts (src); - while (parts) { - part = TNY_MSG_MIME_PART_IFACE(parts->data); - if (tny_msg_mime_part_iface_content_type_is (part, textorhtml ? "text/html" : "text/plain")) { - body = part; - break; - } - parts = parts->next; - } + /* the cast makes me uneasy... */ + body = modest_tny_msg_actions_find_body_part((TnyMsgIface *) src, + textorhtml ? "text/html" : "text/plain"); - if (!body) { + if (!body) return NULL; - } - + if (textorhtml == TRUE) { - buf = htmltotext(body); + buf = htmltotext (body); } else { - buf = gtk_text_buffer_new (NULL); - stream = TNY_STREAM_IFACE(tny_text_buffer_stream_new (buf)); + buf = gtk_text_buffer_new (NULL); + stream = TNY_STREAM_IFACE (tny_text_buffer_stream_new (buf)); tny_stream_iface_reset (stream); tny_msg_mime_part_iface_decode_to_stream (body, stream); tny_stream_iface_reset (stream); + g_object_unref (stream); } - - quoted = modest_text_utils_quote (buf, from, sent_date, limit); - - g_object_unref(stream); - g_object_unref(buf); + + quoted = modest_text_utils_quote_text_buffer (buf, from, sent_date, limit); + + g_object_unref (buf); return quoted; } + + +TnyMsgMimePartIface * +modest_tny_msg_actions_find_body_part (TnyMsgIface *self, const gchar *mime_type) +{ + TnyMsgMimePartIface *part = NULL; + GList *parts; + + g_return_val_if_fail (self, NULL); + g_return_val_if_fail (mime_type, NULL); + + parts = (GList*) tny_msg_iface_get_parts (self); + while (parts && !part) { + part = TNY_MSG_MIME_PART_IFACE(parts->data); + if (!tny_msg_mime_part_iface_content_type_is (part, mime_type)) + part = NULL; + parts = parts->next; + } + + return part; +} diff --git a/src/modest-tny-msg-actions.h b/src/modest-tny-msg-actions.h index 9253a67..d3fc496 100644 --- a/src/modest-tny-msg-actions.h +++ b/src/modest-tny-msg-actions.h @@ -13,10 +13,12 @@ * Returns: a string containing the quoted message */ -gchar *modest_tny_msg_actions_quote(const TnyMsgIface *self, - const gchar *from, - time_t sent_date, - gint limit, - GtkTextBuffer *to_quote); +gchar *modest_tny_msg_actions_quote (const TnyMsgIface * self, + const gchar * from, + time_t sent_date, + gint limit, char *to_quote); -#endif /* __MODEST_TNY_MSG_ACTIONS_H__*/ +TnyMsgMimePartIface *modest_tny_msg_actions_find_body_part (TnyMsgIface * self, + const gchar * mime_type); + +#endif /* __MODEST_TNY_MSG_ACTIONS_H__ */ diff --git a/src/modest-tny-msg-view.c b/src/modest-tny-msg-view.c index 6eff96b..dc6464a 100644 --- a/src/modest-tny-msg-view.c +++ b/src/modest-tny-msg-view.c @@ -4,6 +4,8 @@ #include "modest-tny-msg-view.h" #include "modest-tny-stream-gtkhtml.h" +#include "modest-tny-msg-actions.h" + #include #include #include @@ -428,26 +430,6 @@ fill_gtkhtml_with_txt (GtkHTML* gtkhtml, const gchar* txt) -static TnyMsgMimePartIface * -find_body_part (TnyMsgIface *msg, const gchar *mime_type) -{ - TnyMsgMimePartIface *part = NULL; - GList *parts; - - g_return_val_if_fail (msg, NULL); - g_return_val_if_fail (mime_type, NULL); - - parts = (GList*) tny_msg_iface_get_parts (msg); - while (parts && !part) { - part = TNY_MSG_MIME_PART_IFACE(parts->data); - if (!tny_msg_mime_part_iface_content_type_is (part, mime_type)) - part = NULL; - parts = parts->next; - } - - return part; -} - static gboolean set_html_message (ModestTnyMsgView *self, TnyMsgMimePartIface *tny_body) { @@ -508,7 +490,7 @@ set_text_message (ModestTnyMsgView *self, TnyMsgMimePartIface *tny_body) return TRUE; } -GtkTextBuffer * +gchar * modest_tny_msg_view_get_selected_text (ModestTnyMsgView *self) { ModestTnyMsgViewPrivate *priv; @@ -525,19 +507,13 @@ modest_tny_msg_view_get_selected_text (ModestTnyMsgView *self) /* I'm sure there is a better way to check for selected text */ sel = gtk_html_get_selection_html(GTK_HTML(html), &len); - if (sel == NULL) + if (!sel) return NULL; + g_free(sel); clip = gtk_widget_get_clipboard(html, GDK_SELECTION_PRIMARY); - text = gtk_clipboard_wait_for_text(clip); - if (text == NULL) - return NULL; - - buf = gtk_text_buffer_new(NULL); - gtk_text_buffer_set_text(buf, text, -1); - g_free(text); - return buf; + return gtk_clipboard_wait_for_text(clip); } void @@ -557,13 +533,13 @@ modest_tny_msg_view_set_message (ModestTnyMsgView *self, TnyMsgIface *msg) if (!msg) return; - body = find_body_part (msg, "text/html"); + body = modest_tny_msg_actions_find_body_part (msg, "text/html"); if (body) { set_html_message (self, body); return; } - body = find_body_part (msg, "text/plain"); + body = modest_tny_msg_actions_find_body_part (msg, "text/plain"); if (body) { set_text_message (self, body); return; diff --git a/src/modest-tny-msg-view.h b/src/modest-tny-msg-view.h index ba98311..2ef059a 100644 --- a/src/modest-tny-msg-view.h +++ b/src/modest-tny-msg-view.h @@ -66,7 +66,7 @@ GtkWidget* modest_tny_msg_view_new (TnyMsgIface *tny_msg); void modest_tny_msg_view_set_message (ModestTnyMsgView *self, TnyMsgIface *tny_msg); -GtkTextBuffer * modest_tny_msg_view_get_selected_text (ModestTnyMsgView *self); +gchar * modest_tny_msg_view_get_selected_text (ModestTnyMsgView *self); G_END_DECLS -- 1.7.9.5