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,12 +43,14 @@ 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
mu_guile_scm_is_msg(SCM scm) mu_guile_scm_is_msg(SCM scm)
@ -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;
@ -42,19 +41,19 @@ mu_guile_scm_from_str(const char* str)
return SCM_BOOL_F; return SCM_BOOL_F;
else else
return scm_from_stringn(str, return scm_from_stringn(str,
strlen(str), strlen(str),
"UTF-8", "UTF-8",
SCM_FAILED_CONVERSION_QUESTION_MARK); SCM_FAILED_CONVERSION_QUESTION_MARK);
} }
SCM SCM
mu_guile_error(const char* func_name, int status, const char* fmt, SCM args) mu_guile_error(const char* func_name, int status, const char* fmt, SCM args)
{ {
scm_error_scm(scm_from_locale_symbol("MuError"), scm_error_scm(scm_from_locale_symbol("MuError"),
scm_from_utf8_string(func_name ? func_name : "<nameless>"), scm_from_utf8_string(func_name ? func_name : "<nameless>"),
scm_from_utf8_string(fmt), scm_from_utf8_string(fmt),
args, args,
scm_list_1(scm_from_int(status))); scm_list_1(scm_from_int(status)));
return SCM_UNSPECIFIED; return SCM_UNSPECIFIED;
} }
@ -63,10 +62,10 @@ SCM
mu_guile_g_error(const char* func_name, GError* err) mu_guile_g_error(const char* func_name, GError* err)
{ {
scm_error_scm(scm_from_locale_symbol("MuError"), scm_error_scm(scm_from_locale_symbol("MuError"),
scm_from_utf8_string(func_name), scm_from_utf8_string(func_name),
scm_from_utf8_string(err ? err->message : "error"), scm_from_utf8_string(err ? err->message : "error"),
SCM_UNDEFINED, SCM_UNDEFINED,
SCM_UNDEFINED); SCM_UNDEFINED);
return SCM_UNSPECIFIED; return SCM_UNSPECIFIED;
} }
@ -85,9 +84,9 @@ try {
StoreSingleton = std::make_unique<Mu::Store>(mu_runtime_path(MU_RUNTIME_PATH_XAPIANDB)); StoreSingleton = std::make_unique<Mu::Store>(mu_runtime_path(MU_RUNTIME_PATH_XAPIANDB));
g_debug("mu-guile: opened store @ %s (n=%zu); maildir: %s", g_debug("mu-guile: opened store @ %s (n=%zu); maildir: %s",
StoreSingleton->properties().database_path.c_str(), StoreSingleton->properties().database_path.c_str(),
StoreSingleton->size(), StoreSingleton->size(),
StoreSingleton->properties().root_maildir.c_str()); StoreSingleton->properties().root_maildir.c_str());
return TRUE; return TRUE;
@ -121,24 +120,24 @@ mu_guile_initialized()
} }
SCM_DEFINE_PUBLIC(mu_initialize, SCM_DEFINE_PUBLIC(mu_initialize,
"mu:initialize", "mu:initialize",
0, 0,
1, 1,
0, 0,
(SCM MUHOME), (SCM MUHOME),
"Initialize mu - needed before you call any of the other " "Initialize mu - needed before you call any of the other "
"functions. Optionally, you can provide MUHOME which should be an " "functions. Optionally, you can provide MUHOME which should be an "
"absolute path to your mu home directory " "absolute path to your mu home directory "
"-- typically, the default, ~/.cache/mu, should be just fine.") "-- typically, the default, ~/.cache/mu, should be just fine.")
#define FUNC_NAME s_mu_initialize #define FUNC_NAME s_mu_initialize
{ {
char* muhome; char* muhome;
gboolean rv; gboolean rv;
SCM_ASSERT(scm_is_string(MUHOME) || MUHOME == SCM_BOOL_F || SCM_UNBNDP(MUHOME), SCM_ASSERT(scm_is_string(MUHOME) || MUHOME == SCM_BOOL_F || SCM_UNBNDP(MUHOME),
MUHOME, MUHOME,
SCM_ARG1, SCM_ARG1,
FUNC_NAME); FUNC_NAME);
if (mu_guile_initialized()) if (mu_guile_initialized())
return mu_guile_error(FUNC_NAME, 0, "Already initialized", SCM_UNSPECIFIED); return mu_guile_error(FUNC_NAME, 0, "Already initialized", SCM_UNSPECIFIED);
@ -155,7 +154,7 @@ SCM_DEFINE_PUBLIC(mu_initialize,
} }
g_debug("mu-guile: initialized @ %s (%p)", g_debug("mu-guile: initialized @ %s (%p)",
muhome ? muhome : "<default>", StoreSingleton.get()); muhome ? muhome : "<default>", StoreSingleton.get());
free(muhome); free(muhome);
/* cleanup when we're exiting */ /* cleanup when we're exiting */
@ -166,12 +165,12 @@ SCM_DEFINE_PUBLIC(mu_initialize,
#undef FUNC_NAME #undef FUNC_NAME
SCM_DEFINE_PUBLIC(mu_initialized_p, SCM_DEFINE_PUBLIC(mu_initialized_p,
"mu:initialized?", "mu:initialized?",
0, 0,
0, 0,
0, 0,
(void), (void),
"Whether mu is initialized or not.\n") "Whether mu is initialized or not.\n")
#define FUNC_NAME s_mu_initialized_p #define FUNC_NAME s_mu_initialized_p
{ {
return mu_guile_initialized() ? SCM_BOOL_T : SCM_BOOL_F; return mu_guile_initialized() ? SCM_BOOL_T : SCM_BOOL_F;
@ -179,13 +178,13 @@ SCM_DEFINE_PUBLIC(mu_initialized_p,
#undef FUNC_NAME #undef FUNC_NAME
SCM_DEFINE(log_func, SCM_DEFINE(log_func,
"mu:c:log", "mu:c:log",
1, 1,
0, 0,
1, 1,
(SCM LEVEL, SCM FRM, SCM ARGS), (SCM LEVEL, SCM FRM, SCM ARGS),
"log some message at LEVEL using a list of ARGS applied to FRM" "log some message at LEVEL using a list of ARGS applied to FRM"
"(in 'simple-format' notation).\n") "(in 'simple-format' notation).\n")
#define FUNC_NAME s_log_func #define FUNC_NAME s_log_func
{ {
gchar* output; gchar* output;