1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
/*
* Copyright (c) 2012 Catalin Badea
* Licensed under the simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/
#include <Swift/Controllers/HistoryViewController.h>
#include <Swift/Controllers/UIInterfaces/HistoryWindowFactory.h>
#include <Swift/Controllers/UIEvents/RequestHistoryUIEvent.h>
#include <Swift/Controllers/HistoryController.h>
#include <Swiften/History/HistoryMessage.h>
#include <Swiften/Base/foreach.h>
#include <Swiften/Client/NickResolver.h>
#include <Swiften/Avatars/AvatarManager.h>
namespace Swift {
HistoryViewController::HistoryViewController(
const JID& selfJID,
UIEventStream* uiEventStream,
HistoryController* historyController,
NickResolver* nickResolver,
AvatarManager* avatarManager,
HistoryWindowFactory* historyWindowFactory) :
selfJID_(selfJID),
uiEventStream_(uiEventStream),
historyController_(historyController),
nickResolver_(nickResolver),
avatarManager_(avatarManager),
historyWindowFactory_(historyWindowFactory),
historyWindow_(NULL),
selectedItem_(NULL),
currentDate_(boost::gregorian::not_a_date_time) {
uiEventStream_->onUIEvent.connect(boost::bind(&HistoryViewController::handleUIEvent, this, _1));
historyController_->onNewMessage.connect(boost::bind(&HistoryViewController::handleNewMessage, this, _1));
roster_ = new Roster(false, true);
}
HistoryViewController::~HistoryViewController() {
uiEventStream_->onUIEvent.disconnect(boost::bind(&HistoryViewController::handleUIEvent, this, _1));
historyController_->onNewMessage.disconnect(boost::bind(&HistoryViewController::handleNewMessage, this, _1));
if (historyWindow_) {
historyWindow_->onSelectedContactChanged.disconnect(boost::bind(&HistoryViewController::handleSelectedContactChanged, this, _1));
historyWindow_->onReturnPressed.disconnect(boost::bind(&HistoryViewController::handleReturnPressed, this, _1));
delete historyWindow_;
}
delete roster_;
}
void HistoryViewController::handleUIEvent(boost::shared_ptr<UIEvent> rawEvent) {
// TODO: add new nick manager
boost::shared_ptr<RequestHistoryUIEvent> event = boost::dynamic_pointer_cast<RequestHistoryUIEvent>(rawEvent);
if (event != NULL) {
if (historyWindow_ == NULL) {
historyWindow_ = historyWindowFactory_->createHistoryWindow(uiEventStream_);
historyWindow_->onSelectedContactChanged.connect(boost::bind(&HistoryViewController::handleSelectedContactChanged, this, _1));
historyWindow_->onReturnPressed.connect(boost::bind(&HistoryViewController::handleReturnPressed, this, _1));
historyWindow_->setRosterModel(roster_);
}
// populate roster by doing an empty search
handleReturnPressed(std::string());
historyWindow_->activate();
}
}
void HistoryViewController::handleSelectedContactChanged(RosterItem* newContact) {
// FIXME: signal is triggerd twice.
ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(newContact);
if (contact && selectedItem_ != contact) {
selectedItem_ = contact;
historyWindow_->resetConversationView();
}
else {
return;
}
JID contactJID = contact->getJID();
std::vector<HistoryMessage> messages;
if (roomPrivateContacts_.count(contactJID)) {
currentDate_ = *roomPrivateContacts_[contactJID].rbegin();
messages = historyController_->getMessages(selfJID_, contactJID, HistoryMessage::PrivateMessage, currentDate_);
}
else if (contacts_.count(contactJID)) {
currentDate_ = *contacts_[contactJID].rbegin();
messages = historyController_->getMessages(selfJID_, contactJID, HistoryMessage::Chat, currentDate_);
}
else {
currentDate_ = *rooms_[contactJID].rbegin();
messages = historyController_->getMessages(selfJID_, contactJID, HistoryMessage::Groupchat, currentDate_);
}
foreach (const HistoryMessage& message, messages) {
addNewMessage(message);
}
}
void HistoryViewController::handleNewMessage(const HistoryMessage& message) {
JID contactJID = message.getFromJID().toBare() == selfJID_ ? message.getToJID() : message.getFromJID();
JID displayJID;
if (message.getType() == HistoryMessage::PrivateMessage) {
displayJID = contactJID;
}
else {
displayJID = contactJID.toBare();
}
// check current conversation
if (selectedItem_ && selectedItem_->getJID() == displayJID) {
addNewMessage(message);
}
// add new contact
else if (message.getType() == HistoryMessage::Groupchat && !rooms_.count(displayJID)) {
roster_->addContact(displayJID, displayJID, nickResolver_->jidToNick(displayJID), "MUC", avatarManager_->getAvatarPath(displayJID).string());
//
// TODO add message date here
rooms_[displayJID] = std::set<boost::gregorian::date>();
}
else if (message.getType() == HistoryMessage::Chat && !contacts_.count(displayJID)) {
roster_->addContact(displayJID, displayJID, nickResolver_->jidToNick(displayJID), "Contacts", avatarManager_->getAvatarPath(displayJID).string());
contacts_[displayJID] = std::set<boost::gregorian::date>();
}
else if (message.getType() == HistoryMessage::PrivateMessage && !roomPrivateContacts_.count(displayJID)) {
roster_->addContact(displayJID, displayJID, nickResolver_->jidToNick(displayJID), "Contacts", avatarManager_->getAvatarPath(displayJID).string());
roomPrivateContacts_[displayJID] = std::set<boost::gregorian::date>();
}
}
void HistoryViewController::addNewMessage(const HistoryMessage& message) {
bool senderIsSelf = message.getFromJID().toBare() == selfJID_;
std::string avatarPath = avatarManager_->getAvatarPath(message.getFromJID()).string();
historyWindow_->addMessage(message.getMessage(), nickResolver_->jidToNick(message.getFromJID()), senderIsSelf, avatarPath, message.getTime());
}
void HistoryViewController::handleReturnPressed(const std::string& keyword) {
roster_->removeAll();
rooms_.clear();
contacts_.clear();
roomPrivateContacts_.clear();
selectedItem_ = NULL;
historyWindow_->resetConversationView();
// TODO set date
// MUCs
rooms_ = historyController_->getContacts(selfJID_, HistoryMessage::Groupchat, keyword);
for (ContactsMap::const_iterator room = rooms_.begin(); room != rooms_.end(); room++) {
const JID& jid = room->first;
roster_->addContact(jid, jid, nickResolver_->jidToNick(jid), "MUC", avatarManager_->getAvatarPath(jid).string());
}
// Contacts
contacts_ = historyController_->getContacts(selfJID_, HistoryMessage::Chat, keyword);
for (ContactsMap::const_iterator contact = contacts_.begin(); contact != contacts_.end(); contact++) {
const JID& jid = contact->first;
roster_->addContact(jid, jid, nickResolver_->jidToNick(jid), "Contacts", avatarManager_->getAvatarPath(jid).string());
}
// MUC contacts
roomPrivateContacts_ = historyController_->getContacts(selfJID_, HistoryMessage::PrivateMessage, keyword);
for (ContactsMap::const_iterator contact = roomPrivateContacts_.begin(); contact != roomPrivateContacts_.end(); contact++) {
const JID& jid = contact->first;
roster_->addContact(jid, jid, nickResolver_->jidToNick(jid), "Contacts", avatarManager_->getAvatarPath(jid).string());
}
}
}
|