From b629b5c198020e0992ee6cf01669b7a5191e23ef Mon Sep 17 00:00:00 2001
From: Catalin Badea <catalin.badea392@gmail.com>
Date: Mon, 9 Jul 2012 22:35:27 +0300
Subject: Calendar widget follows the currently viewed message


diff --git a/Swift/QtUI/MessageSnippet.cpp b/Swift/QtUI/MessageSnippet.cpp
index 7505905..51c9c78 100644
--- a/Swift/QtUI/MessageSnippet.cpp
+++ b/Swift/QtUI/MessageSnippet.cpp
@@ -37,6 +37,7 @@ MessageSnippet::MessageSnippet(const QString& message, const QString& sender, co
 	content_.replace("%time%", wrapResizable("<span class='swift_time'>" + timeToEscapedString(time) + "</span>"));
 	content_.replace("%userIconPath%", escape(iconURI));
 	content_ = "<div id='" + id + "'>" + content_ + "</div>";
+	content_ = "<div class='date" + time.date().toString(Qt::ISODate) + "'>" + content_ + "</span>";
 }
 
 MessageSnippet::~MessageSnippet() {
diff --git a/Swift/QtUI/QtChatView.cpp b/Swift/QtUI/QtChatView.cpp
index a55161f..153e3f9 100644
--- a/Swift/QtUI/QtChatView.cpp
+++ b/Swift/QtUI/QtChatView.cpp
@@ -61,6 +61,7 @@ QtChatView::QtChatView(QtChatTheme* theme, QWidget* parent) : QWidget(parent), f
 	//webPage_->settings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
 	webView_->setPage(webPage_);
 	connect(webPage_, SIGNAL(selectionChanged()), SLOT(copySelectionToClipboard()));
+	connect(webPage_, SIGNAL(scrollRequested(int, int, const QRect&)), SLOT(handleScrollRequested(int, int, const QRect&)));
 
 	viewReady_ = false;
 	isAtBottom_ = true;
@@ -384,4 +385,15 @@ void QtChatView::setMUCInvitationJoined(QString id) {
 	}
 }
 
+void QtChatView::handleScrollRequested(int, int dy, const QRect&) {
+	int pos = webPage_->mainFrame()->scrollBarValue(Qt::Vertical);
+	emit scrollRequested(pos - dy);
+}
+
+int QtChatView::getSnippetPositionByDate(const QDate& date) {
+	QWebElement message = webPage_->mainFrame()->documentElement().findFirst(".date" + date.toString(Qt::ISODate));
+
+	return message.geometry().top();
+}
+
 }
diff --git a/Swift/QtUI/QtChatView.h b/Swift/QtUI/QtChatView.h
index fdbdd5a..84771ee 100644
--- a/Swift/QtUI/QtChatView.h
+++ b/Swift/QtUI/QtChatView.h
@@ -20,6 +20,7 @@
 
 class QWebPage;
 class QUrl;
