From 7def4c8d77f25a72df6e2115012f69f3377bd8fd Mon Sep 17 00:00:00 2001
From: Catalin Badea <catalin.badea392@gmail.com>
Date: Thu, 2 Aug 2012 20:08:05 +0300
Subject: Enable calendar widget.


diff --git a/Swift/Controllers/HistoryViewController.cpp b/Swift/Controllers/HistoryViewController.cpp
index 819ba4e..43575d5 100644
--- a/Swift/Controllers/HistoryViewController.cpp
+++ b/Swift/Controllers/HistoryViewController.cpp
@@ -47,6 +47,7 @@ HistoryViewController::~HistoryViewController() {
 		historyWindow_->onScrollReachedBottom.disconnect(boost::bind(&HistoryViewController::handleScrollReachedBottom, this, _1));
 		historyWindow_->onPreviousButtonClicked.disconnect(boost::bind(&HistoryViewController::handlePreviousButtonClicked, this));
 		historyWindow_->onNextButtonClicked.disconnect(boost::bind(&HistoryViewController::handleNextButtonClicked, this));
+		historyWindow_->onCalendarClicked.disconnect(boost::bind(&HistoryViewController::handleCalendarClicked, this, _1));
 		historyController_->onNewMessage.disconnect(boost::bind(&HistoryViewController::handleNewMessage, this, _1));
 		delete historyWindow_;
 	}
@@ -64,6 +65,7 @@ void HistoryViewController::handleUIEvent(boost::shared_ptr<UIEvent> rawEvent) {
 			historyWindow_->onScrollReachedBottom.connect(boost::bind(&HistoryViewController::handleScrollReachedBottom, this, _1));
 			historyWindow_->onPreviousButtonClicked.connect(boost::bind(&HistoryViewController::handlePreviousButtonClicked, this));
 			historyWindow_->onNextButtonClicked.connect(boost::bind(&HistoryViewController::handleNextButtonClicked, this));
+			historyWindow_->onCalendarClicked.connect(boost::bind(&HistoryViewController::handleCalendarClicked, this, _1));
 			historyController_->onNewMessage.connect(boost::bind(&HistoryViewController::handleNewMessage, this, _1));
 
 			historyWindow_->setRosterModel(roster_);
@@ -236,4 +238,45 @@ void HistoryViewController::reset() {
 	historyWindow_->resetConversationView();
 }
 
+void HistoryViewController::handleCalendarClicked(const boost::gregorian::date& date) {
+	if (!selectedItem_) {
+		return;
+	}
+
+	boost::gregorian::date newDate;
+	if (contacts_[selectedItemType_][selectedItem_->getJID()].count(date)) {
+		newDate = date;
+	}
+	else if (date < currentResultDate_) {
+		foreach(const boost::gregorian::date& current, contacts_[selectedItemType_][selectedItem_->getJID()]) {
+			if (current > date) {
+				newDate = current;
+				break;
+			}
+		}
+	}
+	else {
+		reverse_foreach(const boost::gregorian::date& current, contacts_[selectedItemType_][selectedItem_->getJID()]) {
+			if (current < date) {
+				newDate = current;
+				break;
+			}
+		}
+	}
+
+	historyWindow_->setDate(newDate);
+	if (newDate == currentResultDate_) {
+		return;
+	}
+	currentResultDate_ = newDate;
+	historyWindow_->resetConversationView();
+
+	std::vector<HistoryMessage> messages = historyController_->getMessagesFromDate(selfJID_, selectedItem_->getJID(), selectedItemType_, currentResultDate_);
+	historyWindow_->setDate(currentResultDate_);
+
+	foreach (const HistoryMessage& message, messages) {
+		addNewMessage(message, false);
+	}
+}
+
 }
diff --git a/Swift/Controllers/HistoryViewController.h b/Swift/Controllers/HistoryViewController.h
index e78b5be..2302355 100644
--- a/Swift/Controllers/HistoryViewController.h
+++ b/Swift/Controllers/HistoryViewController.h
@@ -40,6 +40,7 @@ namespace Swift {
 			void handleScrollReachedBottom(const boost::gregorian::date& date);
 			void handlePreviousButtonClicked();
 			void handleNextButtonClicked();
+			void handleCalendarClicked(const boost::gregorian::date& date);
 
 			void addNewMessage(const HistoryMessage& message, bool addAtTheTop);
 			void reset();
diff --git a/Swift/Controllers/UIInterfaces/HistoryWindow.h b/Swift/Controllers/UIInterfaces/HistoryWindow.h
index 7b21184..ffb0ad5 100644
--- a/Swift/Controllers/UIInterfaces/HistoryWindow.h
+++ b/Swift/Controllers/UIInterfaces/HistoryWindow.h
@@ -29,5 +29,6 @@ namespace Swift {
 			boost::signal<void (const boost::gregorian::date&)> onScrollReachedBottom;
 			boost::signal<void ()> onPreviousButtonClicked;
 			boost::signal<void ()> onNextButtonClicked;
+			boost::signal<void (const boost::gregorian::date&)> onCalendarClicked;
 	};
 }
diff --git a/Swift/QtUI/QtHistoryWindow.cpp b/Swift/QtUI/QtHistoryWindow.cpp
index d926db7..b9f2e91 100644
--- a/Swift/QtUI/QtHistoryWindow.cpp
+++ b/Swift/QtUI/QtHistoryWindow.cpp
@@ -63,6 +63,7 @@ QtHistoryWindow::QtHistoryWindow(SettingsProvider* settings, UIEventStream* even
 	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_.calendarWidget_, SIGNAL(activated(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()));
 }
@@ -74,6 +75,7 @@ QtHistoryWindow::~QtHistoryWindow() {
 	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_.calendarWidget_, SIGNAL(activated(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()));
 
@@ -186,6 +188,10 @@ void QtHistoryWindow::handleReturnPressed() {
 }
 
 void QtHistoryWindow::handleCalendarClicked(const QDate& date) {
+	int year, month, day;
+	QDate tempDate = date; // getDate discards const qualifier
+	tempDate.getDate(&year, &month, &day);
+	onCalendarClicked(boost::gregorian::date(year, month, day));
 }
 
 void QtHistoryWindow::setDate(const boost::gregorian::date& date) {
-- 
cgit v0.10.2-6-g49f6