diff --git a/src/Makefile.am b/src/Makefile.am index a82e1b91..1a33efe9 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -70,6 +70,8 @@ libmu_la_SOURCES= \ mu-msg-data.h \ mu-msg-data-cache.c \ mu-msg-data-cache.h \ + mu-msg-db.c \ + mu-msg-db.h \ mu-msg-fields.c \ mu-msg-fields.h \ mu-msg-file.c \ diff --git a/src/mu-msg-db.cc b/src/mu-msg-db.cc new file mode 100644 index 00000000..c9e934d8 --- /dev/null +++ b/src/mu-msg-db.cc @@ -0,0 +1,98 @@ +/* +** Copyright (C) 2008-2011 Dirk-Jan C. Binnema +** +** 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 of the License, 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. +** +*/ + +#include +#include +#include +#include +#include + +#include "mu-util.h" +#include "mu-msg-fields.h" +#include "mu-msg-db.h" + +struct _MuMsgDb { + _MuMsgDb (const Xapian::Document& doc) : _doc (doc) {} + const Xapian::Document doc() const { return _doc; } +private: + const Xapian::Document& _doc; + +}; + + +MuMsgDb* +mu_msg_db_new (XapianDocument *doc, GError **err) +{ + g_return_val_if_fail (doc, NULL); + + try { + MuMsgDb *db = new MuMsgDb ((const Xapian::Document&)*doc); + return db; + + } MU_XAPIAN_CATCH_BLOCK_G_ERROR_RETURN(err, MU_ERROR_XAPIAN, NULL); + + return FALSE; +} + +void +mu_msg_db_destroy (MuMsgDb *self) +{ + try { + delete self; + + } MU_XAPIAN_CATCH_BLOCK; +} + + +gchar* +mu_msg_db_get_str_field (MuMsgDb *self, MuMsgFieldId mfid, gboolean *do_free) +{ + g_return_val_if_fail (self, NULL); + g_return_val_if_fail (mu_msg_field_id_is_valid(mfid), NULL); + g_return_val_if_fail (mu_msg_field_is_string(mfid), NULL); + + *do_free = TRUE; + + try { + const std::string s (self->doc().get_value(mfid)); + return s.empty() ? NULL : g_strdup (s.c_str()); + + } MU_XAPIAN_CATCH_BLOCK_RETURN(NULL); +} + + +gint64 +mu_msg_db_get_num_field (MuMsgDb *self, MuMsgFieldId mfid) +{ + g_return_val_if_fail (self, -1); + g_return_val_if_fail (mu_msg_field_id_is_valid(mfid), -1); + g_return_val_if_fail (mu_msg_field_is_numeric(mfid), -1); + + try { + const std::string s (self->doc().get_value(mfid)); + if (s.empty()) + return -1; + else + return static_cast(Xapian::sortable_unserialise(s)); + + } MU_XAPIAN_CATCH_BLOCK_RETURN(-1); + +} + + diff --git a/src/mu-msg-db.h b/src/mu-msg-db.h new file mode 100644 index 00000000..75cb861d --- /dev/null +++ b/src/mu-msg-db.h @@ -0,0 +1,87 @@ +/* +** Copyright (C) 2011 Dirk-Jan C. Binnema +** +** 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. +** +*/ + +#ifndef __MU_MSG_DB_H__ +#define __MU_MSG_DB_H__ + +#include + +G_BEGIN_DECLS + +struct _MuMsgDb; +typedef struct _MuMsgDb MuMsgDb; + +typedef gpointer XapianDocument; + +/** + * create a new MuMsgDb instance + * + * @param doc a Xapian::Document* (you'll need to cast the + * Xapian::Document* to XapianDocument*, because only C (not C++) is + * allowed in this header file. + * @param err receives error info, or NULL + * + * @return a new MuMsgDb instance (free with mu_msg_db_destroy), or + * NULL in case of error. + */ +MuMsgDb* mu_msg_db_new (XapianDocument *doc, GError **err); + + +/** + * destroy a MuMsgDb instance -- free all the resources. Note, after + * destroying, any strings returned from mu_msg_db_get_str_field with + * do_free==FALSE are no longer valid + * + * @param self a MuMsgDb instance + */ +void mu_msg_db_destroy (MuMsgDb *self); + + +/** + * get a string parameter from the msgdb + * + * @param self a MuMsgDb instance + * @param mfid a MuMsgFieldId for a string field + * @param do_free receives either TRUE or FALSE, where TRUE means that + * the caller owns the string, and has to free it (g_free) when done + * with it; FALSE means that the MuMsgDb owns the string, and it is + * only valid as long as the MuMsgDb is valid (ie., before + * mu_msg_db_destroy). + * + * @return a string for the given field (see do_free), or NULL in case of error + */ +gchar* mu_msg_db_get_str_field (MuMsgDb *self, MuMsgFieldId mfid, gboolean *do_free); + +/** + * + * get a numeric parameter from the msgdb + * + * @param self a MuMsgDb instance + * @param mfid a MuMsgFieldId for a numeric field + * + * @return the numerical value, or -1 in case of error. You'll need to + * cast this value to the actual type (e.g. time_t for MU_MSG_FIELD_ID_DATE) + */ +gint64 mu_msg_db_get_num_field (MuMsgDb *self, MuMsgFieldId mfid); + + + +G_END_DECLS + +#endif /*__MU_MSG_DB_H__*/