Do ignore empty recipients when checking names
[modest] / src / hildon2 / modest-signature-editor-dialog.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
30 #include "modest-signature-editor-dialog.h"
31 #include "widgets/modest-ui-constants.h"
32 #include "modest-hildon-includes.h"
33 #include "widgets/modest-validating-entry.h"
34 #include "modest-runtime.h"
35 #include <modest-account-mgr-helpers.h>
36 #include <gtk/gtkhbox.h>
37 #include <gtk/gtkvbox.h>
38 #include <gtk/gtktextview.h>
39 #include <gtk/gtklabel.h>
40 #include <hildon/hildon-pannable-area.h>
41 #include <gtk/gtkstock.h>
42 #include <glib/gi18n.h>
43 #include <modest-maemo-utils.h>
44 #include "modest-text-utils.h"
45 #include <hildon/hildon-text-view.h>
46
47 G_DEFINE_TYPE (ModestSignatureEditorDialog, modest_signature_editor_dialog, GTK_TYPE_DIALOG);
48
49 #define SIGNATURE_EDITOR_DIALOG_GET_PRIVATE(o) \
50         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_SIGNATURE_EDITOR_DIALOG, ModestSignatureEditorDialogPrivate))
51
52 typedef struct _ModestSignatureEditorDialogPrivate ModestSignatureEditorDialogPrivate;
53
54 struct _ModestSignatureEditorDialogPrivate
55 {
56         GtkWidget *checkbox_use;
57         GtkWidget *label;
58         GtkWidget *pannable;
59         GtkWidget *textview;
60
61         guint correct_scroll_idle;
62 };
63
64 static void text_buffer_end_user_action (GtkTextBuffer *buffer,
65                                          ModestSignatureEditorDialog *userdata);
66
67 static void
68 modest_signature_editor_dialog_get_property (GObject *object, guint property_id,
69                                                                                                                         GValue *value, GParamSpec *pspec)
70 {
71         switch (property_id) {
72         default:
73                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
74         }
75 }
76
77 static void
78 modest_signature_editor_dialog_set_property (GObject *object, guint property_id,
79                                                                                                                         const GValue *value, GParamSpec *pspec)
80 {
81         switch (property_id) {
82         default:
83                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
84         }
85 }
86
87 static void
88 modest_signature_editor_dialog_dispose (GObject *object)
89 {
90         if (G_OBJECT_CLASS (modest_signature_editor_dialog_parent_class)->dispose)
91                 G_OBJECT_CLASS (modest_signature_editor_dialog_parent_class)->dispose (object);
92 }
93
94 static void
95 modest_signature_editor_dialog_finalize (GObject *object)
96 {
97         ModestSignatureEditorDialogPrivate *priv;
98
99         priv = SIGNATURE_EDITOR_DIALOG_GET_PRIVATE (object);
100
101         if (priv->correct_scroll_idle > 0) {
102                 g_source_remove (priv->correct_scroll_idle);
103                 priv->correct_scroll_idle = 0;
104         }
105         G_OBJECT_CLASS (modest_signature_editor_dialog_parent_class)->finalize (object);
106 }
107
108 static void
109 modest_signature_editor_dialog_class_init (ModestSignatureEditorDialogClass *klass)
110 {
111         GObjectClass *object_class = G_OBJECT_CLASS (klass);
112
113         g_type_class_add_private (klass, sizeof (ModestSignatureEditorDialogPrivate));
114
115         object_class->get_property = modest_signature_editor_dialog_get_property;
116         object_class->set_property = modest_signature_editor_dialog_set_property;
117         object_class->dispose = modest_signature_editor_dialog_dispose;
118         object_class->finalize = modest_signature_editor_dialog_finalize;
119 }
120
121 static void
122 enable_widgets (ModestSignatureEditorDialog *self)
123 {
124         ModestSignatureEditorDialogPrivate *priv = 
125                 SIGNATURE_EDITOR_DIALOG_GET_PRIVATE (self);
126                 
127         const gboolean enable = hildon_check_button_get_active (HILDON_CHECK_BUTTON (priv->checkbox_use));
128         gtk_widget_set_sensitive (priv->label, enable);
129         gtk_widget_set_sensitive (priv->textview, enable);
130         gtk_text_view_set_editable (GTK_TEXT_VIEW (priv->textview), enable);
131 }
132
133 static void
134 on_toggle_button_changed (GtkToggleButton *togglebutton, gpointer user_data)
135 {
136         ModestSignatureEditorDialog *self = MODEST_SIGNATURE_EDITOR_DIALOG (user_data);
137         enable_widgets (self);
138 }
139
140 static void
141 modest_signature_editor_dialog_init (ModestSignatureEditorDialog *self)
142 {
143         ModestSignatureEditorDialogPrivate *priv = 
144                 SIGNATURE_EDITOR_DIALOG_GET_PRIVATE (self);
145         GtkWidget *top_box, *align;
146         
147         gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_email_signatures_edit_title"));
148                 
149         GtkWidget *box = GTK_DIALOG(self)->vbox; /* gtk_vbox_new (FALSE, MODEST_MARGIN_HALF); */
150         top_box = gtk_vbox_new (FALSE, 0);
151
152         priv->checkbox_use = hildon_check_button_new (HILDON_SIZE_FINGER_HEIGHT);
153         gtk_button_set_label (GTK_BUTTON (priv->checkbox_use), 
154                               _("mcen_fi_email_signatures_use_signature"));
155         gtk_button_set_alignment (GTK_BUTTON (priv->checkbox_use), 0.0, 0.5);
156         gtk_box_pack_start (GTK_BOX (top_box), priv->checkbox_use, FALSE, FALSE, 0);
157         gtk_widget_show (priv->checkbox_use);
158         
159         g_signal_connect (G_OBJECT (priv->checkbox_use), "toggled",
160                           G_CALLBACK (on_toggle_button_changed), self);         
161         
162         priv->label = gtk_label_new (""); /* Set in modest_signature_editor_dialog_set_settings(). */
163         gtk_misc_set_alignment (GTK_MISC (priv->label), 0.0, 0.0);
164         gtk_misc_set_padding (GTK_MISC (priv->label), MODEST_MARGIN_DOUBLE, MODEST_MARGIN_DOUBLE);
165         gtk_box_pack_start (GTK_BOX (top_box), priv->label, FALSE, FALSE, 0);
166         gtk_widget_show (priv->label);
167         
168         priv->textview = hildon_text_view_new ();
169         gtk_widget_show (priv->textview);
170         GtkTextBuffer *buffer = hildon_text_view_get_buffer (HILDON_TEXT_VIEW (priv->textview));
171         gtk_text_buffer_set_text (buffer, _("mcen_va_default_signature_tablet"), -1); /* Default, as per the UI spec. */
172         gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (priv->textview), GTK_WRAP_WORD_CHAR);
173         gtk_box_pack_start (GTK_BOX (top_box), priv->textview, TRUE, TRUE, 0);
174
175         g_signal_connect (G_OBJECT (buffer), "end-user-action",
176                           G_CALLBACK (text_buffer_end_user_action), self);
177         
178         /* Add the buttons: */
179         gtk_dialog_add_button (GTK_DIALOG (self), _HL("wdgt_bd_save"), GTK_RESPONSE_OK);
180
181         align = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
182         gtk_alignment_set_padding (GTK_ALIGNMENT (align), 0, 0, MODEST_MARGIN_DOUBLE, 0);
183         gtk_widget_show (align);
184         gtk_container_add (GTK_CONTAINER (align), top_box);
185         
186         gtk_widget_show (top_box);
187
188         priv->pannable = hildon_pannable_area_new ();
189         hildon_pannable_area_add_with_viewport (HILDON_PANNABLE_AREA (priv->pannable), align);
190         gtk_container_add (GTK_CONTAINER (GTK_DIALOG (self)->vbox), priv->pannable);
191         gtk_widget_show (priv->pannable);               
192
193         gtk_widget_show (box);
194         gtk_widget_set_size_request (GTK_WIDGET (self), -1, MODEST_DIALOG_WINDOW_MAX_HEIGHT);
195         
196         /* When this window is shown, hibernation should not be possible, 
197          * because there is no sensible way to save the state: */
198         modest_window_mgr_prevent_hibernation_while_window_is_shown (
199                 modest_runtime_get_window_mgr (), GTK_WINDOW (self)); 
200
201         priv->correct_scroll_idle = 0;
202         
203 }
204
205 ModestSignatureEditorDialog*
206 modest_signature_editor_dialog_new (void)
207 {
208         return g_object_new (MODEST_TYPE_SIGNATURE_EDITOR_DIALOG, NULL);
209 }
210
211 void
212 modest_signature_editor_dialog_set_settings (
213         ModestSignatureEditorDialog *window, gboolean use_signature, const gchar* signature, 
214         const gchar* account_title)
215 {
216         ModestSignatureEditorDialogPrivate *priv = 
217                 SIGNATURE_EDITOR_DIALOG_GET_PRIVATE (window);
218
219         /* This causes a warning because of the %s in the translation, but not in the original string: */
220         gchar* label_text = g_strdup_printf (_("mcen_ia_email_signatures_edit_dlg_label"), 
221                 account_title);
222         gtk_label_set_text (GTK_LABEL (priv->label), label_text);
223         gtk_label_set_ellipsize (GTK_LABEL (priv->label),  PANGO_ELLIPSIZE_END);
224         g_free (label_text);
225         
226         hildon_check_button_set_active (HILDON_CHECK_BUTTON (priv->checkbox_use), use_signature);
227         
228         GtkTextBuffer *buffer = hildon_text_view_get_buffer (HILDON_TEXT_VIEW (priv->textview));
229         if (signature && signature[0] != '\0')
230                 gtk_text_buffer_set_text (buffer, signature, -1);
231         else
232                 gtk_text_buffer_set_text (buffer, _("mcen_va_default_signature_tablet"), -1); /* Default, as per the UI spec. */
233                 
234         enable_widgets (window);
235 }
236
237 /*
238  * The result must be freed with g_free(). */
239 gchar*
240 modest_signature_editor_dialog_get_settings (
241         ModestSignatureEditorDialog *window, gboolean* use_signature)
242 {
243         ModestSignatureEditorDialogPrivate *priv = 
244                 SIGNATURE_EDITOR_DIALOG_GET_PRIVATE (window);
245                 
246         g_assert(use_signature);
247         
248         *use_signature = hildon_check_button_get_active (HILDON_CHECK_BUTTON (priv->checkbox_use));
249                         
250         GtkTextBuffer *buffer = hildon_text_view_get_buffer (HILDON_TEXT_VIEW (priv->textview));
251         
252         GtkTextIter start, end;
253         gtk_text_buffer_get_bounds (buffer, &start, &end);
254         return gtk_text_buffer_get_text (buffer, &start, &end, TRUE);
255 }
256
257 static gboolean 
258 correct_scroll_idle_func (gpointer userdata)
259 {
260         ModestSignatureEditorDialog *self = (ModestSignatureEditorDialog *) userdata;
261         ModestSignatureEditorDialogPrivate *priv;
262         GtkTextBuffer *buffer;
263         GtkTextIter iter;
264         GdkRectangle rectangle;
265         gint offset_min, offset_max;
266         GtkTextMark *insert;
267         GtkAdjustment *vadj;
268
269         /* It could happen that the window was already closed */
270         if (!GTK_WIDGET_VISIBLE (self))
271                 return FALSE;
272
273         priv = SIGNATURE_EDITOR_DIALOG_GET_PRIVATE(self);
274         buffer = hildon_text_view_get_buffer (HILDON_TEXT_VIEW (priv->textview));
275
276         insert = gtk_text_buffer_get_insert (buffer);
277         gtk_text_buffer_get_iter_at_mark (buffer, &iter, insert);
278
279         gtk_text_view_get_iter_location (GTK_TEXT_VIEW (priv->textview), &iter, &rectangle);
280         offset_min = priv->textview->allocation.y + rectangle.y;
281         offset_max = offset_min + rectangle.height;
282
283         vadj = hildon_pannable_area_get_vadjustment (HILDON_PANNABLE_AREA (priv->pannable));
284         offset_min = MAX (offset_min - 48, 0);
285         offset_max = MIN (offset_max + 48, vadj->upper);
286
287         if ((offset_min < vadj->value) || (offset_max > vadj->value + vadj->page_size)) {
288                 /* We check if the current center of the visible area is already matching the center
289                    of the selection */
290                 gint offset_center;
291                 gint center_top, center_bottom;
292
293                 offset_center = (offset_min + offset_max) / 2;
294                 center_top = vadj->value + vadj->page_size / 3;
295                 center_bottom = vadj->value + vadj->page_size * 2 / 3;
296
297                 if ((offset_center < center_top) ||
298                     (offset_center > center_bottom))
299                         hildon_pannable_area_scroll_to (HILDON_PANNABLE_AREA (priv->pannable), -1, offset_center);
300         }
301
302         priv->correct_scroll_idle = 0;
303         return FALSE;
304 }
305
306 static void correct_scroll (ModestSignatureEditorDialog *self)
307 {
308         ModestSignatureEditorDialogPrivate *priv;
309
310         priv = SIGNATURE_EDITOR_DIALOG_GET_PRIVATE(self);
311
312         if (!gtk_widget_is_focus (priv->textview))
313                 return;
314
315         if (priv->correct_scroll_idle > 0) {
316                 return;
317         }
318
319         priv->correct_scroll_idle = g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
320                                                      (GSourceFunc) correct_scroll_idle_func,
321                                                      g_object_ref (self),
322                                                      g_object_unref);
323 }
324
325 static void
326 text_buffer_end_user_action (GtkTextBuffer *buffer,
327                              ModestSignatureEditorDialog *userdata)
328 {
329
330         correct_scroll (userdata);
331 }
332