2019-04-28 12:57:07 +02:00
|
|
|
/*
|
2020-05-17 08:45:47 +02:00
|
|
|
** Copyright (C) 2020 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
2019-04-28 12:57:07 +02: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.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __MU_CONTACTS_HH__
|
|
|
|
#define __MU_CONTACTS_HH__
|
|
|
|
|
2019-07-12 14:02:40 +02:00
|
|
|
#include <glib.h>
|
|
|
|
#include <time.h>
|
2019-04-28 12:57:07 +02:00
|
|
|
#include <memory>
|
|
|
|
#include <functional>
|
|
|
|
#include <chrono>
|
2019-05-25 09:36:16 +02:00
|
|
|
#include <string>
|
2019-04-28 12:57:07 +02:00
|
|
|
#include <time.h>
|
|
|
|
#include <inttypes.h>
|
2020-10-13 22:38:26 +02:00
|
|
|
#include <utils/mu-utils.hh>
|
2019-04-28 12:57:07 +02:00
|
|
|
|
|
|
|
namespace Mu {
|
|
|
|
|
|
|
|
/// Data-structure representing information about some contact.
|
|
|
|
|
|
|
|
struct ContactInfo {
|
|
|
|
/**
|
|
|
|
* Construct a new ContactInfo
|
|
|
|
*
|
|
|
|
* @param _full_address the full email address + name.
|
2019-11-06 16:13:39 +01:00
|
|
|
* @param _email email address
|
2019-04-28 12:57:07 +02:00
|
|
|
* @param _name name or empty
|
|
|
|
* @param _personal is this a personal contact?
|
|
|
|
* @param _last_seen when was this contact last seen?
|
|
|
|
* @param _freq how often was this contact seen?
|
|
|
|
*/
|
|
|
|
ContactInfo (const std::string& _full_address,
|
|
|
|
const std::string& _email,
|
|
|
|
const std::string& _name,
|
2020-10-13 22:38:26 +02:00
|
|
|
bool personal,
|
|
|
|
time_t _last_seen,
|
2020-10-18 10:58:32 +02:00
|
|
|
size_t freq=1);
|
2019-04-28 12:57:07 +02:00
|
|
|
|
|
|
|
std::string full_address; /**< Full name <email> */
|
|
|
|
std::string email; /**< email address */
|
|
|
|
std::string name; /**< name (or empty) */
|
2020-10-13 22:38:26 +02:00
|
|
|
bool personal{}; /**< is this a personal contact? */
|
|
|
|
time_t last_seen{}; /**< when was this contact last seen? */
|
|
|
|
std::size_t freq{}; /**< how often was this contact seen? */
|
2019-04-28 12:57:07 +02:00
|
|
|
|
2020-10-13 22:38:26 +02:00
|
|
|
int64_t tstamp{}; /**< Time-stamp, as per g_get_monotonic_time */
|
2019-04-28 12:57:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/// All contacts
|
|
|
|
class Contacts {
|
|
|
|
public:
|
|
|
|
/**
|
2019-07-28 13:12:06 +02:00
|
|
|
* Construct a new contacts objects
|
2019-04-28 12:57:07 +02:00
|
|
|
*
|
|
|
|
* @param serialized serialized contacts
|
2020-10-13 22:38:26 +02:00
|
|
|
* @param personal personal addresses
|
2019-04-28 12:57:07 +02:00
|
|
|
*/
|
2020-10-13 22:38:26 +02:00
|
|
|
Contacts (const std::string& serialized = "",
|
|
|
|
const StringVec& personal={});
|
2019-04-28 12:57:07 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* DTOR
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
~Contacts ();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a contact
|
|
|
|
*
|
|
|
|
* @param ci A contact-info object
|
2021-03-16 16:09:37 +01:00
|
|
|
*
|
|
|
|
* @return the inserted / updated / washed contact info. Note that
|
|
|
|
* this is return _as copy_ to make it thread-safe.
|
2019-04-28 12:57:07 +02:00
|
|
|
*/
|
2021-03-16 16:09:37 +01:00
|
|
|
const ContactInfo add(ContactInfo&& ci);
|
2019-04-28 12:57:07 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear all contacts
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void clear();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the number of contacts
|
|
|
|
*
|
|
|
|
|
|
|
|
* @return number of contacts
|
|
|
|
*/
|
|
|
|
std::size_t size() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Are there no contacts?
|
|
|
|
*
|
|
|
|
* @return true or false
|
|
|
|
*/
|
|
|
|
bool empty() const { return size() == 0; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the contacts, serialized.
|
|
|
|
*
|
|
|
|
* @return serialized contacts
|
|
|
|
*/
|
|
|
|
std::string serialize() const;
|
|
|
|
|
2020-10-13 22:38:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Does this look like a 'personal' address?
|
|
|
|
*
|
|
|
|
* @param addr some e-mail address
|
|
|
|
*
|
|
|
|
* @return true or false
|
|
|
|
*/
|
|
|
|
bool is_personal(const std::string& addr) const;
|
|
|
|
|
2019-04-28 12:57:07 +02:00
|
|
|
/**
|
|
|
|
* Find a contact based on the email address. This is not safe, since
|
|
|
|
* the returned ptr can be invalidated at any time; only for unit-tests.
|
|
|
|
*
|
|
|
|
* @param email email address
|
|
|
|
*
|
|
|
|
* @return contact info, or {} if not found
|
|
|
|
*/
|
|
|
|
const ContactInfo* _find (const std::string& email) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prototype for a callable that receives a contact
|
|
|
|
*
|
|
|
|
* @param contact some contact
|
|
|
|
*/
|
|
|
|
using EachContactFunc = std::function<void (const ContactInfo& contact_info)>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoke some callable for each contact, in order of rank.
|
|
|
|
*
|
|
|
|
* @param each_contact
|
|
|
|
*/
|
|
|
|
void for_each (const EachContactFunc& each_contact) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct Private;
|
|
|
|
std::unique_ptr<Private> priv_;
|
|
|
|
};
|
|
|
|
|
2019-07-12 14:02:40 +02:00
|
|
|
} // namespace Mu
|
|
|
|
|
2019-04-28 12:57:07 +02:00
|
|
|
#endif /* __MU_CONTACTS_HH__ */
|