mu/lib/mu-store.cc

123 lines
2.6 KiB
C++
Raw Normal View History

2011-05-21 19:20:07 +02:00
/* -*-mode: c++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8-*- */
/*
** Copyright (C) 2008-2017 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.
**
*/
#if HAVE_CONFIG_H
#include "config.h"
#endif /*HAVE_CONFIG_H*/
#include <cstdio>
#include <xapian.h>
#include <cstring>
#include <stdexcept>
#include <unistd.h>
2011-08-29 22:35:12 +02:00
#include <errno.h>
#include "mu-store.h"
#include "mu-store-priv.hh" /* _MuStore */
#include "mu-msg.h"
#include "mu-msg-part.h"
2010-08-25 20:55:08 +02:00
#include "mu-store.h"
#include "mu-util.h"
2010-11-22 23:44:18 +01:00
#include "mu-str.h"
2011-07-20 23:38:28 +02:00
#include "mu-date.h"
2011-08-11 21:44:23 +02:00
#include "mu-flags.h"
#include "mu-contacts.hh"
2011-01-12 22:13:03 +01:00
2011-08-30 21:02:28 +02:00
MuStore*
mu_store_ref (MuStore *store)
{
2011-08-30 21:02:28 +02:00
g_return_val_if_fail (store, NULL);
store->ref();
return store;
}
2011-08-30 21:02:28 +02:00
MuStore*
mu_store_unref (MuStore *store)
{
g_return_val_if_fail (store, NULL);
if (store->unref() == 0) {
try { delete store; } MU_XAPIAN_CATCH_BLOCK;
}
return NULL;
}
2011-08-29 22:35:12 +02:00
static char*
xapian_get_metadata (const gchar *xpath, const gchar *key)
2011-01-12 22:13:03 +01:00
{
2011-08-29 22:35:12 +02:00
g_return_val_if_fail (xpath, NULL);
2011-01-12 22:13:03 +01:00
g_return_val_if_fail (key, NULL);
if (access(xpath, F_OK) != 0) {
2011-08-29 22:35:12 +02:00
g_warning ("cannot access %s: %s", xpath, strerror(errno));
return NULL;
}
try {
2011-08-29 22:35:12 +02:00
Xapian::Database db (xpath);
const std::string val(db.get_metadata (key));
return val.empty() ? NULL : g_strdup (val.c_str());
} MU_XAPIAN_CATCH_BLOCK;
2011-08-29 22:35:12 +02:00
return NULL;
}
2011-08-29 22:35:12 +02:00
char*
mu_store_database_version (const gchar *xpath)
{
2011-08-29 22:35:12 +02:00
g_return_val_if_fail (xpath, NULL);
2011-08-29 22:35:12 +02:00
return xapian_get_metadata (xpath, MU_STORE_VERSION_KEY);
}
2011-01-12 22:13:03 +01:00
gboolean
2011-08-29 22:35:12 +02:00
mu_store_database_is_locked (const gchar *xpath)
{
2011-08-29 22:35:12 +02:00
g_return_val_if_fail (xpath, FALSE);
try {
2011-08-29 22:35:12 +02:00
Xapian::WritableDatabase db (xpath, Xapian::DB_OPEN);
} catch (const Xapian::DatabaseLockError& xer) {
return TRUE;
} catch (const Xapian::Error &xer) {
g_warning ("%s: error: %s", __func__,
2011-08-29 22:35:12 +02:00
xer.get_msg().c_str());
}
2011-08-29 22:35:12 +02:00
return FALSE;
}
2012-06-19 16:59:16 +02:00
void
mu_store_set_my_addresses (MuStore *store, const char **my_addresses)
{
g_return_if_fail (store);
store->set_my_addresses (my_addresses);
}