main-db.h 6.93 KB
Newer Older
Ronan's avatar
Ronan committed
1
/*
2
 * main-db.h
3
 * Copyright (C) 2010-2018 Belledonne Communications SARL
Ronan's avatar
Ronan committed
4
 *
Ghislain MARY's avatar
Ghislain MARY committed
5 6 7 8
 * 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 2
 * of the License, or (at your option) any later version.
Ronan's avatar
Ronan committed
9 10 11 12 13 14 15
 *
 * 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
Ghislain MARY's avatar
Ghislain MARY committed
16 17
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Ronan's avatar
Ronan committed
18 19
 */

20 21
#ifndef _L_MAIN_DB_H_
#define _L_MAIN_DB_H_
Ronan's avatar
Ronan committed
22

23
#include <functional>
24 25
#include <list>

26 27
#include "linphone/utils/enum-mask.h"

Ronan's avatar
Ronan committed
28
#include "abstract/abstract-db.h"
29
#include "chat/chat-message/chat-message.h"
30
#include "chat/chat-room/chat-room-id.h"
Ronan's avatar
Ronan committed
31
#include "core/core-accessor.h"
Ronan's avatar
Ronan committed
32 33 34 35 36

// =============================================================================

LINPHONE_BEGIN_NAMESPACE

37
class AbstractChatRoom;
38
class ChatMessage;
39
class Core;
Ronan's avatar
Ronan committed
40
class EventLog;
41
class MainDbKey;
42
class MainDbPrivate;
43
class ParticipantDevice;
44

45
class LINPHONE_INTERNAL_PUBLIC MainDb : public AbstractDb, public CoreAccessor {
46
	template<typename Function>
47
	friend class DbTransaction;
48

49
	friend class MainDbChatMessageKey;
50 51
	friend class MainDbEventKey;

Ronan's avatar
Ronan committed
52
public:
53 54
	enum Filter {
		NoFilter = 0x0,
55 56
		ConferenceCallFilter = 0x1,
		ConferenceChatMessageFilter = 0x2,
57
		ConferenceInfoFilter = 0x4,
58
		ConferenceInfoNoDeviceFilter = 0x8
59 60
	};

61
	typedef EnumMask<Filter> FilterMask;
62

63 64 65 66 67 68 69 70 71
	struct ParticipantState {
		ParticipantState (const IdentityAddress &address, ChatMessage::State state, time_t timestamp)
			: address(address), state(state), timestamp(timestamp) {}

		IdentityAddress address;
		ChatMessage::State state = ChatMessage::State::Idle;
		time_t timestamp = 0;
	};

Ronan's avatar
Ronan committed
72
	MainDb (const std::shared_ptr<Core> &core);
Ronan's avatar
Ronan committed
73

74
	// ---------------------------------------------------------------------------
75
	// Generic.
76 77
	// ---------------------------------------------------------------------------

78
	bool addEvent (const std::shared_ptr<EventLog> &eventLog);
79
	bool updateEvent (const std::shared_ptr<EventLog> &eventLog);
80
	static bool deleteEvent (const std::shared_ptr<const EventLog> &eventLog);
81
	int getEventCount (FilterMask mask = NoFilter) const;
82

83 84
	static std::shared_ptr<EventLog> getEventFromKey (const MainDbKey &dbKey);

85 86 87 88
	// ---------------------------------------------------------------------------
	// Conference notified events.
	// ---------------------------------------------------------------------------

89
	std::list<std::shared_ptr<EventLog>> getConferenceNotifiedEvents (
90
		const ChatRoomId &chatRoomId,
91
		unsigned int lastNotifyId
Ronan's avatar
Ronan committed
92
	) const;
93

94 95 96 97
	// ---------------------------------------------------------------------------
	// Conference chat message events.
	// ---------------------------------------------------------------------------

98 99
	using ParticipantStateRetrievalFunc = std::function<std::list<ParticipantState>(const std::shared_ptr<EventLog> &eventLog)>;

100 101
	int getChatMessageCount (const ChatRoomId &chatRoomId = ChatRoomId()) const;
	int getUnreadChatMessageCount (const ChatRoomId &chatRoomId = ChatRoomId()) const;
102 103
	void markChatMessagesAsRead (const ChatRoomId &chatRoomId) const;
	std::list<std::shared_ptr<ChatMessage>> getUnreadChatMessages (const ChatRoomId &chatRoomId) const;
104

105 106 107
	std::list<ParticipantState> getChatMessageParticipantsByImdnState (
		const std::shared_ptr<EventLog> &eventLog,
		ChatMessage::State state
108
	) const;
109 110 111 112 113 114 115 116
	std::list<ChatMessage::State> getChatMessageParticipantStates (const std::shared_ptr<EventLog> &eventLog) const;
	ChatMessage::State getChatMessageParticipantState (
		const std::shared_ptr<EventLog> &eventLog,
		const IdentityAddress &participantAddress
	) const;
	void setChatMessageParticipantState (
		const std::shared_ptr<EventLog> &eventLog,
		const IdentityAddress &participantAddress,
117 118
		ChatMessage::State state,
		time_t stateChangeTime
119 120
	);

121 122 123 124 125 126
	std::shared_ptr<ChatMessage> getLastChatMessage (const ChatRoomId &chatRoomId) const;

