summaryrefslogtreecommitdiff
path: root/lib/mu-contacts.hh
blob: 604b64a48dca2e5ff4d313f256d3cc9b98b5cf30 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
** Copyright (C) 2019 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.
**
*/

#ifndef __MU_CONTACTS_HH__
#define __MU_CONTACTS_HH__

#include <glib.h>
#include <time.h>

struct _MuContacts;
typedef struct _MuContacts MuContacts;

#ifdef __cplusplus

#include <memory>
#include <functional>
#include <chrono>
#include <string>
#include <time.h>
#include <inttypes.h>

namespace Mu {

/// Data-structure representing information about some contact.

struct ContactInfo {
        /**
         * Construct a new ContactInfo
         *
         * @param _full_address the full email address + name.
         * @param _email email address
         * @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?
         *
         * @return
         */
        ContactInfo (const std::string& _full_address,
                     const std::string& _email,
                     const std::string& _name,
                     bool _personal, time_t _last_seen, size_t _freq=1);

        std::string full_address; /**< Full name <email> */
        std::string email;        /**< email address */
        std::string name;         /**< name (or empty) */
        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? */

        int64_t      tstamp;      /**< Time-stamp, as per g_get_monotonic_time */
};

/// All contacts
class Contacts {
public:
        /**
         * Construct a new contacts objects
         *
         * @param serialized serialized contacts
         */
        Contacts (const std::string& serialized = "");

        /**
         * DTOR
         *
         */
        ~Contacts ();

        /**
         * Add a contact
         *
         * @param ci A contact-info object
         */
        void add(ContactInfo&& ci);

        /**
         * 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;

        /**
         * 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;

        /**
         * For C compatiblityy
         *
         * @return a MuContacts* referring to this.
         */
        const MuContacts* mu_contacts() const {
                return reinterpret_cast<const MuContacts*>(this);
        }



private:
        struct                   Private;
        std::unique_ptr<Private> priv_;
};

} // namespace Mu

#endif /*__cplusplus*/

G_BEGIN_DECLS


/**
 * return the number of contacts
 *
 * @param self a contacts object
 *
 * @return the number of contacts
 */
size_t mu_contacts_count (const MuContacts *self);

/**
 * Function called for mu_contacts_foreach; returns the e-mail address, name
 * (which may be NULL) , whether the message is 'personal', the timestamp for
 * the address (when it was last seen), and the frequency (in how many message
 * did this contact participate) and the tstamp (last modification)
 *
 */
typedef void (*MuContactsForeachFunc) (const char *full_address,
                                       const char *email, const char *name,
                                       gboolean personal,
                                       time_t last_seen, unsigned freq,
                                       gint64 tstamp, gpointer user_data);

/**
 * call a function for either each contact, or each contact satisfying
 * a regular expression,
 *
 * @param self contacts object
 * @param func callback function to be called for each
 * @param user_data user data to pass to the callback
 *
 * @return TRUE if the function succeeded, or FALSE if the provide regular
 * expression was invalid (and not NULL)
 */
gboolean mu_contacts_foreach (const MuContacts *self,
                              MuContactsForeachFunc func,
                              gpointer user_data);

G_END_DECLS

#endif /* __MU_CONTACTS_HH__ */