* moded find_body_part from modest-tny-msg-view to modest-tny-msg-actions
[modest] / src / gtk-glade / modest-ui.c
index 193b3b2..ba5a004 100644 (file)
@@ -24,6 +24,8 @@
 #include "../modest-tny-msg-view.h"
 #include "../modest-tny-transport-actions.h"
 
+#include "../modest-text-utils.h"
+#include "../modest-tny-msg-actions.h"
 
 #define MODEST_GLADE          PREFIX "/share/modest/glade/modest.glade"
 #define MODEST_GLADE_MAIN_WIN "main"
@@ -64,6 +66,8 @@ static void on_send_button_clicked (GtkWidget *widget, ModestUI *modest_ui);
 
 static void register_toolbar_callbacks (ModestUI *modest_ui);
 
+static void reply_to_msg (ModestUI *modest_ui, TnyMsgHeaderIface *header,
+                                               ModestTnyMsgView *msg_view);
 
 /* list my signals */
 enum {
@@ -81,7 +85,7 @@ struct _ModestUIPrivate {
        ModestWindowMgr      *modest_window_mgr;
        TnyAccountStoreIface *account_store;
 
-       GtkWindow            *main_window;
+       GtkWindow            *main_window;
        GladeXML             *glade_xml;
 
 
@@ -625,8 +629,6 @@ static GtkWidget*
 modest_main_window_folder_tree (ModestAccountMgr *modest_acc_mgr,
                                TnyAccountStoreIface *account_store)
 {
-       GtkTreeViewColumn *column;
-       GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
        GtkWidget *folder_tree;
 
        folder_tree = GTK_WIDGET(modest_tny_folder_tree_view_new (account_store));
@@ -634,25 +636,7 @@ modest_main_window_folder_tree (ModestAccountMgr *modest_acc_mgr,
                g_warning ("could not create folder list");
                return NULL;
        }
-
-       column = gtk_tree_view_column_new_with_attributes(_("All Mail Folders"),
-                                                         renderer,"text",
-                                                         TNY_ACCOUNT_TREE_MODEL_NAME_COLUMN,
-                                                         NULL);
-       gtk_tree_view_column_set_resizable (column, TRUE);
-       gtk_tree_view_append_column (GTK_TREE_VIEW(folder_tree), column);
-
-       column = gtk_tree_view_column_new_with_attributes(_("Unread"),
-                                                         renderer, "text",
-                                                         TNY_ACCOUNT_TREE_MODEL_UNREAD_COLUMN,
-                                                         NULL);
-       gtk_tree_view_column_set_resizable (column, TRUE);
-       gtk_tree_view_append_column (GTK_TREE_VIEW(folder_tree), column);
-
-
-       gtk_tree_view_set_headers_visible   (GTK_TREE_VIEW(folder_tree), TRUE);
-       gtk_tree_view_set_headers_clickable (GTK_TREE_VIEW(folder_tree), TRUE);
-
+       
        return folder_tree;
 }
 
@@ -664,226 +648,45 @@ on_new_mail_clicked (GtkWidget *widget, ModestUI *modest_ui)
        modest_ui_show_edit_window (modest_ui, "", "", "", "", "", NULL);
 }
 
-
-static GString *
-get_next_line(GtkTextBuffer *b, GtkTextIter *iter)
-{
-       GtkTextIter iter2;
-       gchar *tmp;
-       gint debi;
-       
-       debi = gtk_text_iter_get_line(iter);
-       gtk_text_buffer_get_iter_at_line_offset(b,
-                       &iter2,
-                       gtk_text_iter_get_line(iter),
-                       gtk_text_iter_get_chars_in_line(iter) -1
-               );
-       tmp = gtk_text_buffer_get_text(b, &iter2, iter, FALSE);
-       gtk_text_iter_forward_line(iter);
-       return g_string_new(tmp);
-}
-
-static int
-get_indent_level(const char *l)
-{
-       int indent = 0;
-       while (l[0]) {
-               if (l[0] == '>') {
-                       indent++;
-                       if (l[1] == ' ') {
-                               l++;
-                       }
-               } else {
-                       break;
-               }
-               l++;
-               
-       }
-       return indent;
-}
-
 static void
-unquote_line(GString *l) {
-       GString *r;
-       gchar *p;
-       
-       p = l->str;
-       while (p[0]) {
-               if (p[0] == '>') {
-                       if (p[1] == ' ') {
-                               p++;
-                       }
-               } else {
-                       break;
-               }
-               p++;
-       }
-       g_string_erase (l, 0, p - l->str);
-}
-
-static void
-append_quoted(GString *buf, const int indent, const GString *str, const int cutpoint) {
-       int i;
-       
-       for (i=0; i<=indent; i++) {
-               g_string_append(buf, "> ");
-       }
-       if (cutpoint > 0) {
-               g_string_append_len(buf, str->str, cutpoint);
-       } else {
-               g_string_append(buf, str->str);
-       }
-       g_string_append(buf, "\n");
-}
-
-static gint
-get_breakpoint(const gchar *s, const gint indent, const gint limit) {
-       gint i, last;
-       
-       last = strlen(s);
-       if (last + 2 * indent < limit)
-               return last;
-       
-       i = strlen(s);
-       for ( ; i>0; i--) {
-               if (s[i] == ' ') {
-                       if (i + 2 * indent <= limit) {
-                               return i;
-                       } else {
-                               last = i;
-                       }
-               }
-       }
-       return last;
-}
-       
-static gchar *
-modest_ui_quote_msg(const TnyMsgIface *src, const gchar *from, time_t sent_date)
-{
-       GList *parts;
-       TnyMsgMimePartIface *part;
-       TnyStreamIface* stream;
-       TnyTextBufferStream *dest;
-       TnyMsgMimePartIface *body = NULL;
-       GtkTextBuffer *buf;
-       GtkTextIter begin, end, iter1, iter2, iter3;
-       gchar *txt;
-       gint tmp;
-       gint limit = 76;
-       gint indent, breakpoint;
-       gint rem_indent;
-       gchar sent_str[101];
-       GString *q, *l, *remaining;
-       
-
-       buf = gtk_text_buffer_new(NULL);
-       dest = tny_text_buffer_stream_new(buf);
-       
-       /* is the warning in this line due to a bug in tinymail? */
-       parts  = (GList*) tny_msg_iface_get_parts (src);
-
-       while (parts) {
-               /* TODO: maybe we'd like to quote more than one part?
-                *       cleanup, fix leaks
-                */
-               TnyMsgMimePartIface *part =
-                       TNY_MSG_MIME_PART_IFACE(parts->data);
-               if (tny_msg_mime_part_iface_content_type_is (part, "text/plain")) {
-                       body = part;
-                       break;
-               }
-               parts = parts->next;
-       }
-       if (!body) {
-               return NULL;
-       }
-       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);
-
-       /* format sent_date */
-       strftime(sent_str, 100, "%c", localtime(&sent_date));
-       q = g_string_new("");
-       g_string_printf(q, "On %s, %s wrote:\n", sent_str, from);
-               
-       remaining = g_string_new("");
-       gtk_text_buffer_get_iter_at_line(buf, &iter1, 0);
-       do {
-               l = get_next_line(buf, &iter1);
-               indent = get_indent_level(l->str);
-               unquote_line(l);
-               
-               if (remaining->len) {
-                       if (l->len && indent == rem_indent) {
-                               g_string_prepend(l, " ");
-                               g_string_prepend(l, remaining->str);
-                       } else {
-                               do {
-                                       breakpoint = get_breakpoint(remaining->str, rem_indent, limit);
-                                       append_quoted(q, rem_indent, remaining, breakpoint);
-                                       g_string_erase(remaining, 0, breakpoint);
-                                       if (remaining->str[0] == ' ') {
-                                               g_string_erase(remaining, 0, 1);
-                                       }               
-                               } while (remaining->len);
-                       }
-               }
-               g_string_free(remaining, TRUE);
-               breakpoint = get_breakpoint(l->str, indent, limit);
-               remaining = g_string_new(l->str + breakpoint);
-               if (remaining->str[0] == ' ') {
-                       g_string_erase(remaining, 0, 1);
-               }
-               rem_indent = indent;
-               append_quoted(q, indent, l, breakpoint);
-               g_string_free(l, TRUE);
-       } while (!gtk_text_iter_is_end(&iter1));
-       
-       g_object_unref(stream);
-       g_object_unref(buf);
-       return g_string_free(q, FALSE);
-}
-
-static void
-modest_ui_reply_to_msg (ModestUI *modest_ui, TnyMsgHeaderIface *header,
+reply_to_msg (ModestUI *modest_ui, TnyMsgHeaderIface *header,
                                                ModestTnyMsgView *msg_view) {
        const TnyMsgIface *msg;
        const TnyMsgFolderIface *folder;
        GString *re_sub;
        const gchar *subject, *from;
-       gchar *quoted;
+       gchar *unquoted, *quoted;
        time_t sent_date;
+       gint line_limit = 76;
        
-       quoted = "";
-       if (header) {
-               folder = tny_msg_header_iface_get_folder (TNY_MSG_HEADER_IFACE(header));
-               if (!folder) {
-                       g_warning ("cannot find folder");
-                       return;
-               }
-
-               msg = tny_msg_folder_iface_get_message (TNY_MSG_FOLDER_IFACE(folder), header);
-               if (!msg) {
-                       g_warning ("cannot find msg");
-                       return;
-               }
-               subject = tny_msg_header_iface_get_subject(header);
-               re_sub = g_string_new(subject);
-               g_string_prepend(re_sub, "Re: ");
-               /* FIXME: honor replyto, cc */
-               from = tny_msg_header_iface_get_from(header);
-               sent_date = tny_msg_header_iface_get_date_sent(header);
-               quoted = modest_ui_quote_msg(msg, from, sent_date);
-       } else {
+       if (!header) {
                g_warning("no header");
                return;
        }
+       folder = tny_msg_header_iface_get_folder (TNY_MSG_HEADER_IFACE(header));
+       if (!folder) {
+               g_warning ("cannot find folder");
+               return;
+       }
 
+       msg = tny_msg_folder_iface_get_message (TNY_MSG_FOLDER_IFACE(folder), header);
+       if (!msg) {
+               g_warning ("cannot find msg");
+               return;
+       }
+       subject = tny_msg_header_iface_get_subject(header);
+       re_sub = g_string_new(subject);
+       g_string_prepend(re_sub, "Re: ");
+       /* FIXME: honor replyto, cc */
+       from = tny_msg_header_iface_get_from(header);
+       sent_date = tny_msg_header_iface_get_date_sent(header);
+       
+       unquoted = modest_tny_msg_view_get_selected_text(msg_view);
+       quoted = modest_tny_msg_actions_quote(msg, from, sent_date, line_limit, unquoted);
+       
        modest_ui_show_edit_window (modest_ui, from, /* cc */ "", /* bcc */ "", re_sub->str, quoted, NULL);
        g_free(quoted);
+       g_free(unquoted);
        g_string_free(re_sub, TRUE);
 }
 
@@ -932,7 +735,7 @@ on_reply_clicked (GtkWidget *widget, ModestUI *modest_ui)
                            TNY_MSG_HEADER_LIST_MODEL_INSTANCE_COLUMN,
                            &header, -1);
 
-       modest_ui_reply_to_msg (modest_ui, header, msg_view);
+       reply_to_msg (modest_ui, header, msg_view);
 }
 
 
