summaryrefslogtreecommitdiff
path: root/lib/mu-msg-doc.cc
blob: 249986b3cb32ae745d70b0c58a004e4d233d4203 (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
/*
** 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 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 <stdlib.h>
#include <iostream>
#include <string.h>
#include <errno.h>
#include <xapian.h>

#include "mu-util.h"
#include "mu-msg-fields.h"
#include "mu-msg-doc.h"
#include "mu-str.h"
#include "mu-date.h"

struct _MuMsgDoc {

	_MuMsgDoc (Xapian::Document *doc): _doc (doc) { }
	~_MuMsgDoc () {	delete _doc; }
	const Xapian::Document doc() const { return *_doc; }
private:
	Xapian::Document *_doc;
};


MuMsgDoc*
mu_msg_doc_new (XapianDocument *doc, GError **err)
{
	g_return_val_if_fail (doc, NULL);

	try {
		return new MuMsgDoc ((Xapian::Document*)doc);

	} MU_XAPIAN_CATCH_BLOCK_G_ERROR_RETURN(err, MU_ERROR_XAPIAN, NULL);

	return FALSE;
}

void
mu_msg_doc_destroy (MuMsgDoc *self)
{
	try {
		delete self;

	} MU_XAPIAN_CATCH_BLOCK;
}


gchar*
mu_msg_doc_get_str_field (MuMsgDoc *self, MuMsgFieldId mfid)
{
	g_return_val_if_fail (self, NULL);
	g_return_val_if_fail (mu_msg_field_id_is_valid(mfid), NULL);

	// disable this check:
	//    g_return_val_if_fail (mu_msg_field_is_string(mfid), NULL);
	// because it's useful to get numerical field as strings,
	// for example when sorting (which is much faster if don't
	// have to convert to numbers first, esp. when it's a date
	// time_t)

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


GSList*
mu_msg_doc_get_str_list_field (MuMsgDoc *self, MuMsgFieldId mfid)
{
	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_list(mfid), NULL);

	try {
		/* return a comma-separated string as a GSList */
		const std::string s (self->doc().get_value(mfid));
		return s.empty() ? NULL : mu_str_to_list(s.c_str(),',',TRUE);

	} MU_XAPIAN_CATCH_BLOCK_RETURN(NULL);
}


gint64
mu_msg_doc_get_num_field (MuMsgDoc *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);

	/* date is a special case, because we store dates as
	 * strings */
	try {
		const std::string s (self->doc().get_value(mfid));
		if (s.empty())
			return 0;
		else if (mfid == MU_MSG_FIELD_ID_DATE) {
			time_t t;
			t = mu_date_str_to_time_t (s.c_str(), FALSE/*utc*/);
			return static_cast<gint64>(t);
		} else {
			return static_cast<gint64>
				(Xapian::sortable_unserialise(s));
		}

	} MU_XAPIAN_CATCH_BLOCK_RETURN(-1);
}