3e60a02b3853e67b211115457330d81329115f44
[modest] / src / widgets / modest-recpt-editor.c
1 /* Copyright (c) 2007, 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
30 #include <config.h>
31
32 #include <glib/gi18n-lib.h>
33
34 #include <gtk/gtkscrolledwindow.h>
35 #include <gtk/gtktextview.h>
36 #include <gtk/gtkimage.h>
37 #include <gtk/gtkbutton.h>
38
39 #include <modest-text-utils.h>
40 #include <modest-recpt-editor.h>
41 #include <modest-scroll-text.h>
42 #include <pango/pango-attributes.h>
43 #include <string.h>
44 #include <gdk/gdkkeysyms.h>
45 #include <gtk/gtk.h>
46
47 static GObjectClass *parent_class = NULL;
48
49 #define RECIPIENT_TAG_ID "recpt-id"
50
51 /* signals */
52 enum {
53         OPEN_ADDRESSBOOK_SIGNAL,
54         LAST_SIGNAL
55 };
56
57 typedef struct _ModestRecptEditorPrivate ModestRecptEditorPrivate;
58
59 struct _ModestRecptEditorPrivate
60 {
61         GtkWidget *text_view;
62         GtkWidget *abook_button;
63         GtkWidget *scrolled_window;
64         gchar *recipients;
65
66 };
67
68 #define MODEST_RECPT_EDITOR_GET_PRIVATE(o)      \
69         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_RECPT_EDITOR, ModestRecptEditorPrivate))
70
71 static guint signals[LAST_SIGNAL] = {0};
72
73 /* static functions: GObject */
74 static void modest_recpt_editor_instance_init (GTypeInstance *instance, gpointer g_class);
75 static void modest_recpt_editor_finalize (GObject *object);
76 static void modest_recpt_editor_class_init (ModestRecptEditorClass *klass);
77
78 /* widget events */
79 static void modest_recpt_editor_on_abook_clicked (GtkButton *button,
80                                                   ModestRecptEditor *editor);
81 static gboolean modest_recpt_editor_on_button_release_event (GtkWidget *widget,
82                                                              GdkEventButton *event,
83                                                              ModestRecptEditor *editor);
84 static void modest_recpt_editor_move_cursor_to_end (ModestRecptEditor *editor);
85 static void modest_recpt_editor_on_focus_in (GtkTextView *text_view,
86                                              GdkEventFocus *event,
87                                              ModestRecptEditor *editor);
88 static void modest_recpt_editor_on_insert_text (GtkTextBuffer *buffer,
89                                                 GtkTextIter *location,
90                                                 gchar *text,
91                                                 gint len,
92                                                 ModestRecptEditor *editor);
93 static gboolean modest_recpt_editor_on_key_press_event (GtkTextView *text_view,
94                                                           GdkEventKey *key,
95                                                           ModestRecptEditor *editor);
96 static GtkTextTag *iter_has_recipient (GtkTextIter *iter);
97 static gunichar iter_previous_char (GtkTextIter *iter);
98 /* static gunichar iter_next_char (GtkTextIter *iter); */
99 static GtkTextTag *prev_iter_has_recipient (GtkTextIter *iter);
100 /* static GtkTextTag *next_iter_has_recipient (GtkTextIter *iter); */
101 static void select_tag_of_iter (GtkTextIter *iter, GtkTextTag *tag);
102 static gboolean quote_opened (GtkTextIter *iter);
103
104 /**
105  * modest_recpt_editor_new:
106  *
107  * Return value: a new #ModestRecptEditor instance implemented for Gtk+
108  **/
109 GtkWidget*
110 modest_recpt_editor_new (void)
111 {
112         ModestRecptEditor *self = g_object_new (MODEST_TYPE_RECPT_EDITOR, 
113                                                 "homogeneous", FALSE,
114                                                 "spacing", 1,
115                                                 NULL);
116
117         return GTK_WIDGET (self);
118 }
119
120 void
121 modest_recpt_editor_set_recipients (ModestRecptEditor *recpt_editor, const gchar *recipients)
122 {
123         ModestRecptEditorPrivate *priv;
124         GtkTextBuffer *buffer = NULL;
125
126         g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
127         priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
128
129         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
130
131         g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
132         gtk_text_buffer_set_text (buffer, recipients, -1);
133         if (GTK_WIDGET_REALIZED (recpt_editor))
134                 gtk_widget_queue_resize (GTK_WIDGET (recpt_editor));
135         g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
136
137 }
138
139 void
140 modest_recpt_editor_add_recipients (ModestRecptEditor *recpt_editor, const gchar *recipients)
141 {
142         ModestRecptEditorPrivate *priv;
143         GtkTextBuffer *buffer = NULL;
144         GtkTextIter iter;
145         gchar * string_to_add;
146
147         g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
148         priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
149
150         if (recipients == NULL)
151                 return;
152
153         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
154
155         if (gtk_text_buffer_get_char_count (buffer) > 0) {
156                 string_to_add = g_strconcat (";\n", recipients, NULL);
157         } else {
158                 string_to_add = g_strdup (recipients);
159         }
160
161         gtk_text_buffer_get_end_iter (buffer, &iter);
162
163         g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
164
165         gtk_text_buffer_insert (buffer, &iter, recipients, -1);
166         
167         g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
168
169         if (GTK_WIDGET_REALIZED (recpt_editor))
170                 gtk_widget_queue_resize (GTK_WIDGET (recpt_editor));
171
172 }
173
174 void 
175 modest_recpt_editor_add_resolved_recipient (ModestRecptEditor *recpt_editor, GSList *email_list, const gchar * recipient_id)
176 {
177         ModestRecptEditorPrivate *priv;
178         GtkTextBuffer *buffer = NULL;
179         GtkTextIter start, end, iter;
180         GtkTextTag *tag = NULL;
181         GSList *node;
182         gboolean is_first_recipient = TRUE;
183       
184         g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
185         priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
186
187         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
188
189         g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
190         gtk_text_buffer_get_bounds (buffer, &start, &end);
191         if (gtk_text_buffer_get_char_count (buffer) > 0) {
192                 gchar * buffer_contents;
193
194                 gtk_text_buffer_get_bounds (buffer, &start, &end);
195                 buffer_contents = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
196                 if (!g_str_has_suffix (buffer_contents, "\n")) {
197                         if (g_str_has_suffix (buffer_contents, ";")||(g_str_has_suffix (buffer_contents, ",")))
198                                 gtk_text_buffer_insert (buffer, &end, "\n", -1);
199                         else 
200                                 gtk_text_buffer_insert (buffer, &end, ";\n", -1);
201                 }
202                 g_free (buffer_contents);
203         }
204
205         gtk_text_buffer_get_end_iter (buffer, &iter);
206
207         tag = gtk_text_buffer_create_tag (buffer, NULL, 
208                                           "underline", PANGO_UNDERLINE_SINGLE,
209                                           "wrap-mode", GTK_WRAP_NONE,
210                                           "editable", TRUE, NULL);
211
212         g_object_set_data (G_OBJECT (tag), "recipient-tag-id", GINT_TO_POINTER (RECIPIENT_TAG_ID));
213         g_object_set_data_full (G_OBJECT (tag), "recipient-id", g_strdup (recipient_id), (GDestroyNotify) g_free);
214
215         for (node = email_list; node != NULL; node = g_slist_next (node)) {
216                 gchar *recipient = (gchar *) node->data;
217
218                 if ((recipient) && (strlen (recipient) != 0)) {
219
220                         if (!is_first_recipient)
221                         gtk_text_buffer_insert (buffer, &iter, "\n", -1);
222
223                         gtk_text_buffer_insert_with_tags (buffer, &iter, recipient, -1, tag, NULL);
224                         gtk_text_buffer_insert (buffer, &iter, ";", -1);
225                         is_first_recipient = FALSE;
226                 }
227         }
228         g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
229
230         modest_recpt_editor_move_cursor_to_end (recpt_editor);
231
232 }
233
234 void 
235 modest_recpt_editor_replace_with_resolved_recipient (ModestRecptEditor *recpt_editor, 
236                                                      GtkTextIter *start, GtkTextIter *end,
237                                                      GSList *email_list, const gchar * recipient_id)
238 {
239         ModestRecptEditorPrivate *priv;
240         GtkTextBuffer *buffer;
241         GtkTextTag *tag;
242         GSList *node;
243         gboolean is_first_recipient = TRUE;
244
245         g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
246         priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
247
248         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
249         g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
250
251         gtk_text_buffer_delete (buffer, start, end);
252
253         tag = gtk_text_buffer_create_tag (buffer, NULL, 
254                                           "underline", PANGO_UNDERLINE_SINGLE,
255                                           "wrap-mode", GTK_WRAP_NONE,
256                                           "editable", TRUE, NULL);
257
258         g_object_set_data (G_OBJECT (tag), "recipient-tag-id", GINT_TO_POINTER (RECIPIENT_TAG_ID));
259         g_object_set_data_full (G_OBJECT (tag), "recipient-id", g_strdup (recipient_id), (GDestroyNotify) g_free);
260
261         for (node = email_list; node != NULL; node = g_slist_next (node)) {
262                 gchar *recipient = (gchar *) node->data;
263
264                 if ((recipient) && (strlen (recipient) != 0)) {
265
266                         if (!is_first_recipient)
267                         gtk_text_buffer_insert (buffer, start, "\n", -1);
268
269                         gtk_text_buffer_insert_with_tags (buffer, start, recipient, -1, tag, NULL);
270
271                         if (node->next != NULL)
272                                 gtk_text_buffer_insert (buffer, start, ";", -1);
273                         is_first_recipient = FALSE;
274                 }
275         }
276         g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
277
278 }
279
280
281 const gchar *
282 modest_recpt_editor_get_recipients (ModestRecptEditor *recpt_editor)
283 {
284         ModestRecptEditorPrivate *priv;
285         GtkTextBuffer *buffer = NULL;
286         GtkTextIter start, end;
287         gchar *c;
288
289         g_return_val_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor), NULL);
290         priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
291
292         if (priv->recipients != NULL) {
293                 g_free (priv->recipients);
294                 priv->recipients = NULL;
295         }
296
297         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
298
299         gtk_text_buffer_get_start_iter (buffer, &start);
300         gtk_text_buffer_get_end_iter (buffer, &end);
301
302         priv->recipients = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
303         for (c = priv->recipients; *c != '\0'; c = g_utf8_next_char (c)) {
304                 if (*c == '\n') {
305                         *c = ' ';
306                 }
307         }
308
309         return priv->recipients;
310
311 }
312
313 static void
314 modest_recpt_editor_instance_init (GTypeInstance *instance, gpointer g_class)
315 {
316         ModestRecptEditorPrivate *priv;
317         GtkWidget *abook_icon;
318         GtkTextBuffer *buffer;
319
320         priv = MODEST_RECPT_EDITOR_GET_PRIVATE (instance);
321
322         priv->abook_button = gtk_button_new ();
323         gtk_button_set_relief (GTK_BUTTON (priv->abook_button), GTK_RELIEF_NONE);
324         gtk_button_set_focus_on_click (GTK_BUTTON (priv->abook_button), FALSE);
325         GTK_WIDGET_UNSET_FLAGS (priv->abook_button, GTK_CAN_FOCUS);
326         gtk_button_set_alignment (GTK_BUTTON (priv->abook_button), 1.0, 1.0);
327         abook_icon = gtk_image_new_from_icon_name ("qgn_list_addressbook", GTK_ICON_SIZE_BUTTON);
328         gtk_container_add (GTK_CONTAINER (priv->abook_button), abook_icon);
329
330         priv->text_view = gtk_text_view_new ();
331         priv->recipients = NULL;
332
333         priv->scrolled_window = modest_scroll_text_new (GTK_TEXT_VIEW (priv->text_view), 5);
334         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window), GTK_POLICY_NEVER,
335                                         GTK_POLICY_AUTOMATIC);
336         gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window), GTK_SHADOW_IN);
337 /*      gtk_container_add (GTK_CONTAINER (priv->scrolled_window), priv->text_view); */
338
339         gtk_box_pack_start (GTK_BOX (instance), priv->scrolled_window, TRUE, TRUE, 0);
340 /*      gtk_box_pack_start (GTK_BOX (instance), priv->text_view, TRUE, TRUE, 0); */
341         gtk_box_pack_end (GTK_BOX (instance), priv->abook_button, FALSE, FALSE, 0);
342
343         gtk_text_view_set_accepts_tab (GTK_TEXT_VIEW (priv->text_view), FALSE);
344         gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (priv->text_view), TRUE);
345         gtk_text_view_set_editable (GTK_TEXT_VIEW (priv->text_view), TRUE);
346
347         gtk_text_view_set_justification (GTK_TEXT_VIEW (priv->text_view), GTK_JUSTIFY_LEFT);
348         gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (priv->text_view), GTK_WRAP_CHAR);
349
350         gtk_widget_set_size_request (priv->text_view, 75, -1);
351
352         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
353         g_signal_connect (G_OBJECT (priv->abook_button), "clicked", G_CALLBACK (modest_recpt_editor_on_abook_clicked), instance);
354         g_signal_connect (G_OBJECT (priv->text_view), "button-release-event", G_CALLBACK (modest_recpt_editor_on_button_release_event), instance);
355         g_signal_connect (G_OBJECT (priv->text_view), "key-press-event", G_CALLBACK (modest_recpt_editor_on_key_press_event), instance);
356         g_signal_connect (G_OBJECT (priv->text_view), "focus-in-event", G_CALLBACK (modest_recpt_editor_on_focus_in), instance);
357         g_signal_connect (G_OBJECT (buffer), "insert-text", G_CALLBACK (modest_recpt_editor_on_insert_text), instance);
358
359 /*      gtk_container_set_focus_child (GTK_CONTAINER (instance), priv->text_view); */
360
361         return;
362 }
363
364 void
365 modest_recpt_editor_set_field_size_group (ModestRecptEditor *recpt_editor, GtkSizeGroup *size_group)
366 {
367         ModestRecptEditorPrivate *priv;
368
369         g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
370         g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
371         priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
372
373         gtk_size_group_add_widget (size_group, priv->scrolled_window);
374 }
375
376 GtkTextBuffer *
377 modest_recpt_editor_get_buffer (ModestRecptEditor *recpt_editor)
378 {
379         ModestRecptEditorPrivate *priv;
380
381         g_return_val_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor), NULL);
382         priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
383
384         return gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
385 }
386
387 static void
388 modest_recpt_editor_on_abook_clicked (GtkButton *button, ModestRecptEditor *editor)
389 {
390         g_return_if_fail (MODEST_IS_RECPT_EDITOR (editor));
391
392         g_signal_emit_by_name (G_OBJECT (editor), "open-addressbook");
393 }
394
395 static gboolean
396 modest_recpt_editor_on_button_release_event (GtkWidget *widget,
397                                              GdkEventButton *event,
398                                              ModestRecptEditor *recpt_editor)
399 {
400         ModestRecptEditorPrivate *priv;
401         GtkTextIter location, start, end;
402         GtkTextMark *mark;
403         GtkTextBuffer *buffer;
404         GtkTextTag *tag;
405         gboolean selection_changed = FALSE;
406         
407         priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
408
409         buffer = modest_recpt_editor_get_buffer (recpt_editor);
410         mark = gtk_text_buffer_get_insert (buffer);
411         gtk_text_buffer_get_iter_at_mark (buffer, &location, mark);
412
413         gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
414
415         tag = iter_has_recipient (&start);
416         if (tag != NULL)
417                 if (!gtk_text_iter_begins_tag (&start, tag)) {
418                         gtk_text_iter_backward_to_tag_toggle (&start, tag);
419                         selection_changed = TRUE;
420                 } 
421
422         tag = iter_has_recipient (&end);
423         if (tag != NULL) 
424                 if (!gtk_text_iter_ends_tag (&end, tag)) {
425                         gtk_text_iter_forward_to_tag_toggle (&end, tag);
426                         selection_changed = TRUE;
427                 }
428
429         gtk_text_buffer_select_range (buffer, &start, &end);
430
431         return FALSE;
432 }
433
434 static void 
435 modest_recpt_editor_on_focus_in (GtkTextView *text_view,
436                                  GdkEventFocus *event,
437                                  ModestRecptEditor *editor)
438 {
439         ModestRecptEditorPrivate *priv = MODEST_RECPT_EDITOR_GET_PRIVATE (editor);
440         gtk_text_view_place_cursor_onscreen (GTK_TEXT_VIEW (priv->text_view));
441 }
442
443 static gboolean
444 is_valid_insert (const gchar *text, gint len)
445 {
446         gunichar c;
447         gint i= 0;
448         const gchar *current;
449         if (text == NULL)
450                 return TRUE;
451         current = text;
452
453         while (((len == -1)||(i < len)) && (*text != '\0')) {
454                 c = g_utf8_get_char (current);
455                 if (c == 0x2022 || c == 0xfffc)
456                         return FALSE;
457                 current = g_utf8_next_char (current);
458                 i = current - text;
459         }
460         return TRUE;
461 }
462
463 static gchar *
464 create_valid_text (const gchar *text, gint len)
465 {
466         gunichar c;
467         gint i= 0;
468         GString *str;
469         const gchar *current;
470
471         if (text == NULL)
472                 return NULL;
473
474         str = g_string_new ("");
475         current = text;
476
477         while (((len == -1)||(i < len)) && (*text != '\0')) {
478                 c = g_utf8_get_char (current);
479                 if (c != 0x2022 && c != 0xfffc)
480                         str = g_string_append_unichar (str, c);
481                 current = g_utf8_next_char (current);
482                 i = current - text;
483         }
484         return g_string_free (str, FALSE);
485 }
486
487 static void 
488 modest_recpt_editor_on_insert_text (GtkTextBuffer *buffer,
489                                     GtkTextIter *location,
490                                     gchar *text,
491                                     gint len,
492                                     ModestRecptEditor *editor)
493 {
494         GtkTextIter prev;
495         gunichar prev_char;
496         ModestRecptEditorPrivate *priv = MODEST_RECPT_EDITOR_GET_PRIVATE (editor);
497
498         if (!is_valid_insert (text, len)) {
499                 gchar *new_text = create_valid_text (text, len);
500                 g_signal_stop_emission_by_name (G_OBJECT (buffer), "insert-text");
501                 gtk_text_buffer_insert (buffer, location, new_text, -1);
502                 g_free (new_text);
503         }
504
505         if (iter_has_recipient (location)) {
506                 gtk_text_buffer_get_end_iter (buffer, location);
507                 gtk_text_buffer_place_cursor (buffer, location);
508         }
509
510         if (gtk_text_iter_is_start (location))
511                 return;
512
513         if (gtk_text_iter_is_end (location)) {
514                 prev = *location;
515                 if (!gtk_text_iter_backward_char (&prev))
516                         return;
517                 prev_char = gtk_text_iter_get_char (&prev);
518                 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
519                 if ((prev_char == ';'||prev_char == ',')&&(!quote_opened(location))) {
520                         GtkTextMark *insert;
521                         gtk_text_buffer_insert (buffer, location, "\n",-1);
522                         insert = gtk_text_buffer_get_insert (buffer);
523                         gtk_text_view_scroll_to_iter (GTK_TEXT_VIEW (priv->text_view), location, 0.0,TRUE, 0.0, 1.0);
524                 }
525                 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
526                 
527         }
528
529 }
530
531 static GtkTextTag *
532 iter_has_recipient (GtkTextIter *iter)
533 {
534         GSList *tags, *node;
535         GtkTextTag *result = NULL;
536
537         tags = gtk_text_iter_get_tags (iter);
538
539         for (node = tags; node != NULL; node = g_slist_next (node)) {
540                 GtkTextTag *tag = GTK_TEXT_TAG (node->data);
541
542                 if (g_object_get_data (G_OBJECT (tag), "recipient-tag-id")) {
543                         result = tag;
544                         break;
545                 }
546         }
547         g_slist_free (tags);
548         return result;
549 }
550
551 static GtkTextTag *
552 prev_iter_has_recipient (GtkTextIter *iter)
553 {
554         GtkTextIter prev;
555
556         prev = *iter;
557         gtk_text_iter_backward_char (&prev);
558         return iter_has_recipient (&prev);
559 }
560
561 /* static GtkTextTag * */
562 /* next_iter_has_recipient (GtkTextIter *iter) */
563 /* { */
564 /*      GtkTextIter next; */
565
566 /*      next = *iter; */
567 /*      return iter_has_recipient (&next); */
568 /* } */
569
570 static gunichar 
571 iter_previous_char (GtkTextIter *iter)
572 {
573         GtkTextIter prev;
574
575         prev = *iter;
576         gtk_text_iter_backward_char (&prev);
577         return gtk_text_iter_get_char (&prev);
578 }
579
580 /* static gunichar  */
581 /* iter_next_char (GtkTextIter *iter) */
582 /* { */
583 /*      GtkTextIter next; */
584
585 /*      next = *iter; */
586 /*      gtk_text_iter_forward_char (&next); */
587 /*      return gtk_text_iter_get_char (&next); */
588 /* } */
589
590 static void
591 select_tag_of_iter (GtkTextIter *iter, GtkTextTag *tag)
592 {
593         GtkTextIter start, end;
594
595         start = *iter;
596         if (!gtk_text_iter_begins_tag (&start, tag))
597                 gtk_text_iter_backward_to_tag_toggle (&start, tag);
598         end = *iter;
599         if (!gtk_text_iter_ends_tag (&end, tag))
600                 gtk_text_iter_forward_to_tag_toggle (&end, tag);
601         gtk_text_buffer_select_range (gtk_text_iter_get_buffer (iter), &start, &end);
602 }
603
604 static gboolean 
605 quote_opened (GtkTextIter *iter)
606 {
607         GtkTextIter start;
608         GtkTextBuffer *buffer;
609         gboolean opened = FALSE;
610
611         buffer = gtk_text_iter_get_buffer (iter);
612         gtk_text_buffer_get_start_iter (buffer, &start);
613
614         while (!gtk_text_iter_equal (&start, iter)) {
615                 gunichar current_char = gtk_text_iter_get_char (&start);
616                 if (current_char == '"')
617                         opened = !opened;
618                 else if (current_char == '\\')
619                         gtk_text_iter_forward_char (&start);
620                 if (!gtk_text_iter_equal (&start, iter))
621                         gtk_text_iter_forward_char (&start);
622                         
623         }
624         return opened;
625
626 }
627
628
629 static gboolean
630 modest_recpt_editor_on_key_press_event (GtkTextView *text_view,
631                                           GdkEventKey *key,
632                                           ModestRecptEditor *editor)
633 {
634         GtkTextMark *insert;
635         GtkTextBuffer * buffer;
636         GtkTextIter location;
637         GtkTextTag *tag;
638      
639         buffer = gtk_text_view_get_buffer (text_view);
640         insert = gtk_text_buffer_get_insert (buffer);
641
642         /* cases to cover:
643          *    * cursor is on resolved recipient:
644          *        - right should go to first character after the recipient (usually ; or ,)
645          *        - left should fo to the first character before the recipient
646          *        - return should run check names on the recipient.
647          *    * cursor is just after a recipient:
648          *        - right should go to the next character. If it's a recipient, should select
649          *          it
650          *        - left should go to the previous character. If it's a recipient, should go
651          *          to the first character of the recipient, and select it.
652          *    * cursor is on arbitrary text:
653          *        - return should add a ; and go to the next line
654          *        - left or right standard ones.
655          *    * cursor is after a \n:
656          *        - left should go to the character before the \n (as if \n was not a character)
657          *    * cursor is before a \n:
658          *        - right should go to the character after the \n
659          */
660
661         gtk_text_buffer_get_iter_at_mark (buffer, &location, insert);
662
663         switch (key->keyval) {
664         case GDK_Left:
665         case GDK_KP_Left: 
666         {
667                 gboolean cursor_ready = FALSE;
668                 while (!cursor_ready) {
669                         if (iter_previous_char (&location) == '\n') {
670                                 gtk_text_iter_backward_char (&location);
671                         } else {
672                                 cursor_ready = TRUE;
673                         }
674                 }
675                 tag = iter_has_recipient (&location);
676                 if ((tag != NULL)&& (gtk_text_iter_is_start (&location) || !(gtk_text_iter_begins_tag (&location, tag)))) {
677                         select_tag_of_iter (&location, tag);
678                         gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (text_view), insert, 0.0, FALSE, 0.0, 1.0);
679                         return TRUE;
680                 }
681                 gtk_text_iter_backward_char (&location);
682                 tag = iter_has_recipient (&location);
683                 if (tag != NULL)
684                         select_tag_of_iter (&location, tag);
685                 else {
686                         gtk_text_buffer_place_cursor (buffer, &location);
687                 }
688                 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (text_view), insert, 0.0, FALSE, 0.0, 1.0);
689
690                 return TRUE;
691         }
692         break;
693         case GDK_Right:
694         case GDK_KP_Right:
695         {
696                 gboolean cursor_moved = FALSE;
697
698                 tag = iter_has_recipient (&location);
699                 if ((tag != NULL)&&(!gtk_text_iter_ends_tag (&location, tag))) {
700                         gtk_text_iter_forward_to_tag_toggle (&location, tag);
701                         while (gtk_text_iter_get_char (&location) == '\n')
702                                 gtk_text_iter_forward_char (&location);
703                         gtk_text_buffer_place_cursor (buffer, &location);
704                         gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (text_view), insert, 0.0, FALSE, 0.0, 1.0);
705                         return TRUE;
706                 }
707
708                 while (gtk_text_iter_get_char (&location) == '\n') {
709                         gtk_text_iter_forward_char (&location);
710                         cursor_moved = TRUE;
711                 }
712                 if (!cursor_moved)
713                         gtk_text_iter_forward_char (&location);
714                 while (gtk_text_iter_get_char (&location) == '\n') {
715                         gtk_text_iter_forward_char (&location);
716                         cursor_moved = TRUE;
717                 }
718
719                 tag = iter_has_recipient (&location);
720                 if (tag != NULL)
721                         select_tag_of_iter (&location, tag);
722                 else
723                         gtk_text_buffer_place_cursor (buffer, &location);
724                 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (text_view), insert, 0.0, FALSE, 0.0, 1.0);
725                 return TRUE;
726         }
727         break;
728         case GDK_Return:
729         case GDK_KP_Enter:
730         {
731                 g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
732                 tag = iter_has_recipient (&location);
733                 if (tag != NULL) {
734                         gtk_text_buffer_get_end_iter (buffer, &location);
735                         gtk_text_buffer_place_cursor (buffer, &location);
736                         if ((iter_previous_char (&location) != ';')&&(iter_previous_char (&location) != ','))
737                                 gtk_text_buffer_insert_at_cursor (buffer, ";", -1);
738                         gtk_text_buffer_insert_at_cursor (buffer, "\n", -1);
739                 } else {
740                         gunichar prev_char = iter_previous_char (&location);
741                         if ((gtk_text_iter_is_start (&location))||(prev_char == '\n')
742                             ||(prev_char == ';')||(prev_char == ',')) 
743                                 g_signal_emit_by_name (G_OBJECT (editor), "open-addressbook");
744                         else {
745                                 if ((prev_char != ';') && (prev_char != ','))
746                                         gtk_text_buffer_insert_at_cursor (buffer, ";", -1);
747                                 gtk_text_buffer_insert_at_cursor (buffer, "\n", -1);
748                         }
749                 }
750                 g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
751                 return TRUE;
752         }
753         break;
754         case GDK_BackSpace:
755         {
756                 #if GTK_CHECK_VERSION(2, 10, 0) /* gtk_text_buffer_get_has_selection is only available in GTK+ 2.10 */
757                 if (gtk_text_buffer_get_has_selection (buffer)) {
758                         gtk_text_buffer_delete_selection (buffer, TRUE, TRUE);
759                         return TRUE;
760                 }
761                 #else
762                 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
763                         gtk_text_buffer_delete_selection (buffer, TRUE, TRUE);
764                         return TRUE;
765                 }
766                 #endif
767
768                 tag = prev_iter_has_recipient (&location);
769                 if (tag != NULL) {
770                         GtkTextIter iter_in_tag;
771                         iter_in_tag = location;
772                         gtk_text_iter_backward_char (&iter_in_tag);
773                         select_tag_of_iter (&iter_in_tag, tag);
774                         gtk_text_buffer_delete_selection (buffer, TRUE, TRUE);
775                         return TRUE;
776                 }
777                 return FALSE;
778         }
779         break;
780         default:
781                 return FALSE;
782         }
783 }
784
785 static void 
786 modest_recpt_editor_move_cursor_to_end (ModestRecptEditor *editor)
787 {
788         ModestRecptEditorPrivate *priv = MODEST_RECPT_EDITOR_GET_PRIVATE (editor);
789         GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
790         GtkTextIter start, end;
791
792         gtk_text_buffer_get_end_iter (buffer, &start);
793         end = start;
794         gtk_text_buffer_select_range (buffer, &start, &end);
795
796
797 }
798
799 void
800 modest_recpt_editor_grab_focus (ModestRecptEditor *recpt_editor)
801 {
802         ModestRecptEditorPrivate *priv;
803         
804         g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
805         priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
806
807         gtk_widget_grab_focus (priv->text_view);
808 }
809
810 static void
811 modest_recpt_editor_finalize (GObject *object)
812 {
813         ModestRecptEditorPrivate *priv;
814         priv = MODEST_RECPT_EDITOR_GET_PRIVATE (object);
815
816         if (priv->recipients) {
817                 g_free (priv->recipients);
818                 priv->recipients = NULL;
819         }
820
821         (*parent_class->finalize) (object);
822
823         return;
824 }
825
826 static void 
827 modest_recpt_editor_class_init (ModestRecptEditorClass *klass)
828 {
829         GObjectClass *object_class;
830         GtkWidgetClass *widget_class;
831
832         parent_class = g_type_class_peek_parent (klass);
833         object_class = (GObjectClass*) klass;
834         widget_class = GTK_WIDGET_CLASS (klass);
835
836         object_class->finalize = modest_recpt_editor_finalize;
837
838         g_type_class_add_private (object_class, sizeof (ModestRecptEditorPrivate));
839
840         signals[OPEN_ADDRESSBOOK_SIGNAL] = 
841                 g_signal_new ("open-addressbook",
842                               G_TYPE_FROM_CLASS (object_class),
843                               G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
844                               G_STRUCT_OFFSET (ModestRecptEditorClass, open_addressbook),
845                               NULL, NULL,
846                               g_cclosure_marshal_VOID__VOID,
847                               G_TYPE_NONE, 0);
848
849         return;
850 }
851
852 GType 
853 modest_recpt_editor_get_type (void)
854 {
855         static GType type = 0;
856
857         if (G_UNLIKELY(type == 0))
858         {
859                 static const GTypeInfo info = 
860                 {
861                   sizeof (ModestRecptEditorClass),
862                   NULL,   /* base_init */
863                   NULL,   /* base_finalize */
864                   (GClassInitFunc) modest_recpt_editor_class_init,   /* class_init */
865                   NULL,   /* class_finalize */
866                   NULL,   /* class_data */
867                   sizeof (ModestRecptEditor),
868                   0,      /* n_preallocs */
869                   modest_recpt_editor_instance_init    /* instance_init */
870                 };
871
872                 type = g_type_register_static (GTK_TYPE_HBOX,
873                         "ModestRecptEditor",
874                         &info, 0);
875
876         }
877
878         return type;
879 }