2 #include <glade/glade.h>
3 #include <glib/gi18n.h>
8 #endif /*HAVE_CONFIG_H*/
10 #include <hildon-widgets/hildon-window.h>
11 #include <hildon-widgets/hildon-program.h>
13 /* TODO: put in auto* */
14 #include <tny-text-buffer-stream.h>
15 #include <tny-msg-folder.h>
17 #include "../modest-ui.h"
18 #include "../modest-window-mgr.h"
19 #include "../modest-account-mgr.h"
20 #include "../modest-account-mgr.h"
21 #include "../modest-identity-mgr.h"
23 #include "../modest-tny-account-store.h"
24 #include "../modest-tny-folder-tree-view.h"
25 #include "../modest-tny-header-tree-view.h"
26 #include "../modest-tny-msg-view.h"
27 #include "../modest-tny-transport-actions.h"
28 #include "../modest-tny-store-actions.h"
30 #include "../modest-text-utils.h"
31 #include "../modest-tny-msg-actions.h"
33 #include "../modest-tny-attachment.h"
35 #include "../modest-editor-window.h"
37 #include "modest-ui-glade.h"
38 #include "modest-ui-wizard.h"
40 #include "modest-ui-message-editor.h"
43 static void on_attach_button_clicked (GtkWidget *widget, ModestEditorWindow *modest_editwin);
45 static void on_send_button_clicked (GtkWidget *widget, ModestEditorWindow *modest_editwin);
50 ModestEditorWindow *edit_win;
56 static gboolean close_edit_confirm_dialog(ModestEditorWindow *edit_win)
61 mdialog = gtk_message_dialog_new(GTK_WINDOW(edit_win->window),
62 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
65 _("Message was modified.\nDiscard Changes?"));
66 gtk_widget_show_all (mdialog);
68 res=gtk_dialog_run(GTK_DIALOG(mdialog));
69 gtk_widget_destroy (mdialog);
70 if (res == GTK_RESPONSE_YES)
77 close_edit_window (GtkWidget *win, GdkEvent *event, gpointer data)
79 ModestEditorWindow *edit_win;
80 ModestUIPrivate *priv;
81 EditWinData *win_data;
83 edit_win = (ModestEditorWindow *)data;
84 win_data = modest_editor_window_get_data(edit_win);
85 priv = MODEST_UI_GET_PRIVATE(win_data->modest_ui);
87 if (modest_editor_window_get_modified(edit_win)) {
88 if (close_edit_confirm_dialog(edit_win)) {
89 gtk_widget_hide (GTK_WIDGET(edit_win->window));
90 modest_window_mgr_unregister(priv->modest_window_mgr, HILDON_WINDOW (edit_win->window));
91 hildon_program_remove_window (priv->program, HILDON_WINDOW (edit_win->window));
92 g_object_unref(edit_win);
93 g_message("closing window");
95 g_message("not closing window");
98 gtk_widget_hide (GTK_WIDGET(edit_win->window));
99 modest_window_mgr_unregister(priv->modest_window_mgr, HILDON_WINDOW (edit_win->window));
100 hildon_program_remove_window (priv->program, HILDON_WINDOW (edit_win->window));
101 g_object_unref(edit_win);
102 g_message("closing window");
108 *modest_ui_new_editor_window (ModestUI *modest_ui, gpointer *user_data)
110 GtkWidget *top_container;
112 EditWinData *win_data;
114 glade_xml = glade_xml_new(MODEST_GLADE, "new_mail", NULL);
118 win_data = g_malloc(sizeof(EditWinData));
119 win_data->modest_ui = modest_ui;
120 win_data->glade_xml = glade_xml;
121 win_data->attachments = NULL;
123 *user_data = win_data;
125 top_container = glade_xml_get_widget(glade_xml, "new_mail");
126 if (!top_container) {
127 g_warning ("Could not create window\n");
128 g_object_unref(G_OBJECT(glade_xml));
132 return GTK_CONTAINER(top_container);
137 modest_ui_editor_window_set_to_header(const gchar *to, gpointer window_data)
141 EditWinData *win_data;
143 win_data = (EditWinData *)window_data;
144 glade_xml = win_data->glade_xml;
145 w = glade_xml_get_widget(glade_xml, "to_entry");
146 gtk_entry_set_text(GTK_ENTRY(w), to);
153 modest_ui_editor_window_set_cc_header(const gchar *cc, gpointer window_data)
157 EditWinData *win_data;
159 win_data = (EditWinData *)window_data;
160 glade_xml = win_data->glade_xml;
162 w = glade_xml_get_widget(glade_xml, "cc_entry");
163 gtk_entry_set_text(GTK_ENTRY(w), cc);
170 modest_ui_editor_window_set_bcc_header(const gchar *bcc, gpointer window_data)
174 EditWinData *win_data;
176 win_data = (EditWinData *)window_data;
177 glade_xml = win_data->glade_xml;
179 w = glade_xml_get_widget(glade_xml, "bcc_entry");
180 gtk_entry_set_text(GTK_ENTRY(w), bcc);
187 modest_ui_editor_window_set_subject_header(const gchar *subject, gpointer window_data)
191 EditWinData *win_data;
193 win_data = (EditWinData *)window_data;
194 glade_xml = win_data->glade_xml;
196 w = glade_xml_get_widget(glade_xml, "subject_entry");
197 gtk_entry_set_text(GTK_ENTRY(w), subject);
204 modest_ui_editor_window_set_body(const gchar *body, gpointer window_data)
207 GtkWidget *body_view;
209 EditWinData *win_data;
211 win_data = (EditWinData *)window_data;
212 glade_xml = win_data->glade_xml;
214 body_view = glade_xml_get_widget(glade_xml, "body_view");
215 buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(body_view));
217 gtk_text_buffer_set_text(GTK_TEXT_BUFFER(buf), body, -1);
224 modest_ui_editor_window_update_attachments(gpointer window_data)
228 glade_xml = ((EditWinData *) window_data)->glade_xml;
230 //body_view = glade_xml_get_widget(glade_xml, "body_view");
237 void on_editor_entry_changed(GtkEditable *editable,
241 EditWinData *windata;
243 edit_win = (GtkWidget *)user_data;
244 windata = (EditWinData *)modest_editor_window_get_data(MODEST_EDITOR_WINDOW(edit_win));
246 modest_editor_window_set_modified(MODEST_EDITOR_WINDOW(edit_win), TRUE);
251 void on_editor_buffer_changed (GtkTextBuffer *textbuffer,
256 edit_win = (GtkWidget *)user_data;
257 modest_editor_window_set_modified(MODEST_EDITOR_WINDOW(edit_win), TRUE);
262 new_editor_with_presets (ModestUI *modest_ui, const gchar *to_header,
263 const gchar *cc_header, const gchar *bcc_header,
264 const gchar *subject_header, const gchar *body,
265 const GList *attachments)
267 ModestEditorWindow *edit_win;
271 EditWinData *windata;
272 ModestUIPrivate *priv;
273 GtkWidget *toolbar, *menubar, *menu, *menu_item;
275 g_return_if_fail (modest_ui);
277 edit_win = MODEST_EDITOR_WINDOW (modest_editor_window_new(modest_ui));
278 windata = (EditWinData *)modest_editor_window_get_data(MODEST_EDITOR_WINDOW(edit_win));
279 g_return_if_fail(windata);
281 glade_xml = windata->glade_xml;
282 btn = glade_xml_get_widget (glade_xml, "toolb_send");
283 g_signal_connect (btn, "clicked", G_CALLBACK(on_send_button_clicked),
285 btn = glade_xml_get_widget (glade_xml, "toolb_attach");
286 g_signal_connect (btn, "clicked", G_CALLBACK(on_attach_button_clicked),
289 w = glade_xml_get_widget (glade_xml, "to_entry");
290 g_signal_connect(w, "changed", G_CALLBACK(on_editor_entry_changed), edit_win);
291 w = glade_xml_get_widget (glade_xml, "subject_entry");
292 g_signal_connect(w, "changed", G_CALLBACK(on_editor_entry_changed), edit_win);
293 w = glade_xml_get_widget (glade_xml, "body_view");
294 buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(w));
295 g_signal_connect(buf, "changed", G_CALLBACK(on_editor_buffer_changed), edit_win);
297 g_signal_connect (edit_win->window, "destroy-event", G_CALLBACK(close_edit_window),
299 g_signal_connect (edit_win->window, "delete-event", G_CALLBACK(close_edit_window),
302 priv = MODEST_UI_GET_PRIVATE(modest_ui);
304 if (strlen(subject_header) > 0)
305 gtk_window_set_title (GTK_WINDOW(edit_win->window), subject_header);
307 gtk_window_set_title (GTK_WINDOW(edit_win->window), _("Untitled"));
309 modest_window_mgr_register(priv->modest_window_mgr, G_OBJECT(edit_win->window), MODEST_EDIT_WINDOW, 0);
311 modest_editor_window_set_to_header(MODEST_EDITOR_WINDOW(edit_win), to_header);
312 modest_editor_window_set_cc_header(MODEST_EDITOR_WINDOW(edit_win), cc_header);
313 modest_editor_window_set_bcc_header(MODEST_EDITOR_WINDOW(edit_win), bcc_header);
314 modest_editor_window_set_subject_header(MODEST_EDITOR_WINDOW(edit_win), subject_header);
315 modest_editor_window_set_body(MODEST_EDITOR_WINDOW(edit_win), body);
316 modest_editor_window_set_attachments(MODEST_EDITOR_WINDOW(edit_win), attachments);
318 modest_editor_window_set_modified(MODEST_EDITOR_WINDOW(edit_win), FALSE);
320 menu = gtk_menu_new();
321 menu_item = glade_xml_get_widget (priv->glade_xml, "menuitem5");
322 gtk_widget_reparent(menu_item, menu);
323 menu_item = glade_xml_get_widget (priv->glade_xml, "menuitem13");
324 gtk_widget_reparent(menu_item, menu);
325 menu_item = glade_xml_get_widget (priv->glade_xml, "menuitem22");
326 gtk_widget_reparent(menu_item, menu);
327 menu_item = glade_xml_get_widget (priv->glade_xml, "menuitem27");
328 gtk_widget_reparent(menu_item, menu);
329 menu_item = glade_xml_get_widget (priv->glade_xml, "menuitem29");
330 gtk_widget_reparent(menu_item, menu);
331 menu_item = glade_xml_get_widget (priv->glade_xml, "menuitem34");
332 gtk_widget_reparent(menu_item, menu);
333 menu_item = glade_xml_get_widget (priv->glade_xml, "menuitem41");
334 gtk_widget_reparent(menu_item, menu);
336 hildon_window_set_menu (HILDON_WINDOW (edit_win->window), GTK_MENU(menu));
338 toolbar = glade_xml_get_widget (priv->glade_xml, "toolbar2");
339 gtk_widget_hide (toolbar);
340 g_object_ref (toolbar);
341 gtk_container_remove (GTK_CONTAINER (glade_xml_get_widget (priv->glade_xml,
342 "new_mail_top_container")), toolbar);
343 hildon_window_add_toolbar (HILDON_WINDOW (edit_win->window), GTK_TOOLBAR (toolbar));
344 gtk_widget_show_all (GTK_WIDGET (edit_win->window));
346 while (gtk_events_pending ())
347 gtk_main_iteration ();
349 toolbar = glade_xml_get_widget (priv->glade_xml, "toolbar2");
350 gtk_widget_hide (toolbar);
352 menubar = glade_xml_get_widget (priv->glade_xml, "menubar2");
353 gtk_widget_hide (GTK_WIDGET(menubar));
355 hildon_program_add_window (priv->program, HILDON_WINDOW (edit_win->window));
360 on_new_mail_clicked (GtkWidget *widget, gpointer user_data)
362 ModestUI *modest_ui = (ModestUI *) user_data;
364 new_editor_with_presets(modest_ui, "", "", "", "", "", NULL);
368 ui_on_mailto_clicked (GtkWidget *widget, const gchar *uri, gpointer user_data)
370 ModestUI *modest_ui = (ModestUI *) user_data;
372 new_editor_with_presets(modest_ui, uri, "", "", "", "", NULL);
377 quoted_send_msg (ModestUI *modest_ui, quoted_send_type qstype)
379 GtkTreeSelection *sel;
383 TnyMsgHeaderIface *header;
385 ModestTnyHeaderTreeView *header_view;
386 ModestTnyMsgView *msg_view;
387 ModestUIPrivate *priv;
389 const TnyMsgIface *msg;
390 const TnyMsgFolderIface *folder;
392 const gchar *subject, *from;
393 gchar *unquoted, *quoted;
395 gint line_limit = 76;
397 GList *attachments = NULL;
399 g_return_if_fail (modest_ui);
401 priv = MODEST_UI_GET_PRIVATE(modest_ui);
403 msg_view = MODEST_TNY_MSG_VIEW(priv->message_view);
404 g_return_if_fail (msg_view);
406 header_view = MODEST_TNY_HEADER_TREE_VIEW(priv->header_view);
407 g_return_if_fail (header_view);
409 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(header_view));
410 g_return_if_fail (sel);
412 if (!gtk_tree_selection_get_selected (sel, &model, &iter)) {
413 g_warning("nothing to reply to");
417 gtk_tree_model_get (model, &iter,
418 TNY_MSG_HEADER_LIST_MODEL_INSTANCE_COLUMN,
422 g_warning("no header");
426 folder = tny_msg_header_iface_get_folder (TNY_MSG_HEADER_IFACE(header));
428 g_warning ("cannot find folder");
432 msg = tny_msg_folder_iface_get_message (TNY_MSG_FOLDER_IFACE(folder), header);
434 g_warning ("cannot find msg");
438 subject = tny_msg_header_iface_get_subject(header);
439 re_sub = g_string_new(subject);
440 /* FIXME: honor replyto, cc */
441 from = tny_msg_header_iface_get_from(header);
442 sent_date = tny_msg_header_iface_get_date_sent(header);
444 unquoted = modest_tny_msg_view_get_selected_text(msg_view);
445 quoted = modest_tny_msg_actions_quote(msg, from, sent_date, line_limit, unquoted);
448 case QUOTED_SEND_REPLY:
449 g_string_prepend(re_sub, _("Re: "));
450 new_editor_with_presets(modest_ui, from, /* cc */ "", /* bcc */ "", re_sub->str, quoted, attachments);
452 case QUOTED_SEND_FORWARD:
453 attachments = modest_tny_attachment_new_list_from_msg(msg, FALSE);
454 g_string_prepend(re_sub, _("Fwd: "));
455 new_editor_with_presets(modest_ui, /* from */ "", /* cc */ "", /* bcc */ "", re_sub->str, quoted, attachments);
457 case QUOTED_SEND_FORWARD_ATTACHED:
458 attachments = modest_tny_attachment_new_list_from_msg(msg, TRUE);
459 g_string_prepend(re_sub, _("Fwd: "));
460 new_editor_with_presets(modest_ui, /* from */ "", /* cc */ "", /* bcc */ "", re_sub->str, "", attachments);
467 g_string_free(re_sub, TRUE);
472 on_attach_button_clicked (GtkWidget *widget, ModestEditorWindow *modest_editwin)
474 /* open file selector */
476 ModestTnyAttachment *attachment;
477 gchar *filename = NULL;
479 dialog = gtk_file_chooser_dialog_new ("Open File",
480 GTK_WINDOW(modest_editwin->window),
481 GTK_FILE_CHOOSER_ACTION_OPEN,
482 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
483 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
486 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
489 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
490 printf ("file:%s\n", filename);
492 gtk_widget_destroy (dialog);
498 attachment = modest_tny_attachment_new();
499 modest_tny_attachment_set_filename(attachment, filename);
500 modest_tny_attachment_guess_mime_type(attachment);
502 modest_editor_window_attach_file(modest_editwin, attachment);
509 on_send_button_clicked (GtkWidget *widget, ModestEditorWindow *modest_editwin)
511 ModestTnyTransportActions *actions;
513 ModestUIPrivate *priv;
514 GtkWidget *to_entry, *subject_entry, *body_view;
515 const gchar *to, *subject, *email_from;
517 GtkTextIter start, end;
519 TnyAccountStoreIface *account_store;
520 const GList *transport_accounts;
521 TnyTransportAccountIface *transport_account;
522 ModestIdentityMgr *id_mgr;
523 EditWinData *win_data;
527 win_data = modest_editor_window_get_data(modest_editwin);
528 modest_ui = win_data->modest_ui;
530 g_return_if_fail (modest_ui);
532 actions = MODEST_TNY_TRANSPORT_ACTIONS
533 (modest_tny_transport_actions_new ());
535 priv = MODEST_UI_GET_PRIVATE(modest_ui);
537 account_store = priv->account_store;
539 tny_account_store_iface_get_transport_accounts (account_store);
540 if (!transport_accounts) {
541 g_message ("cannot send message: no transport account defined");
543 } else /* take the first one! */
545 TNY_TRANSPORT_ACCOUNT_IFACE(transport_accounts->data);
547 to_entry = glade_xml_get_widget (win_data->glade_xml, "to_entry");
548 subject_entry = glade_xml_get_widget (win_data->glade_xml, "subject_entry");
549 body_view = glade_xml_get_widget (win_data->glade_xml, "body_view");
551 to = gtk_entry_get_text (GTK_ENTRY(to_entry));
552 subject = gtk_entry_get_text (GTK_ENTRY(subject_entry));
554 buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(body_view));
555 gtk_text_buffer_get_bounds (buf, &start, &end);
556 body = gtk_text_buffer_get_text (buf, &start, &end, FALSE);
558 id_mgr = priv->modest_id_mgr;
559 email_from = modest_identity_mgr_get_identity_string(id_mgr,
560 MODEST_IDENTITY_DEFAULT_IDENTITY,
561 MODEST_IDENTITY_EMAIL, NULL);
562 attachments = modest_editor_window_get_attachments(modest_editwin);
566 g_message("sending \"%s\" %s ==> %s", subject, email_from, to);
568 modest_tny_transport_actions_send_message (actions,
575 modest_editor_window_set_attachments(modest_editwin, NULL); /* This unrefs them, too. */
577 g_object_unref (G_OBJECT(actions));
579 gtk_widget_hide (GTK_WIDGET(modest_editwin->window));
580 modest_window_mgr_unregister(priv->modest_window_mgr, G_OBJECT(modest_editwin));
581 if (G_IS_OBJECT(modest_editwin)) {
582 g_object_unref(modest_editwin);
584 g_warning("editor window has vanished!");