From 3ef9dabfc62cb3daf0b88543a5d65d31e8eb5977 Mon Sep 17 00:00:00 2001
From: Kevin Smith <git@kismith.co.uk>
Date: Thu, 7 Jul 2011 21:29:10 +0100
Subject: Add setUnreadCount to ChatListView

Resolves: #904

diff --git a/Swift/Controllers/Chat/ChatControllerBase.cpp b/Swift/Controllers/Chat/ChatControllerBase.cpp
index fcaf4ce..5e3c45f 100644
--- a/Swift/Controllers/Chat/ChatControllerBase.cpp
+++ b/Swift/Controllers/Chat/ChatControllerBase.cpp
@@ -87,6 +87,11 @@ void ChatControllerBase::handleAllMessagesRead() {
 	}
 	unreadMessages_.clear();
 	chatWindow_->setUnreadMessageCount(0);
+	onUnreadCountChanged();
+}
+
+int ChatControllerBase::getUnreadCount() {
+	return unreadMessages_.size();
 }
 
 void ChatControllerBase::handleSendMessageRequest(const std::string &body, bool isCorrectionMessage) {
@@ -203,6 +208,7 @@ void ChatControllerBase::handleIncomingMessage(boost::shared_ptr<MessageEvent> m
 	}
 	chatWindow_->show();
 	chatWindow_->setUnreadMessageCount(unreadMessages_.size());
+	onUnreadCountChanged();
 	postHandleIncomingMessage(messageEvent);
 }
 
