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 "mu-guile-message.hh"
#include "libguile/scm.h"
#include "mu-message-flags.hh" #include "mu-message-flags.hh"
#include <config.h> #include <config.h>
@ -80,9 +81,9 @@ typedef struct {
do { \ do { \
if (!(mu_guile_initialized())) { \ if (!(mu_guile_initialized())) { \
mu_guile_error(FUNC_NAME, \ mu_guile_error(FUNC_NAME, \
0, \ 0, \
"mu not initialized; call mu:initialize", \ "mu not initialized; call mu:initialize", \
SCM_UNDEFINED); \ SCM_UNDEFINED); \
return SCM_UNSPECIFIED; \ return SCM_UNSPECIFIED; \
} \ } \
} while (0) } while (0)
@ -158,12 +159,12 @@ get_body(MuMsg* msg, gboolean html)
} }
SCM_DEFINE(get_field, SCM_DEFINE(get_field,
"mu:c:get-field", "mu:c:get-field",
2, 2,
0, 0,
0, 0,
(SCM MSG, SCM FIELD), (SCM MSG, SCM FIELD),
"Get the field FIELD from message MSG.\n") "Get the field FIELD from message MSG.\n")
#define FUNC_NAME s_get_field #define FUNC_NAME s_get_field
{ {
MuMsgWrapper* msgwrap; MuMsgWrapper* msgwrap;
@ -177,9 +178,9 @@ SCM_DEFINE(get_field,
mfid = scm_to_int(FIELD); mfid = scm_to_int(FIELD);
SCM_ASSERT(mfid < MU_MSG_FIELD_ID_NUM || mfid == MU_GUILE_MSG_FIELD_ID_TIMESTAMP, SCM_ASSERT(mfid < MU_MSG_FIELD_ID_NUM || mfid == MU_GUILE_MSG_FIELD_ID_TIMESTAMP,
FIELD, FIELD,
SCM_ARG2, SCM_ARG2,
FUNC_NAME); FUNC_NAME);
switch (mfid) { switch (mfid) {
case MU_MSG_FIELD_ID_PRIO: return get_prio_scm(msgwrap->_msg); case MU_MSG_FIELD_ID_PRIO: return get_prio_scm(msgwrap->_msg);
@ -196,8 +197,7 @@ SCM_DEFINE(get_field,
switch (mu_msg_field_type(mfid)) { switch (mu_msg_field_type(mfid)) {
case MU_MSG_FIELD_TYPE_STRING: case MU_MSG_FIELD_TYPE_STRING:
return mu_guile_scm_from_str(mu_msg_get_field_string(msgwrap->_msg, mfid)); 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_BYTESIZE:
case MU_MSG_FIELD_TYPE_TIME_T: case MU_MSG_FIELD_TYPE_TIME_T:
return scm_from_uint(mu_msg_get_field_numeric(msgwrap->_msg, mfid)); return scm_from_uint(mu_msg_get_field_numeric(msgwrap->_msg, mfid));
case MU_MSG_FIELD_TYPE_INT: case MU_MSG_FIELD_TYPE_INT:
@ -208,77 +208,74 @@ SCM_DEFINE(get_field,
} }
#undef FUNC_NAME #undef FUNC_NAME
struct _EachContactData { static SCM
SCM lst; contacts_to_list(MuMsg *msg, Mu::MessageContact::Type mtype)
MuMsgContactType ctype;
};
typedef struct _EachContactData EachContactData;
static void
contacts_to_list(MuMsgContact* contact, EachContactData* ecdata)
{ {
SCM item; SCM list{SCM_EOL};
if (ecdata->ctype != MU_MSG_CONTACT_TYPE_ALL && const auto contacts{mu_msg_get_contacts(msg, mtype)};
mu_msg_contact_type(contact) != ecdata->ctype) for (auto&& contact: mu_msg_get_contacts(msg, mtype)) {
return; 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)), return list;
mu_guile_scm_from_str(mu_msg_contact_email(contact))));
ecdata->lst = scm_append_x(scm_list_2(ecdata->lst, item));
} }
SCM_DEFINE(get_contacts, SCM_DEFINE(get_contacts,
"mu:c:get-contacts", "mu:c:get-contacts",
2, 2,
0, 0,
0, 0,
(SCM MSG, SCM CONTACT_TYPE), (SCM MSG, SCM CONTACT_TYPE),
"Get a list of contact information pairs.\n") "Get a list of contact information pairs.\n")
#define FUNC_NAME s_get_contacts #define FUNC_NAME s_get_contacts
{ {
MuMsgWrapper* msgwrap; MuMsgWrapper* msgwrap;
EachContactData ecdata; SCM list;
Mu::MessageContact::Type mtype;
MU_GUILE_INITIALIZED_OR_ERROR; MU_GUILE_INITIALIZED_OR_ERROR;
SCM_ASSERT(mu_guile_scm_is_msg(MSG), MSG, SCM_ARG1, FUNC_NAME); SCM_ASSERT(mu_guile_scm_is_msg(MSG), MSG, SCM_ARG1, FUNC_NAME);
SCM_ASSERT(scm_symbol_p(CONTACT_TYPE) || scm_is_bool(CONTACT_TYPE), SCM_ASSERT(scm_symbol_p(CONTACT_TYPE) || scm_is_bool(CONTACT_TYPE),
CONTACT_TYPE, CONTACT_TYPE,
SCM_ARG2, SCM_ARG2,
FUNC_NAME); FUNC_NAME);
if (CONTACT_TYPE == SCM_BOOL_F) if (CONTACT_TYPE == SCM_BOOL_F)
return SCM_UNSPECIFIED; /* nothing to do */ 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 { else {
if (scm_is_eq(CONTACT_TYPE, SYMB_CONTACT_TO)) 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)) 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)) 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)) else if (scm_is_eq(CONTACT_TYPE, SYMB_CONTACT_FROM))
ecdata.ctype = MU_MSG_CONTACT_TYPE_FROM; mtype = Mu::MessageContact::Type::From;
else { else {
mu_guile_error(FUNC_NAME, 0, "invalid contact type", SCM_UNDEFINED); mu_guile_error(FUNC_NAME, 0, "invalid contact type", SCM_UNDEFINED);
return SCM_UNSPECIFIED; return SCM_UNSPECIFIED;
} }
} }
ecdata.lst = SCM_EOL;
msgwrap = (MuMsgWrapper*)SCM_CDR(MSG); msgwrap = (MuMsgWrapper*)SCM_CDR(MSG);
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-function-type" #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 #pragma GCC diagnostic pop
/* explicitly close the file backend, so we won't run out of fds */ /* explicitly close the file backend, so we won't run out of fds */
mu_msg_unload_msg_file(msgwrap->_msg); mu_msg_unload_msg_file(msgwrap->_msg);
return ecdata.lst; return list;
} }
#undef FUNC_NAME #undef FUNC_NAME
@ -321,14 +318,14 @@ each_part(MuMsg* msg, MuMsgPart* part, AttInfo* attinfo)
} }
SCM_DEFINE(get_parts, SCM_DEFINE(get_parts,
"mu:c:get-parts", "mu:c:get-parts",
1, 1,
1, 1,
0, 0,
(SCM MSG, SCM ATTS_ONLY), (SCM MSG, SCM ATTS_ONLY),
"Get the list of mime-parts for MSG. If ATTS_ONLY is #t, only" "Get the list of mime-parts for MSG. If ATTS_ONLY is #t, only"
"get parts that are (look like) attachments. The resulting list has " "get parts that are (look like) attachments. The resulting list has "
"elements which are list of the form (index name mime-type size).\n") "elements which are list of the form (index name mime-type size).\n")
#define FUNC_NAME s_get_parts #define FUNC_NAME s_get_parts
{ {
MuMsgWrapper* msgwrap; MuMsgWrapper* msgwrap;
@ -344,9 +341,9 @@ SCM_DEFINE(get_parts,
msgwrap = (MuMsgWrapper*)SCM_CDR(MSG); msgwrap = (MuMsgWrapper*)SCM_CDR(MSG);
mu_msg_part_foreach(msgwrap->_msg, mu_msg_part_foreach(msgwrap->_msg,
MU_MSG_OPTION_NONE, MU_MSG_OPTION_NONE,
(MuMsgPartForeachFunc)each_part, (MuMsgPartForeachFunc)each_part,
&attinfo); &attinfo);
/* explicitly close the file backend, so we won't run of fds */ /* explicitly close the file backend, so we won't run of fds */
mu_msg_unload_msg_file(msgwrap->_msg); mu_msg_unload_msg_file(msgwrap->_msg);
@ -356,12 +353,12 @@ SCM_DEFINE(get_parts,
#undef FUNC_NAME #undef FUNC_NAME
SCM_DEFINE(get_header, SCM_DEFINE(get_header,
"mu:c:get-header", "mu:c:get-header",
2, 2,
0, 0,
0, 0,
(SCM MSG, SCM HEADER), (SCM MSG, SCM HEADER),
"Get an arbitrary HEADER from MSG.\n") "Get an arbitrary HEADER from MSG.\n")
#define FUNC_NAME s_get_header #define FUNC_NAME s_get_header
{ {
MuMsgWrapper* msgwrap; MuMsgWrapper* msgwrap;
@ -392,16 +389,16 @@ get_query_results(Mu::Store& store, const char* expr, int maxnum)
} }
SCM_DEFINE(for_each_message, SCM_DEFINE(for_each_message,
"mu:c:for-each-message", "mu:c:for-each-message",
3, 3,
0, 0,
0, 0,
(SCM FUNC, SCM EXPR, SCM MAXNUM), (SCM FUNC, SCM EXPR, SCM MAXNUM),
"Call FUNC for each msg in the message store matching EXPR. EXPR is" "Call FUNC for each msg in the message store matching EXPR. EXPR is"
"either a string containing a mu search expression or a boolean; in the former " "either a string containing a mu search expression or a boolean; in the former "
"case, limit the messages to only those matching the expression, in the " "case, limit the messages to only those matching the expression, in the "
"latter case, match /all/ messages if the EXPR equals #t, and match " "latter case, match /all/ messages if the EXPR equals #t, and match "
"none if EXPR equals #f.") "none if EXPR equals #f.")
#define FUNC_NAME s_for_each_message #define FUNC_NAME s_for_each_message
{ {
char* expr{}; char* expr{};