2022-03-19 17:56:10 +01:00
|
|
|
/** Copyright (C) 2022 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
2022-03-19 17:55:08 +01:00
|
|
|
**
|
|
|
|
** 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.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
2022-03-19 17:56:10 +01:00
|
|
|
#ifndef MU_DOCUMENT_HH__
|
|
|
|
#define MU_DOCUMENT_HH__
|
2022-03-19 17:55:08 +01:00
|
|
|
|
|
|
|
#include <xapian.h>
|
|
|
|
#include <utility>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include "utils/mu-xapian-utils.hh"
|
|
|
|
|
2022-03-19 17:56:10 +01:00
|
|
|
#include "mu-fields.hh"
|
|
|
|
#include "mu-priority.hh"
|
|
|
|
#include "mu-flags.hh"
|
|
|
|
#include "mu-contact.hh"
|
2022-03-26 15:19:08 +01:00
|
|
|
#include <utils/mu-option.hh>
|
2022-05-05 00:22:14 +02:00
|
|
|
#include <utils/mu-sexp.hh>
|
2022-03-19 17:55:08 +01:00
|
|
|
|
|
|
|
namespace Mu {
|
|
|
|
|
|
|
|
/**
|
2022-03-19 17:56:10 +01:00
|
|
|
* A Document describes the information about a message that is
|
2022-03-19 17:55:08 +01:00
|
|
|
* or can be stored in the database.
|
|
|
|
*
|
|
|
|
*/
|
2022-03-19 17:56:10 +01:00
|
|
|
class Document {
|
2022-03-19 17:55:08 +01:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Construct a message for a new Xapian Document
|
|
|
|
*
|
|
|
|
*/
|
2022-03-19 17:56:10 +01:00
|
|
|
Document() {}
|
2022-03-19 17:55:08 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a message document based on on existing Xapian document.
|
|
|
|
*
|
|
|
|
* @param doc
|
|
|
|
*/
|
2022-03-19 17:56:10 +01:00
|
|
|
Document(const Xapian::Document& doc): xdoc_{doc} {}
|
2022-03-19 17:55:08 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a reference to the underlying Xapian document.
|
|
|
|
*
|
|
|
|
*/
|
2022-03-19 17:56:10 +01:00
|
|
|
const Xapian::Document& xapian_document() const { return xdoc_; }
|
|
|
|
|
2022-04-30 00:19:52 +02:00
|
|
|
/**
|
|
|
|
* Get the doc-id for this document
|
|
|
|
*
|
|
|
|
* @return the docid
|
|
|
|
*/
|
|
|
|
Xapian::docid docid() const { return xdoc_.get_docid(); }
|
|
|
|
|
2022-03-19 17:55:08 +01:00
|
|
|
/*
|
|
|
|
* updating a document with terms & values
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a string value to the document
|
|
|
|
*
|
|
|
|
* @param field_id field id
|
|
|
|
* @param val string value
|
|
|
|
*/
|
2022-03-19 17:56:10 +01:00
|
|
|
void add(Field::Id field_id, const std::string& val);
|
2022-03-19 17:55:08 +01:00
|
|
|
|
|
|
|
/**
|
2022-03-26 15:19:08 +01:00
|
|
|
* Add a string-vec value to the document, if non-empty
|
2022-03-19 17:55:08 +01:00
|
|
|
*
|
|
|
|
* @param field_id field id
|
|
|
|
* @param val string-vec value
|
|
|
|
*/
|
2022-03-19 17:56:10 +01:00
|
|
|
void add(Field::Id field_id, const std::vector<std::string>& vals);
|
2022-03-19 17:55:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-03-26 15:19:08 +01:00
|
|
|
* Add message-contacts to the document, if non-empty
|
2022-03-19 17:55:08 +01:00
|
|
|
*
|
|
|
|
* @param field_id field id
|
|
|
|
* @param contacts message contacts
|
|
|
|
*/
|
2022-03-19 17:56:10 +01:00
|
|
|
void add(Field::Id id, const Contacts& contacts);
|
2022-03-19 17:55:08 +01:00
|
|
|
|
2022-05-05 00:22:14 +02:00
|
|
|
/**
|
2022-05-21 16:41:21 +02:00
|
|
|
* Add some extra contacts with the given propname; this is useful for
|
|
|
|
* ":reply-to" and ":list-post" which don't have a Field::Id and are
|
|
|
|
* only present in the sexp, not in the terms/values
|
2022-05-05 00:22:14 +02:00
|
|
|
*
|
|
|
|
* @param propname property name (e.g.,. ":reply-to")
|
|
|
|
* @param contacts contacts for this property.
|
|
|
|
*/
|
|
|
|
void add_extra_contacts(const std::string& propname,
|
|
|
|
const Contacts& contacts);
|
2022-03-26 15:19:08 +01:00
|
|
|
|
2022-03-19 17:55:08 +01:00
|
|
|
/**
|
|
|
|
* Add an integer value to the document
|
|
|
|
*
|
|
|
|
* @param field_id field id
|
|
|
|
* @param val integer value
|
|
|
|
*/
|
2022-03-19 17:56:10 +01:00
|
|
|
void add(Field::Id field_id, int64_t val);
|
2022-03-19 17:55:08 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a message priority to the document
|
|
|
|
*
|
|
|
|
* @param prio priority
|
|
|
|
*/
|
2022-03-19 17:56:10 +01:00
|
|
|
void add(Priority prio);
|
2022-03-19 17:55:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-03-26 15:19:08 +01:00
|
|
|
* Add message flags to the document
|
2022-03-19 17:55:08 +01:00
|
|
|
*
|
|
|
|
* @param flags mesage flags.
|
|
|
|
*/
|
2022-03-19 17:56:10 +01:00
|
|
|
void add(Flags flags);
|
2022-03-19 17:55:08 +01:00
|
|
|
|
2022-05-05 00:22:14 +02:00
|
|
|
/**
|
|
|
|
* Remove values and terms for some field.
|
|
|
|
*
|
|
|
|
* @param field_id
|
|
|
|
*/
|
|
|
|
void remove(Field::Id field_id);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the cached sexp from the sexp_list_
|
|
|
|
*/
|
|
|
|
void update_cached_sexp();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the cached s-expression
|
|
|
|
*
|
|
|
|
* @return a string
|
|
|
|
*/
|
|
|
|
std::string cached_sexp() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the cached s-expressionl useful for changing
|
|
|
|
* it (call update_sexp_cache() when done)
|
|
|
|
*
|
|
|
|
* @return the cache s-expression
|
|
|
|
*/
|
|
|
|
Sexp::List& sexp_list();
|
|
|
|
|
2022-03-26 15:19:08 +01:00
|
|
|
/**
|
|
|
|
* Generically adds an optional value, if set, to the document
|
|
|
|
*
|
|
|
|
* @param id the field 0d
|
|
|
|
* @param an optional value
|
|
|
|
*/
|
|
|
|
template<typename T> void add(Field::Id id, const Option<T>& val) {
|
|
|
|
if (val)
|
|
|
|
add(id, val.value());
|
|
|
|
}
|
|
|
|
|
2022-03-19 17:55:08 +01:00
|
|
|
/*
|
|
|
|
* Retrieving values
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a message-field as a string-value
|
|
|
|
*
|
|
|
|
* @param field_id id of the field to get.
|
|
|
|
*
|
|
|
|
* @return a string (empty if not found)
|
|
|
|
*/
|
2022-03-19 17:56:10 +01:00
|
|
|
std::string string_value(Field::Id field_id) const noexcept {
|
2022-03-19 17:55:08 +01:00
|
|
|
return xapian_try([&]{
|
2022-03-19 17:56:10 +01:00
|
|
|
return xdoc_.get_value(field_from_id(field_id).value_no());
|
2022-03-19 17:55:08 +01:00
|
|
|
}, std::string{});
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Get a vec of string values.
|
|
|
|
*
|
|
|
|
* @param field_id id of the field to get
|
|
|
|
*
|
|
|
|
* @return a string list
|
|
|
|
*/
|
2022-03-19 17:56:10 +01:00
|
|
|
std::vector<std::string> string_vec_value(Field::Id field_id) const noexcept;
|
2022-03-19 17:55:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an integer value
|
|
|
|
*
|
|
|
|
* @param field_id id of the field to get
|
|
|
|
*
|
|
|
|
* @return an integer or 0 if not found.
|
|
|
|
*/
|
2022-03-19 17:56:10 +01:00
|
|
|
int64_t integer_value(Field::Id field_id) const noexcept;
|
2022-03-19 17:55:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get contacts
|
|
|
|
*
|
|
|
|
* @param field_id id of the contacts field to get
|
|
|
|
*
|
2022-04-28 21:53:31 +02:00
|
|
|
* @return a contacts list
|
2022-03-19 17:55:08 +01:00
|
|
|
*/
|
2022-03-19 17:56:10 +01:00
|
|
|
Contacts contacts_value(Field::Id id) const noexcept;
|
2022-03-19 17:55:08 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the priority
|
|
|
|
*
|
|
|
|
* @return the message priority
|
|
|
|
*/
|
2022-03-19 17:56:10 +01:00
|
|
|
Priority priority_value() const noexcept;
|
2022-03-19 17:55:08 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the message flags
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return flags
|
|
|
|
*/
|
2022-03-19 17:56:10 +01:00
|
|
|
Flags flags_value() const noexcept;
|
2022-03-19 17:55:08 +01:00
|
|
|
|
|
|
|
private:
|
2022-05-05 00:22:14 +02:00
|
|
|
Xapian::Document xdoc_;
|
|
|
|
Sexp::List sexp_list_;
|
|
|
|
|
2022-03-19 17:55:08 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namepace Mu
|
|
|
|
|
2022-03-19 17:56:10 +01:00
|
|
|
#endif /* MU_DOCUMENT_HH__ */
|