@@ -945,13 +748,15 @@ on_send_button_clicked (GtkWidget *widget, ModestUI *modest_ui)
        ModestTnyTransportActions *actions;
        ModestUIPrivate *priv;
        GtkWidget *to_entry, *subject_entry, *body_view;
-       const gchar *to, *subject;
+       const gchar *to, *subject, *email_from;
        gchar *body;
        GtkTextIter start, end;
        GtkTextBuffer *buf;
        TnyAccountStoreIface *account_store;
        const GList *transport_accounts;
        TnyTransportAccountIface *transport_account;
+       ModestConf       *conf;
+       ModestAccountMgr *acc_mgr;
 
        g_return_if_fail (modest_ui);
 
@@ -980,10 +785,20 @@ on_send_button_clicked (GtkWidget *widget, ModestUI *modest_ui)
        gtk_text_buffer_get_bounds (buf, &start, &end);
        body    = gtk_text_buffer_get_text (buf, &start, &end, FALSE);
 
-       g_message ("sending %s ==> %s", subject, to);
+       /* FIXME: HACK! */
+       conf = MODEST_CONF(modest_conf_new());
+       acc_mgr = MODEST_ACCOUNT_MGR(modest_account_mgr_new (conf));
+       if (!acc_mgr) {
+               g_warning ("failed to instantiate account mgr");
+               return;
+       }
+       email_from = modest_account_mgr_get_identity_string(acc_mgr, "myidentity", MODEST_ACCOUNT_EMAIL, NULL);
+       /* end HACK */
+       
+       g_message("sending \"%s\" %s ==> %s", subject, email_from, to);
        modest_tny_transport_actions_send_message (actions,
                                                   transport_account,
-                                                  "dirk-jan.binnema@nokia.com",
+                                                  email_from,
                                                   to, "", "", subject,
                                                   body);
        g_free (body);