From 6f33f7679741fa46ea697073da30685d43b0b312 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Sat, 14 May 2011 18:14:24 +0300 Subject: [PATCH] * merge mu-msg-contact.[ch] with mu-msg.[ch] --- src/Makefile.am | 2 - src/mu-msg-contact.c | 166 ---------------------------------------- src/mu-msg-contact.h | 126 ------------------------------ src/mu-msg.c | 143 +++++++++++++++++++++++++++++++++- src/mu-msg.h | 96 +++++++++++++++++++++++ src/mu-store.cc | 1 - src/tests/test-mu-msg.c | 1 - 7 files changed, 236 insertions(+), 299 deletions(-) delete mode 100644 src/mu-msg-contact.c delete mode 100644 src/mu-msg-contact.h diff --git a/src/Makefile.am b/src/Makefile.am index 39b97245..a82e1b91 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -64,8 +64,6 @@ libmu_la_SOURCES= \ mu-log.h \ mu-maildir.c \ mu-maildir.h \ - mu-msg-contact.c \ - mu-msg-contact.h \ mu-msg-cache.c \ mu-msg-cache.h \ mu-msg-data.c \ diff --git a/src/mu-msg-contact.c b/src/mu-msg-contact.c deleted file mode 100644 index 3f621bb1..00000000 --- a/src/mu-msg-contact.c +++ /dev/null @@ -1,166 +0,0 @@ -/* -** Copyright (C) 2008-2010 Dirk-Jan C. Binnema -** -** 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 3, 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -** -*/ - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif /*HAVE_CONFIG_H*/ - -#include -#include - -#include "mu-msg-priv.h" -#include "mu-msg.h" - -#include "mu-msg-contact.h" -#include "mu-util.h" - - -MuMsgContact * -mu_msg_contact_new (const char *name, const char *address, - MuMsgContactType type) -{ - MuMsgContact *self; - - g_return_val_if_fail (name, NULL); - g_return_val_if_fail (address, NULL); - g_return_val_if_fail (!mu_msg_contact_type_is_valid(type), - NULL); - - self = g_slice_new (MuMsgContact); - - self->name = g_strdup(name); - self->address = g_strdup(address); - self->type = type; - - return self; -} - - -void -mu_msg_contact_destroy (MuMsgContact *self) -{ - - if (!self) - return; - - g_free ((void*)self->name); - g_free ((void*)self->address); - g_slice_free (MuMsgContact, self); -} - - -static gboolean -fill_contact (MuMsgContact *self, InternetAddress *addr, - MuMsgContactType ctype) -{ - if (!addr) - return FALSE; - - self->name = internet_address_get_name (addr); - self->type = ctype; - - /* we only support internet mailbox addresses; if we don't - * check, g_mime hits an assert - */ - if (INTERNET_ADDRESS_IS_MAILBOX(addr)) - self->address = internet_address_mailbox_get_addr - (INTERNET_ADDRESS_MAILBOX(addr)); - else - self->address = NULL; - - return TRUE; -} - - -static void -address_list_foreach (InternetAddressList *addrlist, - MuMsgContactType ctype, - MuMsgContactForeachFunc func, - gpointer user_data) -{ - int i; - - if (!addrlist) - return; - - for (i = 0; i != internet_address_list_length(addrlist); ++i) { - - MuMsgContact contact; - if (!fill_contact(&contact, - internet_address_list_get_address (addrlist, i), - ctype)) { - MU_WRITE_LOG ("ignoring contact"); - continue; - } - - if (!(func)(&contact, user_data)) - break; - } -} - - - -static void -get_contacts_from (MuMsg *msg, MuMsgContactForeachFunc func, - gpointer user_data) -{ - InternetAddressList *lst; - - /* we go through this whole excercise of trying to get a *list* - * of 'From:' address (usually there is only one...), because - * internet_address_parse_string has the nice side-effect of - * splitting in names and addresses for us */ - lst = internet_address_list_parse_string ( - g_mime_message_get_sender (msg->_file->_mime_msg)); - - if (lst) { - address_list_foreach (lst, MU_MSG_CONTACT_TYPE_FROM, - func, user_data); - g_object_unref (G_OBJECT(lst)); - } -} - - -void -mu_msg_contact_foreach (MuMsg *msg, MuMsgContactForeachFunc func, - gpointer user_data) -{ - int i; - struct { - GMimeRecipientType _gmime_type; - MuMsgContactType _type; - } ctypes[] = { - {GMIME_RECIPIENT_TYPE_TO, MU_MSG_CONTACT_TYPE_TO}, - {GMIME_RECIPIENT_TYPE_CC, MU_MSG_CONTACT_TYPE_CC}, - {GMIME_RECIPIENT_TYPE_BCC, MU_MSG_CONTACT_TYPE_BCC}, - }; - - g_return_if_fail (func && msg); - - /* first, get the from address(es) */ - get_contacts_from (msg, func, user_data); - - /* get to, cc, bcc */ - for (i = 0; i != G_N_ELEMENTS(ctypes); ++i) { - InternetAddressList *addrlist; - addrlist = g_mime_message_get_recipients (msg->_file->_mime_msg, - ctypes[i]._gmime_type); - address_list_foreach (addrlist, ctypes[i]._type, func, user_data); - } -} diff --git a/src/mu-msg-contact.h b/src/mu-msg-contact.h deleted file mode 100644 index a65f4142..00000000 --- a/src/mu-msg-contact.h +++ /dev/null @@ -1,126 +0,0 @@ -/* -** Copyright (C) 2008-2010 Dirk-Jan C. Binnema -** -** 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 3, 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -** -*/ - -#ifndef __MU_MSG_CONTACT_H__ -#define __MU_MSG_CONTACT_H__ - -#include -#include "mu-msg.h" - -G_BEGIN_DECLS - -enum _MuMsgContactType { /* Reply-To:? */ - MU_MSG_CONTACT_TYPE_TO = 0, - MU_MSG_CONTACT_TYPE_FROM, - MU_MSG_CONTACT_TYPE_CC, - MU_MSG_CONTACT_TYPE_BCC, - - MU_MSG_CONTACT_TYPE_NUM -}; -typedef guint MuMsgContactType; - -#define mu_msg_contact_type_is_valid(MCT)\ - ((MCT) < MU_MSG_CONTACT_TYPE_NUM) - -struct _MuMsgContact { - const char *name; /* Foo Bar */ - const char *address; /* foo@bar.cuux */ - MuMsgContactType type; /* MU_MSG_CONTACT_TYPE_{ TO, - * CC, BCC, FROM} */ -}; -typedef struct _MuMsgContact MuMsgContact; - -/** - * create a new MuMsgContact object; note, in many case, this is not - * needed, any a stack-allocated struct can be uses. - * - * @param name the name of the contact - * @param address the e-mail address of the contact - * @param type the type of contact: cc, bcc, from, to - * - * @return a newly allocated MuMsgConcact or NULL in case of - * error. use mu_msg_contact_destroy to destroy it when it's no longer - * needed. - */ -MuMsgContact *mu_msg_contact_new (const char *name, const char *address, - MuMsgContactType type) - G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT; - -/** - * destroy a MuMsgConcact object - * - * @param contact a contact object, or NULL - */ -void mu_msg_contact_destroy (MuMsgContact *contact); - -/** - * macro to get the name of a contact - * - * @param ct a MuMsgContact - * - * @return the name - */ -#define mu_msg_contact_name(ct) ((ct)->name) - -/** - * macro to get the address of a contact - * - * @param ct a MuMsgContact - * - * @return the address - */ -#define mu_msg_contact_address(ct) ((ct)->address) - -/** - * macro to get the contact type - * - * @param ct a MuMsgContact - * - * @return the contact type - */ -#define mu_msg_contact_type(ct) ((ct)->type) - - -/** - * callback function - * - * @param contact - * @param user_data a user provided data pointer - * - * @return TRUE if we should continue the foreach, FALSE otherwise - */ -typedef gboolean (*MuMsgContactForeachFunc) (MuMsgContact* contact, - gpointer user_data); - - -/** - * call a function for each of the contacts in a message - * - * @param msg a valid MuMsgGMime* instance - * @param func a callback function to call for each contact; when - * the callback does not return TRUE, it won't be called again - * @param user_data a user-provide pointer that will be passed to the callback - * - */ -void mu_msg_contact_foreach (MuMsg *msg, MuMsgContactForeachFunc func, - gpointer user_data); - -G_END_DECLS - -#endif /*__MU_MSG_CONTACT_H__*/ diff --git a/src/mu-msg.c b/src/mu-msg.c index f6a4c3cc..30a22f54 100644 --- a/src/mu-msg.c +++ b/src/mu-msg.c @@ -187,10 +187,10 @@ get_str_field (MuMsg *self, MuMsgFieldId mfid) static gint64 get_num_field (MuMsg *self, MuMsgFieldId mfid) { - guint64 val; + guint64 val; - if (mu_msg_cache_cached (self->_cache, mfid)) - return mu_msg_cache_num (self->_cache, mfid); + if (mu_msg_cache_cached (self->_cache, mfid)) + return mu_msg_cache_num (self->_cache, mfid); /* if we don't have a file object yet, we need to create from * the file on disk */ @@ -361,3 +361,140 @@ mu_msg_get_field_numeric (MuMsg *self, MuMsgFieldId mfid) g_return_val_if_fail (self, -1); return get_num_field (self, mfid); } + + + +MuMsgContact * +mu_msg_contact_new (const char *name, const char *address, + MuMsgContactType type) +{ + MuMsgContact *self; + + g_return_val_if_fail (name, NULL); + g_return_val_if_fail (address, NULL); + g_return_val_if_fail (!mu_msg_contact_type_is_valid(type), + NULL); + + self = g_slice_new (MuMsgContact); + + self->name = g_strdup(name); + self->address = g_strdup(address); + self->type = type; + + return self; +} + + +void +mu_msg_contact_destroy (MuMsgContact *self) +{ + + if (!self) + return; + + g_free ((void*)self->name); + g_free ((void*)self->address); + g_slice_free (MuMsgContact, self); +} + + +static gboolean +fill_contact (MuMsgContact *self, InternetAddress *addr, + MuMsgContactType ctype) +{ + if (!addr) + return FALSE; + + self->name = internet_address_get_name (addr); + self->type = ctype; + + /* we only support internet mailbox addresses; if we don't + * check, g_mime hits an assert + */ + if (INTERNET_ADDRESS_IS_MAILBOX(addr)) + self->address = internet_address_mailbox_get_addr + (INTERNET_ADDRESS_MAILBOX(addr)); + else + self->address = NULL; + + return TRUE; +} + + +static void +address_list_foreach (InternetAddressList *addrlist, + MuMsgContactType ctype, + MuMsgContactForeachFunc func, + gpointer user_data) +{ + int i; + + if (!addrlist) + return; + + for (i = 0; i != internet_address_list_length(addrlist); ++i) { + + MuMsgContact contact; + if (!fill_contact(&contact, + internet_address_list_get_address (addrlist, i), + ctype)) { + MU_WRITE_LOG ("ignoring contact"); + continue; + } + + if (!(func)(&contact, user_data)) + break; + } +} + + + +static void +get_contacts_from (MuMsg *msg, MuMsgContactForeachFunc func, + gpointer user_data) +{ + InternetAddressList *lst; + + /* we go through this whole excercise of trying to get a *list* + * of 'From:' address (usually there is only one...), because + * internet_address_parse_string has the nice side-effect of + * splitting in names and addresses for us */ + lst = internet_address_list_parse_string ( + g_mime_message_get_sender (msg->_file->_mime_msg)); + + if (lst) { + address_list_foreach (lst, MU_MSG_CONTACT_TYPE_FROM, + func, user_data); + g_object_unref (G_OBJECT(lst)); + } +} + + +void +mu_msg_contact_foreach (MuMsg *msg, MuMsgContactForeachFunc func, + gpointer user_data) +{ + int i; + struct { + GMimeRecipientType _gmime_type; + MuMsgContactType _type; + } ctypes[] = { + {GMIME_RECIPIENT_TYPE_TO, MU_MSG_CONTACT_TYPE_TO}, + {GMIME_RECIPIENT_TYPE_CC, MU_MSG_CONTACT_TYPE_CC}, + {GMIME_RECIPIENT_TYPE_BCC, MU_MSG_CONTACT_TYPE_BCC}, + }; + + g_return_if_fail (func && msg); + + /* first, get the from address(es) */ + get_contacts_from (msg, func, user_data); + + /* get to, cc, bcc */ + for (i = 0; i != G_N_ELEMENTS(ctypes); ++i) { + InternetAddressList *addrlist; + addrlist = g_mime_message_get_recipients (msg->_file->_mime_msg, + ctypes[i]._gmime_type); + address_list_foreach (addrlist, ctypes[i]._type, func, user_data); + } +} + diff --git a/src/mu-msg.h b/src/mu-msg.h index a2e74d63..494b027a 100644 --- a/src/mu-msg.h +++ b/src/mu-msg.h @@ -300,6 +300,102 @@ const GSList *mu_msg_get_references (MuMsg *msg); */ const char* mu_msg_get_references_str (MuMsg *msg); + +enum _MuMsgContactType { /* Reply-To:? */ + MU_MSG_CONTACT_TYPE_TO = 0, + MU_MSG_CONTACT_TYPE_FROM, + MU_MSG_CONTACT_TYPE_CC, + MU_MSG_CONTACT_TYPE_BCC, + + MU_MSG_CONTACT_TYPE_NUM +}; +typedef guint MuMsgContactType; + +#define mu_msg_contact_type_is_valid(MCT)\ + ((MCT) < MU_MSG_CONTACT_TYPE_NUM) + +struct _MuMsgContact { + const char *name; /* Foo Bar */ + const char *address; /* foo@bar.cuux */ + MuMsgContactType type; /* MU_MSG_CONTACT_TYPE_{ TO, + * CC, BCC, FROM} */ +}; +typedef struct _MuMsgContact MuMsgContact; + +/** + * create a new MuMsgContact object; note, in many case, this is not + * needed, any a stack-allocated struct can be uses. + * + * @param name the name of the contact + * @param address the e-mail address of the contact + * @param type the type of contact: cc, bcc, from, to + * + * @return a newly allocated MuMsgConcact or NULL in case of + * error. use mu_msg_contact_destroy to destroy it when it's no longer + * needed. + */ +MuMsgContact *mu_msg_contact_new (const char *name, const char *address, + MuMsgContactType type) + G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT; + +/** + * destroy a MuMsgConcact object + * + * @param contact a contact object, or NULL + */ +void mu_msg_contact_destroy (MuMsgContact *contact); + +/** + * macro to get the name of a contact + * + * @param ct a MuMsgContact + * + * @return the name + */ +#define mu_msg_contact_name(ct) ((ct)->name) + +/** + * macro to get the address of a contact + * + * @param ct a MuMsgContact + * + * @return the address + */ +#define mu_msg_contact_address(ct) ((ct)->address) + +/** + * macro to get the contact type + * + * @param ct a MuMsgContact + * + * @return the contact type + */ +#define mu_msg_contact_type(ct) ((ct)->type) + + +/** + * callback function + * + * @param contact + * @param user_data a user provided data pointer + * + * @return TRUE if we should continue the foreach, FALSE otherwise + */ +typedef gboolean (*MuMsgContactForeachFunc) (MuMsgContact* contact, + gpointer user_data); + +/** + * call a function for each of the contacts in a message + * + * @param msg a valid MuMsgGMime* instance + * @param func a callback function to call for each contact; when + * the callback does not return TRUE, it won't be called again + * @param user_data a user-provide pointer that will be passed to the callback + * + */ +void mu_msg_contact_foreach (MuMsg *msg, MuMsgContactForeachFunc func, + gpointer user_data); + G_END_DECLS #endif /*__MU_MSG_H__*/ diff --git a/src/mu-store.cc b/src/mu-store.cc index fb88c802..9453538a 100644 --- a/src/mu-store.cc +++ b/src/mu-store.cc @@ -27,7 +27,6 @@ #include #include "mu-msg.h" -#include "mu-msg-contact.h" #include "mu-store.h" #include "mu-util.h" #include "mu-str.h" diff --git a/src/tests/test-mu-msg.c b/src/tests/test-mu-msg.c index d5d46cad..9ff03edf 100644 --- a/src/tests/test-mu-msg.c +++ b/src/tests/test-mu-msg.c @@ -30,7 +30,6 @@ #include "test-mu-common.h" #include "src/mu-msg.h" -#include "src/mu-msg-contact.h" static gboolean check_contact_01 (MuMsgContact *contact, int *idx)