1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of the Nokia Corporation nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <glib/gi18n.h>
31 #include <tny-account-store.h>
32 #include <tny-simple-list.h>
33 #include <modest-conf.h>
34 #include <modest-runtime.h>
35 #include <modest-tny-msg.h>
37 #include <widgets/modest-window-priv.h>
38 #include <widgets/modest-msg-edit-window.h>
39 #include <widgets/modest-msg-edit-window-ui.h>
40 #include <widgets/modest-combo-box.h>
42 #include <modest-widget-memory.h>
43 #include <modest-account-mgr-helpers.h>
45 static void modest_msg_edit_window_class_init (ModestMsgEditWindowClass *klass);
46 static void modest_msg_edit_window_init (ModestMsgEditWindow *obj);
47 static void modest_msg_edit_window_finalize (GObject *obj);
56 typedef struct _ModestMsgEditWindowPrivate ModestMsgEditWindowPrivate;
57 struct _ModestMsgEditWindowPrivate {
64 ModestProtoList *from_field_protos;
65 GtkWidget *from_field;
70 GtkWidget *subject_field;
73 #define MODEST_MSG_EDIT_WINDOW_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
74 MODEST_TYPE_MSG_EDIT_WINDOW, \
75 ModestMsgEditWindowPrivate))
77 static GtkWindowClass *parent_class = NULL;
79 /* uncomment the following if you have defined any signals */
80 /* static guint signals[LAST_SIGNAL] = {0}; */
83 modest_msg_edit_window_get_type (void)
85 static GType my_type = 0;
87 static const GTypeInfo my_info = {
88 sizeof(ModestMsgEditWindowClass),
90 NULL, /* base finalize */
91 (GClassInitFunc) modest_msg_edit_window_class_init,
92 NULL, /* class finalize */
93 NULL, /* class data */
94 sizeof(ModestMsgEditWindow),
96 (GInstanceInitFunc) modest_msg_edit_window_init,
99 my_type = g_type_register_static (MODEST_TYPE_WINDOW,
100 "ModestMsgEditWindow",
108 save_state (ModestWindow *self)
110 modest_widget_memory_save (modest_runtime_get_conf (),
111 G_OBJECT(self), "modest-edit-msg-window");
116 restore_settings (ModestMsgEditWindow *self)
118 modest_widget_memory_restore (modest_runtime_get_conf (),
119 G_OBJECT(self), "modest-edit-msg-window");
123 modest_msg_edit_window_class_init (ModestMsgEditWindowClass *klass)
125 GObjectClass *gobject_class;
126 ModestWindowClass *modest_window_class;
128 gobject_class = (GObjectClass*) klass;
129 modest_window_class = (ModestWindowClass*) klass;
131 parent_class = g_type_class_peek_parent (klass);
132 gobject_class->finalize = modest_msg_edit_window_finalize;
134 g_type_class_add_private (gobject_class, sizeof(ModestMsgEditWindowPrivate));
136 modest_window_class->save_state_func = save_state;
140 modest_msg_edit_window_init (ModestMsgEditWindow *obj)
142 ModestMsgEditWindowPrivate *priv;
143 priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE(obj);
145 priv->toolbar = NULL;
146 priv->menubar = NULL;
147 priv->msg_body = NULL;
148 priv->from_field = NULL;
149 priv->to_field = NULL;
150 priv->cc_field = NULL;
151 priv->bcc_field = NULL;
152 priv->subject_field = NULL;
156 * @result: A ModestPairList, which must be freed with modest_pair_list_free().
158 static ModestPairList*
159 get_transports (void)
161 ModestAccountMgr *account_mgr;
162 GSList *transports = NULL;
163 GSList *cursor, *accounts;
165 account_mgr = modest_runtime_get_account_mgr();
166 cursor = accounts = modest_account_mgr_account_names (account_mgr, TRUE);
168 gchar *account_name = cursor->data ? g_strdup((gchar*)cursor->data) : NULL;
169 gchar *from_string = modest_account_mgr_get_from_string (account_mgr,
172 /* something went wrong: ignore this one */
173 g_free (account_name);
177 pair = modest_pair_new ((gpointer) account_name,
178 (gpointer) from_string , TRUE);
179 transports = g_slist_prepend (transports, pair);
180 } /* don't free account name; it's freed when the transports list is freed */
181 cursor = cursor->next;
183 g_slist_free (accounts);
189 on_from_combo_changed (ModestComboBox *combo, ModestWindow *win)
191 modest_window_set_active_account (
192 win, modest_combo_box_get_active_id(combo));
198 init_window (ModestMsgEditWindow *obj, const gchar* account)
200 GtkWidget *to_button, *cc_button, *bcc_button;
201 GtkWidget *header_table;
202 GtkWidget *main_vbox;
203 ModestMsgEditWindowPrivate *priv;
204 ModestWindowPrivate *parent_priv;
206 priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE(obj);
207 parent_priv = MODEST_WINDOW_GET_PRIVATE(obj);
209 to_button = gtk_button_new_with_label (_("To..."));
210 cc_button = gtk_button_new_with_label (_("Cc..."));
211 bcc_button = gtk_button_new_with_label (_("Bcc..."));
213 /* Note: This ModestPairList* must exist for as long as the combo
214 * that uses it, because the ModestComboBox uses the ID opaquely,
215 * so it can't know how to manage its memory. */
216 priv->from_field_protos = get_transports ();
217 priv->from_field = modest_combo_box_new (priv->from_field_protos, g_str_equal);
220 modest_combo_box_set_active_id (MODEST_COMBO_BOX(priv->from_field),
222 modest_window_set_active_account (MODEST_WINDOW(obj), account);
224 /* auto-update the active account */
225 g_signal_connect (G_OBJECT(priv->from_field), "changed", G_CALLBACK(on_from_combo_changed), obj);
227 priv->to_field = gtk_entry_new_with_max_length (80);
228 priv->cc_field = gtk_entry_new_with_max_length (80);
229 priv->bcc_field = gtk_entry_new_with_max_length (80);
230 priv->subject_field = gtk_entry_new_with_max_length (80);
232 header_table = gtk_table_new (5,2, FALSE);
234 gtk_table_attach (GTK_TABLE(header_table), gtk_label_new (_("From:")),
235 0,1,0,1, GTK_SHRINK, 0, 0, 0);
236 gtk_table_attach (GTK_TABLE(header_table), to_button, 0,1,1,2, GTK_SHRINK, 0, 0, 0);
237 gtk_table_attach (GTK_TABLE(header_table), cc_button, 0,1,2,3, GTK_SHRINK, 0, 0, 0);
238 gtk_table_attach (GTK_TABLE(header_table), bcc_button, 0,1,3,4, GTK_SHRINK, 0, 0, 0);
239 gtk_table_attach (GTK_TABLE(header_table), gtk_label_new (_("Subject:")),
240 0,1,4,5, GTK_SHRINK, 0, 0, 0);
242 gtk_table_attach_defaults (GTK_TABLE(header_table), priv->from_field, 1,2,0,1);
243 gtk_table_attach_defaults (GTK_TABLE(header_table), priv->to_field, 1,2,1,2);
244 gtk_table_attach_defaults (GTK_TABLE(header_table), priv->cc_field, 1,2,2,3);
245 gtk_table_attach_defaults (GTK_TABLE(header_table), priv->bcc_field, 1,2,3,4);
246 gtk_table_attach_defaults (GTK_TABLE(header_table), priv->subject_field,1,2,4,5);
248 priv->msg_body = gtk_text_view_new ();
250 main_vbox = gtk_vbox_new (FALSE, 6);
252 gtk_box_pack_start (GTK_BOX(main_vbox), priv->menubar, FALSE, FALSE, 0);
253 gtk_box_pack_start (GTK_BOX(main_vbox), priv->toolbar, FALSE, FALSE, 0);
254 gtk_box_pack_start (GTK_BOX(main_vbox), header_table, FALSE, FALSE, 6);
255 gtk_box_pack_start (GTK_BOX(main_vbox), priv->msg_body, TRUE, TRUE, 6);
257 gtk_widget_show_all (GTK_WIDGET(main_vbox));
258 gtk_container_add (GTK_CONTAINER(obj), main_vbox);
263 modest_msg_edit_window_finalize (GObject *obj)
265 ModestMsgEditWindowPrivate *priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE(obj);
267 /* These had to stay alive for as long as the comboboxes that used them: */
268 modest_pair_list_free (priv->from_field_protos);
270 G_OBJECT_CLASS(parent_class)->finalize (obj);
276 on_delete_event (GtkWidget *widget, GdkEvent *event, ModestMsgEditWindow *self)
278 modest_window_save_state (MODEST_WINDOW(self));
284 set_msg (ModestMsgEditWindow *self, TnyMsg *msg)
288 const gchar *to, *cc, *bcc, *subject;
289 ModestMsgEditWindowPrivate *priv;
292 g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (self));
293 g_return_if_fail (TNY_IS_MSG (msg));
295 priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (self);
297 header = tny_msg_get_header (msg);
298 to = tny_header_get_to (header);
299 cc = tny_header_get_cc (header);
300 bcc = tny_header_get_bcc (header);
301 subject = tny_header_get_subject (header);
304 gtk_entry_set_text (GTK_ENTRY(priv->to_field), to);
306 gtk_entry_set_text (GTK_ENTRY(priv->cc_field), cc);
308 gtk_entry_set_text (GTK_ENTRY(priv->bcc_field), bcc);
310 gtk_entry_set_text (GTK_ENTRY(priv->subject_field), subject);
313 buf = gtk_text_view_get_buffer (GTK_TEXT_VIEW(priv->msg_body));
314 body = modest_tny_msg_get_body (msg, FALSE);
316 gtk_text_buffer_set_text (buf, body, -1);
322 modest_msg_edit_window_new (TnyMsg *msg, const gchar *account)
324 ModestMsgEditWindow *self;
325 ModestMsgEditWindowPrivate *priv;
326 ModestWindowPrivate *parent_priv;
327 GtkActionGroup *action_group;
328 GError *error = NULL;
330 g_return_val_if_fail (msg, NULL);
332 self = MODEST_MSG_EDIT_WINDOW(g_object_new(MODEST_TYPE_MSG_EDIT_WINDOW, NULL));
333 priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE(self);
334 parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
336 parent_priv->ui_manager = gtk_ui_manager_new();
337 action_group = gtk_action_group_new ("ModestMsgEditWindowActions");
339 /* Add common actions */
340 gtk_action_group_add_actions (action_group,
341 modest_msg_edit_action_entries,
342 G_N_ELEMENTS (modest_msg_edit_action_entries),
344 gtk_action_group_add_toggle_actions (action_group,
345 modest_msg_edit_toggle_action_entries,
346 G_N_ELEMENTS (modest_msg_edit_toggle_action_entries),
348 gtk_ui_manager_insert_action_group (parent_priv->ui_manager, action_group, 0);
349 g_object_unref (action_group);
351 /* Load the UI definition */
352 gtk_ui_manager_add_ui_from_file (parent_priv->ui_manager,
353 MODEST_UIDIR "modest-msg-edit-window-ui.xml",
356 g_printerr ("modest: could not merge modest-msg-edit-window-ui.xml: %s\n", error->message);
357 g_error_free (error);
362 /* Add accelerators */
363 gtk_window_add_accel_group (GTK_WINDOW (self),
364 gtk_ui_manager_get_accel_group (parent_priv->ui_manager));
366 /* Toolbar / Menubar */
367 priv->toolbar = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar");
368 priv->menubar = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/MenuBar");
370 gtk_toolbar_set_tooltips (GTK_TOOLBAR (priv->toolbar), TRUE);
373 init_window (MODEST_MSG_EDIT_WINDOW(self), account);
375 restore_settings (MODEST_MSG_EDIT_WINDOW(self));
377 gtk_window_set_title (GTK_WINDOW(self), "Modest");
378 gtk_window_set_icon_from_file (GTK_WINDOW(self), MODEST_APP_ICON, NULL);
380 g_signal_connect (G_OBJECT(self), "delete-event",
381 G_CALLBACK(on_delete_event), self);
385 return MODEST_WINDOW(self);
390 modest_msg_edit_window_get_msg_data (ModestMsgEditWindow *edit_window)
394 const gchar *account_name;
395 gchar *from_string = NULL;
396 ModestMsgEditWindowPrivate *priv;
398 g_return_val_if_fail (MODEST_IS_MSG_EDIT_WINDOW (edit_window), NULL);
400 priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (edit_window);
402 account_name = (gchar*)modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->from_field));
404 from_string = modest_account_mgr_get_from_string (
405 modest_runtime_get_account_mgr(), account_name);
407 g_printerr ("modest: cannot get from string\n");
413 data = g_slice_new0 (MsgData);
414 data->from = from_string; /* will be freed when data is freed */
415 data->to = g_strdup (gtk_entry_get_text (GTK_ENTRY(priv->to_field)));
416 data->cc = g_strdup ( gtk_entry_get_text (GTK_ENTRY(priv->cc_field)));
417 data->bcc = g_strdup ( gtk_entry_get_text (GTK_ENTRY(priv->bcc_field)));
418 data->subject = g_strdup ( gtk_entry_get_text (GTK_ENTRY(priv->subject_field)));
421 GtkTextBuffer *buf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->msg_body));
423 gtk_text_buffer_get_bounds (buf, &b, &e);
424 data->plain_body = gtk_text_buffer_get_text (buf, &b, &e, FALSE); /* Returns a copy. */
426 /* No rich supported yet, then html body is NULL */
427 data->html_body = NULL;
433 modest_msg_edit_window_free_msg_data (ModestMsgEditWindow *edit_window,
436 g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (edit_window));
444 g_free (data->subject);
445 g_free (data->plain_body);
446 g_free (data->html_body);
448 /* TODO: Free data->attachments? */
450 g_slice_free (MsgData, data);
453 /* Rich formatting API functions */
455 modest_msg_edit_window_get_format (ModestMsgEditWindow *self)
457 return MODEST_MSG_EDIT_FORMAT_TEXT;
461 modest_msg_edit_window_set_format (ModestMsgEditWindow *self,
462 ModestMsgEditFormat format)
465 case MODEST_MSG_EDIT_FORMAT_TEXT:
467 case MODEST_MSG_EDIT_FORMAT_HTML:
468 g_message ("HTML format not supported in Gnome ModestMsgEditWindow");
475 ModestMsgEditFormatState *
476 modest_msg_edit_window_get_format_state (ModestMsgEditWindow *self)
478 ModestMsgEditFormatState *format_state;
480 g_return_val_if_fail (MODEST_IS_MSG_EDIT_WINDOW (self), NULL);
482 format_state = g_new0 (ModestMsgEditFormatState, 1);
488 modest_msg_edit_window_set_format_state (ModestMsgEditWindow *self,
489 const ModestMsgEditFormatState *format_state)
491 g_return_if_fail (MODEST_MSG_EDIT_WINDOW (self));
493 /* Ends silently as set_format_state should do nothing when edit window format
499 modest_msg_edit_window_select_color (ModestMsgEditWindow *window)
501 g_return_if_fail (MODEST_MSG_EDIT_WINDOW (window));
503 g_message ("Select color operation is not supported");
507 modest_msg_edit_window_select_file_format (ModestMsgEditWindow *window,
510 g_return_if_fail (MODEST_MSG_EDIT_WINDOW (window));
512 g_message ("Select file format operation is not supported");
516 modest_msg_edit_window_select_font (ModestMsgEditWindow *window)
518 g_return_if_fail (MODEST_MSG_EDIT_WINDOW (window));
520 g_message ("Select font operation is not supported");
524 modest_msg_edit_window_select_background_color (ModestMsgEditWindow *window)
526 g_return_if_fail (MODEST_MSG_EDIT_WINDOW (window));
528 g_message ("Select background color operation is not supported");
532 modest_msg_edit_window_insert_image (ModestMsgEditWindow *window)
534 g_return_if_fail (MODEST_MSG_EDIT_WINDOW (window));
536 g_message ("Insert image operation is not supported");
540 modest_msg_edit_window_attach_file (ModestMsgEditWindow *window)
542 g_return_if_fail (MODEST_MSG_EDIT_WINDOW (window));
544 g_message ("Attach file operation is not supported");
548 modest_msg_edit_window_remove_attachments (ModestMsgEditWindow *window,
551 g_return_if_fail (MODEST_MSG_EDIT_WINDOW (window));
553 g_message ("Remove attachments operation is not supported");
557 modest_msg_edit_window_show_cc (ModestMsgEditWindow *window,
560 g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
562 g_message ("not implemented yet %s", __FUNCTION__);
565 modest_msg_edit_window_show_bcc (ModestMsgEditWindow *window,
568 g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
570 g_message ("not implemented yet %s", __FUNCTION__);
573 modest_msg_edit_window_undo (ModestMsgEditWindow *window)
575 g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
577 g_message ("not implemented yet %s", __FUNCTION__);
580 modest_msg_edit_window_toggle_fullscreen (ModestMsgEditWindow *window)
582 g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
584 g_message ("not implemented yet %s", __FUNCTION__);
587 modest_msg_edit_window_set_priority_flags (ModestMsgEditWindow *window,
588 TnyHeaderFlags priority_flags)
590 g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
592 g_message ("not implemented yet %s", __FUNCTION__);
597 modest_msg_edit_window_select_contacts (ModestMsgEditWindow *window)
599 g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
601 g_message ("not implemented yet %s", __FUNCTION__);
605 modest_msg_edit_window_check_names (ModestMsgEditWindow *window)
607 g_return_val_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window), FALSE);
609 g_message ("not implemented yet %s", __FUNCTION__);
614 modest_msg_edit_window_set_file_format (ModestMsgEditWindow *window,
617 g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
619 g_message ("not implemented yet %s", __FUNCTION__);