guile: upgrade to Mu::Message

This commit is contained in:
Dirk-Jan C. Binnema 2022-04-22 07:59:48 +03:00
parent b686c9da25
commit 85a3ff71ae
2 changed files with 78 additions and 100 deletions

View File

@ -32,8 +32,6 @@
#include <mu-runtime.hh> #include <mu-runtime.hh>
#include <mu-store.hh> #include <mu-store.hh>
#include <mu-query.hh> #include <mu-query.hh>
#include <mu-msg.hh>
#include <mu-msg-part.hh>
using namespace Mu; using namespace Mu;
@ -45,11 +43,13 @@ static SCM SYMB_PRIO_LOW, SYMB_PRIO_NORMAL, SYMB_PRIO_HIGH;
static std::array<SCM, AllMessageFlagInfos.size()> SYMB_FLAGS; static std::array<SCM, AllMessageFlagInfos.size()> SYMB_FLAGS;
static SCM SYMB_CONTACT_TO, SYMB_CONTACT_CC, SYMB_CONTACT_BCC, SYMB_CONTACT_FROM; static SCM SYMB_CONTACT_TO, SYMB_CONTACT_CC, SYMB_CONTACT_BCC, SYMB_CONTACT_FROM;
struct _MuMsgWrapper { struct MuMsgWrapper {
MuMsg* _msg; MuMsgWrapper(const Message& msg, bool unrefme):
gboolean _unrefme; _msg{msg}, _unrefme(unrefme)
{}
Message _msg;
bool _unrefme;
}; };
typedef struct _MuMsgWrapper MuMsgWrapper;
static long MSG_TAG; static long MSG_TAG;
static gboolean static gboolean
@ -59,12 +59,10 @@ mu_guile_scm_is_msg(SCM scm)
} }
SCM SCM
mu_guile_msg_to_scm(MuMsg* msg) mu_guile_msg_to_scm(const Message& msg)
{ {
MuMsgWrapper* msgwrap; MuMsgWrapper* msgwrap;
g_return_val_if_fail(msg, SCM_UNDEFINED);
msgwrap = (MuMsgWrapper*)scm_gc_malloc(sizeof(MuMsgWrapper), "msg"); msgwrap = (MuMsgWrapper*)scm_gc_malloc(sizeof(MuMsgWrapper), "msg");
msgwrap->_msg = msg; msgwrap->_msg = msg;
msgwrap->_unrefme = FALSE; msgwrap->_unrefme = FALSE;
@ -90,10 +88,10 @@ typedef struct {
static SCM static SCM
get_flags_scm(MuMsg* msg) get_flags_scm(const Message& msg)
{ {
SCM lst{SCM_EOL}; SCM lst{SCM_EOL};
const auto flags{mu_msg_get_flags(msg)}; const auto flags{msg.flags()};
for (auto i = 0; i != AllMessageFlagInfos.size(); ++i) { for (auto i = 0; i != AllMessageFlagInfos.size(); ++i) {
const auto& info{AllMessageFlagInfos.at(i)}; const auto& info{AllMessageFlagInfos.at(i)};
@ -105,9 +103,9 @@ get_flags_scm(MuMsg* msg)
} }
static SCM static SCM
get_prio_scm(MuMsg* msg) get_prio_scm(const Message& msg)
{ {
switch (mu_msg_get_prio(msg)) { switch (msg.priority()) {
case Priority::Low: return SYMB_PRIO_LOW; case Priority::Low: return SYMB_PRIO_LOW;
case Priority::Normal: return SYMB_PRIO_NORMAL; case Priority::Normal: return SYMB_PRIO_NORMAL;
case Priority::High: return SYMB_PRIO_HIGH; case Priority::High: return SYMB_PRIO_HIGH;
@ -117,16 +115,12 @@ get_prio_scm(MuMsg* msg)
} }
static SCM static SCM
msg_string_list_field(MuMsg* msg, Field::Id field_id) msg_string_list_field(const Message& msg, Field::Id field_id)
{ {
SCM scmlst; SCM scmlst{SCM_EOL};
const GSList* lst; for (auto&& val: msg.document().string_vec_value(field_id)) {
lst = mu_msg_get_field_string_list(msg, field_id);
for (scmlst = SCM_EOL; lst; lst = g_slist_next(lst)) {
SCM item; SCM item;
item = scm_list_1(mu_guile_scm_from_str((const char*)lst->data)); item = scm_list_1(mu_guile_scm_from_str(val.c_str()));
scmlst = scm_append_x(scm_list_2(scmlst, item)); scmlst = scm_append_x(scm_list_2(scmlst, item));
} }
@ -134,28 +128,12 @@ msg_string_list_field(MuMsg* msg, Field::Id field_id)
} }
static SCM static SCM
get_body(MuMsg* msg, gboolean html) get_body(const Message& msg, bool html)
{ {
SCM data; if (const auto body = html ? msg.body_html() : msg.body_text(); body)
const char* body; return mu_guile_scm_from_str(body->c_str());
MuMsgOptions opts;
opts = MU_MSG_OPTION_NONE;
if (html)
body = mu_msg_get_body_html(msg, opts);
else else
body = mu_msg_get_body_text(msg, opts); return SCM_BOOL_F;
if (body)
data = mu_guile_scm_from_str(body);
else
data = SCM_BOOL_F;
/* explicitly close the file backend, so we won't run of fds */
mu_msg_unload_msg_file(msg);
return data;
} }
SCM_DEFINE(get_field, SCM_DEFINE(get_field,
@ -170,6 +148,7 @@ SCM_DEFINE(get_field,
MuMsgWrapper* msgwrap; MuMsgWrapper* msgwrap;
size_t field_id; size_t field_id;
msgwrap = (MuMsgWrapper*)SCM_CDR(MSG); msgwrap = (MuMsgWrapper*)SCM_CDR(MSG);
const auto& msg{msgwrap->_msg};
MU_GUILE_INITIALIZED_OR_ERROR; MU_GUILE_INITIALIZED_OR_ERROR;
@ -178,47 +157,48 @@ SCM_DEFINE(get_field,
field_id = scm_to_int(FIELD); field_id = scm_to_int(FIELD);
if (field_id == MU_GUILE_MSG_FIELD_ID_TIMESTAMP) if (field_id == MU_GUILE_MSG_FIELD_ID_TIMESTAMP)
return scm_from_uint((unsigned)mu_msg_get_timestamp(msgwrap->_msg)); return scm_from_uint((unsigned)msgwrap->_msg.date());
const auto field_opt{field_from_number(static_cast<size_t>(field_id))}; const auto field_opt{field_from_number(static_cast<size_t>(field_id))};
SCM_ASSERT(!!field_opt, FIELD, SCM_ARG2, FUNC_NAME); SCM_ASSERT(!!field_opt, FIELD, SCM_ARG2, FUNC_NAME);
switch (field_opt->id) { switch (field_opt->id) {
case Field::Id::Priority: case Field::Id::Priority:
return get_prio_scm(msgwrap->_msg); return get_prio_scm(msg);
case Field::Id::Flags: case Field::Id::Flags:
return get_flags_scm(msgwrap->_msg); return get_flags_scm(msg);
case Field::Id::BodyHtml: case Field::Id::BodyHtml:
return get_body(msgwrap->_msg, TRUE); return get_body(msg, true);
case Field::Id::BodyText: case Field::Id::BodyText:
return get_body(msgwrap->_msg, FALSE); return get_body(msg, false);
default: break; default: break;
} }
switch (field_opt->type) { switch (field_opt->type) {
case Field::Type::String: case Field::Type::String:
return mu_guile_scm_from_str(mu_msg_get_field_string(msgwrap->_msg, return mu_guile_scm_from_str(msg.document().string_value(field_opt->id).c_str());
field_opt->id));
case Field::Type::ByteSize: case Field::Type::ByteSize:
case Field::Type::TimeT: case Field::Type::TimeT:
return scm_from_uint(mu_msg_get_field_numeric(msgwrap->_msg, field_opt->id));
case Field::Type::Integer: case Field::Type::Integer:
return scm_from_int(mu_msg_get_field_numeric(msgwrap->_msg, field_opt->id)); return scm_from_uint(msg.document().integer_value(field_opt->id));
case Field::Type::StringList: case Field::Type::StringList:
return msg_string_list_field(msgwrap->_msg, field_opt->id); return msg_string_list_field(msg, field_opt->id);
default: SCM_ASSERT(0, FIELD, SCM_ARG2, FUNC_NAME); default: SCM_ASSERT(0, FIELD, SCM_ARG2, FUNC_NAME);
} }
} }
#undef FUNC_NAME #undef FUNC_NAME
static SCM static SCM
contacts_to_list(MuMsg *msg, Option<Field::Id> field_id) contacts_to_list(const Message& msg, Option<Field::Id> field_id)
{ {
SCM list{SCM_EOL}; SCM list{SCM_EOL};
const auto contacts{mu_msg_get_contacts(msg, field_id)}; const auto contacts{field_id ?
for (auto&& contact: mu_msg_get_contacts(msg, field_id)) { msg.document().contacts_value(*field_id) :
msg.all_contacts()};
for (auto&& contact: contacts) {
SCM item{scm_list_1( SCM item{scm_list_1(
scm_cons(mu_guile_scm_from_str(contact.name.c_str()), scm_cons(mu_guile_scm_from_str(contact.name.c_str()),
mu_guile_scm_from_str(contact.email.c_str())))}; mu_guile_scm_from_str(contact.email.c_str())))};
@ -278,7 +258,7 @@ SCM_DEFINE(get_contacts,
#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); msg.u
return list; return list;
} }
@ -428,9 +408,8 @@ SCM_DEFINE(for_each_message,
return SCM_UNSPECIFIED; return SCM_UNSPECIFIED;
for (auto&& mi : *res) { for (auto&& mi : *res) {
auto msg{mi.floating_msg()}; if (auto msg{mi.message()}; msg) {
if (msg) { auto msgsmob{mu_guile_msg_to_scm(msg)};
auto msgsmob{mu_guile_msg_to_scm(mu_msg_ref(msg))};
scm_call_1(FUNC, msgsmob); scm_call_1(FUNC, msgsmob);
} }
} }

View File

@ -31,7 +31,6 @@
#include <mu-runtime.hh> #include <mu-runtime.hh>
#include <mu-store.hh> #include <mu-store.hh>
#include <mu-query.hh> #include <mu-query.hh>
#include <mu-msg.hh>
using namespace Mu; using namespace Mu;