If no connection is available and bs embedded image is not fetched, show
[modest] / src / widgets / modest-validating-entry.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 "modest-validating-entry.h"
31 #include <modest-ui-constants.h>
32 #include <gtk/gtksignal.h> /* For the gtk_signal_stop_emit_by_name() convenience function. */
33 #include <string.h> /* For strlen(). */
34
35 /* Include config.h so that _() works: */
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40
41 #ifdef MODEST_TOOLKIT_HILDON2
42 G_DEFINE_TYPE (ModestValidatingEntry, modest_validating_entry, HILDON_TYPE_ENTRY);
43 #else
44 G_DEFINE_TYPE (ModestValidatingEntry, modest_validating_entry, GTK_TYPE_ENTRY);
45 #endif
46
47 #define VALIDATING_ENTRY_GET_PRIVATE(o) \
48         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_VALIDATING_ENTRY, ModestValidatingEntryPrivate))
49
50 typedef struct _ModestValidatingEntryPrivate ModestValidatingEntryPrivate;
51
52 struct _ModestValidatingEntryPrivate
53 {
54         /* A list of gunichar, rather than char*,
55          * because gunichar is easier to deal with internally,
56          * but gchar* is easier to supply from the external interface.
57          */
58         GList *list_prevent;
59         
60         gboolean prevent_whitespace;
61         
62         EasySetupValidatingEntryFunc func;
63         gpointer func_user_data;
64
65         EasySetupValidatingEntryMaxFunc max_func;
66         gpointer max_func_user_data;
67 };
68
69 static void
70 modest_validating_entry_get_property (GObject *object, guint property_id,
71                                                                                                                         GValue *value, GParamSpec *pspec)
72 {
73         switch (property_id) {
74         default:
75                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
76         }
77 }
78
79 static void
80 modest_validating_entry_set_property (GObject *object, guint property_id,
81                                                                                                                         const GValue *value, GParamSpec *pspec)
82 {
83         switch (property_id) {
84         default:
85                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
86         }
87 }
88
89 static void
90 modest_validating_entry_dispose (GObject *object)
91 {
92         if (G_OBJECT_CLASS (modest_validating_entry_parent_class)->dispose)
93                 G_OBJECT_CLASS (modest_validating_entry_parent_class)->dispose (object);
94 }
95
96 static void
97 modest_validating_entry_finalize (GObject *object)
98 {
99         ModestValidatingEntryPrivate *priv = VALIDATING_ENTRY_GET_PRIVATE (object);
100         
101         /* Free the list and its items: */
102         if (priv->list_prevent) {
103                 g_list_foreach (priv->list_prevent, (GFunc)&g_free, NULL);
104                 g_list_free (priv->list_prevent);
105         }
106         
107         G_OBJECT_CLASS (modest_validating_entry_parent_class)->finalize (object);
108 }
109
110 static void
111 modest_validating_entry_class_init (ModestValidatingEntryClass *klass)
112 {
113         GObjectClass *object_class = G_OBJECT_CLASS (klass);
114
115         g_type_class_add_private (klass, sizeof (ModestValidatingEntryPrivate));
116
117         object_class->get_property = modest_validating_entry_get_property;
118         object_class->set_property = modest_validating_entry_set_property;
119         object_class->dispose = modest_validating_entry_dispose;
120         object_class->finalize = modest_validating_entry_finalize;
121 }
122
123 static gint
124 on_list_compare(gconstpointer a, gconstpointer b)
125 {
126         gunichar* unichar_a = (gunichar*)(a);
127         gunichar* unichar_b = (gunichar*)(b);
128         if(*unichar_a == *unichar_b)
129                 return 0;
130         else
131                 return -1; /* Really, we should return > and <, but we don't use this for sorting. */
132 }
133                                              
134 static void 
135 on_insert_text(GtkEditable *editable,
136         gchar *new_text, gint new_text_length, 
137         gint *position,
138     gpointer user_data)
139 {
140         ModestValidatingEntry *self = MODEST_VALIDATING_ENTRY (user_data);
141         ModestValidatingEntryPrivate *priv = VALIDATING_ENTRY_GET_PRIVATE (self);
142         
143         if(!new_text_length)
144                 return;
145                 
146         /* Note: new_text_length is documented as the number of bytes, not characters. */
147         if(!g_utf8_validate (new_text, new_text_length, NULL))
148                 return;
149         
150         /* Look at each UTF-8 character in the text (it could be several via a drop or a paste),
151          * and check them */
152         gboolean allow = TRUE;
153         gchar *iter = new_text; /* new_text seems to be NULL-terminated, though that is not documented. */
154         while (iter)
155         {
156                 if(priv->list_prevent) {
157                         /* If the character is in our prevent list, 
158                          * then do not allow this text to be entered.
159                          * 
160                          * This prevents entry of all text, without removing the unwanted characters.
161                          * It is debatable whether that is the best thing to do.
162                          */
163                         gunichar one_char = g_utf8_get_char (iter);
164                         GList *found = g_list_find_custom(priv->list_prevent, &one_char, &on_list_compare);
165                         if(found) {
166                                 allow = FALSE;
167                                 if (priv->func)
168                                 {
169                                         priv->func(self, iter, priv->func_user_data);
170                                 }
171                                 break;
172                         }       
173                 }
174                 
175                 if(priv->prevent_whitespace) {
176                         /* Check for whitespace characters: */
177                         gunichar one_char = g_utf8_get_char (iter);
178                         if (g_unichar_isspace (one_char)) {
179                                 allow = FALSE;
180                                 if (priv->func)
181                                 {
182                                         priv->func(self, NULL, priv->func_user_data);
183                                 }
184                                 break;
185                         }
186                 }
187
188                 /* Crashes. Don't know why: iter = g_utf8_next_char (iter); 
189                  * Maybe it doesn't check for null-termination. */      
190                 iter = g_utf8_find_next_char (iter, new_text + new_text_length);
191         }
192         
193         /* Prevent more than the max characters.
194          * The regular GtkEntry does this already, but we also want to call a specified callback,
195          * so that the application can show a warning dialog. */
196         if(priv->max_func) {
197                 const gint max_num = gtk_entry_get_max_length (GTK_ENTRY (self));
198                 if (max_num > 0) {
199                         const gchar *existing_text = gtk_entry_get_text (GTK_ENTRY(self));
200                         const gint existing_length = existing_text ? g_utf8_strlen (existing_text, -1) : 0;
201                         const gint new_length_chars = g_utf8_strlen (new_text, new_text_length);
202                         
203                         if ((existing_length + new_length_chars) > max_num) {
204                                 priv->max_func (self, priv->max_func_user_data);
205                                 /* We shouldn't need to stop the signal because the underlying code will check too.
206                                 * Well, that would maybe be a performance optimization, 
207                                  * but it's generally safer not to interfere too much. */       
208                         }
209                 }
210         }
211         
212         if(!allow) {
213                 /* The signal documentation says 
214                  * "by connecting to this signal and then stopping the signal with 
215                  * gtk_signal_emit_stop(), it is possible to modify the inserted text, 
216                  * or prevent it from being inserted entirely."
217                  */
218                  gtk_signal_emit_stop_by_name (GTK_OBJECT (self), "insert-text");
219         }
220
221
222                                             
223 static void
224 modest_validating_entry_init (ModestValidatingEntry *self)
225 {
226         /* Connect to the GtkEditable::insert-text signal 
227          * so we can filter out some characters:
228          * We connect _before_ so we can stop the default signal handler from running.
229          */
230         g_signal_connect (G_OBJECT (self), "insert-text", (GCallback)&on_insert_text, self);
231 }
232
233 ModestValidatingEntry*
234 modest_validating_entry_new (void)
235 {
236         ModestValidatingEntry *entry;
237         
238         entry = g_object_new (MODEST_TYPE_VALIDATING_ENTRY, NULL);
239
240 #ifdef MODEST_TOOLKIT_HILDON2
241         hildon_gtk_widget_set_theme_size (GTK_WIDGET (entry), MODEST_EDITABLE_SIZE);
242 #endif
243
244         return entry;
245 }
246
247 /** Specify characters that may not be entered into this GtkEntry.
248  *  
249  * list: A list of gchar* strings. Each one identifies a UTF-8 character.
250  */
251 void modest_validating_entry_set_unallowed_characters (ModestValidatingEntry *self, GList *list)
252 {
253         ModestValidatingEntryPrivate *priv = VALIDATING_ENTRY_GET_PRIVATE (self);
254             
255         /* Free the list and its items: */      
256         if (priv->list_prevent) {
257                 g_list_foreach (priv->list_prevent, (GFunc)&g_free, NULL);
258                 g_list_free (priv->list_prevent);
259         }
260      
261     /* Do a deep copy of the list, converting gchar* to gunichar: */
262     priv->list_prevent = NULL;
263     GList *iter = NULL;               
264     for (iter = list; iter != NULL; iter = iter->next) {
265         gunichar *one_char = g_new0 (gunichar, 1);
266         if(iter->data)
267                 *one_char = g_utf8_get_char ((gchar*)iter->data);
268         else
269                 *one_char = 0;
270                 
271         priv->list_prevent = g_list_append (priv->list_prevent, one_char);      
272     }
273 }
274
275 /** Specify that no whitespace characters may be entered into this GtkEntry.
276  *  
277  */
278 void modest_validating_entry_set_unallowed_characters_whitespace (ModestValidatingEntry *self)
279 {
280         ModestValidatingEntryPrivate *priv = VALIDATING_ENTRY_GET_PRIVATE (self);
281         priv->prevent_whitespace = TRUE;
282 }
283
284 /** Set a callback to be called when the maximum number of characters have been entered.
285  * This may be used to show an informative dialog.
286  */
287 void modest_validating_entry_set_max_func (ModestValidatingEntry *self, EasySetupValidatingEntryMaxFunc func, gpointer user_data)
288 {
289         ModestValidatingEntryPrivate *priv = VALIDATING_ENTRY_GET_PRIVATE (self);
290         priv->max_func = func;
291         priv->max_func_user_data = user_data;
292 }
293
294 /** Set a callback to be called when a character was prevented so that a
295  * note can be shown by the application to inform the user. For whitespaces,
296  * character will be NULL
297  */
298 void modest_validating_entry_set_func (ModestValidatingEntry *self, EasySetupValidatingEntryFunc func, gpointer user_data)
299 {
300         ModestValidatingEntryPrivate *priv = VALIDATING_ENTRY_GET_PRIVATE (self);
301         priv->func = func;
302         priv->func_user_data = user_data;
303 }
304