qTox  Version: nightly | Commit: bc751c8e1cac455f9690654fcfe0f560d2d7dfdd
history.h
Go to the documentation of this file.
1 /*
2  Copyright © 2015-2019 by The qTox Project Contributors
3 
4  This file is part of qTox, a Qt-based graphical interface for Tox.
5 
6  qTox is libre software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  qTox is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with qTox. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #pragma once
21 
22 #include <QDateTime>
23 #include <QHash>
24 #include <QPointer>
25 #include <QVector>
26 
27 #include <cassert>
28 #include <cstdint>
29 #include <tox/toxencryptsave.h>
30 
31 #include "src/core/extension.h"
32 #include "src/core/toxfile.h"
33 #include "src/core/toxpk.h"
37 #include "src/widget/searchtypes.h"
38 
39 class Profile;
40 class HistoryKeeper;
41 
43 {
44  message,
45  file,
46  system,
47 };
48 
50 {
51 public:
53  : data(std::make_shared<QString>(std::move(message)))
55  {}
56 
58  : data(std::make_shared<ToxFile>(std::move(file)))
60  {}
61 
63  : data(std::make_shared<SystemMessage>(std::move(systemMessage)))
65  {}
66 
68  {
69  return type;
70  }
71 
72  QString& asMessage()
73  {
75  return *static_cast<QString*>(data.get());
76  }
77 
79  {
81  return *static_cast<ToxFile*>(data.get());
82  }
83 
85  {
87  return *static_cast<SystemMessage*>(data.get());
88  }
89 
90  const QString& asMessage() const
91  {
93  return *static_cast<QString*>(data.get());
94  }
95 
96  const ToxFile& asFile() const
97  {
99  return *static_cast<ToxFile*>(data.get());
100  }
101 
103  {
105  return *static_cast<SystemMessage*>(data.get());
106  }
107 
108 private:
109  // Not really shared but shared_ptr has support for shared_ptr<void>
110  std::shared_ptr<void> data;
112 };
113 
115 {
117 
120  QString fileId;
121  QString fileName;
122  QString filePath;
123  int64_t size;
125 };
127 
128 enum class MessageState
129 {
130  complete,
131  pending,
132  broken
133 };
134 
135 class History : public QObject, public std::enable_shared_from_this<History>
136 {
137  Q_OBJECT
138 public:
139  struct HistMessage
140  {
142  QString sender, QString message)
143  : chat{chat}
144  , sender{sender}
145  , dispName{dispName}
147  , id{id}
148  , state{state}
150  , content(std::move(message))
151  {}
152 
153  HistMessage(RowId id, MessageState state, QDateTime timestamp, QString chat, QString dispName,
154  QString sender, ToxFile file)
155  : chat{chat}
156  , sender{sender}
157  , dispName{dispName}
159  , id{id}
160  , state{state}
161  , content(std::move(file))
162  {}
163 
164  HistMessage(RowId id, QDateTime timestamp, QString chat, SystemMessage systemMessage)
165  : chat{chat}
167  , id{id}
169  , content(std::move(systemMessage))
170  {}
171 
172  QString chat;
173  QString sender;
174  QString dispName;
175  QDateTime timestamp;
180  };
181 
182  struct DateIdx
183  {
184  QDate date;
186  };
187 
188 public:
189  explicit History(std::shared_ptr<RawDatabase> db);
190  ~History();
191 
192  bool isValid();
193 
194  bool historyExists(const ToxPk& friendPk);
195 
196  void eraseHistory();
197  void removeFriendHistory(const ToxPk& friendPk);
198  void addNewMessage(const ToxPk& friendPk, const QString& message, const ToxPk& sender,
199  const QDateTime& time, bool isDelivered, ExtensionSet extensions,
200  QString dispName, const std::function<void(RowId)>& insertIdCallback = {});
201 
202  void addNewFileMessage(const ToxPk& friendPk, const QString& fileId,
203  const QString& fileName, const QString& filePath, int64_t size,
204  const ToxPk& sender, const QDateTime& time, QString const& dispName);
205 
206  void addNewSystemMessage(const ToxPk& friendPk, const SystemMessage& systemMessage);
207 
208  void setFileFinished(const QString& fileId, bool success, const QString& filePath, const QByteArray& fileHash);
209  size_t getNumMessagesForFriend(const ToxPk& friendPk);
210  size_t getNumMessagesForFriendBeforeDate(const ToxPk& friendPk, const QDateTime& date);
211  QList<HistMessage> getMessagesForFriend(const ToxPk& friendPk, size_t firstIdx, size_t lastIdx);
213  QDateTime getDateWhereFindPhrase(const ToxPk& friendPk, const QDateTime& from, QString phrase,
214  const ParameterSearch& parameter);
216  const QDate& from, size_t maxNum);
217 
218  void markAsDelivered(RowId messageId);
219  void markAsBroken(RowId messageId, BrokenMessageReason reason);
220 
221 signals:
222  void fileInserted(RowId dbId, QString fileId);
223 
224 private slots:
225  void onFileInserted(RowId dbId, QString fileId);
226 
227 private:
228  QVector<RawDatabase::Query>
229  generateNewFileTransferQueries(const ToxPk& friendPk, const ToxPk& sender, const QDateTime& time,
230  const QString& dispName, const FileDbInsertionData& insertionData);
231  bool historyAccessBlocked();
232  static RawDatabase::Query generateFileFinished(RowId fileId, bool success,
233  const QString& filePath, const QByteArray& fileHash);
234 
235  int64_t getPeerId(ToxPk const& pk);
236 
237  std::shared_ptr<RawDatabase> db;
238 
239 
240  struct FileInfo
241  {
242  bool finished = false;
243  bool success = false;
244  QString filePath;
245  QByteArray fileHash;
247  };
248 
249  // This needs to be a shared pointer to avoid callback lifetime issues
251 };
History::fileInserted
void fileInserted(RowId dbId, QString fileId)
History::setFileFinished
void setFileFinished(const QString &fileId, bool success, const QString &filePath, const QByteArray &fileHash)
Definition: history.cpp:989
systemmessage.h
RawDatabase::Query
Definition: rawdatabase.h:57
History::isValid
bool isValid()
Checks if the database was opened successfully.
Definition: history.cpp:749
History::~History
~History()
Definition: history.cpp:734
FileDbInsertionData::fileId
QString fileId
Definition: history.h:120
Q_DECLARE_METATYPE
Q_DECLARE_METATYPE(ExtendedReceiptNum)
toxfile.h
HistMessageContent::HistMessageContent
HistMessageContent(SystemMessage systemMessage)
Definition: history.h:62
HistMessageContent::asMessage
QString & asMessage()
Definition: history.h:72
Profile
Handles all qTox internal paths.
Definition: profile.h:42
HistMessageContent::HistMessageContent
HistMessageContent(ToxFile file)
Definition: history.h:57
History::HistMessage::state
MessageState state
Definition: history.h:177
History::markAsBroken
void markAsBroken(RowId messageId, BrokenMessageReason reason)
Definition: history.cpp:1402
History::HistMessage::HistMessage
HistMessage(RowId id, QDateTime timestamp, QString chat, SystemMessage systemMessage)
Definition: history.h:164
ParameterSearch
Definition: searchtypes.h:47
History::generateFileFinished
static RawDatabase::Query generateFileFinished(RowId fileId, bool success, const QString &filePath, const QByteArray &fileHash)
Definition: history.cpp:895
RowId
NamedType< int64_t, struct RowIdTag, Orderable > RowId
Definition: rawdatabase.h:49
History::eraseHistory
void eraseHistory()
Erases all the chat history from the database.
Definition: history.cpp:771
HistMessageContentType::file
@ file
searchtypes.h
HistMessageContent::asMessage
const QString & asMessage() const
Definition: history.h:90
FileDbInsertionData
Definition: history.h:114
History::HistMessage::chat
QString chat
Definition: history.h:172
History::removeFriendHistory
void removeFriendHistory(const ToxPk &friendPk)
Erases the chat history with one friend.
Definition: history.cpp:792
History::HistMessage::HistMessage
HistMessage(RowId id, MessageState state, ExtensionSet extensionSet, QDateTime timestamp, QString chat, QString dispName, QString sender, QString message)
Definition: history.h:141
brokenmessagereason.h
History::DateIdx::numMessagesIn
size_t numMessagesIn
Definition: history.h:185
SystemMessage
Definition: systemmessage.h:47
QList
Definition: friendlist.h:25
toxpk.h
History::FileInfo::success
bool success
Definition: history.h:243
History::historyExists
bool historyExists(const ToxPk &friendPk)
Checks if a friend has chat history.
Definition: history.cpp:759
HistMessageContent::getType
HistMessageContentType getType() const
Definition: history.h:67
History::HistMessage::extensionSet
ExtensionSet extensionSet
Definition: history.h:178
HistMessageContentType::system
@ system
History::historyAccessBlocked
bool historyAccessBlocked()
Determines if history access should be blocked.
Definition: history.cpp:1387
History::addNewSystemMessage
void addNewSystemMessage(const ToxPk &friendPk, const SystemMessage &systemMessage)
Definition: history.cpp:957
HistMessageContentType
HistMessageContentType
Definition: history.h:42
History::markAsDelivered
void markAsDelivered(RowId messageId)
Marks a message as delivered. Removing message from the faux-offline pending messages list.
Definition: history.cpp:1374
History::db
std::shared_ptr< RawDatabase > db
Definition: history.h:237
History::onFileInserted
void onFileInserted(RowId dbId, QString fileId)
Definition: history.cpp:833
HistMessageContent::HistMessageContent
HistMessageContent(QString message)
Definition: history.h:52
FileDbInsertionData::FileDbInsertionData
FileDbInsertionData()
Definition: history.cpp:700
FileDbInsertionData::size
int64_t size
Definition: history.h:123
History::getNumMessagesForFriendBeforeDate
size_t getNumMessagesForFriendBeforeDate(const ToxPk &friendPk, const QDateTime &date)
Definition: history.cpp:1018
History::getUndeliveredMessagesForFriend
QList< HistMessage > getUndeliveredMessagesForFriend(const ToxPk &friendPk)
Definition: history.cpp:1158
HistMessageContent::asSystemMessage
const SystemMessage & asSystemMessage() const
Definition: history.h:102
HistMessageContentType::message
@ message
ToxPk
This class represents a Tox Public Key, which is a part of Tox ID.
Definition: toxpk.h:26
extension.h
History::FileInfo::filePath
QString filePath
Definition: history.h:244
History::getNumMessagesForFriendBeforeDateBoundaries
QList< DateIdx > getNumMessagesForFriendBeforeDateBoundaries(const ToxPk &friendPk, const QDate &from, size_t maxNum)
Gets date boundaries in conversation with friendPk. History doesn't model conversation indexes,...
Definition: history.cpp:1313
HistMessageContent::type
HistMessageContentType type
Definition: history.h:111
History::FileInfo::fileHash
QByteArray fileHash
Definition: history.h:245
History::getMessagesForFriend
QList< HistMessage > getMessagesForFriend(const ToxPk &friendPk, size_t firstIdx, size_t lastIdx)
Definition: history.cpp:1046
History::HistMessage::HistMessage
HistMessage(RowId id, MessageState state, QDateTime timestamp, QString chat, QString dispName, QString sender, ToxFile file)
Definition: history.h:153
QHash
Definition: friendlist.h:27
History::DateIdx
Definition: history.h:182
History::History
History(std::shared_ptr< RawDatabase > db)
Prepares the database to work with the history.
Definition: history.cpp:710
BrokenMessageReason
BrokenMessageReason
Definition: brokenmessagereason.h:23
History::FileInfo
Definition: history.h:240
History::FileInfo::finished
bool finished
Definition: history.h:242
ExtensionSet
std::bitset< ExtensionType::max > ExtensionSet
Definition: extension.h:32
History
Interacts with the profile database to save the chat history.
Definition: history.h:135
History::fileInfos
QHash< QString, FileInfo > fileInfos
Definition: history.h:250
History::addNewFileMessage
void addNewFileMessage(const ToxPk &friendPk, const QString &fileId, const QString &fileName, const QString &filePath, int64_t size, const ToxPk &sender, const QDateTime &time, QString const &dispName)
Definition: history.cpp:915
HistMessageContent
Definition: history.h:49
History::getNumMessagesForFriend
size_t getNumMessagesForFriend(const ToxPk &friendPk)
Definition: history.cpp:1009
History::HistMessage::content
HistMessageContent content
Definition: history.h:179
MessageState::complete
@ complete
HistMessageContent::asFile
ToxFile & asFile()
Definition: history.h:78
FileDbInsertionData::filePath
QString filePath
Definition: history.h:122
History::FileInfo::fileId
RowId fileId
Definition: history.h:246
MessageState
MessageState
Definition: history.h:128
History::addNewMessage
void addNewMessage(const ToxPk &friendPk, const QString &message, const ToxPk &sender, const QDateTime &time, bool isDelivered, ExtensionSet extensions, QString dispName, const std::function< void(RowId)> &insertIdCallback={})
Saves a chat message in the database.
Definition: history.cpp:977
HistMessageContent::data
std::shared_ptr< void > data
Definition: history.h:110
FileDbInsertionData::fileName
QString fileName
Definition: history.h:121
FileDbInsertionData::historyId
RowId historyId
Definition: history.h:118
rawdatabase.h
ToxFile
Definition: toxfile.h:32
HistMessageContent::asSystemMessage
SystemMessage & asSystemMessage()
Definition: history.h:84
MessageState::broken
@ broken
History::getDateWhereFindPhrase
QDateTime getDateWhereFindPhrase(const ToxPk &friendPk, const QDateTime &from, QString phrase, const ParameterSearch &parameter)
Search phrase in chat messages.
Definition: history.cpp:1211
History::HistMessage::sender
QString sender
Definition: history.h:173
HistMessageContent::asFile
const ToxFile & asFile() const
Definition: history.h:96
History::HistMessage::dispName
QString dispName
Definition: history.h:174
History::HistMessage::id
RowId id
Definition: history.h:176
FileDbInsertionData::direction
int direction
Definition: history.h:124
FileDbInsertionData::friendPk
ToxPk friendPk
Definition: history.h:119
History::HistMessage::timestamp
QDateTime timestamp
Definition: history.h:175
History::generateNewFileTransferQueries
QVector< RawDatabase::Query > generateNewFileTransferQueries(const ToxPk &friendPk, const ToxPk &sender, const QDateTime &time, const QString &dispName, const FileDbInsertionData &insertionData)
Definition: history.cpp:847
History::HistMessage
Definition: history.h:139
History::getPeerId
int64_t getPeerId(ToxPk const &pk)
MessageState::pending
@ pending
History::DateIdx::date
QDate date
Definition: history.h:184