From f2266ded1e36e806b0e4b5a6ab434dd0799bdad5 Mon Sep 17 00:00:00 2001
From: Tobias Markmann <tm@ayena.de>
Date: Fri, 22 Apr 2016 16:30:13 +0200
Subject: Mark file-transfer notice as read when chat window is activated

Applied some foreach modernization in process.

Test-Information:

Tested by sending files between two Swift instances and
verifying the new behavior works as expected.

Tested on OS X 10.11.4 with Qt 5.4.2.

Change-Id: Ie3f9c2126d946d9041b08be98a19cbfb468871f3

diff --git a/Swift/Controllers/Chat/ChatController.cpp b/Swift/Controllers/Chat/ChatController.cpp
index dbc5a79..b8bf4c3 100644
--- a/Swift/Controllers/Chat/ChatController.cpp
+++ b/Swift/Controllers/Chat/ChatController.cpp
@@ -14,7 +14,6 @@
 #include <Swiften/Base/Algorithm.h>
 #include <Swiften/Base/DateTime.h>
 #include <Swiften/Base/Log.h>
-#include <Swiften/Base/foreach.h>
 #include <Swiften/Base/format.h>
 #include <Swiften/Chat/ChatStateNotifier.h>
 #include <Swiften/Chat/ChatStateTracker.h>
@@ -44,6 +43,7 @@
 #include <Swift/Controllers/UIEvents/UIEventStream.h>
 #include <Swift/Controllers/UIInterfaces/ChatWindowFactory.h>
 #include <Swift/Controllers/XMPPEvents/EventController.h>