	std::list<std::shared_ptr<ChatMessage>> findChatMessages (
		const ChatRoomId &chatRoomId,
		const std::string &imdnMessageId
	) const;
127

128 129 130 131
	std::list<std::shared_ptr<ChatMessage>> findChatMessagesToBeNotifiedAsDelivered (
		const ChatRoomId &chatRoomId
	) const;

132 133 134 135
	// ---------------------------------------------------------------------------
	// Conference events.
	// ---------------------------------------------------------------------------

136
	std::list<std::shared_ptr<EventLog>> getHistory (
137
		const ChatRoomId &chatRoomId,
138 139 140
		int nLast,
		FilterMask mask = NoFilter
	) const;
141
	std::list<std::shared_ptr<EventLog>> getHistoryRange (
142
		const ChatRoomId &chatRoomId,
143 144 145 146
		int begin,
		int end,
		FilterMask mask = NoFilter
	) const;
147 148 149

	int getHistorySize (const ChatRoomId &chatRoomId, FilterMask mask = NoFilter) const;

150
	void cleanHistory (const ChatRoomId &chatRoomId, FilterMask mask = NoFilter);
Ronan's avatar
Ronan committed
151

152 153 154 155 156 157
	// ---------------------------------------------------------------------------
	// Chat messages.
	// ---------------------------------------------------------------------------

	void loadChatMessageContents (const std::shared_ptr<ChatMessage> &chatMessage);

158 159 160 161
	// ---------------------------------------------------------------------------
	// Chat rooms.
	// ---------------------------------------------------------------------------

162
	std::list<std::shared_ptr<AbstractChatRoom>> getChatRooms () const;
163
	void insertChatRoom (const std::shared_ptr<AbstractChatRoom> &chatRoom, unsigned int notifyId = 0);
164
	void deleteChatRoom (const ChatRoomId &chatRoomId);
165
	void enableChatRoomMigration (const ChatRoomId &chatRoomId, bool enable);
166

167
	void migrateBasicToClientGroupChatRoom (
168 169
		const std::shared_ptr<AbstractChatRoom> &basicChatRoom,
		const std::shared_ptr<AbstractChatRoom> &clientGroupChatRoom
170 171
	);

172 173 174 175
	IdentityAddress findMissingOneToOneConferenceChatRoomParticipantAddress (
		const std::shared_ptr<AbstractChatRoom> &chatRoom,
		const IdentityAddress &presentParticipantAddr
	);
176 177 178 179
	IdentityAddress findOneToOneConferenceChatRoomAddress (
		const IdentityAddress &participantA,
		const IdentityAddress &participantB
	) const;
180 181
	void insertOneToOneConferenceChatRoom (const std::shared_ptr<AbstractChatRoom> &chatRoom);

182 183 184 185
	void updateChatRoomParticipantDevice (
		const std::shared_ptr<AbstractChatRoom> &chatRoom,
		const std::shared_ptr<ParticipantDevice> &device
	);
186

187 188 189 190
	// ---------------------------------------------------------------------------
	// Other.
	// ---------------------------------------------------------------------------

191
	// Import legacy calls/messages from old db.
192 193
	bool import (Backend backend, const std::string &parameters) override;

Ronan's avatar
Ronan committed
194 195 196 197
protected:
	void init () override;

private:
198 199
	L_DECLARE_PRIVATE(MainDb);
	L_DISABLE_COPY(MainDb);
Ronan's avatar
Ronan committed
200 201 202 203
};

LINPHONE_END_NAMESPACE

204
#endif // ifndef _L_MAIN_DB_H_