diff --git a/TODO b/TODO index 8d57344f..78fc2294 100644 --- a/TODO +++ b/TODO @@ -24,6 +24,7 @@ - [X] fix rhel5 installation issue - [X] don't require gio - [X] summary + - [ ] attachment handling - [ ] review docs - [ ] xml,json,sexp output diff --git a/gtest.mk b/gtest.mk index a7afc955..39200b76 100644 --- a/gtest.mk +++ b/gtest.mk @@ -23,4 +23,6 @@ test: all $(TEST_PROGS) test "$$subdir" = "." || \ (cd $$subdir && $(MAKE) $(AM_MAKEFLAGS) $@ ) || exit $$? ; \ done -.PHONY: test + +.PHONY: test gprof + diff --git a/src/mu-msg-gmime.c b/src/mu-msg-gmime.c index 7074d793..95b10f39 100644 --- a/src/mu-msg-gmime.c +++ b/src/mu-msg-gmime.c @@ -772,6 +772,24 @@ mu_msg_gmime_get_summary (MuMsgGMime *msg, size_t max_lines) } +void +mu_msg_gmime_attach_foreach (MuMsgGMime* msg, MuMsgGMimeAttachForeachFunc func, + gpointer user_data) +{ + /* FIXME */ +} + + +gboolean +mu_msg_gmime_save_attachment (MuMsgGMime *msg, unsigned num, + const char *targetdir) +{ + return TRUE; /* FIXME */ +} + + + + const char* mu_msg_gmime_get_field_string (MuMsgGMime *msg, const MuMsgField* field) { @@ -844,8 +862,8 @@ fill_contact (MuMsgContact *contact, InternetAddress *addr, static int address_list_foreach (InternetAddressList *addrlist, MuMsgContactType ctype, - MuMsgGMimeContactsCallback cb, - void *ptr) + MuMsgGMimeContactsForeachFunc func, + gpointer user_data) { int i,rv; @@ -862,7 +880,7 @@ address_list_foreach (InternetAddressList *addrlist, continue; } - rv = (cb)(&contact, ptr); + rv = (func)(&contact, user_data); if (rv != 0) break; } @@ -872,8 +890,8 @@ address_list_foreach (InternetAddressList *addrlist, static int -mu_msg_gmime_get_contacts_from (MuMsgGMime *msg, MuMsgGMimeContactsCallback cb, - void *ptr) +mu_msg_gmime_get_contacts_from (MuMsgGMime *msg, MuMsgGMimeContactsForeachFunc func, + gpointer user_data) { InternetAddressList *list; int rv; @@ -886,7 +904,7 @@ mu_msg_gmime_get_contacts_from (MuMsgGMime *msg, MuMsgGMimeContactsCallback cb, list = internet_address_list_parse_string ( g_mime_message_get_sender (msg->_mime_msg)); - rv = address_list_foreach (list, MU_MSG_CONTACT_TYPE_FROM, cb, ptr); + rv = address_list_foreach (list, MU_MSG_CONTACT_TYPE_FROM, func, user_data); if (list) g_object_unref (G_OBJECT(list)); @@ -895,9 +913,9 @@ mu_msg_gmime_get_contacts_from (MuMsgGMime *msg, MuMsgGMimeContactsCallback cb, } -int -mu_msg_gmime_get_contacts_foreach (MuMsgGMime *msg, MuMsgGMimeContactsCallback cb, - void *ptr) +void +mu_msg_gmime_contacts_foreach (MuMsgGMime *msg, MuMsgGMimeContactsForeachFunc func, + gpointer user_data) { int i, rv; struct { @@ -909,23 +927,21 @@ mu_msg_gmime_get_contacts_foreach (MuMsgGMime *msg, MuMsgGMimeContactsCallback c {GMIME_RECIPIENT_TYPE_BCC, MU_MSG_CONTACT_TYPE_BCC}, }; - g_return_val_if_fail (cb && msg, -1); + g_return_if_fail (func && msg); /* first, get the from address */ - rv = mu_msg_gmime_get_contacts_from (msg, cb, ptr); + rv = mu_msg_gmime_get_contacts_from (msg, func, user_data); if (rv != 0) - return rv; /* callback told us to stop */ + return; /* callback told us to stop */ for (i = 0, rv = 0; i != G_N_ELEMENTS(ctypes); ++i) { InternetAddressList *addrlist; addrlist = g_mime_message_get_recipients (msg->_mime_msg, ctypes[i]._gmime_type); - rv = address_list_foreach (addrlist, ctypes[i]._type,cb, ptr); + rv = address_list_foreach (addrlist, ctypes[i]._type, func, user_data); if (rv != 0) break; } - - return rv; } diff --git a/src/mu-msg-gmime.h b/src/mu-msg-gmime.h index 46db8225..40defa0b 100644 --- a/src/mu-msg-gmime.h +++ b/src/mu-msg-gmime.h @@ -112,6 +112,46 @@ const char* mu_msg_gmime_get_body_html (MuMsgGMime *msg); */ const char* mu_msg_gmime_get_summary (MuMsgGMime *msg, size_t max_lines); + +struct _MuMsgGMimeAttach { + unsigned index; /* index of the attachment (names may not be unique) */ + const char* name; /* name of the attachment */ + size_t size; /* size in bytes, or 0 if not available */ + const char* mime_type; /* the mime type */ +}; +typedef struct _MuMsgGMimeAttach MuMsgGMimeAttach; + +typedef gboolean (*MuMsgGMimeAttachForeachFunc) (MuMsgGMimeAttach *att, gpointer data); + +/** + * call a user function for each attachment found in the message. the + * function will be calle d for each attachment and as long a the + * user-function returns TRUE + * + * @param msg a valid MuMsgGMime + * @param the function to call (callback) + * @param user_data a user pointer which will be passed to the callback function + * + * @return a list of attachment + */ +void mu_msg_gmime_attach_foreach (MuMsgGMime* msg, + MuMsgGMimeAttachForeachFunc func, + gpointer user_data); + +/** + * save a specific attachment to some targetdir + * + * @param msg a valid MuMsgGMime instance + * @param index index of the attachment you want to save + * @param targetdir filesystem directory to save the attachment + * + * @return TRUE if saving succeeded, FALSE otherwise + */ +gboolean mu_msg_gmime_save_attachment (MuMsgGMime *msg, unsigned num, + const char *targetdir); + + + /** * get the sender (From:) of this message * @@ -199,9 +239,7 @@ const char* mu_msg_gmime_get_msgid (MuMsgGMime *msg); * is no such header. the returned string should *not* be modified or freed. */ const char* mu_msg_gmime_get_header (MuMsgGMime *msg, - const char* header); - - + const char* header); /** * get the message date/time (the Date: field) as time_t, using UTC @@ -278,25 +316,21 @@ MuMsgPriority mu_msg_gmime_get_priority (MuMsgGMime *msg); */ time_t mu_msg_gmime_get_timestamp (MuMsgGMime *msg); - -typedef int (*MuMsgGMimeContactsCallback) (MuMsgContact*, void *ptr); - +typedef gboolean (*MuMsgGMimeContactsForeachFunc) (MuMsgContact* contact, + gpointer user_data); /** * call a function for each of the contacts in a message * * @param msg a valid MuMsgGMime* instance - * @param cb a callback function to call for each contact; when - * the callback returns non-0, the function stops, and this last - * callback return value is returned - * @param ptr a user-provide pointer that will be passed to the callback + * @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 * - * @return 0 if the callback was called for each recipient, -1 if there - * was an error and any other != 0 number the callback returned */ -int mu_msg_gmime_get_contacts_foreach (MuMsgGMime *msg, - MuMsgGMimeContactsCallback cb, - void *ptr); +void mu_msg_gmime_contacts_foreach (MuMsgGMime *msg, + MuMsgGMimeContactsForeachFunc func, + gpointer user_data); G_END_DECLS #endif /*__MU_MSG_GIME_H__*/ diff --git a/src/tests/Makefile.am b/src/tests/Makefile.am index a8ba40dc..87349957 100644 --- a/src/tests/Makefile.am +++ b/src/tests/Makefile.am @@ -44,6 +44,11 @@ TEST_PROGS += test-mu-query test_mu_query_SOURCES= test-mu-query.c dummy.cc test_mu_query_LDADD= libtestmucommon.la +TEST_PROGS += test-mu-msg-gmime +test_mu_msg_gmime_SOURCES= test-mu-msg-gmime.c +test_mu_msg_gmime_LDADD= libtestmucommon.la + + libtestmucommon_la_SOURCES= \ test-mu-common.c \ test-mu-common.h diff --git a/src/tests/test-mu-msg-gmime.c b/src/tests/test-mu-msg-gmime.c new file mode 100644 index 00000000..56d056f8 --- /dev/null +++ b/src/tests/test-mu-msg-gmime.c @@ -0,0 +1,110 @@ +/* +** 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 +#include + +#include + +#include "test-mu-common.h" +#include "src/mu-msg-gmime.h" + +static gchar* +get_mailpath (unsigned idx) +{ + const char* mailfile; + + switch (idx) { + case 1: mailfile = "cur/1220863042.12663_1.mindcrime!2,S"; break; + case 2: mailfile = "cur/1220863087.12663_19.mindcrime!2,S"; break; + default: mailfile = NULL; + } + + if (mailfile) + return g_strdup_printf ("%s/%s", MU_TESTMAILDIR, mailfile); + else + return NULL; +} + + + +static void +test_mu_msg_gmime_01 (void) +{ + char *mfile; + MuMsgGMime *msg; + + mu_msg_gmime_init (); + mfile = get_mailpath (1); + + msg = mu_msg_gmime_new (mfile, NULL); + + g_assert_cmpstr (mu_msg_gmime_get_to(msg), + ==, "gcc-help@gcc.gnu.org"); + g_assert_cmpstr (mu_msg_gmime_get_subject(msg), + ==, "gcc include search order"); + g_assert_cmpstr (mu_msg_gmime_get_from(msg), + ==, "anon@example.com"); + g_assert_cmpstr (mu_msg_gmime_get_msgid(msg), + ==, "3BE9E6535E3029448670913581E7A1A20D852173@" + "emss35m06.us.lmco.com"); + g_assert_cmpuint (mu_msg_gmime_get_priority(msg), /* 'klub' */ + ==, MU_MSG_PRIORITY_NORMAL); + g_assert_cmpuint (mu_msg_gmime_get_date(msg), + ==, 1217530645); + + mu_msg_gmime_destroy (msg); + + g_free (mfile); + mu_msg_gmime_uninit (); +} + +static gboolean +ignore_error (const char* log_domain, GLogLevelFlags log_level, const gchar* msg, + gpointer user_data) +{ + return FALSE; /* don't abort */ +} + +static void +shutup (void) {} + + + +int +main (int argc, char *argv[]) +{ + g_test_init (&argc, &argv, NULL); + + /* mu_msg_str_date */ + g_test_add_func ("/mu-msg-gmime/mu-msg-gmime", + test_mu_msg_gmime_01); + + g_log_set_handler (NULL, + G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION, + (GLogFunc)shutup, NULL); + + return g_test_run (); +}