qTox  Version: nightly | Commit: bc751c8e1cac455f9690654fcfe0f560d2d7dfdd
ichatlog.h
Go to the documentation of this file.
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 #pragma once
21 
22 #include "message.h"
23 #include "src/core/core.h"
24 #include "src/core/toxfile.h"
25 #include "src/core/toxpk.h"
26 #include "src/friendlist.h"
27 #include "src/grouplist.h"
28 #include "src/model/chatlogitem.h"
29 #include "src/model/friend.h"
30 #include "src/model/group.h"
32 #include "src/widget/searchtypes.h"
33 #include "util/strongtype.h"
34 
35 #include <cassert>
36 
37 using ChatLogIdx =
38  NamedType<size_t, struct ChatLogIdxTag, Orderable, UnderlyingAddable, UnitlessDifferencable, Incrementable>;
40 
41 struct SearchPos
42 {
43  // Index to the chat log item we want
45  // Number of matches we've had. This is always number of matches from the
46  // start even if we're searching backwards.
47  size_t numMatches;
48 
49  bool operator==(const SearchPos& other) const
50  {
51  return tie() == other.tie();
52  }
53 
54  bool operator!=(const SearchPos& other) const
55  {
56  return tie() != other.tie();
57  }
58 
59  bool operator<(const SearchPos& other) const
60  {
61  return tie() < other.tie();
62  }
63 
64  std::tuple<ChatLogIdx, size_t> tie() const
65  {
66  return std::tie(logIdx, numMatches);
67  }
68 };
69 
71 {
72  bool found;
74  size_t start;
75  size_t len;
76 
77  // This is unfortunately needed to shoehorn our API into the highlighting
78  // API of above classes. They expect to re-search the same thing we did
79  // for some reason
80  QRegularExpression exp;
81 };
82 
83 class IChatLog : public QObject
84 {
85  Q_OBJECT
86 public:
87  virtual ~IChatLog() = default;
88 
95  virtual const ChatLogItem& at(ChatLogIdx idx) const = 0;
96 
103  virtual SearchResult searchForward(SearchPos startIdx, const QString& phrase,
104  const ParameterSearch& parameter) const = 0;
105 
112  virtual SearchResult searchBackward(SearchPos startIdx, const QString& phrase,
113  const ParameterSearch& parameter) const = 0;
114 
119  virtual ChatLogIdx getFirstIdx() const = 0;
120 
124  virtual ChatLogIdx getNextIdx() const = 0;
125 
127  {
128  QDate date;
130  };
131 
137  virtual std::vector<DateChatLogIdxPair> getDateIdxs(const QDate& startDate,
138  size_t maxDates) const = 0;
139 
144  virtual void addSystemMessage(const SystemMessage& message) = 0;
145 
146 signals:
147  void itemUpdated(ChatLogIdx idx);
148 };
SearchResult::pos
SearchPos pos
Definition: ichatlog.h:73
SearchResult::len
size_t len
Definition: ichatlog.h:75
systemmessage.h
ChatLogItem
Definition: chatlogitem.h:42
SearchPos::operator<
bool operator<(const SearchPos &other) const
Definition: ichatlog.h:59
IChatLog::DateChatLogIdxPair::idx
ChatLogIdx idx
Definition: ichatlog.h:129
friend.h
Q_DECLARE_METATYPE
Q_DECLARE_METATYPE(ExtendedReceiptNum)
toxfile.h
friendlist.h
SearchPos::tie
std::tuple< ChatLogIdx, size_t > tie() const
Definition: ichatlog.h:64
IChatLog::addSystemMessage
virtual void addSystemMessage(const SystemMessage &message)=0
Inserts a system message at the end of the chat.
ParameterSearch
Definition: searchtypes.h:47
group.h
searchtypes.h
IChatLog::searchBackward
virtual SearchResult searchBackward(SearchPos startIdx, const QString &phrase, const ParameterSearch &parameter) const =0
searches backwards through the chat log until phrase is found according to parameter
IChatLog::DateChatLogIdxPair::date
QDate date
Definition: ichatlog.h:128
SystemMessage
Definition: systemmessage.h:47
toxpk.h
SearchResult::start
size_t start
Definition: ichatlog.h:74
IChatLog::at
virtual const ChatLogItem & at(ChatLogIdx idx) const =0
Returns reference to item at idx.
IChatLog::getFirstIdx
virtual ChatLogIdx getFirstIdx() const =0
The underlying chat log instance may not want to start at 0.
IChatLog
Definition: ichatlog.h:83
IChatLog::searchForward
virtual SearchResult searchForward(SearchPos startIdx, const QString &phrase, const ParameterSearch &parameter) const =0
searches forwards through the chat log until phrase is found according to parameter
message.h
HistMessageContentType::message
@ message
grouplist.h
SearchPos::operator!=
bool operator!=(const SearchPos &other) const
Definition: ichatlog.h:54
IChatLog::getDateIdxs
virtual std::vector< DateChatLogIdxPair > getDateIdxs(const QDate &startDate, size_t maxDates) const =0
Gets indexes for each new date starting at startDate.
IChatLog::itemUpdated
void itemUpdated(ChatLogIdx idx)
SearchPos::numMatches
size_t numMatches
Definition: ichatlog.h:47
ChatLogIdx
NamedType< size_t, struct ChatLogIdxTag, Orderable, UnderlyingAddable, UnitlessDifferencable, Incrementable > ChatLogIdx
Definition: ichatlog.h:38
IChatLog::DateChatLogIdxPair
Definition: ichatlog.h:126
SearchPos::operator==
bool operator==(const SearchPos &other) const
Definition: ichatlog.h:49
SearchPos
Definition: ichatlog.h:41
IChatLog::getNextIdx
virtual ChatLogIdx getNextIdx() const =0
core.h
chatlogitem.h
IChatLog::~IChatLog
virtual ~IChatLog()=default
SearchResult::exp
QRegularExpression exp
Definition: ichatlog.h:80
SearchPos::logIdx
ChatLogIdx logIdx
Definition: ichatlog.h:44
SearchResult
Definition: ichatlog.h:70
SearchResult::found
bool found
Definition: ichatlog.h:72