+#include <Swift/Controllers/XMPPEvents/IncomingFileTransferEvent.h>
 
 namespace Swift {
 
@@ -346,9 +346,8 @@ void ChatController::handleStanzaAcked(std::shared_ptr<Stanza> stanza) {
 
 void ChatController::setOnline(bool online) {
     if (!online) {
-        std::map<std::shared_ptr<Stanza>, std::string>::iterator it = unackedStanzas_.begin();
-        for ( ; it != unackedStanzas_.end(); ++it) {
-            chatWindow_->setAckState(it->second, ChatWindow::Failed);
+        for (auto& stanzaIdPair : unackedStanzas_) {
+            chatWindow_->setAckState(stanzaIdPair.second, ChatWindow::Failed);
         }
         unackedStanzas_.clear();
 
@@ -365,6 +364,18 @@ void ChatController::handleNewFileTransferController(FileTransferController* ftc
     std::string ftID = ftc->setChatWindow(chatWindow_, nick);
     ftControllers[ftID] = ftc;
     lastWasPresence_ = false;
+
+    if (ftc->isIncoming()) {
+        auto incomingFileTransferEvent = std::make_shared<IncomingFileTransferEvent>(ftc->getOtherParty());
+        if (hasOpenWindow()) {
+            incomingFileTransferEvent->conclude();
+        }
+        else {
+            unreadMessages_.push_back(incomingFileTransferEvent);
+            updateMessageCount();
+        }
+        eventController_->handleIncomingEvent(incomingFileTransferEvent);
+    }
 }
 
 void ChatController::handleWhiteboardSessionRequest(bool senderIsSelf) {
diff --git a/Swift/Controllers/Chat/ChatControllerBase.cpp b/Swift/Controllers/Chat/ChatControllerBase.cpp
index 80f4167..15f6112 100644
--- a/Swift/Controllers/Chat/ChatControllerBase.cpp
+++ b/Swift/Controllers/Chat/ChatControllerBase.cpp
@@ -17,7 +17,6 @@
 
 #include <Swiften/Avatars/AvatarManager.h>
 #include <Swiften/Base/Path.h>
-#include <Swiften/Base/foreach.h>
 #include <Swiften/Base/format.h>
 #include <Swiften/Client/StanzaChannel.h>
 #include <Swiften/Disco/EntityCapsProvider.h>
@@ -129,12 +128,11 @@ void ChatControllerBase::setAvailableServerFeatures(std::shared_ptr<DiscoInfo> i
 void ChatControllerBase::handleAllMessagesRead() {
     if (!unreadMessages_.empty()) {
         targetedUnreadMessages_.clear();
-        foreach (std::shared_ptr<StanzaEvent> stanzaEvent, unreadMessages_) {
+        for (std::shared_ptr<StanzaEvent> stanzaEvent : unreadMessages_) {
             stanzaEvent->conclude();
         }
         unreadMessages_.clear();
-        chatWindow_->setUnreadMessageCount(0);
-        onUnreadCountChanged();
+        updateMessageCount();
     }
 }
 
@@ -219,7 +217,7 @@ void ChatControllerBase::handleHighlightActions(const ChatWindow::ChatMessage& c
         highlighter_->handleHighlightAction(chatMessage.getFullMessageHighlightAction());
         playedSounds.insert(chatMessage.getFullMessageHighlightAction().getSoundFile());
     }
-    foreach(std::shared_ptr<ChatWindow::ChatMessagePart> part, chatMessage.getParts()) {
+    for (std::shared_ptr<ChatWindow::ChatMessagePart> part : chatMessage.getParts()) {
         std::shared_ptr<ChatWindow::ChatHighlightingMessagePart> highlightMessage = std::dynamic_pointer_cast<ChatWindow::ChatHighlightingMessagePart>(part);
         if (highlightMessage && highlightMessage->action.playSound()) {
             if (playedSounds.find(highlightMessage->action.getSoundFile()) == playedSounds.end()) {
@@ -230,6 +228,11 @@ void ChatControllerBase::handleHighlightActions(const ChatWindow::ChatMessage& c
     }
 }
 
+void ChatControllerBase::updateMessageCount() {
+    chatWindow_->setUnreadMessageCount(boost::numeric_cast<int>(unreadMessages_.size()));
+    onUnreadCountChanged();
+}
+
 std::string ChatControllerBase::addMessage(const ChatWindow::ChatMessage& chatMessage, const std::string& senderName, bool senderIsSelf, const std::shared_ptr<SecurityLabel> label, const boost::filesystem::path& avatarPath, const boost::posix_time::ptime& time) {
     if (chatMessage.isMeCommand()) {
         return chatWindow_->addAction(chatMessage, senderName, senderIsSelf, label, pathToString(avatarPath), time);
@@ -330,8 +333,7 @@ void ChatControllerBase::handleIncomingMessage(std::shared_ptr<MessageEvent> mes
         logMessage(body, from, selfJID_, timeStamp, true);
     }
     chatWindow_->show();
-    chatWindow_->setUnreadMessageCount(boost::numeric_cast<int>(unreadMessages_.size()));
-    onUnreadCountChanged();
+    updateMessageCount();
     postHandleIncomingMessage(messageEvent, chatMessage);
 }
 
@@ -377,8 +379,7 @@ std::string ChatControllerBase::getErrorMessage(std::shared_ptr<ErrorPayload> er
 void ChatControllerBase::handleGeneralMUCInvitation(MUCInviteEvent::ref event) {
     unreadMessages_.push_back(event);
     chatWindow_->show();
-    chatWindow_->setUnreadMessageCount(boost::numeric_cast<int>(unreadMessages_.size()));
-    onUnreadCountChanged();
+    updateMessageCount();
     chatWindow_->addMUCInvitation(senderDisplayNameFromMessage(event->getInviter()), event->getRoomJID(), event->getReason(), event->getPassword(), event->getDirect(), event->getImpromptu());
     eventController_->handleIncomingEvent(event);
 }
diff --git a/Swift/Controllers/Chat/ChatControllerBase.h b/Swift/Controllers/Chat/ChatControllerBase.h
index 309d2fe..71390d9 100644
--- a/Swift/Controllers/Chat/ChatControllerBase.h
+++ b/Swift/Controllers/Chat/ChatControllerBase.h
@@ -97,6 +97,7 @@ namespace Swift {
             virtual void logMessage(const std::string& message, const JID& fromJID, const JID& toJID, const boost::posix_time::ptime& timeStamp, bool isIncoming) = 0;
             ChatWindow::ChatMessage buildChatWindowChatMessage(const std::string& message, bool senderIsSelf, const HighlightAction& fullMessageHighlightAction);
             void handleHighlightActions(const ChatWindow::ChatMessage& chatMessage);
+            void updateMessageCount();
 
         private:
             IDGenerator idGenerator_;
diff --git a/Swift/Controllers/Chat/ChatsManager.cpp b/Swift/Controllers/Chat/ChatsManager.cpp
index ffca925..be0af3e 100644
--- a/Swift/Controllers/Chat/ChatsManager.cpp
+++ b/Swift/Controllers/Chat/ChatsManager.cpp
@@ -20,7 +20,6 @@
 
 #include <Swiften/Avatars/AvatarManager.h>
 #include <Swiften/Base/Log.h>
-#include <Swiften/Base/foreach.h>
 #include <Swiften/Client/ClientBlockListManager.h>
 #include <Swiften/Client/NickResolver.h>
 #include <Swiften/Client/StanzaChannel.h>
@@ -62,7 +61,6 @@
 #include <Swift/Controllers/UIInterfaces/JoinMUCWindowFactory.h>
 #include <Swift/Controllers/WhiteboardManager.h>
 #include <Swift/Controllers/XMPPEvents/EventController.h>
-#include <Swift/Controllers/XMPPEvents/IncomingFileTransferEvent.h>
 
 BOOST_CLASS_VERSION(Swift::ChatListWindow::Chat, 1)
 
@@ -199,10 +197,10 @@ ChatsManager::~ChatsManager() {
     roster_->onJIDUpdated.disconnect(boost::bind(&ChatsManager::handleJIDUpdatedInRoster, this, _1));
     roster_->onRosterCleared.disconnect(boost::bind(&ChatsManager::handleRosterCleared, this));
     delete joinMUCWindow_;
-    foreach (JIDChatControllerPair controllerPair, chatControllers_) {
+    for (JIDChatControllerPair controllerPair : chatControllers_) {
         delete controllerPair.second;
     }
-    foreach (JIDMUCControllerPair controllerPair, mucControllers_) {
+    for (JIDMUCControllerPair controllerPair : mucControllers_) {
         delete controllerPair.second;
     }
     delete mucBookmarkManager_;
@@ -218,7 +216,7 @@ void ChatsManager::saveRecents() {
         recentsLimited.erase(recentsLimited.begin() + 25, recentsLimited.end());
     }
     if (eagleMode_) {
-        foreach(ChatListWindow::Chat& chat, recentsLimited) {
+        for (ChatListWindow::Chat& chat : recentsLimited) {
             chat.activity = "";
         }
     }
@@ -259,7 +257,7 @@ void ChatsManager::handleRosterCleared() {
     /*    Setting that all chat controllers aren't receiving presence anymore;
         including MUC 1-to-1 chats due to the assumtion that this handler
         is only called on log out. */
-    foreach(JIDChatControllerPair pair, chatControllers_) {
+    for (JIDChatControllerPair pair : chatControllers_) {
         pair.second->setContactIsReceivingPresence(false);
     }
 }
@@ -299,7 +297,7 @@ void ChatsManager::loadRecents() {
         std::vector<std::string> recents;
         boost::split(recents, recentsString, boost::is_any_of("\n"));
         int i = 0;
-        foreach (std::string recentString, recents) {
+        for (std::string recentString : recents) {
             if (i++ > 30) {
                 break;
             }
@@ -433,7 +431,7 @@ void ChatsManager::handleUnreadCountChanged(ChatControllerBase* controller) {
     int unreadTotal = 0;
     bool controllerIsMUC = dynamic_cast<MUCController*>(controller);
     bool isPM = controller && !controllerIsMUC && mucRegistry_->isMUC(controller->getToJID().toBare());
-    foreach (ChatListWindow::Chat& chatItem, recentChats_) {
+    for (ChatListWindow::Chat& chatItem : recentChats_) {
         bool match = false;
         if (controller) {
             /* Matching MUC item */
@@ -466,7 +464,7 @@ boost::optional<ChatListWindow::Chat> ChatsManager::removeExistingChat(const Cha
 void ChatsManager::cleanupPrivateMessageRecents() {
     /* if we leave a MUC and close a PM, remove it's recent chat entry */
     const std::list<ChatListWindow::Chat> chats = recentChats_;
-    foreach (const ChatListWindow::Chat& chat, chats) {
+    for (const ChatListWindow::Chat& chat : chats) {
         if (chat.isPrivateMessage) {
             typedef std::map<JID, MUCController*> ControllerMap;
             ControllerMap::iterator muc = mucControllers_.find(chat.jid.toBare());
@@ -503,7 +501,7 @@ void ChatsManager::handleUserLeftMUC(MUCController* mucController) {
     std::map<JID, MUCController*>::iterator it;
     for (it = mucControllers_.begin(); it != mucControllers_.end(); ++it) {
         if ((*it).second == mucController) {
-            foreach (ChatListWindow::Chat& chat, recentChats_) {
+            for (ChatListWindow::Chat& chat : recentChats_) {
                 if (chat.isMUC && chat.jid == (*it).first) {
                     chat.statusType = StatusShow::None;
                 }
@@ -530,7 +528,7 @@ void ChatsManager::finalizeImpromptuJoin(MUC::ref muc, const std::vector<JID>& j
 
     typedef std::pair<std::string, MUCOccupant> StringMUCOccupantPair;
     std::map<std::string, MUCOccupant> occupants = muc->getOccupants();
-    foreach(StringMUCOccupantPair occupant, occupants) {
+    for (StringMUCOccupantPair occupant : occupants) {
         boost::optional<JID> realJID = occupant.second.getRealJID();
         if (realJID) {
             missingJIDsToInvite.erase(std::remove(missingJIDsToInvite.begin(), missingJIDsToInvite.end(), realJID->toBare()), missingJIDsToInvite.end());
@@ -540,7 +538,7 @@ void ChatsManager::finalizeImpromptuJoin(MUC::ref muc, const std::vector<JID>& j
     if (reuseChatJID) {
         muc->invitePerson(reuseChatJID.get(), reason, true, true);
     }
-    foreach(const JID& jid, missingJIDsToInvite) {
+    for (const JID& jid : missingJIDsToInvite) {
         muc->invitePerson(jid, reason, true);
     }
 }
@@ -594,7 +592,7 @@ void ChatsManager::handleUIEvent(std::shared_ptr<UIEvent> event) {
 }
 
 void ChatsManager::markAllRecentsOffline() {
-    foreach (ChatListWindow::Chat& chat, recentChats_) {
+    for (ChatListWindow::Chat& chat : recentChats_) {
         chat.setStatusType(StatusShow::None);
     }
 
@@ -623,7 +621,7 @@ void ChatsManager::handleTransformChatToMUC(ChatController* chatController, Chat
 void ChatsManager::handlePresenceChange(std::shared_ptr<Presence> newPresence) {
     if (mucRegistry_->isMUC(newPresence->getFrom().toBare())) return;
 
-    foreach (ChatListWindow::Chat& chat, recentChats_) {
+    for (ChatListWindow::Chat& chat : recentChats_) {
         if (newPresence->getFrom().toBare() == chat.jid.toBare() && !chat.isMUC) {
             Presence::ref presence = presenceOracle_->getHighestPriorityPresence(chat.jid.toBare());
             chat.setStatusType(presence ? presence->getShow() : StatusShow::None);
@@ -647,7 +645,7 @@ void ChatsManager::setAvatarManager(AvatarManager* avatarManager) {
         avatarManager_->onAvatarChanged.disconnect(boost::bind(&ChatsManager::handleAvatarChanged, this, _1));
     }
     avatarManager_ = avatarManager;
-    foreach (ChatListWindow::Chat& chat, recentChats_) {
+    for (ChatListWindow::Chat& chat : recentChats_) {
         if (!chat.isMUC) {
             chat.setAvatarPath(avatarManager_->getAvatarPath(chat.jid));
         }
@@ -656,7 +654,7 @@ void ChatsManager::setAvatarManager(AvatarManager* avatarManager) {
 }
 
 void ChatsManager::handleAvatarChanged(const JID& jid) {
-    foreach (ChatListWindow::Chat& chat, recentChats_) {
+    for (ChatListWindow::Chat& chat : recentChats_) {
         if (!chat.isMUC && jid.toBare() == chat.jid.toBare()) {
             chat.setAvatarPath(avatarManager_->getAvatarPath(jid));
             break;
@@ -666,10 +664,10 @@ void ChatsManager::handleAvatarChanged(const JID& jid) {
 
 void ChatsManager::setServerDiscoInfo(std::shared_ptr<DiscoInfo> info) {
     serverDiscoInfo_ = info;
-    foreach (JIDChatControllerPair pair, chatControllers_) {
+    for (JIDChatControllerPair pair : chatControllers_) {
         pair.second->setAvailableServerFeatures(info);
     }
-    foreach (JIDMUCControllerPair pair, mucControllers_) {
+    for (JIDMUCControllerPair pair : mucControllers_) {
         pair.second->setAvailableServerFeatures(info);
     }
 }
@@ -678,10 +676,10 @@ void ChatsManager::setServerDiscoInfo(std::shared_ptr<DiscoInfo> info) {
  * This is to be called on connect/disconnect.
  */
 void ChatsManager::setOnline(bool enabled) {
-    foreach (JIDChatControllerPair controllerPair, chatControllers_) {
+    for (JIDChatControllerPair controllerPair : chatControllers_) {
         controllerPair.second->setOnline(enabled);
     }
-    foreach (JIDMUCControllerPair controllerPair, mucControllers_) {
+    for (JIDMUCControllerPair controllerPair : mucControllers_) {
         controllerPair.second->setOnline(enabled);
         if (enabled) {
             controllerPair.second->rejoin();
@@ -712,7 +710,7 @@ void ChatsManager::handleChatRequest(const std::string &contact) {
 ChatController* ChatsManager::getChatControllerOrFindAnother(const JID &contact) {
     ChatController* controller = getChatControllerIfExists(contact);
     if (!controller && !mucRegistry_->isMUC(contact.toBare())) {
-        foreach (JIDChatControllerPair pair, chatControllers_) {
+        for (JIDChatControllerPair pair : chatControllers_) {
             if (pair.first.toBare() == contact.toBare()) {
                 controller = pair.second;
                 break;
@@ -757,7 +755,7 @@ ChatController* ChatsManager::getChatControllerIfExists(const JID &contact, bool
                 return chatControllers_[bare];
             }
         } else {
-            foreach (JIDChatControllerPair pair, chatControllers_) {
+            for (JIDChatControllerPair pair : chatControllers_) {
                 if (pair.first.toBare() == contact.toBare()) {
                     if (rebindIfNeeded) {
                         rebindControllerJID(pair.first, contact);
@@ -944,10 +942,6 @@ void ChatsManager::handleMUCBookmarkActivated(const MUCBookmark& mucBookmark) {
 void ChatsManager::handleNewFileTransferController(FileTransferController* ftc) {
     ChatController* chatController = getChatControllerOrCreate(ftc->getOtherParty());
     chatController->handleNewFileTransferController(ftc);
-    chatController->activateChatWindow();
-    if (ftc->isIncoming()) {
-        eventController_->handleIncomingEvent(std::make_shared<IncomingFileTransferEvent>(ftc->getOtherParty()));
-    }
 }
 
 void ChatsManager::handleWhiteboardSessionRequest(const JID& contact, bool senderIsSelf) {
@@ -977,7 +971,7 @@ void ChatsManager::handleRecentActivated(const ChatListWindow::Chat& chat) {
     if (chat.isMUC && !chat.impromptuJIDs.empty()) {
         typedef std::pair<std::string, JID> StringJIDPair;
         std::vector<JID> inviteJIDs;
-        foreach(StringJIDPair pair, chat.impromptuJIDs) {
+        for (StringJIDPair pair : chat.impromptuJIDs) {
             inviteJIDs.push_back(pair.second);
         }
         uiEventStream_->send(std::make_shared<CreateImpromptuMUCUIEvent>(inviteJIDs, chat.jid, ""));
@@ -992,7 +986,7 @@ void ChatsManager::handleRecentActivated(const ChatListWindow::Chat& chat) {
 }
 
 void ChatsManager::handleLocalServiceFound(const JID& service, std::shared_ptr<DiscoInfo> info) {
-    foreach (DiscoInfo::Identity identity, info->getIdentities()) {
+    for (DiscoInfo::Identity identity : info->getIdentities()) {
             if ((identity.getCategory() == "directory"
                 && identity.getType() == "chatroom")
                 || (identity.getCategory() == "conference"
@@ -1007,10 +1001,10 @@ void ChatsManager::handleLocalServiceFound(const JID& service, std::shared_ptr<D
 
 void ChatsManager::handleLocalServiceWalkFinished() {
     bool impromptuMUCSupported = !localMUCServiceJID_.toString().empty();
-    foreach (JIDChatControllerPair controllerPair, chatControllers_) {
+    for (JIDChatControllerPair controllerPair : chatControllers_) {
         controllerPair.second->setCanStartImpromptuChats(impromptuMUCSupported);
     }
-    foreach (JIDMUCControllerPair controllerPair, mucControllers_) {
+    for (JIDMUCControllerPair controllerPair : mucControllers_) {
         controllerPair.second->setCanStartImpromptuChats(impromptuMUCSupported);
     }
     onImpromptuMUCServiceDiscovered(impromptuMUCSupported);
@@ -1022,7 +1016,7 @@ std::vector<ChatListWindow::Chat> ChatsManager::getRecentChats() const {
 
 std::vector<Contact::ref> Swift::ChatsManager::getContacts(bool withMUCNicks) {
     std::vector<Contact::ref> result;
-    foreach (ChatListWindow::Chat chat, recentChats_) {
+    for (ChatListWindow::Chat chat : recentChats_) {
         if (!chat.isMUC) {
             result.push_back(std::make_shared<Contact>(chat.chatName.empty() ? chat.jid.toString() : chat.chatName, chat.jid, chat.statusType, chat.avatarPath));
         }
@@ -1030,11 +1024,11 @@ std::vector<Contact::ref> Swift::ChatsManager::getContacts(bool withMUCNicks) {
     if (withMUCNicks) {
         /* collect MUC nicks */
         typedef std::map<JID, MUCController*>::value_type Item;
-        foreach (const Item& item, mucControllers_) {
+        for (const Item& item : mucControllers_) {
             JID mucJID = item.second->getToJID();
             std::map<std::string, JID> participants = item.second->getParticipantJIDs();
             typedef std::map<std::string, JID>::value_type ParticipantType;
-            foreach (const ParticipantType& participant, participants) {
+            for (const ParticipantType& participant : participants) {
                 const JID nickJID = JID(mucJID.getNode(), mucJID.getDomain(), participant.first);
                 Presence::ref presence = presenceOracle_->getLastPresence(nickJID);
                 const boost::filesystem::path avatar = avatarManager_->getAvatarPath(nickJID);
-- 
cgit v0.10.2-6-g49f6