* Cleaned some header files, deleting unused includes
[modest] / src / maemo / modest-edit-msg-window.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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.
16  *
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.
28  */
29 #include <glib/gi18n.h>
30 #include <tny-account-store.h>
31 #include "modest-edit-msg-window.h"
32 #include "modest-icon-names.h"
33 #include "modest-icon-factory.h"
34 #include "modest-widget-memory.h"
35 #include "modest-mail-operation.h"
36 #include "modest-tny-platform-factory.h"
37 #include "modest-tny-msg-actions.h"
38 #include <tny-simple-list.h>
39
40 static void  modest_edit_msg_window_class_init   (ModestEditMsgWindowClass *klass);
41 static void  modest_edit_msg_window_init         (ModestEditMsgWindow *obj);
42 static void  modest_edit_msg_window_finalize     (GObject *obj);
43
44 /* list my signals */
45 enum {
46         /* MY_SIGNAL_1, */
47         /* MY_SIGNAL_2, */
48         LAST_SIGNAL
49 };
50
51 typedef struct _ModestEditMsgWindowPrivate ModestEditMsgWindowPrivate;
52 struct _ModestEditMsgWindowPrivate {
53
54         ModestWidgetFactory *widget_factory;
55         TnyPlatformFactory *fact;
56         GtkUIManager *ui_manager;
57         
58         GtkWidget      *toolbar, *menubar;
59         GtkWidget      *msg_body;
60         GtkWidget      *from_field, *to_field, *cc_field, *bcc_field,
61                        *subject_field;
62 };
63
64 #define MODEST_EDIT_MSG_WINDOW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
65                                                     MODEST_TYPE_EDIT_MSG_WINDOW, \
66                                                     ModestEditMsgWindowPrivate))
67 /* globals */
68 static GtkWindowClass *parent_class = NULL;
69
70 /* uncomment the following if you have defined any signals */
71 /* static guint signals[LAST_SIGNAL] = {0}; */
72
73 GType
74 modest_edit_msg_window_get_type (void)
75 {
76         static GType my_type = 0;
77         if (!my_type) {
78                 static const GTypeInfo my_info = {
79                         sizeof(ModestEditMsgWindowClass),
80                         NULL,           /* base init */
81                         NULL,           /* base finalize */
82                         (GClassInitFunc) modest_edit_msg_window_class_init,
83                         NULL,           /* class finalize */
84                         NULL,           /* class data */
85                         sizeof(ModestEditMsgWindow),
86                         1,              /* n_preallocs */
87                         (GInstanceInitFunc) modest_edit_msg_window_init,
88                         NULL
89                 };
90                 my_type = g_type_register_static (HILDON_TYPE_WINDOW,
91                                                   "ModestEditMsgWindow",
92                                                   &my_info, 0);
93         }
94         return my_type;
95 }
96
97 static void
98 modest_edit_msg_window_class_init (ModestEditMsgWindowClass *klass)
99 {
100         GObjectClass *gobject_class;
101         gobject_class = (GObjectClass*) klass;
102
103         parent_class            = g_type_class_peek_parent (klass);
104         gobject_class->finalize = modest_edit_msg_window_finalize;
105
106         g_type_class_add_private (gobject_class, sizeof(ModestEditMsgWindowPrivate));
107
108         /* signal definitions go here, e.g.: */
109 /*      signals[MY_SIGNAL_1] = */
110 /*              g_signal_new ("my_signal_1",....); */
111 /*      signals[MY_SIGNAL_2] = */
112 /*              g_signal_new ("my_signal_2",....); */
113 /*      etc. */
114 }
115
116 static void
117 modest_edit_msg_window_init (ModestEditMsgWindow *obj)
118 {
119         ModestEditMsgWindowPrivate *priv;
120         priv = MODEST_EDIT_MSG_WINDOW_GET_PRIVATE(obj);
121
122         priv->fact = modest_tny_platform_factory_get_instance ();
123         priv->widget_factory = NULL;
124         priv->toolbar = NULL;
125         priv->menubar = NULL;
126 }
127
128
129
130 static void
131 save_settings (ModestEditMsgWindow *self)
132 {
133         ModestEditMsgWindowPrivate *priv;
134         ModestConf *conf;
135
136         priv = MODEST_EDIT_MSG_WINDOW_GET_PRIVATE(self);
137         conf = modest_tny_platform_factory_get_modest_conf_instance (priv->fact);
138
139         modest_widget_memory_save (conf, G_OBJECT(self), "modest-edit-msg-window");
140 }
141
142
143 static void
144 restore_settings (ModestEditMsgWindow *self)
145 {
146         ModestEditMsgWindowPrivate *priv;
147         ModestConf *conf;
148
149         priv = MODEST_EDIT_MSG_WINDOW_GET_PRIVATE(self);
150         conf = modest_tny_platform_factory_get_modest_conf_instance (priv->fact);
151
152         modest_widget_memory_restore (conf, G_OBJECT(self), "modest-edit-msg-window");
153 }
154
155
156 /* /\* Our menu, an array of GtkItemFactoryEntry structures that defines each menu item *\/ */
157 /* static GtkItemFactoryEntry menu_items[] = { */
158 /*      { "/_File",             NULL,                   NULL,           0, "<Branch>" ,NULL}, */
159 /*      { "/File/_New",         "<control>N",           NULL,           0, "<StockItem>", GTK_STOCK_NEW }, */
160 /*      { "/File/_Open",        "<control>O",           NULL,           0, "<StockItem>", GTK_STOCK_OPEN }, */
161 /*      { "/File/_Save",        "<control>S",           NULL,           0, "<StockItem>", GTK_STOCK_SAVE }, */
162 /*      { "/File/Save _As",     NULL,                   NULL,           0, "<Item>", NULL} , */
163 /*      { "/File/Save Draft",   "<control><shift>S",    NULL,           0, "<Item>",NULL }, */
164
165
166 /*      { "/File/sep1",         NULL,                   NULL,           0, "<Separator>" ,NULL }, */
167 /*      { "/File/_Quit",        "<CTRL>Q",              on_menu_quit,   0, "<StockItem>", GTK_STOCK_QUIT }, */
168
169 /*      { "/_Edit",             NULL,                   NULL,           0, "<Branch>" ,NULL }, */
170 /*      { "/Edit/_Undo",        "<CTRL>Z",              NULL,           0, "<StockItem>", GTK_STOCK_UNDO }, */
171 /*      { "/Edit/_Redo",        "<shift><CTRL>Z",       NULL,           0, "<StockItem>", GTK_STOCK_REDO }, */
172 /*      { "/File/sep1",         NULL,                   NULL,           0, "<Separator>",NULL }, */
173 /*      { "/Edit/Cut",          "<control>X",           NULL,           0, "<StockItem>", GTK_STOCK_CUT  }, */
174 /*      { "/Edit/Copy",         "<CTRL>C",              NULL,           0, "<StockItem>", GTK_STOCK_COPY }, */
175 /*      { "/Edit/Paste",        NULL,                   NULL,           0, "<StockItem>", GTK_STOCK_PASTE}, */
176 /*      { "/Edit/sep1",         NULL,                   NULL,           0, "<Separator>",NULL }, */
177 /*      { "/Edit/Delete",       "<CTRL>Q",              NULL,           0, "<Item>" ,NULL }, */
178 /*      { "/Edit/Select all",   "<CTRL>A",              NULL,           0, "<Item>" ,NULL }, */
179 /*      { "/Edit/Deselect all",  "<Shift><CTRL>A",      NULL,           0, "<Item>",NULL }, */
180
181 /*      { "/_View",             NULL,           NULL,                   0, "<Branch>",NULL }, */
182 /*      { "/View/To-field",          NULL,              NULL,           0, "<CheckItem>",NULL }, */
183         
184 /*      { "/View/Cc-field:",          NULL,             NULL,           0, "<CheckItem>",NULL }, */
185 /*      { "/View/Bcc-field:",          NULL,            NULL,           0, "<CheckItem>",NULL }, */
186         
187         
188 /*      { "/_Insert",             NULL,         NULL,           0, "<Branch>",NULL }, */
189 /* /\*  { "/Actions/_Reply",    NULL,                   NULL,           0, "<Item>" }, *\/ */
190 /* /\*  { "/Actions/_Forward",  NULL,                   NULL,           0, "<Item>" }, *\/ */
191 /* /\*  { "/Actions/_Bounce",   NULL,                   NULL,           0, "<Item>" },   *\/ */
192         
193 /*      { "/_Format",            NULL,                  NULL,           0, "<Branch>",NULL } */
194 /* /\*  { "/Options/_Accounts",  NULL,                  on_menu_accounts,0, "<Item>" }, *\/ */
195 /* /\*  { "/Options/_Contacts",  NULL,                  NULL,           0, "<Item>" }, *\/ */
196
197
198 /* /\*  { "/_Help",         NULL,                       NULL,           0, "<Branch>" }, *\/ */
199 /* /\*  { "/_Help/About",   NULL,                       on_menu_about,  0, "<StockItem>", GTK_STOCK_ABOUT}, *\/ */
200 /* }; */
201
202 /* static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]); */
203
204
205 /* static void */
206 /* send_mail (ModestEditMsgWindow *self) */
207 /* { */
208 /*      const gchar *to, *cc, *bcc, *subject; */
209 /*      gchar *body, *from; */
210 /*      ModestEditMsgWindowPrivate *priv; */
211 /*      TnyTransportAccount *transport_account; */
212 /*      ModestMailOperation *mail_operation; */
213 /*      ModestAccountData *data; */
214         
215 /*      GtkTextBuffer *buf; */
216 /*      GtkTextIter b, e; */
217         
218 /*      priv = MODEST_EDIT_MSG_WINDOW_GET_PRIVATE(self); */
219 /*      data = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->from_field)); */
220
221 /*      /\* don't free these (except from) *\/ */
222 /*      from    =  g_strdup_printf ("%s <%s>", data->full_name, data->email) ; */
223 /*      to      =  gtk_entry_get_text (GTK_ENTRY(priv->to_field)); */
224 /*      cc      =  gtk_entry_get_text (GTK_ENTRY(priv->cc_field)); */
225 /*      bcc     =  gtk_entry_get_text (GTK_ENTRY(priv->bcc_field)); */
226 /*      subject =  gtk_entry_get_text (GTK_ENTRY(priv->subject_field)); */
227         
228 /*      /\* don't unref *\/ */
229 /*      buf   =  gtk_text_view_get_buffer (GTK_TEXT_VIEW(priv->msg_body)); */
230         
231 /*      gtk_text_buffer_get_bounds (buf, &b, &e); */
232 /*      body  = gtk_text_buffer_get_text (buf, &b, &e, */
233 /*                                        FALSE); /\* free this one *\/ */
234
235 /*      /\* FIXME: Code added just for testing. The final version will */
236 /*         use the send queue provided by tinymail and some */
237 /*         classifier *\/ */
238 /*      { */
239 /*              TnyList *accounts; */
240 /*              TnyIterator *iter; */
241 /*              TnyAccountStore *account_store; */
242
243 /*              accounts = TNY_LIST(tny_simple_list_new ()); */
244 /*              account_store = tny_platform_factory_new_account_store (priv->fact); */
245 /*              tny_account_store_get_accounts (account_store, accounts, */
246 /*                                              TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS); */
247
248 /*              iter = tny_list_create_iterator(accounts); */
249 /*              tny_iterator_first (iter); */
250 /*              if (tny_iterator_is_done (iter)) { */
251 /*                      /\* FIXME: Add error handling through mail operation *\/ */
252 /*                      g_printerr("modest: no transport accounts defined\n"); */
253 /*                      g_free (body); */
254 /*                      return; */
255 /*              } */
256 /*              transport_account = TNY_TRANSPORT_ACCOUNT (tny_iterator_get_current(iter)); */
257 /*              g_object_ref (transport_account); */
258
259 /*              tny_list_foreach (accounts, (GFunc) g_object_unref, NULL); */
260 /*              g_object_unref (G_OBJECT (accounts)); */
261 /*              g_object_unref (G_OBJECT (iter)); */
262 /*      } */
263
264 /*      mail_operation = modest_mail_operation_new (); */
265
266 /*      modest_mail_operation_send_new_mail (mail_operation, */
267 /*                                           transport_account, */
268 /*                                           from, to, cc, bcc, */
269 /*                                           subject, body, NULL); */
270 /*      /\* Clean up *\/ */
271 /*      g_object_unref (G_OBJECT (mail_operation)); */
272 /*      g_object_unref (G_OBJECT (transport_account)); */
273 /*      g_free (from); */
274 /*      g_free (body); */
275 /* } */
276
277
278 /* static void */
279 /* on_toolbar_button_clicked (ModestToolbar *toolbar, ModestToolbarButton button_id, */
280 /*                         ModestEditMsgWindow *self) */
281 /* { */
282 /*      switch (button_id) { */
283 /*      case MODEST_TOOLBAR_BUTTON_MAIL_SEND: */
284 /*              send_mail (self); */
285 /*              save_settings (self); */
286 /*              gtk_widget_destroy (GTK_WIDGET(self)); */
287 /*              break; */
288                 
289 /*      case MODEST_TOOLBAR_BUTTON_REPLY: */
290 /*      case MODEST_TOOLBAR_BUTTON_REPLY_ALL: */
291 /*      case MODEST_TOOLBAR_BUTTON_FORWARD: */
292 /*      case MODEST_TOOLBAR_BUTTON_SEND_RECEIVE: */
293 /*      case MODEST_TOOLBAR_BUTTON_NEXT: */
294 /*      case MODEST_TOOLBAR_BUTTON_PREV: */
295 /*      case MODEST_TOOLBAR_BUTTON_DELETE: */
296
297 /*      default: */
298 /*              g_printerr ("modest: key %d pressed\n", button_id); */
299 /*      } */
300 /* } */
301
302
303
304 static void
305 init_window (ModestEditMsgWindow *obj)
306 {
307         GtkWidget *to_button, *cc_button, *bcc_button; 
308         GtkWidget *header_table;
309         GtkWidget *main_vbox;
310         
311         ModestEditMsgWindowPrivate *priv;
312         priv = MODEST_EDIT_MSG_WINDOW_GET_PRIVATE(obj);
313
314         to_button     = gtk_button_new_with_label (_("To..."));
315         cc_button     = gtk_button_new_with_label (_("Cc..."));
316         bcc_button    = gtk_button_new_with_label (_("Bcc..."));
317
318         priv->from_field    = modest_widget_factory_get_combo_box (priv->widget_factory,
319                                                                    MODEST_COMBO_BOX_TYPE_TRANSPORTS);
320         priv->to_field      = gtk_entry_new_with_max_length (80);
321         priv->cc_field      = gtk_entry_new_with_max_length (80);
322         priv->bcc_field     = gtk_entry_new_with_max_length (80);
323         priv->subject_field = gtk_entry_new_with_max_length (80);
324         
325         header_table = gtk_table_new (5,2, FALSE);
326         
327         gtk_table_attach (GTK_TABLE(header_table), gtk_label_new (_("From:")),
328                           0,1,0,1, GTK_SHRINK, 0, 0, 0);
329         gtk_table_attach (GTK_TABLE(header_table), to_button,     0,1,1,2, GTK_SHRINK, 0, 0, 0);
330         gtk_table_attach (GTK_TABLE(header_table), cc_button,     0,1,2,3, GTK_SHRINK, 0, 0, 0);
331         gtk_table_attach (GTK_TABLE(header_table), bcc_button,    0,1,3,4, GTK_SHRINK, 0, 0, 0);
332         gtk_table_attach (GTK_TABLE(header_table), gtk_label_new (_("Subject:")),
333                           0,1,4,5, GTK_SHRINK, 0, 0, 0);
334
335         gtk_table_attach_defaults (GTK_TABLE(header_table), priv->from_field,   1,2,0,1);
336         gtk_table_attach_defaults (GTK_TABLE(header_table), priv->to_field,     1,2,1,2);
337         gtk_table_attach_defaults (GTK_TABLE(header_table), priv->cc_field,     1,2,2,3);
338         gtk_table_attach_defaults (GTK_TABLE(header_table), priv->bcc_field,    1,2,3,4);
339         gtk_table_attach_defaults (GTK_TABLE(header_table), priv->subject_field,1,2,4,5);
340
341         priv->msg_body = gtk_text_view_new ();
342         
343         main_vbox = gtk_vbox_new  (FALSE, 6);
344
345         gtk_box_pack_start (GTK_BOX(main_vbox), priv->menubar, FALSE, FALSE, 0);
346         gtk_box_pack_start (GTK_BOX(main_vbox), priv->toolbar, FALSE, FALSE, 0);
347         gtk_box_pack_start (GTK_BOX(main_vbox), header_table, FALSE, FALSE, 6);
348         gtk_box_pack_start (GTK_BOX(main_vbox), priv->msg_body, TRUE, TRUE, 6);
349
350         gtk_widget_show_all (GTK_WIDGET(main_vbox));
351         gtk_container_add (GTK_CONTAINER(obj), main_vbox);
352 }
353         
354
355
356 static void
357 modest_edit_msg_window_finalize (GObject *obj)
358 {
359         ModestEditMsgWindowPrivate *priv;
360
361         priv = MODEST_EDIT_MSG_WINDOW_GET_PRIVATE(obj);
362
363         g_object_unref (G_OBJECT(priv->widget_factory));
364         priv->widget_factory = NULL;
365         
366         G_OBJECT_CLASS(parent_class)->finalize (obj);
367
368 }
369
370
371
372 static gboolean
373 on_delete_event (GtkWidget *widget, GdkEvent *event, ModestEditMsgWindow *self)
374 {
375         save_settings (self);
376         return FALSE;
377 }
378
379
380 GtkWidget*
381 modest_edit_msg_window_new (ModestWidgetFactory *factory,
382                             GtkUIManager *ui_manager,
383                             ModestEditType type)
384 {
385         GObject *obj;
386         ModestEditMsgWindowPrivate *priv;
387
388         g_return_val_if_fail (factory, NULL);
389         g_return_val_if_fail (type < MODEST_EDIT_TYPE_NUM, NULL);
390         
391         obj = g_object_new(MODEST_TYPE_EDIT_MSG_WINDOW, NULL);
392         priv = MODEST_EDIT_MSG_WINDOW_GET_PRIVATE(obj);
393
394         priv->widget_factory = g_object_ref (factory);
395         priv->ui_manager = g_object_ref (ui_manager);
396
397         /* Add accelerators */
398         gtk_window_add_accel_group (GTK_WINDOW (obj), 
399                                     gtk_ui_manager_get_accel_group (priv->ui_manager));
400
401
402         /* Toolbar / Menubar */
403         priv->toolbar = gtk_ui_manager_get_widget (priv->ui_manager, "/EditMsgWindowToolBar");
404         priv->menubar = gtk_ui_manager_get_widget (priv->ui_manager, "/EditMsgWindowMenuBar");
405
406         gtk_toolbar_set_tooltips (GTK_TOOLBAR (priv->toolbar), TRUE);
407
408         /* Init window */
409         init_window (MODEST_EDIT_MSG_WINDOW(obj));
410
411         restore_settings (MODEST_EDIT_MSG_WINDOW(obj));
412         
413         gtk_window_set_title (GTK_WINDOW(obj), "Modest");
414         gtk_window_set_icon  (GTK_WINDOW(obj),
415                               modest_icon_factory_get_icon (MODEST_APP_ICON));
416
417         g_signal_connect (G_OBJECT(obj), "delete-event",
418                           G_CALLBACK(on_delete_event), obj);
419
420         return GTK_WIDGET (obj);
421 }
422
423 void
424 modest_edit_msg_window_set_msg (ModestEditMsgWindow *self, TnyMsg *msg)
425 {
426         TnyHeader *header;
427         GtkTextBuffer *buf;
428         const gchar *to, *cc, *bcc, *subject;
429         ModestEditMsgWindowPrivate *priv;
430
431         g_return_if_fail (MODEST_IS_EDIT_MSG_WINDOW (self));
432         g_return_if_fail (TNY_IS_MSG (msg));
433
434         priv = MODEST_EDIT_MSG_WINDOW_GET_PRIVATE (self);
435
436         header = tny_msg_get_header (msg);
437         to      = tny_header_get_to (header);
438         cc      = tny_header_get_cc (header);
439         bcc     = tny_header_get_bcc (header);
440         subject = tny_header_get_subject (header);
441
442         if (to)
443                 gtk_entry_set_text (GTK_ENTRY(priv->to_field),  to);
444         if (cc)
445                 gtk_entry_set_text (GTK_ENTRY(priv->cc_field),  cc);
446         if (bcc)
447                 gtk_entry_set_text (GTK_ENTRY(priv->bcc_field), bcc);
448         if (subject)
449                 gtk_entry_set_text (GTK_ENTRY(priv->subject_field), subject);   
450         
451         buf = gtk_text_view_get_buffer (GTK_TEXT_VIEW(priv->msg_body));
452         gtk_text_buffer_set_text (buf,
453                                   (const gchar *) modest_tny_msg_actions_find_body (msg, TRUE),
454                                   -1);
455
456         /* TODO: lower priority, select in the From: combo to the
457            value that comes from msg <- not sure, should it be
458            allowed? */
459         
460         /* TODO: set attachments */
461 }