diff --git a/Swift/Controllers/Chat/ChatControllerBase.h b/Swift/Controllers/Chat/ChatControllerBase.h
index 3364ccb..86c1ef2 100644
--- a/Swift/Controllers/Chat/ChatControllerBase.h
+++ b/Swift/Controllers/Chat/ChatControllerBase.h
@@ -49,6 +49,9 @@ namespace Swift {
 			virtual void setToJID(const JID& jid) {toJID_ = jid;};
 			/** Used for determining when something is recent.*/
 			boost::signal<void (const std::string& /*activity*/)> onActivity;
+			boost::signal<void ()> onUnreadCountChanged;
+			int getUnreadCount();
+			const JID& getToJID() {return toJID_;}
 		protected:
 			ChatControllerBase(const JID& self, StanzaChannel* stanzaChannel, IQRouter* iqRouter, ChatWindowFactory* chatWindowFactory, const JID &toJID, PresenceOracle* presenceOracle, AvatarManager* avatarManager, bool useDelayForLatency, UIEventStream* eventStream, EventController* eventController, TimerFactory* timerFactory);
 
diff --git a/Swift/Controllers/Chat/ChatsManager.cpp b/Swift/Controllers/Chat/ChatsManager.cpp
index 9d6d9c8..66817aa 100644
--- a/Swift/Controllers/Chat/ChatsManager.cpp
+++ b/Swift/Controllers/Chat/ChatsManager.cpp
@@ -132,7 +132,7 @@ void ChatsManager::loadRecents() {
 		std::string activity(recent[1]);
 		bool isMUC = recent[2] == "true";
 		std::string nick(recent[3]);
-		ChatListWindow::Chat chat(jid, nickResolver_->jidToNick(jid), activity, isMUC, nick);
+		ChatListWindow::Chat chat(jid, nickResolver_->jidToNick(jid), activity, 0, isMUC, nick);
 		prependRecent(chat);
 	}
 	chatListWindow_->setRecents(recentChats_);
@@ -171,15 +171,37 @@ void ChatsManager::handleMUCBookmarkRemoved(const MUCBookmark& bookmark) {
 	chatListWindow_->removeMUCBookmark(bookmark);
 }
 
+ChatListWindow::Chat ChatsManager::createChatListChatItem(const JID& jid, const std::string& activity) {
+	int unreadCount = 0;
+	ChatController* controller = getChatControllerIfExists(jid);
+	if (controller) {
+		unreadCount = controller->getUnreadCount();
+	}
+	return ChatListWindow::Chat(jid, nickResolver_->jidToNick(jid), activity, unreadCount, false);
+}
+
 void ChatsManager::handleChatActivity(const JID& jid, const std::string& activity) {
+	ChatListWindow::Chat chat = createChatListChatItem(jid, activity);
 	/* FIXME: MUC use requires changes here. */
-	ChatListWindow::Chat chat(jid, nickResolver_->jidToNick(jid), activity, false);
+
 	/* FIXME: handle nick changes */
 	appendRecent(chat);
 	chatListWindow_->setRecents(recentChats_);
 	saveRecents();
 }
 
+void ChatsManager::handleUnreadCountChanged(ChatController* controller) {
+	int unreadTotal = 0;
+	foreach (ChatListWindow::Chat chatItem, recentChats_) {
+		if (chatItem.jid == controller->getToJID()) {
+			chatItem.setUnreadCount(controller->getUnreadCount());
+		}
+		unreadTotal += chatItem.unreadCount;
+	}
+	chatListWindow_->setRecents(recentChats_);
+	chatListWindow_->setUnreadCount(unreadTotal);
+}
+
 void ChatsManager::appendRecent(const ChatListWindow::Chat& chat) {
 	recentChats_.erase(std::remove(recentChats_.begin(), recentChats_.end(), chat), recentChats_.end());
 	recentChats_.push_front(chat);
@@ -314,6 +336,7 @@ ChatController* ChatsManager::createNewChatController(const JID& contact) {
 	chatControllers_[contact] = controller;
 	controller->setAvailableServerFeatures(serverDiscoInfo_);
 	controller->onActivity.connect(boost::bind(&ChatsManager::handleChatActivity, this, contact, _1));
+	controller->onUnreadCountChanged.connect(boost::bind(&ChatsManager::handleUnreadCountChanged, this, controller));
 	return controller;
 }
 
diff --git a/Swift/Controllers/Chat/ChatsManager.h b/Swift/Controllers/Chat/ChatsManager.h
index 24b0f97..ccfdb3a 100644
--- a/Swift/Controllers/Chat/ChatsManager.h
+++ b/Swift/Controllers/Chat/ChatsManager.h
@@ -51,6 +51,7 @@ namespace Swift {
 			void setOnline(bool enabled);
 			void setServerDiscoInfo(boost::shared_ptr<DiscoInfo> info);
 			void handleIncomingMessage(boost::shared_ptr<Message> message);
+			ChatListWindow::Chat createChatListChatItem(const JID& jid, const std::string& activity);
 
 		private:
 			void handleChatRequest(const std::string& contact);
@@ -73,6 +74,7 @@ namespace Swift {
 			void handleChatMadeRecent();
 			void handleMUCBookmarkActivated(const MUCBookmark&);
 			void handleRecentActivated(const ChatListWindow::Chat&);
+			void handleUnreadCountChanged(ChatController* controller);
 
 			ChatController* getChatControllerOrFindAnother(const JID &contact);
 			ChatController* createNewChatController(const JID &contact);
diff --git a/Swift/Controllers/Chat/UnitTest/MockChatListWindow.h b/Swift/Controllers/Chat/UnitTest/MockChatListWindow.h
index 408a490..b1e1329 100644
--- a/Swift/Controllers/Chat/UnitTest/MockChatListWindow.h
+++ b/Swift/Controllers/Chat/UnitTest/MockChatListWindow.h
@@ -18,6 +18,7 @@ namespace Swift {
 			void removeMUCBookmark(const MUCBookmark& /*bookmark*/) {}
 			void setBookmarksEnabled(bool /*enabled*/) {}
 			void setRecents(const std::list<ChatListWindow::Chat>& /*recents*/) {}
+			void setUnreadCount(int unread) {}
 			void clearBookmarks() {}
 	};
 
diff --git a/Swift/Controllers/UIInterfaces/ChatListWindow.h b/Swift/Controllers/UIInterfaces/ChatListWindow.h
index ce75ae8..477de04 100644
--- a/Swift/Controllers/UIInterfaces/ChatListWindow.h
+++ b/Swift/Controllers/UIInterfaces/ChatListWindow.h
@@ -17,14 +17,22 @@ namespace Swift {
 		public:
 			class Chat {
 				public:
-					Chat(const JID& jid, const std::string& chatName, const std::string& activity, bool isMUC, const std::string& nick = "") : jid(jid), chatName(chatName), activity(activity), isMUC(isMUC), nick(nick) {}
-					/** Assume that nicks aren't important for equality */
-					bool operator==(const Chat& other) const {return jid.toBare() == other.jid.toBare() && isMUC == other.isMUC;};
+					Chat(const JID& jid, const std::string& chatName, const std::string& activity, int unreadCount, bool isMUC, const std::string& nick = "")
+					: jid(jid), chatName(chatName), activity(activity), isMUC(isMUC), nick(nick), unreadCount(unreadCount) {}
+					/** Assume that nicks and other transient features aren't important for equality */
+					bool operator==(const Chat& other) const {
+						return jid.toBare() == other.jid.toBare()
+								&& isMUC == other.isMUC;
+					};
+					void setUnreadCount(int unread) {
+						unreadCount = unread;
+					}
 					JID jid;
 					std::string chatName;
 					std::string activity;
 					bool isMUC;
 					std::string nick;
+					int unreadCount;
 			};
 			virtual ~ChatListWindow();
 
@@ -32,6 +40,7 @@ namespace Swift {
 			virtual void addMUCBookmark(const MUCBookmark& bookmark) = 0;
 			virtual void removeMUCBookmark(const MUCBookmark& bookmark) = 0;
 			virtual void setRecents(const std::list<Chat>& recents) = 0;
+			virtual void setUnreadCount(int unread) = 0;
 			virtual void clearBookmarks() = 0;
 
 			boost::signal<void (const MUCBookmark&)> onMUCBookmarkActivated;
diff --git a/Swift/QtUI/ChatList/QtChatListWindow.cpp b/Swift/QtUI/ChatList/QtChatListWindow.cpp
index 7ef6ae5..cfbbcb9 100644
--- a/Swift/QtUI/ChatList/QtChatListWindow.cpp
+++ b/Swift/QtUI/ChatList/QtChatListWindow.cpp
@@ -99,6 +99,10 @@ void QtChatListWindow::setRecents(const std::list<ChatListWindow::Chat>& recents
 	model_->setRecents(recents);
 }
 
+void QtChatListWindow::setUnreadCount(int unread) {
+
+}
+
 void QtChatListWindow::handleRemoveBookmark() {
 	ChatListMUCItem* mucItem = dynamic_cast<ChatListMUCItem*>(contextMenuItem_);
 	if (!mucItem) return;
diff --git a/Swift/QtUI/ChatList/QtChatListWindow.h b/Swift/QtUI/ChatList/QtChatListWindow.h
index f5c12f6..67f2c41 100644
--- a/Swift/QtUI/ChatList/QtChatListWindow.h
+++ b/Swift/QtUI/ChatList/QtChatListWindow.h
@@ -24,6 +24,7 @@ namespace Swift {
 			void removeMUCBookmark(const MUCBookmark& bookmark);
 			void setBookmarksEnabled(bool enabled);
 			void setRecents(const std::list<ChatListWindow::Chat>& recents);
+			void setUnreadCount(int unread);
 			void clearBookmarks();
 		private slots:
 			void handleItemActivated(const QModelIndex&);
-- 
cgit v0.10.2-6-g49f6