1 /*
2     Copyright © 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 #ifndef CHAT_HISTORY_H
21 #define CHAT_HISTORY_H
22 
23 #include "ichatlog.h"
24 #include "sessionchatlog.h"
25 #include "src/persistence/history.h"
26 
27 #include <QSet>
28 
29 class Settings;
30 
31 class ChatHistory : public IChatLog
32 {
33     Q_OBJECT
34 public:
35     ChatHistory(Friend& f_, History* history_, const ICoreIdHandler& coreIdHandler,
36                 const Settings& settings, IMessageDispatcher& messageDispatcher);
37     const ChatLogItem& at(ChatLogIdx idx) const override;
38     SearchResult searchForward(SearchPos startIdx, const QString& phrase,
39                                const ParameterSearch& parameter) const override;
40     SearchResult searchBackward(SearchPos startIdx, const QString& phrase,
41                                 const ParameterSearch& parameter) const override;
42     ChatLogIdx getFirstIdx() const override;
43     ChatLogIdx getNextIdx() const override;
44     std::vector<DateChatLogIdxPair> getDateIdxs(const QDate& startDate, size_t maxDates) const override;
45 
46 public slots:
47     void onFileUpdated(const ToxPk& sender, const ToxFile& file);
48     void onFileTransferRemotePausedUnpaused(const ToxPk& sender, const ToxFile& file, bool paused);
49     void onFileTransferBrokenUnbroken(const ToxPk& sender, const ToxFile& file, bool broken);
50 
51 private slots:
52     void onMessageReceived(const ToxPk& sender, const Message& message);
53     void onMessageSent(DispatchedMessageId id, const Message& message);
54     void onMessageComplete(DispatchedMessageId id);
55 
56 private:
57     void ensureIdxInSessionChatLog(ChatLogIdx idx) const;
58     void loadHistoryIntoSessionChatLog(ChatLogIdx start) const;
59     void dispatchUnsentMessages(IMessageDispatcher& messageDispatcher);
60     void handleDispatchedMessage(DispatchedMessageId dispatchId, RowId historyId);
61     void completeMessage(DispatchedMessageId id);
62     bool canUseHistory() const;
63     ChatLogIdx getInitialChatLogIdx() const;
64 
65     Friend& f;
66     History* history;
67     const Settings& settings;
68     const ICoreIdHandler& coreIdHandler;
69     mutable SessionChatLog sessionChatLog;
70 
71     // If a message completes before it's inserted into history it will end up
72     // in this set
73     QSet<DispatchedMessageId> completedMessages;
74 
75     // If a message is inserted into history before it gets a completion
76     // callback it will end up in this map
77     QMap<DispatchedMessageId, RowId> dispatchedMessageRowIdMap;
78 };
79 
80 #endif /*CHAT_HISTORY_H*/
81