From 6db3199e0e2133c08609b42732194b041cd532ad Mon Sep 17 00:00:00 2001
From: Catalin Badea <catalin.badea392@gmail.com>
Date: Sun, 29 Jul 2012 19:39:43 +0300
Subject: Properly handle new messages.


diff --git a/Swift/Controllers/HistoryViewController.cpp b/Swift/Controllers/HistoryViewController.cpp
index c6e663b..082be1b 100644
--- a/Swift/Controllers/HistoryViewController.cpp
+++ b/Swift/Controllers/HistoryViewController.cpp
@@ -34,14 +34,12 @@ HistoryViewController::HistoryViewController(
 			selectedItem_(NULL),
 			currentResultDate_(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));
@@ -49,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));
+		historyController_->onNewMessage.disconnect(boost::bind(&HistoryViewController::handleNewMessage, this, _1));
 		delete historyWindow_;
 	}
 	delete roster_;
@@ -66,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));
+			historyController_->onNewMessage.connect(boost::bind(&HistoryViewController::handleNewMessage, this, _1));
 
 			historyWindow_->setRosterModel(roster_);
 		}
@@ -122,16 +122,22 @@ void HistoryViewController::handleNewMessage(const HistoryMessage& message) {
 
 	// check current conversation
 	if (selectedItem_ && selectedItem_->getJID() == displayJID) {
-		addNewMessage(message, false);
+		if (historyWindow_->getLastVisibleDate() == message.getTime().date()) {
+			addNewMessage(message, false);
+		}
+	}
+
+	// check if the new message matches the query
+	if (message.getMessage().find(historyWindow_->getSearchBoxText()) == std::string::npos) {
+		return;
 	}
 
-	// add new contact
-	else if (contacts_[message.getType()].count(displayJID)) {
+	// update contacts
+	if (!contacts_[message.getType()].count(displayJID)) {
 		roster_->addContact(displayJID, displayJID, nickResolver_->jidToNick(displayJID), category[message.getType()], avatarManager_->getAvatarPath(displayJID).string());
-		//
-		// TODO add message date here
-		contacts_[message.getType()][displayJID] = std::set<boost::gregorian::date>();
 	}
+
+	contacts_[message.getType()][displayJID].insert(message.getTime().date());
 }
 
 void HistoryViewController::addNewMessage(const HistoryMessage& message, bool addAtTheTop) {
diff --git a/Swift/Controllers/UIInterfaces/HistoryWindow.h b/Swift/Controllers/UIInterfaces/HistoryWindow.h
index edc4af3..7b21184 100644
--- a/Swift/Controllers/UIInterfaces/HistoryWindow.h
+++ b/Swift/Controllers/UIInterfaces/HistoryWindow.h
@@ -20,6 +20,9 @@ namespace Swift {
 			virtual void resetConversationViewTopInsertPoint() = 0; // this is a temporary fix used in adding messages at the top
 			virtual void setDate(const boost::gregorian::date& date) = 0;
 
+			virtual std::string getSearchBoxText() = 0;
+			virtual boost::gregorian::date getLastVisibleDate() = 0;
+
 			boost::signal<void (RosterItem*)> onSelectedContactChanged;
 			boost::signal<void (const std::string&)> onReturnPressed;
 			boost::signal<void (const boost::gregorian::date&)> onScrollReachedTop;
diff --git a/Swift/QtUI/QtHistoryWindow.cpp b/Swift/QtUI/QtHistoryWindow.cpp
index d9b587f..94d604e 100644
--- a/Swift/QtUI/QtHistoryWindow.cpp
+++ b/Swift/QtUI/QtHistoryWindow.cpp
@@ -212,4 +212,19 @@ void QtHistoryWindow::resetConversationViewTopInsertPoint() {
 	conversation_->resetTopInsertPoint();
 }
 
+std::string QtHistoryWindow::getSearchBoxText() {
+	return ui_.searchBox_->lineEdit()->text().toStdString();
+}
+
+boost::gregorian::date QtHistoryWindow::getLastVisibleDate() {
+	if (!dates_.empty()) {
+		QDate lastDate = *dates_.rbegin();
+		int year, month, day;
+		lastDate.getDate(&year, &month, &day);
+
+		return boost::gregorian::date(year, month, day);
+	}
+	return boost::gregorian::date(boost::gregorian::not_a_date_time);
+}
+
 }
diff --git a/Swift/QtUI/QtHistoryWindow.h b/Swift/QtUI/QtHistoryWindow.h
index 13d3f14..49de098 100644
--- a/Swift/QtUI/QtHistoryWindow.h
+++ b/Swift/QtUI/QtHistoryWindow.h
@@ -31,6 +31,9 @@ namespace Swift {
 			void closeEvent(QCloseEvent* event);
 			void showEvent(QShowEvent* event);
 
+			std::string getSearchBoxText();
+			boost::gregorian::date getLastVisibleDate();
+
 		signals:
 			void fontResized(int);
 
-- 
cgit v0.10.2-6-g49f6