summaryrefslogtreecommitdiffstats
blob: b3603a83c25a425ee3ccfdc39e93ae03443d0285 (plain)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * Copyright (c) 2012 Catalin Badea
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

#include <QtHistoryWindow.h>
#include <QtTabbable.h>

#include <QtSwiftUtil.h>
#include <MessageSnippet.h>
#include <Swiften/History/HistoryMessage.h>
#include <string>

#include <boost/shared_ptr.hpp>

#include <QTime>
#include <QUrl>
#include <QMenu>
#include <QTextDocument>
#include <QDateTime>
#include <Swift/QtUI/QtScaledAvatarCache.h>
#include <QLineEdit>

#include <boost/smart_ptr/make_shared.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>

namespace Swift {

QtHistoryWindow::QtHistoryWindow(SettingsProvider* settings, UIEventStream* eventStream) {
	ui_.setupUi(this);

	theme_ = new QtChatTheme("");
	idCounter_ = 0;

	delete ui_.conversation_;
	conversation_ = new QtChatView(theme_, this);
	QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
	sizePolicy.setHorizontalStretch(80);
	sizePolicy.setVerticalStretch(0);
	conversation_->setSizePolicy(sizePolicy);

	ui_.conversation_ = conversation_;
	ui_.bottomLayout_->addWidget(conversation_);

	delete ui_.conversationRoster_;
	conversationRoster_ = new QtTreeWidget(eventStream, settings, this);
	QSizePolicy sizePolicy2(QSizePolicy::Preferred, QSizePolicy::Expanding);
	sizePolicy2.setVerticalStretch(80);
	conversationRoster_->setSizePolicy(sizePolicy2);
	ui_.conversationRoster_ = conversationRoster_;
	ui_.bottomLeftLayout_->setDirection(QBoxLayout::BottomToTop);
	ui_.bottomLeftLayout_->addWidget(conversationRoster_);

	setWindowTitle(tr("History"));

	conversationRoster_->onSomethingSelectedChanged.connect(boost::bind(&QtHistoryWindow::handleSomethingSelectedChanged, this, _1));
	connect(conversation_, SIGNAL(scrollRequested(int)), this, SLOT(handleScrollRequested(int)));
	connect(conversation_, SIGNAL(scrollReachedTop()), this, SLOT(handleScrollReachedTop()));
	connect(conversation_, SIGNAL(scrollReachedBottom()), this, SLOT(handleScrollReachedBottom()));
	connect(conversation_, SIGNAL(fontResized(int)), this, SLOT(handleFontResized(int)));
	connect(ui_.searchBox_->lineEdit(), SIGNAL(returnPressed()), this, SLOT(handleReturnPressed()));
	connect(ui_.calendarWidget_, SIGNAL(clicked(const QDate&)), this, SLOT(handleCalendarClicked(const QDate&)));
	connect(ui_.previousButton_, SIGNAL(clicked(bool)), this, SLOT(handlePreviousButtonClicked()));
	connect(ui_.nextButton_, SIGNAL(clicked(bool)), this, SLOT(handleNextButtonClicked()));
}

QtHistoryWindow::~QtHistoryWindow() {
	disconnect(conversation_, SIGNAL(scrollRequested(int)), this, SLOT(handleScrollRequested(int)));
	disconnect(conversation_, SIGNAL(scrollReachedTop()), this, SLOT(handleScrollReachedTop()));
	disconnect(conversation_, SIGNAL(scrollReachedBottom()), this, SLOT(handleScrollReachedBottom()));
	disconnect(conversation_, SIGNAL(fontResized(int)), this, SLOT(handleFontResized(int)));
	disconnect(ui_.searchBox_->lineEdit(), SIGNAL(returnPressed()), this, SLOT(handleReturnPressed()));
	disconnect(ui_.calendarWidget_, SIGNAL(clicked(const QDate&)), this, SLOT(handleCalendarClicked(const QDate&)));
	disconnect(ui_.previousButton_, SIGNAL(clicked(bool)), this, SLOT(handlePreviousButtonClicked()));
	disconnect(ui_.nextButton_, SIGNAL(clicked(bool)), this, SLOT(handleNextButtonClicked()));

	delete theme_;
	delete conversation_;
	// TODO: delete ui_
}

void QtHistoryWindow::activate() {
	emit wantsToActivate();
}

void QtHistoryWindow::showEvent(QShowEvent* event) {
	emit windowOpening();
	emit titleUpdated();
	QWidget::showEvent(event);
}

void QtHistoryWindow::closeEvent(QCloseEvent* event) {
	emit windowClosing();
	event->accept();
}

void QtHistoryWindow::setRosterModel(Roster* model) {
	conversationRoster_->setRosterModel(model);
}

void QtHistoryWindow::addMessage(const std::string &message, const std::string &senderName, bool senderIsSelf, const std::string& avatarPath, const boost::posix_time::ptime& time, bool addAtTheTop) {
	QString scaledAvatarPath = QtScaledAvatarCache(32).getScaledAvatarPath(avatarPath.c_str());

	QString messageHTML(P2QSTRING(message));
	messageHTML = Qt::escape(messageHTML);
	QDateTime qTime = B2QDATE(time);
	QDate date = qTime.date();

	std::string id = "id" + boost::lexical_cast<std::string>(idCounter_++);

	QString qAvatarPath =  scaledAvatarPath.isEmpty() ? "qrc:/icons/avatar.png" : QUrl::fromLocalFile(scaledAvatarPath).toEncoded();

	if (addAtTheTop) {
		conversation_->addMessageTop(boost::shared_ptr<ChatSnippet>(new MessageSnippet(messageHTML, Qt::escape(P2QSTRING(senderName)), qTime, qAvatarPath, senderIsSelf, false, theme_, P2QSTRING(id))));
	}
	else {
		conversation_->addMessageBottom(boost::shared_ptr<ChatSnippet>(new MessageSnippet(messageHTML, Qt::escape(P2QSTRING(senderName)), qTime, qAvatarPath, senderIsSelf, false, theme_, P2QSTRING(id))));
	}

	// keep track of the days viewable in the chatView
	if (!dates_.count(date)) {
		dates_.insert(date);
	}
}

void QtHistoryWindow::handleSomethingSelectedChanged(RosterItem* item) {
	onSelectedContactChanged(item);
}

void QtHistoryWindow::resetConversationView() {
	dates_.clear();
	conversation_->resetView();
}

void QtHistoryWindow::handleScrollRequested(int pos) {
	// first message starts with offset 5
	if (pos < 5) {
		pos = 5;
	}

	QDate currentDate;
	foreach (const QDate& date, dates_) {
		int snippetPosition = conversation_->getSnippetPositionByDate(date);
		if (snippetPosition <= pos) {
			currentDate = date;
		}
	}

	if (ui_.calendarWidget_->selectedDate() != currentDate) {
		ui_.calendarWidget_->setSelectedDate(currentDate);
	}
}

void QtHistoryWindow::handleScrollReachedTop() {
	int year, month, day;
	ui_.calendarWidget_->selectedDate().getDate(&year, &month, &day);
	onScrollReachedTop(boost::gregorian::date(year, month, day));
}

void QtHistoryWindow::handleScrollReachedBottom() {
	int year, month, day;
	ui_.calendarWidget_->selectedDate().getDate(&year, &month, &day);
	onScrollReachedBottom(boost::gregorian::date(year, month, day));
}

void QtHistoryWindow::handleReturnPressed() {
	onReturnPressed(ui_.searchBox_->lineEdit()->text().toStdString());
}

void QtHistoryWindow::handleCalendarClicked(const QDate& date) {
}

void QtHistoryWindow::setDate(const boost::gregorian::date& date) {
	ui_.calendarWidget_->setSelectedDate(QDate::fromJulianDay(date.julian_day()));
}

void QtHistoryWindow::handleNextButtonClicked() {
	onNextButtonClicked();
}

void QtHistoryWindow::handlePreviousButtonClicked() {
	onPreviousButtonClicked();
}

void QtHistoryWindow::handleFontResized(int fontSizeSteps) {
	conversation_->resizeFont(fontSizeSteps);

	emit fontResized(fontSizeSteps);
}

}