Aggiunta la funzione SolipaMail::get_addresses_from_string.
authorAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Fri, 6 May 2011 11:02:27 +0000 (13:02 +0200)
committerAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Fri, 6 May 2011 11:02:27 +0000 (13:02 +0200)
TODO: test della correttezza della parte indirizzo mail.

src/mail.c
src/mail.h
tests/Makefile.am
tests/mail_get_addresses_from_string.c [new file with mode: 0644]

index 033cfb37dba24bdff5ac4e0e96fad87b8395bf4f..cc78e95e1e7a76d8468baffdae175fad1bcf496c 100644 (file)
@@ -470,6 +470,61 @@ noconnect:
 #endif
 }
 
+GSList
+*solipa_mail_get_addresses_from_string (const gchar *addresses)
+{
+       GSList *lst_ret;
+
+       gchar *_addrs;
+
+       gchar **addrs_parts;
+       gchar **addr_parts;
+       gchar *addr;
+       guint n_addrs_parts;
+       guint n_addr_parts;
+       guint i;
+
+       GHashTable *ht;
+
+       g_return_val_if_fail (addresses != NULL, NULL);
+
+       _addrs = g_strstrip (g_strdup (addresses));
+
+       g_return_val_if_fail (g_strcmp0 (_addrs, "") != 0, NULL);
+
+       lst_ret = NULL;
+
+       addrs_parts = g_strsplit_set (_addrs, ",;", -1);
+       n_addrs_parts = g_strv_length (addrs_parts);
+       for (i = 0; i < n_addrs_parts; i++)
+               {
+                       addr_parts = g_strsplit_set (addrs_parts[i], "<>", -1);
+                       n_addr_parts = g_strv_length (addr_parts);
+                       if (n_addr_parts > 0)
+                               {
+                                       ht = g_hash_table_new (g_str_hash, g_str_equal);
+                                       if (n_addr_parts == 1 || n_addr_parts == 2)
+                                               {
+                                                       /* TODO test address */
+                                                       addr = g_strstrip (g_strjoinv (NULL, g_strsplit_set (addr_parts[0], "<>", -1)));
+                                                       g_hash_table_insert (ht, "address", addr);
+                                               }
+                                       else if (n_addr_parts == 3)
+                                               {
+                                                       g_hash_table_insert (ht, "name", g_strstrip (g_strjoinv (NULL, g_strsplit (addr_parts[0], "\"", -1))));
+                                                       /* TODO test address */
+                                                       addr = g_strstrip (g_strjoinv (NULL, g_strsplit_set (addr_parts[1], "<>", -1)));
+                                                       g_hash_table_insert (ht, "address", addr);
+                                               }
+                                       lst_ret = g_slist_append (lst_ret, (gpointer)ht);
+                               }
+               }
+
+       g_strfreev (addrs_parts);
+
+       return lst_ret;
+}
+
 /* PRIVATE */
 static void
 solipa_mail_set_property (GObject *object,
index a61584c7df1e9375bc74085c35952da84900d6ae..f3c40aa2a6751a656de293193e052c248793316c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2010-2011 Andrea Zagli <azagli@libero.it>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -76,6 +76,8 @@ gchar *solipa_mail_get_as_string (SolipaMail *mail);
 
 void solipa_mail_send (SolipaMail *mail, const gchar *url);
 
+GSList *solipa_mail_get_addresses_from_string (const gchar *addresses);
+
 
 G_END_DECLS
 
index 60b78f5ca4a3db7be59fe4f676854928eaec7b85..1abf03d1190874e99f33269c8ef74b3946380d01 100644 (file)
@@ -11,6 +11,7 @@ LDADD = $(top_builddir)/src/libsolipa.la
 
 noinst_PROGRAMS = allegato \
                   mail \
+                  mail_get_addresses_from_string \
                   mailui \
                   utils \
                   utils_codfisc_piva \
diff --git a/tests/mail_get_addresses_from_string.c b/tests/mail_get_addresses_from_string.c
new file mode 100644 (file)
index 0000000..d94f19d
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2011 Andrea Zagli <azagli@libero.it>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <mail.h>
+
+int
+main (int argc, char **argv)
+{
+       GSList *lst;
+       GHashTable *ht;
+       gchar *name;
+       gchar *address;
+
+       g_type_init ();
+
+       if (argc != 2) return 0;
+
+       lst = solipa_mail_get_addresses_from_string (argv[1]);
+
+       while (lst != NULL)
+               {
+                       ht = (GHashTable *)lst->data;
+                       name = g_hash_table_lookup (ht, "name");
+                       address = g_hash_table_lookup (ht, "address");
+
+                       g_message ("%s: name %s address %s",
+                                          argv[1],
+                                          name,
+                                          address);
+
+                       lst = g_slist_next (lst);
+               }
+
+       return 0;
+}