summaryrefslogtreecommitdiff
path: root/lib/mu-store.cc
blob: 22b7c92196388a2cb4100c8f03bf8798431e11ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* -*-mode: c++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8-*- */
/*
** Copyright (C) 2008-2013 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>

#include <errno.h>

#include "mu-store.h"
#include "mu-store-priv.hh" /* _MuStore */


#include "mu-msg.h"
#include "mu-msg-part.h"
#include "mu-store.h"
#include "mu-util.h"
#include "mu-str.h"
#include "mu-date.h"
#include "mu-flags.h"
#include "mu-contacts.h"



MuStore*
mu_store_ref (MuStore *store)
{
	g_return_val_if_fail (store, NULL);
	store->ref();
	return store;
}

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;
}




static char*
xapian_get_metadata (const gchar *xpath, const gchar *key)
{
	g_return_val_if_fail (xpath, NULL);
	g_return_val_if_fail (key, NULL);

	if (!access(xpath, F_OK) == 0) {
		g_warning ("cannot access %s: %s", xpath, strerror(errno));
		return NULL;
	}

	try {
		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;

	return NULL;
}

char*
mu_store_database_version (const gchar *xpath)
{
	g_return_val_if_fail (xpath, NULL);

	return xapian_get_metadata (xpath, MU_STORE_VERSION_KEY);
}




gboolean
mu_store_database_is_locked (const gchar *xpath)
{
	g_return_val_if_fail (xpath, FALSE);

	try {
		Xapian::WritableDatabase db (xpath, Xapian::DB_OPEN);
	} catch (const Xapian::DatabaseLockError& xer) {
		return TRUE;
	} catch (const Xapian::Error &xer) {
		g_warning ("%s: error: %s", __FUNCTION__,
			   xer.get_msg().c_str());
	}

	return FALSE;
}


void
mu_store_set_my_addresses (MuStore *store, const char **my_addresses)
{
	g_return_if_fail (store);

	store->set_my_addresses (my_addresses);
}