mu/guile/mu-guile.cc

242 lines
5.6 KiB
C++
Raw Normal View History

/*
2021-03-17 17:33:05 +01:00
** Copyright (C) 2011-2021 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** 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.
**
*/
2020-11-03 09:01:44 +01:00
#include "mu-guile.hh"
#include <config.h>
#include <locale.h>
2012-01-01 17:17:29 +01:00
#include <glib-object.h>
2020-02-25 06:39:00 +01:00
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wredundant-decls"
2012-01-01 17:17:29 +01:00
#include <libguile.h>
2020-02-25 06:39:00 +01:00
#pragma GCC diagnostic pop
2012-01-01 17:17:29 +01:00
#include <mu-runtime.hh>
2019-07-28 13:27:05 +02:00
#include <mu-store.hh>
2020-11-03 09:01:44 +01:00
#include <mu-query.hh>
2020-11-28 09:08:04 +01:00
#include <mu-msg.hh>
using namespace Mu;
2012-01-01 17:17:29 +01:00
SCM
mu_guile_scm_from_str (const char *str)
{
if (!str)
return SCM_BOOL_F;
else
return scm_from_stringn (str, strlen(str), "UTF-8",
SCM_FAILED_CONVERSION_QUESTION_MARK);
}
SCM
mu_guile_error (const char *func_name, int status,
2012-01-01 17:17:29 +01:00
const char *fmt, SCM args)
{
scm_error_scm (scm_from_locale_symbol ("MuError"),
scm_from_utf8_string (func_name ? func_name : "<nameless>"),
scm_from_utf8_string (fmt), args,
scm_list_1 (scm_from_int (status)));
return SCM_UNSPECIFIED;
2012-01-01 17:17:29 +01:00
}
SCM
mu_guile_g_error (const char *func_name, GError *err)
2012-01-01 17:17:29 +01:00
{
scm_error_scm (scm_from_locale_symbol ("MuError"),
scm_from_utf8_string (func_name),
scm_from_utf8_string (err ? err->message : "error"),
SCM_UNDEFINED, SCM_UNDEFINED);
return SCM_UNSPECIFIED;
2012-01-01 17:17:29 +01:00
}
/* there can be only one */
2021-03-17 17:33:05 +01:00
static std::unique_ptr<Mu::Store> StoreSingleton;
2011-12-30 11:36:59 +01:00
static gboolean
2020-11-03 09:01:44 +01:00
mu_guile_init_instance (const char *muhome) try
2011-12-30 11:36:59 +01:00
{
setlocale (LC_ALL, "");
2021-03-17 17:33:05 +01:00
if (!mu_runtime_init (muhome, "guile", true) || StoreSingleton)
2011-12-30 11:36:59 +01:00
return FALSE;
2021-03-17 17:33:05 +01:00
StoreSingleton = std::make_unique<Mu::Store>(
mu_runtime_path(MU_RUNTIME_PATH_XAPIANDB));
g_debug ("mu-guile: opened store @ %s (n=%zu); maildir: %s",
StoreSingleton->metadata().database_path.c_str(),
StoreSingleton->size(),
StoreSingleton->metadata().root_maildir.c_str());
2011-12-30 11:36:59 +01:00
return TRUE;
2012-07-31 17:46:27 +02:00
2020-11-03 09:01:44 +01:00
} catch (...) {
2021-03-17 17:33:05 +01:00
return FALSE;
2011-12-30 11:36:59 +01:00
}
static void
2020-11-03 09:01:44 +01:00
mu_guile_uninit_instance ()
2011-12-30 11:36:59 +01:00
{
2021-03-17 17:33:05 +01:00
StoreSingleton.reset();
2011-12-30 11:36:59 +01:00
mu_runtime_uninit ();
}
2021-03-17 17:33:05 +01:00
Mu::Store&
mu_guile_store ()
{
2021-03-17 17:33:05 +01:00
if (!StoreSingleton)
g_error("mu guile not initialized");
2020-11-03 09:01:44 +01:00
2021-03-17 17:33:05 +01:00
return *StoreSingleton.get();
}
gboolean
2020-11-03 09:01:44 +01:00
mu_guile_initialized ()
{
2021-03-17 17:33:05 +01:00
g_debug ("initialized ? %u", !!StoreSingleton);
return !!StoreSingleton;
}
SCM_DEFINE_PUBLIC (mu_initialize, "mu:initialize", 0, 1, 0,
(SCM MUHOME),
"Initialize mu - needed before you call any of the other "
"functions. Optionally, you can provide MUHOME which should be an "
"absolute path to your mu home directory "
2020-04-19 13:02:48 +02:00
"-- typically, the default, ~/.cache/mu, should be just fine.")
2011-12-30 11:36:59 +01:00
#define FUNC_NAME s_mu_initialize
{
char *muhome;
gboolean rv;
SCM_ASSERT (scm_is_string (MUHOME) || MUHOME == SCM_BOOL_F ||
SCM_UNBNDP(MUHOME), MUHOME, SCM_ARG1, FUNC_NAME);
if (mu_guile_initialized())
return mu_guile_error (FUNC_NAME, 0, "Already initialized",
SCM_UNSPECIFIED);
2012-01-01 17:17:29 +01:00
if (SCM_UNBNDP(MUHOME) || MUHOME == SCM_BOOL_F)
muhome = NULL;
else
muhome = scm_to_utf8_string (MUHOME);
2012-07-31 17:46:27 +02:00
rv = mu_guile_init_instance (muhome);
free (muhome);
if (!rv)
return mu_guile_error (FUNC_NAME, 0, "Failed to initialize mu",
SCM_UNSPECIFIED);
2021-03-17 17:33:05 +01:00
g_debug ("mu-guile: initialized @ %s (%p)",
muhome ? muhome : "<default>", StoreSingleton.get());
2011-12-19 22:54:54 +01:00
/* cleanup when we're exiting */
atexit (mu_guile_uninit_instance);
2011-12-19 22:54:54 +01:00
2012-01-01 21:47:03 +01:00
return SCM_UNSPECIFIED;
}
#undef FUNC_NAME
2012-01-01 17:17:29 +01:00
SCM_DEFINE_PUBLIC (mu_initialized_p, "mu:initialized?", 0, 0, 0,
(void), "Whether mu is initialized or not.\n")
2011-12-30 11:36:59 +01:00
#define FUNC_NAME s_mu_initialized_p
{
return mu_guile_initialized() ? SCM_BOOL_T : SCM_BOOL_F;
2011-12-30 11:36:59 +01:00
}
#undef FUNC_NAME
SCM_DEFINE (log_func, "mu:c:log", 1, 0, 1, (SCM LEVEL, SCM FRM, SCM ARGS),
"log some message at LEVEL using a list of ARGS applied to FRM"
"(in 'simple-format' notation).\n")
#define FUNC_NAME s_log_func
2011-12-30 11:36:59 +01:00
{
gchar *output;
2011-12-30 11:36:59 +01:00
SCM str;
int level;
2011-12-30 11:36:59 +01:00
SCM_ASSERT (scm_integer_p(LEVEL), LEVEL, SCM_ARG1, FUNC_NAME);
SCM_ASSERT (scm_is_string(FRM), FRM, SCM_ARG2, "<write_log>");
2011-12-30 11:36:59 +01:00
SCM_VALIDATE_REST_ARGUMENT(ARGS);
level = scm_to_int (LEVEL);
if (level != G_LOG_LEVEL_MESSAGE &&
level != G_LOG_LEVEL_WARNING &&
level != G_LOG_LEVEL_CRITICAL)
return mu_guile_error (FUNC_NAME, 0, "invalid log level",
SCM_UNSPECIFIED);
2011-12-30 11:36:59 +01:00
str = scm_simple_format (SCM_BOOL_F, FRM, ARGS);
if (!scm_is_string (str))
return SCM_UNSPECIFIED;
2011-12-30 11:36:59 +01:00
output = scm_to_utf8_string (str);
2020-11-03 09:01:44 +01:00
g_log (G_LOG_DOMAIN, (GLogLevelFlags)level, "%s", output);
free (output);
2011-12-30 11:36:59 +01:00
return SCM_UNSPECIFIED;
}
#undef FUNC_NAME
2011-12-30 11:36:59 +01:00
static struct {
const char* name;
unsigned val;
} VAR_PAIRS[] = {
{ "mu:message", G_LOG_LEVEL_MESSAGE },
{ "mu:warning", G_LOG_LEVEL_WARNING },
{ "mu:critical", G_LOG_LEVEL_CRITICAL }
};
static void
define_vars (void)
{
unsigned u;
for (u = 0; u != G_N_ELEMENTS(VAR_PAIRS); ++u) {
scm_c_define (VAR_PAIRS[u].name,
scm_from_uint (VAR_PAIRS[u].val));
scm_c_export (VAR_PAIRS[u].name, NULL);
}
}
2011-12-30 11:36:59 +01:00
void*
mu_guile_init (void *data)
{
define_vars ();
#ifndef SCM_MAGIC_SNARFER
#include "mu-guile.x"
#endif /*SCM_MAGIC_SNARFER*/
return NULL;
}