qTox  Version: nightly | Commit: bc751c8e1cac455f9690654fcfe0f560d2d7dfdd
chatlogitem.cpp
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 #include "chatlogitem.h"
21 #include "src/model/friend.h"
22 #include "src/model/group.h"
23 
24 #include <cassert>
25 
26 namespace {
27 
31 template <typename T>
32 struct ChatLogItemDeleter
33 {
34  static void doDelete(void* ptr)
35  {
36  delete static_cast<T*>(ptr);
37  }
38 };
39 } // namespace
40 
41 ChatLogItem::ChatLogItem(ToxPk sender_, const QString& displayName, ChatLogFile file_)
42  : ChatLogItem(std::move(sender_), displayName, ContentType::fileTransfer,
43  ContentPtr(new ChatLogFile(std::move(file_)),
44  ChatLogItemDeleter<ChatLogFile>::doDelete))
45 {}
46 
47 ChatLogItem::ChatLogItem(ToxPk sender_, const QString& displayName, ChatLogMessage message_)
48  : ChatLogItem(sender_, displayName, ContentType::message,
49  ContentPtr(new ChatLogMessage(std::move(message_)),
50  ChatLogItemDeleter<ChatLogMessage>::doDelete))
51 {}
52 
54  : contentType(ContentType::systemMessage)
55  , content(new SystemMessage(std::move(systemMessage)), ChatLogItemDeleter<SystemMessage>::doDelete)
56 {}
57 
58 ChatLogItem::ChatLogItem(ToxPk sender_, const QString& displayName_, ContentType contentType_, ContentPtr content_)
59  : sender(std::move(sender_))
60  , displayName(displayName_)
61  , contentType(contentType_)
62  , content(std::move(content_))
63 {}
64 
66 {
67  return sender;
68 }
69 
71 {
72  return contentType;
73 }
74 
76 {
78  return *static_cast<ChatLogFile*>(content.get());
79 }
80 
82 {
84  return *static_cast<ChatLogFile*>(content.get());
85 }
86 
88 {
90  return *static_cast<ChatLogMessage*>(content.get());
91 }
92 
94 {
96  return *static_cast<ChatLogMessage*>(content.get());
97 }
98 
100 {
102  return *static_cast<SystemMessage*>(content.get());
103 }
104 
106 {
108  return *static_cast<SystemMessage*>(content.get());
109 }
110 
111 
112 QDateTime ChatLogItem::getTimestamp() const
113 {
114  switch (contentType) {
116  const auto& message = getContentAsMessage();
117  return message.message.timestamp;
118  }
120  const auto& file = getContentAsFile();
121  return file.timestamp;
122  }
124  const auto& systemMessage = getContentAsSystemMessage();
125  return systemMessage.timestamp;
126  }
127  }
128 
129  assert(false);
130  return QDateTime();
131 }
132 
133 void ChatLogItem::setDisplayName(QString name)
134 {
135  displayName = name;
136 }
137 
138 const QString& ChatLogItem::getDisplayName() const
139 {
140  return displayName;
141 }
ChatLogItem::getContentAsFile
ChatLogFile & getContentAsFile()
Definition: chatlogitem.cpp:75
ChatLogItem::getTimestamp
QDateTime getTimestamp() const
Definition: chatlogitem.cpp:112
ChatLogItem
Definition: chatlogitem.h:42
friend.h
ChatLogItem::setDisplayName
void setDisplayName(QString name)
Definition: chatlogitem.cpp:133
ChatLogItem::getSender
const ToxPk & getSender() const
Definition: chatlogitem.cpp:65
ChatLogItem::ContentType
ContentType
Definition: chatlogitem.h:48
group.h
ChatLogItem::ContentType::message
@ message
HistMessageContentType::file
@ file
SystemMessage
Definition: systemmessage.h:47
ChatLogItem::sender
ToxPk sender
Definition: chatlogitem.h:73
ChatLogItem::getContentType
ContentType getContentType() const
Definition: chatlogitem.cpp:70
ChatLogItem::contentType
ContentType contentType
Definition: chatlogitem.h:75
ChatLogItem::ChatLogItem
ChatLogItem(ToxPk sender, const QString &displayName, ChatLogFile file)
Definition: chatlogitem.cpp:41
HistMessageContentType::message
@ message
ToxPk
This class represents a Tox Public Key, which is a part of Tox ID.
Definition: toxpk.h:26
ChatLogMessage
Definition: chatlogitem.h:30
ChatLogItem::displayName
QString displayName
Definition: chatlogitem.h:74
ChatLogItem::content
ContentPtr content
Definition: chatlogitem.h:77
ChatLogItem::getDisplayName
const QString & getDisplayName() const
Definition: chatlogitem.cpp:138
ChatLogItem::getContentAsMessage
ChatLogMessage & getContentAsMessage()
Definition: chatlogitem.cpp:87
chatlogitem.h
ChatLogItem::ContentType::systemMessage
@ systemMessage
ChatLogItem::getContentAsSystemMessage
SystemMessage & getContentAsSystemMessage()
Definition: chatlogitem.cpp:99
ChatLogItem::ContentPtr
std::unique_ptr< void, void(*)(void *)> ContentPtr
Definition: chatlogitem.h:45
ChatLogItem::ContentType::fileTransfer
@ fileTransfer
ChatLogFile
Definition: chatlogitem.h:36