guile: use Mu::MessageContact

Use the new contact class
This commit is contained in:
Dirk-Jan C. Binnema 2022-02-19 18:59:42 +02:00
parent 3aa053e158
commit 6a7e706354
1 changed files with 74 additions and 77 deletions

View File

@ -17,6 +17,7 @@
**
*/
#include "mu-guile-message.hh"
#include "libguile/scm.h"
#include "mu-message-flags.hh"
#include <config.h>
@ -196,8 +197,7 @@ SCM_DEFINE(get_field,
switch (mu_msg_field_type(mfid)) {
case MU_MSG_FIELD_TYPE_STRING:
return mu_guile_scm_from_str(mu_msg_get_field_string(msgwrap->_msg, mfid));
case MU_MSG_FIELD_TYPE_BYTESIZE:
return mu_guile_scm_from_str(mu_msg_get_field_string(msgwrap->_msg, mfid)); case MU_MSG_FIELD_TYPE_BYTESIZE:
case MU_MSG_FIELD_TYPE_TIME_T:
return scm_from_uint(mu_msg_get_field_numeric(msgwrap->_msg, mfid));
case MU_MSG_FIELD_TYPE_INT:
@ -208,25 +208,20 @@ SCM_DEFINE(get_field,
}
#undef FUNC_NAME
struct _EachContactData {
SCM lst;
MuMsgContactType ctype;
};
typedef struct _EachContactData EachContactData;
static void
contacts_to_list(MuMsgContact* contact, EachContactData* ecdata)
static SCM
contacts_to_list(MuMsg *msg, Mu::MessageContact::Type mtype)
{
SCM item;
SCM list{SCM_EOL};
if (ecdata->ctype != MU_MSG_CONTACT_TYPE_ALL &&
mu_msg_contact_type(contact) != ecdata->ctype)
return;
const auto contacts{mu_msg_get_contacts(msg, mtype)};
for (auto&& contact: mu_msg_get_contacts(msg, mtype)) {
SCM item{scm_list_1(
scm_cons(mu_guile_scm_from_str(contact.name.c_str()),
mu_guile_scm_from_str(contact.email.c_str())))};
list = scm_append_x(scm_list_2(list, item));
}
item = scm_list_1(scm_cons(mu_guile_scm_from_str(mu_msg_contact_name(contact)),
mu_guile_scm_from_str(mu_msg_contact_email(contact))));
ecdata->lst = scm_append_x(scm_list_2(ecdata->lst, item));
return list;
}
SCM_DEFINE(get_contacts,
@ -239,7 +234,8 @@ SCM_DEFINE(get_contacts,
#define FUNC_NAME s_get_contacts
{
MuMsgWrapper* msgwrap;
EachContactData ecdata;
SCM list;
Mu::MessageContact::Type mtype;
MU_GUILE_INITIALIZED_OR_ERROR;
@ -251,34 +247,35 @@ SCM_DEFINE(get_contacts,
if (CONTACT_TYPE == SCM_BOOL_F)
return SCM_UNSPECIFIED; /* nothing to do */
else if (CONTACT_TYPE == SCM_BOOL_T)
ecdata.ctype = MU_MSG_CONTACT_TYPE_ALL;
if (CONTACT_TYPE == SCM_BOOL_T)
mtype = Mu::MessageContact::Type::Unknown; /* get all */
else {
if (scm_is_eq(CONTACT_TYPE, SYMB_CONTACT_TO))
ecdata.ctype = MU_MSG_CONTACT_TYPE_TO;
mtype = Mu::MessageContact::Type::To;
else if (scm_is_eq(CONTACT_TYPE, SYMB_CONTACT_CC))
ecdata.ctype = MU_MSG_CONTACT_TYPE_CC;
mtype = Mu::MessageContact::Type::Cc;
else if (scm_is_eq(CONTACT_TYPE, SYMB_CONTACT_BCC))
ecdata.ctype = MU_MSG_CONTACT_TYPE_BCC;
mtype = Mu::MessageContact::Type::Bcc;
else if (scm_is_eq(CONTACT_TYPE, SYMB_CONTACT_FROM))
ecdata.ctype = MU_MSG_CONTACT_TYPE_FROM;
mtype = Mu::MessageContact::Type::From;
else {
mu_guile_error(FUNC_NAME, 0, "invalid contact type", SCM_UNDEFINED);
return SCM_UNSPECIFIED;
}
}
ecdata.lst = SCM_EOL;
msgwrap = (MuMsgWrapper*)SCM_CDR(MSG);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-function-type"
mu_msg_contact_foreach(msgwrap->_msg, (MuMsgContactForeachFunc)contacts_to_list, &ecdata);
list = contacts_to_list(msgwrap->_msg, mtype);
#pragma GCC diagnostic pop
/* explicitly close the file backend, so we won't run out of fds */
mu_msg_unload_msg_file(msgwrap->_msg);
return ecdata.lst;
return list;
}
#undef FUNC_NAME