+class QDate;
 
 namespace Swift {
 	class QtWebView;
@@ -44,10 +45,12 @@ namespace Swift {
 			void setFileTransferStatus(QString id, const ChatWindow::FileTransferState state, const QString& msg);
 			void setMUCInvitationJoined(QString id);
 			void showEmoticons(bool show);
+			int getSnippetPositionByDate(const QDate& date);
 		signals:
 			void gotFocus();
 			void fontResized(int);
 			void logCleared();
+			void scrollRequested(int pos);
 
 		public slots:
 			void copySelectionToClipboard();
@@ -63,6 +66,7 @@ namespace Swift {
 			void handleViewLoadFinished(bool);
 			void handleFrameSizeChanged();
 			void handleClearRequested();
+			void handleScrollRequested(int dx, int dy, const QRect& rectToScroll);
 
 		private:
 			void headerEncode();
diff --git a/Swift/QtUI/QtHistoryWindow.cpp b/Swift/QtUI/QtHistoryWindow.cpp
index 50fd66e..95a47ac 100644
--- a/Swift/QtUI/QtHistoryWindow.cpp
+++ b/Swift/QtUI/QtHistoryWindow.cpp
@@ -18,6 +18,7 @@
 #include <QUrl>
 #include <QMenu>
 #include <QTextDocument>
+#include <QDateTime>
 #include <Swift/QtUI/QtScaledAvatarCache.h>
 
 #include <boost/smart_ptr/make_shared.hpp>
@@ -51,10 +52,15 @@ QtHistoryWindow::QtHistoryWindow(SettingsProvider* settings, UIEventStream* even
 	setWindowTitle(tr("History"));
 
 	conversationRoster_->onSomethingSelectedChanged.connect(boost::bind(&QtHistoryWindow::handleSomethingSelectedChanged, this, _1));
+	connect(conversation_, SIGNAL(scrollRequested(int)), this, SLOT(handleScrollRequested(int)));
 }
 
 QtHistoryWindow::~QtHistoryWindow() {
+	disconnect(conversation_, SIGNAL(scrollRequested(int)), this, SLOT(handleScrollRequested(int)));
+
 	delete theme_;
+	delete conversation_;
+	// TODO: delete ui_
 }
 
 void QtHistoryWindow::activate() {
@@ -81,19 +87,46 @@ void QtHistoryWindow::addMessage(const std::string &message, const std::string &
 
 	QString messageHTML(P2QSTRING(message));
 	messageHTML = Qt::escape(messageHTML);
+	QDateTime qTime = B2QDATE(time);
+	QDate date = qTime.date();
 
 	QString qAvatarPath =  scaledAvatarPath.isEmpty() ? "qrc:/icons/avatar.png" : QUrl::fromLocalFile(scaledAvatarPath).toEncoded();
 
-	conversation_->addMessage(boost::shared_ptr<ChatSnippet>(new MessageSnippet(messageHTML, Qt::escape(P2QSTRING(senderName)), B2QDATE(time), qAvatarPath, senderIsSelf, false, theme_, "id")));
-}
+	conversation_->addMessage(boost::shared_ptr<ChatSnippet>(new MessageSnippet(messageHTML, Qt::escape(P2QSTRING(senderName)), qTime, qAvatarPath, senderIsSelf, false, theme_, "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 (currentDate_ != currentDate) {
+		currentDate_ = currentDate;
+		ui_.calendarWidget_->setSelectedDate(currentDate_);
+	}
+}
+
 }
diff --git a/Swift/QtUI/QtHistoryWindow.h b/Swift/QtUI/QtHistoryWindow.h
index 279657d..448780f 100644
--- a/Swift/QtUI/QtHistoryWindow.h
+++ b/Swift/QtUI/QtHistoryWindow.h
@@ -11,6 +11,8 @@
 #include <QtChatView.h>
 #include <QtTabbable.h>
 #include <Swift/QtUI/Roster/QtTreeWidget.h>
+#include <set>
+#include <QDate>
 
 namespace Swift {
 	class QtHistoryWindow : public QtTabbable, public HistoryWindow {
@@ -24,15 +26,20 @@ namespace Swift {
 			void addMessage(const std::string &message, const std::string &senderName, bool senderIsSelf, const std::string& avatarPath, const boost::posix_time::ptime& time);
 			void resetConversationView();
 
-		private:
-			virtual void closeEvent(QCloseEvent* event);
-			virtual void showEvent(QShowEvent* event);
+			void closeEvent(QCloseEvent* event);
+			void showEvent(QShowEvent* event);
+
+		protected slots:
+			void handleScrollRequested(int pos);
 
+		private:
 			void handleSomethingSelectedChanged(RosterItem* item);
 
 			Ui::QtHistoryWindow ui_;
 			QtChatTheme* theme_;
 			QtChatView* conversation_;
 			QtTreeWidget* conversationRoster_;
+			std::set<QDate> dates_;
+			QDate currentDate_;
 	};
 }
-- 
cgit v0.10.2-6-g49f6