* validate the domain names
[modest] / src / modest-text-utils.c
index 6122f69..f01e706 100644 (file)
@@ -79,7 +79,7 @@ struct _url_match_t {
        { "(file|rtsp|http|ftp|https)://[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]+[-A-Za-z0-9_$%&=?/~#]",\
          NULL, NULL },\
        { "www\\.[-a-z0-9.]+[-a-z0-9](:[0-9]*)?(/[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]*[^]}\\),?!;:\"]?)?",\
-         NULL, "http://" },\
+                       NULL, "http://" },                              \
        { "ftp\\.[-a-z0-9.]+[-a-z0-9](:[0-9]*)?(/[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]*[^]}\\),?!;:\"]?)?",\
          NULL, "ftp://" },\
        { "(voipto|callto|chatto|jabberto|xmpp):[-_a-z@0-9.\\+]+", \
@@ -964,7 +964,26 @@ modest_text_utils_get_display_address (gchar *address)
        return address;
 }
 
+gchar *
+modest_text_utils_get_email_address (const gchar *full_address)
+{
+       const gchar *left, *right;
+       
+       if (!full_address)
+               return NULL;
+       
+       g_return_val_if_fail (g_utf8_validate (full_address, -1, NULL), NULL);
+       
+       left = g_strrstr_len (full_address, strlen(full_address), "<");
+       if (left == NULL)
+               return g_strdup (full_address);
 
+       right = g_strstr_len (left, strlen(left), ">");
+       if (right == NULL)
+               return g_strdup (full_address);
+
+       return g_strndup (left + 1, right - left - 1);
+}
 
 gint 
 modest_text_utils_get_subject_prefix_len (const gchar *sub)
@@ -1023,23 +1042,59 @@ gchar*
 modest_text_utils_get_display_date (time_t date)
 {
        time_t now;
-       const guint BUF_SIZE = 64; 
+       static const guint BUF_SIZE = 64; 
+       static const guint ONE_DAY = 24 * 60 * 60; /* seconds in one day */
        gchar date_buf[BUF_SIZE];  
-       gchar now_buf [BUF_SIZE];  
-       
+       gchar today_buf [BUF_SIZE];  
+
+       modest_text_utils_strftime (date_buf, BUF_SIZE, "%x", date); 
+
        now = time (NULL);
 
-       /* use the localized dates */
-       modest_text_utils_strftime (date_buf, BUF_SIZE, "%x", date); 
-       modest_text_utils_strftime (now_buf,  BUF_SIZE, "%x", now); 
-       
-       /* if this is today, get the time instead of the date */
-       if (strcmp (date_buf, now_buf) == 0)
-               modest_text_utils_strftime (date_buf, BUF_SIZE, "%X", date);
+       /* we check if the date is within the last 24h, if not, we don't
+        * have to do the extra, expensive strftime, which was very visible
+        * in the profiles.
+        */
+       if (abs(now - date) < ONE_DAY) {
+               
+               /* it's within the last 24 hours, but double check */
+               /* use the localized dates */
+               modest_text_utils_strftime (today_buf,  BUF_SIZE, "%x", now); 
+
+               /* if it's today, use the time instead */
+               if (strcmp (date_buf, today_buf) == 0)
+                       modest_text_utils_strftime (date_buf, BUF_SIZE, "%X", date);
+       }
        
        return g_strdup(date_buf);
 }
 
+
+gboolean
+modest_text_utils_validate_domain_name (const gchar *domain)
+{
+       gboolean valid = FALSE;
+       regex_t rx;
+       const gchar* domain_regex = "^[a-z0-9]([.]?[a-z0-9-])*[a-z0-9]$";
+
+       if (!domain)
+               return FALSE;
+       
+       /* domain name: all alphanum or '-' or '.',
+        * but beginning/ending in alphanum */  
+       if (regcomp (&rx, domain_regex, REG_ICASE|REG_EXTENDED|REG_NOSUB)) {
+               g_warning ("BUG: error in regexp");
+               return FALSE;
+       }
+       
+       valid = (regexec (&rx, domain, 1, NULL, 0) == 0);
+       regfree (&rx);
+               
+       return valid;
+}
+
+
+
 gboolean
 modest_text_utils_validate_email_address (const gchar *email_address, const gchar **invalid_char_position)
 {
@@ -1229,3 +1284,38 @@ modest_text_utils_get_color_string (GdkColor *color)
                                (color->blue >> 12)  & 0xf, (color->blue >> 8)  & 0xf,
                                (color->blue >>  4)  & 0xf, (color->blue)       & 0xf);
 }
+
+gchar *
+modest_text_utils_text_buffer_get_text (GtkTextBuffer *buffer)
+{
+       GtkTextIter start, end;
+       gchar *slice, *current;
+       GString *result = g_string_new ("");
+
+       g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
+
+       gtk_text_buffer_get_start_iter (buffer, &start);
+       gtk_text_buffer_get_end_iter (buffer, &end);
+
+       slice = gtk_text_buffer_get_slice (buffer, &start, &end, FALSE);
+       current = slice;
+
+       while (current && current != '\0') {
+               if (g_utf8_get_char (current) == 0xFFFC) {
+                       result = g_string_append_c (result, ' ');
+                       current = g_utf8_next_char (current);
+               } else {
+                       gchar *next = g_utf8_strchr (current, -1, 0xFFFC);
+                       if (next == NULL) {
+                               result = g_string_append (result, current);
+                       } else {
+                               result = g_string_append_len (result, current, next - current);
+                       }
+                       current = next;
+               }
+       }
+       g_free (slice);
+
+       return g_string_free (result, FALSE);
+       
+}