diff options
Diffstat (limited to 'Swift/Controllers')
287 files changed, 19981 insertions, 13661 deletions
diff --git a/Swift/Controllers/AccountController.cpp b/Swift/Controllers/AccountController.cpp new file mode 100644 index 0000000..a257cc4 --- /dev/null +++ b/Swift/Controllers/AccountController.cpp @@ -0,0 +1,873 @@ +/* + * Copyright (c) 2010-2019 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#include <Swift/Controllers/AccountController.h> + +#include <cstdlib> +#include <memory> + +#include <boost/bind.hpp> +#include <boost/lexical_cast.hpp> + +#include <Swiften/Base/Algorithm.h> +#include <Swiften/Base/Log.h> +#include <Swiften/Base/String.h> +#include <Swiften/Base/format.h> +#include <Swiften/Client/Client.h> +#include <Swiften/Client/ClientBlockListManager.h> +#include <Swiften/Client/ClientXMLTracer.h> +#include <Swiften/Client/NickResolver.h> +#include <Swiften/Client/StanzaChannel.h> +#include <Swiften/Client/Storages.h> +#include <Swiften/Crypto/CryptoProvider.h> +#include <Swiften/Disco/CapsInfoGenerator.h> +#include <Swiften/Disco/ClientDiscoManager.h> +#include <Swiften/Disco/GetDiscoInfoRequest.h> +#include <Swiften/Elements/ChatState.h> +#include <Swiften/Elements/DiscoInfo.h> +#include <Swiften/Elements/Presence.h> +#include <Swiften/Elements/VCardUpdate.h> +#include <Swiften/FileTransfer/FileTransferManager.h> +#include <Swiften/Network/NetworkFactories.h> +#include <Swiften/Network/TimerFactory.h> +#include <Swiften/Presence/PresenceSender.h> +#include <Swiften/Queries/Requests/EnableCarbonsRequest.h> +#include <Swiften/StringCodecs/Base64.h> +#include <Swiften/StringCodecs/Hexify.h> +#include <Swiften/VCards/GetVCardRequest.h> +#include <Swiften/VCards/VCardManager.h> + +#ifdef SWIFTEN_PLATFORM_WIN32 +#include <Swiften/SASL/WindowsAuthentication.h> +#endif + +#include <Swift/Controllers/AdHocManager.h> +#include <Swift/Controllers/BlockListController.h> +#include <Swift/Controllers/BuildVersion.h> +#include <Swift/Controllers/Chat/Chattables.h> +#include <Swift/Controllers/Chat/ChatsManager.h> +#include <Swift/Controllers/Chat/MUCController.h> +#include <Swift/Controllers/Chat/UserSearchController.h> +#include <Swift/Controllers/ContactEditController.h> +#include <Swift/Controllers/ContactSuggester.h> +#include <Swift/Controllers/ContactsFromXMPPRoster.h> +#include <Swift/Controllers/EventNotifier.h> +#include <Swift/Controllers/EventWindowController.h> +#include <Swift/Controllers/FdpFormSubmitController.h> +#include <Swift/Controllers/FileTransfer/FileTransferOverview.h> +#include <Swift/Controllers/FileTransferListController.h> +#include <Swift/Controllers/Highlighting/HighlightEditorController.h> +#include <Swift/Controllers/Highlighting/HighlightManager.h> +#include <Swift/Controllers/HistoryController.h> +#include <Swift/Controllers/HistoryViewController.h> +#include <Swift/Controllers/Intl.h> +#include <Swift/Controllers/PresenceNotifier.h> +#include <Swift/Controllers/ProfileController.h> +#include <Swift/Controllers/Roster/RosterController.h> +#include <Swift/Controllers/SettingConstants.h> +#include <Swift/Controllers/Settings/SettingsProvider.h> +#include <Swift/Controllers/ShowProfileController.h> +#include <Swift/Controllers/SoundEventController.h> +#include <Swift/Controllers/SoundPlayer.h> +#include <Swift/Controllers/StatusTracker.h> +#include <Swift/Controllers/Storages/CertificateStorageFactory.h> +#include <Swift/Controllers/Storages/CertificateStorageTrustChecker.h> +#include <Swift/Controllers/Storages/StoragesFactory.h> +#include <Swift/Controllers/SystemTray.h> +#include <Swift/Controllers/SystemTrayController.h> +#include <Swift/Controllers/UIEvents/JoinMUCUIEvent.h> +#include <Swift/Controllers/UIEvents/RequestChatUIEvent.h> +#include <Swift/Controllers/UIEvents/UIEventStream.h> +#include <Swift/Controllers/UIInterfaces/LoginWindow.h> +#include <Swift/Controllers/UIInterfaces/LoginWindowFactory.h> +#include <Swift/Controllers/UIInterfaces/MainWindow.h> +#include <Swift/Controllers/UIInterfaces/UIFactory.h> +#include <Swift/Controllers/WhiteboardManager.h> +#include <Swift/Controllers/XMLConsoleController.h> +#include <Swift/Controllers/XMPPEvents/EventController.h> +#include <Swift/Controllers/XMPPURIController.h> + +#include <SwifTools/Dock/Dock.h> +#include <SwifTools/Idle/IdleDetector.h> +#include <SwifTools/Notifier/TogglableNotifier.h> + +namespace Swift { + +static const std::string CLIENT_NAME = "Swift"; +static const std::string CLIENT_NODE = "http://swift.im"; + + +AccountController::AccountController( + EventLoop* eventLoop, + NetworkFactories* networkFactories, + UIFactory* uiFactories, + SettingsProvider* settings, + SystemTray* systemTray, + SoundPlayer* soundPlayer, + StoragesFactory* storagesFactory, + CertificateStorageFactory* certificateStorageFactory, + Dock* dock, + Notifier* notifier, + URIHandler* uriHandler, + IdleDetector* idleDetector, + const std::map<std::string, std::string>& emoticons, + bool useDelayForLatency) : + eventLoop_(eventLoop), + networkFactories_(networkFactories), + uiFactory_(uiFactories), + storagesFactory_(storagesFactory), + certificateStorageFactory_(certificateStorageFactory), + settings_(settings), + uriHandler_(uriHandler), + idleDetector_(idleDetector), + loginWindow_(nullptr) , + useDelayForLatency_(useDelayForLatency), + ftOverview_(nullptr), + emoticons_(emoticons), + fdpFormSubmitController_(nullptr) { + storages_ = nullptr; + certificateStorage_ = nullptr; + certificateTrustChecker_ = nullptr; + statusTracker_ = nullptr; + presenceNotifier_ = nullptr; + eventNotifier_ = nullptr; + rosterController_ = nullptr; + chatsManager_ = nullptr; + historyController_ = nullptr; + historyViewController_ = nullptr; + eventWindowController_ = nullptr; + profileController_ = nullptr; + blockListController_ = nullptr; + showProfileController_ = nullptr; + contactEditController_ = nullptr; + userSearchControllerChat_ = nullptr; + userSearchControllerAdd_ = nullptr; + userSearchControllerInvite_ = nullptr; + contactsFromRosterProvider_ = nullptr; + contactSuggesterWithoutRoster_ = nullptr; + contactSuggesterWithRoster_ = nullptr; + whiteboardManager_ = nullptr; + adHocManager_ = nullptr; + quitRequested_ = false; + clientInitialized_ = false; + offlineRequested_ = false; + + timeBeforeNextReconnect_ = -1; + dock_ = dock; + uiEventStream_ = new UIEventStream(); + + notifier_ = new TogglableNotifier(notifier); + notifier_->setPersistentEnabled(settings_->getSetting(SettingConstants::SHOW_NOTIFICATIONS)); + eventController_ = new EventController(); + eventController_->onEventQueueLengthChange.connect(boost::bind(&AccountController::handleEventQueueLengthChange, this, _1)); + + systemTrayController_ = new SystemTrayController(eventController_, systemTray); + loginWindow_ = uiFactory_->createLoginWindow(uiEventStream_); + loginWindow_->setShowNotificationToggle(!notifier->isExternallyConfigured()); + + highlightManager_ = new HighlightManager(settings_); + highlightEditorController_ = new HighlightEditorController(uiEventStream_, uiFactory_, highlightManager_); + + soundEventController_ = new SoundEventController(eventController_, soundPlayer, settings, highlightManager_); + + xmppURIController_ = new XMPPURIController(uriHandler_, uiEventStream_); + + loginWindow_->onLoginRequest.connect(boost::bind(&AccountController::handleLoginRequest, this, _1, _2, _3, _4, _5, _6, _7)); + loginWindow_->onPurgeSavedLoginRequest.connect(boost::bind(&AccountController::handlePurgeSavedLoginRequest, this, _1)); + loginWindow_->onCancelLoginRequest.connect(boost::bind(&AccountController::handleCancelLoginRequest, this)); + loginWindow_->onQuitRequest.connect(boost::bind(&AccountController::handleQuitRequest, this)); + + idleDetector_->setIdleTimeSeconds(settings->getSetting(SettingConstants::IDLE_TIMEOUT)); + idleDetector_->onIdleChanged.connect(boost::bind(&AccountController::handleInputIdleChanged, this, _1)); + + xmlConsoleController_ = new XMLConsoleController(uiEventStream_, uiFactory_); + + fileTransferListController_ = new FileTransferListController(uiEventStream_, uiFactory_); + + settings_->onSettingChanged.connect(boost::bind(&AccountController::handleSettingChanged, this, _1)); + +} + +AccountController::~AccountController() { + idleDetector_->onIdleChanged.disconnect(boost::bind(&AccountController::handleInputIdleChanged, this, _1)); + + purgeCachedCredentials(); + //setManagersOffline(); + eventController_->disconnectAll(); + + resetClient(); + delete highlightEditorController_; + delete highlightManager_; + delete fileTransferListController_; + delete xmlConsoleController_; + delete xmppURIController_; + delete soundEventController_; + delete systemTrayController_; + delete eventController_; + delete notifier_; + delete uiEventStream_; +} + +void AccountController::purgeCachedCredentials() { + safeClear(password_); +} + +void AccountController::resetClient() { + purgeCachedCredentials(); + resetCurrentError(); + resetPendingReconnects(); + vCardPhotoHash_.clear(); + delete contactEditController_; + contactEditController_ = nullptr; + delete profileController_; + profileController_ = nullptr; + delete showProfileController_; + showProfileController_ = nullptr; + delete eventWindowController_; + eventWindowController_ = nullptr; + delete chatsManager_; + chatsManager_ = nullptr; +#ifdef SWIFT_EXPERIMENTAL_HISTORY + delete historyViewController_; + historyViewController_ = nullptr; + delete historyController_; + historyController_ = nullptr; +#endif + fileTransferListController_->setFileTransferOverview(nullptr); + delete ftOverview_; + ftOverview_ = nullptr; + delete blockListController_; + blockListController_ = nullptr; + delete rosterController_; + rosterController_ = nullptr; + chattables_.reset(); + delete eventNotifier_; + eventNotifier_ = nullptr; + delete presenceNotifier_; + presenceNotifier_ = nullptr; + delete certificateTrustChecker_; + certificateTrustChecker_ = nullptr; + delete certificateStorage_; + certificateStorage_ = nullptr; + delete storages_; + storages_ = nullptr; + delete statusTracker_; + statusTracker_ = nullptr; + delete profileSettings_; + profileSettings_ = nullptr; + delete userSearchControllerChat_; + userSearchControllerChat_ = nullptr; + delete userSearchControllerAdd_; + userSearchControllerAdd_ = nullptr; + delete userSearchControllerInvite_; + userSearchControllerInvite_ = nullptr; + delete contactSuggesterWithoutRoster_; + contactSuggesterWithoutRoster_ = nullptr; + delete contactSuggesterWithRoster_; + contactSuggesterWithRoster_ = nullptr; + delete contactsFromRosterProvider_; + contactsFromRosterProvider_ = nullptr; + delete adHocManager_; + adHocManager_ = nullptr; + delete whiteboardManager_; + whiteboardManager_ = nullptr; + clientInitialized_ = false; +} + +void AccountController::handleSettingChanged(const std::string& settingPath) { + if (settingPath == SettingConstants::SHOW_NOTIFICATIONS.getKey()) { + notifier_->setPersistentEnabled(settings_->getSetting(SettingConstants::SHOW_NOTIFICATIONS)); + } +} + +void AccountController::resetPendingReconnects() { + timeBeforeNextReconnect_ = -1; + if (reconnectTimer_) { + reconnectTimer_->stop(); + reconnectTimer_.reset(); + } + resetCurrentError(); +} + +void AccountController::resetCurrentError() { + if (lastDisconnectError_) { + lastDisconnectError_->conclude(); + lastDisconnectError_ = std::shared_ptr<ErrorEvent>(); + } +} + +void AccountController::handleConnected() { + boundJID_ = client_->getJID(); + resetCurrentError(); + resetPendingReconnects(); + + if (settings_->getSetting(SettingConstants::FORGET_PASSWORDS)) { + purgeCachedCredentials(); + } + + bool freshLogin = rosterController_ == nullptr; + myStatusLooksOnline_ = true; + if (freshLogin) { + profileController_ = new ProfileController(client_->getVCardManager(), uiFactory_, uiEventStream_); + showProfileController_ = new ShowProfileController(client_->getVCardManager(), uiFactory_, uiEventStream_); + ftOverview_ = new FileTransferOverview(client_->getFileTransferManager()); + fileTransferListController_->setFileTransferOverview(ftOverview_); + chattables_ = std::make_unique<Chattables>(); + rosterController_ = new RosterController(boundJID_, client_->getRoster(), client_->getAvatarManager(), uiFactory_, client_->getNickManager(), client_->getNickResolver(), client_->getPresenceOracle(), client_->getSubscriptionManager(), eventController_, uiEventStream_, client_->getIQRouter(), settings_, client_->getEntityCapsProvider(), client_->getClientBlockListManager(), client_->getVCardManager(), *chattables_); + rosterController_->onChangeStatusRequest.connect(boost::bind(&AccountController::handleChangeStatusRequest, this, _1, _2)); + rosterController_->onSignOutRequest.connect(boost::bind(&AccountController::signOut, this)); + rosterController_->getWindow()->onShowCertificateRequest.connect(boost::bind(&AccountController::handleShowCertificateRequest, this)); + + blockListController_ = new BlockListController(client_->getClientBlockListManager(), uiEventStream_, uiFactory_, eventController_); + + contactEditController_ = new ContactEditController(rosterController_, client_->getVCardManager(), uiFactory_, uiEventStream_); + whiteboardManager_ = new WhiteboardManager(uiFactory_, uiEventStream_, client_->getNickResolver(), client_->getWhiteboardSessionManager()); + + /* Doing this early as an ordering fix. Various things later will + * want to have the user's nick available and this means it will + * be before they receive stanzas that need it (e.g. bookmarks).*/ + client_->getVCardManager()->requestOwnVCard(); + + contactSuggesterWithoutRoster_ = new ContactSuggester(); + contactSuggesterWithRoster_ = new ContactSuggester(); + + userSearchControllerInvite_ = new UserSearchController(UserSearchController::Type::InviteToChat, jid_, uiEventStream_, client_->getVCardManager(), uiFactory_, client_->getIQRouter(), rosterController_, contactSuggesterWithRoster_, client_->getAvatarManager(), client_->getPresenceOracle(), profileSettings_); +#ifdef SWIFT_EXPERIMENTAL_HISTORY + historyController_ = new HistoryController(storages_->getHistoryStorage()); + historyViewController_ = new HistoryViewController(jid_, uiEventStream_, historyController_, client_->getNickResolver(), client_->getAvatarManager(), client_->getPresenceOracle(), uiFactory_); + chatsManager_ = new ChatsManager(jid_, client_->getStanzaChannel(), client_->getIQRouter(), eventController_, uiFactory_, uiFactory_, client_->getNickResolver(), client_->getPresenceOracle(), client_->getPresenceSender(), uiEventStream_, uiFactory_, useDelayForLatency_, networkFactories_->getTimerFactory(), client_->getMUCRegistry(), client_->getEntityCapsProvider(), client_->getMUCManager(), uiFactory_, profileSettings_, ftOverview_, client_->getRoster(), !settings_->getSetting(SettingConstants::REMEMBER_RECENT_CHATS), settings_, historyController_, whiteboardManager_, highlightManager_, client_->getClientBlockListManager(), emoticons_, client_->getVCardManager()); +#else + chatsManager_ = new ChatsManager(jid_, client_->getStanzaChannel(), client_->getIQRouter(), eventController_, uiFactory_, uiFactory_, client_->getNickResolver(), client_->getPresenceOracle(), client_->getPresenceSender(), uiEventStream_, uiFactory_, useDelayForLatency_, networkFactories_->getTimerFactory(), client_->getMUCRegistry(), client_->getEntityCapsProvider(), client_->getMUCManager(), uiFactory_, profileSettings_, ftOverview_, client_->getRoster(), !settings_->getSetting(SettingConstants::REMEMBER_RECENT_CHATS), settings_, nullptr, whiteboardManager_, highlightManager_, client_->getClientBlockListManager(), emoticons_, client_->getVCardManager(), *chattables_); +#endif + contactsFromRosterProvider_ = new ContactsFromXMPPRoster(client_->getRoster(), client_->getAvatarManager(), client_->getPresenceOracle()); + contactSuggesterWithoutRoster_->addContactProvider(chatsManager_); + contactSuggesterWithRoster_->addContactProvider(chatsManager_); + contactSuggesterWithRoster_->addContactProvider(contactsFromRosterProvider_); + highlightEditorController_->setContactSuggester(contactSuggesterWithoutRoster_); + + client_->onMessageReceived.connect(boost::bind(&ChatsManager::handleIncomingMessage, chatsManager_, _1)); + chatsManager_->setAvatarManager(client_->getAvatarManager()); + + eventWindowController_ = new EventWindowController(eventController_, uiFactory_); + + loginWindow_->morphInto(rosterController_->getWindow()); + + DiscoInfo discoInfo; + discoInfo.addIdentity(DiscoInfo::Identity(CLIENT_NAME, "client", "pc")); + discoInfo.addFeature(DiscoInfo::ChatStatesFeature); + discoInfo.addFeature(DiscoInfo::SecurityLabelsFeature); + discoInfo.addFeature(DiscoInfo::MessageCorrectionFeature); +#ifdef SWIFT_EXPERIMENTAL_FT + discoInfo.addFeature(DiscoInfo::JingleFeature); + discoInfo.addFeature(DiscoInfo::JingleFTFeature); + discoInfo.addFeature(DiscoInfo::JingleTransportsIBBFeature); + discoInfo.addFeature(DiscoInfo::JingleTransportsS5BFeature); +#endif +#ifdef SWIFT_EXPERIMENTAL_WB + discoInfo.addFeature(DiscoInfo::WhiteboardFeature); +#endif + discoInfo.addFeature(DiscoInfo::MessageDeliveryReceiptsFeature); + client_->getDiscoManager()->setCapsNode(CLIENT_NODE); + client_->getDiscoManager()->setDiscoInfo(discoInfo); + + userSearchControllerChat_ = new UserSearchController(UserSearchController::Type::StartChat, jid_, uiEventStream_, client_->getVCardManager(), uiFactory_, client_->getIQRouter(), rosterController_, contactSuggesterWithRoster_, client_->getAvatarManager(), client_->getPresenceOracle(), profileSettings_); + userSearchControllerAdd_ = new UserSearchController(UserSearchController::Type::AddContact, jid_, uiEventStream_, client_->getVCardManager(), uiFactory_, client_->getIQRouter(), rosterController_, contactSuggesterWithoutRoster_, client_->getAvatarManager(), client_->getPresenceOracle(), profileSettings_); + adHocManager_ = new AdHocManager(JID(boundJID_.getDomain()), uiFactory_, client_->getIQRouter(), uiEventStream_, rosterController_->getWindow()); + + chatsManager_->onImpromptuMUCServiceDiscovered.connect(boost::bind(&UserSearchController::setCanInitiateImpromptuMUC, userSearchControllerChat_, _1)); + } + loginWindow_->setIsLoggingIn(false); + + client_->requestRoster(); + + GetDiscoInfoRequest::ref discoInfoRequest = GetDiscoInfoRequest::create(JID(boundJID_.getDomain()), client_->getIQRouter()); + discoInfoRequest->onResponse.connect(boost::bind(&AccountController::handleServerDiscoInfoResponse, this, _1, _2)); + discoInfoRequest->send(); + + client_->getVCardManager()->requestOwnVCard(); + + rosterController_->setJID(boundJID_); + rosterController_->setEnabled(true); + rosterController_->getWindow()->setStreamEncryptionStatus(client_->isStreamEncrypted()); + profileController_->setAvailable(true); + contactEditController_->setAvailable(true); + /* Send presence later to catch all the incoming presences. */ + sendPresence(statusTracker_->getNextPresence()); + + /* Enable chats last of all, so rejoining MUCs has the right sent presence */ + assert(chatsManager_); + chatsManager_->setOnline(true); + adHocManager_->setOnline(true); +} + +void AccountController::handleEventQueueLengthChange(size_t count) { + dock_->setNumberOfPendingMessages(count); +} + +void AccountController::reconnectAfterError() { + if (reconnectTimer_) { + reconnectTimer_->stop(); + } + performLoginFromCachedCredentials(); +} + +void AccountController::handleChangeStatusRequest(StatusShow::Type show, const std::string &statusText) { + std::shared_ptr<Presence> presence(new Presence()); + if (show == StatusShow::None) { + // Note: this is misleading, None doesn't mean unavailable on the wire. + presence->setType(Presence::Unavailable); + resetPendingReconnects(); + myStatusLooksOnline_ = false; + offlineRequested_ = true; + } + else { + offlineRequested_ = false; + presence->setShow(show); + } + presence->setStatus(statusText); + statusTracker_->setRequestedPresence(presence); + if (presence->getType() != Presence::Unavailable) { + profileSettings_->storeInt("lastShow", presence->getShow()); + profileSettings_->storeString("lastStatus", presence->getStatus()); + } + if (presence->getType() != Presence::Unavailable && !client_->isAvailable()) { + performLoginFromCachedCredentials(); + } else { + sendPresence(presence); + } +} + +void AccountController::sendPresence(std::shared_ptr<Presence> presence) { + rosterController_->getWindow()->setMyStatusType(presence->getShow()); + rosterController_->getWindow()->setMyStatusText(presence->getStatus()); + systemTrayController_->setMyStatusType(presence->getShow()); + notifier_->setTemporarilyDisabled(presence->getShow() == StatusShow::DND); + + // Add information and send + presence->updatePayload(std::make_shared<VCardUpdate>(vCardPhotoHash_)); + client_->getPresenceSender()->sendPresence(presence); + if (presence->getType() == Presence::Unavailable) { + logout(); + } +} + +void AccountController::handleInputIdleChanged(bool idle) { + if (!statusTracker_) { + //Haven't logged in yet. + return; + } + + if (settings_->getSetting(SettingConstants::IDLE_GOES_OFFLINE)) { + if (idle) { + logout(); + } + } + else { + if (idle) { + if (statusTracker_->goAutoAway(idleDetector_->getIdleTimeSeconds())) { + if (client_ && client_->isAvailable()) { + sendPresence(statusTracker_->getNextPresence()); + } + } + } else { + if (statusTracker_->goAutoUnAway()) { + if (client_ && client_->isAvailable()) { + sendPresence(statusTracker_->getNextPresence()); + } + } + } + } +} + +void AccountController::handleShowCertificateRequest() { + std::vector<Certificate::ref> chain = client_->getStanzaChannel()->getPeerCertificateChain(); + rosterController_->getWindow()->openCertificateDialog(chain); +} + +void AccountController::handleLoginRequest(const std::string &username, const std::string &password, const std::string& certificatePath, CertificateWithKey::ref certificate, const ClientOptions& options, bool remember, bool loginAutomatically) { + jid_ = JID(username); + if (options.singleSignOn && (!jid_.isValid() || !jid_.getNode().empty())) { + loginWindow_->setMessage(QT_TRANSLATE_NOOP("", "User address invalid. User address should be of the form 'wonderland.lit'")); + loginWindow_->setIsLoggingIn(false); + } else if (!options.singleSignOn && (!jid_.isValid() || jid_.getNode().empty())) { + loginWindow_->setMessage(QT_TRANSLATE_NOOP("", "User address invalid. User address should be of the form 'alice@wonderland.lit'")); + loginWindow_->setIsLoggingIn(false); + } else { +#ifdef SWIFTEN_PLATFORM_WIN32 + if (options.singleSignOn) { + std::string userName; + std::string clientName; + std::string serverName; + std::shared_ptr<boost::system::error_code> errorCode = getUserNameEx(userName, clientName, serverName); + + if (!errorCode) { + /* Create JID using the Windows logon name and user provided domain name */ + jid_ = JID(clientName, username); + } + else { + loginWindow_->setMessage(str(format(QT_TRANSLATE_NOOP("", "Error obtaining Windows user name (%1%)")) % errorCode->message())); + loginWindow_->setIsLoggingIn(false); + return; + } + } +#endif + + loginWindow_->setMessage(""); + loginWindow_->setIsLoggingIn(true); + profileSettings_ = new ProfileSettingsProvider(username, settings_); + if (!settings_->getSetting(SettingConstants::FORGET_PASSWORDS)) { + profileSettings_->storeString("jid", username); + profileSettings_->storeString("certificate", certificatePath); + profileSettings_->storeString("pass", (remember || loginAutomatically) ? password : ""); + std::string optionString = serializeClientOptions(options); + profileSettings_->storeString("options", optionString); + profileSettings_->storeInt("enabled", 1); + loginWindow_->addAvailableAccount(profileSettings_->getStringSetting("jid"), profileSettings_->getStringSetting("pass"), profileSettings_->getStringSetting("certificate"), options); + } + + password_ = password; + certificate_ = certificate; + clientOptions_ = options; + performLoginFromCachedCredentials(); + } +} + +void AccountController::handlePurgeSavedLoginRequest(const std::string& username) { + settings_->removeProfile(username); + loginWindow_->removeAvailableAccount(username); +} + +void AccountController::performLoginFromCachedCredentials() { + if (settings_->getSetting(SettingConstants::FORGET_PASSWORDS) && password_.empty()) { + /* Then we can't try to login again. */ + return; + } + /* If we logged in with a bare JID, and we have a full bound JID, re-login with the + * bound JID to try and keep dynamically assigned resources */ + JID clientJID = jid_; + if (boundJID_.isValid() && jid_.isBare() && boundJID_.toBare() == jid_) { + clientJID = boundJID_; + } + + if (!statusTracker_) { + statusTracker_ = new StatusTracker(); + } + if (!clientInitialized_) { + storages_ = storagesFactory_->createStorages(jid_.toBare()); + certificateStorage_ = certificateStorageFactory_->createCertificateStorage(jid_.toBare()); + certificateTrustChecker_ = new CertificateStorageTrustChecker(certificateStorage_); + + client_ = std::make_shared<Swift::Client>(clientJID, createSafeByteArray(password_.c_str()), networkFactories_, storages_); + clientInitialized_ = true; + client_->setCertificateTrustChecker(certificateTrustChecker_); + client_->onDataRead.connect(boost::bind(&XMLConsoleController::handleDataRead, xmlConsoleController_, _1)); + client_->onDataWritten.connect(boost::bind(&XMLConsoleController::handleDataWritten, xmlConsoleController_, _1)); + client_->onDisconnected.connect(boost::bind(&AccountController::handleDisconnected, this, _1)); + client_->onConnected.connect(boost::bind(&AccountController::handleConnected, this)); + + client_->setSoftwareVersion(CLIENT_NAME, buildVersion); + + client_->getVCardManager()->onVCardChanged.connect(boost::bind(&AccountController::handleVCardReceived, this, _1, _2)); + presenceNotifier_ = new PresenceNotifier(client_->getStanzaChannel(), notifier_, client_->getMUCRegistry(), client_->getAvatarManager(), client_->getNickResolver(), client_->getPresenceOracle(), networkFactories_->getTimerFactory()); + presenceNotifier_->onNotificationActivated.connect(boost::bind(&AccountController::handleNotificationClicked, this, _1)); + eventNotifier_ = new EventNotifier(eventController_, notifier_, client_->getAvatarManager(), client_->getNickResolver()); + eventNotifier_->onNotificationActivated.connect(boost::bind(&AccountController::handleNotificationClicked, this, _1)); + if (certificate_) { + client_->setCertificate(certificate_); + } + std::shared_ptr<Presence> presence(new Presence()); + presence->setShow(static_cast<StatusShow::Type>(profileSettings_->getIntSetting("lastShow", StatusShow::Online))); + presence->setStatus(profileSettings_->getStringSetting("lastStatus")); + statusTracker_->setRequestedPresence(presence); + fdpFormSubmitController_ = std::make_unique<FdpFormSubmitController>(jid_, client_->getIQRouter(), uiEventStream_, uiFactory_); + } else { + /* In case we're in the middle of another login, make sure they don't overlap */ + client_->disconnect(); + } + systemTrayController_->setConnecting(); + if (rosterController_) { + rosterController_->getWindow()->setConnecting(); + } + ClientOptions clientOptions = clientOptions_; + bool eagle = settings_->getSetting(SettingConstants::FORGET_PASSWORDS); + clientOptions.forgetPassword = eagle; + clientOptions.useTLS = eagle ? ClientOptions::RequireTLS : clientOptions_.useTLS; + client_->connect(clientOptions); +} + +void AccountController::handleDisconnected(const boost::optional<ClientError>& error) { + if (rosterController_) { + rosterController_->getWindow()->setStreamEncryptionStatus(false); + } + if (adHocManager_) { + adHocManager_->setOnline(false); + } + if (settings_->getSetting(SettingConstants::FORGET_PASSWORDS)) { + purgeCachedCredentials(); + } + if (quitRequested_) { + resetClient(); + loginWindow_->quit(); + } + else if (error) { + std::string message; + std::string certificateErrorMessage; + bool forceSignout = false; + switch(error->getType()) { + case ClientError::UnknownError: message = QT_TRANSLATE_NOOP("", "Unknown Error"); break; + case ClientError::DomainNameResolveError: message = QT_TRANSLATE_NOOP("", "Unable to find server"); break; + case ClientError::ConnectionError: message = QT_TRANSLATE_NOOP("", "Error connecting to server"); break; + case ClientError::ConnectionReadError: message = QT_TRANSLATE_NOOP("", "Error while receiving server data"); break; + case ClientError::ConnectionWriteError: message = QT_TRANSLATE_NOOP("", "Error while sending data to the server"); break; + case ClientError::XMLError: message = QT_TRANSLATE_NOOP("", "Error parsing server data"); break; + case ClientError::AuthenticationFailedError: message = QT_TRANSLATE_NOOP("", "Login/password invalid"); break; + case ClientError::CompressionFailedError: message = QT_TRANSLATE_NOOP("", "Error while compressing stream"); break; + case ClientError::ServerVerificationFailedError: message = QT_TRANSLATE_NOOP("", "Server verification failed"); break; + case ClientError::NoSupportedAuthMechanismsError: message = QT_TRANSLATE_NOOP("", "Authentication mechanisms not supported"); break; + case ClientError::UnexpectedElementError: message = QT_TRANSLATE_NOOP("", "Unexpected response"); break; + case ClientError::ResourceBindError: message = QT_TRANSLATE_NOOP("", "Error binding resource"); break; + case ClientError::SessionStartError: message = QT_TRANSLATE_NOOP("", "Error starting session"); break; + case ClientError::StreamError: message = QT_TRANSLATE_NOOP("", "Stream error"); break; + case ClientError::TLSError: message = QT_TRANSLATE_NOOP("", "Encryption error"); break; + case ClientError::ClientCertificateLoadError: message = QT_TRANSLATE_NOOP("", "Error loading certificate (Invalid file or password?)"); break; + case ClientError::ClientCertificateError: message = QT_TRANSLATE_NOOP("", "Certificate not authorized"); break; + case ClientError::CertificateCardRemoved: message = QT_TRANSLATE_NOOP("", "Certificate card removed"); forceSignout = true; break; + + case ClientError::UnknownCertificateError: certificateErrorMessage = QT_TRANSLATE_NOOP("", "Unknown certificate"); break; + case ClientError::CertificateExpiredError: certificateErrorMessage = QT_TRANSLATE_NOOP("", "Certificate has expired"); break; + case ClientError::CertificateNotYetValidError: certificateErrorMessage = QT_TRANSLATE_NOOP("", "Certificate is not yet valid"); break; + case ClientError::CertificateSelfSignedError: certificateErrorMessage = QT_TRANSLATE_NOOP("", "Certificate is self-signed"); break; + case ClientError::CertificateRejectedError: certificateErrorMessage = QT_TRANSLATE_NOOP("", "Certificate has been rejected"); break; + case ClientError::CertificateUntrustedError: certificateErrorMessage = QT_TRANSLATE_NOOP("", "Certificate is not trusted"); break; + case ClientError::InvalidCertificatePurposeError: certificateErrorMessage = QT_TRANSLATE_NOOP("", "Certificate cannot be used for encrypting your connection"); break; + case ClientError::CertificatePathLengthExceededError: certificateErrorMessage = QT_TRANSLATE_NOOP("", "Certificate path length constraint exceeded"); break; + case ClientError::InvalidCertificateSignatureError: certificateErrorMessage = QT_TRANSLATE_NOOP("", "Invalid certificate signature"); break; + case ClientError::InvalidCAError: certificateErrorMessage = QT_TRANSLATE_NOOP("", "Invalid Certificate Authority"); break; + case ClientError::InvalidServerIdentityError: certificateErrorMessage = QT_TRANSLATE_NOOP("", "Certificate does not match the host identity"); break; + case ClientError::RevokedError: certificateErrorMessage = QT_TRANSLATE_NOOP("", "Certificate has been revoked"); break; + case ClientError::RevocationCheckFailedError: certificateErrorMessage = QT_TRANSLATE_NOOP("", "Unable to determine certificate revocation state"); break; + } + bool forceReconnectAfterCertificateTrust = false; + if (!certificateErrorMessage.empty()) { + std::vector<Certificate::ref> certificates = certificateTrustChecker_->getLastCertificateChain(); + if (!certificates.empty() && loginWindow_->askUserToTrustCertificatePermanently(certificateErrorMessage, certificates)) { + certificateStorage_->addCertificate(certificates[0]); + forceReconnectAfterCertificateTrust = true; + } + else { + message = QT_TRANSLATE_NOOP("", "Certificate error"); + } + } + + if (!message.empty() && error->getErrorCode()) { + message = str(format(QT_TRANSLATE_NOOP("", "%1% (%2%)")) % message % error->getErrorCode()->message()); + } + + if (forceReconnectAfterCertificateTrust && settings_->getSetting(SettingConstants::FORGET_PASSWORDS)) { + forceReconnectAfterCertificateTrust = false; + forceSignout = true; + message = QT_TRANSLATE_NOOP("", "Re-enter credentials and retry"); + } + + if (forceReconnectAfterCertificateTrust) { + performLoginFromCachedCredentials(); + } + else if (forceSignout || !rosterController_) { //hasn't been logged in yet or permanent error + signOut(); + loginWindow_->setMessage(message); + loginWindow_->setIsLoggingIn(false); + } else { + logout(); + if (settings_->getSetting(SettingConstants::FORGET_PASSWORDS)) { + message = str(format(QT_TRANSLATE_NOOP("", "Disconnected from %1%: %2%. To reconnect, Sign Out and provide your password again.")) % jid_.getDomain() % message); + } else { + if (!offlineRequested_) { + setReconnectTimer(); + } + if (lastDisconnectError_) { + message = str(format(QT_TRANSLATE_NOOP("", "Reconnect to %1% failed: %2%. Will retry in %3% seconds.")) % jid_.getDomain() % message % std::to_string(timeBeforeNextReconnect_)); + lastDisconnectError_->conclude(); + } else { + message = str(format(QT_TRANSLATE_NOOP("", "Disconnected from %1%: %2%.")) % jid_.getDomain() % message); + } + lastDisconnectError_ = std::make_shared<ErrorEvent>(JID(jid_.getDomain()), message); + eventController_->handleIncomingEvent(lastDisconnectError_); + } + } + } + else if (!rosterController_) { //hasn't been logged in yet + loginWindow_->setIsLoggingIn(false); + } +} + +void AccountController::setReconnectTimer() { + if (timeBeforeNextReconnect_ < 0) { + timeBeforeNextReconnect_ = 1; + } else { + timeBeforeNextReconnect_ = timeBeforeNextReconnect_ >= 150 ? 300 : timeBeforeNextReconnect_ * 2; // Randomly selected by roll of a die, as required by 3920bis + } + if (reconnectTimer_) { + reconnectTimer_->stop(); + } + reconnectTimer_ = networkFactories_->getTimerFactory()->createTimer(timeBeforeNextReconnect_ * 1000); + reconnectTimer_->onTick.connect(boost::bind(&AccountController::reconnectAfterError, this)); + reconnectTimer_->start(); +} + +void AccountController::handleCancelLoginRequest() { + signOut(); +} + +void AccountController::signOut() { + if (settings_->getSetting(SettingConstants::FORGET_PASSWORDS)) { + purgeCachedCredentials(); + } + profileSettings_->storeInt("enabled", 0); + eventController_->clear(); + logout(); + loginWindow_->loggedOut(); + resetClient(); +} + +void AccountController::logout() { + if (settings_->getSetting(SettingConstants::FORGET_PASSWORDS)) { + purgeCachedCredentials(); + } + systemTrayController_->setMyStatusType(StatusShow::None); + if (clientInitialized_ /*&& client_->isAvailable()*/) { + client_->disconnect(); + } + if (rosterController_ && myStatusLooksOnline_) { + rosterController_->getWindow()->setMyStatusType(StatusShow::None); + rosterController_->getWindow()->setMyStatusText(""); + myStatusLooksOnline_ = false; + } + setManagersOffline(); +} + +void AccountController::setManagersOffline() { + if (chatsManager_) { + chatsManager_->setOnline(false); + } + if (rosterController_) { + rosterController_->setEnabled(false); + } + if (profileController_) { + profileController_->setAvailable(false); + } + if (contactEditController_) { + contactEditController_->setAvailable(false); + } +} + +void AccountController::handleServerDiscoInfoResponse(std::shared_ptr<DiscoInfo> info, ErrorPayload::ref error) { + if (!error) { + chatsManager_->setServerDiscoInfo(info); + adHocManager_->setServerDiscoInfo(info); + if (info->hasFeature(DiscoInfo::BlockingCommandFeature)) { + rosterController_->getWindow()->setBlockingCommandAvailable(true); + rosterController_->initBlockingCommand(); + } + if (info->hasFeature(DiscoInfo::MessageCarbonsFeature)) { + enableMessageCarbons(); + } + } +} + +void AccountController::enableMessageCarbons() { + auto enableCarbonsRequest = EnableCarbonsRequest::create(client_->getIQRouter()); + enableCarbonsRequestHandlerConnection_ = enableCarbonsRequest->onResponse.connect([&](Payload::ref /*payload*/, ErrorPayload::ref error) { + if (error) { + SWIFT_LOG(warning) << "Failed to enable carbons."; + } + else { + SWIFT_LOG(debug) << "Successfully enabled carbons."; + } + enableCarbonsRequestHandlerConnection_.disconnect(); + }); + enableCarbonsRequest->send(); +} + +void AccountController::handleVCardReceived(const JID& jid, VCard::ref vCard) { + if (!jid.equals(jid_, JID::WithoutResource) || !vCard) { + return; + } + std::string hash; + if (!vCard->getPhoto().empty()) { + hash = Hexify::hexify(networkFactories_->getCryptoProvider()->getSHA1Hash(vCard->getPhoto())); + } + if (hash != vCardPhotoHash_) { + vCardPhotoHash_ = hash; + if (client_ && client_->isAvailable()) { + sendPresence(statusTracker_->getNextPresence()); + } + } +} + +void AccountController::handleNotificationClicked(const JID& jid) { + assert(chatsManager_); + if (clientInitialized_) { + if (client_->getMUCRegistry()->isMUC(jid)) { + uiEventStream_->send(std::make_shared<JoinMUCUIEvent>(jid)); + } + else { + uiEventStream_->send(std::make_shared<RequestChatUIEvent>(jid)); + } + } +} + +void AccountController::handleQuitRequest() { + if (client_ && client_->isActive()) { + quitRequested_ = true; + client_->disconnect(); + } + else { + resetClient(); + loginWindow_->quit(); + } +} + +//FIXME: Switch all this to boost::serialise + +#define SERIALIZE_BOOL(option) result += options.option ? "1" : "0"; result += ","; +#define SERIALIZE_INT(option) result += boost::lexical_cast<std::string>(options.option); result += ","; +#define SERIALIZE_STRING(option) result += Base64::encode(createByteArray(options.option)); result += ","; +#define SERIALIZE_SAFE_STRING(option) result += safeByteArrayToString(Base64::encode(options.option)); result += ","; +#define SERIALIZE_URL(option) SERIALIZE_STRING(option.toString()) + +std::string AccountController::serializeClientOptions(const ClientOptions& options) { + std::string result; + SERIALIZE_BOOL(useStreamCompression) + switch (options.useTLS) { + case ClientOptions::NeverUseTLS: result += "1";break; + case ClientOptions::UseTLSWhenAvailable: result += "2";break; + case ClientOptions::RequireTLS: result += "3";break; + } + result += ","; + SERIALIZE_BOOL(allowPLAINWithoutTLS) + SERIALIZE_BOOL(useStreamResumption) + SERIALIZE_BOOL(useAcks) + SERIALIZE_STRING(manualHostname) + SERIALIZE_INT(manualPort) + switch (options.proxyType) { + case ClientOptions::NoProxy: result += "1";break; + case ClientOptions::SystemConfiguredProxy: result += "2";break; + case ClientOptions::SOCKS5Proxy: result += "3";break; + case ClientOptions::HTTPConnectProxy: result += "4";break; + } + result += ","; + SERIALIZE_STRING(manualProxyHostname) + SERIALIZE_INT(manualProxyPort) + SERIALIZE_URL(boshURL) + SERIALIZE_URL(boshHTTPConnectProxyURL) + SERIALIZE_SAFE_STRING(boshHTTPConnectProxyAuthID) + SERIALIZE_SAFE_STRING(boshHTTPConnectProxyAuthPassword) + SERIALIZE_BOOL(tlsOptions.schannelTLS1_0Workaround) + SERIALIZE_BOOL(singleSignOn) + return result; +} + +} diff --git a/Swift/Controllers/AccountController.h b/Swift/Controllers/AccountController.h new file mode 100644 index 0000000..4a31645 --- /dev/null +++ b/Swift/Controllers/AccountController.h @@ -0,0 +1,203 @@ +/* + * Copyright (c) 2010-2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#pragma once + +#include <map> +#include <memory> +#include <string> +#include <vector> + +#include <boost/signals2.hpp> + +#include <Swiften/Client/ClientError.h> +#include <Swiften/Client/ClientXMLTracer.h> +#include <Swiften/Elements/CapsInfo.h> +#include <Swiften/Elements/DiscoInfo.h> +#include <Swiften/Elements/ErrorPayload.h> +#include <Swiften/Elements/Presence.h> +#include <Swiften/Elements/VCard.h> +#include <Swiften/JID/JID.h> +#include <Swiften/Network/Timer.h> + +#include <Swift/Controllers/ProfileSettingsProvider.h> +#include <Swift/Controllers/Settings/SettingsProvider.h> +#include <Swift/Controllers/UIEvents/UIEvent.h> +#include <Swift/Controllers/XMPPEvents/ErrorEvent.h> + +namespace Swift { + class IdleDetector; + class UIFactory; + class EventLoop; + class Client; + class Chattables; + class ChatController; + class ChatsManager; + class CertificateStorageFactory; + class CertificateStorage; + class CertificateStorageTrustChecker; + class EventController; + class MainWindow; + class RosterController; + class LoginWindow; + class EventLoop; + class MUCController; + class Notifier; + class ProfileController; + class ShowProfileController; + class ContactEditController; + class TogglableNotifier; + class PresenceNotifier; + class EventNotifier; + class SystemTray; + class SystemTrayController; + class SoundEventController; + class SoundPlayer; + class XMLConsoleController; + class HistoryViewController; + class HistoryController; + class FileTransferListController; + class UIEventStream; + class EventWindowFactory; + class EventWindowController; + class MUCSearchController; + class UserSearchController; + class StatusTracker; + class Dock; + class Storages; + class StoragesFactory; + class NetworkFactories; + class URIHandler; + class XMPPURIController; + class AdHocManager; + class AdHocCommandWindowFactory; + class FileTransferOverview; + class WhiteboardManager; + class HighlightManager; + class HighlightEditorController; + class BlockListController; + class ContactSuggester; + class ContactsFromXMPPRoster; + class FdpFormSubmitController; + + class AccountController { + public: + AccountController( + EventLoop* eventLoop, + NetworkFactories* networkFactories, + UIFactory* uiFactories, + SettingsProvider *settings, + SystemTray* systemTray, + SoundPlayer* soundPlayer, + StoragesFactory* storagesFactory, + CertificateStorageFactory* certificateStorageFactory, + Dock* dock, + Notifier* notifier, + URIHandler* uriHandler, + IdleDetector* idleDetector, + const std::map<std::string, std::string>& emoticons, + bool useDelayForLatency); + ~AccountController(); + + private: + void resetClient(); + void handleConnected(); + void handleLoginRequest(const std::string& username, const std::string& password, const std::string& certificatePath, CertificateWithKey::ref certificate, const ClientOptions& options, bool remember, bool loginAutomatically); + void handleCancelLoginRequest(); + void handleQuitRequest(); + void handleChangeStatusRequest(StatusShow::Type show, const std::string &statusText); + void handleDisconnected(const boost::optional<ClientError>& error); + void handleServerDiscoInfoResponse(std::shared_ptr<DiscoInfo>, ErrorPayload::ref); + void handleEventQueueLengthChange(size_t count); + void handleVCardReceived(const JID& j, VCard::ref vCard); + void handleSettingChanged(const std::string& settingPath); + void handlePurgeSavedLoginRequest(const std::string& username); + void sendPresence(std::shared_ptr<Presence> presence); + void handleInputIdleChanged(bool); + void handleShowCertificateRequest(); + void logout(); + void signOut(); + void setReconnectTimer(); + void resetPendingReconnects(); + void resetCurrentError(); + void enableMessageCarbons(); + + void performLoginFromCachedCredentials(); + void reconnectAfterError(); + void setManagersOffline(); + void handleNotificationClicked(const JID& jid); + void handleForceQuit(); + void purgeCachedCredentials(); + std::string serializeClientOptions(const ClientOptions& options); + + private: + EventLoop* eventLoop_; + NetworkFactories* networkFactories_; + UIFactory* uiFactory_; + StoragesFactory* storagesFactory_; + Storages* storages_; + CertificateStorageFactory* certificateStorageFactory_; + CertificateStorage* certificateStorage_; + CertificateStorageTrustChecker* certificateTrustChecker_; + bool clientInitialized_; + std::shared_ptr<Client> client_; + SettingsProvider *settings_; + ProfileSettingsProvider* profileSettings_ = nullptr; + Dock* dock_; + URIHandler* uriHandler_; + IdleDetector* idleDetector_; + TogglableNotifier* notifier_; + PresenceNotifier* presenceNotifier_; + EventNotifier* eventNotifier_; + std::unique_ptr<Chattables> chattables_; + RosterController* rosterController_; + EventController* eventController_; + EventWindowController* eventWindowController_; + AdHocManager* adHocManager_; + LoginWindow* loginWindow_; + UIEventStream* uiEventStream_; + XMLConsoleController* xmlConsoleController_; + HistoryViewController* historyViewController_; + HistoryController* historyController_; + FileTransferListController* fileTransferListController_; + BlockListController* blockListController_; + ChatsManager* chatsManager_; + ProfileController* profileController_; + ShowProfileController* showProfileController_; + ContactEditController* contactEditController_; + ContactsFromXMPPRoster* contactsFromRosterProvider_; + ContactSuggester* contactSuggesterWithoutRoster_; + ContactSuggester* contactSuggesterWithRoster_; + JID jid_; + JID boundJID_; + SystemTrayController* systemTrayController_; + SoundEventController* soundEventController_; + XMPPURIController* xmppURIController_; + std::string vCardPhotoHash_; + std::string password_; + CertificateWithKey::ref certificate_; + ClientOptions clientOptions_; + std::shared_ptr<ErrorEvent> lastDisconnectError_; + bool useDelayForLatency_; + UserSearchController* userSearchControllerChat_; + UserSearchController* userSearchControllerAdd_; + UserSearchController* userSearchControllerInvite_; + int timeBeforeNextReconnect_; + Timer::ref reconnectTimer_; + StatusTracker* statusTracker_; + bool myStatusLooksOnline_ = false; + bool quitRequested_; + bool offlineRequested_; + static const int SecondsToWaitBeforeForceQuitting; + FileTransferOverview* ftOverview_; + WhiteboardManager* whiteboardManager_; + HighlightManager* highlightManager_; + HighlightEditorController* highlightEditorController_; + std::map<std::string, std::string> emoticons_; + boost::signals2::connection enableCarbonsRequestHandlerConnection_; + std::unique_ptr<FdpFormSubmitController> fdpFormSubmitController_; + }; +} diff --git a/Swift/Controllers/AdHocController.cpp b/Swift/Controllers/AdHocController.cpp new file mode 100644 index 0000000..5e10beb --- /dev/null +++ b/Swift/Controllers/AdHocController.cpp @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#include <Swift/Controllers/AdHocController.h> + +#include <boost/bind.hpp> + +#include <Swift/Controllers/UIInterfaces/AdHocCommandWindowFactory.h> + +namespace Swift { + +AdHocController::AdHocController(AdHocCommandWindowFactory* factory, std::shared_ptr<OutgoingAdHocCommandSession> command) { + window_ = factory->createAdHocCommandWindow(command); + window_->onClosing.connect(boost::bind(&AdHocController::handleWindowClosed, this)); +} + +AdHocController::~AdHocController() { + window_->onClosing.disconnect(boost::bind(&AdHocController::handleWindowClosed, this)); + delete window_; +} + +void AdHocController::setOnline(bool online) { + window_->setOnline(online); +} + +void AdHocController::handleWindowClosed() { + onDeleting(); +} + +} diff --git a/Swift/Controllers/AdHocController.h b/Swift/Controllers/AdHocController.h new file mode 100644 index 0000000..a6a5c70 --- /dev/null +++ b/Swift/Controllers/AdHocController.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#pragma once + +#include <memory> + +#include <Swiften/AdHoc/OutgoingAdHocCommandSession.h> + +namespace Swift { + +class AdHocCommandWindowFactory; +class AdHocCommandWindow; + +class AdHocController { +public: + AdHocController(AdHocCommandWindowFactory* factory, std::shared_ptr<OutgoingAdHocCommandSession> command); + ~AdHocController(); + boost::signals2::signal<void ()> onDeleting; + void setOnline(bool online); +private: + void handleWindowClosed(); + AdHocCommandWindow* window_; +}; + +} diff --git a/Swift/Controllers/AdHocManager.cpp b/Swift/Controllers/AdHocManager.cpp index e926138..717f083 100644 --- a/Swift/Controllers/AdHocManager.cpp +++ b/Swift/Controllers/AdHocManager.cpp @@ -1,70 +1,94 @@ /* - * Copyright (c) 2010-2011 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #include <Swift/Controllers/AdHocManager.h> +#include <memory> + #include <boost/bind.hpp> -#include <boost/shared_ptr.hpp> -#include <boost/smart_ptr/make_shared.hpp> -#include <Swiften/Base/foreach.h> -#include <Swiften/Queries/IQRouter.h> #include <Swiften/AdHoc/OutgoingAdHocCommandSession.h> -#include <Swift/Controllers/UIInterfaces/MainWindow.h> -#include <Swift/Controllers/UIInterfaces/AdHocCommandWindowFactory.h> -#include <Swift/Controllers/UIEvents/UIEventStream.h> +#include <Swiften/Queries/IQRouter.h> + #include <Swift/Controllers/UIEvents/RequestAdHocUIEvent.h> +#include <Swift/Controllers/UIEvents/RequestAdHocWithJIDUIEvent.h> +#include <Swift/Controllers/UIEvents/UIEventStream.h> +#include <Swift/Controllers/UIInterfaces/AdHocCommandWindowFactory.h> +#include <Swift/Controllers/UIInterfaces/MainWindow.h> namespace Swift { AdHocManager::AdHocManager(const JID& jid, AdHocCommandWindowFactory* factory, IQRouter* iqRouter, UIEventStream* uiEventStream, MainWindow* mainWindow) : jid_(jid) { - iqRouter_ = iqRouter; - uiEventStream_ = uiEventStream; - mainWindow_ = mainWindow; - factory_ = factory; + iqRouter_ = iqRouter; + uiEventStream_ = uiEventStream; + mainWindow_ = mainWindow; + factory_ = factory; - uiEventStream_->onUIEvent.connect(boost::bind(&AdHocManager::handleUIEvent, this, _1)); + uiEventStream_->onUIEvent.connect(boost::bind(&AdHocManager::handleUIEvent, this, _1)); } AdHocManager::~AdHocManager() { - uiEventStream_->onUIEvent.disconnect(boost::bind(&AdHocManager::handleUIEvent, this, _1)); + uiEventStream_->onUIEvent.disconnect(boost::bind(&AdHocManager::handleUIEvent, this, _1)); + for (auto& controller : controllers_) { + controller->onDeleting.disconnect(boost::bind(&AdHocManager::removeController, this, controller)); + } } -void AdHocManager::setServerDiscoInfo(boost::shared_ptr<DiscoInfo> info) { - if (iqRouter_->isAvailable() && info->hasFeature(DiscoInfo::CommandsFeature)) { - if (discoItemsRequest_) { - discoItemsRequest_->onResponse.disconnect(boost::bind(&AdHocManager::handleServerDiscoItemsResponse, this, _1, _2)); - discoItemsRequest_.reset(); - } - discoItemsRequest_ = GetDiscoItemsRequest::create(JID(jid_.getDomain()), DiscoInfo::CommandsFeature, iqRouter_); - discoItemsRequest_->onResponse.connect(boost::bind(&AdHocManager::handleServerDiscoItemsResponse, this, _1, _2)); - discoItemsRequest_->send(); - } else { - mainWindow_->setAvailableAdHocCommands(std::vector<DiscoItems::Item>()); - } +void AdHocManager::removeController(std::shared_ptr<AdHocController> controller) { + controller->onDeleting.disconnect(boost::bind(&AdHocManager::removeController, this, controller)); + controllers_.erase(std::find(controllers_.begin(), controllers_.end(), controller)); +} + +void AdHocManager::setServerDiscoInfo(std::shared_ptr<DiscoInfo> info) { + if (iqRouter_->isAvailable() && info->hasFeature(DiscoInfo::CommandsFeature)) { + if (discoItemsRequest_) { + discoItemsRequest_->onResponse.disconnect(boost::bind(&AdHocManager::handleServerDiscoItemsResponse, this, _1, _2)); + discoItemsRequest_.reset(); + } + discoItemsRequest_ = GetDiscoItemsRequest::create(JID(jid_.getDomain()), DiscoInfo::CommandsFeature, iqRouter_); + discoItemsRequest_->onResponse.connect(boost::bind(&AdHocManager::handleServerDiscoItemsResponse, this, _1, _2)); + discoItemsRequest_->send(); + } else { + mainWindow_->setAvailableAdHocCommands(std::vector<DiscoItems::Item>()); + } +} +void AdHocManager::setOnline(bool online) { + for (auto&& controller : controllers_) { + controller->setOnline(online); + } } -void AdHocManager::handleServerDiscoItemsResponse(boost::shared_ptr<DiscoItems> items, ErrorPayload::ref error) { - std::vector<DiscoItems::Item> commands; - if (!error) { - foreach (DiscoItems::Item item, items->getItems()) { - if (item.getNode() != "http://isode.com/xmpp/commands#test") { - commands.push_back(item); - } - } - } - mainWindow_->setAvailableAdHocCommands(commands); +void AdHocManager::handleServerDiscoItemsResponse(std::shared_ptr<DiscoItems> items, ErrorPayload::ref error) { + std::vector<DiscoItems::Item> commands; + if (!error) { + for (const auto& item : items->getItems()) { + if (item.getNode() != "http://isode.com/xmpp/commands#test") { + commands.push_back(item); + } + } + } + mainWindow_->setAvailableAdHocCommands(commands); } -void AdHocManager::handleUIEvent(boost::shared_ptr<UIEvent> event) { - boost::shared_ptr<RequestAdHocUIEvent> adHocEvent = boost::dynamic_pointer_cast<RequestAdHocUIEvent>(event); - if (adHocEvent) { - factory_->createAdHocCommandWindow(boost::make_shared<OutgoingAdHocCommandSession>(adHocEvent->getCommand().getJID(), adHocEvent->getCommand().getNode(), iqRouter_)); - } +void AdHocManager::handleUIEvent(std::shared_ptr<UIEvent> event) { + std::shared_ptr<RequestAdHocUIEvent> adHocEvent = std::dynamic_pointer_cast<RequestAdHocUIEvent>(event); + if (adHocEvent) { + std::shared_ptr<OutgoingAdHocCommandSession> command = std::make_shared<OutgoingAdHocCommandSession>(adHocEvent->getCommand().getJID(), adHocEvent->getCommand().getNode(), iqRouter_); + std::shared_ptr<AdHocController> controller = std::make_shared<AdHocController>(factory_, command); + controller->onDeleting.connect(boost::bind(&AdHocManager::removeController, this, controller)); + controllers_.push_back(controller); + } + std::shared_ptr<RequestAdHocWithJIDUIEvent> adHocJIDEvent = std::dynamic_pointer_cast<RequestAdHocWithJIDUIEvent>(event); + if (!!adHocJIDEvent) { + std::shared_ptr<OutgoingAdHocCommandSession> command = std::make_shared<OutgoingAdHocCommandSession>(adHocJIDEvent->getJID(), adHocJIDEvent->getNode(), iqRouter_); + std::shared_ptr<AdHocController> controller = std::make_shared<AdHocController>(factory_, command); + controller->onDeleting.connect(boost::bind(&AdHocManager::removeController, this, controller)); + controllers_.push_back(controller); + } } } diff --git a/Swift/Controllers/AdHocManager.h b/Swift/Controllers/AdHocManager.h index 47b03cd..0786370 100644 --- a/Swift/Controllers/AdHocManager.h +++ b/Swift/Controllers/AdHocManager.h @@ -1,40 +1,51 @@ /* - * Copyright (c) 2010-2011 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <boost/shared_ptr.hpp> #include <vector> -#include <Swiften/Base/boost_bsignals.h> -#include <Swiften/JID/JID.h> +#include <boost/signals2.hpp> + +#include <Swiften/Disco/GetDiscoItemsRequest.h> #include <Swiften/Elements/DiscoInfo.h> #include <Swiften/Elements/DiscoItems.h> #include <Swiften/Elements/ErrorPayload.h> -#include <Swiften/Disco/GetDiscoItemsRequest.h> +#include <Swiften/JID/JID.h> + +#include <Swift/Controllers/AdHocController.h> #include <Swift/Controllers/UIEvents/UIEvent.h> namespace Swift { - class IQRouter; - class MainWindow; - class UIEventStream; - class AdHocCommandWindowFactory; - class AdHocManager { - public: - AdHocManager(const JID& jid, AdHocCommandWindowFactory* factory, IQRouter* iqRouter, UIEventStream* uiEventStream, MainWindow* mainWindow); - ~AdHocManager(); - void setServerDiscoInfo(boost::shared_ptr<DiscoInfo> info); - private: - void handleUIEvent(boost::shared_ptr<UIEvent> event); - void handleServerDiscoItemsResponse(boost::shared_ptr<DiscoItems>, ErrorPayload::ref error); - JID jid_; - IQRouter* iqRouter_; - UIEventStream* uiEventStream_; - MainWindow* mainWindow_; - AdHocCommandWindowFactory* factory_; - GetDiscoItemsRequest::ref discoItemsRequest_; - }; +class IQRouter; +class MainWindow; +class UIEventStream; +class AdHocCommandWindowFactory; + +class AdHocManager { +public: + AdHocManager(const JID& jid, AdHocCommandWindowFactory* factory, IQRouter* iqRouter, UIEventStream* uiEventStream, MainWindow* mainWindow); + ~AdHocManager(); + void removeController(std::shared_ptr<AdHocController> contoller); + void setServerDiscoInfo(std::shared_ptr<DiscoInfo> info); + void setOnline(bool online); + +private: + void handleServerDiscoItemsResponse(std::shared_ptr<DiscoItems>, ErrorPayload::ref error); + void handleUIEvent(std::shared_ptr<UIEvent> event); + boost::signals2::signal<void (const AdHocController&)> onControllerComplete; + +private: + JID jid_; + IQRouter* iqRouter_; + UIEventStream* uiEventStream_; + MainWindow* mainWindow_; + AdHocCommandWindowFactory* factory_; + GetDiscoItemsRequest::ref discoItemsRequest_; + std::vector<std::shared_ptr<AdHocController> > controllers_; +}; + } diff --git a/Swift/Controllers/ApplicationInfo.h b/Swift/Controllers/ApplicationInfo.h index 8e6db31..788383a 100644 --- a/Swift/Controllers/ApplicationInfo.h +++ b/Swift/Controllers/ApplicationInfo.h @@ -1,7 +1,7 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once diff --git a/Swift/Controllers/BlockListController.cpp b/Swift/Controllers/BlockListController.cpp index e7bc45d..37c536b 100644 --- a/Swift/Controllers/BlockListController.cpp +++ b/Swift/Controllers/BlockListController.cpp @@ -4,156 +4,182 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ +/* + * Copyright (c) 2014-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + #include <Swift/Controllers/BlockListController.h> #include <boost/bind.hpp> +#include <Swiften/Base/format.h> #include <Swiften/Client/ClientBlockListManager.h> -#include <Swiften/Base/foreach.h> -#include <Swiften/Base/format.h> #include <Swift/Controllers/Intl.h> -#include <Swift/Controllers/UIEvents/RequestChangeBlockStateUIEvent.h> #include <Swift/Controllers/UIEvents/RequestBlockListDialogUIEvent.h> -#include <Swift/Controllers/XMPPEvents/ErrorEvent.h> +#include <Swift/Controllers/UIEvents/RequestChangeBlockStateUIEvent.h> #include <Swift/Controllers/UIInterfaces/BlockListEditorWidget.h> +#include <Swift/Controllers/XMPPEvents/ErrorEvent.h> #include <Swift/Controllers/XMPPEvents/EventController.h> namespace Swift { -BlockListController::BlockListController(ClientBlockListManager* blockListManager, UIEventStream* uiEventStream, BlockListEditorWidgetFactory* blockListEditorWidgetFactory, EventController* eventController) : blockListManager_(blockListManager), blockListEditorWidgetFactory_(blockListEditorWidgetFactory), blockListEditorWidget_(0), eventController_(eventController), remainingRequests_(0) { - uiEventStream->onUIEvent.connect(boost::bind(&BlockListController::handleUIEvent, this, _1)); - blockListManager_->getBlockList()->onItemAdded.connect(boost::bind(&BlockListController::handleBlockListChanged, this)); - blockListManager_->getBlockList()->onItemRemoved.connect(boost::bind(&BlockListController::handleBlockListChanged, this)); +BlockListController::BlockListController(ClientBlockListManager* blockListManager, UIEventStream* uiEventStream, BlockListEditorWidgetFactory* blockListEditorWidgetFactory, EventController* eventController) : blockListManager_(blockListManager), blockListEditorWidgetFactory_(blockListEditorWidgetFactory), blockListEditorWidget_(nullptr), eventController_(eventController), remainingRequests_(0), uiEventStream_(uiEventStream) { + uiEventStream->onUIEvent.connect(boost::bind(&BlockListController::handleUIEvent, this, _1)); + blockListManager_->getBlockList()->onItemAdded.connect(boost::bind(&BlockListController::handleBlockListChanged, this)); + blockListManager_->getBlockList()->onItemRemoved.connect(boost::bind(&BlockListController::handleBlockListChanged, this)); } BlockListController::~BlockListController() { - blockListManager_->getBlockList()->onItemAdded.disconnect(boost::bind(&BlockListController::handleBlockListChanged, this)); - blockListManager_->getBlockList()->onItemRemoved.disconnect(boost::bind(&BlockListController::handleBlockListChanged, this)); + uiEventStream_->onUIEvent.disconnect(boost::bind(&BlockListController::handleUIEvent, this, _1)); + blockListManager_->getBlockList()->onItemAdded.disconnect(boost::bind(&BlockListController::handleBlockListChanged, this)); + blockListManager_->getBlockList()->onItemRemoved.disconnect(boost::bind(&BlockListController::handleBlockListChanged, this)); } void BlockListController::blockListDifferences(const std::vector<JID> &newBlockList, std::vector<JID> &jidsToUnblock, std::vector<JID> &jidsToBlock) const { - foreach (const JID& jid, blockListBeforeEdit) { - if (std::find(newBlockList.begin(), newBlockList.end(), jid) == newBlockList.end()) { - jidsToUnblock.push_back(jid); - } - } - - foreach (const JID& jid, newBlockList) { - if (std::find(blockListBeforeEdit.begin(), blockListBeforeEdit.end(), jid) == blockListBeforeEdit.end()) { - jidsToBlock.push_back(jid); - } - } + for (const auto& jid : blockListBeforeEdit) { + if (std::find(newBlockList.begin(), newBlockList.end(), jid) == newBlockList.end()) { + jidsToUnblock.push_back(jid); + } + } + + for (const auto& jid : newBlockList) { + if (std::find(blockListBeforeEdit.begin(), blockListBeforeEdit.end(), jid) == blockListBeforeEdit.end()) { + jidsToBlock.push_back(jid); + } + } } -void BlockListController::handleUIEvent(boost::shared_ptr<UIEvent> rawEvent) { - // handle UI dialog - boost::shared_ptr<RequestBlockListDialogUIEvent> requestDialogEvent = boost::dynamic_pointer_cast<RequestBlockListDialogUIEvent>(rawEvent); - if (requestDialogEvent != NULL) { - if (blockListEditorWidget_ == NULL) { - blockListEditorWidget_ = blockListEditorWidgetFactory_->createBlockListEditorWidget(); - blockListEditorWidget_->onSetNewBlockList.connect(boost::bind(&BlockListController::handleSetNewBlockList, this, _1)); - } - blockListBeforeEdit = blockListManager_->getBlockList()->getItems(); - blockListEditorWidget_->setCurrentBlockList(blockListBeforeEdit); - blockListEditorWidget_->show(); - return; - } - - // handle block state change - boost::shared_ptr<RequestChangeBlockStateUIEvent> changeStateEvent = boost::dynamic_pointer_cast<RequestChangeBlockStateUIEvent>(rawEvent); - if (changeStateEvent != NULL) { - if (changeStateEvent->getBlockState() == RequestChangeBlockStateUIEvent::Blocked) { - GenericRequest<BlockPayload>::ref blockRequest = blockListManager_->createBlockJIDRequest(changeStateEvent->getContact()); - blockRequest->onResponse.connect(boost::bind(&BlockListController::handleBlockResponse, this, blockRequest, _1, _2, std::vector<JID>(1, changeStateEvent->getContact()), false)); - blockRequest->send(); - } else if (changeStateEvent->getBlockState() == RequestChangeBlockStateUIEvent::Unblocked) { - GenericRequest<UnblockPayload>::ref unblockRequest = blockListManager_->createUnblockJIDRequest(changeStateEvent->getContact()); - unblockRequest->onResponse.connect(boost::bind(&BlockListController::handleUnblockResponse, this, unblockRequest, _1, _2, std::vector<JID>(1, changeStateEvent->getContact()), false)); - unblockRequest->send(); - } - return; - } +void BlockListController::handleUIEvent(std::shared_ptr<UIEvent> rawEvent) { + // handle UI dialog + std::shared_ptr<RequestBlockListDialogUIEvent> requestDialogEvent = std::dynamic_pointer_cast<RequestBlockListDialogUIEvent>(rawEvent); + if (requestDialogEvent != nullptr) { + if (blockListEditorWidget_ == nullptr) { + blockListEditorWidget_ = blockListEditorWidgetFactory_->createBlockListEditorWidget(); + blockListEditorWidget_->onSetNewBlockList.connect(boost::bind(&BlockListController::handleSetNewBlockList, this, _1)); + } + blockListBeforeEdit = blockListManager_->getBlockList()->getItems(); + blockListEditorWidget_->setCurrentBlockList(blockListBeforeEdit); + blockListEditorWidget_->setError(""); + blockListEditorWidget_->show(); + return; + } + + // handle block state change + std::shared_ptr<RequestChangeBlockStateUIEvent> changeStateEvent = std::dynamic_pointer_cast<RequestChangeBlockStateUIEvent>(rawEvent); + if (changeStateEvent != nullptr) { + if (changeStateEvent->getBlockState() == RequestChangeBlockStateUIEvent::Blocked) { + GenericRequest<BlockPayload>::ref blockRequest = blockListManager_->createBlockJIDRequest(changeStateEvent->getContact()); + blockRequest->onResponse.connect(boost::bind(&BlockListController::handleBlockResponse, this, blockRequest, _1, _2, std::vector<JID>(1, changeStateEvent->getContact()), false)); + blockRequest->send(); + } else if (changeStateEvent->getBlockState() == RequestChangeBlockStateUIEvent::Unblocked) { + GenericRequest<UnblockPayload>::ref unblockRequest = blockListManager_->createUnblockJIDRequest(changeStateEvent->getContact()); + unblockRequest->onResponse.connect(boost::bind(&BlockListController::handleUnblockResponse, this, unblockRequest, _1, _2, std::vector<JID>(1, changeStateEvent->getContact()), false)); + unblockRequest->send(); + } + return; + } } -void BlockListController::handleBlockResponse(GenericRequest<BlockPayload>::ref request, boost::shared_ptr<BlockPayload>, ErrorPayload::ref error, const std::vector<JID>& jids, bool originEditor) { - if (error) { - std::string errorMessage; - // FIXME: Handle reporting of list of JIDs in a translatable way. - errorMessage = str(format(QT_TRANSLATE_NOOP("", "Failed to block %1%.")) % jids.at(0).toString()); - if (!error->getText().empty()) { - errorMessage = str(format(QT_TRANSLATE_NOOP("", "%1%: %2%.")) % errorMessage % error->getText()); - } - eventController_->handleIncomingEvent(boost::make_shared<ErrorEvent>(request->getReceiver(), errorMessage)); - } - if (originEditor) { - remainingRequests_--; - if (blockListEditorWidget_ && (remainingRequests_ == 0)) { - blockListEditorWidget_->setBusy(false); - } - } +void BlockListController::handleBlockResponse(GenericRequest<BlockPayload>::ref request, std::shared_ptr<BlockPayload>, ErrorPayload::ref error, const std::vector<JID>& jids, bool originEditor) { + if (error) { + std::string errorMessage; + // FIXME: Handle reporting of list of JIDs in a translatable way. + errorMessage = str(format(QT_TRANSLATE_NOOP("", "Failed to block %1%.")) % jids.at(0).toString()); + if (!error->getText().empty()) { + errorMessage = str(format(QT_TRANSLATE_NOOP("", "%1%: %2%.")) % errorMessage % error->getText()); + } + if (blockListEditorWidget_ && originEditor) { + blockListEditorWidget_->setError(errorMessage); + blockListEditorWidget_->setBusy(false); + } + else { + eventController_->handleIncomingEvent(std::make_shared<ErrorEvent>(request->getReceiver(), errorMessage)); + } + } + if (originEditor) { + remainingRequests_--; + if (blockListEditorWidget_ && (remainingRequests_ == 0) && !error) { + blockListEditorWidget_->setBusy(false); + blockListEditorWidget_->hide(); + } + } } -void BlockListController::handleUnblockResponse(GenericRequest<UnblockPayload>::ref request, boost::shared_ptr<UnblockPayload>, ErrorPayload::ref error, const std::vector<JID>& jids, bool originEditor) { - if (error) { - std::string errorMessage; - // FIXME: Handle reporting of list of JIDs in a translatable way. - errorMessage = str(format(QT_TRANSLATE_NOOP("", "Failed to unblock %1%.")) % jids.at(0).toString()); - if (!error->getText().empty()) { - errorMessage = str(format(QT_TRANSLATE_NOOP("", "%1%: %2%.")) % errorMessage % error->getText()); - } - eventController_->handleIncomingEvent(boost::make_shared<ErrorEvent>(request->getReceiver(), errorMessage)); - } - if (originEditor) { - remainingRequests_--; - if (blockListEditorWidget_ && (remainingRequests_ == 0)) { - blockListEditorWidget_->setBusy(false); - } - } +void BlockListController::handleUnblockResponse(GenericRequest<UnblockPayload>::ref request, std::shared_ptr<UnblockPayload>, ErrorPayload::ref error, const std::vector<JID>& jids, bool originEditor) { + if (error) { + std::string errorMessage; + // FIXME: Handle reporting of list of JIDs in a translatable way. + errorMessage = str(format(QT_TRANSLATE_NOOP("", "Failed to unblock %1%.")) % jids.at(0).toString()); + if (!error->getText().empty()) { + errorMessage = str(format(QT_TRANSLATE_NOOP("", "%1%: %2%.")) % errorMessage % error->getText()); + } + if (blockListEditorWidget_ && originEditor) { + blockListEditorWidget_->setError(errorMessage); + blockListEditorWidget_->setBusy(false); + } + else { + eventController_->handleIncomingEvent(std::make_shared<ErrorEvent>(request->getReceiver(), errorMessage)); + } + } + if (originEditor) { + remainingRequests_--; + if (blockListEditorWidget_ && (remainingRequests_ == 0) && !error) { + blockListEditorWidget_->setBusy(false); + blockListEditorWidget_->hide(); + } + } } void BlockListController::handleSetNewBlockList(const std::vector<JID> &newBlockList) { - std::vector<JID> jidsToBlock; - std::vector<JID> jidsToUnblock; - - blockListDifferences(newBlockList, jidsToUnblock, jidsToBlock); - - if (!jidsToBlock.empty()) { - remainingRequests_++; - GenericRequest<BlockPayload>::ref blockRequest = blockListManager_->createBlockJIDsRequest(jidsToBlock); - blockRequest->onResponse.connect(boost::bind(&BlockListController::handleBlockResponse, this, blockRequest, _1, _2, jidsToBlock, true)); - blockRequest->send(); - } - if (!jidsToUnblock.empty()) { - remainingRequests_++; - GenericRequest<UnblockPayload>::ref unblockRequest = blockListManager_->createUnblockJIDsRequest(jidsToUnblock); - unblockRequest->onResponse.connect(boost::bind(&BlockListController::handleUnblockResponse, this, unblockRequest, _1, _2, jidsToUnblock, true)); - unblockRequest->send(); - } - if (!jidsToBlock.empty() || jidsToUnblock.empty()) { - assert(blockListEditorWidget_); - blockListEditorWidget_->setBusy(true); - } + std::vector<JID> jidsToBlock; + std::vector<JID> jidsToUnblock; + + blockListDifferences(newBlockList, jidsToUnblock, jidsToBlock); + + if (!jidsToBlock.empty()) { + remainingRequests_++; + GenericRequest<BlockPayload>::ref blockRequest = blockListManager_->createBlockJIDsRequest(jidsToBlock); + blockRequest->onResponse.connect(boost::bind(&BlockListController::handleBlockResponse, this, blockRequest, _1, _2, jidsToBlock, true)); + blockRequest->send(); + } + if (!jidsToUnblock.empty()) { + remainingRequests_++; + GenericRequest<UnblockPayload>::ref unblockRequest = blockListManager_->createUnblockJIDsRequest(jidsToUnblock); + unblockRequest->onResponse.connect(boost::bind(&BlockListController::handleUnblockResponse, this, unblockRequest, _1, _2, jidsToUnblock, true)); + unblockRequest->send(); + } + if (!jidsToBlock.empty() || !jidsToUnblock.empty()) { + assert(blockListEditorWidget_); + blockListEditorWidget_->setBusy(true); + blockListEditorWidget_->setError(""); + } else { + blockListEditorWidget_->hide(); + } } void BlockListController::handleBlockListChanged() { - if (blockListEditorWidget_) { - std::vector<JID> jidsToBlock; - std::vector<JID> jidsToUnblock; + if (blockListEditorWidget_) { + std::vector<JID> jidsToBlock; + std::vector<JID> jidsToUnblock; - blockListDifferences(blockListEditorWidget_->getCurrentBlockList(), jidsToUnblock, jidsToBlock); - blockListBeforeEdit = blockListManager_->getBlockList()->getItems(); + blockListDifferences(blockListEditorWidget_->getCurrentBlockList(), jidsToUnblock, jidsToBlock); + blockListBeforeEdit = blockListManager_->getBlockList()->getItems(); - foreach (const JID& jid, jidsToBlock) { - blockListBeforeEdit.push_back(jid); - } + for (const auto& jid : jidsToBlock) { + if (std::find(blockListBeforeEdit.begin(), blockListBeforeEdit.end(), jid) == blockListBeforeEdit.end()) { + blockListBeforeEdit.push_back(jid); + } + } - foreach (const JID& jid, jidsToUnblock) { - blockListBeforeEdit.erase(std::remove(blockListBeforeEdit.begin(), blockListBeforeEdit.end(), jid), blockListBeforeEdit.end());; - } + for (const auto& jid : jidsToUnblock) { + blockListBeforeEdit.erase(std::remove(blockListBeforeEdit.begin(), blockListBeforeEdit.end(), jid), blockListBeforeEdit.end()); + } - blockListEditorWidget_->setCurrentBlockList(blockListBeforeEdit); - } + blockListEditorWidget_->setCurrentBlockList(blockListBeforeEdit); + } } } diff --git a/Swift/Controllers/BlockListController.h b/Swift/Controllers/BlockListController.h index 4c9caad..c1f6fee 100644 --- a/Swift/Controllers/BlockListController.h +++ b/Swift/Controllers/BlockListController.h @@ -4,11 +4,18 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ +/* + * Copyright (c) 2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + #pragma once -#include <boost/shared_ptr.hpp> +#include <memory> #include <Swiften/Queries/GenericRequest.h> + #include <Swift/Controllers/UIEvents/UIEventStream.h> #include <Swift/Controllers/UIInterfaces/BlockListEditorWidgetFactory.h> @@ -21,28 +28,29 @@ class EventController; class BlockListController { public: - BlockListController(ClientBlockListManager* blockListManager, UIEventStream* uiEventStream, BlockListEditorWidgetFactory* blockListEditorWidgetFactory, EventController* eventController); - ~BlockListController(); + BlockListController(ClientBlockListManager* blockListManager, UIEventStream* uiEventStream, BlockListEditorWidgetFactory* blockListEditorWidgetFactory, EventController* eventController); + ~BlockListController(); private: - void blockListDifferences(const std::vector<JID> &newBlockList, std::vector<JID>& jidsToUnblock, std::vector<JID>& jidsToBlock) const; + void blockListDifferences(const std::vector<JID> &newBlockList, std::vector<JID>& jidsToUnblock, std::vector<JID>& jidsToBlock) const; - void handleUIEvent(boost::shared_ptr<UIEvent> event); + void handleUIEvent(std::shared_ptr<UIEvent> event); - void handleBlockResponse(GenericRequest<BlockPayload>::ref, boost::shared_ptr<BlockPayload>, ErrorPayload::ref error, const std::vector<JID>& jids, bool originEditor); - void handleUnblockResponse(GenericRequest<UnblockPayload>::ref, boost::shared_ptr<UnblockPayload>, ErrorPayload::ref error, const std::vector<JID>& jids, bool originEditor); + void handleBlockResponse(GenericRequest<BlockPayload>::ref, std::shared_ptr<BlockPayload>, ErrorPayload::ref error, const std::vector<JID>& jids, bool originEditor); + void handleUnblockResponse(GenericRequest<UnblockPayload>::ref, std::shared_ptr<UnblockPayload>, ErrorPayload::ref error, const std::vector<JID>& jids, bool originEditor); - void handleSetNewBlockList(const std::vector<JID>& newBlockList); + void handleSetNewBlockList(const std::vector<JID>& newBlockList); - void handleBlockListChanged(); + void handleBlockListChanged(); private: - ClientBlockListManager* blockListManager_; - BlockListEditorWidgetFactory* blockListEditorWidgetFactory_; - BlockListEditorWidget* blockListEditorWidget_; - EventController* eventController_; - std::vector<JID> blockListBeforeEdit; - int remainingRequests_; + ClientBlockListManager* blockListManager_; + BlockListEditorWidgetFactory* blockListEditorWidgetFactory_; + BlockListEditorWidget* blockListEditorWidget_; + EventController* eventController_; + std::vector<JID> blockListBeforeEdit; + int remainingRequests_; + UIEventStream* uiEventStream_; }; } diff --git a/Swift/Controllers/CertificateMemoryStorageFactory.h b/Swift/Controllers/CertificateMemoryStorageFactory.h index adbce80..080b157 100644 --- a/Swift/Controllers/CertificateMemoryStorageFactory.h +++ b/Swift/Controllers/CertificateMemoryStorageFactory.h @@ -1,28 +1,28 @@ /* - * Copyright (c) 2011 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2011-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <Swift/Controllers/Storages/CertificateStorageFactory.h> #include <Swift/Controllers/Storages/CertificateMemoryStorage.h> +#include <Swift/Controllers/Storages/CertificateStorageFactory.h> namespace Swift { - class CertificateFactory; + class CertificateFactory; - class CertificateMemoryStorageFactory : public CertificateStorageFactory { - public: - CertificateMemoryStorageFactory() { - } + class CertificateMemoryStorageFactory : public CertificateStorageFactory { + public: + CertificateMemoryStorageFactory() { + } - virtual CertificateStorage* createCertificateStorage(const JID&) const { - return new CertificateMemoryStorage(); - } + virtual CertificateStorage* createCertificateStorage(const JID&) const { + return new CertificateMemoryStorage(); + } - private: - boost::filesystem::path basePath; - CertificateFactory* certificateFactory; - }; + private: + boost::filesystem::path basePath; + CertificateFactory* certificateFactory; + }; } diff --git a/Swift/Controllers/Chat/AutoAcceptMUCInviteDecider.h b/Swift/Controllers/Chat/AutoAcceptMUCInviteDecider.h index 0f85a8a..646612b 100644 --- a/Swift/Controllers/Chat/AutoAcceptMUCInviteDecider.h +++ b/Swift/Controllers/Chat/AutoAcceptMUCInviteDecider.h @@ -4,39 +4,51 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ +/* + * Copyright (c) 2014-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + #pragma once -#include <Swiften/Roster/XMPPRoster.h> #include <Swiften/Elements/MUCInvitationPayload.h> -#include <Swift/Controllers/Settings/SettingsProvider.h> +#include <Swiften/Roster/XMPPRoster.h> + #include <Swift/Controllers/SettingConstants.h> +#include <Swift/Controllers/Settings/SettingsProvider.h> namespace Swift { - class AutoAcceptMUCInviteDecider { - public: - AutoAcceptMUCInviteDecider(const JID& domain, XMPPRoster* roster, SettingsProvider* settings) : domain_(domain), roster_(roster), settings_(settings) { - } - - bool isAutoAcceptedInvite(const JID& from, MUCInvitationPayload::ref invite) { - if (!invite->getIsImpromptu() && !invite->getIsContinuation()) { - return false; - } - - std::string auto_accept_mode = settings_->getSetting(SettingConstants::INVITE_AUTO_ACCEPT_MODE); - if (auto_accept_mode == "no") { - return false; - } else if (auto_accept_mode == "presence") { - return roster_->getSubscriptionStateForJID(from) == RosterItemPayload::From || roster_->getSubscriptionStateForJID(from) == RosterItemPayload::Both; - } else if (auto_accept_mode == "domain") { - return roster_->getSubscriptionStateForJID(from) == RosterItemPayload::From || roster_->getSubscriptionStateForJID(from) == RosterItemPayload::Both || from.getDomain() == domain_; - } else { - assert(false); - } - } - - private: - JID domain_; - XMPPRoster* roster_; - SettingsProvider* settings_; - }; + class AutoAcceptMUCInviteDecider { + public: + AutoAcceptMUCInviteDecider(const JID& domain, XMPPRoster* roster, SettingsProvider* settings) : domain_(domain), roster_(roster), settings_(settings) { + } + + bool isAutoAcceptedInvite(const JID& from, MUCInvitationPayload::ref invite) { + if (!invite->getIsImpromptu()) { + return false; /* always ask the user for normal MUC invites */ + } + + if (invite->getIsContinuation()) { + return true; + } + + std::string auto_accept_mode = settings_->getSetting(SettingConstants::INVITE_AUTO_ACCEPT_MODE); + if (auto_accept_mode == "no") { + return false; + } else if (auto_accept_mode == "presence") { + return roster_->getSubscriptionStateForJID(from) == RosterItemPayload::From || roster_->getSubscriptionStateForJID(from) == RosterItemPayload::Both; + } else if (auto_accept_mode == "domain") { + return roster_->getSubscriptionStateForJID(from) == RosterItemPayload::From || roster_->getSubscriptionStateForJID(from) == RosterItemPayload::Both || from.getDomain() == domain_; + } else { + assert(false); + return false; + } + } + + private: + JID domain_; + XMPPRoster* roster_; + SettingsProvider* settings_; + }; } diff --git a/Swift/Controllers/Chat/ChatController.cpp b/Swift/Controllers/Chat/ChatController.cpp index 13fd22b..debd83f 100644 --- a/Swift/Controllers/Chat/ChatController.cpp +++ b/Swift/Controllers/Chat/ChatController.cpp @@ -1,499 +1,607 @@ /* - * Copyright (c) 2010-2013 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2019 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #include <Swift/Controllers/Chat/ChatController.h> +#include <memory> + #include <boost/bind.hpp> -#include <boost/smart_ptr/make_shared.hpp> -#include <stdio.h> -#include <Swiften/Base/format.h> -#include <Swiften/Base/Algorithm.h> #include <Swiften/Avatars/AvatarManager.h> +#include <Swiften/Base/Algorithm.h> +#include <Swiften/Base/Log.h> +#include <Swiften/Base/Path.h> +#include <Swiften/Base/format.h> #include <Swiften/Chat/ChatStateNotifier.h> #include <Swiften/Chat/ChatStateTracker.h> -#include <Swiften/Client/StanzaChannel.h> +#include <Swiften/Client/ClientBlockListManager.h> #include <Swiften/Client/NickResolver.h> +#include <Swiften/Client/StanzaChannel.h> #include <Swiften/Disco/EntityCapsProvider.h> -#include <Swiften/Base/foreach.h> -#include <Swiften/Base/DateTime.h> +#include <Swiften/Disco/FeatureOracle.h> #include <Swiften/Elements/DeliveryReceipt.h> #include <Swiften/Elements/DeliveryReceiptRequest.h> #include <Swiften/Elements/Idle.h> -#include <Swiften/Base/Log.h> -#include <Swiften/Client/ClientBlockListManager.h> +#include <Swiften/FileTransfer/FileTransferManager.h> +#include <Swiften/Network/TimerFactory.h> -#include <Swift/Controllers/Intl.h> -#include <Swift/Controllers/UIInterfaces/ChatWindowFactory.h> -#include <Swift/Controllers/XMPPEvents/EventController.h> +#include <Swift/Controllers/Chat/ChatMessageParser.h> #include <Swift/Controllers/FileTransfer/FileTransferController.h> +#include <Swift/Controllers/Highlighting/Highlighter.h> +#include <Swift/Controllers/Intl.h> +#include <Swift/Controllers/SettingConstants.h> #include <Swift/Controllers/StatusUtil.h> -#include <Swift/Controllers/UIEvents/UIEventStream.h> -#include <Swift/Controllers/UIEvents/SendFileUIEvent.h> +#include <Swift/Controllers/Translator.h> #include <Swift/Controllers/UIEvents/AcceptWhiteboardSessionUIEvent.h> #include <Swift/Controllers/UIEvents/CancelWhiteboardSessionUIEvent.h> -#include <Swift/Controllers/UIEvents/ShowWhiteboardUIEvent.h> -#include <Swift/Controllers/UIEvents/RequestChangeBlockStateUIEvent.h> #include <Swift/Controllers/UIEvents/InviteToMUCUIEvent.h> +#include <Swift/Controllers/UIEvents/RequestChangeBlockStateUIEvent.h> #include <Swift/Controllers/UIEvents/RequestInviteToMUCUIEvent.h> -#include <Swift/Controllers/SettingConstants.h> -#include <Swift/Controllers/Highlighter.h> -#include <Swift/Controllers/Chat/ChatMessageParser.h> +#include <Swift/Controllers/UIEvents/SendFileUIEvent.h> +#include <Swift/Controllers/UIEvents/ShowWhiteboardUIEvent.h> +#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 { - + /** * The controller does not gain ownership of the stanzaChannel, nor the factory. */ -ChatController::ChatController(const JID& self, StanzaChannel* stanzaChannel, IQRouter* iqRouter, ChatWindowFactory* chatWindowFactory, const JID &contact, NickResolver* nickResolver, PresenceOracle* presenceOracle, AvatarManager* avatarManager, bool isInMUC, bool useDelayForLatency, UIEventStream* eventStream, EventController* eventController, TimerFactory* timerFactory, EntityCapsProvider* entityCapsProvider, bool userWantsReceipts, SettingsProvider* settings, HistoryController* historyController, MUCRegistry* mucRegistry, HighlightManager* highlightManager, ClientBlockListManager* clientBlockListManager, ChatMessageParser* chatMessageParser, AutoAcceptMUCInviteDecider* autoAcceptMUCInviteDecider) - : ChatControllerBase(self, stanzaChannel, iqRouter, chatWindowFactory, contact, presenceOracle, avatarManager, useDelayForLatency, eventStream, eventController, timerFactory, entityCapsProvider, historyController, mucRegistry, highlightManager, chatMessageParser, autoAcceptMUCInviteDecider), eventStream_(eventStream), userWantsReceipts_(userWantsReceipts), settings_(settings), clientBlockListManager_(clientBlockListManager) { - isInMUC_ = isInMUC; - lastWasPresence_ = false; - chatStateNotifier_ = new ChatStateNotifier(stanzaChannel, contact, entityCapsProvider); - chatStateTracker_ = new ChatStateTracker(); - nickResolver_ = nickResolver; - presenceOracle_->onPresenceChange.connect(boost::bind(&ChatController::handlePresenceChange, this, _1)); - chatStateTracker_->onChatStateChange.connect(boost::bind(&ChatWindow::setContactChatState, chatWindow_, _1)); - stanzaChannel_->onStanzaAcked.connect(boost::bind(&ChatController::handleStanzaAcked, this, _1)); - nickResolver_->onNickChanged.connect(boost::bind(&ChatController::handleContactNickChanged, this, _1, _2)); - std::string nick = nickResolver_->jidToNick(toJID_); - chatWindow_->setName(nick); - std::string startMessage; - Presence::ref theirPresence; - if (isInMUC) { - startMessage = str(format(QT_TRANSLATE_NOOP("", "Starting chat with %1% in chatroom %2%")) % nick % contact.toBare().toString()); - theirPresence = presenceOracle->getLastPresence(contact); - } else { - startMessage = str(format(QT_TRANSLATE_NOOP("", "Starting chat with %1% - %2%")) % nick % contact.toBare().toString()); - theirPresence = contact.isBare() ? presenceOracle->getHighestPriorityPresence(contact.toBare()) : presenceOracle->getLastPresence(contact); - } - Idle::ref idle; - if (theirPresence && (idle = theirPresence->getPayload<Idle>())) { - startMessage += str(format(QT_TRANSLATE_NOOP("", ", who has been idle since %1%")) % dateTimeToLocalString(idle->getSince())); - } - startMessage += ": " + statusShowTypeToFriendlyName(theirPresence ? theirPresence->getShow() : StatusShow::None); - if (theirPresence && !theirPresence->getStatus().empty()) { - startMessage += " (" + theirPresence->getStatus() + ")"; - } - lastShownStatus_ = theirPresence ? theirPresence->getShow() : StatusShow::None; - chatStateNotifier_->setContactIsOnline(theirPresence && theirPresence->getType() == Presence::Available); - startMessage += "."; - chatWindow_->addSystemMessage(chatMessageParser_->parseMessageBody(startMessage), ChatWindow::DefaultDirection); - chatWindow_->onUserTyping.connect(boost::bind(&ChatStateNotifier::setUserIsTyping, chatStateNotifier_)); - chatWindow_->onUserCancelsTyping.connect(boost::bind(&ChatStateNotifier::userCancelledNewMessage, chatStateNotifier_)); - chatWindow_->onFileTransferStart.connect(boost::bind(&ChatController::handleFileTransferStart, this, _1, _2)); - chatWindow_->onFileTransferAccept.connect(boost::bind(&ChatController::handleFileTransferAccept, this, _1, _2)); - chatWindow_->onFileTransferCancel.connect(boost::bind(&ChatController::handleFileTransferCancel, this, _1)); - chatWindow_->onSendFileRequest.connect(boost::bind(&ChatController::handleSendFileRequest, this, _1)); - chatWindow_->onWhiteboardSessionAccept.connect(boost::bind(&ChatController::handleWhiteboardSessionAccept, this)); - chatWindow_->onWhiteboardSessionCancel.connect(boost::bind(&ChatController::handleWhiteboardSessionCancel, this)); - chatWindow_->onWhiteboardWindowShow.connect(boost::bind(&ChatController::handleWhiteboardWindowShow, this)); - chatWindow_->onBlockUserRequest.connect(boost::bind(&ChatController::handleBlockUserRequest, this)); - chatWindow_->onUnblockUserRequest.connect(boost::bind(&ChatController::handleUnblockUserRequest, this)); - chatWindow_->onInviteToChat.connect(boost::bind(&ChatController::handleInviteToChat, this, _1)); - handleBareJIDCapsChanged(toJID_); - - settings_->onSettingChanged.connect(boost::bind(&ChatController::handleSettingChanged, this, _1)); - eventStream_->onUIEvent.connect(boost::bind(&ChatController::handleUIEvent, this, _1)); +ChatController::ChatController(const JID& self, StanzaChannel* stanzaChannel, IQRouter* iqRouter, ChatWindowFactory* chatWindowFactory, const JID &contact, NickResolver* nickResolver, PresenceOracle* presenceOracle, AvatarManager* avatarManager, bool isInMUC, bool useDelayForLatency, UIEventStream* eventStream, TimerFactory* timerFactory, EventController* eventController, EntityCapsProvider* entityCapsProvider, bool userWantsReceipts, HistoryController* historyController, MUCRegistry* mucRegistry, HighlightManager* highlightManager, ClientBlockListManager* clientBlockListManager, std::shared_ptr<ChatMessageParser> chatMessageParser, AutoAcceptMUCInviteDecider* autoAcceptMUCInviteDecider, SettingsProvider* settings, Chattables& chattables) + : ChatControllerBase(self, stanzaChannel, iqRouter, chatWindowFactory, contact, nickResolver, presenceOracle, avatarManager, useDelayForLatency, eventStream, eventController, entityCapsProvider, historyController, mucRegistry, highlightManager, chatMessageParser, autoAcceptMUCInviteDecider, settings, chattables), userWantsReceipts_(userWantsReceipts), clientBlockListManager_(clientBlockListManager) { + isInMUC_ = isInMUC; + lastWasPresence_ = false; + chatStateNotifier_ = new ChatStateNotifier(stanzaChannel, contact, entityCapsProvider, timerFactory, 20000); + chatStateTracker_ = new ChatStateTracker(); + nickResolver_ = nickResolver; + presenceOracle_->onPresenceChange.connect(boost::bind(&ChatController::handlePresenceChange, this, _1)); + chatStateTracker_->onChatStateChange.connect(boost::bind(&ChatWindow::setContactChatState, chatWindow_, _1)); + stanzaChannel_->onStanzaAcked.connect(boost::bind(&ChatController::handleStanzaAcked, this, _1)); + nickResolver_->onNickChanged.connect(boost::bind(&ChatController::handleContactNickChanged, this, _1, _2)); + std::string nick = nickResolver_->jidToNick(toJID_); + chatWindow_->setName(nick); + std::string startMessage; + Presence::ref theirPresence; + if (isInMUC) { + startMessage = str(format(QT_TRANSLATE_NOOP("", "Starting chat with %1% in chatroom %2%")) % nick % contact.toBare().toString()); + theirPresence = presenceOracle->getLastPresence(contact); + } else { + startMessage = str(format(QT_TRANSLATE_NOOP("", "Starting chat with %1% - %2%")) % nick % contact.toBare().toString()); + theirPresence = contact.isBare() ? presenceOracle->getAccountPresence(contact) : presenceOracle->getLastPresence(contact); + } + Idle::ref idle; + if (theirPresence && (idle = theirPresence->getPayload<Idle>())) { + startMessage += str(format(QT_TRANSLATE_NOOP("", ", who has been idle since %1%")) % Swift::Translator::getInstance()->ptimeToHumanReadableString(idle->getSince())); + } + startMessage += ": " + statusShowTypeToFriendlyName(theirPresence ? theirPresence->getShow() : StatusShow::None); + if (theirPresence && !theirPresence->getStatus().empty()) { + startMessage += " (" + theirPresence->getStatus() + ")"; + } + lastShownStatus_ = theirPresence ? theirPresence->getShow() : StatusShow::None; + chatStateNotifier_->setContactIsOnline(theirPresence && theirPresence->getType() == Presence::Available); + startMessage += "."; + chatWindow_->addSystemMessage(chatMessageParser_->parseMessageBody(startMessage), ChatWindow::DefaultDirection); + continuationsBrokenConnection_ = chatWindow_->onContinuationsBroken.connect([this, startMessage]() { chatWindow_->addSystemMessage(chatMessageParser_->parseMessageBody(startMessage), ChatWindow::DefaultDirection); }); + chatWindow_->onUserTyping.connect(boost::bind(&ChatStateNotifier::setUserIsTyping, chatStateNotifier_)); + chatWindow_->onUserCancelsTyping.connect(boost::bind(&ChatStateNotifier::userCancelledNewMessage, chatStateNotifier_)); + chatWindow_->onFileTransferStart.connect(boost::bind(&ChatController::handleFileTransferStart, this, _1, _2)); + chatWindow_->onFileTransferAccept.connect(boost::bind(&ChatController::handleFileTransferAccept, this, _1, _2)); + chatWindow_->onFileTransferCancel.connect(boost::bind(&ChatController::handleFileTransferCancel, this, _1)); + chatWindow_->onSendFileRequest.connect(boost::bind(&ChatController::handleSendFileRequest, this, _1)); + chatWindow_->onWhiteboardSessionAccept.connect(boost::bind(&ChatController::handleWhiteboardSessionAccept, this)); + chatWindow_->onWhiteboardSessionCancel.connect(boost::bind(&ChatController::handleWhiteboardSessionCancel, this)); + chatWindow_->onWhiteboardWindowShow.connect(boost::bind(&ChatController::handleWhiteboardWindowShow, this)); + chatWindow_->onBlockUserRequest.connect(boost::bind(&ChatController::handleBlockUserRequest, this)); + chatWindow_->onUnblockUserRequest.connect(boost::bind(&ChatController::handleUnblockUserRequest, this)); + chatWindow_->onInviteToChat.connect(boost::bind(&ChatController::handleInviteToChat, this, _1)); + chatWindow_->onClosed.connect(boost::bind(&ChatController::handleWindowClosed, this)); + ChatController::handleBareJIDCapsChanged(toJID_); + + settings_->onSettingChanged.connect(boost::bind(&ChatController::handleSettingChanged, this, _1)); + eventStream_->onUIEvent.connect(boost::bind(&ChatController::handleUIEvent, this, _1)); } void ChatController::handleContactNickChanged(const JID& jid, const std::string& /*oldNick*/) { - if (jid.toBare() == toJID_.toBare()) { - chatWindow_->setName(nickResolver_->jidToNick(jid)); - } + if (jid.toBare() == toJID_.toBare()) { + chatWindow_->setName(nickResolver_->jidToNick(toJID_)); + } } ChatController::~ChatController() { - eventStream_->onUIEvent.disconnect(boost::bind(&ChatController::handleUIEvent, this, _1)); - settings_->onSettingChanged.disconnect(boost::bind(&ChatController::handleSettingChanged, this, _1)); - nickResolver_->onNickChanged.disconnect(boost::bind(&ChatController::handleContactNickChanged, this, _1, _2)); - delete chatStateNotifier_; - delete chatStateTracker_; + eventStream_->onUIEvent.disconnect(boost::bind(&ChatController::handleUIEvent, this, _1)); + settings_->onSettingChanged.disconnect(boost::bind(&ChatController::handleSettingChanged, this, _1)); + nickResolver_->onNickChanged.disconnect(boost::bind(&ChatController::handleContactNickChanged, this, _1, _2)); + delete chatStateNotifier_; + delete chatStateTracker_; } JID ChatController::getBaseJID() { - return isInMUC_ ? toJID_ : ChatControllerBase::getBaseJID(); + return isInMUC_ ? toJID_ : ChatControllerBase::getBaseJID(); } void ChatController::cancelReplaces() { - lastWasPresence_ = false; + lastWasPresence_ = false; } void ChatController::handleBareJIDCapsChanged(const JID& /*jid*/) { - DiscoInfo::ref disco = entityCapsProvider_->getCaps(toJID_); - if (disco) { - if (disco->hasFeature(DiscoInfo::MessageCorrectionFeature)) { - chatWindow_->setCorrectionEnabled(ChatWindow::Yes); - } else { - chatWindow_->setCorrectionEnabled(ChatWindow::No); - } - if (disco->hasFeature(DiscoInfo::MessageDeliveryReceiptsFeature)) { - contactSupportsReceipts_ = ChatWindow::Yes; - } else { - contactSupportsReceipts_ = ChatWindow::No; - } - } else { - SWIFT_LOG(debug) << "No disco info :(" << std::endl; - chatWindow_->setCorrectionEnabled(ChatWindow::Maybe); - contactSupportsReceipts_ = ChatWindow::Maybe; - } - checkForDisplayingDisplayReceiptsAlert(); + FeatureOracle featureOracle(entityCapsProvider_, presenceOracle_); + + chatWindow_->setCorrectionEnabled(featureOracle.isMessageCorrectionSupported(toJID_)); + chatWindow_->setFileTransferEnabled(isInMUC_ ? No : featureOracle.isFileTransferSupported(toJID_)); + contactSupportsReceipts_ = featureOracle.isMessageReceiptsSupported(toJID_); + + checkForDisplayingDisplayReceiptsAlert(); } void ChatController::setToJID(const JID& jid) { - chatStateNotifier_->setContact(jid); - ChatControllerBase::setToJID(jid); - Presence::ref presence; - if (isInMUC_) { - presence = presenceOracle_->getLastPresence(jid); - } else { - presence = jid.isBare() ? presenceOracle_->getHighestPriorityPresence(jid.toBare()) : presenceOracle_->getLastPresence(jid); - } - chatStateNotifier_->setContactIsOnline(presence && presence->getType() == Presence::Available); - handleBareJIDCapsChanged(toJID_); -} - -void ChatController::setAvailableServerFeatures(boost::shared_ptr<DiscoInfo> info) { - ChatControllerBase::setAvailableServerFeatures(info); - if (iqRouter_->isAvailable() && info->hasFeature(DiscoInfo::BlockingCommandFeature)) { - boost::shared_ptr<BlockList> blockList = clientBlockListManager_->getBlockList(); - - blockingOnStateChangedConnection_ = blockList->onStateChanged.connect(boost::bind(&ChatController::handleBlockingStateChanged, this)); - blockingOnItemAddedConnection_ = blockList->onItemAdded.connect(boost::bind(&ChatController::handleBlockingItemAdded, this, _1)); - blockingOnItemRemovedConnection_ = blockList->onItemRemoved.connect(boost::bind(&ChatController::handleBlockingItemRemoved, this, _1)); - - handleBlockingStateChanged(); - } -} - -bool ChatController::isIncomingMessageFromMe(boost::shared_ptr<Message>) { - return false; -} - -void ChatController::preHandleIncomingMessage(boost::shared_ptr<MessageEvent> messageEvent) { - if (messageEvent->isReadable()) { - chatWindow_->flash(); - lastWasPresence_ = false; - } - boost::shared_ptr<Message> message = messageEvent->getStanza(); - JID from = message->getFrom(); - if (!from.equals(toJID_, JID::WithResource)) { - if (toJID_.equals(from, JID::WithoutResource) && toJID_.isBare()){ - setToJID(from); - } - } - chatStateTracker_->handleMessageReceived(message); - chatStateNotifier_->receivedMessageFromContact(message->getPayload<ChatState>()); - - if (boost::shared_ptr<DeliveryReceipt> receipt = message->getPayload<DeliveryReceipt>()) { - SWIFT_LOG(debug) << "received receipt for id: " << receipt->getReceivedID() << std::endl; - if (requestedReceipts_.find(receipt->getReceivedID()) != requestedReceipts_.end()) { - chatWindow_->setMessageReceiptState(requestedReceipts_[receipt->getReceivedID()], ChatWindow::ReceiptReceived); - requestedReceipts_.erase(receipt->getReceivedID()); - } - } else if (message->getPayload<DeliveryReceiptRequest>()) { - if (receivingPresenceFromUs_) { - boost::shared_ptr<Message> receiptMessage = boost::make_shared<Message>(); - receiptMessage->setTo(toJID_); - receiptMessage->addPayload(boost::make_shared<DeliveryReceipt>(message->getID())); - stanzaChannel_->sendMessage(receiptMessage); - } - } -} - -void ChatController::postHandleIncomingMessage(boost::shared_ptr<MessageEvent> messageEvent, const HighlightAction& highlight) { - eventController_->handleIncomingEvent(messageEvent); - if (!messageEvent->getConcluded()) { - highlighter_->handleHighlightAction(highlight); - } -} - - -void ChatController::preSendMessageRequest(boost::shared_ptr<Message> message) { - chatStateNotifier_->addChatStateRequest(message); - if (userWantsReceipts_ && (contactSupportsReceipts_ != ChatWindow::No) && message) { - message->addPayload(boost::make_shared<DeliveryReceiptRequest>()); - } + chatStateNotifier_->setContact(jid); + ChatControllerBase::setToJID(jid); + Presence::ref presence; + if (isInMUC_) { + presence = presenceOracle_->getLastPresence(jid); + } else { + presence = jid.isBare() ? presenceOracle_->getAccountPresence(jid.toBare()) : presenceOracle_->getLastPresence(jid); + } + chatStateNotifier_->setContactIsOnline(presence && presence->getType() == Presence::Available); + handleBareJIDCapsChanged(toJID_); +} + +void ChatController::setAvailableServerFeatures(std::shared_ptr<DiscoInfo> info) { + ChatControllerBase::setAvailableServerFeatures(info); + if (iqRouter_->isAvailable() && info->hasFeature(DiscoInfo::BlockingCommandFeature)) { + std::shared_ptr<BlockList> blockList = clientBlockListManager_->getBlockList(); + + blockingOnStateChangedConnection_ = blockList->onStateChanged.connect(boost::bind(&ChatController::handleBlockingStateChanged, this)); + blockingOnItemAddedConnection_ = blockList->onItemAdded.connect(boost::bind(&ChatController::handleBlockingStateChanged, this)); + blockingOnItemRemovedConnection_ = blockList->onItemRemoved.connect(boost::bind(&ChatController::handleBlockingStateChanged, this)); + + handleBlockingStateChanged(); + } +} + +bool ChatController::isIncomingMessageFromMe(std::shared_ptr<Message>) { + return false; +} + +void ChatController::preHandleIncomingMessage(std::shared_ptr<MessageEvent> messageEvent) { + if (messageEvent->isReadable()) { + chatWindow_->flash(); + lastWasPresence_ = false; + } + std::shared_ptr<Message> message = messageEvent->getStanza(); + JID from = message->getFrom(); + if (!from.equals(toJID_, JID::WithResource)) { + if (toJID_.equals(from, JID::WithoutResource) && toJID_.isBare()){ + // Bind controller to a full JID if message contains body text or is a typing chat state. + ChatState::ref chatState = message->getPayload<ChatState>(); + if (!message->getBody().get_value_or("").empty() || (chatState && chatState->getChatState() == ChatState::Composing)) { + setToJID(from); + } + } + } + chatStateTracker_->handleMessageReceived(message); + chatStateNotifier_->receivedMessageFromContact(!!message->getPayload<ChatState>()); + + // handle XEP-0184 Message Receipts + // incomming receipts + if (std::shared_ptr<DeliveryReceipt> receipt = message->getPayload<DeliveryReceipt>()) { + SWIFT_LOG(debug) << "received receipt for id: " << receipt->getReceivedID(); + if (requestedReceipts_.find(receipt->getReceivedID()) != requestedReceipts_.end()) { + chatWindow_->setMessageReceiptState(requestedReceipts_[receipt->getReceivedID()], ChatWindow::ReceiptReceived); + requestedReceipts_.erase(receipt->getReceivedID()); + } + // incomming errors in response to send out receipts + } else if (message->getPayload<DeliveryReceiptRequest>() && (message->getType() == Message::Error)) { + if (requestedReceipts_.find(message->getID()) != requestedReceipts_.end()) { + chatWindow_->setMessageReceiptState(requestedReceipts_[message->getID()], ChatWindow::ReceiptFailed); + requestedReceipts_.erase(message->getID()); + } + // incoming receipt requests + } else if (message->getPayload<DeliveryReceiptRequest>()) { + if (receivingPresenceFromUs_) { + std::shared_ptr<Message> receiptMessage = std::make_shared<Message>(); + receiptMessage->setTo(toJID_); + receiptMessage->addPayload(std::make_shared<DeliveryReceipt>(message->getID())); + stanzaChannel_->sendMessage(receiptMessage); + } + } +} + +void ChatController::postHandleIncomingMessage(std::shared_ptr<MessageEvent> messageEvent, const ChatWindow::ChatMessage& chatMessage) { + highlighter_->handleSystemNotifications(chatMessage, messageEvent); + eventController_->handleIncomingEvent(messageEvent); + if (!messageEvent->getConcluded()) { + highlighter_->handleSoundNotifications(chatMessage); + } +} + + +void ChatController::preSendMessageRequest(std::shared_ptr<Message> message) { + chatStateNotifier_->addChatStateRequest(message); + if (userWantsReceipts_ && (contactSupportsReceipts_ != No) && message) { + message->addPayload(std::make_shared<DeliveryReceiptRequest>()); + } } void ChatController::setContactIsReceivingPresence(bool isReceivingPresence) { - receivingPresenceFromUs_ = isReceivingPresence; + receivingPresenceFromUs_ = isReceivingPresence; } void ChatController::handleSettingChanged(const std::string& settingPath) { - if (settingPath == SettingConstants::REQUEST_DELIVERYRECEIPTS.getKey()) { - userWantsReceipts_ = settings_->getSetting(SettingConstants::REQUEST_DELIVERYRECEIPTS); - checkForDisplayingDisplayReceiptsAlert(); - } + if (settingPath == SettingConstants::REQUEST_DELIVERYRECEIPTS.getKey()) { + userWantsReceipts_ = settings_->getSetting(SettingConstants::REQUEST_DELIVERYRECEIPTS); + checkForDisplayingDisplayReceiptsAlert(); + } } void ChatController::checkForDisplayingDisplayReceiptsAlert() { - if (userWantsReceipts_ && (contactSupportsReceipts_ == ChatWindow::No)) { - chatWindow_->setAlert(QT_TRANSLATE_NOOP("", "This chat doesn't support delivery receipts.")); - } else if (userWantsReceipts_ && (contactSupportsReceipts_ == ChatWindow::Maybe)) { - chatWindow_->setAlert(QT_TRANSLATE_NOOP("", "This chat may not support delivery receipts. You might not receive delivery receipts for the messages you sent.")); - } else { - chatWindow_->cancelAlert(); - } + boost::optional<ChatWindow::AlertID> newDeliverReceiptAlert; + if (userWantsReceipts_ && (contactSupportsReceipts_ == No)) { + newDeliverReceiptAlert = chatWindow_->addAlert(QT_TRANSLATE_NOOP("", "This chat doesn't support delivery receipts.")); + } else if (userWantsReceipts_ && (contactSupportsReceipts_ == Maybe)) { + newDeliverReceiptAlert = chatWindow_->addAlert(QT_TRANSLATE_NOOP("", "This chat may not support delivery receipts. You might not receive delivery receipts for the messages you send.")); + } else { + if (deliveryReceiptAlert_) { + chatWindow_->removeAlert(*deliveryReceiptAlert_); + deliveryReceiptAlert_.reset(); + } + } + if (newDeliverReceiptAlert) { + if (deliveryReceiptAlert_) { + chatWindow_->removeAlert(*deliveryReceiptAlert_); + } + deliveryReceiptAlert_ = newDeliverReceiptAlert; + } } void ChatController::handleBlockingStateChanged() { - boost::shared_ptr<BlockList> blockList = clientBlockListManager_->getBlockList(); - if (blockList->getState() == BlockList::Available) { - if (isInMUC_ ? blockList->isBlocked(toJID_) : blockList->isBlocked(toJID_.toBare())) { - chatWindow_->setAlert(QT_TRANSLATE_NOOP("", "You've currently blocked this contact. To continue your conversation you have to unblock the contact first.")); - chatWindow_->setInputEnabled(false); - chatWindow_->setBlockingState(ChatWindow::IsBlocked); - } else { - chatWindow_->setBlockingState(ChatWindow::IsUnblocked); - } - } -} - -void ChatController::handleBlockingItemAdded(const JID& jid) { - if (toJID_ == (isInMUC_ ? jid: jid.toBare())) { - chatWindow_->setAlert(QT_TRANSLATE_NOOP("", "You've currently blocked this contact. To continue your conversation you have to unblock the contact first.")); - chatWindow_->setInputEnabled(false); - chatWindow_->setBlockingState(ChatWindow::IsBlocked); - } -} - -void ChatController::handleBlockingItemRemoved(const JID& jid) { - if (toJID_ == (isInMUC_ ? jid: jid.toBare())) { - // FIXME: Support for different types of alerts. - chatWindow_->cancelAlert(); - chatWindow_->setInputEnabled(true); - chatWindow_->setBlockingState(ChatWindow::IsUnblocked); - } + std::shared_ptr<BlockList> blockList = clientBlockListManager_->getBlockList(); + if (blockList->getState() == BlockList::Available) { + if (isInMUC_ ? blockList->isBlocked(toJID_) : blockList->isBlocked(toJID_.toBare())) { + if (!blockedContactAlert_) { + blockedContactAlert_ = chatWindow_->addAlert(QT_TRANSLATE_NOOP("", "You've currently blocked this contact. To continue your conversation you have to unblock the contact first.")); + } + chatWindow_->setBlockingState(ChatWindow::IsBlocked); + + // disconnect typing events to prevent chat state notifciations to blocked contacts + chatWindow_->onUserTyping.disconnect(boost::bind(&ChatStateNotifier::setUserIsTyping, chatStateNotifier_)); + chatWindow_->onUserCancelsTyping.disconnect(boost::bind(&ChatStateNotifier::userCancelledNewMessage, chatStateNotifier_)); + } else { + if (blockedContactAlert_) { + chatWindow_->removeAlert(*blockedContactAlert_); + blockedContactAlert_.reset(); + } + chatWindow_->setBlockingState(ChatWindow::IsUnblocked); + + chatWindow_->onUserTyping.connect(boost::bind(&ChatStateNotifier::setUserIsTyping, chatStateNotifier_)); + chatWindow_->onUserCancelsTyping.connect(boost::bind(&ChatStateNotifier::userCancelledNewMessage, chatStateNotifier_)); + } + } } void ChatController::handleBlockUserRequest() { - if (isInMUC_) { - eventStream_->send(boost::make_shared<RequestChangeBlockStateUIEvent>(RequestChangeBlockStateUIEvent::Blocked, toJID_)); - } else { - eventStream_->send(boost::make_shared<RequestChangeBlockStateUIEvent>(RequestChangeBlockStateUIEvent::Blocked, toJID_.toBare())); - } + if (isInMUC_) { + eventStream_->send(std::make_shared<RequestChangeBlockStateUIEvent>(RequestChangeBlockStateUIEvent::Blocked, toJID_)); + } else { + eventStream_->send(std::make_shared<RequestChangeBlockStateUIEvent>(RequestChangeBlockStateUIEvent::Blocked, toJID_.toBare())); + } } void ChatController::handleUnblockUserRequest() { - if (isInMUC_) { - eventStream_->send(boost::make_shared<RequestChangeBlockStateUIEvent>(RequestChangeBlockStateUIEvent::Unblocked, toJID_)); - } else { - eventStream_->send(boost::make_shared<RequestChangeBlockStateUIEvent>(RequestChangeBlockStateUIEvent::Unblocked, toJID_.toBare())); - } + if (isInMUC_) { + eventStream_->send(std::make_shared<RequestChangeBlockStateUIEvent>(RequestChangeBlockStateUIEvent::Unblocked, toJID_)); + } else { + eventStream_->send(std::make_shared<RequestChangeBlockStateUIEvent>(RequestChangeBlockStateUIEvent::Unblocked, toJID_.toBare())); + } } void ChatController::handleInviteToChat(const std::vector<JID>& droppedJIDs) { - boost::shared_ptr<UIEvent> event(new RequestInviteToMUCUIEvent(toJID_.toBare(), droppedJIDs)); - eventStream_->send(event); + std::shared_ptr<UIEvent> event(new RequestInviteToMUCUIEvent(getToJID(), droppedJIDs, RequestInviteToMUCUIEvent::Impromptu)); + eventStream_->send(event); } -void ChatController::handleUIEvent(boost::shared_ptr<UIEvent> event) { - boost::shared_ptr<InviteToMUCUIEvent> inviteEvent = boost::dynamic_pointer_cast<InviteToMUCUIEvent>(event); - if (inviteEvent && inviteEvent->getRoom() == toJID_.toBare()) { - onConvertToMUC(detachChatWindow(), inviteEvent->getInvites(), inviteEvent->getReason()); - } +void ChatController::handleWindowClosed() { + onWindowClosed(); } +void ChatController::handleUIEvent(std::shared_ptr<UIEvent> event) { + std::shared_ptr<InviteToMUCUIEvent> inviteEvent = std::dynamic_pointer_cast<InviteToMUCUIEvent>(event); + if (inviteEvent && inviteEvent->getOriginator() == getToJID()) { + onConvertToMUC(detachChatWindow(), inviteEvent->getInvites(), inviteEvent->getReason()); + } +} -void ChatController::postSendMessage(const std::string& body, boost::shared_ptr<Stanza> sentStanza) { - boost::shared_ptr<Replace> replace = sentStanza->getPayload<Replace>(); - if (replace) { - eraseIf(unackedStanzas_, PairSecondEquals<boost::shared_ptr<Stanza>, std::string>(myLastMessageUIID_)); - replaceMessage(body, myLastMessageUIID_, boost::posix_time::microsec_clock::universal_time(), HighlightAction()); - } else { - myLastMessageUIID_ = addMessage(body, QT_TRANSLATE_NOOP("", "me"), true, labelsEnabled_ ? chatWindow_->getSelectedSecurityLabel().getLabel() : boost::shared_ptr<SecurityLabel>(), avatarManager_->getAvatarPath(selfJID_), boost::posix_time::microsec_clock::universal_time(), HighlightAction()); - } +void ChatController::handleIncomingOwnMessage(std::shared_ptr<Message> message) { + if (!message->getBody().get_value_or("").empty()) { + postSendMessage(message->getBody().get_value_or(""), message); + handleStanzaAcked(message); + onActivity(message->getBody().get_value_or("")); + } +} - if (stanzaChannel_->getStreamManagementEnabled() && !myLastMessageUIID_.empty() ) { - chatWindow_->setAckState(myLastMessageUIID_, ChatWindow::Pending); - unackedStanzas_[sentStanza] = myLastMessageUIID_; - } +void ChatController::postSendMessage(const std::string& body, std::shared_ptr<Stanza> sentStanza) { + std::shared_ptr<Replace> replace = sentStanza->getPayload<Replace>(); + if (replace) { + eraseIf(unackedStanzas_, PairSecondEquals<std::shared_ptr<Stanza>, std::string>(myLastMessageUIID_)); + replaceMessage(chatMessageParser_->parseMessageBody(body, "", true), myLastMessageUIID_, boost::posix_time::microsec_clock::universal_time()); + } else { + myLastMessageUIID_ = addMessage(chatMessageParser_->parseMessageBody(body, "", true), QT_TRANSLATE_NOOP("", "me"), true, labelsEnabled_ ? chatWindow_->getSelectedSecurityLabel().getLabel() : std::shared_ptr<SecurityLabel>(), avatarManager_->getAvatarPath(selfJID_), boost::posix_time::microsec_clock::universal_time()); + } - if (sentStanza->getPayload<DeliveryReceiptRequest>()) { - requestedReceipts_[sentStanza->getID()] = myLastMessageUIID_; - chatWindow_->setMessageReceiptState(myLastMessageUIID_, ChatWindow::ReceiptRequested); - } + if (stanzaChannel_->getStreamManagementEnabled() && !myLastMessageUIID_.empty() ) { + chatWindow_->setAckState(myLastMessageUIID_, ChatWindow::Pending); + unackedStanzas_[sentStanza] = myLastMessageUIID_; + } - lastWasPresence_ = false; - chatStateNotifier_->userSentMessage(); + if (sentStanza->getPayload<DeliveryReceiptRequest>()) { + requestedReceipts_[sentStanza->getID()] = myLastMessageUIID_; + chatWindow_->setMessageReceiptState(myLastMessageUIID_, ChatWindow::ReceiptRequested); + } + + lastWasPresence_ = false; + chatStateNotifier_->userSentMessage(); } -void ChatController::handleStanzaAcked(boost::shared_ptr<Stanza> stanza) { - std::map<boost::shared_ptr<Stanza>, std::string>::iterator unackedStanza = unackedStanzas_.find(stanza); - if (unackedStanza != unackedStanzas_.end()) { - chatWindow_->setAckState(unackedStanza->second, ChatWindow::Received); - unackedStanzas_.erase(unackedStanza); - } +void ChatController::handleStanzaAcked(std::shared_ptr<Stanza> stanza) { + std::map<std::shared_ptr<Stanza>, std::string>::iterator unackedStanza = unackedStanzas_.find(stanza); + if (unackedStanza != unackedStanzas_.end()) { + chatWindow_->setAckState(unackedStanza->second, ChatWindow::Received); + unackedStanzas_.erase(unackedStanza); + } } void ChatController::setOnline(bool online) { - if (!online) { - std::map<boost::shared_ptr<Stanza>, std::string>::iterator it = unackedStanzas_.begin(); - for ( ; it != unackedStanzas_.end(); ++it) { - chatWindow_->setAckState(it->second, ChatWindow::Failed); - } - unackedStanzas_.clear(); + if (!online) { + for (auto& stanzaIdPair : unackedStanzas_) { + chatWindow_->setAckState(stanzaIdPair.second, ChatWindow::Failed); + failedStanzas_[stanzaIdPair.second] = stanzaIdPair.first; + } + unackedStanzas_.clear(); - Presence::ref fakeOffline(new Presence()); - fakeOffline->setFrom(toJID_); - fakeOffline->setType(Presence::Unavailable); - chatStateTracker_->handlePresenceChange(fakeOffline); - } - ChatControllerBase::setOnline(online); + Presence::ref fakeOffline(new Presence()); + fakeOffline->setFrom(toJID_); + fakeOffline->setType(Presence::Unavailable); + chatStateTracker_->handlePresenceChange(fakeOffline); + } + ChatControllerBase::setOnline(online); } void ChatController::handleNewFileTransferController(FileTransferController* ftc) { - std::string nick = senderDisplayNameFromMessage(ftc->getOtherParty()); - std::string ftID = ftc->setChatWindow(chatWindow_, nick); - - ftControllers[ftID] = ftc; + std::string nick = senderDisplayNameFromMessage(ftc->getOtherParty()); + std::string avatarPath; + if (ftc->isIncoming()) { + avatarPath = pathToString(avatarManager_->getAvatarPath(ftc->getOtherParty())); + } + else { + avatarPath = pathToString(avatarManager_->getAvatarPath(selfJID_)); + } + + std::string ftID = ftc->setChatWindow(chatWindow_, nick, avatarPath); + 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) { - lastWbID_ = chatWindow_->addWhiteboardRequest(senderIsSelf); + lastWbID_ = chatWindow_->addWhiteboardRequest(senderIsSelf); } void ChatController::handleWhiteboardStateChange(const ChatWindow::WhiteboardSessionState state) { - chatWindow_->setWhiteboardSessionStatus(lastWbID_, state); + chatWindow_->setWhiteboardSessionStatus(lastWbID_, state); } void ChatController::handleFileTransferCancel(std::string id) { - SWIFT_LOG(debug) << "handleFileTransferCancel(" << id << ")" << std::endl; - if (ftControllers.find(id) != ftControllers.end()) { - ftControllers[id]->cancel(); - } else { - std::cerr << "unknown file transfer UI id" << std::endl; - } + SWIFT_LOG(debug) << "handleFileTransferCancel(" << id << ")"; + if (ftControllers.find(id) != ftControllers.end()) { + ftControllers[id]->cancel(); + } else { + std::cerr << "unknown file transfer UI id" << std::endl; + } } void ChatController::handleFileTransferStart(std::string id, std::string description) { - SWIFT_LOG(debug) << "handleFileTransferStart(" << id << ", " << description << ")" << std::endl; - if (ftControllers.find(id) != ftControllers.end()) { - ftControllers[id]->start(description); - } else { - std::cerr << "unknown file transfer UI id" << std::endl; - } + SWIFT_LOG(debug) << "handleFileTransferStart(" << id << ", " << description << ")"; + if (ftControllers.find(id) != ftControllers.end()) { + ftControllers[id]->start(description); + } else { + std::cerr << "unknown file transfer UI id" << std::endl; + } } void ChatController::handleFileTransferAccept(std::string id, std::string filename) { - SWIFT_LOG(debug) << "handleFileTransferAccept(" << id << ", " << filename << ")" << std::endl; - if (ftControllers.find(id) != ftControllers.end()) { - ftControllers[id]->accept(filename); - } else { - std::cerr << "unknown file transfer UI id" << std::endl; - } + SWIFT_LOG(debug) << "handleFileTransferAccept(" << id << ", " << filename << ")"; + if (ftControllers.find(id) != ftControllers.end()) { + ftControllers[id]->accept(filename); + } else { + std::cerr << "unknown file transfer UI id" << std::endl; + } } void ChatController::handleSendFileRequest(std::string filename) { - SWIFT_LOG(debug) << "ChatController::handleSendFileRequest(" << filename << ")" << std::endl; - eventStream_->send(boost::make_shared<SendFileUIEvent>(getToJID(), filename)); + SWIFT_LOG(debug) << "ChatController::handleSendFileRequest(" << filename << ")"; + eventStream_->send(std::make_shared<SendFileUIEvent>(getToJID(), filename)); } void ChatController::handleWhiteboardSessionAccept() { - eventStream_->send(boost::make_shared<AcceptWhiteboardSessionUIEvent>(toJID_)); + eventStream_->send(std::make_shared<AcceptWhiteboardSessionUIEvent>(toJID_)); } void ChatController::handleWhiteboardSessionCancel() { - eventStream_->send(boost::make_shared<CancelWhiteboardSessionUIEvent>(toJID_)); + eventStream_->send(std::make_shared<CancelWhiteboardSessionUIEvent>(toJID_)); } void ChatController::handleWhiteboardWindowShow() { - eventStream_->send(boost::make_shared<ShowWhiteboardUIEvent>(toJID_)); + eventStream_->send(std::make_shared<ShowWhiteboardUIEvent>(toJID_)); +} + +std::string ChatController::senderHighlightNameFromMessage(const JID& from) { + if (isInMUC_) { + return nickResolver_->jidToNick(from); + } + else { + return from.toBare().toString(); + } } std::string ChatController::senderDisplayNameFromMessage(const JID& from) { - return nickResolver_->jidToNick(from); -} - -std::string ChatController::getStatusChangeString(boost::shared_ptr<Presence> presence) { - std::string nick = senderDisplayNameFromMessage(presence->getFrom()); - std::string response; - if (!presence || presence->getType() == Presence::Unavailable || presence->getType() == Presence::Error) { - response = QT_TRANSLATE_NOOP("", "%1% has gone offline"); - } else if (presence->getType() == Presence::Available) { - StatusShow::Type show = presence->getShow(); - if (show == StatusShow::Online || show == StatusShow::FFC) { - response = QT_TRANSLATE_NOOP("", "%1% has become available"); - } else if (show == StatusShow::Away || show == StatusShow::XA) { - response = QT_TRANSLATE_NOOP("", "%1% has gone away"); - } else if (show == StatusShow::DND) { - response = QT_TRANSLATE_NOOP("", "%1% is now busy"); - } - } - Idle::ref idle; - if ((idle = presence->getPayload<Idle>())) { - response += str(format(QT_TRANSLATE_NOOP("", " and has been idle since %1%")) % dateTimeToLocalString(idle->getSince())); - } - - if (!response.empty()) { - response = str(format(response) % nick); - } - - if (!presence->getStatus().empty()) { - response += " (" + presence->getStatus() + ")"; - } - return response + "."; -} - -void ChatController::handlePresenceChange(boost::shared_ptr<Presence> newPresence) { - bool me = false; - if (toJID_.isBare()) { - newPresence = presenceOracle_->getHighestPriorityPresence(toJID_); - if ((newPresence ? newPresence->getShow() : StatusShow::None) != lastShownStatus_) { - me = true; - } - } else if (toJID_.equals(newPresence->getFrom(), JID::WithResource)) { - me = true; - } - if (!me) { - return; - } - if (!newPresence) { - newPresence = boost::make_shared<Presence>(); - newPresence->setType(Presence::Unavailable); - } - lastShownStatus_ = newPresence->getShow(); - - chatStateTracker_->handlePresenceChange(newPresence); - chatStateNotifier_->setContactIsOnline(newPresence->getType() == Presence::Available); - std::string newStatusChangeString = getStatusChangeString(newPresence); - if (newStatusChangeString != lastStatusChangeString_) { - if (lastWasPresence_) { - chatWindow_->replaceLastMessage(chatMessageParser_->parseMessageBody(newStatusChangeString)); - } else { - chatWindow_->addPresenceMessage(chatMessageParser_->parseMessageBody(newStatusChangeString), ChatWindow::DefaultDirection); - } - lastStatusChangeString_ = newStatusChangeString; - lastWasPresence_ = true; - } -} - -boost::optional<boost::posix_time::ptime> ChatController::getMessageTimestamp(boost::shared_ptr<Message> message) const { - return message->getTimestamp(); + return nickResolver_->jidToNick(from); +} + +std::string ChatController::getStatusChangeString(std::shared_ptr<Presence> presence) { + std::string nick = senderDisplayNameFromMessage(presence->getFrom()); + std::string response; + if (!presence || presence->getType() == Presence::Unavailable || presence->getType() == Presence::Error) { + response = QT_TRANSLATE_NOOP("", "%1% has gone offline"); + } else if (presence->getType() == Presence::Available) { + StatusShow::Type show = presence->getShow(); + if (show == StatusShow::Online || show == StatusShow::FFC) { + response = QT_TRANSLATE_NOOP("", "%1% has become available"); + } else if (show == StatusShow::Away || show == StatusShow::XA) { + response = QT_TRANSLATE_NOOP("", "%1% has gone away"); + } else if (show == StatusShow::DND) { + response = QT_TRANSLATE_NOOP("", "%1% is now busy"); + } + } + Idle::ref idle; + if ((idle = presence->getPayload<Idle>())) { + response += str(format(QT_TRANSLATE_NOOP("", " and has been idle since %1%")) % Swift::Translator::getInstance()->ptimeToHumanReadableString(idle->getSince())); + } + + if (!response.empty()) { + response = str(format(response) % nick); + } + + if (!presence->getStatus().empty()) { + response += " (" + presence->getStatus() + ")"; + } + return response + "."; +} + +void ChatController::handlePresenceChange(std::shared_ptr<Presence> newPresence) { + bool relevantPresence = false; + + if (isInMUC_) { + // For MUC participants we only have a single presence to choose one and + // even for multi-session nicknames multiple resources are not distinguishable + // to other participants. + if (toJID_.equals(newPresence->getFrom(), JID::WithResource)) { + relevantPresence = true; + } + } + else { + // For standard chats we retrieve the account presence from the PresenceOracle, + // as there can be multiple presences to choose from. + if (toJID_.equals(newPresence->getFrom(), JID::WithoutResource)) { + // Presence matches ChatController JID. + newPresence = presenceOracle_->getAccountPresence(toJID_); + relevantPresence = true; + } + } + + if (!relevantPresence) { + return; + } + + if (!newPresence) { + newPresence = std::make_shared<Presence>(); + newPresence->setType(Presence::Unavailable); + } + lastShownStatus_ = newPresence->getShow(); + + chatStateTracker_->handlePresenceChange(newPresence); + chatStateNotifier_->setContactIsOnline(newPresence->getType() == Presence::Available); + std::string newStatusChangeString = getStatusChangeString(newPresence); + if (newStatusChangeString != lastStatusChangeString_) { + if (lastWasPresence_) { + chatWindow_->replaceLastMessage(chatMessageParser_->parseMessageBody(newStatusChangeString), ChatWindow::UpdateTimestamp); + } else { + chatWindow_->addPresenceMessage(chatMessageParser_->parseMessageBody(newStatusChangeString), ChatWindow::DefaultDirection); + } + lastStatusChangeString_ = newStatusChangeString; + lastWasPresence_ = true; + } +} + +boost::optional<boost::posix_time::ptime> ChatController::getMessageTimestamp(std::shared_ptr<Message> message) const { + return message->getTimestamp(); +} + +void ChatController::addMessageHandleIncomingMessage(const JID& from, const ChatWindow::ChatMessage& message, const std::string& messageID, bool senderIsSelf, std::shared_ptr<SecurityLabel> label, const boost::posix_time::ptime& timeStamp) { + lastMessagesIDs_[from.toBare()] = {messageID, addMessage(message, senderDisplayNameFromMessage(from), senderIsSelf, label, avatarManager_->getAvatarPath(from), timeStamp)}; +} + +void ChatController::handleIncomingReplaceMessage(const JID& from, const ChatWindow::ChatMessage& message, const std::string& messageID, const std::string& idToReplace, bool senderIsSelf, std::shared_ptr<SecurityLabel> label, const boost::posix_time::ptime& timeStamp) { + auto lastMessage = lastMessagesIDs_.find(from.toBare()); + if ((lastMessage != lastMessagesIDs_.end()) && (lastMessage->second.idInStream == idToReplace)) { + replaceMessage(message, lastMessage->second.idInWindow, timeStamp); + } + else { + addMessageHandleIncomingMessage(from, message, messageID, senderIsSelf, label, timeStamp); + } } void ChatController::logMessage(const std::string& message, const JID& fromJID, const JID& toJID, const boost::posix_time::ptime& timeStamp, bool /* isIncoming */) { - HistoryMessage::Type type; - if (mucRegistry_->isMUC(fromJID.toBare()) || mucRegistry_->isMUC(toJID.toBare())) { - type = HistoryMessage::PrivateMessage; - } - else { - type = HistoryMessage::Chat; - } + HistoryMessage::Type type; + if (mucRegistry_->isMUC(fromJID.toBare()) || mucRegistry_->isMUC(toJID.toBare())) { + type = HistoryMessage::PrivateMessage; + } + else { + type = HistoryMessage::Chat; + } + + if (historyController_) { + historyController_->addMessage(message, fromJID, toJID, type, timeStamp); + } +} + +bool ChatController::shouldIgnoreMessage(std::shared_ptr<Message> message) { + if (!message->getID().empty()) { + if (message->getID() == lastHandledMessageID_) { + return true; + } + else { + lastHandledMessageID_ = message->getID(); + } + } + return false; +} - if (historyController_) { - historyController_->addMessage(message, fromJID, toJID, type, timeStamp); - } +JID ChatController::messageCorrectionJID(const JID& fromJID) { + return fromJID.toBare(); } ChatWindow* ChatController::detachChatWindow() { - chatWindow_->onUserTyping.disconnect(boost::bind(&ChatStateNotifier::setUserIsTyping, chatStateNotifier_)); - chatWindow_->onUserCancelsTyping.disconnect(boost::bind(&ChatStateNotifier::userCancelledNewMessage, chatStateNotifier_)); - return ChatControllerBase::detachChatWindow(); + continuationsBrokenConnection_.disconnect(); + chatWindow_->onClosed.disconnect(boost::bind(&ChatController::handleWindowClosed, this)); + chatWindow_->onInviteToChat.disconnect(boost::bind(&ChatController::handleInviteToChat, this, _1)); + chatWindow_->onUnblockUserRequest.disconnect(boost::bind(&ChatController::handleUnblockUserRequest, this)); + chatWindow_->onBlockUserRequest.disconnect(boost::bind(&ChatController::handleBlockUserRequest, this)); + chatWindow_->onWhiteboardWindowShow.disconnect(boost::bind(&ChatController::handleWhiteboardWindowShow, this)); + chatWindow_->onWhiteboardSessionCancel.disconnect(boost::bind(&ChatController::handleWhiteboardSessionCancel, this)); + chatWindow_->onWhiteboardSessionAccept.disconnect(boost::bind(&ChatController::handleWhiteboardSessionAccept, this)); + chatWindow_->onSendFileRequest.disconnect(boost::bind(&ChatController::handleSendFileRequest, this, _1)); + chatWindow_->onFileTransferCancel.disconnect(boost::bind(&ChatController::handleFileTransferCancel, this, _1)); + chatWindow_->onFileTransferAccept.disconnect(boost::bind(&ChatController::handleFileTransferAccept, this, _1, _2)); + chatWindow_->onFileTransferStart.disconnect(boost::bind(&ChatController::handleFileTransferStart, this, _1, _2)); + chatWindow_->onUserCancelsTyping.disconnect(boost::bind(&ChatStateNotifier::userCancelledNewMessage, chatStateNotifier_)); + chatWindow_->onUserTyping.disconnect(boost::bind(&ChatStateNotifier::setUserIsTyping, chatStateNotifier_)); + return ChatControllerBase::detachChatWindow(); } } diff --git a/Swift/Controllers/Chat/ChatController.h b/Swift/Controllers/Chat/ChatController.h index f8b6d8b..447e263 100644 --- a/Swift/Controllers/Chat/ChatController.h +++ b/Swift/Controllers/Chat/ChatController.h @@ -1,113 +1,117 @@ /* - * Copyright (c) 2010-2013 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <Swift/Controllers/Chat/ChatControllerBase.h> - #include <map> #include <string> +#include <Swiften/Base/Tristate.h> + +#include <Swift/Controllers/Chat/ChatControllerBase.h> #include <Swift/Controllers/UIInterfaces/ChatWindow.h> namespace Swift { - class AvatarManager; - class ChatStateNotifier; - class ChatStateTracker; - class NickResolver; - class EntityCapsProvider; - class FileTransferController; - class SettingsProvider; - class HistoryController; - class HighlightManager; - class ClientBlockListManager; - class UIEvent; - - class ChatController : public ChatControllerBase { - public: - ChatController(const JID& self, StanzaChannel* stanzaChannel, IQRouter* iqRouter, ChatWindowFactory* chatWindowFactory, const JID &contact, NickResolver* nickResolver, PresenceOracle* presenceOracle, AvatarManager* avatarManager, bool isInMUC, bool useDelayForLatency, UIEventStream* eventStream, EventController* eventController, TimerFactory* timerFactory, EntityCapsProvider* entityCapsProvider, bool userWantsReceipts, SettingsProvider* settings, HistoryController* historyController, MUCRegistry* mucRegistry, HighlightManager* highlightManager, ClientBlockListManager* clientBlockListManager, ChatMessageParser* chatMessageParser, AutoAcceptMUCInviteDecider* autoAcceptMUCInviteDecider); - virtual ~ChatController(); - virtual void setToJID(const JID& jid); - virtual void setAvailableServerFeatures(boost::shared_ptr<DiscoInfo> info); - virtual void setOnline(bool online); - virtual void handleNewFileTransferController(FileTransferController* ftc); - virtual void handleWhiteboardSessionRequest(bool senderIsSelf); - virtual void handleWhiteboardStateChange(const ChatWindow::WhiteboardSessionState state); - virtual void setContactIsReceivingPresence(bool /*isReceivingPresence*/); - virtual ChatWindow* detachChatWindow(); - - protected: - void cancelReplaces(); - JID getBaseJID(); - void logMessage(const std::string& message, const JID& fromJID, const JID& toJID, const boost::posix_time::ptime& timeStamp, bool isIncoming); - - private: - void handlePresenceChange(boost::shared_ptr<Presence> newPresence); - std::string getStatusChangeString(boost::shared_ptr<Presence> presence); - bool isIncomingMessageFromMe(boost::shared_ptr<Message> message); - void postSendMessage(const std::string &body, boost::shared_ptr<Stanza> sentStanza); - void preHandleIncomingMessage(boost::shared_ptr<MessageEvent> messageEvent); - void postHandleIncomingMessage(boost::shared_ptr<MessageEvent> messageEvent, const HighlightAction&); - void preSendMessageRequest(boost::shared_ptr<Message>); - std::string senderDisplayNameFromMessage(const JID& from); - virtual boost::optional<boost::posix_time::ptime> getMessageTimestamp(boost::shared_ptr<Message>) const; - void handleStanzaAcked(boost::shared_ptr<Stanza> stanza); - void dayTicked() {lastWasPresence_ = false;} - void handleContactNickChanged(const JID& jid, const std::string& /*oldNick*/); - void handleBareJIDCapsChanged(const JID& jid); - - void handleFileTransferCancel(std::string /* id */); - void handleFileTransferStart(std::string /* id */, std::string /* description */); - void handleFileTransferAccept(std::string /* id */, std::string /* filename */); - void handleSendFileRequest(std::string filename); - - void handleWhiteboardSessionAccept(); - void handleWhiteboardSessionCancel(); - void handleWhiteboardWindowShow(); - - void handleSettingChanged(const std::string& settingPath); - void checkForDisplayingDisplayReceiptsAlert(); - - void handleBlockingStateChanged(); - void handleBlockingItemAdded(const JID&); - void handleBlockingItemRemoved(const JID&); - - void handleBlockUserRequest(); - void handleUnblockUserRequest(); - - void handleInviteToChat(const std::vector<JID>& droppedJIDs); - void handleInviteToMUCWindowDismissed(); - void handleInviteToMUCWindowCompleted(); - - void handleUIEvent(boost::shared_ptr<UIEvent> event); - - private: - NickResolver* nickResolver_; - ChatStateNotifier* chatStateNotifier_; - ChatStateTracker* chatStateTracker_; - std::string myLastMessageUIID_; - bool isInMUC_; - bool lastWasPresence_; - std::string lastStatusChangeString_; - std::map<boost::shared_ptr<Stanza>, std::string> unackedStanzas_; - std::map<std::string, std::string> requestedReceipts_; - StatusShow::Type lastShownStatus_; - UIEventStream* eventStream_; - - ChatWindow::Tristate contactSupportsReceipts_; - bool receivingPresenceFromUs_; - bool userWantsReceipts_; - std::map<std::string, FileTransferController*> ftControllers; - SettingsProvider* settings_; - std::string lastWbID_; - - ClientBlockListManager* clientBlockListManager_; - boost::bsignals::scoped_connection blockingOnStateChangedConnection_; - boost::bsignals::scoped_connection blockingOnItemAddedConnection_; - boost::bsignals::scoped_connection blockingOnItemRemovedConnection_; - }; + class AvatarManager; + class ChatStateNotifier; + class ChatStateTracker; + class ClientBlockListManager; + class EntityCapsProvider; + class FileTransferController; + class HighlightManager; + class HistoryController; + class NickResolver; + class SettingsProvider; + class TimerFactory; + class UIEvent; + + class ChatController : public ChatControllerBase { + public: + ChatController(const JID& self, StanzaChannel* stanzaChannel, IQRouter* iqRouter, ChatWindowFactory* chatWindowFactory, const JID &contact, NickResolver* nickResolver, PresenceOracle* presenceOracle, AvatarManager* avatarManager, bool isInMUC, bool useDelayForLatency, UIEventStream* eventStream, TimerFactory* timerFactory, EventController* eventController, EntityCapsProvider* entityCapsProvider, bool userWantsReceipts, HistoryController* historyController, MUCRegistry* mucRegistry, HighlightManager* highlightManager, ClientBlockListManager* clientBlockListManager, std::shared_ptr<ChatMessageParser> chatMessageParser, AutoAcceptMUCInviteDecider* autoAcceptMUCInviteDecider, SettingsProvider* settings, Chattables& chattables); + virtual ~ChatController() override; + virtual void setToJID(const JID& jid) override; + virtual void setAvailableServerFeatures(std::shared_ptr<DiscoInfo> info) override; + virtual void setOnline(bool online) override; + virtual void handleNewFileTransferController(FileTransferController* ftc); + virtual void handleWhiteboardSessionRequest(bool senderIsSelf); + virtual void handleWhiteboardStateChange(const ChatWindow::WhiteboardSessionState state); + virtual void setContactIsReceivingPresence(bool /*isReceivingPresence*/) override; + virtual ChatWindow* detachChatWindow() override; + virtual void handleIncomingOwnMessage(std::shared_ptr<Message> message) override; + + protected: + virtual void cancelReplaces() override; + virtual JID getBaseJID() override; + virtual void logMessage(const std::string& message, const JID& fromJID, const JID& toJID, const boost::posix_time::ptime& timeStamp, bool isIncoming) override; + virtual bool shouldIgnoreMessage(std::shared_ptr<Message> message) override; + virtual JID messageCorrectionJID(const JID& fromJID) override; + + private: + void handlePresenceChange(std::shared_ptr<Presence> newPresence); + std::string getStatusChangeString(std::shared_ptr<Presence> presence); + virtual bool isIncomingMessageFromMe(std::shared_ptr<Message> message) override; + virtual void postSendMessage(const std::string &body, std::shared_ptr<Stanza> sentStanza) override; + virtual void preHandleIncomingMessage(std::shared_ptr<MessageEvent> messageEvent) override; + virtual void addMessageHandleIncomingMessage(const JID& from, const ChatWindow::ChatMessage& message, const std::string& messageID, bool senderIsSelf, std::shared_ptr<SecurityLabel> label, const boost::posix_time::ptime& timeStamp) override; + virtual void handleIncomingReplaceMessage(const JID& from, const ChatWindow::ChatMessage& message, const std::string& messageID, const std::string& idToReplace, bool senderIsSelf, std::shared_ptr<SecurityLabel> label, const boost::posix_time::ptime& timeStamp) override; + virtual void postHandleIncomingMessage(std::shared_ptr<MessageEvent> messageEvent, const ChatWindow::ChatMessage& chatMessage) override; + virtual void preSendMessageRequest(std::shared_ptr<Message>) override; + virtual std::string senderHighlightNameFromMessage(const JID& from) override; + virtual std::string senderDisplayNameFromMessage(const JID& from) override; + virtual boost::optional<boost::posix_time::ptime> getMessageTimestamp(std::shared_ptr<Message>) const override; + void handleStanzaAcked(std::shared_ptr<Stanza> stanza); + virtual void dayTicked() override { lastWasPresence_ = false; } + void handleContactNickChanged(const JID& jid, const std::string& /*oldNick*/); + virtual void handleBareJIDCapsChanged(const JID& jid) override; + + void handleFileTransferCancel(std::string /* id */); + void handleFileTransferStart(std::string /* id */, std::string /* description */); + void handleFileTransferAccept(std::string /* id */, std::string /* filename */); + void handleSendFileRequest(std::string filename); + + void handleWhiteboardSessionAccept(); + void handleWhiteboardSessionCancel(); + void handleWhiteboardWindowShow(); + + void handleSettingChanged(const std::string& settingPath); + void checkForDisplayingDisplayReceiptsAlert(); + + void handleBlockingStateChanged(); + void handleBlockUserRequest(); + void handleUnblockUserRequest(); + + void handleInviteToChat(const std::vector<JID>& droppedJIDs); + + void handleWindowClosed(); + + void handleUIEvent(std::shared_ptr<UIEvent> event); + + private: + NickResolver* nickResolver_; + ChatStateNotifier* chatStateNotifier_; + ChatStateTracker* chatStateTracker_; + std::string myLastMessageUIID_; + bool isInMUC_; + std::string lastStatusChangeString_; + StatusShow::Type lastShownStatus_; + + Tristate contactSupportsReceipts_; + bool receivingPresenceFromUs_ = false; + bool userWantsReceipts_; + std::map<std::string, FileTransferController*> ftControllers; + std::string lastWbID_; + std::string lastHandledMessageID_; + + ClientBlockListManager* clientBlockListManager_; + boost::signals2::scoped_connection blockingOnStateChangedConnection_; + boost::signals2::scoped_connection blockingOnItemAddedConnection_; + boost::signals2::scoped_connection blockingOnItemRemovedConnection_; + boost::signals2::scoped_connection continuationsBrokenConnection_; + + boost::optional<ChatWindow::AlertID> deliveryReceiptAlert_; + boost::optional<ChatWindow::AlertID> blockedContactAlert_; + }; } - diff --git a/Swift/Controllers/Chat/ChatControllerBase.cpp b/Swift/Controllers/Chat/ChatControllerBase.cpp index 23137dc..3805084 100644 --- a/Swift/Controllers/Chat/ChatControllerBase.cpp +++ b/Swift/Controllers/Chat/ChatControllerBase.cpp @@ -1,369 +1,421 @@ /* - * Copyright (c) 2010-2013 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #include <Swift/Controllers/Chat/ChatControllerBase.h> -#include <sstream> #include <map> +#include <memory> +#include <sstream> +#include <boost/algorithm/string.hpp> #include <boost/bind.hpp> -#include <boost/shared_ptr.hpp> -#include <boost/smart_ptr/make_shared.hpp> -#include <boost/date_time/posix_time/posix_time.hpp> #include <boost/numeric/conversion/cast.hpp> -#include <boost/algorithm/string.hpp> -#include <Swiften/Base/format.h> +#include <Swiften/Avatars/AvatarManager.h> #include <Swiften/Base/Path.h> -#include <Swiften/Base/String.h> +#include <Swiften/Base/format.h> #include <Swiften/Client/StanzaChannel.h> +#include <Swiften/Disco/EntityCapsProvider.h> #include <Swiften/Elements/Delay.h> +#include <Swiften/Elements/DeliveryReceipt.h> +#include <Swiften/Elements/DeliveryReceiptRequest.h> #include <Swiften/Elements/MUCInvitationPayload.h> #include <Swiften/Elements/MUCUserPayload.h> -#include <Swiften/Base/foreach.h> -#include <Swiften/Disco/EntityCapsProvider.h> #include <Swiften/Queries/Requests/GetSecurityLabelsCatalogRequest.h> -#include <Swiften/Avatars/AvatarManager.h> +#include <Swift/Controllers/Chat/AutoAcceptMUCInviteDecider.h> +#include <Swift/Controllers/Chat/Chattables.h> +#include <Swift/Controllers/Chat/ChatMessageParser.h> +#include <Swift/Controllers/Highlighting/HighlightManager.h> +#include <Swift/Controllers/Highlighting/Highlighter.h> #include <Swift/Controllers/Intl.h> -#include <Swift/Controllers/XMPPEvents/EventController.h> #include <Swift/Controllers/UIEvents/JoinMUCUIEvent.h> #include <Swift/Controllers/UIEvents/UIEventStream.h> #include <Swift/Controllers/UIInterfaces/ChatWindow.h> #include <Swift/Controllers/UIInterfaces/ChatWindowFactory.h> +#include <Swift/Controllers/XMPPEvents/EventController.h> #include <Swift/Controllers/XMPPEvents/MUCInviteEvent.h> -#include <Swift/Controllers/HighlightManager.h> -#include <Swift/Controllers/Highlighter.h> -#include <Swift/Controllers/Chat/AutoAcceptMUCInviteDecider.h> -#include <Swift/Controllers/Chat/ChatMessageParser.h> namespace Swift { -ChatControllerBase::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, EntityCapsProvider* entityCapsProvider, HistoryController* historyController, MUCRegistry* mucRegistry, HighlightManager* highlightManager, ChatMessageParser* chatMessageParser, AutoAcceptMUCInviteDecider* autoAcceptMUCInviteDecider) : selfJID_(self), stanzaChannel_(stanzaChannel), iqRouter_(iqRouter), chatWindowFactory_(chatWindowFactory), toJID_(toJID), labelsEnabled_(false), presenceOracle_(presenceOracle), avatarManager_(avatarManager), useDelayForLatency_(useDelayForLatency), eventController_(eventController), timerFactory_(timerFactory), entityCapsProvider_(entityCapsProvider), historyController_(historyController), mucRegistry_(mucRegistry), chatMessageParser_(chatMessageParser), autoAcceptMUCInviteDecider_(autoAcceptMUCInviteDecider), eventStream_(eventStream) { - chatWindow_ = chatWindowFactory_->createChatWindow(toJID, eventStream); - chatWindow_->onAllMessagesRead.connect(boost::bind(&ChatControllerBase::handleAllMessagesRead, this)); - chatWindow_->onSendMessageRequest.connect(boost::bind(&ChatControllerBase::handleSendMessageRequest, this, _1, _2)); - chatWindow_->onLogCleared.connect(boost::bind(&ChatControllerBase::handleLogCleared, this)); - entityCapsProvider_->onCapsChanged.connect(boost::bind(&ChatControllerBase::handleCapsChanged, this, _1)); - highlighter_ = highlightManager->createHighlighter(); - setOnline(stanzaChannel->isAvailable() && iqRouter->isAvailable()); - createDayChangeTimer(); +ChatControllerBase::ChatControllerBase(const JID& self, StanzaChannel* stanzaChannel, IQRouter* iqRouter, ChatWindowFactory* chatWindowFactory, const JID &toJID, NickResolver* nickResolver, PresenceOracle* presenceOracle, AvatarManager* avatarManager, bool useDelayForLatency, UIEventStream* eventStream, EventController* eventController, EntityCapsProvider* entityCapsProvider, HistoryController* historyController, MUCRegistry* mucRegistry, HighlightManager* highlightManager, std::shared_ptr<ChatMessageParser> chatMessageParser, AutoAcceptMUCInviteDecider* autoAcceptMUCInviteDecider, SettingsProvider* settings, Chattables& chattables) : selfJID_(self), stanzaChannel_(stanzaChannel), iqRouter_(iqRouter), chatWindowFactory_(chatWindowFactory), toJID_(toJID), labelsEnabled_(false), presenceOracle_(presenceOracle), avatarManager_(avatarManager), useDelayForLatency_(useDelayForLatency), eventController_(eventController), entityCapsProvider_(entityCapsProvider), historyController_(historyController), mucRegistry_(mucRegistry), chatMessageParser_(chatMessageParser), autoAcceptMUCInviteDecider_(autoAcceptMUCInviteDecider), eventStream_(eventStream), roomSecurityMarking_(""), previousMessageSecurityMarking_(""), settings_(settings), chattables_(chattables) { + chatWindow_ = chatWindowFactory_->createChatWindow(toJID, eventStream); + chatWindow_->onAllMessagesRead.connect(boost::bind(&ChatControllerBase::handleAllMessagesRead, this)); + chatWindow_->onSendMessageRequest.connect(boost::bind(&ChatControllerBase::handleSendMessageRequest, this, _1, _2)); + chatWindow_->onContinuationsBroken.connect(boost::bind(&ChatControllerBase::handleContinuationsBroken, this)); + scopedConnectionResendMessage_ = chatWindow_->onResendMessageRequest.connect(boost::bind(&ChatControllerBase::handleResendMessageRequest, this, _1)); + entityCapsProvider_->onCapsChanged.connect(boost::bind(&ChatControllerBase::handleCapsChanged, this, _1)); + highlighter_ = highlightManager->createHighlighter(nickResolver); + ChatControllerBase::setOnline(stanzaChannel->isAvailable() && iqRouter->isAvailable()); } ChatControllerBase::~ChatControllerBase() { - delete chatWindow_; + delete highlighter_; + delete chatWindow_; } -void ChatControllerBase::handleLogCleared() { - cancelReplaces(); +void ChatControllerBase::handleContinuationsBroken() { + cancelReplaces(); } ChatWindow* ChatControllerBase::detachChatWindow() { - ChatWindow* chatWindow = chatWindow_; - chatWindow_ = NULL; - return chatWindow; + chatWindow_->onContinuationsBroken.disconnect(boost::bind(&ChatControllerBase::handleContinuationsBroken, this)); + chatWindow_->onSendMessageRequest.disconnect(boost::bind(&ChatControllerBase::handleSendMessageRequest, this, _1, _2)); + chatWindow_->onAllMessagesRead.disconnect(boost::bind(&ChatControllerBase::handleAllMessagesRead, this)); + scopedConnectionResendMessage_.disconnect(); + ChatWindow* chatWindow = chatWindow_; + chatWindow_ = nullptr; + return chatWindow; } void ChatControllerBase::handleCapsChanged(const JID& jid) { - if (jid.compare(toJID_, JID::WithoutResource) == 0) { - handleBareJIDCapsChanged(jid); - } + if (jid.compare(toJID_, JID::WithoutResource) == 0) { + handleBareJIDCapsChanged(jid); + } } void ChatControllerBase::setCanStartImpromptuChats(bool supportsImpromptu) { - if (chatWindow_) { - chatWindow_->setCanInitiateImpromptuChats(supportsImpromptu); - } -} - -void ChatControllerBase::createDayChangeTimer() { - if (timerFactory_) { - boost::posix_time::ptime now = boost::posix_time::second_clock::local_time(); - boost::posix_time::ptime midnight(now.date() + boost::gregorian::days(1)); - int millisecondsUntilMidnight = boost::numeric_cast<int>((midnight - now).total_milliseconds()); - dateChangeTimer_ = boost::shared_ptr<Timer>(timerFactory_->createTimer(millisecondsUntilMidnight)); - dateChangeTimer_->onTick.connect(boost::bind(&ChatControllerBase::handleDayChangeTick, this)); - dateChangeTimer_->start(); - } -} - -void ChatControllerBase::handleDayChangeTick() { - dateChangeTimer_->stop(); - boost::posix_time::ptime now = boost::posix_time::second_clock::local_time(); - chatWindow_->addSystemMessage(chatMessageParser_->parseMessageBody(str(format(QT_TRANSLATE_NOOP("", "The day is now %1%")) % std::string(boost::posix_time::to_iso_extended_string(now)).substr(0,10))), ChatWindow::DefaultDirection); - dayTicked(); - createDayChangeTimer(); + if (chatWindow_) { + chatWindow_->setCanInitiateImpromptuChats(supportsImpromptu); + } } void ChatControllerBase::setEnabled(bool enabled) { - chatWindow_->setInputEnabled(enabled); + chatWindow_->setOnline(enabled); + chatWindow_->setCanInitiateImpromptuChats(false); } void ChatControllerBase::setOnline(bool online) { - setEnabled(online); + setEnabled(online); } JID ChatControllerBase::getBaseJID() { - return JID(toJID_.toBare()); + return JID(toJID_.toBare()); } -void ChatControllerBase::setAvailableServerFeatures(boost::shared_ptr<DiscoInfo> info) { - if (iqRouter_->isAvailable() && info->hasFeature(DiscoInfo::SecurityLabelsCatalogFeature)) { - GetSecurityLabelsCatalogRequest::ref request = GetSecurityLabelsCatalogRequest::create(getBaseJID(), iqRouter_); - request->onResponse.connect(boost::bind(&ChatControllerBase::handleSecurityLabelsCatalogResponse, this, _1, _2)); - request->send(); - } else { - chatWindow_->setSecurityLabelsEnabled(false); - labelsEnabled_ = false; - } +void ChatControllerBase::setAvailableServerFeatures(std::shared_ptr<DiscoInfo> info) { + if (iqRouter_->isAvailable() && info->hasFeature(DiscoInfo::SecurityLabelsCatalogFeature)) { + GetSecurityLabelsCatalogRequest::ref request = GetSecurityLabelsCatalogRequest::create(getBaseJID(), iqRouter_); + request->onResponse.connect(boost::bind(&ChatControllerBase::handleSecurityLabelsCatalogResponse, this, _1, _2)); + request->send(); + } else { + chatWindow_->setSecurityLabelsEnabled(false); + labelsEnabled_ = false; + } } void ChatControllerBase::handleAllMessagesRead() { - if (!unreadMessages_.empty()) { - targetedUnreadMessages_.clear(); - foreach (boost::shared_ptr<StanzaEvent> stanzaEvent, unreadMessages_) { - stanzaEvent->conclude(); - } - unreadMessages_.clear(); - chatWindow_->setUnreadMessageCount(0); - onUnreadCountChanged(); - } + if (!unreadMessages_.empty()) { + targetedUnreadMessages_.clear(); + for (std::shared_ptr<StanzaEvent> stanzaEvent : unreadMessages_) { + stanzaEvent->conclude(); + } + unreadMessages_.clear(); + updateMessageCount(); + } } -int ChatControllerBase::getUnreadCount() { - return boost::numeric_cast<int>(targetedUnreadMessages_.size()); +size_t ChatControllerBase::getUnreadCount() { + return targetedUnreadMessages_.size(); } void ChatControllerBase::handleSendMessageRequest(const std::string &body, bool isCorrectionMessage) { - if (!stanzaChannel_->isAvailable() || body.empty()) { - return; - } - boost::shared_ptr<Message> message(new Message()); - message->setTo(toJID_); - message->setType(Swift::Message::Chat); - message->setBody(body); - if (labelsEnabled_) { - if (!isCorrectionMessage) { - lastLabel_ = chatWindow_->getSelectedSecurityLabel(); - } - SecurityLabelsCatalog::Item labelItem = lastLabel_; - if (labelItem.getLabel()) { - message->addPayload(labelItem.getLabel()); - } - } - preSendMessageRequest(message); - - boost::posix_time::ptime now = boost::posix_time::microsec_clock::universal_time(); - if (useDelayForLatency_) { - message->addPayload(boost::make_shared<Delay>(now, selfJID_)); - } - if (isCorrectionMessage) { - message->addPayload(boost::shared_ptr<Replace> (new Replace(lastSentMessageStanzaID_))); - } - message->setID(lastSentMessageStanzaID_ = idGenerator_.generateID()); - stanzaChannel_->sendMessage(message); - postSendMessage(message->getBody(), boost::dynamic_pointer_cast<Stanza>(message)); - onActivity(message->getBody()); + if (!stanzaChannel_->isAvailable() || body.empty()) { + return; + } + std::shared_ptr<Message> message(new Message()); + message->setTo(toJID_); + message->setType(Swift::Message::Chat); + message->setBody(body); + if (labelsEnabled_) { + if (!isCorrectionMessage) { + lastLabel_ = chatWindow_->getSelectedSecurityLabel(); + } + SecurityLabelsCatalog::Item labelItem = lastLabel_; + if (labelItem.getLabel()) { + message->addPayload(labelItem.getLabel()); + } + } + preSendMessageRequest(message); + + boost::posix_time::ptime now = boost::posix_time::microsec_clock::universal_time(); + if (useDelayForLatency_) { + message->addPayload(std::make_shared<Delay>(now, selfJID_)); + } + if (isCorrectionMessage) { + message->addPayload(std::shared_ptr<Replace> (new Replace(lastSentMessageStanzaID_))); + } + message->setID(lastSentMessageStanzaID_ = idGenerator_.generateID()); + stanzaChannel_->sendMessage(message); + postSendMessage(message->getBody().get(), std::dynamic_pointer_cast<Stanza>(message)); + onActivity(message->getBody().get()); #ifdef SWIFT_EXPERIMENTAL_HISTORY - logMessage(body, selfJID_, toJID_, now, false); + logMessage(body, selfJID_, toJID_, now, false); #endif } -void ChatControllerBase::handleSecurityLabelsCatalogResponse(boost::shared_ptr<SecurityLabelsCatalog> catalog, ErrorPayload::ref error) { - if (catalog && !error) { - if (catalog->getItems().size() == 0) { - chatWindow_->setSecurityLabelsEnabled(false); - labelsEnabled_ = false; - } else { - labelsEnabled_ = true; - chatWindow_->setAvailableSecurityLabels(catalog->getItems()); - chatWindow_->setSecurityLabelsEnabled(true); - } - } else { - labelsEnabled_ = false; - chatWindow_->setSecurityLabelsError(); - } +void ChatControllerBase::handleSecurityLabelsCatalogResponse(std::shared_ptr<SecurityLabelsCatalog> catalog, ErrorPayload::ref error) { + if (catalog && !error) { + if (catalog->getItems().size() == 0) { + chatWindow_->setSecurityLabelsEnabled(false); + labelsEnabled_ = false; + } else { + labelsEnabled_ = true; + chatWindow_->setAvailableSecurityLabels(catalog->getItems()); + chatWindow_->setSecurityLabelsEnabled(true); + } + } else { + labelsEnabled_ = false; + chatWindow_->setSecurityLabelsError(); + } } void ChatControllerBase::showChatWindow() { - chatWindow_->show(); + chatWindow_->show(); } void ChatControllerBase::activateChatWindow() { - chatWindow_->activate(); + chatWindow_->activate(); +} + +bool ChatControllerBase::hasOpenWindow() const { + return chatWindow_ && chatWindow_->isVisible(); +} + +ChatWindow::ChatMessage ChatControllerBase::buildChatWindowChatMessage(const std::string& message, const std::string& senderName, bool senderIsSelf) { + ChatWindow::ChatMessage chatMessage; + chatMessage = chatMessageParser_->parseMessageBody(message, senderName, senderIsSelf); + return chatMessage; } -std::string ChatControllerBase::addMessage(const std::string& message, const std::string& senderName, bool senderIsSelf, const boost::shared_ptr<SecurityLabel> label, const boost::filesystem::path& avatarPath, const boost::posix_time::ptime& time, const HighlightAction& highlight) { - if (boost::starts_with(message, "/me ")) { - return chatWindow_->addAction(chatMessageParser_->parseMessageBody(String::getSplittedAtFirst(message, ' ').second), senderName, senderIsSelf, label, pathToString(avatarPath), time, highlight); - } else { - return chatWindow_->addMessage(chatMessageParser_->parseMessageBody(message), senderName, senderIsSelf, label, pathToString(avatarPath), time, highlight); - } +void ChatControllerBase::updateMessageCount() { + auto baseJID = getBaseJID(); + auto state = chattables_.getState(baseJID); + state.unreadCount = unreadMessages_.size(); + chatWindow_->setUnreadMessageCount(state.unreadCount); + chattables_.setState(baseJID, state); +#ifndef NOT_YET + onUnreadCountChanged(); +#endif +} + +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) { + auto displayedLabel = label; + + if (settings_->getSetting(SettingConstants::MUC_MARKING_ELISION)) { + if (roomSecurityMarking_ != "") { + + if (label && label->getDisplayMarking() == roomSecurityMarking_) { + if (label->getDisplayMarking() == previousMessageSecurityMarking_) { + displayedLabel = std::make_shared<SecurityLabel>(); + } + previousMessageSecurityMarking_ = label->getDisplayMarking(); + } + else if (!label || label->getDisplayMarking().empty()) { + displayedLabel = std::make_shared<SecurityLabel>(); + displayedLabel->setDisplayMarking("Unmarked"); + previousMessageSecurityMarking_ = "Unmarked"; + } + else { + previousMessageSecurityMarking_ = displayedLabel->getDisplayMarking(); + } + } + } + + if (chatMessage.isMeCommand()) { + return chatWindow_->addAction(chatMessage, senderName, senderIsSelf, displayedLabel, pathToString(avatarPath), time); + } + else { + return chatWindow_->addMessage(chatMessage, senderName, senderIsSelf, displayedLabel, pathToString(avatarPath), time); + } } -void ChatControllerBase::replaceMessage(const std::string& message, const std::string& id, const boost::posix_time::ptime& time, const HighlightAction& highlight) { - if (boost::starts_with(message, "/me ")) { - chatWindow_->replaceWithAction(chatMessageParser_->parseMessageBody(String::getSplittedAtFirst(message, ' ').second), id, time, highlight); - } else { - chatWindow_->replaceMessage(chatMessageParser_->parseMessageBody(message), id, time, highlight); - } +void ChatControllerBase::replaceMessage(const ChatWindow::ChatMessage& chatMessage, const std::string& id, const boost::posix_time::ptime& time) { + if (chatMessage.isMeCommand()) { + chatWindow_->replaceWithAction(chatMessage, id, time); + } + else { + chatWindow_->replaceMessage(chatMessage, id, time); + } } bool ChatControllerBase::isFromContact(const JID& from) { - return from.toBare() == toJID_.toBare(); + return from.toBare() == toJID_.toBare(); } -void ChatControllerBase::handleIncomingMessage(boost::shared_ptr<MessageEvent> messageEvent) { - preHandleIncomingMessage(messageEvent); - if (messageEvent->isReadable() && !messageEvent->getConcluded()) { - unreadMessages_.push_back(messageEvent); - if (messageEvent->targetsMe()) { - targetedUnreadMessages_.push_back(messageEvent); - } - } - boost::shared_ptr<Message> message = messageEvent->getStanza(); - std::string body = message->getBody(); - HighlightAction highlight; - if (message->isError()) { - if (!message->getTo().getResource().empty()) { - std::string errorMessage = str(format(QT_TRANSLATE_NOOP("", "Couldn't send message: %1%")) % getErrorMessage(message->getPayload<ErrorPayload>())); - chatWindow_->addErrorMessage(chatMessageParser_->parseMessageBody(errorMessage)); - } - } - else if (messageEvent->getStanza()->getPayload<MUCInvitationPayload>()) { - handleMUCInvitation(messageEvent->getStanza()); - return; - } - else if (messageEvent->getStanza()->getPayload<MUCUserPayload>() && messageEvent->getStanza()->getPayload<MUCUserPayload>()->getInvite()) { - handleMediatedMUCInvitation(messageEvent->getStanza()); - return; - } - else { - if (!messageEvent->isReadable()) { - return; - } - showChatWindow(); - JID from = message->getFrom(); - std::vector<boost::shared_ptr<Delay> > delayPayloads = message->getPayloads<Delay>(); - for (size_t i = 0; useDelayForLatency_ && i < delayPayloads.size(); i++) { - if (!delayPayloads[i]->getFrom()) { - continue; - } - boost::posix_time::ptime now = boost::posix_time::microsec_clock::universal_time(); - std::ostringstream s; - s << "The following message took " << (now - delayPayloads[i]->getStamp()).total_milliseconds() / 1000.0 << " seconds to be delivered from " << delayPayloads[i]->getFrom()->toString() << "."; - chatWindow_->addSystemMessage(chatMessageParser_->parseMessageBody(std::string(s.str())), ChatWindow::DefaultDirection); - } - boost::shared_ptr<SecurityLabel> label = message->getPayload<SecurityLabel>(); - - // Determine the timestamp - boost::posix_time::ptime timeStamp = boost::posix_time::microsec_clock::universal_time(); - boost::optional<boost::posix_time::ptime> messageTimeStamp = getMessageTimestamp(message); - if (messageTimeStamp) { - timeStamp = *messageTimeStamp; - } - onActivity(body); - - // Highlight - if (!isIncomingMessageFromMe(message)) { - highlight = highlighter_->findAction(body, senderDisplayNameFromMessage(from)); - } - - boost::shared_ptr<Replace> replace = message->getPayload<Replace>(); - if (replace) { - std::string body = message->getBody(); - // Should check if the user has a previous message - std::map<JID, std::string>::iterator lastMessage; - lastMessage = lastMessagesUIID_.find(from); - if (lastMessage != lastMessagesUIID_.end()) { - replaceMessage(body, lastMessagesUIID_[from], timeStamp, highlight); - } - } - else { - lastMessagesUIID_[from] = addMessage(body, senderDisplayNameFromMessage(from), isIncomingMessageFromMe(message), label, avatarManager_->getAvatarPath(from), timeStamp, highlight); - } - - logMessage(body, from, selfJID_, timeStamp, true); - } - chatWindow_->show(); - chatWindow_->setUnreadMessageCount(boost::numeric_cast<int>(unreadMessages_.size())); - onUnreadCountChanged(); - postHandleIncomingMessage(messageEvent, highlight); +void ChatControllerBase::handleIncomingMessage(std::shared_ptr<MessageEvent> messageEvent) { + std::shared_ptr<Message> message = messageEvent->getStanza(); + if (shouldIgnoreMessage(message)) { + return; + } + + preHandleIncomingMessage(messageEvent); + if (messageEvent->isReadable() && !messageEvent->getConcluded()) { + unreadMessages_.push_back(messageEvent); + if (messageEvent->targetsMe()) { + targetedUnreadMessages_.push_back(messageEvent); + } + } + + ChatWindow::ChatMessage chatMessage; + boost::optional<std::string> optionalBody = message->getBody(); + std::string body = optionalBody.get_value_or(""); + if (message->isError()) { + if (!message->getTo().getResource().empty()) { + std::string errorMessage; + if (message->getPayload<Swift::ErrorPayload>()->getCondition() == ErrorPayload::ItemNotFound) { + errorMessage = QT_TRANSLATE_NOOP("", "This user could not be found in the room."); + } + else { + errorMessage = str(format(QT_TRANSLATE_NOOP("", "Couldn't send message: %1%")) % getErrorMessage(message->getPayload<ErrorPayload>())); + } + chatWindow_->addErrorMessage(chatMessageParser_->parseMessageBody(errorMessage)); + } + } + else if (messageEvent->getStanza()->getPayload<MUCInvitationPayload>()) { + handleMUCInvitation(messageEvent->getStanza()); + return; + } + else if (messageEvent->getStanza()->getPayload<MUCUserPayload>() && messageEvent->getStanza()->getPayload<MUCUserPayload>()->getInvite()) { + handleMediatedMUCInvitation(messageEvent->getStanza()); + return; + } + else { + if (!messageEvent->isReadable()) { + return; + } + showChatWindow(); + JID from = message->getFrom(); + std::vector<std::shared_ptr<Delay> > delayPayloads = message->getPayloads<Delay>(); + for (size_t i = 0; useDelayForLatency_ && i < delayPayloads.size(); i++) { + if (!delayPayloads[i]->getFrom()) { + continue; + } + boost::posix_time::ptime now = boost::posix_time::microsec_clock::universal_time(); + std::ostringstream s; + s << "The following message took " << (now - delayPayloads[i]->getStamp()).total_milliseconds() / 1000.0 << " seconds to be delivered from " << delayPayloads[i]->getFrom()->toString() << "."; + chatWindow_->addSystemMessage(chatMessageParser_->parseMessageBody(std::string(s.str())), ChatWindow::DefaultDirection); + } + std::shared_ptr<SecurityLabel> label = message->getPayload<SecurityLabel>(); + + // Determine the timestamp + boost::posix_time::ptime timeStamp = boost::posix_time::microsec_clock::universal_time(); + boost::optional<boost::posix_time::ptime> messageTimeStamp = getMessageTimestamp(message); + if (messageTimeStamp) { + timeStamp = *messageTimeStamp; + } + onActivity(body); + + std::shared_ptr<Replace> replace = message->getPayload<Replace>(); + bool senderIsSelf = isIncomingMessageFromMe(message); + chatMessage = buildChatWindowChatMessage(body, senderHighlightNameFromMessage(from), senderIsSelf); + if (replace) { + handleIncomingReplaceMessage(from, chatMessage, message->getID(), replace->getID(), senderIsSelf, label, timeStamp); + } + else { + addMessageHandleIncomingMessage(from, chatMessage, message->getID(), senderIsSelf, label, timeStamp); + } + +#ifdef SWIFT_EXPERIMENTAL_HISTORY + logMessage(body, from, selfJID_, timeStamp, true); +#endif + } + chatWindow_->show(); + updateMessageCount(); + postHandleIncomingMessage(messageEvent, chatMessage); } -std::string ChatControllerBase::getErrorMessage(boost::shared_ptr<ErrorPayload> error) { - std::string defaultMessage = QT_TRANSLATE_NOOP("", "Error sending message"); - if (!error->getText().empty()) { - return error->getText(); - } - else { - switch (error->getCondition()) { - case ErrorPayload::BadRequest: return QT_TRANSLATE_NOOP("", "Bad request"); - case ErrorPayload::Conflict: return QT_TRANSLATE_NOOP("", "Conflict"); - case ErrorPayload::FeatureNotImplemented: return QT_TRANSLATE_NOOP("", "This feature is not implemented"); - case ErrorPayload::Forbidden: return QT_TRANSLATE_NOOP("", "Forbidden"); - case ErrorPayload::Gone: return QT_TRANSLATE_NOOP("", "Recipient can no longer be contacted"); - case ErrorPayload::InternalServerError: return QT_TRANSLATE_NOOP("", "Internal server error"); - case ErrorPayload::ItemNotFound: return QT_TRANSLATE_NOOP("", "Item not found"); - case ErrorPayload::JIDMalformed: return QT_TRANSLATE_NOOP("", "JID Malformed"); - case ErrorPayload::NotAcceptable: return QT_TRANSLATE_NOOP("", "Message was rejected"); - case ErrorPayload::NotAllowed: return QT_TRANSLATE_NOOP("", "Not allowed"); - case ErrorPayload::NotAuthorized: return QT_TRANSLATE_NOOP("", "Not authorized"); - case ErrorPayload::PaymentRequired: return QT_TRANSLATE_NOOP("", "Payment is required"); - case ErrorPayload::RecipientUnavailable: return QT_TRANSLATE_NOOP("", "Recipient is unavailable"); - case ErrorPayload::Redirect: return QT_TRANSLATE_NOOP("", "Redirect"); - case ErrorPayload::RegistrationRequired: return QT_TRANSLATE_NOOP("", "Registration required"); - case ErrorPayload::RemoteServerNotFound: return QT_TRANSLATE_NOOP("", "Recipient's server not found"); - case ErrorPayload::RemoteServerTimeout: return QT_TRANSLATE_NOOP("", "Remote server timeout"); - case ErrorPayload::ResourceConstraint: return QT_TRANSLATE_NOOP("", "The server is low on resources"); - case ErrorPayload::ServiceUnavailable: return QT_TRANSLATE_NOOP("", "The service is unavailable"); - case ErrorPayload::SubscriptionRequired: return QT_TRANSLATE_NOOP("", "A subscription is required"); - case ErrorPayload::UndefinedCondition: return QT_TRANSLATE_NOOP("", "Undefined condition"); - case ErrorPayload::UnexpectedRequest: return QT_TRANSLATE_NOOP("", "Unexpected request"); - } - } - assert(false); - return defaultMessage; +std::string ChatControllerBase::getErrorMessage(std::shared_ptr<ErrorPayload> error) { + std::string defaultMessage = QT_TRANSLATE_NOOP("", "Error sending message"); + if (!error->getText().empty()) { + return error->getText(); + } + else { + switch (error->getCondition()) { + case ErrorPayload::BadRequest: return QT_TRANSLATE_NOOP("", "Bad request"); + case ErrorPayload::Conflict: return QT_TRANSLATE_NOOP("", "Conflict"); + case ErrorPayload::FeatureNotImplemented: return QT_TRANSLATE_NOOP("", "This feature is not implemented"); + case ErrorPayload::Forbidden: return QT_TRANSLATE_NOOP("", "Forbidden"); + case ErrorPayload::Gone: return QT_TRANSLATE_NOOP("", "Recipient can no longer be contacted"); + case ErrorPayload::InternalServerError: return QT_TRANSLATE_NOOP("", "Internal server error"); + case ErrorPayload::ItemNotFound: return QT_TRANSLATE_NOOP("", "Item not found"); + case ErrorPayload::JIDMalformed: return QT_TRANSLATE_NOOP("", "JID Malformed"); + case ErrorPayload::NotAcceptable: return QT_TRANSLATE_NOOP("", "Message was rejected"); + case ErrorPayload::NotAllowed: return QT_TRANSLATE_NOOP("", "Not allowed"); + case ErrorPayload::NotAuthorized: return QT_TRANSLATE_NOOP("", "Not authorized"); + case ErrorPayload::PaymentRequired: return QT_TRANSLATE_NOOP("", "Payment is required"); + case ErrorPayload::RecipientUnavailable: return QT_TRANSLATE_NOOP("", "Recipient is unavailable"); + case ErrorPayload::Redirect: return QT_TRANSLATE_NOOP("", "Redirect"); + case ErrorPayload::RegistrationRequired: return QT_TRANSLATE_NOOP("", "Registration required"); + case ErrorPayload::RemoteServerNotFound: return QT_TRANSLATE_NOOP("", "Recipient's server not found"); + case ErrorPayload::RemoteServerTimeout: return QT_TRANSLATE_NOOP("", "Remote server timeout"); + case ErrorPayload::ResourceConstraint: return QT_TRANSLATE_NOOP("", "The server is low on resources"); + case ErrorPayload::ServiceUnavailable: return QT_TRANSLATE_NOOP("", "The service is unavailable"); + case ErrorPayload::SubscriptionRequired: return QT_TRANSLATE_NOOP("", "A subscription is required"); + case ErrorPayload::UndefinedCondition: return QT_TRANSLATE_NOOP("", "Undefined condition"); + case ErrorPayload::UnexpectedRequest: return QT_TRANSLATE_NOOP("", "Unexpected request"); + } + } + assert(false); + return defaultMessage; } void ChatControllerBase::handleGeneralMUCInvitation(MUCInviteEvent::ref event) { - unreadMessages_.push_back(event); - chatWindow_->show(); - chatWindow_->setUnreadMessageCount(boost::numeric_cast<int>(unreadMessages_.size())); - onUnreadCountChanged(); - chatWindow_->addMUCInvitation(senderDisplayNameFromMessage(event->getInviter()), event->getRoomJID(), event->getReason(), event->getPassword(), event->getDirect(), event->getImpromptu()); - eventController_->handleIncomingEvent(event); + unreadMessages_.push_back(event); + chatWindow_->show(); + updateMessageCount(); + chatWindow_->addMUCInvitation(senderDisplayNameFromMessage(event->getInviter()), event->getRoomJID(), event->getReason(), event->getPassword(), event->getDirect(), event->getImpromptu()); + eventController_->handleIncomingEvent(event); + lastWasPresence_ = false; } void ChatControllerBase::handleMUCInvitation(Message::ref message) { - MUCInvitationPayload::ref invite = message->getPayload<MUCInvitationPayload>(); - - if (autoAcceptMUCInviteDecider_->isAutoAcceptedInvite(message->getFrom(), invite)) { - eventStream_->send(boost::make_shared<JoinMUCUIEvent>(invite->getJID(), boost::optional<std::string>(), boost::optional<std::string>(), false, false, true)); - } else { - MUCInviteEvent::ref inviteEvent = boost::make_shared<MUCInviteEvent>(toJID_, invite->getJID(), invite->getReason(), invite->getPassword(), true, invite->getIsImpromptu()); - handleGeneralMUCInvitation(inviteEvent); - } + MUCInvitationPayload::ref invite = message->getPayload<MUCInvitationPayload>(); + + if (autoAcceptMUCInviteDecider_->isAutoAcceptedInvite(message->getFrom(), invite)) { + eventStream_->send(std::make_shared<JoinMUCUIEvent>(invite->getJID(), boost::optional<std::string>(), boost::optional<std::string>(), false, true)); + } else { + MUCInviteEvent::ref inviteEvent = std::make_shared<MUCInviteEvent>(toJID_, invite->getJID(), invite->getReason(), invite->getPassword(), true, invite->getIsImpromptu()); + handleGeneralMUCInvitation(inviteEvent); + } } void ChatControllerBase::handleMediatedMUCInvitation(Message::ref message) { - MUCUserPayload::Invite invite = *message->getPayload<MUCUserPayload>()->getInvite(); - JID from = message->getFrom(); - std::string reason; - if (!invite.reason.empty()) { - reason = invite.reason; - } - std::string password; - if (message->getPayload<MUCUserPayload>()->getPassword()) { - password = *message->getPayload<MUCUserPayload>()->getPassword(); - } - - MUCInviteEvent::ref inviteEvent = boost::make_shared<MUCInviteEvent>(invite.from, from, reason, password, false, false); - handleGeneralMUCInvitation(inviteEvent); + MUCUserPayload::Invite invite = *message->getPayload<MUCUserPayload>()->getInvite(); + JID from = message->getFrom(); + std::string reason; + if (!invite.reason.empty()) { + reason = invite.reason; + } + std::string password; + if (message->getPayload<MUCUserPayload>()->getPassword()) { + password = *message->getPayload<MUCUserPayload>()->getPassword(); + } + + MUCInviteEvent::ref inviteEvent = std::make_shared<MUCInviteEvent>(invite.from, from, reason, password, false, false); + handleGeneralMUCInvitation(inviteEvent); +} + +void ChatControllerBase::handleResendMessageRequest(const std::string& id) { + if (failedStanzas_.find(id) != failedStanzas_.end()) { + if (auto resendMsg = std::dynamic_pointer_cast<Message>(failedStanzas_[id])) { + stanzaChannel_->sendMessage(resendMsg); + if (stanzaChannel_->getStreamManagementEnabled()) { + chatWindow_->setAckState(id, ChatWindow::Pending); + unackedStanzas_[failedStanzas_[id]] = id; + } + if (resendMsg->getPayload<DeliveryReceiptRequest>()) { + requestedReceipts_[resendMsg->getID()] = id; + chatWindow_->setMessageReceiptState(id, ChatWindow::ReceiptRequested); + } + lastWasPresence_ = false; + failedStanzas_.erase(id); + } + } } } diff --git a/Swift/Controllers/Chat/ChatControllerBase.h b/Swift/Controllers/Chat/ChatControllerBase.h index 7db94a4..92c6175 100644 --- a/Swift/Controllers/Chat/ChatControllerBase.h +++ b/Swift/Controllers/Chat/ChatControllerBase.h @@ -1,134 +1,165 @@ /* - * Copyright (c) 2010-2013 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once #include <map> -#include <vector> +#include <memory> #include <string> +#include <vector> -#include <boost/shared_ptr.hpp> +#include <boost/date_time/posix_time/posix_time.hpp> #include <boost/filesystem/path.hpp> #include <boost/optional.hpp> -#include <boost/date_time/posix_time/posix_time.hpp> +#include <boost/signals2.hpp> -#include <Swiften/Network/Timer.h> -#include <Swiften/Network/TimerFactory.h> -#include <Swiften/Elements/Stanza.h> -#include <Swiften/Base/boost_bsignals.h> +#include <Swiften/Base/IDGenerator.h> #include <Swiften/Elements/DiscoInfo.h> -#include <Swiften/JID/JID.h> -#include <Swiften/Elements/SecurityLabelsCatalog.h> #include <Swiften/Elements/ErrorPayload.h> +#include <Swiften/Elements/SecurityLabelsCatalog.h> +#include <Swiften/Elements/Stanza.h> +#include <Swiften/JID/JID.h> +#include <Swiften/MUC/MUCRegistry.h> #include <Swiften/Presence/PresenceOracle.h> #include <Swiften/Queries/IQRouter.h> -#include <Swiften/Base/IDGenerator.h> -#include <Swiften/MUC/MUCRegistry.h> -#include <Swift/Controllers/XMPPEvents/MessageEvent.h> -#include <Swift/Controllers/XMPPEvents/MUCInviteEvent.h> +#include <Swift/Controllers/Highlighting/HighlightManager.h> #include <Swift/Controllers/HistoryController.h> -#include <Swift/Controllers/HighlightManager.h> +#include <Swift/Controllers/Settings/SettingsProvider.h> #include <Swift/Controllers/UIInterfaces/ChatWindow.h> +#include <Swift/Controllers/XMPPEvents/MUCInviteEvent.h> +#include <Swift/Controllers/XMPPEvents/MessageEvent.h> namespace Swift { - class IQRouter; - class StanzaChannel; - class ChatWindowFactory; - class AvatarManager; - class UIEventStream; - class EventController; - class EntityCapsProvider; - class HighlightManager; - class Highlighter; - class ChatMessageParser; - class AutoAcceptMUCInviteDecider; + class AutoAcceptMUCInviteDecider; + class AvatarManager; + class ChatMessageParser; + class Chattables; + class ChatWindowFactory; + class EntityCapsProvider; + class EventController; + class Highlighter; + class HighlightManager; + class IQRouter; + class NickResolver; + class StanzaChannel; + class UIEventStream; + + class ChatControllerBase : public boost::signals2::trackable { + public: + class StreamWindowMessageIDPair { + public: + std::string idInStream; + std::string idInWindow; + }; + + public: + virtual ~ChatControllerBase(); + void showChatWindow(); + void activateChatWindow(); + bool hasOpenWindow() const; + virtual void setAvailableServerFeatures(std::shared_ptr<DiscoInfo> info); + virtual void handleIncomingOwnMessage(std::shared_ptr<Message> /*message*/) {} + void handleIncomingMessage(std::shared_ptr<MessageEvent> message); + std::string addMessage(const ChatWindow::ChatMessage& chatMessage, const std::string& senderName, bool senderIsSelf, std::shared_ptr<SecurityLabel> label, const boost::filesystem::path& avatarPath, const boost::posix_time::ptime& time); + void replaceMessage(const ChatWindow::ChatMessage& chatMessage, const std::string& id, const boost::posix_time::ptime& time); + virtual void setOnline(bool online); + void setEnabled(bool enabled); + virtual void setToJID(const JID& jid) {toJID_ = jid;} + /** Used for determining when something is recent.*/ + boost::signals2::signal<void (const std::string& /*activity*/)> onActivity; + boost::signals2::signal<void ()> onUnreadCountChanged; + boost::signals2::signal<void ()> onWindowClosed; + size_t getUnreadCount(); + const JID& getToJID() {return toJID_;} + void handleCapsChanged(const JID& jid); + void setCanStartImpromptuChats(bool supportsImpromptu); + virtual ChatWindow* detachChatWindow(); + boost::signals2::signal<void(ChatWindow* /*window to reuse*/, const std::vector<JID>& /*invite people*/, const std::string& /*reason*/)> onConvertToMUC; + + protected: + ChatControllerBase(const JID& self, StanzaChannel* stanzaChannel, IQRouter* iqRouter, ChatWindowFactory* chatWindowFactory, const JID &toJID, NickResolver* nickResolver, PresenceOracle* presenceOracle, AvatarManager* avatarManager, bool useDelayForLatency, UIEventStream* eventStream, EventController* eventController, EntityCapsProvider* entityCapsProvider, HistoryController* historyController, MUCRegistry* mucRegistry, HighlightManager* highlightManager, std::shared_ptr<ChatMessageParser> chatMessageParser, AutoAcceptMUCInviteDecider* autoAcceptMUCInviteDecider, SettingsProvider* settings, Chattables& chattables); - class ChatControllerBase : public boost::bsignals::trackable { - public: - virtual ~ChatControllerBase(); - void showChatWindow(); - void activateChatWindow(); - virtual void setAvailableServerFeatures(boost::shared_ptr<DiscoInfo> info); - void handleIncomingMessage(boost::shared_ptr<MessageEvent> message); - std::string addMessage(const std::string& message, const std::string& senderName, bool senderIsSelf, boost::shared_ptr<SecurityLabel> label, const boost::filesystem::path& avatarPath, const boost::posix_time::ptime& time, const HighlightAction& highlight); - void replaceMessage(const std::string& message, const std::string& id, const boost::posix_time::ptime& time, const HighlightAction& highlight); - virtual void setOnline(bool online); - virtual void setEnabled(bool enabled); - 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_;} - void handleCapsChanged(const JID& jid); - void setCanStartImpromptuChats(bool supportsImpromptu); - virtual ChatWindow* detachChatWindow(); - boost::signal<void(ChatWindow* /*window to reuse*/, const std::vector<JID>& /*invite people*/, const std::string& /*reason*/)> onConvertToMUC; + /** + * Pass the Message appended, and the stanza used to send it. + */ + virtual void postSendMessage(const std::string&, std::shared_ptr<Stanza>) {} + virtual std::string senderDisplayNameFromMessage(const JID& from) = 0; + virtual std::string senderHighlightNameFromMessage(const JID& from) = 0; + virtual bool isIncomingMessageFromMe(std::shared_ptr<Message>) = 0; + virtual void preHandleIncomingMessage(std::shared_ptr<MessageEvent>) {} + virtual void addMessageHandleIncomingMessage(const JID& from, const ChatWindow::ChatMessage& message, const std::string& messageID, bool senderIsSelf, std::shared_ptr<SecurityLabel> label, const boost::posix_time::ptime& time) = 0; + virtual void handleIncomingReplaceMessage(const JID& from, const ChatWindow::ChatMessage& chatMessage, const std::string& messageID, const std::string& idToReplace, bool senderIsSelf, std::shared_ptr<SecurityLabel> label, const boost::posix_time::ptime& timeStamp) = 0; + virtual void postHandleIncomingMessage(std::shared_ptr<MessageEvent>, const ChatWindow::ChatMessage&) {} + virtual void preSendMessageRequest(std::shared_ptr<Message>) {} + virtual bool isFromContact(const JID& from); + virtual boost::optional<boost::posix_time::ptime> getMessageTimestamp(std::shared_ptr<Message>) const = 0; + virtual void dayTicked() {} + virtual void handleBareJIDCapsChanged(const JID& jid) = 0; + std::string getErrorMessage(std::shared_ptr<ErrorPayload>); + virtual void setContactIsReceivingPresence(bool /* isReceivingPresence */) {} + virtual void cancelReplaces() = 0; + /** JID any iq for account should go to - bare except for PMs */ + virtual JID getBaseJID(); + 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, const std::string& senderName, bool senderIsSelf); + void updateMessageCount(); + virtual bool shouldIgnoreMessage(std::shared_ptr<Message> /* message */) { + return false; + } - 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, EntityCapsProvider* entityCapsProvider, HistoryController* historyController, MUCRegistry* mucRegistry, HighlightManager* highlightManager, ChatMessageParser* chatMessageParser, AutoAcceptMUCInviteDecider* autoAcceptMUCInviteDecider); + /** + * What JID should be used for last message correction (XEP-0308) tracking. + */ + virtual JID messageCorrectionJID(const JID& fromJID) = 0; + virtual void handleResendMessageRequest(const std::string& id); - /** - * Pass the Message appended, and the stanza used to send it. - */ - virtual void postSendMessage(const std::string&, boost::shared_ptr<Stanza>) {} - virtual std::string senderDisplayNameFromMessage(const JID& from) = 0; - virtual bool isIncomingMessageFromMe(boost::shared_ptr<Message>) = 0; - virtual void preHandleIncomingMessage(boost::shared_ptr<MessageEvent>) {} - virtual void postHandleIncomingMessage(boost::shared_ptr<MessageEvent>, const HighlightAction&) {} - virtual void preSendMessageRequest(boost::shared_ptr<Message>) {} - virtual bool isFromContact(const JID& from); - virtual boost::optional<boost::posix_time::ptime> getMessageTimestamp(boost::shared_ptr<Message>) const = 0; - virtual void dayTicked() {} - virtual void handleBareJIDCapsChanged(const JID& jid) = 0; - std::string getErrorMessage(boost::shared_ptr<ErrorPayload>); - virtual void setContactIsReceivingPresence(bool /* isReceivingPresence */) {} - virtual void cancelReplaces() = 0; - /** JID any iq for account should go to - bare except for PMs */ - virtual JID getBaseJID(); - virtual void logMessage(const std::string& message, const JID& fromJID, const JID& toJID, const boost::posix_time::ptime& timeStamp, bool isIncoming) = 0; + private: + IDGenerator idGenerator_; + std::string lastSentMessageStanzaID_; - private: - IDGenerator idGenerator_; - std::string lastSentMessageStanzaID_; - void createDayChangeTimer(); - void handleSendMessageRequest(const std::string &body, bool isCorrectionMessage); - void handleAllMessagesRead(); - void handleSecurityLabelsCatalogResponse(boost::shared_ptr<SecurityLabelsCatalog>, ErrorPayload::ref error); - void handleDayChangeTick(); - void handleMUCInvitation(Message::ref message); - void handleMediatedMUCInvitation(Message::ref message); - void handleGeneralMUCInvitation(MUCInviteEvent::ref event); - void handleLogCleared(); + void handleSendMessageRequest(const std::string &body, bool isCorrectionMessage); + void handleAllMessagesRead(); + void handleSecurityLabelsCatalogResponse(std::shared_ptr<SecurityLabelsCatalog>, ErrorPayload::ref error); + void handleMUCInvitation(Message::ref message); + void handleMediatedMUCInvitation(Message::ref message); + void handleGeneralMUCInvitation(MUCInviteEvent::ref event); + void handleContinuationsBroken(); - protected: - JID selfJID_; - std::vector<boost::shared_ptr<StanzaEvent> > unreadMessages_; - std::vector<boost::shared_ptr<StanzaEvent> > targetedUnreadMessages_; - StanzaChannel* stanzaChannel_; - IQRouter* iqRouter_; - ChatWindowFactory* chatWindowFactory_; - ChatWindow* chatWindow_; - JID toJID_; - bool labelsEnabled_; - std::map<JID, std::string> lastMessagesUIID_; - PresenceOracle* presenceOracle_; - AvatarManager* avatarManager_; - bool useDelayForLatency_; - EventController* eventController_; - boost::shared_ptr<Timer> dateChangeTimer_; - TimerFactory* timerFactory_; - EntityCapsProvider* entityCapsProvider_; - SecurityLabelsCatalog::Item lastLabel_; - HistoryController* historyController_; - MUCRegistry* mucRegistry_; - Highlighter* highlighter_; - ChatMessageParser* chatMessageParser_; - AutoAcceptMUCInviteDecider* autoAcceptMUCInviteDecider_; - UIEventStream* eventStream_; - }; + protected: + JID selfJID_; + std::vector<std::shared_ptr<StanzaEvent> > unreadMessages_; + std::vector<std::shared_ptr<StanzaEvent> > targetedUnreadMessages_; + StanzaChannel* stanzaChannel_; + IQRouter* iqRouter_; + ChatWindowFactory* chatWindowFactory_; + ChatWindow* chatWindow_; + JID toJID_; + bool labelsEnabled_; + std::map<JID, StreamWindowMessageIDPair> lastMessagesIDs_; + PresenceOracle* presenceOracle_; + AvatarManager* avatarManager_; + bool useDelayForLatency_; + EventController* eventController_; + EntityCapsProvider* entityCapsProvider_; + SecurityLabelsCatalog::Item lastLabel_; + HistoryController* historyController_; + MUCRegistry* mucRegistry_; + Highlighter* highlighter_; + std::shared_ptr<ChatMessageParser> chatMessageParser_; + AutoAcceptMUCInviteDecider* autoAcceptMUCInviteDecider_; + UIEventStream* eventStream_; + bool lastWasPresence_ = false; + std::string roomSecurityMarking_; + std::string previousMessageSecurityMarking_; + SettingsProvider* settings_; + boost::signals2::scoped_connection scopedConnectionResendMessage_; + std::map<std::string, std::string> requestedReceipts_; + std::map<std::shared_ptr<Stanza>, std::string> unackedStanzas_; + std::map<std::string, std::shared_ptr<Stanza>> failedStanzas_; + Chattables& chattables_; + }; } diff --git a/Swift/Controllers/Chat/ChatListWindowChatBoostSerialize.h b/Swift/Controllers/Chat/ChatListWindowChatBoostSerialize.h new file mode 100644 index 0000000..1e053b2 --- /dev/null +++ b/Swift/Controllers/Chat/ChatListWindowChatBoostSerialize.h @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2017 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#pragma once + +#include <memory> + +#include <boost/version.hpp> +#include <boost/algorithm/string.hpp> +#include <boost/archive/text_iarchive.hpp> +#include <boost/archive/text_oarchive.hpp> +#include <boost/bind.hpp> +#include <boost/serialization/map.hpp> +#include <boost/serialization/optional.hpp> +#include <boost/serialization/split_free.hpp> +#include <boost/serialization/string.hpp> +#include <boost/serialization/vector.hpp> + +#include <Swift/Controllers/UIInterfaces/ChatListWindow.h> + +BOOST_CLASS_VERSION(Swift::ChatListWindow::Chat, 3) +namespace Swift { + const boost::archive::library_version_type BoostArchiveSkipVersion(15); +} + +namespace boost { + namespace serialization { + template<class Archive> void save(Archive& ar, const Swift::JID& jid, const unsigned int /*version*/) { + std::string jidStr = jid.toString(); + ar << jidStr; + } + + template<class Archive> void load(Archive& ar, Swift::JID& jid, const unsigned int /*version*/) { + std::string stringJID; + ar >> stringJID; + jid = Swift::JID(stringJID); + } + + template<class Archive> inline void serialize(Archive& ar, Swift::JID& t, const unsigned int file_version) { + split_free(ar, t, file_version); + } + + template<class Archive> void serialize(Archive& ar, Swift::ChatListWindow::Chat& chat, const unsigned int version) { + auto archiveLibraryVersion = boost::archive::BOOST_ARCHIVE_VERSION(); + int archiveVersion = 0; + archive::text_oarchive* outputStream = dynamic_cast<archive::text_oarchive*>(&ar); + if (outputStream) { + archiveVersion = outputStream->get_library_version(); + } + archive::text_iarchive* inputStream = dynamic_cast<archive::text_iarchive*>(&ar); + if (inputStream) { + archiveVersion = inputStream->get_library_version(); + //Due to https://svn.boost.org/trac10/ticket/13050 the password field may fail to load and crash the client. Therefore we skip loading the values from previous versions. + if (archiveLibraryVersion == Swift::BoostArchiveSkipVersion && archiveLibraryVersion > archiveVersion) { + return; + } + } + ar & chat.jid; + ar & chat.chatName; + ar & chat.activity; + ar & chat.isMUC; + ar & chat.nick; + ar & chat.impromptuJIDs; + if (version > 0) { + if (outputStream && archiveLibraryVersion == Swift::BoostArchiveSkipVersion) { + //The bug does not affect the case boost::optional doesn't have a value. Therefore we store always that value, to avoid problem on future launches of the client. + boost::optional<std::string> empty; + ar & empty; + } + else { + ar & chat.password; + } + } + if (version > 1) { + ar & chat.inviteesNames; + } + } + } +} diff --git a/Swift/Controllers/Chat/ChatMessageParser.cpp b/Swift/Controllers/Chat/ChatMessageParser.cpp index ce184ea..31be451 100644 --- a/Swift/Controllers/Chat/ChatMessageParser.cpp +++ b/Swift/Controllers/Chat/ChatMessageParser.cpp @@ -1,112 +1,264 @@ /* - * Copyright (c) 2013 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2013-2017 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #include <Swift/Controllers/Chat/ChatMessageParser.h> -#include <vector> +#include <algorithm> +#include <memory> #include <utility> +#include <vector> -#include <boost/smart_ptr/make_shared.hpp> #include <boost/algorithm/string.hpp> +#include <boost/regex.hpp> #include <Swiften/Base/Regex.h> -#include <Swiften/Base/foreach.h> +#include <Swiften/Base/String.h> #include <SwifTools/Linkify.h> - namespace Swift { - ChatMessageParser::ChatMessageParser(const std::map<std::string, std::string>& emoticons) : emoticons_(emoticons) { - - } - - typedef std::pair<std::string, std::string> StringPair; - - ChatWindow::ChatMessage ChatMessageParser::parseMessageBody(const std::string& body) { - ChatWindow::ChatMessage parsedMessage; - std::string remaining = body; - /* Parse one, URLs */ - while (!remaining.empty()) { - bool found = false; - std::pair<std::vector<std::string>, size_t> links = Linkify::splitLink(remaining); - remaining = ""; - for (size_t i = 0; i < links.first.size(); i++) { - const std::string& part = links.first[i]; - if (found) { - // Must be on the last part, then - remaining = part; - } - else { - if (i == links.second) { - found = true; - parsedMessage.append(boost::make_shared<ChatWindow::ChatURIMessagePart>(part)); - } - else { - parsedMessage.append(boost::make_shared<ChatWindow::ChatTextMessagePart>(part)); - } - } - } - } - - - - std::string regexString; - /* Parse two, emoticons */ - foreach (StringPair emoticon, emoticons_) { - /* Construct a regexp that finds an instance of any of the emoticons inside a group */ - regexString += regexString.empty() ? "(" : "|"; - regexString += Regex::escape(emoticon.first); - } - if (!regexString.empty()) { - regexString += ")"; - boost::regex emoticonRegex(regexString); - - ChatWindow::ChatMessage newMessage; - foreach (boost::shared_ptr<ChatWindow::ChatMessagePart> part, parsedMessage.getParts()) { - boost::shared_ptr<ChatWindow::ChatTextMessagePart> textPart; - if ((textPart = boost::dynamic_pointer_cast<ChatWindow::ChatTextMessagePart>(part))) { - try { - boost::match_results<std::string::const_iterator> match; - const std::string& text = textPart->text; - std::string::const_iterator start = text.begin(); - while (regex_search(start, text.end(), match, emoticonRegex)) { - std::string::const_iterator matchStart = match[0].first; - std::string::const_iterator matchEnd = match[0].second; - if (start != matchStart) { - /* If we're skipping over plain text since the previous emoticon, record it as plain text */ - newMessage.append(boost::make_shared<ChatWindow::ChatTextMessagePart>(std::string(start, matchStart))); - } - boost::shared_ptr<ChatWindow::ChatEmoticonMessagePart> emoticonPart = boost::make_shared<ChatWindow::ChatEmoticonMessagePart>(); - std::map<std::string, std::string>::const_iterator emoticonIterator = emoticons_.find(match.str()); - assert (emoticonIterator != emoticons_.end()); - const StringPair& emoticon = *emoticonIterator; - emoticonPart->imagePath = emoticon.second; - emoticonPart->alternativeText = emoticon.first; - newMessage.append(emoticonPart); - start = matchEnd; - } - if (start != text.end()) { - /* If there's plain text after the last emoticon, record it */ - newMessage.append(boost::make_shared<ChatWindow::ChatTextMessagePart>(std::string(start, text.end()))); - } - - } - catch (std::runtime_error) { - /* Basically too expensive to compute the regex results and it gave up, so pass through as text */ - newMessage.append(part); - } - } - else { - newMessage.append(part); - } - } - parsedMessage = newMessage; - - } - return parsedMessage; - } + ChatMessageParser::ChatMessageParser(const std::map<std::string, std::string>& emoticons, std::shared_ptr<HighlightConfiguration> highlightConfiguration, Mode mode) : emoticons_(emoticons), highlightConfiguration_(highlightConfiguration), mode_(mode) { + } + + typedef std::pair<std::string, std::string> StringPair; + + ChatWindow::ChatMessage ChatMessageParser::parseMessageBody(const std::string& body, const std::string& senderNickname, bool senderIsSelf) { + ChatWindow::ChatMessage parsedMessage; + + std::string remaining = body; + if (boost::starts_with(body, "/me ")) { + remaining = String::getSplittedAtFirst(body, ' ').second; + parsedMessage.setIsMeCommand(true); + } + + /* Parse one, URLs */ + while (!remaining.empty()) { + bool found = false; + std::pair<std::vector<std::string>, size_t> links = Linkify::splitLink(remaining); + remaining = ""; + for (size_t i = 0; i < links.first.size(); i++) { + const std::string& part = links.first[i]; + if (found) { + // Must be on the last part, then + remaining = part; + } + else { + if (i == links.second) { + found = true; + parsedMessage.append(std::make_shared<ChatWindow::ChatURIMessagePart>(part)); + } + else { + parsedMessage.append(std::make_shared<ChatWindow::ChatTextMessagePart>(part)); + } + } + } + } + + /* do emoticon substitution */ + parsedMessage = emoticonHighlight(parsedMessage); + + if (!senderIsSelf) { /* do not highlight our own messsages */ + // Highlight keywords and own mentions. + parsedMessage = splitHighlight(parsedMessage); + + // Highlight full message events like, specific sender, general + // incoming group message, or general incoming direct message. + parsedMessage = fullMessageHighlight(parsedMessage, senderNickname); + } + + return parsedMessage; + } + + ChatWindow::ChatMessage ChatMessageParser::emoticonHighlight(const ChatWindow::ChatMessage& message) { + ChatWindow::ChatMessage parsedMessage = message; + + std::string regexString; + /* Parse two, emoticons */ + for (StringPair emoticon : emoticons_) { + /* Construct a regexp that finds an instance of any of the emoticons inside a group + * at the start or end of the line, or beside whitespace. + */ + regexString += regexString.empty() ? "" : "|"; + std::string escaped = "(" + Regex::escape(emoticon.first) + ")"; + regexString += "^" + escaped + "|"; + regexString += escaped + "$|"; + regexString += "\\s" + escaped + "|"; + regexString += escaped + "\\s"; + + } + if (!regexString.empty()) { + regexString += ""; + boost::regex emoticonRegex(regexString); + + ChatWindow::ChatMessage newMessage; + for (const auto& part : parsedMessage.getParts()) { + std::shared_ptr<ChatWindow::ChatTextMessagePart> textPart; + if ((textPart = std::dynamic_pointer_cast<ChatWindow::ChatTextMessagePart>(part))) { + try { + boost::match_results<std::string::const_iterator> match; + const std::string& text = textPart->text; + std::string::const_iterator start = text.begin(); + while (regex_search(start, text.end(), match, emoticonRegex)) { + int matchIndex = 0; + for (matchIndex = 1; matchIndex < static_cast<int>(match.size()); matchIndex++) { + if (match[matchIndex].length() > 0) { + //This is the matching subgroup + break; + } + } + std::string::const_iterator matchStart = match[matchIndex].first; + std::string::const_iterator matchEnd = match[matchIndex].second; + if (start != matchStart) { + /* If we're skipping over plain text since the previous emoticon, record it as plain text */ + newMessage.append(std::make_shared<ChatWindow::ChatTextMessagePart>(std::string(start, matchStart))); + } + std::shared_ptr<ChatWindow::ChatEmoticonMessagePart> emoticonPart = std::make_shared<ChatWindow::ChatEmoticonMessagePart>(); + std::string matchString = match[matchIndex].str(); + std::map<std::string, std::string>::const_iterator emoticonIterator = emoticons_.find(matchString); + assert (emoticonIterator != emoticons_.end()); + const StringPair& emoticon = *emoticonIterator; + emoticonPart->imagePath = emoticon.second; + emoticonPart->alternativeText = emoticon.first; + newMessage.append(emoticonPart); + start = matchEnd; + } + if (start != text.end()) { + /* If there's plain text after the last emoticon, record it */ + newMessage.append(std::make_shared<ChatWindow::ChatTextMessagePart>(std::string(start, text.end()))); + } + + } + catch (const std::runtime_error&) { + /* Basically too expensive to compute the regex results and it gave up, so pass through as text */ + newMessage.append(part); + } + } + else { + newMessage.append(part); + } + } + parsedMessage.setParts(newMessage.getParts()); + } + + return parsedMessage; + } + + ChatWindow::ChatMessage ChatMessageParser::splitHighlight(const ChatWindow::ChatMessage& message) { + auto keywordToRegEx = [](const std::string& keyword, bool matchCaseSensitive) { + std::string escaped = Regex::escape(keyword); + boost::regex::flag_type flags = boost::regex::normal; + if (!matchCaseSensitive) { + flags |= boost::regex::icase; + } + return boost::regex("\\b" + escaped + "\\b", flags); + }; + + auto highlightKeywordInChatMessage = [&](const ChatWindow::ChatMessage& message, const std::string& keyword, bool matchCaseSensitive, const HighlightAction& action) { + ChatWindow::ChatMessage resultMessage; + + for (const auto& part : message.getParts()) { + std::shared_ptr<ChatWindow::ChatTextMessagePart> textPart; + if ((textPart = std::dynamic_pointer_cast<ChatWindow::ChatTextMessagePart>(part))) { + try { + boost::match_results<std::string::const_iterator> match; + const std::string& text = textPart->text; + std::string::const_iterator start = text.begin(); + while (regex_search(start, text.end(), match, keywordToRegEx(keyword, matchCaseSensitive))) { + std::string::const_iterator matchStart = match[0].first; + std::string::const_iterator matchEnd = match[0].second; + if (start != matchStart) { + /* If we're skipping over plain text since the previous emoticon, record it as plain text */ + resultMessage.append(std::make_shared<ChatWindow::ChatTextMessagePart>(std::string(start, matchStart))); + } + std::shared_ptr<ChatWindow::ChatHighlightingMessagePart> highlightPart = std::make_shared<ChatWindow::ChatHighlightingMessagePart>(); + highlightPart->text = match.str(); + highlightPart->action = action; + resultMessage.append(highlightPart); + start = matchEnd; + } + if (start != text.end()) { + /* If there's plain text after the last emoticon, record it */ + resultMessage.append(std::make_shared<ChatWindow::ChatTextMessagePart>(std::string(start, text.end()))); + } + } + catch (const std::runtime_error&) { + /* Basically too expensive to compute the regex results and it gave up, so pass through as text */ + resultMessage.append(part); + } + } else { + resultMessage.append(part); + } + } + return resultMessage; + }; + + ChatWindow::ChatMessage parsedMessage = message; + + // detect mentions of own nickname + HighlightAction ownMentionKeywordAction = highlightConfiguration_->ownMentionAction; + ownMentionKeywordAction.setSoundFilePath(boost::optional<std::string>()); + ownMentionKeywordAction.setSystemNotificationEnabled(false); + if (!getNick().empty() && !highlightConfiguration_->ownMentionAction.isEmpty()) { + auto nicknameHighlightedMessage = highlightKeywordInChatMessage(parsedMessage, nick_, false, ownMentionKeywordAction); + auto highlightedParts = nicknameHighlightedMessage.getParts(); + auto ownNicknamePart = std::find_if(highlightedParts.begin(), highlightedParts.end(), [&](std::shared_ptr<ChatWindow::ChatMessagePart>& part){ + auto highlightPart = std::dynamic_pointer_cast<ChatWindow::ChatHighlightingMessagePart>(part); + if (highlightPart && highlightPart->text == nick_) { + return true; + } + return false; + }); + if (ownNicknamePart != highlightedParts.end()) { + parsedMessage.setHighlightActionOwnMention(highlightConfiguration_->ownMentionAction); + } + parsedMessage.setParts(nicknameHighlightedMessage.getParts()); + } + + // detect keywords + for (const auto& keywordHighlight : highlightConfiguration_->keywordHighlights) { + if (keywordHighlight.keyword.empty() || keywordHighlight.action.isEmpty()) { + continue; + } + auto newMessage = highlightKeywordInChatMessage(parsedMessage, keywordHighlight.keyword, keywordHighlight.matchCaseSensitive, keywordHighlight.action); + parsedMessage.setParts(newMessage.getParts()); + } + + return parsedMessage; + } + + ChatWindow::ChatMessage ChatMessageParser::fullMessageHighlight(const ChatWindow::ChatMessage& parsedMessage, const std::string& sender) { + auto fullHighlightedMessage = parsedMessage; + + // contact highlighting + for (const auto& contactHighlight : highlightConfiguration_->contactHighlights) { + if (sender == contactHighlight.name) { + fullHighlightedMessage.setHighlightActionSender(contactHighlight.action); + break; + } + } + + // general incoming messages + HighlightAction groupAction; + HighlightAction chatAction; + + switch (mode_) { + case Mode::GroupChat: + groupAction.setSoundFilePath(highlightConfiguration_->playSoundOnIncomingGroupchatMessages ? boost::optional<std::string>("") : boost::optional<std::string>()); + groupAction.setSystemNotificationEnabled(highlightConfiguration_->showNotificationOnIncomingGroupchatMessages); + fullHighlightedMessage.setHighlightActionGroupMessage(groupAction); + break; + + case Mode::Chat: + chatAction.setSoundFilePath(highlightConfiguration_->playSoundOnIncomingDirectMessages ? boost::optional<std::string>("") : boost::optional<std::string>()); + chatAction.setSystemNotificationEnabled(highlightConfiguration_->showNotificationOnIncomingDirectMessages); + fullHighlightedMessage.setHighlightActonDirectMessage(chatAction); + break; + } + + return fullHighlightedMessage; + } } diff --git a/Swift/Controllers/Chat/ChatMessageParser.h b/Swift/Controllers/Chat/ChatMessageParser.h index c9b9456..de5eac9 100644 --- a/Swift/Controllers/Chat/ChatMessageParser.h +++ b/Swift/Controllers/Chat/ChatMessageParser.h @@ -1,23 +1,44 @@ /* - * Copyright (c) 2013 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2013-2017 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once +#include <memory> #include <string> +#include <Swift/Controllers/Highlighting/HighlightConfiguration.h> #include <Swift/Controllers/UIInterfaces/ChatWindow.h> namespace Swift { - class ChatMessageParser { - public: - ChatMessageParser(const std::map<std::string, std::string>& emoticons); - ChatWindow::ChatMessage parseMessageBody(const std::string& body); - private: - std::map<std::string, std::string> emoticons_; + /** + * @brief The ChatMessageParser class takes an emoticon map, a \ref HighlightConfiguration, and a boolean that indicates if the message context is in a MUC or not. + * The class handles parsing a message string and identifies emoticons, URLs, and various highlights. + */ + class ChatMessageParser { + public: + enum class Mode { Chat, GroupChat }; - }; + public: + ChatMessageParser(const std::map<std::string, std::string>& emoticons, std::shared_ptr<HighlightConfiguration> highlightConfiguration, Mode mode = Mode::Chat); + + void setNick(const std::string& nick) { nick_ = nick; } + std::string getNick() const { return nick_; } + + ChatWindow::ChatMessage parseMessageBody(const std::string& body, const std::string& sender = "", bool senderIsSelf = false); + + private: + ChatWindow::ChatMessage emoticonHighlight(const ChatWindow::ChatMessage& parsedMessage); + ChatWindow::ChatMessage splitHighlight(const ChatWindow::ChatMessage& parsedMessage); + ChatWindow::ChatMessage fullMessageHighlight(const ChatWindow::ChatMessage& parsedMessage, const std::string& sender); + + private: + std::map<std::string, std::string> emoticons_; + std::shared_ptr<HighlightConfiguration> highlightConfiguration_; + Mode mode_; + std::string nick_; + }; } diff --git a/Swift/Controllers/Chat/ChatsManager.cpp b/Swift/Controllers/Chat/ChatsManager.cpp index 5d69019..193af7f 100644 --- a/Swift/Controllers/Chat/ChatsManager.cpp +++ b/Swift/Controllers/Chat/ChatsManager.cpp @@ -1,945 +1,1200 @@ /* - * Copyright (c) 2010-2013 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2019 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #include <Swift/Controllers/Chat/ChatsManager.h> -#include <boost/bind.hpp> +#include <memory> + #include <boost/algorithm/string.hpp> -#include <boost/smart_ptr/make_shared.hpp> -#include <boost/archive/text_oarchive.hpp> -#include <boost/archive/text_iarchive.hpp> -#include <boost/serialization/vector.hpp> -#include <boost/serialization/map.hpp> -#include <boost/serialization/string.hpp> -#include <boost/serialization/split_free.hpp> - -#include <Swiften/Base/foreach.h> -#include <Swiften/Presence/PresenceSender.h> +#include <boost/bind.hpp> + +#include <Swiften/Avatars/AvatarManager.h> +#include <Swiften/Base/Log.h> +#include <Swiften/Client/ClientBlockListManager.h> #include <Swiften/Client/NickResolver.h> -#include <Swiften/MUC/MUCManager.h> +#include <Swiften/Client/StanzaChannel.h> +#include <Swiften/Disco/DiscoServiceWalker.h> +#include <Swiften/Disco/FeatureOracle.h> +#include <Swiften/Elements/CarbonsReceived.h> +#include <Swiften/Elements/CarbonsSent.h> #include <Swiften/Elements/ChatState.h> -#include <Swiften/Elements/MUCUserPayload.h> #include <Swiften/Elements/DeliveryReceipt.h> #include <Swiften/Elements/DeliveryReceiptRequest.h> -#include <Swiften/MUC/MUCBookmarkManager.h> -#include <Swiften/Avatars/AvatarManager.h> +#include <Swiften/Elements/Forwarded.h> #include <Swiften/Elements/MUCInvitationPayload.h> +#include <Swiften/Elements/MUCUserPayload.h> +#include <Swiften/MUC/MUCBookmarkManager.h> +#include <Swiften/MUC/MUCManager.h> +#include <Swiften/Presence/PresenceSender.h> #include <Swiften/Roster/XMPPRoster.h> -#include <Swiften/Client/ClientBlockListManager.h> -#include <Swiften/Client/StanzaChannel.h> +#include <Swiften/StringCodecs/Base64.h> +#include <Swiften/VCards/VCardManager.h> +#include <Swift/Controllers/Chat/AutoAcceptMUCInviteDecider.h> +#include <Swift/Controllers/Chat/Chattables.h> #include <Swift/Controllers/Chat/ChatController.h> #include <Swift/Controllers/Chat/ChatControllerBase.h> -#include <Swift/Controllers/Chat/MUCSearchController.h> -#include <Swift/Controllers/Chat/AutoAcceptMUCInviteDecider.h> -#include <Swift/Controllers/XMPPEvents/EventController.h> +#include <Swift/Controllers/Chat/ChatListWindowChatBoostSerialize.h> +#include <Swift/Controllers/Chat/ChatMessageParser.h> #include <Swift/Controllers/Chat/MUCController.h> -#include <Swift/Controllers/UIEvents/RequestChatUIEvent.h> -#include <Swift/Controllers/UIEvents/CreateImpromptuMUCUIEvent.h> -#include <Swift/Controllers/UIEvents/JoinMUCUIEvent.h> -#include <Swift/Controllers/UIEvents/RequestJoinMUCUIEvent.h> +#include <Swift/Controllers/Chat/MUCSearchController.h> +#include <Swift/Controllers/Chat/UserSearchController.h> +#include <Swift/Controllers/FileTransfer/FileTransferController.h> +#include <Swift/Controllers/FileTransfer/FileTransferOverview.h> +#include <Swift/Controllers/ProfileSettingsProvider.h> +#include <Swift/Controllers/SettingConstants.h> +#include <Swift/Controllers/Settings/SettingsProvider.h> #include <Swift/Controllers/UIEvents/AddMUCBookmarkUIEvent.h> -#include <Swift/Controllers/UIEvents/RemoveMUCBookmarkUIEvent.h> +#include <Swift/Controllers/UIEvents/CreateImpromptuMUCUIEvent.h> #include <Swift/Controllers/UIEvents/EditMUCBookmarkUIEvent.h> #include <Swift/Controllers/UIEvents/InviteToMUCUIEvent.h> +#include <Swift/Controllers/UIEvents/JoinMUCUIEvent.h> +#include <Swift/Controllers/UIEvents/RemoveMUCBookmarkUIEvent.h> +#include <Swift/Controllers/UIEvents/RequestChatUIEvent.h> +#include <Swift/Controllers/UIEvents/RequestJoinMUCUIEvent.h> +#include <Swift/Controllers/UIEvents/SendFileUIEvent.h> #include <Swift/Controllers/UIInterfaces/ChatListWindowFactory.h> #include <Swift/Controllers/UIInterfaces/JoinMUCWindow.h> #include <Swift/Controllers/UIInterfaces/JoinMUCWindowFactory.h> -#include <Swift/Controllers/FileTransfer/FileTransferController.h> -#include <Swift/Controllers/FileTransfer/FileTransferOverview.h> -#include <Swift/Controllers/ProfileSettingsProvider.h> -#include <Swift/Controllers/Settings/SettingsProvider.h> -#include <Swift/Controllers/SettingConstants.h> #include <Swift/Controllers/WhiteboardManager.h> -#include <Swift/Controllers/Chat/ChatMessageParser.h> -#include <Swift/Controllers/Chat/UserSearchController.h> -#include <Swiften/Disco/DiscoServiceWalker.h> -#include <Swiften/Client/ClientBlockListManager.h> -#include <Swiften/StringCodecs/Base64.h> -#include <Swiften/Base/Log.h> - -namespace boost { -namespace serialization { - template<class Archive> void save(Archive& ar, const Swift::JID& jid, const unsigned int /*version*/) { - std::string jidStr = jid.toString(); - ar << jidStr; - } - - template<class Archive> void load(Archive& ar, Swift::JID& jid, const unsigned int /*version*/) { - std::string stringJID; - ar >> stringJID; - jid = Swift::JID(stringJID); - } - - template<class Archive> inline void serialize(Archive& ar, Swift::JID& t, const unsigned int file_version){ - split_free(ar, t, file_version); - } - - template<class Archive> void serialize(Archive& ar, Swift::ChatListWindow::Chat& chat, const unsigned int /*version*/) { - ar & chat.jid; - ar & chat.chatName; - ar & chat.activity; - ar & chat.isMUC; - ar & chat.nick; - ar & chat.impromptuJIDs; - } -} -} +#include <Swift/Controllers/XMPPEvents/EventController.h> namespace Swift { typedef std::pair<JID, ChatController*> JIDChatControllerPair; typedef std::pair<JID, MUCController*> JIDMUCControllerPair; +#ifndef NOT_YET #define RECENT_CHATS "recent_chats" +#endif ChatsManager::ChatsManager( - JID jid, StanzaChannel* stanzaChannel, - IQRouter* iqRouter, - EventController* eventController, - ChatWindowFactory* chatWindowFactory, - JoinMUCWindowFactory* joinMUCWindowFactory, - NickResolver* nickResolver, - PresenceOracle* presenceOracle, - PresenceSender* presenceSender, - UIEventStream* uiEventStream, - ChatListWindowFactory* chatListWindowFactory, - bool useDelayForLatency, - TimerFactory* timerFactory, - MUCRegistry* mucRegistry, - EntityCapsProvider* entityCapsProvider, - MUCManager* mucManager, - MUCSearchWindowFactory* mucSearchWindowFactory, - ProfileSettingsProvider* profileSettings, - FileTransferOverview* ftOverview, - XMPPRoster* roster, - bool eagleMode, - SettingsProvider* settings, - HistoryController* historyController, - WhiteboardManager* whiteboardManager, - HighlightManager* highlightManager, - ClientBlockListManager* clientBlockListManager, - const std::map<std::string, std::string>& emoticons, - UserSearchController* inviteUserSearchController) : - jid_(jid), - joinMUCWindowFactory_(joinMUCWindowFactory), - useDelayForLatency_(useDelayForLatency), - mucRegistry_(mucRegistry), - entityCapsProvider_(entityCapsProvider), - mucManager(mucManager), - ftOverview_(ftOverview), - roster_(roster), - eagleMode_(eagleMode), - settings_(settings), - historyController_(historyController), - whiteboardManager_(whiteboardManager), - highlightManager_(highlightManager), - clientBlockListManager_(clientBlockListManager), - inviteUserSearchController_(inviteUserSearchController) { - timerFactory_ = timerFactory; - eventController_ = eventController; - stanzaChannel_ = stanzaChannel; - iqRouter_ = iqRouter; - chatWindowFactory_ = chatWindowFactory; - nickResolver_ = nickResolver; - presenceOracle_ = presenceOracle; - avatarManager_ = NULL; - serverDiscoInfo_ = boost::make_shared<DiscoInfo>(); - presenceSender_ = presenceSender; - uiEventStream_ = uiEventStream; - mucBookmarkManager_ = NULL; - profileSettings_ = profileSettings; - chatMessageParser_ = new ChatMessageParser(emoticons); - presenceOracle_->onPresenceChange.connect(boost::bind(&ChatsManager::handlePresenceChange, this, _1)); - uiEventConnection_ = uiEventStream_->onUIEvent.connect(boost::bind(&ChatsManager::handleUIEvent, this, _1)); - - chatListWindow_ = chatListWindowFactory->createChatListWindow(uiEventStream_); - chatListWindow_->onMUCBookmarkActivated.connect(boost::bind(&ChatsManager::handleMUCBookmarkActivated, this, _1)); - chatListWindow_->onRecentActivated.connect(boost::bind(&ChatsManager::handleRecentActivated, this, _1)); - chatListWindow_->onClearRecentsRequested.connect(boost::bind(&ChatsManager::handleClearRecentsRequested, this)); - - joinMUCWindow_ = NULL; - mucSearchController_ = new MUCSearchController(jid_, mucSearchWindowFactory, iqRouter, profileSettings_); - mucSearchController_->onMUCSelected.connect(boost::bind(&ChatsManager::handleMUCSelectedAfterSearch, this, _1)); - ftOverview_->onNewFileTransferController.connect(boost::bind(&ChatsManager::handleNewFileTransferController, this, _1)); - whiteboardManager_->onSessionRequest.connect(boost::bind(&ChatsManager::handleWhiteboardSessionRequest, this, _1, _2)); - whiteboardManager_->onRequestAccepted.connect(boost::bind(&ChatsManager::handleWhiteboardStateChange, this, _1, ChatWindow::WhiteboardAccepted)); - whiteboardManager_->onSessionTerminate.connect(boost::bind(&ChatsManager::handleWhiteboardStateChange, this, _1, ChatWindow::WhiteboardTerminated)); - whiteboardManager_->onRequestRejected.connect(boost::bind(&ChatsManager::handleWhiteboardStateChange, this, _1, ChatWindow::WhiteboardRejected)); - roster_->onJIDAdded.connect(boost::bind(&ChatsManager::handleJIDAddedToRoster, this, _1)); - roster_->onJIDRemoved.connect(boost::bind(&ChatsManager::handleJIDRemovedFromRoster, this, _1)); - roster_->onJIDUpdated.connect(boost::bind(&ChatsManager::handleJIDUpdatedInRoster, this, _1)); - roster_->onRosterCleared.connect(boost::bind(&ChatsManager::handleRosterCleared, this)); - - settings_->onSettingChanged.connect(boost::bind(&ChatsManager::handleSettingChanged, this, _1)); - - userWantsReceipts_ = settings_->getSetting(SettingConstants::REQUEST_DELIVERYRECEIPTS); - - setupBookmarks(); - loadRecents(); - - autoAcceptMUCInviteDecider_ = new AutoAcceptMUCInviteDecider(jid.getDomain(), roster_, settings_); + JID jid, StanzaChannel* stanzaChannel, + IQRouter* iqRouter, + EventController* eventController, + ChatWindowFactory* chatWindowFactory, + JoinMUCWindowFactory* joinMUCWindowFactory, + NickResolver* nickResolver, + PresenceOracle* presenceOracle, + PresenceSender* presenceSender, + UIEventStream* uiEventStream, +#ifndef NOT_YET + ChatListWindowFactory* chatListWindowFactory, +#endif + bool useDelayForLatency, + TimerFactory* timerFactory, + MUCRegistry* mucRegistry, + EntityCapsProvider* entityCapsProvider, + MUCManager* mucManager, + MUCSearchWindowFactory* mucSearchWindowFactory, + ProfileSettingsProvider* profileSettings, + FileTransferOverview* ftOverview, + XMPPRoster* roster, + bool eagleMode, + SettingsProvider* settings, + HistoryController* historyController, + WhiteboardManager* whiteboardManager, + HighlightManager* highlightManager, + ClientBlockListManager* clientBlockListManager, + const std::map<std::string, std::string>& emoticons, + VCardManager* vcardManager, + Chattables& chattables) : + jid_(jid), + joinMUCWindowFactory_(joinMUCWindowFactory), + useDelayForLatency_(useDelayForLatency), + mucRegistry_(mucRegistry), + entityCapsProvider_(entityCapsProvider), + mucManager(mucManager), + ftOverview_(ftOverview), + roster_(roster), + eagleMode_(eagleMode), + settings_(settings), + historyController_(historyController), + whiteboardManager_(whiteboardManager), + highlightManager_(highlightManager), + emoticons_(emoticons), + clientBlockListManager_(clientBlockListManager), + vcardManager_(vcardManager), + chattables_(chattables) { + timerFactory_ = timerFactory; + eventController_ = eventController; + stanzaChannel_ = stanzaChannel; + iqRouter_ = iqRouter; + chatWindowFactory_ = chatWindowFactory; + nickResolver_ = nickResolver; + presenceOracle_ = presenceOracle; + avatarManager_ = nullptr; + serverDiscoInfo_ = std::make_shared<DiscoInfo>(); + presenceSender_ = presenceSender; + uiEventStream_ = uiEventStream; + mucBookmarkManager_ = nullptr; + profileSettings_ = profileSettings; + presenceOracle_->onPresenceChange.connect(boost::bind(&ChatsManager::handlePresenceChange, this, _1)); + uiEventConnection_ = uiEventStream_->onUIEvent.connect(boost::bind(&ChatsManager::handleUIEvent, this, _1)); +#ifndef NOT_YET + chatListWindow_ = chatListWindowFactory->createChatListWindow(uiEventStream_); + chatListWindow_->onMUCBookmarkActivated.connect(boost::bind(&ChatsManager::handleMUCBookmarkActivated, this, _1)); + chatListWindow_->onRecentActivated.connect(boost::bind(&ChatsManager::handleRecentActivated, this, _1)); + chatListWindow_->onClearRecentsRequested.connect(boost::bind(&ChatsManager::handleClearRecentsRequested, this)); +#endif + joinMUCWindow_ = nullptr; + mucSearchController_ = new MUCSearchController(jid_, mucSearchWindowFactory, iqRouter, profileSettings_); + mucSearchController_->onMUCSelected.connect(boost::bind(&ChatsManager::handleMUCSelectedAfterSearch, this, _1)); + ftOverview_->onNewFileTransferController.connect(boost::bind(&ChatsManager::handleNewFileTransferController, this, _1)); + whiteboardManager_->onSessionRequest.connect(boost::bind(&ChatsManager::handleWhiteboardSessionRequest, this, _1, _2)); + whiteboardManager_->onRequestAccepted.connect(boost::bind(&ChatsManager::handleWhiteboardStateChange, this, _1, ChatWindow::WhiteboardAccepted)); + whiteboardManager_->onSessionTerminate.connect(boost::bind(&ChatsManager::handleWhiteboardStateChange, this, _1, ChatWindow::WhiteboardTerminated)); + whiteboardManager_->onRequestRejected.connect(boost::bind(&ChatsManager::handleWhiteboardStateChange, this, _1, ChatWindow::WhiteboardRejected)); + roster_->onJIDAdded.connect(boost::bind(&ChatsManager::handleJIDAddedToRoster, this, _1)); + roster_->onJIDRemoved.connect(boost::bind(&ChatsManager::handleJIDRemovedFromRoster, this, _1)); + roster_->onJIDUpdated.connect(boost::bind(&ChatsManager::handleJIDUpdatedInRoster, this, _1)); + roster_->onRosterCleared.connect(boost::bind(&ChatsManager::handleRosterCleared, this)); + + chattables_.onActivated.connect(boost::bind(&ChatsManager::handleChattableActivated, this, _1)); + + settings_->onSettingChanged.connect(boost::bind(&ChatsManager::handleSettingChanged, this, _1)); + + userWantsReceipts_ = settings_->getSetting(SettingConstants::REQUEST_DELIVERYRECEIPTS); + + setupBookmarks(); +#ifndef NOT_YET + loadRecents(); +#endif + autoAcceptMUCInviteDecider_ = new AutoAcceptMUCInviteDecider(jid.getDomain(), roster_, settings_); } ChatsManager::~ChatsManager() { - settings_->onSettingChanged.disconnect(boost::bind(&ChatsManager::handleSettingChanged, this, _1)); - roster_->onJIDAdded.disconnect(boost::bind(&ChatsManager::handleJIDAddedToRoster, this, _1)); - roster_->onJIDRemoved.disconnect(boost::bind(&ChatsManager::handleJIDRemovedFromRoster, this, _1)); - roster_->onJIDUpdated.disconnect(boost::bind(&ChatsManager::handleJIDUpdatedInRoster, this, _1)); - roster_->onRosterCleared.disconnect(boost::bind(&ChatsManager::handleRosterCleared, this)); - delete joinMUCWindow_; - foreach (JIDChatControllerPair controllerPair, chatControllers_) { - delete controllerPair.second; - } - foreach (JIDMUCControllerPair controllerPair, mucControllers_) { - delete controllerPair.second; - } - delete mucBookmarkManager_; - delete mucSearchController_; - delete chatMessageParser_; - delete autoAcceptMUCInviteDecider_; + settings_->onSettingChanged.disconnect(boost::bind(&ChatsManager::handleSettingChanged, this, _1)); + chattables_.onActivated.disconnect(boost::bind(&ChatsManager::handleChattableActivated, this, _1)); + roster_->onJIDAdded.disconnect(boost::bind(&ChatsManager::handleJIDAddedToRoster, this, _1)); + roster_->onJIDRemoved.disconnect(boost::bind(&ChatsManager::handleJIDRemovedFromRoster, this, _1)); + roster_->onJIDUpdated.disconnect(boost::bind(&ChatsManager::handleJIDUpdatedInRoster, this, _1)); + roster_->onRosterCleared.disconnect(boost::bind(&ChatsManager::handleRosterCleared, this)); + ftOverview_->onNewFileTransferController.disconnect(boost::bind(&ChatsManager::handleNewFileTransferController, this, _1)); + delete joinMUCWindow_; + SWIFT_LOG(debug) << "Destroying ChatsManager, containing " << chatControllers_.size() << " chats and " << mucControllers_.size() << " MUCs"; + for (JIDChatControllerPair controllerPair : chatControllers_) { + delete controllerPair.second; + } + for (JIDMUCControllerPair controllerPair : mucControllers_) { + delete controllerPair.second; + } + delete mucBookmarkManager_; + delete mucSearchController_; + delete autoAcceptMUCInviteDecider_; } +#ifndef NOT_YET void ChatsManager::saveRecents() { - std::stringstream serializeStream; - boost::archive::text_oarchive oa(serializeStream); - std::vector<ChatListWindow::Chat> recentsLimited = std::vector<ChatListWindow::Chat>(recentChats_.begin(), recentChats_.end()); - if (recentsLimited.size() > 25) { - recentsLimited.erase(recentsLimited.begin() + 25, recentsLimited.end()); - } - if (eagleMode_) { - foreach(ChatListWindow::Chat& chat, recentsLimited) { - chat.activity = ""; - } - } - - oa << recentsLimited; - std::string serializedStr = Base64::encode(createByteArray(serializeStream.str())); - profileSettings_->storeString(RECENT_CHATS, serializedStr); + std::stringstream serializeStream; + boost::archive::text_oarchive oa(serializeStream); + std::vector<ChatListWindow::Chat> recentsLimited = std::vector<ChatListWindow::Chat>(recentChats_.begin(), recentChats_.end()); + if (recentsLimited.size() > 25) { + recentsLimited.erase(recentsLimited.begin() + 25, recentsLimited.end()); + } + if (eagleMode_) { + for (ChatListWindow::Chat& chat : recentsLimited) { + chat.activity = ""; + } + } + + class RemoveRecent { + public: + static bool ifPrivateMessage(const ChatListWindow::Chat& chat) { + return chat.isPrivateMessage; + } + }; + + recentsLimited.erase(std::remove_if(recentsLimited.begin(), recentsLimited.end(), RemoveRecent::ifPrivateMessage), recentsLimited.end()); + + oa & recentsLimited; + std::string serializedStr = Base64::encode(createByteArray(serializeStream.str())); + profileSettings_->storeString(RECENT_CHATS, serializedStr); } void ChatsManager::handleClearRecentsRequested() { - recentChats_.clear(); - saveRecents(); - handleUnreadCountChanged(NULL); + recentChats_.clear(); + saveRecents(); + handleUnreadCountChanged(nullptr); } +#endif void ChatsManager::handleJIDAddedToRoster(const JID &jid) { - updatePresenceReceivingStateOnChatController(jid); + updatePresenceReceivingStateOnChatController(jid); } void ChatsManager::handleJIDRemovedFromRoster(const JID &jid) { - updatePresenceReceivingStateOnChatController(jid); + updatePresenceReceivingStateOnChatController(jid); } void ChatsManager::handleJIDUpdatedInRoster(const JID &jid) { - updatePresenceReceivingStateOnChatController(jid); + updatePresenceReceivingStateOnChatController(jid); } 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_) { - pair.second->setContactIsReceivingPresence(false); - } + /* 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. */ + for (JIDChatControllerPair pair : chatControllers_) { + pair.second->setContactIsReceivingPresence(false); + } } void ChatsManager::updatePresenceReceivingStateOnChatController(const JID &jid) { - ChatController* controller = getChatControllerIfExists(jid); - if (controller) { - if (!mucRegistry_->isMUC(jid.toBare())) { - RosterItemPayload::Subscription subscription = roster_->getSubscriptionStateForJID(jid); - controller->setContactIsReceivingPresence(subscription == RosterItemPayload::From || subscription == RosterItemPayload::Both); - } else { - controller->setContactIsReceivingPresence(true); - } - } + ChatController* controller = getChatControllerIfExists(jid); + if (controller) { + if (!mucRegistry_->isMUC(jid.toBare())) { + RosterItemPayload::Subscription subscription = roster_->getSubscriptionStateForJID(jid); + controller->setContactIsReceivingPresence(subscription == RosterItemPayload::From || subscription == RosterItemPayload::Both); + } else { + controller->setContactIsReceivingPresence(true); + } + } } +#ifndef NOT_YET ChatListWindow::Chat ChatsManager::updateChatStatusAndAvatarHelper(const ChatListWindow::Chat& chat) const { - ChatListWindow::Chat fixedChat = chat; - if (fixedChat.isMUC) { - if (mucControllers_.find(fixedChat.jid.toBare()) != mucControllers_.end()) { - fixedChat.statusType = StatusShow::Online; - } - } else { - if (avatarManager_) { - fixedChat.avatarPath = avatarManager_->getAvatarPath(fixedChat.jid); - } - Presence::ref presence = presenceOracle_->getHighestPriorityPresence(fixedChat.jid.toBare()); - fixedChat.statusType = presence ? presence->getShow() : StatusShow::None; - } - return fixedChat; + ChatListWindow::Chat fixedChat = chat; + if (fixedChat.isMUC) { + if (mucControllers_.find(fixedChat.jid.toBare()) != mucControllers_.end()) { + fixedChat.statusType = StatusShow::Online; + } + } else { + if (avatarManager_) { + fixedChat.avatarPath = avatarManager_->getAvatarPath(fixedChat.jid); + } + Presence::ref presence = presenceOracle_->getAccountPresence(fixedChat.jid.toBare()); + fixedChat.statusType = presence ? presence->getShow() : StatusShow::None; + } + return fixedChat; } void ChatsManager::loadRecents() { - std::string recentsString(profileSettings_->getStringSetting(RECENT_CHATS)); - if (recentsString.find("\t") != std::string::npos) { - // old format - std::vector<std::string> recents; - boost::split(recents, recentsString, boost::is_any_of("\n")); - int i = 0; - foreach (std::string recentString, recents) { - if (i++ > 30) { - break; - } - std::vector<std::string> recent; - boost::split(recent, recentString, boost::is_any_of("\t")); - if (recent.size() < 4) { - continue; - } - JID jid(recent[0]); - if (!jid.isValid()) { - continue; - } - std::string activity(recent[1]); - bool isMUC = recent[2] == "true"; - std::string nick(recent[3]); - StatusShow::Type type = StatusShow::None; - boost::filesystem::path path; - - ChatListWindow::Chat chat(jid, nickResolver_->jidToNick(jid), activity, 0, type, path, isMUC, nick); - chat = updateChatStatusAndAvatarHelper(chat); - prependRecent(chat); - } - } else if (!recentsString.empty()){ - // boost searilaize based format - ByteArray debase64 = Base64::decode(recentsString); - std::vector<ChatListWindow::Chat> recentChats; - std::stringstream deserializeStream(std::string((const char*)debase64.data(), debase64.size())); - try { - boost::archive::text_iarchive ia(deserializeStream); - ia >> recentChats; - } catch (const boost::archive::archive_exception& e) { - SWIFT_LOG(debug) << "Failed to load recents: " << e.what() << std::endl; - return; - } - - foreach(ChatListWindow::Chat chat, recentChats) { - chat.statusType = StatusShow::None; - chat = updateChatStatusAndAvatarHelper(chat); - prependRecent(chat); - } - } - handleUnreadCountChanged(NULL); + std::string recentsString(profileSettings_->getStringSetting(RECENT_CHATS)); + if (recentsString.find("\t") != std::string::npos) { + // old format + std::vector<std::string> recents; + boost::split(recents, recentsString, boost::is_any_of("\n")); + int i = 0; + for (std::string recentString : recents) { + if (i++ > 30) { + break; + } + std::vector<std::string> recent; + boost::split(recent, recentString, boost::is_any_of("\t")); + if (recent.size() < 4) { + continue; + } + JID jid(recent[0]); + if (!jid.isValid()) { + continue; + } + std::string activity(recent[1]); + bool isMUC = recent[2] == "true"; + std::string nick(recent[3]); + StatusShow::Type type = StatusShow::None; + boost::filesystem::path path; + + ChatListWindow::Chat chat(jid, nickResolver_->jidToNick(jid), activity, 0, type, path, isMUC, false, nick); + chat = updateChatStatusAndAvatarHelper(chat); + prependRecent(chat); + } + } else if (!recentsString.empty()){ + // boost searilaize based format + ByteArray debase64 = Base64::decode(recentsString); + std::vector<ChatListWindow::Chat> recentChats; + std::stringstream deserializeStream(std::string(reinterpret_cast<const char*>(vecptr(debase64)), debase64.size())); + try { + boost::archive::text_iarchive ia(deserializeStream); + ia >> recentChats; + } catch (const boost::archive::archive_exception& e) { + SWIFT_LOG(debug) << "Failed to load recents: " << e.what(); + return; + } + recentChats.erase(std::remove(recentChats.begin(), recentChats.end(), ChatListWindow::Chat()), recentChats.end()); + for (auto chat : recentChats) { + chat.statusType = StatusShow::None; + chat = updateChatStatusAndAvatarHelper(chat); + prependRecent(chat); + } + } + handleUnreadCountChanged(nullptr); } +#endif void ChatsManager::setupBookmarks() { - if (!mucBookmarkManager_) { - mucBookmarkManager_ = new MUCBookmarkManager(iqRouter_); - mucBookmarkManager_->onBookmarksReady.connect(boost::bind(&ChatsManager::handleBookmarksReady, this)); - mucBookmarkManager_->onBookmarkAdded.connect(boost::bind(&ChatsManager::handleMUCBookmarkAdded, this, _1)); - mucBookmarkManager_->onBookmarkRemoved.connect(boost::bind(&ChatsManager::handleMUCBookmarkRemoved, this, _1)); - - if (chatListWindow_) { - chatListWindow_->setBookmarksEnabled(false); - chatListWindow_->clearBookmarks(); - } - } + if (!mucBookmarkManager_) { + mucBookmarkManager_ = new MUCBookmarkManager(iqRouter_); + mucBookmarkManager_->onBookmarksReady.connect(boost::bind(&ChatsManager::handleBookmarksReady, this)); + mucBookmarkManager_->onBookmarkAdded.connect(boost::bind(&ChatsManager::handleMUCBookmarkAdded, this, _1)); + mucBookmarkManager_->onBookmarkRemoved.connect(boost::bind(&ChatsManager::handleMUCBookmarkRemoved, this, _1)); + +#ifndef NOT_YET + if (chatListWindow_) { + chatListWindow_->setBookmarksEnabled(false); + chatListWindow_->clearBookmarks(); + } +#endif + } } void ChatsManager::handleBookmarksReady() { - if (chatListWindow_) { - chatListWindow_->setBookmarksEnabled(true); - } +#ifndef NOT_YET + if (chatListWindow_) { + chatListWindow_->setBookmarksEnabled(true); + } +#endif } void ChatsManager::handleMUCBookmarkAdded(const MUCBookmark& bookmark) { - std::map<JID, MUCController*>::iterator it = mucControllers_.find(bookmark.getRoom()); - if (it == mucControllers_.end() && bookmark.getAutojoin()) { - handleJoinMUCRequest(bookmark.getRoom(), bookmark.getPassword(), bookmark.getNick(), false, false, false ); - } - chatListWindow_->addMUCBookmark(bookmark); + if (bookmark.getRoom().isBare() && !bookmark.getRoom().getNode().empty()) { + std::map<JID, MUCController*>::iterator it = mucControllers_.find(bookmark.getRoom()); + if (it == mucControllers_.end() && bookmark.getAutojoin()) { + handleJoinMUCRequest(bookmark.getRoom(), bookmark.getPassword(), bookmark.getNick(), false, false); + } +#ifndef NOT_YET + //Only one entry of the bookmark should exist + chatListWindow_->removeMUCBookmark(bookmark); + chatListWindow_->addMUCBookmark(bookmark); +#endif + chattables_.addJID(bookmark.getRoom(), Chattables::State::Type::Room); + } } void ChatsManager::handleMUCBookmarkRemoved(const MUCBookmark& bookmark) { - chatListWindow_->removeMUCBookmark(bookmark); -} - -ChatListWindow::Chat ChatsManager::createChatListChatItem(const JID& jid, const std::string& activity) { - int unreadCount = 0; - if (mucRegistry_->isMUC(jid)) { - MUCController* controller = mucControllers_[jid.toBare()]; - StatusShow::Type type = StatusShow::None; - std::string nick = ""; - if (controller) { - unreadCount = controller->getUnreadCount(); - if (controller->isJoined()) { - type = StatusShow::Online; - } - nick = controller->getNick(); - - if (controller->isImpromptu()) { - ChatListWindow::Chat chat = ChatListWindow::Chat(jid, jid.toString(), activity, unreadCount, type, boost::filesystem::path(), true, nick); - typedef std::pair<std::string, JID> StringJIDPair; - std::map<std::string, JID> participants = controller->getParticipantJIDs(); - chat.impromptuJIDs = participants; - return chat; - } - } - return ChatListWindow::Chat(jid, jid.toString(), activity, unreadCount, type, boost::filesystem::path(), true, nick); - } else { - ChatController* controller = getChatControllerIfExists(jid, false); - if (controller) { - unreadCount = controller->getUnreadCount(); - } - JID bareishJID = mucRegistry_->isMUC(jid.toBare()) ? jid : jid.toBare(); - Presence::ref presence = presenceOracle_->getHighestPriorityPresence(bareishJID); - StatusShow::Type type = presence ? presence->getShow() : StatusShow::None; - boost::filesystem::path avatarPath = avatarManager_ ? avatarManager_->getAvatarPath(bareishJID) : boost::filesystem::path(); - return ChatListWindow::Chat(bareishJID, nickResolver_->jidToNick(bareishJID), activity, unreadCount, type, avatarPath, false); - } +#ifndef NOT_YET + chatListWindow_->removeMUCBookmark(bookmark); +#endif } +#ifndef NOT_YET +ChatListWindow::Chat ChatsManager::createChatListChatItem(const JID& jid, const std::string& activity, bool privateMessage) { + size_t unreadCount = 0; + if (mucRegistry_->isMUC(jid)) { + MUCController* controller = mucControllers_[jid.toBare()]; + StatusShow::Type type = StatusShow::None; + std::string nick = ""; + std::string password = ""; + if (controller) { + unreadCount = controller->getUnreadCount(); + if (controller->isJoined()) { + type = StatusShow::Online; + } + nick = controller->getNick(); + + if (controller->getPassword()) { + password = *controller->getPassword(); + } + + if (controller->isImpromptu()) { + ChatListWindow::Chat chat = ChatListWindow::Chat(jid, jid.toString(), activity, unreadCount, type, boost::filesystem::path(), true, privateMessage, nick, password); + std::map<std::string, JID> participants = controller->getParticipantJIDs(); + chat.impromptuJIDs = participants; + + std::map<JID, std::string> participantsNames; + for (auto& i : invitees_[jid]) { + participantsNames.emplace(i, roster_->getNameForJID(i)); + } + chat.inviteesNames = participantsNames; + return chat; + } + } + return ChatListWindow::Chat(jid, jid.toString(), activity, unreadCount, type, boost::filesystem::path(), true, privateMessage, nick, password); + } else { + ChatController* controller = getChatControllerIfExists(jid, false); + if (controller) { + unreadCount = controller->getUnreadCount(); + } + JID bareishJID = mucRegistry_->isMUC(jid.toBare()) ? jid : jid.toBare(); + Presence::ref presence = presenceOracle_->getAccountPresence(bareishJID); + StatusShow::Type type = presence ? presence->getShow() : StatusShow::None; + boost::filesystem::path avatarPath = avatarManager_ ? avatarManager_->getAvatarPath(bareishJID) : boost::filesystem::path(); + return ChatListWindow::Chat(bareishJID, nickResolver_->jidToNick(bareishJID), activity, unreadCount, type, avatarPath, false, privateMessage); + } +} +#endif + void ChatsManager::handleChatActivity(const JID& jid, const std::string& activity, bool isMUC) { - if (mucRegistry_->isMUC(jid.toBare()) && !isMUC) { - /* Don't include PMs in MUC rooms.*/ - return; - } - ChatListWindow::Chat chat = createChatListChatItem(jid, activity); - /* FIXME: handle nick changes */ - appendRecent(chat); - handleUnreadCountChanged(NULL); - saveRecents(); +#ifndef NOT_YET + const bool privateMessage = mucRegistry_->isMUC(jid.toBare()) && !isMUC; + ChatListWindow::Chat chat = createChatListChatItem(jid, activity, privateMessage); + /* FIXME: handle nick changes */ + appendRecent(chat); + handleUnreadCountChanged(nullptr); + saveRecents(); + + // Look up potential MUC controller and update title accordingly as people + // join impromptu chats. + if (mucControllers_.find(jid) != mucControllers_.end()) { + auto chatListWindowIter = std::find_if(recentChats_.begin(), recentChats_.end(), [&](const ChatListWindow::Chat& chatListWindow) { return jid == (chatListWindow.jid); }); + if (chatListWindowIter != recentChats_.end() && (mucControllers_[jid]->isImpromptu() || !chatListWindowIter->impromptuJIDs.empty())) { + mucControllers_[jid]->setChatWindowTitle(chatListWindowIter->getTitle()); + } + } +#endif } +void ChatsManager::handleChatClosed(const JID& /*jid*/) { + cleanupPrivateMessageRecents(); +#ifndef NOT_YET + chatListWindow_->setRecents(recentChats_); +#endif +} + +#ifndef NOT_YET + 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_) { - bool match = false; - if (controller) { - /* Matching MUC item */ - match |= chatItem.isMUC == controllerIsMUC && chatItem.jid.toBare() == controller->getToJID().toBare(); - /* Matching PM */ - match |= isPM && chatItem.jid == controller->getToJID(); - /* Matching non-PM */ - match |= !isPM && !controllerIsMUC && chatItem.jid.toBare() == controller->getToJID().toBare(); - } - if (match) { - chatItem.setUnreadCount(controller->getUnreadCount()); - } - unreadTotal += chatItem.unreadCount; - } - chatListWindow_->setRecents(recentChats_); - chatListWindow_->setUnreadCount(unreadTotal); + size_t unreadTotal = 0; + bool controllerIsMUC = dynamic_cast<MUCController*>(controller); + bool isPM = controller && !controllerIsMUC && mucRegistry_->isMUC(controller->getToJID().toBare()); + for (ChatListWindow::Chat& chatItem : recentChats_) { + bool match = false; + if (controller) { + /* Matching MUC item */ + match |= chatItem.isMUC == controllerIsMUC && chatItem.jid.toBare() == controller->getToJID().toBare(); + /* Matching PM */ + match |= isPM && chatItem.jid == controller->getToJID(); + /* Matching non-PM */ + match |= !isPM && !controllerIsMUC && chatItem.jid.toBare() == controller->getToJID().toBare(); + } + if (match) { + chatItem.setUnreadCount(controller->getUnreadCount()); + } + unreadTotal += chatItem.unreadCount; + } + chatListWindow_->setRecents(recentChats_); + chatListWindow_->setUnreadCount(unreadTotal); } boost::optional<ChatListWindow::Chat> ChatsManager::removeExistingChat(const ChatListWindow::Chat& chat) { - std::list<ChatListWindow::Chat>::iterator result = std::find(recentChats_.begin(), recentChats_.end(), chat); - if (result != recentChats_.end()) { - ChatListWindow::Chat existingChat = *result; - recentChats_.erase(std::remove(recentChats_.begin(), recentChats_.end(), chat), recentChats_.end()); - return boost::optional<ChatListWindow::Chat>(existingChat); - } else { - return boost::optional<ChatListWindow::Chat>(); - } + std::list<ChatListWindow::Chat>::iterator result = std::find(recentChats_.begin(), recentChats_.end(), chat); + if (result != recentChats_.end()) { + ChatListWindow::Chat existingChat = *result; + recentChats_.erase(std::remove(recentChats_.begin(), recentChats_.end(), chat), recentChats_.end()); + return boost::optional<ChatListWindow::Chat>(existingChat); + } else { + return boost::optional<ChatListWindow::Chat>(); + } +} +#endif + +#ifndef NOT_YET +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_; + for (const ChatListWindow::Chat& chat : chats) { + if (chat.isPrivateMessage) { + typedef std::map<JID, MUCController*> ControllerMap; + ControllerMap::iterator muc = mucControllers_.find(chat.jid.toBare()); + if (muc == mucControllers_.end() || !muc->second->isJoined()) { + ChatController* chatController = getChatControllerIfExists(chat.jid); + if (!chatController || !chatController->hasOpenWindow()) { + removeExistingChat(chat); + break; + } + } + } + } } void ChatsManager::appendRecent(const ChatListWindow::Chat& chat) { - boost::optional<ChatListWindow::Chat> oldChat = removeExistingChat(chat); - ChatListWindow::Chat mergedChat = chat; - if (oldChat && !oldChat->impromptuJIDs.empty()) { - mergedChat.impromptuJIDs.insert(oldChat->impromptuJIDs.begin(), oldChat->impromptuJIDs.end()); - } - recentChats_.push_front(mergedChat); + boost::optional<ChatListWindow::Chat> oldChat = removeExistingChat(chat); + ChatListWindow::Chat mergedChat = chat; + if (oldChat) { + mergedChat.inviteesNames.insert(oldChat->inviteesNames.begin(), oldChat->inviteesNames.end()); + mergedChat.impromptuJIDs.insert(oldChat->impromptuJIDs.begin(), oldChat->impromptuJIDs.end()); + } + recentChats_.push_front(mergedChat); } void ChatsManager::prependRecent(const ChatListWindow::Chat& chat) { - boost::optional<ChatListWindow::Chat> oldChat = removeExistingChat(chat); - ChatListWindow::Chat mergedChat = chat; - if (oldChat && !oldChat->impromptuJIDs.empty()) { - mergedChat.impromptuJIDs.insert(oldChat->impromptuJIDs.begin(), oldChat->impromptuJIDs.end()); - } - recentChats_.push_back(mergedChat); + boost::optional<ChatListWindow::Chat> oldChat = removeExistingChat(chat); + ChatListWindow::Chat mergedChat = chat; + if (oldChat) { + mergedChat.inviteesNames.insert(oldChat->inviteesNames.begin(), oldChat->inviteesNames.end()); + mergedChat.impromptuJIDs.insert(oldChat->impromptuJIDs.begin(), oldChat->impromptuJIDs.end()); + } + recentChats_.push_back(mergedChat); } +#endif 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_) { - if (chat.isMUC && chat.jid == (*it).first) { - chat.statusType = StatusShow::None; - chatListWindow_->setRecents(recentChats_); - break; - } - } - mucControllers_.erase(it); - delete mucController; - return; - } - } + std::map<JID, MUCController*>::iterator it; + for (it = mucControllers_.begin(); it != mucControllers_.end(); ++it) { + if ((*it).second == mucController) { +#ifndef NOT_YET + for (ChatListWindow::Chat& chat : recentChats_) { + if (chat.isMUC && chat.jid == (*it).first) { + chat.statusType = StatusShow::None; + } + } +#endif + const auto& jid = it->first; + auto state = chattables_.getState(jid); + state.status = StatusShow::None; + chattables_.setState(jid, state); + //If user deletes bookmark from chatListWindow_ and then decides to leave the room, or if the server doesn't support bookmarks, the bookmark will not exist. + if (auto bookmarkFound = mucBookmarkManager_->lookupBookmark(jid)) { + MUCBookmark newBookmark(bookmarkFound.get()); + newBookmark.setAutojoin(false); + mucBookmarkManager_->replaceBookmark(bookmarkFound.get(), newBookmark); + } + mucControllers_.erase(it); + delete mucController; + break; + } + } + cleanupPrivateMessageRecents(); +#ifndef NOT_YET + chatListWindow_->setRecents(recentChats_); +#endif } void ChatsManager::handleSettingChanged(const std::string& settingPath) { - if (settingPath == SettingConstants::REQUEST_DELIVERYRECEIPTS.getKey()) { - userWantsReceipts_ = settings_->getSetting(SettingConstants::REQUEST_DELIVERYRECEIPTS); - return; - } + if (settingPath == SettingConstants::REQUEST_DELIVERYRECEIPTS.getKey()) { + userWantsReceipts_ = settings_->getSetting(SettingConstants::REQUEST_DELIVERYRECEIPTS); + return; + } } void ChatsManager::finalizeImpromptuJoin(MUC::ref muc, const std::vector<JID>& jidsToInvite, const std::string& reason, const boost::optional<JID>& reuseChatJID) { - // send impromptu invites for the new MUC - std::vector<JID> missingJIDsToInvite = jidsToInvite; - - typedef std::pair<std::string, MUCOccupant> StringMUCOccupantPair; - std::map<std::string, MUCOccupant> occupants = muc->getOccupants(); - foreach(StringMUCOccupantPair occupant, occupants) { - boost::optional<JID> realJID = occupant.second.getRealJID(); - if (realJID) { - missingJIDsToInvite.erase(std::remove(missingJIDsToInvite.begin(), missingJIDsToInvite.end(), realJID->toBare()), missingJIDsToInvite.end()); - } - } - - if (reuseChatJID) { - muc->invitePerson(reuseChatJID.get(), reason, true, true); - } - foreach(const JID& jid, missingJIDsToInvite) { - muc->invitePerson(jid, reason, true); - } -} - -void ChatsManager::handleUIEvent(boost::shared_ptr<UIEvent> event) { - boost::shared_ptr<RequestChatUIEvent> chatEvent = boost::dynamic_pointer_cast<RequestChatUIEvent>(event); - if (chatEvent) { - handleChatRequest(chatEvent->getContact()); - return; - } - boost::shared_ptr<RemoveMUCBookmarkUIEvent> removeMUCBookmarkEvent = boost::dynamic_pointer_cast<RemoveMUCBookmarkUIEvent>(event); - if (removeMUCBookmarkEvent) { - mucBookmarkManager_->removeBookmark(removeMUCBookmarkEvent->getBookmark()); - return; - } - boost::shared_ptr<AddMUCBookmarkUIEvent> addMUCBookmarkEvent = boost::dynamic_pointer_cast<AddMUCBookmarkUIEvent>(event); - if (addMUCBookmarkEvent) { - mucBookmarkManager_->addBookmark(addMUCBookmarkEvent->getBookmark()); - return; - } - - boost::shared_ptr<CreateImpromptuMUCUIEvent> createImpromptuMUCEvent = boost::dynamic_pointer_cast<CreateImpromptuMUCUIEvent>(event); - if (createImpromptuMUCEvent) { - assert(!localMUCServiceJID_.toString().empty()); - // create new muc - JID roomJID = createImpromptuMUCEvent->getRoomJID().toString().empty() ? JID(idGenerator_.generateID(), localMUCServiceJID_) : createImpromptuMUCEvent->getRoomJID(); - - // join muc - MUC::ref muc = handleJoinMUCRequest(roomJID, boost::optional<std::string>(), nickResolver_->jidToNick(jid_), false, true, true); - mucControllers_[roomJID]->onImpromptuConfigCompleted.connect(boost::bind(&ChatsManager::finalizeImpromptuJoin, this, muc, createImpromptuMUCEvent->getJIDs(), createImpromptuMUCEvent->getReason(), boost::optional<JID>())); - mucControllers_[roomJID]->activateChatWindow(); - } - - boost::shared_ptr<EditMUCBookmarkUIEvent> editMUCBookmarkEvent = boost::dynamic_pointer_cast<EditMUCBookmarkUIEvent>(event); - if (editMUCBookmarkEvent) { - mucBookmarkManager_->replaceBookmark(editMUCBookmarkEvent->getOldBookmark(), editMUCBookmarkEvent->getNewBookmark()); - } - else if (JoinMUCUIEvent::ref joinEvent = boost::dynamic_pointer_cast<JoinMUCUIEvent>(event)) { - handleJoinMUCRequest(joinEvent->getJID(), joinEvent->getPassword(), joinEvent->getNick(), joinEvent->getShouldJoinAutomatically(), joinEvent->getCreateAsReservedRoomIfNew(), joinEvent->isImpromptu()); - mucControllers_[joinEvent->getJID()]->activateChatWindow(); - } - else if (boost::shared_ptr<RequestJoinMUCUIEvent> joinEvent = boost::dynamic_pointer_cast<RequestJoinMUCUIEvent>(event)) { - if (!joinMUCWindow_) { - joinMUCWindow_ = joinMUCWindowFactory_->createJoinMUCWindow(uiEventStream_); - joinMUCWindow_->onSearchMUC.connect(boost::bind(&ChatsManager::handleSearchMUCRequest, this)); - } - joinMUCWindow_->setMUC(joinEvent->getRoom()); - joinMUCWindow_->setNick(nickResolver_->jidToNick(jid_)); - joinMUCWindow_->show(); - } + // send impromptu invites for the new MUC + std::vector<JID> missingJIDsToInvite = jidsToInvite; + + typedef std::pair<std::string, MUCOccupant> StringMUCOccupantPair; + std::map<std::string, MUCOccupant> occupants = muc->getOccupants(); + 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()); + } + } + + if (reuseChatJID) { + muc->invitePerson(reuseChatJID.get(), reason, true, true); + } + for (const JID& jid : missingJIDsToInvite) { + muc->invitePerson(jid, reason, true); + } +} + +void ChatsManager::handleUIEvent(std::shared_ptr<UIEvent> event) { + std::shared_ptr<RequestChatUIEvent> chatEvent = std::dynamic_pointer_cast<RequestChatUIEvent>(event); + if (chatEvent) { + handleChatRequest(chatEvent->getContact()); + return; + } + std::shared_ptr<RemoveMUCBookmarkUIEvent> removeMUCBookmarkEvent = std::dynamic_pointer_cast<RemoveMUCBookmarkUIEvent>(event); + if (removeMUCBookmarkEvent) { + mucBookmarkManager_->removeBookmark(removeMUCBookmarkEvent->getBookmark()); + return; + } + std::shared_ptr<AddMUCBookmarkUIEvent> addMUCBookmarkEvent = std::dynamic_pointer_cast<AddMUCBookmarkUIEvent>(event); + if (addMUCBookmarkEvent) { + mucBookmarkManager_->addBookmark(addMUCBookmarkEvent->getBookmark()); + return; + } + std::shared_ptr<SendFileUIEvent> sendFileEvent = std::dynamic_pointer_cast<SendFileUIEvent>(event); + if (sendFileEvent) { + JID fileReceiver = sendFileEvent->getJID(); + if (fileReceiver.isBare()) { + // See if there is a chat controller for a conversation with a bound + // full JID. Check if this JID supports file transfer and use it instead + // of the bare JID. + ChatController* controller = getChatControllerIfExists(fileReceiver, false); + if (controller) { + JID controllerJID = controller->getToJID(); + if (!controllerJID.isBare() && (FeatureOracle(entityCapsProvider_, presenceOracle_).isFileTransferSupported(controllerJID) == Yes)) { + fileReceiver = controllerJID; + } + } + } + ftOverview_->sendFile(fileReceiver, sendFileEvent->getFilename()); + return; + } + + std::shared_ptr<CreateImpromptuMUCUIEvent> createImpromptuMUCEvent = std::dynamic_pointer_cast<CreateImpromptuMUCUIEvent>(event); + if (createImpromptuMUCEvent) { + assert(!localMUCServiceJID_.toString().empty()); + // The room JID is random for new impromptu rooms, or a predefined JID for impromptu rooms resumed from the 'Recent chats' list. + JID roomJID = createImpromptuMUCEvent->getRoomJID().toString().empty() ? JID(idGenerator_.generateID(), localMUCServiceJID_) : createImpromptuMUCEvent->getRoomJID(); + + std::vector<JID> missingJIDsToInvite = createImpromptuMUCEvent->getJIDs(); + for (const JID& jid : missingJIDsToInvite) { + invitees_[roomJID].insert(jid); + } + // join muc + MUC::ref muc = handleJoinMUCRequest(roomJID, boost::optional<std::string>(), nickResolver_->jidToNick(jid_), true, true); + mucControllers_[roomJID]->onImpromptuConfigCompleted.connect(boost::bind(&ChatsManager::finalizeImpromptuJoin, this, muc, createImpromptuMUCEvent->getJIDs(), createImpromptuMUCEvent->getReason(), boost::optional<JID>())); + mucControllers_[roomJID]->activateChatWindow(); + } + + std::shared_ptr<EditMUCBookmarkUIEvent> editMUCBookmarkEvent = std::dynamic_pointer_cast<EditMUCBookmarkUIEvent>(event); + if (editMUCBookmarkEvent) { + mucBookmarkManager_->replaceBookmark(editMUCBookmarkEvent->getOldBookmark(), editMUCBookmarkEvent->getNewBookmark()); + } + else if (JoinMUCUIEvent::ref joinEvent = std::dynamic_pointer_cast<JoinMUCUIEvent>(event)) { + handleJoinMUCRequest(joinEvent->getJID(), joinEvent->getPassword(), joinEvent->getNick(), joinEvent->getCreateAsReservedRoomIfNew(), joinEvent->isImpromptu()); + mucControllers_[joinEvent->getJID()]->activateChatWindow(); + } + else if (std::shared_ptr<RequestJoinMUCUIEvent> joinEvent = std::dynamic_pointer_cast<RequestJoinMUCUIEvent>(event)) { + if (!joinMUCWindow_) { + joinMUCWindow_ = joinMUCWindowFactory_->createJoinMUCWindow(uiEventStream_); + joinMUCWindow_->onSearchMUC.connect(boost::bind(&ChatsManager::handleSearchMUCRequest, this)); + } + joinMUCWindow_->setMUC(joinEvent->getRoom()); + joinMUCWindow_->setNick(nickResolver_->jidToNick(jid_)); + joinMUCWindow_->show(); + } } +#ifndef NOT_YET void ChatsManager::markAllRecentsOffline() { - foreach (ChatListWindow::Chat& chat, recentChats_) { - chat.setStatusType(StatusShow::None); - } + for (ChatListWindow::Chat& chat : recentChats_) { + chat.setStatusType(StatusShow::None); + } - chatListWindow_->setRecents(recentChats_); + chatListWindow_->setRecents(recentChats_); } +#endif void ChatsManager::handleTransformChatToMUC(ChatController* chatController, ChatWindow* chatWindow, const std::vector<JID>& jidsToInvite, const std::string& reason) { - JID reuseChatInvite = chatController->getToJID(); - chatControllers_.erase(chatController->getToJID()); - delete chatController; + JID reuseChatInvite = chatController->getToJID(); + chatControllers_.erase(chatController->getToJID()); + delete chatController; - // join new impromptu muc - assert(!localMUCServiceJID_.toString().empty()); + // join new impromptu muc + assert(!localMUCServiceJID_.toString().empty()); - // create new muc - JID roomJID = JID(idGenerator_.generateID(), localMUCServiceJID_); + // create new muc + JID roomJID = JID(idGenerator_.generateID(), localMUCServiceJID_); - // join muc - MUC::ref muc = handleJoinMUCRequest(roomJID, boost::optional<std::string>(), nickResolver_->jidToNick(jid_), false, true, true, chatWindow); - mucControllers_[roomJID]->onImpromptuConfigCompleted.connect(boost::bind(&ChatsManager::finalizeImpromptuJoin, this, muc, jidsToInvite, reason, boost::optional<JID>(reuseChatInvite))); + // join muc + MUC::ref muc = handleJoinMUCRequest(roomJID, boost::optional<std::string>(), nickResolver_->jidToNick(jid_), true, true, chatWindow); + mucControllers_[roomJID]->onImpromptuConfigCompleted.connect(boost::bind(&ChatsManager::finalizeImpromptuJoin, this, muc, jidsToInvite, reason, boost::optional<JID>(reuseChatInvite))); } /** * If a resource goes offline, release bound chatdialog to that resource. */ -void ChatsManager::handlePresenceChange(boost::shared_ptr<Presence> newPresence) { - if (mucRegistry_->isMUC(newPresence->getFrom().toBare())) return; - - foreach (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); - chatListWindow_->setRecents(recentChats_); - break; - } - } - - //if (newPresence->getType() != Presence::Unavailable) return; - JID fullJID(newPresence->getFrom()); - std::map<JID, ChatController*>::iterator it = chatControllers_.find(fullJID); - if (it == chatControllers_.end()) return; - JID bareJID(fullJID.toBare()); - //It doesn't make sense to have two unbound dialogs. - if (chatControllers_.find(bareJID) != chatControllers_.end()) return; - rebindControllerJID(fullJID, bareJID); +void ChatsManager::handlePresenceChange(std::shared_ptr<Presence> newPresence) { + if (mucRegistry_->isMUC(newPresence->getFrom().toBare())) return; +#ifndef NOT_YET + 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); + chatListWindow_->setRecents(recentChats_); + break; + } + } +#endif + //if (newPresence->getType() != Presence::Unavailable) return; + + JID fullJID(newPresence->getFrom()); + std::map<JID, ChatController*>::iterator it = chatControllers_.find(fullJID); + if (it == chatControllers_.end()) return; + JID bareJID(fullJID.toBare()); + //It doesn't make sense to have two unbound dialogs. + if (chatControllers_.find(bareJID) != chatControllers_.end()) return; + rebindControllerJID(fullJID, bareJID); } void ChatsManager::setAvatarManager(AvatarManager* avatarManager) { - if (avatarManager_) { - avatarManager_->onAvatarChanged.disconnect(boost::bind(&ChatsManager::handleAvatarChanged, this, _1)); - } - avatarManager_ = avatarManager; - foreach (ChatListWindow::Chat& chat, recentChats_) { - if (!chat.isMUC) { - chat.setAvatarPath(avatarManager_->getAvatarPath(chat.jid)); - } - } - avatarManager_->onAvatarChanged.connect(boost::bind(&ChatsManager::handleAvatarChanged, this, _1)); + if (avatarManager_) { + avatarManager_->onAvatarChanged.disconnect(boost::bind(&ChatsManager::handleAvatarChanged, this, _1)); + } + avatarManager_ = avatarManager; +#ifndef NOT_YET + for (ChatListWindow::Chat& chat : recentChats_) { + if (!chat.isMUC) { + chat.setAvatarPath(avatarManager_->getAvatarPath(chat.jid)); + } + } +#endif + avatarManager_->onAvatarChanged.connect(boost::bind(&ChatsManager::handleAvatarChanged, this, _1)); } void ChatsManager::handleAvatarChanged(const JID& jid) { - foreach (ChatListWindow::Chat& chat, recentChats_) { - if (!chat.isMUC && jid.toBare() == chat.jid.toBare()) { - chat.setAvatarPath(avatarManager_->getAvatarPath(jid)); - break; - } - } +#ifndef NOT_YET + for (ChatListWindow::Chat& chat : recentChats_) { + if (!chat.isMUC && jid.toBare() == chat.jid.toBare()) { + chat.setAvatarPath(avatarManager_->getAvatarPath(jid)); + break; + } + } +#endif } -void ChatsManager::setServerDiscoInfo(boost::shared_ptr<DiscoInfo> info) { - serverDiscoInfo_ = info; - foreach (JIDChatControllerPair pair, chatControllers_) { - pair.second->setAvailableServerFeatures(info); - } - foreach (JIDMUCControllerPair pair, mucControllers_) { - pair.second->setAvailableServerFeatures(info); - } +void ChatsManager::setServerDiscoInfo(std::shared_ptr<DiscoInfo> info) { + serverDiscoInfo_ = info; + for (JIDChatControllerPair pair : chatControllers_) { + pair.second->setAvailableServerFeatures(info); + } + for (JIDMUCControllerPair pair : mucControllers_) { + pair.second->setAvailableServerFeatures(info); + } } /** * This is to be called on connect/disconnect. - */ + */ void ChatsManager::setOnline(bool enabled) { - foreach (JIDChatControllerPair controllerPair, chatControllers_) { - controllerPair.second->setOnline(enabled); - } - foreach (JIDMUCControllerPair controllerPair, mucControllers_) { - controllerPair.second->setOnline(enabled); - if (enabled) { - controllerPair.second->rejoin(); - } - } - if (!enabled) { - delete mucBookmarkManager_; - mucBookmarkManager_ = NULL; - chatListWindow_->setBookmarksEnabled(false); - markAllRecentsOffline(); - } else { - setupBookmarks(); - localMUCServiceFinderWalker_ = boost::make_shared<DiscoServiceWalker>(jid_.getDomain(), iqRouter_); - localMUCServiceFinderWalker_->onServiceFound.connect(boost::bind(&ChatsManager::handleLocalServiceFound, this, _1, _2)); - localMUCServiceFinderWalker_->onWalkAborted.connect(boost::bind(&ChatsManager::handleLocalServiceWalkFinished, this)); - localMUCServiceFinderWalker_->onWalkComplete.connect(boost::bind(&ChatsManager::handleLocalServiceWalkFinished, this)); - localMUCServiceFinderWalker_->beginWalk(); - } - + for (JIDChatControllerPair controllerPair : chatControllers_) { + controllerPair.second->setOnline(enabled); + } + for (JIDMUCControllerPair controllerPair : mucControllers_) { + controllerPair.second->setOnline(enabled); + if (enabled) { + controllerPair.second->rejoin(); + } + } + if (!enabled) { + markAllRecentsOffline(); + } else { + setupBookmarks(); + localMUCServiceJID_ = JID(); + localMUCServiceFinderWalker_ = std::make_shared<DiscoServiceWalker>(jid_.getDomain(), iqRouter_); + localMUCServiceFinderWalker_->onServiceFound.connect(boost::bind(&ChatsManager::handleLocalServiceFound, this, _1, _2)); + localMUCServiceFinderWalker_->onWalkAborted.connect(boost::bind(&ChatsManager::handleLocalServiceWalkFinished, this)); + localMUCServiceFinderWalker_->onWalkComplete.connect(boost::bind(&ChatsManager::handleLocalServiceWalkFinished, this)); + localMUCServiceFinderWalker_->beginWalk(); + } +#ifndef NOT_YET + if (chatListWindow_) { + chatListWindow_->setBookmarksEnabled(enabled); + } +#endif } void ChatsManager::handleChatRequest(const std::string &contact) { - ChatController* controller = getChatControllerOrFindAnother(JID(contact)); - controller->activateChatWindow(); + ChatController* controller = getChatControllerOrFindAnother(JID(contact)); + controller->activateChatWindow(); } ChatController* ChatsManager::getChatControllerOrFindAnother(const JID &contact) { - ChatController* controller = getChatControllerIfExists(contact); - if (!controller && !mucRegistry_->isMUC(contact.toBare())) { - foreach (JIDChatControllerPair pair, chatControllers_) { - if (pair.first.toBare() == contact.toBare()) { - controller = pair.second; - break; - } - } - } - return controller ? controller : createNewChatController(contact); + ChatController* controller = getChatControllerIfExists(contact); + if (!controller && !mucRegistry_->isMUC(contact.toBare())) { + for (JIDChatControllerPair pair : chatControllers_) { + if (pair.first.toBare() == contact.toBare()) { + controller = pair.second; + break; + } + } + } + return controller ? controller : createNewChatController(contact); } ChatController* ChatsManager::createNewChatController(const JID& contact) { - assert(chatControllers_.find(contact) == chatControllers_.end()); - ChatController* controller = new ChatController(jid_, stanzaChannel_, iqRouter_, chatWindowFactory_, contact, nickResolver_, presenceOracle_, avatarManager_, mucRegistry_->isMUC(contact.toBare()), useDelayForLatency_, uiEventStream_, eventController_, timerFactory_, entityCapsProvider_, userWantsReceipts_, settings_, historyController_, mucRegistry_, highlightManager_, clientBlockListManager_, chatMessageParser_, autoAcceptMUCInviteDecider_); - chatControllers_[contact] = controller; - controller->setAvailableServerFeatures(serverDiscoInfo_); - controller->onActivity.connect(boost::bind(&ChatsManager::handleChatActivity, this, contact, _1, false)); - controller->onUnreadCountChanged.connect(boost::bind(&ChatsManager::handleUnreadCountChanged, this, controller)); - controller->onConvertToMUC.connect(boost::bind(&ChatsManager::handleTransformChatToMUC, this, controller, _1, _2, _3)); - updatePresenceReceivingStateOnChatController(contact); - controller->setCanStartImpromptuChats(!localMUCServiceJID_.toString().empty()); - return controller; + assert(chatControllers_.find(contact) == chatControllers_.end()); + std::shared_ptr<ChatMessageParser> chatMessageParser = std::make_shared<ChatMessageParser>(emoticons_, highlightManager_->getConfiguration(), ChatMessageParser::Mode::Chat); /* a message parser that knows this is a chat (not a room/MUC) */ + auto controller = new ChatController(jid_, stanzaChannel_, iqRouter_, chatWindowFactory_, contact, nickResolver_, presenceOracle_, avatarManager_, mucRegistry_->isMUC(contact.toBare()), useDelayForLatency_, uiEventStream_, timerFactory_, eventController_, entityCapsProvider_, userWantsReceipts_, historyController_, mucRegistry_, highlightManager_, clientBlockListManager_, chatMessageParser, autoAcceptMUCInviteDecider_, settings_, chattables_); + chatControllers_[contact] = controller; + controller->setAvailableServerFeatures(serverDiscoInfo_); + controller->onActivity.connect(boost::bind(&ChatsManager::handleChatActivity, this, contact, _1, false)); + controller->onWindowClosed.connect(boost::bind(&ChatsManager::handleChatClosed, this, contact)); +#ifndef NOT_YET + controller->onUnreadCountChanged.connect(boost::bind(&ChatsManager::handleUnreadCountChanged, this, controller)); +#endif + controller->onConvertToMUC.connect(boost::bind(&ChatsManager::handleTransformChatToMUC, this, controller, _1, _2, _3)); + updatePresenceReceivingStateOnChatController(contact); + controller->setCanStartImpromptuChats(!localMUCServiceJID_.toString().empty()); + return controller; } ChatController* ChatsManager::getChatControllerOrCreate(const JID &contact) { - ChatController* controller = getChatControllerIfExists(contact); - return controller ? controller : createNewChatController(contact); + ChatController* controller = getChatControllerIfExists(contact); + return controller ? controller : createNewChatController(contact); } ChatController* ChatsManager::getChatControllerIfExists(const JID &contact, bool rebindIfNeeded) { - if (chatControllers_.find(contact) == chatControllers_.end()) { - if (mucRegistry_->isMUC(contact.toBare())) { - return NULL; - } - //Need to look for an unbound window to bind first - JID bare(contact.toBare()); - if (chatControllers_.find(bare) != chatControllers_.end()) { - rebindControllerJID(bare, contact); - } else { - foreach (JIDChatControllerPair pair, chatControllers_) { - if (pair.first.toBare() == contact.toBare()) { - if (rebindIfNeeded) { - rebindControllerJID(pair.first, contact); - return chatControllers_[contact]; - } else { - return pair.second; - } - } - } - return NULL; - } - } - return chatControllers_[contact]; + if (chatControllers_.find(contact) == chatControllers_.end()) { + if (mucRegistry_->isMUC(contact.toBare())) { + return nullptr; + } + //Need to look for an unbound window to bind first + JID bare(contact.toBare()); + if (chatControllers_.find(bare) != chatControllers_.end()) { + if (rebindIfNeeded) { + rebindControllerJID(bare, contact); + } + else { + return chatControllers_[bare]; + } + } else { + for (JIDChatControllerPair pair : chatControllers_) { + if (pair.first.toBare() == contact.toBare()) { + if (rebindIfNeeded) { + rebindControllerJID(pair.first, contact); + return chatControllers_[contact]; + } else { + return pair.second; + } + } + } + return nullptr; + } + } + return chatControllers_[contact]; } void ChatsManager::rebindControllerJID(const JID& from, const JID& to) { - chatControllers_[to] = chatControllers_[from]; - chatControllers_.erase(from); - chatControllers_[to]->setToJID(to); -} - -MUC::ref ChatsManager::handleJoinMUCRequest(const JID &mucJID, const boost::optional<std::string>& password, const boost::optional<std::string>& nickMaybe, bool addAutoJoin, bool createAsReservedIfNew, bool isImpromptu, ChatWindow* reuseChatwindow) { - MUC::ref muc; - if (!stanzaChannel_->isAvailable()) { - /* This is potentially not the optimal solution, but it will avoid consistency issues.*/ - return muc; - } - if (addAutoJoin) { - MUCBookmark bookmark(mucJID, mucJID.getNode()); - bookmark.setAutojoin(true); - if (nickMaybe) { - bookmark.setNick(*nickMaybe); - } - if (password) { - bookmark.setPassword(*password); - } - mucBookmarkManager_->addBookmark(bookmark); - } - - std::map<JID, MUCController*>::iterator it = mucControllers_.find(mucJID); - if (it != mucControllers_.end()) { - it->second->rejoin(); - } else { - std::string nick = (nickMaybe && !(*nickMaybe).empty()) ? nickMaybe.get() : nickResolver_->jidToNick(jid_); - muc = mucManager->createMUC(mucJID); - if (createAsReservedIfNew) { - muc->setCreateAsReservedIfNew(); - } - if (isImpromptu) { - muc->setCreateAsReservedIfNew(); - } - - MUCController* controller = NULL; - SingleChatWindowFactoryAdapter* chatWindowFactoryAdapter = NULL; - if (reuseChatwindow) { - chatWindowFactoryAdapter = new SingleChatWindowFactoryAdapter(reuseChatwindow); - } - controller = new MUCController(jid_, muc, password, nick, stanzaChannel_, iqRouter_, reuseChatwindow ? chatWindowFactoryAdapter : chatWindowFactory_, presenceOracle_, avatarManager_, uiEventStream_, false, timerFactory_, eventController_, entityCapsProvider_, roster_, historyController_, mucRegistry_, highlightManager_, chatMessageParser_, isImpromptu, autoAcceptMUCInviteDecider_); - if (chatWindowFactoryAdapter) { - /* The adapters are only passed to chat windows, which are deleted in their - * controllers' dtor, which are deleted in ChatManager's dtor. The adapters - * are also deleted there.*/ - chatWindowFactoryAdapters_[controller] = chatWindowFactoryAdapter; - } - - mucControllers_[mucJID] = controller; - controller->setAvailableServerFeatures(serverDiscoInfo_); - controller->onUserLeft.connect(boost::bind(&ChatsManager::handleUserLeftMUC, this, controller)); - controller->onUserJoined.connect(boost::bind(&ChatsManager::handleChatActivity, this, mucJID.toBare(), "", true)); - controller->onActivity.connect(boost::bind(&ChatsManager::handleChatActivity, this, mucJID.toBare(), _1, true)); - controller->onUnreadCountChanged.connect(boost::bind(&ChatsManager::handleUnreadCountChanged, this, controller)); - handleChatActivity(mucJID.toBare(), "", true); - } - - mucControllers_[mucJID]->showChatWindow(); - return muc; + chatControllers_[to] = chatControllers_[from]; + chatControllers_.erase(from); + chatControllers_[to]->setToJID(to); +} + +MUC::ref ChatsManager::handleJoinMUCRequest(const JID &mucJID, const boost::optional<std::string>& password, const boost::optional<std::string>& nickMaybe, bool createAsReservedIfNew, bool isImpromptu, ChatWindow* reuseChatwindow) { + MUC::ref muc; + std::map<JID, MUCController*>::iterator it = mucControllers_.find(mucJID); + if (it != mucControllers_.end()) { + if (stanzaChannel_->isAvailable()) { + it->second->rejoin(); + } + } else { + std::string nick = (nickMaybe && !(*nickMaybe).empty()) ? nickMaybe.get() : nickResolver_->jidToNick(jid_); + muc = mucManager->createMUC(mucJID); + if (createAsReservedIfNew) { + muc->setCreateAsReservedIfNew(); + } + if (isImpromptu) { + muc->setCreateAsReservedIfNew(); + } + + MUCController* controller = nullptr; + SingleChatWindowFactoryAdapter* chatWindowFactoryAdapter = nullptr; + if (reuseChatwindow) { + chatWindowFactoryAdapter = new SingleChatWindowFactoryAdapter(reuseChatwindow); + } + std::shared_ptr<ChatMessageParser> chatMessageParser = std::make_shared<ChatMessageParser>(emoticons_, highlightManager_->getConfiguration(), ChatMessageParser::Mode::GroupChat); /* a message parser that knows this is a room/MUC (not a chat) */ + controller = new MUCController(jid_, muc, password, nick, stanzaChannel_, iqRouter_, reuseChatwindow ? chatWindowFactoryAdapter : chatWindowFactory_, nickResolver_, presenceOracle_, avatarManager_, uiEventStream_, false, timerFactory_, eventController_, entityCapsProvider_, roster_, historyController_, mucRegistry_, highlightManager_, clientBlockListManager_, chatMessageParser, isImpromptu, autoAcceptMUCInviteDecider_, vcardManager_, mucBookmarkManager_, settings_, chattables_); + chattables_.addJID(muc->getJID(), Chattables::State::Type::Room); + auto state = chattables_.getState(muc->getJID()); + state.status = StatusShow::Online; + chattables_.setState(muc->getJID(), state); + if (chatWindowFactoryAdapter) { + /* The adapters are only passed to chat windows, which are deleted in their + * controllers' dtor, which are deleted in ChatManager's dtor. The adapters + * are also deleted there.*/ + chatWindowFactoryAdapters_[controller] = chatWindowFactoryAdapter; + } + + mucControllers_[mucJID] = controller; + controller->setAvailableServerFeatures(serverDiscoInfo_); + controller->onUserLeft.connect(boost::bind(&ChatsManager::handleUserLeftMUC, this, controller)); + controller->onUserJoined.connect(boost::bind(&ChatsManager::handleChatActivity, this, mucJID.toBare(), "", true)); + controller->onUserNicknameChanged.connect(boost::bind(&ChatsManager::handleUserNicknameChanged, this, controller, _1, _2)); + controller->onActivity.connect(boost::bind(&ChatsManager::handleChatActivity, this, mucJID.toBare(), _1, true)); +#ifndef NOT_YET + controller->onUnreadCountChanged.connect(boost::bind(&ChatsManager::handleUnreadCountChanged, this, controller)); +#endif + if (!stanzaChannel_->isAvailable()) { + /* When online, the MUC is added to the registry in MUCImpl::internalJoin. This method is not + * called when Swift is offline, so we add it here as only MUCs in the registry are rejoined + * when going back online. + */ + mucRegistry_->addMUC(mucJID.toBare()); + } + handleChatActivity(mucJID.toBare(), "", true); + } +#ifndef NOT_YET + auto chatListWindowIter = std::find_if(recentChats_.begin(), recentChats_.end(), [&](const ChatListWindow::Chat& chatListWindow) { return mucJID == (chatListWindow.jid); }); + if (chatListWindowIter != recentChats_.end() && (mucControllers_[mucJID]->isImpromptu() || !chatListWindowIter->impromptuJIDs.empty())) { + mucControllers_[mucJID]->setChatWindowTitle(chatListWindowIter->getTitle()); + } +#endif + if (auto existingBookmark = mucBookmarkManager_->lookupBookmark(mucJID)) { + if (!existingBookmark->getAutojoin()) { + MUCBookmark newbookmark(existingBookmark.get()); + newbookmark.setAutojoin(true); + mucBookmarkManager_->replaceBookmark(*existingBookmark, newbookmark); + } + } + else { + MUCBookmark bookmark(mucJID, mucJID.getNode()); + bookmark.setAutojoin(true); + if (nickMaybe) { + bookmark.setNick(*nickMaybe); + } + if (password) { + bookmark.setPassword(*password); + } + mucBookmarkManager_->addBookmark(bookmark); + } + mucControllers_[mucJID]->showChatWindow(); + return muc; } void ChatsManager::handleSearchMUCRequest() { - mucSearchController_->openSearchWindow(); -} - -void ChatsManager::handleIncomingMessage(boost::shared_ptr<Message> message) { - JID jid = message->getFrom(); - boost::shared_ptr<MessageEvent> event(new MessageEvent(message)); - bool isInvite = message->getPayload<MUCInvitationPayload>(); - bool isMediatedInvite = (message->getPayload<MUCUserPayload>() && message->getPayload<MUCUserPayload>()->getInvite()); - if (isMediatedInvite) { - jid = (*message->getPayload<MUCUserPayload>()->getInvite()).from; - } - if (!event->isReadable() && !message->getPayload<ChatState>() && !message->getPayload<DeliveryReceipt>() && !message->getPayload<DeliveryReceiptRequest>() && !isInvite && !isMediatedInvite && !message->hasSubject()) { - return; - } - - // Try to deliver it to a MUC - if (message->getType() == Message::Groupchat || message->getType() == Message::Error /*|| (isInvite && message->getType() == Message::Normal)*/) { - std::map<JID, MUCController*>::iterator i = mucControllers_.find(jid.toBare()); - if (i != mucControllers_.end()) { - i->second->handleIncomingMessage(event); - return; - } - else if (message->getType() == Message::Groupchat) { - //FIXME: Error handling - groupchat messages from an unknown muc. - return; - } - } - - // check for impromptu invite to potentially auto-accept - MUCInvitationPayload::ref invite = message->getPayload<MUCInvitationPayload>(); - if (invite && autoAcceptMUCInviteDecider_->isAutoAcceptedInvite(message->getFrom(), invite)) { - if (invite->getIsContinuation()) { - // check for existing chat controller for the from JID - ChatController* controller = getChatControllerIfExists(jid); - if (controller) { - ChatWindow* window = controller->detachChatWindow(); - chatControllers_.erase(jid); - delete controller; - handleJoinMUCRequest(invite->getJID(), boost::optional<std::string>(), boost::optional<std::string>(), false, false, true, window); - } - } else { - handleJoinMUCRequest(invite->getJID(), boost::optional<std::string>(), boost::optional<std::string>(), false, false, true); - return; - } - } - - //if not a mucroom - if (!event->isReadable() && !isInvite && !isMediatedInvite) { - /* Only route such messages if a window exists, don't open new windows for them.*/ - ChatController* controller = getChatControllerIfExists(jid); - if (controller) { - controller->handleIncomingMessage(event); - } - } else { - getChatControllerOrCreate(jid)->handleIncomingMessage(event); - } + mucSearchController_->openSearchWindow(); +} + +void ChatsManager::handleUserNicknameChanged(MUCController* mucController, const std::string& oldNickname, const std::string& newNickname) { + JID oldMUCChatJID = mucController->getToJID().withResource(oldNickname); + JID newMUCChatJID = mucController->getToJID().withResource(newNickname); + + SWIFT_LOG(debug) << "nickname change in " << mucController->getToJID().toString() << " from " << oldNickname << " to " << newNickname; + + // get current chat controller + ChatController *chatController = getChatControllerIfExists(oldMUCChatJID); + if (chatController) { + // adjust chat controller + chatController->setToJID(newMUCChatJID); + nickResolver_->onNickChanged(newMUCChatJID, oldNickname); + chatControllers_.erase(oldMUCChatJID); + chatControllers_[newMUCChatJID] = chatController; + + chatController->onActivity.disconnect(boost::bind(&ChatsManager::handleChatActivity, this, oldMUCChatJID, _1, false)); + chatController->onActivity.connect(boost::bind(&ChatsManager::handleChatActivity, this, newMUCChatJID, _1, false)); + } +} + +bool ChatsManager::messageCausesSessionBinding(std::shared_ptr<Message> message) { + bool causesRebind = false; + ChatState::ref chatState = message->getPayload<ChatState>(); + if (!message->getBody().get_value_or("").empty() || (chatState && chatState->getChatState() == ChatState::Composing)) { + causesRebind = true; + } + return causesRebind; +} + +void ChatsManager::handleIncomingMessage(std::shared_ptr<Message> incomingMessage) { + std::shared_ptr<Message> message = incomingMessage; + if (message->getFrom().toBare() == jid_.toBare()) { + CarbonsReceived::ref carbonsReceived; + CarbonsSent::ref carbonsSent; + Forwarded::ref forwarded; + Message::ref forwardedMessage; + if ((carbonsReceived = incomingMessage->getPayload<CarbonsReceived>()) && + (forwarded = carbonsReceived->getForwarded()) && + (forwardedMessage = std::dynamic_pointer_cast<Message>(forwarded->getStanza()))) { + message = forwardedMessage; + } + else if ((carbonsSent = incomingMessage->getPayload<CarbonsSent>()) && + (forwarded = carbonsSent->getForwarded()) && + (forwardedMessage = std::dynamic_pointer_cast<Message>(forwarded->getStanza()))) { + JID toJID = forwardedMessage->getTo(); + + ChatController* controller = getChatControllerOrCreate(toJID); + if (controller) { + controller->handleIncomingOwnMessage(forwardedMessage); + } + else { + SWIFT_LOG(error) << "Carbons message ignored."; + } + return; + } + } + JID fromJID = message->getFrom(); + + std::shared_ptr<MessageEvent> event(new MessageEvent(message)); + bool isInvite = !!message->getPayload<MUCInvitationPayload>(); + bool isMediatedInvite = (message->getPayload<MUCUserPayload>() && message->getPayload<MUCUserPayload>()->getInvite()); + if (isMediatedInvite) { + fromJID = (*message->getPayload<MUCUserPayload>()->getInvite()).from; + } + if (!event->isReadable() && !message->getPayload<ChatState>() && !message->getPayload<DeliveryReceipt>() && !message->getPayload<DeliveryReceiptRequest>() && !isInvite && !isMediatedInvite && !message->hasSubject()) { + return; + } + + // Try to deliver MUC errors to a MUC PM window if a suitable window is open. + if (message->getType() == Message::Error) { + auto controller = getChatControllerIfExists(fromJID, messageCausesSessionBinding(message)); + if (controller) { + controller->handleIncomingMessage(event); + return; + } + } + + // Try to deliver it to a MUC. + if (message->getType() == Message::Groupchat || message->getType() == Message::Error) { + // Try to deliver it to a MUC room. + std::map<JID, MUCController*>::iterator i = mucControllers_.find(fromJID.toBare()); + if (i != mucControllers_.end()) { + i->second->handleIncomingMessage(event); + return; + } + else if (message->getType() == Message::Groupchat) { + //FIXME: Error handling - groupchat messages from an unknown muc. + return; + } + } + + // check for impromptu invite to potentially auto-accept + MUCInvitationPayload::ref invite = message->getPayload<MUCInvitationPayload>(); + if (invite && autoAcceptMUCInviteDecider_->isAutoAcceptedInvite(message->getFrom(), invite)) { + if (invite->getIsContinuation()) { + // check for existing chat controller for the from JID + ChatController* controller = getChatControllerIfExists(fromJID); + if (controller) { + ChatWindow* window = controller->detachChatWindow(); + chatControllers_.erase(fromJID); + delete controller; + handleJoinMUCRequest(invite->getJID(), boost::optional<std::string>(), boost::optional<std::string>(), false, true, window); + return; + } + } else { + handleJoinMUCRequest(invite->getJID(), boost::optional<std::string>(), boost::optional<std::string>(), false, true); + return; + } + } + + //if not a mucroom + if (!event->isReadable() && !isInvite && !isMediatedInvite) { + /* Only route such messages if a window exists, don't open new windows for them.*/ + + // Do not bind a controller to a full JID, for delivery receipts or chat state notifications. + ChatController* controller = getChatControllerIfExists(fromJID, messageCausesSessionBinding(message)); + if (controller) { + controller->handleIncomingMessage(event); + } + } else { + getChatControllerOrCreate(fromJID)->handleIncomingMessage(event); + } } void ChatsManager::handleMUCSelectedAfterSearch(const JID& muc) { - if (joinMUCWindow_) { - joinMUCWindow_->setMUC(muc.toString()); - } + if (joinMUCWindow_) { + joinMUCWindow_->setMUC(muc.toString()); + } } void ChatsManager::handleMUCBookmarkActivated(const MUCBookmark& mucBookmark) { - uiEventStream_->send(boost::make_shared<JoinMUCUIEvent>(mucBookmark.getRoom(), mucBookmark.getPassword(), mucBookmark.getNick())); + uiEventStream_->send(std::make_shared<JoinMUCUIEvent>(mucBookmark.getRoom(), mucBookmark.getPassword(), mucBookmark.getNick())); } void ChatsManager::handleNewFileTransferController(FileTransferController* ftc) { - ChatController* chatController = getChatControllerOrCreate(ftc->getOtherParty()); - chatController->handleNewFileTransferController(ftc); - chatController->activateChatWindow(); + ChatController* chatController = getChatControllerOrCreate(ftc->getOtherParty()); + chatController->handleNewFileTransferController(ftc); + if (!ftc->isIncoming()) { + chatController->activateChatWindow(); + } } void ChatsManager::handleWhiteboardSessionRequest(const JID& contact, bool senderIsSelf) { - ChatController* chatController = getChatControllerOrCreate(contact); - chatController->handleWhiteboardSessionRequest(senderIsSelf); - chatController->activateChatWindow(); + ChatController* chatController = getChatControllerOrCreate(contact); + chatController->handleWhiteboardSessionRequest(senderIsSelf); + chatController->activateChatWindow(); } void ChatsManager::handleWhiteboardStateChange(const JID& contact, const ChatWindow::WhiteboardSessionState state) { - ChatController* chatController = getChatControllerOrCreate(contact); - chatController->handleWhiteboardStateChange(state); - chatController->activateChatWindow(); - if (state == ChatWindow::WhiteboardAccepted) { - boost::filesystem::path path; - JID bareJID = contact.toBare(); - if (avatarManager_) { - path = avatarManager_->getAvatarPath(bareJID); - } - ChatListWindow::Chat chat(bareJID, nickResolver_->jidToNick(bareJID), "", 0, StatusShow::None, path, false); - chatListWindow_->addWhiteboardSession(chat); - } else { - chatListWindow_->removeWhiteboardSession(contact.toBare()); - } + ChatController* chatController = getChatControllerOrCreate(contact); + chatController->handleWhiteboardStateChange(state); + chatController->activateChatWindow(); + if (state == ChatWindow::WhiteboardAccepted) { + boost::filesystem::path path; + JID bareJID = contact.toBare(); + if (avatarManager_) { + path = avatarManager_->getAvatarPath(bareJID); + } + ChatListWindow::Chat chat(bareJID, nickResolver_->jidToNick(bareJID), "", 0, StatusShow::None, path, false); + chatListWindow_->addWhiteboardSession(chat); + } else { + chatListWindow_->removeWhiteboardSession(contact.toBare()); + } } - +#ifndef NOT_YET 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) { - inviteJIDs.push_back(pair.second); - } - uiEventStream_->send(boost::make_shared<CreateImpromptuMUCUIEvent>(inviteJIDs, chat.jid, "")); - } - else if (chat.isMUC) { - /* FIXME: This means that recents requiring passwords will just flat-out not work */ - uiEventStream_->send(boost::make_shared<JoinMUCUIEvent>(chat.jid, boost::optional<std::string>(), chat.nick)); - } - else { - uiEventStream_->send(boost::make_shared<RequestChatUIEvent>(chat.jid)); - } -} - -void ChatsManager::handleLocalServiceFound(const JID& service, boost::shared_ptr<DiscoInfo> info) { - foreach (DiscoInfo::Identity identity, info->getIdentities()) { - if ((identity.getCategory() == "directory" - && identity.getType() == "chatroom") - || (identity.getCategory() == "conference" - && identity.getType() == "text")) { - localMUCServiceJID_ = service; - localMUCServiceFinderWalker_->endWalk(); - SWIFT_LOG(debug) << "Use following MUC service for impromptu chats: " << localMUCServiceJID_ << std::endl; - break; - } - } + if (chat.isMUC && !chat.impromptuJIDs.empty()) { + typedef std::pair<std::string, JID> StringJIDPair; + std::vector<JID> inviteJIDs; + for (StringJIDPair pair : chat.impromptuJIDs) { + inviteJIDs.push_back(pair.second); + } + uiEventStream_->send(std::make_shared<CreateImpromptuMUCUIEvent>(inviteJIDs, chat.jid, "")); + } + else if (chat.isMUC) { + bool isImpromptu = (!chat.inviteesNames.empty() || !chat.impromptuJIDs.empty()); + /* FIXME: This means that recents requiring passwords will just flat-out not work */ + uiEventStream_->send(std::make_shared<JoinMUCUIEvent>(chat.jid, boost::optional<std::string>(), chat.nick, false, isImpromptu)); + } + else { + uiEventStream_->send(std::make_shared<RequestChatUIEvent>(chat.jid)); + } +} + +#endif + +void ChatsManager::handleChattableActivated(const JID& jid) { + auto state = chattables_.getState(jid); + if (state.type == Chattables::State::Type::Person) { + uiEventStream_->send(std::make_shared<RequestChatUIEvent>(jid)); + } + else if (state.type == Chattables::State::Type::Room) { + if (auto foundBookmark = mucBookmarkManager_->lookupBookmark(jid)) { + handleMUCBookmarkActivated(foundBookmark.get()); + } + else { + uiEventStream_->send(std::make_shared<JoinMUCUIEvent>(jid, boost::optional<std::string>(), boost::optional<std::string>())); // Just a quick hack to reuse already open MUCs + } + } +} + +void ChatsManager::handleLocalServiceFound(const JID& service, std::shared_ptr<DiscoInfo> info) { + for (DiscoInfo::Identity identity : info->getIdentities()) { + if ((identity.getCategory() == "directory" + && identity.getType() == "chatroom") + || (identity.getCategory() == "conference" + && identity.getType() == "text")) { + localMUCServiceJID_ = service; + localMUCServiceFinderWalker_->endWalk(); + SWIFT_LOG(debug) << "Use following MUC service for impromptu chats: " << localMUCServiceJID_; + break; + } + } } void ChatsManager::handleLocalServiceWalkFinished() { - onImpromptuMUCServiceDiscovered(!localMUCServiceJID_.toString().empty()); + bool impromptuMUCSupported = !localMUCServiceJID_.toString().empty(); + for (JIDChatControllerPair controllerPair : chatControllers_) { + controllerPair.second->setCanStartImpromptuChats(impromptuMUCSupported); + } + for (JIDMUCControllerPair controllerPair : mucControllers_) { + controllerPair.second->setCanStartImpromptuChats(impromptuMUCSupported); + } + onImpromptuMUCServiceDiscovered(impromptuMUCSupported); } +#ifndef NOT_YET std::vector<ChatListWindow::Chat> ChatsManager::getRecentChats() const { - return std::vector<ChatListWindow::Chat>(recentChats_.begin(), recentChats_.end()); + return std::vector<ChatListWindow::Chat>(recentChats_.begin(), recentChats_.end()); } - -std::vector<Contact> Swift::ChatsManager::getContacts() { - std::vector<Contact> result; - foreach (ChatListWindow::Chat chat, recentChats_) { - if (!chat.isMUC) { - result.push_back(Contact(chat.chatName.empty() ? chat.jid.toString() : chat.chatName, chat.jid, chat.statusType, chat.avatarPath)); - } - } - return result; +#endif + +std::vector<Contact::ref> Swift::ChatsManager::getContacts(bool withMUCNicks) { + std::vector<Contact::ref> result; +#ifndef NOT_YET + 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)); + } + } +#endif + if (withMUCNicks) { + /* collect MUC nicks */ + typedef std::map<JID, MUCController*>::value_type Item; + 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; + 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); + result.push_back(std::make_shared<Contact>(participant.first, JID(), presence->getShow(), avatar)); + } + } + } + return result; } ChatsManager::SingleChatWindowFactoryAdapter::SingleChatWindowFactoryAdapter(ChatWindow* chatWindow) : chatWindow_(chatWindow) {} ChatsManager::SingleChatWindowFactoryAdapter::~SingleChatWindowFactoryAdapter() {} ChatWindow* ChatsManager::SingleChatWindowFactoryAdapter::createChatWindow(const JID &, UIEventStream*) { - return chatWindow_; + return chatWindow_; } } diff --git a/Swift/Controllers/Chat/ChatsManager.h b/Swift/Controllers/Chat/ChatsManager.h index c9dd856..e120831 100644 --- a/Swift/Controllers/Chat/ChatsManager.h +++ b/Swift/Controllers/Chat/ChatsManager.h @@ -1,25 +1,24 @@ /* - * Copyright (c) 2010-2013 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once #include <map> +#include <memory> #include <string> - -#include <boost/shared_ptr.hpp> +#include <set> #include <Swiften/Base/IDGenerator.h> #include <Swiften/Elements/DiscoInfo.h> #include <Swiften/Elements/Message.h> #include <Swiften/Elements/Presence.h> #include <Swiften/JID/JID.h> -#include <Swiften/MUC/MUCRegistry.h> -#include <Swiften/MUC/MUCBookmark.h> #include <Swiften/MUC/MUC.h> - +#include <Swiften/MUC/MUCBookmark.h> +#include <Swiften/MUC/MUCRegistry.h> #include <Swift/Controllers/ContactProvider.h> #include <Swift/Controllers/UIEvents/UIEventStream.h> @@ -27,158 +26,183 @@ #include <Swift/Controllers/UIInterfaces/ChatWindow.h> #include <Swift/Controllers/UIInterfaces/ChatWindowFactory.h> - namespace Swift { - class EventController; - class ChatController; - class ChatControllerBase; - class MUCController; - class MUCManager; - class JoinMUCWindow; - class JoinMUCWindowFactory; - class NickResolver; - class PresenceOracle; - class AvatarManager; - class StanzaChannel; - class IQRouter; - class PresenceSender; - class MUCBookmarkManager; - class ChatListWindowFactory; - class TimerFactory; - class EntityCapsProvider; - class DirectedPresenceSender; - class MUCSearchWindowFactory; - class ProfileSettingsProvider; - class MUCSearchController; - class FileTransferOverview; - class FileTransferController; - class XMPPRoster; - class SettingsProvider; - class WhiteboardManager; - class HistoryController; - class HighlightManager; - class ClientBlockListManager; - class ChatMessageParser; - class DiscoServiceWalker; - class AutoAcceptMUCInviteDecider; - class UserSearchController; - - class ChatsManager : public ContactProvider { - public: - ChatsManager(JID jid, StanzaChannel* stanzaChannel, IQRouter* iqRouter, EventController* eventController, ChatWindowFactory* chatWindowFactory, JoinMUCWindowFactory* joinMUCWindowFactory, NickResolver* nickResolver, PresenceOracle* presenceOracle, PresenceSender* presenceSender, UIEventStream* uiEventStream, ChatListWindowFactory* chatListWindowFactory, bool useDelayForLatency, TimerFactory* timerFactory, MUCRegistry* mucRegistry, EntityCapsProvider* entityCapsProvider, MUCManager* mucManager, MUCSearchWindowFactory* mucSearchWindowFactory, ProfileSettingsProvider* profileSettings, FileTransferOverview* ftOverview, XMPPRoster* roster, bool eagleMode, SettingsProvider* settings, HistoryController* historyController_, WhiteboardManager* whiteboardManager, HighlightManager* highlightManager, ClientBlockListManager* clientBlockListManager, const std::map<std::string, std::string>& emoticons, UserSearchController* inviteUserSearchController); - virtual ~ChatsManager(); - void setAvatarManager(AvatarManager* avatarManager); - void setOnline(bool enabled); - void setServerDiscoInfo(boost::shared_ptr<DiscoInfo> info); - void handleIncomingMessage(boost::shared_ptr<Message> message); - std::vector<ChatListWindow::Chat> getRecentChats() const; - virtual std::vector<Contact> getContacts(); - - boost::signal<void (bool supportsImpromptu)> onImpromptuMUCServiceDiscovered; - - private: - class SingleChatWindowFactoryAdapter : public ChatWindowFactory { - public: - SingleChatWindowFactoryAdapter(ChatWindow* chatWindow); - virtual ~SingleChatWindowFactoryAdapter(); - virtual ChatWindow* createChatWindow(const JID &, UIEventStream*); - - private: - ChatWindow* chatWindow_; - }; - - private: - ChatListWindow::Chat createChatListChatItem(const JID& jid, const std::string& activity); - void handleChatRequest(const std::string& contact); - void finalizeImpromptuJoin(MUC::ref muc, const std::vector<JID>& jidsToInvite, const std::string& reason, const boost::optional<JID>& reuseChatJID = boost::optional<JID>()); - MUC::ref handleJoinMUCRequest(const JID& muc, const boost::optional<std::string>& password, const boost::optional<std::string>& nick, bool addAutoJoin, bool createAsReservedIfNew, bool isImpromptu, ChatWindow* reuseChatwindow = 0); - void handleSearchMUCRequest(); - void handleMUCSelectedAfterSearch(const JID&); - void rebindControllerJID(const JID& from, const JID& to); - void handlePresenceChange(boost::shared_ptr<Presence> newPresence); - void handleUIEvent(boost::shared_ptr<UIEvent> event); - void handleMUCBookmarkAdded(const MUCBookmark& bookmark); - void handleMUCBookmarkRemoved(const MUCBookmark& bookmark); - void handleUserLeftMUC(MUCController* mucController); - void handleBookmarksReady(); - void handleChatActivity(const JID& jid, const std::string& activity, bool isMUC); - void handleNewFileTransferController(FileTransferController*); - void handleWhiteboardSessionRequest(const JID& contact, bool senderIsSelf); - void handleWhiteboardStateChange(const JID& contact, const ChatWindow::WhiteboardSessionState state); - boost::optional<ChatListWindow::Chat> removeExistingChat(const ChatListWindow::Chat& chat); - void appendRecent(const ChatListWindow::Chat& chat); - void prependRecent(const ChatListWindow::Chat& chat); - void setupBookmarks(); - void loadRecents(); - void saveRecents(); - void handleChatMadeRecent(); - void handleMUCBookmarkActivated(const MUCBookmark&); - void handleRecentActivated(const ChatListWindow::Chat&); - void handleUnreadCountChanged(ChatControllerBase* controller); - void handleAvatarChanged(const JID& jid); - void handleClearRecentsRequested(); - void handleJIDAddedToRoster(const JID&); - void handleJIDRemovedFromRoster(const JID&); - void handleJIDUpdatedInRoster(const JID&); - void handleRosterCleared(); - void handleSettingChanged(const std::string& settingPath); - void markAllRecentsOffline(); - void handleTransformChatToMUC(ChatController* chatController, ChatWindow* chatWindow, const std::vector<JID>& jidsToInvite, const std::string& reason); - - void handleLocalServiceFound(const JID& service, boost::shared_ptr<DiscoInfo> info); - void handleLocalServiceWalkFinished(); - - void updatePresenceReceivingStateOnChatController(const JID&); - ChatListWindow::Chat updateChatStatusAndAvatarHelper(const ChatListWindow::Chat& chat) const; - - - ChatController* getChatControllerOrFindAnother(const JID &contact); - ChatController* createNewChatController(const JID &contact); - ChatController* getChatControllerOrCreate(const JID &contact); - ChatController* getChatControllerIfExists(const JID &contact, bool rebindIfNeeded = true); - - private: - std::map<JID, MUCController*> mucControllers_; - std::map<JID, ChatController*> chatControllers_; - std::map<ChatControllerBase*, SingleChatWindowFactoryAdapter*> chatWindowFactoryAdapters_; - EventController* eventController_; - JID jid_; - StanzaChannel* stanzaChannel_; - IQRouter* iqRouter_; - ChatWindowFactory* chatWindowFactory_; - JoinMUCWindowFactory* joinMUCWindowFactory_; - NickResolver* nickResolver_; - PresenceOracle* presenceOracle_; - AvatarManager* avatarManager_; - PresenceSender* presenceSender_; - UIEventStream* uiEventStream_; - MUCBookmarkManager* mucBookmarkManager_; - boost::shared_ptr<DiscoInfo> serverDiscoInfo_; - ChatListWindow* chatListWindow_; - JoinMUCWindow* joinMUCWindow_; - boost::bsignals::scoped_connection uiEventConnection_; - bool useDelayForLatency_; - TimerFactory* timerFactory_; - MUCRegistry* mucRegistry_; - EntityCapsProvider* entityCapsProvider_; - MUCManager* mucManager; - MUCSearchController* mucSearchController_; - std::list<ChatListWindow::Chat> recentChats_; - ProfileSettingsProvider* profileSettings_; - FileTransferOverview* ftOverview_; - XMPPRoster* roster_; - bool eagleMode_; - bool userWantsReceipts_; - SettingsProvider* settings_; - HistoryController* historyController_; - WhiteboardManager* whiteboardManager_; - HighlightManager* highlightManager_; - ClientBlockListManager* clientBlockListManager_; - ChatMessageParser* chatMessageParser_; - JID localMUCServiceJID_; - boost::shared_ptr<DiscoServiceWalker> localMUCServiceFinderWalker_; - AutoAcceptMUCInviteDecider* autoAcceptMUCInviteDecider_; - UserSearchController* inviteUserSearchController_; - IDGenerator idGenerator_; - }; + class AutoAcceptMUCInviteDecider; + class AvatarManager; + class ChatController; + class ChatControllerBase; + class ChatListWindowFactory; + class ChatMessageParser; + class Chattables; + class ClientBlockListManager; + class DirectedPresenceSender; + class DiscoServiceWalker; + class EntityCapsProvider; + class EventController; + class FileTransferController; + class FileTransferOverview; + class HighlightManager; + class HistoryController; + class IQRouter; + class JoinMUCWindow; + class JoinMUCWindowFactory; + class MUCBookmarkManager; + class MUCController; + class MUCManager; + class MUCSearchController; + class MUCSearchWindowFactory; + class NickResolver; + class PresenceOracle; + class PresenceSender; + class ProfileSettingsProvider; + class SettingsProvider; + class StanzaChannel; + class TimerFactory; + class VCardManager; + class WhiteboardManager; + class XMPPRoster; + + class ChatsManager : public ContactProvider { + public: + ChatsManager(JID jid, StanzaChannel* stanzaChannel, IQRouter* iqRouter, EventController* eventController, ChatWindowFactory* chatWindowFactory, JoinMUCWindowFactory* joinMUCWindowFactory, NickResolver* nickResolver, PresenceOracle* presenceOracle, PresenceSender* presenceSender, UIEventStream* uiEventStream, ChatListWindowFactory* chatListWindowFactory, bool useDelayForLatency, TimerFactory* timerFactory, MUCRegistry* mucRegistry, EntityCapsProvider* entityCapsProvider, MUCManager* mucManager, MUCSearchWindowFactory* mucSearchWindowFactory, ProfileSettingsProvider* profileSettings, FileTransferOverview* ftOverview, XMPPRoster* roster, bool eagleMode, SettingsProvider* settings, HistoryController* historyController_, WhiteboardManager* whiteboardManager, HighlightManager* highlightManager, ClientBlockListManager* clientBlockListManager, const std::map<std::string, std::string>& emoticons, VCardManager* vcardManager, Chattables& chattables); + virtual ~ChatsManager(); + void setAvatarManager(AvatarManager* avatarManager); + void setOnline(bool enabled); + void setServerDiscoInfo(std::shared_ptr<DiscoInfo> info); + void handleIncomingMessage(std::shared_ptr<Message> incomingMessage); +#ifndef NOT_YET + std::vector<ChatListWindow::Chat> getRecentChats() const; +#endif + virtual std::vector<Contact::ref> getContacts(bool withMUCNicks); + + boost::signals2::signal<void (bool supportsImpromptu)> onImpromptuMUCServiceDiscovered; + + private: + class SingleChatWindowFactoryAdapter : public ChatWindowFactory { + public: + SingleChatWindowFactoryAdapter(ChatWindow* chatWindow); + virtual ~SingleChatWindowFactoryAdapter(); + virtual ChatWindow* createChatWindow(const JID &, UIEventStream*); + + private: + ChatWindow* chatWindow_; + }; + + private: +#ifndef NOT_YET + ChatListWindow::Chat createChatListChatItem(const JID& jid, const std::string& activity, bool privateMessage); +#endif + void handleChatRequest(const std::string& contact); + void finalizeImpromptuJoin(MUC::ref muc, const std::vector<JID>& jidsToInvite, const std::string& reason, const boost::optional<JID>& reuseChatJID = boost::optional<JID>()); + MUC::ref handleJoinMUCRequest(const JID& muc, const boost::optional<std::string>& password, const boost::optional<std::string>& nick, bool createAsReservedIfNew, bool isImpromptu, ChatWindow* reuseChatwindow = nullptr); + void handleSearchMUCRequest(); + void handleMUCSelectedAfterSearch(const JID&); + void rebindControllerJID(const JID& from, const JID& to); + void handlePresenceChange(std::shared_ptr<Presence> newPresence); + void handleUIEvent(std::shared_ptr<UIEvent> event); + void handleMUCBookmarkAdded(const MUCBookmark& bookmark); + void handleMUCBookmarkRemoved(const MUCBookmark& bookmark); + void handleUserLeftMUC(MUCController* mucController); + void handleUserNicknameChanged(MUCController* mucController, const std::string& oldNickname, const std::string& newNickname); + void handleBookmarksReady(); + void handleChatActivity(const JID& jid, const std::string& activity, bool isMUC); + void handleChatClosed(const JID& jid); + void handleNewFileTransferController(FileTransferController*); + void handleWhiteboardSessionRequest(const JID& contact, bool senderIsSelf); + void handleWhiteboardStateChange(const JID& contact, const ChatWindow::WhiteboardSessionState state); +#ifndef NOT_YET + boost::optional<ChatListWindow::Chat> removeExistingChat(const ChatListWindow::Chat& chat); +#endif + bool messageCausesSessionBinding(std::shared_ptr<Message> message); + void cleanupPrivateMessageRecents(); +#ifndef NOT_YET + void appendRecent(const ChatListWindow::Chat& chat); + void prependRecent(const ChatListWindow::Chat& chat); +#endif + void setupBookmarks(); +#ifndef NOT_YET + void loadRecents(); + void saveRecents(); + void handleChatMadeRecent(); + void handleMUCBookmarkActivated(const MUCBookmark&); + void handleRecentActivated(const ChatListWindow::Chat&); + void handleUnreadCountChanged(ChatControllerBase* controller); +#endif + void handleChattableActivated(const JID& jid); + void handleAvatarChanged(const JID& jid); + void handleClearRecentsRequested(); + void handleJIDAddedToRoster(const JID&); + void handleJIDRemovedFromRoster(const JID&); + void handleJIDUpdatedInRoster(const JID&); + void handleRosterCleared(); + void handleSettingChanged(const std::string& settingPath); + void markAllRecentsOffline(); + void handleTransformChatToMUC(ChatController* chatController, ChatWindow* chatWindow, const std::vector<JID>& jidsToInvite, const std::string& reason); + void handleLocalServiceFound(const JID& service, std::shared_ptr<DiscoInfo> info); + void handleLocalServiceWalkFinished(); + void updatePresenceReceivingStateOnChatController(const JID&); +#ifndef NOT_YET + ChatListWindow::Chat updateChatStatusAndAvatarHelper(const ChatListWindow::Chat& chat) const; +#endif + + + + ChatController* getChatControllerOrFindAnother(const JID &contact); + ChatController* createNewChatController(const JID &contact); + ChatController* getChatControllerOrCreate(const JID &contact); + ChatController* getChatControllerIfExists(const JID &contact, bool rebindIfNeeded = true); + + protected: + MUCBookmarkManager* mucBookmarkManager_; + + private: + std::map<JID, MUCController*> mucControllers_; + std::map<JID, ChatController*> chatControllers_; + std::map<ChatControllerBase*, SingleChatWindowFactoryAdapter*> chatWindowFactoryAdapters_; + EventController* eventController_; + JID jid_; + StanzaChannel* stanzaChannel_; + IQRouter* iqRouter_; + ChatWindowFactory* chatWindowFactory_; + JoinMUCWindowFactory* joinMUCWindowFactory_; + NickResolver* nickResolver_; + PresenceOracle* presenceOracle_; + AvatarManager* avatarManager_; + PresenceSender* presenceSender_; + UIEventStream* uiEventStream_; + std::shared_ptr<DiscoInfo> serverDiscoInfo_; +#ifndef NOT_YET + ChatListWindow* chatListWindow_; +#endif + JoinMUCWindow* joinMUCWindow_; + boost::signals2::scoped_connection uiEventConnection_; + bool useDelayForLatency_; + TimerFactory* timerFactory_; + MUCRegistry* mucRegistry_; + EntityCapsProvider* entityCapsProvider_; + MUCManager* mucManager; + MUCSearchController* mucSearchController_; +#ifndef NOT_YET + std::list<ChatListWindow::Chat> recentChats_; +#endif + ProfileSettingsProvider* profileSettings_; + FileTransferOverview* ftOverview_; + XMPPRoster* roster_; + bool eagleMode_; + bool userWantsReceipts_; + SettingsProvider* settings_; + HistoryController* historyController_; + WhiteboardManager* whiteboardManager_; + HighlightManager* highlightManager_; + std::map<std::string, std::string> emoticons_; + ClientBlockListManager* clientBlockListManager_; + JID localMUCServiceJID_; + std::shared_ptr<DiscoServiceWalker> localMUCServiceFinderWalker_; + AutoAcceptMUCInviteDecider* autoAcceptMUCInviteDecider_; + IDGenerator idGenerator_; + VCardManager* vcardManager_; + Chattables& chattables_; + + std::map<JID, std::set<JID>> invitees_; + }; } diff --git a/Swift/Controllers/Chat/Chattables.cpp b/Swift/Controllers/Chat/Chattables.cpp new file mode 100644 index 0000000..707046f --- /dev/null +++ b/Swift/Controllers/Chat/Chattables.cpp @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#include <Swift/Controllers/Chat/Chattables.h> + +namespace Swift { + +const std::vector<JID>& Chattables::get() const { + return list_; +} + +const Chattables::State& Chattables::getState(const JID& jid) const { + auto it = states_.find(jid); + return it != states_.end() ? it->second : unknown_; +} + +void Chattables::addJID(const JID& jid, State::Type type) { + if (states_.find(jid) != states_.end()) { + return; + } + State state; + state.type = type; + state.jid = jid; + onBeginAdd(static_cast<int>(list_.size())); + list_.push_back(jid); + states_[jid] = state; + onAdded(); +} + +void Chattables::setState(const JID& jid, State state) { + auto stateIter = states_.find(jid); + if (stateIter == states_.end()) { + return; + } + stateIter->second = state; + auto listPos = static_cast<int>(std::distance(list_.begin(), std::find(list_.begin(), list_.end(), jid))); + onChanged(jid, listPos); +} + +} diff --git a/Swift/Controllers/Chat/Chattables.h b/Swift/Controllers/Chat/Chattables.h new file mode 100644 index 0000000..3b5817a --- /dev/null +++ b/Swift/Controllers/Chat/Chattables.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#pragma once + +#include <map> +#include <vector> + +#include <boost/signals2.hpp> + +#include <Swiften/Elements/StatusShow.h> +#include <Swiften/JID/JID.h> + +namespace Swift { +class Chattables { + public: + struct State { + enum class Type {Room, Person}; + JID jid; + /// Empty for no name + std::string name; + size_t unreadCount = 0; + Type type; + StatusShow::Type status = StatusShow::None; + //avatar + //status + }; + const std::vector<JID>& get() const; + const State& getState(const JID& jid) const; + + void addJID(const JID& jid, State::Type type); + void setState(const JID& jid, State state); + + boost::signals2::signal<void (int)> onBeginAdd; + boost::signals2::signal<void ()> onAdded; + boost::signals2::signal<void (const JID&, int)> onChanged; + /// The UI has activated a chattable item (e.g. clicked in the roster) + boost::signals2::signal<void (const JID&)> onActivated; + private: + std::vector<JID> list_; + std::map<JID, State> states_; + State unknown_; +}; +} diff --git a/Swift/Controllers/Chat/MUCController.cpp b/Swift/Controllers/Chat/MUCController.cpp index 37631a5..30d4933 100644 --- a/Swift/Controllers/Chat/MUCController.cpp +++ b/Swift/Controllers/Chat/MUCController.cpp @@ -1,989 +1,1295 @@ /* - * Copyright (c) 2010-2013 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #include <Swift/Controllers/Chat/MUCController.h> +#include <algorithm> +#include <cassert> +#include <memory> + #include <boost/bind.hpp> #include <boost/regex.hpp> #include <boost/algorithm/string.hpp> +#include <boost/range/adaptor/reversed.hpp> -#include <Swift/Controllers/Intl.h> +#include <Swiften/Avatars/AvatarManager.h> +#include <Swiften/Base/Log.h> #include <Swiften/Base/format.h> +#include <Swiften/Base/Tristate.h> +#include <Swiften/Client/BlockList.h> +#include <Swiften/Client/ClientBlockListManager.h> +#include <Swiften/Client/StanzaChannel.h> +#include <Swiften/Disco/EntityCapsProvider.h> +#include <Swiften/Disco/GetDiscoInfoRequest.h> +#include <Swiften/Elements/Delay.h> +#include <Swiften/Elements/Thread.h> +#include <Swiften/MUC/MUC.h> +#include <Swiften/MUC/MUCBookmark.h> +#include <Swiften/MUC/MUCBookmarkManager.h> #include <Swiften/Network/Timer.h> #include <Swiften/Network/TimerFactory.h> -#include <Swiften/Base/foreach.h> +#include <Swiften/Roster/XMPPRoster.h> + #include <SwifTools/TabComplete.h> -#include <Swiften/Base/foreach.h> -#include <Swift/Controllers/XMPPEvents/EventController.h> -#include <Swift/Controllers/UIInterfaces/ChatWindow.h> -#include <Swift/Controllers/UIInterfaces/ChatWindowFactory.h> -#include <Swift/Controllers/UIEvents/UIEventStream.h> -#include <Swift/Controllers/UIEvents/RequestChatUIEvent.h> -#include <Swift/Controllers/UIEvents/RequestAddUserDialogUIEvent.h> -#include <Swift/Controllers/UIEvents/ShowProfileForRosterItemUIEvent.h> -#include <Swift/Controllers/UIEvents/RequestInviteToMUCUIEvent.h> -#include <Swift/Controllers/UIEvents/InviteToMUCUIEvent.h> -#include <Swift/Controllers/Roster/GroupRosterItem.h> + +#include <Swift/Controllers/Chat/ChatMessageParser.h> +#include <Swift/Controllers/Chat/Chattables.h> +#include <Swift/Controllers/Highlighting/Highlighter.h> +#include <Swift/Controllers/Intl.h> #include <Swift/Controllers/Roster/ContactRosterItem.h> -#include <Swiften/Avatars/AvatarManager.h> -#include <Swiften/Elements/Delay.h> -#include <Swiften/MUC/MUC.h> -#include <Swiften/Client/StanzaChannel.h> +#include <Swift/Controllers/Roster/GroupRosterItem.h> +#include <Swift/Controllers/Roster/ItemOperations/SetAvatar.h> +#include <Swift/Controllers/Roster/ItemOperations/SetMUC.h> +#include <Swift/Controllers/Roster/ItemOperations/SetPresence.h> #include <Swift/Controllers/Roster/Roster.h> -#include <Swift/Controllers/Roster/SetAvatar.h> -#include <Swift/Controllers/Roster/SetPresence.h> -#include <Swiften/Disco/EntityCapsProvider.h> -#include <Swiften/Roster/XMPPRoster.h> -#include <Swift/Controllers/Highlighter.h> -#include <Swift/Controllers/Chat/ChatMessageParser.h> - -#include <Swiften/Base/Log.h> +#include <Swift/Controllers/Roster/RosterVCardProvider.h> +#include <Swift/Controllers/UIEvents/InviteToMUCUIEvent.h> +#include <Swift/Controllers/UIEvents/RequestAddUserDialogUIEvent.h> +#include <Swift/Controllers/UIEvents/RequestChangeBlockStateUIEvent.h> +#include <Swift/Controllers/UIEvents/RequestChatUIEvent.h> +#include <Swift/Controllers/UIEvents/RequestInviteToMUCUIEvent.h> +#include <Swift/Controllers/UIEvents/ShowProfileForRosterItemUIEvent.h> +#include <Swift/Controllers/UIEvents/UIEventStream.h> +#include <Swift/Controllers/UIInterfaces/ChatWindow.h> +#include <Swift/Controllers/UIInterfaces/ChatWindowFactory.h> +#include <Swift/Controllers/XMPPEvents/EventController.h> #define MUC_JOIN_WARNING_TIMEOUT_MILLISECONDS 60000 namespace Swift { - + /** * The controller does not gain ownership of the stanzaChannel, nor the factory. */ MUCController::MUCController ( - const JID& self, - MUC::ref muc, - const boost::optional<std::string>& password, - const std::string &nick, - StanzaChannel* stanzaChannel, - IQRouter* iqRouter, - ChatWindowFactory* chatWindowFactory, - PresenceOracle* presenceOracle, - AvatarManager* avatarManager, - UIEventStream* uiEventStream, - bool useDelayForLatency, - TimerFactory* timerFactory, - EventController* eventController, - EntityCapsProvider* entityCapsProvider, - XMPPRoster* roster, - HistoryController* historyController, - MUCRegistry* mucRegistry, - HighlightManager* highlightManager, - ChatMessageParser* chatMessageParser, - bool isImpromptu, - AutoAcceptMUCInviteDecider* autoAcceptMUCInviteDecider) : - ChatControllerBase(self, stanzaChannel, iqRouter, chatWindowFactory, muc->getJID(), presenceOracle, avatarManager, useDelayForLatency, uiEventStream, eventController, timerFactory, entityCapsProvider, historyController, mucRegistry, highlightManager, chatMessageParser, autoAcceptMUCInviteDecider), muc_(muc), nick_(nick), desiredNick_(nick), password_(password), renameCounter_(0), isImpromptu_(isImpromptu), isImpromptuAlreadyConfigured_(false) { - parting_ = true; - joined_ = false; - lastWasPresence_ = false; - shouldJoinOnReconnect_ = true; - doneGettingHistory_ = false; - events_ = uiEventStream; - xmppRoster_ = roster; - - roster_ = new Roster(false, true); - completer_ = new TabComplete(); - chatWindow_->setRosterModel(roster_); - chatWindow_->setTabComplete(completer_); - chatWindow_->onClosed.connect(boost::bind(&MUCController::handleWindowClosed, this)); - chatWindow_->onOccupantSelectionChanged.connect(boost::bind(&MUCController::handleWindowOccupantSelectionChanged, this, _1)); - chatWindow_->onOccupantActionSelected.connect(boost::bind(&MUCController::handleActionRequestedOnOccupant, this, _1, _2)); - chatWindow_->onChangeSubjectRequest.connect(boost::bind(&MUCController::handleChangeSubjectRequest, this, _1)); - chatWindow_->onConfigureRequest.connect(boost::bind(&MUCController::handleConfigureRequest, this, _1)); - chatWindow_->onConfigurationFormCancelled.connect(boost::bind(&MUCController::handleConfigurationCancelled, this)); - chatWindow_->onDestroyRequest.connect(boost::bind(&MUCController::handleDestroyRoomRequest, this)); - chatWindow_->onInviteToChat.connect(boost::bind(&MUCController::handleInvitePersonToThisMUCRequest, this, _1)); - chatWindow_->onGetAffiliationsRequest.connect(boost::bind(&MUCController::handleGetAffiliationsRequest, this)); - chatWindow_->onChangeAffiliationsRequest.connect(boost::bind(&MUCController::handleChangeAffiliationsRequest, this, _1)); - muc_->onJoinComplete.connect(boost::bind(&MUCController::handleJoinComplete, this, _1)); - muc_->onJoinFailed.connect(boost::bind(&MUCController::handleJoinFailed, this, _1)); - muc_->onOccupantJoined.connect(boost::bind(&MUCController::handleOccupantJoined, this, _1)); - muc_->onOccupantPresenceChange.connect(boost::bind(&MUCController::handleOccupantPresenceChange, this, _1)); - muc_->onOccupantLeft.connect(boost::bind(&MUCController::handleOccupantLeft, this, _1, _2, _3)); - muc_->onOccupantRoleChanged.connect(boost::bind(&MUCController::handleOccupantRoleChanged, this, _1, _2, _3)); - muc_->onOccupantAffiliationChanged.connect(boost::bind(&MUCController::handleOccupantAffiliationChanged, this, _1, _2, _3)); - muc_->onRoleChangeFailed.connect(boost::bind(&MUCController::handleOccupantRoleChangeFailed, this, _1, _2, _3)); - muc_->onAffiliationListReceived.connect(boost::bind(&MUCController::handleAffiliationListReceived, this, _1, _2)); - muc_->onConfigurationFailed.connect(boost::bind(&MUCController::handleConfigurationFailed, this, _1)); - muc_->onConfigurationFormReceived.connect(boost::bind(&MUCController::handleConfigurationFormReceived, this, _1)); - highlighter_->setMode(Highlighter::MUCMode); - highlighter_->setNick(nick_); - if (timerFactory) { - loginCheckTimer_ = boost::shared_ptr<Timer>(timerFactory->createTimer(MUC_JOIN_WARNING_TIMEOUT_MILLISECONDS)); - loginCheckTimer_->onTick.connect(boost::bind(&MUCController::handleJoinTimeoutTick, this)); - loginCheckTimer_->start(); - } - if (isImpromptu) { - muc_->onUnlocked.connect(boost::bind(&MUCController::handleRoomUnlocked, this)); - chatWindow_->convertToMUC(true); - } else { - chatWindow_->convertToMUC(); - chatWindow_->setName(muc->getJID().getNode()); - } - setOnline(true); - if (avatarManager_ != NULL) { - avatarChangedConnection_ = (avatarManager_->onAvatarChanged.connect(boost::bind(&MUCController::handleAvatarChanged, this, _1))); - } - handleBareJIDCapsChanged(muc->getJID()); - eventStream_->onUIEvent.connect(boost::bind(&MUCController::handleUIEvent, this, _1)); + const JID& self, + MUC::ref muc, + const boost::optional<std::string>& password, + const std::string &nick, + StanzaChannel* stanzaChannel, + IQRouter* iqRouter, + ChatWindowFactory* chatWindowFactory, + NickResolver* nickResolver, + PresenceOracle* presenceOracle, + AvatarManager* avatarManager, + UIEventStream* uiEventStream, + bool useDelayForLatency, + TimerFactory* timerFactory, + EventController* eventController, + EntityCapsProvider* entityCapsProvider, + XMPPRoster* xmppRoster, + HistoryController* historyController, + MUCRegistry* mucRegistry, + HighlightManager* highlightManager, + ClientBlockListManager* clientBlockListManager, + std::shared_ptr<ChatMessageParser> chatMessageParser, + bool isImpromptu, + AutoAcceptMUCInviteDecider* autoAcceptMUCInviteDecider, + VCardManager* vcardManager, + MUCBookmarkManager* mucBookmarkManager, + SettingsProvider* settings, + Chattables& chattables) : + ChatControllerBase(self, stanzaChannel, iqRouter, chatWindowFactory, muc->getJID(), nickResolver, presenceOracle, avatarManager, useDelayForLatency, uiEventStream, eventController, entityCapsProvider, historyController, mucRegistry, highlightManager, chatMessageParser, autoAcceptMUCInviteDecider, settings, chattables), muc_(muc), nick_(nick), desiredNick_(nick), password_(password), renameCounter_(0), isImpromptu_(isImpromptu), isImpromptuAlreadyConfigured_(false), clientBlockListManager_(clientBlockListManager), mucBookmarkManager_(mucBookmarkManager) { + assert(avatarManager_); + + parting_ = true; + joined_ = false; + lastWasPresence_ = false; + shouldJoinOnReconnect_ = true; + doneGettingHistory_ = false; + xmppRoster_ = xmppRoster; + subject_ = ""; + isInitialJoin_ = true; + chatWindowTitle_ = ""; + + roster_ = std::make_unique<Roster>(false, true); + rosterVCardProvider_ = new RosterVCardProvider(roster_.get(), vcardManager, JID::WithResource); + completer_ = new TabComplete(); + chatWindow_->setRosterModel(roster_.get()); + chatWindow_->setTabComplete(completer_); + chatWindow_->onClosed.connect(boost::bind(&MUCController::handleWindowClosed, this)); + chatWindow_->onOccupantSelectionChanged.connect(boost::bind(&MUCController::handleWindowOccupantSelectionChanged, this, _1)); + chatWindow_->onOccupantActionSelected.connect(boost::bind(&MUCController::handleActionRequestedOnOccupant, this, _1, _2)); + chatWindow_->onChangeSubjectRequest.connect(boost::bind(&MUCController::handleChangeSubjectRequest, this, _1)); + chatWindow_->onBookmarkRequest.connect(boost::bind(&MUCController::handleBookmarkRequest, this)); + chatWindow_->onConfigureRequest.connect(boost::bind(&MUCController::handleConfigureRequest, this, _1)); + chatWindow_->onConfigurationFormCancelled.connect(boost::bind(&MUCController::handleConfigurationCancelled, this)); + chatWindow_->onDestroyRequest.connect(boost::bind(&MUCController::handleDestroyRoomRequest, this)); + chatWindow_->onInviteToChat.connect(boost::bind(&MUCController::handleInvitePersonToThisMUCRequest, this, _1)); + chatWindow_->onGetAffiliationsRequest.connect(boost::bind(&MUCController::handleGetAffiliationsRequest, this)); + chatWindow_->onChangeAffiliationsRequest.connect(boost::bind(&MUCController::handleChangeAffiliationsRequest, this, _1)); + chatWindow_->onUnblockUserRequest.connect(boost::bind(&MUCController::handleUnblockUserRequest, this)); + chatWindow_->onContinuationsBroken.connect(boost::bind(&MUCController::addChatSystemMessage, this)); + muc_->onJoinComplete.connect(boost::bind(&MUCController::handleJoinComplete, this, _1)); + muc_->onJoinFailed.connect(boost::bind(&MUCController::handleJoinFailed, this, _1)); + muc_->onOccupantJoined.connect(boost::bind(&MUCController::handleOccupantJoined, this, _1)); + muc_->onOccupantNicknameChanged.connect(boost::bind(&MUCController::handleOccupantNicknameChanged, this, _1, _2)); + muc_->onOccupantPresenceChange.connect(boost::bind(&MUCController::handleOccupantPresenceChange, this, _1)); + muc_->onOccupantLeft.connect(boost::bind(&MUCController::handleOccupantLeft, this, _1, _2, _3)); + muc_->onRoleChangeFailed.connect(boost::bind(&MUCController::handleOccupantRoleChangeFailed, this, _1, _2, _3)); + muc_->onAffiliationListReceived.connect(boost::bind(&MUCController::handleAffiliationListReceived, this, _1, _2)); + muc_->onConfigurationFailed.connect(boost::bind(&MUCController::handleConfigurationFailed, this, _1)); + muc_->onConfigurationFormReceived.connect(boost::bind(&MUCController::handleConfigurationFormReceived, this, _1)); + chatMessageParser_->setNick(nick_); + if (timerFactory && stanzaChannel_->isAvailable()) { + loginCheckTimer_ = std::shared_ptr<Timer>(timerFactory->createTimer(MUC_JOIN_WARNING_TIMEOUT_MILLISECONDS)); + loginCheckTimer_->onTick.connect(boost::bind(&MUCController::handleJoinTimeoutTick, this)); + loginCheckTimer_->start(); + } + else { + chatWindow_->addSystemMessage(chatMessageParser_->parseMessageBody(QT_TRANSLATE_NOOP("", "You are currently offline. You will enter this room when you are connected.")), ChatWindow::DefaultDirection); + } + if (isImpromptu) { + muc_->onUnlocked.connect(boost::bind(&MUCController::handleRoomUnlocked, this)); + chatWindow_->convertToMUC(ChatWindow::ImpromptuMUC); + } else { + muc_->onOccupantRoleChanged.connect(boost::bind(&MUCController::handleOccupantRoleChanged, this, _1, _2, _3)); + muc_->onOccupantAffiliationChanged.connect(boost::bind(&MUCController::handleOccupantAffiliationChanged, this, _1, _2, _3)); + chatWindow_->convertToMUC(ChatWindow::StandardMUC); + chatWindow_->setName(muc->getJID().getNode()); + } + if (stanzaChannel->isAvailable()) { + MUCController::setOnline(true); + } + avatarChangedConnection_ = (avatarManager_->onAvatarChanged.connect(boost::bind(&MUCController::handleAvatarChanged, this, _1))); + + MUCController::handleBareJIDCapsChanged(muc->getJID()); + eventStream_->onUIEvent.connect(boost::bind(&MUCController::handleUIEvent, this, _1)); + + + // setup handling of MUC bookmark changes + mucBookmarkManagerBookmarkAddedConnection_ = (mucBookmarkManager_->onBookmarkAdded.connect(boost::bind(&MUCController::handleMUCBookmarkAdded, this, _1))); + mucBookmarkManagerBookmarkRemovedConnection_ = (mucBookmarkManager_->onBookmarkRemoved.connect(boost::bind(&MUCController::handleMUCBookmarkRemoved, this, _1))); + + updateChatWindowBookmarkStatus(mucBookmarkManager_->lookupBookmark(muc->getJID())); } MUCController::~MUCController() { - eventStream_->onUIEvent.disconnect(boost::bind(&MUCController::handleUIEvent, this, _1)); - chatWindow_->setRosterModel(NULL); - delete roster_; - if (loginCheckTimer_) { - loginCheckTimer_->stop(); - } - chatWindow_->setTabComplete(NULL); - delete completer_; + eventStream_->onUIEvent.disconnect(boost::bind(&MUCController::handleUIEvent, this, _1)); + chatWindow_->setRosterModel(nullptr); + delete rosterVCardProvider_; + if (loginCheckTimer_) { + loginCheckTimer_->stop(); + } + chatWindow_->setTabComplete(nullptr); + delete completer_; } void MUCController::cancelReplaces() { - lastWasPresence_ = false; + lastWasPresence_ = false; } void MUCController::handleWindowOccupantSelectionChanged(ContactRosterItem* item) { - std::vector<ChatWindow::OccupantAction> actions; - - if (item) { - MUCOccupant::Affiliation affiliation = muc_->getOccupant(getNick()).getAffiliation(); - MUCOccupant::Role role = muc_->getOccupant(getNick()).getRole(); - if (role == MUCOccupant::Moderator) - { - if (affiliation == MUCOccupant::Admin || affiliation == MUCOccupant::Owner) { - actions.push_back(ChatWindow::Ban); - } - - actions.push_back(ChatWindow::Kick); - actions.push_back(ChatWindow::MakeModerator); - actions.push_back(ChatWindow::MakeParticipant); - actions.push_back(ChatWindow::MakeVisitor); - } - // Add contact is available only if the real JID is also available - if (muc_->getOccupant(item->getJID().getResource()).getRealJID()) { - actions.push_back(ChatWindow::AddContact); - } - actions.push_back(ChatWindow::ShowProfile); - } - chatWindow_->setAvailableOccupantActions(actions); + std::vector<ChatWindow::OccupantAction> actions; + + if (item) { + MUCOccupant::Affiliation affiliation = muc_->getOccupant(getNick()).getAffiliation(); + MUCOccupant::Role role = muc_->getOccupant(getNick()).getRole(); + if (role == MUCOccupant::Moderator && !isImpromptu_) + { + if (affiliation == MUCOccupant::Admin || affiliation == MUCOccupant::Owner) { + actions.push_back(ChatWindow::Ban); + } + + actions.push_back(ChatWindow::Kick); + actions.push_back(ChatWindow::MakeModerator); + actions.push_back(ChatWindow::MakeParticipant); + actions.push_back(ChatWindow::MakeVisitor); + } + // Add contact is available only if the real JID is also available + if (muc_->getOccupant(item->getJID().getResource()).getRealJID()) { + actions.push_back(ChatWindow::AddContact); + } + actions.push_back(ChatWindow::ShowProfile); + } + chatWindow_->setAvailableOccupantActions(actions); } void MUCController::handleActionRequestedOnOccupant(ChatWindow::OccupantAction action, ContactRosterItem* item) { - JID mucJID = item->getJID(); - MUCOccupant occupant = muc_->getOccupant(mucJID.getResource()); - JID realJID; - if (occupant.getRealJID()) { - realJID = occupant.getRealJID().get(); - } - switch (action) { - case ChatWindow::Kick: muc_->kickOccupant(mucJID);break; - case ChatWindow::Ban: muc_->changeAffiliation(realJID, MUCOccupant::Outcast);break; - case ChatWindow::MakeModerator: muc_->changeOccupantRole(mucJID, MUCOccupant::Moderator);break; - case ChatWindow::MakeParticipant: muc_->changeOccupantRole(mucJID, MUCOccupant::Participant);break; - case ChatWindow::MakeVisitor: muc_->changeOccupantRole(mucJID, MUCOccupant::Visitor);break; - case ChatWindow::AddContact: if (occupant.getRealJID()) events_->send(boost::make_shared<RequestAddUserDialogUIEvent>(realJID, occupant.getNick()));break; - case ChatWindow::ShowProfile: events_->send(boost::make_shared<ShowProfileForRosterItemUIEvent>(mucJID));break; - } + JID mucJID = item->getJID(); + MUCOccupant occupant = muc_->getOccupant(mucJID.getResource()); + JID realJID; + if (occupant.getRealJID()) { + realJID = occupant.getRealJID().get(); + } + switch (action) { + case ChatWindow::Kick: muc_->kickOccupant(mucJID);break; + case ChatWindow::Ban: muc_->changeAffiliation(realJID, MUCOccupant::Outcast);break; + case ChatWindow::MakeModerator: muc_->changeOccupantRole(mucJID, MUCOccupant::Moderator);break; + case ChatWindow::MakeParticipant: muc_->changeOccupantRole(mucJID, MUCOccupant::Participant);break; + case ChatWindow::MakeVisitor: muc_->changeOccupantRole(mucJID, MUCOccupant::Visitor);break; + case ChatWindow::AddContact: if (occupant.getRealJID()) eventStream_->send(std::make_shared<RequestAddUserDialogUIEvent>(realJID, occupant.getNick()));break; + case ChatWindow::ShowProfile: eventStream_->send(std::make_shared<ShowProfileForRosterItemUIEvent>(mucJID));break; + } } void MUCController::handleBareJIDCapsChanged(const JID& /*jid*/) { - ChatWindow::Tristate support = ChatWindow::Yes; - bool any = false; - foreach (const std::string& nick, currentOccupants_) { - DiscoInfo::ref disco = entityCapsProvider_->getCaps(toJID_.toBare().toString() + "/" + nick); - if (disco && disco->hasFeature(DiscoInfo::MessageCorrectionFeature)) { - any = true; - } else { - support = ChatWindow::Maybe; - } - } - if (!any) { - support = ChatWindow::No; - } - chatWindow_->setCorrectionEnabled(support); + Tristate support = Yes; + bool any = false; + for (const auto& nick : currentOccupants_) { + DiscoInfo::ref disco = entityCapsProvider_->getCapsCached(toJID_.toBare().toString() + "/" + nick); + if (disco && disco->hasFeature(DiscoInfo::MessageCorrectionFeature)) { + any = true; + } else { + support = Maybe; + } + } + if (!any) { + support = No; + } + chatWindow_->setCorrectionEnabled(support); } /** * Join the MUC if not already in it. */ void MUCController::rejoin() { - if (parting_) { - joined_ = false; - parting_ = false; - if (password_) { - muc_->setPassword(*password_); - } - //FIXME: check for received activity + if (parting_) { + joined_ = false; + parting_ = false; + if (password_) { + muc_->setPassword(*password_); + } + //FIXME: check for received activity #ifdef SWIFT_EXPERIMENTAL_HISTORY - if (lastActivity_ == boost::posix_time::not_a_date_time && historyController_) { - lastActivity_ = historyController_->getLastTimeStampFromMUC(selfJID_, toJID_); - } + if (lastActivity_ == boost::posix_time::not_a_date_time && historyController_) { + lastActivity_ = historyController_->getLastTimeStampFromMUC(selfJID_, toJID_); + } #endif - if (lastActivity_ == boost::posix_time::not_a_date_time) { - muc_->joinAs(nick_); - } - else { - muc_->joinWithContextSince(nick_, lastActivity_); - } - } + + requestSecurityMarking(); + + if (lastActivity_ == boost::posix_time::not_a_date_time) { + muc_->joinAs(nick_); + } + else { + muc_->joinWithContextSince(nick_, lastActivity_); + } + } } bool MUCController::isJoined() { - return joined_; + return joined_; } const std::string& MUCController::getNick() { - return nick_; + return nick_; +} + +const boost::optional<std::string> MUCController::getPassword() const { + return password_; } bool MUCController::isImpromptu() const { - return isImpromptu_; + return isImpromptu_; } std::map<std::string, JID> MUCController::getParticipantJIDs() const { - std::map<std::string, JID> participants; - typedef std::pair<std::string, MUCOccupant> MUCOccupantPair; - std::map<std::string, MUCOccupant> occupants = muc_->getOccupants(); - foreach(const MUCOccupantPair& occupant, occupants) { - if (occupant.first != nick_) { - participants[occupant.first] = occupant.second.getRealJID().is_initialized() ? occupant.second.getRealJID().get().toBare() : JID(); - } - } - return participants; + std::map<std::string, JID> participants; + std::map<std::string, MUCOccupant> occupants = muc_->getOccupants(); + for (const auto& occupant : occupants) { + if (occupant.first != nick_) { + participants[occupant.first] = occupant.second.getRealJID().is_initialized() ? occupant.second.getRealJID().get().toBare() : JID(); + } + } + return participants; } void MUCController::sendInvites(const std::vector<JID>& jids, const std::string& reason) const { - foreach (const JID& jid, jids) { - muc_->invitePerson(jid, reason, isImpromptu_); - } + for (const auto& jid : jids) { + muc_->invitePerson(jid, reason, isImpromptu_); + } } void MUCController::handleJoinTimeoutTick() { - receivedActivity(); - chatWindow_->addSystemMessage(chatMessageParser_->parseMessageBody(str(format(QT_TRANSLATE_NOOP("", "Room %1% is not responding. This operation may never complete.")) % toJID_.toString())), ChatWindow::DefaultDirection); + receivedActivity(); + chatWindow_->addSystemMessage(chatMessageParser_->parseMessageBody(str(format(QT_TRANSLATE_NOOP("", "Room %1% is not responding. This operation may never complete.")) % toJID_.toString())), ChatWindow::DefaultDirection); } void MUCController::receivedActivity() { - if (loginCheckTimer_) { - loginCheckTimer_->stop(); - } + if (loginCheckTimer_) { + loginCheckTimer_->stop(); + } } #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wswitch-enum" -void MUCController::handleJoinFailed(boost::shared_ptr<ErrorPayload> error) { - receivedActivity(); - std::string errorMessage = QT_TRANSLATE_NOOP("", "Unable to enter this room"); - std::string rejoinNick; - if (error) { - switch (error->getCondition()) { - case ErrorPayload::Conflict: - rejoinNick = nick_ + "_"; - errorMessage = str(format(QT_TRANSLATE_NOOP("", "Unable to enter this room as %1%, retrying as %2%")) % nick_ % rejoinNick); - break; - case ErrorPayload::JIDMalformed: - errorMessage += ": "; - errorMessage += QT_TRANSLATE_NOOP("", "No nickname specified"); - break; - case ErrorPayload::NotAuthorized: - errorMessage += ": "; - errorMessage += QT_TRANSLATE_NOOP("", "The correct room password is needed"); - break; - case ErrorPayload::RegistrationRequired: - errorMessage += ": "; - errorMessage += QT_TRANSLATE_NOOP("", "Only members may enter"); - break; - case ErrorPayload::Forbidden: - errorMessage += ": "; - errorMessage += QT_TRANSLATE_NOOP("", "You are banned from the room"); - break; - case ErrorPayload::ServiceUnavailable: - errorMessage += ": "; - errorMessage += QT_TRANSLATE_NOOP("", "The room is full"); - break; - case ErrorPayload::ItemNotFound: - errorMessage += ": "; - errorMessage += QT_TRANSLATE_NOOP("", "The room does not exist"); - break; - - default: break; - } - } - errorMessage = str(format(QT_TRANSLATE_NOOP("", "Couldn't join room: %1%.")) % errorMessage); - chatWindow_->addErrorMessage(chatMessageParser_->parseMessageBody(errorMessage)); - parting_ = true; - if (!rejoinNick.empty() && renameCounter_ < 10) { - renameCounter_++; - setNick(rejoinNick); - rejoin(); - } +void MUCController::handleJoinFailed(std::shared_ptr<ErrorPayload> error) { + receivedActivity(); + std::string errorMessage = QT_TRANSLATE_NOOP("", "Unable to enter this room"); + std::string rejoinNick; + if (error) { + switch (error->getCondition()) { + case ErrorPayload::Conflict: + rejoinNick = nick_ + "_"; + errorMessage = str(format(QT_TRANSLATE_NOOP("", "Unable to enter this room as %1%, retrying as %2%")) % nick_ % rejoinNick); + break; + case ErrorPayload::JIDMalformed: + errorMessage += ": "; + errorMessage += QT_TRANSLATE_NOOP("", "No nickname specified"); + break; + case ErrorPayload::NotAuthorized: + errorMessage += ": "; + errorMessage += QT_TRANSLATE_NOOP("", "The correct room password is needed"); + break; + case ErrorPayload::RegistrationRequired: + errorMessage += ": "; + errorMessage += QT_TRANSLATE_NOOP("", "Only members may enter"); + break; + case ErrorPayload::Forbidden: + errorMessage += ": "; + errorMessage += QT_TRANSLATE_NOOP("", "You are banned from the room"); + break; + case ErrorPayload::ServiceUnavailable: + errorMessage += ": "; + errorMessage += QT_TRANSLATE_NOOP("", "The room is full"); + break; + case ErrorPayload::ItemNotFound: + errorMessage += ": "; + errorMessage += QT_TRANSLATE_NOOP("", "The room does not exist"); + break; + + default: break; + } + } + errorMessage = str(format(QT_TRANSLATE_NOOP("", "Couldn't enter room: %1%.")) % errorMessage); + chatWindow_->addErrorMessage(chatMessageParser_->parseMessageBody(errorMessage)); + parting_ = true; + if (!rejoinNick.empty() && renameCounter_ < 10) { + renameCounter_++; + setNick(rejoinNick); + rejoin(); + } } #pragma clang diagnostic pop void MUCController::handleJoinComplete(const std::string& nick) { - receivedActivity(); - renameCounter_ = 0; - joined_ = true; - std::string joinMessage; - if (isImpromptu_) { - joinMessage = str(format(QT_TRANSLATE_NOOP("", "You have entered chat as %1%.")) % nick); - } else { - joinMessage = str(format(QT_TRANSLATE_NOOP("", "You have entered room %1% as %2%.")) % toJID_.toString() % nick); - } - setNick(nick); - chatWindow_->addSystemMessage(chatMessageParser_->parseMessageBody(joinMessage), ChatWindow::DefaultDirection); + receivedActivity(); + renameCounter_ = 0; + joined_ = true; + if (isImpromptu_) { + lastStartMessage_ = str(format(QT_TRANSLATE_NOOP("", "You have joined the chat as %1%.")) % nick); + } + else { + lastStartMessage_ = str(format(QT_TRANSLATE_NOOP("", "You have entered room %1% as %2%.")) % toJID_.toString() % nick); + } + setNick(nick); + chatWindow_->replaceSystemMessage(chatMessageParser_->parseMessageBody(lastStartMessage_, "", true), lastJoinMessageUID_, ChatWindow::UpdateTimestamp); + lastJoinMessageUID_ = ""; #ifdef SWIFT_EXPERIMENTAL_HISTORY - addRecentLogs(); + addRecentLogs(); #endif - clearPresenceQueue(); - shouldJoinOnReconnect_ = true; - setEnabled(true); - MUCOccupant occupant = muc_->getOccupant(nick); - setAvailableRoomActions(occupant.getAffiliation(), occupant.getRole()); - onUserJoined(); - - if (isImpromptu_) { - setImpromptuWindowTitle(); - } + clearPresenceQueue(); + shouldJoinOnReconnect_ = true; + setEnabled(true); + if (isImpromptu_) { + setAvailableRoomActions(MUCOccupant::NoAffiliation, MUCOccupant::Participant); + } else { + MUCOccupant occupant = muc_->getOccupant(nick); + setAvailableRoomActions(occupant.getAffiliation(), occupant.getRole()); + } + onUserJoined(); } void MUCController::handleAvatarChanged(const JID& jid) { - if (parting_ || !jid.equals(toJID_, JID::WithoutResource)) { - return; - } - roster_->applyOnItems(SetAvatar(jid, avatarManager_->getAvatarPath(jid), JID::WithResource)); + if (parting_ || !jid.equals(toJID_, JID::WithoutResource)) { + return; + } + roster_->applyOnItems(SetAvatar(jid, avatarManager_->getAvatarPath(jid), JID::WithResource)); } void MUCController::handleWindowClosed() { - parting_ = true; - shouldJoinOnReconnect_ = false; - muc_->part(); - onUserLeft(); + parting_ = true; + shouldJoinOnReconnect_ = false; + muc_->part(); + onUserLeft(); } void MUCController::handleOccupantJoined(const MUCOccupant& occupant) { - if (nick_ != occupant.getNick()) { - completer_->addWord(occupant.getNick()); - } - receivedActivity(); - JID jid(nickToJID(occupant.getNick())); - JID realJID; - if (occupant.getRealJID()) { - realJID = occupant.getRealJID().get(); - } - currentOccupants_.insert(occupant.getNick()); - NickJoinPart event(occupant.getNick(), Join); - appendToJoinParts(joinParts_, event); - std::string groupName(roleToGroupName(occupant.getRole())); - roster_->addContact(jid, realJID, occupant.getNick(), groupName, avatarManager_->getAvatarPath(jid)); - roster_->getGroup(groupName)->setManualSort(roleToSortName(occupant.getRole())); - if (joined_) { - std::string joinString; - MUCOccupant::Role role = occupant.getRole(); - if (role != MUCOccupant::NoRole && role != MUCOccupant::Participant) { - joinString = str(format(QT_TRANSLATE_NOOP("", "%1% has entered the %3% as a %2%.")) % occupant.getNick() % roleToFriendlyName(role) % (isImpromptu_ ? QT_TRANSLATE_NOOP("", "chat") : QT_TRANSLATE_NOOP("", "room"))); - } - else { - joinString = str(format(QT_TRANSLATE_NOOP("", "%1% has entered the %2%.")) % occupant.getNick() % (isImpromptu_ ? QT_TRANSLATE_NOOP("", "chat") : QT_TRANSLATE_NOOP("", "room"))); - } - if (shouldUpdateJoinParts()) { - updateJoinParts(); - } else { - addPresenceMessage(joinString); - } - - if (isImpromptu_) { - setImpromptuWindowTitle(); - onActivity(""); - } - } - if (avatarManager_ != NULL) { - handleAvatarChanged(jid); - } + if (nick_ != occupant.getNick()) { + completer_->addWord(occupant.getNick()); + } + receivedActivity(); + JID jid(nickToJID(occupant.getNick())); + JID realJID; + if (occupant.getRealJID()) { + realJID = occupant.getRealJID().get(); + } + currentOccupants_.insert(occupant.getNick()); + NickJoinPart event(occupant.getNick(), Join); + appendToJoinParts(joinParts_, event); + MUCOccupant::Role role = MUCOccupant::Participant; + MUCOccupant::Affiliation affiliation = MUCOccupant::NoAffiliation; + if (!isImpromptu_) { + role = occupant.getRole(); + affiliation = occupant.getAffiliation(); + } + std::string groupName(roleToGroupName(role)); + roster_->addContact(jid, realJID, occupant.getNick(), groupName, avatarManager_->getAvatarPath(jid)); + roster_->applyOnItems(SetMUC(jid, role, affiliation)); + roster_->getGroup(groupName)->setManualSort(roleToSortName(role)); + if (joined_) { + std::string joinString; + if (role != MUCOccupant::NoRole && role != MUCOccupant::Participant) { + joinString = str(format(QT_TRANSLATE_NOOP("", "%1% has entered the %3% as a %2%.")) % occupant.getNick() % roleToFriendlyName(role) % (isImpromptu_ ? QT_TRANSLATE_NOOP("", "chat") : QT_TRANSLATE_NOOP("", "room"))); + } + else { + joinString = str(format(QT_TRANSLATE_NOOP("", "%1% has entered the %2%.")) % occupant.getNick() % (isImpromptu_ ? QT_TRANSLATE_NOOP("", "chat") : QT_TRANSLATE_NOOP("", "room"))); + } + if (shouldUpdateJoinParts()) { + updateJoinParts(); + } else { + addPresenceMessage(joinString); + } + + if (isImpromptu_) { + onActivity(""); + } + } + handleAvatarChanged(jid); } void MUCController::addPresenceMessage(const std::string& message) { - lastWasPresence_ = true; - chatWindow_->addPresenceMessage(chatMessageParser_->parseMessageBody(message), ChatWindow::DefaultDirection); + lastWasPresence_ = true; + chatWindow_->addPresenceMessage(chatMessageParser_->parseMessageBody(message), ChatWindow::DefaultDirection); } void MUCController::setAvailableRoomActions(const MUCOccupant::Affiliation& affiliation, const MUCOccupant::Role& role) { - std::vector<ChatWindow::RoomAction> actions; - - if (role <= MUCOccupant::Participant) { - actions.push_back(ChatWindow::ChangeSubject); - } - if (affiliation == MUCOccupant::Owner) { - actions.push_back(ChatWindow::Configure); - } - if (affiliation <= MUCOccupant::Admin) { - actions.push_back(ChatWindow::Affiliations); - } - if (affiliation == MUCOccupant::Owner) { - actions.push_back(ChatWindow::Destroy); - } - if (role <= MUCOccupant::Visitor) { - actions.push_back(ChatWindow::Invite); - } - chatWindow_->setAvailableRoomActions(actions); + std::vector<ChatWindow::RoomAction> actions; + + if (role <= MUCOccupant::Participant) { + actions.push_back(ChatWindow::ChangeSubject); + } + if (affiliation == MUCOccupant::Owner) { + actions.push_back(ChatWindow::Configure); + } + if (affiliation <= MUCOccupant::Admin) { + actions.push_back(ChatWindow::Affiliations); + } + if (affiliation == MUCOccupant::Owner) { + actions.push_back(ChatWindow::Destroy); + } + if (role <= MUCOccupant::Visitor) { + actions.push_back(ChatWindow::Invite); + } + actions.push_back(ChatWindow::Leave); + chatWindow_->setAvailableRoomActions(actions); } void MUCController::clearPresenceQueue() { - lastWasPresence_ = false; - joinParts_.clear(); + lastWasPresence_ = false; + joinParts_.clear(); } std::string MUCController::roleToFriendlyName(MUCOccupant::Role role) { - switch (role) { - case MUCOccupant::Moderator: return QT_TRANSLATE_NOOP("", "moderator"); - case MUCOccupant::Participant: return QT_TRANSLATE_NOOP("", "participant"); - case MUCOccupant::Visitor: return QT_TRANSLATE_NOOP("", "visitor"); - case MUCOccupant::NoRole: return ""; - } - assert(false); - return ""; + switch (role) { + case MUCOccupant::Moderator: return QT_TRANSLATE_NOOP("", "moderator"); + case MUCOccupant::Participant: return QT_TRANSLATE_NOOP("", "participant"); + case MUCOccupant::Visitor: return QT_TRANSLATE_NOOP("", "visitor"); + case MUCOccupant::NoRole: return ""; + } + assert(false); + return ""; } std::string MUCController::roleToSortName(MUCOccupant::Role role) { - switch (role) { - case MUCOccupant::Moderator: return "1"; - case MUCOccupant::Participant: return "2"; - case MUCOccupant::Visitor: return "3"; - case MUCOccupant::NoRole: return "4"; - } - assert(false); - return "5"; + switch (role) { + case MUCOccupant::Moderator: return "1"; + case MUCOccupant::Participant: return "2"; + case MUCOccupant::Visitor: return "3"; + case MUCOccupant::NoRole: return "4"; + } + assert(false); + return "5"; } JID MUCController::nickToJID(const std::string& nick) { - return JID(toJID_.getNode(), toJID_.getDomain(), nick); -} - -bool MUCController::messageTargetsMe(boost::shared_ptr<Message> message) { - std::string stringRegexp(".*\\b" + boost::to_lower_copy(nick_) + "\\b.*"); - boost::regex myRegexp(stringRegexp); - return boost::regex_match(boost::to_lower_copy(message->getBody()), myRegexp); -} - -void MUCController::preHandleIncomingMessage(boost::shared_ptr<MessageEvent> messageEvent) { - if (messageEvent->getStanza()->getType() == Message::Groupchat) { - lastActivity_ = boost::posix_time::microsec_clock::universal_time(); - } - clearPresenceQueue(); - boost::shared_ptr<Message> message = messageEvent->getStanza(); - if (joined_ && messageEvent->getStanza()->getFrom().getResource() != nick_ && messageTargetsMe(message) && !message->getPayload<Delay>() && messageEvent->isReadable() -) { - chatWindow_->flash(); - } - else { - messageEvent->setTargetsMe(false); - } - if (joined_) { - std::string nick = message->getFrom().getResource(); - if (nick != nick_ && currentOccupants_.find(nick) != currentOccupants_.end()) { - completer_->addWord(nick); - } - } - /*Buggy implementations never send the status code, so use an incoming message as a hint that joining's done (e.g. the old ejabberd on psi-im.org).*/ - receivedActivity(); - joined_ = true; - - if (message->hasSubject() && message->getBody().empty()) { - chatWindow_->addSystemMessage(chatMessageParser_->parseMessageBody(str(format(QT_TRANSLATE_NOOP("", "The room subject is now: %1%")) % message->getSubject())), ChatWindow::DefaultDirection);; - chatWindow_->setSubject(message->getSubject()); - doneGettingHistory_ = true; - } - - if (!doneGettingHistory_ && !message->getPayload<Delay>()) { - doneGettingHistory_ = true; - } - - if (!doneGettingHistory_) { - checkDuplicates(message); - messageEvent->conclude(); - } -} - -void MUCController::postHandleIncomingMessage(boost::shared_ptr<MessageEvent> messageEvent, const HighlightAction& highlight) { - boost::shared_ptr<Message> message = messageEvent->getStanza(); - if (joined_ && messageEvent->getStanza()->getFrom().getResource() != nick_ && messageTargetsMe(message) && !message->getPayload<Delay>()) { - eventController_->handleIncomingEvent(messageEvent); - if (!messageEvent->getConcluded()) { - highlighter_->handleHighlightAction(highlight); - } - } + return muc_->getJID().withResource(nick); +} + +bool MUCController::messageTargetsMe(std::shared_ptr<Message> message) { + std::string stringRegexp(".*\\b" + boost::to_lower_copy(nick_) + "\\b.*"); + boost::regex myRegexp(stringRegexp); + return boost::regex_match(boost::to_lower_copy(message->getBody().get_value_or("")), myRegexp); +} + +void MUCController::preHandleIncomingMessage(std::shared_ptr<MessageEvent> messageEvent) { + if (messageEvent->getStanza()->getType() == Message::Groupchat) { + lastActivity_ = boost::posix_time::microsec_clock::universal_time(); + } + std::shared_ptr<Message> message = messageEvent->getStanza(); + + // This avoids clearing join/leave queue for chat state notification messages + // which are not readable (e.g. have no body content). + if (!(!messageEvent->isReadable() && message->getPayload<ChatState>())) { + clearPresenceQueue(); + } + + if (joined_ && messageEvent->getStanza()->getFrom().getResource() != nick_ && messageTargetsMe(message) && !message->getPayload<Delay>() && messageEvent->isReadable()) { + chatWindow_->flash(); + } + else { + messageEvent->setTargetsMe(false); + } + if (messageEvent->isReadable() && isImpromptu_) { + chatWindow_->flash(); /* behave like a regular char*/ + } + if (joined_) { + std::string nick = message->getFrom().getResource(); + if (nick != nick_ && currentOccupants_.find(nick) != currentOccupants_.end()) { + completer_->addWord(nick); + } + } + /*Buggy implementations never send the status code, so use an incoming message as a hint that joining's done (e.g. the old ejabberd on psi-im.org).*/ + receivedActivity(); + joined_ = true; + + if (message->hasSubject() && !message->getPayload<Body>() && !message->getPayload<Thread>()) { + if (!isInitialJoin_) { + displaySubjectIfChanged(message->getSubject()); + } + isInitialJoin_ = false; + chatWindow_->setSubject(message->getSubject()); + doneGettingHistory_ = true; + subject_ = message->getSubject(); + } + + if (!doneGettingHistory_ && !message->getPayload<Delay>()) { + doneGettingHistory_ = true; + } + + if (!doneGettingHistory_) { + checkDuplicates(message); + messageEvent->conclude(); + } +} + +void MUCController::addMessageHandleIncomingMessage(const JID& from, const ChatWindow::ChatMessage& message, const std::string& messageID, bool senderIsSelf, std::shared_ptr<SecurityLabel> label, const boost::posix_time::ptime& time) { + if (from.isBare()) { + chatWindow_->addSystemMessage(message, ChatWindow::DefaultDirection); + } + else { + lastMessagesIDs_[from] = {messageID, addMessage(message, senderDisplayNameFromMessage(from), senderIsSelf, label, avatarManager_->getAvatarPath(from), time)}; + } +} + +void MUCController::handleIncomingReplaceMessage(const JID& from, const ChatWindow::ChatMessage& message, const std::string& messageID, const std::string& /*idToReplace*/, bool senderIsSelf, std::shared_ptr<SecurityLabel> label, const boost::posix_time::ptime& timeStamp) { + auto lastMessage = lastMessagesIDs_.find(from); + if (lastMessage != lastMessagesIDs_.end()) { + replaceMessage(message, lastMessage->second.idInWindow, timeStamp); + } + else { + addMessageHandleIncomingMessage(from, message, messageID, senderIsSelf, label, timeStamp); + } +} + +void MUCController::postHandleIncomingMessage(std::shared_ptr<MessageEvent> messageEvent, const ChatWindow::ChatMessage& chatMessage) { + std::shared_ptr<Message> message = messageEvent->getStanza(); + if (joined_ && messageEvent->getStanza()->getFrom().getResource() != nick_ && !message->getPayload<Delay>()) { + highlighter_->handleSystemNotifications(chatMessage, messageEvent); + if (!messageEvent->getNotifications().empty()) { + eventController_->handleIncomingEvent(messageEvent); + } + if (!messageEvent->getConcluded()) { + highlighter_->handleSoundNotifications(chatMessage); + } + } } void MUCController::handleOccupantRoleChanged(const std::string& nick, const MUCOccupant& occupant, const MUCOccupant::Role& oldRole) { - clearPresenceQueue(); - receivedActivity(); - JID jid(nickToJID(nick)); - roster_->removeContactFromGroup(jid, roleToGroupName(oldRole)); - JID realJID; - if (occupant.getRealJID()) { - realJID = occupant.getRealJID().get(); - } - std::string group(roleToGroupName(occupant.getRole())); - roster_->addContact(jid, realJID, nick, group, avatarManager_->getAvatarPath(jid)); - roster_->getGroup(group)->setManualSort(roleToSortName(occupant.getRole())); - chatWindow_->addSystemMessage(chatMessageParser_->parseMessageBody(str(format(QT_TRANSLATE_NOOP("", "%1% is now a %2%")) % nick % roleToFriendlyName(occupant.getRole()))), ChatWindow::DefaultDirection); - if (nick == nick_) { - setAvailableRoomActions(occupant.getAffiliation(), occupant.getRole()); - } + clearPresenceQueue(); + receivedActivity(); + JID jid(nickToJID(nick)); + roster_->removeContactFromGroup(jid, roleToGroupName(oldRole)); + JID realJID; + if (occupant.getRealJID()) { + realJID = occupant.getRealJID().get(); + } + std::string group(roleToGroupName(occupant.getRole())); + roster_->addContact(jid, realJID, nick, group, avatarManager_->getAvatarPath(jid)); + roster_->getGroup(group)->setManualSort(roleToSortName(occupant.getRole())); + roster_->applyOnItems(SetMUC(jid, occupant.getRole(), occupant.getAffiliation())); + chatWindow_->addSystemMessage(chatMessageParser_->parseMessageBody(str(format(QT_TRANSLATE_NOOP("", "%1% is now a %2%")) % nick % roleToFriendlyName(occupant.getRole()))), ChatWindow::DefaultDirection); + if (nick == nick_) { + setAvailableRoomActions(occupant.getAffiliation(), occupant.getRole()); + } } void MUCController::handleOccupantAffiliationChanged(const std::string& nick, const MUCOccupant::Affiliation& affiliation, const MUCOccupant::Affiliation& /*oldAffiliation*/) { - if (nick == nick_) { - setAvailableRoomActions(affiliation, muc_->getOccupant(nick_).getRole()); - } + if (nick == nick_) { + setAvailableRoomActions(affiliation, muc_->getOccupant(nick_).getRole()); + } + JID jid(nickToJID(nick)); + MUCOccupant occupant = muc_->getOccupant(nick); + roster_->applyOnItems(SetMUC(jid, occupant.getRole(), affiliation)); } std::string MUCController::roleToGroupName(MUCOccupant::Role role) { - std::string result; - switch (role) { - case MUCOccupant::Moderator: result = QT_TRANSLATE_NOOP("", "Moderators"); break; - case MUCOccupant::Participant: result = QT_TRANSLATE_NOOP("", "Participants"); break; - case MUCOccupant::Visitor: result = QT_TRANSLATE_NOOP("", "Visitors"); break; - case MUCOccupant::NoRole: result = QT_TRANSLATE_NOOP("", "Occupants"); break; - } - return result; + std::string result; + switch (role) { + case MUCOccupant::Moderator: result = QT_TRANSLATE_NOOP("", "Moderators"); break; + case MUCOccupant::Participant: result = QT_TRANSLATE_NOOP("", "Participants"); break; + case MUCOccupant::Visitor: result = QT_TRANSLATE_NOOP("", "Visitors"); break; + case MUCOccupant::NoRole: result = QT_TRANSLATE_NOOP("", "Occupants"); break; + } + return result; } void MUCController::setOnline(bool online) { - ChatControllerBase::setOnline(online); - if (!online) { - muc_->part(); - parting_ = true; - processUserPart(); - } else { - if (shouldJoinOnReconnect_) { - renameCounter_ = 0; - if (isImpromptu_) { - chatWindow_->addSystemMessage(chatMessageParser_->parseMessageBody(QT_TRANSLATE_NOOP("", "Trying to enter chat")), ChatWindow::DefaultDirection); - } else { - chatWindow_->addSystemMessage(chatMessageParser_->parseMessageBody(str(format(QT_TRANSLATE_NOOP("", "Trying to enter room %1%")) % toJID_.toString())), ChatWindow::DefaultDirection); - } - if (loginCheckTimer_) { - loginCheckTimer_->start(); - } - setNick(desiredNick_); - rejoin(); - } - } + ChatControllerBase::setOnline(online); + if (!online) { + muc_->part(); + parting_ = true; + processUserPart(); + } else { + if (shouldJoinOnReconnect_) { + renameCounter_ = 0; + std::shared_ptr<BlockList> blockList = clientBlockListManager_->getBlockList(); + if (blockList && blockList->isBlocked(muc_->getJID())) { + handleBlockingStateChanged(); + lastStartMessage_ = QT_TRANSLATE_NOOP("", "You've blocked this room. To enter the room, first unblock it using the cog menu and try again"); + lastJoinMessageUID_ = chatWindow_->addSystemMessage(chatMessageParser_->parseMessageBody(lastStartMessage_ ), ChatWindow::DefaultDirection); + } + else { + if (isImpromptu_) { + lastStartMessage_ = QT_TRANSLATE_NOOP("", "Trying to join chat"); + lastJoinMessageUID_ = chatWindow_->addSystemMessage(chatMessageParser_->parseMessageBody(lastStartMessage_), ChatWindow::DefaultDirection); + } else { + lastStartMessage_ = str(format(QT_TRANSLATE_NOOP("", "Trying to enter room %1%")) % toJID_.toString()); + lastJoinMessageUID_ = chatWindow_->addSystemMessage(chatMessageParser_->parseMessageBody(lastStartMessage_), ChatWindow::DefaultDirection); + } + if (loginCheckTimer_) { + loginCheckTimer_->start(); + } + setNick(desiredNick_); + rejoin(); + } + } + } } void MUCController::processUserPart() { - roster_->removeAll(); - /* handleUserLeft won't throw a part back up unless this is called - when it doesn't yet know we've left - which only happens on - disconnect, so call with disconnect here so if the signal does - bubble back up, it'll be with the right type.*/ - muc_->handleUserLeft(MUC::Disconnect); - setEnabled(false); + roster_->removeAll(); + /* handleUserLeft won't throw a part back up unless this is called + when it doesn't yet know we've left - which only happens on + disconnect, so call with disconnect here so if the signal does + bubble back up, it'll be with the right type.*/ + muc_->handleUserLeft(MUC::Disconnect); + setEnabled(false); } bool MUCController::shouldUpdateJoinParts() { - return lastWasPresence_; + return lastWasPresence_; } void MUCController::handleOccupantLeft(const MUCOccupant& occupant, MUC::LeavingType type, const std::string& reason) { - NickJoinPart event(occupant.getNick(), Part); - appendToJoinParts(joinParts_, event); - currentOccupants_.erase(occupant.getNick()); - completer_->removeWord(occupant.getNick()); - std::string partMessage; - bool clearAfter = false; - if (occupant.getNick() != nick_) { - std::string partType; - switch (type) { - case MUC::LeaveKick: clearPresenceQueue(); clearAfter = true; partType = " (kicked)"; break; - case MUC::LeaveBan: clearPresenceQueue(); clearAfter = true; partType = " (banned)"; break; - case MUC::LeaveNotMember: clearPresenceQueue(); clearAfter = true; partType = " (no longer a member)"; break; - case MUC::LeaveDestroy: - case MUC::Disconnect: - case MUC::LeavePart: break; - } - partMessage = str(format(QT_TRANSLATE_NOOP("", "%1% has left the room%2%")) % occupant.getNick() % partType); - } - else { - switch (type) { - case MUC::LeaveKick: clearPresenceQueue(); clearAfter = true; partMessage = QT_TRANSLATE_NOOP("", "You have been kicked out of the room"); break; - case MUC::LeaveBan: clearPresenceQueue(); clearAfter = true; partMessage = QT_TRANSLATE_NOOP("", "You have been banned from the room"); break; - case MUC::LeaveNotMember: clearPresenceQueue(); clearAfter = true; partMessage = QT_TRANSLATE_NOOP("", "You are no longer a member of the room and have been removed"); break; - case MUC::LeaveDestroy: clearPresenceQueue(); clearAfter = true; partMessage = QT_TRANSLATE_NOOP("", "The room has been destroyed"); break; - case MUC::Disconnect: - case MUC::LeavePart: partMessage = QT_TRANSLATE_NOOP("", "You have left the room"); - } - } - if (!reason.empty()) { - partMessage += " (" + reason + ")"; - } - partMessage += "."; - - if (occupant.getNick() != nick_) { - if (shouldUpdateJoinParts()) { - updateJoinParts(); - } else { - addPresenceMessage(partMessage); - } - roster_->removeContact(JID(toJID_.getNode(), toJID_.getDomain(), occupant.getNick())); - } else { - addPresenceMessage(partMessage); - parting_ = true; - processUserPart(); - } - if (clearAfter) { - clearPresenceQueue(); - } - - if (isImpromptu_) { - setImpromptuWindowTitle(); - } -} - -void MUCController::handleOccupantPresenceChange(boost::shared_ptr<Presence> presence) { - receivedActivity(); - roster_->applyOnItems(SetPresence(presence, JID::WithResource)); -} - -bool MUCController::isIncomingMessageFromMe(boost::shared_ptr<Message> message) { - JID from = message->getFrom(); - return nick_ == from.getResource(); + NickJoinPart event(occupant.getNick(), Part); + appendToJoinParts(joinParts_, event); + currentOccupants_.erase(occupant.getNick()); + completer_->removeWord(occupant.getNick()); + std::string partMessage; + bool clearAfter = false; + if (occupant.getNick() != nick_) { + std::string partType; + switch (type) { + case MUC::LeaveKick: clearPresenceQueue(); clearAfter = true; partType = " (kicked)"; break; + case MUC::LeaveBan: clearPresenceQueue(); clearAfter = true; partType = " (banned)"; break; + case MUC::LeaveNotMember: clearPresenceQueue(); clearAfter = true; partType = " (no longer a member)"; break; + case MUC::LeaveDestroy: + case MUC::Disconnect: + case MUC::LeavePart: break; + } + if (isImpromptu_) { + partMessage = str(format(QT_TRANSLATE_NOOP("", "%1% has left the chat%2%")) % occupant.getNick() % partType); + } else { + partMessage = str(format(QT_TRANSLATE_NOOP("", "%1% has left the room%2%")) % occupant.getNick() % partType); + } + } + else if (isImpromptu_) { + switch (type) { + case MUC::LeaveKick: + case MUC::LeaveBan: clearPresenceQueue(); clearAfter = true; partMessage = QT_TRANSLATE_NOOP("", "You have been removed from this chat"); break; + case MUC::LeaveNotMember: clearPresenceQueue(); clearAfter = true; partMessage = QT_TRANSLATE_NOOP("", "You have been removed from this chat"); break; + case MUC::LeaveDestroy: clearPresenceQueue(); clearAfter = true; partMessage = QT_TRANSLATE_NOOP("", "This chat has ended"); break; + case MUC::Disconnect: + case MUC::LeavePart: partMessage = QT_TRANSLATE_NOOP("", "You have left the chat"); + } + } + else { + switch (type) { + case MUC::LeaveKick: clearPresenceQueue(); clearAfter = true; partMessage = QT_TRANSLATE_NOOP("", "You have been kicked out of the room"); break; + case MUC::LeaveBan: clearPresenceQueue(); clearAfter = true; partMessage = QT_TRANSLATE_NOOP("", "You have been banned from the room"); break; + case MUC::LeaveNotMember: clearPresenceQueue(); clearAfter = true; partMessage = QT_TRANSLATE_NOOP("", "You are no longer a member of the room and have been removed"); break; + case MUC::LeaveDestroy: clearPresenceQueue(); clearAfter = true; partMessage = QT_TRANSLATE_NOOP("", "The room has been destroyed"); break; + case MUC::Disconnect: + case MUC::LeavePart: partMessage = QT_TRANSLATE_NOOP("", "You have left the room"); + } + } + if (!reason.empty()) { + partMessage += " (" + reason + ")"; + } + partMessage += "."; + + if (occupant.getNick() != nick_) { + if (shouldUpdateJoinParts()) { + updateJoinParts(); + } else { + addPresenceMessage(partMessage); + } + roster_->removeContact(JID(toJID_.getNode(), toJID_.getDomain(), occupant.getNick())); + } else { + addPresenceMessage(partMessage); + parting_ = true; + processUserPart(); + } + if (clearAfter) { + clearPresenceQueue(); + } +} + +void MUCController::handleOccupantNicknameChanged(const std::string& oldNickname, const std::string& newNickname) { + addPresenceMessage(generateNicknameChangeString(oldNickname, newNickname)); + JID oldJID = muc_->getJID().withResource(oldNickname); + JID newJID = muc_->getJID().withResource(newNickname); + + // adjust occupants + currentOccupants_.erase(oldNickname); + currentOccupants_.insert(newNickname); + + // adjust completer + completer_->removeWord(oldNickname); + completer_->addWord(newNickname); + + // update contact + roster_->removeContact(oldJID); + auto it = currentOccupants_.find(newNickname); + if (it != currentOccupants_.end()) { + roster_->removeContact(newJID); + } + + MUCOccupant occupant = muc_->getOccupant(newNickname); + + JID realJID; + if (occupant.getRealJID()) { + realJID = occupant.getRealJID().get(); + } + MUCOccupant::Role role = MUCOccupant::Participant; + MUCOccupant::Affiliation affiliation = MUCOccupant::NoAffiliation; + if (!isImpromptu_) { + role = occupant.getRole(); + affiliation = occupant.getAffiliation(); + } + std::string groupName(roleToGroupName(role)); + roster_->addContact(newJID, realJID, newNickname, groupName, avatarManager_->getAvatarPath(newJID)); + roster_->applyOnItems(SetMUC(newJID, role, affiliation)); + handleAvatarChanged(newJID); + + clearPresenceQueue(); + onUserNicknameChanged(oldNickname, newNickname); +} + +void MUCController::handleOccupantPresenceChange(std::shared_ptr<Presence> presence) { + receivedActivity(); + roster_->applyOnItems(SetPresence(presence, JID::WithResource)); +} + +bool MUCController::isIncomingMessageFromMe(std::shared_ptr<Message> message) { + JID from = message->getFrom(); + return nick_ == from.getResource(); +} + +std::string MUCController::senderHighlightNameFromMessage(const JID& from) { + return from.getResource(); } std::string MUCController::senderDisplayNameFromMessage(const JID& from) { - return from.getResource(); + return from.getResource(); } -void MUCController::preSendMessageRequest(boost::shared_ptr<Message> message) { - message->setType(Swift::Message::Groupchat); +void MUCController::preSendMessageRequest(std::shared_ptr<Message> message) { + message->setType(Swift::Message::Groupchat); } -boost::optional<boost::posix_time::ptime> MUCController::getMessageTimestamp(boost::shared_ptr<Message> message) const { - return message->getTimestampFrom(toJID_); +boost::optional<boost::posix_time::ptime> MUCController::getMessageTimestamp(std::shared_ptr<Message> message) const { + return message->getTimestampFrom(toJID_); } void MUCController::updateJoinParts() { - chatWindow_->replaceLastMessage(chatMessageParser_->parseMessageBody(generateJoinPartString(joinParts_, isImpromptu()))); + chatWindow_->replaceLastMessage(chatMessageParser_->parseMessageBody(generateJoinPartString(joinParts_, isImpromptu())), ChatWindow::UpdateTimestamp); } void MUCController::appendToJoinParts(std::vector<NickJoinPart>& joinParts, const NickJoinPart& newEvent) { - std::vector<NickJoinPart>::iterator it = joinParts.begin(); - bool matched = false; - for (; it != joinParts.end(); ++it) { - if ((*it).nick == newEvent.nick) { - matched = true; - JoinPart type = (*it).type; - switch (newEvent.type) { - case Join: type = (type == Part) ? PartThenJoin : Join; break; - case Part: type = (type == Join) ? JoinThenPart : Part; break; - case PartThenJoin: break; - case JoinThenPart: break; - } - (*it).type = type; - break; - } - } - if (!matched) { - joinParts.push_back(newEvent); - } + std::vector<NickJoinPart>::iterator it = joinParts.begin(); + bool matched = false; + for (; it != joinParts.end(); ++it) { + if ((*it).nick == newEvent.nick) { + matched = true; + JoinPart type = (*it).type; + switch (newEvent.type) { + case Join: type = (type == Part) ? PartThenJoin : Join; break; + case Part: type = (type == Join) ? JoinThenPart : Part; break; + case PartThenJoin: break; + case JoinThenPart: break; + } + (*it).type = type; + break; + } + } + if (!matched) { + joinParts.push_back(newEvent); + } } std::string MUCController::concatenateListOfNames(const std::vector<NickJoinPart>& joinParts) { - std::string result; - for (size_t i = 0; i < joinParts.size(); i++) { - if (i > 0) { - if (i < joinParts.size() - 1) { - result += ", "; - } else { - result += QT_TRANSLATE_NOOP("", " and "); - } - } - NickJoinPart event = joinParts[i]; - result += event.nick; - } - return result; + std::string result; + for (size_t i = 0; i < joinParts.size(); i++) { + if (i > 0) { + if (i < joinParts.size() - 1) { + result += ", "; + } else { + result += QT_TRANSLATE_NOOP("", " and "); + } + } + NickJoinPart event = joinParts[i]; + result += event.nick; + } + return result; } std::string MUCController::generateJoinPartString(const std::vector<NickJoinPart>& joinParts, bool isImpromptu) { - std::vector<NickJoinPart> sorted[4]; - std::string eventStrings[4]; - foreach (NickJoinPart event, joinParts) { - sorted[event.type].push_back(event); - } - std::string result; - std::vector<JoinPart> populatedEvents; - for (size_t i = 0; i < 4; i++) { - std::string names = concatenateListOfNames(sorted[i]); - if (!names.empty()) { - std::string eventString; - switch (i) { - case Join: - if (sorted[i].size() > 1) { - eventString = (isImpromptu ? QT_TRANSLATE_NOOP("", "%1% have joined the chat") : QT_TRANSLATE_NOOP("", "%1% have entered the room")); - } - else { - eventString = (isImpromptu ? QT_TRANSLATE_NOOP("", "%1% has joined the chat") : QT_TRANSLATE_NOOP("", "%1% has entered the room")); - } - break; - case Part: - if (sorted[i].size() > 1) { - eventString = (isImpromptu ? QT_TRANSLATE_NOOP("", "%1% have left the chat") : QT_TRANSLATE_NOOP("", "%1% have left the room")); - } - else { - eventString = (isImpromptu ? QT_TRANSLATE_NOOP("", "%1% have left the chat") : QT_TRANSLATE_NOOP("", "%1% has left the room")); - } - break; - case JoinThenPart: - if (sorted[i].size() > 1) { - eventString = (isImpromptu ? QT_TRANSLATE_NOOP("", "%1% have joined then left the chat") : QT_TRANSLATE_NOOP("", "%1% have entered then left the room")); - } - else { - eventString = (isImpromptu ? QT_TRANSLATE_NOOP("", "%1% has joined then left the chat") : QT_TRANSLATE_NOOP("", "%1% has entered then left the room")); - } - break; - case PartThenJoin: - if (sorted[i].size() > 1) { - eventString = (isImpromptu ? QT_TRANSLATE_NOOP("", "%1% have left then returned to the chat") : QT_TRANSLATE_NOOP("", "%1% have left then returned to the room")); - } - else { - eventString = (isImpromptu ? QT_TRANSLATE_NOOP("", "%1% has left then returned to the chat") : QT_TRANSLATE_NOOP("", "%1% has left then returned to the room")); - } - break; - } - populatedEvents.push_back(static_cast<JoinPart>(i)); - eventStrings[i] = str(boost::format(eventString) % names); - } - } - for (size_t i = 0; i < populatedEvents.size(); i++) { - if (i > 0) { - if (i < populatedEvents.size() - 1) { - result += ", "; - } else { - result += QT_TRANSLATE_NOOP("", " and "); - } - } - result += eventStrings[populatedEvents[i]]; - } - return result; + std::vector<NickJoinPart> sorted[4]; + std::string eventStrings[4]; + for (const auto& event : joinParts) { + sorted[event.type].push_back(event); + } + std::string result; + std::vector<JoinPart> populatedEvents; + for (size_t i = 0; i < 4; i++) { + std::string names = concatenateListOfNames(sorted[i]); + if (!names.empty()) { + std::string eventString; + switch (i) { + case Join: + if (sorted[i].size() > 1) { + eventString = (isImpromptu ? QT_TRANSLATE_NOOP("", "%1% have joined the chat") : QT_TRANSLATE_NOOP("", "%1% have entered the room")); + } + else { + eventString = (isImpromptu ? QT_TRANSLATE_NOOP("", "%1% has joined the chat") : QT_TRANSLATE_NOOP("", "%1% has entered the room")); + } + break; + case Part: + if (sorted[i].size() > 1) { + eventString = (isImpromptu ? QT_TRANSLATE_NOOP("", "%1% have left the chat") : QT_TRANSLATE_NOOP("", "%1% have left the room")); + } + else { + eventString = (isImpromptu ? QT_TRANSLATE_NOOP("", "%1% have left the chat") : QT_TRANSLATE_NOOP("", "%1% has left the room")); + } + break; + case JoinThenPart: + if (sorted[i].size() > 1) { + eventString = (isImpromptu ? QT_TRANSLATE_NOOP("", "%1% have joined then left the chat") : QT_TRANSLATE_NOOP("", "%1% have entered then left the room")); + } + else { + eventString = (isImpromptu ? QT_TRANSLATE_NOOP("", "%1% has joined then left the chat") : QT_TRANSLATE_NOOP("", "%1% has entered then left the room")); + } + break; + case PartThenJoin: + if (sorted[i].size() > 1) { + eventString = (isImpromptu ? QT_TRANSLATE_NOOP("", "%1% have left then returned to the chat") : QT_TRANSLATE_NOOP("", "%1% have left then returned to the room")); + } + else { + eventString = (isImpromptu ? QT_TRANSLATE_NOOP("", "%1% has left then returned to the chat") : QT_TRANSLATE_NOOP("", "%1% has left then returned to the room")); + } + break; + } + populatedEvents.push_back(static_cast<JoinPart>(i)); + eventStrings[i] = str(boost::format(eventString) % names); + } + } + for (size_t i = 0; i < populatedEvents.size(); i++) { + if (i > 0) { + if (i < populatedEvents.size() - 1) { + result += ", "; + } else { + result += QT_TRANSLATE_NOOP("", " and "); + } + } + result += eventStrings[populatedEvents[i]]; + } + return result; +} + +std::string MUCController::generateNicknameChangeString(const std::string& oldNickname, const std::string& newNickname) { + return str(boost::format(QT_TRANSLATE_NOOP("", "%1% is now known as %2%.")) % oldNickname % newNickname); } void MUCController::handleChangeSubjectRequest(const std::string& subject) { - muc_->changeSubject(subject); + muc_->changeSubject(subject); +} + +void MUCController::handleBookmarkRequest() { + const JID jid = muc_->getJID(); + + // Prepare new bookmark for this room. + MUCBookmark roomBookmark(jid, jid.toBare().toString()); + roomBookmark.setPassword(password_); + roomBookmark.setNick(nick_); + + // Check for existing bookmark for this room and, if it exists, use it instead. + std::vector<MUCBookmark> bookmarks = mucBookmarkManager_->getBookmarks(); + for (const auto& bookmark : bookmarks) { + if (bookmark.getRoom() == jid.toBare()) { + roomBookmark = bookmark; + break; + } + } + + chatWindow_->showBookmarkWindow(roomBookmark); } void MUCController::handleConfigureRequest(Form::ref form) { - if (form) { - muc_->configureRoom(form); - } - else { - muc_->requestConfigurationForm(); - } + if (form) { + muc_->configureRoom(form); + } + else { + muc_->requestConfigurationForm(); + } } void MUCController::handleConfigurationFailed(ErrorPayload::ref error) { - std::string errorMessage = getErrorMessage(error); - errorMessage = str(format(QT_TRANSLATE_NOOP("", "Room configuration failed: %1%.")) % errorMessage); - chatWindow_->addErrorMessage(chatMessageParser_->parseMessageBody(errorMessage)); + std::string errorMessage = getErrorMessage(error); + errorMessage = str(format(QT_TRANSLATE_NOOP("", "Room configuration failed: %1%.")) % errorMessage); + chatWindow_->addErrorMessage(chatMessageParser_->parseMessageBody(errorMessage)); } void MUCController::handleOccupantRoleChangeFailed(ErrorPayload::ref error, const JID&, MUCOccupant::Role) { - std::string errorMessage = getErrorMessage(error); - errorMessage = str(format(QT_TRANSLATE_NOOP("", "Occupant role change failed: %1%.")) % errorMessage); - chatWindow_->addErrorMessage(chatMessageParser_->parseMessageBody(errorMessage)); + std::string errorMessage = getErrorMessage(error); + errorMessage = str(format(QT_TRANSLATE_NOOP("", "Occupant role change failed: %1%.")) % errorMessage); + chatWindow_->addErrorMessage(chatMessageParser_->parseMessageBody(errorMessage)); } void MUCController::configureAsImpromptuRoom(Form::ref form) { - muc_->configureRoom(buildImpromptuRoomConfiguration(form)); - isImpromptuAlreadyConfigured_ = true; - onImpromptuConfigCompleted(); + muc_->configureRoom(buildImpromptuRoomConfiguration(form)); + isImpromptuAlreadyConfigured_ = true; + onImpromptuConfigCompleted(); } void MUCController::handleConfigurationFormReceived(Form::ref form) { - if (isImpromptu_) { - if (!isImpromptuAlreadyConfigured_) { - configureAsImpromptuRoom(form); - } - } else { - chatWindow_->showRoomConfigurationForm(form); - } + if (isImpromptu_) { + if (!isImpromptuAlreadyConfigured_) { + configureAsImpromptuRoom(form); + } + } else { + chatWindow_->showRoomConfigurationForm(form); + } } void MUCController::handleConfigurationCancelled() { - muc_->cancelConfigureRoom(); + muc_->cancelConfigureRoom(); } void MUCController::handleDestroyRoomRequest() { - muc_->destroyRoom(); + muc_->destroyRoom(); } void MUCController::handleInvitePersonToThisMUCRequest(const std::vector<JID>& jidsToInvite) { - boost::shared_ptr<UIEvent> event(new RequestInviteToMUCUIEvent(muc_->getJID(), jidsToInvite)); - eventStream_->send(event); + RequestInviteToMUCUIEvent::ImpromptuMode mode = isImpromptu_ ? RequestInviteToMUCUIEvent::Impromptu : RequestInviteToMUCUIEvent::NotImpromptu; + eventStream_->send(std::make_shared<RequestInviteToMUCUIEvent>(getToJID(), jidsToInvite, mode)); } -void MUCController::handleUIEvent(boost::shared_ptr<UIEvent> event) { - boost::shared_ptr<InviteToMUCUIEvent> inviteEvent = boost::dynamic_pointer_cast<InviteToMUCUIEvent>(event); - if (inviteEvent && inviteEvent->getRoom() == muc_->getJID()) { - foreach (const JID& jid, inviteEvent->getInvites()) { - muc_->invitePerson(jid, inviteEvent->getReason(), isImpromptu_); - } - } +void MUCController::handleUIEvent(std::shared_ptr<UIEvent> event) { + std::shared_ptr<InviteToMUCUIEvent> inviteEvent = std::dynamic_pointer_cast<InviteToMUCUIEvent>(event); + if (inviteEvent && inviteEvent->getOriginator() == muc_->getJID()) { + for (const auto& jid : inviteEvent->getInvites()) { + muc_->invitePerson(jid, inviteEvent->getReason(), isImpromptu_); + } + } } void MUCController::handleGetAffiliationsRequest() { - muc_->requestAffiliationList(MUCOccupant::Owner); - muc_->requestAffiliationList(MUCOccupant::Admin); - muc_->requestAffiliationList(MUCOccupant::Member); - muc_->requestAffiliationList(MUCOccupant::Outcast); + muc_->requestAffiliationList(MUCOccupant::Owner); + muc_->requestAffiliationList(MUCOccupant::Admin); + muc_->requestAffiliationList(MUCOccupant::Member); + muc_->requestAffiliationList(MUCOccupant::Outcast); } -typedef std::pair<MUCOccupant::Affiliation, JID> AffiliationChangePair; - void MUCController::handleChangeAffiliationsRequest(const std::vector<std::pair<MUCOccupant::Affiliation, JID> >& changes) { - std::set<JID> addedJIDs; - foreach (const AffiliationChangePair& change, changes) { - if (change.first != MUCOccupant::NoAffiliation) { - addedJIDs.insert(change.second); - } - } - foreach (const AffiliationChangePair& change, changes) { - if (change.first != MUCOccupant::NoAffiliation || addedJIDs.find(change.second) == addedJIDs.end()) { - muc_->changeAffiliation(change.second, change.first); - } - } + std::set<JID> addedJIDs; + for (const auto& change : changes) { + if (change.first != MUCOccupant::NoAffiliation) { + addedJIDs.insert(change.second); + } + } + for (const auto& change : changes) { + if (change.first != MUCOccupant::NoAffiliation || addedJIDs.find(change.second) == addedJIDs.end()) { + muc_->changeAffiliation(change.second, change.first); + } + } +} + +void MUCController::handleUnblockUserRequest() { + eventStream_->send(std::make_shared<RequestChangeBlockStateUIEvent>(RequestChangeBlockStateUIEvent::Unblocked, muc_->getJID())); +} + +void MUCController::handleBlockingStateChanged() { + std::shared_ptr<BlockList> blockList = clientBlockListManager_->getBlockList(); + if (blockList->getState() == BlockList::Available) { + if (blockList->isBlocked(toJID_)) { + if (!blockedContactAlert_) { + blockedContactAlert_ = chatWindow_->addAlert(QT_TRANSLATE_NOOP("", "You've blocked this room. To enter the room, first unblock it using the cog menu and try again")); + } + chatWindow_->setBlockingState(ChatWindow::IsBlocked); + } else { + if (blockedContactAlert_) { + chatWindow_->removeAlert(*blockedContactAlert_); + blockedContactAlert_.reset(); + } + chatWindow_->setBlockingState(ChatWindow::IsUnblocked); + } + } } void MUCController::handleAffiliationListReceived(MUCOccupant::Affiliation affiliation, const std::vector<JID>& jids) { - chatWindow_->setAffiliations(affiliation, jids); + chatWindow_->setAffiliations(affiliation, jids); } void MUCController::logMessage(const std::string& message, const JID& fromJID, const JID& toJID, const boost::posix_time::ptime& timeStamp, bool isIncoming) { - // log only incoming messages - if (isIncoming && historyController_) { - historyController_->addMessage(message, fromJID, toJID, HistoryMessage::Groupchat, timeStamp); - } + // log only incoming messages + if (isIncoming && historyController_) { + historyController_->addMessage(message, fromJID, toJID, HistoryMessage::Groupchat, timeStamp); + } +} + +JID MUCController::messageCorrectionJID(const JID& fromJID) { + return fromJID; } void MUCController::addRecentLogs() { - if (!historyController_) { - return; - } + if (!historyController_) { + return; + } - joinContext_ = historyController_->getMUCContext(selfJID_, toJID_, lastActivity_); + joinContext_ = historyController_->getMUCContext(selfJID_, toJID_, lastActivity_); - foreach (const HistoryMessage& message, joinContext_) { - bool senderIsSelf = nick_ == message.getFromJID().getResource(); + for (const auto& message : joinContext_) { + bool senderIsSelf = nick_ == message.getFromJID().getResource(); - // the chatWindow uses utc timestamps - addMessage(message.getMessage(), senderDisplayNameFromMessage(message.getFromJID()), senderIsSelf, boost::shared_ptr<SecurityLabel>(new SecurityLabel()), avatarManager_->getAvatarPath(message.getFromJID()), message.getTime() - boost::posix_time::hours(message.getOffset()), HighlightAction()); - } + // the chatWindow uses utc timestamps + addMessage(chatMessageParser_->parseMessageBody(message.getMessage()), senderDisplayNameFromMessage(message.getFromJID()), senderIsSelf, std::make_shared<SecurityLabel>(), avatarManager_->getAvatarPath(message.getFromJID()), message.getTime() - boost::posix_time::hours(message.getOffset())); + } } -void MUCController::checkDuplicates(boost::shared_ptr<Message> newMessage) { - std::string body = newMessage->getBody(); - JID jid = newMessage->getFrom(); - boost::optional<boost::posix_time::ptime> time = newMessage->getTimestamp(); +void MUCController::checkDuplicates(std::shared_ptr<Message> newMessage) { + std::string body = newMessage->getBody().get_value_or(""); + JID jid = newMessage->getFrom(); + boost::optional<boost::posix_time::ptime> time = newMessage->getTimestamp(); - reverse_foreach (const HistoryMessage& message, joinContext_) { - boost::posix_time::ptime messageTime = message.getTime() - boost::posix_time::hours(message.getOffset()); - if (time && time < messageTime) { - break; - } - if (time && time != messageTime) { - continue; - } - if (message.getFromJID() != jid) { - continue; - } - if (message.getMessage() != body) { - continue; - } + for (const auto& message : boost::adaptors::reverse(joinContext_)) { + boost::posix_time::ptime messageTime = message.getTime() - boost::posix_time::hours(message.getOffset()); + if (time && time < messageTime) { + break; + } + if (time && time != messageTime) { + continue; + } + if (message.getFromJID() != jid) { + continue; + } + if (message.getMessage() != body) { + continue; + } - // Mark the message as unreadable - newMessage->setBody(""); - } + // Mark the message as unreadable + newMessage->setBody(""); + } } void MUCController::setNick(const std::string& nick) { - nick_ = nick; - highlighter_->setNick(nick_); + nick_ = nick; + chatMessageParser_->setNick(nick); } Form::ref MUCController::buildImpromptuRoomConfiguration(Form::ref roomConfigurationForm) { - Form::ref result = boost::make_shared<Form>(Form::SubmitType); - std::string impromptuConfigs[] = { "muc#roomconfig_enablelogging", "muc#roomconfig_persistentroom", "muc#roomconfig_publicroom", "muc#roomconfig_whois"}; - std::set<std::string> impromptuConfigsMissing(impromptuConfigs, impromptuConfigs + 4); - foreach (boost::shared_ptr<FormField> field, roomConfigurationForm->getFields()) { - boost::shared_ptr<FormField> resultField; - if (field->getName() == "muc#roomconfig_enablelogging") { - resultField = boost::make_shared<FormField>(FormField::BooleanType, "0"); - } - if (field->getName() == "muc#roomconfig_persistentroom") { - resultField = boost::make_shared<FormField>(FormField::BooleanType, "0"); - } - if (field->getName() == "muc#roomconfig_publicroom") { - resultField = boost::make_shared<FormField>(FormField::BooleanType, "0"); - } - if (field->getName() == "muc#roomconfig_whois") { - resultField = boost::make_shared<FormField>(FormField::ListSingleType, "anyone"); - } - - if (field->getName() == "FORM_TYPE") { - resultField = boost::make_shared<FormField>(FormField::HiddenType, "http://jabber.org/protocol/muc#roomconfig"); - } - - if (resultField) { - impromptuConfigsMissing.erase(field->getName()); - resultField->setName(field->getName()); - result->addField(resultField); - } - } - - foreach (const std::string& config, impromptuConfigsMissing) { - if (config == "muc#roomconfig_publicroom") { - chatWindow_->addSystemMessage(chatMessageParser_->parseMessageBody(QT_TRANSLATE_NOOP("", "This server doesn't support hiding your chat from other users.")), ChatWindow::DefaultDirection); - } else if (config == "muc#roomconfig_whois") { - chatWindow_->addSystemMessage(chatMessageParser_->parseMessageBody(QT_TRANSLATE_NOOP("", "This server doesn't support sharing people's real identity in this chat.")), ChatWindow::DefaultDirection); - } - } - - return result; -} - -void MUCController::setImpromptuWindowTitle() { - std::string title; - typedef std::pair<std::string, MUCOccupant> StringMUCOccupantPair; - std::map<std::string, MUCOccupant> occupants = muc_->getOccupants(); - if (occupants.size() <= 1) { - title = QT_TRANSLATE_NOOP("", "Empty Chat"); - } else { - foreach (StringMUCOccupantPair pair, occupants) { - if (pair.first != nick_) { - title += (title.empty() ? "" : ", ") + pair.first; - } - } - } - chatWindow_->setName(title); + Form::ref result = std::make_shared<Form>(Form::SubmitType); + std::string impromptuConfigs[] = { "muc#roomconfig_enablelogging", "muc#roomconfig_persistentroom", "muc#roomconfig_publicroom", "muc#roomconfig_whois"}; + std::set<std::string> impromptuConfigsMissing(impromptuConfigs, impromptuConfigs + 4); + for (const auto& field : roomConfigurationForm->getFields()) { + std::shared_ptr<FormField> resultField; + if (field->getName() == "muc#roomconfig_enablelogging") { + resultField = std::make_shared<FormField>(FormField::BooleanType, "0"); + } + if (field->getName() == "muc#roomconfig_persistentroom") { + resultField = std::make_shared<FormField>(FormField::BooleanType, "0"); + } + if (field->getName() == "muc#roomconfig_publicroom") { + resultField = std::make_shared<FormField>(FormField::BooleanType, "0"); + } + if (field->getName() == "muc#roomconfig_whois") { + resultField = std::make_shared<FormField>(FormField::ListSingleType, "anyone"); + } + + if (field->getName() == "FORM_TYPE") { + resultField = std::make_shared<FormField>(FormField::HiddenType, "http://jabber.org/protocol/muc#roomconfig"); + } + + if (resultField) { + impromptuConfigsMissing.erase(field->getName()); + resultField->setName(field->getName()); + result->addField(resultField); + } + } + + for (const auto& config : impromptuConfigsMissing) { + if (config == "muc#roomconfig_publicroom") { + chatWindow_->addSystemMessage(chatMessageParser_->parseMessageBody(QT_TRANSLATE_NOOP("", "This server doesn't support hiding your chat from other users.")), ChatWindow::DefaultDirection); + } else if (config == "muc#roomconfig_whois") { + chatWindow_->addSystemMessage(chatMessageParser_->parseMessageBody(QT_TRANSLATE_NOOP("", "This server doesn't support sharing people's real identity in this chat.")), ChatWindow::DefaultDirection); + } + } + + return result; } void MUCController::handleRoomUnlocked() { - // Handle buggy MUC implementations where the joined room already exists and is unlocked. - // Configure the room again in this case. - if (!isImpromptuAlreadyConfigured_) { - if (isImpromptu_ && (muc_->getOccupant(nick_).getAffiliation() == MUCOccupant::Owner)) { - muc_->requestConfigurationForm(); - } else if (isImpromptu_) { - onImpromptuConfigCompleted(); - } - } + // Handle buggy MUC implementations where the joined room already exists and is unlocked. + // Configure the room again in this case. + if (!isImpromptuAlreadyConfigured_) { + if (isImpromptu_ && (muc_->getOccupant(nick_).getAffiliation() == MUCOccupant::Owner)) { + muc_->requestConfigurationForm(); + } else if (isImpromptu_) { + onImpromptuConfigCompleted(); + } + } +} + +void MUCController::setAvailableServerFeatures(std::shared_ptr<DiscoInfo> info) { + ChatControllerBase::setAvailableServerFeatures(info); + if (iqRouter_->isAvailable() && info->hasFeature(DiscoInfo::BlockingCommandFeature)) { + std::shared_ptr<BlockList> blockList = clientBlockListManager_->getBlockList(); + + blockingOnStateChangedConnection_ = blockList->onStateChanged.connect(boost::bind(&MUCController::handleBlockingStateChanged, this)); + blockingOnItemAddedConnection_ = blockList->onItemAdded.connect(boost::bind(&MUCController::handleBlockingStateChanged, this)); + blockingOnItemRemovedConnection_ = blockList->onItemRemoved.connect(boost::bind(&MUCController::handleBlockingStateChanged, this)); + + handleBlockingStateChanged(); + } +} + +void MUCController::handleMUCBookmarkAdded(const MUCBookmark& bookmark) { + if (bookmark.getRoom() == muc_->getJID()) { + updateChatWindowBookmarkStatus(bookmark); + } +} + +void MUCController::handleMUCBookmarkRemoved(const MUCBookmark& bookmark) { + if (bookmark.getRoom() == muc_->getJID()) { + updateChatWindowBookmarkStatus(boost::optional<MUCBookmark>()); + } +} + +void MUCController::updateChatWindowBookmarkStatus(const boost::optional<MUCBookmark>& bookmark) { + assert(chatWindow_); + if (bookmark) { + if (bookmark->getAutojoin()) { + chatWindow_->setBookmarkState(ChatWindow::RoomAutoJoined); + } + else { + chatWindow_->setBookmarkState(ChatWindow::RoomBookmarked); + } + } + else { + chatWindow_->setBookmarkState(ChatWindow::RoomNotBookmarked); + } +} + +void MUCController::displaySubjectIfChanged(const std::string& subject) { + if (subject_ != subject) { + if (!subject.empty()) { + chatWindow_->addSystemMessage(chatMessageParser_->parseMessageBody(str(format(QT_TRANSLATE_NOOP("", "The room subject is now: %1%")) % subject)), ChatWindow::DefaultDirection); + } + else { + chatWindow_->addSystemMessage(chatMessageParser_->parseMessageBody(str(format(QT_TRANSLATE_NOOP("", "The room subject has been removed")))), ChatWindow::DefaultDirection); + } + subject_ = subject; + } +} + +void MUCController::addChatSystemMessage() { + lastJoinMessageUID_ = chatWindow_->addSystemMessage(chatMessageParser_->parseMessageBody(lastStartMessage_), ChatWindow::DefaultDirection); +} + +void MUCController::setChatWindowTitle(const std::string& title) { + chatWindowTitle_ = title; + chatWindow_->setName(chatWindowTitle_); +} + +void MUCController::requestSecurityMarking() { + auto discoInfoRequest = GetDiscoInfoRequest::create(muc_->getJID(), iqRouter_); + discoInfoRequest->onResponse.connect( + [this](std::shared_ptr<DiscoInfo> discoInfoRef, ErrorPayload::ref errorPayloadRef) { + if (!discoInfoRef || errorPayloadRef) { + return; + } + const std::vector<Form::ref>& extensionsList = discoInfoRef->getExtensions(); + if (extensionsList.empty()) { + return; + } + // Get the correct form if it exists + Form::ref roomInfoForm; + for (const auto& form : extensionsList) { + if (form->getFormType() == "http://jabber.org/protocol/muc#roominfo") { + roomInfoForm = form; + break; + } + } + if (!roomInfoForm) { + return; + } + // It exists, now examine the security marking data + auto marking = roomInfoForm->getField("x-isode#roominfo_marking"); + if (!marking) { + return; + } + // Now we know the marking is valid + auto markingValue = marking->getTextSingleValue(); + if (markingValue == "") { + setMUCSecurityMarkingDefault(); + return; + } + auto markingForegroundColor = roomInfoForm->getField("x-isode#roominfo_marking_fg_color"); + auto markingBackgroundColor = roomInfoForm->getField("x-isode#roominfo_marking_bg_color"); + std::string markingForegroundColorValue = "Black"; + std::string markingBackgroundColorValue = "White"; + if (markingForegroundColor && markingForegroundColor->getTextSingleValue() != "") { + markingForegroundColorValue = markingForegroundColor->getTextSingleValue(); + } + if (markingBackgroundColor && markingBackgroundColor->getTextSingleValue() != "") { + markingBackgroundColorValue = markingBackgroundColor->getTextSingleValue(); + } + setMUCSecurityMarking(markingValue, markingForegroundColorValue, markingBackgroundColorValue); + } + ); + discoInfoRequest->send(); +} + +void MUCController::setMUCSecurityMarking(const std::string& markingValue, const std::string& markingForegroundColorValue, const std::string& markingBackgroundColorValue) { + roomSecurityMarking_ = markingValue; + chatWindow_->setChatSecurityMarking(markingValue, markingForegroundColorValue, markingBackgroundColorValue); +} + +void MUCController::setMUCSecurityMarkingDefault() { + roomSecurityMarking_ = ""; + chatWindow_->removeChatSecurityMarking(); } } diff --git a/Swift/Controllers/Chat/MUCController.h b/Swift/Controllers/Chat/MUCController.h index 9283438..bd1148f 100644 --- a/Swift/Controllers/Chat/MUCController.h +++ b/Swift/Controllers/Chat/MUCController.h @@ -1,151 +1,195 @@ /* - * Copyright (c) 2010-2013 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once +#include <map> +#include <memory> #include <set> #include <string> -#include <map> -#include <boost/shared_ptr.hpp> -#include <boost/signals/connection.hpp> +#include <boost/signals2.hpp> +#include <boost/signals2/connection.hpp> -#include <Swiften/Base/boost_bsignals.h> -#include <Swiften/Network/Timer.h> -#include <Swiften/Elements/Message.h> #include <Swiften/Elements/DiscoInfo.h> +#include <Swiften/Elements/MUCOccupant.h> +#include <Swiften/Elements/Message.h> #include <Swiften/JID/JID.h> #include <Swiften/MUC/MUC.h> -#include <Swiften/Elements/MUCOccupant.h> +#include <Swiften/Network/Timer.h> #include <Swift/Controllers/Chat/ChatControllerBase.h> #include <Swift/Controllers/Roster/RosterItem.h> #include <Swift/Controllers/UIInterfaces/ChatWindow.h> namespace Swift { - class StanzaChannel; - class IQRouter; - class ChatWindowFactory; - class Roster; - class AvatarManager; - class UIEventStream; - class TimerFactory; - class TabComplete; - class XMPPRoster; - class HighlightManager; - class UIEvent; - - enum JoinPart {Join, Part, JoinThenPart, PartThenJoin}; - - struct NickJoinPart { - NickJoinPart(const std::string& nick, JoinPart type) : nick(nick), type(type) {} - std::string nick; - JoinPart type; - }; - - class MUCController : public ChatControllerBase { - public: - MUCController(const JID& self, MUC::ref muc, const boost::optional<std::string>& password, const std::string &nick, StanzaChannel* stanzaChannel, IQRouter* iqRouter, ChatWindowFactory* chatWindowFactory, PresenceOracle* presenceOracle, AvatarManager* avatarManager, UIEventStream* events, bool useDelayForLatency, TimerFactory* timerFactory, EventController* eventController, EntityCapsProvider* entityCapsProvider, XMPPRoster* roster, HistoryController* historyController, MUCRegistry* mucRegistry, HighlightManager* highlightManager, ChatMessageParser* chatMessageParser, bool isImpromptu, AutoAcceptMUCInviteDecider* autoAcceptMUCInviteDecider); - ~MUCController(); - boost::signal<void ()> onUserLeft; - boost::signal<void ()> onUserJoined; - boost::signal<void ()> onImpromptuConfigCompleted; - virtual void setOnline(bool online); - void rejoin(); - static void appendToJoinParts(std::vector<NickJoinPart>& joinParts, const NickJoinPart& newEvent); - static std::string generateJoinPartString(const std::vector<NickJoinPart>& joinParts, bool isImpromptu); - static std::string concatenateListOfNames(const std::vector<NickJoinPart>& joinParts); - bool isJoined(); - const std::string& getNick(); - bool isImpromptu() const; - std::map<std::string, JID> getParticipantJIDs() const; - void sendInvites(const std::vector<JID>& jids, const std::string& reason) const; - - protected: - void preSendMessageRequest(boost::shared_ptr<Message> message); - bool isIncomingMessageFromMe(boost::shared_ptr<Message> message); - std::string senderDisplayNameFromMessage(const JID& from); - boost::optional<boost::posix_time::ptime> getMessageTimestamp(boost::shared_ptr<Message> message) const; - void preHandleIncomingMessage(boost::shared_ptr<MessageEvent>); - void postHandleIncomingMessage(boost::shared_ptr<MessageEvent>, const HighlightAction&); - void cancelReplaces(); - void logMessage(const std::string& message, const JID& fromJID, const JID& toJID, const boost::posix_time::ptime& timeStamp, bool isIncoming); - - private: - void setAvailableRoomActions(const MUCOccupant::Affiliation& affiliation, const MUCOccupant::Role& role); - void clearPresenceQueue(); - void addPresenceMessage(const std::string& message); - void handleWindowOccupantSelectionChanged(ContactRosterItem* item); - void handleActionRequestedOnOccupant(ChatWindow::OccupantAction, ContactRosterItem* item); - void handleWindowClosed(); - void handleAvatarChanged(const JID& jid); - void handleOccupantJoined(const MUCOccupant& occupant); - void handleOccupantLeft(const MUCOccupant& occupant, MUC::LeavingType type, const std::string& reason); - void handleOccupantPresenceChange(boost::shared_ptr<Presence> presence); - void handleOccupantRoleChanged(const std::string& nick, const MUCOccupant& occupant,const MUCOccupant::Role& oldRole); - void handleOccupantAffiliationChanged(const std::string& nick, const MUCOccupant::Affiliation& affiliation,const MUCOccupant::Affiliation& oldAffiliation); - void handleJoinComplete(const std::string& nick); - void handleJoinFailed(boost::shared_ptr<ErrorPayload> error); - void handleJoinTimeoutTick(); - void handleChangeSubjectRequest(const std::string&); - std::string roleToGroupName(MUCOccupant::Role role); - std::string roleToSortName(MUCOccupant::Role role); - JID nickToJID(const std::string& nick); - std::string roleToFriendlyName(MUCOccupant::Role role); - void receivedActivity(); - bool messageTargetsMe(boost::shared_ptr<Message> message); - void updateJoinParts(); - bool shouldUpdateJoinParts(); - void dayTicked() {clearPresenceQueue();} - void processUserPart(); - void handleBareJIDCapsChanged(const JID& jid); - void handleConfigureRequest(Form::ref); - void handleConfigurationFailed(ErrorPayload::ref); - void handleConfigurationFormReceived(Form::ref); - void handleDestroyRoomRequest(); - void handleInvitePersonToThisMUCRequest(const std::vector<JID>& jidsToInvite); - void handleConfigurationCancelled(); - void handleOccupantRoleChangeFailed(ErrorPayload::ref, const JID&, MUCOccupant::Role); - void handleGetAffiliationsRequest(); - void handleAffiliationListReceived(MUCOccupant::Affiliation affiliation, const std::vector<JID>& jids); - void handleChangeAffiliationsRequest(const std::vector<std::pair<MUCOccupant::Affiliation, JID> >& changes); - void handleInviteToMUCWindowDismissed(); - void handleInviteToMUCWindowCompleted(); - void handleUIEvent(boost::shared_ptr<UIEvent> event); - void addRecentLogs(); - void checkDuplicates(boost::shared_ptr<Message> newMessage); - void setNick(const std::string& nick); - void setImpromptuWindowTitle(); - void handleRoomUnlocked(); - void configureAsImpromptuRoom(Form::ref form); - Form::ref buildImpromptuRoomConfiguration(Form::ref roomConfigurationForm); - - private: - MUC::ref muc_; - UIEventStream* events_; - std::string nick_; - std::string desiredNick_; - Roster* roster_; - TabComplete* completer_; - bool parting_; - bool joined_; - bool lastWasPresence_; - bool shouldJoinOnReconnect_; - bool doneGettingHistory_; - boost::bsignals::scoped_connection avatarChangedConnection_; - boost::shared_ptr<Timer> loginCheckTimer_; - std::set<std::string> currentOccupants_; - std::vector<NickJoinPart> joinParts_; - boost::posix_time::ptime lastActivity_; - boost::optional<std::string> password_; - XMPPRoster* xmppRoster_; - std::vector<HistoryMessage> joinContext_; - size_t renameCounter_; - bool isImpromptu_; - bool isImpromptuAlreadyConfigured_; - }; -} + class StanzaChannel; + class IQRouter; + class ChatWindowFactory; + class Roster; + class AvatarManager; + class UIEventStream; + class TimerFactory; + class TabComplete; + class XMPPRoster; + class HighlightManager; + class UIEvent; + class VCardManager; + class RosterVCardProvider; + class ClientBlockListManager; + class MUCBookmarkManager; + class MUCBookmark; + + enum JoinPart {Join, Part, JoinThenPart, PartThenJoin}; + + struct NickJoinPart { + NickJoinPart(const std::string& nick, JoinPart type) : nick(nick), type(type) {} + std::string nick; + JoinPart type; + }; + + class MUCController : public ChatControllerBase { + public: + MUCController(const JID& self, MUC::ref muc, const boost::optional<std::string>& password, const std::string &nick, StanzaChannel* stanzaChannel, IQRouter* iqRouter, ChatWindowFactory* chatWindowFactory, NickResolver* nickResolver, PresenceOracle* presenceOracle, AvatarManager* avatarManager, UIEventStream* events, bool useDelayForLatency, TimerFactory* timerFactory, EventController* eventController, EntityCapsProvider* entityCapsProvider, XMPPRoster* xmppRoster, HistoryController* historyController, MUCRegistry* mucRegistry, HighlightManager* highlightManager, ClientBlockListManager* clientBlockListManager, std::shared_ptr<ChatMessageParser> chatMessageParser, bool isImpromptu, AutoAcceptMUCInviteDecider* autoAcceptMUCInviteDecider, VCardManager* vcardManager, MUCBookmarkManager* mucBookmarkManager, SettingsProvider* settings, Chattables& chattables); + virtual ~MUCController() override; + boost::signals2::signal<void ()> onUserLeft; + boost::signals2::signal<void ()> onUserJoined; + boost::signals2::signal<void ()> onImpromptuConfigCompleted; + boost::signals2::signal<void (const std::string&, const std::string& )> onUserNicknameChanged; + virtual void setOnline(bool online) override; + virtual void setAvailableServerFeatures(std::shared_ptr<DiscoInfo> info) override; + void rejoin(); + static void appendToJoinParts(std::vector<NickJoinPart>& joinParts, const NickJoinPart& newEvent); + static std::string generateJoinPartString(const std::vector<NickJoinPart>& joinParts, bool isImpromptu); + static std::string concatenateListOfNames(const std::vector<NickJoinPart>& joinParts); + static std::string generateNicknameChangeString(const std::string& oldNickname, const std::string& newNickname); + bool isJoined(); + const std::string& getNick(); + const boost::optional<std::string> getPassword() const; + bool isImpromptu() const; + std::map<std::string, JID> getParticipantJIDs() const; + void sendInvites(const std::vector<JID>& jids, const std::string& reason) const; + void setChatWindowTitle(const std::string& title); + protected: + virtual void preSendMessageRequest(std::shared_ptr<Message> message) override; + virtual bool isIncomingMessageFromMe(std::shared_ptr<Message> message) override; + virtual std::string senderHighlightNameFromMessage(const JID& from) override; + virtual std::string senderDisplayNameFromMessage(const JID& from) override; + virtual boost::optional<boost::posix_time::ptime> getMessageTimestamp(std::shared_ptr<Message> message) const override; + virtual void preHandleIncomingMessage(std::shared_ptr<MessageEvent>) override; + virtual void addMessageHandleIncomingMessage(const JID& from, const ChatWindow::ChatMessage& message, const std::string& messageID, bool senderIsSelf, std::shared_ptr<SecurityLabel> label, const boost::posix_time::ptime& time) override; + virtual void handleIncomingReplaceMessage(const JID& from, const ChatWindow::ChatMessage& message, const std::string& messageID, const std::string& idToReplace, bool senderIsSelf, std::shared_ptr<SecurityLabel> label, const boost::posix_time::ptime& timeStamp) override; + virtual void postHandleIncomingMessage(std::shared_ptr<MessageEvent>, const ChatWindow::ChatMessage& chatMessage) override; + virtual void cancelReplaces() override; + virtual void logMessage(const std::string& message, const JID& fromJID, const JID& toJID, const boost::posix_time::ptime& timeStamp, bool isIncoming) override; + virtual JID messageCorrectionJID(const JID& fromJID) override; + + private: + void setAvailableRoomActions(const MUCOccupant::Affiliation& affiliation, const MUCOccupant::Role& role); + void clearPresenceQueue(); + void addPresenceMessage(const std::string& message); + void handleWindowOccupantSelectionChanged(ContactRosterItem* item); + void handleActionRequestedOnOccupant(ChatWindow::OccupantAction, ContactRosterItem* item); + void handleWindowClosed(); + void handleAvatarChanged(const JID& jid); + void handleOccupantJoined(const MUCOccupant& occupant); + void handleOccupantNicknameChanged(const std::string& oldNickname, const std::string& newNickname); + void handleOccupantLeft(const MUCOccupant& occupant, MUC::LeavingType type, const std::string& reason); + void handleOccupantPresenceChange(std::shared_ptr<Presence> presence); + void handleOccupantRoleChanged(const std::string& nick, const MUCOccupant& occupant,const MUCOccupant::Role& oldRole); + void handleOccupantAffiliationChanged(const std::string& nick, const MUCOccupant::Affiliation& affiliation,const MUCOccupant::Affiliation& oldAffiliation); + void handleJoinComplete(const std::string& nick); + void handleJoinFailed(std::shared_ptr<ErrorPayload> error); + void handleJoinTimeoutTick(); + void handleChangeSubjectRequest(const std::string&); + void handleBookmarkRequest(); + std::string roleToGroupName(MUCOccupant::Role role); + std::string roleToSortName(MUCOccupant::Role role); + JID nickToJID(const std::string& nick); + std::string roleToFriendlyName(MUCOccupant::Role role); + void receivedActivity(); + bool messageTargetsMe(std::shared_ptr<Message> message); + void updateJoinParts(); + bool shouldUpdateJoinParts(); + virtual void dayTicked() override { clearPresenceQueue(); } + void processUserPart(); + virtual void handleBareJIDCapsChanged(const JID& jid) override; + void handleConfigureRequest(Form::ref); + void handleConfigurationFailed(ErrorPayload::ref); + void handleConfigurationFormReceived(Form::ref); + void handleDestroyRoomRequest(); + void handleInvitePersonToThisMUCRequest(const std::vector<JID>& jidsToInvite); + void handleConfigurationCancelled(); + void handleOccupantRoleChangeFailed(ErrorPayload::ref, const JID&, MUCOccupant::Role); + void handleGetAffiliationsRequest(); + void handleAffiliationListReceived(MUCOccupant::Affiliation affiliation, const std::vector<JID>& jids); + void handleChangeAffiliationsRequest(const std::vector<std::pair<MUCOccupant::Affiliation, JID> >& changes); + void handleInviteToMUCWindowDismissed(); + void handleInviteToMUCWindowCompleted(); + void handleUIEvent(std::shared_ptr<UIEvent> event); + void addRecentLogs(); + void checkDuplicates(std::shared_ptr<Message> newMessage); + void setNick(const std::string& nick); + void handleRoomUnlocked(); + void configureAsImpromptuRoom(Form::ref form); + Form::ref buildImpromptuRoomConfiguration(Form::ref roomConfigurationForm); + + void handleUnblockUserRequest(); + void handleBlockingStateChanged(); + + void handleMUCBookmarkAdded(const MUCBookmark& bookmark); + void handleMUCBookmarkRemoved(const MUCBookmark& bookmark); + void updateChatWindowBookmarkStatus(const boost::optional<MUCBookmark>& bookmark); + + void displaySubjectIfChanged(const std::string& sucject); + void addChatSystemMessage(); + + void requestSecurityMarking(); + void setMUCSecurityMarking(const std::string& markingValue, const std::string& markingForegroundColorValue, const std::string& markingBackgroundColorValue); + void setMUCSecurityMarkingDefault(); + + private: + MUC::ref muc_; + std::string nick_; + std::string desiredNick_; + TabComplete* completer_; + bool parting_; + bool joined_; + bool shouldJoinOnReconnect_; + bool doneGettingHistory_; + boost::signals2::scoped_connection avatarChangedConnection_; + std::shared_ptr<Timer> loginCheckTimer_; + std::set<std::string> currentOccupants_; + std::vector<NickJoinPart> joinParts_; + boost::posix_time::ptime lastActivity_; + boost::optional<std::string> password_; + XMPPRoster* xmppRoster_; + std::unique_ptr<Roster> roster_; + std::vector<HistoryMessage> joinContext_; + size_t renameCounter_; + bool isImpromptu_; + bool isImpromptuAlreadyConfigured_; + RosterVCardProvider* rosterVCardProvider_; + std::string lastJoinMessageUID_; + std::string lastStartMessage_; + + ClientBlockListManager* clientBlockListManager_; + boost::signals2::scoped_connection blockingOnStateChangedConnection_; + boost::signals2::scoped_connection blockingOnItemAddedConnection_; + boost::signals2::scoped_connection blockingOnItemRemovedConnection_; + + boost::optional<ChatWindow::AlertID> blockedContactAlert_; + + MUCBookmarkManager* mucBookmarkManager_; + boost::signals2::scoped_connection mucBookmarkManagerBookmarkAddedConnection_; + boost::signals2::scoped_connection mucBookmarkManagerBookmarkRemovedConnection_; + + std::string subject_; + bool isInitialJoin_; + std::string chatWindowTitle_; + }; +} diff --git a/Swift/Controllers/Chat/MUCSearchController.cpp b/Swift/Controllers/Chat/MUCSearchController.cpp index f0f8a37..0b54d25 100644 --- a/Swift/Controllers/Chat/MUCSearchController.cpp +++ b/Swift/Controllers/Chat/MUCSearchController.cpp @@ -1,183 +1,183 @@ /* - * Copyright (c) 2010-2011 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2019 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ -#include "Swift/Controllers/Chat/MUCSearchController.h" +#include <Swift/Controllers/Chat/MUCSearchController.h> #include <iostream> +#include <memory> #include <boost/bind.hpp> -#include <boost/shared_ptr.hpp> -#include <Swiften/Base/foreach.h> -#include <Swiften/Disco/GetDiscoItemsRequest.h> #include <Swiften/Base/Log.h> #include <Swiften/Base/String.h> +#include <Swiften/Client/NickResolver.h> +#include <Swiften/Disco/DiscoServiceWalker.h> +#include <Swiften/Disco/GetDiscoItemsRequest.h> + #include <Swift/Controllers/UIEvents/UIEventStream.h> #include <Swift/Controllers/UIInterfaces/MUCSearchWindowFactory.h> -#include <Swiften/Disco/DiscoServiceWalker.h> -#include <Swiften/Client/NickResolver.h> namespace Swift { static const std::string SEARCHED_SERVICES = "searchedServices"; -MUCSearchController::MUCSearchController(const JID& jid, MUCSearchWindowFactory* factory, IQRouter* iqRouter, ProfileSettingsProvider* settings) : jid_(jid), factory_(factory), iqRouter_(iqRouter), settings_(settings), window_(NULL), walker_(NULL) { - itemsInProgress_ = 0; - loadSavedServices(); +MUCSearchController::MUCSearchController(const JID& jid, MUCSearchWindowFactory* factory, IQRouter* iqRouter, ProfileSettingsProvider* settings) : jid_(jid), factory_(factory), iqRouter_(iqRouter), settings_(settings), window_(nullptr), walker_(nullptr) { + itemsInProgress_ = 0; + loadSavedServices(); } MUCSearchController::~MUCSearchController() { - delete walker_; - delete window_; + delete walker_; + delete window_; } void MUCSearchController::openSearchWindow() { - if (!window_) { - window_ = factory_->createMUCSearchWindow(); - window_->onSearchService.connect(boost::bind(&MUCSearchController::handleSearchService, this, _1)); - window_->onFinished.connect(boost::bind(&MUCSearchController::handleMUCSearchFinished, this, _1)); - window_->addSavedServices(savedServices_); - } - handleSearchService(JID(jid_.getDomain())); - window_->show(); + if (!window_) { + window_ = factory_->createMUCSearchWindow(); + window_->onSearchService.connect(boost::bind(&MUCSearchController::handleSearchService, this, _1)); + window_->onFinished.connect(boost::bind(&MUCSearchController::handleMUCSearchFinished, this, _1)); + window_->addSavedServices(savedServices_); + } + handleSearchService(JID(jid_.getDomain())); + window_->show(); } void MUCSearchController::loadSavedServices() { - savedServices_.clear(); - foreach (std::string stringItem, String::split(settings_->getStringSetting(SEARCHED_SERVICES), '\n')) { - savedServices_.push_back(JID(stringItem)); - } + savedServices_.clear(); + for (auto&& stringItem : String::split(settings_->getStringSetting(SEARCHED_SERVICES), '\n')) { + savedServices_.push_back(JID(stringItem)); + } } void MUCSearchController::addToSavedServices(const JID& jid) { - savedServices_.erase(std::remove(savedServices_.begin(), savedServices_.end(), jid), savedServices_.end()); - savedServices_.push_front(jid); - - std::string collapsed; - int i = 0; - foreach (JID jidItem, savedServices_) { - if (i >= 15) { - break; - } - if (!collapsed.empty()) { - collapsed += "\n"; - } - collapsed += jidItem.toString(); - ++i; - } - settings_->storeString(SEARCHED_SERVICES, collapsed); - window_->addSavedServices(savedServices_); + savedServices_.erase(std::remove(savedServices_.begin(), savedServices_.end(), jid), savedServices_.end()); + savedServices_.push_front(jid); + + std::string collapsed; + int i = 0; + for (auto&& jidItem : savedServices_) { + if (i >= 15) { + break; + } + if (!collapsed.empty()) { + collapsed += "\n"; + } + collapsed += jidItem.toString(); + ++i; + } + settings_->storeString(SEARCHED_SERVICES, collapsed); + window_->addSavedServices(savedServices_); } void MUCSearchController::handleSearchService(const JID& jid) { - if (!jid.isValid()) { - //Set Window to say error this isn't valid - return; - } - addToSavedServices(jid); - - services_.clear(); - serviceDetails_.clear(); - - window_->setSearchInProgress(true); - refreshView(); - - if (walker_) { - walker_->endWalk(); - walker_->onServiceFound.disconnect(boost::bind(&MUCSearchController::handleDiscoServiceFound, this, _1, _2)); - walker_->onWalkComplete.disconnect(boost::bind(&MUCSearchController::handleDiscoWalkFinished, this)); - delete walker_; - } - - SWIFT_LOG(debug) << "Starting walking MUC services" << std::endl; - itemsInProgress_ = 0; - walker_ = new DiscoServiceWalker(jid, iqRouter_); - walker_->onServiceFound.connect(boost::bind(&MUCSearchController::handleDiscoServiceFound, this, _1, _2)); - walker_->onWalkComplete.connect(boost::bind(&MUCSearchController::handleDiscoWalkFinished, this)); - walker_->beginWalk(); + if (!jid.isValid()) { + //Set Window to say error this isn't valid + return; + } + addToSavedServices(jid); + + services_.clear(); + serviceDetails_.clear(); + + window_->setSearchInProgress(true); + refreshView(); + + if (walker_) { + walker_->endWalk(); + walker_->onServiceFound.disconnect(boost::bind(&MUCSearchController::handleDiscoServiceFound, this, _1, _2)); + walker_->onWalkComplete.disconnect(boost::bind(&MUCSearchController::handleDiscoWalkFinished, this)); + delete walker_; + } + + SWIFT_LOG(debug) << "Starting walking MUC services"; + itemsInProgress_ = 0; + walker_ = new DiscoServiceWalker(jid, iqRouter_); + walker_->onServiceFound.connect(boost::bind(&MUCSearchController::handleDiscoServiceFound, this, _1, _2)); + walker_->onWalkComplete.connect(boost::bind(&MUCSearchController::handleDiscoWalkFinished, this)); + walker_->beginWalk(); } -void MUCSearchController::handleDiscoServiceFound(const JID& jid, boost::shared_ptr<DiscoInfo> info) { - bool isMUC = false; - std::string name; - foreach (DiscoInfo::Identity identity, info->getIdentities()) { - if ((identity.getCategory() == "directory" - && identity.getType() == "chatroom") - || (identity.getCategory() == "conference" - && identity.getType() == "text")) { - isMUC = true; - name = identity.getName(); - } - } - if (isMUC) { - SWIFT_LOG(debug) << "MUC Service found: " << jid << std::endl; - services_.erase(std::remove(services_.begin(), services_.end(), jid), services_.end()); - services_.push_back(jid); - serviceDetails_[jid].setName(name); - serviceDetails_[jid].setJID(jid); - serviceDetails_[jid].setComplete(false); - itemsInProgress_++; - SWIFT_LOG(debug) << "Requesting items of " << jid << " (" << itemsInProgress_ << " item requests in progress)" << std::endl; - GetDiscoItemsRequest::ref discoItemsRequest = GetDiscoItemsRequest::create(jid, iqRouter_); - discoItemsRequest->onResponse.connect(boost::bind(&MUCSearchController::handleRoomsItemsResponse, this, _1, _2, jid)); - discoItemsRequest->send(); - } - else { - removeService(jid); - } - refreshView(); +void MUCSearchController::handleDiscoServiceFound(const JID& jid, std::shared_ptr<DiscoInfo> info) { + bool isMUC = false; + std::string name; + for (auto&& identity : info->getIdentities()) { + if ((identity.getCategory() == "directory" + && identity.getType() == "chatroom") + || (identity.getCategory() == "conference" + && identity.getType() == "text")) { + isMUC = true; + name = identity.getName(); + } + } + if (isMUC) { + SWIFT_LOG(debug) << "MUC Service found: " << jid; + services_.erase(std::remove(services_.begin(), services_.end(), jid), services_.end()); + services_.push_back(jid); + serviceDetails_[jid].setName(name); + serviceDetails_[jid].setJID(jid); + serviceDetails_[jid].setComplete(false); + itemsInProgress_++; + SWIFT_LOG(debug) << "Requesting items of " << jid << " (" << itemsInProgress_ << " item requests in progress)"; + GetDiscoItemsRequest::ref discoItemsRequest = GetDiscoItemsRequest::create(jid, iqRouter_); + discoItemsRequest->onResponse.connect(boost::bind(&MUCSearchController::handleRoomsItemsResponse, this, _1, _2, jid)); + discoItemsRequest->send(); + } + else { + removeService(jid); + } + refreshView(); } void MUCSearchController::handleDiscoWalkFinished() { - SWIFT_LOG(debug) << "MUC Walk finished" << std::endl; - updateInProgressness(); + SWIFT_LOG(debug) << "MUC Walk finished"; + updateInProgressness(); } void MUCSearchController::removeService(const JID& jid) { - serviceDetails_.erase(jid); - services_.erase(std::remove(services_.begin(), services_.end(), jid), services_.end()); - refreshView(); + serviceDetails_.erase(jid); + services_.erase(std::remove(services_.begin(), services_.end(), jid), services_.end()); + refreshView(); } -void MUCSearchController::handleRoomsItemsResponse(boost::shared_ptr<DiscoItems> items, ErrorPayload::ref error, const JID& jid) { - itemsInProgress_--; - SWIFT_LOG(debug) << "Items received for " << jid << " (" << itemsInProgress_ << " item requests in progress)" << std::endl; - updateInProgressness(); - if (error) { - handleDiscoError(jid, error); - return; - } - serviceDetails_[jid].clearRooms(); - foreach (DiscoItems::Item item, items->getItems()) { - serviceDetails_[jid].addRoom(MUCService::MUCRoom(item.getJID().getNode(), item.getName(), -1)); - } - serviceDetails_[jid].setComplete(true); - refreshView(); +void MUCSearchController::handleRoomsItemsResponse(std::shared_ptr<DiscoItems> items, ErrorPayload::ref error, const JID& jid) { + itemsInProgress_--; + SWIFT_LOG(debug) << "Items received for " << jid << " (" << itemsInProgress_ << " item requests in progress)"; + updateInProgressness(); + if (error) { + handleDiscoError(jid, error); + return; + } + serviceDetails_[jid].clearRooms(); + for (auto&& item : items->getItems()) { + serviceDetails_[jid].addRoom(MUCService::MUCRoom(item.getJID().getNode(), item.getName(), -1)); + } + serviceDetails_[jid].setComplete(true); + refreshView(); } void MUCSearchController::handleDiscoError(const JID& jid, ErrorPayload::ref error) { - serviceDetails_[jid].setComplete(true); - serviceDetails_[jid].setError(error->getText()); + serviceDetails_[jid].setComplete(true); + serviceDetails_[jid].setError(error->getText()); } void MUCSearchController::refreshView() { - window_->clearList(); - foreach (JID jid, services_) { - window_->addService(serviceDetails_[jid]); - } + window_->clearList(); + for (auto&& jid : services_) { + window_->addService(serviceDetails_[jid]); + } } void MUCSearchController::updateInProgressness() { - window_->setSearchInProgress((walker_ && walker_->isActive()) || itemsInProgress_ > 0); + window_->setSearchInProgress((walker_ && walker_->isActive()) || itemsInProgress_ > 0); } void MUCSearchController::handleMUCSearchFinished(const boost::optional<JID>& result) { - if (result) { - onMUCSelected(*result); - } + if (result) { + onMUCSelected(*result); + } } } diff --git a/Swift/Controllers/Chat/MUCSearchController.h b/Swift/Controllers/Chat/MUCSearchController.h index f90e4a7..f853bcd 100644 --- a/Swift/Controllers/Chat/MUCSearchController.h +++ b/Swift/Controllers/Chat/MUCSearchController.h @@ -1,124 +1,124 @@ /* - * Copyright (c) 2010-2011 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <vector> #include <map> +#include <memory> +#include <string> +#include <vector> -#include <boost/shared_ptr.hpp> -#include "Swiften/Base/boost_bsignals.h" +#include <boost/signals2.hpp> -#include <string> -#include "Swiften/JID/JID.h" +#include <Swiften/Elements/DiscoInfo.h> +#include <Swiften/Elements/DiscoItems.h> +#include <Swiften/Elements/ErrorPayload.h> +#include <Swiften/JID/JID.h> -#include "Swift/Controllers/UIEvents/UIEvent.h" -#include "Swift/Controllers/ProfileSettingsProvider.h" -#include "Swiften/Elements/DiscoInfo.h" -#include "Swiften/Elements/DiscoItems.h" -#include "Swiften/Elements/ErrorPayload.h" +#include <Swift/Controllers/ProfileSettingsProvider.h> +#include <Swift/Controllers/UIEvents/UIEvent.h> namespace Swift { - class UIEventStream; - class MUCSearchWindow; - class MUCSearchWindowFactory; - class IQRouter; - class DiscoServiceWalker; - class NickResolver; - - class MUCService { - public: - class MUCRoom { - public: - MUCRoom(const std::string& node, const std::string& name, int occupants) : node_(node), name_(name), occupants_(occupants) {} - std::string getNode() {return node_;} - std::string getName() {return name_;} - int getOccupantCount() {return occupants_;} - private: - std::string node_; - std::string name_; - int occupants_; - }; - - MUCService() {error_ = false; complete_ = false;} - - void setComplete(bool complete) { - complete_ = complete; - } - - void setName(const std::string& name) { - name_ = name; - } - - void setJID(const JID& jid) { - jid_ = jid; - } - - bool getComplete() const { - return complete_; - } - - JID getJID() const { - return jid_; - } - - std::string getName() const { - return name_; - } - - void setError(const std::string& errorText) {error_ = true; errorText_ = errorText;} - - void clearRooms() {rooms_.clear();} - - void addRoom(const MUCRoom& room) {rooms_.push_back(room);} - - std::vector<MUCRoom> getRooms() const {return rooms_;} - private: - std::string name_; - JID jid_; - std::vector<MUCRoom> rooms_; - bool complete_; - bool error_; - std::string errorText_; - }; - - class MUCSearchController { - public: - MUCSearchController(const JID& jid, MUCSearchWindowFactory* mucSearchWindowFactory, IQRouter* iqRouter, ProfileSettingsProvider* settings); - ~MUCSearchController(); - - void openSearchWindow(); - - public: - boost::signal<void (const JID&)> onMUCSelected; - - private: - void handleSearchService(const JID& jid); - void handleRoomsItemsResponse(boost::shared_ptr<DiscoItems> items, ErrorPayload::ref error, const JID& jid); - void handleDiscoError(const JID& jid, ErrorPayload::ref error); - void handleDiscoServiceFound(const JID&, boost::shared_ptr<DiscoInfo>); - void handleDiscoWalkFinished(); - void handleMUCSearchFinished(const boost::optional<JID>& result); - void removeService(const JID& jid); - void refreshView(); - void loadSavedServices(); - void addToSavedServices(const JID& jid); - void updateInProgressness(); - - private: - JID jid_; - MUCSearchWindowFactory* factory_; - IQRouter* iqRouter_; - ProfileSettingsProvider* settings_; - MUCSearchWindow* window_; - DiscoServiceWalker* walker_; - std::list<JID> services_; - std::list<JID> savedServices_; - std::map<JID, MUCService> serviceDetails_; - std::vector<DiscoServiceWalker*> walksInProgress_; - int itemsInProgress_; - }; + class UIEventStream; + class MUCSearchWindow; + class MUCSearchWindowFactory; + class IQRouter; + class DiscoServiceWalker; + class NickResolver; + + class MUCService { + public: + class MUCRoom { + public: + MUCRoom(const std::string& node, const std::string& name, int occupants) : node_(node), name_(name), occupants_(occupants) {} + std::string getNode() {return node_;} + std::string getName() {return name_;} + int getOccupantCount() {return occupants_;} + private: + std::string node_; + std::string name_; + int occupants_; + }; + + MUCService() {error_ = false; complete_ = false;} + + void setComplete(bool complete) { + complete_ = complete; + } + + void setName(const std::string& name) { + name_ = name; + } + + void setJID(const JID& jid) { + jid_ = jid; + } + + bool getComplete() const { + return complete_; + } + + JID getJID() const { + return jid_; + } + + std::string getName() const { + return name_; + } + + void setError(const std::string& errorText) {error_ = true; errorText_ = errorText;} + + void clearRooms() {rooms_.clear();} + + void addRoom(const MUCRoom& room) {rooms_.push_back(room);} + + std::vector<MUCRoom> getRooms() const {return rooms_;} + private: + std::string name_; + JID jid_; + std::vector<MUCRoom> rooms_; + bool complete_; + bool error_; + std::string errorText_; + }; + + class MUCSearchController { + public: + MUCSearchController(const JID& jid, MUCSearchWindowFactory* mucSearchWindowFactory, IQRouter* iqRouter, ProfileSettingsProvider* settings); + ~MUCSearchController(); + + void openSearchWindow(); + + public: + boost::signals2::signal<void (const JID&)> onMUCSelected; + + private: + void handleSearchService(const JID& jid); + void handleRoomsItemsResponse(std::shared_ptr<DiscoItems> items, ErrorPayload::ref error, const JID& jid); + void handleDiscoError(const JID& jid, ErrorPayload::ref error); + void handleDiscoServiceFound(const JID&, std::shared_ptr<DiscoInfo>); + void handleDiscoWalkFinished(); + void handleMUCSearchFinished(const boost::optional<JID>& result); + void removeService(const JID& jid); + void refreshView(); + void loadSavedServices(); + void addToSavedServices(const JID& jid); + void updateInProgressness(); + + private: + JID jid_; + MUCSearchWindowFactory* factory_; + IQRouter* iqRouter_; + ProfileSettingsProvider* settings_; + MUCSearchWindow* window_; + DiscoServiceWalker* walker_; + std::list<JID> services_; + std::list<JID> savedServices_; + std::map<JID, MUCService> serviceDetails_; + std::vector<DiscoServiceWalker*> walksInProgress_; + int itemsInProgress_; + }; } diff --git a/Swift/Controllers/Chat/UnitTest/ChatControllerTest.cpp b/Swift/Controllers/Chat/UnitTest/ChatControllerTest.cpp new file mode 100644 index 0000000..e010656 --- /dev/null +++ b/Swift/Controllers/Chat/UnitTest/ChatControllerTest.cpp @@ -0,0 +1,162 @@ +/* + * Copyright (c) 2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#include <memory> +#include <string> + +#include <gtest/gtest.h> +#include <hippomocks.h> + +#include <Swiften/Avatars/NullAvatarManager.h> +#include <Swiften/Client/ClientBlockListManager.h> +#include <Swiften/Client/DummyStanzaChannel.h> +#include <Swiften/Client/NickResolver.h> +#include <Swiften/Disco/DummyEntityCapsProvider.h> +#include <Swiften/Network/DummyTimerFactory.h> +#include <Swiften/Queries/DummyIQChannel.h> +#include <Swiften/Roster/XMPPRoster.h> +#include <Swiften/Roster/XMPPRosterImpl.h> +#include <Swiften/VCards/VCardManager.h> +#include <Swiften/VCards/VCardMemoryStorage.h> + +#include <Swift/Controllers/Chat/AutoAcceptMUCInviteDecider.h> +#include <Swift/Controllers/Chat/ChatController.h> +#include <Swift/Controllers/Chat/ChatMessageParser.h> +#include <Swift/Controllers/Chat/Chattables.h> +#include <Swift/Controllers/Settings/DummySettingsProvider.h> +#include <Swift/Controllers/UIEvents/UIEventStream.h> +#include <Swift/Controllers/UIInterfaces/ChatWindowFactory.h> +#include <Swift/Controllers/UnitTest/MockChatWindow.h> +#include <Swift/Controllers/XMPPEvents/EventController.h> + +using namespace Swift; + +/** + * Most of the ChatController tests are in ChatsManagerTest. + * New tests related with ChatController should be added here, + * and old tests should be migrated when possible. + */ + +class ExtendedChatController : public ChatController { +public: + ExtendedChatController(const JID& self, StanzaChannel* stanzaChannel, IQRouter* iqRouter, ChatWindowFactory* chatWindowFactory, const JID &contact, NickResolver* nickResolver, PresenceOracle* presenceOracle, AvatarManager* avatarManager, bool isInMUC, bool useDelayForLatency, UIEventStream* eventStream, TimerFactory* timerFactory, EventController* eventController, EntityCapsProvider* entityCapsProvider, bool userWantsReceipts, HistoryController* historyController, MUCRegistry* mucRegistry, HighlightManager* highlightManager, ClientBlockListManager* clientBlockListManager, std::shared_ptr<ChatMessageParser> chatMessageParser, AutoAcceptMUCInviteDecider* autoAcceptMUCInviteDecider, SettingsProvider* settings, Chattables& chattables) : + ChatController(self, stanzaChannel, iqRouter, chatWindowFactory, contact, nickResolver, presenceOracle, avatarManager, isInMUC, useDelayForLatency, eventStream, timerFactory, eventController, entityCapsProvider, userWantsReceipts, historyController, mucRegistry, highlightManager, clientBlockListManager, chatMessageParser, autoAcceptMUCInviteDecider, settings, chattables) { + } + + std::map<std::shared_ptr<Stanza>, std::string> getUnackedStanzas() { return unackedStanzas_; } + std::map<std::string, std::shared_ptr<Stanza>> getFailedStanzas() { return failedStanzas_; } +}; + +class ChatControllerTest : public ::testing::Test { +protected: + virtual void SetUp() { + self_ = JID("alice@wonderland.lit"); + other_ = JID("whiterabbit@wonderland.lit"); + stanzaChannel_ = new DummyStanzaChannel(); + iqChannel_ = new DummyIQChannel(); + iqRouter_ = new IQRouter(iqChannel_); + eventController_ = new EventController(); + xmppRoster_ = new XMPPRosterImpl(); + vCardManager_ = new VCardManager(self_, iqRouter_, vCardMemoryStorage_); + mucRegistry_ = new MUCRegistry(); + nickResolver_ = new NickResolver(self_, xmppRoster_, vCardManager_, mucRegistry_); + presenceOracle_ = new PresenceOracle(stanzaChannel_, xmppRoster_); + avatarManager_ = new NullAvatarManager(); + uiEventStream_ = new UIEventStream(); + timerFactory_ = new DummyTimerFactory(); + entityCapsProvider_ = new DummyEntityCapsProvider(); + settings_ = new DummySettingsProvider(); + highlightManager_ = new HighlightManager(settings_); + highlightManager_->resetToDefaultConfiguration(); + clientBlockListManager_ = new ClientBlockListManager(iqRouter_); + autoAcceptMUCInviteDecider_ = new AutoAcceptMUCInviteDecider(self_.getDomain(), xmppRoster_, settings_); + chatMessageParser_ = std::make_shared<ChatMessageParser>(std::map<std::string, std::string>(), highlightManager_->getConfiguration(), ChatMessageParser::Mode::GroupChat); + mocks_ = new MockRepository(); + window_ = new MockChatWindow(); + chatWindowFactory_ = mocks_->InterfaceMock<ChatWindowFactory>(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(other_, uiEventStream_).Return(window_); + chattables_ = std::make_unique<Chattables>(); + + controller_ = new ExtendedChatController(self_, stanzaChannel_, iqRouter_, chatWindowFactory_, other_, nickResolver_, presenceOracle_, avatarManager_, false, false, uiEventStream_, timerFactory_, eventController_, entityCapsProvider_, false, nullptr, mucRegistry_, highlightManager_, clientBlockListManager_, chatMessageParser_, nullptr, settings_, *chattables_); + } + virtual void TearDown() { + delete controller_; + chattables_.reset(); + chatMessageParser_.reset(); + delete autoAcceptMUCInviteDecider_; + delete clientBlockListManager_; + delete highlightManager_; + delete settings_; + delete entityCapsProvider_; + delete timerFactory_; + delete uiEventStream_; + delete avatarManager_; + delete presenceOracle_; + delete nickResolver_; + delete mucRegistry_; + delete vCardManager_; + delete xmppRoster_; + delete eventController_; + delete iqRouter_; + delete iqChannel_; + delete stanzaChannel_; + } + + JID self_, other_; + AvatarManager* avatarManager_ = nullptr; + ExtendedChatController* controller_ = nullptr; + ChatWindowFactory* chatWindowFactory_; + ClientBlockListManager* clientBlockListManager_; + EventController* eventController_ = nullptr; + EntityCapsProvider* entityCapsProvider_ = nullptr; + IQChannel* iqChannel_ = nullptr; + IQRouter* iqRouter_ = nullptr; + MockRepository* mocks_; + MockChatWindow* window_; + MUCRegistry* mucRegistry_ = nullptr; + NickResolver* nickResolver_ = nullptr; + PresenceOracle* presenceOracle_ = nullptr; + DummyStanzaChannel* stanzaChannel_ = nullptr; + TimerFactory* timerFactory_; + XMPPRosterImpl* xmppRoster_ = nullptr; + UIEventStream* uiEventStream_; + VCardManager* vCardManager_ = nullptr; + VCardMemoryStorage* vCardMemoryStorage_ = nullptr; + DummySettingsProvider* settings_; + HighlightManager* highlightManager_; + std::shared_ptr<ChatMessageParser> chatMessageParser_; + AutoAcceptMUCInviteDecider* autoAcceptMUCInviteDecider_; + std::unique_ptr<Chattables> chattables_; + +}; + +TEST_F(ChatControllerTest, testResendMessage) { + std::string msgBody("TestMsg"); + stanzaChannel_->setStreamManagementEnabled(true); + window_->onSendMessageRequest(msgBody, false); + { + auto failedStanzas = controller_->getFailedStanzas(); + auto unackedStanzas = controller_->getUnackedStanzas(); + ASSERT_EQ(failedStanzas.size(), 0); + ASSERT_EQ(unackedStanzas.size(), 1); + } + //Disconnecting to fail the stanza + controller_->setOnline(false); + controller_->setOnline(true); + { + auto failedStanzas = controller_->getFailedStanzas(); + auto unackedStanzas = controller_->getUnackedStanzas(); + ASSERT_EQ(failedStanzas.size(), 1); + ASSERT_EQ(unackedStanzas.size(), 0); + } + window_->onResendMessageRequest("id"); + { + auto failedStanzas = controller_->getFailedStanzas(); + auto unackedStanzas = controller_->getUnackedStanzas(); + ASSERT_EQ(failedStanzas.size(), 0); + ASSERT_EQ(unackedStanzas.size(), 1); + } +} diff --git a/Swift/Controllers/Chat/UnitTest/ChatListWindowChatTest.cpp b/Swift/Controllers/Chat/UnitTest/ChatListWindowChatTest.cpp new file mode 100644 index 0000000..9561e2b --- /dev/null +++ b/Swift/Controllers/Chat/UnitTest/ChatListWindowChatTest.cpp @@ -0,0 +1,292 @@ +/* + * Copyright (c) 2017 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#include <memory> +#include <string> +#include <vector> + +#include <boost/algorithm/string.hpp> +#include <boost/version.hpp> + +#include <gtest/gtest.h> + +#include <Swiften/Base/ByteArray.h> +#include <Swiften/StringCodecs/Base64.h> + +#include <Swift/Controllers/Chat/ChatListWindowChatBoostSerialize.h> +#include <Swift/Controllers/UIInterfaces/ChatListWindow.h> + +using namespace Swift; + +class ChatListWindowChatTest : public ::testing::Test { +protected: + virtual void SetUp() {} + virtual void TearDown() {} + + void testOptionalPasswordValue(const boost::optional<std::string>& value1, const std::string& value2) { + auto archiveLibraryVersion = boost::archive::BOOST_ARCHIVE_VERSION(); + if (archiveLibraryVersion != Swift::BoostArchiveSkipVersion) { + EXPECT_EQ(value1.get_value_or(""), value2); + } + else { + EXPECT_EQ(value1.get_value_or(""), ""); + } + } + + std::string chatsSerialise(const std::vector<ChatListWindow::Chat>& chats) { + std::stringstream serializeStream; + boost::archive::text_oarchive oa(serializeStream); + oa & chats; + std::string serializedStr = Base64::encode(createByteArray(serializeStream.str())); + return serializedStr; + } + + std::vector<ChatListWindow::Chat> chatsDeserialise(const std::string& b64chats) { + ByteArray debase64 = Base64::decode(b64chats); + std::vector<ChatListWindow::Chat> recentChats; + std::stringstream deserializeStream(std::string(reinterpret_cast<const char*>(vecptr(debase64)), debase64.size())); + try { + boost::archive::text_iarchive ia(deserializeStream); + ia >> recentChats; + } + catch (const boost::archive::archive_exception& e) { + EXPECT_TRUE(false) << "Archive Version:" << boost::archive::BOOST_ARCHIVE_VERSION() << " " << e.what() << std::endl; + recentChats.clear(); + return recentChats; + } + recentChats.erase(std::remove(recentChats.begin(), recentChats.end(), ChatListWindow::Chat()), recentChats.end()); + return recentChats; + } +}; + +TEST_F(ChatListWindowChatTest, testNormalSerialization) { + ChatListWindow::Chat chat1("swift@rooms.swift.im", "swift@rooms.swift.im", "Some text 0", 0, StatusShow::None, "", false, false, "Nick Name"); + ChatListWindow::Chat chat2("testuser1@domain.com", "swift@rooms2.swift.im", "Some text 1", 0, StatusShow::None, "", false, false, "Nick Name", std::string("pass")); + ChatListWindow::Chat chat3("testuser2@domain.com", "room 2", "Some text 2", 0, StatusShow::None, "", true, false, "Nick Name"); + ChatListWindow::Chat chat4("testuser3@domain.com", "room 3", "Some text 2", 0, StatusShow::None, "", true, false, "Nick Name", std::string("pass")); + + std::map<std::string, JID> impromptuJIDs; + impromptuJIDs["testuser1@domain.com"] = "testuser1@domain.com"; + impromptuJIDs["testuser2@domain.com"] = "testuser2@domain.com"; + std::map<JID, std::string> inviteesNames; + inviteesNames["user1@domain.com"] = "user1@domain.com"; + + chat3.impromptuJIDs = impromptuJIDs; + chat3.inviteesNames = inviteesNames; + chat4.impromptuJIDs = impromptuJIDs; + chat4.inviteesNames = inviteesNames; + + std::vector<ChatListWindow::Chat> chats; + chats.push_back(chat1); + chats.push_back(chat2); + chats.push_back(chat3); + chats.push_back(chat4); + + auto base64 = chatsSerialise(chats); + ASSERT_TRUE(base64.size() > 0); + auto restoredChats = chatsDeserialise(base64); + ASSERT_EQ(restoredChats.size(), 4); + + EXPECT_FALSE(restoredChats[0].isMUC); + EXPECT_EQ(restoredChats[0].jid, "swift@rooms.swift.im"); + EXPECT_EQ(restoredChats[0].chatName, "swift@rooms.swift.im"); + EXPECT_EQ(restoredChats[0].activity, "Some text 0"); + EXPECT_EQ(restoredChats[0].nick, "Nick Name"); + EXPECT_EQ(restoredChats[0].impromptuJIDs.size(), 0); + testOptionalPasswordValue(restoredChats[0].password, ""); + EXPECT_EQ(restoredChats[0].inviteesNames.size(), 0); + + EXPECT_FALSE(restoredChats[1].isMUC); + EXPECT_EQ(restoredChats[1].jid, "testuser1@domain.com"); + EXPECT_EQ(restoredChats[1].chatName, "swift@rooms2.swift.im"); + EXPECT_EQ(restoredChats[1].activity, "Some text 1"); + EXPECT_EQ(restoredChats[1].nick, "Nick Name"); + EXPECT_EQ(restoredChats[1].impromptuJIDs.size(), 0); + testOptionalPasswordValue(restoredChats[1].password, "pass"); + EXPECT_EQ(restoredChats[1].inviteesNames.size(), 0); + + EXPECT_TRUE(restoredChats[2].isMUC); + EXPECT_EQ(restoredChats[2].jid, "testuser2@domain.com"); + EXPECT_EQ(restoredChats[2].chatName, "room 2"); + EXPECT_EQ(restoredChats[2].activity, "Some text 2"); + EXPECT_EQ(restoredChats[2].nick, "Nick Name"); + ASSERT_EQ(restoredChats[2].impromptuJIDs.size(), 2); + EXPECT_EQ(restoredChats[2].impromptuJIDs["testuser1@domain.com"], "testuser1@domain.com"); + EXPECT_EQ(restoredChats[2].impromptuJIDs["testuser2@domain.com"], "testuser2@domain.com"); + testOptionalPasswordValue(restoredChats[2].password, ""); + ASSERT_EQ(restoredChats[2].inviteesNames.size(), 1); + EXPECT_EQ(restoredChats[2].inviteesNames["user1@domain.com"], "user1@domain.com"); + + EXPECT_TRUE(restoredChats[3].isMUC); + EXPECT_EQ(restoredChats[3].jid, "testuser3@domain.com"); + EXPECT_EQ(restoredChats[3].chatName, "room 3"); + EXPECT_EQ(restoredChats[3].activity, "Some text 2"); + EXPECT_EQ(restoredChats[3].nick, "Nick Name"); + ASSERT_EQ(restoredChats[3].impromptuJIDs.size(), 2); + EXPECT_EQ(restoredChats[3].impromptuJIDs["testuser1@domain.com"], "testuser1@domain.com"); + EXPECT_EQ(restoredChats[3].impromptuJIDs["testuser2@domain.com"], "testuser2@domain.com"); + testOptionalPasswordValue(restoredChats[3].password, "pass"); + ASSERT_EQ(restoredChats[3].inviteesNames.size(), 1); + EXPECT_EQ(restoredChats[3].inviteesNames["user1@domain.com"], "user1@domain.com"); +} + +TEST_F(ChatListWindowChatTest, testVersionsSerialization) { + auto archiveLibraryVersion = boost::archive::BOOST_ARCHIVE_VERSION(); + /* + The following strings are base64 serialised vectors with these Swift::ChatListWindow::Chat elements: + + Chat1: Jid = "swift@rooms.swift.im", ChatName = "swift@rooms.swift.im", Activity = "Some text 0", isMUC = false, nick="Nick Name" + Chat2: Jid = "testuser1@domain.com", ChatName = "swift@rooms2.swift.im", Activity = "Some text 1", isMUC = false, nick="Nick Name", password = "pass" + Chat3: Jid = "testuser2@domain.com", ChatName = "room2", Activity = "Some text 2", isMUC = true, nick="Nick Name", impromptuJIDs, inviteesNames + Chat4: Jid = "testuser3@domain.com", ChatName = "room3", Activity = "Some text 2", isMUC = true, nick="Nick Name", impromptuJIDs, password = "pass", inviteesNames + + impromptuJIDs = {("testuser1@domain.com","testuser1@domain.com"), ("testuser2@domain.com", "testuser2@domain.com")} + inviteesNames = {("user1@domain.com","user1@domain.com")} + */ + std::string serializedChats_BoostArchiveV10_ClassVersion_0 = "MjIgc2VyaWFsaXphdGlvbjo6YXJjaGl2ZSAxMCAwIDAgNCAwIDAgMCAwIDAgMjAgc3dpZnRAcm9vbXMuc3dpZnQuaW0gMjAgc3dpZnRAcm9vbXMuc3dpZnQuaW0gMTEgU29tZSB0ZXh0IDAgMCA5IE5pY2sgTmFtZSAwIDAgMCAwIDIwIHRlc3R1c2VyMUBkb21haW4uY29tIDIxIHN3aWZ0QHJvb21zMi5zd2lmdC5pbSAxMSBTb21lIHRleHQgMSAwIDkgTmljayBOYW1lIDAgMCAyMCB0ZXN0dXNlcjJAZG9tYWluLmNvbSA2IHJvb20gMiAxMSBTb21lIHRleHQgMiAxIDkgTmljayBOYW1lIDIgMCAwIDAgMjAgdGVzdHVzZXIxQGRvbWFpbi5jb20gMjAgdGVzdHVzZXIxQGRvbWFpbi5jb20gMjAgdGVzdHVzZXIyQGRvbWFpbi5jb20gMjAgdGVzdHVzZXIyQGRvbWFpbi5jb20gMjAgdGVzdHVzZXIzQGRvbWFpbi5jb20gNiByb29tIDMgMTEgU29tZSB0ZXh0IDIgMSA5IE5pY2sgTmFtZSAyIDAgMjAgdGVzdHVzZXIxQGRvbWFpbi5jb20gMjAgdGVzdHVzZXIxQGRvbWFpbi5jb20gMjAgdGVzdHVzZXIyQGRvbWFpbi5jb20gMjAgdGVzdHVzZXIyQGRvbWFpbi5jb20="; + { + auto restoredChats = chatsDeserialise(serializedChats_BoostArchiveV10_ClassVersion_0); + if (archiveLibraryVersion == Swift::BoostArchiveSkipVersion) { + ASSERT_EQ(restoredChats.size(), 0); + } + else { + ASSERT_EQ(restoredChats.size(), 4); + + EXPECT_FALSE(restoredChats[0].isMUC); + EXPECT_EQ(restoredChats[0].jid, "swift@rooms.swift.im"); + EXPECT_EQ(restoredChats[0].chatName, "swift@rooms.swift.im"); + EXPECT_EQ(restoredChats[0].activity, "Some text 0"); + EXPECT_EQ(restoredChats[0].nick, "Nick Name"); + EXPECT_EQ(restoredChats[0].impromptuJIDs.size(), 0); + + EXPECT_FALSE(restoredChats[1].isMUC); + EXPECT_EQ(restoredChats[1].jid, "testuser1@domain.com"); + EXPECT_EQ(restoredChats[1].chatName, "swift@rooms2.swift.im"); + EXPECT_EQ(restoredChats[1].activity, "Some text 1"); + EXPECT_EQ(restoredChats[1].nick, "Nick Name"); + EXPECT_EQ(restoredChats[1].impromptuJIDs.size(), 0); + + EXPECT_TRUE(restoredChats[2].isMUC); + EXPECT_EQ(restoredChats[2].jid, "testuser2@domain.com"); + EXPECT_EQ(restoredChats[2].chatName, "room 2"); + EXPECT_EQ(restoredChats[2].activity, "Some text 2"); + EXPECT_EQ(restoredChats[2].nick, "Nick Name"); + ASSERT_EQ(restoredChats[2].impromptuJIDs.size(), 2); + EXPECT_EQ(restoredChats[2].impromptuJIDs["testuser1@domain.com"], "testuser1@domain.com"); + EXPECT_EQ(restoredChats[2].impromptuJIDs["testuser2@domain.com"], "testuser2@domain.com"); + + EXPECT_TRUE(restoredChats[3].isMUC); + EXPECT_EQ(restoredChats[3].jid, "testuser3@domain.com"); + EXPECT_EQ(restoredChats[3].chatName, "room 3"); + EXPECT_EQ(restoredChats[3].activity, "Some text 2"); + EXPECT_EQ(restoredChats[3].nick, "Nick Name"); + ASSERT_EQ(restoredChats[3].impromptuJIDs.size(), 2); + EXPECT_EQ(restoredChats[3].impromptuJIDs["testuser1@domain.com"], "testuser1@domain.com"); + EXPECT_EQ(restoredChats[3].impromptuJIDs["testuser2@domain.com"], "testuser2@domain.com"); + } + } + + std::string serializedChats_BoostArchiveV10_ClassVersion_1 = "MjIgc2VyaWFsaXphdGlvbjo6YXJjaGl2ZSAxMCAwIDAgNCAxIDAgMSAwIDAgMjAgc3dpZnRAcm9vbXMuc3dpZnQuaW0gMjAgc3dpZnRAcm9vbXMuc3dpZnQuaW0gMTEgU29tZSB0ZXh0IDAgMCA5IE5pY2sgTmFtZSAwIDAgMCAwIDAgMCAwIDIwIHRlc3R1c2VyMUBkb21haW4uY29tIDIxIHN3aWZ0QHJvb21zMi5zd2lmdC5pbSAxMSBTb21lIHRleHQgMSAwIDkgTmljayBOYW1lIDAgMCAxIDAgNCBwYXNzIDIwIHRlc3R1c2VyMkBkb21haW4uY29tIDYgcm9vbSAyIDExIFNvbWUgdGV4dCAyIDEgOSBOaWNrIE5hbWUgMiAwIDAgMCAyMCB0ZXN0dXNlcjFAZG9tYWluLmNvbSAyMCB0ZXN0dXNlcjFAZG9tYWluLmNvbSAyMCB0ZXN0dXNlcjJAZG9tYWluLmNvbSAyMCB0ZXN0dXNlcjJAZG9tYWluLmNvbSAwIDIwIHRlc3R1c2VyM0Bkb21haW4uY29tIDYgcm9vbSAzIDExIFNvbWUgdGV4dCAyIDEgOSBOaWNrIE5hbWUgMiAwIDIwIHRlc3R1c2VyMUBkb21haW4uY29tIDIwIHRlc3R1c2VyMUBkb21haW4uY29tIDIwIHRlc3R1c2VyMkBkb21haW4uY29tIDIwIHRlc3R1c2VyMkBkb21haW4uY29tIDEgMCA0IHBhc3M="; + { + auto restoredChats = chatsDeserialise(serializedChats_BoostArchiveV10_ClassVersion_1); + if (archiveLibraryVersion == Swift::BoostArchiveSkipVersion) { + ASSERT_EQ(restoredChats.size(), 0); + } + else { + ASSERT_EQ(restoredChats.size(), 4); + + EXPECT_FALSE(restoredChats[0].isMUC); + EXPECT_EQ(restoredChats[0].jid, "swift@rooms.swift.im"); + EXPECT_EQ(restoredChats[0].chatName, "swift@rooms.swift.im"); + EXPECT_EQ(restoredChats[0].activity, "Some text 0"); + EXPECT_EQ(restoredChats[0].nick, "Nick Name"); + EXPECT_EQ(restoredChats[0].impromptuJIDs.size(), 0); + EXPECT_EQ(restoredChats[0].password.get_value_or(""), ""); + + EXPECT_FALSE(restoredChats[1].isMUC); + EXPECT_EQ(restoredChats[1].jid, "testuser1@domain.com"); + EXPECT_EQ(restoredChats[1].chatName, "swift@rooms2.swift.im"); + EXPECT_EQ(restoredChats[1].activity, "Some text 1"); + EXPECT_EQ(restoredChats[1].nick, "Nick Name"); + EXPECT_EQ(restoredChats[1].impromptuJIDs.size(), 0); + EXPECT_EQ(restoredChats[1].password.get_value_or(""), "pass"); + + EXPECT_TRUE(restoredChats[2].isMUC); + EXPECT_EQ(restoredChats[2].jid, "testuser2@domain.com"); + EXPECT_EQ(restoredChats[2].chatName, "room 2"); + EXPECT_EQ(restoredChats[2].activity, "Some text 2"); + EXPECT_EQ(restoredChats[2].nick, "Nick Name"); + ASSERT_EQ(restoredChats[2].impromptuJIDs.size(), 2); + EXPECT_EQ(restoredChats[2].impromptuJIDs["testuser1@domain.com"], "testuser1@domain.com"); + EXPECT_EQ(restoredChats[2].impromptuJIDs["testuser2@domain.com"], "testuser2@domain.com"); + EXPECT_EQ(restoredChats[2].password.get_value_or(""), ""); + + EXPECT_TRUE(restoredChats[3].isMUC); + EXPECT_EQ(restoredChats[3].jid, "testuser3@domain.com"); + EXPECT_EQ(restoredChats[3].chatName, "room 3"); + EXPECT_EQ(restoredChats[3].activity, "Some text 2"); + EXPECT_EQ(restoredChats[3].nick, "Nick Name"); + ASSERT_EQ(restoredChats[3].impromptuJIDs.size(), 2); + EXPECT_EQ(restoredChats[3].impromptuJIDs["testuser1@domain.com"], "testuser1@domain.com"); + EXPECT_EQ(restoredChats[3].impromptuJIDs["testuser2@domain.com"], "testuser2@domain.com"); + EXPECT_EQ(restoredChats[3].password.get_value_or(""), "pass"); + } + } + + std::string serializedChats_BoostArchiveV10_ClassVersion_2 = "MjIgc2VyaWFsaXphdGlvbjo6YXJjaGl2ZSAxMCAwIDAgNCAyIDAgMiAwIDAgMjAgc3dpZnRAcm9vbXMuc3dpZnQuaW0gMjAgc3dpZnRAcm9vbXMuc3dpZnQuaW0gMTEgU29tZSB0ZXh0IDAgMCA5IE5pY2sgTmFtZSAwIDAgMCAwIDAgMCAwIDAgMCAwIDAgMjAgdGVzdHVzZXIxQGRvbWFpbi5jb20gMjEgc3dpZnRAcm9vbXMyLnN3aWZ0LmltIDExIFNvbWUgdGV4dCAxIDAgOSBOaWNrIE5hbWUgMCAwIDEgMCA0IHBhc3MgMCAwIDIwIHRlc3R1c2VyMkBkb21haW4uY29tIDYgcm9vbSAyIDExIFNvbWUgdGV4dCAyIDEgOSBOaWNrIE5hbWUgMiAwIDAgMCAyMCB0ZXN0dXNlcjFAZG9tYWluLmNvbSAyMCB0ZXN0dXNlcjFAZG9tYWluLmNvbSAyMCB0ZXN0dXNlcjJAZG9tYWluLmNvbSAyMCB0ZXN0dXNlcjJAZG9tYWluLmNvbSAwIDEgMCAwIDAgMTYgdXNlcjFAZG9tYWluLmNvbSAxNiB1c2VyMUBkb21haW4uY29tIDIwIHRlc3R1c2VyM0Bkb21haW4uY29tIDYgcm9vbSAzIDExIFNvbWUgdGV4dCAyIDEgOSBOaWNrIE5hbWUgMiAwIDIwIHRlc3R1c2VyMUBkb21haW4uY29tIDIwIHRlc3R1c2VyMUBkb21haW4uY29tIDIwIHRlc3R1c2VyMkBkb21haW4uY29tIDIwIHRlc3R1c2VyMkBkb21haW4uY29tIDEgMCA0IHBhc3MgMSAwIDE2IHVzZXIxQGRvbWFpbi5jb20gMTYgdXNlcjFAZG9tYWluLmNvbQ=="; + { + auto restoredChats = chatsDeserialise(serializedChats_BoostArchiveV10_ClassVersion_2); + if (archiveLibraryVersion == Swift::BoostArchiveSkipVersion) { + ASSERT_EQ(restoredChats.size(), 0); + } + else { + ASSERT_EQ(restoredChats.size(), 4); + + EXPECT_FALSE(restoredChats[0].isMUC); + EXPECT_EQ(restoredChats[0].jid, "swift@rooms.swift.im"); + EXPECT_EQ(restoredChats[0].chatName, "swift@rooms.swift.im"); + EXPECT_EQ(restoredChats[0].activity, "Some text 0"); + EXPECT_EQ(restoredChats[0].nick, "Nick Name"); + EXPECT_EQ(restoredChats[0].impromptuJIDs.size(), 0); + EXPECT_EQ(restoredChats[0].password.get_value_or(""), ""); + EXPECT_EQ(restoredChats[0].inviteesNames.size(), 0); + + EXPECT_FALSE(restoredChats[1].isMUC); + EXPECT_EQ(restoredChats[1].jid, "testuser1@domain.com"); + EXPECT_EQ(restoredChats[1].chatName, "swift@rooms2.swift.im"); + EXPECT_EQ(restoredChats[1].activity, "Some text 1"); + EXPECT_EQ(restoredChats[1].nick, "Nick Name"); + EXPECT_EQ(restoredChats[1].impromptuJIDs.size(), 0); + EXPECT_EQ(restoredChats[1].password.get_value_or(""), "pass"); + EXPECT_EQ(restoredChats[1].inviteesNames.size(), 0); + + EXPECT_TRUE(restoredChats[2].isMUC); + EXPECT_EQ(restoredChats[2].jid, "testuser2@domain.com"); + EXPECT_EQ(restoredChats[2].chatName, "room 2"); + EXPECT_EQ(restoredChats[2].activity, "Some text 2"); + EXPECT_EQ(restoredChats[2].nick, "Nick Name"); + ASSERT_EQ(restoredChats[2].impromptuJIDs.size(), 2); + EXPECT_EQ(restoredChats[2].impromptuJIDs["testuser1@domain.com"], "testuser1@domain.com"); + EXPECT_EQ(restoredChats[2].impromptuJIDs["testuser2@domain.com"], "testuser2@domain.com"); + EXPECT_EQ(restoredChats[2].password.get_value_or(""), ""); + ASSERT_EQ(restoredChats[2].inviteesNames.size(), 1); + EXPECT_EQ(restoredChats[2].inviteesNames["user1@domain.com"], "user1@domain.com"); + + EXPECT_TRUE(restoredChats[3].isMUC); + EXPECT_EQ(restoredChats[3].jid, "testuser3@domain.com"); + EXPECT_EQ(restoredChats[3].chatName, "room 3"); + EXPECT_EQ(restoredChats[3].activity, "Some text 2"); + EXPECT_EQ(restoredChats[3].nick, "Nick Name"); + ASSERT_EQ(restoredChats[3].impromptuJIDs.size(), 2); + EXPECT_EQ(restoredChats[3].impromptuJIDs["testuser1@domain.com"], "testuser1@domain.com"); + EXPECT_EQ(restoredChats[3].impromptuJIDs["testuser2@domain.com"], "testuser2@domain.com"); + EXPECT_EQ(restoredChats[3].password.get_value_or(""), "pass"); + ASSERT_EQ(restoredChats[3].inviteesNames.size(), 1); + EXPECT_EQ(restoredChats[3].inviteesNames["user1@domain.com"], "user1@domain.com"); + } + } +} + diff --git a/Swift/Controllers/Chat/UnitTest/ChatMessageParserTest.cpp b/Swift/Controllers/Chat/UnitTest/ChatMessageParserTest.cpp index 44d7834..163a38a 100644 --- a/Swift/Controllers/Chat/UnitTest/ChatMessageParserTest.cpp +++ b/Swift/Controllers/Chat/UnitTest/ChatMessageParserTest.cpp @@ -1,74 +1,292 @@ /* - * Copyright (c) 2013 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2013-2017 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ -#include <cppunit/extensions/HelperMacros.h> -#include <cppunit/extensions/TestFactoryRegistry.h> -#include <hippomocks.h> +#include <gtest/gtest.h> #include <Swift/Controllers/Chat/ChatMessageParser.h> +#include <Swift/Controllers/Highlighting/HighlightConfiguration.h> +#include <Swift/Controllers/Highlighting/HighlightManager.h> +#include <Swift/Controllers/Settings/DummySettingsProvider.h> using namespace Swift; -class ChatMessageParserTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(ChatMessageParserTest); - CPPUNIT_TEST(testFullBody); - CPPUNIT_TEST_SUITE_END(); - -public: - void setUp() { - smile1_ = ":)"; - smile1Path_ = "/blah/smile1.png"; - smile2_ = ":("; - smile2Path_ = "/blah/smile2.jpg"; - emoticons_[smile1_] = smile1Path_; - emoticons_[smile2_] = smile2Path_; - } - - void tearDown() { - emoticons_.clear(); - } - - void assertText(const ChatWindow::ChatMessage& result, size_t index, const std::string& text) { - boost::shared_ptr<ChatWindow::ChatTextMessagePart> part = boost::dynamic_pointer_cast<ChatWindow::ChatTextMessagePart>(result.getParts()[index]); - CPPUNIT_ASSERT_EQUAL(text, part->text); - } - - void assertEmoticon(const ChatWindow::ChatMessage& result, size_t index, const std::string& text, const std::string& path) { - boost::shared_ptr<ChatWindow::ChatEmoticonMessagePart> part = boost::dynamic_pointer_cast<ChatWindow::ChatEmoticonMessagePart>(result.getParts()[index]); - CPPUNIT_ASSERT_EQUAL(text, part->alternativeText); - CPPUNIT_ASSERT_EQUAL(path, part->imagePath); - } - - void assertURL(const ChatWindow::ChatMessage& result, size_t index, const std::string& text) { - boost::shared_ptr<ChatWindow::ChatURIMessagePart> part = boost::dynamic_pointer_cast<ChatWindow::ChatURIMessagePart>(result.getParts()[index]); - CPPUNIT_ASSERT_EQUAL(text, part->target); - } - - void testFullBody() { - ChatMessageParser testling(emoticons_); - ChatWindow::ChatMessage result = testling.parseMessageBody(":) shiny :( :) http://wonderland.lit/blah http://denmark.lit boom boom"); - assertEmoticon(result, 0, smile1_, smile1Path_); - assertText(result, 1, " shiny "); - assertEmoticon(result, 2, smile2_, smile2Path_); - assertText(result, 3, " "); - assertEmoticon(result, 4, smile1_, smile1Path_); - assertText(result, 5, " "); - assertURL(result, 6, "http://wonderland.lit/blah"); - assertText(result, 7, " "); - assertURL(result, 8, "http://denmark.lit"); - assertText(result, 9, " boom boom"); - } - -private: - std::map<std::string, std::string> emoticons_; - std::string smile1_; - std::string smile1Path_; - std::string smile2_; - std::string smile2Path_; +// Common test state +class ChatMessageParserTest : public ::testing::Test { +protected: + virtual void SetUp() { + smile1_ = ":)"; + smile1Path_ = "/blah/smile1.png"; + smile2_ = ":("; + smile2Path_ = "/blah/smile2.jpg"; + emoticons_[smile1_] = smile1Path_; + emoticons_[smile2_] = smile2Path_; + } + + virtual void TearDown() { + emoticons_.clear(); + } + + static void assertText(const ChatWindow::ChatMessage& result, size_t index, const std::string& text) { + ASSERT_LT(index, result.getParts().size()); + std::shared_ptr<ChatWindow::ChatTextMessagePart> part = std::dynamic_pointer_cast<ChatWindow::ChatTextMessagePart>(result.getParts()[index]); + ASSERT_EQ(text, part->text); + } + + static void assertEmoticon(const ChatWindow::ChatMessage& result, size_t index, const std::string& text, const std::string& path) { + ASSERT_LT(index, result.getParts().size()); + std::shared_ptr<ChatWindow::ChatEmoticonMessagePart> part = std::dynamic_pointer_cast<ChatWindow::ChatEmoticonMessagePart>(result.getParts()[index]); + ASSERT_NE(nullptr, part); + ASSERT_EQ(text, part->alternativeText); + ASSERT_EQ(path, part->imagePath); + } + + static void assertURL(const ChatWindow::ChatMessage& result, size_t index, const std::string& text) { + ASSERT_LT(index, result.getParts().size()); + std::shared_ptr<ChatWindow::ChatURIMessagePart> part = std::dynamic_pointer_cast<ChatWindow::ChatURIMessagePart>(result.getParts()[index]); + ASSERT_EQ(text, part->target); + } + + void assertHighlight(const ChatWindow::ChatMessage& result, size_t index, const std::string& text, const HighlightAction& action) { + ASSERT_LT(index, result.getParts().size()); + std::shared_ptr<ChatWindow::ChatHighlightingMessagePart> part = std::dynamic_pointer_cast<ChatWindow::ChatHighlightingMessagePart>(result.getParts()[index]); + ASSERT_EQ(std::string(text), part->text); + ASSERT_EQ(action, part->action); + } + + static const std::shared_ptr<HighlightConfiguration> highlightConfigFromKeyword(const std::string& keyword, bool matchCase) { + std::shared_ptr<HighlightConfiguration> config = std::make_shared<HighlightConfiguration>(); + HighlightConfiguration::KeywordHightlight keywordHighlight; + keywordHighlight.keyword = keyword; + keywordHighlight.matchCaseSensitive = matchCase; + keywordHighlight.action.setFrontColor(std::string("#121212")); + config->keywordHighlights.push_back(keywordHighlight); + return config; + } + + static const std::shared_ptr<HighlightConfiguration> mergeHighlightConfig(const std::shared_ptr<HighlightConfiguration>& configA, const std::shared_ptr<HighlightConfiguration>& configB) { + std::shared_ptr<HighlightConfiguration> config = std::make_shared<HighlightConfiguration>(); + config->keywordHighlights.insert(config->keywordHighlights.end(), configA->keywordHighlights.begin(), configA->keywordHighlights.end()); + config->keywordHighlights.insert(config->keywordHighlights.end(), configB->keywordHighlights.begin(), configB->keywordHighlights.end()); + return config; + } + + std::map<std::string, std::string> emoticons_; + std::string smile1_; + std::string smile1Path_; + std::string smile2_; + std::string smile2Path_; }; -CPPUNIT_TEST_SUITE_REGISTRATION(ChatMessageParserTest); +TEST_F(ChatMessageParserTest, testNoHighlightingWithEmtpyConfiguration) { + const std::string no_special_message = "a message with no special content"; + ChatMessageParser testling(emoticons_, std::make_shared<HighlightConfiguration>()); + auto result = testling.parseMessageBody(no_special_message); + assertText(result, 0, no_special_message); +} + +TEST_F(ChatMessageParserTest, testSimpleHighlightAndEmojiAndUrlParsing) { + auto highlightConfig = highlightConfigFromKeyword("trigger", false); + auto testling = ChatMessageParser(emoticons_, highlightConfig); + auto result = testling.parseMessageBody(":) shiny :( trigger :) http://wonderland.lit/blah http://denmark.lit boom boom"); + assertEmoticon(result, 0, smile1_, smile1Path_); + assertText(result, 1, " shiny "); + assertEmoticon(result, 2, smile2_, smile2Path_); + assertText(result, 3, " "); + assertHighlight(result, 4, "trigger", highlightConfig->keywordHighlights[0].action); + assertText(result, 5, " "); + assertEmoticon(result, 6, smile1_, smile1Path_); + assertText(result, 7, " "); + assertURL(result, 8, "http://wonderland.lit/blah"); + assertText(result, 9, " "); + assertURL(result, 10, "http://denmark.lit"); + assertText(result, 11, " boom boom"); +} + +TEST_F(ChatMessageParserTest, testNoKeywordHighlightAsPartOfLongerWords) { + auto testling = ChatMessageParser(emoticons_, highlightConfigFromKeyword("trigger", false)); + auto result = testling.parseMessageBody("testtriggermessage"); + assertText(result, 0, "testtriggermessage"); +} + +TEST_F(ChatMessageParserTest, testCaseInsensitiveKeyordHighlight) { + auto config = highlightConfigFromKeyword("trigger", true); + auto testling = ChatMessageParser(emoticons_, config); + auto result = testling.parseMessageBody("TrIgGeR"); + assertText(result, 0, "TrIgGeR"); + + testling = ChatMessageParser(emoticons_, highlightConfigFromKeyword("trigger", false)); + result = testling.parseMessageBody("TrIgGeR"); + assertHighlight(result, 0, "TrIgGeR", config->keywordHighlights[0].action); +} + +TEST_F(ChatMessageParserTest, testMultipleKeywordHighlights) { + auto config = mergeHighlightConfig(highlightConfigFromKeyword("one", false), highlightConfigFromKeyword("three", false)); + auto testling = ChatMessageParser(emoticons_, config); + auto result = testling.parseMessageBody("zero one two three"); + assertText(result, 0, "zero "); + assertHighlight(result, 1, "one", config->keywordHighlights[0].action); + assertText(result, 2, " two "); + assertHighlight(result, 3, "three", config->keywordHighlights[0].action); +} + +TEST_F(ChatMessageParserTest, testMultipleCaseInsensitiveKeywordHighlights) { + auto config = mergeHighlightConfig(highlightConfigFromKeyword("one", false), highlightConfigFromKeyword("three", false)); + auto testling = ChatMessageParser(emoticons_, config); + auto result = testling.parseMessageBody("zero oNe two tHrEe"); + assertText(result, 0, "zero "); + assertHighlight(result, 1, "oNe", config->keywordHighlights[0].action); + assertText(result, 2, " two "); + assertHighlight(result, 3, "tHrEe", config->keywordHighlights[0].action); +} + +TEST_F(ChatMessageParserTest, testMultipleCaseSensitiveKeywordHighlights) { + auto config = mergeHighlightConfig(highlightConfigFromKeyword("one", false), highlightConfigFromKeyword("three", true)); + auto testling = ChatMessageParser(emoticons_, config); + auto result = testling.parseMessageBody("zero oNe two tHrEe"); + assertText(result, 0, "zero "); + assertHighlight(result, 1, "oNe", config->keywordHighlights[0].action); + assertText(result, 2, " two tHrEe"); + + config = mergeHighlightConfig(highlightConfigFromKeyword("one", true), highlightConfigFromKeyword("three", false)); + testling = ChatMessageParser(emoticons_, config); + result = testling.parseMessageBody("zero oNe two tHrEe"); + assertText(result, 0, "zero oNe two "); + assertHighlight(result, 1, "tHrEe", config->keywordHighlights[0].action); +} + +TEST_F(ChatMessageParserTest, testOneEmoticon) { + auto testling = ChatMessageParser(emoticons_, std::make_shared<HighlightConfiguration>()); + auto result = testling.parseMessageBody(" :) "); + assertText(result, 0, " "); + assertEmoticon(result, 1, smile1_, smile1Path_); + assertText(result, 2, " "); +} + +TEST_F(ChatMessageParserTest, testBareEmoticon) { + auto testling = ChatMessageParser(emoticons_, std::make_shared<HighlightConfiguration>()); + auto result = testling.parseMessageBody(":)"); + assertEmoticon(result, 0, smile1_, smile1Path_); +} + +TEST_F(ChatMessageParserTest, testHiddenEmoticon) { + auto testling = ChatMessageParser(emoticons_, std::make_shared<HighlightConfiguration>()); + auto result = testling.parseMessageBody("b:)a"); + assertText(result, 0, "b:)a"); +} + +TEST_F(ChatMessageParserTest, testEndlineEmoticon) { + auto testling = ChatMessageParser(emoticons_, std::make_shared<HighlightConfiguration>()); + auto result = testling.parseMessageBody("Lazy:)"); + assertText(result, 0, "Lazy"); + assertEmoticon(result, 1, smile1_, smile1Path_); +} + +TEST_F(ChatMessageParserTest, testBoundedEmoticons) { + auto testling = ChatMessageParser(emoticons_, std::make_shared<HighlightConfiguration>()); + auto result = testling.parseMessageBody(":)Lazy:("); + assertEmoticon(result, 0, smile1_, smile1Path_); + assertText(result, 1, "Lazy"); + assertEmoticon(result, 2, smile2_, smile2Path_); +} + +TEST_F(ChatMessageParserTest, testEmoticonParenthesis) { + auto testling = ChatMessageParser(emoticons_, std::make_shared<HighlightConfiguration>()); + auto result = testling.parseMessageBody("(Like this :))"); + assertText(result, 0, "(Like this "); + assertEmoticon(result, 1, smile1_, smile1Path_); + assertText(result, 2, ")"); +} + +TEST_F(ChatMessageParserTest, testSenderAndKeywordHighlighting) { + auto config = std::make_shared<HighlightConfiguration>(); + auto contactHighlight = HighlightConfiguration::ContactHighlight(); + contactHighlight.action.setFrontColor(std::string("#f0f0f0")); + contactHighlight.action.setBackColor(std::string("#0f0f0f")); + contactHighlight.name = "Romeo"; + config->contactHighlights.push_back(contactHighlight); + auto keywordHighlight = HighlightConfiguration::KeywordHightlight(); + keywordHighlight.action.setFrontColor(std::string("#abcdef")); + keywordHighlight.action.setBackColor(std::string("#fedcba")); + keywordHighlight.keyword = "XMPP"; + config->keywordHighlights.push_back(keywordHighlight); + auto testling = ChatMessageParser(emoticons_, config); + auto result = testling.parseMessageBody("Heard any news about xmpp recently?", "Romeo"); + assertText(result, 0, "Heard any news about "); + assertHighlight(result, 1, "xmpp", keywordHighlight.action); + assertText(result, 2, " recently?"); + ASSERT_EQ(contactHighlight.action, result.getHighlightActionSender()); + ASSERT_EQ(HighlightAction(), result.getHighlightActionOwnMention()); +} + +TEST_F(ChatMessageParserTest, testKeywordWithEmptyActionIsIgnored) { + auto config = std::make_shared<HighlightConfiguration>(); + auto keywordHighlight = HighlightConfiguration::KeywordHightlight(); + keywordHighlight.keyword = "XMPP"; + config->keywordHighlights.push_back(keywordHighlight); + auto testling = ChatMessageParser(emoticons_, config); + auto result = testling.parseMessageBody("Heard any news about xmpp recently?", "Romeo"); + assertText(result, 0, "Heard any news about xmpp recently?"); + ASSERT_EQ(HighlightAction(), result.getHighlightActionOwnMention()); +} + +TEST_F(ChatMessageParserTest, testMeMessageAndOwnMention) { + auto config = std::make_shared<HighlightConfiguration>(); + config->ownMentionAction.setFrontColor(std::string("#f0f0f0")); + config->ownMentionAction.setBackColor(std::string("#0f0f0f")); + config->ownMentionAction.setSoundFilePath(std::string("someSoundFile.wav")); + auto ownMentionActionForPart = config->ownMentionAction; + ownMentionActionForPart.setSoundFilePath(boost::optional<std::string>()); + auto testling = ChatMessageParser(emoticons_, config); + testling.setNick("Juliet"); + auto result = testling.parseMessageBody("/me wonders when Juliet is coming?", "Romeo"); + assertText(result, 0, "wonders when "); + assertHighlight(result, 1, "Juliet", ownMentionActionForPart); + assertText(result, 2, " is coming?"); + ASSERT_EQ(true, result.isMeCommand()); + ASSERT_EQ(config->ownMentionAction, result.getHighlightActionOwnMention()); +} + +TEST_F(ChatMessageParserTest, testSoundAndNotificationOnDirectMessage) { + auto defaultConfiguration = std::make_shared<HighlightConfiguration>(); + defaultConfiguration->playSoundOnIncomingDirectMessages = true; + defaultConfiguration->showNotificationOnIncomingDirectMessages = true; + defaultConfiguration->ownMentionAction.setFrontColor(std::string("black")); + defaultConfiguration->ownMentionAction.setBackColor(std::string("yellow")); + defaultConfiguration->ownMentionAction.setSoundFilePath(std::string("")); + defaultConfiguration->ownMentionAction.setSystemNotificationEnabled(true); + + auto testling = ChatMessageParser(emoticons_, defaultConfiguration, ChatMessageParser::Mode::Chat); + auto result = testling.parseMessageBody("I wonder when Juliet is coming?", "Romeo"); + + ASSERT_EQ(std::string(""), result.getHighlightActionDirectMessage().getSoundFilePath().get_value_or(std::string("somethingElse"))); + ASSERT_EQ(true, result.getHighlightActionDirectMessage().isSystemNotificationEnabled()); + ASSERT_EQ(HighlightAction(), result.getHighlightActionOwnMention()); +} + +TEST_F(ChatMessageParserTest, testWithDefaultConfiguration) { + DummySettingsProvider settings; + HighlightManager manager(&settings); + manager.resetToDefaultConfiguration(); + auto testling = ChatMessageParser(emoticons_, manager.getConfiguration(), ChatMessageParser::Mode::GroupChat); + testling.setNick("Juliet"); + auto result = testling.parseMessageBody("Hello, how is it going?", "Romeo"); + assertText(result, 0, "Hello, how is it going?"); + ASSERT_EQ(HighlightAction(), result.getHighlightActionOwnMention()); + ASSERT_EQ(HighlightAction(), result.getHighlightActionDirectMessage()); + ASSERT_EQ(HighlightAction(), result.getHighlightActionGroupMessage()); + ASSERT_EQ(HighlightAction(), result.getHighlightActionSender()); + result = testling.parseMessageBody("Juliet, seen the new interface design?", "Romeo"); + auto mentionKeywordAction = manager.getConfiguration()->ownMentionAction; + mentionKeywordAction.setSoundFilePath(boost::optional<std::string>()); + mentionKeywordAction.setSystemNotificationEnabled(false); + assertHighlight(result, 0, "Juliet", mentionKeywordAction); + assertText(result, 1, ", seen the new interface design?"); + ASSERT_EQ(manager.getConfiguration()->ownMentionAction, result.getHighlightActionOwnMention()); + ASSERT_EQ(HighlightAction(), result.getHighlightActionDirectMessage()); + ASSERT_EQ(HighlightAction(), result.getHighlightActionGroupMessage()); + ASSERT_EQ(HighlightAction(), result.getHighlightActionSender()); +} diff --git a/Swift/Controllers/Chat/UnitTest/ChatsManagerTest.cpp b/Swift/Controllers/Chat/UnitTest/ChatsManagerTest.cpp index f5a3003..954dd2f 100644 --- a/Swift/Controllers/Chat/UnitTest/ChatsManagerTest.cpp +++ b/Swift/Controllers/Chat/UnitTest/ChatsManagerTest.cpp @@ -1,495 +1,1904 @@ /* - * Copyright (c) 2010-2012 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ +#include <map> +#include <set> + +#include <boost/bind.hpp> + #include <cppunit/extensions/HelperMacros.h> #include <cppunit/extensions/TestFactoryRegistry.h> #include <hippomocks.h> -#include <boost/bind.hpp> +#include <Swiften/Avatars/AvatarMemoryStorage.h> +#include <Swiften/Avatars/NullAvatarManager.h> +#include <Swiften/Base/Algorithm.h> +#include <Swiften/Base/Log.h> +#include <Swiften/Base/LogSerializers.h> +#include <Swiften/Client/Client.h> +#include <Swiften/Client/ClientBlockListManager.h> +#include <Swiften/Client/DummyStanzaChannel.h> +#include <Swiften/Client/NickResolver.h> +#include <Swiften/Crypto/CryptoProvider.h> +#include <Swiften/Crypto/PlatformCryptoProvider.h> +#include <Swiften/Disco/DummyEntityCapsProvider.h> +#include <Swiften/Elements/CarbonsReceived.h> +#include <Swiften/Elements/CarbonsSent.h> +#include <Swiften/Elements/DeliveryReceipt.h> +#include <Swiften/Elements/DeliveryReceiptRequest.h> +#include <Swiften/Elements/Forwarded.h> +#include <Swiften/Elements/MUCInvitationPayload.h> +#include <Swiften/Elements/MUCUserPayload.h> +#include <Swiften/Elements/PrivateStorage.h> +#include <Swiften/Elements/Storage.h> +#include <Swiften/FileTransfer/UnitTest/DummyFileTransferManager.h> +#include <Swiften/Jingle/JingleSessionManager.h> +#include <Swiften/MUC/MUCManager.h> +#include <Swiften/MUC/UnitTest/MockMUC.h> +#include <Swiften/MUC/MUCBookmarkManager.h> +#include <Swiften/Network/DummyTimerFactory.h> +#include <Swiften/Presence/DirectedPresenceSender.h> +#include <Swiften/Presence/PresenceOracle.h> +#include <Swiften/Presence/StanzaChannelPresenceSender.h> +#include <Swiften/Queries/DummyIQChannel.h> +#include <Swiften/Roster/XMPPRosterImpl.h> +#include <Swiften/VCards/VCardManager.h> +#include <Swiften/VCards/VCardMemoryStorage.h> +#include <Swiften/Whiteboard/WhiteboardSessionManager.h> +#include <Swift/Controllers/Chat/ChatController.h> #include <Swift/Controllers/Chat/ChatsManager.h> - +#include <Swift/Controllers/Chat/Chattables.h> +#include <Swift/Controllers/Chat/MUCController.h> #include <Swift/Controllers/Chat/UnitTest/MockChatListWindow.h> -#include <Swift/Controllers/UIInterfaces/ChatWindow.h> +#include <Swift/Controllers/EventNotifier.h> +#include <Swift/Controllers/FileTransfer/FileTransferOverview.h> +#include <Swift/Controllers/ProfileSettingsProvider.h> +#include <Swift/Controllers/SettingConstants.h> #include <Swift/Controllers/Settings/DummySettingsProvider.h> -#include <Swift/Controllers/UIInterfaces/ChatWindowFactory.h> +#include <Swift/Controllers/UIEvents/AddMUCBookmarkUIEvent.h> +#include <Swift/Controllers/UIEvents/CreateImpromptuMUCUIEvent.h> +#include <Swift/Controllers/UIEvents/JoinMUCUIEvent.h> +#include <Swift/Controllers/UIEvents/RequestChatUIEvent.h> +#include <Swift/Controllers/UIEvents/UIEventStream.h> #include <Swift/Controllers/UIInterfaces/ChatListWindowFactory.h> -#include <Swift/Controllers/UIInterfaces/WhiteboardWindowFactory.h> +#include <Swift/Controllers/UIInterfaces/ChatWindow.h> +#include <Swift/Controllers/UIInterfaces/ChatWindowFactory.h> #include <Swift/Controllers/UIInterfaces/JoinMUCWindowFactory.h> #include <Swift/Controllers/UIInterfaces/MUCSearchWindowFactory.h> -#include <Swiften/Client/Client.h> -#include <Swiften/Disco/EntityCapsManager.h> -#include <Swiften/Disco/CapsProvider.h> -#include <Swiften/MUC/MUCManager.h> -#include <Swift/Controllers/Chat/ChatController.h> -#include <Swift/Controllers/XMPPEvents/EventController.h> -#include <Swift/Controllers/Chat/MUCController.h> -#include <Swiften/Presence/StanzaChannelPresenceSender.h> -#include <Swiften/Avatars/NullAvatarManager.h> -#include <Swiften/Avatars/AvatarMemoryStorage.h> -#include <Swiften/VCards/VCardManager.h> -#include <Swiften/VCards/VCardMemoryStorage.h> -#include <Swiften/Client/NickResolver.h> -#include <Swiften/Presence/DirectedPresenceSender.h> -#include <Swiften/Roster/XMPPRosterImpl.h> +#include <Swift/Controllers/UIInterfaces/WhiteboardWindowFactory.h> #include <Swift/Controllers/UnitTest/MockChatWindow.h> -#include <Swiften/Client/DummyStanzaChannel.h> -#include <Swiften/Queries/DummyIQChannel.h> -#include <Swiften/Presence/PresenceOracle.h> -#include <Swiften/Jingle/JingleSessionManager.h> -#include <Swiften/FileTransfer/UnitTest/DummyFileTransferManager.h> -#include <Swift/Controllers/UIEvents/RequestChatUIEvent.h> -#include <Swift/Controllers/UIEvents/JoinMUCUIEvent.h> -#include <Swift/Controllers/UIEvents/UIEventStream.h> -#include <Swift/Controllers/ProfileSettingsProvider.h> -#include <Swift/Controllers/FileTransfer/FileTransferOverview.h> -#include <Swiften/Elements/DeliveryReceiptRequest.h> -#include <Swiften/Elements/DeliveryReceipt.h> -#include <Swiften/Base/Algorithm.h> -#include <Swift/Controllers/SettingConstants.h> #include <Swift/Controllers/WhiteboardManager.h> -#include <Swiften/Whiteboard/WhiteboardSessionManager.h> -#include <Swiften/Client/ClientBlockListManager.h> +#include <Swift/Controllers/XMPPEvents/EventController.h> + +#include <SwifTools/Notifier/Notifier.h> + +#include <Swift/QtUI/QtSwiftUtil.h> using namespace Swift; -class DummyCapsProvider : public CapsProvider { - DiscoInfo::ref getCaps(const std::string&) const {return DiscoInfo::ref(new DiscoInfo());} +class DummyNotifier : public Notifier { + public: + virtual void showMessage( + Type type, + const std::string& subject, + const std::string& description, + const boost::filesystem::path& picture, + boost::function<void()> callback) { + notifications.push_back({type, subject, description, picture, callback}); + } + + /** Remove any pending callbacks. */ + virtual void purgeCallbacks() { + + } + + public: + struct Notification { + Type type; + std::string subject; + std::string description; + boost::filesystem::path picture; + boost::function<void()> callback; + }; + + public: + std::vector<Notification> notifications; +}; + +class ExtendedChatsManager : public ChatsManager { +public: + ExtendedChatsManager(JID jid, StanzaChannel* stanzaChannel, IQRouter* iqRouter, EventController* eventController, ChatWindowFactory* chatWindowFactory, JoinMUCWindowFactory* joinMUCWindowFactory, NickResolver* nickResolver, PresenceOracle* presenceOracle, PresenceSender* presenceSender, UIEventStream* uiEventStream, ChatListWindowFactory* chatListWindowFactory, bool useDelayForLatency, TimerFactory* timerFactory, MUCRegistry* mucRegistry, EntityCapsProvider* entityCapsProvider, MUCManager* mucManager, MUCSearchWindowFactory* mucSearchWindowFactory, ProfileSettingsProvider* profileSettings, FileTransferOverview* ftOverview, XMPPRoster* roster, bool eagleMode, SettingsProvider* settings, HistoryController* historyController_, WhiteboardManager* whiteboardManager, HighlightManager* highlightManager, ClientBlockListManager* clientBlockListManager, const std::map<std::string, std::string>& emoticons, VCardManager* vcardManager, Chattables& chattables) : + ChatsManager(jid, stanzaChannel, iqRouter, eventController, chatWindowFactory, joinMUCWindowFactory, nickResolver, presenceOracle, presenceSender, uiEventStream, chatListWindowFactory, useDelayForLatency, timerFactory, mucRegistry, entityCapsProvider, mucManager, mucSearchWindowFactory, profileSettings, ftOverview, roster, eagleMode, settings, historyController_, whiteboardManager, highlightManager, clientBlockListManager, emoticons, vcardManager, chattables) { + } + MUCBookmarkManager* getBookmarkManager() { + return mucBookmarkManager_; + } }; + class ChatsManagerTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(ChatsManagerTest); - CPPUNIT_TEST(testFirstOpenWindowIncoming); - CPPUNIT_TEST(testSecondOpenWindowIncoming); - CPPUNIT_TEST(testFirstOpenWindowOutgoing); - CPPUNIT_TEST(testFirstOpenWindowBareToFull); - CPPUNIT_TEST(testSecondWindow); - CPPUNIT_TEST(testUnbindRebind); - CPPUNIT_TEST(testNoDuplicateUnbind); - CPPUNIT_TEST(testThreeMUCWindows); - CPPUNIT_TEST(testChatControllerPresenceAccessUpdatedOnRemoveFromRoster); - CPPUNIT_TEST(testChatControllerPresenceAccessUpdatedOnAddToRoster); - CPPUNIT_TEST(testChatControllerPresenceAccessUpdatedOnSubscriptionChangeToBoth); - CPPUNIT_TEST(testChatControllerPresenceAccessUpdatedOnSubscriptionChangeToFrom); - CPPUNIT_TEST_SUITE_END(); - + CPPUNIT_TEST_SUITE(ChatsManagerTest); + CPPUNIT_TEST(testFirstOpenWindowIncoming); + CPPUNIT_TEST(testSecondOpenWindowIncoming); + CPPUNIT_TEST(testFirstOpenWindowOutgoing); + CPPUNIT_TEST(testFirstOpenWindowBareToFull); + CPPUNIT_TEST(testSecondWindow); + CPPUNIT_TEST(testUnbindRebind); + CPPUNIT_TEST(testNoDuplicateUnbind); + CPPUNIT_TEST(testThreeMUCWindows); + CPPUNIT_TEST(testChatControllerPresenceAccessUpdatedOnRemoveFromRoster); + CPPUNIT_TEST(testChatControllerPresenceAccessUpdatedOnAddToRoster); + CPPUNIT_TEST(testChatControllerPresenceAccessUpdatedOnSubscriptionChangeToBoth); + CPPUNIT_TEST(testChatControllerPresenceAccessUpdatedOnSubscriptionChangeToFrom); + CPPUNIT_TEST(testChatControllerFullJIDBindingOnMessageAndNotReceipt); + CPPUNIT_TEST(testChatControllerFullJIDBindingOnTypingAndNotActive); + CPPUNIT_TEST(testLocalMUCServiceDiscoveryResetOnDisconnect); + CPPUNIT_TEST(testPresenceChangeDoesNotReplaceMUCInvite); + CPPUNIT_TEST(testNotSplittingMUCPresenceJoinLeaveLinesOnChatStateNotifications); + + // MUC PM Tests + CPPUNIT_TEST(testChatControllerPMPresenceHandling); + CPPUNIT_TEST(testChatControllerMucPmUnavailableErrorHandling); + + // Highlighting tests + CPPUNIT_TEST(testChatControllerHighlightingNotificationTesting); + CPPUNIT_TEST(testChatControllerHighlightingNotificationDeduplicateSounds); + CPPUNIT_TEST(testChatControllerHighlightingNotificationKeyword); + + CPPUNIT_TEST(testChatControllerMeMessageHandling); + CPPUNIT_TEST(testRestartingMUCComponentCrash); + CPPUNIT_TEST(testChatControllerMeMessageHandlingInMUC); + + // Carbons tests + CPPUNIT_TEST(testCarbonsForwardedIncomingMessageToSecondResource); + CPPUNIT_TEST(testCarbonsForwardedOutgoingMessageFromSecondResource); + CPPUNIT_TEST(testCarbonsForwardedIncomingDuplicates); + + // Message correction tests + CPPUNIT_TEST(testChatControllerMessageCorrectionCorrectReplaceID); + CPPUNIT_TEST(testChatControllerMessageCorrectionIncorrectReplaceID); + CPPUNIT_TEST(testChatControllerMessageCorrectionReplaceBySameResource); + CPPUNIT_TEST(testChatControllerMessageCorrectionReplaceByOtherResource); + CPPUNIT_TEST(testMUCControllerMessageCorrectionNoIDMatchRequired); + + // Chat window title tests + CPPUNIT_TEST(testImpromptuChatTitle); + CPPUNIT_TEST(testImpromptuChatWindowTitle); + CPPUNIT_TEST(testStandardMUCChatWindowTitle); + + // Bookmark tests + CPPUNIT_TEST(testReceivingBookmarksWithDomainJID); + CPPUNIT_TEST(testReceivingBookmarksWithBareJID); + CPPUNIT_TEST(testReceivingBookmarksWithFullJID); + CPPUNIT_TEST(testAutoJoinBookmarksAndChattables); + CPPUNIT_TEST(testJoinNoAutojoinBookmark); + CPPUNIT_TEST(testJoinAndBookmarkMUC); + CPPUNIT_TEST(testReceivingNoBookmarks); + CPPUNIT_TEST(testReceivingNullBookmarks); + CPPUNIT_TEST(testReceivingBookmarksError); + + CPPUNIT_TEST_SUITE_END(); + public: - void setUp() { - mocks_ = new MockRepository(); - jid_ = JID("test@test.com/resource"); - stanzaChannel_ = new DummyStanzaChannel(); - iqChannel_ = new DummyIQChannel(); - iqRouter_ = new IQRouter(iqChannel_); - capsProvider_ = new DummyCapsProvider(); - eventController_ = new EventController(); - chatWindowFactory_ = mocks_->InterfaceMock<ChatWindowFactory>(); - joinMUCWindowFactory_ = mocks_->InterfaceMock<JoinMUCWindowFactory>(); - xmppRoster_ = new XMPPRosterImpl(); - mucRegistry_ = new MUCRegistry(); - nickResolver_ = new NickResolver(jid_.toBare(), xmppRoster_, NULL, mucRegistry_); - presenceOracle_ = new PresenceOracle(stanzaChannel_); - serverDiscoInfo_ = boost::make_shared<DiscoInfo>(); - presenceSender_ = new StanzaChannelPresenceSender(stanzaChannel_); - directedPresenceSender_ = new DirectedPresenceSender(presenceSender_); - mucManager_ = new MUCManager(stanzaChannel_, iqRouter_, directedPresenceSender_, mucRegistry_); - uiEventStream_ = new UIEventStream(); - entityCapsManager_ = new EntityCapsManager(capsProvider_, stanzaChannel_); - chatListWindowFactory_ = mocks_->InterfaceMock<ChatListWindowFactory>(); - mucSearchWindowFactory_ = mocks_->InterfaceMock<MUCSearchWindowFactory>(); - settings_ = new DummySettingsProvider(); - profileSettings_ = new ProfileSettingsProvider("a", settings_); - chatListWindow_ = new MockChatListWindow(); - ftManager_ = new DummyFileTransferManager(); - ftOverview_ = new FileTransferOverview(ftManager_); - avatarManager_ = new NullAvatarManager(); - wbSessionManager_ = new WhiteboardSessionManager(iqRouter_, stanzaChannel_, presenceOracle_, entityCapsManager_); - wbManager_ = new WhiteboardManager(whiteboardWindowFactory_, uiEventStream_, nickResolver_, wbSessionManager_); - highlightManager_ = new HighlightManager(settings_); - - mocks_->ExpectCall(chatListWindowFactory_, ChatListWindowFactory::createChatListWindow).With(uiEventStream_).Return(chatListWindow_); - clientBlockListManager_ = new ClientBlockListManager(iqRouter_); - manager_ = new ChatsManager(jid_, stanzaChannel_, iqRouter_, eventController_, chatWindowFactory_, joinMUCWindowFactory_, nickResolver_, presenceOracle_, directedPresenceSender_, uiEventStream_, chatListWindowFactory_, true, NULL, mucRegistry_, entityCapsManager_, mucManager_, mucSearchWindowFactory_, profileSettings_, ftOverview_, xmppRoster_, false, settings_, NULL, wbManager_, highlightManager_, clientBlockListManager_, emoticons_, NULL); - - manager_->setAvatarManager(avatarManager_); - } - - void tearDown() { - delete highlightManager_; - //delete chatListWindowFactory - delete profileSettings_; - delete avatarManager_; - delete manager_; - delete clientBlockListManager_; - delete ftOverview_; - delete ftManager_; - delete wbSessionManager_; - delete wbManager_; - delete directedPresenceSender_; - delete presenceSender_; - delete presenceOracle_; - delete nickResolver_; - delete mucRegistry_; - delete stanzaChannel_; - delete eventController_; - delete iqRouter_; - delete iqChannel_; - delete uiEventStream_; - delete mucManager_; - delete xmppRoster_; - delete entityCapsManager_; - delete capsProvider_; - delete chatListWindow_; - delete mocks_; - delete settings_; - } - - void testFirstOpenWindowIncoming() { - JID messageJID("testling@test.com/resource1"); - - MockChatWindow* window = new MockChatWindow();//mocks_->InterfaceMock<ChatWindow>(); - mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(messageJID, uiEventStream_).Return(window); - - boost::shared_ptr<Message> message(new Message()); - message->setFrom(messageJID); - std::string body("This is a legible message. >HEH@)oeueu"); - message->setBody(body); - manager_->handleIncomingMessage(message); - CPPUNIT_ASSERT_EQUAL(body, window->lastMessageBody_); - } - - void testSecondOpenWindowIncoming() { - JID messageJID1("testling@test.com/resource1"); - - MockChatWindow* window1 = new MockChatWindow();//mocks_->InterfaceMock<ChatWindow>(); - mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(messageJID1, uiEventStream_).Return(window1); - - boost::shared_ptr<Message> message1(new Message()); - message1->setFrom(messageJID1); - std::string body1("This is a legible message. >HEH@)oeueu"); - message1->setBody(body1); - manager_->handleIncomingMessage(message1); - CPPUNIT_ASSERT_EQUAL(body1, window1->lastMessageBody_); - - JID messageJID2("testling@test.com/resource2"); - - //MockChatWindow* window2 = new MockChatWindow();//mocks_->InterfaceMock<ChatWindow>(); - //mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(messageJID2, uiEventStream_).Return(window2); - - boost::shared_ptr<Message> message2(new Message()); - message2->setFrom(messageJID2); - std::string body2("This is a legible message. .cmaulm.chul"); - message2->setBody(body2); - manager_->handleIncomingMessage(message2); - CPPUNIT_ASSERT_EQUAL(body2, window1->lastMessageBody_); - } - - void testFirstOpenWindowOutgoing() { - std::string messageJIDString("testling@test.com"); - - ChatWindow* window = new MockChatWindow();//mocks_->InterfaceMock<ChatWindow>(); - mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(JID(messageJIDString), uiEventStream_).Return(window); - - uiEventStream_->send(boost::shared_ptr<UIEvent>(new RequestChatUIEvent(JID(messageJIDString)))); - } - - - void testFirstOpenWindowBareToFull() { - std::string bareJIDString("testling@test.com"); - std::string fullJIDString("testling@test.com/resource1"); - - MockChatWindow* window = new MockChatWindow();//mocks_->InterfaceMock<ChatWindow>(); - mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(JID(bareJIDString), uiEventStream_).Return(window); - uiEventStream_->send(boost::shared_ptr<UIEvent>(new RequestChatUIEvent(JID(bareJIDString)))); - - boost::shared_ptr<Message> message(new Message()); - message->setFrom(JID(fullJIDString)); - std::string body("This is a legible message. mjuga3089gm8G(*>M)@*("); - message->setBody(body); - manager_->handleIncomingMessage(message); - CPPUNIT_ASSERT_EQUAL(body, window->lastMessageBody_); - } - - void testSecondWindow() { - std::string messageJIDString1("testling1@test.com"); - ChatWindow* window1 = new MockChatWindow();//mocks_->InterfaceMock<ChatWindow>(); - mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(JID(messageJIDString1), uiEventStream_).Return(window1); - uiEventStream_->send(boost::shared_ptr<UIEvent>(new RequestChatUIEvent(JID(messageJIDString1)))); - - std::string messageJIDString2("testling2@test.com"); - ChatWindow* window2 = new MockChatWindow();//mocks_->InterfaceMock<ChatWindow>(); - mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(JID(messageJIDString2), uiEventStream_).Return(window2); - - uiEventStream_->send(boost::shared_ptr<UIEvent>(new RequestChatUIEvent(JID(messageJIDString2)))); - } - - /** Complete cycle. - Create unbound window. - Bind it. - Unbind it. - Rebind it. - */ - void testUnbindRebind() { - std::string bareJIDString("testling@test.com"); - std::string fullJIDString1("testling@test.com/resource1"); - std::string fullJIDString2("testling@test.com/resource2"); - - MockChatWindow* window = new MockChatWindow();//mocks_->InterfaceMock<ChatWindow>(); - mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(JID(bareJIDString), uiEventStream_).Return(window); - uiEventStream_->send(boost::shared_ptr<UIEvent>(new RequestChatUIEvent(JID(bareJIDString)))); - - boost::shared_ptr<Message> message1(new Message()); - message1->setFrom(JID(fullJIDString1)); - std::string messageBody1("This is a legible message."); - message1->setBody(messageBody1); - manager_->handleIncomingMessage(message1); - CPPUNIT_ASSERT_EQUAL(messageBody1, window->lastMessageBody_); - - boost::shared_ptr<Presence> jid1Online(new Presence()); - jid1Online->setFrom(JID(fullJIDString1)); - boost::shared_ptr<Presence> jid1Offline(new Presence()); - jid1Offline->setFrom(JID(fullJIDString1)); - jid1Offline->setType(Presence::Unavailable); - presenceOracle_->onPresenceChange(jid1Offline); - - boost::shared_ptr<Message> message2(new Message()); - message2->setFrom(JID(fullJIDString2)); - std::string messageBody2("This is another legible message."); - message2->setBody(messageBody2); - manager_->handleIncomingMessage(message2); - CPPUNIT_ASSERT_EQUAL(messageBody2, window->lastMessageBody_); - } - - /** - * Test that MUC PMs get opened in the right windows - */ - void testThreeMUCWindows() { - JID muc("testling@test.com"); - ChatWindow* mucWindow = new MockChatWindow(); - mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(muc, uiEventStream_).Return(mucWindow); - uiEventStream_->send(boost::make_shared<JoinMUCUIEvent>(muc, std::string("nick"))); - - - std::string messageJIDString1("testling@test.com/1"); - ChatWindow* window1 = new MockChatWindow();//mocks_->InterfaceMock<ChatWindow>(); - mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(JID(messageJIDString1), uiEventStream_).Return(window1); - uiEventStream_->send(boost::shared_ptr<UIEvent>(new RequestChatUIEvent(JID(messageJIDString1)))); - - std::string messageJIDString2("testling@test.com/2"); - ChatWindow* window2 = new MockChatWindow();//mocks_->InterfaceMock<ChatWindow>(); - mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(JID(messageJIDString2), uiEventStream_).Return(window2); - - uiEventStream_->send(boost::shared_ptr<UIEvent>(new RequestChatUIEvent(JID(messageJIDString2)))); - - std::string messageJIDString3("testling@test.com/3"); - ChatWindow* window3 = new MockChatWindow();//mocks_->InterfaceMock<ChatWindow>(); - mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(JID(messageJIDString3), uiEventStream_).Return(window3); - - uiEventStream_->send(boost::shared_ptr<UIEvent>(new RequestChatUIEvent(JID(messageJIDString3)))); - - /* Refetch an earlier window */ - /* We do not expect a new window to be created */ - uiEventStream_->send(boost::shared_ptr<UIEvent>(new RequestChatUIEvent(JID(messageJIDString1)))); - - } - - /** - Test that a second window isn't unbound where there's already an unbound one. - Bind 1 - Bind 2 - Unbind 1 - Unbind 2 (but it doesn't) - Sent to bound 2 - Rebind 1 - */ - void testNoDuplicateUnbind() { - JID messageJID1("testling@test.com/resource1"); - - MockChatWindow* window1 = new MockChatWindow();//mocks_->InterfaceMock<ChatWindow>(); - mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(messageJID1, uiEventStream_).Return(window1); - - boost::shared_ptr<Message> message1(new Message()); - message1->setFrom(messageJID1); - message1->setBody("This is a legible message1."); - manager_->handleIncomingMessage(message1); - - JID messageJID2("testling@test.com/resource2"); - - //MockChatWindow* window2 = new MockChatWindow();//mocks_->InterfaceMock<ChatWindow>(); - //mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(messageJID2, uiEventStream_).Return(window2); - - boost::shared_ptr<Message> message2(new Message()); - message2->setFrom(messageJID2); - message2->setBody("This is a legible message2."); - manager_->handleIncomingMessage(message2); - - boost::shared_ptr<Presence> jid1Online(new Presence()); - jid1Online->setFrom(JID(messageJID1)); - boost::shared_ptr<Presence> jid1Offline(new Presence()); - jid1Offline->setFrom(JID(messageJID1)); - jid1Offline->setType(Presence::Unavailable); - presenceOracle_->onPresenceChange(jid1Offline); - - boost::shared_ptr<Presence> jid2Online(new Presence()); - jid2Online->setFrom(JID(messageJID2)); - boost::shared_ptr<Presence> jid2Offline(new Presence()); - jid2Offline->setFrom(JID(messageJID2)); - jid2Offline->setType(Presence::Unavailable); - presenceOracle_->onPresenceChange(jid2Offline); - - JID messageJID3("testling@test.com/resource3"); - - boost::shared_ptr<Message> message3(new Message()); - message3->setFrom(messageJID3); - std::string body3("This is a legible message3."); - message3->setBody(body3); - manager_->handleIncomingMessage(message3); - CPPUNIT_ASSERT_EQUAL(body3, window1->lastMessageBody_); - - boost::shared_ptr<Message> message2b(new Message()); - message2b->setFrom(messageJID2); - std::string body2b("This is a legible message2b."); - message2b->setBody(body2b); - manager_->handleIncomingMessage(message2b); - CPPUNIT_ASSERT_EQUAL(body2b, window1->lastMessageBody_); - } - - /** - * Test that ChatController doesn't send receipts anymore after removal of the contact from the roster. - */ - void testChatControllerPresenceAccessUpdatedOnRemoveFromRoster() { - JID messageJID("testling@test.com/resource1"); - xmppRoster_->addContact(messageJID, "foo", std::vector<std::string>(), RosterItemPayload::Both); - - MockChatWindow* window = new MockChatWindow();//mocks_->InterfaceMock<ChatWindow>(); - mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(messageJID, uiEventStream_).Return(window); - settings_->storeSetting(SettingConstants::REQUEST_DELIVERYRECEIPTS, true); - - boost::shared_ptr<Message> message = makeDeliveryReceiptTestMessage(messageJID, "1"); - manager_->handleIncomingMessage(message); - Stanza::ref stanzaContactOnRoster = stanzaChannel_->getStanzaAtIndex<Stanza>(0); - CPPUNIT_ASSERT_EQUAL(st(1), stanzaChannel_->sentStanzas.size()); - CPPUNIT_ASSERT(stanzaContactOnRoster->getPayload<DeliveryReceipt>() != 0); - - xmppRoster_->removeContact(messageJID); - - message->setID("2"); - manager_->handleIncomingMessage(message); - CPPUNIT_ASSERT_EQUAL(st(1), stanzaChannel_->sentStanzas.size()); - } - - /** - * Test that ChatController sends receipts after the contact has been added to the roster. - */ - void testChatControllerPresenceAccessUpdatedOnAddToRoster() { - JID messageJID("testling@test.com/resource1"); - - MockChatWindow* window = new MockChatWindow();//mocks_->InterfaceMock<ChatWindow>(); - mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(messageJID, uiEventStream_).Return(window); - settings_->storeSetting(SettingConstants::REQUEST_DELIVERYRECEIPTS, true); - - boost::shared_ptr<Message> message = makeDeliveryReceiptTestMessage(messageJID, "1"); - manager_->handleIncomingMessage(message); - - CPPUNIT_ASSERT_EQUAL(st(0), stanzaChannel_->sentStanzas.size()); - - xmppRoster_->addContact(messageJID, "foo", std::vector<std::string>(), RosterItemPayload::Both); - message->setID("2"); - manager_->handleIncomingMessage(message); - - CPPUNIT_ASSERT_EQUAL(st(1), stanzaChannel_->sentStanzas.size()); - Stanza::ref stanzaContactOnRoster = stanzaChannel_->getStanzaAtIndex<Stanza>(0); - CPPUNIT_ASSERT(stanzaContactOnRoster->getPayload<DeliveryReceipt>() != 0); - } - - /** - * Test that ChatController sends receipts if requested after change from subscription state To to subscription state Both. - */ - void testChatControllerPresenceAccessUpdatedOnSubscriptionChangeToBoth() { - testhelperChatControllerPresenceAccessUpdatedOnSubscriptionChangeReceiptsAllowed(RosterItemPayload::To, RosterItemPayload::Both); - } - - /** - * Test that ChatController sends receipts if requested after change from subscription state To to subscription state From. - */ - void testChatControllerPresenceAccessUpdatedOnSubscriptionChangeToFrom() { - testhelperChatControllerPresenceAccessUpdatedOnSubscriptionChangeReceiptsAllowed(RosterItemPayload::To, RosterItemPayload::From); - } - - void testhelperChatControllerPresenceAccessUpdatedOnSubscriptionChangeReceiptsAllowed(RosterItemPayload::Subscription from, RosterItemPayload::Subscription to) { - JID messageJID("testling@test.com/resource1"); - xmppRoster_->addContact(messageJID, "foo", std::vector<std::string>(), from); - - MockChatWindow* window = new MockChatWindow();//mocks_->InterfaceMock<ChatWindow>(); - mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(messageJID, uiEventStream_).Return(window); - settings_->storeSetting(SettingConstants::REQUEST_DELIVERYRECEIPTS, true); - - boost::shared_ptr<Message> message = makeDeliveryReceiptTestMessage(messageJID, "1"); - manager_->handleIncomingMessage(message); - - CPPUNIT_ASSERT_EQUAL(st(0), stanzaChannel_->sentStanzas.size()); - - xmppRoster_->addContact(messageJID, "foo", std::vector<std::string>(), to); - message->setID("2"); - manager_->handleIncomingMessage(message); - - CPPUNIT_ASSERT_EQUAL(st(1), stanzaChannel_->sentStanzas.size()); - Stanza::ref stanzaContactOnRoster = stanzaChannel_->getStanzaAtIndex<Stanza>(0); - CPPUNIT_ASSERT(stanzaContactOnRoster->getPayload<DeliveryReceipt>() != 0); - } + void setUp() { + mocks_ = new MockRepository(); + notifier_ = std::make_unique<DummyNotifier>(); + jid_ = JID("test@test.com/resource"); + stanzaChannel_ = new DummyStanzaChannel(); + iqRouter_ = new IQRouter(stanzaChannel_); + eventController_ = new EventController(); + chatWindowFactory_ = mocks_->InterfaceMock<ChatWindowFactory>(); + joinMUCWindowFactory_ = mocks_->InterfaceMock<JoinMUCWindowFactory>(); + xmppRoster_ = new XMPPRosterImpl(); + mucRegistry_ = new MUCRegistry(); + nickResolver_ = new NickResolver(jid_.toBare(), xmppRoster_, nullptr, mucRegistry_); + presenceOracle_ = new PresenceOracle(stanzaChannel_, xmppRoster_); + serverDiscoInfo_ = std::make_shared<DiscoInfo>(); + presenceSender_ = new StanzaChannelPresenceSender(stanzaChannel_); + directedPresenceSender_ = new DirectedPresenceSender(presenceSender_); + mucManager_ = new MUCManager(stanzaChannel_, iqRouter_, directedPresenceSender_, mucRegistry_); + uiEventStream_ = new UIEventStream(); + entityCapsProvider_ = new DummyEntityCapsProvider(); + chatListWindowFactory_ = mocks_->InterfaceMock<ChatListWindowFactory>(); + mucSearchWindowFactory_ = mocks_->InterfaceMock<MUCSearchWindowFactory>(); + settings_ = new DummySettingsProvider(); + profileSettings_ = new ProfileSettingsProvider("a", settings_); + chatListWindow_ = new MockChatListWindow(); + ftManager_ = new DummyFileTransferManager(); + ftOverview_ = new FileTransferOverview(ftManager_); + avatarManager_ = new NullAvatarManager(); + eventNotifier_ = new EventNotifier(eventController_, notifier_.get(), avatarManager_, nickResolver_); + wbSessionManager_ = new WhiteboardSessionManager(iqRouter_, stanzaChannel_, presenceOracle_, entityCapsProvider_); + wbManager_ = new WhiteboardManager(whiteboardWindowFactory_, uiEventStream_, nickResolver_, wbSessionManager_); + highlightManager_ = new HighlightManager(settings_); + highlightManager_->resetToDefaultConfiguration(); + handledHighlightActions_ = 0; + soundsPlayed_.clear(); + highlightManager_->onHighlight.connect(boost::bind(&ChatsManagerTest::handleHighlightAction, this, _1)); + + crypto_ = PlatformCryptoProvider::create(); + vcardStorage_ = new VCardMemoryStorage(crypto_); + vcardManager_ = new VCardManager(jid_, iqRouter_, vcardStorage_); + mocks_->ExpectCall(chatListWindowFactory_, ChatListWindowFactory::createChatListWindow).With(uiEventStream_).Return(chatListWindow_); + clientBlockListManager_ = new ClientBlockListManager(iqRouter_); + timerFactory_ = new DummyTimerFactory(); + chattables_ = std::make_unique<Chattables>(); + manager_ = new ExtendedChatsManager(jid_, stanzaChannel_, iqRouter_, eventController_, chatWindowFactory_, joinMUCWindowFactory_, nickResolver_, presenceOracle_, directedPresenceSender_, uiEventStream_, chatListWindowFactory_, true, timerFactory_, mucRegistry_, entityCapsProvider_, mucManager_, mucSearchWindowFactory_, profileSettings_, ftOverview_, xmppRoster_, false, settings_, nullptr, wbManager_, highlightManager_, clientBlockListManager_, emoticons_, vcardManager_, *chattables_); + + manager_->setAvatarManager(avatarManager_); + } + + void tearDown() { + delete highlightManager_; + delete profileSettings_; + delete eventNotifier_; + delete avatarManager_; + delete manager_; + delete timerFactory_; + delete clientBlockListManager_; + delete vcardManager_; + delete vcardStorage_; + delete crypto_; + delete ftOverview_; + delete ftManager_; + delete wbSessionManager_; + delete wbManager_; + delete directedPresenceSender_; + delete presenceSender_; + delete presenceOracle_; + delete nickResolver_; + delete mucRegistry_; + delete iqRouter_; + delete stanzaChannel_; + delete eventController_; + delete uiEventStream_; + delete mucManager_; + delete xmppRoster_; + delete entityCapsProvider_; + delete chatListWindow_; + delete mocks_; + delete settings_; + } + + void testFirstOpenWindowIncoming() { + JID messageJID("testling@test.com/resource1"); + + MockChatWindow* window = new MockChatWindow(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(messageJID, uiEventStream_).Return(window); + + std::shared_ptr<Message> message(new Message()); + message->setFrom(messageJID); + std::string body("This is a legible message. >HEH@)oeueu"); + message->setBody(body); + manager_->handleIncomingMessage(message); + CPPUNIT_ASSERT_EQUAL(body, MockChatWindow::bodyFromMessage(window->lastAddedMessage_)); + } + + void testSecondOpenWindowIncoming() { + JID messageJID1("testling@test.com/resource1"); + + MockChatWindow* window1 = new MockChatWindow(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(messageJID1, uiEventStream_).Return(window1); + + std::shared_ptr<Message> message1(new Message()); + message1->setFrom(messageJID1); + std::string body1("This is a legible message. >HEH@)oeueu"); + message1->setBody(body1); + manager_->handleIncomingMessage(message1); + CPPUNIT_ASSERT_EQUAL(body1, MockChatWindow::bodyFromMessage(window1->lastAddedMessage_)); + + JID messageJID2("testling@test.com/resource2"); + + std::shared_ptr<Message> message2(new Message()); + message2->setFrom(messageJID2); + std::string body2("This is a legible message. .cmaulm.chul"); + message2->setBody(body2); + manager_->handleIncomingMessage(message2); + CPPUNIT_ASSERT_EQUAL(body2, MockChatWindow::bodyFromMessage(window1->lastAddedMessage_)); + } + + void testFirstOpenWindowOutgoing() { + std::string messageJIDString("testling@test.com"); + + ChatWindow* window = new MockChatWindow(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(JID(messageJIDString), uiEventStream_).Return(window); + + uiEventStream_->send(std::make_shared<RequestChatUIEvent>(JID(messageJIDString))); + } + + + void testFirstOpenWindowBareToFull() { + std::string bareJIDString("testling@test.com"); + std::string fullJIDString("testling@test.com/resource1"); + + MockChatWindow* window = new MockChatWindow(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(JID(bareJIDString), uiEventStream_).Return(window); + uiEventStream_->send(std::make_shared<RequestChatUIEvent>(JID(bareJIDString))); + + std::shared_ptr<Message> message(new Message()); + message->setFrom(JID(fullJIDString)); + std::string body("This is a legible message. mjuga3089gm8G(*>M)@*("); + message->setBody(body); + manager_->handleIncomingMessage(message); + CPPUNIT_ASSERT_EQUAL(body, MockChatWindow::bodyFromMessage(window->lastAddedMessage_)); + } + + void testSecondWindow() { + std::string messageJIDString1("testling1@test.com"); + ChatWindow* window1 = new MockChatWindow(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(JID(messageJIDString1), uiEventStream_).Return(window1); + uiEventStream_->send(std::make_shared<RequestChatUIEvent>(JID(messageJIDString1))); + + std::string messageJIDString2("testling2@test.com"); + ChatWindow* window2 = new MockChatWindow(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(JID(messageJIDString2), uiEventStream_).Return(window2); + + uiEventStream_->send(std::make_shared<RequestChatUIEvent>(JID(messageJIDString2))); + } + + /** Complete cycle. + Create unbound window. + Bind it. + Unbind it. + Rebind it. + */ + void testUnbindRebind() { + std::string bareJIDString("testling@test.com"); + std::string fullJIDString1("testling@test.com/resource1"); + std::string fullJIDString2("testling@test.com/resource2"); + + MockChatWindow* window = new MockChatWindow(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(JID(bareJIDString), uiEventStream_).Return(window); + uiEventStream_->send(std::make_shared<RequestChatUIEvent>(JID(bareJIDString))); + + std::shared_ptr<Message> message1(new Message()); + message1->setFrom(JID(fullJIDString1)); + std::string messageBody1("This is a legible message."); + message1->setBody(messageBody1); + manager_->handleIncomingMessage(message1); + CPPUNIT_ASSERT_EQUAL(messageBody1, MockChatWindow::bodyFromMessage(window->lastAddedMessage_)); + + std::shared_ptr<Presence> jid1Online(new Presence()); + jid1Online->setFrom(JID(fullJIDString1)); + std::shared_ptr<Presence> jid1Offline(new Presence()); + jid1Offline->setFrom(JID(fullJIDString1)); + jid1Offline->setType(Presence::Unavailable); + presenceOracle_->onPresenceChange(jid1Offline); + + std::shared_ptr<Message> message2(new Message()); + message2->setFrom(JID(fullJIDString2)); + std::string messageBody2("This is another legible message."); + message2->setBody(messageBody2); + manager_->handleIncomingMessage(message2); + CPPUNIT_ASSERT_EQUAL(messageBody2, MockChatWindow::bodyFromMessage(window->lastAddedMessage_)); + } + + /** + * Test that MUC PMs get opened in the right windows + */ + void testThreeMUCWindows() { + JID muc("testling@test.com"); + ChatWindow* mucWindow = new MockChatWindow(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(muc, uiEventStream_).Return(mucWindow); + uiEventStream_->send(std::make_shared<JoinMUCUIEvent>(muc, std::string("nick"))); + + + std::string messageJIDString1("testling@test.com/1"); + ChatWindow* window1 = new MockChatWindow(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(JID(messageJIDString1), uiEventStream_).Return(window1); + uiEventStream_->send(std::make_shared<RequestChatUIEvent>(JID(messageJIDString1))); + + std::string messageJIDString2("testling@test.com/2"); + ChatWindow* window2 = new MockChatWindow(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(JID(messageJIDString2), uiEventStream_).Return(window2); + + uiEventStream_->send(std::make_shared<RequestChatUIEvent>(JID(messageJIDString2))); + + std::string messageJIDString3("testling@test.com/3"); + ChatWindow* window3 = new MockChatWindow(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(JID(messageJIDString3), uiEventStream_).Return(window3); + + uiEventStream_->send(std::make_shared<RequestChatUIEvent>(JID(messageJIDString3))); + + /* Refetch an earlier window */ + /* We do not expect a new window to be created */ + uiEventStream_->send(std::make_shared<RequestChatUIEvent>(JID(messageJIDString1))); + + } + + /** + Test that a second window isn't unbound where there's already an unbound one. + Bind 1 + Bind 2 + Unbind 1 + Unbind 2 (but it doesn't) + Sent to bound 2 + Rebind 1 + */ + void testNoDuplicateUnbind() { + JID messageJID1("testling@test.com/resource1"); + + MockChatWindow* window1 = new MockChatWindow(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(messageJID1, uiEventStream_).Return(window1); + + std::shared_ptr<Message> message1(new Message()); + message1->setFrom(messageJID1); + message1->setBody("This is a legible message1."); + manager_->handleIncomingMessage(message1); + + JID messageJID2("testling@test.com/resource2"); + + std::shared_ptr<Message> message2(new Message()); + message2->setFrom(messageJID2); + message2->setBody("This is a legible message2."); + manager_->handleIncomingMessage(message2); + + std::shared_ptr<Presence> jid1Online(new Presence()); + jid1Online->setFrom(JID(messageJID1)); + std::shared_ptr<Presence> jid1Offline(new Presence()); + jid1Offline->setFrom(JID(messageJID1)); + jid1Offline->setType(Presence::Unavailable); + presenceOracle_->onPresenceChange(jid1Offline); + + std::shared_ptr<Presence> jid2Online(new Presence()); + jid2Online->setFrom(JID(messageJID2)); + std::shared_ptr<Presence> jid2Offline(new Presence()); + jid2Offline->setFrom(JID(messageJID2)); + jid2Offline->setType(Presence::Unavailable); + presenceOracle_->onPresenceChange(jid2Offline); + + JID messageJID3("testling@test.com/resource3"); + + std::shared_ptr<Message> message3(new Message()); + message3->setFrom(messageJID3); + std::string body3("This is a legible message3."); + message3->setBody(body3); + manager_->handleIncomingMessage(message3); + CPPUNIT_ASSERT_EQUAL(body3, MockChatWindow::bodyFromMessage(window1->lastAddedMessage_)); + + std::shared_ptr<Message> message2b(new Message()); + message2b->setFrom(messageJID2); + std::string body2b("This is a legible message2b."); + message2b->setBody(body2b); + manager_->handleIncomingMessage(message2b); + CPPUNIT_ASSERT_EQUAL(body2b, MockChatWindow::bodyFromMessage(window1->lastAddedMessage_)); + } + + /** + * Test that ChatController doesn't send receipts anymore after removal of the contact from the roster. + */ + void testChatControllerPresenceAccessUpdatedOnRemoveFromRoster() { + JID messageJID("testling@test.com/resource1"); + xmppRoster_->addContact(messageJID, "foo", std::vector<std::string>(), RosterItemPayload::Both); + + MockChatWindow* window = new MockChatWindow();//mocks_->InterfaceMock<ChatWindow>(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(messageJID, uiEventStream_).Return(window); + settings_->storeSetting(SettingConstants::REQUEST_DELIVERYRECEIPTS, true); + + std::shared_ptr<Message> message = makeDeliveryReceiptTestMessage(messageJID, "1"); + manager_->handleIncomingMessage(message); + Stanza::ref stanzaContactOnRoster = stanzaChannel_->getStanzaAtIndex<Stanza>(1); + CPPUNIT_ASSERT_EQUAL(st(1), stanzaChannel_->countSentStanzaOfType<Message>()); + CPPUNIT_ASSERT(stanzaContactOnRoster->getPayload<DeliveryReceipt>() != nullptr); + + xmppRoster_->removeContact(messageJID); + + message->setID("2"); + manager_->handleIncomingMessage(message); + CPPUNIT_ASSERT_EQUAL(st(1), stanzaChannel_->countSentStanzaOfType<Message>()); + } + + /** + * Test that ChatController sends receipts after the contact has been added to the roster. + */ + void testChatControllerPresenceAccessUpdatedOnAddToRoster() { + JID messageJID("testling@test.com/resource1"); + + MockChatWindow* window = new MockChatWindow(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(messageJID, uiEventStream_).Return(window); + settings_->storeSetting(SettingConstants::REQUEST_DELIVERYRECEIPTS, true); + + std::shared_ptr<Message> message = makeDeliveryReceiptTestMessage(messageJID, "1"); + manager_->handleIncomingMessage(message); + + CPPUNIT_ASSERT_EQUAL(st(0), stanzaChannel_->countSentStanzaOfType<Message>()); + + xmppRoster_->addContact(messageJID, "foo", std::vector<std::string>(), RosterItemPayload::Both); + message->setID("2"); + manager_->handleIncomingMessage(message); + + CPPUNIT_ASSERT_EQUAL(st(1), stanzaChannel_->countSentStanzaOfType<Message>()); + Stanza::ref stanzaContactOnRoster = stanzaChannel_->getStanzaAtIndex<Stanza>(1); + CPPUNIT_ASSERT(stanzaContactOnRoster->getPayload<DeliveryReceipt>() != nullptr); + } + + /** + * Test that ChatController sends receipts if requested after change from subscription state To to subscription state Both. + */ + void testChatControllerPresenceAccessUpdatedOnSubscriptionChangeToBoth() { + testhelperChatControllerPresenceAccessUpdatedOnSubscriptionChangeReceiptsAllowed(RosterItemPayload::To, RosterItemPayload::Both); + } + + /** + * Test that ChatController sends receipts if requested after change from subscription state To to subscription state From. + */ + void testChatControllerPresenceAccessUpdatedOnSubscriptionChangeToFrom() { + testhelperChatControllerPresenceAccessUpdatedOnSubscriptionChangeReceiptsAllowed(RosterItemPayload::To, RosterItemPayload::From); + } + + void testChatControllerFullJIDBindingOnMessageAndNotReceipt() { + JID ownJID("test@test.com/resource"); + JID sender("foo@test.com"); + std::vector<JID> senderResource; + senderResource.push_back(sender.withResource("resourceA")); + senderResource.push_back(sender.withResource("resourceB")); + + // We support delivery receipts. + settings_->storeSetting(SettingConstants::REQUEST_DELIVERYRECEIPTS, true); + + // Open chat window to a sender. + MockChatWindow* window = new MockChatWindow(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(sender, uiEventStream_).Return(window); + + uiEventStream_->send(std::make_shared<RequestChatUIEvent>(sender)); + + for (const auto& senderJID : senderResource) { + // The sender supports delivery receipts. + DiscoInfo::ref disco = std::make_shared<DiscoInfo>(); + disco->addFeature(DiscoInfo::MessageDeliveryReceiptsFeature); + entityCapsProvider_->caps[senderJID] = disco; + + // The sender is online. + Presence::ref senderPresence = std::make_shared<Presence>(); + senderPresence->setFrom(senderJID); + senderPresence->setTo(ownJID); + stanzaChannel_->onPresenceReceived(senderPresence); + + entityCapsProvider_->onCapsChanged(senderJID); + } + + // Send first message. + window->onSendMessageRequest("hello there", false); + + // A bare message is send because no resources is bound. + CPPUNIT_ASSERT_EQUAL(sender, stanzaChannel_->getStanzaAtIndex<Message>(1)->getTo()); + CPPUNIT_ASSERT(stanzaChannel_->getStanzaAtIndex<Message>(1)->getPayload<DeliveryReceiptRequest>()); + + // Two resources respond with message receipts. + for (const auto& senderJID : senderResource) { + Message::ref receiptReply = std::make_shared<Message>(); + receiptReply->setFrom(senderJID); + receiptReply->setTo(ownJID); + + std::shared_ptr<DeliveryReceipt> receipt = std::make_shared<DeliveryReceipt>(); + receipt->setReceivedID(stanzaChannel_->getStanzaAtIndex<Message>(1)->getID()); + receiptReply->addPayload(receipt); + manager_->handleIncomingMessage(receiptReply); + } + + // Send second message. + window->onSendMessageRequest("how are you?", false); + + // A bare message is send because no resources is bound. + CPPUNIT_ASSERT_EQUAL(sender, stanzaChannel_->getStanzaAtIndex<Message>(1)->getTo()); + CPPUNIT_ASSERT(stanzaChannel_->getStanzaAtIndex<Message>(1)->getPayload<DeliveryReceiptRequest>()); + + // Two resources respond with message receipts. + for (const auto& senderJID : senderResource) { + Message::ref receiptReply = std::make_shared<Message>(); + receiptReply->setFrom(senderJID); + receiptReply->setTo(ownJID); + + std::shared_ptr<DeliveryReceipt> receipt = std::make_shared<DeliveryReceipt>(); + receipt->setReceivedID(stanzaChannel_->getStanzaAtIndex<Message>(1)->getID()); + receiptReply->addPayload(receipt); + manager_->handleIncomingMessage(receiptReply); + } + + // Reply with a message including a body text. + Message::ref reply = std::make_shared<Message>(); + reply->setFrom(senderResource[0]); + reply->setTo(ownJID); + reply->setBody("fine."); + manager_->handleIncomingMessage(reply); + + // Send third message. + window->onSendMessageRequest("great to hear.", false); + + // The chat session is bound to the full JID of the first resource. + CPPUNIT_ASSERT_EQUAL(senderResource[0], stanzaChannel_->getStanzaAtIndex<Message>(3)->getTo()); + CPPUNIT_ASSERT(stanzaChannel_->getStanzaAtIndex<Message>(2)->getPayload<DeliveryReceiptRequest>()); + + // Receive random receipt from second sender resource. + reply = std::make_shared<Message>(); + reply->setFrom(senderResource[1]); + reply->setTo(ownJID); + + std::shared_ptr<DeliveryReceipt> receipt = std::make_shared<DeliveryReceipt>(); + receipt->setReceivedID(stanzaChannel_->getStanzaAtIndex<Message>(2)->getID()); + reply->addPayload(receipt); + manager_->handleIncomingMessage(reply); + + // Send forth message. + window->onSendMessageRequest("what else is new?", false); + + // The chat session is bound to the full JID of the first resource. + CPPUNIT_ASSERT_EQUAL(senderResource[0], stanzaChannel_->getStanzaAtIndex<Message>(3)->getTo()); + CPPUNIT_ASSERT(stanzaChannel_->getStanzaAtIndex<Message>(3)->getPayload<DeliveryReceiptRequest>()); + + // Reply with a message including a body text from second resource. + reply = std::make_shared<Message>(); + reply->setFrom(senderResource[1]); + reply->setTo(ownJID); + reply->setBody("nothing."); + manager_->handleIncomingMessage(reply); + + // Send fifth message. + window->onSendMessageRequest("okay", false); + + // The chat session is now bound to the full JID of the second resource. + CPPUNIT_ASSERT_EQUAL(senderResource[1], stanzaChannel_->getStanzaAtIndex<Message>(5)->getTo()); + CPPUNIT_ASSERT(stanzaChannel_->getStanzaAtIndex<Message>(5)->getPayload<DeliveryReceiptRequest>()); + } + + void testChatControllerFullJIDBindingOnTypingAndNotActive() { + JID ownJID("test@test.com/resource"); + JID sender("foo@test.com"); + std::vector<JID> senderResource; + senderResource.push_back(sender.withResource("resourceA")); + senderResource.push_back(sender.withResource("resourceB")); + + // We support delivery receipts. + settings_->storeSetting(SettingConstants::REQUEST_DELIVERYRECEIPTS, true); + + // Open chat window to a sender. + MockChatWindow* window = new MockChatWindow(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(sender, uiEventStream_).Return(window); + + uiEventStream_->send(std::make_shared<RequestChatUIEvent>(sender)); + + for (const auto& senderJID : senderResource) { + // The sender supports delivery receipts. + DiscoInfo::ref disco = std::make_shared<DiscoInfo>(); + disco->addFeature(DiscoInfo::MessageDeliveryReceiptsFeature); + entityCapsProvider_->caps[senderJID] = disco; + + // The sender is online. + Presence::ref senderPresence = std::make_shared<Presence>(); + senderPresence->setFrom(senderJID); + senderPresence->setTo(ownJID); + stanzaChannel_->onPresenceReceived(senderPresence); + + entityCapsProvider_->onCapsChanged(senderJID); + } + + // Send first message. + window->onSendMessageRequest("hello there", false); + + // A bare message is send because no resources is bound. + CPPUNIT_ASSERT_EQUAL(sender, stanzaChannel_->getStanzaAtIndex<Message>(1)->getTo()); + CPPUNIT_ASSERT(stanzaChannel_->getStanzaAtIndex<Message>(1)->getPayload<DeliveryReceiptRequest>()); + + // Two resources respond with message receipts. + for (const auto& senderJID : senderResource) { + Message::ref reply = std::make_shared<Message>(); + reply->setFrom(senderJID); + reply->setTo(ownJID); + + std::shared_ptr<ChatState> csn = std::make_shared<ChatState>(); + csn->setChatState(ChatState::Active); + reply->addPayload(csn); + manager_->handleIncomingMessage(reply); + } + + // Send second message. + window->onSendMessageRequest("how are you?", false); + + // A bare message is send because no resources is bound. + CPPUNIT_ASSERT_EQUAL(sender, stanzaChannel_->getStanzaAtIndex<Message>(1)->getTo()); + CPPUNIT_ASSERT(stanzaChannel_->getStanzaAtIndex<Message>(1)->getPayload<DeliveryReceiptRequest>()); + + // Two resources respond with message receipts. + for (const auto& senderJID : senderResource) { + Message::ref receiptReply = std::make_shared<Message>(); + receiptReply->setFrom(senderJID); + receiptReply->setTo(ownJID); + + std::shared_ptr<DeliveryReceipt> receipt = std::make_shared<DeliveryReceipt>(); + receipt->setReceivedID(stanzaChannel_->getStanzaAtIndex<Message>(1)->getID()); + receiptReply->addPayload(receipt); + manager_->handleIncomingMessage(receiptReply); + } + + // Reply with a message including a CSN. + Message::ref reply = std::make_shared<Message>(); + reply->setFrom(senderResource[0]); + reply->setTo(ownJID); + + std::shared_ptr<ChatState> csn = std::make_shared<ChatState>(); + csn->setChatState(ChatState::Composing); + reply->addPayload(csn); + manager_->handleIncomingMessage(reply); + + // Send third message. + window->onSendMessageRequest("great to hear.", false); + + // The chat session is now bound to the full JID of the first resource due to its recent composing message. + CPPUNIT_ASSERT_EQUAL(senderResource[0], stanzaChannel_->getStanzaAtIndex<Message>(3)->getTo()); + CPPUNIT_ASSERT(stanzaChannel_->getStanzaAtIndex<Message>(3)->getPayload<DeliveryReceiptRequest>()); + + // Reply with a message including a CSN from the other resource. + reply = std::make_shared<Message>(); + reply->setFrom(senderResource[1]); + reply->setTo(ownJID); + + csn = std::make_shared<ChatState>(); + csn->setChatState(ChatState::Composing); + reply->addPayload(csn); + manager_->handleIncomingMessage(reply); + + // Send third message. + window->onSendMessageRequest("ping.", false); + + // The chat session is now bound to the full JID of the second resource due to its recent composing message. + CPPUNIT_ASSERT_EQUAL(senderResource[1], stanzaChannel_->getStanzaAtIndex<Message>(4)->getTo()); + CPPUNIT_ASSERT(stanzaChannel_->getStanzaAtIndex<Message>(4)->getPayload<DeliveryReceiptRequest>()); + } + + void testChatControllerPMPresenceHandling() { + JID participantA = JID("test@rooms.test.com/participantA"); + JID participantB = JID("test@rooms.test.com/participantB"); + + mucRegistry_->addMUC("test@rooms.test.com"); + + MockChatWindow* window = new MockChatWindow(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(participantA, uiEventStream_).Return(window); + + uiEventStream_->send(std::make_shared<RequestChatUIEvent>(JID(participantA))); + + Presence::ref presence = Presence::create(); + presence->setFrom(participantA); + presence->setShow(StatusShow::Online); + stanzaChannel_->onPresenceReceived(presence); + CPPUNIT_ASSERT_EQUAL(std::string("participantA has become available."), MockChatWindow::bodyFromMessage(window->lastAddedPresence_)); + + presence = Presence::create(); + presence->setFrom(participantB); + presence->setShow(StatusShow::Away); + stanzaChannel_->onPresenceReceived(presence); + + presence = Presence::create(); + presence->setFrom(participantA); + presence->setShow(StatusShow::None); + presence->setType(Presence::Unavailable); + stanzaChannel_->onPresenceReceived(presence); + CPPUNIT_ASSERT_EQUAL(std::string("participantA has gone offline."), MockChatWindow::bodyFromMessage(window->lastReplacedLastMessage_)); + } + + void testChatControllerMucPmUnavailableErrorHandling() { + auto mucJID = JID("test@rooms.test.com"); + auto participantA = mucJID.withResource("participantA"); + auto participantB = mucJID.withResource("participantB"); + + auto mucWindow = new MockChatWindow(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(mucJID, uiEventStream_).Return(mucWindow); + uiEventStream_->send(std::make_shared<JoinMUCUIEvent>(mucJID, participantB.getResource())); + CPPUNIT_ASSERT_EQUAL(true, mucWindow->mucType_.is_initialized()); + + auto window = new MockChatWindow(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(participantA, uiEventStream_).Return(window); + uiEventStream_->send(std::make_shared<RequestChatUIEvent>(participantA)); + CPPUNIT_ASSERT_EQUAL(false, window->mucType_.is_initialized()); + + Presence::ref presence = Presence::create(); + presence->setFrom(participantA); + presence->setShow(StatusShow::Online); + stanzaChannel_->onPresenceReceived(presence); + CPPUNIT_ASSERT_EQUAL(std::string("participantA has become available."), MockChatWindow::bodyFromMessage(window->lastAddedPresence_)); + + // send message to participantA + auto messageBody = std::string("message body to send"); + window->onSendMessageRequest(messageBody, false); + auto sendMessageStanza = stanzaChannel_->getStanzaAtIndex<Message>(3); + CPPUNIT_ASSERT_EQUAL(messageBody, *sendMessageStanza->getBody()); + + // receive reply with error + auto messageErrorReply = std::make_shared<Message>(); + messageErrorReply->setID(stanzaChannel_->getNewIQID()); + messageErrorReply->setType(Message::Error); + messageErrorReply->setFrom(participantA); + messageErrorReply->setTo(jid_); + messageErrorReply->addPayload(std::make_shared<ErrorPayload>(ErrorPayload::ItemNotFound, ErrorPayload::Cancel, "Recipient not in room")); + + auto lastMUCWindowErrorMessageBeforeError = MockChatWindow::bodyFromMessage(mucWindow->lastAddedErrorMessage_); + manager_->handleIncomingMessage(messageErrorReply); + + // assert that error is not routed to MUC window + CPPUNIT_ASSERT_EQUAL(lastMUCWindowErrorMessageBeforeError, MockChatWindow::bodyFromMessage(mucWindow->lastAddedErrorMessage_)); + // assert that error is routed to PM + CPPUNIT_ASSERT_EQUAL(std::string("This user could not be found in the room."), MockChatWindow::bodyFromMessage(window->lastAddedErrorMessage_)); + } + + void testLocalMUCServiceDiscoveryResetOnDisconnect() { + JID ownJID("test@test.com/resource"); + JID sender("foo@test.com"); + + manager_->setOnline(true); + + // Open chat window to a sender. + MockChatWindow* window = new MockChatWindow(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(sender, uiEventStream_).Return(window); + + uiEventStream_->send(std::make_shared<RequestChatUIEvent>(sender)); + + CPPUNIT_ASSERT_EQUAL(false, window->impromptuMUCSupported_); + + std::shared_ptr<IQ> infoRequest = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[1]); + std::shared_ptr<IQ> infoResponse = IQ::createResult(infoRequest->getFrom(), infoRequest->getTo(), infoRequest->getID()); + + DiscoInfo info; + info.addIdentity(DiscoInfo::Identity("Shakespearean Chat Service", "conference", "text")); + info.addFeature("http://jabber.org/protocol/muc"); + infoResponse->addPayload(std::make_shared<DiscoInfo>(info)); + stanzaChannel_->onIQReceived(infoResponse); + + CPPUNIT_ASSERT_EQUAL(true, window->impromptuMUCSupported_); + manager_->setOnline(false); + CPPUNIT_ASSERT_EQUAL(false, window->impromptuMUCSupported_); + } + + void testhelperChatControllerPresenceAccessUpdatedOnSubscriptionChangeReceiptsAllowed(RosterItemPayload::Subscription from, RosterItemPayload::Subscription to) { + JID messageJID("testling@test.com/resource1"); + xmppRoster_->addContact(messageJID, "foo", std::vector<std::string>(), from); + + MockChatWindow* window = new MockChatWindow();//mocks_->InterfaceMock<ChatWindow>(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(messageJID, uiEventStream_).Return(window); + settings_->storeSetting(SettingConstants::REQUEST_DELIVERYRECEIPTS, true); + + std::shared_ptr<Message> message = makeDeliveryReceiptTestMessage(messageJID, "1"); + manager_->handleIncomingMessage(message); + + CPPUNIT_ASSERT_EQUAL(st(0), stanzaChannel_->countSentStanzaOfType<Message>()); + + xmppRoster_->addContact(messageJID, "foo", std::vector<std::string>(), to); + message->setID("2"); + manager_->handleIncomingMessage(message); + + CPPUNIT_ASSERT_EQUAL(st(1), stanzaChannel_->countSentStanzaOfType<Message>()); + Stanza::ref stanzaContactOnRoster = stanzaChannel_->getStanzaAtIndex<Stanza>(1); + CPPUNIT_ASSERT(stanzaContactOnRoster->getPayload<DeliveryReceipt>() != nullptr); + } + + void testChatControllerHighlightingNotificationTesting() { + HighlightConfiguration::KeywordHightlight keywordRuleA; + keywordRuleA.keyword = "Romeo"; + keywordRuleA.action.setFrontColor(boost::optional<std::string>("yellow")); + keywordRuleA.action.setSoundFilePath(boost::optional<std::string>("")); + highlightManager_->getConfiguration()->keywordHighlights.push_back(keywordRuleA); + + HighlightConfiguration::KeywordHightlight keywordRuleB; + keywordRuleB.keyword = "Juliet"; + keywordRuleB.action.setFrontColor(boost::optional<std::string>("green")); + keywordRuleB.action.setSoundFilePath(boost::optional<std::string>("/tmp/someotherfile.wav")); + highlightManager_->getConfiguration()->keywordHighlights.push_back(keywordRuleB); + + JID messageJID = JID("testling@test.com"); + + MockChatWindow* window = new MockChatWindow(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(messageJID, uiEventStream_).Return(window); + + std::shared_ptr<Message> message(new Message()); + message->setFrom(messageJID); + std::string body("This message should cause two sounds: Juliet and Romeo."); + message->setBody(body); + manager_->handleIncomingMessage(message); + + CPPUNIT_ASSERT_EQUAL(2, handledHighlightActions_); + CPPUNIT_ASSERT(soundsPlayed_.find(keywordRuleA.action.getSoundFilePath().get_value_or("")) != soundsPlayed_.end()); + CPPUNIT_ASSERT(soundsPlayed_.find(keywordRuleB.action.getSoundFilePath().get_value_or("")) != soundsPlayed_.end()); + CPPUNIT_ASSERT_EQUAL(size_t(1), notifier_->notifications.size()); + } + + void testChatControllerHighlightingNotificationDeduplicateSounds() { + auto keywordRuleA = HighlightConfiguration::KeywordHightlight(); + keywordRuleA.keyword = "Romeo"; + keywordRuleA.action.setFrontColor(boost::optional<std::string>("yellow")); + keywordRuleA.action.setSoundFilePath(boost::optional<std::string>("")); + highlightManager_->getConfiguration()->keywordHighlights.push_back(keywordRuleA); + + auto keywordRuleB = HighlightConfiguration::KeywordHightlight(); + keywordRuleB.keyword = "Juliet"; + keywordRuleB.action.setFrontColor(boost::optional<std::string>("green")); + keywordRuleB.action.setSoundFilePath(boost::optional<std::string>("")); + highlightManager_->getConfiguration()->keywordHighlights.push_back(keywordRuleB); + + JID messageJID = JID("testling@test.com"); + + MockChatWindow* window = new MockChatWindow(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(messageJID, uiEventStream_).Return(window); + + std::shared_ptr<Message> message(new Message()); + message->setFrom(messageJID); + std::string body("This message should cause one sound, because both actions have the same sound: Juliet and Romeo."); + message->setBody(body); + manager_->handleIncomingMessage(message); + + CPPUNIT_ASSERT_EQUAL(1, handledHighlightActions_); + CPPUNIT_ASSERT(soundsPlayed_.find(keywordRuleA.action.getSoundFilePath().get_value_or("")) != soundsPlayed_.end()); + CPPUNIT_ASSERT(soundsPlayed_.find(keywordRuleB.action.getSoundFilePath().get_value_or("")) != soundsPlayed_.end()); + CPPUNIT_ASSERT_EQUAL(size_t(1), notifier_->notifications.size()); + } + + void testChatControllerHighlightingNotificationKeyword() { + auto keywordRuleA = HighlightConfiguration::KeywordHightlight(); + keywordRuleA.keyword = "Swift"; + keywordRuleA.action.setFrontColor(boost::optional<std::string>("yellow")); + keywordRuleA.action.setSoundFilePath(boost::optional<std::string>("")); + keywordRuleA.action.setSystemNotificationEnabled(true); + highlightManager_->getConfiguration()->keywordHighlights.push_back(keywordRuleA); + + JID messageJID = JID("testling@test.com"); + + MockChatWindow* window = new MockChatWindow(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(messageJID, uiEventStream_).Return(window); + + std::shared_ptr<Message> message(new Message()); + message->setFrom(messageJID); + std::string body("Let's see if the Swift highlight kicks off a system notification."); + message->setBody(body); + manager_->handleIncomingMessage(message); + + CPPUNIT_ASSERT_EQUAL(size_t(2), notifier_->notifications.size()); + } + + void testChatControllerMeMessageHandling() { + JID messageJID("testling@test.com/resource1"); + + MockChatWindow* window = new MockChatWindow(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(messageJID, uiEventStream_).Return(window); + + std::shared_ptr<Message> message(new Message()); + message->setFrom(messageJID); + std::string body("/me is feeling delighted."); + message->setBody(body); + manager_->handleIncomingMessage(message); + CPPUNIT_ASSERT_EQUAL(std::string("is feeling delighted."), window->bodyFromMessage(window->lastAddedAction_)); + } + + void testRestartingMUCComponentCrash() { + JID mucJID = JID("teaparty@rooms.wonderland.lit"); + JID self = JID("girl@wonderland.lit/rabbithole"); + std::string nick = "aLiCe"; + + MockChatWindow* window; + + auto genRemoteMUCPresence = [=]() { + auto presence = Presence::create(); + presence->setFrom(mucJID.withResource(nick)); + presence->setTo(self); + return presence; + }; + + // User rejoins. + window = new MockChatWindow(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(mucJID, uiEventStream_).Return(window); + + // Join room + { + auto joinRoomEvent = std::make_shared<JoinMUCUIEvent>(mucJID, boost::optional<std::string>(), nick); + uiEventStream_->send(joinRoomEvent); + } + + { + auto firstPresence = genRemoteMUCPresence(); + firstPresence->setType(Presence::Unavailable); + auto userPayload = std::make_shared<MUCUserPayload>(); + userPayload->addItem(MUCItem(MUCOccupant::Owner, MUCOccupant::NoRole)); + firstPresence->addPayload(userPayload); + stanzaChannel_->onPresenceReceived(firstPresence); + } + CPPUNIT_ASSERT_EQUAL(std::string("Couldn't enter room: Unable to enter this room."), MockChatWindow::bodyFromMessage(window->lastAddedErrorMessage_)); + + { + auto presence = genRemoteMUCPresence(); + presence->setType(Presence::Unavailable); + auto userPayload = std::make_shared<MUCUserPayload>(); + userPayload->addStatusCode(303); + auto item = MUCItem(MUCOccupant::Owner, self, MUCOccupant::Moderator); + item.nick = nick; + userPayload->addItem(item); + userPayload->addStatusCode(110); + presence->addPayload(userPayload); + stanzaChannel_->onPresenceReceived(presence); + } + } + + void testChatControllerMeMessageHandlingInMUC() { + JID mucJID("mucroom@rooms.test.com"); + std::string nickname = "toodles"; + + //highlightManager_->resetToDefaultConfiguration(); + + // add highlight rule for 'foo' + HighlightConfiguration::KeywordHightlight keywordHighlight; + keywordHighlight.keyword = "foo"; + keywordHighlight.action.setBackColor(boost::optional<std::string>("green")); + highlightManager_->getConfiguration()->keywordHighlights.push_back(keywordHighlight); + + HighlightConfiguration::KeywordHightlight keywordHighlightNotification; + keywordHighlightNotification.keyword = "Swift"; + keywordHighlightNotification.action.setBackColor(boost::optional<std::string>("green")); + keywordHighlightNotification.action.setSystemNotificationEnabled(true); + highlightManager_->getConfiguration()->keywordHighlights.push_back(keywordHighlightNotification); + + MockChatWindow* window = new MockChatWindow(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(mucJID, uiEventStream_).Return(window); + + uiEventStream_->send(std::make_shared<JoinMUCUIEvent>(mucJID, boost::optional<std::string>(), nickname)); + + auto genRemoteMUCPresence = [=]() { + auto presence = Presence::create(); + presence->setFrom(mucJID.withResource(nickname)); + presence->setTo(jid_); + return presence; + }; + + { + auto presence = genRemoteMUCPresence(); + auto userPayload = std::make_shared<MUCUserPayload>(); + userPayload->addStatusCode(110); + userPayload->addItem(MUCItem(MUCOccupant::Owner, jid_, MUCOccupant::Moderator)); + presence->addPayload(userPayload); + stanzaChannel_->onPresenceReceived(presence); + } + + { + auto presence = genRemoteMUCPresence(); + presence->setFrom(mucJID.withResource("someDifferentNickname")); + auto userPayload = std::make_shared<MUCUserPayload>(); + userPayload->addItem(MUCItem(MUCOccupant::Member, JID("foo@bar.com"), MUCOccupant::Moderator)); + presence->addPayload(userPayload); + stanzaChannel_->onPresenceReceived(presence); + } + + window->onSendMessageRequest("/me sends a test message with foo", false); + + window->resetLastMessages(); + { + Message::ref mucMirrored = std::make_shared<Message>(); + mucMirrored->setFrom(mucJID.withResource(nickname)); + mucMirrored->setTo(jid_); + mucMirrored->setType(Message::Groupchat); + mucMirrored->setBody("/me sends a test message with foo"); + manager_->handleIncomingMessage(mucMirrored); + } + CPPUNIT_ASSERT_EQUAL(std::string("sends a test message with foo"), window->bodyFromMessage(window->lastAddedAction_)); + + window->resetLastMessages(); + { + Message::ref mucMirrored = std::make_shared<Message>(); + mucMirrored->setFrom(mucJID.withResource("someDifferentNickname")); + mucMirrored->setTo(jid_); + mucMirrored->setType(Message::Groupchat); + mucMirrored->setBody("/me says hello with a test message with foo and foo"); + manager_->handleIncomingMessage(mucMirrored); + } + CPPUNIT_ASSERT_EQUAL(std::string("says hello with a test message with foo and foo"), window->bodyFromMessage(window->lastAddedAction_)); + + window->resetLastMessages(); + { + Message::ref keywordMessage = std::make_shared<Message>(); + keywordMessage->setFrom(mucJID.withResource("someOtherDifferentNickname")); + keywordMessage->setTo(jid_); + keywordMessage->setType(Message::Groupchat); + keywordMessage->setBody("Let's mention Swift and see what happens."); + manager_->handleIncomingMessage(keywordMessage); + } + CPPUNIT_ASSERT_EQUAL(std::string("Let's mention Swift and see what happens."), window->bodyFromMessage(window->lastAddedMessage_)); + CPPUNIT_ASSERT_EQUAL(size_t(1), notifier_->notifications.size()); + } + + void testPresenceChangeDoesNotReplaceMUCInvite() { + JID messageJID("testling@test.com/resource1"); + + auto generateIncomingPresence = [=](Presence::Type type) { + auto presence = std::make_shared<Presence>(); + presence->setType(type); + presence->setFrom(messageJID); + presence->setTo(jid_); + return presence; + }; + + stanzaChannel_->onPresenceReceived(generateIncomingPresence(Presence::Available)); + + MockChatWindow* window = new MockChatWindow(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(messageJID, uiEventStream_).Return(window); + + std::shared_ptr<Message> message(new Message()); + message->setFrom(messageJID); + std::string body("This is a legible message. >HEH@)oeueu"); + message->setBody(body); + manager_->handleIncomingMessage(message); + CPPUNIT_ASSERT_EQUAL(body, MockChatWindow::bodyFromMessage(window->lastAddedMessage_)); + + auto incomingMUCInvite = std::make_shared<Message>(); + incomingMUCInvite->setFrom(messageJID); + + auto invitePayload = std::make_shared<MUCInvitationPayload>(); + invitePayload->setJID("room@muc.service.com"); + incomingMUCInvite->addPayload(invitePayload); + + stanzaChannel_->onPresenceReceived(generateIncomingPresence(Presence::Unavailable)); + stanzaChannel_->onPresenceReceived(generateIncomingPresence(Presence::Available)); + + window->resetLastMessages(); + + manager_->handleIncomingMessage(incomingMUCInvite); + CPPUNIT_ASSERT_EQUAL(JID("room@muc.service.com"), window->lastMUCInvitationJID_); + + stanzaChannel_->onPresenceReceived(generateIncomingPresence(Presence::Unavailable)); + CPPUNIT_ASSERT_EQUAL(std::string(""), MockChatWindow::bodyFromMessage(window->lastReplacedLastMessage_)); + CPPUNIT_ASSERT_EQUAL(std::string("testling@test.com has gone offline."), MockChatWindow::bodyFromMessage(window->lastAddedPresence_)); + } + + void testNotSplittingMUCPresenceJoinLeaveLinesOnChatStateNotifications() { + JID mucJID("mucroom@rooms.test.com"); + std::string nickname = "toodles"; + + MockChatWindow* window = new MockChatWindow(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(mucJID, uiEventStream_).Return(window); + + uiEventStream_->send(std::make_shared<JoinMUCUIEvent>(mucJID, boost::optional<std::string>(), nickname)); + + auto genRemoteMUCPresence = [=]() { + auto presence = Presence::create(); + presence->setFrom(mucJID.withResource(nickname)); + presence->setTo(jid_); + return presence; + }; + + { + auto presence = genRemoteMUCPresence(); + auto userPayload = std::make_shared<MUCUserPayload>(); + userPayload->addStatusCode(110); + userPayload->addItem(MUCItem(MUCOccupant::Owner, jid_, MUCOccupant::Moderator)); + presence->addPayload(userPayload); + stanzaChannel_->onPresenceReceived(presence); + } + + { + auto presence = genRemoteMUCPresence(); + presence->setFrom(mucJID.withResource("someDifferentNickname")); + auto userPayload = std::make_shared<MUCUserPayload>(); + presence->addPayload(userPayload); + stanzaChannel_->onPresenceReceived(presence); + } + CPPUNIT_ASSERT_EQUAL(std::string("someDifferentNickname has entered the room."), window->bodyFromMessage(window->lastAddedPresence_)); + CPPUNIT_ASSERT_EQUAL(std::string(), window->bodyFromMessage(window->lastReplacedLastMessage_)); + window->resetLastMessages(); + + { + auto presence = genRemoteMUCPresence(); + presence->setFrom(mucJID.withResource("Romeo")); + auto userPayload = std::make_shared<MUCUserPayload>(); + presence->addPayload(userPayload); + stanzaChannel_->onPresenceReceived(presence); + } + CPPUNIT_ASSERT_EQUAL(std::string(), window->bodyFromMessage(window->lastAddedPresence_)); + CPPUNIT_ASSERT_EQUAL(std::string("someDifferentNickname and Romeo have entered the room"), window->bodyFromMessage(window->lastReplacedLastMessage_)); + window->resetLastMessages(); + + { + auto message = std::make_shared<Message>(); + message->setFrom(mucJID.withResource("Romeo")); + message->setTo(mucJID); + message->setType(Message::Groupchat); + message->addPayload(std::make_shared<ChatState>(ChatState::Composing)); + manager_->handleIncomingMessage(message); + } + + { + auto presence = genRemoteMUCPresence(); + presence->setFrom(mucJID.withResource("Juliet")); + auto userPayload = std::make_shared<MUCUserPayload>(); + presence->addPayload(userPayload); + stanzaChannel_->onPresenceReceived(presence); + } + CPPUNIT_ASSERT_EQUAL(std::string(), window->bodyFromMessage(window->lastAddedPresence_)); + CPPUNIT_ASSERT_EQUAL(std::string("someDifferentNickname, Romeo and Juliet have entered the room"), window->bodyFromMessage(window->lastReplacedLastMessage_)); + } + + template <typename CarbonsType> + Message::ref createCarbonsMessage(std::shared_ptr<CarbonsType> carbons, std::shared_ptr<Message> forwardedMessage) { + auto messageWrapper = std::make_shared<Message>(); + messageWrapper->setFrom(jid_.toBare()); + messageWrapper->setTo(jid_); + messageWrapper->setType(Message::Chat); + + messageWrapper->addPayload(carbons); + auto forwarded = std::make_shared<Forwarded>(); + carbons->setForwarded(forwarded); + forwarded->setStanza(forwardedMessage); + return messageWrapper; + } + + void testCarbonsForwardedIncomingMessageToSecondResource() { + JID messageJID("testling@test.com/resource1"); + JID jid2 = jid_.toBare().withResource("someOtherResource"); + + MockChatWindow* window = new MockChatWindow(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(messageJID, uiEventStream_).Return(window); + + std::shared_ptr<Message> message(new Message()); + message->setFrom(messageJID); + std::string body("This is a legible message. >HEH@)oeueu"); + message->setBody(body); + manager_->handleIncomingMessage(message); + CPPUNIT_ASSERT_EQUAL(body, MockChatWindow::bodyFromMessage(window->lastAddedMessage_)); + + // incoming carbons message from another resource + { + auto originalMessage = std::make_shared<Message>(); + originalMessage->setFrom(messageJID); + originalMessage->setTo(jid2); + originalMessage->setType(Message::Chat); + std::string forwardedBody = "Some further text."; + originalMessage->setBody(forwardedBody); + + auto messageWrapper = createCarbonsMessage(std::make_shared<CarbonsReceived>(), originalMessage); + + manager_->handleIncomingMessage(messageWrapper); + + CPPUNIT_ASSERT_EQUAL(forwardedBody, MockChatWindow::bodyFromMessage(window->lastAddedMessage_)); + CPPUNIT_ASSERT_EQUAL(false, window->lastAddedMessageSenderIsSelf_); + + auto recentChats = manager_->getRecentChats(); + CPPUNIT_ASSERT_EQUAL(recentChats.size(), size_t(1)); + CPPUNIT_ASSERT_EQUAL(recentChats[0].jid, originalMessage->getFrom().toBare()); + CPPUNIT_ASSERT_EQUAL(recentChats[0].activity, std::string("Some further text.")); + } + } + + void testCarbonsForwardedOutgoingMessageFromSecondResource() { + JID messageJID("testling@test.com/resource1"); + JID jid2 = jid_.toBare().withResource("someOtherResource"); + + MockChatWindow* window = new MockChatWindow(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(messageJID, uiEventStream_).Return(window); + + std::shared_ptr<Message> message(new Message()); + message->setFrom(messageJID); + std::string body("This is a legible message. >HEH@)oeueu"); + message->setBody(body); + manager_->handleIncomingMessage(message); + CPPUNIT_ASSERT_EQUAL(body, MockChatWindow::bodyFromMessage(window->lastAddedMessage_)); + + // incoming carbons message from another resource + { + auto originalMessage = std::make_shared<Message>(); + originalMessage->setFrom(jid2); + originalMessage->setTo(messageJID); + originalMessage->setType(Message::Chat); + originalMessage->setID("abcdefg123456"); + std::string forwardedBody = "Some text my other resource sent."; + originalMessage->setBody(forwardedBody); + originalMessage->addPayload(std::make_shared<DeliveryReceiptRequest>()); + + auto messageWrapper = createCarbonsMessage(std::make_shared<CarbonsSent>(), originalMessage); + + manager_->handleIncomingMessage(messageWrapper); + + CPPUNIT_ASSERT_EQUAL(forwardedBody, MockChatWindow::bodyFromMessage(window->lastAddedMessage_)); + CPPUNIT_ASSERT_EQUAL(true, window->lastAddedMessageSenderIsSelf_); + CPPUNIT_ASSERT_EQUAL(size_t(1), window->receiptChanges_.size()); + CPPUNIT_ASSERT_EQUAL(ChatWindow::ReceiptRequested, window->receiptChanges_[0].second); + + auto recentChats = manager_->getRecentChats(); + CPPUNIT_ASSERT_EQUAL(recentChats.size(), size_t(1)); + CPPUNIT_ASSERT_EQUAL(recentChats[0].jid, originalMessage->getTo().toBare()); + CPPUNIT_ASSERT_EQUAL(recentChats[0].activity, std::string("Some text my other resource sent.")); + } + + // incoming carbons message for the received delivery receipt to the other resource + { + auto originalMessage = std::make_shared<Message>(); + originalMessage->setFrom(messageJID); + originalMessage->setTo(jid2); + originalMessage->setType(Message::Chat); + originalMessage->addPayload(std::make_shared<DeliveryReceipt>("abcdefg123456")); + + auto messageWrapper = createCarbonsMessage(std::make_shared<CarbonsReceived>(), originalMessage); + + manager_->handleIncomingMessage(messageWrapper); + + CPPUNIT_ASSERT_EQUAL(size_t(2), window->receiptChanges_.size()); + CPPUNIT_ASSERT_EQUAL(ChatWindow::ReceiptReceived, window->receiptChanges_[1].second); + + //Delivery receipt should not change the latest recent entry. Checking for the original message. + auto recentChats = manager_->getRecentChats(); + CPPUNIT_ASSERT_EQUAL(recentChats.size(), size_t(1)); + CPPUNIT_ASSERT_EQUAL(recentChats[0].jid, messageJID.toBare()); + CPPUNIT_ASSERT_EQUAL(recentChats[0].activity, std::string("Some text my other resource sent.")); + } + } + + void testCarbonsForwardedIncomingDuplicates() { + JID messageJID("testling@test.com/resource1"); + JID jid2 = jid_.toBare().withResource("someOtherResource"); + + MockChatWindow* window = new MockChatWindow(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(messageJID, uiEventStream_).Return(window); + + std::shared_ptr<Message> message(new Message()); + message->setFrom(messageJID); + std::string body("This is a legible message. >HEH@)oeueu"); + message->setBody(body); + manager_->handleIncomingMessage(message); + CPPUNIT_ASSERT_EQUAL(body, MockChatWindow::bodyFromMessage(window->lastAddedMessage_)); + + // incoming carbons message from another resource and duplicate of it + { + auto originalMessage = std::make_shared<Message>(); + originalMessage->setFrom(messageJID); + originalMessage->setTo(jid2); + originalMessage->setID("BDD82F0B-2523-48BF-B8CA-17B23A314BC2"); + originalMessage->setType(Message::Chat); + std::string forwardedBody = "Some further text."; + originalMessage->setBody(forwardedBody); + + auto messageWrapper = createCarbonsMessage(std::make_shared<CarbonsReceived>(), originalMessage); + + manager_->handleIncomingMessage(messageWrapper); + + CPPUNIT_ASSERT_EQUAL(forwardedBody, MockChatWindow::bodyFromMessage(window->lastAddedMessage_)); + CPPUNIT_ASSERT_EQUAL(false, window->lastAddedMessageSenderIsSelf_); + window->resetLastMessages(); + + messageWrapper = createCarbonsMessage(std::make_shared<CarbonsReceived>(), originalMessage); + manager_->handleIncomingMessage(messageWrapper); + CPPUNIT_ASSERT_EQUAL(std::string(), MockChatWindow::bodyFromMessage(window->lastAddedMessage_)); + CPPUNIT_ASSERT_EQUAL(false, window->lastAddedMessageSenderIsSelf_); + + auto recentChats = manager_->getRecentChats(); + CPPUNIT_ASSERT_EQUAL(recentChats.size(), size_t(1)); + CPPUNIT_ASSERT_EQUAL(recentChats[0].jid, originalMessage->getFrom().toBare()); + CPPUNIT_ASSERT_EQUAL(recentChats[0].activity, std::string("Some further text.")); + } + } + + /** + * This test case ensures correct handling of the ideal case where the replace + * message refers to a message with a known ID. This results in the last + * message being replaced. + */ + void testChatControllerMessageCorrectionCorrectReplaceID() { + JID messageJID("testling@test.com/resource1"); + + MockChatWindow* window = new MockChatWindow(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(messageJID, uiEventStream_).Return(window); + + auto message = std::make_shared<Message>(); + message->setFrom(messageJID); + message->setTo(jid_); + message->setType(Message::Chat); + message->setBody("text before edit"); + message->setID("someID"); + manager_->handleIncomingMessage(message); + + CPPUNIT_ASSERT_EQUAL(std::string("text before edit"), MockChatWindow::bodyFromMessage(window->lastAddedMessage_)); + + message = std::make_shared<Message>(); + message->setFrom(messageJID); + message->setTo(jid_); + message->setType(Message::Chat); + message->setBody("text after edit"); + message->addPayload(std::make_shared<Replace>("someID")); + manager_->handleIncomingMessage(message); + + CPPUNIT_ASSERT_EQUAL(std::string("text before edit"), MockChatWindow::bodyFromMessage(window->lastAddedMessage_)); + CPPUNIT_ASSERT_EQUAL(std::string("text after edit"), MockChatWindow::bodyFromMessage(window->lastReplacedMessage_)); + } + + /** + * This test case ensures correct handling of the case where the replace + * message refers to a message with a unknown ID. The replace message should + * be treated like a non-repalce message in this case, with no replacement + * occuring. + */ + void testChatControllerMessageCorrectionIncorrectReplaceID() { + JID messageJID("testling@test.com/resource1"); + + MockChatWindow* window = new MockChatWindow(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(messageJID, uiEventStream_).Return(window); + + auto message = std::make_shared<Message>(); + message->setFrom(messageJID); + message->setTo(jid_); + message->setType(Message::Chat); + message->setBody("text before edit"); + message->setID("someID"); + manager_->handleIncomingMessage(message); + + CPPUNIT_ASSERT_EQUAL(std::string("text before edit"), MockChatWindow::bodyFromMessage(window->lastAddedMessage_)); + + message = std::make_shared<Message>(); + message->setFrom(messageJID); + message->setTo(jid_); + message->setType(Message::Chat); + message->setBody("text after failed edit"); + message->addPayload(std::make_shared<Replace>("wrongID")); + manager_->handleIncomingMessage(message); + + CPPUNIT_ASSERT_EQUAL(std::string("text after failed edit"), MockChatWindow::bodyFromMessage(window->lastAddedMessage_)); + CPPUNIT_ASSERT_EQUAL(std::string(""), MockChatWindow::bodyFromMessage(window->lastReplacedMessage_)); + } + + void testChatControllerMessageCorrectionReplaceBySameResource() { + JID messageJID("testling@test.com/resource1"); + + MockChatWindow* window = new MockChatWindow(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(messageJID, uiEventStream_).Return(window); + + auto message = std::make_shared<Message>(); + message->setFrom(messageJID); + message->setTo(jid_); + message->setType(Message::Chat); + message->setBody("text before edit"); + message->setID("someID"); + manager_->handleIncomingMessage(message); + + CPPUNIT_ASSERT_EQUAL(std::string("text before edit"), MockChatWindow::bodyFromMessage(window->lastAddedMessage_)); + + message = std::make_shared<Message>(); + message->setFrom(messageJID); + message->setTo(jid_); + message->setType(Message::Chat); + message->setBody("text after edit"); + message->addPayload(std::make_shared<Replace>("someID")); + manager_->handleIncomingMessage(message); + + CPPUNIT_ASSERT_EQUAL(std::string("text after edit"), MockChatWindow::bodyFromMessage(window->lastReplacedMessage_)); + } + + void testChatControllerMessageCorrectionReplaceByOtherResource() { + JID messageJID("testling@test.com/resource1"); + + MockChatWindow* window = new MockChatWindow(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(messageJID, uiEventStream_).Return(window); + + auto message = std::make_shared<Message>(); + message->setFrom(messageJID); + message->setTo(jid_); + message->setType(Message::Chat); + message->setBody("text before edit"); + message->setID("someID"); + manager_->handleIncomingMessage(message); + + CPPUNIT_ASSERT_EQUAL(std::string("text before edit"), MockChatWindow::bodyFromMessage(window->lastAddedMessage_)); + + message = std::make_shared<Message>(); + message->setFrom(messageJID.toBare().withResource("resource2")); + message->setTo(jid_); + message->setType(Message::Chat); + message->setBody("text after edit"); + message->addPayload(std::make_shared<Replace>("someID")); + manager_->handleIncomingMessage(message); + + CPPUNIT_ASSERT_EQUAL(std::string("text after edit"), MockChatWindow::bodyFromMessage(window->lastReplacedMessage_)); + } + + void testMUCControllerMessageCorrectionNoIDMatchRequired() { + JID mucJID("SomeMUCRoom@test.com"); + manager_->setOnline(true); + + // Open chat window to a sender. + MockChatWindow* window = new MockChatWindow(); + + std::vector<JID> jids; + jids.emplace_back("foo@test.com"); + jids.emplace_back("bar@test.com"); + + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(mucJID, uiEventStream_).Return(window); + + auto nickname = std::string("SomeNickName"); + // Join room + { + auto joinRoomEvent = std::make_shared<JoinMUCUIEvent>(mucJID, boost::optional<std::string>(), nickname); + uiEventStream_->send(joinRoomEvent); + } + + auto genRemoteMUCPresence = [=]() { + auto presence = Presence::create(); + presence->setFrom(mucJID.withResource(nickname)); + presence->setTo(jid_); + return presence; + }; + + { + auto presence = genRemoteMUCPresence(); + auto userPayload = std::make_shared<MUCUserPayload>(); + userPayload->addStatusCode(110); + userPayload->addItem(MUCItem(MUCOccupant::Owner, jid_, MUCOccupant::Moderator)); + presence->addPayload(userPayload); + stanzaChannel_->onPresenceReceived(presence); + } + + { + auto presence = genRemoteMUCPresence(); + presence->setFrom(mucJID.withResource("someDifferentNickname")); + auto userPayload = std::make_shared<MUCUserPayload>(); + userPayload->addItem(MUCItem(MUCOccupant::Member, JID("foo@bar.com"), MUCOccupant::Moderator)); + presence->addPayload(userPayload); + stanzaChannel_->onPresenceReceived(presence); + } + + { + Message::ref mucMirrored = std::make_shared<Message>(); + mucMirrored->setFrom(mucJID.withResource(nickname)); + mucMirrored->setTo(jid_); + mucMirrored->setType(Message::Groupchat); + mucMirrored->setID("fooBlaID_1"); + mucMirrored->setBody("Some misssssspelled message."); + manager_->handleIncomingMessage(mucMirrored); + } + CPPUNIT_ASSERT_EQUAL(std::string("Some misssssspelled message."), window->bodyFromMessage(window->lastAddedMessage_)); + + // Replace message with non-matching ID + { + Message::ref mucMirrored = std::make_shared<Message>(); + mucMirrored->setFrom(mucJID.withResource(nickname)); + mucMirrored->setTo(jid_); + mucMirrored->setType(Message::Groupchat); + mucMirrored->setID("fooBlaID_3"); + mucMirrored->setBody("Some correctly spelled message."); + mucMirrored->addPayload(std::make_shared<Replace>("fooBlaID_2")); + manager_->handleIncomingMessage(mucMirrored); + } + CPPUNIT_ASSERT_EQUAL(std::string("Some correctly spelled message."), window->bodyFromMessage(window->lastReplacedMessage_)); + } + + void impromptuChatSetup(MockChatWindow* window) { + stanzaChannel_->uniqueIDs_ = true; + JID mucJID("795B7BBE-9099-4A0D-81BA-C816F78E275C@test.com"); + manager_->setOnline(true); + + std::shared_ptr<IQ> infoRequest = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[1]); + CPPUNIT_ASSERT(infoRequest); + + std::shared_ptr<IQ> infoResponse = IQ::createResult(infoRequest->getFrom(), infoRequest->getTo(), infoRequest->getID()); + + DiscoInfo info; + info.addIdentity(DiscoInfo::Identity("Shakespearean Chat Service", "conference", "text")); + info.addFeature("http://jabber.org/protocol/muc"); + infoResponse->addPayload(std::make_shared<DiscoInfo>(info)); + stanzaChannel_->onIQReceived(infoResponse); + + std::vector<JID> jids; + jids.emplace_back("foo@test.com"); + jids.emplace_back("bar@test.com"); + + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(mucJID, uiEventStream_).Return(window); + uiEventStream_->send(std::make_shared<CreateImpromptuMUCUIEvent>(jids, mucJID, "")); + CPPUNIT_ASSERT_EQUAL(std::string("bar@test.com, foo@test.com"), manager_->getRecentChats()[0].getTitle()); + + // Check the MUC security marking request + auto mucInfoRequest = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[2]); + CPPUNIT_ASSERT(mucInfoRequest); + + auto mucJoinPresence = std::dynamic_pointer_cast<Presence>(stanzaChannel_->sentStanzas[3]); + CPPUNIT_ASSERT(mucJoinPresence); + + // MUC presence reply + auto mucResponse = Presence::create(); + mucResponse->setTo(jid_); + mucResponse->setFrom(mucJoinPresence->getTo()); + mucResponse->addPayload([]() { + auto mucUser = std::make_shared<MUCUserPayload>(); + mucUser->addItem(MUCItem(MUCOccupant::Member, MUCOccupant::Participant)); + mucUser->addStatusCode(MUCUserPayload::StatusCode(110)); + mucUser->addStatusCode(MUCUserPayload::StatusCode(210)); + return mucUser; + }()); + stanzaChannel_->onPresenceReceived(mucResponse); + + // Before people join the impromptu room, the title is based on names coming from Roster + CPPUNIT_ASSERT_EQUAL(std::string("bar@test.com, foo@test.com"), manager_->getRecentChats()[0].getTitle()); + + auto mucParticipantJoined = [&](const JID& jid) { + auto participantJoinedPresence = Presence::create(); + participantJoinedPresence->setTo(jid_); + participantJoinedPresence->setFrom(mucJID.withResource(jid.toString())); + auto mucUser = std::make_shared<MUCUserPayload>(); + mucUser->addItem(MUCItem(MUCOccupant::Member, MUCOccupant::Participant)); + participantJoinedPresence->addPayload(mucUser); + return participantJoinedPresence; + }; + + for (const auto& participantJID : jids) { + stanzaChannel_->onPresenceReceived(mucParticipantJoined(participantJID)); + } + } + + void testImpromptuChatTitle() { + // Open a new chat window to a sender. + MockChatWindow* window = new MockChatWindow(); + impromptuChatSetup(window); + // After people joined, the title is the list of participant nicknames or names coming from Roster (if nicknames are unavailable) + CPPUNIT_ASSERT_EQUAL(std::string("bar@test.com, foo@test.com"), manager_->getRecentChats()[0].getTitle()); + } + + void testImpromptuChatWindowTitle() { + // Open a new chat window to a sender. + MockChatWindow* window = new MockChatWindow(); + impromptuChatSetup(window); + // After people joined, the title of chat window is combined of participant nicknames or names coming from Roster (if nicknames are unavailable) + CPPUNIT_ASSERT_EQUAL(std::string("bar@test.com, foo@test.com"), window->name_); + } + + void testStandardMUCChatWindowTitle() { + JID mucJID("mucroom@rooms.test.com"); + std::string nickname = "toodles"; + + MockChatWindow* window = new MockChatWindow(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(mucJID, uiEventStream_).Return(window); + + uiEventStream_->send(std::make_shared<JoinMUCUIEvent>(mucJID, boost::optional<std::string>(), nickname)); + CPPUNIT_ASSERT_EQUAL(std::string("mucroom"), window->name_); + } + + std::shared_ptr<Storage> createBookmarkStorageWithJID(std::shared_ptr<IQ> bookmarkRequest, const JID& jid, const bool autojoin) { + CPPUNIT_ASSERT(bookmarkRequest); + CPPUNIT_ASSERT_EQUAL(IQ::Get, bookmarkRequest->getType()); + + auto privateStorage = bookmarkRequest->getPayload<PrivateStorage>(); + CPPUNIT_ASSERT(privateStorage); + + auto storage = std::dynamic_pointer_cast<Storage>(privateStorage->getPayload()); + CPPUNIT_ASSERT(storage); + + auto roomsStorage = std::make_shared<Storage>(); + if (jid.isValid()) { + auto room = Storage::Room(); + room.jid = jid; + room.autoJoin = autojoin; + roomsStorage->addRoom(room); + } + return roomsStorage; + } + + void testReceivingBookmarksWithDomainJID() { + auto bookmarkRequest = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[0]); + auto response = IQ::createResult( + bookmarkRequest->getFrom(), + bookmarkRequest->getTo(), + bookmarkRequest->getID(), + std::make_shared<PrivateStorage>(createBookmarkStorageWithJID(bookmarkRequest, "montague.lit", true)) + ); + stanzaChannel_->onIQReceived(response); + } + + void testReceivingBookmarksWithBareJID() { + auto bookmarkRequest = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[0]); + MockChatWindow* window = new MockChatWindow(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(JID("example@montague.lit"), uiEventStream_).Return(window); + auto response = IQ::createResult( + bookmarkRequest->getFrom(), + bookmarkRequest->getTo(), + bookmarkRequest->getID(), + std::make_shared<PrivateStorage>(createBookmarkStorageWithJID(bookmarkRequest, "example@montague.lit", true)) + ); + stanzaChannel_->onIQReceived(response); + } + + void testReceivingNoBookmarks() { + auto bookmarkRequest = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[0]); + auto response = IQ::createResult( + bookmarkRequest->getFrom(), + bookmarkRequest->getTo(), + bookmarkRequest->getID(), + std::make_shared<PrivateStorage>() + ); + stanzaChannel_->onIQReceived(response); + } + + void testReceivingNullBookmarks() { + auto bookmarkRequest = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[0]); + auto response = IQ::createResult( + bookmarkRequest->getFrom(), + bookmarkRequest->getTo(), + bookmarkRequest->getID(), + nullptr + ); + stanzaChannel_->onIQReceived(response); + } + + void testReceivingBookmarksError() { + auto bookmarkRequest = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[0]); + auto response = IQ::createError( + bookmarkRequest->getFrom(), + bookmarkRequest->getTo(), + bookmarkRequest->getID(), + ErrorPayload::Condition::ServiceUnavailable, + ErrorPayload::Type::Cancel, + nullptr + ); + stanzaChannel_->onIQReceived(response); + } + + void testReceivingBookmarksWithFullJID() { + auto bookmarkRequest = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[0]); + auto response = IQ::createResult( + bookmarkRequest->getFrom(), + bookmarkRequest->getTo(), + bookmarkRequest->getID(), + std::make_shared<PrivateStorage>(createBookmarkStorageWithJID(bookmarkRequest, "example@montague.lit/someresource", true)) + ); + stanzaChannel_->onIQReceived(response); + } + + void testAutoJoinBookmarksAndChattables() { + + auto bookmarkRequest = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[0]); + auto roomsStorage = createBookmarkStorageWithJID(bookmarkRequest, "autojoin@bookmark.lit", true); + auto room = Storage::Room(); + room.jid = "noAutojoin@bookmark.lit"; + roomsStorage->addRoom(room); + + //Only autojoin@bookmark.lit window should open. + MockChatWindow* autojoinBookmarkWindow = new MockChatWindow(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(JID("autojoin@bookmark.lit"), uiEventStream_).Return(autojoinBookmarkWindow); + + auto response = IQ::createResult( + bookmarkRequest->getFrom(), + bookmarkRequest->getTo(), + bookmarkRequest->getID(), + std::make_shared<PrivateStorage>(roomsStorage) + ); + stanzaChannel_->onIQReceived(response); + //Both bookmarks should be added to the chattables. + CPPUNIT_ASSERT_EQUAL(size_t(2), chattables_->get().size()); + auto autoJoinState = chattables_->getState("autojoin@bookmark.lit"); + CPPUNIT_ASSERT(autoJoinState.type == Chattables::State::Type::Room); + CPPUNIT_ASSERT_EQUAL(autoJoinState.status, StatusShow::Online); + auto noAutoJoinState = chattables_->getState("noAutojoin@bookmark.lit"); + CPPUNIT_ASSERT(noAutoJoinState.type == Chattables::State::Type::Room); + CPPUNIT_ASSERT_EQUAL(noAutoJoinState.status, StatusShow::None); + } + + void testJoinNoAutojoinBookmark() { + + auto bookmarkRequest = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[0]); + auto roomsStorage = createBookmarkStorageWithJID(bookmarkRequest, "example@montague.lit", false); + + auto response = IQ::createResult( + bookmarkRequest->getFrom(), + bookmarkRequest->getTo(), + bookmarkRequest->getID(), + std::make_shared<PrivateStorage>(roomsStorage) + ); + stanzaChannel_->onIQReceived(response); + + //Join previous bookmarked room, expecting no increase in chattables and change of autojoin in bookmark to true + MockChatWindow* newExampleChatWindow = new MockChatWindow(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(JID("example@montague.lit"), uiEventStream_).Return(newExampleChatWindow); + uiEventStream_->send(std::make_shared<JoinMUCUIEvent>("example@montague.lit", boost::optional<std::string>(), boost::optional<std::string>())); + CPPUNIT_ASSERT_EQUAL(size_t(1), chattables_->get().size()); + auto state = chattables_->getState("example@montague.lit"); + CPPUNIT_ASSERT(state.type == Chattables::State::Type::Room); + CPPUNIT_ASSERT_EQUAL(state.status, StatusShow::Online); + + auto bookmarks = manager_->getBookmarkManager()->getBookmarks(); + CPPUNIT_ASSERT_EQUAL(bookmarks.size(), size_t(1)); + CPPUNIT_ASSERT(bookmarks[0].getRoom() == JID("example@montague.lit")); + CPPUNIT_ASSERT(bookmarks[0].getAutojoin()); + } + + void testJoinAndBookmarkMUC() { + auto bookmarkRequest = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[0]); + auto roomsStorage = createBookmarkStorageWithJID(bookmarkRequest, "", true); + auto response = IQ::createResult( + bookmarkRequest->getFrom(), + bookmarkRequest->getTo(), + bookmarkRequest->getID(), + std::make_shared<PrivateStorage>(roomsStorage) + ); + stanzaChannel_->onIQReceived(response); + + //Join non-bookmarked room expecting for the room to get bookmarked with autojoin to true + MockChatWindow* exampleChatWindow = new MockChatWindow(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With("example@montague.lit", uiEventStream_).Return(exampleChatWindow); + uiEventStream_->send(std::make_shared<JoinMUCUIEvent>("example@montague.lit", boost::optional<std::string>(), boost::optional<std::string>())); + { + CPPUNIT_ASSERT_EQUAL(size_t(1), chattables_->get().size()); + auto state = chattables_->getState("example@montague.lit"); + CPPUNIT_ASSERT(state.type == Chattables::State::Type::Room); + CPPUNIT_ASSERT_EQUAL(state.status, StatusShow::Online); + + auto bookmarks = manager_->getBookmarkManager()->getBookmarks(); + CPPUNIT_ASSERT_EQUAL(bookmarks.size(), size_t(1)); + CPPUNIT_ASSERT(bookmarks[0].getRoom() == JID("example@montague.lit")); + CPPUNIT_ASSERT(bookmarks[0].getAutojoin()); + + } + //Exiting room that is bookmarked, expecting chattable to stay but bookmark autojoin change to false. + exampleChatWindow->onClosed(); + { + CPPUNIT_ASSERT_EQUAL(size_t(1), chattables_->get().size()); + auto bookmarks = manager_->getBookmarkManager()->getBookmarks(); + CPPUNIT_ASSERT_EQUAL(bookmarks.size(), size_t(1)); + CPPUNIT_ASSERT(bookmarks[0].getRoom() == JID("example@montague.lit")); + CPPUNIT_ASSERT(!bookmarks[0].getAutojoin()); + } + } private: - boost::shared_ptr<Message> makeDeliveryReceiptTestMessage(const JID& from, const std::string& id) { - boost::shared_ptr<Message> message = boost::make_shared<Message>(); - message->setFrom(from); - message->setID(id); - message->setBody("This will cause the window to open"); - message->addPayload(boost::make_shared<DeliveryReceiptRequest>()); - return message; - } - - size_t st(int i) { - return static_cast<size_t>(i); - } + std::shared_ptr<Message> makeDeliveryReceiptTestMessage(const JID& from, const std::string& id) { + std::shared_ptr<Message> message = std::make_shared<Message>(); + message->setFrom(from); + message->setID(id); + message->setBody("This will cause the window to open"); + message->addPayload(std::make_shared<DeliveryReceiptRequest>()); + return message; + } + + size_t st(int i) { + return static_cast<size_t>(i); + } + + void handleHighlightAction(const HighlightAction& action) { + handledHighlightActions_++; + if (action.getSoundFilePath()) { + soundsPlayed_.insert(action.getSoundFilePath().get_value_or("")); + } + } private: - JID jid_; - ChatsManager* manager_; - DummyStanzaChannel* stanzaChannel_; - IQChannel* iqChannel_; - IQRouter* iqRouter_; - EventController* eventController_; - ChatWindowFactory* chatWindowFactory_; - JoinMUCWindowFactory* joinMUCWindowFactory_; - NickResolver* nickResolver_; - PresenceOracle* presenceOracle_; - AvatarManager* avatarManager_; - boost::shared_ptr<DiscoInfo> serverDiscoInfo_; - XMPPRosterImpl* xmppRoster_; - PresenceSender* presenceSender_; - MockRepository* mocks_; - UIEventStream* uiEventStream_; - ChatListWindowFactory* chatListWindowFactory_; - WhiteboardWindowFactory* whiteboardWindowFactory_; - MUCSearchWindowFactory* mucSearchWindowFactory_; - MUCRegistry* mucRegistry_; - DirectedPresenceSender* directedPresenceSender_; - EntityCapsManager* entityCapsManager_; - CapsProvider* capsProvider_; - MUCManager* mucManager_; - DummySettingsProvider* settings_; - ProfileSettingsProvider* profileSettings_; - ChatListWindow* chatListWindow_; - FileTransferOverview* ftOverview_; - FileTransferManager* ftManager_; - WhiteboardSessionManager* wbSessionManager_; - WhiteboardManager* wbManager_; - HighlightManager* highlightManager_; - ClientBlockListManager* clientBlockListManager_; - std::map<std::string, std::string> emoticons_; + JID jid_; + std::unique_ptr<DummyNotifier> notifier_; + ExtendedChatsManager* manager_; + DummyStanzaChannel* stanzaChannel_; + IQRouter* iqRouter_; + EventController* eventController_; + ChatWindowFactory* chatWindowFactory_; + JoinMUCWindowFactory* joinMUCWindowFactory_; + NickResolver* nickResolver_; + PresenceOracle* presenceOracle_; + AvatarManager* avatarManager_; + EventNotifier* eventNotifier_; + std::shared_ptr<DiscoInfo> serverDiscoInfo_; + XMPPRosterImpl* xmppRoster_; + PresenceSender* presenceSender_; + MockRepository* mocks_; + UIEventStream* uiEventStream_; + ChatListWindowFactory* chatListWindowFactory_; + WhiteboardWindowFactory* whiteboardWindowFactory_; + MUCSearchWindowFactory* mucSearchWindowFactory_; + MUCRegistry* mucRegistry_; + DirectedPresenceSender* directedPresenceSender_; + DummyEntityCapsProvider* entityCapsProvider_; + MUCManager* mucManager_; + DummySettingsProvider* settings_; + ProfileSettingsProvider* profileSettings_; + ChatListWindow* chatListWindow_; + FileTransferOverview* ftOverview_; + FileTransferManager* ftManager_; + WhiteboardSessionManager* wbSessionManager_; + WhiteboardManager* wbManager_; + HighlightManager* highlightManager_; + ClientBlockListManager* clientBlockListManager_; + VCardManager* vcardManager_; + CryptoProvider* crypto_; + VCardStorage* vcardStorage_; + std::map<std::string, std::string> emoticons_; + int handledHighlightActions_; + std::set<std::string> soundsPlayed_; + DummyTimerFactory* timerFactory_; + std::unique_ptr<Chattables> chattables_; + }; CPPUNIT_TEST_SUITE_REGISTRATION(ChatsManagerTest); - diff --git a/Swift/Controllers/Chat/UnitTest/ChattablesTest.cpp b/Swift/Controllers/Chat/UnitTest/ChattablesTest.cpp new file mode 100644 index 0000000..f30e3fd --- /dev/null +++ b/Swift/Controllers/Chat/UnitTest/ChattablesTest.cpp @@ -0,0 +1,140 @@ +/* + * Copyright (c) 2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#include <gtest/gtest.h> +#include <hippomocks.h> + +#include <Swift/Controllers/Chat/Chattables.h> + +// Clang wrongly things that tests for 0 are using 0 as null. +#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" + +using namespace Swift; + +class ChattablesTest : public ::testing::Test { + protected: + void SetUp() { + beginListSize_ = 0; + endListSize_ = 0; + callsToBegin_ = 0; + callsToEnd_ = 0; + } + + void TearDown() { + } + + void updateBeginListSize(int listSize); + void updateEndListSize(); + + Chattables chattables_; + int beginListSize_; + int endListSize_; + int callsToBegin_; + int callsToEnd_; +}; + +void ChattablesTest::updateBeginListSize(int listSize) { + beginListSize_ = listSize; + callsToBegin_++; +} + +void ChattablesTest::updateEndListSize() { + endListSize_ = chattables_.get().size(); + callsToEnd_++; +} + +TEST_F(ChattablesTest, testAddJID) { + chattables_.onBeginAdd.connect([this](int listSize){ updateBeginListSize(listSize); }); + chattables_.onAdded.connect([this](){ updateEndListSize(); }); + JID jid("user@swift.jid"); + + chattables_.addJID(jid, Chattables::State::Type::Person); + ASSERT_EQ(0, beginListSize_); + ASSERT_EQ(1, endListSize_); + ASSERT_EQ(1, callsToBegin_); + ASSERT_EQ(1, callsToEnd_); + + ASSERT_EQ(jid, chattables_.get()[0]); + Chattables::State state = chattables_.getState(jid); + ASSERT_EQ(jid, state.jid); + ASSERT_EQ(Chattables::State::Type::Person, state.type); +} + +TEST_F(ChattablesTest, testAddMultipleJIDs) { + chattables_.onBeginAdd.connect([this](int listSize){ updateBeginListSize(listSize); }); + chattables_.onAdded.connect([this](){ updateEndListSize(); }); + JID jid0("user0@swift.jid"); + JID jid1("user1@swift.jid"); + JID jid2("user2@swift.jid"); + + chattables_.addJID(jid0, Chattables::State::Type::Person); + ASSERT_EQ(0, beginListSize_); + ASSERT_EQ(1, endListSize_); + ASSERT_EQ(1, callsToBegin_); + ASSERT_EQ(1, callsToEnd_); + chattables_.addJID(jid1, Chattables::State::Type::Person); + ASSERT_EQ(1, beginListSize_); + ASSERT_EQ(2, endListSize_); + ASSERT_EQ(2, callsToBegin_); + ASSERT_EQ(2, callsToEnd_); + chattables_.addJID(jid2, Chattables::State::Type::Person); + ASSERT_EQ(2, beginListSize_); + ASSERT_EQ(3, endListSize_); + ASSERT_EQ(3, callsToBegin_); + ASSERT_EQ(3, callsToEnd_); +} + +TEST_F(ChattablesTest, testAddRoomJID) { + chattables_.onBeginAdd.connect([this](int listSize){ updateBeginListSize(listSize); }); + chattables_.onAdded.connect([this](){ updateEndListSize(); }); + JID jid("room@swift.jid"); + + chattables_.addJID(jid, Chattables::State::Type::Room); + ASSERT_EQ(0, beginListSize_); + ASSERT_EQ(1, endListSize_); + ASSERT_EQ(1, callsToBegin_); + ASSERT_EQ(1, callsToEnd_); + + ASSERT_EQ(jid, chattables_.get()[0]); + Chattables::State state = chattables_.getState(jid); + ASSERT_EQ(jid, state.jid); + ASSERT_EQ(Chattables::State::Type::Room, state.type); +} + +TEST_F(ChattablesTest, testSetState) { + JID jid("user@swift.jid"); + chattables_.addJID(jid, Chattables::State::Type::Person); + ASSERT_EQ(1, chattables_.get().size()); + ASSERT_EQ(jid, chattables_.get()[0]); + ASSERT_EQ(Chattables::State::Type::Person, chattables_.getState(jid).type); + ASSERT_EQ(StatusShow::None, chattables_.getState(jid).status); + + JID returnedJID; + int returnedIndex; + int callsToChanged = 0; + chattables_.onChanged.connect([&returnedJID, &returnedIndex, &callsToChanged](const JID& jid, int index){ + returnedJID = jid; + returnedIndex = index; + callsToChanged++; + }); + + Chattables::State newState; + newState.jid = jid; + newState.type = Chattables::State::Type::Room; + newState.status = StatusShow::Online; + chattables_.setState(jid, newState); + + auto storedState = chattables_.getState(jid); + + ASSERT_EQ(1, chattables_.get().size()); + ASSERT_EQ(jid, chattables_.get()[0]); + ASSERT_EQ(jid, returnedJID); + ASSERT_EQ(0, returnedIndex); + ASSERT_EQ(1, callsToChanged); + ASSERT_EQ(jid, storedState.jid); + ASSERT_EQ(Chattables::State::Type::Room, storedState.type); + ASSERT_EQ(StatusShow::Online, storedState.status); +} diff --git a/Swift/Controllers/Chat/UnitTest/MUCControllerTest.cpp b/Swift/Controllers/Chat/UnitTest/MUCControllerTest.cpp index 5ca0687..5339c7b 100644 --- a/Swift/Controllers/Chat/UnitTest/MUCControllerTest.cpp +++ b/Swift/Controllers/Chat/UnitTest/MUCControllerTest.cpp @@ -1,371 +1,964 @@ /* - * Copyright (c) 2010-2013 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ -#include <cppunit/extensions/HelperMacros.h> -#include <cppunit/extensions/TestFactoryRegistry.h> #include <boost/algorithm/string.hpp> + +#include <memory> + +#include <gtest/gtest.h> #include <hippomocks.h> -#include "Swift/Controllers/XMPPEvents/EventController.h" -#include "Swiften/Presence/DirectedPresenceSender.h" -#include "Swiften/Presence/StanzaChannelPresenceSender.h" -#include "Swiften/Avatars/NullAvatarManager.h" -#include "Swift/Controllers/Chat/MUCController.h" -#include "Swift/Controllers/UIInterfaces/ChatWindow.h" -#include "Swift/Controllers/UIInterfaces/ChatWindowFactory.h" -#include "Swiften/Client/NickResolver.h" -#include "Swiften/Roster/XMPPRoster.h" -#include "Swift/Controllers/UIEvents/UIEventStream.h" -#include "Swift/Controllers/UnitTest/MockChatWindow.h" -#include "Swiften/Client/DummyStanzaChannel.h" -#include "Swiften/Queries/DummyIQChannel.h" -#include "Swiften/Presence/PresenceOracle.h" -#include "Swiften/Network/TimerFactory.h" -#include "Swiften/Elements/MUCUserPayload.h" -#include "Swiften/Disco/DummyEntityCapsProvider.h" -#include <Swiften/VCards/VCardMemoryStorage.h> +#include <Swiften/Avatars/NullAvatarManager.h> +#include <Swiften/Client/ClientBlockListManager.h> +#include <Swiften/Client/DummyStanzaChannel.h> +#include <Swiften/Client/NickResolver.h> +#include <Swiften/Crypto/CryptoProvider.h> #include <Swiften/Crypto/PlatformCryptoProvider.h> +#include <Swiften/Disco/DummyEntityCapsProvider.h> +#include <Swiften/Elements/MUCUserPayload.h> +#include <Swiften/Elements/Thread.h> +#include <Swiften/MUC/MUCBookmarkManager.h> +#include <Swiften/MUC/UnitTest/MockMUC.h> +#include <Swiften/Network/TimerFactory.h> +#include <Swiften/Presence/DirectedPresenceSender.h> +#include <Swiften/Presence/PresenceOracle.h> +#include <Swiften/Presence/StanzaChannelPresenceSender.h> +#include <Swiften/Queries/DummyIQChannel.h> +#include <Swiften/Roster/XMPPRoster.h> +#include <Swiften/Roster/XMPPRosterImpl.h> #include <Swiften/VCards/VCardManager.h> -#include <Swift/Controllers/Settings/DummySettingsProvider.h> +#include <Swiften/VCards/VCardMemoryStorage.h> + #include <Swift/Controllers/Chat/ChatMessageParser.h> +#include <Swift/Controllers/Chat/Chattables.h> +#include <Swift/Controllers/Chat/MUCController.h> #include <Swift/Controllers/Chat/UserSearchController.h> +#include <Swift/Controllers/Roster/GroupRosterItem.h> +#include <Swift/Controllers/Roster/Roster.h> +#include <Swift/Controllers/SettingConstants.h> +#include <Swift/Controllers/Settings/DummySettingsProvider.h> +#include <Swift/Controllers/UIEvents/UIEventStream.h> +#include <Swift/Controllers/UIInterfaces/ChatWindow.h> +#include <Swift/Controllers/UIInterfaces/ChatWindowFactory.h> #include <Swift/Controllers/UIInterfaces/UserSearchWindowFactory.h> -#include <Swiften/Crypto/CryptoProvider.h> +#include <Swift/Controllers/UnitTest/MockChatWindow.h> +#include <Swift/Controllers/XMPPEvents/EventController.h> + +// Clang wrongly things that tests for 0 are using 0 as null. +#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" using namespace Swift; -class MUCControllerTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(MUCControllerTest); - CPPUNIT_TEST(testJoinPartStringContructionSimple); - CPPUNIT_TEST(testJoinPartStringContructionMixed); - CPPUNIT_TEST(testAppendToJoinParts); - CPPUNIT_TEST(testAddressedToSelf); - CPPUNIT_TEST(testNotAddressedToSelf); - CPPUNIT_TEST(testAddressedToSelfBySelf); - CPPUNIT_TEST(testMessageWithEmptyLabelItem); - CPPUNIT_TEST(testMessageWithLabelItem); - CPPUNIT_TEST(testCorrectMessageWithLabelItem); - CPPUNIT_TEST_SUITE_END(); - -public: - void setUp() { - crypto_ = boost::shared_ptr<CryptoProvider>(PlatformCryptoProvider::create()); - self_ = JID("girl@wonderland.lit/rabbithole"); - nick_ = "aLiCe"; - mucJID_ = JID("teaparty@rooms.wonderland.lit"); - mocks_ = new MockRepository(); - stanzaChannel_ = new DummyStanzaChannel(); - iqChannel_ = new DummyIQChannel(); - iqRouter_ = new IQRouter(iqChannel_); - eventController_ = new EventController(); - chatWindowFactory_ = mocks_->InterfaceMock<ChatWindowFactory>(); - userSearchWindowFactory_ = mocks_->InterfaceMock<UserSearchWindowFactory>(); - presenceOracle_ = new PresenceOracle(stanzaChannel_); - presenceSender_ = new StanzaChannelPresenceSender(stanzaChannel_); - directedPresenceSender_ = new DirectedPresenceSender(presenceSender_); - uiEventStream_ = new UIEventStream(); - avatarManager_ = new NullAvatarManager(); - TimerFactory* timerFactory = NULL; - window_ = new MockChatWindow(); - mucRegistry_ = new MUCRegistry(); - entityCapsProvider_ = new DummyEntityCapsProvider(); - settings_ = new DummySettingsProvider(); - highlightManager_ = new HighlightManager(settings_); - muc_ = boost::make_shared<MUC>(stanzaChannel_, iqRouter_, directedPresenceSender_, mucJID_, mucRegistry_); - mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(muc_->getJID(), uiEventStream_).Return(window_); - chatMessageParser_ = new ChatMessageParser(std::map<std::string, std::string>()); - vcardStorage_ = new VCardMemoryStorage(crypto_.get()); - vcardManager_ = new VCardManager(self_, iqRouter_, vcardStorage_); - controller_ = new MUCController (self_, muc_, boost::optional<std::string>(), nick_, stanzaChannel_, iqRouter_, chatWindowFactory_, presenceOracle_, avatarManager_, uiEventStream_, false, timerFactory, eventController_, entityCapsProvider_, NULL, NULL, mucRegistry_, highlightManager_, chatMessageParser_, false, NULL); - } - - void tearDown() { - delete vcardManager_; - delete vcardStorage_; - delete highlightManager_; - delete settings_; - delete entityCapsProvider_; - delete controller_; - delete eventController_; - delete presenceOracle_; - delete mocks_; - delete uiEventStream_; - delete stanzaChannel_; - delete presenceSender_; - delete directedPresenceSender_; - delete iqRouter_; - delete iqChannel_; - delete mucRegistry_; - delete avatarManager_; - delete chatMessageParser_; - } - - void finishJoin() { - Presence::ref presence(new Presence()); - presence->setFrom(JID(muc_->getJID().toString() + "/" + nick_)); - MUCUserPayload::ref status(new MUCUserPayload()); - MUCUserPayload::StatusCode code; - code.code = 110; - status->addStatusCode(code); - presence->addPayload(status); - stanzaChannel_->onPresenceReceived(presence); - } - - void testAddressedToSelf() { - finishJoin(); - Message::ref message(new Message()); - - message = Message::ref(new Message()); - message->setFrom(JID(muc_->getJID().toString() + "/otherperson")); - message->setBody("basic " + nick_ + " test."); - message->setType(Message::Groupchat); - controller_->handleIncomingMessage(MessageEvent::ref(new MessageEvent(message))); - CPPUNIT_ASSERT_EQUAL((size_t)1, eventController_->getEvents().size()); - - message = Message::ref(new Message()); - message->setFrom(JID(muc_->getJID().toString() + "/otherperson")); - message->setBody(nick_ + ": hi there"); - message->setType(Message::Groupchat); - controller_->handleIncomingMessage(MessageEvent::ref(new MessageEvent(message))); - CPPUNIT_ASSERT_EQUAL((size_t)2, eventController_->getEvents().size()); - - message->setFrom(JID(muc_->getJID().toString() + "/other")); - message->setBody("Hi there " + nick_); - message->setType(Message::Groupchat); - controller_->handleIncomingMessage(MessageEvent::ref(new MessageEvent(message))); - CPPUNIT_ASSERT_EQUAL((size_t)3, eventController_->getEvents().size()); - - message = Message::ref(new Message()); - message->setFrom(JID(muc_->getJID().toString() + "/other2")); - message->setBody("Hi " + boost::to_lower_copy(nick_) + "."); - message->setType(Message::Groupchat); - controller_->handleIncomingMessage(MessageEvent::ref(new MessageEvent(message))); - CPPUNIT_ASSERT_EQUAL((size_t)4, eventController_->getEvents().size()); - - message = Message::ref(new Message()); - message->setFrom(JID(muc_->getJID().toString() + "/other3")); - message->setBody("Hi bert."); - message->setType(Message::Groupchat); - controller_->handleIncomingMessage(MessageEvent::ref(new MessageEvent(message))); - CPPUNIT_ASSERT_EQUAL((size_t)4, eventController_->getEvents().size()); - - message = Message::ref(new Message()); - message->setFrom(JID(muc_->getJID().toString() + "/other2")); - message->setBody("Hi " + boost::to_lower_copy(nick_) + "ie."); - message->setType(Message::Groupchat); - controller_->handleIncomingMessage(MessageEvent::ref(new MessageEvent(message))); - CPPUNIT_ASSERT_EQUAL((size_t)4, eventController_->getEvents().size()); - } - - void testNotAddressedToSelf() { - finishJoin(); - Message::ref message(new Message()); - message->setFrom(JID(muc_->getJID().toString() + "/other3")); - message->setBody("Hi there Hatter"); - message->setType(Message::Groupchat); - controller_->handleIncomingMessage(MessageEvent::ref(new MessageEvent(message))); - CPPUNIT_ASSERT_EQUAL((size_t)0, eventController_->getEvents().size()); - } - - void testAddressedToSelfBySelf() { - finishJoin(); - Message::ref message(new Message()); - message->setFrom(JID(muc_->getJID().toString() + "/" + nick_)); - message->setBody("Hi there " + nick_); - message->setType(Message::Groupchat); - controller_->handleIncomingMessage(MessageEvent::ref(new MessageEvent(message))); - CPPUNIT_ASSERT_EQUAL((size_t)0, eventController_->getEvents().size()); - } - - void testMessageWithEmptyLabelItem() { - SecurityLabelsCatalog::Item label; - label.setSelector("Bob"); - window_->label_ = label; - boost::shared_ptr<DiscoInfo> features = boost::make_shared<DiscoInfo>(); - features->addFeature(DiscoInfo::SecurityLabelsCatalogFeature); - controller_->setAvailableServerFeatures(features); - IQ::ref iq = iqChannel_->iqs_[iqChannel_->iqs_.size() - 1]; - SecurityLabelsCatalog::ref labelPayload = boost::make_shared<SecurityLabelsCatalog>(); - labelPayload->addItem(label); - IQ::ref result = IQ::createResult(self_, iq->getID(), labelPayload); - iqChannel_->onIQReceived(result); - std::string messageBody("agamemnon"); - window_->onSendMessageRequest(messageBody, false); - boost::shared_ptr<Stanza> rawStanza = stanzaChannel_->sentStanzas[stanzaChannel_->sentStanzas.size() - 1]; - Message::ref message = boost::dynamic_pointer_cast<Message>(rawStanza); - CPPUNIT_ASSERT_EQUAL(iq->getTo(), result->getFrom()); - CPPUNIT_ASSERT(window_->labelsEnabled_); - CPPUNIT_ASSERT(stanzaChannel_->isAvailable()); /* Otherwise will prevent sends. */ - CPPUNIT_ASSERT(message); - CPPUNIT_ASSERT_EQUAL(messageBody, message->getBody()); - CPPUNIT_ASSERT(!message->getPayload<SecurityLabel>()); - } - - void testMessageWithLabelItem() { - SecurityLabel::ref label = boost::make_shared<SecurityLabel>(); - label->setLabel("a"); - SecurityLabelsCatalog::Item labelItem; - labelItem.setSelector("Bob"); - labelItem.setLabel(label); - window_->label_ = labelItem; - boost::shared_ptr<DiscoInfo> features = boost::make_shared<DiscoInfo>(); - features->addFeature(DiscoInfo::SecurityLabelsCatalogFeature); - controller_->setAvailableServerFeatures(features); - IQ::ref iq = iqChannel_->iqs_[iqChannel_->iqs_.size() - 1]; - SecurityLabelsCatalog::ref labelPayload = boost::make_shared<SecurityLabelsCatalog>(); - labelPayload->addItem(labelItem); - IQ::ref result = IQ::createResult(self_, iq->getID(), labelPayload); - iqChannel_->onIQReceived(result); - std::string messageBody("agamemnon"); - window_->onSendMessageRequest(messageBody, false); - boost::shared_ptr<Stanza> rawStanza = stanzaChannel_->sentStanzas[stanzaChannel_->sentStanzas.size() - 1]; - Message::ref message = boost::dynamic_pointer_cast<Message>(rawStanza); - CPPUNIT_ASSERT_EQUAL(iq->getTo(), result->getFrom()); - CPPUNIT_ASSERT(window_->labelsEnabled_); - CPPUNIT_ASSERT(stanzaChannel_->isAvailable()); /* Otherwise will prevent sends. */ - CPPUNIT_ASSERT(message); - CPPUNIT_ASSERT_EQUAL(messageBody, message->getBody()); - CPPUNIT_ASSERT_EQUAL(label, message->getPayload<SecurityLabel>()); - } - - void testCorrectMessageWithLabelItem() { - SecurityLabel::ref label = boost::make_shared<SecurityLabel>(); - label->setLabel("a"); - SecurityLabelsCatalog::Item labelItem; - labelItem.setSelector("Bob"); - labelItem.setLabel(label); - SecurityLabel::ref label2 = boost::make_shared<SecurityLabel>(); - label->setLabel("b"); - SecurityLabelsCatalog::Item labelItem2; - labelItem2.setSelector("Charlie"); - labelItem2.setLabel(label2); - window_->label_ = labelItem; - boost::shared_ptr<DiscoInfo> features = boost::make_shared<DiscoInfo>(); - features->addFeature(DiscoInfo::SecurityLabelsCatalogFeature); - controller_->setAvailableServerFeatures(features); - IQ::ref iq = iqChannel_->iqs_[iqChannel_->iqs_.size() - 1]; - SecurityLabelsCatalog::ref labelPayload = boost::make_shared<SecurityLabelsCatalog>(); - labelPayload->addItem(labelItem); - IQ::ref result = IQ::createResult(self_, iq->getID(), labelPayload); - iqChannel_->onIQReceived(result); - std::string messageBody("agamemnon"); - window_->onSendMessageRequest(messageBody, false); - boost::shared_ptr<Stanza> rawStanza = stanzaChannel_->sentStanzas[stanzaChannel_->sentStanzas.size() - 1]; - Message::ref message = boost::dynamic_pointer_cast<Message>(rawStanza); - CPPUNIT_ASSERT_EQUAL(iq->getTo(), result->getFrom()); - CPPUNIT_ASSERT(window_->labelsEnabled_); - CPPUNIT_ASSERT(stanzaChannel_->isAvailable()); /* Otherwise will prevent sends. */ - CPPUNIT_ASSERT(message); - CPPUNIT_ASSERT_EQUAL(messageBody, message->getBody()); - CPPUNIT_ASSERT_EQUAL(label, message->getPayload<SecurityLabel>()); - window_->label_ = labelItem2; - window_->onSendMessageRequest(messageBody, true); - rawStanza = stanzaChannel_->sentStanzas[stanzaChannel_->sentStanzas.size() - 1]; - message = boost::dynamic_pointer_cast<Message>(rawStanza); - CPPUNIT_ASSERT_EQUAL(messageBody, message->getBody()); - CPPUNIT_ASSERT_EQUAL(label, message->getPayload<SecurityLabel>()); - } - - void checkEqual(const std::vector<NickJoinPart>& expected, const std::vector<NickJoinPart>& actual) { - CPPUNIT_ASSERT_EQUAL(expected.size(), actual.size()); - for (size_t i = 0; i < expected.size(); i++) { - CPPUNIT_ASSERT_EQUAL(expected[i].nick, actual[i].nick); - CPPUNIT_ASSERT_EQUAL(expected[i].type, actual[i].type); - } - } - - void testAppendToJoinParts() { - std::vector<NickJoinPart> list; - std::vector<NickJoinPart> gold; - MUCController::appendToJoinParts(list, NickJoinPart("Kev", Join)); - gold.push_back(NickJoinPart("Kev", Join)); - checkEqual(gold, list); - MUCController::appendToJoinParts(list, NickJoinPart("Remko", Join)); - gold.push_back(NickJoinPart("Remko", Join)); - checkEqual(gold, list); - MUCController::appendToJoinParts(list, NickJoinPart("Bert", Join)); - gold.push_back(NickJoinPart("Bert", Join)); - checkEqual(gold, list); - MUCController::appendToJoinParts(list, NickJoinPart("Bert", Part)); - gold[2].type = JoinThenPart; - checkEqual(gold, list); - MUCController::appendToJoinParts(list, NickJoinPart("Kev", Part)); - gold[0].type = JoinThenPart; - checkEqual(gold, list); - MUCController::appendToJoinParts(list, NickJoinPart("Remko", Part)); - gold[1].type = JoinThenPart; - checkEqual(gold, list); - MUCController::appendToJoinParts(list, NickJoinPart("Ernie", Part)); - gold.push_back(NickJoinPart("Ernie", Part)); - checkEqual(gold, list); - MUCController::appendToJoinParts(list, NickJoinPart("Ernie", Join)); - gold[3].type = PartThenJoin; - checkEqual(gold, list); - MUCController::appendToJoinParts(list, NickJoinPart("Kev", Join)); - gold[0].type = Join; - checkEqual(gold, list); - MUCController::appendToJoinParts(list, NickJoinPart("Ernie", Part)); - gold[3].type = Part; - checkEqual(gold, list); - - } - - void testJoinPartStringContructionSimple() { - std::vector<NickJoinPart> list; - list.push_back(NickJoinPart("Kev", Join)); - CPPUNIT_ASSERT_EQUAL(std::string("Kev has entered the room"), MUCController::generateJoinPartString(list, false)); - list.push_back(NickJoinPart("Remko", Part)); - CPPUNIT_ASSERT_EQUAL(std::string("Kev has entered the room and Remko has left the room"), MUCController::generateJoinPartString(list, false)); - list.push_back(NickJoinPart("Bert", Join)); - CPPUNIT_ASSERT_EQUAL(std::string("Kev and Bert have entered the room and Remko has left the room"), MUCController::generateJoinPartString(list, false)); - list.push_back(NickJoinPart("Ernie", Join)); - CPPUNIT_ASSERT_EQUAL(std::string("Kev, Bert and Ernie have entered the room and Remko has left the room"), MUCController::generateJoinPartString(list, false)); - } - - void testJoinPartStringContructionMixed() { - std::vector<NickJoinPart> list; - list.push_back(NickJoinPart("Kev", JoinThenPart)); - CPPUNIT_ASSERT_EQUAL(std::string("Kev has entered then left the room"), MUCController::generateJoinPartString(list, false)); - list.push_back(NickJoinPart("Remko", Part)); - CPPUNIT_ASSERT_EQUAL(std::string("Remko has left the room and Kev has entered then left the room"), MUCController::generateJoinPartString(list, false)); - list.push_back(NickJoinPart("Bert", PartThenJoin)); - CPPUNIT_ASSERT_EQUAL(std::string("Remko has left the room, Kev has entered then left the room and Bert has left then returned to the room"), MUCController::generateJoinPartString(list, false)); - list.push_back(NickJoinPart("Ernie", JoinThenPart)); - CPPUNIT_ASSERT_EQUAL(std::string("Remko has left the room, Kev and Ernie have entered then left the room and Bert has left then returned to the room"), MUCController::generateJoinPartString(list, false)); - } - -private: - JID self_; - JID mucJID_; - MUC::ref muc_; - std::string nick_; - DummyStanzaChannel* stanzaChannel_; - DummyIQChannel* iqChannel_; - IQRouter* iqRouter_; - EventController* eventController_; - ChatWindowFactory* chatWindowFactory_; - UserSearchWindowFactory* userSearchWindowFactory_; - MUCController* controller_; -// NickResolver* nickResolver_; - PresenceOracle* presenceOracle_; - AvatarManager* avatarManager_; - StanzaChannelPresenceSender* presenceSender_; - DirectedPresenceSender* directedPresenceSender_; - MockRepository* mocks_; - UIEventStream* uiEventStream_; - MockChatWindow* window_; - MUCRegistry* mucRegistry_; - DummyEntityCapsProvider* entityCapsProvider_; - DummySettingsProvider* settings_; - HighlightManager* highlightManager_; - ChatMessageParser* chatMessageParser_; - boost::shared_ptr<CryptoProvider> crypto_; - VCardManager* vcardManager_; - VCardMemoryStorage* vcardStorage_; +class MUCControllerTest : public ::testing::Test { + + protected: + void SetUp() { + crypto_ = std::shared_ptr<CryptoProvider>(PlatformCryptoProvider::create()); + self_ = JID("girl@wonderland.lit/rabbithole"); + nick_ = "aLiCe"; + mucJID_ = JID("teaparty@rooms.wonderland.lit"); + mocks_ = new MockRepository(); + stanzaChannel_ = new DummyStanzaChannel(); + iqChannel_ = new DummyIQChannel(); + iqRouter_ = new IQRouter(iqChannel_); + eventController_ = new EventController(); + chatWindowFactory_ = mocks_->InterfaceMock<ChatWindowFactory>(); + userSearchWindowFactory_ = mocks_->InterfaceMock<UserSearchWindowFactory>(); + xmppRoster_ = new XMPPRosterImpl(); + presenceOracle_ = new PresenceOracle(stanzaChannel_, xmppRoster_); + presenceSender_ = new StanzaChannelPresenceSender(stanzaChannel_); + directedPresenceSender_ = new DirectedPresenceSender(presenceSender_); + uiEventStream_ = new UIEventStream(); + avatarManager_ = new NullAvatarManager(); + TimerFactory* timerFactory = nullptr; + window_ = new MockChatWindow(); + mucRegistry_ = new MUCRegistry(); + entityCapsProvider_ = new DummyEntityCapsProvider(); + settings_ = new DummySettingsProvider(); + highlightManager_ = new HighlightManager(settings_); + highlightManager_->resetToDefaultConfiguration(); + muc_ = std::make_shared<MockMUC>(mucJID_); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(muc_->getJID(), uiEventStream_).Return(window_); + chatMessageParser_ = std::make_shared<ChatMessageParser>(std::map<std::string, std::string>(), highlightManager_->getConfiguration(), ChatMessageParser::Mode::GroupChat); + vcardStorage_ = new VCardMemoryStorage(crypto_.get()); + vcardManager_ = new VCardManager(self_, iqRouter_, vcardStorage_); + nickResolver_ = new NickResolver(self_, xmppRoster_, vcardManager_, mucRegistry_); + clientBlockListManager_ = new ClientBlockListManager(iqRouter_); + mucBookmarkManager_ = new MUCBookmarkManager(iqRouter_); + chattables_ = std::make_unique<Chattables>(); + controller_ = new MUCController (self_, muc_, boost::optional<std::string>(), nick_, stanzaChannel_, iqRouter_, chatWindowFactory_, nickResolver_, presenceOracle_, avatarManager_, uiEventStream_, false, timerFactory, eventController_, entityCapsProvider_, nullptr, nullptr, mucRegistry_, highlightManager_, clientBlockListManager_, chatMessageParser_, false, nullptr, vcardManager_, mucBookmarkManager_, settings_, *chattables_); + } + + void TearDown() { + delete controller_; + delete mucBookmarkManager_; + delete clientBlockListManager_; + delete nickResolver_; + delete vcardManager_; + delete vcardStorage_; + delete highlightManager_; + delete settings_; + delete entityCapsProvider_; + delete eventController_; + delete presenceOracle_; + delete xmppRoster_; + delete mocks_; + delete uiEventStream_; + delete stanzaChannel_; + delete presenceSender_; + delete directedPresenceSender_; + delete iqRouter_; + delete iqChannel_; + delete mucRegistry_; + delete avatarManager_; + } + + void finishJoin() { + Presence::ref presence(new Presence()); + presence->setFrom(JID(muc_->getJID().toString() + "/" + nick_)); + MUCUserPayload::ref status(new MUCUserPayload()); + MUCUserPayload::StatusCode code; + code.code = 110; + status->addStatusCode(code); + presence->addPayload(status); + stanzaChannel_->onPresenceReceived(presence); + } + + void joinCompleted() { + std::string messageBody("test message"); + window_->onSendMessageRequest(messageBody, false); + std::shared_ptr<Stanza> rawStanza = stanzaChannel_->sentStanzas[stanzaChannel_->sentStanzas.size() - 1]; + Message::ref message = std::dynamic_pointer_cast<Message>(rawStanza); + ASSERT_TRUE(stanzaChannel_->isAvailable()); /* Otherwise will prevent sends. */ + ASSERT_TRUE(message); + ASSERT_EQ(messageBody, message->getBody().get_value_or("")); + + { + Message::ref message = std::make_shared<Message>(); + message->setType(Message::Groupchat); + message->setTo(self_); + message->setFrom(mucJID_.withResource("SomeNickname")); + message->setID(iqChannel_->getNewIQID()); + message->setSubject("Initial"); + + controller_->handleIncomingMessage(std::make_shared<MessageEvent>(message)); + } + } + + void checkEqual(const std::vector<NickJoinPart>& expected, const std::vector<NickJoinPart>& actual) { + ASSERT_EQ(expected.size(), actual.size()); + for (size_t i = 0; i < expected.size(); i++) { + ASSERT_EQ(expected[i].nick, actual[i].nick); + ASSERT_EQ(expected[i].type, actual[i].type); + } + } + + JID jidFromOccupant(const MUCOccupant& occupant) { + return JID(mucJID_.toString()+"/"+occupant.getNick()); + } + + void testRoleAffiliationStatesVerify(const std::map<std::string, MUCOccupant> &occupants) { + /* verify that the roster is in sync */ + GroupRosterItem* group = window_->getRosterModel()->getRoot(); + for (auto rosterItem : group->getChildren()) { + GroupRosterItem* child = dynamic_cast<GroupRosterItem*>(rosterItem); + ASSERT_TRUE(child); + for (auto childItem : child->getChildren()) { + ContactRosterItem* item = dynamic_cast<ContactRosterItem*>(childItem); + ASSERT_TRUE(item); + std::map<std::string, MUCOccupant>::const_iterator occupant = occupants.find(item->getJID().getResource()); + ASSERT_TRUE(occupant != occupants.end()); + ASSERT_TRUE(item->getMUCRole() == occupant->second.getRole()); + ASSERT_TRUE(item->getMUCAffiliation() == occupant->second.getAffiliation()); + } + } + } + + void setMUCSecurityMarking(const std::string& markingValue, const std::string& markingForegroundColorValue, const std::string& markingBackgroundColorValue, const bool includeFormTypeField = true) { + auto form = std::make_shared<Form>(Form::Type::ResultType); + + if (includeFormTypeField) { + std::shared_ptr<FormField> formTypeField = std::make_shared<FormField>(FormField::Type::HiddenType, "http://jabber.org/protocol/muc#roominfo"); + formTypeField->setName("FORM_TYPE"); + form->addField(formTypeField); + } + + auto markingField = std::make_shared<FormField>(FormField::Type::TextSingleType, markingValue); + auto markingForegroundColorField = std::make_shared<FormField>(FormField::Type::TextSingleType, markingForegroundColorValue); + auto markingBackgroundColorField = std::make_shared<FormField>(FormField::Type::TextSingleType, markingBackgroundColorValue); + + markingField->setName("x-isode#roominfo_marking"); + markingForegroundColorField->setName("x-isode#roominfo_marking_fg_color"); + markingBackgroundColorField->setName("x-isode#roominfo_marking_bg_color"); + + form->addField(markingField); + form->addField(markingForegroundColorField); + form->addField(markingBackgroundColorField); + + auto discoInfoRef = std::make_shared<DiscoInfo>(); + discoInfoRef->addExtension(form); + + auto infoResponse = IQ::createResult(self_, mucJID_, "test-id", discoInfoRef); + iqChannel_->onIQReceived(infoResponse); + } + + Message::ref createTestMessageWithoutSecurityLabel() { + auto message = std::make_shared<Message>(); + message->setType(Message::Type::Groupchat); + message->setID("test-id"); + message->setTo(self_); + message->setFrom(mucJID_.withResource("TestNickname")); + message->setBody("Do Not Read This Message"); + return message; + } + + JID self_; + JID mucJID_; + MockMUC::ref muc_; + std::string nick_; + DummyStanzaChannel* stanzaChannel_; + DummyIQChannel* iqChannel_; + IQRouter* iqRouter_; + EventController* eventController_; + ChatWindowFactory* chatWindowFactory_; + UserSearchWindowFactory* userSearchWindowFactory_; + MUCController* controller_; + NickResolver* nickResolver_; + PresenceOracle* presenceOracle_; + AvatarManager* avatarManager_; + StanzaChannelPresenceSender* presenceSender_; + DirectedPresenceSender* directedPresenceSender_; + MockRepository* mocks_; + UIEventStream* uiEventStream_; + MockChatWindow* window_; + MUCRegistry* mucRegistry_; + DummyEntityCapsProvider* entityCapsProvider_; + DummySettingsProvider* settings_; + HighlightManager* highlightManager_; + std::shared_ptr<ChatMessageParser> chatMessageParser_; + std::shared_ptr<CryptoProvider> crypto_; + VCardManager* vcardManager_; + VCardMemoryStorage* vcardStorage_; + ClientBlockListManager* clientBlockListManager_; + MUCBookmarkManager* mucBookmarkManager_; + XMPPRoster* xmppRoster_; + std::unique_ptr<Chattables> chattables_; }; -CPPUNIT_TEST_SUITE_REGISTRATION(MUCControllerTest); +TEST_F(MUCControllerTest, testAddressedToSelf) { + finishJoin(); + Message::ref message(new Message()); + + message = Message::ref(new Message()); + message->setFrom(JID(muc_->getJID().toString() + "/otherperson")); + message->setBody("basic " + nick_ + " test."); + message->setType(Message::Groupchat); + controller_->handleIncomingMessage(MessageEvent::ref(new MessageEvent(message))); + ASSERT_EQ(1, eventController_->getEvents().size()); + + message = Message::ref(new Message()); + message->setFrom(JID(muc_->getJID().toString() + "/otherperson")); + message->setBody(nick_ + ": hi there"); + message->setType(Message::Groupchat); + controller_->handleIncomingMessage(MessageEvent::ref(new MessageEvent(message))); + ASSERT_EQ(2, eventController_->getEvents().size()); + + message->setFrom(JID(muc_->getJID().toString() + "/other")); + message->setBody("Hi there " + nick_); + message->setType(Message::Groupchat); + controller_->handleIncomingMessage(MessageEvent::ref(new MessageEvent(message))); + ASSERT_EQ(3, eventController_->getEvents().size()); + + message = Message::ref(new Message()); + message->setFrom(JID(muc_->getJID().toString() + "/other2")); + message->setBody("Hi " + boost::to_lower_copy(nick_) + "."); + message->setType(Message::Groupchat); + controller_->handleIncomingMessage(MessageEvent::ref(new MessageEvent(message))); + + // The last message is ignored because self-mention highlights are matched case + // sensitive against the nickname. + ASSERT_EQ(3, eventController_->getEvents().size()); + + message = Message::ref(new Message()); + message->setFrom(JID(muc_->getJID().toString() + "/other3")); + message->setBody("Hi bert."); + message->setType(Message::Groupchat); + controller_->handleIncomingMessage(MessageEvent::ref(new MessageEvent(message))); + ASSERT_EQ(3, eventController_->getEvents().size()); + + message = Message::ref(new Message()); + message->setFrom(JID(muc_->getJID().toString() + "/other2")); + message->setBody("Hi " + boost::to_lower_copy(nick_) + "ie."); + message->setType(Message::Groupchat); + controller_->handleIncomingMessage(MessageEvent::ref(new MessageEvent(message))); + ASSERT_EQ(3, eventController_->getEvents().size()); +} + +TEST_F(MUCControllerTest, testNotAddressedToSelf) { + finishJoin(); + Message::ref message(new Message()); + message->setFrom(JID(muc_->getJID().toString() + "/other3")); + message->setBody("Hi there Hatter"); + message->setType(Message::Groupchat); + controller_->handleIncomingMessage(MessageEvent::ref(new MessageEvent(message))); + ASSERT_EQ(0, eventController_->getEvents().size()); +} + +TEST_F(MUCControllerTest, testAddressedToSelfBySelf) { + finishJoin(); + Message::ref message(new Message()); + message->setFrom(JID(muc_->getJID().toString() + "/" + nick_)); + message->setBody("Hi there " + nick_); + message->setType(Message::Groupchat); + controller_->handleIncomingMessage(MessageEvent::ref(new MessageEvent(message))); + ASSERT_EQ(0, eventController_->getEvents().size()); +} + +TEST_F(MUCControllerTest, testMessageWithEmptyLabelItem) { + SecurityLabelsCatalog::Item label; + label.setSelector("Bob"); + window_->label_ = label; + std::shared_ptr<DiscoInfo> features = std::make_shared<DiscoInfo>(); + features->addFeature(DiscoInfo::SecurityLabelsCatalogFeature); + controller_->setAvailableServerFeatures(features); + IQ::ref iq = iqChannel_->iqs_[iqChannel_->iqs_.size() - 1]; + SecurityLabelsCatalog::ref labelPayload = std::make_shared<SecurityLabelsCatalog>(); + labelPayload->addItem(label); + IQ::ref result = IQ::createResult(self_, iq->getID(), labelPayload); + iqChannel_->onIQReceived(result); + std::string messageBody("agamemnon"); + window_->onSendMessageRequest(messageBody, false); + std::shared_ptr<Stanza> rawStanza = stanzaChannel_->sentStanzas[stanzaChannel_->sentStanzas.size() - 1]; + Message::ref message = std::dynamic_pointer_cast<Message>(rawStanza); + ASSERT_EQ(iq->getTo(), result->getFrom()); + ASSERT_TRUE(window_->labelsEnabled_); + ASSERT_TRUE(stanzaChannel_->isAvailable()); /* Otherwise will prevent sends. */ + ASSERT_TRUE(message); + ASSERT_EQ(messageBody, message->getBody().get()); + ASSERT_FALSE(message->getPayload<SecurityLabel>()); +} + +TEST_F(MUCControllerTest, testMessageWithLabelItem) { + std::shared_ptr<SecurityLabel> label = std::make_shared<SecurityLabel>(); + label->setLabel("a"); + SecurityLabelsCatalog::Item labelItem; + labelItem.setSelector("Bob"); + labelItem.setLabel(label); + window_->label_ = labelItem; + std::shared_ptr<DiscoInfo> features = std::make_shared<DiscoInfo>(); + features->addFeature(DiscoInfo::SecurityLabelsCatalogFeature); + controller_->setAvailableServerFeatures(features); + IQ::ref iq = iqChannel_->iqs_[iqChannel_->iqs_.size() - 1]; + SecurityLabelsCatalog::ref labelPayload = std::make_shared<SecurityLabelsCatalog>(); + labelPayload->addItem(labelItem); + IQ::ref result = IQ::createResult(self_, iq->getID(), labelPayload); + iqChannel_->onIQReceived(result); + std::string messageBody("agamemnon"); + window_->onSendMessageRequest(messageBody, false); + std::shared_ptr<Stanza> rawStanza = stanzaChannel_->sentStanzas[stanzaChannel_->sentStanzas.size() - 1]; + Message::ref message = std::dynamic_pointer_cast<Message>(rawStanza); + ASSERT_EQ(iq->getTo(), result->getFrom()); + ASSERT_TRUE(window_->labelsEnabled_); + ASSERT_TRUE(stanzaChannel_->isAvailable()); /* Otherwise will prevent sends. */ + ASSERT_TRUE(message); + ASSERT_EQ(messageBody, message->getBody().get()); + ASSERT_EQ(label, message->getPayload<SecurityLabel>()); +} + +TEST_F(MUCControllerTest, testCorrectMessageWithLabelItem) { + std::shared_ptr<SecurityLabel> label = std::make_shared<SecurityLabel>(); + label->setLabel("a"); + SecurityLabelsCatalog::Item labelItem; + labelItem.setSelector("Bob"); + labelItem.setLabel(label); + std::shared_ptr<SecurityLabel> label2 = std::make_shared<SecurityLabel>(); + label->setLabel("b"); + SecurityLabelsCatalog::Item labelItem2; + labelItem2.setSelector("Charlie"); + labelItem2.setLabel(label2); + window_->label_ = labelItem; + std::shared_ptr<DiscoInfo> features = std::make_shared<DiscoInfo>(); + features->addFeature(DiscoInfo::SecurityLabelsCatalogFeature); + controller_->setAvailableServerFeatures(features); + IQ::ref iq = iqChannel_->iqs_[iqChannel_->iqs_.size() - 1]; + SecurityLabelsCatalog::ref labelPayload = std::make_shared<SecurityLabelsCatalog>(); + labelPayload->addItem(labelItem); + IQ::ref result = IQ::createResult(self_, iq->getID(), labelPayload); + iqChannel_->onIQReceived(result); + std::string messageBody("agamemnon"); + window_->onSendMessageRequest(messageBody, false); + std::shared_ptr<Stanza> rawStanza = stanzaChannel_->sentStanzas[stanzaChannel_->sentStanzas.size() - 1]; + Message::ref message = std::dynamic_pointer_cast<Message>(rawStanza); + ASSERT_EQ(iq->getTo(), result->getFrom()); + ASSERT_TRUE(window_->labelsEnabled_); + ASSERT_TRUE(stanzaChannel_->isAvailable()); /* Otherwise will prevent sends. */ + ASSERT_TRUE(message); + ASSERT_EQ(messageBody, message->getBody().get()); + ASSERT_EQ(label, message->getPayload<SecurityLabel>()); + window_->label_ = labelItem2; + window_->onSendMessageRequest(messageBody, true); + rawStanza = stanzaChannel_->sentStanzas[stanzaChannel_->sentStanzas.size() - 1]; + message = std::dynamic_pointer_cast<Message>(rawStanza); + ASSERT_EQ(messageBody, message->getBody().get()); + ASSERT_EQ(label, message->getPayload<SecurityLabel>()); +} + +TEST_F(MUCControllerTest, testAppendToJoinParts) { + std::vector<NickJoinPart> list; + std::vector<NickJoinPart> gold; + MUCController::appendToJoinParts(list, NickJoinPart("Kev", Join)); + gold.push_back(NickJoinPart("Kev", Join)); + checkEqual(gold, list); + MUCController::appendToJoinParts(list, NickJoinPart("Remko", Join)); + gold.push_back(NickJoinPart("Remko", Join)); + checkEqual(gold, list); + MUCController::appendToJoinParts(list, NickJoinPart("Bert", Join)); + gold.push_back(NickJoinPart("Bert", Join)); + checkEqual(gold, list); + MUCController::appendToJoinParts(list, NickJoinPart("Bert", Part)); + gold[2].type = JoinThenPart; + checkEqual(gold, list); + MUCController::appendToJoinParts(list, NickJoinPart("Kev", Part)); + gold[0].type = JoinThenPart; + checkEqual(gold, list); + MUCController::appendToJoinParts(list, NickJoinPart("Remko", Part)); + gold[1].type = JoinThenPart; + checkEqual(gold, list); + MUCController::appendToJoinParts(list, NickJoinPart("Ernie", Part)); + gold.push_back(NickJoinPart("Ernie", Part)); + checkEqual(gold, list); + MUCController::appendToJoinParts(list, NickJoinPart("Ernie", Join)); + gold[3].type = PartThenJoin; + checkEqual(gold, list); + MUCController::appendToJoinParts(list, NickJoinPart("Kev", Join)); + gold[0].type = Join; + checkEqual(gold, list); + MUCController::appendToJoinParts(list, NickJoinPart("Ernie", Part)); + gold[3].type = Part; + checkEqual(gold, list); + +} + +TEST_F(MUCControllerTest, testJoinPartStringContructionSimple) { + std::vector<NickJoinPart> list; + list.push_back(NickJoinPart("Kev", Join)); + ASSERT_EQ(std::string("Kev has entered the room"), MUCController::generateJoinPartString(list, false)); + list.push_back(NickJoinPart("Remko", Part)); + ASSERT_EQ(std::string("Kev has entered the room and Remko has left the room"), MUCController::generateJoinPartString(list, false)); + list.push_back(NickJoinPart("Bert", Join)); + ASSERT_EQ(std::string("Kev and Bert have entered the room and Remko has left the room"), MUCController::generateJoinPartString(list, false)); + list.push_back(NickJoinPart("Ernie", Join)); + ASSERT_EQ(std::string("Kev, Bert and Ernie have entered the room and Remko has left the room"), MUCController::generateJoinPartString(list, false)); +} + +TEST_F(MUCControllerTest, testJoinPartStringContructionMixed) { + std::vector<NickJoinPart> list; + list.push_back(NickJoinPart("Kev", JoinThenPart)); + ASSERT_EQ(std::string("Kev has entered then left the room"), MUCController::generateJoinPartString(list, false)); + list.push_back(NickJoinPart("Remko", Part)); + ASSERT_EQ(std::string("Remko has left the room and Kev has entered then left the room"), MUCController::generateJoinPartString(list, false)); + list.push_back(NickJoinPart("Bert", PartThenJoin)); + ASSERT_EQ(std::string("Remko has left the room, Kev has entered then left the room and Bert has left then returned to the room"), MUCController::generateJoinPartString(list, false)); + list.push_back(NickJoinPart("Ernie", JoinThenPart)); + ASSERT_EQ(std::string("Remko has left the room, Kev and Ernie have entered then left the room and Bert has left then returned to the room"), MUCController::generateJoinPartString(list, false)); +} + +TEST_F(MUCControllerTest, testRoleAffiliationStates) { + + typedef std::map<std::string, MUCOccupant> occupant_map; + occupant_map occupants; + occupants.insert(occupant_map::value_type("Kev", MUCOccupant("Kev", MUCOccupant::Participant, MUCOccupant::Owner))); + occupants.insert(occupant_map::value_type("Remko", MUCOccupant("Remko", MUCOccupant::Participant, MUCOccupant::Owner))); + occupants.insert(occupant_map::value_type("Bert", MUCOccupant("Bert", MUCOccupant::Participant, MUCOccupant::Owner))); + occupants.insert(occupant_map::value_type("Ernie", MUCOccupant("Ernie", MUCOccupant::Participant, MUCOccupant::Owner))); + + /* populate the MUC with fake users */ + for (auto&& occupant : occupants) { + muc_->insertOccupant(occupant.second); + } + + std::vector<MUCOccupant> alterations; + alterations.push_back(MUCOccupant("Kev", MUCOccupant::Visitor, MUCOccupant::Admin)); + alterations.push_back(MUCOccupant("Remko", MUCOccupant::Moderator, MUCOccupant::Member)); + alterations.push_back(MUCOccupant("Bert", MUCOccupant::Visitor, MUCOccupant::Outcast)); + alterations.push_back(MUCOccupant("Ernie", MUCOccupant::NoRole, MUCOccupant::Member)); + alterations.push_back(MUCOccupant("Bert", MUCOccupant::Moderator, MUCOccupant::Owner)); + alterations.push_back(MUCOccupant("Kev", MUCOccupant::Participant, MUCOccupant::Outcast)); + alterations.push_back(MUCOccupant("Bert", MUCOccupant::Visitor, MUCOccupant::NoAffiliation)); + alterations.push_back(MUCOccupant("Remko", MUCOccupant::NoRole, MUCOccupant::NoAffiliation)); + alterations.push_back(MUCOccupant("Ernie", MUCOccupant::Visitor, MUCOccupant::Outcast)); + + for (const auto& alteration : alterations) { + /* perform an alteration to a user's role and affiliation */ + occupant_map::iterator occupant = occupants.find(alteration.getNick()); + ASSERT_TRUE(occupant != occupants.end()); + const JID jid = jidFromOccupant(occupant->second); + /* change the affiliation, leave the role in place */ + muc_->changeAffiliation(jid, alteration.getAffiliation()); + occupant->second = MUCOccupant(occupant->first, occupant->second.getRole(), alteration.getAffiliation()); + testRoleAffiliationStatesVerify(occupants); + /* change the role, leave the affiliation in place */ + muc_->changeOccupantRole(jid, alteration.getRole()); + occupant->second = MUCOccupant(occupant->first, alteration.getRole(), occupant->second.getAffiliation()); + testRoleAffiliationStatesVerify(occupants); + } +} + +TEST_F(MUCControllerTest, testSubjectChangeCorrect) { + joinCompleted(); + + { + Message::ref message = std::make_shared<Message>(); + message->setType(Message::Groupchat); + message->setTo(self_); + message->setFrom(mucJID_.withResource("SomeNickname")); + message->setID("3FB99C56-7C92-4755-91B0-9C0098BC7AE0"); + message->setSubject("New Room Subject"); + + controller_->handleIncomingMessage(std::make_shared<MessageEvent>(message)); + ASSERT_EQ(std::string("The room subject is now: New Room Subject"), std::dynamic_pointer_cast<ChatWindow::ChatTextMessagePart>(window_->lastAddedSystemMessage_.getParts()[0])->text); + } +} + +/* + * Test that message stanzas with subject element and non-empty body element do not cause a subject change. + */ +TEST_F(MUCControllerTest, testSubjectChangeIncorrectA) { + joinCompleted(); + + { + Message::ref message = std::make_shared<Message>(); + message->setType(Message::Groupchat); + message->setTo(self_); + message->setFrom(mucJID_.withResource("SomeNickname")); + message->setID(iqChannel_->getNewIQID()); + message->setSubject("New Room Subject"); + message->setBody("Some body text that prevents this stanza from being a subject change."); + + controller_->handleIncomingMessage(std::make_shared<MessageEvent>(message)); + ASSERT_EQ(std::string("Trying to enter room teaparty@rooms.wonderland.lit"), std::dynamic_pointer_cast<ChatWindow::ChatTextMessagePart>(window_->lastAddedSystemMessage_.getParts()[0])->text); + } +} + +/* + * Test that message stanzas with subject element and thread element do not cause a subject change. + */ +TEST_F(MUCControllerTest, testSubjectChangeIncorrectB) { + joinCompleted(); + + { + Message::ref message = std::make_shared<Message>(); + message->setType(Message::Groupchat); + message->setTo(self_); + message->setFrom(mucJID_.withResource("SomeNickname")); + message->setID(iqChannel_->getNewIQID()); + message->setSubject("New Room Subject"); + message->addPayload(std::make_shared<Thread>("Thread that prevents the subject change.")); + + controller_->handleIncomingMessage(std::make_shared<MessageEvent>(message)); + ASSERT_EQ(std::string("Trying to enter room teaparty@rooms.wonderland.lit"), std::dynamic_pointer_cast<ChatWindow::ChatTextMessagePart>(window_->lastAddedSystemMessage_.getParts()[0])->text); + } +} + +/* + * Test that message stanzas with subject element and empty body element do not cause a subject change. + */ +TEST_F(MUCControllerTest, testSubjectChangeIncorrectC) { + joinCompleted(); + + { + Message::ref message = std::make_shared<Message>(); + message->setType(Message::Groupchat); + message->setTo(self_); + message->setFrom(mucJID_.withResource("SomeNickname")); + message->setID(iqChannel_->getNewIQID()); + message->setSubject("New Room Subject"); + message->setBody(""); + + controller_->handleIncomingMessage(std::make_shared<MessageEvent>(message)); + ASSERT_EQ(std::string("Trying to enter room teaparty@rooms.wonderland.lit"), std::dynamic_pointer_cast<ChatWindow::ChatTextMessagePart>(window_->lastAddedSystemMessage_.getParts()[0])->text); + } +} + +TEST_F(MUCControllerTest, testHandleOccupantNicknameChanged) { + const auto occupantCount = [&](const std::string & nick) { + auto roster = window_->getRosterModel(); + const auto currentOccupantsJIDs = roster->getJIDs(); + int count = 0; + for (auto & p : currentOccupantsJIDs) { + if (p.getResource() == nick) { + ++count; + } + } + return count; + }; + + muc_->insertOccupant(MUCOccupant("TestUserOne", MUCOccupant::Participant, MUCOccupant::Owner)); + muc_->insertOccupant(MUCOccupant("TestUserTwo", MUCOccupant::Participant, MUCOccupant::Owner)); + muc_->insertOccupant(MUCOccupant("TestUserThree", MUCOccupant::Participant, MUCOccupant::Owner)); + + muc_->onOccupantNicknameChanged("TestUserOne", "TestUserTwo"); + + ASSERT_EQ(0, occupantCount("TestUserOne")); + ASSERT_EQ(1, occupantCount("TestUserTwo")); + ASSERT_EQ(1, occupantCount("TestUserThree")); +} + +TEST_F(MUCControllerTest, testHandleOccupantNicknameChangedRoster) { + const auto occupantCount = [&](const std::string & nick) { + auto roster = window_->getRosterModel(); + const auto participants = roster->getGroup("Participants"); + const auto displayedParticipants = participants->getDisplayedChildren(); + int count = 0; + for (auto & p : displayedParticipants) { + if (p->getDisplayName() == nick) { + ++count; + } + } + return count; + }; + + muc_->insertOccupant(MUCOccupant("TestUserOne", MUCOccupant::Participant, MUCOccupant::Owner)); + muc_->insertOccupant(MUCOccupant("TestUserTwo", MUCOccupant::Participant, MUCOccupant::Owner)); + muc_->insertOccupant(MUCOccupant("TestUserThree", MUCOccupant::Participant, MUCOccupant::Owner)); + ASSERT_EQ(1, occupantCount("TestUserOne")); + ASSERT_EQ(1, occupantCount("TestUserTwo")); + ASSERT_EQ(1, occupantCount("TestUserThree")); + + muc_->onOccupantNicknameChanged("TestUserOne", "TestUserTwo"); + + ASSERT_EQ(0, occupantCount("TestUserOne")); + ASSERT_EQ(1, occupantCount("TestUserTwo")); + ASSERT_EQ(1, occupantCount("TestUserThree")); +} + +TEST_F(MUCControllerTest, testHandleChangeSubjectRequest) { + std::string testStr("New Subject"); + ASSERT_EQ(std::string(""), muc_->newSubjectSet_); + window_->onChangeSubjectRequest(testStr); + ASSERT_EQ(testStr, muc_->newSubjectSet_); +} + +TEST_F(MUCControllerTest, testNonImpromptuMUCWindowTitle) { + ASSERT_EQ(muc_->getJID().getNode(), window_->name_); +} + +TEST_F(MUCControllerTest, testSecurityMarkingRequestCompleteMarking) { + setMUCSecurityMarking("Test|Highest Possible Security", "Black", "Red", true); + + ASSERT_EQ(std::string("Test|Highest Possible Security"), window_->markingValue_); + ASSERT_EQ(std::string("Black"), window_->markingForegroundColorValue_); + ASSERT_EQ(std::string("Red"), window_->markingBackgroundColorValue_); +} + +TEST_F(MUCControllerTest, testSecurityMarkingRequestCompleteMarkingWithExtraForm) { + auto formTypeField = std::make_shared<FormField>(FormField::Type::HiddenType, "http://jabber.org/protocol/muc#roominfo"); + auto markingField = std::make_shared<FormField>(FormField::Type::TextSingleType, "Test|Highest Possible Security"); + auto markingForegroundColorField = std::make_shared<FormField>(FormField::Type::TextSingleType, "Black"); + auto markingBackgroundColorField = std::make_shared<FormField>(FormField::Type::TextSingleType, "Red"); + formTypeField->setName("FORM_TYPE"); + markingField->setName("x-isode#roominfo_marking"); + markingForegroundColorField->setName("x-isode#roominfo_marking_fg_color"); + markingBackgroundColorField->setName("x-isode#roominfo_marking_bg_color"); + + auto extraForm = std::make_shared<Form>(Form::Type::ResultType); + auto form = std::make_shared<Form>(Form::Type::ResultType); + form->addField(formTypeField); + form->addField(markingField); + form->addField(markingForegroundColorField); + form->addField(markingBackgroundColorField); + + auto discoInfoRef = std::make_shared<DiscoInfo>(); + discoInfoRef->addExtension(extraForm); + discoInfoRef->addExtension(form); + + auto infoResponse = IQ::createResult(self_, mucJID_, "test-id", discoInfoRef); + iqChannel_->onIQReceived(infoResponse); + ASSERT_EQ(std::string("Test|Highest Possible Security"), window_->markingValue_); + ASSERT_EQ(std::string("Black"), window_->markingForegroundColorValue_); + ASSERT_EQ(std::string("Red"), window_->markingBackgroundColorValue_); +} + +TEST_F(MUCControllerTest, testSecurityMarkingRequestNoColorsInMarking) { + auto formTypeField = std::make_shared<FormField>(FormField::Type::HiddenType, "http://jabber.org/protocol/muc#roominfo"); + auto markingField = std::make_shared<FormField>(FormField::Type::TextSingleType, "Test|Highest Possible Security"); + auto markingForegroundColorField = std::make_shared<FormField>(FormField::Type::TextSingleType, ""); + auto markingBackgroundColorField = std::make_shared<FormField>(FormField::Type::TextSingleType, ""); + formTypeField->setName("FORM_TYPE"); + markingField->setName("x-isode#roominfo_marking"); + markingForegroundColorField->setName("x-isode#roominfo_marking_fg_color"); + markingBackgroundColorField->setName("x-isode#roominfo_marking_bg_color"); + + auto form = std::make_shared<Form>(Form::Type::ResultType); + form->addField(formTypeField); + form->addField(markingField); + form->addField(markingForegroundColorField); + form->addField(markingBackgroundColorField); + + auto discoInfoRef = std::make_shared<DiscoInfo>(); + discoInfoRef->addExtension(form); + + auto infoResponse = IQ::createResult(self_, mucJID_, "test-id", discoInfoRef); + iqChannel_->onIQReceived(infoResponse); + ASSERT_EQ(std::string("Test|Highest Possible Security"), window_->markingValue_); + ASSERT_EQ(std::string("Black"), window_->markingForegroundColorValue_); + ASSERT_EQ(std::string("White"), window_->markingBackgroundColorValue_); +} + +TEST_F(MUCControllerTest, testSecurityMarkingRequestEmptyMarking) { + setMUCSecurityMarking("", "", "", true); + + ASSERT_EQ(std::string(""), window_->markingValue_); + ASSERT_EQ(std::string(""), window_->markingForegroundColorValue_); + ASSERT_EQ(std::string(""), window_->markingBackgroundColorValue_); +} + +TEST_F(MUCControllerTest, testSecurityMarkingRequestWithMarkingNoFormType) { + setMUCSecurityMarking("Test|Highest Possible Security", "Black", "Red", false); + + ASSERT_EQ(std::string(""), window_->markingValue_); + ASSERT_EQ(std::string(""), window_->markingForegroundColorValue_); + ASSERT_EQ(std::string(""), window_->markingBackgroundColorValue_); +} + +TEST_F(MUCControllerTest, testSecurityMarkingRequestNoMarking) { + auto form = std::make_shared<Form>(Form::Type::ResultType); + + auto discoInfoRef = std::make_shared<DiscoInfo>(); + discoInfoRef->addExtension(form); + + auto infoResponse = IQ::createResult(self_, mucJID_, "test-id", discoInfoRef); + iqChannel_->onIQReceived(infoResponse); + ASSERT_EQ(std::string(""), window_->markingValue_); + ASSERT_EQ(std::string(""), window_->markingForegroundColorValue_); + ASSERT_EQ(std::string(""), window_->markingBackgroundColorValue_); +} + +TEST_F(MUCControllerTest, testSecurityMarkingRequestNoForm) { + auto discoInfoRef = std::make_shared<DiscoInfo>(); + + auto infoResponse = IQ::createResult( self_, mucJID_, "test-id", discoInfoRef); + iqChannel_->onIQReceived(infoResponse); + ASSERT_EQ(std::string(""), window_->markingValue_); + ASSERT_EQ(std::string(""), window_->markingForegroundColorValue_); + ASSERT_EQ(std::string(""), window_->markingBackgroundColorValue_); +} + +TEST_F(MUCControllerTest, testSecurityMarkingRequestError) { + auto errorPayload = std::make_shared<ErrorPayload>(ErrorPayload::Condition::NotAuthorized, ErrorPayload::Type::Auth); + + auto infoResponse = IQ::createResult( self_, mucJID_, "test-id", errorPayload); + iqChannel_->onIQReceived(infoResponse); + ASSERT_EQ(std::string(""), window_->markingValue_); + ASSERT_EQ(std::string(""), window_->markingForegroundColorValue_); + ASSERT_EQ(std::string(""), window_->markingBackgroundColorValue_); +} + +TEST_F(MUCControllerTest, testSecurityMarkingAddedToMessage_Elision_NoRoomMarkingA) { + settings_->storeSetting(SettingConstants::MUC_MARKING_ELISION, true); + setMUCSecurityMarking("", "Black", "Red"); + + auto messageLabel = std::make_shared<SecurityLabel>(); + messageLabel->setDisplayMarking("Test|Highest Possible Security"); + + auto sentMessage = createTestMessageWithoutSecurityLabel(); + sentMessage->addPayload(messageLabel); + + auto sentMessageEvent = std::make_shared<MessageEvent>(sentMessage); + controller_->handleIncomingMessage(sentMessageEvent); + + auto storedSecurityLabel = window_->lastAddedMessageSecurityLabel_; + + ASSERT_EQ(false, storedSecurityLabel == nullptr); + // This is the potentially altered security label that is displayed on the screen + ASSERT_EQ(std::string("Test|Highest Possible Security"), storedSecurityLabel->getDisplayMarking()); +} + +TEST_F(MUCControllerTest, testSecurityMarkingAddedToMessage_Elision_NoRoomMarkingB) { + settings_->storeSetting(SettingConstants::MUC_MARKING_ELISION, true); + setMUCSecurityMarking("", "Black", "Red"); + + auto messageLabel = std::make_shared<SecurityLabel>(); + messageLabel->setDisplayMarking(""); + + auto sentMessage = createTestMessageWithoutSecurityLabel(); + sentMessage->addPayload(messageLabel); + + auto sentMessageEvent = std::make_shared<MessageEvent>(sentMessage); + controller_->handleIncomingMessage(sentMessageEvent); + + auto storedSecurityLabel = window_->lastAddedMessageSecurityLabel_; + + ASSERT_EQ(false, storedSecurityLabel == nullptr); + // This is the potentially altered security label that is displayed on the screen + ASSERT_EQ(std::string(""), storedSecurityLabel->getDisplayMarking()); +} + +TEST_F(MUCControllerTest, testSecurityMarkingAddedToMessage_Elision_WithRoomMarkingA) { + settings_->storeSetting(SettingConstants::MUC_MARKING_ELISION, true); + setMUCSecurityMarking("Test|Highest Possible Security", "Black", "Red"); + + auto messageLabel = std::make_shared<SecurityLabel>(); + messageLabel->setDisplayMarking("Test|Highest Possible Security"); + + auto sentMessage = createTestMessageWithoutSecurityLabel(); + sentMessage->addPayload(messageLabel); + + // Test the first message matching MUC marking. This message SHOULD have a marking + + auto sentMessageEvent1 = std::make_shared<MessageEvent>(sentMessage); + controller_->handleIncomingMessage(sentMessageEvent1); + + auto storedSecurityLabel1 = window_->lastAddedMessageSecurityLabel_; + + ASSERT_EQ(false, storedSecurityLabel1 == nullptr); + // This is the potentially altered security label that is displayed on the screen + ASSERT_EQ(std::string("Test|Highest Possible Security"), storedSecurityLabel1->getDisplayMarking()); + + // Test a consecutive message matching MUC marking. This message SHOULD NOT have a marking + + auto sentMessageEvent2 = std::make_shared<MessageEvent>(sentMessage); + controller_->handleIncomingMessage(sentMessageEvent2); + + auto storedSecurityLabel2 = window_->lastAddedMessageSecurityLabel_; + + ASSERT_EQ(false, storedSecurityLabel2 == nullptr); + // This is the potentially altered security label that is displayed on the screen + ASSERT_EQ(std::string(""), storedSecurityLabel2->getDisplayMarking()); +} + +TEST_F(MUCControllerTest, testSecurityMarkingAddedToMessage_Elision_WithRoomMarkingB) { + settings_->storeSetting(SettingConstants::MUC_MARKING_ELISION, true); + setMUCSecurityMarking("Test|Lower Security Marking", "Black", "Red"); + + auto messageLabel = std::make_shared<SecurityLabel>(); + messageLabel->setDisplayMarking("Test|Highest Possible Security"); + + auto sentMessage = createTestMessageWithoutSecurityLabel(); + sentMessage->addPayload(messageLabel); + + auto sentMessageEvent = std::make_shared<MessageEvent>(sentMessage); + controller_->handleIncomingMessage(sentMessageEvent); + + auto storedSecurityLabel = window_->lastAddedMessageSecurityLabel_; + + ASSERT_EQ(false, storedSecurityLabel == nullptr); + // This is the potentially altered security label that is displayed on the screen + ASSERT_EQ(std::string("Test|Highest Possible Security"), storedSecurityLabel->getDisplayMarking()); +} + +TEST_F(MUCControllerTest, testSecurityMarkingAddedToMessage_Elision_WithRoomMarkingC) { + settings_->storeSetting(SettingConstants::MUC_MARKING_ELISION, true); + setMUCSecurityMarking("Test|Highest Possible Security", "Black", "Red"); + + auto messageLabel = std::make_shared<SecurityLabel>(); + messageLabel->setDisplayMarking(""); + + auto sentMessage = createTestMessageWithoutSecurityLabel(); + sentMessage->addPayload(messageLabel); + + auto sentMessageEvent = std::make_shared<MessageEvent>(sentMessage); + controller_->handleIncomingMessage(sentMessageEvent); + + auto storedSecurityLabel = window_->lastAddedMessageSecurityLabel_; + + ASSERT_EQ(false, storedSecurityLabel == nullptr); + // This is the potentially altered security label that is displayed on the screen + ASSERT_EQ(std::string("Unmarked"), storedSecurityLabel->getDisplayMarking()); +} + +TEST_F(MUCControllerTest, testSecurityMarkingAddedToMessage_NoElision_NoRoomMarkingA) { + settings_->storeSetting(SettingConstants::MUC_MARKING_ELISION, false); + setMUCSecurityMarking("", "Black", "Red"); + + auto messageLabel = std::make_shared<SecurityLabel>(); + messageLabel->setDisplayMarking("Test|Highest Possible Security"); + + auto sentMessage = createTestMessageWithoutSecurityLabel(); + sentMessage->addPayload(messageLabel); + + auto sentMessageEvent = std::make_shared<MessageEvent>(sentMessage); + controller_->handleIncomingMessage(sentMessageEvent); + + auto storedSecurityLabel = window_->lastAddedMessageSecurityLabel_; + + ASSERT_EQ(false, storedSecurityLabel == nullptr); + // This is the potentially altered security label that is displayed on the screen + ASSERT_EQ(std::string("Test|Highest Possible Security"), storedSecurityLabel->getDisplayMarking()); +} + +TEST_F(MUCControllerTest, testSecurityMarkingAddedToMessage_NoElision_NoRoomMarkingB) { + settings_->storeSetting(SettingConstants::MUC_MARKING_ELISION, false); + setMUCSecurityMarking("", "Black", "Red"); + + auto messageLabel = std::make_shared<SecurityLabel>(); + messageLabel->setDisplayMarking(""); + + auto sentMessage = createTestMessageWithoutSecurityLabel(); + sentMessage->addPayload(messageLabel); + + auto sentMessageEvent = std::make_shared<MessageEvent>(sentMessage); + controller_->handleIncomingMessage(sentMessageEvent); + + auto storedSecurityLabel = window_->lastAddedMessageSecurityLabel_; + + ASSERT_EQ(false, storedSecurityLabel == nullptr); + // This is the potentially altered security label that is displayed on the screen + ASSERT_EQ(std::string(""), storedSecurityLabel->getDisplayMarking()); +} + +TEST_F(MUCControllerTest, testSecurityMarkingAddedToMessage_NoElision_WithRoomMarkingA) { + settings_->storeSetting(SettingConstants::MUC_MARKING_ELISION, false); + setMUCSecurityMarking("Test|Highest Possible Security", "Black", "Red"); + + auto messageLabel = std::make_shared<SecurityLabel>(); + messageLabel->setDisplayMarking("Test|Highest Possible Security"); + + auto sentMessage = createTestMessageWithoutSecurityLabel(); + sentMessage->addPayload(messageLabel); + + // Test the first message matching MUC marking. This message SHOULD have a marking + + auto sentMessageEvent1 = std::make_shared<MessageEvent>(sentMessage); + controller_->handleIncomingMessage(sentMessageEvent1); + + auto storedSecurityLabel1 = window_->lastAddedMessageSecurityLabel_; + + ASSERT_EQ(false, storedSecurityLabel1 == nullptr); + // This is the potentially altered security label that is displayed on the screen + ASSERT_EQ(std::string("Test|Highest Possible Security"), storedSecurityLabel1->getDisplayMarking()); + + // Test a consecutive message matching MUC marking. This message SHOULD ALSO have a marking + + auto sentMessageEvent2 = std::make_shared<MessageEvent>(sentMessage); + controller_->handleIncomingMessage(sentMessageEvent2); + + auto storedSecurityLabel2 = window_->lastAddedMessageSecurityLabel_; + + ASSERT_EQ(false, storedSecurityLabel2 == nullptr); + // This is the potentially altered security label that is displayed on the screen + ASSERT_EQ(std::string("Test|Highest Possible Security"), storedSecurityLabel2->getDisplayMarking()); +} + +TEST_F(MUCControllerTest, testSecurityMarkingAddedToMessage_NoElision_WithRoomMarkingB) { + settings_->storeSetting(SettingConstants::MUC_MARKING_ELISION, false); + setMUCSecurityMarking("", "Black", "Red"); + + auto messageLabel = std::make_shared<SecurityLabel>(); + messageLabel->setDisplayMarking(""); + + auto sentMessage = createTestMessageWithoutSecurityLabel(); + sentMessage->addPayload(messageLabel); + + auto sentMessageEvent = std::make_shared<MessageEvent>(sentMessage); + controller_->handleIncomingMessage(sentMessageEvent); + + auto storedSecurityLabel = window_->lastAddedMessageSecurityLabel_; + ASSERT_EQ(false, storedSecurityLabel == nullptr); + // This is the potentially altered security label that is displayed on the screen + ASSERT_EQ(std::string(""), storedSecurityLabel->getDisplayMarking()); +} diff --git a/Swift/Controllers/Chat/UnitTest/MockChatListWindow.h b/Swift/Controllers/Chat/UnitTest/MockChatListWindow.h index 5fa264d..1d980d3 100644 --- a/Swift/Controllers/Chat/UnitTest/MockChatListWindow.h +++ b/Swift/Controllers/Chat/UnitTest/MockChatListWindow.h @@ -1,27 +1,28 @@ /* - * Copyright (c) 2011 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2011-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include "Swift/Controllers/UIInterfaces/ChatListWindow.h" +#include <Swift/Controllers/UIInterfaces/ChatListWindow.h> namespace Swift { - class MockChatListWindow : public ChatListWindow { - public: - MockChatListWindow() {} - virtual ~MockChatListWindow() {} - void addMUCBookmark(const MUCBookmark& /*bookmark*/) {} - void removeMUCBookmark(const MUCBookmark& /*bookmark*/) {} - void addWhiteboardSession(const ChatListWindow::Chat& /*chat*/) {} - void removeWhiteboardSession(const JID& /*jid*/) {} - void setBookmarksEnabled(bool /*enabled*/) {} - void setRecents(const std::list<ChatListWindow::Chat>& /*recents*/) {} - void setUnreadCount(int /*unread*/) {} - void clearBookmarks() {} - }; + class MockChatListWindow : public ChatListWindow { + public: + MockChatListWindow() {} + virtual ~MockChatListWindow() {} + void addMUCBookmark(const MUCBookmark& /*bookmark*/) {} + void removeMUCBookmark(const MUCBookmark& /*bookmark*/) {} + void addWhiteboardSession(const ChatListWindow::Chat& /*chat*/) {} + void removeWhiteboardSession(const JID& /*jid*/) {} + void setBookmarksEnabled(bool /*enabled*/) {} + void setRecents(const std::list<ChatListWindow::Chat>& /*recents*/) {} + void setUnreadCount(size_t /*unread*/) {} + void clearBookmarks() {} + void setOnline(bool /*isOnline*/) {} + }; } diff --git a/Swift/Controllers/Chat/UserSearchController.cpp b/Swift/Controllers/Chat/UserSearchController.cpp index 3c7eb67..30c3c77 100644 --- a/Swift/Controllers/Chat/UserSearchController.cpp +++ b/Swift/Controllers/Chat/UserSearchController.cpp @@ -1,286 +1,393 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2017 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #include <Swift/Controllers/Chat/UserSearchController.h> +#include <memory> + #include <boost/bind.hpp> -#include <boost/shared_ptr.hpp> -#include <boost/smart_ptr/make_shared.hpp> -#include <Swiften/Base/foreach.h> +#include <Swiften/Avatars/AvatarManager.h> +#include <Swiften/Base/String.h> +#include <Swiften/Disco/DiscoServiceWalker.h> #include <Swiften/Disco/GetDiscoInfoRequest.h> #include <Swiften/Disco/GetDiscoItemsRequest.h> -#include <Swiften/Disco/DiscoServiceWalker.h> -#include <Swiften/VCards/VCardManager.h> #include <Swiften/Presence/PresenceOracle.h> -#include <Swiften/Avatars/AvatarManager.h> +#include <Swiften/VCards/VCardManager.h> + #include <Swift/Controllers/ContactEditController.h> -#include <Swift/Controllers/UIEvents/UIEventStream.h> -#include <Swift/Controllers/UIEvents/RequestChatWithUserDialogUIEvent.h> +#include <Swift/Controllers/ContactSuggester.h> +#include <Swift/Controllers/Intl.h> +#include <Swift/Controllers/ProfileSettingsProvider.h> +#include <Swift/Controllers/Roster/RosterController.h> #include <Swift/Controllers/UIEvents/RequestAddUserDialogUIEvent.h> +#include <Swift/Controllers/UIEvents/RequestChatWithUserDialogUIEvent.h> #include <Swift/Controllers/UIEvents/RequestInviteToMUCUIEvent.h> +#include <Swift/Controllers/UIEvents/UIEventStream.h> #include <Swift/Controllers/UIInterfaces/UserSearchWindow.h> #include <Swift/Controllers/UIInterfaces/UserSearchWindowFactory.h> -#include <Swift/Controllers/Roster/RosterController.h> -#include <Swift/Controllers/ContactSuggester.h> namespace Swift { -UserSearchController::UserSearchController(Type type, const JID& jid, UIEventStream* uiEventStream, VCardManager* vcardManager, UserSearchWindowFactory* factory, IQRouter* iqRouter, RosterController* rosterController, ContactSuggester* contactSuggester, AvatarManager* avatarManager, PresenceOracle* presenceOracle) : type_(type), jid_(jid), uiEventStream_(uiEventStream), vcardManager_(vcardManager), factory_(factory), iqRouter_(iqRouter), rosterController_(rosterController), contactSuggester_(contactSuggester), avatarManager_(avatarManager), presenceOracle_(presenceOracle) { - uiEventStream_->onUIEvent.connect(boost::bind(&UserSearchController::handleUIEvent, this, _1)); - vcardManager_->onVCardChanged.connect(boost::bind(&UserSearchController::handleVCardChanged, this, _1, _2)); - avatarManager_->onAvatarChanged.connect(boost::bind(&UserSearchController::handleAvatarChanged, this, _1)); - presenceOracle_->onPresenceChange.connect(boost::bind(&UserSearchController::handlePresenceChanged, this, _1)); - window_ = NULL; - discoWalker_ = NULL; + +static const std::string SEARCHED_DIRECTORIES = "searchedDirectories"; + +UserSearchController::UserSearchController(Type type, const JID& jid, UIEventStream* uiEventStream, VCardManager* vcardManager, UserSearchWindowFactory* factory, IQRouter* iqRouter, RosterController* rosterController, ContactSuggester* contactSuggester, AvatarManager* avatarManager, PresenceOracle* presenceOracle, ProfileSettingsProvider* settings) : type_(type), jid_(jid), uiEventStream_(uiEventStream), vcardManager_(vcardManager), factory_(factory), iqRouter_(iqRouter), rosterController_(rosterController), contactSuggester_(contactSuggester), avatarManager_(avatarManager), presenceOracle_(presenceOracle), settings_(settings) { + uiEventStream_->onUIEvent.connect(boost::bind(&UserSearchController::handleUIEvent, this, _1)); + vcardManager_->onVCardChanged.connect(boost::bind(&UserSearchController::handleVCardChanged, this, _1, _2)); + avatarManager_->onAvatarChanged.connect(boost::bind(&UserSearchController::handleAvatarChanged, this, _1)); + presenceOracle_->onPresenceChange.connect(boost::bind(&UserSearchController::handlePresenceChanged, this, _1)); + window_ = nullptr; + discoWalker_ = nullptr; + loadSavedDirectories(); } UserSearchController::~UserSearchController() { - endDiscoWalker(); - delete discoWalker_; - if (window_) { - window_->onNameSuggestionRequested.disconnect(boost::bind(&UserSearchController::handleNameSuggestionRequest, this, _1)); - window_->onFormRequested.disconnect(boost::bind(&UserSearchController::handleFormRequested, this, _1)); - window_->onSearchRequested.disconnect(boost::bind(&UserSearchController::handleSearch, this, _1, _2)); - window_->onJIDUpdateRequested.disconnect(boost::bind(&UserSearchController::handleJIDUpdateRequested, this, _1)); - delete window_; - } - presenceOracle_->onPresenceChange.disconnect(boost::bind(&UserSearchController::handlePresenceChanged, this, _1)); - avatarManager_->onAvatarChanged.disconnect(boost::bind(&UserSearchController::handleAvatarChanged, this, _1)); - vcardManager_->onVCardChanged.disconnect(boost::bind(&UserSearchController::handleVCardChanged, this, _1, _2)); - uiEventStream_->onUIEvent.disconnect(boost::bind(&UserSearchController::handleUIEvent, this, _1)); + endDiscoWalker(); + delete discoWalker_; + if (window_) { + window_->onNameSuggestionRequested.disconnect(boost::bind(&UserSearchController::handleNameSuggestionRequest, this, _1)); + window_->onFormRequested.disconnect(boost::bind(&UserSearchController::handleFormRequested, this, _1)); + window_->onSearchRequested.disconnect(boost::bind(&UserSearchController::handleSearch, this, _1, _2)); + window_->onContactSuggestionsRequested.disconnect(boost::bind(&UserSearchController::handleContactSuggestionsRequested, this, _1)); + window_->onJIDUpdateRequested.disconnect(boost::bind(&UserSearchController::handleJIDUpdateRequested, this, _1)); + window_->onJIDAddRequested.disconnect(boost::bind(&UserSearchController::handleJIDAddRequested, this, _1)); + window_->onJIDEditFieldChanged.disconnect(boost::bind(&UserSearchController::handleJIDEditingFinished, this, _1)); + delete window_; + } + presenceOracle_->onPresenceChange.disconnect(boost::bind(&UserSearchController::handlePresenceChanged, this, _1)); + avatarManager_->onAvatarChanged.disconnect(boost::bind(&UserSearchController::handleAvatarChanged, this, _1)); + vcardManager_->onVCardChanged.disconnect(boost::bind(&UserSearchController::handleVCardChanged, this, _1, _2)); + uiEventStream_->onUIEvent.disconnect(boost::bind(&UserSearchController::handleUIEvent, this, _1)); } UserSearchWindow* UserSearchController::getUserSearchWindow() { - initializeUserWindow(); - assert(window_); - return window_; + initializeUserWindow(); + assert(window_); + return window_; } void UserSearchController::setCanInitiateImpromptuMUC(bool supportsImpromptu) { - if (!window_) { - initializeUserWindow(); - } - window_->setCanStartImpromptuChats(supportsImpromptu); + if (!window_) { + initializeUserWindow(); + } + if (window_) { + window_->setCanStartImpromptuChats(supportsImpromptu); + } // Else doesn't support search } -void UserSearchController::handleUIEvent(boost::shared_ptr<UIEvent> event) { - bool handle = false; - boost::shared_ptr<RequestAddUserDialogUIEvent> addUserRequest = boost::shared_ptr<RequestAddUserDialogUIEvent>(); - RequestInviteToMUCUIEvent::ref inviteToMUCRequest = RequestInviteToMUCUIEvent::ref(); - switch (type_) { - case AddContact: - if ((addUserRequest = boost::dynamic_pointer_cast<RequestAddUserDialogUIEvent>(event))) { - handle = true; - } - break; - case StartChat: - if (boost::dynamic_pointer_cast<RequestChatWithUserDialogUIEvent>(event)) { - handle = true; - } - break; - case InviteToChat: - if ((inviteToMUCRequest = boost::dynamic_pointer_cast<RequestInviteToMUCUIEvent>(event))) { - handle = true; - } - break; - } - if (handle) { - initializeUserWindow(); - window_->show(); - if (addUserRequest) { - const std::string& name = addUserRequest->getPredefinedName(); - const JID& jid = addUserRequest->getPredefinedJID(); - if (!name.empty() && jid.isValid()) { - window_->prepopulateJIDAndName(jid, name); - } - } else if (inviteToMUCRequest) { - window_->setJIDs(inviteToMUCRequest->getInvites()); - window_->setRoomJID(inviteToMUCRequest->getRoom()); - } - return; - } +void UserSearchController::handleUIEvent(std::shared_ptr<UIEvent> event) { + bool handle = false; + std::shared_ptr<RequestAddUserDialogUIEvent> addUserRequest = std::shared_ptr<RequestAddUserDialogUIEvent>(); + auto inviteToMUCRequest = RequestInviteToMUCUIEvent::ref(); + switch (type_) { + case Type::AddContact: + if ((addUserRequest = std::dynamic_pointer_cast<RequestAddUserDialogUIEvent>(event))) { + handle = true; + } + break; + case Type::StartChat: + if (std::dynamic_pointer_cast<RequestChatWithUserDialogUIEvent>(event)) { + handle = true; + } + break; + case Type::InviteToChat: + if ((inviteToMUCRequest = std::dynamic_pointer_cast<RequestInviteToMUCUIEvent>(event))) { + handle = true; + } + break; + } + if (handle) { + initializeUserWindow(); + window_->show(); + window_->addSavedServices(savedDirectories_); + if (addUserRequest) { + const std::string& name = addUserRequest->getPredefinedName(); + const JID& jid = addUserRequest->getPredefinedJID(); + if (!name.empty() && jid.isValid()) { + window_->prepopulateJIDAndName(jid, name); + } + } + else if (inviteToMUCRequest) { + window_->setCanSupplyDescription(!inviteToMUCRequest->isImpromptu()); + window_->setJIDs(inviteToMUCRequest->getInvites()); + window_->setOriginator(inviteToMUCRequest->getOriginator()); + } + return; + } } void UserSearchController::handleFormRequested(const JID& service) { - window_->setSearchError(false); - window_->setServerSupportsSearch(true); - - //Abort a previous search if is active - endDiscoWalker(); - delete discoWalker_; - discoWalker_ = new DiscoServiceWalker(service, iqRouter_); - discoWalker_->onServiceFound.connect(boost::bind(&UserSearchController::handleDiscoServiceFound, this, _1, _2)); - discoWalker_->onWalkComplete.connect(boost::bind(&UserSearchController::handleDiscoWalkFinished, this)); - discoWalker_->beginWalk(); + window_->setSearchError(false); + window_->setServerSupportsSearch(true); + + //Abort a previous search if is active + endDiscoWalker(); + delete discoWalker_; + discoWalker_ = new DiscoServiceWalker(service, iqRouter_); + discoWalker_->onServiceFound.connect(boost::bind(&UserSearchController::handleDiscoServiceFound, this, _1, _2)); + discoWalker_->onWalkComplete.connect(boost::bind(&UserSearchController::handleDiscoWalkFinished, this)); + discoWalker_->beginWalk(); } void UserSearchController::endDiscoWalker() { - if (discoWalker_) { - discoWalker_->endWalk(); - discoWalker_->onServiceFound.disconnect(boost::bind(&UserSearchController::handleDiscoServiceFound, this, _1, _2)); - discoWalker_->onWalkComplete.disconnect(boost::bind(&UserSearchController::handleDiscoWalkFinished, this)); - } + if (discoWalker_) { + discoWalker_->endWalk(); + discoWalker_->onServiceFound.disconnect(boost::bind(&UserSearchController::handleDiscoServiceFound, this, _1, _2)); + discoWalker_->onWalkComplete.disconnect(boost::bind(&UserSearchController::handleDiscoWalkFinished, this)); + } } -void UserSearchController::handleDiscoServiceFound(const JID& jid, boost::shared_ptr<DiscoInfo> info) { - //bool isUserDirectory = false; - bool supports55 = false; - foreach (DiscoInfo::Identity identity, info->getIdentities()) { - if ((identity.getCategory() == "directory" - && identity.getType() == "user")) { - //isUserDirectory = true; - } - } - std::vector<std::string> features = info->getFeatures(); - supports55 = std::find(features.begin(), features.end(), DiscoInfo::JabberSearchFeature) != features.end(); - if (/*isUserDirectory && */supports55) { //FIXME: once M-Link correctly advertises directoryness. - /* Abort further searches.*/ - endDiscoWalker(); - boost::shared_ptr<GenericRequest<SearchPayload> > searchRequest(new GenericRequest<SearchPayload>(IQ::Get, jid, boost::make_shared<SearchPayload>(), iqRouter_)); - searchRequest->onResponse.connect(boost::bind(&UserSearchController::handleFormResponse, this, _1, _2)); - searchRequest->send(); - } +void UserSearchController::handleDiscoServiceFound(const JID& jid, std::shared_ptr<DiscoInfo> info) { + //bool isUserDirectory = false; + bool supports55 = false; + // TODO: Cleanup code + for (const auto& identity : info->getIdentities()) { + if ((identity.getCategory() == "directory" + && identity.getType() == "user")) { + //isUserDirectory = true; + } + } + std::vector<std::string> features = info->getFeatures(); + supports55 = std::find(features.begin(), features.end(), DiscoInfo::JabberSearchFeature) != features.end(); + if (/*isUserDirectory && */supports55) { //FIXME: once M-Link correctly advertises directoryness. + /* Abort further searches.*/ + endDiscoWalker(); + std::shared_ptr<GenericRequest<SearchPayload> > searchRequest(new GenericRequest<SearchPayload>(IQ::Get, jid, std::make_shared<SearchPayload>(), iqRouter_)); + searchRequest->onResponse.connect(boost::bind(&UserSearchController::handleFormResponse, this, _1, _2)); + searchRequest->send(); + } } -void UserSearchController::handleFormResponse(boost::shared_ptr<SearchPayload> fields, ErrorPayload::ref error) { - if (error || !fields) { - window_->setServerSupportsSearch(false); - return; - } - window_->setSearchFields(fields); +void UserSearchController::handleFormResponse(std::shared_ptr<SearchPayload> fields, ErrorPayload::ref error) { + if (error || !fields) { + window_->setServerSupportsSearch(false); + return; + } + window_->setSearchFields(fields); } -void UserSearchController::handleSearch(boost::shared_ptr<SearchPayload> fields, const JID& jid) { - boost::shared_ptr<GenericRequest<SearchPayload> > searchRequest(new GenericRequest<SearchPayload>(IQ::Set, jid, fields, iqRouter_)); - searchRequest->onResponse.connect(boost::bind(&UserSearchController::handleSearchResponse, this, _1, _2)); - searchRequest->send(); +void UserSearchController::handleSearch(std::shared_ptr<SearchPayload> fields, const JID& jid) { + addToSavedDirectories(jid); + std::shared_ptr<GenericRequest<SearchPayload> > searchRequest(new GenericRequest<SearchPayload>(IQ::Set, jid, fields, iqRouter_)); + searchRequest->onResponse.connect(boost::bind(&UserSearchController::handleSearchResponse, this, _1, _2)); + searchRequest->send(); } -void UserSearchController::handleSearchResponse(boost::shared_ptr<SearchPayload> resultsPayload, ErrorPayload::ref error) { - if (error || !resultsPayload) { - window_->setSearchError(true); - return; - } - - std::vector<UserSearchResult> results; - - if (resultsPayload->getForm()) { - window_->setResultsForm(resultsPayload->getForm()); - } else { - foreach (SearchPayload::Item item, resultsPayload->getItems()) { - JID jid(item.jid); - std::map<std::string, std::string> fields; - fields["first"] = item.first; - fields["last"] = item.last; - fields["nick"] = item.nick; - fields["email"] = item.email; - UserSearchResult result(jid, fields); - results.push_back(result); - } - window_->setResults(results); - } +void UserSearchController::handleSearchResponse(std::shared_ptr<SearchPayload> resultsPayload, ErrorPayload::ref error) { + if (error || !resultsPayload) { + window_->setSearchError(true); + return; + } + + std::vector<UserSearchResult> results; + + if (resultsPayload->getForm()) { + window_->setResultsForm(resultsPayload->getForm()); + } else { + for (auto&& item : resultsPayload->getItems()) { + JID jid(item.jid); + std::map<std::string, std::string> fields; + fields["first"] = item.first; + fields["last"] = item.last; + fields["nick"] = item.nick; + fields["email"] = item.email; + UserSearchResult result(jid, fields); + results.push_back(result); + } + window_->setResults(results); + } } void UserSearchController::handleNameSuggestionRequest(const JID &jid) { - suggestionsJID_= jid; - VCard::ref vcard = vcardManager_->getVCardAndRequestWhenNeeded(jid); - if (vcard) { - handleVCardChanged(jid, vcard); - } + suggestionsJID_= jid; + VCard::ref vcard = vcardManager_->getVCardAndRequestWhenNeeded(jid); + if (vcard) { + handleVCardChanged(jid, vcard); + } +} + +void UserSearchController::handleJIDEditingFinished(const JID& jid) { + if (jid.isValid()) { + if (rosterController_->getItem(jid)) { + window_->setWarning(QT_TRANSLATE_NOOP("", "This contact is already on your contact list.")); + } + else if (jid.getNode().empty()) { + window_->setWarning(QT_TRANSLATE_NOOP("", "Part of the address you have entered is missing. An address has a structure of 'user@example.com'.")); + } + else { + window_->setWarning(boost::optional<std::string>()); + } + } + else { + window_->setWarning(QT_TRANSLATE_NOOP("", "The address you have entered is invalid.")); + } } void UserSearchController::handleContactSuggestionsRequested(std::string text) { - window_->setContactSuggestions(contactSuggester_->getSuggestions(text)); + const std::vector<JID> existingJIDs = window_->getJIDs(); + std::vector<Contact::ref> suggestions = contactSuggester_->getSuggestions(text, false); + /* do not suggest contacts that have already been added to the chat list */ + std::vector<Contact::ref>::iterator i = suggestions.begin(); + while (i != suggestions.end()) { + bool found = false; + for (const auto& jid : existingJIDs) { + if ((*i)->jid == jid) { + found = true; + break; + } + } + + // remove contact suggestions which are already on the contact list in add-contact-mode + if (type_ == Type::AddContact) { + if (!found && !!rosterController_->getItem((*i)->jid)) { + found = true; + } + } + + if (found) { + i = suggestions.erase(i); + } else { + i++; + } + } + window_->setContactSuggestions(suggestions); } void UserSearchController::handleVCardChanged(const JID& jid, VCard::ref vcard) { - if (jid == suggestionsJID_) { - window_->setNameSuggestions(ContactEditController::nameSuggestionsFromVCard(vcard)); - suggestionsJID_ = JID(); - } - handleJIDUpdateRequested(std::vector<JID>(1, jid)); + if (jid == suggestionsJID_) { + window_->setNameSuggestions(ContactEditController::nameSuggestionsFromVCard(vcard)); + suggestionsJID_ = JID(); + } + handleJIDUpdateRequested(std::vector<JID>(1, jid)); } void UserSearchController::handleAvatarChanged(const JID& jid) { - handleJIDUpdateRequested(std::vector<JID>(1, jid)); + handleJIDUpdateRequested(std::vector<JID>(1, jid)); } void UserSearchController::handlePresenceChanged(Presence::ref presence) { - handleJIDUpdateRequested(std::vector<JID>(1, presence->getFrom().toBare())); + handleJIDUpdateRequested(std::vector<JID>(1, presence->getFrom().toBare())); } void UserSearchController::handleJIDUpdateRequested(const std::vector<JID>& jids) { - if (window_) { - std::vector<Contact> updates; - foreach(const JID& jid, jids) { - updates.push_back(convertJIDtoContact(jid)); - } - window_->updateContacts(updates); - } + if (window_) { + std::vector<Contact::ref> updates; + for (const auto& jid : jids) { + updates.push_back(convertJIDtoContact(jid)); + } + window_->updateContacts(updates); + } +} + +void UserSearchController::handleJIDAddRequested(const std::vector<JID>& jids) { + std::vector<Contact::ref> contacts; + for (const auto& jid : jids) { + contacts.push_back(convertJIDtoContact(jid)); + } + window_->addContacts(contacts); } -Contact UserSearchController::convertJIDtoContact(const JID& jid) { - Contact contact; - contact.jid = jid; - - // name lookup - boost::optional<XMPPRosterItem> rosterItem = rosterController_->getItem(jid); - if (rosterItem && !rosterItem->getName().empty()) { - contact.name = rosterItem->getName(); - } else { - VCard::ref vcard = vcardManager_->getVCard(jid); - if (vcard && !vcard->getFullName().empty()) { - contact.name = vcard->getFullName(); - } else { - contact.name = jid.toString(); - } - } - - // presence lookup - Presence::ref presence = presenceOracle_->getHighestPriorityPresence(jid); - if (presence) { - contact.statusType = presence->getShow(); - } else { - contact.statusType = StatusShow::None; - } - - // avatar lookup - contact.avatarPath = avatarManager_->getAvatarPath(jid); - return contact; +Contact::ref UserSearchController::convertJIDtoContact(const JID& jid) { + Contact::ref contact = std::make_shared<Contact>(); + contact->jid = jid; + + // name lookup + boost::optional<XMPPRosterItem> rosterItem = rosterController_->getItem(jid); + if (rosterItem && !rosterItem->getName().empty()) { + contact->name = rosterItem->getName(); + } else { + VCard::ref vcard = vcardManager_->getVCard(jid); + if (vcard && !vcard->getFullName().empty()) { + contact->name = vcard->getFullName(); + } else { + contact->name = jid.toString(); + } + } + + // presence lookup + Presence::ref presence = presenceOracle_->getAccountPresence(jid); + if (presence) { + contact->statusType = presence->getShow(); + } else { + contact->statusType = StatusShow::None; + } + + // avatar lookup + contact->avatarPath = avatarManager_->getAvatarPath(jid); + return contact; } void UserSearchController::handleDiscoWalkFinished() { - window_->setServerSupportsSearch(false); - endDiscoWalker(); + window_->setServerSupportsSearch(false); + endDiscoWalker(); } void UserSearchController::initializeUserWindow() { - if (!window_) { - UserSearchWindow::Type windowType = UserSearchWindow::AddContact; - switch(type_) { - case AddContact: - windowType = UserSearchWindow::AddContact; - break; - case StartChat: - windowType = UserSearchWindow::ChatToContact; - break; - case InviteToChat: - windowType = UserSearchWindow::InviteToChat; - break; - } - - window_ = factory_->createUserSearchWindow(windowType, uiEventStream_, rosterController_->getGroups()); - window_->onNameSuggestionRequested.connect(boost::bind(&UserSearchController::handleNameSuggestionRequest, this, _1)); - window_->onFormRequested.connect(boost::bind(&UserSearchController::handleFormRequested, this, _1)); - window_->onSearchRequested.connect(boost::bind(&UserSearchController::handleSearch, this, _1, _2)); - window_->onContactSuggestionsRequested.connect(boost::bind(&UserSearchController::handleContactSuggestionsRequested, this, _1)); - window_->onJIDUpdateRequested.connect(boost::bind(&UserSearchController::handleJIDUpdateRequested, this, _1)); - window_->setSelectedService(JID(jid_.getDomain())); - window_->clear(); - } + if (!window_) { + auto windowType = UserSearchWindow::Type::AddContact; + switch(type_) { + case Type::AddContact: + windowType = UserSearchWindow::Type::AddContact; + break; + case Type::StartChat: + windowType = UserSearchWindow::Type::ChatToContact; + break; + case Type::InviteToChat: + windowType = UserSearchWindow::Type::InviteToChat; + break; + } + + window_ = factory_->createUserSearchWindow(windowType, uiEventStream_, rosterController_->getGroups()); + if (!window_) { + // UI Doesn't support user search + return; + } + window_->onNameSuggestionRequested.connect(boost::bind(&UserSearchController::handleNameSuggestionRequest, this, _1)); + window_->onFormRequested.connect(boost::bind(&UserSearchController::handleFormRequested, this, _1)); + window_->onSearchRequested.connect(boost::bind(&UserSearchController::handleSearch, this, _1, _2)); + window_->onContactSuggestionsRequested.connect(boost::bind(&UserSearchController::handleContactSuggestionsRequested, this, _1)); + window_->onJIDUpdateRequested.connect(boost::bind(&UserSearchController::handleJIDUpdateRequested, this, _1)); + window_->onJIDAddRequested.connect(boost::bind(&UserSearchController::handleJIDAddRequested, this, _1)); + window_->onJIDEditFieldChanged.connect(boost::bind(&UserSearchController::handleJIDEditingFinished, this, _1)); + window_->setSelectedService(JID(jid_.getDomain())); + window_->clear(); + } +} + +void UserSearchController::loadSavedDirectories() { + savedDirectories_.clear(); + for (auto&& stringItem : String::split(settings_->getStringSetting(SEARCHED_DIRECTORIES), '\n')) { + if(!stringItem.empty()) { + savedDirectories_.push_back(JID(stringItem)); + } + } +} + +void UserSearchController::addToSavedDirectories(const JID& jid) { + if (!jid.isValid()) { + return; + } + + savedDirectories_.erase(std::remove(savedDirectories_.begin(), savedDirectories_.end(), jid), savedDirectories_.end()); + savedDirectories_.insert(savedDirectories_.begin(), jid); + + std::string collapsed; + int i = 0; + for (const auto& jidItem : savedDirectories_) { + if (i >= 15) { + break; + } + if (!collapsed.empty()) { + collapsed += "\n"; + } + collapsed += jidItem.toString(); + ++i; + } + settings_->storeString(SEARCHED_DIRECTORIES, collapsed); + window_->addSavedServices(savedDirectories_); } } diff --git a/Swift/Controllers/Chat/UserSearchController.h b/Swift/Controllers/Chat/UserSearchController.h index 21cad5e..49bb63c 100644 --- a/Swift/Controllers/Chat/UserSearchController.h +++ b/Swift/Controllers/Chat/UserSearchController.h @@ -1,89 +1,99 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2017 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <boost/shared_ptr.hpp> #include <map> +#include <memory> +#include <string> #include <vector> -#include <Swiften/Base/boost_bsignals.h> -#include <Swiften/Elements/SearchPayload.h> -#include <string> -#include <Swiften/JID/JID.h> +#include <boost/signals2.hpp> + #include <Swiften/Elements/DiscoInfo.h> #include <Swiften/Elements/DiscoItems.h> #include <Swiften/Elements/ErrorPayload.h> -#include <Swiften/Elements/VCard.h> #include <Swiften/Elements/Presence.h> +#include <Swiften/Elements/SearchPayload.h> +#include <Swiften/Elements/VCard.h> +#include <Swiften/JID/JID.h> + +#include <Swift/Controllers/Contact.h> namespace Swift { - class UIEventStream; - class UIEvent; - class UserSearchWindow; - class UserSearchWindowFactory; - class IQRouter; - class DiscoServiceWalker; - class RosterController; - class VCardManager; - class ContactSuggester; - class AvatarManager; - class PresenceOracle; - class Contact; + class UIEventStream; + class UIEvent; + class UserSearchWindow; + class UserSearchWindowFactory; + class IQRouter; + class DiscoServiceWalker; + class RosterController; + class VCardManager; + class ContactSuggester; + class AvatarManager; + class PresenceOracle; + class ProfileSettingsProvider; + + class UserSearchResult { + public: + UserSearchResult(const JID& jid, const std::map<std::string, std::string>& fields) : jid_(jid), fields_(fields) {} + const JID& getJID() const {return jid_;} + const std::map<std::string, std::string>& getFields() const {return fields_;} + private: + JID jid_; + std::map<std::string, std::string> fields_; + }; - class UserSearchResult { - public: - UserSearchResult(const JID& jid, const std::map<std::string, std::string>& fields) : jid_(jid), fields_(fields) {} - const JID& getJID() const {return jid_;} - const std::map<std::string, std::string>& getFields() const {return fields_;} - private: - JID jid_; - std::map<std::string, std::string> fields_; - }; + class UserSearchController { + public: + enum class Type {AddContact, StartChat, InviteToChat}; + UserSearchController(Type type, const JID& jid, UIEventStream* uiEventStream, VCardManager* vcardManager, UserSearchWindowFactory* userSearchWindowFactory, IQRouter* iqRouter, RosterController* rosterController, ContactSuggester* contactSuggester, AvatarManager* avatarManager, PresenceOracle* presenceOracle, ProfileSettingsProvider* settings); + ~UserSearchController(); - class UserSearchController { - public: - enum Type {AddContact, StartChat, InviteToChat}; - UserSearchController(Type type, const JID& jid, UIEventStream* uiEventStream, VCardManager* vcardManager, UserSearchWindowFactory* userSearchWindowFactory, IQRouter* iqRouter, RosterController* rosterController, ContactSuggester* contactSuggester, AvatarManager* avatarManager, PresenceOracle* presenceOracle); - ~UserSearchController(); + UserSearchWindow* getUserSearchWindow(); + void setCanInitiateImpromptuMUC(bool supportsImpromptu); - UserSearchWindow* getUserSearchWindow(); - void setCanInitiateImpromptuMUC(bool supportsImpromptu); + private: + void handleUIEvent(std::shared_ptr<UIEvent> event); + void handleFormRequested(const JID& service); + void handleDiscoServiceFound(const JID& jid, std::shared_ptr<DiscoInfo> info); + void handleDiscoWalkFinished(); + void handleFormResponse(std::shared_ptr<SearchPayload> items, ErrorPayload::ref error); + void handleSearch(std::shared_ptr<SearchPayload> fields, const JID& jid); + void handleSearchResponse(std::shared_ptr<SearchPayload> results, ErrorPayload::ref error); + void handleNameSuggestionRequest(const JID& jid); + void handleContactSuggestionsRequested(std::string text); + void handleVCardChanged(const JID& jid, VCard::ref vcard); + void handleAvatarChanged(const JID& jid); + void handlePresenceChanged(Presence::ref presence); + void handleJIDUpdateRequested(const std::vector<JID>& jids); + void handleJIDAddRequested(const std::vector<JID>& jids); + void handleJIDEditingFinished(const JID& jid); + Contact::ref convertJIDtoContact(const JID& jid); + void endDiscoWalker(); + void initializeUserWindow(); - private: - void handleUIEvent(boost::shared_ptr<UIEvent> event); - void handleFormRequested(const JID& service); - void handleDiscoServiceFound(const JID& jid, boost::shared_ptr<DiscoInfo> info); - void handleDiscoWalkFinished(); - void handleFormResponse(boost::shared_ptr<SearchPayload> items, ErrorPayload::ref error); - void handleSearch(boost::shared_ptr<SearchPayload> fields, const JID& jid); - void handleSearchResponse(boost::shared_ptr<SearchPayload> results, ErrorPayload::ref error); - void handleNameSuggestionRequest(const JID& jid); - void handleContactSuggestionsRequested(std::string text); - void handleVCardChanged(const JID& jid, VCard::ref vcard); - void handleAvatarChanged(const JID& jid); - void handlePresenceChanged(Presence::ref presence); - void handleJIDUpdateRequested(const std::vector<JID>& jids); - Contact convertJIDtoContact(const JID& jid); - void endDiscoWalker(); - void initializeUserWindow(); + void loadSavedDirectories(); + void addToSavedDirectories(const JID& jid); - private: - Type type_; - JID jid_; - JID suggestionsJID_; - UIEventStream* uiEventStream_; - VCardManager* vcardManager_; - UserSearchWindowFactory* factory_; - IQRouter* iqRouter_; - RosterController* rosterController_; - UserSearchWindow* window_; - DiscoServiceWalker* discoWalker_; - ContactSuggester* contactSuggester_; - AvatarManager* avatarManager_; - PresenceOracle* presenceOracle_; - }; + private: + Type type_; + JID jid_; + JID suggestionsJID_; + UIEventStream* uiEventStream_; + VCardManager* vcardManager_; + UserSearchWindowFactory* factory_; + IQRouter* iqRouter_; + RosterController* rosterController_; + UserSearchWindow* window_; + DiscoServiceWalker* discoWalker_; + ContactSuggester* contactSuggester_; + AvatarManager* avatarManager_; + PresenceOracle* presenceOracle_; + std::vector<JID> savedDirectories_; + ProfileSettingsProvider* settings_; + }; } diff --git a/Swift/Controllers/ChatMessageSummarizer.cpp b/Swift/Controllers/ChatMessageSummarizer.cpp index 014e032..ac3d896 100644 --- a/Swift/Controllers/ChatMessageSummarizer.cpp +++ b/Swift/Controllers/ChatMessageSummarizer.cpp @@ -1,45 +1,45 @@ /* - * Copyright (c) 2011-2013 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2011-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #include <Swift/Controllers/ChatMessageSummarizer.h> #include <Swiften/Base/format.h> + #include <Swift/Controllers/Intl.h> -#include <Swiften/Base/foreach.h> using namespace Swift; using namespace std; string ChatMessageSummarizer::getSummary(const string& current, const vector<UnreadPair>& unreads) { - vector<UnreadPair> others; - int currentUnread = 0; - int otherCount = 0; - foreach (UnreadPair unread, unreads) { - if (unread.first == current) { - currentUnread += unread.second; - } else { - if (unread.second > 0) { - otherCount += unread.second; - others.push_back(unread); - } - } - } - string myString(current); + vector<UnreadPair> others; + int currentUnread = 0; + int otherCount = 0; + for (const auto& unread : unreads) { + if (unread.first == current) { + currentUnread += unread.second; + } else { + if (unread.second > 0) { + otherCount += unread.second; + others.push_back(unread); + } + } + } + string myString(current); - if (currentUnread > 0) { - string result(QT_TRANSLATE_NOOP("", "%1% (%2%)")); - myString = str(format(result) % current % currentUnread); - } + if (currentUnread > 0) { + string result(QT_TRANSLATE_NOOP("", "%1% (%2%)")); + myString = str(format(result) % current % currentUnread); + } - if (others.size() > 1) { - string result(QT_TRANSLATE_NOOP("", "%1% and %2% others (%3%)")); - myString = str(format(result) % myString % others.size() % otherCount); - } else if (!others.empty()) { - string result(QT_TRANSLATE_NOOP("", "%1%; %2% (%3%)")); - myString = str(format(result) % myString % others[0].first % otherCount); - } - return myString; + if (others.size() > 1) { + string result(QT_TRANSLATE_NOOP("", "%1% and %2% others (%3%)")); + myString = str(format(result) % myString % others.size() % otherCount); + } else if (!others.empty()) { + string result(QT_TRANSLATE_NOOP("", "%1%; %2% (%3%)")); + myString = str(format(result) % myString % others[0].first % otherCount); + } + return myString; } diff --git a/Swift/Controllers/ChatMessageSummarizer.h b/Swift/Controllers/ChatMessageSummarizer.h index d4ff188..0b4df21 100644 --- a/Swift/Controllers/ChatMessageSummarizer.h +++ b/Swift/Controllers/ChatMessageSummarizer.h @@ -1,20 +1,20 @@ /* - * Copyright (c) 2011 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2011-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <vector> -#include <utility> #include <string> +#include <utility> +#include <vector> namespace Swift { typedef std::pair<std::string, int> UnreadPair; - class ChatMessageSummarizer { - public: - std::string getSummary(const std::string& current, const std::vector<UnreadPair>& unreads); - }; + class ChatMessageSummarizer { + public: + std::string getSummary(const std::string& current, const std::vector<UnreadPair>& unreads); + }; } diff --git a/Swift/Controllers/ConnectionSettings.h b/Swift/Controllers/ConnectionSettings.h index c02c5d4..7ce45cd 100644 --- a/Swift/Controllers/ConnectionSettings.h +++ b/Swift/Controllers/ConnectionSettings.h @@ -1,7 +1,7 @@ /* - * Copyright (c) 2012 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2012 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once @@ -9,32 +9,32 @@ #include <string> struct ConnectionSettings { - enum Method { - Automatic, - Manual, - BOSH - }; - enum ProxyType { - None, - System, - SOCKS5, - HTTPConnect - }; + enum Method { + Automatic, + Manual, + BOSH + }; + enum ProxyType { + None, + System, + SOCKS5, + HTTPConnect + }; - Method method; - struct { - bool useManualServer; - std::string manualServerHostname; - int manualServerPort; - ProxyType proxyType; - bool useManualProxy; - std::string manualProxyHostname; - int manualProxyPort; - } manualSettings; - struct { - std::string boshURI; - bool useManualProxy; - std::string manualProxyHostname; - int manualProxyPort; - } boshSettings; + Method method; + struct { + bool useManualServer; + std::string manualServerHostname; + int manualServerPort; + ProxyType proxyType; + bool useManualProxy; + std::string manualProxyHostname; + int manualProxyPort; + } manualSettings; + struct { + std::string boshURI; + bool useManualProxy; + std::string manualProxyHostname; + int manualProxyPort; + } boshSettings; }; diff --git a/Swift/Controllers/Contact.cpp b/Swift/Controllers/Contact.cpp index 7eb446c..b9b98c3 100644 --- a/Swift/Controllers/Contact.cpp +++ b/Swift/Controllers/Contact.cpp @@ -4,14 +4,74 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ +/* + * Copyright (c) 2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + #include <Swift/Controllers/Contact.h> +#include <boost/algorithm/string.hpp> +#include <boost/algorithm/string/find.hpp> + namespace Swift { -Contact::Contact() { +Contact::Contact() : statusType(StatusShow::None) { } Contact::Contact(const std::string& name, const JID& jid, StatusShow::Type statusType, const boost::filesystem::path& path) : name(name), jid(jid), statusType(statusType), avatarPath(path) { } +bool Contact::lexicographicalSortPredicate(const Contact::ref& a, const Contact::ref& b) { + if (a->jid.isValid() && b->jid.isValid()) { + return a->jid < b->jid; + } else { + return a->name < b->name; + } +} + +bool Contact::equalityPredicate(const Contact::ref& a, const Contact::ref& b) { + if (a->jid.isValid() && b->jid.isValid()) { + return a->jid == b->jid; + } else { + return a->name == b->name; + } +} + +bool Contact::sortPredicate(const Contact::ref& a, const Contact::ref& b, const std::string& search) { + /* perform case insensitive comparisons */ + std::string aLower = a->name; + boost::to_lower(aLower); + std::string bLower = b->name; + boost::to_lower(bLower); + std::string searchLower = search; + boost::to_lower(searchLower); + + /* name starts with the search term */ + if (aLower.find(searchLower) == 0 && bLower.find(searchLower) != 0) { + return true; + } else if (bLower.find(searchLower) == 0 && aLower.find(searchLower) != 0) { + return false; + } + + /* name contains search term */ + if (aLower.find(searchLower) != std::string::npos && bLower.find(searchLower) == std::string::npos) { + return true; + } else if (bLower.find(searchLower) != std::string::npos && aLower.find(searchLower) == std::string::npos) { + return false; + } + + /* Levenshtein should be done here */ + /* if edit distances are equal, fall through to the tests below */ + + /* lexicographical sort */ + if (a->statusType == b->statusType) { + return aLower.compare(bLower) < 0; + } + + /* online status */ + return a->statusType < b->statusType; +} + } diff --git a/Swift/Controllers/Contact.h b/Swift/Controllers/Contact.h index 039cd23..47dda43 100644 --- a/Swift/Controllers/Contact.h +++ b/Swift/Controllers/Contact.h @@ -4,8 +4,16 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ +/* + * Copyright (c) 2014-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + #pragma once +#include <memory> + #include <boost/filesystem/path.hpp> #include <Swiften/Elements/StatusShow.h> @@ -13,16 +21,22 @@ namespace Swift { -class Contact { - public: - Contact(); - Contact(const std::string& name, const JID& jid, StatusShow::Type statusType, const boost::filesystem::path& path); +class Contact : public std::enable_shared_from_this<Contact> { + public: + typedef std::shared_ptr<Contact> ref; + + Contact(); + Contact(const std::string& name, const JID& jid, StatusShow::Type statusType, const boost::filesystem::path& path); + + static bool lexicographicalSortPredicate(const Contact::ref& a, const Contact::ref& b); + static bool equalityPredicate(const Contact::ref& a, const Contact::ref& b); + static bool sortPredicate(const Contact::ref& a, const Contact::ref& b, const std::string& search); - public: - std::string name; - JID jid; - StatusShow::Type statusType; - boost::filesystem::path avatarPath; + public: + std::string name; + JID jid; + StatusShow::Type statusType; + boost::filesystem::path avatarPath; }; } diff --git a/Swift/Controllers/ContactEditController.cpp b/Swift/Controllers/ContactEditController.cpp index 2117d7a..2ea1f7e 100644 --- a/Swift/Controllers/ContactEditController.cpp +++ b/Swift/Controllers/ContactEditController.cpp @@ -1,109 +1,110 @@ /* - * Copyright (c) 2010-2012 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #include <Swift/Controllers/ContactEditController.h> +#include <memory> + #include <boost/algorithm/string.hpp> #include <boost/bind.hpp> -#include <boost/smart_ptr/make_shared.hpp> +#include <Swiften/VCards/VCardManager.h> + +#include <Swift/Controllers/Roster/RosterController.h> +#include <Swift/Controllers/UIEvents/RemoveRosterItemUIEvent.h> #include <Swift/Controllers/UIEvents/RequestContactEditorUIEvent.h> #include <Swift/Controllers/UIEvents/UIEventStream.h> -#include <Swift/Controllers/UIEvents/RemoveRosterItemUIEvent.h> #include <Swift/Controllers/UIInterfaces/ContactEditWindowFactory.h> -#include <Swift/Controllers/Roster/RosterController.h> -#include <Swiften/VCards/VCardManager.h> - namespace Swift { -ContactEditController::ContactEditController(RosterController* rosterController, VCardManager* vcardManager, ContactEditWindowFactory* contactEditWindowFactory, UIEventStream* uiEventStream) : rosterController(rosterController), vcardManager(vcardManager), contactEditWindowFactory(contactEditWindowFactory), uiEventStream(uiEventStream), contactEditWindow(NULL) { - uiEventStream->onUIEvent.connect(boost::bind(&ContactEditController::handleUIEvent, this, _1)); - vcardManager->onVCardChanged.connect(boost::bind(&ContactEditController::handleVCardChanged, this, _1, _2)); +ContactEditController::ContactEditController(RosterController* rosterController, VCardManager* vcardManager, ContactEditWindowFactory* contactEditWindowFactory, UIEventStream* uiEventStream) : rosterController(rosterController), vcardManager(vcardManager), contactEditWindowFactory(contactEditWindowFactory), uiEventStream(uiEventStream), contactEditWindow(nullptr) { + uiEventStream->onUIEvent.connect(boost::bind(&ContactEditController::handleUIEvent, this, _1)); + vcardManager->onVCardChanged.connect(boost::bind(&ContactEditController::handleVCardChanged, this, _1, _2)); } ContactEditController::~ContactEditController() { - if (contactEditWindow) { - contactEditWindow->onChangeContactRequest.disconnect(boost::bind(&ContactEditController::handleChangeContactRequest, this, _1, _2)); - contactEditWindow->onRemoveContactRequest.disconnect(boost::bind(&ContactEditController::handleRemoveContactRequest, this)); - delete contactEditWindow; - } - uiEventStream->onUIEvent.disconnect(boost::bind(&ContactEditController::handleUIEvent, this, _1)); + if (contactEditWindow) { + contactEditWindow->onChangeContactRequest.disconnect(boost::bind(&ContactEditController::handleChangeContactRequest, this, _1, _2)); + contactEditWindow->onRemoveContactRequest.disconnect(boost::bind(&ContactEditController::handleRemoveContactRequest, this)); + delete contactEditWindow; + } + uiEventStream->onUIEvent.disconnect(boost::bind(&ContactEditController::handleUIEvent, this, _1)); } void ContactEditController::handleUIEvent(UIEvent::ref event) { - RequestContactEditorUIEvent::ref editEvent = boost::dynamic_pointer_cast<RequestContactEditorUIEvent>(event); - if (!editEvent) { - return; - } - - if (!contactEditWindow) { - contactEditWindow = contactEditWindowFactory->createContactEditWindow(); - contactEditWindow->onRemoveContactRequest.connect(boost::bind(&ContactEditController::handleRemoveContactRequest, this)); - contactEditWindow->onChangeContactRequest.connect(boost::bind(&ContactEditController::handleChangeContactRequest, this, _1, _2)); - } - currentContact = rosterController->getItem(editEvent->getJID()); - assert(currentContact); - jid = rosterController->getItem(editEvent->getJID())->getJID(); - contactEditWindow->setContact(jid, currentContact->getName(), currentContact->getGroups(), rosterController->getGroups()); - contactEditWindow->show(); - - if (vcardManager) { - VCard::ref vcard = vcardManager->getVCardAndRequestWhenNeeded(jid); - if (vcard) { - handleVCardChanged(jid, vcard); - } - } + RequestContactEditorUIEvent::ref editEvent = std::dynamic_pointer_cast<RequestContactEditorUIEvent>(event); + if (!editEvent) { + return; + } + + if (!contactEditWindow) { + contactEditWindow = contactEditWindowFactory->createContactEditWindow(); + contactEditWindow->onRemoveContactRequest.connect(boost::bind(&ContactEditController::handleRemoveContactRequest, this)); + contactEditWindow->onChangeContactRequest.connect(boost::bind(&ContactEditController::handleChangeContactRequest, this, _1, _2)); + } + currentContact = rosterController->getItem(editEvent->getJID()); + assert(currentContact); + jid = rosterController->getItem(editEvent->getJID())->getJID(); + contactEditWindow->setContact(jid, currentContact->getName(), currentContact->getGroups(), rosterController->getGroups()); + contactEditWindow->show(); + + if (vcardManager) { + VCard::ref vcard = vcardManager->getVCardAndRequestWhenNeeded(jid); + if (vcard) { + handleVCardChanged(jid, vcard); + } + } } void ContactEditController::handleVCardChanged(const JID &jid, VCard::ref vcard) { - if (jid == this->jid) { - contactEditWindow->setNameSuggestions(nameSuggestionsFromVCard(vcard)); - } + if (jid == this->jid) { + contactEditWindow->setNameSuggestions(nameSuggestionsFromVCard(vcard)); + } } void ContactEditController::setAvailable(bool b) { - if (contactEditWindow) { - contactEditWindow->setEnabled(b); - } + if (contactEditWindow) { + contactEditWindow->setEnabled(b); + } } std::vector<std::string> ContactEditController::nameSuggestionsFromVCard(VCard::ref vcard) { - std::vector<std::string> suggestions; - if (!vcard->getNickname().empty()) { - suggestions.push_back(vcard->getNickname()); - } - if (!vcard->getFullName().empty()) { - suggestions.push_back(vcard->getFullName()); - } - if (!vcard->getGivenName().empty()) { - std::string suggestedName; - suggestedName = vcard->getGivenName(); - boost::algorithm::trim(suggestedName); - suggestions.push_back(suggestedName); - } - return suggestions; + std::vector<std::string> suggestions; + if (!vcard->getNickname().empty()) { + suggestions.push_back(vcard->getNickname()); + } + if (!vcard->getFullName().empty()) { + suggestions.push_back(vcard->getFullName()); + } + if (!vcard->getGivenName().empty()) { + std::string suggestedName; + suggestedName = vcard->getGivenName(); + boost::algorithm::trim(suggestedName); + suggestions.push_back(suggestedName); + } + return suggestions; } void ContactEditController::handleRemoveContactRequest() { - assert(currentContact); - uiEventStream->send(boost::make_shared<RemoveRosterItemUIEvent>(currentContact->getJID())); - contactEditWindow->hide(); + assert(currentContact); + uiEventStream->send(std::make_shared<RemoveRosterItemUIEvent>(currentContact->getJID())); + contactEditWindow->hide(); } void ContactEditController::handleChangeContactRequest(const std::string& name, const std::set<std::string>& newGroups) { - std::vector<std::string> oldGroupsVector = currentContact->getGroups(); - std::set<std::string> oldGroups(oldGroupsVector.begin(), oldGroupsVector.end()); - if (oldGroups != newGroups || currentContact->getName() != name) { - XMPPRosterItem newContact(*currentContact); - newContact.setName(name); - newContact.setGroups(std::vector<std::string>(newGroups.begin(), newGroups.end())); - rosterController->updateItem(newContact); - } - contactEditWindow->hide(); + std::vector<std::string> oldGroupsVector = currentContact->getGroups(); + std::set<std::string> oldGroups(oldGroupsVector.begin(), oldGroupsVector.end()); + if (oldGroups != newGroups || currentContact->getName() != name) { + XMPPRosterItem newContact(*currentContact); + newContact.setName(name); + newContact.setGroups(std::vector<std::string>(newGroups.begin(), newGroups.end())); + rosterController->updateItem(newContact); + } + contactEditWindow->hide(); } } diff --git a/Swift/Controllers/ContactEditController.h b/Swift/Controllers/ContactEditController.h index 5f64a6a..ab2b52e 100644 --- a/Swift/Controllers/ContactEditController.h +++ b/Swift/Controllers/ContactEditController.h @@ -1,54 +1,56 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <vector> #include <set> +#include <string> +#include <vector> + #include <boost/optional.hpp> -#include <Swiften/JID/JID.h> -#include <string> #include <Swiften/Elements/VCard.h> -#include <Swift/Controllers/UIEvents/UIEvent.h> +#include <Swiften/JID/JID.h> #include <Swiften/Roster/XMPPRosterItem.h> +#include <Swift/Controllers/UIEvents/UIEvent.h> + namespace Swift { - class UIEventStream; - class ContactEditWindowFactory; - class ContactEditWindow; - class RosterController; - class VCardManager; - - class ContactEditController { - public: - ContactEditController(RosterController* rosterController, VCardManager* vcardManager, ContactEditWindowFactory* contactEditWindowFactory, UIEventStream* uiEventStream); - ~ContactEditController(); - - void setAvailable(bool b); - - public: - static std::vector<std::string> nameSuggestionsFromVCard(VCard::ref vcard); - - private: - void handleRemoveContactRequest(); - void handleChangeContactRequest(const std::string& name, const std::set<std::string>& groups); - - private: - void handleUIEvent(UIEvent::ref event); - void handleVCardChanged(const JID& jid, VCard::ref vcard); - - private: - boost::optional<XMPPRosterItem> currentContact; - RosterController* rosterController; - VCardManager* vcardManager; - ContactEditWindowFactory* contactEditWindowFactory; - UIEventStream* uiEventStream; - JID jid; - ContactEditWindow* contactEditWindow; - }; + class UIEventStream; + class ContactEditWindowFactory; + class ContactEditWindow; + class RosterController; + class VCardManager; + + class ContactEditController { + public: + ContactEditController(RosterController* rosterController, VCardManager* vcardManager, ContactEditWindowFactory* contactEditWindowFactory, UIEventStream* uiEventStream); + ~ContactEditController(); + + void setAvailable(bool b); + + public: + static std::vector<std::string> nameSuggestionsFromVCard(VCard::ref vcard); + + private: + void handleRemoveContactRequest(); + void handleChangeContactRequest(const std::string& name, const std::set<std::string>& groups); + + private: + void handleUIEvent(UIEvent::ref event); + void handleVCardChanged(const JID& jid, VCard::ref vcard); + + private: + boost::optional<XMPPRosterItem> currentContact; + RosterController* rosterController; + VCardManager* vcardManager; + ContactEditWindowFactory* contactEditWindowFactory; + UIEventStream* uiEventStream; + JID jid; + ContactEditWindow* contactEditWindow; + }; } diff --git a/Swift/Controllers/ContactProvider.h b/Swift/Controllers/ContactProvider.h index 9ce371f..423f8a3 100644 --- a/Swift/Controllers/ContactProvider.h +++ b/Swift/Controllers/ContactProvider.h @@ -4,6 +4,12 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ +/* + * Copyright (c) 2014 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + #pragma once #include <vector> @@ -13,9 +19,9 @@ namespace Swift { class ContactProvider { - public: - virtual ~ContactProvider(); - virtual std::vector<Contact> getContacts() = 0; + public: + virtual ~ContactProvider(); + virtual std::vector<Contact::ref> getContacts(bool withMUCNicks) = 0; }; } diff --git a/Swift/Controllers/ContactSuggester.cpp b/Swift/Controllers/ContactSuggester.cpp index f1104b0..4b621db 100644 --- a/Swift/Controllers/ContactSuggester.cpp +++ b/Swift/Controllers/ContactSuggester.cpp @@ -4,24 +4,27 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ +/* + * Copyright (c) 2014-2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + #include <Swift/Controllers/ContactSuggester.h> +#include <algorithm> +#include <set> +#include <vector> + +#include <boost/algorithm/string.hpp> #include <boost/algorithm/string/find.hpp> -#include <boost/lambda/lambda.hpp> -#include <boost/lambda/bind.hpp> +#include <boost/bind.hpp> #include <Swiften/Base/Algorithm.h> -#include <Swiften/Base/foreach.h> #include <Swiften/JID/JID.h> #include <Swift/Controllers/ContactProvider.h> -#include <algorithm> -#include <vector> -#include <set> - -namespace lambda = boost::lambda; - namespace Swift { ContactSuggester::ContactSuggester() { @@ -31,50 +34,50 @@ ContactSuggester::~ContactSuggester() { } void ContactSuggester::addContactProvider(ContactProvider* provider) { - contactProviders_.push_back(provider); + contactProviders_.push_back(provider); } -bool ContactSuggester::matchContact(const std::string& search, const Contact& c) { - return fuzzyMatch(c.name, search) || fuzzyMatch(c.jid.toString(), search); +bool ContactSuggester::matchContact(const std::string& search, const Contact::ref& c) { + if (fuzzyMatch(c->name, search)) { + return true; + } + else if (c->jid.isValid()) { + return fuzzyMatch(c->jid.toString(), search); + } + return false; } -std::vector<Contact> ContactSuggester::getSuggestions(const std::string& search) const { - std::vector<Contact> results; +std::vector<Contact::ref> ContactSuggester::getSuggestions(const std::string& search, bool withMUCNicks) const { + std::vector<Contact::ref> results; - foreach(ContactProvider* provider, contactProviders_) { - append(results, provider->getContacts()); - } + for (auto provider : contactProviders_) { + append(results, provider->getContacts(withMUCNicks)); + } - std::sort(results.begin(), results.end(), - lambda::bind(&Contact::jid, lambda::_1) < lambda::bind(&Contact::jid, lambda::_2)); - results.erase(std::unique(results.begin(), results.end(), - lambda::bind(&Contact::jid, lambda::_1) == lambda::bind(&Contact::jid, lambda::_2)), - results.end()); - results.erase(std::remove_if(results.begin(), results.end(), !lambda::bind(&ContactSuggester::matchContact, search, lambda::_1)), - results.end()); - std::sort(results.begin(), results.end(), ContactSuggester::chatSortPredicate); + std::sort(results.begin(), results.end(), Contact::lexicographicalSortPredicate); + results.erase(std::unique(results.begin(), results.end(), Contact::equalityPredicate), results.end()); + results.erase(std::remove_if(results.begin(), results.end(), [&](const Contact::ref contact) { + return !matchContact(search, contact); + }), results.end()); + std::sort(results.begin(), results.end(), boost::bind(&Contact::sortPredicate, _1, _2, search)); - return results; + return results; } bool ContactSuggester::fuzzyMatch(std::string text, std::string match) { - for (std::string::iterator currentQueryChar = match.begin(); currentQueryChar != match.end(); currentQueryChar++) { - //size_t result = text.find(*currentQueryChar); - std::string::iterator result = boost::algorithm::ifind_first(text, std::string(currentQueryChar, currentQueryChar+1)).begin(); - if (result == text.end()) { - return false; - } - text.erase(result); - } - return true; -} - -bool ContactSuggester::chatSortPredicate(const Contact& a, const Contact& b) { - if (a.statusType == b.statusType) { - return a.name.compare(b.name) < 0; - } else { - return a.statusType < b.statusType; - } + std::string lowerText = text; + boost::algorithm::to_lower(lowerText); + std::string lowerMatch = match; + boost::algorithm::to_lower(lowerMatch); + size_t lastMatch = 0; + for (char i : lowerMatch) { + size_t where = lowerText.find_first_of(i, lastMatch); + if (where == std::string::npos) { + return false; + } + lastMatch = where + 1; + } + return true; } } diff --git a/Swift/Controllers/ContactSuggester.h b/Swift/Controllers/ContactSuggester.h index 137e5d3..f91879d 100644 --- a/Swift/Controllers/ContactSuggester.h +++ b/Swift/Controllers/ContactSuggester.h @@ -4,6 +4,12 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ +/* + * Copyright (c) 2014 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + #pragma once #include <string> @@ -11,26 +17,27 @@ #include <Swift/Controllers/Contact.h> +class ContactSuggesterTest; + namespace Swift { - class ContactProvider; - - class ContactSuggester { - public: - ContactSuggester(); - ~ContactSuggester(); - - void addContactProvider(ContactProvider* provider); - - std::vector<Contact> getSuggestions(const std::string& search) const; - private: - static bool matchContact(const std::string& search, const Contact& c); - /** - * Performs fuzzy matching on the string text. Matches when each character of match string is present in sequence in text string. - */ - static bool fuzzyMatch(std::string text, std::string match); - static bool chatSortPredicate(const Contact& a, const Contact& b); - - private: - std::vector<ContactProvider*> contactProviders_; - }; + class ContactProvider; + + class ContactSuggester { + public: + ContactSuggester(); + ~ContactSuggester(); + + void addContactProvider(ContactProvider* provider); + + std::vector<Contact::ref> getSuggestions(const std::string& search, bool withMUCNicks) const; + public: + static bool matchContact(const std::string& search, const Contact::ref& c); + /** + * Performs fuzzy matching on the string text. Matches when each character of match string is present in sequence in text string. + */ + static bool fuzzyMatch(std::string text, std::string match); + + private: + std::vector<ContactProvider*> contactProviders_; + }; } diff --git a/Swift/Controllers/ContactsFromXMPPRoster.cpp b/Swift/Controllers/ContactsFromXMPPRoster.cpp index 15a7767..1d1ccd4 100644 --- a/Swift/Controllers/ContactsFromXMPPRoster.cpp +++ b/Swift/Controllers/ContactsFromXMPPRoster.cpp @@ -4,9 +4,13 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ -#include <Swift/Controllers/ContactsFromXMPPRoster.h> +/* + * Copyright (c) 2014-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ -#include <Swiften/Base/foreach.h> +#include <Swift/Controllers/ContactsFromXMPPRoster.h> #include <Swiften/Avatars/AvatarManager.h> #include <Swiften/Presence/PresenceOracle.h> @@ -21,16 +25,16 @@ ContactsFromXMPPRoster::ContactsFromXMPPRoster(XMPPRoster* roster, AvatarManager ContactsFromXMPPRoster::~ContactsFromXMPPRoster() { } -std::vector<Contact> ContactsFromXMPPRoster::getContacts() { - std::vector<Contact> results; - std::vector<XMPPRosterItem> rosterItems = roster_->getItems(); - foreach(const XMPPRosterItem& rosterItem, rosterItems) { - Contact contact(rosterItem.getName().empty() ? rosterItem.getJID().toString() : rosterItem.getName(), rosterItem.getJID(), StatusShow::None,""); - contact.statusType = presenceOracle_->getHighestPriorityPresence(contact.jid) ? presenceOracle_->getHighestPriorityPresence(contact.jid)->getShow() : StatusShow::None; - contact.avatarPath = avatarManager_->getAvatarPath(contact.jid); - results.push_back(contact); - } - return results; +std::vector<Contact::ref> ContactsFromXMPPRoster::getContacts(bool /*withMUCNicks*/) { + std::vector<Contact::ref> results; + std::vector<XMPPRosterItem> rosterItems = roster_->getItems(); + for (const auto& rosterItem : rosterItems) { + Contact::ref contact = std::make_shared<Contact>(rosterItem.getName().empty() ? rosterItem.getJID().toString() : rosterItem.getName(), rosterItem.getJID(), StatusShow::None,""); + contact->statusType = presenceOracle_->getAccountPresence(contact->jid) ? presenceOracle_->getAccountPresence(contact->jid)->getShow() : StatusShow::None; + contact->avatarPath = avatarManager_->getAvatarPath(contact->jid); + results.push_back(contact); + } + return results; } } diff --git a/Swift/Controllers/ContactsFromXMPPRoster.h b/Swift/Controllers/ContactsFromXMPPRoster.h index 3815a99..2782dcc 100644 --- a/Swift/Controllers/ContactsFromXMPPRoster.h +++ b/Swift/Controllers/ContactsFromXMPPRoster.h @@ -4,6 +4,12 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ +/* + * Copyright (c) 2014 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + #pragma once #include <Swift/Controllers/ContactProvider.h> @@ -15,15 +21,15 @@ class AvatarManager; class XMPPRoster; class ContactsFromXMPPRoster : public ContactProvider { - public: - ContactsFromXMPPRoster(XMPPRoster* roster, AvatarManager* avatarManager, PresenceOracle* presenceOracle); - virtual ~ContactsFromXMPPRoster(); - - virtual std::vector<Contact> getContacts(); - private: - XMPPRoster* roster_; - AvatarManager* avatarManager_; - PresenceOracle* presenceOracle_; + public: + ContactsFromXMPPRoster(XMPPRoster* roster, AvatarManager* avatarManager, PresenceOracle* presenceOracle); + virtual ~ContactsFromXMPPRoster(); + + virtual std::vector<Contact::ref> getContacts(bool withMUCNicks); + private: + XMPPRoster* roster_; + AvatarManager* avatarManager_; + PresenceOracle* presenceOracle_; }; } diff --git a/Swift/Controllers/DummySoundPlayer.h b/Swift/Controllers/DummySoundPlayer.h index b91192c..e297cae 100644 --- a/Swift/Controllers/DummySoundPlayer.h +++ b/Swift/Controllers/DummySoundPlayer.h @@ -1,16 +1,16 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2015 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include "Swift/Controllers/SoundPlayer.h" +#include <Swift/Controllers/SoundPlayer.h> namespace Swift { - class DummySoundPlayer : public SoundPlayer { - public: - void playSound(SoundEffect sound) {} - }; + class DummySoundPlayer : public SoundPlayer { + public: + void playSound(SoundEffect /*sound*/, const std::string& /*soundResource*/) {} + }; } diff --git a/Swift/Controllers/DummySystemTray.h b/Swift/Controllers/DummySystemTray.h index 451588e..b36ed28 100644 --- a/Swift/Controllers/DummySystemTray.h +++ b/Swift/Controllers/DummySystemTray.h @@ -1,18 +1,18 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include "Swift/Controllers/SystemTray.h" +#include <Swift/Controllers/SystemTray.h> namespace Swift { - class DummySystemTray : public SystemTray { - public: - void setUnreadMessages(bool some) {} - void setStatusType(StatusShow::Type type) {} - void setConnecting() {} - }; + class DummySystemTray : public SystemTray { + public: + void setUnreadMessages(bool /*some*/) {} + void setStatusType(StatusShow::Type /*type*/) {} + void setConnecting() {} + }; } diff --git a/Swift/Controllers/EventNotifier.cpp b/Swift/Controllers/EventNotifier.cpp index 7feaac8..f22a58c 100644 --- a/Swift/Controllers/EventNotifier.cpp +++ b/Swift/Controllers/EventNotifier.cpp @@ -1,78 +1,77 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2017 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ -#include "Swift/Controllers/EventNotifier.h" +#include <Swift/Controllers/EventNotifier.h> -#include <boost/bind.hpp> #include <boost/algorithm/string.hpp> +#include <boost/bind.hpp> -#include <Swift/Controllers/Intl.h> -#include <Swiften/Base/format.h> +#include <Swiften/Avatars/AvatarManager.h> #include <Swiften/Base/String.h> -#include "Swift/Controllers/XMPPEvents/EventController.h" -#include "SwifTools/Notifier/Notifier.h" -#include "Swiften/Avatars/AvatarManager.h" -#include "Swiften/Client/NickResolver.h" -#include "Swiften/JID/JID.h" -#include "Swift/Controllers/XMPPEvents/MessageEvent.h" -#include "Swift/Controllers/XMPPEvents/SubscriptionRequestEvent.h" -#include "Swift/Controllers/XMPPEvents/ErrorEvent.h" -#include "Swift/Controllers/XMPPEvents/MUCInviteEvent.h" -#include "Swift/Controllers/Settings/SettingsProvider.h" +#include <Swiften/Base/format.h> +#include <Swiften/Client/NickResolver.h> +#include <Swiften/JID/JID.h> + +#include <Swift/Controllers/Intl.h> +#include <Swift/Controllers/Settings/SettingsProvider.h> +#include <Swift/Controllers/XMPPEvents/ErrorEvent.h> +#include <Swift/Controllers/XMPPEvents/EventController.h> +#include <Swift/Controllers/XMPPEvents/MUCInviteEvent.h> +#include <Swift/Controllers/XMPPEvents/MessageEvent.h> +#include <Swift/Controllers/XMPPEvents/SubscriptionRequestEvent.h> + +#include <SwifTools/Notifier/Notifier.h> namespace Swift { EventNotifier::EventNotifier(EventController* eventController, Notifier* notifier, AvatarManager* avatarManager, NickResolver* nickResolver) : eventController(eventController), notifier(notifier), avatarManager(avatarManager), nickResolver(nickResolver) { - eventController->onEventQueueEventAdded.connect(boost::bind(&EventNotifier::handleEventAdded, this, _1)); + eventController->onEventQueueEventAdded.connect(boost::bind(&EventNotifier::handleEventAdded, this, _1)); } EventNotifier::~EventNotifier() { - notifier->purgeCallbacks(); - eventController->onEventQueueEventAdded.disconnect(boost::bind(&EventNotifier::handleEventAdded, this, _1)); + notifier->purgeCallbacks(); + eventController->onEventQueueEventAdded.disconnect(boost::bind(&EventNotifier::handleEventAdded, this, _1)); } -void EventNotifier::handleEventAdded(boost::shared_ptr<StanzaEvent> event) { - if (event->getConcluded()) { - return; - } - if (boost::shared_ptr<MessageEvent> messageEvent = boost::dynamic_pointer_cast<MessageEvent>(event)) { - JID jid = messageEvent->getStanza()->getFrom(); - std::string title = nickResolver->jidToNick(jid); - if (!messageEvent->getStanza()->isError() && !messageEvent->getStanza()->getBody().empty()) { - JID activationJID = jid; - if (messageEvent->getStanza()->getType() == Message::Groupchat) { - activationJID = jid.toBare(); - } - std::string messageText = messageEvent->getStanza()->getBody(); - if (boost::starts_with(messageText, "/me ")) { - messageText = "*" + String::getSplittedAtFirst(messageText, ' ').second + "*"; - } - notifier->showMessage(Notifier::IncomingMessage, title, messageText, avatarManager->getAvatarPath(jid), boost::bind(&EventNotifier::handleNotificationActivated, this, activationJID)); - } - } - else if(boost::shared_ptr<SubscriptionRequestEvent> subscriptionEvent = boost::dynamic_pointer_cast<SubscriptionRequestEvent>(event)) { - JID jid = subscriptionEvent->getJID(); - std::string title = jid; - std::string message = str(format(QT_TRANSLATE_NOOP("", "%1% wants to add you to his/her contact list")) % nickResolver->jidToNick(jid)); - notifier->showMessage(Notifier::SystemMessage, title, message, boost::filesystem::path(), boost::function<void()>()); - } - else if(boost::shared_ptr<ErrorEvent> errorEvent = boost::dynamic_pointer_cast<ErrorEvent>(event)) { - notifier->showMessage(Notifier::SystemMessage, QT_TRANSLATE_NOOP("", "Error"), errorEvent->getText(), boost::filesystem::path(), boost::function<void()>()); - } - else if (boost::shared_ptr<MUCInviteEvent> mucInviteEvent = boost::dynamic_pointer_cast<MUCInviteEvent>(event)) { - std::string title = mucInviteEvent->getInviter(); - std::string message = str(format(QT_TRANSLATE_NOOP("", "%1% has invited you to enter the %2% room")) % nickResolver->jidToNick(mucInviteEvent->getInviter()) % mucInviteEvent->getRoomJID()); - // FIXME: not show avatar or greyed out avatar for mediated invites - notifier->showMessage(Notifier::SystemMessage, title, message, avatarManager->getAvatarPath(mucInviteEvent->getInviter()), boost::bind(&EventNotifier::handleNotificationActivated, this, mucInviteEvent->getInviter())); - } +void EventNotifier::handleEventAdded(std::shared_ptr<StanzaEvent> event) { + if (event->getConcluded()) { + return; + } + if (std::shared_ptr<MessageEvent> messageEvent = std::dynamic_pointer_cast<MessageEvent>(event)) { + JID jid = messageEvent->getStanza()->getFrom(); + if (!messageEvent->getStanza()->isError() && !messageEvent->getStanza()->getBody().get_value_or("").empty()) { + JID activationJID = jid; + if (messageEvent->getStanza()->getType() == Message::Groupchat) { + activationJID = jid.toBare(); + } + for (const auto& notification : messageEvent->getNotifications()) { + notifier->showMessage(Notifier::IncomingMessage, notification.title, notification.message, avatarManager->getAvatarPath(jid), boost::bind(&EventNotifier::handleNotificationActivated, this, activationJID)); + } + } + } + else if(std::shared_ptr<SubscriptionRequestEvent> subscriptionEvent = std::dynamic_pointer_cast<SubscriptionRequestEvent>(event)) { + JID jid = subscriptionEvent->getJID(); + std::string title = jid; + std::string message = str(format(QT_TRANSLATE_NOOP("", "%1% wants to add you to his/her contact list")) % nickResolver->jidToNick(jid)); + notifier->showMessage(Notifier::SystemMessage, title, message, boost::filesystem::path(), boost::function<void()>()); + } + else if(std::shared_ptr<ErrorEvent> errorEvent = std::dynamic_pointer_cast<ErrorEvent>(event)) { + notifier->showMessage(Notifier::SystemMessage, QT_TRANSLATE_NOOP("", "Error"), errorEvent->getText(), boost::filesystem::path(), boost::function<void()>()); + } + else if (std::shared_ptr<MUCInviteEvent> mucInviteEvent = std::dynamic_pointer_cast<MUCInviteEvent>(event)) { + std::string title = mucInviteEvent->getInviter(); + std::string message = str(format(QT_TRANSLATE_NOOP("", "%1% has invited you to enter the %2% room")) % nickResolver->jidToNick(mucInviteEvent->getInviter()) % mucInviteEvent->getRoomJID()); + // FIXME: not show avatar or greyed out avatar for mediated invites + notifier->showMessage(Notifier::SystemMessage, title, message, avatarManager->getAvatarPath(mucInviteEvent->getInviter()), boost::bind(&EventNotifier::handleNotificationActivated, this, mucInviteEvent->getInviter())); + } } void EventNotifier::handleNotificationActivated(JID jid) { - onNotificationActivated(jid); + onNotificationActivated(jid); } } diff --git a/Swift/Controllers/EventNotifier.h b/Swift/Controllers/EventNotifier.h index d88fa80..b44615d 100644 --- a/Swift/Controllers/EventNotifier.h +++ b/Swift/Controllers/EventNotifier.h @@ -1,42 +1,45 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <boost/shared_ptr.hpp> +#include <memory> -#include "SwifTools/Notifier/Notifier.h" -#include "Swiften/Base/boost_bsignals.h" -#include "Swift/Controllers/XMPPEvents/StanzaEvent.h" -#include "Swiften/JID/JID.h" +#include <boost/signals2.hpp> + +#include <Swiften/JID/JID.h> + +#include <Swift/Controllers/XMPPEvents/StanzaEvent.h> + +#include <SwifTools/Notifier/Notifier.h> namespace Swift { - class EventController; - class Notifier; - class AvatarManager; - class NickResolver; - class JID; - class UIEventStream; - class SettingsProvider; - - class EventNotifier { - public: - EventNotifier(EventController* eventController, Notifier* notifier, AvatarManager* avatarManager, NickResolver* nickResolver); - ~EventNotifier(); - - boost::signal<void (const JID&)> onNotificationActivated; - - private: - void handleEventAdded(boost::shared_ptr<StanzaEvent>); - void handleNotificationActivated(JID jid); - - private: - EventController* eventController; - Notifier* notifier; - AvatarManager* avatarManager; - NickResolver* nickResolver; - }; + class EventController; + class Notifier; + class AvatarManager; + class NickResolver; + class JID; + class UIEventStream; + class SettingsProvider; + + class EventNotifier { + public: + EventNotifier(EventController* eventController, Notifier* notifier, AvatarManager* avatarManager, NickResolver* nickResolver); + ~EventNotifier(); + + boost::signals2::signal<void (const JID&)> onNotificationActivated; + + private: + void handleEventAdded(std::shared_ptr<StanzaEvent>); + void handleNotificationActivated(JID jid); + + private: + EventController* eventController; + Notifier* notifier; + AvatarManager* avatarManager; + NickResolver* nickResolver; + }; } diff --git a/Swift/Controllers/EventWindowController.cpp b/Swift/Controllers/EventWindowController.cpp index a6ba3c1..412bb71 100644 --- a/Swift/Controllers/EventWindowController.cpp +++ b/Swift/Controllers/EventWindowController.cpp @@ -1,53 +1,54 @@ /* - * Copyright (c) 2010-2012 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #include <Swift/Controllers/EventWindowController.h> +#include <boost/bind.hpp> + #include <Swift/Controllers/XMPPEvents/MessageEvent.h> #include <Swift/Controllers/XMPPEvents/SubscriptionRequestEvent.h> -#include <boost/bind.hpp> namespace Swift { EventWindowController::EventWindowController(EventController* eventController, EventWindowFactory* windowFactory) { - eventController_ = eventController; - windowFactory_ = windowFactory; - window_ = windowFactory_->createEventWindow(); - eventAddedConnection_ = eventController_->onEventQueueEventAdded.connect(boost::bind(&EventWindowController::handleEventQueueEventAdded, this, _1)); + eventController_ = eventController; + windowFactory_ = windowFactory; + window_ = windowFactory_->createEventWindow(); + eventAddedConnection_ = eventController_->onEventQueueEventAdded.connect(boost::bind(&EventWindowController::handleEventQueueEventAdded, this, _1)); } EventWindowController::~EventWindowController() { - if (window_->canDelete()) { - delete window_; - } + if (window_->canDelete()) { + delete window_; + } } -void EventWindowController::handleEventQueueEventAdded(boost::shared_ptr<StanzaEvent> event) { - if (event->getConcluded()) { - handleEventConcluded(event); - } else { - boost::shared_ptr<MessageEvent> message = boost::dynamic_pointer_cast<MessageEvent>(event); - if (!(message && message->isReadable())) { - event->onConclusion.connect(boost::bind(&EventWindowController::handleEventConcluded, this, event)); - window_->addEvent(event, true); - } - } +void EventWindowController::handleEventQueueEventAdded(std::shared_ptr<StanzaEvent> event) { + if (event->getConcluded()) { + handleEventConcluded(event); + } else { + std::shared_ptr<MessageEvent> message = std::dynamic_pointer_cast<MessageEvent>(event); + if (!(message && message->isReadable())) { + event->onConclusion.connect(boost::bind(&EventWindowController::handleEventConcluded, this, event)); + window_->addEvent(event, true); + } + } } -void EventWindowController::handleEventConcluded(boost::shared_ptr<StanzaEvent> event) { - window_->removeEvent(event); - bool includeAsCompleted = true; - /* Because subscription requests get duplicated, don't add them back */ - if (boost::dynamic_pointer_cast<SubscriptionRequestEvent>(event) || boost::dynamic_pointer_cast<MessageEvent>(event)) { - includeAsCompleted = false; - } - if (includeAsCompleted) { - window_->addEvent(event, false); - } - event->onConclusion.disconnect(boost::bind(&EventWindowController::handleEventConcluded, this, event)); +void EventWindowController::handleEventConcluded(std::shared_ptr<StanzaEvent> event) { + window_->removeEvent(event); + bool includeAsCompleted = true; + /* Because subscription requests get duplicated, don't add them back */ + if (std::dynamic_pointer_cast<SubscriptionRequestEvent>(event) || std::dynamic_pointer_cast<MessageEvent>(event)) { + includeAsCompleted = false; + } + if (includeAsCompleted) { + window_->addEvent(event, false); + } + event->onConclusion.disconnect(boost::bind(&EventWindowController::handleEventConcluded, this, event)); } } diff --git a/Swift/Controllers/EventWindowController.h b/Swift/Controllers/EventWindowController.h index 28b9bf6..aa730d0 100644 --- a/Swift/Controllers/EventWindowController.h +++ b/Swift/Controllers/EventWindowController.h @@ -1,30 +1,29 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include "Swift/Controllers/UIInterfaces/EventWindowFactory.h" -#include "Swift/Controllers/UIInterfaces/EventWindow.h" -#include "Swift/Controllers/XMPPEvents/EventController.h" - +#include <Swift/Controllers/UIInterfaces/EventWindow.h> +#include <Swift/Controllers/UIInterfaces/EventWindowFactory.h> +#include <Swift/Controllers/XMPPEvents/EventController.h> namespace Swift { - class EventWindowController { - public: - EventWindowController(EventController* eventController, EventWindowFactory* windowFactory); - ~EventWindowController(); - private: - void handleEventQueueEventAdded(boost::shared_ptr<StanzaEvent> event); - void handleEventConcluded(boost::shared_ptr<StanzaEvent> event); + class EventWindowController { + public: + EventWindowController(EventController* eventController, EventWindowFactory* windowFactory); + ~EventWindowController(); + private: + void handleEventQueueEventAdded(std::shared_ptr<StanzaEvent> event); + void handleEventConcluded(std::shared_ptr<StanzaEvent> event); - EventController* eventController_; - EventWindowFactory* windowFactory_; - EventWindow* window_; - boost::bsignals::scoped_connection eventAddedConnection_; - }; + EventController* eventController_; + EventWindowFactory* windowFactory_; + EventWindow* window_; + boost::signals2::scoped_connection eventAddedConnection_; + }; } diff --git a/Swift/Controllers/FdpFormSubmitController.cpp b/Swift/Controllers/FdpFormSubmitController.cpp new file mode 100644 index 0000000..639b4e9 --- /dev/null +++ b/Swift/Controllers/FdpFormSubmitController.cpp @@ -0,0 +1,145 @@ +/* + * Copyright (c) 2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#include <Swift/Controllers/FdpFormSubmitController.h> + +#include <Swiften/Disco/GetDiscoItemsRequest.h> +#include <Swiften/Elements/DiscoItems.h> +#include <Swiften/Queries/IQRouter.h> +#include <Swiften/Queries/PubSubRequest.h> + +#include <Swift/Controllers/UIEvents/FdpFormSubmitWindowOpenUIEvent.h> +#include <Swift/Controllers/UIEvents/UIEventStream.h> +#include <Swift/Controllers/UIInterfaces/FdpFormSubmitWindow.h> +#include <Swift/Controllers/UIInterfaces/FdpFormSubmitWindowFactory.h> + +namespace Swift { + +FdpFormSubmitController::FdpFormSubmitController(const JID& self, IQRouter* iqRouter, UIEventStream* uiEventStream, FdpFormSubmitWindowFactory* factory) : selfJID_(self), iqRouter_(iqRouter), uiEventStream_(uiEventStream), factory_(factory), formSubmitWindow_(nullptr) { + fdpFormSubmitWindowOpenUIEventConnection_= uiEventStream_->onUIEvent.connect( [this](const std::shared_ptr<UIEvent>& uiEvent){ handleUIEvent(uiEvent); }); +} + +FdpFormSubmitController::~FdpFormSubmitController() { +} + +void FdpFormSubmitController::handleUIEvent(const std::shared_ptr<UIEvent>& uiEvent) { + if (auto openEvent = std::dynamic_pointer_cast<FdpFormSubmitWindowOpenUIEvent>(uiEvent)) { + if (formSubmitWindow_) { + formSubmitWindow_->raise(); + } + else { + createFormSubmitWindow(); + } + } +} + +void FdpFormSubmitController::createFormSubmitWindow() { + formSubmitWindow_ = factory_->createFdpFormSubmitWindow(); + formSubmitWindow_->onCloseEvent.connect([this](){ closeFormSubmitWindow(); }); + formSubmitWindow_->onRequestPubSubNodeData.connect([this](const std::string& string){ requestPubSubNodeData(string); }); + formSubmitWindow_->onRequestTemplateForm.connect([this](const std::string& fdpTemplateNodeName){ requestTemplateForm(fdpTemplateNodeName); }); + formSubmitWindow_->onSubmitForm.connect([this](const std::shared_ptr<Form>& form){ submitForm(form); }); + formSubmitWindow_->show(); +} + +void FdpFormSubmitController::closeFormSubmitWindow() { + formSubmitWindow_.reset(); +} + +void FdpFormSubmitController::requestPubSubNodeData(const std::string& domainName) { + JID domainJID(domainName); + auto discoItemsRequest = GetDiscoItemsRequest::create(domainJID, iqRouter_); + discoItemsRequest->onResponse.connect( + [this, domainName](std::shared_ptr<DiscoItems> discoItems, ErrorPayload::ref errorPayloadRef) { + if (!discoItems || errorPayloadRef) { + formSubmitWindow_->showNodePlaceholder(FdpFormSubmitWindow::NodeError::DomainNotFound); + return; + } + currentDomain_ = domainName; + + bool templateNodeAdded = false; + formSubmitWindow_->clearNodeData(); + auto discoItemList = discoItems->getItems(); + for (auto discoItem : discoItemList) { + auto node = discoItem.getNode(); + if (node.substr(0, 13) != "fdp/template/") { + continue; + } + + std::string nodeName = discoItem.getName().empty() ? node : discoItem.getName(); + + formSubmitWindow_->addNode(node, nodeName); + templateNodeAdded = true; + } + if (!templateNodeAdded) { + formSubmitWindow_->showNodePlaceholder(FdpFormSubmitWindow::NodeError::NoFdpNodesInDomain); + } + } + ); + discoItemsRequest->send(); +} + +void FdpFormSubmitController::requestTemplateForm(const std::string& nodeName) { + auto pubSubItems = std::make_shared<PubSubItems>(nodeName); + pubSubItems->setMaximumItems(1); + auto formRequest = std::make_shared<PubSubRequest<PubSubItems>>(IQ::Type::Get, selfJID_, currentDomain_, pubSubItems, iqRouter_); + + formRequest->onResponse.connect( + [this, nodeName](std::shared_ptr<PubSubItems> response, ErrorPayload::ref errorPayload) { + if (!response || errorPayload) { + formSubmitWindow_->showFormPlaceholder(FdpFormSubmitWindow::TemplateError::RequestFailed); + return; + } + auto pubSubItemList = response->getItems(); + if (pubSubItemList.empty()) { + formSubmitWindow_->showFormPlaceholder(FdpFormSubmitWindow::TemplateError::CannotLocateForm); + return; + } + auto payloadList = pubSubItemList[0]->getData(); + if (payloadList.empty()) { + formSubmitWindow_->showFormPlaceholder(FdpFormSubmitWindow::TemplateError::CannotLocateForm); + return; + } + if (auto form = std::dynamic_pointer_cast<Form>(payloadList[0])) { + currentTemplateNode_ = nodeName; + formSubmitWindow_->setFormData(form); + } + else { + formSubmitWindow_->showFormPlaceholder(FdpFormSubmitWindow::TemplateError::InvalidPayload); + return; + } + } + ); + + formRequest->send(); +} + +void FdpFormSubmitController::submitForm(const std::shared_ptr<Form>& form) { + std::string submittedNode = currentTemplateNode_; + submittedNode.replace(submittedNode.find("/template/", 0), 10, "/submitted/"); + auto pubSubItem = std::make_shared<PubSubItem>(); + auto pubSubItems = std::make_shared<PubSubItems>(submittedNode); + auto pubSubPublish = std::make_shared<PubSubPublish>(); + pubSubPublish->setNode(submittedNode); + pubSubPublish->addItem(pubSubItem); + pubSubItem->addData(form); + pubSubItems->addItem(pubSubItem); + auto formRequest = std::make_shared<PubSubRequest<PubSubPublish>>(IQ::Type::Set, selfJID_, currentDomain_, pubSubPublish, iqRouter_); + + formRequest->onResponse.connect( + [this](std::shared_ptr<PubSubPublish> response, ErrorPayload::ref errorPayload) { + if (!response || errorPayload) { + formSubmitWindow_->handleSubmitServerResponse(false); + return; + } + formSubmitWindow_->handleSubmitServerResponse(true); + } + ); + + formRequest->send(); +} + +} diff --git a/Swift/Controllers/FdpFormSubmitController.h b/Swift/Controllers/FdpFormSubmitController.h new file mode 100644 index 0000000..69db08c --- /dev/null +++ b/Swift/Controllers/FdpFormSubmitController.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#pragma once + +#include <memory> +#include <string> + +#include <boost/signals2.hpp> + +#include <Swiften/JID/JID.h> + +namespace Swift { + class FdpFormSubmitWindow; + class FdpFormSubmitWindowFactory; + class Form; + class IQRouter; + class UIEvent; + class UIEventStream; + + class FdpFormSubmitController { + public: + FdpFormSubmitController(const JID& self, IQRouter* iqRouter, UIEventStream* uiEventStream, FdpFormSubmitWindowFactory* factory); + ~FdpFormSubmitController(); + + private: + void handleUIEvent(const std::shared_ptr<UIEvent>& uiEvent); + void createFormSubmitWindow(); + void closeFormSubmitWindow(); + void requestPubSubNodeData(const std::string& domainName); + void requestTemplateForm(const std::string& nodeName); + void submitForm(const std::shared_ptr<Form>& form); + + JID selfJID_; + IQRouter* iqRouter_; + UIEventStream* uiEventStream_; + FdpFormSubmitWindowFactory* factory_; + std::unique_ptr<FdpFormSubmitWindow> formSubmitWindow_; + std::string currentDomain_; + std::string currentTemplateNode_; + + boost::signals2::scoped_connection fdpFormSubmitWindowOpenUIEventConnection_; + }; +} diff --git a/Swift/Controllers/FileTransfer/FileTransferController.cpp b/Swift/Controllers/FileTransfer/FileTransferController.cpp index 0160a7a..5b86a7b 100644 --- a/Swift/Controllers/FileTransfer/FileTransferController.cpp +++ b/Swift/Controllers/FileTransfer/FileTransferController.cpp @@ -4,150 +4,160 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ -#include "FileTransferController.h" -#include "Swiften/FileTransfer/OutgoingJingleFileTransfer.h" -#include "Swiften/FileTransfer/FileTransferManager.h" -#include <Swiften/FileTransfer/FileReadBytestream.h> -#include <Swiften/Base/boost_bsignals.h> +/* + * Copyright (c) 2015-2019 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#include <Swift/Controllers/FileTransfer/FileTransferController.h> + +#include <memory> + #include <boost/bind.hpp> #include <boost/filesystem.hpp> -#include "Swift/Controllers/UIInterfaces/ChatWindow.h" +#include <boost/signals2.hpp> + #include <Swiften/Base/Log.h> -#include <Swift/Controllers/Intl.h> +#include <Swiften/FileTransfer/FileReadBytestream.h> +#include <Swiften/FileTransfer/FileTransferManager.h> +#include <Swiften/FileTransfer/OutgoingJingleFileTransfer.h> -#include <boost/smart_ptr/make_shared.hpp> +#include <Swift/Controllers/Intl.h> +#include <Swift/Controllers/UIInterfaces/ChatWindow.h> namespace Swift { FileTransferController::FileTransferController(const JID& receipient, const std::string& filename, FileTransferManager* fileTransferManager) : - sending(true), otherParty(receipient), filename(filename), ftManager(fileTransferManager), ftProgressInfo(0), chatWindow(0), currentState(FileTransfer::State::WaitingForStart) { - + sending(true), otherParty(receipient), filename(filename), ftManager(fileTransferManager), ftProgressInfo(nullptr), chatWindow(nullptr), currentState(FileTransfer::State::WaitingForStart) { + } FileTransferController::FileTransferController(IncomingFileTransfer::ref transfer) : - sending(false), otherParty(transfer->getSender()), filename(transfer->getFileName()), transfer(transfer), ftManager(0), ftProgressInfo(0), chatWindow(0), currentState(FileTransfer::State::WaitingForStart) { - + sending(false), otherParty(transfer->getSender()), filename(transfer->getFileName()), transfer(transfer), ftManager(nullptr), ftProgressInfo(nullptr), chatWindow(nullptr), currentState(FileTransfer::State::WaitingForStart) { + transfer->onStateChanged.connect(boost::bind(&FileTransferController::handleFileTransferStateChange, this, _1)); } FileTransferController::~FileTransferController() { - delete ftProgressInfo; + delete ftProgressInfo; + transfer->onStateChanged.disconnect(boost::bind(&FileTransferController::handleFileTransferStateChange, this, _1)); } const JID &FileTransferController::getOtherParty() const { - return otherParty; + return otherParty; } -std::string FileTransferController::setChatWindow(ChatWindow* wnd, std::string nickname) { - chatWindow = wnd; - if (sending) { - uiID = wnd->addFileTransfer(QT_TRANSLATE_NOOP("", "me"), true, filename, boost::filesystem::file_size(boost::filesystem::path(filename))); - } else { - uiID = wnd->addFileTransfer(nickname, false, filename, transfer->getFileSizeInBytes()); - } - return uiID; +std::string FileTransferController::setChatWindow(ChatWindow* wnd, const std::string& nickname, const std::string& avatarPath) { + chatWindow = wnd; + if (sending) { + uiID = wnd->addFileTransfer(QT_TRANSLATE_NOOP("", "me"), avatarPath, true, filename, boost::filesystem::file_size(boost::filesystem::path(filename)), ""); + } else { + uiID = wnd->addFileTransfer(nickname, avatarPath, false, filename, transfer->getFileSizeInBytes(), transfer->getDescription()); + } + return uiID; } void FileTransferController::setReceipient(const JID& receipient) { - this->otherParty = receipient; + this->otherParty = receipient; } bool FileTransferController::isIncoming() const { - return !sending; + return !sending; } FileTransfer::State FileTransferController::getState() const { - return currentState; + return currentState; } int FileTransferController::getProgress() const { - return ftProgressInfo ? ftProgressInfo->getPercentage() : 0; + return ftProgressInfo ? ftProgressInfo->getPercentage() : 0; } boost::uintmax_t FileTransferController::getSize() const { - if (transfer) { - return transfer->getFileSizeInBytes(); - } else { - return 0; - } + if (transfer) { + return transfer->getFileSizeInBytes(); + } else { + return 0; + } } void FileTransferController::start(std::string& description) { - SWIFT_LOG(debug) << "FileTransferController::start" << std::endl; - fileReadStream = boost::make_shared<FileReadBytestream>(boost::filesystem::path(filename)); - OutgoingFileTransfer::ref outgoingTransfer = ftManager->createOutgoingFileTransfer(otherParty, boost::filesystem::path(filename), description, fileReadStream); - if (outgoingTransfer) { - ftProgressInfo = new FileTransferProgressInfo(outgoingTransfer->getFileSizeInBytes()); - ftProgressInfo->onProgressPercentage.connect(boost::bind(&FileTransferController::handleProgressPercentageChange, this, _1)); - outgoingTransfer->onStateChanged.connect(boost::bind(&FileTransferController::handleFileTransferStateChange, this, _1)); - outgoingTransfer->onProcessedBytes.connect(boost::bind(&FileTransferProgressInfo::setBytesProcessed, ftProgressInfo, _1)); - outgoingTransfer->start(); - transfer = outgoingTransfer; - } else { - std::cerr << "File transfer not supported!" << std::endl; - } + SWIFT_LOG(debug) << "FileTransferController::start"; + fileReadStream = std::make_shared<FileReadBytestream>(boost::filesystem::path(filename)); + OutgoingFileTransfer::ref outgoingTransfer = ftManager->createOutgoingFileTransfer(otherParty, boost::filesystem::path(filename), description, fileReadStream); + if (outgoingTransfer) { + ftProgressInfo = new FileTransferProgressInfo(outgoingTransfer->getFileSizeInBytes()); + ftProgressInfo->onProgressPercentage.connect(boost::bind(&FileTransferController::handleProgressPercentageChange, this, _1)); + outgoingTransfer->onStateChanged.connect(boost::bind(&FileTransferController::handleFileTransferStateChange, this, _1)); + outgoingTransfer->onProcessedBytes.connect(boost::bind(&FileTransferProgressInfo::setBytesProcessed, ftProgressInfo, _1)); + outgoingTransfer->start(); + transfer = outgoingTransfer; + } else { + std::cerr << "File transfer not supported!" << std::endl; + } } void FileTransferController::accept(std::string& file) { - SWIFT_LOG(debug) << "FileTransferController::accept" << std::endl; - IncomingFileTransfer::ref incomingTransfer = boost::dynamic_pointer_cast<IncomingFileTransfer>(transfer); - if (incomingTransfer) { - fileWriteStream = boost::make_shared<FileWriteBytestream>(boost::filesystem::path(file)); + SWIFT_LOG(debug) << "FileTransferController::accept"; + IncomingFileTransfer::ref incomingTransfer = std::dynamic_pointer_cast<IncomingFileTransfer>(transfer); + if (incomingTransfer) { + fileWriteStream = std::make_shared<FileWriteBytestream>(boost::filesystem::path(file)); - ftProgressInfo = new FileTransferProgressInfo(transfer->getFileSizeInBytes()); - ftProgressInfo->onProgressPercentage.connect(boost::bind(&FileTransferController::handleProgressPercentageChange, this, _1)); - transfer->onStateChanged.connect(boost::bind(&FileTransferController::handleFileTransferStateChange, this, _1)); - transfer->onProcessedBytes.connect(boost::bind(&FileTransferProgressInfo::setBytesProcessed, ftProgressInfo, _1)); - incomingTransfer->accept(fileWriteStream); - } else { - std::cerr << "Expected an incoming transfer in this situation!" << std::endl; - } + ftProgressInfo = new FileTransferProgressInfo(transfer->getFileSizeInBytes()); + ftProgressInfo->onProgressPercentage.connect(boost::bind(&FileTransferController::handleProgressPercentageChange, this, _1)); + transfer->onProcessedBytes.connect(boost::bind(&FileTransferProgressInfo::setBytesProcessed, ftProgressInfo, _1)); + incomingTransfer->accept(fileWriteStream); + } else { + std::cerr << "Expected an incoming transfer in this situation!" << std::endl; + } } void FileTransferController::cancel() { - if (transfer) { - transfer->cancel(); - } else { - chatWindow->setFileTransferStatus(uiID, ChatWindow::Canceled); - } + if (transfer) { + transfer->cancel(); + } else { + chatWindow->setFileTransferStatus(uiID, ChatWindow::Canceled); + } } void FileTransferController::handleFileTransferStateChange(FileTransfer::State state) { - currentState = state; - onStateChage(); - switch(state.type) { - case FileTransfer::State::Initial: - assert(false); - return; - case FileTransfer::State::Negotiating: - chatWindow->setFileTransferStatus(uiID, ChatWindow::Negotiating); - return; - case FileTransfer::State::Transferring: - chatWindow->setFileTransferStatus(uiID, ChatWindow::Transferring); - return; - case FileTransfer::State::Canceled: - chatWindow->setFileTransferStatus(uiID, ChatWindow::Canceled); - return; - case FileTransfer::State::Finished: - chatWindow->setFileTransferStatus(uiID, ChatWindow::Finished); - if (fileWriteStream) { - fileWriteStream->close(); - } - return; - case FileTransfer::State::Failed: - chatWindow->setFileTransferStatus(uiID, ChatWindow::FTFailed); - return; - case FileTransfer::State::WaitingForAccept: - chatWindow->setFileTransferStatus(uiID, ChatWindow::WaitingForAccept); - return; - case FileTransfer::State::WaitingForStart: - return; - } - assert(false); + currentState = state; + onStateChanged(); + switch(state.type) { + case FileTransfer::State::Initial: + assert(false); + return; + case FileTransfer::State::Negotiating: + chatWindow->setFileTransferStatus(uiID, ChatWindow::Negotiating); + return; + case FileTransfer::State::Transferring: + chatWindow->setFileTransferStatus(uiID, ChatWindow::Transferring); + return; + case FileTransfer::State::Canceled: + chatWindow->setFileTransferStatus(uiID, ChatWindow::Canceled); + return; + case FileTransfer::State::Finished: + chatWindow->setFileTransferStatus(uiID, ChatWindow::Finished); + if (fileWriteStream) { + fileWriteStream->close(); + } + return; + case FileTransfer::State::Failed: + chatWindow->setFileTransferStatus(uiID, ChatWindow::FTFailed); + return; + case FileTransfer::State::WaitingForAccept: + chatWindow->setFileTransferStatus(uiID, ChatWindow::WaitingForAccept); + return; + case FileTransfer::State::WaitingForStart: + chatWindow->setFileTransferStatus(uiID, ChatWindow::Initialisation); + return; + } + assert(false); } void FileTransferController::handleProgressPercentageChange(int percentage) { - onProgressChange(); - chatWindow->setFileTransferProgress(uiID, percentage); + onProgressChange(); + chatWindow->setFileTransferProgress(uiID, percentage); } } diff --git a/Swift/Controllers/FileTransfer/FileTransferController.h b/Swift/Controllers/FileTransfer/FileTransferController.h index 3d6f7d5..e36cac8 100644 --- a/Swift/Controllers/FileTransfer/FileTransferController.h +++ b/Swift/Controllers/FileTransfer/FileTransferController.h @@ -4,18 +4,25 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ +/* + * Copyright (c) 2015-2017 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + #pragma once +#include <memory> #include <string> -#include <boost/shared_ptr.hpp> #include <boost/cstdint.hpp> -#include <Swiften/JID/JID.h> -#include <Swiften/FileTransfer/FileTransfer.h> -#include <Swiften/FileTransfer/IncomingFileTransfer.h> #include <Swiften/FileTransfer/FileReadBytestream.h> +#include <Swiften/FileTransfer/FileTransfer.h> #include <Swiften/FileTransfer/FileWriteBytestream.h> +#include <Swiften/FileTransfer/IncomingFileTransfer.h> +#include <Swiften/JID/JID.h> + #include <Swift/Controllers/FileTransfer/FileTransferProgressInfo.h> namespace Swift { @@ -25,49 +32,49 @@ class ChatWindow; class FileTransferController { public: - /** - * For outgoing file transfers. It'll create a file transfer via FileTransferManager as soon as the descriptive information is available. - */ - FileTransferController(const JID&, const std::string&, FileTransferManager*); + /** + * For outgoing file transfers. It'll create a file transfer via FileTransferManager as soon as the descriptive information is available. + */ + FileTransferController(const JID&, const std::string&, FileTransferManager*); + + /** + * For incoming file transfers. + */ + FileTransferController(IncomingFileTransfer::ref transfer); + ~FileTransferController(); - /** - * For incoming file transfers. - */ - FileTransferController(IncomingFileTransfer::ref transfer); - ~FileTransferController(); + std::string setChatWindow(ChatWindow*, const std::string& nickname, const std::string& avatarPath); + void setReceipient(const JID& otherParty); - std::string setChatWindow(ChatWindow*, std::string nickname); - void setReceipient(const JID& otherParty); + void start(std::string& description); + void accept(std::string& file); + void cancel(); - void start(std::string& description); - void accept(std::string& file); - void cancel(); + const JID &getOtherParty() const; + bool isIncoming() const; + FileTransfer::State getState() const; + int getProgress() const; + boost::uintmax_t getSize() const; - const JID &getOtherParty() const; - bool isIncoming() const; - FileTransfer::State getState() const; - int getProgress() const; - boost::uintmax_t getSize() const; + boost::signals2::signal<void ()> onStateChanged; + boost::signals2::signal<void ()> onProgressChange; - boost::signal<void ()> onStateChage; - boost::signal<void ()> onProgressChange; +private: + void handleFileTransferStateChange(FileTransfer::State); + void handleProgressPercentageChange(int percentage); private: - void handleFileTransferStateChange(FileTransfer::State); - void handleProgressPercentageChange(int percentage); - -private: - bool sending; - JID otherParty; - std::string filename; - FileTransfer::ref transfer; - boost::shared_ptr<FileReadBytestream> fileReadStream; - boost::shared_ptr<FileWriteBytestream> fileWriteStream; - FileTransferManager* ftManager; - FileTransferProgressInfo* ftProgressInfo; - ChatWindow* chatWindow; - std::string uiID; - FileTransfer::State currentState; + bool sending; + JID otherParty; + std::string filename; + FileTransfer::ref transfer; + std::shared_ptr<FileReadBytestream> fileReadStream; + std::shared_ptr<FileWriteBytestream> fileWriteStream; + FileTransferManager* ftManager; + FileTransferProgressInfo* ftProgressInfo; + ChatWindow* chatWindow; + std::string uiID; + FileTransfer::State currentState; }; } diff --git a/Swift/Controllers/FileTransfer/FileTransferOverview.cpp b/Swift/Controllers/FileTransfer/FileTransferOverview.cpp index 2a8c319..fcc35e4 100644 --- a/Swift/Controllers/FileTransfer/FileTransferOverview.cpp +++ b/Swift/Controllers/FileTransfer/FileTransferOverview.cpp @@ -4,41 +4,88 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ -#include "FileTransferOverview.h" +/* + * Copyright (c) 2015-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#include <Swift/Controllers/FileTransfer/FileTransferOverview.h> #include <boost/bind.hpp> #include <boost/filesystem.hpp> -#include <Swiften/Base/boost_bsignals.h> +#include <boost/signals2.hpp> +#include <Swiften/Base/Log.h> #include <Swiften/FileTransfer/FileTransferManager.h> namespace Swift { FileTransferOverview::FileTransferOverview(FileTransferManager* ftm) : fileTransferManager(ftm) { - fileTransferManager->onIncomingFileTransfer.connect(boost::bind(&FileTransferOverview::handleIncomingFileTransfer, this, _1)); + fileTransferManager->onIncomingFileTransfer.connect(boost::bind(&FileTransferOverview::handleIncomingFileTransfer, this, _1)); + onNewFileTransferController.connect(boost::bind(&FileTransferOverview::handleNewFileTransferController, this, _1)); } FileTransferOverview::~FileTransferOverview() { - + onNewFileTransferController.disconnect(boost::bind(&FileTransferOverview::handleNewFileTransferController, this, _1)); + fileTransferManager->onIncomingFileTransfer.disconnect(boost::bind(&FileTransferOverview::handleIncomingFileTransfer, this, _1)); + for (auto controller : fileTransfers) { + controller->onStateChanged.disconnect(boost::bind(&FileTransferOverview::handleFileTransferStateChanged, this)); + } } - -void FileTransferOverview::sendFile(const JID& jid, const std::string& filename) { - if (boost::filesystem::exists(filename) && boost::filesystem::file_size(filename) > 0) { - FileTransferController* controller = new FileTransferController(jid, filename, fileTransferManager); - fileTransfers.push_back(controller); - onNewFileTransferController(controller); - } +void FileTransferOverview::sendFile(const JID& jid, const std::string& filename) { + if (boost::filesystem::exists(filename) && boost::filesystem::file_size(filename) > 0) { + FileTransferController* controller = new FileTransferController(jid, filename, fileTransferManager); + onNewFileTransferController(controller); + } } void FileTransferOverview::handleIncomingFileTransfer(IncomingFileTransfer::ref transfer) { - FileTransferController* controller = new FileTransferController(transfer); - fileTransfers.push_back(controller); - onNewFileTransferController(controller); + FileTransferController* controller = new FileTransferController(transfer); + onNewFileTransferController(controller); +} + +void FileTransferOverview::handleNewFileTransferController(FileTransferController* controller) { + fileTransfers.push_back(controller); + controller->onStateChanged.connect(boost::bind(&FileTransferOverview::handleFileTransferStateChanged, this)); +} + +void FileTransferOverview::handleFileTransferStateChanged() { + onFileTransferListChanged(); } const std::vector<FileTransferController*>& FileTransferOverview::getFileTransfers() const { - return fileTransfers; + return fileTransfers; +} + +void FileTransferOverview::clearFinished() { + for (std::vector<FileTransferController*>::iterator it = fileTransfers.begin(); it != fileTransfers.end(); ) { + if((*it)->getState().type == FileTransfer::State::Finished + || (*it)->getState().type == FileTransfer::State::Failed + || (*it)->getState().type == FileTransfer::State::Canceled) { + FileTransferController* controller = *it; + it = fileTransfers.erase(it); + controller->onStateChanged.disconnect(boost::bind(&FileTransferOverview::handleFileTransferStateChanged, this)); + delete controller; + } else { + ++it; + } + } + onFileTransferListChanged(); +} + +bool FileTransferOverview::isClearable() const { + bool isClearable = false; + for (auto controller : fileTransfers) { + if(controller->getState().type == FileTransfer::State::Finished + || controller->getState().type == FileTransfer::State::Failed + || controller->getState().type == FileTransfer::State::Canceled) { + isClearable = true; + break; + } + } + return isClearable; } } diff --git a/Swift/Controllers/FileTransfer/FileTransferOverview.h b/Swift/Controllers/FileTransfer/FileTransferOverview.h index 716666a..c311cb7 100644 --- a/Swift/Controllers/FileTransfer/FileTransferOverview.h +++ b/Swift/Controllers/FileTransfer/FileTransferOverview.h @@ -4,13 +4,19 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ +/* + * Copyright (c) 2015-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + #pragma once #include <vector> -#include "Swift/Controllers/FileTransfer/FileTransferController.h" +#include <boost/signals2.hpp> -#include <Swiften/Base/boost_bsignals.h> +#include <Swift/Controllers/FileTransfer/FileTransferController.h> namespace Swift { @@ -19,20 +25,25 @@ class FileTransferManager; class FileTransferOverview { public: - FileTransferOverview(FileTransferManager*); - ~FileTransferOverview(); - - void sendFile(const JID&, const std::string&); - const std::vector<FileTransferController*>& getFileTransfers() const; + FileTransferOverview(FileTransferManager*); + ~FileTransferOverview(); + + void sendFile(const JID&, const std::string&); + const std::vector<FileTransferController*>& getFileTransfers() const; + void clearFinished(); + bool isClearable() const; - boost::signal<void (FileTransferController*)> onNewFileTransferController; + boost::signals2::signal<void (FileTransferController*)> onNewFileTransferController; + boost::signals2::signal<void ()> onFileTransferListChanged; private: - void handleIncomingFileTransfer(IncomingFileTransfer::ref transfer); + void handleIncomingFileTransfer(IncomingFileTransfer::ref transfer); + void handleNewFileTransferController(FileTransferController* controller); + void handleFileTransferStateChanged(); private: - std::vector<FileTransferController*> fileTransfers; - FileTransferManager *fileTransferManager; + std::vector<FileTransferController*> fileTransfers; + FileTransferManager *fileTransferManager; }; } diff --git a/Swift/Controllers/FileTransfer/FileTransferProgressInfo.cpp b/Swift/Controllers/FileTransfer/FileTransferProgressInfo.cpp index 3081f71..eddace9 100644 --- a/Swift/Controllers/FileTransfer/FileTransferProgressInfo.cpp +++ b/Swift/Controllers/FileTransfer/FileTransferProgressInfo.cpp @@ -4,7 +4,13 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ -#include "FileTransferProgressInfo.h" +/* + * Copyright (c) 2016-2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#include <Swift/Controllers/FileTransfer/FileTransferProgressInfo.h> #include <boost/numeric/conversion/cast.hpp> @@ -12,22 +18,22 @@ namespace Swift { -FileTransferProgressInfo::FileTransferProgressInfo(boost::uintmax_t completeBytes) : completeBytes(completeBytes), completedBytes(0), percentage(0) { - onProgressPercentage(0); +FileTransferProgressInfo::FileTransferProgressInfo(size_t completeBytes) : completeBytes(completeBytes), completedBytes(0), percentage(0) { + onProgressPercentage(0); } -void FileTransferProgressInfo::setBytesProcessed(int processedBytes) { - int oldPercentage = int(double(completedBytes) / double(completeBytes) * 100.0); - completedBytes += boost::numeric_cast<boost::uintmax_t>(processedBytes); - int newPercentage = int(double(completedBytes) / double(completeBytes) * 100.0); - if (oldPercentage != newPercentage) { - onProgressPercentage(newPercentage); - } - percentage = newPercentage; +void FileTransferProgressInfo::setBytesProcessed(size_t processedBytes) { + int oldPercentage = int(double(completedBytes) / double(completeBytes) * 100.0); + completedBytes += processedBytes; + int newPercentage = int(double(completedBytes) / double(completeBytes) * 100.0); + if (oldPercentage != newPercentage) { + onProgressPercentage(newPercentage); + } + percentage = newPercentage; } int FileTransferProgressInfo::getPercentage() const { - return percentage; + return percentage; } } diff --git a/Swift/Controllers/FileTransfer/FileTransferProgressInfo.h b/Swift/Controllers/FileTransfer/FileTransferProgressInfo.h index e324e33..869ceba 100644 --- a/Swift/Controllers/FileTransfer/FileTransferProgressInfo.h +++ b/Swift/Controllers/FileTransfer/FileTransferProgressInfo.h @@ -4,27 +4,34 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ +/* + * Copyright (c) 2016-2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + #pragma once -#include <Swiften/Base/boost_bsignals.h> -#include <boost/cstdint.hpp> +#include <cstddef> + +#include <boost/signals2.hpp> namespace Swift { class FileTransferProgressInfo { public: - FileTransferProgressInfo(boost::uintmax_t completeBytes); + FileTransferProgressInfo(size_t completeBytes); public: - void setBytesProcessed(int processedBytes); + void setBytesProcessed(size_t processedBytes); - int getPercentage() const; - boost::signal<void (int)> onProgressPercentage; + int getPercentage() const; + boost::signals2::signal<void (int)> onProgressPercentage; private: - boost::uintmax_t completeBytes; - boost::uintmax_t completedBytes; - int percentage; + size_t completeBytes; + size_t completedBytes; + int percentage; }; } diff --git a/Swift/Controllers/FileTransferListController.cpp b/Swift/Controllers/FileTransferListController.cpp index 093a3c4..4f85b81 100644 --- a/Swift/Controllers/FileTransferListController.cpp +++ b/Swift/Controllers/FileTransferListController.cpp @@ -4,42 +4,48 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ -#include "Swift/Controllers/FileTransferListController.h" +/* + * Copyright (c) 2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#include <Swift/Controllers/FileTransferListController.h> #include <boost/bind.hpp> -#include "Swift/Controllers/UIInterfaces/FileTransferListWidgetFactory.h" -#include "Swift/Controllers/UIEvents/RequestFileTransferListUIEvent.h" +#include <Swift/Controllers/UIEvents/RequestFileTransferListUIEvent.h> +#include <Swift/Controllers/UIInterfaces/FileTransferListWidgetFactory.h> namespace Swift { -FileTransferListController::FileTransferListController(UIEventStream* uiEventStream, FileTransferListWidgetFactory* fileTransferListWidgetFactory) : fileTransferListWidgetFactory(fileTransferListWidgetFactory), fileTransferListWidget(NULL), fileTransferOverview(0) { - uiEventStream->onUIEvent.connect(boost::bind(&FileTransferListController::handleUIEvent, this, _1)); +FileTransferListController::FileTransferListController(UIEventStream* uiEventStream, FileTransferListWidgetFactory* fileTransferListWidgetFactory) : fileTransferListWidgetFactory(fileTransferListWidgetFactory), fileTransferListWidget(nullptr), fileTransferOverview(nullptr) { + uiEventStream->onUIEvent.connect(boost::bind(&FileTransferListController::handleUIEvent, this, _1)); } FileTransferListController::~FileTransferListController() { - delete fileTransferListWidget; + delete fileTransferListWidget; } void FileTransferListController::setFileTransferOverview(FileTransferOverview *overview) { - fileTransferOverview = overview; - if (fileTransferListWidget) { - fileTransferListWidget->setFileTransferOverview(fileTransferOverview); - } + fileTransferOverview = overview; + if (fileTransferListWidget) { + fileTransferListWidget->setFileTransferOverview(fileTransferOverview); + } } -void FileTransferListController::handleUIEvent(boost::shared_ptr<UIEvent> rawEvent) { - boost::shared_ptr<RequestFileTransferListUIEvent> event = boost::dynamic_pointer_cast<RequestFileTransferListUIEvent>(rawEvent); - if (event != NULL) { - if (fileTransferListWidget == NULL) { - fileTransferListWidget = fileTransferListWidgetFactory->createFileTransferListWidget(); - if (fileTransferOverview) { - fileTransferListWidget->setFileTransferOverview(fileTransferOverview); - } - } - fileTransferListWidget->show(); - fileTransferListWidget->activate(); - } +void FileTransferListController::handleUIEvent(std::shared_ptr<UIEvent> rawEvent) { + std::shared_ptr<RequestFileTransferListUIEvent> event = std::dynamic_pointer_cast<RequestFileTransferListUIEvent>(rawEvent); + if (event != nullptr) { + if (fileTransferListWidget == nullptr) { + fileTransferListWidget = fileTransferListWidgetFactory->createFileTransferListWidget(); + if (fileTransferOverview) { + fileTransferListWidget->setFileTransferOverview(fileTransferOverview); + } + } + fileTransferListWidget->show(); + fileTransferListWidget->activate(); + } } } diff --git a/Swift/Controllers/FileTransferListController.h b/Swift/Controllers/FileTransferListController.h index c5c8893..832a99c 100644 --- a/Swift/Controllers/FileTransferListController.h +++ b/Swift/Controllers/FileTransferListController.h @@ -4,11 +4,17 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ +/* + * Copyright (c) 2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + #pragma once -#include <boost/shared_ptr.hpp> +#include <memory> -#include "Swift/Controllers/UIEvents/UIEventStream.h" +#include <Swift/Controllers/UIEvents/UIEventStream.h> namespace Swift { @@ -18,18 +24,18 @@ class FileTransferOverview; class FileTransferListController { public: - FileTransferListController(UIEventStream* uiEventStream, FileTransferListWidgetFactory* fileTransferListWidgetFactory); - ~FileTransferListController(); + FileTransferListController(UIEventStream* uiEventStream, FileTransferListWidgetFactory* fileTransferListWidgetFactory); + ~FileTransferListController(); - void setFileTransferOverview(FileTransferOverview* overview); + void setFileTransferOverview(FileTransferOverview* overview); private: - void handleUIEvent(boost::shared_ptr<UIEvent> event); + void handleUIEvent(std::shared_ptr<UIEvent> event); private: - FileTransferListWidgetFactory* fileTransferListWidgetFactory; - FileTransferListWidget* fileTransferListWidget; - FileTransferOverview* fileTransferOverview; + FileTransferListWidgetFactory* fileTransferListWidgetFactory; + FileTransferListWidget* fileTransferListWidget; + FileTransferOverview* fileTransferOverview; }; } diff --git a/Swift/Controllers/HighlightAction.cpp b/Swift/Controllers/HighlightAction.cpp deleted file mode 100644 index d4d199d..0000000 --- a/Swift/Controllers/HighlightAction.cpp +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (c) 2012 Maciej Niedzielski - * Licensed under the simplified BSD license. - * See Documentation/Licenses/BSD-simplified.txt for more information. - */ - -#include <Swift/Controllers/HighlightAction.h> - -namespace Swift { - -void HighlightAction::setHighlightText(bool highlightText) -{ - highlightText_ = highlightText; - if (!highlightText_) { - textColor_.clear(); - textBackground_.clear(); - } -} - -void HighlightAction::setPlaySound(bool playSound) -{ - playSound_ = playSound; - if (!playSound_) { - soundFile_.clear(); - } -} - -} diff --git a/Swift/Controllers/HighlightAction.h b/Swift/Controllers/HighlightAction.h deleted file mode 100644 index bfbed74..0000000 --- a/Swift/Controllers/HighlightAction.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (c) 2012 Maciej Niedzielski - * Licensed under the simplified BSD license. - * See Documentation/Licenses/BSD-simplified.txt for more information. - */ - -#pragma once - -#include <string> - -namespace Swift { - - class HighlightRule; - - class HighlightAction { - public: - HighlightAction() : highlightText_(false), playSound_(false) {} - - bool highlightText() const { return highlightText_; } - void setHighlightText(bool highlightText); - - const std::string& getTextColor() const { return textColor_; } - void setTextColor(const std::string& textColor) { textColor_ = textColor; } - - const std::string& getTextBackground() const { return textBackground_; } - void setTextBackground(const std::string& textBackground) { textBackground_ = textBackground; } - - bool playSound() const { return playSound_; } - void setPlaySound(bool playSound); - - const std::string& getSoundFile() const { return soundFile_; } - void setSoundFile(const std::string& soundFile) { soundFile_ = soundFile; } - - bool isEmpty() const { return !highlightText_ && !playSound_; } - - private: - bool highlightText_; - std::string textColor_; - std::string textBackground_; - - bool playSound_; - std::string soundFile_; - }; - -} diff --git a/Swift/Controllers/HighlightEditorController.cpp b/Swift/Controllers/HighlightEditorController.cpp deleted file mode 100644 index 899e4bb..0000000 --- a/Swift/Controllers/HighlightEditorController.cpp +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2012 Maciej Niedzielski - * Licensed under the simplified BSD license. - * See Documentation/Licenses/BSD-simplified.txt for more information. - */ - -#include <boost/bind.hpp> - -#include <Swift/Controllers/HighlightEditorController.h> -#include <Swift/Controllers/UIInterfaces/HighlightEditorWidget.h> -#include <Swift/Controllers/UIInterfaces/HighlightEditorWidgetFactory.h> -#include <Swift/Controllers/UIEvents/RequestHighlightEditorUIEvent.h> -#include <Swift/Controllers/UIEvents/UIEventStream.h> - -namespace Swift { - -HighlightEditorController::HighlightEditorController(UIEventStream* uiEventStream, HighlightEditorWidgetFactory* highlightEditorWidgetFactory, HighlightManager* highlightManager) : highlightEditorWidgetFactory_(highlightEditorWidgetFactory), highlightEditorWidget_(NULL), highlightManager_(highlightManager) -{ - uiEventStream->onUIEvent.connect(boost::bind(&HighlightEditorController::handleUIEvent, this, _1)); -} - -HighlightEditorController::~HighlightEditorController() -{ - delete highlightEditorWidget_; - highlightEditorWidget_ = NULL; -} - -void HighlightEditorController::handleUIEvent(boost::shared_ptr<UIEvent> rawEvent) -{ - boost::shared_ptr<RequestHighlightEditorUIEvent> event = boost::dynamic_pointer_cast<RequestHighlightEditorUIEvent>(rawEvent); - if (event) { - if (!highlightEditorWidget_) { - highlightEditorWidget_ = highlightEditorWidgetFactory_->createHighlightEditorWidget(); - highlightEditorWidget_->setHighlightManager(highlightManager_); - } - highlightEditorWidget_->show(); - } -} - -} diff --git a/Swift/Controllers/HighlightEditorController.h b/Swift/Controllers/HighlightEditorController.h deleted file mode 100644 index 3868251..0000000 --- a/Swift/Controllers/HighlightEditorController.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (c) 2012 Maciej Niedzielski - * Licensed under the simplified BSD license. - * See Documentation/Licenses/BSD-simplified.txt for more information. - */ - -#pragma once - -#include <boost/shared_ptr.hpp> - -#include <Swift/Controllers/UIEvents/UIEvent.h> - -namespace Swift { - - class UIEventStream; - - class HighlightEditorWidgetFactory; - class HighlightEditorWidget; - - class HighlightManager; - - class HighlightEditorController { - public: - HighlightEditorController(UIEventStream* uiEventStream, HighlightEditorWidgetFactory* highlightEditorWidgetFactory, HighlightManager* highlightManager); - ~HighlightEditorController(); - - HighlightManager* getHighlightManager() const { return highlightManager_; } - - private: - void handleUIEvent(boost::shared_ptr<UIEvent> event); - - private: - HighlightEditorWidgetFactory* highlightEditorWidgetFactory_; - HighlightEditorWidget* highlightEditorWidget_; - HighlightManager* highlightManager_; - }; - -} diff --git a/Swift/Controllers/HighlightManager.cpp b/Swift/Controllers/HighlightManager.cpp deleted file mode 100644 index 7ab578e..0000000 --- a/Swift/Controllers/HighlightManager.cpp +++ /dev/null @@ -1,139 +0,0 @@ -/* - * Copyright (c) 2012 Maciej Niedzielski - * Licensed under the simplified BSD license. - * See Documentation/Licenses/BSD-simplified.txt for more information. - */ - -#include <cassert> - -#include <boost/algorithm/string.hpp> -#include <boost/regex.hpp> -#include <boost/bind.hpp> -#include <boost/numeric/conversion/cast.hpp> - -#include <Swiften/Base/foreach.h> -#include <Swift/Controllers/HighlightManager.h> -#include <Swift/Controllers/Highlighter.h> -#include <Swift/Controllers/Settings/SettingsProvider.h> -#include <Swift/Controllers/SettingConstants.h> - -/* How does highlighting work? - * - * HighlightManager manages a list of if-then rules used to highlight messages. - * Rule is represented by HighlightRule. Action ("then" part) is HighlightAction. - * - * - * HighlightManager is also used as a factory for Highlighter objects. - * Each ChatControllerBase has its own Highlighter. - * Highligher may be customized by using setNick(), etc. - * - * ChatControllerBase passes incoming messages to Highlighter and gets HighlightAction in return - * (first matching rule is returned). - * If needed, HighlightAction is then passed back to Highlighter for further handling. - * This results in HighlightManager emiting onHighlight event, - * which is handled by SoundController to play sound notification - */ - -namespace Swift { - -HighlightManager::HighlightManager(SettingsProvider* settings) - : settings_(settings) - , storingSettings_(false) -{ - loadSettings(); - settings_->onSettingChanged.connect(boost::bind(&HighlightManager::handleSettingChanged, this, _1)); -} - -void HighlightManager::handleSettingChanged(const std::string& settingPath) -{ - if (!storingSettings_ && SettingConstants::HIGHLIGHT_RULES.getKey() == settingPath) { - loadSettings(); - } -} - -void HighlightManager::loadSettings() -{ - std::string highlightRules = settings_->getSetting(SettingConstants::HIGHLIGHT_RULES); - if (highlightRules == "@") { - rules_ = getDefaultRules(); - } else { - rules_ = rulesFromString(highlightRules); - } -} - -std::string HighlightManager::rulesToString() const -{ - std::string s; - foreach (HighlightRule r, rules_) { - s += r.toString() + '\f'; - } - if (s.size()) { - s.erase(s.end() - 1); - } - return s; -} - -std::vector<HighlightRule> HighlightManager::rulesFromString(const std::string& rulesString) -{ - std::vector<HighlightRule> rules; - std::string s(rulesString); - typedef boost::split_iterator<std::string::iterator> split_iterator; - for (split_iterator it = boost::make_split_iterator(s, boost::first_finder("\f")); it != split_iterator(); ++it) { - HighlightRule r = HighlightRule::fromString(boost::copy_range<std::string>(*it)); - if (!r.isEmpty()) { - rules.push_back(r); - } - } - return rules; -} - -std::vector<HighlightRule> HighlightManager::getDefaultRules() -{ - std::vector<HighlightRule> rules; - HighlightRule r; - r.setMatchChat(true); - r.getAction().setPlaySound(true); - rules.push_back(r); - return rules; -} - -void HighlightManager::storeSettings() -{ - storingSettings_ = true; // don't reload settings while saving - settings_->storeSetting(SettingConstants::HIGHLIGHT_RULES, rulesToString()); - storingSettings_ = false; -} - -HighlightRule HighlightManager::getRule(int index) const -{ - assert(index >= 0 && static_cast<size_t>(index) < rules_.size()); - return rules_[static_cast<size_t>(index)]; -} - -void HighlightManager::setRule(int index, const HighlightRule& rule) -{ - assert(index >= 0 && static_cast<size_t>(index) < rules_.size()); - rules_[static_cast<size_t>(index)] = rule; - storeSettings(); -} - -void HighlightManager::insertRule(int index, const HighlightRule& rule) -{ - assert(index >= 0 && boost::numeric_cast<std::vector<std::string>::size_type>(index) <= rules_.size()); - rules_.insert(rules_.begin() + index, rule); - storeSettings(); -} - -void HighlightManager::removeRule(int index) -{ - assert(index >= 0 && boost::numeric_cast<std::vector<std::string>::size_type>(index) < rules_.size()); - rules_.erase(rules_.begin() + index); - storeSettings(); -} - -Highlighter* HighlightManager::createHighlighter() -{ - return new Highlighter(this); -} - -} diff --git a/Swift/Controllers/HighlightManager.h b/Swift/Controllers/HighlightManager.h deleted file mode 100644 index d195d05..0000000 --- a/Swift/Controllers/HighlightManager.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (c) 2012 Maciej Niedzielski - * Licensed under the simplified BSD license. - * See Documentation/Licenses/BSD-simplified.txt for more information. - */ - -#pragma once - -#include <vector> -#include <string> - -#include <Swiften/Base/boost_bsignals.h> -#include <Swift/Controllers/HighlightRule.h> - -namespace Swift { - - class SettingsProvider; - class Highlighter; - - class HighlightManager { - public: - HighlightManager(SettingsProvider* settings); - - Highlighter* createHighlighter(); - - const std::vector<HighlightRule>& getRules() const { return rules_; } - HighlightRule getRule(int index) const; - void setRule(int index, const HighlightRule& rule); - void insertRule(int index, const HighlightRule& rule); - void removeRule(int index); - - boost::signal<void (const HighlightAction&)> onHighlight; - - private: - void handleSettingChanged(const std::string& settingPath); - - std::string rulesToString() const; - static std::vector<HighlightRule> rulesFromString(const std::string&); - static std::vector<HighlightRule> getDefaultRules(); - - SettingsProvider* settings_; - bool storingSettings_; - void storeSettings(); - void loadSettings(); - - std::vector<HighlightRule> rules_; - }; - -} diff --git a/Swift/Controllers/HighlightRule.cpp b/Swift/Controllers/HighlightRule.cpp deleted file mode 100644 index 9ca7d86..0000000 --- a/Swift/Controllers/HighlightRule.cpp +++ /dev/null @@ -1,192 +0,0 @@ -/* - * Copyright (c) 2012 Maciej Niedzielski - * Licensed under the simplified BSD license. - * See Documentation/Licenses/BSD-simplified.txt for more information. - */ - -#include <algorithm> -#include <boost/algorithm/string.hpp> -#include <boost/lambda/lambda.hpp> - -#include <Swiften/Base/foreach.h> -#include <Swiften/Base/Regex.h> -#include <Swift/Controllers/HighlightRule.h> - -namespace Swift { - -HighlightRule::HighlightRule() - : nickIsKeyword_(false) - , matchCase_(false) - , matchWholeWords_(false) - , matchChat_(false) - , matchMUC_(false) -{ -} - -boost::regex HighlightRule::regexFromString(const std::string & s) const -{ - std::string escaped = Regex::escape(s); - std::string word = matchWholeWords_ ? "\\b" : ""; - boost::regex::flag_type flags = boost::regex::normal; - if (!matchCase_) { - flags |= boost::regex::icase; - } - return boost::regex(word + escaped + word, flags); -} - -void HighlightRule::updateRegex() const -{ - keywordRegex_.clear(); - foreach (const std::string & k, keywords_) { - keywordRegex_.push_back(regexFromString(k)); - } - senderRegex_.clear(); - foreach (const std::string & s, senders_) { - senderRegex_.push_back(regexFromString(s)); - } -} - -std::string HighlightRule::boolToString(bool b) -{ - return b ? "1" : "0"; -} - -bool HighlightRule::boolFromString(const std::string& s) -{ - return s == "1"; -} - -std::string HighlightRule::toString() const -{ - std::vector<std::string> v; - v.push_back(boost::join(senders_, "\t")); - v.push_back(boost::join(keywords_, "\t")); - v.push_back(boolToString(nickIsKeyword_)); - v.push_back(boolToString(matchChat_)); - v.push_back(boolToString(matchMUC_)); - v.push_back(boolToString(matchCase_)); - v.push_back(boolToString(matchWholeWords_)); - v.push_back(boolToString(action_.highlightText())); - v.push_back(action_.getTextColor()); - v.push_back(action_.getTextBackground()); - v.push_back(boolToString(action_.playSound())); - v.push_back(action_.getSoundFile()); - return boost::join(v, "\n"); -} - -HighlightRule HighlightRule::fromString(const std::string& s) -{ - std::vector<std::string> v; - boost::split(v, s, boost::is_any_of("\n")); - - HighlightRule r; - size_t i = 0; - try { - boost::split(r.senders_, v.at(i++), boost::is_any_of("\t")); - r.senders_.erase(std::remove_if(r.senders_.begin(), r.senders_.end(), boost::lambda::_1 == ""), r.senders_.end()); - boost::split(r.keywords_, v.at(i++), boost::is_any_of("\t")); - r.keywords_.erase(std::remove_if(r.keywords_.begin(), r.keywords_.end(), boost::lambda::_1 == ""), r.keywords_.end()); - r.nickIsKeyword_ = boolFromString(v.at(i++)); - r.matchChat_ = boolFromString(v.at(i++)); - r.matchMUC_ = boolFromString(v.at(i++)); - r.matchCase_ = boolFromString(v.at(i++)); - r.matchWholeWords_ = boolFromString(v.at(i++)); - r.action_.setHighlightText(boolFromString(v.at(i++))); - r.action_.setTextColor(v.at(i++)); - r.action_.setTextBackground(v.at(i++)); - r.action_.setPlaySound(boolFromString(v.at(i++))); - r.action_.setSoundFile(v.at(i++)); - } - catch (std::out_of_range) { - // this may happen if more properties are added to HighlightRule object, etc... - // in such case, default values (set by default constructor) will be used - } - - r.updateRegex(); - - return r; -} - -bool HighlightRule::isMatch(const std::string& body, const std::string& sender, const std::string& nick, MessageType messageType) const -{ - if ((messageType == HighlightRule::ChatMessage && matchChat_) || (messageType == HighlightRule::MUCMessage && matchMUC_)) { - - bool matchesKeyword = keywords_.empty() && (nick.empty() || !nickIsKeyword_); - bool matchesSender = senders_.empty(); - - foreach (const boost::regex & rx, keywordRegex_) { - if (boost::regex_search(body, rx)) { - matchesKeyword = true; - break; - } - } - - if (!matchesKeyword && nickIsKeyword_ && !nick.empty()) { - if (boost::regex_search(body, regexFromString(nick))) { - matchesKeyword = true; - } - } - - foreach (const boost::regex & rx, senderRegex_) { - if (boost::regex_search(sender, rx)) { - matchesSender = true; - break; - } - } - - if (matchesKeyword && matchesSender) { - return true; - } - } - - return false; -} - -void HighlightRule::setSenders(const std::vector<std::string>& senders) -{ - senders_ = senders; - updateRegex(); -} - -void HighlightRule::setKeywords(const std::vector<std::string>& keywords) -{ - keywords_ = keywords; - updateRegex(); -} - -void HighlightRule::setNickIsKeyword(bool nickIsKeyword) -{ - nickIsKeyword_ = nickIsKeyword; - updateRegex(); -} - -void HighlightRule::setMatchCase(bool matchCase) -{ - matchCase_ = matchCase; - updateRegex(); -} - -void HighlightRule::setMatchWholeWords(bool matchWholeWords) -{ - matchWholeWords_ = matchWholeWords; - updateRegex(); -} - -void HighlightRule::setMatchChat(bool matchChat) -{ - matchChat_ = matchChat; - updateRegex(); -} - -void HighlightRule::setMatchMUC(bool matchMUC) -{ - matchMUC_ = matchMUC; - updateRegex(); -} - -bool HighlightRule::isEmpty() const -{ - return senders_.empty() && keywords_.empty() && !nickIsKeyword_ && !matchChat_ && !matchMUC_ && action_.isEmpty(); -} - -} diff --git a/Swift/Controllers/HighlightRule.h b/Swift/Controllers/HighlightRule.h deleted file mode 100644 index 1abfa5a..0000000 --- a/Swift/Controllers/HighlightRule.h +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Copyright (c) 2012 Maciej Niedzielski - * Licensed under the simplified BSD license. - * See Documentation/Licenses/BSD-simplified.txt for more information. - */ - -#pragma once - -#include <vector> -#include <string> - -#include <boost/regex.hpp> - -#include <Swift/Controllers/HighlightAction.h> - -namespace Swift { - - class HighlightRule { - public: - HighlightRule(); - - enum MessageType { ChatMessage, MUCMessage }; - - bool isMatch(const std::string& body, const std::string& sender, const std::string& nick, MessageType) const; - - const HighlightAction& getAction() const { return action_; } - HighlightAction& getAction() { return action_; } - - static HighlightRule fromString(const std::string&); - std::string toString() const; - - const std::vector<std::string>& getSenders() const { return senders_; } - void setSenders(const std::vector<std::string>&); - - const std::vector<std::string>& getKeywords() const { return keywords_; } - void setKeywords(const std::vector<std::string>&); - - bool getNickIsKeyword() const { return nickIsKeyword_; } - void setNickIsKeyword(bool); - - bool getMatchCase() const { return matchCase_; } - void setMatchCase(bool); - - bool getMatchWholeWords() const { return matchWholeWords_; } - void setMatchWholeWords(bool); - - bool getMatchChat() const { return matchChat_; } - void setMatchChat(bool); - - bool getMatchMUC() const { return matchMUC_; } - void setMatchMUC(bool); - - bool isEmpty() const; - - private: - static std::string boolToString(bool); - static bool boolFromString(const std::string&); - - std::vector<std::string> senders_; - std::vector<std::string> keywords_; - bool nickIsKeyword_; - - mutable std::vector<boost::regex> senderRegex_; - mutable std::vector<boost::regex> keywordRegex_; - void updateRegex() const; - boost::regex regexFromString(const std::string&) const; - - bool matchCase_; - bool matchWholeWords_; - - bool matchChat_; - bool matchMUC_; - - HighlightAction action_; - }; - -} diff --git a/Swift/Controllers/Highlighter.cpp b/Swift/Controllers/Highlighter.cpp deleted file mode 100644 index 754641a..0000000 --- a/Swift/Controllers/Highlighter.cpp +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2012 Maciej Niedzielski - * Licensed under the simplified BSD license. - * See Documentation/Licenses/BSD-simplified.txt for more information. - */ - -#include <Swiften/Base/foreach.h> -#include <Swift/Controllers/Highlighter.h> -#include <Swift/Controllers/HighlightManager.h> - -namespace Swift { - -Highlighter::Highlighter(HighlightManager* manager) - : manager_(manager) -{ - setMode(ChatMode); -} - -void Highlighter::setMode(Mode mode) -{ - mode_ = mode; - messageType_ = mode_ == ChatMode ? HighlightRule::ChatMessage : HighlightRule::MUCMessage; -} - -HighlightAction Highlighter::findAction(const std::string& body, const std::string& sender) const -{ - foreach (const HighlightRule & r, manager_->getRules()) { - if (r.isMatch(body, sender, nick_, messageType_)) { - return r.getAction(); - } - } - - return HighlightAction(); -} - -void Highlighter::handleHighlightAction(const HighlightAction& action) -{ - manager_->onHighlight(action); -} - -} diff --git a/Swift/Controllers/Highlighter.h b/Swift/Controllers/Highlighter.h deleted file mode 100644 index d026f29..0000000 --- a/Swift/Controllers/Highlighter.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (c) 2012 Maciej Niedzielski - * Licensed under the simplified BSD license. - * See Documentation/Licenses/BSD-simplified.txt for more information. - */ - -#pragma once - -#include <string> - -#include <Swift/Controllers/HighlightRule.h> - -namespace Swift { - - class HighlightManager; - - class Highlighter { - public: - Highlighter(HighlightManager* manager); - - enum Mode { ChatMode, MUCMode }; - void setMode(Mode mode); - - void setNick(const std::string& nick) { nick_ = nick; } - - HighlightAction findAction(const std::string& body, const std::string& sender) const; - - void handleHighlightAction(const HighlightAction& action); - - private: - HighlightManager* manager_; - Mode mode_; - HighlightRule::MessageType messageType_; - std::string nick_; - }; - -} diff --git a/Swift/Controllers/Highlighting/HighlightAction.cpp b/Swift/Controllers/Highlighting/HighlightAction.cpp new file mode 100644 index 0000000..60ace42 --- /dev/null +++ b/Swift/Controllers/Highlighting/HighlightAction.cpp @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2012 Maciej Niedzielski + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +/* + * Copyright (c) 2015-2017 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#include <Swift/Controllers/Highlighting/HighlightAction.h> + +BOOST_CLASS_VERSION(Swift::HighlightAction, 1) + +namespace Swift { + + + +bool operator ==(const HighlightAction& a, const HighlightAction& b) { + if (a.getFrontColor() != b.getFrontColor()) { + return false; + } + if (a.getBackColor() != b.getBackColor()) { + return false; + } + if (a.getSoundFilePath() != b.getSoundFilePath()) { + return false; + } + if (a.isSystemNotificationEnabled() != b.isSystemNotificationEnabled()) { + return false; + } + return true; +} + +bool operator !=(const HighlightAction& a, const HighlightAction& b) { + return !(a == b); +} + +void HighlightAction::setFrontColor(const boost::optional<std::string>& frontColor) { + frontColor_ = frontColor; +} + +boost::optional<std::string> HighlightAction::getFrontColor() const { + return frontColor_; +} + +void HighlightAction::setBackColor(const boost::optional<std::string>& backColor) { + backColor_ = backColor; +} + +boost::optional<std::string> HighlightAction::getBackColor() const { + return backColor_; +} + +void HighlightAction::setSoundFilePath(const boost::optional<std::string>& soundFilePath) { + soundFilePath_ = soundFilePath; +} + +boost::optional<std::string> HighlightAction::getSoundFilePath() const { + return soundFilePath_; +} + +void HighlightAction::setSystemNotificationEnabled(bool systemNotificationEnabled) { + systemNotificaitonEnabled_ = systemNotificationEnabled; +} + +bool HighlightAction::isSystemNotificationEnabled() const { + return systemNotificaitonEnabled_; +} + +bool HighlightAction::isEmpty() const { + return !frontColor_.is_initialized() && !backColor_.is_initialized() && !soundFilePath_.is_initialized() && !systemNotificaitonEnabled_; +} + +} diff --git a/Swift/Controllers/Highlighting/HighlightAction.h b/Swift/Controllers/Highlighting/HighlightAction.h new file mode 100644 index 0000000..d11f3ec --- /dev/null +++ b/Swift/Controllers/Highlighting/HighlightAction.h @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2012 Maciej Niedzielski + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +/* + * Copyright (c) 2014-2017 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#pragma once + +#include <map> +#include <memory> +#include <string> + +#include <boost/archive/text_iarchive.hpp> +#include <boost/archive/text_oarchive.hpp> +#include <boost/optional.hpp> +#include <boost/serialization/map.hpp> +#include <boost/serialization/optional.hpp> + +namespace Swift { + class HighlightAction { + public: + void setFrontColor(const boost::optional<std::string>& frontColor); + boost::optional<std::string> getFrontColor() const; + + void setBackColor(const boost::optional<std::string>& backColor); + boost::optional<std::string> getBackColor() const; + + void setSoundFilePath(const boost::optional<std::string>& soundFilePath); + boost::optional<std::string> getSoundFilePath() const; + + void setSystemNotificationEnabled(bool systemNotificationEnabled); + bool isSystemNotificationEnabled() const; + + // @return returns true if the HighlightAction would result in no + // noticable action to the user. + bool isEmpty() const; + + private: + friend class boost::serialization::access; + template<class Archive> void serialize(Archive & ar, const unsigned int version); + + private: + // Highlight color. + boost::optional<std::string> frontColor_; + boost::optional<std::string> backColor_; + + // Audio notification. + boost::optional<std::string> soundFilePath_; + + // macOS Notification Center or similar. + bool systemNotificaitonEnabled_ = false; + }; + + bool operator ==(const HighlightAction& a, const HighlightAction& b); + bool operator !=(const HighlightAction& a, const HighlightAction& b); + + + template<class Archive> + void HighlightAction::serialize(Archive& ar, const unsigned int version) { + auto inputStream = dynamic_cast<boost::archive::text_iarchive*>(&ar); + auto outputStream = dynamic_cast<boost::archive::text_oarchive*>(&ar); + + if (version == 0) { + if (inputStream) { + const boost::archive::library_version_type BoostArchiveSkipVersion(15); + auto archiveLibraryVersion = boost::archive::BOOST_ARCHIVE_VERSION(); + //Due to https://svn.boost.org/trac10/ticket/13050 the boost::optional fields may fail to load and crash the client. Therefore we skip loading the values from previous versions. + if (archiveLibraryVersion == BoostArchiveSkipVersion && archiveLibraryVersion > inputStream->get_library_version()) { + return; + } + } + ar & frontColor_; + ar & backColor_; + ar & soundFilePath_; + ar & systemNotificaitonEnabled_; + } + else if (version == 1) { + //Using a map instead of optional values that may cause a problems when serialised with boost::archive 15 version + std::map<std::string, std::string> properties; + if (outputStream) { + if (frontColor_.is_initialized()) { + properties["frontColor"] = frontColor_.get(); + } + if (backColor_.is_initialized()) { + properties["backColor"] = backColor_.get(); + } + if (soundFilePath_.is_initialized()) { + properties["soundFilePath"] = soundFilePath_.get(); + } + } + ar & properties; + ar & systemNotificaitonEnabled_; + if (inputStream) { + if (properties.find("frontColor") != properties.end()) { + frontColor_ = properties["frontColor"]; + } + if (properties.find("backColor") != properties.end()) { + backColor_ = properties["backColor"]; + } + if (properties.find("soundFilePath") != properties.end()) { + soundFilePath_ = properties["soundFilePath"]; + } + } + } + } +} diff --git a/Swift/Controllers/Highlighting/HighlightConfiguration.cpp b/Swift/Controllers/Highlighting/HighlightConfiguration.cpp new file mode 100644 index 0000000..e82adb8 --- /dev/null +++ b/Swift/Controllers/Highlighting/HighlightConfiguration.cpp @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2016-2017 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#include <Swift/Controllers/Highlighting/HighlightConfiguration.h> + +namespace Swift { + +bool operator ==(const HighlightConfiguration& a, const HighlightConfiguration& b) { + if (a.keywordHighlights != b.keywordHighlights) { + return false; + } + if (a.contactHighlights != b.contactHighlights) { + return false; + } + if (a.ownMentionAction != b.ownMentionAction) { + return false; + } + if (a.playSoundOnIncomingDirectMessages != b.playSoundOnIncomingDirectMessages) { + return false; + } + if (a.showNotificationOnIncomingDirectMessages != b.showNotificationOnIncomingDirectMessages) { + return false; + } + if (a.playSoundOnIncomingGroupchatMessages != b.playSoundOnIncomingGroupchatMessages) { + return false; + } + if (a.showNotificationOnIncomingGroupchatMessages != b.showNotificationOnIncomingGroupchatMessages) { + return false; + } + return true; +} + +} diff --git a/Swift/Controllers/Highlighting/HighlightConfiguration.h b/Swift/Controllers/Highlighting/HighlightConfiguration.h new file mode 100644 index 0000000..d262dba --- /dev/null +++ b/Swift/Controllers/Highlighting/HighlightConfiguration.h @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2016-2017 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#pragma once + +#include <string> +#include <vector> + +#include <boost/archive/text_iarchive.hpp> +#include <boost/archive/text_oarchive.hpp> + +#include <Swift/Controllers/Highlighting/HighlightAction.h> + +namespace Swift { + +class HighlightConfiguration { +public: + class ContactHighlight { + public: + friend class boost::serialization::access; + template<class Archive> void serialize(Archive & ar, const unsigned int version); + + public: + std::string name; + HighlightAction action; + }; + + class KeywordHightlight { + public: + friend class boost::serialization::access; + template<class Archive> void serialize(Archive & ar, const unsigned int version); + + public: + std::string keyword; + bool matchCaseSensitive = false; + HighlightAction action; + }; + + friend class boost::serialization::access; + template<class Archive> void serialize(Archive & ar, const unsigned int version); + +public: + std::vector<KeywordHightlight> keywordHighlights; + std::vector<ContactHighlight> contactHighlights; + HighlightAction ownMentionAction; + bool playSoundOnIncomingDirectMessages = false; + bool showNotificationOnIncomingDirectMessages = false; + bool playSoundOnIncomingGroupchatMessages = false; + bool showNotificationOnIncomingGroupchatMessages = false; +}; + +bool operator ==(HighlightConfiguration const& a, HighlightConfiguration const& b); + +template<class Archive> +void HighlightConfiguration::ContactHighlight::serialize(Archive& ar, const unsigned int /*version*/) { + ar & name; + ar & action; +} + +template<class Archive> +void HighlightConfiguration::KeywordHightlight::serialize(Archive& ar, const unsigned int /*version*/) { + ar & keyword; + ar & matchCaseSensitive; + ar & action; +} + +template<class Archive> +void HighlightConfiguration::serialize(Archive& ar, const unsigned int /*version*/) { + ar & keywordHighlights; + ar & contactHighlights; + ar & ownMentionAction; + ar & playSoundOnIncomingDirectMessages; + ar & showNotificationOnIncomingDirectMessages; + ar & playSoundOnIncomingGroupchatMessages; + ar & showNotificationOnIncomingGroupchatMessages; +} + +} diff --git a/Swift/Controllers/Highlighting/HighlightEditorController.cpp b/Swift/Controllers/Highlighting/HighlightEditorController.cpp new file mode 100644 index 0000000..50da3dc --- /dev/null +++ b/Swift/Controllers/Highlighting/HighlightEditorController.cpp @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2012 Maciej Niedzielski + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +/* + * Copyright (c) 2014-2017 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#include <Swift/Controllers/Highlighting/HighlightEditorController.h> + +#include <boost/bind.hpp> + +#include <Swift/Controllers/ContactSuggester.h> +#include <Swift/Controllers/UIEvents/RequestHighlightEditorUIEvent.h> +#include <Swift/Controllers/UIEvents/UIEventStream.h> +#include <Swift/Controllers/UIInterfaces/HighlightEditorWindow.h> +#include <Swift/Controllers/UIInterfaces/HighlightEditorWindowFactory.h> + +namespace Swift { + +HighlightEditorController::HighlightEditorController(UIEventStream* uiEventStream, HighlightEditorWindowFactory* highlightEditorWindowFactory, HighlightManager* highlightManager) +: highlightEditorWindowFactory_(highlightEditorWindowFactory), highlightEditorWindow_(nullptr), highlightManager_(highlightManager), contactSuggester_(nullptr) +{ + uiEventStream->onUIEvent.connect(boost::bind(&HighlightEditorController::handleUIEvent, this, _1)); +} + +HighlightEditorController::~HighlightEditorController() +{ + delete highlightEditorWindow_; + highlightEditorWindow_ = nullptr; +} + +void HighlightEditorController::handleUIEvent(std::shared_ptr<UIEvent> rawEvent) +{ + std::shared_ptr<RequestHighlightEditorUIEvent> event = std::dynamic_pointer_cast<RequestHighlightEditorUIEvent>(rawEvent); + if (event) { + if (!highlightEditorWindow_) { + highlightEditorWindow_ = highlightEditorWindowFactory_->createHighlightEditorWindow(); + highlightEditorWindow_->setHighlightManager(highlightManager_); + highlightEditorWindow_->onContactSuggestionsRequested.connect(boost::bind(&HighlightEditorController::handleContactSuggestionsRequested, this, _1)); + } + highlightEditorWindow_->show(); + } +} + +void HighlightEditorController::handleContactSuggestionsRequested(const std::string& text) +{ + if (contactSuggester_) { + highlightEditorWindow_->setContactSuggestions(contactSuggester_->getSuggestions(text, true)); + } +} + +} diff --git a/Swift/Controllers/Highlighting/HighlightEditorController.h b/Swift/Controllers/Highlighting/HighlightEditorController.h new file mode 100644 index 0000000..d7608a5 --- /dev/null +++ b/Swift/Controllers/Highlighting/HighlightEditorController.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2012 Maciej Niedzielski + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +/* + * Copyright (c) 2014-2017 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#pragma once + +#include <memory> +#include <string> + +#include <Swift/Controllers/UIEvents/UIEvent.h> + +namespace Swift { + + class UIEventStream; + + class HighlightEditorWindowFactory; + class HighlightEditorWindow; + + class HighlightManager; + class ContactSuggester; + + class HighlightEditorController { + public: + HighlightEditorController(UIEventStream* uiEventStream, HighlightEditorWindowFactory* highlightEditorWindowFactory, HighlightManager* highlightManager); + ~HighlightEditorController(); + + HighlightManager* getHighlightManager() const { return highlightManager_; } + void setContactSuggester(ContactSuggester *suggester) { contactSuggester_ = suggester; } + + private: + void handleUIEvent(std::shared_ptr<UIEvent> event); + void handleContactSuggestionsRequested(const std::string& text); + + private: + HighlightEditorWindowFactory* highlightEditorWindowFactory_; + HighlightEditorWindow* highlightEditorWindow_; + HighlightManager* highlightManager_; + ContactSuggester* contactSuggester_; + }; + +} diff --git a/Swift/Controllers/Highlighting/HighlightManager.cpp b/Swift/Controllers/Highlighting/HighlightManager.cpp new file mode 100644 index 0000000..f09d94c --- /dev/null +++ b/Swift/Controllers/Highlighting/HighlightManager.cpp @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2012 Maciej Niedzielski + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +/* + * Copyright (c) 2014-2019 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#include <Swift/Controllers/Highlighting/HighlightManager.h> + +#include <cassert> +#include <sstream> + +#include <boost/algorithm/string.hpp> +#include <boost/archive/text_iarchive.hpp> +#include <boost/archive/text_oarchive.hpp> +#include <boost/bind.hpp> +#include <boost/numeric/conversion/cast.hpp> +#include <boost/regex.hpp> +#include <boost/serialization/vector.hpp> + +#include <Swiften/Base/Log.h> + +#include <Swift/Controllers/Highlighting/HighlightConfiguration.h> +#include <Swift/Controllers/Highlighting/Highlighter.h> +#include <Swift/Controllers/SettingConstants.h> +#include <Swift/Controllers/Settings/SettingsProvider.h> + +namespace Swift { + +HighlightManager::HighlightManager(SettingsProvider* settings) + : settings_(settings) + , storingSettings_(false) { + highlightConfiguration_ = std::make_shared<HighlightConfiguration>(); + loadSettings(); + handleSettingChangedConnection_ = settings_->onSettingChanged.connect(boost::bind(&HighlightManager::handleSettingChanged, this, _1)); +} + +void HighlightManager::handleSettingChanged(const std::string& settingPath) { + if (!storingSettings_ && SettingConstants::HIGHLIGHT_RULES.getKey() == settingPath) { + loadSettings(); + } +} + +HighlightConfiguration HighlightManager::getDefaultConfig() { + HighlightConfiguration defaultConfiguration; + defaultConfiguration.playSoundOnIncomingDirectMessages = true; + defaultConfiguration.showNotificationOnIncomingDirectMessages = true; + defaultConfiguration.ownMentionAction.setSoundFilePath(std::string("/sounds/message-received.wav")); + defaultConfiguration.ownMentionAction.setSystemNotificationEnabled(true); + return defaultConfiguration; +} + +void HighlightManager::storeSettings() { + storingSettings_ = true; // don't reload settings while saving + settings_->storeSetting(SettingConstants::HIGHLIGHT_RULES_V2, highlightConfigurationToString(*highlightConfiguration_)); + storingSettings_ = false; +} + +void HighlightManager::loadSettings() { + std::string configString = settings_->getSetting(SettingConstants::HIGHLIGHT_RULES_V2); + *highlightConfiguration_ = highlightConfigurationFromString(configString); +} + +Highlighter* HighlightManager::createHighlighter(NickResolver* nickResolver) { + return new Highlighter(this, nickResolver); +} + +void HighlightManager::resetToDefaultConfiguration() { + *highlightConfiguration_ = getDefaultConfig(); +} + +HighlightConfiguration HighlightManager::highlightConfigurationFromString(const std::string& dataString) { + std::stringstream stream; + stream << dataString; + + HighlightConfiguration configuration; + try { + boost::archive::text_iarchive archive(stream); + archive >> configuration; + } + catch (boost::archive::archive_exception&) { + configuration = getDefaultConfig(); + SWIFT_LOG(warning) << "Failed to load highlight configuration. Will use default configuration instead."; + } + return configuration; +} + +std::string HighlightManager::highlightConfigurationToString(const HighlightConfiguration& configuration) { + std::stringstream stream; + boost::archive::text_oarchive archive(stream); + archive & configuration; + return stream.str(); +} + +} diff --git a/Swift/Controllers/Highlighting/HighlightManager.h b/Swift/Controllers/Highlighting/HighlightManager.h new file mode 100644 index 0000000..13f59fa --- /dev/null +++ b/Swift/Controllers/Highlighting/HighlightManager.h @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2012 Maciej Niedzielski + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +/* + * Copyright (c) 2014-2017 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#pragma once + +#include <memory> +#include <string> + +#include <boost/signals2.hpp> + +#include <Swift/Controllers/Highlighting/HighlightConfiguration.h> + +namespace Swift { + + class NickResolver; + class SettingsProvider; + class Highlighter; + + class HighlightManager { + public: + HighlightManager(SettingsProvider* settings); + + Highlighter* createHighlighter(NickResolver* nickResolver); + + std::shared_ptr<HighlightConfiguration> getConfiguration() const { + return highlightConfiguration_; + } + + void setConfiguration(const HighlightConfiguration& config) { + *highlightConfiguration_ = config; + storeSettings(); + } + + void resetToDefaultConfiguration(); + + void storeSettings(); + void loadSettings(); + + boost::signals2::signal<void (const HighlightAction&)> onHighlight; + + private: + void handleSettingChanged(const std::string& settingPath); + + static HighlightConfiguration getDefaultConfig(); + + static HighlightConfiguration highlightConfigurationFromString(const std::string& dataString); + static std::string highlightConfigurationToString(const HighlightConfiguration& configuration); + + private: + SettingsProvider* settings_; + bool storingSettings_; + std::shared_ptr<HighlightConfiguration> highlightConfiguration_; + boost::signals2::scoped_connection handleSettingChangedConnection_; + }; +} diff --git a/Swift/Controllers/Highlighting/Highlighter.cpp b/Swift/Controllers/Highlighting/Highlighter.cpp new file mode 100644 index 0000000..b05de2d --- /dev/null +++ b/Swift/Controllers/Highlighting/Highlighter.cpp @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2012 Maciej Niedzielski + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +/* + * Copyright (c) 2014-2017 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#include <Swift/Controllers/Highlighting/Highlighter.h> + +#include <set> +#include <string> + +#include <Swiften/Base/String.h> +#include <Swiften/Base/format.h> +#include <Swiften/Client/NickResolver.h> + +#include <Swift/Controllers/Highlighting/HighlightManager.h> +#include <Swift/Controllers/Intl.h> +#include <Swift/Controllers/XMPPEvents/MessageEvent.h> + +namespace Swift { + +Highlighter::Highlighter(HighlightManager* manager, NickResolver* nickResolver) + : manager_(manager), nickResolver_(nickResolver) { +} + +void Highlighter::handleSystemNotifications(const ChatWindow::ChatMessage& message, std::shared_ptr<MessageEvent> event) { + if (std::shared_ptr<MessageEvent> messageEvent = std::dynamic_pointer_cast<MessageEvent>(event)) { + JID jid = messageEvent->getStanza()->getFrom(); + std::string nickname = nickResolver_->jidToNick(jid); + + std::string messageText = messageEvent->getStanza()->getBody().get_value_or(""); + if (boost::starts_with(messageText, "/me ")) { + messageText = "*" + String::getSplittedAtFirst(messageText, ' ').second + "*"; + } + + if (message.getHighlightActionDirectMessage().isSystemNotificationEnabled()) { + // title: Romeo says + // message: message + std::string title = str(format(QT_TRANSLATE_NOOP("", "%1% says")) % nickname); + event->addNotification(title, messageText); + } + if (message.getHighlightActionGroupMessage().isSystemNotificationEnabled()) { + // title: Romeo in $roomJID says + // message: message + std::string roomName = jid.getNode(); + std::string title = str(format(QT_TRANSLATE_NOOP("", "%1% in %2% says")) % nickname % roomName); + event->addNotification(title, messageText); + } + if (message.getHighlightActionOwnMention().isSystemNotificationEnabled()) { + // title: Romeo mentioned you in $roomJID + // message: message + std::string roomName = jid.getNode(); + std::string title = str(format(QT_TRANSLATE_NOOP("", "%1% mentioned you in %2%")) % nickname % roomName); + event->addNotification(title, messageText); + } + if (message.getHighlightActionSender().isSystemNotificationEnabled()) { + // title: Romeo says + // message: message + auto title = str(format(QT_TRANSLATE_NOOP("", "%1% says")) % nickname); + event->addNotification(title, messageText); + } + for (auto&& part : message.getParts()) { + auto highlightPart = std::dynamic_pointer_cast<ChatWindow::ChatHighlightingMessagePart>(part); + if (highlightPart && highlightPart->action.isSystemNotificationEnabled()) { + // title: Romeo mentioned '$keyword' + // message: message + auto title = str(format(QT_TRANSLATE_NOOP("", "%1% mentioned '%2%'")) % nickname % highlightPart->text); + event->addNotification(title, messageText); + } + } + } +} + +void Highlighter::handleSoundNotifications(const ChatWindow::ChatMessage& chatMessage) { + std::set<std::string> playedSoundPaths; + std::vector<HighlightAction> actionsToPlay; + + // collect unique sounds to play + auto checkSoundActionAndQueueUnique = [&](const HighlightAction& action) { + if (action.getSoundFilePath()) { + auto soundFilePath = action.getSoundFilePath().get_value_or(""); + if (playedSoundPaths.find(soundFilePath) == playedSoundPaths.end()) { + playedSoundPaths.insert(soundFilePath); + actionsToPlay.push_back(action); + } + } + }; + + for (auto&& part : chatMessage.getParts()) { + auto highlightMessage = std::dynamic_pointer_cast<ChatWindow::ChatHighlightingMessagePart>(part); + if (highlightMessage) { + checkSoundActionAndQueueUnique(highlightMessage->action); + } + } + + checkSoundActionAndQueueUnique(chatMessage.getHighlightActionSender()); + checkSoundActionAndQueueUnique(chatMessage.getHighlightActionOwnMention()); + checkSoundActionAndQueueUnique(chatMessage.getHighlightActionDirectMessage()); + checkSoundActionAndQueueUnique(chatMessage.getHighlightActionGroupMessage()); + + // play sounds + for (const auto& action : actionsToPlay) { + manager_->onHighlight(action); + } +} + +} diff --git a/Swift/Controllers/Highlighting/Highlighter.h b/Swift/Controllers/Highlighting/Highlighter.h new file mode 100644 index 0000000..51308a1 --- /dev/null +++ b/Swift/Controllers/Highlighting/Highlighter.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2012 Maciej Niedzielski + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +/* + * Copyright (c) 2016-2017 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#pragma once + +#include <string> + +#include <Swift/Controllers/Highlighting/HighlightConfiguration.h> +#include <Swift/Controllers/UIInterfaces/ChatWindow.h> +#include <Swift/Controllers/XMPPEvents/MessageEvent.h> + +namespace Swift { + + class HighlightManager; + class NickResolver; + + class Highlighter { + public: + Highlighter(HighlightManager* manager, NickResolver* nickResolver); + + void handleSystemNotifications(const ChatWindow::ChatMessage& message, std::shared_ptr<MessageEvent> event); + void handleSoundNotifications(const ChatWindow::ChatMessage& chatMessage); + + private: + HighlightManager* manager_; + NickResolver* nickResolver_; + }; + +} diff --git a/Swift/Controllers/HistoryController.cpp b/Swift/Controllers/HistoryController.cpp index d730236..1e5830c 100644 --- a/Swift/Controllers/HistoryController.cpp +++ b/Swift/Controllers/HistoryController.cpp @@ -4,11 +4,21 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ +/* + * Copyright (c) 2014-2015 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + + #include <Swift/Controllers/HistoryController.h> -#include <Swiften/History/HistoryStorage.h> -#include <Swiften/History/HistoryMessage.h> + #include <boost/date_time/c_local_time_adjustor.hpp> +#include <Swiften/History/HistoryMessage.h> +#include <Swiften/History/HistoryStorage.h> +#include <Swiften/JID/JID.h> + namespace Swift { HistoryController::HistoryController(HistoryStorage* localHistoryStorage) : localHistory_(localHistoryStorage) { @@ -18,39 +28,39 @@ HistoryController::~HistoryController() { } void HistoryController::addMessage(const std::string& message, const JID& fromJID, const JID& toJID, HistoryMessage::Type type, const boost::posix_time::ptime& timeStamp) { - // note: using localtime timestamps - boost::posix_time::ptime localTime = boost::date_time::c_local_adjustor<boost::posix_time::ptime>::utc_to_local(timeStamp); - int offset = (localTime - timeStamp).hours(); + // note: using localtime timestamps + boost::posix_time::ptime localTime = boost::date_time::c_local_adjustor<boost::posix_time::ptime>::utc_to_local(timeStamp); + int offset = (localTime - timeStamp).hours(); - HistoryMessage historyMessage(message, fromJID, toJID, type, localTime, offset); + HistoryMessage historyMessage(message, fromJID, toJID, type, localTime, offset); - localHistory_->addMessage(historyMessage); - onNewMessage(historyMessage); + localHistory_->addMessage(historyMessage); + onNewMessage(historyMessage); } std::vector<HistoryMessage> HistoryController::getMessagesFromDate(const JID& selfJID, const JID& contactJID, HistoryMessage::Type type, const boost::gregorian::date& date) const { - return localHistory_->getMessagesFromDate(selfJID, contactJID, type, date); + return localHistory_->getMessagesFromDate(selfJID, contactJID, type, date); } std::vector<HistoryMessage> HistoryController::getMUCContext(const JID& selfJID, const JID& mucJID, const boost::posix_time::ptime& timeStamp) const { - boost::posix_time::ptime localTime = boost::date_time::c_local_adjustor<boost::posix_time::ptime>::utc_to_local(timeStamp); - return getMessagesFromDate(selfJID, mucJID, HistoryMessage::Groupchat, localTime.date()); + boost::posix_time::ptime localTime = boost::date_time::c_local_adjustor<boost::posix_time::ptime>::utc_to_local(timeStamp); + return getMessagesFromDate(selfJID, mucJID, HistoryMessage::Groupchat, localTime.date()); } std::vector<HistoryMessage> HistoryController::getMessagesFromPreviousDate(const JID& selfJID, const JID& contactJID, HistoryMessage::Type type, const boost::gregorian::date& date) const { - return localHistory_->getMessagesFromPreviousDate(selfJID, contactJID, type, date); + return localHistory_->getMessagesFromPreviousDate(selfJID, contactJID, type, date); } std::vector<HistoryMessage> HistoryController::getMessagesFromNextDate(const JID& selfJID, const JID& contactJID, HistoryMessage::Type type, const boost::gregorian::date& date) const { - return localHistory_->getMessagesFromNextDate(selfJID, contactJID, type, date); + return localHistory_->getMessagesFromNextDate(selfJID, contactJID, type, date); } ContactsMap HistoryController::getContacts(const JID& selfJID, HistoryMessage::Type type, const std::string& keyword) const { - return localHistory_->getContacts(selfJID, type, keyword); + return localHistory_->getContacts(selfJID, type, keyword); } boost::posix_time::ptime HistoryController::getLastTimeStampFromMUC(const JID& selfJID, const JID& mucJID) { - return localHistory_->getLastTimeStampFromMUC(selfJID, mucJID); + return localHistory_->getLastTimeStampFromMUC(selfJID, mucJID); } } diff --git a/Swift/Controllers/HistoryController.h b/Swift/Controllers/HistoryController.h index 8c86409..af70505 100644 --- a/Swift/Controllers/HistoryController.h +++ b/Swift/Controllers/HistoryController.h @@ -4,37 +4,42 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ +/* + * Copyright (c) 2015-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + #pragma once -#include <Swiften/JID/JID.h> -#include <boost/date_time/posix_time/posix_time.hpp> #include <vector> -#include <set> -#include <Swiften/Base/boost_bsignals.h> + +#include <boost/date_time/posix_time/posix_time.hpp> +#include <boost/signals2.hpp> + #include <Swiften/History/HistoryMessage.h> #include <Swiften/History/HistoryStorage.h> namespace Swift { - class JID; + class JID; - class HistoryController { - public: - HistoryController(HistoryStorage* localHistoryStorage); - ~HistoryController(); + class HistoryController { + public: + HistoryController(HistoryStorage* localHistoryStorage); + ~HistoryController(); - void addMessage(const std::string& message, const JID& fromJID, const JID& toJID, HistoryMessage::Type type, const boost::posix_time::ptime& timeStamp); - std::vector<HistoryMessage> getMessagesFromDate(const JID& selfJID, const JID& contactJID, HistoryMessage::Type type, const boost::gregorian::date& date) const; - std::vector<HistoryMessage> getMessagesFromPreviousDate(const JID& selfJID, const JID& contactJID, HistoryMessage::Type type, const boost::gregorian::date& date) const; - std::vector<HistoryMessage> getMessagesFromNextDate(const JID& selfJID, const JID& contactJID, HistoryMessage::Type type, const boost::gregorian::date& date) const; - ContactsMap getContacts(const JID& selfJID, HistoryMessage::Type type, const std::string& keyword = std::string()) const; - std::vector<HistoryMessage> getMUCContext(const JID& selfJID, const JID& mucJID, const boost::posix_time::ptime& timeStamp) const; + void addMessage(const std::string& message, const JID& fromJID, const JID& toJID, HistoryMessage::Type type, const boost::posix_time::ptime& timeStamp); + std::vector<HistoryMessage> getMessagesFromDate(const JID& selfJID, const JID& contactJID, HistoryMessage::Type type, const boost::gregorian::date& date) const; + std::vector<HistoryMessage> getMessagesFromPreviousDate(const JID& selfJID, const JID& contactJID, HistoryMessage::Type type, const boost::gregorian::date& date) const; + std::vector<HistoryMessage> getMessagesFromNextDate(const JID& selfJID, const JID& contactJID, HistoryMessage::Type type, const boost::gregorian::date& date) const; + ContactsMap getContacts(const JID& selfJID, HistoryMessage::Type type, const std::string& keyword = std::string()) const; + std::vector<HistoryMessage> getMUCContext(const JID& selfJID, const JID& mucJID, const boost::posix_time::ptime& timeStamp) const; - boost::posix_time::ptime getLastTimeStampFromMUC(const JID& selfJID, const JID& mucJID); + boost::posix_time::ptime getLastTimeStampFromMUC(const JID& selfJID, const JID& mucJID); - boost::signal<void (const HistoryMessage&)> onNewMessage; + boost::signals2::signal<void (const HistoryMessage&)> onNewMessage; - private: - HistoryStorage* localHistory_; - bool remoteArchiveSupported_; - }; + private: + HistoryStorage* localHistory_; + }; } diff --git a/Swift/Controllers/HistoryViewController.cpp b/Swift/Controllers/HistoryViewController.cpp index cfa2482..669b002 100644 --- a/Swift/Controllers/HistoryViewController.cpp +++ b/Swift/Controllers/HistoryViewController.cpp @@ -5,354 +5,356 @@ */ /* - * Copyright (c) 2013 Remko Tronçon + * Copyright (c) 2013-2016 Isode Limited. * Licensed under the GNU General Public License. * See the COPYING file for more information. */ #include <Swift/Controllers/HistoryViewController.h> -#include <Swift/Controllers/UIInterfaces/HistoryWindowFactory.h> -#include <Swift/Controllers/UIEvents/RequestHistoryUIEvent.h> -#include <Swift/Controllers/HistoryController.h> -#include <Swiften/History/HistoryMessage.h> -#include <Swiften/Base/foreach.h> -#include <Swiften/Client/NickResolver.h> +#include <boost/range/adaptor/reversed.hpp> + #include <Swiften/Avatars/AvatarManager.h> -#include <Swift/Controllers/Roster/SetPresence.h> -#include <Swift/Controllers/Roster/SetAvatar.h> #include <Swiften/Base/Path.h> +#include <Swiften/Client/NickResolver.h> +#include <Swiften/History/HistoryMessage.h> + +#include <Swift/Controllers/HistoryController.h> +#include <Swift/Controllers/Roster/ItemOperations/SetAvatar.h> +#include <Swift/Controllers/Roster/ItemOperations/SetPresence.h> +#include <Swift/Controllers/UIEvents/RequestHistoryUIEvent.h> +#include <Swift/Controllers/UIInterfaces/HistoryWindowFactory.h> namespace Swift { - static const std::string category[] = { "Contacts", "MUC", "Contacts" }; + static const std::string category[] = { "Contacts", "MUC", "Contacts" }; HistoryViewController::HistoryViewController( - const JID& selfJID, - UIEventStream* uiEventStream, - HistoryController* historyController, - NickResolver* nickResolver, - AvatarManager* avatarManager, - PresenceOracle* presenceOracle, - HistoryWindowFactory* historyWindowFactory) : - selfJID_(selfJID), - uiEventStream_(uiEventStream), - historyController_(historyController), - nickResolver_(nickResolver), - avatarManager_(avatarManager), - presenceOracle_(presenceOracle), - historyWindowFactory_(historyWindowFactory), - historyWindow_(NULL), - selectedItem_(NULL), - currentResultDate_(boost::gregorian::not_a_date_time) { - uiEventStream_->onUIEvent.connect(boost::bind(&HistoryViewController::handleUIEvent, this, _1)); - - roster_ = new Roster(false, true); + const JID& selfJID, + UIEventStream* uiEventStream, + HistoryController* historyController, + NickResolver* nickResolver, + AvatarManager* avatarManager, + PresenceOracle* presenceOracle, + HistoryWindowFactory* historyWindowFactory) : + selfJID_(selfJID), + uiEventStream_(uiEventStream), + historyController_(historyController), + nickResolver_(nickResolver), + avatarManager_(avatarManager), + presenceOracle_(presenceOracle), + historyWindowFactory_(historyWindowFactory), + historyWindow_(nullptr), + selectedItem_(nullptr), + currentResultDate_(boost::gregorian::not_a_date_time) { + uiEventStream_->onUIEvent.connect(boost::bind(&HistoryViewController::handleUIEvent, this, _1)); + + roster_ = new Roster(false, true); } HistoryViewController::~HistoryViewController() { - uiEventStream_->onUIEvent.disconnect(boost::bind(&HistoryViewController::handleUIEvent, this, _1)); - if (historyWindow_) { - historyWindow_->onSelectedContactChanged.disconnect(boost::bind(&HistoryViewController::handleSelectedContactChanged, this, _1)); - historyWindow_->onReturnPressed.disconnect(boost::bind(&HistoryViewController::handleReturnPressed, this, _1)); - historyWindow_->onScrollReachedTop.disconnect(boost::bind(&HistoryViewController::handleScrollReachedTop, this, _1)); - 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)); - - presenceOracle_->onPresenceChange.disconnect(boost::bind(&HistoryViewController::handlePresenceChanged, this, _1)); - avatarManager_->onAvatarChanged.disconnect(boost::bind(&HistoryViewController::handleAvatarChanged, this, _1)); - - delete historyWindow_; - } - delete roster_; + uiEventStream_->onUIEvent.disconnect(boost::bind(&HistoryViewController::handleUIEvent, this, _1)); + if (historyWindow_) { + historyWindow_->onSelectedContactChanged.disconnect(boost::bind(&HistoryViewController::handleSelectedContactChanged, this, _1)); + historyWindow_->onReturnPressed.disconnect(boost::bind(&HistoryViewController::handleReturnPressed, this, _1)); + historyWindow_->onScrollReachedTop.disconnect(boost::bind(&HistoryViewController::handleScrollReachedTop, this, _1)); + 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)); + + presenceOracle_->onPresenceChange.disconnect(boost::bind(&HistoryViewController::handlePresenceChanged, this, _1)); + avatarManager_->onAvatarChanged.disconnect(boost::bind(&HistoryViewController::handleAvatarChanged, this, _1)); + + delete historyWindow_; + } + delete roster_; } -void HistoryViewController::handleUIEvent(boost::shared_ptr<UIEvent> rawEvent) { - boost::shared_ptr<RequestHistoryUIEvent> event = boost::dynamic_pointer_cast<RequestHistoryUIEvent>(rawEvent); - if (event != NULL) { - if (historyWindow_ == NULL) { - historyWindow_ = historyWindowFactory_->createHistoryWindow(uiEventStream_); - historyWindow_->onSelectedContactChanged.connect(boost::bind(&HistoryViewController::handleSelectedContactChanged, this, _1)); - historyWindow_->onReturnPressed.connect(boost::bind(&HistoryViewController::handleReturnPressed, this, _1)); - historyWindow_->onScrollReachedTop.connect(boost::bind(&HistoryViewController::handleScrollReachedTop, this, _1)); - 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)); - - presenceOracle_->onPresenceChange.connect(boost::bind(&HistoryViewController::handlePresenceChanged, this, _1)); - avatarManager_->onAvatarChanged.connect(boost::bind(&HistoryViewController::handleAvatarChanged, this, _1)); - - historyWindow_->setRosterModel(roster_); - } - - // populate roster by doing an empty search - handleReturnPressed(std::string()); - - historyWindow_->activate(); - } +void HistoryViewController::handleUIEvent(std::shared_ptr<UIEvent> rawEvent) { + std::shared_ptr<RequestHistoryUIEvent> event = std::dynamic_pointer_cast<RequestHistoryUIEvent>(rawEvent); + if (event != nullptr) { + if (historyWindow_ == nullptr) { + historyWindow_ = historyWindowFactory_->createHistoryWindow(uiEventStream_); + historyWindow_->onSelectedContactChanged.connect(boost::bind(&HistoryViewController::handleSelectedContactChanged, this, _1)); + historyWindow_->onReturnPressed.connect(boost::bind(&HistoryViewController::handleReturnPressed, this, _1)); + historyWindow_->onScrollReachedTop.connect(boost::bind(&HistoryViewController::handleScrollReachedTop, this, _1)); + 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)); + + presenceOracle_->onPresenceChange.connect(boost::bind(&HistoryViewController::handlePresenceChanged, this, _1)); + avatarManager_->onAvatarChanged.connect(boost::bind(&HistoryViewController::handleAvatarChanged, this, _1)); + + historyWindow_->setRosterModel(roster_); + } + + // populate roster by doing an empty search + handleReturnPressed(std::string()); + + historyWindow_->activate(); + } } void HistoryViewController::handleSelectedContactChanged(RosterItem* newContact) { - // FIXME: signal is triggerd twice. - ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(newContact); - - if (contact && selectedItem_ != contact) { - selectedItem_ = contact; - historyWindow_->resetConversationView(); - } - else { - return; - } - - JID contactJID = contact->getJID(); - - std::vector<HistoryMessage> messages; - for (int it = HistoryMessage::Chat; it <= HistoryMessage::PrivateMessage; it++) { - HistoryMessage::Type type = static_cast<HistoryMessage::Type>(it); - - if (contacts_[type].count(contactJID)) { - currentResultDate_ = *contacts_[type][contactJID].rbegin(); - selectedItemType_ = type; - messages = historyController_->getMessagesFromDate(selfJID_, contactJID, type, currentResultDate_); - } - } - - historyWindow_->setDate(currentResultDate_); - - foreach (const HistoryMessage& message, messages) { - addNewMessage(message, false); - } + // FIXME: signal is triggerd twice. + ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(newContact); + + if (contact && selectedItem_ != contact) { + selectedItem_ = contact; + historyWindow_->resetConversationView(); + } + else { + return; + } + + JID contactJID = contact->getJID(); + + std::vector<HistoryMessage> messages; + for (int it = HistoryMessage::Chat; it <= HistoryMessage::PrivateMessage; it++) { + HistoryMessage::Type type = static_cast<HistoryMessage::Type>(it); + + if (contacts_[type].count(contactJID)) { + currentResultDate_ = *contacts_[type][contactJID].rbegin(); + selectedItemType_ = type; + messages = historyController_->getMessagesFromDate(selfJID_, contactJID, type, currentResultDate_); + } + } + + historyWindow_->setDate(currentResultDate_); + + for (const auto& message : messages) { + addNewMessage(message, false); + } } void HistoryViewController::handleNewMessage(const HistoryMessage& message) { - JID contactJID = message.getFromJID().toBare() == selfJID_ ? message.getToJID() : message.getFromJID(); - - JID displayJID; - if (message.getType() == HistoryMessage::PrivateMessage) { - displayJID = contactJID; - } - else { - displayJID = contactJID.toBare(); - } - - // check current conversation - if (selectedItem_ && selectedItem_->getJID() == displayJID) { - 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; - } - - // update contacts - if (!contacts_[message.getType()].count(displayJID)) { - roster_->addContact(displayJID, displayJID, nickResolver_->jidToNick(displayJID), category[message.getType()], avatarManager_->getAvatarPath(displayJID)); - } - - contacts_[message.getType()][displayJID].insert(message.getTime().date()); + JID contactJID = message.getFromJID().toBare() == selfJID_ ? message.getToJID() : message.getFromJID(); + + JID displayJID; + if (message.getType() == HistoryMessage::PrivateMessage) { + displayJID = contactJID; + } + else { + displayJID = contactJID.toBare(); + } + + // check current conversation + if (selectedItem_ && selectedItem_->getJID() == displayJID) { + 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; + } + + // update contacts + if (!contacts_[message.getType()].count(displayJID)) { + roster_->addContact(displayJID, displayJID, nickResolver_->jidToNick(displayJID), category[message.getType()], avatarManager_->getAvatarPath(displayJID)); + } + + contacts_[message.getType()][displayJID].insert(message.getTime().date()); } void HistoryViewController::addNewMessage(const HistoryMessage& message, bool addAtTheTop) { - bool senderIsSelf = message.getFromJID().toBare() == selfJID_; - std::string avatarPath = pathToString(avatarManager_->getAvatarPath(message.getFromJID())); + bool senderIsSelf = message.getFromJID().toBare() == selfJID_; + std::string avatarPath = pathToString(avatarManager_->getAvatarPath(message.getFromJID())); - std::string nick = message.getType() != HistoryMessage::Groupchat ? nickResolver_->jidToNick(message.getFromJID()) : message.getFromJID().getResource(); - historyWindow_->addMessage(message.getMessage(), nick, senderIsSelf, avatarPath, message.getTime(), addAtTheTop); + std::string nick = message.getType() != HistoryMessage::Groupchat ? nickResolver_->jidToNick(message.getFromJID()) : message.getFromJID().getResource(); + historyWindow_->addMessage(message.getMessage(), nick, senderIsSelf, avatarPath, message.getTime(), addAtTheTop); } void HistoryViewController::handleReturnPressed(const std::string& keyword) { - reset(); - - for (int it = HistoryMessage::Chat; it <= HistoryMessage::PrivateMessage; it++) { - HistoryMessage::Type type = static_cast<HistoryMessage::Type>(it); - - contacts_[type] = historyController_->getContacts(selfJID_, type, keyword); - - for (ContactsMap::const_iterator contact = contacts_[type].begin(); contact != contacts_[type].end(); contact++) { - const JID& jid = contact->first; - std::string nick; - if (type == HistoryMessage::PrivateMessage) { - nick = jid.toString(); - } - else { - nick = nickResolver_->jidToNick(jid); - } - roster_->addContact(jid, jid, nick, category[type], avatarManager_->getAvatarPath(jid)); - - Presence::ref presence = getPresence(jid, type == HistoryMessage::Groupchat); - - if (presence.get()) { - roster_->applyOnItem(SetPresence(presence, JID::WithoutResource), jid); - } - } - } + reset(); + + for (int it = HistoryMessage::Chat; it <= HistoryMessage::PrivateMessage; it++) { + HistoryMessage::Type type = static_cast<HistoryMessage::Type>(it); + + contacts_[type] = historyController_->getContacts(selfJID_, type, keyword); + + for (ContactsMap::const_iterator contact = contacts_[type].begin(); contact != contacts_[type].end(); contact++) { + const JID& jid = contact->first; + std::string nick; + if (type == HistoryMessage::PrivateMessage) { + nick = jid.toString(); + } + else { + nick = nickResolver_->jidToNick(jid); + } + roster_->addContact(jid, jid, nick, category[type], avatarManager_->getAvatarPath(jid)); + + Presence::ref presence = getPresence(jid, type == HistoryMessage::Groupchat); + + if (presence.get()) { + roster_->applyOnItem(SetPresence(presence, JID::WithoutResource), jid); + } + } + } } void HistoryViewController::handleScrollReachedTop(const boost::gregorian::date& date) { - if (!selectedItem_) { - return; - } + if (!selectedItem_) { + return; + } - std::vector<HistoryMessage> messages = historyController_->getMessagesFromPreviousDate(selfJID_, selectedItem_->getJID(), selectedItemType_, date); + std::vector<HistoryMessage> messages = historyController_->getMessagesFromPreviousDate(selfJID_, selectedItem_->getJID(), selectedItemType_, date); - foreach (const HistoryMessage& message, messages) { - addNewMessage(message, true); - } - historyWindow_->resetConversationViewTopInsertPoint(); + for (const auto& message : messages) { + addNewMessage(message, true); + } + historyWindow_->resetConversationViewTopInsertPoint(); } void HistoryViewController::handleScrollReachedBottom(const boost::gregorian::date& date) { - if (!selectedItem_) { - return; - } + if (!selectedItem_) { + return; + } - std::vector<HistoryMessage> messages = historyController_->getMessagesFromNextDate(selfJID_, selectedItem_->getJID(), selectedItemType_, date); + std::vector<HistoryMessage> messages = historyController_->getMessagesFromNextDate(selfJID_, selectedItem_->getJID(), selectedItemType_, date); - foreach (const HistoryMessage& message, messages) { - addNewMessage(message, false); - } + for (const auto& message : messages) { + addNewMessage(message, false); + } } void HistoryViewController::handleNextButtonClicked() { - if (!selectedItem_) { - return; - } + if (!selectedItem_) { + return; + } - std::set<boost::gregorian::date>::iterator date = contacts_[selectedItemType_][selectedItem_->getJID()].find(currentResultDate_); + std::set<boost::gregorian::date>::iterator date = contacts_[selectedItemType_][selectedItem_->getJID()].find(currentResultDate_); - if (*date == *contacts_[selectedItemType_][selectedItem_->getJID()].rbegin()) { - return; - } + if (*date == *contacts_[selectedItemType_][selectedItem_->getJID()].rbegin()) { + return; + } - historyWindow_->resetConversationView(); - currentResultDate_ = *(++date); - std::vector<HistoryMessage> messages = historyController_->getMessagesFromDate(selfJID_, selectedItem_->getJID(), selectedItemType_, currentResultDate_); - historyWindow_->setDate(currentResultDate_); + historyWindow_->resetConversationView(); + currentResultDate_ = *(++date); + std::vector<HistoryMessage> messages = historyController_->getMessagesFromDate(selfJID_, selectedItem_->getJID(), selectedItemType_, currentResultDate_); + historyWindow_->setDate(currentResultDate_); - foreach (const HistoryMessage& message, messages) { - addNewMessage(message, false); - } + for (const auto& message : messages) { + addNewMessage(message, false); + } } void HistoryViewController::handlePreviousButtonClicked() { - if (!selectedItem_) { - return; - } + if (!selectedItem_) { + return; + } - std::set<boost::gregorian::date>::iterator date = contacts_[selectedItemType_][selectedItem_->getJID()].find(currentResultDate_); + std::set<boost::gregorian::date>::iterator date = contacts_[selectedItemType_][selectedItem_->getJID()].find(currentResultDate_); - if (date == contacts_[selectedItemType_][selectedItem_->getJID()].begin()) { - return; - } + if (date == contacts_[selectedItemType_][selectedItem_->getJID()].begin()) { + return; + } - historyWindow_->resetConversationView(); - currentResultDate_ = *(--date); - std::vector<HistoryMessage> messages = historyController_->getMessagesFromDate(selfJID_, selectedItem_->getJID(), selectedItemType_, currentResultDate_); - historyWindow_->setDate(currentResultDate_); + historyWindow_->resetConversationView(); + currentResultDate_ = *(--date); + std::vector<HistoryMessage> messages = historyController_->getMessagesFromDate(selfJID_, selectedItem_->getJID(), selectedItemType_, currentResultDate_); + historyWindow_->setDate(currentResultDate_); - foreach (const HistoryMessage& message, messages) { - addNewMessage(message, false); - } + for (const auto& message : messages) { + addNewMessage(message, false); + } } void HistoryViewController::reset() { - roster_->removeAll(); - contacts_.clear(); - selectedItem_ = NULL; - historyWindow_->resetConversationView(); + roster_->removeAll(); + contacts_.clear(); + selectedItem_ = nullptr; + 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); - } + if (!selectedItem_) { + return; + } + + boost::gregorian::date newDate; + if (contacts_[selectedItemType_][selectedItem_->getJID()].count(date)) { + newDate = date; + } + else if (date < currentResultDate_) { + for (const auto& current : contacts_[selectedItemType_][selectedItem_->getJID()]) { + if (current > date) { + newDate = current; + break; + } + } + } + else { + for (const auto& current : boost::adaptors::reverse(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_); + + for (const auto& message : messages) { + addNewMessage(message, false); + } } void HistoryViewController::handlePresenceChanged(Presence::ref presence) { - JID jid = presence->getFrom(); - - if (contacts_[HistoryMessage::Chat].count(jid.toBare())) { - roster_->applyOnItems(SetPresence(presence, JID::WithoutResource)); - return; - } - - if (contacts_[HistoryMessage::Groupchat].count(jid.toBare())) { - Presence::ref availablePresence = boost::make_shared<Presence>(Presence()); - availablePresence->setFrom(jid.toBare()); - roster_->applyOnItems(SetPresence(availablePresence, JID::WithResource)); - } - - if (contacts_[HistoryMessage::PrivateMessage].count(jid)) { - roster_->applyOnItems(SetPresence(presence, JID::WithResource)); - } + JID jid = presence->getFrom(); + + if (contacts_[HistoryMessage::Chat].count(jid.toBare())) { + roster_->applyOnItems(SetPresence(presence, JID::WithoutResource)); + return; + } + + if (contacts_[HistoryMessage::Groupchat].count(jid.toBare())) { + Presence::ref availablePresence = std::make_shared<Presence>(Presence()); + availablePresence->setFrom(jid.toBare()); + roster_->applyOnItems(SetPresence(availablePresence, JID::WithResource)); + } + + if (contacts_[HistoryMessage::PrivateMessage].count(jid)) { + roster_->applyOnItems(SetPresence(presence, JID::WithResource)); + } } void HistoryViewController::handleAvatarChanged(const JID& jid) { - roster_->applyOnItems(SetAvatar(jid, avatarManager_->getAvatarPath(jid))); + roster_->applyOnItems(SetAvatar(jid, avatarManager_->getAvatarPath(jid))); } Presence::ref HistoryViewController::getPresence(const JID& jid, bool isMUC) { - if (jid.isBare() && !isMUC) { - return presenceOracle_->getHighestPriorityPresence(jid); - } + if (jid.isBare() && !isMUC) { + return presenceOracle_->getHighestPriorityPresence(jid); + } - std::vector<Presence::ref> mucPresence = presenceOracle_->getAllPresence(jid.toBare()); + std::vector<Presence::ref> mucPresence = presenceOracle_->getAllPresence(jid.toBare()); - if (isMUC && !mucPresence.empty()) { - Presence::ref presence = boost::make_shared<Presence>(Presence()); - presence->setFrom(jid); - return presence; - } + if (isMUC && !mucPresence.empty()) { + Presence::ref presence = std::make_shared<Presence>(Presence()); + presence->setFrom(jid); + return presence; + } - foreach (Presence::ref presence, mucPresence) { - if (presence.get() && presence->getFrom() == jid) { - return presence; - } - } + for (auto&& presence : mucPresence) { + if (presence.get() && presence->getFrom() == jid) { + return presence; + } + } - return Presence::create(); + return Presence::create(); } } diff --git a/Swift/Controllers/HistoryViewController.h b/Swift/Controllers/HistoryViewController.h index f44c968..75fc460 100644 --- a/Swift/Controllers/HistoryViewController.h +++ b/Swift/Controllers/HistoryViewController.h @@ -4,64 +4,72 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ +/* + * Copyright (c) 2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + #pragma once -#include <Swiften/Base/boost_bsignals.h> +#include <memory> +#include <set> + #include <boost/bind.hpp> -#include <boost/shared_ptr.hpp> +#include <boost/signals2.hpp> -#include <Swift/Controllers/UIEvents/UIEventStream.h> +#include <Swiften/History/HistoryStorage.h> #include <Swiften/JID/JID.h> #include <Swiften/Presence/PresenceOracle.h> -#include <Swiften/History/HistoryStorage.h> -#include <set> + +#include <Swift/Controllers/UIEvents/UIEventStream.h> namespace Swift { - class HistoryWindowFactory; - class HistoryWindow; - class Roster; - class RosterItem; - class ContactRosterItem; - class HistoryController; - class NickResolver; - class AvatarManager; + class HistoryWindowFactory; + class HistoryWindow; + class Roster; + class RosterItem; + class ContactRosterItem; + class HistoryController; + class NickResolver; + class AvatarManager; - class HistoryViewController { - public: - HistoryViewController(const JID& selfJID, UIEventStream* uiEventStream, HistoryController* historyController, NickResolver* nickResolver, AvatarManager* avatarManager, PresenceOracle* presenceOracle, HistoryWindowFactory* historyWindowFactory); - ~HistoryViewController(); + class HistoryViewController { + public: + HistoryViewController(const JID& selfJID, UIEventStream* uiEventStream, HistoryController* historyController, NickResolver* nickResolver, AvatarManager* avatarManager, PresenceOracle* presenceOracle, HistoryWindowFactory* historyWindowFactory); + ~HistoryViewController(); - private: - void handleUIEvent(boost::shared_ptr<UIEvent> event); - void handleSelectedContactChanged(RosterItem* item); - void handleNewMessage(const HistoryMessage& message); - void handleReturnPressed(const std::string& keyword); - void handleScrollReachedTop(const boost::gregorian::date& date); - void handleScrollReachedBottom(const boost::gregorian::date& date); - void handlePreviousButtonClicked(); - void handleNextButtonClicked(); - void handleCalendarClicked(const boost::gregorian::date& date); - void handlePresenceChanged(Presence::ref presence); - void handleAvatarChanged(const JID& jid); + private: + void handleUIEvent(std::shared_ptr<UIEvent> event); + void handleSelectedContactChanged(RosterItem* item); + void handleNewMessage(const HistoryMessage& message); + void handleReturnPressed(const std::string& keyword); + void handleScrollReachedTop(const boost::gregorian::date& date); + void handleScrollReachedBottom(const boost::gregorian::date& date); + void handlePreviousButtonClicked(); + void handleNextButtonClicked(); + void handleCalendarClicked(const boost::gregorian::date& date); + void handlePresenceChanged(Presence::ref presence); + void handleAvatarChanged(const JID& jid); - void addNewMessage(const HistoryMessage& message, bool addAtTheTop); - void reset(); - Presence::ref getPresence(const JID& jid, bool isMUC); + void addNewMessage(const HistoryMessage& message, bool addAtTheTop); + void reset(); + Presence::ref getPresence(const JID& jid, bool isMUC); - private: - JID selfJID_; - UIEventStream* uiEventStream_; - HistoryController* historyController_; - NickResolver* nickResolver_; - AvatarManager* avatarManager_; - PresenceOracle* presenceOracle_; - HistoryWindowFactory* historyWindowFactory_; - HistoryWindow* historyWindow_; - Roster* roster_; + private: + JID selfJID_; + UIEventStream* uiEventStream_; + HistoryController* historyController_; + NickResolver* nickResolver_; + AvatarManager* avatarManager_; + PresenceOracle* presenceOracle_; + HistoryWindowFactory* historyWindowFactory_; + HistoryWindow* historyWindow_; + Roster* roster_; - std::map<HistoryMessage::Type, ContactsMap> contacts_; - ContactRosterItem* selectedItem_; - HistoryMessage::Type selectedItemType_; - boost::gregorian::date currentResultDate_; - }; + std::map<HistoryMessage::Type, ContactsMap> contacts_; + ContactRosterItem* selectedItem_; + HistoryMessage::Type selectedItemType_ = HistoryMessage::Chat; + boost::gregorian::date currentResultDate_; + }; } diff --git a/Swift/Controllers/Intl.h b/Swift/Controllers/Intl.h index 3ed664d..c599493 100644 --- a/Swift/Controllers/Intl.h +++ b/Swift/Controllers/Intl.h @@ -1,12 +1,11 @@ /* - * Copyright (c) 2011 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2011-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once #include <Swift/Controllers/Translator.h> - #define QT_TRANSLATE_NOOP(context, text) \ - Swift::Translator::getInstance()->translate(text, context) + Swift::Translator::getInstance()->translate(text, context) diff --git a/Swift/Controllers/MainController.cpp b/Swift/Controllers/MainController.cpp deleted file mode 100644 index 14f0727..0000000 --- a/Swift/Controllers/MainController.cpp +++ /dev/null @@ -1,891 +0,0 @@ -/* - * Copyright (c) 2010-2013 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. - */ - -/* - * Copyright (c) 2013 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. - */ - -#include <Swift/Controllers/MainController.h> - -#include <stdlib.h> - -#include <boost/bind.hpp> -#include <boost/lexical_cast.hpp> -#include <boost/shared_ptr.hpp> -#include <boost/smart_ptr/make_shared.hpp> - -#include <Swiften/Base/format.h> -#include <Swiften/Base/Algorithm.h> -#include <Swiften/Base/String.h> -#include <Swiften/StringCodecs/Base64.h> -#include <Swiften/Network/TimerFactory.h> -#include <Swiften/Client/Storages.h> -#include <Swiften/VCards/VCardManager.h> -#include <Swiften/Client/NickResolver.h> -#include <Swiften/Base/foreach.h> -#include <Swiften/Client/Client.h> -#include <Swiften/Presence/PresenceSender.h> -#include <Swiften/Elements/ChatState.h> -#include <Swiften/Elements/Presence.h> -#include <Swiften/Elements/VCardUpdate.h> -#include <Swiften/Elements/DiscoInfo.h> -#include <Swiften/Disco/CapsInfoGenerator.h> -#include <Swiften/Disco/GetDiscoInfoRequest.h> -#include <Swiften/Disco/ClientDiscoManager.h> -#include <Swiften/VCards/GetVCardRequest.h> -#include <Swiften/StringCodecs/Hexify.h> -#include <Swiften/Network/NetworkFactories.h> -#include <Swiften/FileTransfer/FileTransferManager.h> -#include <Swiften/Client/ClientXMLTracer.h> -#include <Swiften/Client/StanzaChannel.h> -#include <Swiften/Client/ClientBlockListManager.h> -#include <Swiften/Crypto/CryptoProvider.h> - -#include <SwifTools/Dock/Dock.h> -#include <SwifTools/Notifier/TogglableNotifier.h> -#include <SwifTools/Idle/IdleDetector.h> - -#include <Swift/Controllers/Intl.h> -#include <Swift/Controllers/UIInterfaces/UIFactory.h> -#include <Swift/Controllers/BuildVersion.h> -#include <Swift/Controllers/Chat/UserSearchController.h> -#include <Swift/Controllers/Chat/ChatsManager.h> -#include <Swift/Controllers/XMPPEvents/EventController.h> -#include <Swift/Controllers/EventWindowController.h> -#include <Swift/Controllers/UIInterfaces/LoginWindow.h> -#include <Swift/Controllers/UIInterfaces/LoginWindowFactory.h> -#include <Swift/Controllers/UIInterfaces/MainWindow.h> -#include <Swift/Controllers/Chat/MUCController.h> -#include <Swift/Controllers/Roster/RosterController.h> -#include <Swift/Controllers/SoundEventController.h> -#include <Swift/Controllers/SoundPlayer.h> -#include <Swift/Controllers/StatusTracker.h> -#include <Swift/Controllers/SystemTray.h> -#include <Swift/Controllers/SystemTrayController.h> -#include <Swift/Controllers/XMLConsoleController.h> -#include <Swift/Controllers/HistoryController.h> -#include <Swift/Controllers/HistoryViewController.h> -#include <Swift/Controllers/FileTransferListController.h> -#include <Swift/Controllers/UIEvents/UIEventStream.h> -#include <Swift/Controllers/PresenceNotifier.h> -#include <Swift/Controllers/EventNotifier.h> -#include <Swift/Controllers/Storages/StoragesFactory.h> -#include <Swift/Controllers/WhiteboardManager.h> -#include <Swift/Controllers/Settings/SettingsProvider.h> -#include <Swift/Controllers/UIEvents/RequestChatUIEvent.h> -#include <Swift/Controllers/UIEvents/JoinMUCUIEvent.h> -#include <Swift/Controllers/Storages/CertificateStorageFactory.h> -#include <Swift/Controllers/Storages/CertificateStorageTrustChecker.h> -#include <Swift/Controllers/ProfileController.h> -#include <Swift/Controllers/ShowProfileController.h> -#include <Swift/Controllers/ContactEditController.h> -#include <Swift/Controllers/XMPPURIController.h> -#include <Swift/Controllers/AdHocManager.h> -#include <Swift/Controllers/FileTransfer/FileTransferOverview.h> -#include <Swift/Controllers/SettingConstants.h> -#include <Swift/Controllers/HighlightManager.h> -#include <Swift/Controllers/HighlightEditorController.h> -#include <Swift/Controllers/BlockListController.h> -#include <Swiften/Crypto/CryptoProvider.h> -#include <Swift/Controllers/ContactSuggester.h> -#include <Swift/Controllers/ContactsFromXMPPRoster.h> - -namespace Swift { - -static const std::string CLIENT_NAME = "Swift"; -static const std::string CLIENT_NODE = "http://swift.im"; - - -MainController::MainController( - EventLoop* eventLoop, - NetworkFactories* networkFactories, - UIFactory* uiFactories, - SettingsProvider* settings, - SystemTray* systemTray, - SoundPlayer* soundPlayer, - StoragesFactory* storagesFactory, - CertificateStorageFactory* certificateStorageFactory, - Dock* dock, - Notifier* notifier, - URIHandler* uriHandler, - IdleDetector* idleDetector, - const std::map<std::string, std::string>& emoticons, - bool useDelayForLatency) : - eventLoop_(eventLoop), - networkFactories_(networkFactories), - uiFactory_(uiFactories), - storagesFactory_(storagesFactory), - certificateStorageFactory_(certificateStorageFactory), - settings_(settings), - uriHandler_(uriHandler), - idleDetector_(idleDetector), - loginWindow_(NULL) , - useDelayForLatency_(useDelayForLatency), - ftOverview_(NULL), - emoticons_(emoticons) { - storages_ = NULL; - certificateStorage_ = NULL; - statusTracker_ = NULL; - presenceNotifier_ = NULL; - eventNotifier_ = NULL; - rosterController_ = NULL; - chatsManager_ = NULL; - historyController_ = NULL; - historyViewController_ = NULL; - eventWindowController_ = NULL; - profileController_ = NULL; - blockListController_ = NULL; - showProfileController_ = NULL; - contactEditController_ = NULL; - userSearchControllerChat_ = NULL; - userSearchControllerAdd_ = NULL; - userSearchControllerInvite_ = NULL; - contactsFromRosterProvider_ = NULL; - contactSuggesterWithoutRoster_ = NULL; - contactSuggesterWithRoster_ = NULL; - whiteboardManager_ = NULL; - adHocManager_ = NULL; - quitRequested_ = false; - clientInitialized_ = false; - offlineRequested_ = false; - - timeBeforeNextReconnect_ = -1; - dock_ = dock; - uiEventStream_ = new UIEventStream(); - - notifier_ = new TogglableNotifier(notifier); - notifier_->setPersistentEnabled(settings_->getSetting(SettingConstants::SHOW_NOTIFICATIONS)); - eventController_ = new EventController(); - eventController_->onEventQueueLengthChange.connect(boost::bind(&MainController::handleEventQueueLengthChange, this, _1)); - - systemTrayController_ = new SystemTrayController(eventController_, systemTray); - loginWindow_ = uiFactory_->createLoginWindow(uiEventStream_); - loginWindow_->setShowNotificationToggle(!notifier->isExternallyConfigured()); - - highlightManager_ = new HighlightManager(settings_); - highlightEditorController_ = new HighlightEditorController(uiEventStream_, uiFactory_, highlightManager_); - - soundEventController_ = new SoundEventController(eventController_, soundPlayer, settings, highlightManager_); - - xmppURIController_ = new XMPPURIController(uriHandler_, uiEventStream_); - - std::string selectedLoginJID = settings_->getSetting(SettingConstants::LAST_LOGIN_JID); - bool loginAutomatically = settings_->getSetting(SettingConstants::LOGIN_AUTOMATICALLY); - std::string cachedPassword; - std::string cachedCertificate; - ClientOptions cachedOptions; - bool eagle = settings_->getSetting(SettingConstants::FORGET_PASSWORDS); - if (!eagle) { - foreach (std::string profile, settings->getAvailableProfiles()) { - ProfileSettingsProvider profileSettings(profile, settings); - std::string password = profileSettings.getStringSetting("pass"); - std::string certificate = profileSettings.getStringSetting("certificate"); - std::string jid = profileSettings.getStringSetting("jid"); - ClientOptions clientOptions = parseClientOptions(profileSettings.getStringSetting("options")); - loginWindow_->addAvailableAccount(jid, password, certificate, clientOptions); - if (jid == selectedLoginJID) { - cachedPassword = password; - cachedCertificate = certificate; - cachedOptions = clientOptions; - } - } - loginWindow_->selectUser(selectedLoginJID); - loginWindow_->setLoginAutomatically(loginAutomatically); - } - - - loginWindow_->onLoginRequest.connect(boost::bind(&MainController::handleLoginRequest, this, _1, _2, _3, _4, _5, _6, _7)); - loginWindow_->onPurgeSavedLoginRequest.connect(boost::bind(&MainController::handlePurgeSavedLoginRequest, this, _1)); - loginWindow_->onCancelLoginRequest.connect(boost::bind(&MainController::handleCancelLoginRequest, this)); - loginWindow_->onQuitRequest.connect(boost::bind(&MainController::handleQuitRequest, this)); - - idleDetector_->setIdleTimeSeconds(settings->getSetting(SettingConstants::IDLE_TIMEOUT)); - idleDetector_->onIdleChanged.connect(boost::bind(&MainController::handleInputIdleChanged, this, _1)); - - xmlConsoleController_ = new XMLConsoleController(uiEventStream_, uiFactory_); - - fileTransferListController_ = new FileTransferListController(uiEventStream_, uiFactory_); - - settings_->onSettingChanged.connect(boost::bind(&MainController::handleSettingChanged, this, _1)); - - if (loginAutomatically) { - profileSettings_ = new ProfileSettingsProvider(selectedLoginJID, settings_); - /* FIXME: deal with autologin with a cert*/ - handleLoginRequest(selectedLoginJID, cachedPassword, cachedCertificate, CertificateWithKey::ref(), cachedOptions, true, true); - } else { - profileSettings_ = NULL; - } -} - -MainController::~MainController() { - idleDetector_->onIdleChanged.disconnect(boost::bind(&MainController::handleInputIdleChanged, this, _1)); - - purgeCachedCredentials(); - //setManagersOffline(); - eventController_->disconnectAll(); - - resetClient(); - delete highlightEditorController_; - delete highlightManager_; - delete fileTransferListController_; - delete xmlConsoleController_; - delete xmppURIController_; - delete soundEventController_; - delete systemTrayController_; - delete eventController_; - delete notifier_; - delete uiEventStream_; -} - -void MainController::purgeCachedCredentials() { - safeClear(password_); -} - -void MainController::resetClient() { - purgeCachedCredentials(); - resetCurrentError(); - resetPendingReconnects(); - vCardPhotoHash_.clear(); - delete contactEditController_; - contactEditController_ = NULL; - delete profileController_; - profileController_ = NULL; - delete showProfileController_; - showProfileController_ = NULL; - delete eventWindowController_; - eventWindowController_ = NULL; - delete chatsManager_; - chatsManager_ = NULL; -#ifdef SWIFT_EXPERIMENTAL_HISTORY - delete historyViewController_; - historyViewController_ = NULL; - delete historyController_; - historyController_ = NULL; -#endif - delete ftOverview_; - ftOverview_ = NULL; - delete rosterController_; - rosterController_ = NULL; - delete eventNotifier_; - eventNotifier_ = NULL; - delete presenceNotifier_; - presenceNotifier_ = NULL; - delete certificateStorage_; - certificateStorage_ = NULL; - delete storages_; - storages_ = NULL; - delete statusTracker_; - statusTracker_ = NULL; - delete profileSettings_; - profileSettings_ = NULL; - delete userSearchControllerChat_; - userSearchControllerChat_ = NULL; - delete userSearchControllerAdd_; - userSearchControllerAdd_ = NULL; - delete userSearchControllerInvite_; - userSearchControllerInvite_ = NULL; - delete contactSuggesterWithoutRoster_; - contactSuggesterWithoutRoster_ = NULL; - delete contactSuggesterWithRoster_; - contactSuggesterWithRoster_ = NULL; - delete contactsFromRosterProvider_; - contactsFromRosterProvider_ = NULL; - delete adHocManager_; - adHocManager_ = NULL; - delete whiteboardManager_; - whiteboardManager_ = NULL; - clientInitialized_ = false; -} - -void MainController::handleSettingChanged(const std::string& settingPath) { - if (settingPath == SettingConstants::SHOW_NOTIFICATIONS.getKey()) { - notifier_->setPersistentEnabled(settings_->getSetting(SettingConstants::SHOW_NOTIFICATIONS)); - } -} - -void MainController::resetPendingReconnects() { - timeBeforeNextReconnect_ = -1; - if (reconnectTimer_) { - reconnectTimer_->stop(); - reconnectTimer_.reset(); - } - resetCurrentError(); -} - -void MainController::resetCurrentError() { - if (lastDisconnectError_) { - lastDisconnectError_->conclude(); - lastDisconnectError_ = boost::shared_ptr<ErrorEvent>(); - } -} - -void MainController::handleConnected() { - boundJID_ = client_->getJID(); - resetCurrentError(); - resetPendingReconnects(); - - if (settings_->getSetting(SettingConstants::FORGET_PASSWORDS)) { - purgeCachedCredentials(); - } - - bool freshLogin = rosterController_ == NULL; - myStatusLooksOnline_ = true; - if (freshLogin) { - profileController_ = new ProfileController(client_->getVCardManager(), uiFactory_, uiEventStream_); - showProfileController_ = new ShowProfileController(client_->getVCardManager(), uiFactory_, uiEventStream_); - ftOverview_ = new FileTransferOverview(client_->getFileTransferManager()); - fileTransferListController_->setFileTransferOverview(ftOverview_); - rosterController_ = new RosterController(jid_, client_->getRoster(), client_->getAvatarManager(), uiFactory_, client_->getNickManager(), client_->getNickResolver(), client_->getPresenceOracle(), client_->getSubscriptionManager(), eventController_, uiEventStream_, client_->getIQRouter(), settings_, client_->getEntityCapsProvider(), ftOverview_, client_->getClientBlockListManager()); - rosterController_->onChangeStatusRequest.connect(boost::bind(&MainController::handleChangeStatusRequest, this, _1, _2)); - rosterController_->onSignOutRequest.connect(boost::bind(&MainController::signOut, this)); - rosterController_->getWindow()->onShowCertificateRequest.connect(boost::bind(&MainController::handleShowCertificateRequest, this)); - - blockListController_ = new BlockListController(client_->getClientBlockListManager(), uiEventStream_, uiFactory_, eventController_); - - contactEditController_ = new ContactEditController(rosterController_, client_->getVCardManager(), uiFactory_, uiEventStream_); - whiteboardManager_ = new WhiteboardManager(uiFactory_, uiEventStream_, client_->getNickResolver(), client_->getWhiteboardSessionManager()); - - /* Doing this early as an ordering fix. Various things later will - * want to have the user's nick available and this means it will - * be before they receive stanzas that need it (e.g. bookmarks).*/ - client_->getVCardManager()->requestOwnVCard(); - - contactSuggesterWithoutRoster_ = new ContactSuggester(); - contactSuggesterWithRoster_ = new ContactSuggester(); - - userSearchControllerInvite_ = new UserSearchController(UserSearchController::InviteToChat, jid_, uiEventStream_, client_->getVCardManager(), uiFactory_, client_->getIQRouter(), rosterController_, contactSuggesterWithRoster_, client_->getAvatarManager(), client_->getPresenceOracle()); -#ifdef SWIFT_EXPERIMENTAL_HISTORY - historyController_ = new HistoryController(storages_->getHistoryStorage()); - historyViewController_ = new HistoryViewController(jid_, uiEventStream_, historyController_, client_->getNickResolver(), client_->getAvatarManager(), client_->getPresenceOracle(), uiFactory_); - chatsManager_ = new ChatsManager(jid_, client_->getStanzaChannel(), client_->getIQRouter(), eventController_, uiFactory_, uiFactory_, client_->getNickResolver(), client_->getPresenceOracle(), client_->getPresenceSender(), uiEventStream_, uiFactory_, useDelayForLatency_, networkFactories_->getTimerFactory(), client_->getMUCRegistry(), client_->getEntityCapsProvider(), client_->getMUCManager(), uiFactory_, profileSettings_, ftOverview_, client_->getRoster(), !settings_->getSetting(SettingConstants::REMEMBER_RECENT_CHATS), settings_, historyController_, whiteboardManager_, highlightManager_, client_->getClientBlockListManager(), emoticons_, userSearchControllerInvite_); -#else - chatsManager_ = new ChatsManager(jid_, client_->getStanzaChannel(), client_->getIQRouter(), eventController_, uiFactory_, uiFactory_, client_->getNickResolver(), client_->getPresenceOracle(), client_->getPresenceSender(), uiEventStream_, uiFactory_, useDelayForLatency_, networkFactories_->getTimerFactory(), client_->getMUCRegistry(), client_->getEntityCapsProvider(), client_->getMUCManager(), uiFactory_, profileSettings_, ftOverview_, client_->getRoster(), !settings_->getSetting(SettingConstants::REMEMBER_RECENT_CHATS), settings_, NULL, whiteboardManager_, highlightManager_, client_->getClientBlockListManager(), &emoticons_, userSearchControllerInvite_); -#endif - contactsFromRosterProvider_ = new ContactsFromXMPPRoster(client_->getRoster(), client_->getAvatarManager(), client_->getPresenceOracle()); - contactSuggesterWithoutRoster_->addContactProvider(chatsManager_); - contactSuggesterWithRoster_->addContactProvider(chatsManager_); - contactSuggesterWithRoster_->addContactProvider(contactsFromRosterProvider_); - - client_->onMessageReceived.connect(boost::bind(&ChatsManager::handleIncomingMessage, chatsManager_, _1)); - chatsManager_->setAvatarManager(client_->getAvatarManager()); - - eventWindowController_ = new EventWindowController(eventController_, uiFactory_); - - loginWindow_->morphInto(rosterController_->getWindow()); - - DiscoInfo discoInfo; - discoInfo.addIdentity(DiscoInfo::Identity(CLIENT_NAME, "client", "pc")); - discoInfo.addFeature(DiscoInfo::ChatStatesFeature); - discoInfo.addFeature(DiscoInfo::SecurityLabelsFeature); - discoInfo.addFeature(DiscoInfo::MessageCorrectionFeature); -#ifdef SWIFT_EXPERIMENTAL_FT - discoInfo.addFeature(DiscoInfo::JingleFeature); - discoInfo.addFeature(DiscoInfo::JingleFTFeature); - discoInfo.addFeature(DiscoInfo::JingleTransportsIBBFeature); - discoInfo.addFeature(DiscoInfo::JingleTransportsS5BFeature); -#endif -#ifdef SWIFT_EXPERIMENTAL_WB - discoInfo.addFeature(DiscoInfo::WhiteboardFeature); -#endif - discoInfo.addFeature(DiscoInfo::MessageDeliveryReceiptsFeature); - client_->getDiscoManager()->setCapsNode(CLIENT_NODE); - client_->getDiscoManager()->setDiscoInfo(discoInfo); - - userSearchControllerChat_ = new UserSearchController(UserSearchController::StartChat, jid_, uiEventStream_, client_->getVCardManager(), uiFactory_, client_->getIQRouter(), rosterController_, contactSuggesterWithRoster_, client_->getAvatarManager(), client_->getPresenceOracle()); - userSearchControllerAdd_ = new UserSearchController(UserSearchController::AddContact, jid_, uiEventStream_, client_->getVCardManager(), uiFactory_, client_->getIQRouter(), rosterController_, contactSuggesterWithoutRoster_, client_->getAvatarManager(), client_->getPresenceOracle()); - adHocManager_ = new AdHocManager(JID(boundJID_.getDomain()), uiFactory_, client_->getIQRouter(), uiEventStream_, rosterController_->getWindow()); - - chatsManager_->onImpromptuMUCServiceDiscovered.connect(boost::bind(&UserSearchController::setCanInitiateImpromptuMUC, userSearchControllerChat_, _1)); - } - loginWindow_->setIsLoggingIn(false); - - client_->requestRoster(); - - GetDiscoInfoRequest::ref discoInfoRequest = GetDiscoInfoRequest::create(JID(boundJID_.getDomain()), client_->getIQRouter()); - discoInfoRequest->onResponse.connect(boost::bind(&MainController::handleServerDiscoInfoResponse, this, _1, _2)); - discoInfoRequest->send(); - - client_->getVCardManager()->requestOwnVCard(); - - rosterController_->setEnabled(true); - rosterController_->getWindow()->setStreamEncryptionStatus(client_->isStreamEncrypted()); - profileController_->setAvailable(true); - contactEditController_->setAvailable(true); - /* Send presence later to catch all the incoming presences. */ - sendPresence(statusTracker_->getNextPresence()); - - /* Enable chats last of all, so rejoining MUCs has the right sent presence */ - assert(chatsManager_); - chatsManager_->setOnline(true); - -} - -void MainController::handleEventQueueLengthChange(int count) { - dock_->setNumberOfPendingMessages(count); -} - -void MainController::reconnectAfterError() { - if (reconnectTimer_) { - reconnectTimer_->stop(); - } - performLoginFromCachedCredentials(); -} - -void MainController::handleChangeStatusRequest(StatusShow::Type show, const std::string &statusText) { - boost::shared_ptr<Presence> presence(new Presence()); - if (show == StatusShow::None) { - // Note: this is misleading, None doesn't mean unavailable on the wire. - presence->setType(Presence::Unavailable); - resetPendingReconnects(); - myStatusLooksOnline_ = false; - offlineRequested_ = true; - } - else { - offlineRequested_ = false; - presence->setShow(show); - } - presence->setStatus(statusText); - statusTracker_->setRequestedPresence(presence); - if (presence->getType() != Presence::Unavailable) { - profileSettings_->storeInt("lastShow", presence->getShow()); - profileSettings_->storeString("lastStatus", presence->getStatus()); - } - if (presence->getType() != Presence::Unavailable && !client_->isAvailable()) { - performLoginFromCachedCredentials(); - } else { - sendPresence(presence); - } -} - -void MainController::sendPresence(boost::shared_ptr<Presence> presence) { - rosterController_->getWindow()->setMyStatusType(presence->getShow()); - rosterController_->getWindow()->setMyStatusText(presence->getStatus()); - systemTrayController_->setMyStatusType(presence->getShow()); - notifier_->setTemporarilyDisabled(presence->getShow() == StatusShow::DND); - - // Add information and send - if (!vCardPhotoHash_.empty()) { - presence->updatePayload(boost::make_shared<VCardUpdate>(vCardPhotoHash_)); - } - client_->getPresenceSender()->sendPresence(presence); - if (presence->getType() == Presence::Unavailable) { - logout(); - } -} - -void MainController::handleInputIdleChanged(bool idle) { - if (!statusTracker_) { - //Haven't logged in yet. - return; - } - - if (settings_->getSetting(SettingConstants::IDLE_GOES_OFFLINE)) { - if (idle) { - logout(); - } - } - else { - if (idle) { - if (statusTracker_->goAutoAway(idleDetector_->getIdleTimeSeconds())) { - if (client_ && client_->isAvailable()) { - sendPresence(statusTracker_->getNextPresence()); - } - } - } else { - if (statusTracker_->goAutoUnAway()) { - if (client_ && client_->isAvailable()) { - sendPresence(statusTracker_->getNextPresence()); - } - } - } - } -} - -void MainController::handleShowCertificateRequest() { - std::vector<Certificate::ref> chain = client_->getStanzaChannel()->getPeerCertificateChain(); - rosterController_->getWindow()->openCertificateDialog(chain); -} - -void MainController::handleLoginRequest(const std::string &username, const std::string &password, const std::string& certificatePath, CertificateWithKey::ref certificate, const ClientOptions& options, bool remember, bool loginAutomatically) { - jid_ = JID(username); - if (!jid_.isValid() || jid_.getNode().empty()) { - loginWindow_->setMessage(QT_TRANSLATE_NOOP("", "User address invalid. User address should be of the form 'alice@wonderland.lit'")); - loginWindow_->setIsLoggingIn(false); - } else { - loginWindow_->setMessage(""); - loginWindow_->setIsLoggingIn(true); - profileSettings_ = new ProfileSettingsProvider(username, settings_); - if (!settings_->getSetting(SettingConstants::FORGET_PASSWORDS)) { - profileSettings_->storeString("jid", username); - profileSettings_->storeString("certificate", certificatePath); - profileSettings_->storeString("pass", (remember || loginAutomatically) ? password : ""); - std::string optionString = serializeClientOptions(options); - profileSettings_->storeString("options", optionString); - settings_->storeSetting(SettingConstants::LAST_LOGIN_JID, username); - settings_->storeSetting(SettingConstants::LOGIN_AUTOMATICALLY, loginAutomatically); - loginWindow_->addAvailableAccount(profileSettings_->getStringSetting("jid"), profileSettings_->getStringSetting("pass"), profileSettings_->getStringSetting("certificate"), options); - } - - password_ = password; - certificate_ = certificate; - clientOptions_ = options; - performLoginFromCachedCredentials(); - } -} - -void MainController::handlePurgeSavedLoginRequest(const std::string& username) { - settings_->removeProfile(username); - loginWindow_->removeAvailableAccount(username); -} - -void MainController::performLoginFromCachedCredentials() { - if (settings_->getSetting(SettingConstants::FORGET_PASSWORDS) && password_.empty()) { - /* Then we can't try to login again. */ - return; - } - /* If we logged in with a bare JID, and we have a full bound JID, re-login with the - * bound JID to try and keep dynamically assigned resources */ - JID clientJID = jid_; - if (boundJID_.isValid() && jid_.isBare() && boundJID_.toBare() == jid_) { - clientJID = boundJID_; - } - - if (!statusTracker_) { - statusTracker_ = new StatusTracker(); - } - if (!clientInitialized_) { - storages_ = storagesFactory_->createStorages(jid_.toBare()); - certificateStorage_ = certificateStorageFactory_->createCertificateStorage(jid_.toBare()); - certificateTrustChecker_ = new CertificateStorageTrustChecker(certificateStorage_); - - client_ = boost::make_shared<Swift::Client>(clientJID, createSafeByteArray(password_.c_str()), networkFactories_, storages_); - clientInitialized_ = true; - client_->setCertificateTrustChecker(certificateTrustChecker_); - client_->onDataRead.connect(boost::bind(&XMLConsoleController::handleDataRead, xmlConsoleController_, _1)); - client_->onDataWritten.connect(boost::bind(&XMLConsoleController::handleDataWritten, xmlConsoleController_, _1)); - client_->onDisconnected.connect(boost::bind(&MainController::handleDisconnected, this, _1)); - client_->onConnected.connect(boost::bind(&MainController::handleConnected, this)); - - client_->setSoftwareVersion(CLIENT_NAME, buildVersion); - - client_->getVCardManager()->onVCardChanged.connect(boost::bind(&MainController::handleVCardReceived, this, _1, _2)); - presenceNotifier_ = new PresenceNotifier(client_->getStanzaChannel(), notifier_, client_->getMUCRegistry(), client_->getAvatarManager(), client_->getNickResolver(), client_->getPresenceOracle(), networkFactories_->getTimerFactory()); - presenceNotifier_->onNotificationActivated.connect(boost::bind(&MainController::handleNotificationClicked, this, _1)); - eventNotifier_ = new EventNotifier(eventController_, notifier_, client_->getAvatarManager(), client_->getNickResolver()); - eventNotifier_->onNotificationActivated.connect(boost::bind(&MainController::handleNotificationClicked, this, _1)); - if (certificate_ && !certificate_->isNull()) { - client_->setCertificate(certificate_); - } - boost::shared_ptr<Presence> presence(new Presence()); - presence->setShow(static_cast<StatusShow::Type>(profileSettings_->getIntSetting("lastShow", StatusShow::Online))); - presence->setStatus(profileSettings_->getStringSetting("lastStatus")); - statusTracker_->setRequestedPresence(presence); - } else { - /* In case we're in the middle of another login, make sure they don't overlap */ - client_->disconnect(); - } - systemTrayController_->setConnecting(); - if (rosterController_) { - rosterController_->getWindow()->setConnecting(); - } - ClientOptions clientOptions = clientOptions_; - bool eagle = settings_->getSetting(SettingConstants::FORGET_PASSWORDS); - clientOptions.forgetPassword = eagle; - clientOptions.useTLS = eagle ? ClientOptions::RequireTLS : clientOptions_.useTLS; - client_->connect(clientOptions); -} - -void MainController::handleDisconnected(const boost::optional<ClientError>& error) { - if (rosterController_) { - rosterController_->getWindow()->setStreamEncryptionStatus(false); - } - if (settings_->getSetting(SettingConstants::FORGET_PASSWORDS)) { - purgeCachedCredentials(); - } - if (quitRequested_) { - resetClient(); - loginWindow_->quit(); - } - else if (error) { - std::string message; - std::string certificateErrorMessage; - bool forceSignout = false; - switch(error->getType()) { - case ClientError::UnknownError: message = QT_TRANSLATE_NOOP("", "Unknown Error"); break; - case ClientError::DomainNameResolveError: message = QT_TRANSLATE_NOOP("", "Unable to find server"); break; - case ClientError::ConnectionError: message = QT_TRANSLATE_NOOP("", "Error connecting to server"); break; - case ClientError::ConnectionReadError: message = QT_TRANSLATE_NOOP("", "Error while receiving server data"); break; - case ClientError::ConnectionWriteError: message = QT_TRANSLATE_NOOP("", "Error while sending data to the server"); break; - case ClientError::XMLError: message = QT_TRANSLATE_NOOP("", "Error parsing server data"); break; - case ClientError::AuthenticationFailedError: message = QT_TRANSLATE_NOOP("", "Login/password invalid"); break; - case ClientError::CompressionFailedError: message = QT_TRANSLATE_NOOP("", "Error while compressing stream"); break; - case ClientError::ServerVerificationFailedError: message = QT_TRANSLATE_NOOP("", "Server verification failed"); break; - case ClientError::NoSupportedAuthMechanismsError: message = QT_TRANSLATE_NOOP("", "Authentication mechanisms not supported"); break; - case ClientError::UnexpectedElementError: message = QT_TRANSLATE_NOOP("", "Unexpected response"); break; - case ClientError::ResourceBindError: message = QT_TRANSLATE_NOOP("", "Error binding resource"); break; - case ClientError::SessionStartError: message = QT_TRANSLATE_NOOP("", "Error starting session"); break; - case ClientError::StreamError: message = QT_TRANSLATE_NOOP("", "Stream error"); break; - case ClientError::TLSError: message = QT_TRANSLATE_NOOP("", "Encryption error"); break; - case ClientError::ClientCertificateLoadError: message = QT_TRANSLATE_NOOP("", "Error loading certificate (Invalid password?)"); break; - case ClientError::ClientCertificateError: message = QT_TRANSLATE_NOOP("", "Certificate not authorized"); break; - case ClientError::CertificateCardRemoved: message = QT_TRANSLATE_NOOP("", "Certificate card removed"); forceSignout = true; break; - - case ClientError::UnknownCertificateError: certificateErrorMessage = QT_TRANSLATE_NOOP("", "Unknown certificate"); break; - case ClientError::CertificateExpiredError: certificateErrorMessage = QT_TRANSLATE_NOOP("", "Certificate has expired"); break; - case ClientError::CertificateNotYetValidError: certificateErrorMessage = QT_TRANSLATE_NOOP("", "Certificate is not yet valid"); break; - case ClientError::CertificateSelfSignedError: certificateErrorMessage = QT_TRANSLATE_NOOP("", "Certificate is self-signed"); break; - case ClientError::CertificateRejectedError: certificateErrorMessage = QT_TRANSLATE_NOOP("", "Certificate has been rejected"); break; - case ClientError::CertificateUntrustedError: certificateErrorMessage = QT_TRANSLATE_NOOP("", "Certificate is not trusted"); break; - case ClientError::InvalidCertificatePurposeError: certificateErrorMessage = QT_TRANSLATE_NOOP("", "Certificate cannot be used for encrypting your connection"); break; - case ClientError::CertificatePathLengthExceededError: certificateErrorMessage = QT_TRANSLATE_NOOP("", "Certificate path length constraint exceeded"); break; - case ClientError::InvalidCertificateSignatureError: certificateErrorMessage = QT_TRANSLATE_NOOP("", "Invalid certificate signature"); break; - case ClientError::InvalidCAError: certificateErrorMessage = QT_TRANSLATE_NOOP("", "Invalid Certificate Authority"); break; - case ClientError::InvalidServerIdentityError: certificateErrorMessage = QT_TRANSLATE_NOOP("", "Certificate does not match the host identity"); break; - case ClientError::RevokedError: certificateErrorMessage = QT_TRANSLATE_NOOP("", "Certificate has been revoked"); break; - case ClientError::RevocationCheckFailedError: certificateErrorMessage = QT_TRANSLATE_NOOP("", "Unable to determine certificate revocation state"); break; - } - bool forceReconnectAfterCertificateTrust = false; - if (!certificateErrorMessage.empty()) { - std::vector<Certificate::ref> certificates = certificateTrustChecker_->getLastCertificateChain(); - if (!certificates.empty() && loginWindow_->askUserToTrustCertificatePermanently(certificateErrorMessage, certificates)) { - certificateStorage_->addCertificate(certificates[0]); - forceReconnectAfterCertificateTrust = true; - } - else { - message = QT_TRANSLATE_NOOP("", "Certificate error"); - } - } - - if (forceReconnectAfterCertificateTrust && settings_->getSetting(SettingConstants::FORGET_PASSWORDS)) { - forceReconnectAfterCertificateTrust = false; - forceSignout = true; - message = QT_TRANSLATE_NOOP("", "Re-enter credentials and retry"); - } - - if (forceReconnectAfterCertificateTrust) { - performLoginFromCachedCredentials(); - } - else if (forceSignout || !rosterController_) { //hasn't been logged in yet or permanent error - signOut(); - loginWindow_->setMessage(message); - loginWindow_->setIsLoggingIn(false); - } else { - logout(); - if (settings_->getSetting(SettingConstants::FORGET_PASSWORDS)) { - message = str(format(QT_TRANSLATE_NOOP("", "Disconnected from %1%: %2%. To reconnect, Sign Out and provide your password again.")) % jid_.getDomain() % message); - } else { - if (!offlineRequested_) { - setReconnectTimer(); - } - if (lastDisconnectError_) { - message = str(format(QT_TRANSLATE_NOOP("", "Reconnect to %1% failed: %2%. Will retry in %3% seconds.")) % jid_.getDomain() % message % boost::lexical_cast<std::string>(timeBeforeNextReconnect_)); - lastDisconnectError_->conclude(); - } else { - message = str(format(QT_TRANSLATE_NOOP("", "Disconnected from %1%: %2%.")) % jid_.getDomain() % message); - } - lastDisconnectError_ = boost::make_shared<ErrorEvent>(JID(jid_.getDomain()), message); - eventController_->handleIncomingEvent(lastDisconnectError_); - } - } - } - else if (!rosterController_) { //hasn't been logged in yet - loginWindow_->setIsLoggingIn(false); - } -} - -void MainController::setReconnectTimer() { - if (timeBeforeNextReconnect_ < 0) { - timeBeforeNextReconnect_ = 1; - } else { - timeBeforeNextReconnect_ = timeBeforeNextReconnect_ >= 150 ? 300 : timeBeforeNextReconnect_ * 2; // Randomly selected by roll of a die, as required by 3920bis - } - if (reconnectTimer_) { - reconnectTimer_->stop(); - } - reconnectTimer_ = networkFactories_->getTimerFactory()->createTimer(timeBeforeNextReconnect_ * 1000); - reconnectTimer_->onTick.connect(boost::bind(&MainController::reconnectAfterError, this)); - reconnectTimer_->start(); -} - -void MainController::handleCancelLoginRequest() { - signOut(); -} - -void MainController::signOut() { - if (settings_->getSetting(SettingConstants::FORGET_PASSWORDS)) { - purgeCachedCredentials(); - } - eventController_->clear(); - logout(); - loginWindow_->loggedOut(); - resetClient(); -} - -void MainController::logout() { - if (settings_->getSetting(SettingConstants::FORGET_PASSWORDS)) { - purgeCachedCredentials(); - } - systemTrayController_->setMyStatusType(StatusShow::None); - if (clientInitialized_ /*&& client_->isAvailable()*/) { - client_->disconnect(); - } - if (rosterController_ && myStatusLooksOnline_) { - rosterController_->getWindow()->setMyStatusType(StatusShow::None); - rosterController_->getWindow()->setMyStatusText(""); - myStatusLooksOnline_ = false; - } - setManagersOffline(); -} - -void MainController::setManagersOffline() { - if (chatsManager_) { - chatsManager_->setOnline(false); - } - if (rosterController_) { - rosterController_->setEnabled(false); - } - if (profileController_) { - profileController_->setAvailable(false); - } - if (contactEditController_) { - contactEditController_->setAvailable(false); - } -} - -void MainController::handleServerDiscoInfoResponse(boost::shared_ptr<DiscoInfo> info, ErrorPayload::ref error) { - if (!error) { - chatsManager_->setServerDiscoInfo(info); - adHocManager_->setServerDiscoInfo(info); - if (info->hasFeature(DiscoInfo::BlockingCommandFeature)) { - rosterController_->getWindow()->setBlockingCommandAvailable(true); - rosterController_->initBlockingCommand(); - } - } -} - -void MainController::handleVCardReceived(const JID& jid, VCard::ref vCard) { - if (!jid.equals(jid_, JID::WithoutResource) || !vCard || vCard->getPhoto().empty()) { - return; - } - std::string hash = Hexify::hexify(networkFactories_->getCryptoProvider()->getSHA1Hash(vCard->getPhoto())); - if (hash != vCardPhotoHash_) { - vCardPhotoHash_ = hash; - if (client_ && client_->isAvailable()) { - sendPresence(statusTracker_->getNextPresence()); - } - } -} - -void MainController::handleNotificationClicked(const JID& jid) { - assert(chatsManager_); - if (clientInitialized_) { - if (client_->getMUCRegistry()->isMUC(jid)) { - uiEventStream_->send(boost::make_shared<JoinMUCUIEvent>(jid)); - } - else { - uiEventStream_->send(boost::shared_ptr<UIEvent>(new RequestChatUIEvent(jid))); - } - } -} - -void MainController::handleQuitRequest() { - if (client_ && client_->isActive()) { - quitRequested_ = true; - client_->disconnect(); - } - else { - resetClient(); - loginWindow_->quit(); - } -} - -#define SERIALIZE_BOOL(option) result += options.option ? "1" : "0"; result += ","; -#define SERIALIZE_INT(option) result += boost::lexical_cast<std::string>(options.option); result += ","; -#define SERIALIZE_STRING(option) result += Base64::encode(createByteArray(options.option)); result += ","; -#define SERIALIZE_SAFE_STRING(option) result += safeByteArrayToString(Base64::encode(options.option)); result += ","; -#define SERIALIZE_URL(option) SERIALIZE_STRING(option.toString()) - -std::string MainController::serializeClientOptions(const ClientOptions& options) { - std::string result; - SERIALIZE_BOOL(useStreamCompression); - switch (options.useTLS) { - case ClientOptions::NeverUseTLS: result += "1";break; - case ClientOptions::UseTLSWhenAvailable: result += "2";break; - case ClientOptions::RequireTLS: result += "3";break; - } - result += ","; - SERIALIZE_BOOL(allowPLAINWithoutTLS); - SERIALIZE_BOOL(useStreamResumption); - SERIALIZE_BOOL(useAcks); - SERIALIZE_STRING(manualHostname); - SERIALIZE_INT(manualPort); - switch (options.proxyType) { - case ClientOptions::NoProxy: result += "1";break; - case ClientOptions::SystemConfiguredProxy: result += "2";break; - case ClientOptions::SOCKS5Proxy: result += "3";break; - case ClientOptions::HTTPConnectProxy: result += "4";break; - } - result += ","; - SERIALIZE_STRING(manualProxyHostname); - SERIALIZE_INT(manualProxyPort); - SERIALIZE_URL(boshURL); - SERIALIZE_URL(boshHTTPConnectProxyURL); - SERIALIZE_SAFE_STRING(boshHTTPConnectProxyAuthID); - SERIALIZE_SAFE_STRING(boshHTTPConnectProxyAuthPassword); - return result; -} - -#define CHECK_PARSE_LENGTH if (i >= segments.size()) {return result;} -#define PARSE_INT_RAW(defaultValue) CHECK_PARSE_LENGTH intVal = defaultValue; try {intVal = boost::lexical_cast<int>(segments[i]);} catch(const boost::bad_lexical_cast&) {};i++; -#define PARSE_STRING_RAW CHECK_PARSE_LENGTH stringVal = byteArrayToString(Base64::decode(segments[i]));i++; - -#define PARSE_BOOL(option, defaultValue) PARSE_INT_RAW(defaultValue); result.option = (intVal == 1); -#define PARSE_INT(option, defaultValue) PARSE_INT_RAW(defaultValue); result.option = intVal; -#define PARSE_STRING(option) PARSE_STRING_RAW; result.option = stringVal; -#define PARSE_SAFE_STRING(option) PARSE_STRING_RAW; result.option = SafeString(createSafeByteArray(stringVal)); -#define PARSE_URL(option) {PARSE_STRING_RAW; result.option = URL::fromString(stringVal);} - - -ClientOptions MainController::parseClientOptions(const std::string& optionString) { - ClientOptions result; - size_t i = 0; - int intVal = 0; - std::string stringVal; - std::vector<std::string> segments = String::split(optionString, ','); - - PARSE_BOOL(useStreamCompression, 1); - PARSE_INT_RAW(-1); - switch (intVal) { - case 1: result.useTLS = ClientOptions::NeverUseTLS;break; - case 2: result.useTLS = ClientOptions::UseTLSWhenAvailable;break; - case 3: result.useTLS = ClientOptions::RequireTLS;break; - default:; - } - PARSE_BOOL(allowPLAINWithoutTLS, 0); - PARSE_BOOL(useStreamResumption, 0); - PARSE_BOOL(useAcks, 1); - PARSE_STRING(manualHostname); - PARSE_INT(manualPort, -1); - PARSE_INT_RAW(-1); - switch (intVal) { - case 1: result.proxyType = ClientOptions::NoProxy;break; - case 2: result.proxyType = ClientOptions::SystemConfiguredProxy;break; - case 3: result.proxyType = ClientOptions::SOCKS5Proxy;break; - case 4: result.proxyType = ClientOptions::HTTPConnectProxy;break; - } - PARSE_STRING(manualProxyHostname); - PARSE_INT(manualProxyPort, -1); - PARSE_URL(boshURL); - PARSE_URL(boshHTTPConnectProxyURL); - PARSE_SAFE_STRING(boshHTTPConnectProxyAuthID); - PARSE_SAFE_STRING(boshHTTPConnectProxyAuthPassword); - - return result; -} - -} diff --git a/Swift/Controllers/MainController.h b/Swift/Controllers/MainController.h deleted file mode 100644 index 6fbde6d..0000000 --- a/Swift/Controllers/MainController.h +++ /dev/null @@ -1,200 +0,0 @@ -/* - * Copyright (c) 2010-2013 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. - */ - -#pragma once - -#include <vector> -#include <map> -#include <string> - -#include <boost/shared_ptr.hpp> - -#include <Swiften/Base/boost_bsignals.h> -#include <Swiften/Network/Timer.h> -#include <Swiften/Client/ClientError.h> -#include <Swiften/JID/JID.h> -#include <Swiften/Elements/DiscoInfo.h> -#include <Swiften/Elements/VCard.h> -#include <Swiften/Elements/ErrorPayload.h> -#include <Swiften/Elements/Presence.h> -#include <Swiften/Elements/CapsInfo.h> -#include <Swiften/Client/ClientXMLTracer.h> - -#include <Swift/Controllers/Settings/SettingsProvider.h> -#include <Swift/Controllers/ProfileSettingsProvider.h> -#include <Swift/Controllers/XMPPEvents/ErrorEvent.h> -#include <Swift/Controllers/UIEvents/UIEvent.h> - - -namespace Swift { - class IdleDetector; - class UIFactory; - class EventLoop; - class Client; - class ChatController; - class ChatsManager; - class CertificateStorageFactory; - class CertificateStorage; - class CertificateStorageTrustChecker; - class EventController; - class MainWindow; - class RosterController; - class LoginWindow; - class EventLoop; - class MUCController; - class Notifier; - class ProfileController; - class ShowProfileController; - class ContactEditController; - class TogglableNotifier; - class PresenceNotifier; - class EventNotifier; - class SystemTray; - class SystemTrayController; - class SoundEventController; - class SoundPlayer; - class XMLConsoleController; - class HistoryViewController; - class HistoryController; - class FileTransferListController; - class UIEventStream; - class EventWindowFactory; - class EventWindowController; - class MUCSearchController; - class UserSearchController; - class StatusTracker; - class Dock; - class Storages; - class StoragesFactory; - class NetworkFactories; - class URIHandler; - class XMPPURIController; - class AdHocManager; - class AdHocCommandWindowFactory; - class FileTransferOverview; - class WhiteboardManager; - class HighlightManager; - class HighlightEditorController; - class BlockListController; - class ContactSuggester; - class ContactsFromXMPPRoster; - - class MainController { - public: - MainController( - EventLoop* eventLoop, - NetworkFactories* networkFactories, - UIFactory* uiFactories, - SettingsProvider *settings, - SystemTray* systemTray, - SoundPlayer* soundPlayer, - StoragesFactory* storagesFactory, - CertificateStorageFactory* certificateStorageFactory, - Dock* dock, - Notifier* notifier, - URIHandler* uriHandler, - IdleDetector* idleDetector, - const std::map<std::string, std::string>& emoticons, - bool useDelayForLatency); - ~MainController(); - - - private: - void resetClient(); - void handleConnected(); - void handleLoginRequest(const std::string& username, const std::string& password, const std::string& certificatePath, CertificateWithKey::ref certificate, const ClientOptions& options, bool remember, bool loginAutomatically); - void handleCancelLoginRequest(); - void handleQuitRequest(); - void handleChangeStatusRequest(StatusShow::Type show, const std::string &statusText); - void handleDisconnected(const boost::optional<ClientError>& error); - void handleServerDiscoInfoResponse(boost::shared_ptr<DiscoInfo>, ErrorPayload::ref); - void handleEventQueueLengthChange(int count); - void handleVCardReceived(const JID& j, VCard::ref vCard); - void handleSettingChanged(const std::string& settingPath); - void handlePurgeSavedLoginRequest(const std::string& username); - void sendPresence(boost::shared_ptr<Presence> presence); - void handleInputIdleChanged(bool); - void handleShowCertificateRequest(); - void logout(); - void signOut(); - void setReconnectTimer(); - void resetPendingReconnects(); - void resetCurrentError(); - - void performLoginFromCachedCredentials(); - void reconnectAfterError(); - void setManagersOffline(); - void handleNotificationClicked(const JID& jid); - void handleForceQuit(); - void purgeCachedCredentials(); - std::string serializeClientOptions(const ClientOptions& options); - ClientOptions parseClientOptions(const std::string& optionString); - - private: - EventLoop* eventLoop_; - NetworkFactories* networkFactories_; - UIFactory* uiFactory_; - StoragesFactory* storagesFactory_; - Storages* storages_; - CertificateStorageFactory* certificateStorageFactory_; - CertificateStorage* certificateStorage_; - CertificateStorageTrustChecker* certificateTrustChecker_; - bool clientInitialized_; - boost::shared_ptr<Client> client_; - SettingsProvider *settings_; - ProfileSettingsProvider* profileSettings_; - Dock* dock_; - URIHandler* uriHandler_; - IdleDetector* idleDetector_; - TogglableNotifier* notifier_; - PresenceNotifier* presenceNotifier_; - EventNotifier* eventNotifier_; - RosterController* rosterController_; - EventController* eventController_; - EventWindowController* eventWindowController_; - AdHocManager* adHocManager_; - LoginWindow* loginWindow_; - UIEventStream* uiEventStream_; - XMLConsoleController* xmlConsoleController_; - HistoryViewController* historyViewController_; - HistoryController* historyController_; - FileTransferListController* fileTransferListController_; - BlockListController* blockListController_; - ChatsManager* chatsManager_; - ProfileController* profileController_; - ShowProfileController* showProfileController_; - ContactEditController* contactEditController_; - ContactsFromXMPPRoster* contactsFromRosterProvider_; - ContactSuggester* contactSuggesterWithoutRoster_; - ContactSuggester* contactSuggesterWithRoster_; - JID jid_; - JID boundJID_; - SystemTrayController* systemTrayController_; - SoundEventController* soundEventController_; - XMPPURIController* xmppURIController_; - std::string vCardPhotoHash_; - std::string password_; - CertificateWithKey::ref certificate_; - ClientOptions clientOptions_; - boost::shared_ptr<ErrorEvent> lastDisconnectError_; - bool useDelayForLatency_; - UserSearchController* userSearchControllerChat_; - UserSearchController* userSearchControllerAdd_; - UserSearchController* userSearchControllerInvite_; - int timeBeforeNextReconnect_; - Timer::ref reconnectTimer_; - StatusTracker* statusTracker_; - bool myStatusLooksOnline_; - bool quitRequested_; - bool offlineRequested_; - static const int SecondsToWaitBeforeForceQuitting; - FileTransferOverview* ftOverview_; - WhiteboardManager* whiteboardManager_; - HighlightManager* highlightManager_; - HighlightEditorController* highlightEditorController_; - std::map<std::string, std::string> emoticons_; - }; -} diff --git a/Swift/Controllers/PresenceNotifier.cpp b/Swift/Controllers/PresenceNotifier.cpp index e7b25bf..91deae6 100644 --- a/Swift/Controllers/PresenceNotifier.cpp +++ b/Swift/Controllers/PresenceNotifier.cpp @@ -1,136 +1,137 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ -#include "Swift/Controllers/PresenceNotifier.h" +#include <Swift/Controllers/PresenceNotifier.h> #include <boost/bind.hpp> -#include "Swiften/Client/StanzaChannel.h" -#include "Swiften/Base/ByteArray.h" -#include "Swiften/MUC/MUCRegistry.h" -#include "Swiften/Roster/XMPPRoster.h" -#include "Swiften/Presence/PresenceOracle.h" -#include "Swiften/Network/TimerFactory.h" -#include "Swiften/Client/NickResolver.h" +#include <Swiften/Base/ByteArray.h> +#include <Swiften/Client/NickResolver.h> +#include <Swiften/Client/StanzaChannel.h> +#include <Swiften/MUC/MUCRegistry.h> +#include <Swiften/Network/TimerFactory.h> +#include <Swiften/Presence/PresenceOracle.h> +#include <Swiften/Roster/XMPPRoster.h> + #include <Swift/Controllers/StatusUtil.h> namespace Swift { PresenceNotifier::PresenceNotifier(StanzaChannel* stanzaChannel, Notifier* notifier, const MUCRegistry* mucRegistry, AvatarManager* avatarManager, NickResolver* nickResolver, const PresenceOracle* presenceOracle, TimerFactory* timerFactory) : stanzaChannel(stanzaChannel), notifier(notifier), mucRegistry(mucRegistry), avatarManager(avatarManager), nickResolver(nickResolver), presenceOracle(presenceOracle), timerFactory(timerFactory) { - justInitialized = true; - inQuietPeriod = false; - stanzaChannel->onPresenceReceived.connect(boost::bind(&PresenceNotifier::handlePresenceReceived, this, _1)); - stanzaChannel->onAvailableChanged.connect(boost::bind(&PresenceNotifier::handleStanzaChannelAvailableChanged, this, _1)); - setInitialQuietPeriodMS(3000); + justInitialized = true; + inQuietPeriod = false; + stanzaChannel->onPresenceReceived.connect(boost::bind(&PresenceNotifier::handlePresenceReceived, this, _1)); + stanzaChannel->onAvailableChanged.connect(boost::bind(&PresenceNotifier::handleStanzaChannelAvailableChanged, this, _1)); + setInitialQuietPeriodMS(3000); } PresenceNotifier::~PresenceNotifier() { - if (timer) { - timer->stop(); - timer->onTick.disconnect(boost::bind(&PresenceNotifier::handleTimerTick, this)); - timer.reset(); - } - stanzaChannel->onAvailableChanged.disconnect(boost::bind(&PresenceNotifier::handleStanzaChannelAvailableChanged, this, _1)); - stanzaChannel->onPresenceReceived.disconnect(boost::bind(&PresenceNotifier::handlePresenceReceived, this, _1)); + if (timer) { + timer->stop(); + timer->onTick.disconnect(boost::bind(&PresenceNotifier::handleTimerTick, this)); + timer.reset(); + } + stanzaChannel->onAvailableChanged.disconnect(boost::bind(&PresenceNotifier::handleStanzaChannelAvailableChanged, this, _1)); + stanzaChannel->onPresenceReceived.disconnect(boost::bind(&PresenceNotifier::handlePresenceReceived, this, _1)); } -void PresenceNotifier::handlePresenceReceived(boost::shared_ptr<Presence> presence) { - JID from = presence->getFrom(); - - if (mucRegistry->isMUC(from.toBare())) { - return; - } - - if (justInitialized) { - justInitialized = false; - if (timer) { - inQuietPeriod = true; - } - } - - if (inQuietPeriod) { - timer->stop(); - timer->start(); - return; - } - - std::set<JID>::iterator i = availableUsers.find(from); - if (presence->isAvailable()) { - if (i != availableUsers.end()) { - showNotification(from, Notifier::ContactStatusChange); - } - else { - showNotification(from, Notifier::ContactAvailable); - availableUsers.insert(from); - } - } - else { - if (i != availableUsers.end()) { - showNotification(from, Notifier::ContactUnavailable); - availableUsers.erase(i); - } - } +void PresenceNotifier::handlePresenceReceived(std::shared_ptr<Presence> presence) { + JID from = presence->getFrom(); + + if (mucRegistry->isMUC(from.toBare())) { + return; + } + + if (justInitialized) { + justInitialized = false; + if (timer) { + inQuietPeriod = true; + } + } + + if (inQuietPeriod) { + timer->stop(); + timer->start(); + return; + } + + std::set<JID>::iterator i = availableUsers.find(from); + if (presence->isAvailable()) { + if (i != availableUsers.end()) { + showNotification(from, Notifier::ContactStatusChange); + } + else { + showNotification(from, Notifier::ContactAvailable); + availableUsers.insert(from); + } + } + else { + if (i != availableUsers.end()) { + showNotification(from, Notifier::ContactUnavailable); + availableUsers.erase(i); + } + } } void PresenceNotifier::handleStanzaChannelAvailableChanged(bool available) { - if (available) { - availableUsers.clear(); - justInitialized = true; - if (timer) { - timer->stop(); - } - } + if (available) { + availableUsers.clear(); + justInitialized = true; + if (timer) { + timer->stop(); + } + } } void PresenceNotifier::showNotification(const JID& jid, Notifier::Type type) { - std::string name = nickResolver->jidToNick(jid); - std::string title = name + " (" + getStatusType(jid) + ")"; - std::string message = getStatusMessage(jid); - notifier->showMessage(type, title, message, avatarManager->getAvatarPath(jid), boost::bind(&PresenceNotifier::handleNotificationActivated, this, jid)); + std::string name = nickResolver->jidToNick(jid); + std::string title = name + " (" + getStatusType(jid) + ")"; + std::string message = getStatusMessage(jid); + notifier->showMessage(type, title, message, avatarManager->getAvatarPath(jid), boost::bind(&PresenceNotifier::handleNotificationActivated, this, jid)); } void PresenceNotifier::handleNotificationActivated(JID jid) { - onNotificationActivated(jid); + onNotificationActivated(jid); } std::string PresenceNotifier::getStatusType(const JID& jid) const { - Presence::ref presence = presenceOracle->getLastPresence(jid); - if (presence) { - return statusShowTypeToFriendlyName(presence->getShow()); - } - else { - return "Unavailable"; - } + Presence::ref presence = presenceOracle->getLastPresence(jid); + if (presence) { + return statusShowTypeToFriendlyName(presence->getShow()); + } + else { + return "Unavailable"; + } } std::string PresenceNotifier::getStatusMessage(const JID& jid) const { - Presence::ref presence = presenceOracle->getLastPresence(jid); - if (presence) { - return presence->getStatus(); - } - else { - return std::string(); - } + Presence::ref presence = presenceOracle->getLastPresence(jid); + if (presence) { + return presence->getStatus(); + } + else { + return std::string(); + } } void PresenceNotifier::setInitialQuietPeriodMS(int ms) { - if (timer) { - timer->stop(); - timer->onTick.disconnect(boost::bind(&PresenceNotifier::handleTimerTick, this)); - timer.reset(); - } - if (ms > 0) { - timer = timerFactory->createTimer(ms); - timer->onTick.connect(boost::bind(&PresenceNotifier::handleTimerTick, this)); - } + if (timer) { + timer->stop(); + timer->onTick.disconnect(boost::bind(&PresenceNotifier::handleTimerTick, this)); + timer.reset(); + } + if (ms > 0) { + timer = timerFactory->createTimer(ms); + timer->onTick.connect(boost::bind(&PresenceNotifier::handleTimerTick, this)); + } } void PresenceNotifier::handleTimerTick() { - inQuietPeriod = false; - timer->stop(); + inQuietPeriod = false; + timer->stop(); } diff --git a/Swift/Controllers/PresenceNotifier.h b/Swift/Controllers/PresenceNotifier.h index 0d187bd..82678be 100644 --- a/Swift/Controllers/PresenceNotifier.h +++ b/Swift/Controllers/PresenceNotifier.h @@ -1,60 +1,62 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <boost/shared_ptr.hpp> +#include <memory> #include <set> -#include "Swiften/Base/boost_bsignals.h" -#include "Swiften/Elements/Presence.h" -#include "Swiften/JID/JID.h" -#include "SwifTools/Notifier/Notifier.h" -#include "Swiften/Avatars/AvatarManager.h" -#include "Swiften/Network/Timer.h" +#include <boost/signals2.hpp> + +#include <Swiften/Avatars/AvatarManager.h> +#include <Swiften/Elements/Presence.h> +#include <Swiften/JID/JID.h> +#include <Swiften/Network/Timer.h> + +#include <SwifTools/Notifier/Notifier.h> namespace Swift { - class TimerFactory; - class StanzaChannel; - class MUCRegistry; - class NickResolver; - class PresenceOracle; - - class PresenceNotifier { - public: - PresenceNotifier(StanzaChannel* stanzaChannel, Notifier* notifier, const MUCRegistry* mucRegistry, AvatarManager* avatarManager, NickResolver* nickResolver, const PresenceOracle* presenceOracle, TimerFactory* timerFactory); - ~PresenceNotifier(); - - void setInitialQuietPeriodMS(int ms); - - boost::signal<void (const JID&)> onNotificationActivated; - - private: - void handlePresenceReceived(boost::shared_ptr<Presence>); - void handleStanzaChannelAvailableChanged(bool); - void handleNotificationActivated(JID jid); - void handleTimerTick(); - std::string getStatusType(const JID&) const; - std::string getStatusMessage(const JID&) const; - - private: - void showNotification(const JID& jid, Notifier::Type type); - - private: - StanzaChannel* stanzaChannel; - Notifier* notifier; - const MUCRegistry* mucRegistry; - AvatarManager* avatarManager; - NickResolver* nickResolver; - const PresenceOracle* presenceOracle; - TimerFactory* timerFactory; - boost::shared_ptr<Timer> timer; - bool justInitialized; - bool inQuietPeriod; - std::set<JID> availableUsers; - }; + class TimerFactory; + class StanzaChannel; + class MUCRegistry; + class NickResolver; + class PresenceOracle; + + class PresenceNotifier { + public: + PresenceNotifier(StanzaChannel* stanzaChannel, Notifier* notifier, const MUCRegistry* mucRegistry, AvatarManager* avatarManager, NickResolver* nickResolver, const PresenceOracle* presenceOracle, TimerFactory* timerFactory); + ~PresenceNotifier(); + + void setInitialQuietPeriodMS(int ms); + + boost::signals2::signal<void (const JID&)> onNotificationActivated; + + private: + void handlePresenceReceived(std::shared_ptr<Presence>); + void handleStanzaChannelAvailableChanged(bool); + void handleNotificationActivated(JID jid); + void handleTimerTick(); + std::string getStatusType(const JID&) const; + std::string getStatusMessage(const JID&) const; + + private: + void showNotification(const JID& jid, Notifier::Type type); + + private: + StanzaChannel* stanzaChannel; + Notifier* notifier; + const MUCRegistry* mucRegistry; + AvatarManager* avatarManager; + NickResolver* nickResolver; + const PresenceOracle* presenceOracle; + TimerFactory* timerFactory; + std::shared_ptr<Timer> timer; + bool justInitialized; + bool inQuietPeriod; + std::set<JID> availableUsers; + }; } diff --git a/Swift/Controllers/PreviousStatusStore.cpp b/Swift/Controllers/PreviousStatusStore.cpp index ca0a12e..0b2d437 100644 --- a/Swift/Controllers/PreviousStatusStore.cpp +++ b/Swift/Controllers/PreviousStatusStore.cpp @@ -1,12 +1,10 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ -#include "PreviousStatusStore.h" - -#include "Swiften/Base/foreach.h" +#include <Swift/Controllers/PreviousStatusStore.h> namespace Swift { @@ -19,35 +17,35 @@ PreviousStatusStore::~PreviousStatusStore() { } void PreviousStatusStore::addStatus(StatusShow::Type status, const std::string& message) { - //FIXME: remove old entries - store_.push_back(TypeStringPair(status, message)); + //FIXME: remove old entries + store_.push_back(TypeStringPair(status, message)); } std::vector<TypeStringPair> PreviousStatusStore::exactMatchSuggestions(StatusShow::Type status, const std::string& message) { - std::vector<TypeStringPair> suggestions; - suggestions.push_back(TypeStringPair(status, message)); - return suggestions; + std::vector<TypeStringPair> suggestions; + suggestions.push_back(TypeStringPair(status, message)); + return suggestions; } std::vector<TypeStringPair> PreviousStatusStore::getSuggestions(const std::string& message) { - std::vector<TypeStringPair> suggestions; - foreach (TypeStringPair status, store_) { - if (status.second == message) { - suggestions.clear(); - suggestions.push_back(status); - break; - } else if (status.second.find(message) != std::string::npos) { - suggestions.push_back(status); - } - } - if (suggestions.empty()) { - TypeStringPair suggestion(StatusShow::Online, message); - suggestions.push_back(suggestion); - } - if (suggestions.size() == 1) { - suggestions = exactMatchSuggestions(suggestions[0].first, suggestions[0].second); - } - return suggestions; + std::vector<TypeStringPair> suggestions; + for (auto&& status : store_) { + if (status.second == message) { + suggestions.clear(); + suggestions.push_back(status); + break; + } else if (status.second.find(message) != std::string::npos) { + suggestions.push_back(status); + } + } + if (suggestions.empty()) { + TypeStringPair suggestion(StatusShow::Online, message); + suggestions.push_back(suggestion); + } + if (suggestions.size() == 1) { + suggestions = exactMatchSuggestions(suggestions[0].first, suggestions[0].second); + } + return suggestions; } } diff --git a/Swift/Controllers/PreviousStatusStore.h b/Swift/Controllers/PreviousStatusStore.h index 6403123..b106445 100644 --- a/Swift/Controllers/PreviousStatusStore.h +++ b/Swift/Controllers/PreviousStatusStore.h @@ -1,28 +1,29 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once +#include <string> #include <utility> /* std::pair */ #include <vector> -#include <string> -#include "Swiften/Elements/StatusShow.h" +#include <Swiften/Elements/StatusShow.h> namespace Swift { - typedef std::pair<StatusShow::Type, std::string> TypeStringPair; - class PreviousStatusStore { - public: - PreviousStatusStore(); - ~PreviousStatusStore(); - void addStatus(StatusShow::Type status, const std::string& message); - std::vector<TypeStringPair> getSuggestions(const std::string& message); + typedef std::pair<StatusShow::Type, std::string> TypeStringPair; + + class PreviousStatusStore { + public: + PreviousStatusStore(); + ~PreviousStatusStore(); + void addStatus(StatusShow::Type status, const std::string& message); + std::vector<TypeStringPair> getSuggestions(const std::string& message); - private: - std::vector<TypeStringPair> exactMatchSuggestions(StatusShow::Type status, const std::string& message); - std::vector<TypeStringPair> store_; - }; + private: + std::vector<TypeStringPair> exactMatchSuggestions(StatusShow::Type status, const std::string& message); + std::vector<TypeStringPair> store_; + }; } diff --git a/Swift/Controllers/ProfileController.cpp b/Swift/Controllers/ProfileController.cpp index 241cc2e..1a92f29 100644 --- a/Swift/Controllers/ProfileController.cpp +++ b/Swift/Controllers/ProfileController.cpp @@ -1,100 +1,115 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #include <Swift/Controllers/ProfileController.h> #include <boost/bind.hpp> +#include <Swiften/VCards/VCardManager.h> + +#include <Swift/Controllers/Intl.h> #include <Swift/Controllers/UIEvents/RequestProfileEditorUIEvent.h> #include <Swift/Controllers/UIEvents/UIEventStream.h> #include <Swift/Controllers/UIInterfaces/ProfileWindowFactory.h> -#include <Swiften/VCards/VCardManager.h> -#include <Swift/Controllers/Intl.h> - namespace Swift { -ProfileController::ProfileController(VCardManager* vcardManager, ProfileWindowFactory* profileWindowFactory, UIEventStream* uiEventStream) : vcardManager(vcardManager), profileWindowFactory(profileWindowFactory), uiEventStream(uiEventStream), available(true), profileWindow(NULL), gettingVCard(false) { - uiEventStream->onUIEvent.connect(boost::bind(&ProfileController::handleUIEvent, this, _1)); +ProfileController::ProfileController(VCardManager* vcardManager, ProfileWindowFactory* profileWindowFactory, UIEventStream* uiEventStream) : vcardManager(vcardManager), profileWindowFactory(profileWindowFactory), uiEventStream(uiEventStream), available(true), profileWindow(nullptr), gettingVCard(false) { + uiEventStream->onUIEvent.connect(boost::bind(&ProfileController::handleUIEvent, this, _1)); } ProfileController::~ProfileController() { - if (profileWindow) { - vcardManager->onOwnVCardChanged.disconnect(boost::bind(&ProfileController::handleOwnVCardChanged, this, _1)); - profileWindow->onVCardChangeRequest.disconnect(boost::bind(&ProfileController::handleVCardChangeRequest, this, _1)); - profileWindow->onWindowAboutToBeClosed.disconnect(boost::bind(&ProfileController::handleProfileWindowAboutToBeClosed, this, _1)); - } - uiEventStream->onUIEvent.disconnect(boost::bind(&ProfileController::handleUIEvent, this, _1)); + if (profileWindow) { + vcardManager->onOwnVCardChanged.disconnect(boost::bind(&ProfileController::handleOwnVCardChanged, this, _1)); + vcardManager->onVCardRetrievalError.disconnect(boost::bind(&ProfileController::handleVCardRetrievalError, this, _1, _2)); + profileWindow->onVCardChangeRequest.disconnect(boost::bind(&ProfileController::handleVCardChangeRequest, this, _1)); + profileWindow->onWindowAboutToBeClosed.disconnect(boost::bind(&ProfileController::handleProfileWindowAboutToBeClosed, this, _1)); + } + uiEventStream->onUIEvent.disconnect(boost::bind(&ProfileController::handleUIEvent, this, _1)); + delete profileWindow; } void ProfileController::handleUIEvent(UIEvent::ref event) { - if (!boost::dynamic_pointer_cast<RequestProfileEditorUIEvent>(event)) { - return; - } - - if (!profileWindow) { - profileWindow = profileWindowFactory->createProfileWindow(); - profileWindow->setEditable(true); - profileWindow->onVCardChangeRequest.connect(boost::bind(&ProfileController::handleVCardChangeRequest, this, _1)); - profileWindow->onWindowAboutToBeClosed.connect(boost::bind(&ProfileController::handleProfileWindowAboutToBeClosed, this, _1)); - vcardManager->onOwnVCardChanged.connect(boost::bind(&ProfileController::handleOwnVCardChanged, this, _1)); - } - gettingVCard = true; - updateDialogStatus(); - vcardManager->requestOwnVCard(); - profileWindow->show(); + if (!std::dynamic_pointer_cast<RequestProfileEditorUIEvent>(event)) { + return; + } + + if (!profileWindow) { + profileWindow = profileWindowFactory->createProfileWindow(); + profileWindow->setEditable(true); + profileWindow->onVCardChangeRequest.connect(boost::bind(&ProfileController::handleVCardChangeRequest, this, _1)); + profileWindow->onWindowAboutToBeClosed.connect(boost::bind(&ProfileController::handleProfileWindowAboutToBeClosed, this, _1)); + vcardManager->onOwnVCardChanged.connect(boost::bind(&ProfileController::handleOwnVCardChanged, this, _1)); + vcardManager->onVCardRetrievalError.connect(boost::bind(&ProfileController::handleVCardRetrievalError, this, _1, _2)); + } + gettingVCard = true; + updateDialogStatus(); + vcardManager->requestOwnVCard(); + profileWindow->setError(""); + profileWindow->show(); } void ProfileController::handleVCardChangeRequest(VCard::ref vcard) { - assert(!pendingSetVCardRequest); - profileWindow->setError(""); - pendingSetVCardRequest = vcardManager->createSetVCardRequest(vcard); - pendingSetVCardRequest->onResponse.connect(boost::bind(&ProfileController::handleSetVCardResponse, this, _2)); - pendingSetVCardRequest->send(); - updateDialogStatus(); + assert(!pendingSetVCardRequest); + profileWindow->setError(""); + pendingSetVCardRequest = vcardManager->createSetVCardRequest(vcard); + pendingSetVCardRequest->onResponse.connect(boost::bind(&ProfileController::handleSetVCardResponse, this, _2)); + pendingSetVCardRequest->send(); + updateDialogStatus(); } void ProfileController::handleSetVCardResponse(ErrorPayload::ref error) { - pendingSetVCardRequest.reset(); - updateDialogStatus(); - if (error) { - profileWindow->setError(QT_TRANSLATE_NOOP("", "There was an error publishing your profile data")); - } - else { - profileWindow->setError(""); - profileWindow->hide(); - } + pendingSetVCardRequest.reset(); + updateDialogStatus(); + if (error) { + profileWindow->setVCard(vcardBeforeEdit); + profileWindow->setError(QT_TRANSLATE_NOOP("", "There was an error publishing your profile data")); + } + else { + profileWindow->setError(""); + profileWindow->hide(); + } +} + +void ProfileController::handleVCardRetrievalError(const JID& jid, ErrorPayload::ref /* error */) { + if ((jid == JID()) && profileWindow) { + profileWindow->setProcessing(false); + profileWindow->setEnabled(false); + profileWindow->setVCard(std::make_shared<VCard>()); + profileWindow->setError(QT_TRANSLATE_NOOP("", "There was an error fetching your current profile data")); + } } void ProfileController::handleOwnVCardChanged(VCard::ref vcard) { - if (profileWindow) { - profileWindow->setVCard(vcard); - gettingVCard = false; - updateDialogStatus(); - } + if (profileWindow) { + profileWindow->setVCard(vcard); + vcardBeforeEdit = vcard; + gettingVCard = false; + updateDialogStatus(); + } } void ProfileController::handleProfileWindowAboutToBeClosed(const JID&) { - profileWindow = NULL; + profileWindow = nullptr; } void ProfileController::setAvailable(bool b) { - available = b; - if (!available) { - pendingSetVCardRequest.reset(); - } - updateDialogStatus(); + available = b; + if (!available) { + pendingSetVCardRequest.reset(); + } + updateDialogStatus(); } void ProfileController::updateDialogStatus() { - if (profileWindow) { - profileWindow->setEnabled(available && !gettingVCard && !pendingSetVCardRequest); - profileWindow->setProcessing(gettingVCard || pendingSetVCardRequest); - } + if (profileWindow) { + profileWindow->setEnabled(available && !gettingVCard && !pendingSetVCardRequest); + profileWindow->setProcessing(gettingVCard || pendingSetVCardRequest); + } } } diff --git a/Swift/Controllers/ProfileController.h b/Swift/Controllers/ProfileController.h index 538df36..3bb1fce 100644 --- a/Swift/Controllers/ProfileController.h +++ b/Swift/Controllers/ProfileController.h @@ -1,45 +1,48 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <Swift/Controllers/UIEvents/UIEvent.h> -#include <Swiften/Elements/VCard.h> #include <Swiften/Elements/ErrorPayload.h> +#include <Swiften/Elements/VCard.h> #include <Swiften/VCards/SetVCardRequest.h> +#include <Swift/Controllers/UIEvents/UIEvent.h> + namespace Swift { - class UIEventStream; - class ProfileWindowFactory; - class ProfileWindow; - class VCardManager; - - class ProfileController { - public: - ProfileController(VCardManager* vcardManager, ProfileWindowFactory* profileWindowFactory, UIEventStream* uiEventStream); - ~ProfileController(); - - void setAvailable(bool b); - - private: - void handleUIEvent(UIEvent::ref event); - void handleVCardChangeRequest(VCard::ref vcard); - void handleSetVCardResponse(ErrorPayload::ref); - void handleOwnVCardChanged(VCard::ref vcard); - void handleProfileWindowAboutToBeClosed(const JID&); - void updateDialogStatus(); - - private: - VCardManager* vcardManager; - ProfileWindowFactory* profileWindowFactory; - UIEventStream* uiEventStream; - bool available; - SetVCardRequest::ref pendingSetVCardRequest; - ProfileWindow* profileWindow; - bool gettingVCard; - }; + class ProfileWindow; + class ProfileWindowFactory; + class UIEventStream; + class VCardManager; + + class ProfileController { + public: + ProfileController(VCardManager* vcardManager, ProfileWindowFactory* profileWindowFactory, UIEventStream* uiEventStream); + ~ProfileController(); + + void setAvailable(bool b); + + private: + void handleUIEvent(UIEvent::ref event); + void handleVCardChangeRequest(VCard::ref vcard); + void handleSetVCardResponse(ErrorPayload::ref); + void handleOwnVCardChanged(VCard::ref vcard); + void handleVCardRetrievalError(const JID& jid, ErrorPayload::ref error); + void handleProfileWindowAboutToBeClosed(const JID&); + void updateDialogStatus(); + + private: + VCardManager* vcardManager; + ProfileWindowFactory* profileWindowFactory; + UIEventStream* uiEventStream; + bool available; + SetVCardRequest::ref pendingSetVCardRequest; + ProfileWindow* profileWindow; + bool gettingVCard; + VCard::ref vcardBeforeEdit; + }; } diff --git a/Swift/Controllers/ProfileSettingsProvider.cpp b/Swift/Controllers/ProfileSettingsProvider.cpp index c1b4b13..b979555 100644 --- a/Swift/Controllers/ProfileSettingsProvider.cpp +++ b/Swift/Controllers/ProfileSettingsProvider.cpp @@ -1,66 +1,68 @@ /* - * Copyright (c) 2010-2012 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ -#include "Swift/Controllers/ProfileSettingsProvider.h" +#include <Swift/Controllers/ProfileSettingsProvider.h> + +#include <Swift/Controllers/Settings/SettingsProvider.h> namespace Swift { ProfileSettingsProvider::ProfileSettingsProvider(const std::string& profile, SettingsProvider* provider) : - profile_(profile) { - provider_ = provider; - bool found = false; - foreach (std::string existingProfile, provider->getAvailableProfiles()) { - if (existingProfile == profile) { - found = true; - } - } - if (!found) { - provider_->createProfile(profile); - } + profile_(profile) { + provider_ = provider; + bool found = false; + for (const auto& existingProfile : provider->getAvailableProfiles()) { + if (existingProfile == profile) { + found = true; + } + } + if (!found) { + provider_->createProfile(profile); + } } ProfileSettingsProvider::~ProfileSettingsProvider() { } std::string ProfileSettingsProvider::getStringSetting(const std::string &settingPath) { - //FIXME: Remove shim - SettingsProvider::Setting<std::string> setting(profileSettingPath(settingPath), ""); - return provider_->getSetting(setting); + //FIXME: Remove shim + SettingsProvider::Setting<std::string> setting(profileSettingPath(settingPath), ""); + return provider_->getSetting(setting); } void ProfileSettingsProvider::storeString(const std::string &settingPath, const std::string &settingValue) { - //FIXME: Remove shim - if (!getIsSettingFinal(settingPath)) { - SettingsProvider::Setting<std::string> setting(profileSettingPath(settingPath), ""); - provider_->storeSetting(setting, settingValue); - } + //FIXME: Remove shim + if (!getIsSettingFinal(settingPath)) { + SettingsProvider::Setting<std::string> setting(profileSettingPath(settingPath), ""); + provider_->storeSetting(setting, settingValue); + } } int ProfileSettingsProvider::getIntSetting(const std::string& settingPath, int defaultValue) { - //FIXME: Remove shim - SettingsProvider::Setting<int> setting(profileSettingPath(settingPath), defaultValue); - return provider_->getSetting(setting); + //FIXME: Remove shim + SettingsProvider::Setting<int> setting(profileSettingPath(settingPath), defaultValue); + return provider_->getSetting(setting); } void ProfileSettingsProvider::storeInt(const std::string& settingPath, int settingValue) { - //FIXME: Remove shim - if (!getIsSettingFinal(settingPath)) { - SettingsProvider::Setting<int> setting(profileSettingPath(settingPath), 0); - provider_->storeSetting(setting, settingValue); - } + //FIXME: Remove shim + if (!getIsSettingFinal(settingPath)) { + SettingsProvider::Setting<int> setting(profileSettingPath(settingPath), 0); + provider_->storeSetting(setting, settingValue); + } } bool ProfileSettingsProvider::getIsSettingFinal(const std::string& settingPath) { - //FIXME: Remove shim - SettingsProvider::Setting<int> setting(settingPath, 0); - return provider_->getIsSettingFinal(setting); + //FIXME: Remove shim + SettingsProvider::Setting<int> setting(settingPath, 0); + return provider_->getIsSettingFinal(setting); } std::string ProfileSettingsProvider::profileSettingPath(const std::string &settingPath) { - return profile_ + ":" + settingPath; + return profile_ + ":" + settingPath; } diff --git a/Swift/Controllers/ProfileSettingsProvider.h b/Swift/Controllers/ProfileSettingsProvider.h index ebc515a..e309c11 100644 --- a/Swift/Controllers/ProfileSettingsProvider.h +++ b/Swift/Controllers/ProfileSettingsProvider.h @@ -1,31 +1,34 @@ /* - * Copyright (c) 2010-2012 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <Swift/Controllers/Settings/SettingsProvider.h> -#include <Swiften/Base/foreach.h> +#include <string> namespace Swift { +class SettingsProvider; + class ProfileSettingsProvider { - public: - ProfileSettingsProvider(const std::string& profile, SettingsProvider* provider); - virtual ~ProfileSettingsProvider(); - virtual std::string getStringSetting(const std::string &settingPath); - virtual void storeString(const std::string &settingPath, const std::string &settingValue); - virtual int getIntSetting(const std::string& settingPath, int defaultValue); - virtual void storeInt(const std::string& settingPath, int settingValue); - /** See @SettingsProvider::getIsSettingFinal for discussion of what this means.*/ - virtual bool getIsSettingFinal(const std::string& settingPath); - - private: - std::string profileSettingPath(const std::string &settingPath); - SettingsProvider* provider_; - std::string profile_; + public: + ProfileSettingsProvider(const std::string& profile, SettingsProvider* provider); + virtual ~ProfileSettingsProvider(); + virtual std::string getStringSetting(const std::string &settingPath); + virtual void storeString(const std::string &settingPath, const std::string &settingValue); + virtual int getIntSetting(const std::string& settingPath, int defaultValue); + virtual void storeInt(const std::string& settingPath, int settingValue); + /** See \ref SettingsProvider::getIsSettingFinal for discussion of what this means.*/ + virtual bool getIsSettingFinal(const std::string& settingPath); + + private: + std::string profileSettingPath(const std::string &settingPath); + + private: + SettingsProvider* provider_; + std::string profile_; }; } diff --git a/Swift/Controllers/Roster/AppearOffline.h b/Swift/Controllers/Roster/AppearOffline.h deleted file mode 100644 index 8bd53d7..0000000 --- a/Swift/Controllers/Roster/AppearOffline.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. - */ - -#pragma once - -#include <Swift/Controllers/Roster/RosterItemOperation.h> -#include <Swift/Controllers/Roster/ContactRosterItem.h> - -namespace Swift { - -class RosterItem; - -class AppearOffline : public RosterItemOperation { - public: - AppearOffline() { - } - - virtual void operator() (RosterItem* item) const { - ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(item); - if (contact) { - contact->clearPresence(); - } - } - -}; - -} - - diff --git a/Swift/Controllers/Roster/ContactRosterItem.cpp b/Swift/Controllers/Roster/ContactRosterItem.cpp index 70b4a1b..8fdf183 100644 --- a/Swift/Controllers/Roster/ContactRosterItem.cpp +++ b/Swift/Controllers/Roster/ContactRosterItem.cpp @@ -1,147 +1,194 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ -#include "Swift/Controllers/Roster/ContactRosterItem.h" -#include "Swift/Controllers/Roster/GroupRosterItem.h" +#include <Swift/Controllers/Roster/ContactRosterItem.h> + +#include <boost/date_time/posix_time/posix_time.hpp> -#include <Swiften/Base/foreach.h> #include <Swiften/Base/DateTime.h> #include <Swiften/Elements/Idle.h> +#include <Swiften/Elements/Presence.h> -#include <boost/date_time/posix_time/posix_time.hpp> +#include <Swift/Controllers/Intl.h> +#include <Swift/Controllers/Roster/GroupRosterItem.h> namespace Swift { -ContactRosterItem::ContactRosterItem(const JID& jid, const JID& displayJID, const std::string& name, GroupRosterItem* parent) : RosterItem(name, parent), jid_(jid), displayJID_(displayJID), blockState_(BlockingNotSupported) { +ContactRosterItem::ContactRosterItem(const JID& jid, const JID& displayJID, const std::string& name, GroupRosterItem* parent) +: RosterItem(name, parent), jid_(jid), displayJID_(displayJID.toBare()), mucRole_(MUCOccupant::NoRole), mucAffiliation_(MUCOccupant::NoAffiliation), blockState_(BlockingNotSupported) +{ } ContactRosterItem::~ContactRosterItem() { } StatusShow::Type ContactRosterItem::getStatusShow() const { - return shownPresence_ ? shownPresence_->getShow() : StatusShow::None; + return presence_ ? presence_->getShow() : StatusShow::None; } StatusShow::Type ContactRosterItem::getSimplifiedStatusShow() const { - switch (shownPresence_ ? shownPresence_->getShow() : StatusShow::None) { - case StatusShow::Online: return StatusShow::Online; - case StatusShow::Away: return StatusShow::Away; - case StatusShow::XA: return StatusShow::Away; - case StatusShow::FFC: return StatusShow::Online; - case StatusShow::DND: return StatusShow::DND; - case StatusShow::None: return StatusShow::None; - } - assert(false); - return StatusShow::None; + switch (presence_ ? presence_->getShow() : StatusShow::None) { + case StatusShow::Online: return StatusShow::Online; + case StatusShow::Away: return StatusShow::Away; + case StatusShow::XA: return StatusShow::Away; + case StatusShow::FFC: return StatusShow::Online; + case StatusShow::DND: return StatusShow::DND; + case StatusShow::None: return StatusShow::None; + } + assert(false); + return StatusShow::None; } std::string ContactRosterItem::getStatusText() const { - return shownPresence_ ? shownPresence_->getStatus() : ""; + return presence_ ? presence_->getStatus() : ""; } std::string ContactRosterItem::getIdleText() const { - Idle::ref idle = shownPresence_ ? shownPresence_->getPayload<Idle>() : Idle::ref(); - if (!idle || idle->getSince().is_not_a_date_time()) { - return ""; - } else { - return dateTimeToLocalString(idle->getSince()); - } + boost::posix_time::ptime idleTime = getIdle(); + if (idleTime.is_not_a_date_time()) { + return ""; + } else { + return dateTimeToLocalString(idleTime); + } +} + +boost::posix_time::ptime ContactRosterItem::getIdle() const { + Idle::ref idle = presence_ ? presence_->getPayload<Idle>() : Idle::ref(); + if (idle) { + return idle->getSince(); + } + else { + return boost::posix_time::not_a_date_time; + } +} + +std::string ContactRosterItem::getOfflineSinceText() const { + boost::posix_time::ptime offlineSince = getOfflineSince(); + if (!offlineSince.is_not_a_date_time()) { + return dateTimeToLocalString(offlineSince); + } + return ""; +} + +boost::posix_time::ptime ContactRosterItem::getOfflineSince() const { + boost::posix_time::ptime offlineSince = boost::posix_time::not_a_date_time; + if (presence_ && presence_->getType() == Presence::Unavailable) { + boost::optional<boost::posix_time::ptime> delay = presence_->getTimestamp(); + if (delay) { + offlineSince = delay.get(); + } + } + return offlineSince; } void ContactRosterItem::setAvatarPath(const boost::filesystem::path& path) { - avatarPath_ = path; - onDataChanged(); + avatarPath_ = path; + onDataChanged(); } const boost::filesystem::path& ContactRosterItem::getAvatarPath() const { - return avatarPath_; + return avatarPath_; } const JID& ContactRosterItem::getJID() const { - return jid_; + return jid_; } void ContactRosterItem::setDisplayJID(const JID& jid) { - displayJID_ = jid; + displayJID_ = jid; } const JID& ContactRosterItem::getDisplayJID() const { - return displayJID_; + return displayJID_; } -typedef std::pair<std::string, boost::shared_ptr<Presence> > StringPresencePair; +typedef std::pair<std::string, std::shared_ptr<Presence> > StringPresencePair; -void ContactRosterItem::calculateShownPresence() { - shownPresence_ = offlinePresence_; - foreach (StringPresencePair presencePair, presences_) { - boost::shared_ptr<Presence> presence = presencePair.second; - if (!shownPresence_ || presence->getPriority() > shownPresence_->getPriority() || presence->getShow() < shownPresence_->getShow()) { - shownPresence_ = presence; - } - } +void ContactRosterItem::clearPresence() { + presence_.reset(); + onDataChanged(); } -void ContactRosterItem::clearPresence() { - presences_.clear(); - calculateShownPresence(); - onDataChanged(); -} - -void ContactRosterItem::applyPresence(const std::string& resource, boost::shared_ptr<Presence> presence) { - if (offlinePresence_) { - offlinePresence_ = boost::shared_ptr<Presence>(); - } - if (presence->getType() == Presence::Unavailable) { - if (resource.empty()) { - /* Unavailable from the bare JID means all resources are offline.*/ - presences_.clear(); - } else { - if (presences_.find(resource) != presences_.end()) { - presences_.erase(resource); - } - } - if (presences_.empty()) { - offlinePresence_ = presence; - } - } else { - presences_[resource] = presence; - } - calculateShownPresence(); - onDataChanged(); +void ContactRosterItem::applyPresence(std::shared_ptr<Presence> presence) { + presence_ = presence; + onDataChanged(); } const std::vector<std::string>& ContactRosterItem::getGroups() const { - return groups_; + return groups_; } /** Only used so a contact can know about the groups it's in*/ void ContactRosterItem::addGroup(const std::string& group) { - groups_.push_back(group); + groups_.push_back(group); } void ContactRosterItem::removeGroup(const std::string& group) { - groups_.erase(std::remove(groups_.begin(), groups_.end(), group), groups_.end()); + groups_.erase(std::remove(groups_.begin(), groups_.end(), group), groups_.end()); +} + +MUCOccupant::Role ContactRosterItem::getMUCRole() const +{ + return mucRole_; +} + +void ContactRosterItem::setMUCRole(const MUCOccupant::Role& role) +{ + mucRole_ = role; +} + +MUCOccupant::Affiliation ContactRosterItem::getMUCAffiliation() const +{ + return mucAffiliation_; +} + +void ContactRosterItem::setMUCAffiliation(const MUCOccupant::Affiliation& affiliation) +{ + mucAffiliation_ = affiliation; +} + +std::string ContactRosterItem::getMUCAffiliationText() const +{ + std::string affiliationString; + switch (mucAffiliation_) { + case MUCOccupant::Owner: affiliationString = QT_TRANSLATE_NOOP("", "Owner"); break; + case MUCOccupant::Admin: affiliationString = QT_TRANSLATE_NOOP("", "Admin"); break; + case MUCOccupant::Member: affiliationString = QT_TRANSLATE_NOOP("", "Member"); break; + case MUCOccupant::Outcast: affiliationString = QT_TRANSLATE_NOOP("", "Outcast"); break; + case MUCOccupant::NoAffiliation: affiliationString = ""; break; + } + + return affiliationString; } void ContactRosterItem::setSupportedFeatures(const std::set<Feature>& features) { - features_ = features; + features_ = features; + onDataChanged(); } bool ContactRosterItem::supportsFeature(const Feature feature) const { - return features_.find(feature) != features_.end(); + return features_.find(feature) != features_.end(); } void ContactRosterItem::setBlockState(BlockState state) { - blockState_ = state; + blockState_ = state; + onDataChanged(); } ContactRosterItem::BlockState ContactRosterItem::blockState() const { - return blockState_; + return blockState_; } +VCard::ref ContactRosterItem::getVCard() const { + return vcard_; } +void ContactRosterItem::setVCard(VCard::ref vcard) { + vcard_ = vcard; + onDataChanged(); +} +} diff --git a/Swift/Controllers/Roster/ContactRosterItem.h b/Swift/Controllers/Roster/ContactRosterItem.h index 67a9722..a300819 100644 --- a/Swift/Controllers/Roster/ContactRosterItem.h +++ b/Swift/Controllers/Roster/ContactRosterItem.h @@ -1,79 +1,106 @@ /* - * Copyright (c) 2010-2013 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once +#include <memory> +#include <set> #include <string> -#include "Swiften/JID/JID.h" -#include "Swift/Controllers/Roster/RosterItem.h" -#include "Swiften/Elements/StatusShow.h" -#include "Swiften/Elements/Presence.h" +#include <vector> -#include <map> -#include <set> #include <boost/bind.hpp> -#include "Swiften/Base/boost_bsignals.h" -#include <boost/shared_ptr.hpp> #include <boost/date_time/posix_time/ptime.hpp> #include <boost/filesystem/path.hpp> +#include <boost/signals2.hpp> + +#include <Swiften/Elements/MUCOccupant.h> +#include <Swiften/Elements/StatusShow.h> +#include <Swiften/Elements/VCard.h> +#include <Swiften/JID/JID.h> + +#include <Swift/Controllers/Roster/RosterItem.h> namespace Swift { class GroupRosterItem; +class Presence; + class ContactRosterItem : public RosterItem { - public: - enum Feature { - FileTransferFeature, - WhiteboardFeature - }; - - enum BlockState { - BlockingNotSupported, - IsBlocked, - IsUnblocked - }; - - public: - ContactRosterItem(const JID& jid, const JID& displayJID, const std::string& name, GroupRosterItem* parent); - virtual ~ContactRosterItem(); - - StatusShow::Type getStatusShow() const; - StatusShow::Type getSimplifiedStatusShow() const; - std::string getStatusText() const; - std::string getIdleText() const; - void setAvatarPath(const boost::filesystem::path& path); - const boost::filesystem::path& getAvatarPath() const; - const JID& getJID() const; - void setDisplayJID(const JID& jid); - const JID& getDisplayJID() const; - void applyPresence(const std::string& resource, boost::shared_ptr<Presence> presence); - void clearPresence(); - void calculateShownPresence(); - const std::vector<std::string>& getGroups() const; - /** Only used so a contact can know about the groups it's in*/ - void addGroup(const std::string& group); - void removeGroup(const std::string& group); - - void setSupportedFeatures(const std::set<Feature>& features); - bool supportsFeature(Feature feature) const; - - void setBlockState(BlockState state); - BlockState blockState() const; - - private: - JID jid_; - JID displayJID_; - boost::posix_time::ptime lastAvailableTime_; - boost::filesystem::path avatarPath_; - std::map<std::string, boost::shared_ptr<Presence> > presences_; - boost::shared_ptr<Presence> offlinePresence_; - boost::shared_ptr<Presence> shownPresence_; - std::vector<std::string> groups_; - std::set<Feature> features_; - BlockState blockState_; + public: + enum Feature { + FileTransferFeature, + WhiteboardFeature + }; + + enum BlockState { + BlockingNotSupported, + IsBlocked, + IsUnblocked, + IsDomainBlocked + }; + + public: + /** + * @brief ContactRosterItem contains the information of a contact that is part of a XMPP Roster. + * @param jid The JabberID of the contact in the Roster entry. + * @param displayJID An alternate JID that is used instead of the JID this item represents. If not available, + * an empty node should be passed. This parameter will be converted to a bare JID. + * @param name The name or nickname of the contact + * @param parent The roster group that the contact is a member of. The same JID may be in several roster groups, in which case they will have individual ContactRosterItems with the same JID. + */ + ContactRosterItem(const JID& jid, const JID& displayJID, const std::string& name, GroupRosterItem* parent); + virtual ~ContactRosterItem(); + + StatusShow::Type getStatusShow() const; + StatusShow::Type getSimplifiedStatusShow() const; + std::string getStatusText() const; + std::string getIdleText() const; + boost::posix_time::ptime getIdle() const; + std::string getOfflineSinceText() const; + boost::posix_time::ptime getOfflineSince() const; + void setAvatarPath(const boost::filesystem::path& path); + const boost::filesystem::path& getAvatarPath() const; + const JID& getJID() const; + void setDisplayJID(const JID& jid); + const JID& getDisplayJID() const; + void applyPresence(std::shared_ptr<Presence> presence); + const std::vector<std::string>& getGroups() const; + /** Only used so a contact can know about the groups it's in*/ + void addGroup(const std::string& group); + void removeGroup(const std::string& group); + void clearPresence(); + + MUCOccupant::Role getMUCRole() const; + void setMUCRole(const MUCOccupant::Role& role); + MUCOccupant::Affiliation getMUCAffiliation() const; + void setMUCAffiliation(const MUCOccupant::Affiliation& affiliation); + std::string getMUCAffiliationText() const; + + void setSupportedFeatures(const std::set<Feature>& features); + bool supportsFeature(Feature feature) const; + + void setBlockState(BlockState state); + BlockState blockState() const; + + VCard::ref getVCard() const; + void setVCard(VCard::ref vcard); + + boost::signals2::signal<void ()> onVCardRequested; + + private: + JID jid_; + JID displayJID_; + boost::filesystem::path avatarPath_; + std::shared_ptr<Presence> presence_; + std::vector<std::string> groups_; + MUCOccupant::Role mucRole_; + MUCOccupant::Affiliation mucAffiliation_; + std::set<Feature> features_; + BlockState blockState_; + VCard::ref vcard_; }; } diff --git a/Swift/Controllers/Roster/FuzzyRosterFilter.h b/Swift/Controllers/Roster/FuzzyRosterFilter.h new file mode 100644 index 0000000..8c45935 --- /dev/null +++ b/Swift/Controllers/Roster/FuzzyRosterFilter.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2013 Tobias Markmann + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +/* + * Copyright (c) 2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#pragma once + +#include <string> + +#include <Swift/Controllers/ContactSuggester.h> +#include <Swift/Controllers/Roster/ContactRosterItem.h> +#include <Swift/Controllers/Roster/RosterFilter.h> +#include <Swift/Controllers/Roster/RosterItem.h> + +namespace Swift { + +class FuzzyRosterFilter : public RosterFilter { + public: + FuzzyRosterFilter(const std::string& query) : query_(query) { } + virtual ~FuzzyRosterFilter() {} + virtual bool operator() (RosterItem* item) const { + ContactRosterItem *contactItem = dynamic_cast<ContactRosterItem*>(item); + if (contactItem) { + const bool itemMatched = ContactSuggester::fuzzyMatch(contactItem->getDisplayName(), query_) || ContactSuggester::fuzzyMatch(contactItem->getDisplayJID(), query_); + return !itemMatched; + } else { + return false; + } + } + + private: + std::string query_; +}; + +} + + diff --git a/Swift/Controllers/Roster/GroupRosterItem.cpp b/Swift/Controllers/Roster/GroupRosterItem.cpp index 2a7bfa5..af5d0ca 100644 --- a/Swift/Controllers/Roster/GroupRosterItem.cpp +++ b/Swift/Controllers/Roster/GroupRosterItem.cpp @@ -1,19 +1,19 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2017 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ -#include "Swift/Controllers/Roster/GroupRosterItem.h" +#include <Swift/Controllers/Roster/GroupRosterItem.h> + +#include <memory> #include <boost/bind.hpp> -//#include <boost/algorithm.hpp> -#include <iostream> namespace Swift { GroupRosterItem::GroupRosterItem(const std::string& name, GroupRosterItem* parent, bool sortByStatus) : RosterItem(name, parent), sortByStatus_(sortByStatus), manualSort_(false) { - expanded_ = true; + expanded_ = true; } GroupRosterItem::~GroupRosterItem() { @@ -21,214 +21,212 @@ GroupRosterItem::~GroupRosterItem() { } void GroupRosterItem::setManualSort(const std::string& manualSortValue) { - manualSort_ = true; - bool changed = manualSortValue_ != manualSortValue; - manualSortValue_ = manualSortValue; - if (changed) { - onChildrenChanged(); - onDataChanged(); - } + manualSort_ = true; + bool changed = manualSortValue_ != manualSortValue; + manualSortValue_ = manualSortValue; + if (changed) { + onChildrenChanged(); + onDataChanged(); + } } const std::string& GroupRosterItem::getSortableDisplayName() const { - return manualSort_ ? manualSortValue_ : RosterItem::getSortableDisplayName(); + return manualSort_ ? manualSortValue_ : RosterItem::getSortableDisplayName(); } bool GroupRosterItem::isExpanded() const { - return expanded_; + return expanded_; } /** - This has no effect, and is only used by the UI. - If reTransmit is specified, dataChanged will be emitted on a change - - This may be undesireable if called from the UI, so you can use reTransmit=false - to avoid a loop in this case. + This has no effect, and is only used by the UI. + If reTransmit is specified, dataChanged will be emitted on a change - + This may be undesireable if called from the UI, so you can use reTransmit=false + to avoid a loop in this case. */ void GroupRosterItem::setExpanded(bool expanded) { - bool old = expanded_; - expanded_ = expanded; - if (expanded != old) { - onExpandedChanged(expanded); - } + bool old = expanded_; + expanded_ = expanded; + if (expanded != old) { + onExpandedChanged(expanded); + } } const std::vector<RosterItem*>& GroupRosterItem::getChildren() const { - return children_; + return children_; } const std::vector<RosterItem*>& GroupRosterItem::getDisplayedChildren() const { - return displayedChildren_; + return displayedChildren_; } void GroupRosterItem::addChild(RosterItem* item) { - children_.push_back(item); - GroupRosterItem* group = dynamic_cast<GroupRosterItem*>(item); - if (group) { - group->onChildrenChanged.connect(boost::bind(&GroupRosterItem::handleChildrenChanged, this, group)); - } else { - item->onDataChanged.connect(boost::bind(&GroupRosterItem::handleDataChanged, this, item)); - } - onChildrenChanged(); - onDataChanged(); + children_.push_back(item); + GroupRosterItem* group = dynamic_cast<GroupRosterItem*>(item); + if (group) { + group->onChildrenChanged.connect(boost::bind(&GroupRosterItem::handleChildrenChanged, this, group)); + } else { + item->onDataChanged.connect(boost::bind(&GroupRosterItem::handleDataChanged, this, item)); + } + onChildrenChanged(); + onDataChanged(); } /** * Does not emit a changed signal. */ void GroupRosterItem::removeAll() { - std::vector<RosterItem*>::iterator it = children_.begin(); - displayedChildren_.clear(); - while (it != children_.end()) { - ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(*it); - if (contact) { - delete contact; - } else { - GroupRosterItem* group = dynamic_cast<GroupRosterItem*>(*it); - if (group) { - group->removeAll(); - delete group; - } - } - ++it; - } - children_.clear(); + std::vector<RosterItem*>::iterator it = children_.begin(); + displayedChildren_.clear(); + while (it != children_.end()) { + ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(*it); + if (contact) { + delete contact; + } else { + GroupRosterItem* group = dynamic_cast<GroupRosterItem*>(*it); + if (group) { + group->removeAll(); + delete group; + } + } + ++it; + } + children_.clear(); } /** * Returns the removed item - but only if it's the only one, otherwise * the return result is undefined. */ -ContactRosterItem* GroupRosterItem::removeChild(const JID& jid) { - std::vector<RosterItem*>::iterator it = children_.begin(); - ContactRosterItem* removed = NULL; - while (it != children_.end()) { - ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(*it); - if (contact && contact->getJID() == jid) { - displayedChildren_.erase(std::remove(displayedChildren_.begin(), displayedChildren_.end(), contact), displayedChildren_.end()); - removed = contact; - delete contact; - it = children_.erase(it); - continue; - } - GroupRosterItem* group = dynamic_cast<GroupRosterItem*>(*it); - if (group) { - ContactRosterItem* groupRemoved = group->removeChild(jid); - if (groupRemoved) { - removed = groupRemoved; - } - } - ++it; - } - onChildrenChanged(); - onDataChanged(); - return removed; -} - -GroupRosterItem* GroupRosterItem::removeGroupChild(const std::string& groupName) { - std::vector<RosterItem*>::iterator it = children_.begin(); - GroupRosterItem* removed = NULL; - while (it != children_.end()) { - GroupRosterItem* group = dynamic_cast<GroupRosterItem*>(*it); - if (group && group->getDisplayName() == groupName) { - displayedChildren_.erase(std::remove(displayedChildren_.begin(), displayedChildren_.end(), group), displayedChildren_.end()); - removed = group; - delete group; - it = children_.erase(it); - continue; - } - ++it; - } - onChildrenChanged(); - onDataChanged(); - return removed; +std::unique_ptr<ContactRosterItem> GroupRosterItem::removeChild(const JID& jid) { + auto it = children_.begin(); + std::unique_ptr<ContactRosterItem> removed; + while (it != children_.end()) { + ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(*it); + if (contact && contact->getJID() == jid) { + displayedChildren_.erase(std::remove(displayedChildren_.begin(), displayedChildren_.end(), contact), displayedChildren_.end()); + removed = std::unique_ptr<ContactRosterItem>(contact); + it = children_.erase(it); + continue; + } + GroupRosterItem* group = dynamic_cast<GroupRosterItem*>(*it); + if (group) { + auto groupRemoved = group->removeChild(jid); + if (groupRemoved) { + removed = std::move(groupRemoved); + } + } + ++it; + } + onChildrenChanged(); + onDataChanged(); + return removed; +} + +std::unique_ptr<GroupRosterItem> GroupRosterItem::removeGroupChild(const std::string& groupName) { + std::vector<RosterItem*>::iterator it = children_.begin(); + std::unique_ptr<GroupRosterItem> removed; + while (it != children_.end()) { + GroupRosterItem* group = dynamic_cast<GroupRosterItem*>(*it); + if (group && group->getDisplayName() == groupName) { + displayedChildren_.erase(std::remove(displayedChildren_.begin(), displayedChildren_.end(), group), displayedChildren_.end()); + removed = std::unique_ptr<GroupRosterItem>(group); + it = children_.erase(it); + continue; + } + ++it; + } + onChildrenChanged(); + onDataChanged(); + return removed; } /** * Returns false if the list didn't need a resort */ bool GroupRosterItem::sortDisplayed() { - /* Not doing this until we import boost::algorithm*/ -// if (boost::is_sorted(displayedChildren_begin(), displayedChildren_.end(), itemLessThan)) { -// return false; -// } - //Sholudn't need stable_sort here - std::sort(displayedChildren_.begin(), displayedChildren_.end(), sortByStatus_? itemLessThanWithStatus : itemLessThanWithoutStatus); - return true; + /* Not doing this until we import boost::algorithm*/ +// if (boost::is_sorted(displayedChildren_begin(), displayedChildren_.end(), itemLessThan)) { +// return false; +// } + //Sholudn't need stable_sort here + std::sort(displayedChildren_.begin(), displayedChildren_.end(), sortByStatus_? itemLessThanWithStatus : itemLessThanWithoutStatus); + return true; } bool GroupRosterItem::itemLessThanWithoutStatus(const RosterItem* left, const RosterItem* right) { - return left->getSortableDisplayName() < right->getSortableDisplayName(); + return left->getSortableDisplayName() < right->getSortableDisplayName(); } bool GroupRosterItem::itemLessThanWithStatus(const RosterItem* left, const RosterItem* right) { - const ContactRosterItem* leftContact = dynamic_cast<const ContactRosterItem*>(left); - const ContactRosterItem* rightContact = dynamic_cast<const ContactRosterItem*>(right); - if (leftContact) { - if (!rightContact) { - return false; - } - StatusShow::Type leftType = leftContact->getSimplifiedStatusShow(); - StatusShow::Type rightType = rightContact->getSimplifiedStatusShow(); - if (leftType == rightType) { - return left->getSortableDisplayName() < right->getSortableDisplayName(); - } else { - return leftType < rightType; - } - } else { - if (rightContact) { - return true; - } - return left->getSortableDisplayName() < right->getSortableDisplayName(); - } + const ContactRosterItem* leftContact = dynamic_cast<const ContactRosterItem*>(left); + const ContactRosterItem* rightContact = dynamic_cast<const ContactRosterItem*>(right); + if (leftContact) { + if (!rightContact) { + return false; + } + StatusShow::Type leftType = leftContact->getSimplifiedStatusShow(); + StatusShow::Type rightType = rightContact->getSimplifiedStatusShow(); + if (leftType == rightType) { + return left->getSortableDisplayName() < right->getSortableDisplayName(); + } else { + return leftType < rightType; + } + } else { + if (rightContact) { + return true; + } + return left->getSortableDisplayName() < right->getSortableDisplayName(); + } } void GroupRosterItem::setDisplayed(RosterItem* item, bool displayed) { - bool found = false; - for (size_t i = 0; i < displayedChildren_.size(); i++) { - if (displayedChildren_[i] == item) { - found = true; - } - } - if (found == displayed) { - return; - } - if (displayed) { - displayedChildren_.push_back(item); - sortDisplayed(); - } else { - displayedChildren_.erase(std::remove(displayedChildren_.begin(), displayedChildren_.end(), item), displayedChildren_.end()); - } - onChildrenChanged(); - onDataChanged(); + bool found = false; + for (auto& i : displayedChildren_) { + if (i == item) { + found = true; + } + } + if (found == displayed) { + return; + } + if (displayed) { + displayedChildren_.push_back(item); + sortDisplayed(); + } else { + displayedChildren_.erase(std::remove(displayedChildren_.begin(), displayedChildren_.end(), item), displayedChildren_.end()); + } + onChildrenChanged(); + onDataChanged(); } void GroupRosterItem::handleDataChanged(RosterItem* /*item*/) { - if (sortDisplayed()) { - onChildrenChanged(); - } + if (sortDisplayed()) { + onChildrenChanged(); + } } void GroupRosterItem::handleChildrenChanged(GroupRosterItem* group) { - size_t oldSize = getDisplayedChildren().size(); - if (group->getDisplayedChildren().size() > 0) { - bool found = false; - for (size_t i = 0; i < displayedChildren_.size(); i++) { - if (displayedChildren_[i] == group) { - found = true; - } - } - if (!found) { - displayedChildren_.push_back(group); - sortDisplayed(); - } - } else { - displayedChildren_.erase(std::remove(displayedChildren_.begin(), displayedChildren_.end(), group), displayedChildren_.end()); - } - - if (oldSize != getDisplayedChildren().size() || sortDisplayed()) { - onChildrenChanged(); - onDataChanged(); - } + size_t oldSize = getDisplayedChildren().size(); + if (group->getDisplayedChildren().size() > 0) { + bool found = false; + for (auto& i : displayedChildren_) { + if (i == group) { + found = true; + } + } + if (!found) { + displayedChildren_.push_back(group); + sortDisplayed(); + } + } else { + displayedChildren_.erase(std::remove(displayedChildren_.begin(), displayedChildren_.end(), group), displayedChildren_.end()); + } + + if (oldSize != getDisplayedChildren().size() || sortDisplayed()) { + onChildrenChanged(); + onDataChanged(); + } } diff --git a/Swift/Controllers/Roster/GroupRosterItem.h b/Swift/Controllers/Roster/GroupRosterItem.h index beb7705..ebb3849 100644 --- a/Swift/Controllers/Roster/GroupRosterItem.h +++ b/Swift/Controllers/Roster/GroupRosterItem.h @@ -1,49 +1,57 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include "Swift/Controllers/Roster/RosterItem.h" #include <string> -#include "Swift/Controllers/Roster/ContactRosterItem.h" - #include <vector> +#include <Swift/Controllers/Roster/ContactRosterItem.h> +#include <Swift/Controllers/Roster/RosterItem.h> + namespace Swift { class GroupRosterItem : public RosterItem { - public: - GroupRosterItem(const std::string& name, GroupRosterItem* parent, bool sortByStatus); - virtual ~GroupRosterItem(); - const std::vector<RosterItem*>& getChildren() const; - const std::vector<RosterItem*>& getDisplayedChildren() const; - void addChild(RosterItem* item); - ContactRosterItem* removeChild(const JID& jid); - GroupRosterItem* removeGroupChild(const std::string& group); - void removeAll(); - void setDisplayed(RosterItem* item, bool displayed); - boost::signal<void ()> onChildrenChanged; - static bool itemLessThanWithStatus(const RosterItem* left, const RosterItem* right); - static bool itemLessThanWithoutStatus(const RosterItem* left, const RosterItem* right); - void setExpanded(bool expanded); - bool isExpanded() const; - boost::signal<void (bool)> onExpandedChanged; - void setManualSort(const std::string& manualSortValue); - virtual const std::string& getSortableDisplayName() const; - private: - void handleChildrenChanged(GroupRosterItem* group); - void handleDataChanged(RosterItem* item); - bool sortDisplayed(); - std::string name_; - bool expanded_; - std::vector<RosterItem*> children_; - std::vector<RosterItem*> displayedChildren_; - bool sortByStatus_; - bool manualSort_; - std::string manualSortValue_; + public: + GroupRosterItem(const std::string& name, GroupRosterItem* parent, bool sortByStatus); + virtual ~GroupRosterItem(); + + const std::vector<RosterItem*>& getChildren() const; + const std::vector<RosterItem*>& getDisplayedChildren() const; + + void addChild(RosterItem* item); + std::unique_ptr<ContactRosterItem> removeChild(const JID& jid); + std::unique_ptr<GroupRosterItem> removeGroupChild(const std::string& group); + void removeAll(); + + void setDisplayed(RosterItem* item, bool displayed); + void setExpanded(bool expanded); + bool isExpanded() const; + void setManualSort(const std::string& manualSortValue); + virtual const std::string& getSortableDisplayName() const; + + boost::signals2::signal<void (bool)> onExpandedChanged; + boost::signals2::signal<void ()> onChildrenChanged; + + static bool itemLessThanWithStatus(const RosterItem* left, const RosterItem* right); + static bool itemLessThanWithoutStatus(const RosterItem* left, const RosterItem* right); + + private: + void handleChildrenChanged(GroupRosterItem* group); + void handleDataChanged(RosterItem* item); + bool sortDisplayed(); + + private: + std::string name_; + bool expanded_; + std::vector<RosterItem*> children_; + std::vector<RosterItem*> displayedChildren_; + bool sortByStatus_; + bool manualSort_; + std::string manualSortValue_; }; } diff --git a/Swift/Controllers/Roster/ItemOperations/AppearOffline.h b/Swift/Controllers/Roster/ItemOperations/AppearOffline.h new file mode 100644 index 0000000..c57974b --- /dev/null +++ b/Swift/Controllers/Roster/ItemOperations/AppearOffline.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#pragma once + +#include <Swift/Controllers/Roster/ContactRosterItem.h> +#include <Swift/Controllers/Roster/ItemOperations/RosterItemOperation.h> + +namespace Swift { + +class RosterItem; + +class AppearOffline : public RosterItemOperation { + public: + AppearOffline() { + } + + virtual void operator() (RosterItem* item) const { + ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(item); + if (contact) { + contact->clearPresence(); + } + } + +}; + +} + + diff --git a/Swift/Controllers/Roster/ItemOperations/RosterItemOperation.h b/Swift/Controllers/Roster/ItemOperations/RosterItemOperation.h new file mode 100644 index 0000000..c633c20 --- /dev/null +++ b/Swift/Controllers/Roster/ItemOperations/RosterItemOperation.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#pragma once + +#include <Swiften/JID/JID.h> + +#include <Swift/Controllers/Roster/RosterItem.h> + +namespace Swift { + +class RosterItemOperation { + public: + RosterItemOperation(bool requiresLookup = false, const JID& lookupJID = JID()) : requiresLookup_(requiresLookup), lookupJID_(lookupJID) {} + virtual ~RosterItemOperation() {} + bool requiresLookup() const {return requiresLookup_;} + const JID& lookupJID() const {return lookupJID_;} + /** + * This is called when iterating over possible subjects, so must check it's + * applying to the right items - even if requiresLookup() is true an item + * with the same bare JID but different full JID may be passed. + */ + virtual void operator() (RosterItem*) const = 0; + private: + bool requiresLookup_; + JID lookupJID_; +}; + +} diff --git a/Swift/Controllers/Roster/ItemOperations/SetAvailableFeatures.h b/Swift/Controllers/Roster/ItemOperations/SetAvailableFeatures.h new file mode 100644 index 0000000..29f9722 --- /dev/null +++ b/Swift/Controllers/Roster/ItemOperations/SetAvailableFeatures.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2013 Tobias Markmann + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +/* + * Copyright (c) 2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#pragma once + +#include <Swiften/JID/JID.h> + +#include <Swift/Controllers/Roster/ContactRosterItem.h> +#include <Swift/Controllers/Roster/ItemOperations/RosterItemOperation.h> + +namespace Swift { + +class RosterItem; + +class SetAvailableFeatures : public RosterItemOperation { + public: + SetAvailableFeatures(const JID& jid, const std::set<ContactRosterItem::Feature>& availableFeatures, JID::CompareType compareType = JID::WithoutResource) : RosterItemOperation(true, jid), jid_(jid), availableFeatures_(availableFeatures), compareType_(compareType) { + } + + virtual void operator() (RosterItem* item) const { + ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(item); + if (contact && contact->getJID().equals(jid_, compareType_)) { + contact->setSupportedFeatures(availableFeatures_); + } + } + + private: + JID jid_; + std::set<ContactRosterItem::Feature> availableFeatures_; + JID::CompareType compareType_; +}; + +} diff --git a/Swift/Controllers/Roster/ItemOperations/SetAvatar.h b/Swift/Controllers/Roster/ItemOperations/SetAvatar.h new file mode 100644 index 0000000..d47c921 --- /dev/null +++ b/Swift/Controllers/Roster/ItemOperations/SetAvatar.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#pragma once + +#include <boost/filesystem/path.hpp> + +#include <Swiften/Elements/Presence.h> +#include <Swiften/JID/JID.h> + +#include <Swift/Controllers/Roster/ContactRosterItem.h> +#include <Swift/Controllers/Roster/ItemOperations/RosterItemOperation.h> + +namespace Swift { + +class RosterItem; + +class SetAvatar : public RosterItemOperation { + public: + SetAvatar(const JID& jid, const boost::filesystem::path& path, JID::CompareType compareType = JID::WithoutResource) : RosterItemOperation(true, jid), jid_(jid), path_(path), compareType_(compareType) { + } + + virtual void operator() (RosterItem* item) const { + ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(item); + if (contact && contact->getJID().equals(jid_, compareType_)) { + contact->setAvatarPath(path_); + } + } + + private: + JID jid_; + boost::filesystem::path path_; + JID::CompareType compareType_; +}; + +} diff --git a/Swift/Controllers/Roster/ItemOperations/SetBlockingState.h b/Swift/Controllers/Roster/ItemOperations/SetBlockingState.h new file mode 100644 index 0000000..818d9b4 --- /dev/null +++ b/Swift/Controllers/Roster/ItemOperations/SetBlockingState.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2013 Tobias Markmann + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +/* + * Copyright (c) 2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#pragma once + +#include <Swiften/JID/JID.h> + +#include <Swift/Controllers/Roster/ContactRosterItem.h> +#include <Swift/Controllers/Roster/ItemOperations/RosterItemOperation.h> + +namespace Swift { + +class RosterItem; + +class SetBlockingState : public RosterItemOperation { + public: + SetBlockingState(const JID& jid, ContactRosterItem::BlockState state, JID::CompareType compareType = JID::WithoutResource) : RosterItemOperation(!jid.getNode().empty(), jid), jid_(jid), state_(state), compareType_(compareType) { + if (state_ == ContactRosterItem::IsBlocked && jid.getNode().empty()) { + state_ = ContactRosterItem::IsDomainBlocked; + } + } + + virtual void operator() (RosterItem* item) const { + ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(item); + if (jid_.getNode().empty()) { + if (contact && contact->getJID().getDomain() == jid_.getDomain()) { + contact->setBlockState(state_); + } + } else { + if (contact && contact->getJID().equals(jid_, compareType_)) { + contact->setBlockState(state_); + } + } + } + + private: + JID jid_; + ContactRosterItem::BlockState state_; + JID::CompareType compareType_; +}; + +} diff --git a/Swift/Controllers/Roster/ItemOperations/SetMUC.h b/Swift/Controllers/Roster/ItemOperations/SetMUC.h new file mode 100644 index 0000000..7640c24 --- /dev/null +++ b/Swift/Controllers/Roster/ItemOperations/SetMUC.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2013-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#pragma once + +#include <Swiften/JID/JID.h> + +#include <Swift/Controllers/Roster/ContactRosterItem.h> +#include <Swift/Controllers/Roster/ItemOperations/RosterItemOperation.h> + +namespace Swift { + +class RosterItem; + +class SetMUC : public RosterItemOperation { + public: + SetMUC(const JID& jid, const MUCOccupant::Role& role, const MUCOccupant::Affiliation& affiliation) + : RosterItemOperation(true, jid), jid_(jid), mucRole_(role), mucAffiliation_(affiliation) { + } + + virtual void operator() (RosterItem* item) const { + ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(item); + if (contact && contact->getJID().equals(jid_, JID::WithResource)) { + contact->setMUCRole(mucRole_); + contact->setMUCAffiliation(mucAffiliation_); + } + } + + private: + JID jid_; + MUCOccupant::Role mucRole_; + MUCOccupant::Affiliation mucAffiliation_; +}; + +} diff --git a/Swift/Controllers/Roster/ItemOperations/SetName.h b/Swift/Controllers/Roster/ItemOperations/SetName.h new file mode 100644 index 0000000..fa0694d --- /dev/null +++ b/Swift/Controllers/Roster/ItemOperations/SetName.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#pragma once + +#include <Swiften/JID/JID.h> + +#include <Swift/Controllers/Roster/ContactRosterItem.h> +#include <Swift/Controllers/Roster/ItemOperations/RosterItemOperation.h> + +namespace Swift { + +class RosterItem; + +class SetName : public RosterItemOperation { + public: + SetName(const std::string& name, const JID& jid, JID::CompareType compareType = JID::WithoutResource) : RosterItemOperation(true, jid), name_(name), jid_(jid), compareType_(compareType) { + } + + virtual void operator() (RosterItem* item) const { + ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(item); + if (contact && contact->getJID().equals(jid_, compareType_)) { + contact->setDisplayName(name_); + } + } + + private: + std::string name_; + JID jid_; + JID::CompareType compareType_; +}; + +} + + diff --git a/Swift/Controllers/Roster/ItemOperations/SetPresence.h b/Swift/Controllers/Roster/ItemOperations/SetPresence.h new file mode 100644 index 0000000..fc39e5c --- /dev/null +++ b/Swift/Controllers/Roster/ItemOperations/SetPresence.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2010-2015 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#pragma once + +#include <Swiften/Elements/Presence.h> +#include <Swiften/JID/JID.h> + +#include <Swift/Controllers/Roster/ContactRosterItem.h> +#include <Swift/Controllers/Roster/ItemOperations/RosterItemOperation.h> + +namespace Swift { + +class RosterItem; + +class SetPresence : public RosterItemOperation { + public: + SetPresence(Presence::ref presence, JID::CompareType compareType = JID::WithoutResource) : RosterItemOperation(true, compareType == JID::WithoutResource ? presence->getFrom().toBare() : presence->getFrom()), presence_(presence), compareType_(compareType) { + } + + virtual void operator() (RosterItem* item) const { + ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(item); + if (contact && contact->getJID().equals(presence_->getFrom(), compareType_)) { + contact->applyPresence(presence_); + } + } + + private: + Presence::ref presence_; + JID::CompareType compareType_; +}; + +} + diff --git a/Swift/Controllers/Roster/ItemOperations/SetVCard.h b/Swift/Controllers/Roster/ItemOperations/SetVCard.h new file mode 100644 index 0000000..278ae56 --- /dev/null +++ b/Swift/Controllers/Roster/ItemOperations/SetVCard.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2013 Tobias Markmann + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +/* + * Copyright (c) 2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#pragma once + +#include <Swiften/Elements/VCard.h> +#include <Swiften/JID/JID.h> + +#include <Swift/Controllers/Roster/ContactRosterItem.h> +#include <Swift/Controllers/Roster/ItemOperations/RosterItemOperation.h> + +namespace Swift { + +class RosterItem; + +class SetVCard : public RosterItemOperation { + public: + SetVCard(const JID& jid, VCard::ref vcard, JID::CompareType compareType = JID::WithoutResource) : RosterItemOperation(true, jid), jid_(jid), vcard_(vcard), compareType_(compareType) { + } + + virtual void operator() (RosterItem* item) const { + ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(item); + if (contact && contact->getJID().equals(jid_, compareType_)) { + contact->setVCard(vcard_); + } + } + + private: + JID jid_; + VCard::ref vcard_; + JID::CompareType compareType_; +}; + +} diff --git a/Swift/Controllers/Roster/LeastCommonSubsequence.h b/Swift/Controllers/Roster/LeastCommonSubsequence.h index 9d45679..7988ee7 100644 --- a/Swift/Controllers/Roster/LeastCommonSubsequence.h +++ b/Swift/Controllers/Roster/LeastCommonSubsequence.h @@ -1,107 +1,108 @@ /* - * Copyright (c) 2011-2013 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2011-2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once #include <vector> + #include <boost/numeric/conversion/cast.hpp> namespace Swift { - using std::equal_to; + using std::equal_to; - namespace Detail { - template<typename XIt, typename YIt, typename Length, typename Predicate> - void computeLeastCommonSubsequenceMatrix(XIt xBegin, XIt xEnd, YIt yBegin, YIt yEnd, std::vector<Length>& result) { - size_t width = static_cast<size_t>(std::distance(xBegin, xEnd) + 1); - size_t height = static_cast<size_t>(std::distance(yBegin, yEnd) + 1); - result.resize(width * height); + namespace Detail { + template<typename XIt, typename YIt, typename Length, typename Predicate> + void computeLeastCommonSubsequenceMatrix(XIt xBegin, XIt xEnd, YIt yBegin, YIt yEnd, std::vector<Length>& result) { + size_t width = static_cast<size_t>(std::distance(xBegin, xEnd) + 1); + size_t height = static_cast<size_t>(std::distance(yBegin, yEnd) + 1); + result.resize(width * height); - // Initialize first row & column - for (size_t i = 0; i < width; ++i) { - result[i] = 0; - } - for (size_t j = 0; j < height; ++j) { - result[j*width] = 0; - } + // Initialize first row & column + for (size_t i = 0; i < width; ++i) { + result[i] = 0; + } + for (size_t j = 0; j < height; ++j) { + result[j*width] = 0; + } - // Compute the LCS lengths for subsets - Predicate predicate; - for (size_t i = 1; i < width; ++i) { - for (size_t j = 1; j < height; ++j) { - result[i + j*width] = predicate(*(xBegin + boost::numeric_cast<long long>(i)-1), *(yBegin + boost::numeric_cast<long long >(j)-1)) ? result[(i-1) + (j-1)*width] + 1 : std::max(result[i + (j-1)*width], result[i-1 + (j*width)]); - } - } - } - } + // Compute the LCS lengths for subsets + Predicate predicate; + for (size_t i = 1; i < width; ++i) { + for (size_t j = 1; j < height; ++j) { + result[i + j*width] = predicate(*(xBegin + static_cast<long long>(i)-1), *(yBegin + static_cast<long long>(j)-1)) ? result[(i-1) + (j-1)*width] + 1 : std::max(result[i + (j-1)*width], result[i-1 + (j*width)]); + } + } + } + } - template<typename X, typename InsertRemovePredicate, typename UpdatePredicate> - void computeIndexDiff(const std::vector<X>& x, const std::vector<X>& y, std::vector<size_t>& updates, std::vector<size_t>& postUpdates, std::vector<size_t>& removes, std::vector<size_t>& inserts) { - InsertRemovePredicate insertRemovePredicate; - UpdatePredicate updatePredicate; + template<typename X, typename InsertRemovePredicate, typename UpdatePredicate> + void computeIndexDiff(const std::vector<X>& x, const std::vector<X>& y, std::vector<size_t>& updates, std::vector<size_t>& postUpdates, std::vector<size_t>& removes, std::vector<size_t>& inserts) { + InsertRemovePredicate insertRemovePredicate; + UpdatePredicate updatePredicate; - // Find & handle common prefix (Optimization to reduce LCS matrix size) - typename std::vector<X>::const_iterator xBegin = x.begin(); - typename std::vector<X>::const_iterator yBegin = y.begin(); - while (xBegin < x.end() && yBegin < y.end() && insertRemovePredicate(*xBegin, *yBegin)) { - if (updatePredicate(*xBegin, *yBegin)) { - updates.push_back(static_cast<size_t>(std::distance(x.begin(), xBegin))); - postUpdates.push_back(static_cast<size_t>(std::distance(y.begin(), yBegin))); - } - ++xBegin; - ++yBegin; - } - size_t prefixLength = static_cast<size_t>(std::distance(x.begin(), xBegin)); + // Find & handle common prefix (Optimization to reduce LCS matrix size) + typename std::vector<X>::const_iterator xBegin = x.begin(); + typename std::vector<X>::const_iterator yBegin = y.begin(); + while (xBegin < x.end() && yBegin < y.end() && insertRemovePredicate(*xBegin, *yBegin)) { + if (updatePredicate(*xBegin, *yBegin)) { + updates.push_back(static_cast<size_t>(std::distance(x.begin(), xBegin))); + postUpdates.push_back(static_cast<size_t>(std::distance(y.begin(), yBegin))); + } + ++xBegin; + ++yBegin; + } + size_t prefixLength = static_cast<size_t>(std::distance(x.begin(), xBegin)); - // Find & handle common suffix (Optimization to reduce LCS matrix size) - typename std::vector<X>::const_reverse_iterator xEnd = x.rbegin(); - typename std::vector<X>::const_reverse_iterator yEnd = y.rbegin(); - while (xEnd.base() > xBegin && yEnd.base() > yBegin && insertRemovePredicate(*xEnd, *yEnd)) { - if (updatePredicate(*xEnd, *yEnd)) { - updates.push_back(static_cast<size_t>(std::distance(x.begin(), xEnd.base()) - 1)); - postUpdates.push_back(static_cast<size_t>(std::distance(y.begin(), yEnd.base()) - 1)); - } - ++xEnd; - ++yEnd; - } + // Find & handle common suffix (Optimization to reduce LCS matrix size) + typename std::vector<X>::const_reverse_iterator xEnd = x.rbegin(); + typename std::vector<X>::const_reverse_iterator yEnd = y.rbegin(); + while (xEnd.base() > xBegin && yEnd.base() > yBegin && insertRemovePredicate(*xEnd, *yEnd)) { + if (updatePredicate(*xEnd, *yEnd)) { + updates.push_back(static_cast<size_t>(std::distance(x.begin(), xEnd.base()) - 1)); + postUpdates.push_back(static_cast<size_t>(std::distance(y.begin(), yEnd.base()) - 1)); + } + ++xEnd; + ++yEnd; + } - // Compute lengths - size_t xLength = static_cast<size_t>(std::distance(xBegin, xEnd.base())); - size_t yLength = static_cast<size_t>(std::distance(yBegin, yEnd.base())); + // Compute lengths + size_t xLength = static_cast<size_t>(std::distance(xBegin, xEnd.base())); + size_t yLength = static_cast<size_t>(std::distance(yBegin, yEnd.base())); - // Compute LCS matrix - std::vector<unsigned int> lcs; - Detail::computeLeastCommonSubsequenceMatrix<typename std::vector<X>::const_iterator, typename std::vector<X>::const_iterator, unsigned int, InsertRemovePredicate>(xBegin, xEnd.base(), yBegin, yEnd.base(), lcs); + // Compute LCS matrix + std::vector<unsigned int> lcs; + Detail::computeLeastCommonSubsequenceMatrix<typename std::vector<X>::const_iterator, typename std::vector<X>::const_iterator, unsigned int, InsertRemovePredicate>(xBegin, xEnd.base(), yBegin, yEnd.base(), lcs); - // Process LCS matrix - size_t i = xLength; - size_t j = yLength; - size_t width = xLength + 1; - while (true) { - if (i > 0 && j > 0 && insertRemovePredicate(x[prefixLength + i-1], y[prefixLength + j-1])) { - // x[i-1] same - if (updatePredicate(x[prefixLength + i - 1], y[prefixLength + j - 1])) { - updates.push_back(prefixLength + i-1); - postUpdates.push_back(prefixLength + j-1); - } - i -= 1; - j -= 1; - } - else if (j > 0 && (i == 0 || lcs[i + (j-1)*width] >= lcs[i-1 + j*width])) { - // y[j-1] added - inserts.push_back(prefixLength + j-1); - j -= 1; - } - else if (i > 0 && (j == 0 || lcs[i + (j-1)*width] < lcs[i-1 + j*width])) { - // x[i-1] removed - removes.push_back(prefixLength + i-1); - i -= 1; - } - else { - break; - } - } - } + // Process LCS matrix + size_t i = xLength; + size_t j = yLength; + size_t width = xLength + 1; + while (true) { + if (i > 0 && j > 0 && insertRemovePredicate(x[prefixLength + i-1], y[prefixLength + j-1])) { + // x[i-1] same + if (updatePredicate(x[prefixLength + i - 1], y[prefixLength + j - 1])) { + updates.push_back(prefixLength + i-1); + postUpdates.push_back(prefixLength + j-1); + } + i -= 1; + j -= 1; + } + else if (j > 0 && (i == 0 || lcs[i + (j-1)*width] >= lcs[i-1 + j*width])) { + // y[j-1] added + inserts.push_back(prefixLength + j-1); + j -= 1; + } + else if (i > 0 && (j == 0 || lcs[i + (j-1)*width] < lcs[i-1 + j*width])) { + // x[i-1] removed + removes.push_back(prefixLength + i-1); + i -= 1; + } + else { + break; + } + } + } } diff --git a/Swift/Controllers/Roster/OfflineRosterFilter.h b/Swift/Controllers/Roster/OfflineRosterFilter.h index 1af2624..033eecb 100644 --- a/Swift/Controllers/Roster/OfflineRosterFilter.h +++ b/Swift/Controllers/Roster/OfflineRosterFilter.h @@ -1,25 +1,26 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include "Swift/Controllers/Roster/ContactRosterItem.h" -#include "Swift/Controllers/Roster/RosterItem.h" -#include "Swift/Controllers/Roster/RosterFilter.h" -#include "Swiften/Elements/StatusShow.h" +#include <Swiften/Elements/StatusShow.h> + +#include <Swift/Controllers/Roster/ContactRosterItem.h> +#include <Swift/Controllers/Roster/RosterFilter.h> +#include <Swift/Controllers/Roster/RosterItem.h> namespace Swift { class OfflineRosterFilter : public RosterFilter { - public: - virtual ~OfflineRosterFilter() {} - virtual bool operator() (RosterItem *item) const { - ContactRosterItem *contactItem = dynamic_cast<ContactRosterItem*>(item); - return contactItem && contactItem->getStatusShow() == StatusShow::None; - } + public: + virtual ~OfflineRosterFilter() {} + virtual bool operator() (RosterItem *item) const { + ContactRosterItem *contactItem = dynamic_cast<ContactRosterItem*>(item); + return contactItem && contactItem->getStatusShow() == StatusShow::None; + } }; } diff --git a/Swift/Controllers/Roster/Roster.cpp b/Swift/Controllers/Roster/Roster.cpp index 9b45b63..5b7e454 100644 --- a/Swift/Controllers/Roster/Roster.cpp +++ b/Swift/Controllers/Roster/Roster.cpp @@ -1,261 +1,267 @@ /* - * Copyright (c) 2010-2013 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ -#include "Swift/Controllers/Roster/Roster.h" +#include <Swift/Controllers/Roster/Roster.h> -#include "Swiften/Base/foreach.h" +#include <algorithm> +#include <deque> +#include <memory> +#include <set> #include <string> -#include "Swiften/JID/JID.h" -#include "Swift/Controllers/Roster/ContactRosterItem.h" -#include "Swift/Controllers/Roster/RosterItem.h" -#include "Swift/Controllers/Roster/GroupRosterItem.h" -#include "Swift/Controllers/Roster/RosterItemOperation.h" #include <boost/bind.hpp> -#include <iostream> -#include <set> -#include <deque> +#include <Swiften/JID/JID.h> + +#include <Swift/Controllers/Roster/ContactRosterItem.h> +#include <Swift/Controllers/Roster/GroupRosterItem.h> +#include <Swift/Controllers/Roster/ItemOperations/RosterItemOperation.h> +#include <Swift/Controllers/Roster/RosterItem.h> namespace Swift { -Roster::Roster(bool sortByStatus, bool fullJIDMapping) : blockingSupported_(false) { - sortByStatus_ = sortByStatus; - fullJIDMapping_ = fullJIDMapping; - root_ = new GroupRosterItem("Dummy-Root", NULL, sortByStatus_); - root_->onChildrenChanged.connect(boost::bind(&Roster::handleChildrenChanged, this, root_)); +Roster::Roster(bool sortByStatus, bool fullJIDMapping) : fullJIDMapping_(fullJIDMapping), sortByStatus_(sortByStatus), root_(std::make_unique<GroupRosterItem>("Dummy-Root", nullptr, sortByStatus_)) { + root_->onChildrenChanged.connect(boost::bind(&Roster::handleChildrenChanged, this, root_.get())); } Roster::~Roster() { - std::deque<RosterItem*> queue; - queue.push_back(root_); - while (!queue.empty()) { - RosterItem* item = *queue.begin(); - queue.pop_front(); - GroupRosterItem* group = dynamic_cast<GroupRosterItem*>(item); - if (group) { - queue.insert(queue.begin(), group->getChildren().begin(), group->getChildren().end()); - } - delete item; - } + std::deque<RosterItem*> queue; + while (!queue.empty()) { + RosterItem* item = *queue.begin(); + queue.pop_front(); + GroupRosterItem* group = dynamic_cast<GroupRosterItem*>(item); + if (group) { + queue.insert(queue.begin(), group->getChildren().begin(), group->getChildren().end()); + } + ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(item); + if (contact) { + contact->onVCardRequested.disconnect(boost::bind(boost::ref(onVCardUpdateRequested), contact->getJID())); + } + delete item; + } } -GroupRosterItem* Roster::getRoot() { - return root_; +GroupRosterItem* Roster::getRoot() const { + return root_.get(); } -GroupRosterItem* Roster::getGroup(const std::string& groupName) { - foreach (RosterItem *item, root_->getChildren()) { - GroupRosterItem *group = dynamic_cast<GroupRosterItem*>(item); - if (group && group->getDisplayName() == groupName) { - return group; - } - } - GroupRosterItem* group = new GroupRosterItem(groupName, root_, sortByStatus_); - root_->addChild(group); - group->onChildrenChanged.connect(boost::bind(&Roster::handleChildrenChanged, this, group)); - group->onDataChanged.connect(boost::bind(&Roster::handleDataChanged, this, group)); - return group; +std::set<JID> Roster::getJIDs() const { + std::set<JID> jids; + + std::deque<RosterItem*> queue; + queue.push_back(root_.get()); + while (!queue.empty()) { + RosterItem* item = *queue.begin(); + queue.pop_front(); + GroupRosterItem* group = dynamic_cast<GroupRosterItem*>(item); + ContactRosterItem *contact = dynamic_cast<ContactRosterItem*>(item); + if (contact) { + jids.insert(contact->getJID()); + jids.insert(contact->getDisplayJID()); + } + else if (group) { + queue.insert(queue.begin(), group->getChildren().begin(), group->getChildren().end()); + } + } + + return jids; } -void Roster::setAvailableFeatures(const JID& jid, const std::set<ContactRosterItem::Feature>& features) { - ItemMap::const_iterator i = itemMap_.find(fullJIDMapping_ ? jid : jid.toBare()); - if (i == itemMap_.end()) { - return; - } - foreach(ContactRosterItem* item, i->second) { - item->setSupportedFeatures(features); - } +GroupRosterItem* Roster::getGroup(const std::string& groupName) { + for (auto* item : root_->getChildren()) { + GroupRosterItem *group = dynamic_cast<GroupRosterItem*>(item); + if (group && group->getDisplayName() == groupName) { + return group; + } + } + GroupRosterItem* group = new GroupRosterItem(groupName, root_.get(), sortByStatus_); + root_->addChild(group); + group->onChildrenChanged.connect(boost::bind(&Roster::handleChildrenChanged, this, group)); + group->onDataChanged.connect(boost::bind(&Roster::handleDataChanged, this, group)); + return group; } -void Roster::setBlockedState(const std::vector<JID> &jids, ContactRosterItem::BlockState state) { - if (!blockingSupported_ ) { - foreach(ItemMap::value_type i, itemMap_) { - foreach(ContactRosterItem* item, i.second) { - item->setBlockState(ContactRosterItem::IsUnblocked); - } - } - } - - foreach(const JID& jid, jids) { - ItemMap::const_iterator i = itemMap_.find(fullJIDMapping_ ? jid : jid.toBare()); - if (i == itemMap_.end()) { - continue; - } - foreach(ContactRosterItem* item, i->second) { - item->setBlockState(state); - } - } - - blockingSupported_ = true; +void Roster::setBlockingSupported(bool isSupported) { + if (!blockingSupported_) { + for (auto i : itemMap_) { + for (auto* item : i.second) { + item->setBlockState(ContactRosterItem::IsUnblocked); + } + } + } + blockingSupported_ = isSupported; } void Roster::removeGroup(const std::string& group) { - root_->removeGroupChild(group); + root_->removeGroupChild(group); } void Roster::handleDataChanged(RosterItem* item) { - onDataChanged(item); + onDataChanged(item); } void Roster::handleChildrenChanged(GroupRosterItem* item) { - onChildrenChanged(item); + onChildrenChanged(item); } void Roster::addContact(const JID& jid, const JID& displayJID, const std::string& name, const std::string& groupName, const boost::filesystem::path& avatarPath) { - GroupRosterItem* group(getGroup(groupName)); - ContactRosterItem *item = new ContactRosterItem(jid, displayJID, name, group); - item->setAvatarPath(avatarPath); - if (blockingSupported_) { - item->setBlockState(ContactRosterItem::IsUnblocked); - } - group->addChild(item); - ItemMap::iterator i = itemMap_.insert(std::make_pair(fullJIDMapping_ ? jid : jid.toBare(), std::vector<ContactRosterItem*>())).first; - if (!i->second.empty()) { - foreach (const std::string& existingGroup, i->second[0]->getGroups()) { - item->addGroup(existingGroup); - } - } - i->second.push_back(item); - item->onDataChanged.connect(boost::bind(&Roster::handleDataChanged, this, item)); - filterContact(item, group); - - foreach (ContactRosterItem* item, i->second) { - item->addGroup(groupName); - } + GroupRosterItem* group(getGroup(groupName)); + ContactRosterItem *item = new ContactRosterItem(jid, displayJID, name, group); + item->onVCardRequested.connect(boost::bind(boost::ref(onVCardUpdateRequested), jid)); + item->setAvatarPath(avatarPath); + if (blockingSupported_) { + item->setBlockState(ContactRosterItem::IsUnblocked); + } + group->addChild(item); + ItemMap::iterator i = itemMap_.insert(std::make_pair(fullJIDMapping_ ? jid : jid.toBare(), std::vector<ContactRosterItem*>())).first; + if (!i->second.empty()) { + for (const auto& existingGroup : i->second[0]->getGroups()) { + item->addGroup(existingGroup); + } + } + i->second.push_back(item); + item->onDataChanged.connect(boost::bind(&Roster::handleDataChanged, this, item)); + filterContact(item, group); + + for (auto* item : i->second) { + item->addGroup(groupName); + } } struct JIDEqualsTo { - JIDEqualsTo(const JID& jid) : jid(jid) {} - bool operator()(ContactRosterItem* i) const { return jid == i->getJID(); } - JID jid; + JIDEqualsTo(const JID& jid) : jid(jid) {} + bool operator()(ContactRosterItem* i) const { return jid == i->getJID(); } + JID jid; }; void Roster::removeAll() { - root_->removeAll(); - itemMap_.clear(); - onChildrenChanged(root_); - onDataChanged(root_); + root_->removeAll(); + itemMap_.clear(); + onChildrenChanged(root_.get()); + onDataChanged(root_.get()); } void Roster::removeContact(const JID& jid) { - ItemMap::iterator item = itemMap_.find(fullJIDMapping_ ? jid : jid.toBare()); - if (item != itemMap_.end()) { - std::vector<ContactRosterItem*>& items = item->second; - items.erase(std::remove_if(items.begin(), items.end(), JIDEqualsTo(jid)), items.end()); - if (items.empty()) { - itemMap_.erase(item); - } - } - //Causes the delete - root_->removeChild(jid); + ItemMap::iterator item = itemMap_.find(fullJIDMapping_ ? jid : jid.toBare()); + if (item != itemMap_.end()) { + std::vector<ContactRosterItem*>& items = item->second; + items.erase(std::remove_if(items.begin(), items.end(), JIDEqualsTo(jid)), items.end()); + if (items.empty()) { + itemMap_.erase(item); + } + } + //Causes the delete + root_->removeChild(jid); } void Roster::removeContactFromGroup(const JID& jid, const std::string& groupName) { - std::vector<RosterItem*> children = root_->getChildren(); - std::vector<RosterItem*>::iterator it = children.begin(); - ItemMap::iterator itemIt = itemMap_.find(fullJIDMapping_ ? jid : jid.toBare()); - while (it != children.end()) { - GroupRosterItem* group = dynamic_cast<GroupRosterItem*>(*it); - if (group && group->getDisplayName() == groupName) { - ContactRosterItem* deleted = group->removeChild(jid); - if (itemIt != itemMap_.end()) { - std::vector<ContactRosterItem*>& items = itemIt->second; - items.erase(std::remove(items.begin(), items.end(), deleted), items.end()); - } - } - ++it; - } - - if (itemIt != itemMap_.end()) { - foreach (ContactRosterItem* item, itemIt->second) { - item->removeGroup(groupName); - } - } + std::vector<RosterItem*> children = root_->getChildren(); + std::vector<RosterItem*>::iterator it = children.begin(); + ItemMap::iterator itemIt = itemMap_.find(fullJIDMapping_ ? jid : jid.toBare()); + while (it != children.end()) { + GroupRosterItem* group = dynamic_cast<GroupRosterItem*>(*it); + if (group && group->getDisplayName() == groupName) { + auto deleted = group->removeChild(jid); + if (itemIt != itemMap_.end()) { + std::vector<ContactRosterItem*>& items = itemIt->second; + items.erase(std::remove(items.begin(), items.end(), deleted.get()), items.end()); + } + } + ++it; + } + + if (itemIt != itemMap_.end()) { + for (auto* item : itemIt->second) { + item->removeGroup(groupName); + } + } } void Roster::applyOnItems(const RosterItemOperation& operation) { - if (operation.requiresLookup()) { - applyOnItem(operation, operation.lookupJID()); - } else { - applyOnAllItems(operation); - } + if (operation.requiresLookup()) { + applyOnItem(operation, operation.lookupJID()); + } + else { + applyOnAllItems(operation); + } } void Roster::applyOnItem(const RosterItemOperation& operation, const JID& jid) { - ItemMap::iterator i = itemMap_.find(fullJIDMapping_ ? jid : jid.toBare()); - if (i == itemMap_.end()) { - return; - } - foreach (ContactRosterItem* item, i->second) { - operation(item); - filterContact(item, item->getParent()); - } + ItemMap::iterator i = itemMap_.find(fullJIDMapping_ ? jid : jid.toBare()); + if (i == itemMap_.end()) { + return; + } + for (auto* item : i->second) { + operation(item); + filterContact(item, item->getParent()); + } } void Roster::applyOnAllItems(const RosterItemOperation& operation) { - std::deque<RosterItem*> queue; - queue.push_back(root_); - while (!queue.empty()) { - RosterItem* item = *queue.begin(); - queue.pop_front(); - operation(item); - GroupRosterItem* group = dynamic_cast<GroupRosterItem*>(item); - if (group) { - queue.insert(queue.begin(), group->getChildren().begin(), group->getChildren().end()); - } - } - filterAll(); + std::deque<RosterItem*> queue; + queue.push_back(root_.get()); + while (!queue.empty()) { + RosterItem* item = *queue.begin(); + queue.pop_front(); + operation(item); + GroupRosterItem* group = dynamic_cast<GroupRosterItem*>(item); + if (group) { + queue.insert(queue.begin(), group->getChildren().begin(), group->getChildren().end()); + } + } + filterAll(); +} + +void Roster::addFilter(RosterFilter* filter) { + filters_.push_back(filter); + filterAll(); + onFilterAdded(filter); } -void Roster::removeFilter(RosterFilter *filter) { - for (unsigned int i = 0; i < filters_.size(); i++) { - if (filters_[i] == filter) { - filters_.erase(filters_.begin() + i); - break; - } - } - filterAll(); +void Roster::removeFilter(RosterFilter* filter) { + filters_.erase(std::remove(filters_.begin(), filters_.end(), filter), filters_.end()); + filterAll(); + onFilterRemoved(filter); } void Roster::filterContact(ContactRosterItem* contact, GroupRosterItem* group) { - size_t oldDisplayedSize = group->getDisplayedChildren().size(); - bool hide = true; - foreach (RosterFilter *filter, filters_) { - hide &= (*filter)(contact); - } - group->setDisplayed(contact, filters_.empty() || !hide); - size_t newDisplayedSize = group->getDisplayedChildren().size(); - if (oldDisplayedSize == 0 && newDisplayedSize > 0) { - onGroupAdded(group); - } + size_t oldDisplayedSize = group->getDisplayedChildren().size(); + bool hide = true; + for (auto* filter : filters_) { + hide &= (*filter)(contact); + } + group->setDisplayed(contact, filters_.empty() || !hide); + size_t newDisplayedSize = group->getDisplayedChildren().size(); + if (oldDisplayedSize == 0 && newDisplayedSize > 0) { + onGroupAdded(group); + } } void Roster::filterGroup(GroupRosterItem* group) { - foreach (RosterItem* child, group->getChildren()) { - ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(child); - if (contact) { - filterContact(contact, group); - } - } + for (auto* child : group->getChildren()) { + ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(child); + if (contact) { + filterContact(contact, group); + } + } } void Roster::filterAll() { - std::deque<RosterItem*> queue; - queue.push_back(root_); - while (!queue.empty()) { - RosterItem *item = *queue.begin(); - queue.pop_front(); - GroupRosterItem* group = dynamic_cast<GroupRosterItem*>(item); - if (group) { - queue.insert(queue.begin(), group->getChildren().begin(), group->getChildren().end()); - filterGroup(group); - } - } + std::deque<RosterItem*> queue; + queue.push_back(root_.get()); + while (!queue.empty()) { + RosterItem *item = *queue.begin(); + queue.pop_front(); + GroupRosterItem* group = dynamic_cast<GroupRosterItem*>(item); + if (group) { + queue.insert(queue.begin(), group->getChildren().begin(), group->getChildren().end()); + filterGroup(group); + } + } } } diff --git a/Swift/Controllers/Roster/Roster.h b/Swift/Controllers/Roster/Roster.h index a4c8b99..d22b38d 100644 --- a/Swift/Controllers/Roster/Roster.h +++ b/Swift/Controllers/Roster/Roster.h @@ -1,21 +1,24 @@ /* - * Copyright (c) 2010-2013 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once +#include <map> +#include <memory> +#include <set> #include <string> -#include "Swiften/JID/JID.h" -#include "Swift/Controllers/Roster/RosterItemOperation.h" -#include "Swift/Controllers/Roster/RosterFilter.h" -#include <Swift/Controllers/Roster/ContactRosterItem.h> - #include <vector> -#include <map> -#include "Swiften/Base/boost_bsignals.h" -#include <boost/shared_ptr.hpp> + +#include <boost/signals2.hpp> + +#include <Swiften/JID/JID.h> + +#include <Swift/Controllers/Roster/ContactRosterItem.h> +#include <Swift/Controllers/Roster/ItemOperations/RosterItemOperation.h> +#include <Swift/Controllers/Roster/RosterFilter.h> namespace Swift { @@ -24,42 +27,48 @@ class GroupRosterItem; class ContactRosterItem; class Roster { - public: - Roster(bool sortByStatus = true, bool fullJIDMapping = false); - ~Roster(); + public: + Roster(bool sortByStatus = true, bool fullJIDMapping = false); + ~Roster(); + + void addContact(const JID& jid, const JID& displayJID, const std::string& name, const std::string& group, const boost::filesystem::path& avatarPath); + void removeContact(const JID& jid); + void removeContactFromGroup(const JID& jid, const std::string& group); + void removeGroup(const std::string& group); + void removeAll(); + void applyOnItems(const RosterItemOperation& operation); + void applyOnAllItems(const RosterItemOperation& operation); + void applyOnItem(const RosterItemOperation& operation, const JID& jid); + void addFilter(RosterFilter* filter); + void removeFilter(RosterFilter* filter); + GroupRosterItem* getRoot() const; + std::set<JID> getJIDs() const; + + std::vector<RosterFilter*> getFilters() {return filters_;} + boost::signals2::signal<void (GroupRosterItem*)> onChildrenChanged; + boost::signals2::signal<void (GroupRosterItem*)> onGroupAdded; + boost::signals2::signal<void (RosterItem*)> onDataChanged; + boost::signals2::signal<void (JID&)> onVCardUpdateRequested; + boost::signals2::signal<void (RosterFilter* filter)> onFilterAdded; + boost::signals2::signal<void (RosterFilter* filter)> onFilterRemoved; + GroupRosterItem* getGroup(const std::string& groupName); + void setBlockingSupported(bool isSupported); - void addContact(const JID& jid, const JID& displayJID, const std::string& name, const std::string& group, const boost::filesystem::path& avatarPath); - void removeContact(const JID& jid); - void removeContactFromGroup(const JID& jid, const std::string& group); - void removeGroup(const std::string& group); - void removeAll(); - void applyOnItems(const RosterItemOperation& operation); - void applyOnAllItems(const RosterItemOperation& operation); - void applyOnItem(const RosterItemOperation& operation, const JID& jid); - void addFilter(RosterFilter *filter) {filters_.push_back(filter);filterAll();} - void removeFilter(RosterFilter *filter); - GroupRosterItem* getRoot(); - std::vector<RosterFilter*> getFilters() {return filters_;} - boost::signal<void (GroupRosterItem*)> onChildrenChanged; - boost::signal<void (GroupRosterItem*)> onGroupAdded; - boost::signal<void (RosterItem*)> onDataChanged; - GroupRosterItem* getGroup(const std::string& groupName); - void setAvailableFeatures(const JID& jid, const std::set<ContactRosterItem::Feature>& features); - void setBlockedState(const std::vector<JID>& jids, ContactRosterItem::BlockState state); + private: + void handleDataChanged(RosterItem* item); + void handleChildrenChanged(GroupRosterItem* item); + void filterGroup(GroupRosterItem* item); + void filterContact(ContactRosterItem* contact, GroupRosterItem* group); + void filterAll(); - private: - void handleDataChanged(RosterItem* item); - void handleChildrenChanged(GroupRosterItem* item); - void filterGroup(GroupRosterItem* item); - void filterContact(ContactRosterItem* contact, GroupRosterItem* group); - void filterAll(); - GroupRosterItem* root_; - std::vector<RosterFilter*> filters_; - typedef std::map<JID, std::vector<ContactRosterItem*> > ItemMap; - ItemMap itemMap_; - bool fullJIDMapping_; - bool sortByStatus_; - bool blockingSupported_; + private: + std::vector<RosterFilter*> filters_; + typedef std::map<JID, std::vector<ContactRosterItem*> > ItemMap; + ItemMap itemMap_; + bool fullJIDMapping_; + bool sortByStatus_; + bool blockingSupported_ = false; + const std::unique_ptr<GroupRosterItem> root_; }; } diff --git a/Swift/Controllers/Roster/RosterController.cpp b/Swift/Controllers/Roster/RosterController.cpp index d277799..90c5ce1 100644 --- a/Swift/Controllers/Roster/RosterController.cpp +++ b/Swift/Controllers/Roster/RosterController.cpp @@ -1,359 +1,419 @@ /* - * Copyright (c) 2010-2013 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ -#include "Swift/Controllers/Roster/RosterController.h" +#include <Swift/Controllers/Roster/RosterController.h> + +#include <memory> #include <boost/bind.hpp> -#include <boost/smart_ptr/make_shared.hpp> - -#include "Swiften/JID/JID.h" -#include "Swiften/Base/foreach.h" -#include "Swift/Controllers/UIInterfaces/MainWindow.h" -#include "Swift/Controllers/UIInterfaces/MainWindowFactory.h" -#include "Swiften/Client/NickResolver.h" -#include "Swiften/Roster/GetRosterRequest.h" -#include "Swiften/Roster/SetRosterRequest.h" -#include "Swift/Controllers/XMPPEvents/SubscriptionRequestEvent.h" -#include "Swift/Controllers/XMPPEvents/ErrorEvent.h" -#include "Swiften/Presence/PresenceOracle.h" -#include "Swiften/Presence/SubscriptionManager.h" -#include "Swift/Controllers/XMPPEvents/EventController.h" -#include "Swiften/Queries/IQRouter.h" -#include "Swift/Controllers/Roster/Roster.h" -#include "Swift/Controllers/Roster/SetPresence.h" -#include "Swift/Controllers/Roster/AppearOffline.h" -#include "Swift/Controllers/Roster/SetAvatar.h" -#include "Swift/Controllers/Roster/SetName.h" -#include "Swift/Controllers/Roster/OfflineRosterFilter.h" -#include "Swift/Controllers/Roster/GroupRosterItem.h" -#include "Swiften/Roster/XMPPRoster.h" -#include "Swiften/Roster/XMPPRosterItem.h" -#include "Swift/Controllers/UIEvents/AddContactUIEvent.h" -#include "Swift/Controllers/UIEvents/RemoveRosterItemUIEvent.h" -#include "Swift/Controllers/UIEvents/RenameRosterItemUIEvent.h" -#include "Swift/Controllers/UIEvents/RenameGroupUIEvent.h" -#include "Swift/Controllers/UIEvents/SendFileUIEvent.h" -#include <Swiften/FileTransfer/FileTransferManager.h> -#include <Swiften/Client/NickManager.h> -#include <Swift/Controllers/Intl.h> -#include <Swiften/Base/format.h> + +#include <Swiften/Avatars/AvatarManager.h> #include <Swiften/Base/Path.h> -#include <Swiften/Elements/DiscoInfo.h> +#include <Swiften/Base/format.h> +#include <Swiften/Client/ClientBlockListManager.h> +#include <Swiften/Client/NickManager.h> +#include <Swiften/Client/NickResolver.h> #include <Swiften/Disco/EntityCapsManager.h> +#include <Swiften/Disco/FeatureOracle.h> +#include <Swiften/Elements/DiscoInfo.h> +#include <Swiften/FileTransfer/FileTransferManager.h> +#include <Swiften/JID/JID.h> #include <Swiften/Jingle/JingleSessionManager.h> +#include <Swiften/Presence/PresenceOracle.h> +#include <Swiften/Presence/SubscriptionManager.h> +#include <Swiften/Queries/IQRouter.h> +#include <Swiften/Roster/GetRosterRequest.h> +#include <Swiften/Roster/SetRosterRequest.h> +#include <Swiften/Roster/XMPPRoster.h> +#include <Swiften/Roster/XMPPRosterItem.h> +#include <Swiften/VCards/VCardManager.h> + +#include <Swift/Controllers/Chat/Chattables.h> +#include <Swift/Controllers/Intl.h> +#include <Swift/Controllers/Roster/GroupRosterItem.h> +#include <Swift/Controllers/Roster/ItemOperations/AppearOffline.h> +#include <Swift/Controllers/Roster/ItemOperations/SetAvailableFeatures.h> +#include <Swift/Controllers/Roster/ItemOperations/SetAvatar.h> +#include <Swift/Controllers/Roster/ItemOperations/SetBlockingState.h> +#include <Swift/Controllers/Roster/ItemOperations/SetName.h> +#include <Swift/Controllers/Roster/ItemOperations/SetPresence.h> +#include <Swift/Controllers/Roster/ItemOperations/SetVCard.h> +#include <Swift/Controllers/Roster/OfflineRosterFilter.h> +#include <Swift/Controllers/Roster/Roster.h> +#include <Swift/Controllers/Roster/RosterGroupExpandinessPersister.h> +#include <Swift/Controllers/Roster/RosterVCardProvider.h> #include <Swift/Controllers/SettingConstants.h> -#include <Swiften/Client/ClientBlockListManager.h> +#include <Swift/Controllers/UIEvents/AddContactUIEvent.h> +#include <Swift/Controllers/UIEvents/RemoveRosterItemUIEvent.h> +#include <Swift/Controllers/UIEvents/RenameGroupUIEvent.h> +#include <Swift/Controllers/UIEvents/RenameRosterItemUIEvent.h> +#include <Swift/Controllers/UIEvents/UIEventStream.h> +#include <Swift/Controllers/UIInterfaces/MainWindow.h> +#include <Swift/Controllers/UIInterfaces/MainWindowFactory.h> +#include <Swift/Controllers/XMPPEvents/ErrorEvent.h> +#include <Swift/Controllers/XMPPEvents/EventController.h> +#include <Swift/Controllers/XMPPEvents/SubscriptionRequestEvent.h> namespace Swift { /** * The controller does not gain ownership of these parameters. */ -RosterController::RosterController(const JID& jid, XMPPRoster* xmppRoster, AvatarManager* avatarManager, MainWindowFactory* mainWindowFactory, NickManager* nickManager, NickResolver* nickResolver, PresenceOracle* presenceOracle, SubscriptionManager* subscriptionManager, EventController* eventController, UIEventStream* uiEventStream, IQRouter* iqRouter, SettingsProvider* settings, EntityCapsProvider* entityCapsManager, FileTransferOverview* fileTransferOverview, ClientBlockListManager* clientBlockListManager) - : myJID_(jid), xmppRoster_(xmppRoster), mainWindowFactory_(mainWindowFactory), mainWindow_(mainWindowFactory_->createMainWindow(uiEventStream)), roster_(new Roster()), offlineFilter_(new OfflineRosterFilter()), nickManager_(nickManager), nickResolver_(nickResolver), uiEventStream_(uiEventStream), entityCapsManager_(entityCapsManager), ftOverview_(fileTransferOverview), clientBlockListManager_(clientBlockListManager) { - assert(fileTransferOverview); - iqRouter_ = iqRouter; - presenceOracle_ = presenceOracle; - subscriptionManager_ = subscriptionManager; - eventController_ = eventController; - settings_ = settings; - expandiness_ = new RosterGroupExpandinessPersister(roster_, settings); - mainWindow_->setRosterModel(roster_); - - changeStatusConnection_ = mainWindow_->onChangeStatusRequest.connect(boost::bind(&RosterController::handleChangeStatusRequest, this, _1, _2)); - signOutConnection_ = mainWindow_->onSignOutRequest.connect(boost::bind(boost::ref(onSignOutRequest))); - xmppRoster_->onJIDAdded.connect(boost::bind(&RosterController::handleOnJIDAdded, this, _1)); - xmppRoster_->onJIDUpdated.connect(boost::bind(&RosterController::handleOnJIDUpdated, this, _1, _2, _3)); - xmppRoster_->onJIDRemoved.connect(boost::bind(&RosterController::handleOnJIDRemoved, this, _1)); - xmppRoster_->onRosterCleared.connect(boost::bind(&RosterController::handleRosterCleared, this)); - subscriptionManager_->onPresenceSubscriptionRequest.connect(boost::bind(&RosterController::handleSubscriptionRequest, this, _1, _2)); - presenceOracle_->onPresenceChange.connect(boost::bind(&RosterController::handleIncomingPresence, this, _1)); - uiEventConnection_ = uiEventStream->onUIEvent.connect(boost::bind(&RosterController::handleUIEvent, this, _1)); - avatarManager_ = avatarManager; - avatarManager_->onAvatarChanged.connect(boost::bind(&RosterController::handleAvatarChanged, this, _1)); - mainWindow_->setMyAvatarPath(pathToString(avatarManager_->getAvatarPath(myJID_))); - - nickManager_->onOwnNickChanged.connect(boost::bind(&MainWindow::setMyNick, mainWindow_, _1)); - mainWindow_->setMyJID(jid); - mainWindow_->setMyNick(nickManager_->getOwnNick()); - - entityCapsManager_->onCapsChanged.connect(boost::bind(&RosterController::handleOnCapsChanged, this, _1)); - - settings_->onSettingChanged.connect(boost::bind(&RosterController::handleSettingChanged, this, _1)); - - handleShowOfflineToggled(settings_->getSetting(SettingConstants::SHOW_OFFLINE)); +RosterController::RosterController(const JID& jid, XMPPRoster* xmppRoster, AvatarManager* avatarManager, MainWindowFactory* mainWindowFactory, NickManager* nickManager, NickResolver* nickResolver, PresenceOracle* presenceOracle, SubscriptionManager* subscriptionManager, EventController* eventController, UIEventStream* uiEventStream, IQRouter* iqRouter, SettingsProvider* settings, EntityCapsProvider* entityCapsManager, ClientBlockListManager* clientBlockListManager, VCardManager* vcardManager, Chattables& chattables) + : myJID_(jid), xmppRoster_(xmppRoster), mainWindowFactory_(mainWindowFactory), mainWindow_(mainWindowFactory_->createMainWindow(chattables, uiEventStream)), roster_(new Roster()), offlineFilter_(new OfflineRosterFilter()), vcardManager_(vcardManager), avatarManager_(avatarManager), nickManager_(nickManager), nickResolver_(nickResolver), presenceOracle_(presenceOracle), uiEventStream_(uiEventStream), entityCapsManager_(entityCapsManager), clientBlockListManager_(clientBlockListManager), chattables_(chattables) { + iqRouter_ = iqRouter; + subscriptionManager_ = subscriptionManager; + eventController_ = eventController; + settings_ = settings; + expandiness_ = new RosterGroupExpandinessPersister(roster_, settings); +#ifndef NOT_YET + mainWindow_->setRosterModel(roster_); +#endif + rosterVCardProvider_ = new RosterVCardProvider(roster_, vcardManager, JID::WithoutResource); + + changeStatusConnection_ = mainWindow_->onChangeStatusRequest.connect(boost::bind(&RosterController::handleChangeStatusRequest, this, _1, _2)); + signOutConnection_ = mainWindow_->onSignOutRequest.connect(boost::bind(boost::ref(onSignOutRequest))); + xmppRoster_->onJIDAdded.connect(boost::bind(&RosterController::handleOnJIDAdded, this, _1)); + xmppRoster_->onJIDUpdated.connect(boost::bind(&RosterController::handleOnJIDUpdated, this, _1, _2, _3)); + xmppRoster_->onJIDRemoved.connect(boost::bind(&RosterController::handleOnJIDRemoved, this, _1)); + xmppRoster_->onRosterCleared.connect(boost::bind(&RosterController::handleRosterCleared, this)); + subscriptionManager_->onPresenceSubscriptionRequest.connect(boost::bind(&RosterController::handleSubscriptionRequest, this, _1, _2)); + uiEventConnection_ = uiEventStream->onUIEvent.connect(boost::bind(&RosterController::handleUIEvent, this, _1)); + + featureOracle_ = std::make_unique<FeatureOracle>(entityCapsManager_, presenceOracle_); + + vcardManager_->onOwnVCardChanged.connect(boost::bind(&RosterController::handleOwnVCardChanged, this, _1)); + avatarManager_->onAvatarChanged.connect(boost::bind(&RosterController::handleAvatarChanged, this, _1)); + presenceOracle_->onPresenceChange.connect(boost::bind(&RosterController::handlePresenceChanged, this, _1)); + mainWindow_->setMyAvatarPath(pathToString(avatarManager_->getAvatarPath(myJID_.toBare()))); + + nickManager_->onOwnNickChanged.connect(boost::bind(&MainWindow::setMyNick, mainWindow_, _1)); + mainWindow_->setMyJID(jid); + mainWindow_->setMyNick(nickManager_->getOwnNick()); + + entityCapsManager_->onCapsChanged.connect(boost::bind(&RosterController::handleOnCapsChanged, this, _1)); + + settings_->onSettingChanged.connect(boost::bind(&RosterController::handleSettingChanged, this, _1)); + + handleShowOfflineToggled(settings_->getSetting(SettingConstants::SHOW_OFFLINE)); + + ownContact_ = std::make_shared<ContactRosterItem>(myJID_.toBare(), myJID_.toBare(), nickManager_->getOwnNick(), static_cast<GroupRosterItem*>(nullptr)); + ownContact_->setVCard(vcardManager_->getVCard(myJID_.toBare())); + ownContact_->setAvatarPath(pathToString(avatarManager_->getAvatarPath(myJID_.toBare()))); + mainWindow_->setMyContactRosterItem(ownContact_); } -RosterController::~RosterController() { - settings_->onSettingChanged.disconnect(boost::bind(&RosterController::handleSettingChanged, this, _1)); - nickManager_->onOwnNickChanged.disconnect(boost::bind(&MainWindow::setMyNick, mainWindow_, _1)); - - delete offlineFilter_; - delete expandiness_; +RosterController::~RosterController() { + settings_->onSettingChanged.disconnect(boost::bind(&RosterController::handleSettingChanged, this, _1)); + nickManager_->onOwnNickChanged.disconnect(boost::bind(&MainWindow::setMyNick, mainWindow_, _1)); - mainWindow_->setRosterModel(NULL); - if (mainWindow_->canDelete()) { - delete mainWindow_; - } - delete roster_; - + delete offlineFilter_; + delete expandiness_; + + mainWindow_->setRosterModel(nullptr); + if (mainWindow_->canDelete()) { + delete mainWindow_; + } + delete rosterVCardProvider_; + delete roster_; } void RosterController::setEnabled(bool enabled) { - if (!enabled) { - roster_->applyOnItems(AppearOffline()); - } + if (!enabled) { + roster_->applyOnItems(AppearOffline()); + } } void RosterController::handleShowOfflineToggled(bool state) { - if (state) { - roster_->removeFilter(offlineFilter_); - } else { - roster_->addFilter(offlineFilter_); - } + if (state) { + roster_->removeFilter(offlineFilter_); + } else { + roster_->addFilter(offlineFilter_); + } } void RosterController::handleChangeStatusRequest(StatusShow::Type show, const std::string &statusText) { - onChangeStatusRequest(show, statusText); + onChangeStatusRequest(show, statusText); } void RosterController::handleOnJIDAdded(const JID& jid) { - std::vector<std::string> groups = xmppRoster_->getGroupsForJID(jid); - std::string name = nickResolver_->jidToNick(jid); - if (!groups.empty()) { - foreach(const std::string& group, groups) { - roster_->addContact(jid, jid, name, group, avatarManager_->getAvatarPath(jid)); - } - } - else { - roster_->addContact(jid, jid, name, QT_TRANSLATE_NOOP("", "Contacts"), avatarManager_->getAvatarPath(jid)); - } - applyAllPresenceTo(jid); + std::vector<std::string> groups = xmppRoster_->getGroupsForJID(jid); + std::string name = nickResolver_->jidToNick(jid); + if (!groups.empty()) { + for (const auto& group : groups) { + roster_->addContact(jid, jid, name, group, avatarManager_->getAvatarPath(jid)); + } + } + else { + roster_->addContact(jid, jid, name, QT_TRANSLATE_NOOP("", "Contacts"), avatarManager_->getAvatarPath(jid)); + } + applyAllPresenceTo(jid); + + chattables_.addJID(jid, Chattables::State::Type::Person); + auto state = chattables_.getState(jid); + state.name = name; + chattables_.setState(jid, state); } void RosterController::applyAllPresenceTo(const JID& jid) { - foreach (Presence::ref presence, presenceOracle_->getAllPresence(jid)) { - roster_->applyOnItems(SetPresence(presence)); - } + for (auto&& presence : presenceOracle_->getAllPresence(jid)) { + roster_->applyOnItems(SetPresence(presence)); + } } void RosterController::handleRosterCleared() { - roster_->removeAll(); + roster_->removeAll(); } void RosterController::handleOnJIDRemoved(const JID& jid) { - roster_->removeContact(jid); + roster_->removeContact(jid); } void RosterController::handleOnJIDUpdated(const JID& jid, const std::string& oldName, const std::vector<std::string>& passedOldGroups) { - if (oldName != xmppRoster_->getNameForJID(jid)) { - roster_->applyOnItems(SetName(nickResolver_->jidToNick(jid), jid)); - } - std::vector<std::string> groups = xmppRoster_->getGroupsForJID(jid); - std::vector<std::string> oldGroups = passedOldGroups; - std::string name = nickResolver_->jidToNick(jid); - std::string contactsGroup = QT_TRANSLATE_NOOP("", "Contacts"); - if (oldGroups.empty()) { - oldGroups.push_back(contactsGroup); - } - if (groups.empty()) { - groups.push_back(contactsGroup); - } - foreach(const std::string& group, groups) { - if (std::find(oldGroups.begin(), oldGroups.end(), group) == oldGroups.end()) { - roster_->addContact(jid, jid, name, group, avatarManager_->getAvatarPath(jid)); - } - } - foreach(const std::string& group, oldGroups) { - if (std::find(groups.begin(), groups.end(), group) == groups.end()) { - roster_->removeContactFromGroup(jid, group); - if (roster_->getGroup(group)->getChildren().size() == 0) { - roster_->removeGroup(group); - } - } - } - applyAllPresenceTo(jid); + if (oldName != xmppRoster_->getNameForJID(jid)) { + roster_->applyOnItems(SetName(nickResolver_->jidToNick(jid), jid)); + } + std::vector<std::string> groups = xmppRoster_->getGroupsForJID(jid); + std::vector<std::string> oldGroups = passedOldGroups; + std::string name = nickResolver_->jidToNick(jid); + std::string contactsGroup = QT_TRANSLATE_NOOP("", "Contacts"); + if (oldGroups.empty()) { + oldGroups.push_back(contactsGroup); + } + if (groups.empty()) { + groups.push_back(contactsGroup); + } + for (const auto& group : groups) { + if (std::find(oldGroups.begin(), oldGroups.end(), group) == oldGroups.end()) { + roster_->addContact(jid, jid, name, group, avatarManager_->getAvatarPath(jid)); + } + } + for (const auto& group : oldGroups) { + if (std::find(groups.begin(), groups.end(), group) == groups.end()) { + roster_->removeContactFromGroup(jid, group); + if (roster_->getGroup(group)->getChildren().size() == 0) { + roster_->removeGroup(group); + } + } + } + applyAllPresenceTo(jid); } void RosterController::handleSettingChanged(const std::string& settingPath) { - if (settingPath == SettingConstants::SHOW_OFFLINE.getKey()) { - handleShowOfflineToggled(settings_->getSetting(SettingConstants::SHOW_OFFLINE)); - } + if (settingPath == SettingConstants::SHOW_OFFLINE.getKey()) { + handleShowOfflineToggled(settings_->getSetting(SettingConstants::SHOW_OFFLINE)); + } } void RosterController::handleBlockingStateChanged() { - if (clientBlockListManager_->getBlockList()->getState() == BlockList::Available) { - roster_->setBlockedState(clientBlockListManager_->getBlockList()->getItems(), ContactRosterItem::IsBlocked); - } + if (clientBlockListManager_->getBlockList()->getState() == BlockList::Available) { + for (const auto& jid : clientBlockListManager_->getBlockList()->getItems()) { + roster_->applyOnItems(SetBlockingState(jid, ContactRosterItem::IsBlocked)); + } + } } void RosterController::handleBlockingItemAdded(const JID& jid) { - roster_->setBlockedState(std::vector<JID>(1, jid), ContactRosterItem::IsBlocked); + roster_->applyOnItems(SetBlockingState(jid, ContactRosterItem::IsBlocked)); } void RosterController::handleBlockingItemRemoved(const JID& jid) { - roster_->setBlockedState(std::vector<JID>(1, jid), ContactRosterItem::IsUnblocked); + roster_->applyOnItems(SetBlockingState(jid, ContactRosterItem::IsUnblocked)); } -void RosterController::handleUIEvent(boost::shared_ptr<UIEvent> event) { - if (boost::shared_ptr<AddContactUIEvent> addContactEvent = boost::dynamic_pointer_cast<AddContactUIEvent>(event)) { - RosterItemPayload item; - item.setName(addContactEvent->getName()); - item.setJID(addContactEvent->getJID()); - item.setGroups(std::vector<std::string>(addContactEvent->getGroups().begin(), addContactEvent->getGroups().end())); - boost::shared_ptr<RosterPayload> roster(new RosterPayload()); - roster->addItem(item); - SetRosterRequest::ref request = SetRosterRequest::create(roster, iqRouter_); - request->onResponse.connect(boost::bind(&RosterController::handleRosterSetError, this, _1, roster)); - request->send(); - subscriptionManager_->requestSubscription(addContactEvent->getJID()); - } - else if (boost::shared_ptr<RemoveRosterItemUIEvent> removeEvent = boost::dynamic_pointer_cast<RemoveRosterItemUIEvent>(event)) { - RosterItemPayload item(removeEvent->getJID(), "", RosterItemPayload::Remove); - boost::shared_ptr<RosterPayload> roster(new RosterPayload()); - roster->addItem(item); - SetRosterRequest::ref request = SetRosterRequest::create(roster, iqRouter_); - request->onResponse.connect(boost::bind(&RosterController::handleRosterSetError, this, _1, roster)); - request->send(); - - } - else if (boost::shared_ptr<RenameRosterItemUIEvent> renameEvent = boost::dynamic_pointer_cast<RenameRosterItemUIEvent>(event)) { - JID contact(renameEvent->getJID()); - RosterItemPayload item(contact, renameEvent->getNewName(), xmppRoster_->getSubscriptionStateForJID(contact)); - item.setGroups(xmppRoster_->getGroupsForJID(contact)); - boost::shared_ptr<RosterPayload> roster(new RosterPayload()); - roster->addItem(item); - SetRosterRequest::ref request = SetRosterRequest::create(roster, iqRouter_); - request->onResponse.connect(boost::bind(&RosterController::handleRosterSetError, this, _1, roster)); - request->send(); - } - else if (boost::shared_ptr<RenameGroupUIEvent> renameGroupEvent = boost::dynamic_pointer_cast<RenameGroupUIEvent>(event)) { - std::vector<XMPPRosterItem> items = xmppRoster_->getItems(); - std::string group = renameGroupEvent->getGroup(); - // FIXME: We should handle contacts groups specially to avoid clashes - if (group == QT_TRANSLATE_NOOP("", "Contacts")) { - group = ""; - } - foreach(XMPPRosterItem& item, items) { - std::vector<std::string> groups = item.getGroups(); - if ( (group.empty() && groups.empty()) || std::find(groups.begin(), groups.end(), group) != groups.end()) { - groups.erase(std::remove(groups.begin(), groups.end(), group), groups.end()); - if (std::find(groups.begin(), groups.end(), renameGroupEvent->getNewName()) == groups.end()) { - groups.push_back(renameGroupEvent->getNewName()); - } - item.setGroups(groups); - updateItem(item); - } - } - } - else if (boost::shared_ptr<SendFileUIEvent> sendFileEvent = boost::dynamic_pointer_cast<SendFileUIEvent>(event)) { - //TODO add send file dialog to ChatView of receipient jid - ftOverview_->sendFile(sendFileEvent->getJID(), sendFileEvent->getFilename()); - } +void RosterController::handleUIEvent(std::shared_ptr<UIEvent> event) { + if (std::shared_ptr<AddContactUIEvent> addContactEvent = std::dynamic_pointer_cast<AddContactUIEvent>(event)) { + RosterItemPayload item; + item.setName(addContactEvent->getName()); + item.setJID(addContactEvent->getJID()); + item.setGroups(std::vector<std::string>(addContactEvent->getGroups().begin(), addContactEvent->getGroups().end())); + std::shared_ptr<RosterPayload> roster(new RosterPayload()); + roster->addItem(item); + SetRosterRequest::ref request = SetRosterRequest::create(roster, iqRouter_); + request->onResponse.connect(boost::bind(&RosterController::handleRosterSetError, this, _1, roster)); + request->send(); + subscriptionManager_->requestSubscription(addContactEvent->getJID()); + } + else if (std::shared_ptr<RemoveRosterItemUIEvent> removeEvent = std::dynamic_pointer_cast<RemoveRosterItemUIEvent>(event)) { + RosterItemPayload item(removeEvent->getJID(), "", RosterItemPayload::Remove); + std::shared_ptr<RosterPayload> roster(new RosterPayload()); + roster->addItem(item); + SetRosterRequest::ref request = SetRosterRequest::create(roster, iqRouter_); + request->onResponse.connect(boost::bind(&RosterController::handleRosterSetError, this, _1, roster)); + request->send(); + + } + else if (std::shared_ptr<RenameRosterItemUIEvent> renameEvent = std::dynamic_pointer_cast<RenameRosterItemUIEvent>(event)) { + JID contact(renameEvent->getJID()); + RosterItemPayload item(contact, renameEvent->getNewName(), xmppRoster_->getSubscriptionStateForJID(contact)); + item.setGroups(xmppRoster_->getGroupsForJID(contact)); + std::shared_ptr<RosterPayload> roster(new RosterPayload()); + roster->addItem(item); + SetRosterRequest::ref request = SetRosterRequest::create(roster, iqRouter_); + request->onResponse.connect(boost::bind(&RosterController::handleRosterSetError, this, _1, roster)); + request->send(); + } + else if (std::shared_ptr<RenameGroupUIEvent> renameGroupEvent = std::dynamic_pointer_cast<RenameGroupUIEvent>(event)) { + std::vector<XMPPRosterItem> items = xmppRoster_->getItems(); + std::string group = renameGroupEvent->getGroup(); + // FIXME: We should handle contacts groups specially to avoid clashes + if (group == QT_TRANSLATE_NOOP("", "Contacts")) { + group = ""; + } + for (auto& item : items) { + std::vector<std::string> groups = item.getGroups(); + if ( (group.empty() && groups.empty()) || std::find(groups.begin(), groups.end(), group) != groups.end()) { + groups.erase(std::remove(groups.begin(), groups.end(), group), groups.end()); + if (std::find(groups.begin(), groups.end(), renameGroupEvent->getNewName()) == groups.end()) { + groups.push_back(renameGroupEvent->getNewName()); + } + item.setGroups(groups); + updateItem(item); + } + } + } } void RosterController::setContactGroups(const JID& jid, const std::vector<std::string>& groups) { - updateItem(XMPPRosterItem(jid, xmppRoster_->getNameForJID(jid), groups, xmppRoster_->getSubscriptionStateForJID(jid))); + updateItem(XMPPRosterItem(jid, xmppRoster_->getNameForJID(jid), groups, xmppRoster_->getSubscriptionStateForJID(jid))); } void RosterController::updateItem(const XMPPRosterItem& item) { - RosterItemPayload itemPayload(item.getJID(), item.getName(), item.getSubscription()); - itemPayload.setGroups(item.getGroups()); + RosterItemPayload itemPayload(item.getJID(), item.getName(), item.getSubscription()); + itemPayload.setGroups(item.getGroups()); - RosterPayload::ref roster = boost::make_shared<RosterPayload>(); - roster->addItem(itemPayload); + RosterPayload::ref roster = std::make_shared<RosterPayload>(); + roster->addItem(itemPayload); - SetRosterRequest::ref request = SetRosterRequest::create(roster, iqRouter_); - request->onResponse.connect(boost::bind(&RosterController::handleRosterSetError, this, _1, roster)); - request->send(); + SetRosterRequest::ref request = SetRosterRequest::create(roster, iqRouter_); + request->onResponse.connect(boost::bind(&RosterController::handleRosterItemUpdated, this, _1, roster)); + request->send(); } void RosterController::initBlockingCommand() { - boost::shared_ptr<BlockList> blockList = clientBlockListManager_->getBlockList(); - - blockingOnStateChangedConnection_ = blockList->onStateChanged.connect(boost::bind(&RosterController::handleBlockingStateChanged, this)); - blockingOnItemAddedConnection_ = blockList->onItemAdded.connect(boost::bind(&RosterController::handleBlockingItemAdded, this, _1)); - blockingOnItemRemovedConnection_ = blockList->onItemRemoved.connect(boost::bind(&RosterController::handleBlockingItemRemoved, this, _1)); + std::shared_ptr<BlockList> blockList = clientBlockListManager_->requestBlockList(); + + blockingOnStateChangedConnection_ = blockList->onStateChanged.connect(boost::bind(&RosterController::handleBlockingStateChanged, this)); + blockingOnItemAddedConnection_ = blockList->onItemAdded.connect(boost::bind(&RosterController::handleBlockingItemAdded, this, _1)); + blockingOnItemRemovedConnection_ = blockList->onItemRemoved.connect(boost::bind(&RosterController::handleBlockingItemRemoved, this, _1)); + roster_->setBlockingSupported(true); + if (blockList->getState() == BlockList::Available) { + for (const auto& jid : blockList->getItems()) { + roster_->applyOnItems(SetBlockingState(jid, ContactRosterItem::IsBlocked)); + } + } +} - if (blockList->getState() == BlockList::Available) { - roster_->setBlockedState(blockList->getItems(), ContactRosterItem::IsBlocked); - } +void RosterController::handleRosterItemUpdated(ErrorPayload::ref error, std::shared_ptr<RosterPayload> rosterPayload) { + if (!!error) { + handleRosterSetError(error, rosterPayload); + } + std::shared_ptr<BlockList> blockList = clientBlockListManager_->getBlockList(); + std::vector<RosterItemPayload> items = rosterPayload->getItems(); + if (blockList->getState() == BlockList::Available && items.size() > 0) { + std::vector<JID> jids = blockList->getItems(); + if (std::find(jids.begin(), jids.end(), items[0].getJID()) != jids.end()) { + roster_->applyOnItems(SetBlockingState(items[0].getJID(), ContactRosterItem::IsBlocked)); + } + } } -void RosterController::handleRosterSetError(ErrorPayload::ref error, boost::shared_ptr<RosterPayload> rosterPayload) { - if (!error) { - return; - } - std::string text = str(format(QT_TRANSLATE_NOOP("", "Server %1% rejected contact list change to item '%2%'")) % myJID_.getDomain() % rosterPayload->getItems()[0].getJID().toString()); - if (!error->getText().empty()) { - text += ": " + error->getText(); - } - boost::shared_ptr<ErrorEvent> errorEvent(new ErrorEvent(JID(myJID_.getDomain()), text)); - eventController_->handleIncomingEvent(errorEvent); +void RosterController::handleRosterSetError(ErrorPayload::ref error, std::shared_ptr<RosterPayload> rosterPayload) { + if (!error) { + return; + } + std::string text = str(format(QT_TRANSLATE_NOOP("", "Server %1% rejected contact list change to item '%2%'")) % myJID_.getDomain() % rosterPayload->getItems()[0].getJID().toString()); + if (!error->getText().empty()) { + text += ": " + error->getText(); + } + std::shared_ptr<ErrorEvent> errorEvent(new ErrorEvent(JID(myJID_.getDomain()), text)); + eventController_->handleIncomingEvent(errorEvent); } void RosterController::handleIncomingPresence(Presence::ref newPresence) { - if (newPresence->getType() == Presence::Error) { - return; - } - roster_->applyOnItems(SetPresence(newPresence)); + if (newPresence->getType() == Presence::Error) { + return; + } + auto bareFrom = newPresence->getFrom().toBare(); + Presence::ref accountPresence = presenceOracle_->getAccountPresence(bareFrom); + if (!accountPresence) { + accountPresence = Presence::create(); + accountPresence->setFrom(newPresence->getFrom()); + accountPresence->setType(Presence::Unavailable); + } + roster_->applyOnItems(SetPresence(accountPresence)); + auto state = chattables_.getState(bareFrom); + state.status = accountPresence->getShow(); + chattables_.setState(bareFrom, state); } void RosterController::handleSubscriptionRequest(const JID& jid, const std::string& message) { - if (xmppRoster_->containsJID(jid) && (xmppRoster_->getSubscriptionStateForJID(jid) == RosterItemPayload::To || xmppRoster_->getSubscriptionStateForJID(jid) == RosterItemPayload::Both)) { - subscriptionManager_->confirmSubscription(jid); - return; - } - SubscriptionRequestEvent* eventPointer = new SubscriptionRequestEvent(jid, message); - eventPointer->onAccept.connect(boost::bind(&RosterController::handleSubscriptionRequestAccepted, this, eventPointer)); - eventPointer->onDecline.connect(boost::bind(&RosterController::handleSubscriptionRequestDeclined, this, eventPointer)); - boost::shared_ptr<StanzaEvent> event(eventPointer); - eventController_->handleIncomingEvent(event); + if (xmppRoster_->containsJID(jid) && (xmppRoster_->getSubscriptionStateForJID(jid) == RosterItemPayload::To || xmppRoster_->getSubscriptionStateForJID(jid) == RosterItemPayload::Both)) { + subscriptionManager_->confirmSubscription(jid); + return; + } + SubscriptionRequestEvent* eventPointer = new SubscriptionRequestEvent(jid, message); + eventPointer->onAccept.connect(boost::bind(&RosterController::handleSubscriptionRequestAccepted, this, eventPointer)); + eventPointer->onDecline.connect(boost::bind(&RosterController::handleSubscriptionRequestDeclined, this, eventPointer)); + std::shared_ptr<StanzaEvent> event(eventPointer); + eventController_->handleIncomingEvent(event); } void RosterController::handleSubscriptionRequestAccepted(SubscriptionRequestEvent* event) { - subscriptionManager_->confirmSubscription(event->getJID()); - if (!xmppRoster_->containsJID(event->getJID()) || xmppRoster_->getSubscriptionStateForJID(event->getJID()) == RosterItemPayload::None || xmppRoster_->getSubscriptionStateForJID(event->getJID()) == RosterItemPayload::From) { - subscriptionManager_->requestSubscription(event->getJID()); - } + subscriptionManager_->confirmSubscription(event->getJID()); + if (!xmppRoster_->containsJID(event->getJID()) || xmppRoster_->getSubscriptionStateForJID(event->getJID()) == RosterItemPayload::None || xmppRoster_->getSubscriptionStateForJID(event->getJID()) == RosterItemPayload::From) { + subscriptionManager_->requestSubscription(event->getJID()); + } } void RosterController::handleSubscriptionRequestDeclined(SubscriptionRequestEvent* event) { - subscriptionManager_->cancelSubscription(event->getJID()); + subscriptionManager_->cancelSubscription(event->getJID()); +} + +void RosterController::handleOwnVCardChanged(VCard::ref vcard) { + ownContact_->setVCard(vcard); + mainWindow_->setMyContactRosterItem(ownContact_); } void RosterController::handleAvatarChanged(const JID& jid) { - boost::filesystem::path path = avatarManager_->getAvatarPath(jid); - roster_->applyOnItems(SetAvatar(jid, path)); - if (jid.equals(myJID_, JID::WithoutResource)) { - mainWindow_->setMyAvatarPath(pathToString(path)); - } + boost::filesystem::path path = avatarManager_->getAvatarPath(jid); + roster_->applyOnItems(SetAvatar(jid, path)); + if (jid.equals(myJID_, JID::WithoutResource)) { + mainWindow_->setMyAvatarPath(pathToString(path)); + ownContact_->setAvatarPath(pathToString(path)); + mainWindow_->setMyContactRosterItem(ownContact_); + } +} + +void RosterController::handlePresenceChanged(Presence::ref presence) { + if (presence->getFrom().equals(myJID_, JID::WithResource)) { + ownContact_->applyPresence(presence); + mainWindow_->setMyContactRosterItem(ownContact_); + } + handleIncomingPresence(presence); } boost::optional<XMPPRosterItem> RosterController::getItem(const JID& jid) const { - return xmppRoster_->getItem(jid); + return xmppRoster_->getItem(jid); } std::set<std::string> RosterController::getGroups() const { - return xmppRoster_->getGroups(); + return xmppRoster_->getGroups(); } void RosterController::handleOnCapsChanged(const JID& jid) { - DiscoInfo::ref info = entityCapsManager_->getCaps(jid); - if (info) { - std::set<ContactRosterItem::Feature> features; - if (info->hasFeature(DiscoInfo::JingleFeature) && info->hasFeature(DiscoInfo::JingleFTFeature) && info->hasFeature(DiscoInfo::JingleTransportsIBBFeature)) { - features.insert(ContactRosterItem::FileTransferFeature); - } - if (info->hasFeature(DiscoInfo::WhiteboardFeature)) { - features.insert(ContactRosterItem::WhiteboardFeature); - } - roster_->setAvailableFeatures(jid, features); - } + std::set<ContactRosterItem::Feature> features; + if (featureOracle_->isFileTransferSupported(jid.toBare()) == Tristate::Yes || featureOracle_->isFileTransferSupported(jid.toBare()) == Tristate::Maybe) { + features.insert(ContactRosterItem::FileTransferFeature); + } + if (featureOracle_->isWhiteboardSupported(jid.toBare()) == Tristate::Yes) { + features.insert(ContactRosterItem::WhiteboardFeature); + } + roster_->applyOnItems(SetAvailableFeatures(jid, features)); } } diff --git a/Swift/Controllers/Roster/RosterController.h b/Swift/Controllers/Roster/RosterController.h index 06b551e..d5a5671 100644 --- a/Swift/Controllers/Roster/RosterController.h +++ b/Swift/Controllers/Roster/RosterController.h @@ -1,113 +1,128 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include "Swiften/JID/JID.h" -#include <string> +#include <memory> #include <set> -#include "Swiften/Elements/Presence.h" -#include "Swiften/Elements/ErrorPayload.h" -#include "Swiften/Elements/RosterPayload.h" -#include "Swiften/Avatars/AvatarManager.h" -#include "Swift/Controllers/UIEvents/UIEvent.h" -#include "RosterGroupExpandinessPersister.h" -#include "Swift/Controllers/FileTransfer/FileTransferOverview.h" +#include <string> + +#include <boost/signals2.hpp> -#include "Swiften/Base/boost_bsignals.h" -#include <boost/shared_ptr.hpp> +#include <Swiften/Avatars/AvatarManager.h> +#include <Swiften/Elements/ErrorPayload.h> +#include <Swiften/Elements/Presence.h> +#include <Swiften/Elements/RosterPayload.h> +#include <Swiften/Elements/VCard.h> +#include <Swiften/JID/JID.h> + +#include <Swift/Controllers/Roster/ContactRosterItem.h> +#include <Swift/Controllers/UIEvents/UIEvent.h> namespace Swift { - class IQRouter; - class Roster; - class XMPPRoster; - class XMPPRosterItem; - class MainWindow; - class MainWindowFactory; - class OfflineRosterFilter; - class NickResolver; - class PresenceOracle; - class SubscriptionManager; - class EventController; - class SubscriptionRequestEvent; - class UIEventStream; - class IQRouter; - class SettingsProvider; - class NickManager; - class EntityCapsProvider; - class FileTransferManager; - class ClientBlockListManager; + class AvatarManager; + class Chattables; + class ClientBlockListManager; + class EntityCapsProvider; + class EventController; + class FeatureOracle; + class FileTransferManager; + class IQRouter; + class MainWindow; + class MainWindowFactory; + class NickManager; + class NickResolver; + class OfflineRosterFilter; + class PresenceOracle; + class Roster; + class RosterGroupExpandinessPersister; + class RosterVCardProvider; + class SettingsProvider; + class SubscriptionManager; + class SubscriptionRequestEvent; + class UIEventStream; + class VCardManager; + class XMPPRoster; + class XMPPRosterItem; + + class RosterController { + public: + RosterController(const JID& jid, XMPPRoster* xmppRoster, AvatarManager* avatarManager, MainWindowFactory* mainWindowFactory, NickManager* nickManager, NickResolver* nickResolver, PresenceOracle* presenceOracle, SubscriptionManager* subscriptionManager, EventController* eventController, UIEventStream* uiEventStream, IQRouter* iqRouter, SettingsProvider* settings, EntityCapsProvider* entityCapsProvider, ClientBlockListManager* clientBlockListManager, VCardManager* vcardManager, Chattables& chattables); + ~RosterController(); + void showRosterWindow(); + void setJID(const JID& jid) { myJID_ = jid; } + MainWindow* getWindow() {return mainWindow_;} + boost::signals2::signal<void (StatusShow::Type, const std::string&)> onChangeStatusRequest; + boost::signals2::signal<void ()> onSignOutRequest; + void handleOwnVCardChanged(VCard::ref vcard); + void handleAvatarChanged(const JID& jid); + void handlePresenceChanged(Presence::ref presence); + void setEnabled(bool enabled); - class RosterController { - public: - RosterController(const JID& jid, XMPPRoster* xmppRoster, AvatarManager* avatarManager, MainWindowFactory* mainWindowFactory, NickManager* nickManager, NickResolver* nickResolver, PresenceOracle* presenceOracle, SubscriptionManager* subscriptionManager, EventController* eventController, UIEventStream* uiEventStream, IQRouter* iqRouter_, SettingsProvider* settings, EntityCapsProvider* entityCapsProvider, FileTransferOverview* fileTransferOverview, ClientBlockListManager* clientBlockListManager); - ~RosterController(); - void showRosterWindow(); - MainWindow* getWindow() {return mainWindow_;} - boost::signal<void (StatusShow::Type, const std::string&)> onChangeStatusRequest; - boost::signal<void ()> onSignOutRequest; - void handleAvatarChanged(const JID& jid); - void setEnabled(bool enabled); + boost::optional<XMPPRosterItem> getItem(const JID&) const; + std::set<std::string> getGroups() const; - boost::optional<XMPPRosterItem> getItem(const JID&) const; - std::set<std::string> getGroups() const; + void setContactGroups(const JID& jid, const std::vector<std::string>& groups); + void updateItem(const XMPPRosterItem&); - void setContactGroups(const JID& jid, const std::vector<std::string>& groups); - void updateItem(const XMPPRosterItem&); + void initBlockingCommand(); - void initBlockingCommand(); + private: + void handleOnJIDAdded(const JID &jid); + void handleRosterCleared(); + void handleOnJIDRemoved(const JID &jid); + void handleOnJIDUpdated(const JID &jid, const std::string& oldName, const std::vector<std::string>& oldGroups); + void handleStartChatRequest(const JID& contact); + void handleChangeStatusRequest(StatusShow::Type show, const std::string &statusText); + void handleShowOfflineToggled(bool state); + void handleIncomingPresence(std::shared_ptr<Presence> newPresence); + void handleSubscriptionRequest(const JID& jid, const std::string& message); + void handleSubscriptionRequestAccepted(SubscriptionRequestEvent* event); + void handleSubscriptionRequestDeclined(SubscriptionRequestEvent* event); + void handleUIEvent(std::shared_ptr<UIEvent> event); + void handleRosterItemUpdated(ErrorPayload::ref error, std::shared_ptr<RosterPayload> rosterPayload); + void handleRosterSetError(ErrorPayload::ref error, std::shared_ptr<RosterPayload> rosterPayload); + void applyAllPresenceTo(const JID& jid); + void handleEditProfileRequest(); + void handleOnCapsChanged(const JID& jid); + void handleSettingChanged(const std::string& settingPath); - private: - void handleOnJIDAdded(const JID &jid); - void handleRosterCleared(); - void handleOnJIDRemoved(const JID &jid); - void handleOnJIDUpdated(const JID &jid, const std::string& oldName, const std::vector<std::string>& oldGroups); - void handleStartChatRequest(const JID& contact); - void handleChangeStatusRequest(StatusShow::Type show, const std::string &statusText); - void handleShowOfflineToggled(bool state); - void handleIncomingPresence(boost::shared_ptr<Presence> newPresence); - void handleSubscriptionRequest(const JID& jid, const std::string& message); - void handleSubscriptionRequestAccepted(SubscriptionRequestEvent* event); - void handleSubscriptionRequestDeclined(SubscriptionRequestEvent* event); - void handleUIEvent(boost::shared_ptr<UIEvent> event); - void handleRosterSetError(ErrorPayload::ref error, boost::shared_ptr<RosterPayload> rosterPayload); - void applyAllPresenceTo(const JID& jid); - void handleEditProfileRequest(); - void handleOnCapsChanged(const JID& jid); - void handleSettingChanged(const std::string& settingPath); + void handleBlockingStateChanged(); + void handleBlockingItemAdded(const JID& jid); + void handleBlockingItemRemoved(const JID& jid); - void handleBlockingStateChanged(); - void handleBlockingItemAdded(const JID& jid); - void handleBlockingItemRemoved(const JID& jid); + JID myJID_; + XMPPRoster* xmppRoster_; + MainWindowFactory* mainWindowFactory_; + MainWindow* mainWindow_; + Roster* roster_; + OfflineRosterFilter* offlineFilter_; + VCardManager* vcardManager_; + AvatarManager* avatarManager_; + NickManager* nickManager_; + NickResolver* nickResolver_; + PresenceOracle* presenceOracle_; + SubscriptionManager* subscriptionManager_; + EventController* eventController_; + RosterGroupExpandinessPersister* expandiness_; + IQRouter* iqRouter_; + SettingsProvider* settings_; + UIEventStream* uiEventStream_; + EntityCapsProvider* entityCapsManager_; + ClientBlockListManager* clientBlockListManager_; + Chattables& chattables_; + RosterVCardProvider* rosterVCardProvider_; + std::shared_ptr<ContactRosterItem> ownContact_; + std::unique_ptr<FeatureOracle> featureOracle_; - JID myJID_; - XMPPRoster* xmppRoster_; - MainWindowFactory* mainWindowFactory_; - MainWindow* mainWindow_; - Roster* roster_; - OfflineRosterFilter* offlineFilter_; - AvatarManager* avatarManager_; - NickManager* nickManager_; - NickResolver* nickResolver_; - PresenceOracle* presenceOracle_; - SubscriptionManager* subscriptionManager_; - EventController* eventController_; - RosterGroupExpandinessPersister* expandiness_; - IQRouter* iqRouter_; - SettingsProvider* settings_; - UIEventStream* uiEventStream_; - EntityCapsProvider* entityCapsManager_; - FileTransferOverview* ftOverview_; - ClientBlockListManager* clientBlockListManager_; - - boost::bsignals::scoped_connection blockingOnStateChangedConnection_; - boost::bsignals::scoped_connection blockingOnItemAddedConnection_; - boost::bsignals::scoped_connection blockingOnItemRemovedConnection_; - boost::bsignals::scoped_connection changeStatusConnection_; - boost::bsignals::scoped_connection signOutConnection_; - boost::bsignals::scoped_connection uiEventConnection_; - }; + boost::signals2::scoped_connection blockingOnStateChangedConnection_; + boost::signals2::scoped_connection blockingOnItemAddedConnection_; + boost::signals2::scoped_connection blockingOnItemRemovedConnection_; + boost::signals2::scoped_connection changeStatusConnection_; + boost::signals2::scoped_connection signOutConnection_; + boost::signals2::scoped_connection uiEventConnection_; + }; } diff --git a/Swift/Controllers/Roster/RosterFilter.h b/Swift/Controllers/Roster/RosterFilter.h index 508b9da..6075c66 100644 --- a/Swift/Controllers/Roster/RosterFilter.h +++ b/Swift/Controllers/Roster/RosterFilter.h @@ -1,19 +1,19 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include "Swift/Controllers/Roster/RosterItem.h" +#include <Swift/Controllers/Roster/RosterItem.h> namespace Swift { class RosterFilter { - public: - virtual ~RosterFilter() {} - virtual bool operator() (RosterItem* item) const = 0; + public: + virtual ~RosterFilter() {} + virtual bool operator() (RosterItem* item) const = 0; }; } diff --git a/Swift/Controllers/Roster/RosterGroupExpandinessPersister.cpp b/Swift/Controllers/Roster/RosterGroupExpandinessPersister.cpp index 81f0c12..0f07c0b 100644 --- a/Swift/Controllers/Roster/RosterGroupExpandinessPersister.cpp +++ b/Swift/Controllers/Roster/RosterGroupExpandinessPersister.cpp @@ -1,61 +1,62 @@ /* - * Copyright (c) 2010-2012 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #include <Swift/Controllers/Roster/RosterGroupExpandinessPersister.h> -#include <boost/bind.hpp> #include <vector> -#include <Swiften/Base/foreach.h> +#include <boost/bind.hpp> + #include <Swiften/Base/String.h> + #include <Swift/Controllers/Roster/GroupRosterItem.h> #include <Swift/Controllers/SettingConstants.h> namespace Swift { RosterGroupExpandinessPersister::RosterGroupExpandinessPersister(Roster* roster, SettingsProvider* settings) : roster_(roster), settings_(settings) { - load(); - roster_->onGroupAdded.connect(boost::bind(&RosterGroupExpandinessPersister::handleGroupAdded, this, _1)); + load(); + roster_->onGroupAdded.connect(boost::bind(&RosterGroupExpandinessPersister::handleGroupAdded, this, _1)); } void RosterGroupExpandinessPersister::handleGroupAdded(GroupRosterItem* group) { - if (collapsed_.find(group->getDisplayName()) != collapsed_.end()) { - group->setExpanded(false); - } else { - group->setExpanded(true); - } - group->onExpandedChanged.connect(boost::bind(&RosterGroupExpandinessPersister::handleExpandedChanged, this, group, _1)); + if (collapsed_.find(group->getDisplayName()) != collapsed_.end()) { + group->setExpanded(false); + } else { + group->setExpanded(true); + } + group->onExpandedChanged.connect(boost::bind(&RosterGroupExpandinessPersister::handleExpandedChanged, this, group, _1)); } void RosterGroupExpandinessPersister::handleExpandedChanged(GroupRosterItem* group, bool expanded) { - if (expanded) { - std::string displayName = group->getDisplayName(); - //collapsed_.erase(std::remove(collapsed_.begin(), collapsed_.end(), displayName), collapsed_.end()); - collapsed_.erase(displayName); - } else { - collapsed_.insert(group->getDisplayName()); - } - save(); + if (expanded) { + std::string displayName = group->getDisplayName(); + //collapsed_.erase(std::remove(collapsed_.begin(), collapsed_.end(), displayName), collapsed_.end()); + collapsed_.erase(displayName); + } else { + collapsed_.insert(group->getDisplayName()); + } + save(); } void RosterGroupExpandinessPersister::save() { - std::string setting; - foreach (const std::string& group, collapsed_) { - if (!setting.empty()) { - setting += "\n"; - } - setting += group; - } - settings_->storeSetting(SettingConstants::EXPANDED_ROSTER_GROUPS, setting); + std::string setting; + for (const auto& group : collapsed_) { + if (!setting.empty()) { + setting += "\n"; + } + setting += group; + } + settings_->storeSetting(SettingConstants::EXPANDED_ROSTER_GROUPS, setting); } void RosterGroupExpandinessPersister::load() { - std::string saved = settings_->getSetting(SettingConstants::EXPANDED_ROSTER_GROUPS); - std::vector<std::string> collapsed = String::split(saved, '\n'); - collapsed_.insert(collapsed.begin(), collapsed.end()); + std::string saved = settings_->getSetting(SettingConstants::EXPANDED_ROSTER_GROUPS); + std::vector<std::string> collapsed = String::split(saved, '\n'); + collapsed_.insert(collapsed.begin(), collapsed.end()); } diff --git a/Swift/Controllers/Roster/RosterGroupExpandinessPersister.h b/Swift/Controllers/Roster/RosterGroupExpandinessPersister.h index 73c4f29..4cc08a7 100644 --- a/Swift/Controllers/Roster/RosterGroupExpandinessPersister.h +++ b/Swift/Controllers/Roster/RosterGroupExpandinessPersister.h @@ -1,26 +1,27 @@ /* - * Copyright (c) 2010-2012 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once #include <set> -#include "Swift/Controllers/Roster/Roster.h" -#include "Swift/Controllers/Settings/SettingsProvider.h" + +#include <Swift/Controllers/Roster/Roster.h> +#include <Swift/Controllers/Settings/SettingsProvider.h> namespace Swift { - class RosterGroupExpandinessPersister { - public: - RosterGroupExpandinessPersister(Roster* roster, SettingsProvider* settings); - private: - void handleExpandedChanged(GroupRosterItem* group, bool expanded); - void handleGroupAdded(GroupRosterItem* group); - void load(); - void save(); - std::set<std::string> collapsed_; - Roster* roster_; - SettingsProvider* settings_; - }; + class RosterGroupExpandinessPersister { + public: + RosterGroupExpandinessPersister(Roster* roster, SettingsProvider* settings); + private: + void handleExpandedChanged(GroupRosterItem* group, bool expanded); + void handleGroupAdded(GroupRosterItem* group); + void load(); + void save(); + std::set<std::string> collapsed_; + Roster* roster_; + SettingsProvider* settings_; + }; } diff --git a/Swift/Controllers/Roster/RosterItem.cpp b/Swift/Controllers/Roster/RosterItem.cpp index 77db8a3..685613f 100644 --- a/Swift/Controllers/Roster/RosterItem.cpp +++ b/Swift/Controllers/Roster/RosterItem.cpp @@ -1,22 +1,22 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ -#include "Swift/Controllers/Roster/RosterItem.h" +#include <Swift/Controllers/Roster/RosterItem.h> #include <boost/algorithm/string.hpp> -#include "Swift/Controllers/Roster/GroupRosterItem.h" +#include <Swift/Controllers/Roster/GroupRosterItem.h> namespace Swift { RosterItem::RosterItem(const std::string& name, GroupRosterItem* parent) : name_(name), sortableDisplayName_(boost::to_lower_copy(name_)), parent_(parent) { - /* The following would be good, but because of C++'s inheritance not working in constructors, it's not going to work. */ - //if (parent) { - // parent_->addChild(this); - //} + /* The following would be good, but because of C++'s inheritance not working in constructors, it's not going to work. */ + //if (parent) { + // parent_->addChild(this); + //} } RosterItem::~RosterItem() { @@ -24,21 +24,21 @@ RosterItem::~RosterItem() { } GroupRosterItem* RosterItem::getParent() const { - return parent_; + return parent_; } void RosterItem::setDisplayName(const std::string& name) { - name_ = name; - sortableDisplayName_ = boost::to_lower_copy(name_); - onDataChanged(); + name_ = name; + sortableDisplayName_ = boost::to_lower_copy(name_); + onDataChanged(); } const std::string& RosterItem::getDisplayName() const { - return name_; + return name_; } const std::string& RosterItem::getSortableDisplayName() const { - return sortableDisplayName_; + return sortableDisplayName_; } diff --git a/Swift/Controllers/Roster/RosterItem.h b/Swift/Controllers/Roster/RosterItem.h index 769f72d..b834785 100644 --- a/Swift/Controllers/Roster/RosterItem.h +++ b/Swift/Controllers/Roster/RosterItem.h @@ -1,31 +1,31 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include "Swiften/Base/boost_bsignals.h" -#include <boost/shared_ptr.hpp> - +#include <memory> #include <string> +#include <boost/signals2.hpp> + namespace Swift { class GroupRosterItem; class RosterItem { - public: - RosterItem(const std::string& name, GroupRosterItem* parent); - virtual ~RosterItem(); - boost::signal<void ()> onDataChanged; - GroupRosterItem* getParent() const; - void setDisplayName(const std::string& name); - const std::string& getDisplayName() const; - virtual const std::string& getSortableDisplayName() const; - private: - std::string name_; - std::string sortableDisplayName_; - GroupRosterItem* parent_; + public: + RosterItem(const std::string& name, GroupRosterItem* parent); + virtual ~RosterItem(); + boost::signals2::signal<void ()> onDataChanged; + GroupRosterItem* getParent() const; + void setDisplayName(const std::string& name); + const std::string& getDisplayName() const; + virtual const std::string& getSortableDisplayName() const; + private: + std::string name_; + std::string sortableDisplayName_; + GroupRosterItem* parent_; }; } diff --git a/Swift/Controllers/Roster/RosterItemOperation.h b/Swift/Controllers/Roster/RosterItemOperation.h deleted file mode 100644 index f1dff8d..0000000 --- a/Swift/Controllers/Roster/RosterItemOperation.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. - */ - -#pragma once - -#include "Swift/Controllers/Roster/RosterItem.h" - -namespace Swift { - -class RosterItemOperation { - public: - RosterItemOperation(bool requiresLookup = false, const JID& lookupJID = JID()) : requiresLookup_(requiresLookup), lookupJID_(lookupJID) {} - virtual ~RosterItemOperation() {} - bool requiresLookup() const {return requiresLookup_;} - const JID& lookupJID() const {return lookupJID_;} - /** - * This is called when iterating over possible subjects, so must check it's - * applying to the right items - even if requiresLookup() is true an item - * with the same bare JID but different full JID may be passed. - */ - virtual void operator() (RosterItem*) const = 0; - private: - bool requiresLookup_; - JID lookupJID_; -}; - -} diff --git a/Swift/Controllers/Roster/RosterVCardProvider.cpp b/Swift/Controllers/Roster/RosterVCardProvider.cpp new file mode 100644 index 0000000..2aa82a9 --- /dev/null +++ b/Swift/Controllers/Roster/RosterVCardProvider.cpp @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2013 Tobias Markmann + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +/* + * Copyright (c) 2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#include <Swift/Controllers/Roster/RosterVCardProvider.h> + +#include <Swiften/VCards/VCardManager.h> + +#include <Swift/Controllers/Roster/ItemOperations/SetVCard.h> +#include <Swift/Controllers/Roster/Roster.h> + +namespace Swift { + +RosterVCardProvider::RosterVCardProvider(Roster* roster, VCardManager* vcardManager, JID::CompareType compareType) : roster_(roster), vcardManager_(vcardManager), compareType_(compareType) { + vcardUpdateRequestedConnection = roster_->onVCardUpdateRequested.connect(boost::bind(&RosterVCardProvider::handleVCardUpdateRequested, this, _1)); + vcardChangedConnection = vcardManager_->onVCardChanged.connect(boost::bind(&RosterVCardProvider::handleVCardChanged, this, _1, _2)); +} + +RosterVCardProvider::~RosterVCardProvider() { +} + +void RosterVCardProvider::handleVCardUpdateRequested(const JID& jid) { + VCard::ref vcard = vcardManager_->getVCardAndRequestWhenNeeded(jid); + if (vcard) { + handleVCardChanged(jid, vcard); + } +} + +void RosterVCardProvider::handleVCardChanged(const JID& jid, VCard::ref vcard) { + roster_->applyOnItem(SetVCard(jid, vcard, compareType_), jid); +} + + +} diff --git a/Swift/Controllers/Roster/RosterVCardProvider.h b/Swift/Controllers/Roster/RosterVCardProvider.h new file mode 100644 index 0000000..337b0b2 --- /dev/null +++ b/Swift/Controllers/Roster/RosterVCardProvider.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2013 Tobias Markmann + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +/* + * Copyright (c) 2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#pragma once + +#include <boost/signals2.hpp> +#include <boost/signals2/connection.hpp> + +#include <Swiften/Elements/VCard.h> +#include <Swiften/JID/JID.h> + +namespace Swift { + +class Roster; +class VCardManager; + +class RosterVCardProvider { + public: + RosterVCardProvider(Roster* roster, VCardManager* vcardManager, JID::CompareType compareType); + ~RosterVCardProvider(); + + private: + void handleVCardUpdateRequested(const JID& jid); + void handleVCardChanged(const JID& jid, VCard::ref vcard); + + private: + Roster* roster_; + VCardManager* vcardManager_; + JID::CompareType compareType_; + boost::signals2::scoped_connection vcardUpdateRequestedConnection; + boost::signals2::scoped_connection vcardChangedConnection; +}; + +} diff --git a/Swift/Controllers/Roster/SetAvatar.h b/Swift/Controllers/Roster/SetAvatar.h deleted file mode 100644 index 424f0b3..0000000 --- a/Swift/Controllers/Roster/SetAvatar.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (c) 2010-2013 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. - */ - -#pragma once - -#include "Swiften/Elements/Presence.h" -#include "Swiften/JID/JID.h" -#include "Swift/Controllers/Roster/RosterItemOperation.h" -#include "Swift/Controllers/Roster/ContactRosterItem.h" -#include <boost/filesystem/path.hpp> - -namespace Swift { - -class RosterItem; - -class SetAvatar : public RosterItemOperation { - public: - SetAvatar(const JID& jid, const boost::filesystem::path& path, JID::CompareType compareType = JID::WithoutResource) : RosterItemOperation(true, jid), jid_(jid), path_(path), compareType_(compareType) { - } - - virtual void operator() (RosterItem* item) const { - ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(item); - if (contact && contact->getJID().equals(jid_, compareType_)) { - contact->setAvatarPath(path_); - } - } - - private: - JID jid_; - boost::filesystem::path path_; - JID::CompareType compareType_; -}; - -} diff --git a/Swift/Controllers/Roster/SetName.h b/Swift/Controllers/Roster/SetName.h deleted file mode 100644 index aefb0dc..0000000 --- a/Swift/Controllers/Roster/SetName.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. - */ - -#pragma once - -#include "Swiften/JID/JID.h" -#include "Swift/Controllers/Roster/RosterItemOperation.h" -#include "Swift/Controllers/Roster/ContactRosterItem.h" - -namespace Swift { - -class RosterItem; - -class SetName : public RosterItemOperation { - public: - SetName(const std::string& name, const JID& jid, JID::CompareType compareType = JID::WithoutResource) : RosterItemOperation(true, jid), name_(name), jid_(jid), compareType_(compareType) { - } - - virtual void operator() (RosterItem* item) const { - ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(item); - if (contact && contact->getJID().equals(jid_, compareType_)) { - contact->setDisplayName(name_); - } - } - - private: - std::string name_; - JID jid_; - JID::CompareType compareType_; -}; - -} - - diff --git a/Swift/Controllers/Roster/SetPresence.h b/Swift/Controllers/Roster/SetPresence.h deleted file mode 100644 index 06adfa4..0000000 --- a/Swift/Controllers/Roster/SetPresence.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. - */ - -#pragma once - -#include "Swiften/Elements/Presence.h" -#include "Swiften/JID/JID.h" -#include "Swift/Controllers/Roster/RosterItemOperation.h" -#include "Swift/Controllers/Roster/ContactRosterItem.h" - -namespace Swift { - -class RosterItem; - -class SetPresence : public RosterItemOperation { - public: - SetPresence(Presence::ref presence, JID::CompareType compareType = JID::WithoutResource) : RosterItemOperation(true, compareType == JID::WithoutResource ? presence->getFrom().toBare() : presence->getFrom()), presence_(presence), compareType_(compareType) { - } - - virtual void operator() (RosterItem* item) const { - ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(item); - if (contact && contact->getJID().equals(presence_->getFrom(), compareType_)) { - contact->applyPresence(presence_->getFrom().getResource(), presence_); - } - } - - private: - Presence::ref presence_; - JID::CompareType compareType_; -}; - -} - diff --git a/Swift/Controllers/Roster/TableRoster.cpp b/Swift/Controllers/Roster/TableRoster.cpp index eb036db..01bf4a6 100644 --- a/Swift/Controllers/Roster/TableRoster.cpp +++ b/Swift/Controllers/Roster/TableRoster.cpp @@ -1,185 +1,191 @@ /* - * Copyright (c) 2011 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2011-2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #include <Swift/Controllers/Roster/TableRoster.h> -#include <boost/cast.hpp> -#include <cassert> #include <algorithm> +#include <cassert> + +#include <boost/cast.hpp> #include <boost/numeric/conversion/cast.hpp> -#include <Swiften/Base/foreach.h> -#include <Swiften/Network/TimerFactory.h> #include <Swiften/Network/Timer.h> -#include <Swift/Controllers/Roster/Roster.h> +#include <Swiften/Network/TimerFactory.h> + #include <Swift/Controllers/Roster/GroupRosterItem.h> #include <Swift/Controllers/Roster/LeastCommonSubsequence.h> +#include <Swift/Controllers/Roster/Roster.h> namespace Swift { - struct SectionNameEquals { - bool operator()(const TableRoster::Section& s1, const TableRoster::Section& s2) const { - return s1.name == s2.name; - } - }; - - template<typename T> - struct True { - bool operator()(const T&, const T&) const { - return true; - } - }; - - struct ItemEquals { - bool operator()(const TableRoster::Item& i1, const TableRoster::Item& i2) const { - return i1.jid == i2.jid; - } - }; - - - struct ItemNeedsUpdate { - bool operator()(const TableRoster::Item& i1, const TableRoster::Item& i2) const { - return i1.status != i2.status || i1.description != i2.description || i1.name != i2.name || i1.avatarPath.empty() != i2.avatarPath.empty(); - } - }; - - struct CreateIndexForSection { - CreateIndexForSection(size_t section) : section(section) { - } - - TableRoster::Index operator()(size_t row) const { - return TableRoster::Index(section, row); - } - - size_t section; - }; + struct SectionNameEquals { + bool operator()(const TableRoster::Section& s1, const TableRoster::Section& s2) const { + return s1.name == s2.name; + } + }; + + template<typename T> + struct True { + bool operator()(const T&, const T&) const { + return true; + } + }; + + struct ItemEquals { + bool operator()(const TableRoster::Item& i1, const TableRoster::Item& i2) const { + return i1.jid == i2.jid; + } + }; + + + struct ItemNeedsUpdate { + bool operator()(const TableRoster::Item& i1, const TableRoster::Item& i2) const { + return i1.status != i2.status || i1.description != i2.description || i1.name != i2.name || i1.avatarPath.empty() != i2.avatarPath.empty(); + } + }; + + struct CreateIndexForSection { + CreateIndexForSection(size_t section) : section(section) { + } + + TableRoster::Index operator()(size_t row) const { + return TableRoster::Index(section, row); + } + + size_t section; + }; } using namespace Swift; TableRoster::TableRoster(Roster* model, TimerFactory* timerFactory, int updateDelay) : model(model), updatePending(false) { - updateTimer = timerFactory->createTimer(updateDelay); - updateTimer->onTick.connect(boost::bind(&TableRoster::handleUpdateTimerTick, this)); - if (model) { - model->onChildrenChanged.connect(boost::bind(&TableRoster::scheduleUpdate, this)); - model->onGroupAdded.connect(boost::bind(&TableRoster::scheduleUpdate, this)); - model->onDataChanged.connect(boost::bind(&TableRoster::scheduleUpdate, this)); - } + updateTimer = timerFactory->createTimer(updateDelay); + updateTimer->onTick.connect(boost::bind(&TableRoster::handleUpdateTimerTick, this)); + if (model) { + model->onChildrenChanged.connect(boost::bind(&TableRoster::scheduleUpdate, this)); + model->onGroupAdded.connect(boost::bind(&TableRoster::scheduleUpdate, this)); + model->onDataChanged.connect(boost::bind(&TableRoster::scheduleUpdate, this)); + } } TableRoster::~TableRoster() { - updateTimer->stop(); - updateTimer->onTick.disconnect(boost::bind(&TableRoster::handleUpdateTimerTick, this)); - if (model) { - model->onDataChanged.disconnect(boost::bind(&TableRoster::scheduleUpdate, this)); - model->onGroupAdded.disconnect(boost::bind(&TableRoster::scheduleUpdate, this)); - model->onChildrenChanged.disconnect(boost::bind(&TableRoster::scheduleUpdate, this)); - } + updateTimer->stop(); + updateTimer->onTick.disconnect(boost::bind(&TableRoster::handleUpdateTimerTick, this)); + if (model) { + model->onDataChanged.disconnect(boost::bind(&TableRoster::scheduleUpdate, this)); + model->onGroupAdded.disconnect(boost::bind(&TableRoster::scheduleUpdate, this)); + model->onChildrenChanged.disconnect(boost::bind(&TableRoster::scheduleUpdate, this)); + } } - + size_t TableRoster::getNumberOfSections() const { - return sections.size(); + return sections.size(); } const std::string& TableRoster::getSectionTitle(size_t section) { - return sections[section].name; + return sections[section].name; } size_t TableRoster::getNumberOfRowsInSection(size_t section) const { - return sections[section].items.size(); + return sections[section].items.size(); } const TableRoster::Item& TableRoster::getItem(const Index& index) const { - return sections[index.section].items[index.row]; + return sections[index.section].items[index.row]; } - + void TableRoster::handleUpdateTimerTick() { - updateTimer->stop(); - updatePending = false; - - // Get a model for the new roster - std::vector<Section> newSections; - if (model) { - foreach(RosterItem* item, model->getRoot()->getDisplayedChildren()) { - if (GroupRosterItem* groupItem = boost::polymorphic_downcast<GroupRosterItem*>(item)) { - //std::cerr << "* " << groupItem->getDisplayName() << std::endl; - Section section(groupItem->getDisplayName()); - foreach(RosterItem* groupChildItem, groupItem->getDisplayedChildren()) { - if (ContactRosterItem* contact = boost::polymorphic_downcast<ContactRosterItem*>(groupChildItem)) { - //std::cerr << " - " << contact->getDisplayJID() << std::endl; - section.items.push_back(Item(contact->getDisplayName(), contact->getStatusText(), contact->getDisplayJID(), contact->getStatusShow(), contact->getAvatarPath())); - } - } - newSections.push_back(section); - } - } - } - - // Do a diff with the previous roster - Update update; - std::vector<size_t> sectionUpdates; - std::vector<size_t> sectionPostUpdates; - computeIndexDiff<Section,SectionNameEquals,True<Section> >(sections, newSections, sectionUpdates, sectionPostUpdates, update.deletedSections, update.insertedSections); - assert(sectionUpdates.size() == sectionPostUpdates.size()); - for (size_t i = 0; i < sectionUpdates.size(); ++i) { - assert(sectionUpdates[i] < sections.size()); - assert(sectionPostUpdates[i] < newSections.size()); - std::vector<size_t> itemUpdates; - std::vector<size_t> itemPostUpdates; - std::vector<size_t> itemRemoves; - std::vector<size_t> itemInserts; - computeIndexDiff<Item, ItemEquals, ItemNeedsUpdate >(sections[sectionUpdates[i]].items, newSections[sectionPostUpdates[i]].items, itemUpdates, itemPostUpdates, itemRemoves, itemInserts); - size_t end = update.insertedRows.size(); - update.insertedRows.resize(update.insertedRows.size() + itemInserts.size()); - std::transform(itemInserts.begin(), itemInserts.end(), update.insertedRows.begin() + boost::numeric_cast<long long>(end), CreateIndexForSection(sectionPostUpdates[i])); - end = update.deletedRows.size(); - update.deletedRows.resize(update.deletedRows.size() + itemRemoves.size()); - std::transform(itemRemoves.begin(), itemRemoves.end(), update.deletedRows.begin() + boost::numeric_cast<long long>(end), CreateIndexForSection(sectionUpdates[i])); - end = update.updatedRows.size(); - update.updatedRows.resize(update.updatedRows.size() + itemUpdates.size()); - std::transform(itemUpdates.begin(), itemUpdates.end(), update.updatedRows.begin() + boost::numeric_cast<long long>(end), CreateIndexForSection(sectionPostUpdates[i])); - } - - // Switch the old model with the new - sections.swap(newSections); - - /* - std::cerr << "-S: "; - for (size_t i = 0; i < update.deletedSections.size(); ++i) { - std::cerr << update.deletedSections[i] << " "; - } - std::cerr << std::endl; - std::cerr << "+S: "; - for (size_t i = 0; i < update.insertedSections.size(); ++i) { - std::cerr << update.insertedSections[i] << " "; - } - std::cerr << std::endl; - std::cerr << "-R: "; - for (size_t i = 0; i < update.deletedRows.size(); ++i) { - std::cerr << update.deletedRows[i].section << "," << update.deletedRows[i].row << " "; - } - std::cerr << std::endl; - std::cerr << "*R: "; - for (size_t i = 0; i < update.updatedRows.size(); ++i) { - std::cerr << update.updatedRows[i].section << "," << update.updatedRows[i].row << " "; - } - std::cerr << std::endl; - std::cerr << "+R: "; - for (size_t i = 0; i < update.insertedRows.size(); ++i) { - std::cerr << update.insertedRows[i].section << "," << update.insertedRows[i].row << " "; - } - std::cerr << std::endl; - */ - - // Emit the update - onUpdate(update); + updateTimer->stop(); + updatePending = false; + + // Get a model for the new roster + std::vector<Section> newSections; + if (model) { + for (auto item : model->getRoot()->getDisplayedChildren()) { + if (GroupRosterItem* groupItem = boost::polymorphic_downcast<GroupRosterItem*>(item)) { + //std::cerr << "* " << groupItem->getDisplayName() << std::endl; + Section section(groupItem->getDisplayName()); + for (auto groupChildItem : groupItem->getDisplayedChildren()) { + if (ContactRosterItem* contact = boost::polymorphic_downcast<ContactRosterItem*>(groupChildItem)) { + //std::cerr << " - " << contact->getDisplayJID() << std::endl; + section.items.push_back(Item(contact->getDisplayName(), contact->getStatusText(), contact->getDisplayJID(), contact->getStatusShow(), contact->getAvatarPath())); + } + } + newSections.push_back(section); + } + } + } + + // Do a diff with the previous roster + Update update; + std::vector<size_t> sectionUpdates; + std::vector<size_t> sectionPostUpdates; + computeIndexDiff<Section,SectionNameEquals,True<Section> >(sections, newSections, sectionUpdates, sectionPostUpdates, update.deletedSections, update.insertedSections); + assert(sectionUpdates.size() == sectionPostUpdates.size()); + for (size_t i = 0; i < sectionUpdates.size(); ++i) { + assert(sectionUpdates[i] < sections.size()); + assert(sectionPostUpdates[i] < newSections.size()); + std::vector<size_t> itemUpdates; + std::vector<size_t> itemPostUpdates; + std::vector<size_t> itemRemoves; + std::vector<size_t> itemInserts; + computeIndexDiff<Item, ItemEquals, ItemNeedsUpdate >(sections[sectionUpdates[i]].items, newSections[sectionPostUpdates[i]].items, itemUpdates, itemPostUpdates, itemRemoves, itemInserts); + try { + size_t end = update.insertedRows.size(); + update.insertedRows.resize(update.insertedRows.size() + itemInserts.size()); + std::transform(itemInserts.begin(), itemInserts.end(), update.insertedRows.begin() + boost::numeric_cast<long long>(end), CreateIndexForSection(sectionPostUpdates[i])); + end = update.deletedRows.size(); + update.deletedRows.resize(update.deletedRows.size() + itemRemoves.size()); + std::transform(itemRemoves.begin(), itemRemoves.end(), update.deletedRows.begin() + boost::numeric_cast<long long>(end), CreateIndexForSection(sectionUpdates[i])); + end = update.updatedRows.size(); + update.updatedRows.resize(update.updatedRows.size() + itemUpdates.size()); + std::transform(itemUpdates.begin(), itemUpdates.end(), update.updatedRows.begin() + boost::numeric_cast<long long>(end), CreateIndexForSection(sectionPostUpdates[i])); + } + catch (const boost::numeric::bad_numeric_cast&) { + // If any container claims it has more than long long max items, we have bigger issues, so letting this pass + } + } + + // Switch the old model with the new + sections.swap(newSections); + + /* + std::cerr << "-S: "; + for (size_t i = 0; i < update.deletedSections.size(); ++i) { + std::cerr << update.deletedSections[i] << " "; + } + std::cerr << std::endl; + std::cerr << "+S: "; + for (size_t i = 0; i < update.insertedSections.size(); ++i) { + std::cerr << update.insertedSections[i] << " "; + } + std::cerr << std::endl; + std::cerr << "-R: "; + for (size_t i = 0; i < update.deletedRows.size(); ++i) { + std::cerr << update.deletedRows[i].section << "," << update.deletedRows[i].row << " "; + } + std::cerr << std::endl; + std::cerr << "*R: "; + for (size_t i = 0; i < update.updatedRows.size(); ++i) { + std::cerr << update.updatedRows[i].section << "," << update.updatedRows[i].row << " "; + } + std::cerr << std::endl; + std::cerr << "+R: "; + for (size_t i = 0; i < update.insertedRows.size(); ++i) { + std::cerr << update.insertedRows[i].section << "," << update.insertedRows[i].row << " "; + } + std::cerr << std::endl; + */ + + // Emit the update + onUpdate(update); } void TableRoster::scheduleUpdate() { - if (!updatePending) { - updatePending = true; - updateTimer->start(); - } + if (!updatePending) { + updatePending = true; + updateTimer->start(); + } } diff --git a/Swift/Controllers/Roster/TableRoster.h b/Swift/Controllers/Roster/TableRoster.h index f447760..22c9ca9 100644 --- a/Swift/Controllers/Roster/TableRoster.h +++ b/Swift/Controllers/Roster/TableRoster.h @@ -1,84 +1,85 @@ /* - * Copyright (c) 2011 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2011-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once #include <string> #include <vector> -#include <Swiften/Base/boost_bsignals.h> -#include <Swiften/JID/JID.h> -#include <Swiften/Elements/StatusShow.h> #include <boost/filesystem/path.hpp> +#include <boost/signals2.hpp> + +#include <Swiften/Elements/StatusShow.h> +#include <Swiften/JID/JID.h> namespace Swift { - class Roster; - class TimerFactory; - class Timer; - - class TableRoster { - public: - struct Item { - Item(const std::string& name, const std::string& description, const JID& jid, StatusShow::Type status, const boost::filesystem::path& avatarPath) : name(name), description(description), jid(jid), status(status), avatarPath(avatarPath) { - } - std::string name; - std::string description; - JID jid; - StatusShow::Type status; - boost::filesystem::path avatarPath; - }; - - struct Index { - Index(size_t section = 0, size_t row = 0) : section(section), row(row) { - } - size_t section; - size_t row; - - bool operator==(const Index& o) const { - return o.section == section && o.row == row; - } - }; - - struct Update { - std::vector<Index> updatedRows; - std::vector<Index> insertedRows; - std::vector<Index> deletedRows; - std::vector<size_t> insertedSections; - std::vector<size_t> deletedSections; - }; - - TableRoster(Roster* model, TimerFactory* timerFactory, int updateDelay); - ~TableRoster(); - - size_t getNumberOfSections() const; - size_t getNumberOfRowsInSection(size_t section) const; - - const std::string& getSectionTitle(size_t); - - const Item& getItem(const Index&) const; - - boost::signal<void (const Update&)> onUpdate; - - private: - void handleUpdateTimerTick(); - void scheduleUpdate(); - - private: - friend struct SectionNameEquals; - struct Section { - Section(const std::string& name) : name(name) { - } - - std::string name; - std::vector<Item> items; - }; - - Roster* model; - std::vector<Section> sections; - bool updatePending; - boost::shared_ptr<Timer> updateTimer; - }; + class Roster; + class TimerFactory; + class Timer; + + class TableRoster { + public: + struct Item { + Item(const std::string& name, const std::string& description, const JID& jid, StatusShow::Type status, const boost::filesystem::path& avatarPath) : name(name), description(description), jid(jid), status(status), avatarPath(avatarPath) { + } + std::string name; + std::string description; + JID jid; + StatusShow::Type status; + boost::filesystem::path avatarPath; + }; + + struct Index { + Index(size_t section = 0, size_t row = 0) : section(section), row(row) { + } + size_t section; + size_t row; + + bool operator==(const Index& o) const { + return o.section == section && o.row == row; + } + }; + + struct Update { + std::vector<Index> updatedRows; + std::vector<Index> insertedRows; + std::vector<Index> deletedRows; + std::vector<size_t> insertedSections; + std::vector<size_t> deletedSections; + }; + + TableRoster(Roster* model, TimerFactory* timerFactory, int updateDelay); + ~TableRoster(); + + size_t getNumberOfSections() const; + size_t getNumberOfRowsInSection(size_t section) const; + + const std::string& getSectionTitle(size_t); + + const Item& getItem(const Index&) const; + + boost::signals2::signal<void (const Update&)> onUpdate; + + private: + void handleUpdateTimerTick(); + void scheduleUpdate(); + + private: + friend struct SectionNameEquals; + struct Section { + Section(const std::string& name) : name(name) { + } + + std::string name; + std::vector<Item> items; + }; + + Roster* model; + std::vector<Section> sections; + bool updatePending; + std::shared_ptr<Timer> updateTimer; + }; } diff --git a/Swift/Controllers/Roster/UnitTest/LeastCommonSubsequenceTest.cpp b/Swift/Controllers/Roster/UnitTest/LeastCommonSubsequenceTest.cpp index 963c5cd..5844ebe 100644 --- a/Swift/Controllers/Roster/UnitTest/LeastCommonSubsequenceTest.cpp +++ b/Swift/Controllers/Roster/UnitTest/LeastCommonSubsequenceTest.cpp @@ -1,308 +1,311 @@ /* - * Copyright (c) 2011 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2011-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ -#include <boost/assign/list_of.hpp> #include <functional> +#include <boost/assign/list_of.hpp> + #include <QA/Checker/IO.h> + #include <cppunit/extensions/HelperMacros.h> #include <cppunit/extensions/TestFactoryRegistry.h> + #include <Swift/Controllers/Roster/LeastCommonSubsequence.h> using namespace Swift; struct IsBOrC { - bool operator()(char c, char c2) const { - CPPUNIT_ASSERT_EQUAL(c, c2); - return c == 'b' || c == 'c'; - } + bool operator()(char c, char c2) const { + CPPUNIT_ASSERT_EQUAL(c, c2); + return c == 'b' || c == 'c'; + } }; struct IsXOrY { - bool operator()(char c, char c2) const { - CPPUNIT_ASSERT_EQUAL(c, c2); - return c == 'x' || c == 'y'; - } + bool operator()(char c, char c2) const { + CPPUNIT_ASSERT_EQUAL(c, c2); + return c == 'x' || c == 'y'; + } }; struct IsArizonaOrNewJersey { - bool operator()(const std::string& s, const std::string& s2) const { - CPPUNIT_ASSERT_EQUAL(s, s2); - return s == "Arizona" || s == "New Jersey"; - } + bool operator()(const std::string& s, const std::string& s2) const { + CPPUNIT_ASSERT_EQUAL(s, s2); + return s == "Arizona" || s == "New Jersey"; + } }; class LeastCommonSubsequenceTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(LeastCommonSubsequenceTest); - CPPUNIT_TEST(testComputeLeastCommonSubsequenceMatrix_1); - CPPUNIT_TEST(testComputeLeastCommonSubsequenceMatrix_2); - CPPUNIT_TEST(testComputeLeastCommonSubsequenceMatrix_Sequence1Empty); - CPPUNIT_TEST(testComputeLeastCommonSubsequenceMatrix_Sequence2Empty); - CPPUNIT_TEST(testComputeLeastCommonSubsequenceMatrix_BothSequencesEmpty); - CPPUNIT_TEST(testComputeLeastCommonSubsequenceMatrix_NoCommonSequence); - CPPUNIT_TEST(testComputeLeastCommonSubsequenceMatrix_SameSequences); - CPPUNIT_TEST(testComputeIndexDiff_1); - CPPUNIT_TEST(testComputeIndexDiff_2); - CPPUNIT_TEST(testComputeIndexDiff_Sequence1Empty); - CPPUNIT_TEST(testComputeIndexDiff_Sequence2Empty); - CPPUNIT_TEST(testComputeIndexDiff_BothSequencesEmpty); - CPPUNIT_TEST(testComputeIndexDiff_NoCommonSequence); - CPPUNIT_TEST(testComputeIndexDiff_SameSequences); - CPPUNIT_TEST(testComputeIndexDiff_CommonPrefixAndSuffix); - CPPUNIT_TEST_SUITE_END(); - - public: - void testComputeLeastCommonSubsequenceMatrix_1() { - std::vector<char> x = boost::assign::list_of('x')('m')('j')('y')('a')('u')('z'); - std::vector<char> y = boost::assign::list_of('m')('z')('j')('a')('w')('x')('u'); - - std::vector<int> result; - Detail::computeLeastCommonSubsequenceMatrix<std::vector<char>::const_iterator, std::vector<char>::const_iterator, int, std::equal_to<char> >(x.begin(), x.end(), y.begin(), y.end(), result); - - std::vector<int> expected = boost::assign::list_of - (0)(0)(0)(0)(0)(0)(0)(0) - (0)(0)(1)(1)(1)(1)(1)(1) - (0)(0)(1)(1)(1)(1)(1)(2) - (0)(0)(1)(2)(2)(2)(2)(2) - (0)(0)(1)(2)(2)(3)(3)(3) - (0)(0)(1)(2)(2)(3)(3)(3) - (0)(1)(1)(2)(2)(3)(3)(3) - (0)(1)(1)(2)(2)(3)(4)(4); - CPPUNIT_ASSERT_EQUAL(expected, result); - } - - void testComputeLeastCommonSubsequenceMatrix_2() { - std::vector<char> x = boost::assign::list_of('x')('x')('x')('m')('j')('y')('a')('u')('z'); - std::vector<char> y = boost::assign::list_of('m')('z')('j')('a')('w')('x')('u'); - - std::vector<int> result; - Detail::computeLeastCommonSubsequenceMatrix<std::vector<char>::const_iterator, std::vector<char>::const_iterator, int, std::equal_to<char> >(x.begin(), x.end(), y.begin(), y.end(), result); - - std::vector<int> expected = boost::assign::list_of - (0)(0)(0)(0)(0)(0)(0)(0)(0)(0) - (0)(0)(0)(0)(1)(1)(1)(1)(1)(1) - (0)(0)(0)(0)(1)(1)(1)(1)(1)(2) - (0)(0)(0)(0)(1)(2)(2)(2)(2)(2) - (0)(0)(0)(0)(1)(2)(2)(3)(3)(3) - (0)(0)(0)(0)(1)(2)(2)(3)(3)(3) - (0)(1)(1)(1)(1)(2)(2)(3)(3)(3) - (0)(1)(1)(1)(1)(2)(2)(3)(4)(4); - CPPUNIT_ASSERT_EQUAL(expected, result); - } - - void testComputeLeastCommonSubsequenceMatrix_Sequence1Empty() { - std::vector<char> x; - std::vector<char> y = boost::assign::list_of('a')('b')('c'); - - std::vector<int> result; - Detail::computeLeastCommonSubsequenceMatrix<std::vector<char>::const_iterator, std::vector<char>::const_iterator, int, std::equal_to<char> >(x.begin(), x.end(), y.begin(), y.end(), result); - - std::vector<int> expected = boost::assign::list_of - (0) - (0) - (0) - (0); - CPPUNIT_ASSERT_EQUAL(expected, result); - } - - void testComputeLeastCommonSubsequenceMatrix_Sequence2Empty() { - std::vector<char> x = boost::assign::list_of('a')('b')('c'); - std::vector<char> y; - - std::vector<int> result; - Detail::computeLeastCommonSubsequenceMatrix<std::vector<char>::const_iterator, std::vector<char>::const_iterator, int, std::equal_to<char> >(x.begin(), x.end(), y.begin(), y.end(), result); - - std::vector<int> expected = boost::assign::list_of - (0)(0)(0)(0); - CPPUNIT_ASSERT_EQUAL(expected, result); - } - - void testComputeLeastCommonSubsequenceMatrix_BothSequencesEmpty() { - std::vector<char> x; - std::vector<char> y; - - std::vector<int> result; - Detail::computeLeastCommonSubsequenceMatrix<std::vector<char>::const_iterator, std::vector<char>::const_iterator, int, std::equal_to<char> >(x.begin(), x.end(), y.begin(), y.end(), result); - - std::vector<int> expected = boost::assign::list_of(0); - CPPUNIT_ASSERT_EQUAL(expected, result); - } - - void testComputeLeastCommonSubsequenceMatrix_NoCommonSequence() { - std::vector<char> x = boost::assign::list_of('a')('b')('c'); - std::vector<char> y = boost::assign::list_of('d')('e')('f')('g'); - - std::vector<int> result; - Detail::computeLeastCommonSubsequenceMatrix<std::vector<char>::const_iterator, std::vector<char>::const_iterator, int, std::equal_to<char> >(x.begin(), x.end(), y.begin(), y.end(), result); - - std::vector<int> expected = boost::assign::list_of - (0)(0)(0)(0) - (0)(0)(0)(0) - (0)(0)(0)(0) - (0)(0)(0)(0) - (0)(0)(0)(0); - CPPUNIT_ASSERT_EQUAL(expected, result); - } - - void testComputeLeastCommonSubsequenceMatrix_SameSequences() { - std::vector<char> x = boost::assign::list_of('a')('b')('c'); - std::vector<char> y = boost::assign::list_of('a')('b')('c'); - - std::vector<int> result; - Detail::computeLeastCommonSubsequenceMatrix<std::vector<char>::const_iterator, std::vector<char>::const_iterator, int, std::equal_to<char> >(x.begin(), x.end(), y.begin(), y.end(), result); - - std::vector<int> expected = boost::assign::list_of - (0)(0)(0)(0) - (0)(1)(1)(1) - (0)(1)(2)(2) - (0)(1)(2)(3); - CPPUNIT_ASSERT_EQUAL(expected, result); - } - - void testComputeIndexDiff_1() { - std::vector<std::string> x = boost::assign::list_of("Arizona")("California")("Delaware")("New Jersey")("Washington"); - std::vector<std::string> y = boost::assign::list_of("Alaska")("Arizona")("California")("Georgia")("New Jersey")("Virginia"); - - std::vector<size_t> updates; - std::vector<size_t> postUpdates; - std::vector<size_t> removes; - std::vector<size_t> inserts; - computeIndexDiff<std::string, std::equal_to<std::string>, IsArizonaOrNewJersey >(x, y, updates, postUpdates, removes, inserts); - - std::vector<size_t> expectedUpdates = boost::assign::list_of(3)(0); - std::vector<size_t> expectedPostUpdates = boost::assign::list_of(4)(1); - std::vector<size_t> expectedRemoves = boost::assign::list_of(4)(2); - std::vector<size_t> expectedInserts = boost::assign::list_of(5)(3)(0); - CPPUNIT_ASSERT_EQUAL(expectedUpdates, updates); - CPPUNIT_ASSERT_EQUAL(expectedPostUpdates, postUpdates); - CPPUNIT_ASSERT_EQUAL(expectedRemoves, removes); - CPPUNIT_ASSERT_EQUAL(expectedInserts, inserts); - } - - void testComputeIndexDiff_2() { - std::vector<char> x = boost::assign::list_of('x')('y'); - std::vector<char> y = boost::assign::list_of('x'); - - std::vector<size_t> updates; - std::vector<size_t> postUpdates; - std::vector<size_t> removes; - std::vector<size_t> inserts; - computeIndexDiff<char, std::equal_to<char>, IsBOrC >(x, y, updates, postUpdates, removes, inserts); - - std::vector<size_t> expectedRemoves = boost::assign::list_of(1); - CPPUNIT_ASSERT(updates.empty()); - CPPUNIT_ASSERT(postUpdates.empty()); - CPPUNIT_ASSERT(inserts.empty()); - CPPUNIT_ASSERT_EQUAL(expectedRemoves, removes); - } - - void testComputeIndexDiff_Sequence1Empty() { - std::vector<char> x; - std::vector<char> y = boost::assign::list_of('a')('b')('c'); - - std::vector<size_t> updates; - std::vector<size_t> postUpdates; - std::vector<size_t> removes; - std::vector<size_t> inserts; - computeIndexDiff<char, std::equal_to<char>, IsBOrC >(x, y, updates, postUpdates, removes, inserts); - - std::vector<size_t> expectedInserts = boost::assign::list_of(2)(1)(0); - CPPUNIT_ASSERT(updates.empty()); - CPPUNIT_ASSERT(postUpdates.empty()); - CPPUNIT_ASSERT(removes.empty()); - CPPUNIT_ASSERT_EQUAL(expectedInserts, inserts); - } - - void testComputeIndexDiff_Sequence2Empty() { - std::vector<char> x = boost::assign::list_of('a')('b')('c'); - std::vector<char> y; - - std::vector<size_t> updates; - std::vector<size_t> postUpdates; - std::vector<size_t> removes; - std::vector<size_t> inserts; - computeIndexDiff<char, std::equal_to<char>, IsBOrC >(x, y, updates, postUpdates, removes, inserts); - - std::vector<size_t> expectedRemoves = boost::assign::list_of(2)(1)(0); - CPPUNIT_ASSERT(updates.empty()); - CPPUNIT_ASSERT(postUpdates.empty()); - CPPUNIT_ASSERT_EQUAL(expectedRemoves, removes); - CPPUNIT_ASSERT(inserts.empty()); - } - - void testComputeIndexDiff_BothSequencesEmpty() { - std::vector<char> x; - std::vector<char> y; - - std::vector<size_t> updates; - std::vector<size_t> postUpdates; - std::vector<size_t> removes; - std::vector<size_t> inserts; - computeIndexDiff<char, std::equal_to<char>, IsBOrC >(x, y, updates, postUpdates, removes, inserts); - - CPPUNIT_ASSERT(updates.empty()); - CPPUNIT_ASSERT(postUpdates.empty()); - CPPUNIT_ASSERT(removes.empty()); - CPPUNIT_ASSERT(inserts.empty()); - } - - void testComputeIndexDiff_NoCommonSequence() { - std::vector<char> x = boost::assign::list_of('a')('b')('c'); - std::vector<char> y = boost::assign::list_of('d')('e')('f')('g'); - - std::vector<size_t> updates; - std::vector<size_t> postUpdates; - std::vector<size_t> removes; - std::vector<size_t> inserts; - computeIndexDiff<char, std::equal_to<char>, IsBOrC >(x, y, updates, postUpdates, removes, inserts); - - std::vector<size_t> expectedRemoves = boost::assign::list_of(2)(1)(0); - std::vector<size_t> expectedInserts = boost::assign::list_of(3)(2)(1)(0); - CPPUNIT_ASSERT(updates.empty()); - CPPUNIT_ASSERT(postUpdates.empty()); - CPPUNIT_ASSERT_EQUAL(expectedRemoves, removes); - CPPUNIT_ASSERT_EQUAL(expectedInserts, inserts); - } - - void testComputeIndexDiff_SameSequences() { - std::vector<char> x = boost::assign::list_of('a')('b')('c'); - std::vector<char> y = boost::assign::list_of('a')('b')('c'); - - std::vector<size_t> updates; - std::vector<size_t> postUpdates; - std::vector<size_t> removes; - std::vector<size_t> inserts; - computeIndexDiff<char, std::equal_to<char>, IsBOrC >(x, y, updates, postUpdates, removes, inserts); - - std::vector<size_t> expectedUpdates = boost::assign::list_of(1)(2); - CPPUNIT_ASSERT_EQUAL(expectedUpdates, updates); - CPPUNIT_ASSERT_EQUAL(expectedUpdates, postUpdates); - CPPUNIT_ASSERT(removes.empty()); - CPPUNIT_ASSERT(inserts.empty()); - } - - void testComputeIndexDiff_CommonPrefixAndSuffix() { - std::vector<char> x = boost::assign::list_of('x')('x')('x')('x')('a')('b')('c')('d')('e')('y')('y')('y'); - std::vector<char> y = boost::assign::list_of('x')('x')('x')('x')('e')('a')('b')('f')('d')('g')('y')('y')('y'); - - std::vector<size_t> updates; - std::vector<size_t> postUpdates; - std::vector<size_t> removes; - std::vector<size_t> inserts; - computeIndexDiff<char, std::equal_to<char>, IsXOrY >(x, y, updates, postUpdates, removes, inserts); - - std::vector<size_t> expectedUpdates = boost::assign::list_of(0)(1)(2)(3)(11)(10)(9); - std::vector<size_t> expectedPostUpdates = boost::assign::list_of(0)(1)(2)(3)(12)(11)(10); - std::vector<size_t> expectedRemoves = boost::assign::list_of(8)(6); - std::vector<size_t> expectedInserts = boost::assign::list_of(9)(7)(4); - CPPUNIT_ASSERT_EQUAL(expectedUpdates, updates); - CPPUNIT_ASSERT_EQUAL(expectedPostUpdates, postUpdates); - CPPUNIT_ASSERT_EQUAL(expectedRemoves, removes); - CPPUNIT_ASSERT_EQUAL(expectedInserts, inserts); - } + CPPUNIT_TEST_SUITE(LeastCommonSubsequenceTest); + CPPUNIT_TEST(testComputeLeastCommonSubsequenceMatrix_1); + CPPUNIT_TEST(testComputeLeastCommonSubsequenceMatrix_2); + CPPUNIT_TEST(testComputeLeastCommonSubsequenceMatrix_Sequence1Empty); + CPPUNIT_TEST(testComputeLeastCommonSubsequenceMatrix_Sequence2Empty); + CPPUNIT_TEST(testComputeLeastCommonSubsequenceMatrix_BothSequencesEmpty); + CPPUNIT_TEST(testComputeLeastCommonSubsequenceMatrix_NoCommonSequence); + CPPUNIT_TEST(testComputeLeastCommonSubsequenceMatrix_SameSequences); + CPPUNIT_TEST(testComputeIndexDiff_1); + CPPUNIT_TEST(testComputeIndexDiff_2); + CPPUNIT_TEST(testComputeIndexDiff_Sequence1Empty); + CPPUNIT_TEST(testComputeIndexDiff_Sequence2Empty); + CPPUNIT_TEST(testComputeIndexDiff_BothSequencesEmpty); + CPPUNIT_TEST(testComputeIndexDiff_NoCommonSequence); + CPPUNIT_TEST(testComputeIndexDiff_SameSequences); + CPPUNIT_TEST(testComputeIndexDiff_CommonPrefixAndSuffix); + CPPUNIT_TEST_SUITE_END(); + + public: + void testComputeLeastCommonSubsequenceMatrix_1() { + std::vector<char> x = boost::assign::list_of('x')('m')('j')('y')('a')('u')('z'); + std::vector<char> y = boost::assign::list_of('m')('z')('j')('a')('w')('x')('u'); + + std::vector<int> result; + Detail::computeLeastCommonSubsequenceMatrix<std::vector<char>::const_iterator, std::vector<char>::const_iterator, int, std::equal_to<char> >(x.begin(), x.end(), y.begin(), y.end(), result); + + std::vector<int> expected = boost::assign::list_of + (0)(0)(0)(0)(0)(0)(0)(0) + (0)(0)(1)(1)(1)(1)(1)(1) + (0)(0)(1)(1)(1)(1)(1)(2) + (0)(0)(1)(2)(2)(2)(2)(2) + (0)(0)(1)(2)(2)(3)(3)(3) + (0)(0)(1)(2)(2)(3)(3)(3) + (0)(1)(1)(2)(2)(3)(3)(3) + (0)(1)(1)(2)(2)(3)(4)(4); + CPPUNIT_ASSERT_EQUAL(expected, result); + } + + void testComputeLeastCommonSubsequenceMatrix_2() { + std::vector<char> x = boost::assign::list_of('x')('x')('x')('m')('j')('y')('a')('u')('z'); + std::vector<char> y = boost::assign::list_of('m')('z')('j')('a')('w')('x')('u'); + + std::vector<int> result; + Detail::computeLeastCommonSubsequenceMatrix<std::vector<char>::const_iterator, std::vector<char>::const_iterator, int, std::equal_to<char> >(x.begin(), x.end(), y.begin(), y.end(), result); + + std::vector<int> expected = boost::assign::list_of + (0)(0)(0)(0)(0)(0)(0)(0)(0)(0) + (0)(0)(0)(0)(1)(1)(1)(1)(1)(1) + (0)(0)(0)(0)(1)(1)(1)(1)(1)(2) + (0)(0)(0)(0)(1)(2)(2)(2)(2)(2) + (0)(0)(0)(0)(1)(2)(2)(3)(3)(3) + (0)(0)(0)(0)(1)(2)(2)(3)(3)(3) + (0)(1)(1)(1)(1)(2)(2)(3)(3)(3) + (0)(1)(1)(1)(1)(2)(2)(3)(4)(4); + CPPUNIT_ASSERT_EQUAL(expected, result); + } + + void testComputeLeastCommonSubsequenceMatrix_Sequence1Empty() { + std::vector<char> x; + std::vector<char> y = boost::assign::list_of('a')('b')('c'); + + std::vector<int> result; + Detail::computeLeastCommonSubsequenceMatrix<std::vector<char>::const_iterator, std::vector<char>::const_iterator, int, std::equal_to<char> >(x.begin(), x.end(), y.begin(), y.end(), result); + + std::vector<int> expected = boost::assign::list_of + (0) + (0) + (0) + (0); + CPPUNIT_ASSERT_EQUAL(expected, result); + } + + void testComputeLeastCommonSubsequenceMatrix_Sequence2Empty() { + std::vector<char> x = boost::assign::list_of('a')('b')('c'); + std::vector<char> y; + + std::vector<int> result; + Detail::computeLeastCommonSubsequenceMatrix<std::vector<char>::const_iterator, std::vector<char>::const_iterator, int, std::equal_to<char> >(x.begin(), x.end(), y.begin(), y.end(), result); + + std::vector<int> expected = boost::assign::list_of + (0)(0)(0)(0); + CPPUNIT_ASSERT_EQUAL(expected, result); + } + + void testComputeLeastCommonSubsequenceMatrix_BothSequencesEmpty() { + std::vector<char> x; + std::vector<char> y; + + std::vector<int> result; + Detail::computeLeastCommonSubsequenceMatrix<std::vector<char>::const_iterator, std::vector<char>::const_iterator, int, std::equal_to<char> >(x.begin(), x.end(), y.begin(), y.end(), result); + + std::vector<int> expected = boost::assign::list_of(0); + CPPUNIT_ASSERT_EQUAL(expected, result); + } + + void testComputeLeastCommonSubsequenceMatrix_NoCommonSequence() { + std::vector<char> x = boost::assign::list_of('a')('b')('c'); + std::vector<char> y = boost::assign::list_of('d')('e')('f')('g'); + + std::vector<int> result; + Detail::computeLeastCommonSubsequenceMatrix<std::vector<char>::const_iterator, std::vector<char>::const_iterator, int, std::equal_to<char> >(x.begin(), x.end(), y.begin(), y.end(), result); + + std::vector<int> expected = boost::assign::list_of + (0)(0)(0)(0) + (0)(0)(0)(0) + (0)(0)(0)(0) + (0)(0)(0)(0) + (0)(0)(0)(0); + CPPUNIT_ASSERT_EQUAL(expected, result); + } + + void testComputeLeastCommonSubsequenceMatrix_SameSequences() { + std::vector<char> x = boost::assign::list_of('a')('b')('c'); + std::vector<char> y = boost::assign::list_of('a')('b')('c'); + + std::vector<int> result; + Detail::computeLeastCommonSubsequenceMatrix<std::vector<char>::const_iterator, std::vector<char>::const_iterator, int, std::equal_to<char> >(x.begin(), x.end(), y.begin(), y.end(), result); + + std::vector<int> expected = boost::assign::list_of + (0)(0)(0)(0) + (0)(1)(1)(1) + (0)(1)(2)(2) + (0)(1)(2)(3); + CPPUNIT_ASSERT_EQUAL(expected, result); + } + + void testComputeIndexDiff_1() { + std::vector<std::string> x = boost::assign::list_of("Arizona")("California")("Delaware")("New Jersey")("Washington"); + std::vector<std::string> y = boost::assign::list_of("Alaska")("Arizona")("California")("Georgia")("New Jersey")("Virginia"); + + std::vector<size_t> updates; + std::vector<size_t> postUpdates; + std::vector<size_t> removes; + std::vector<size_t> inserts; + computeIndexDiff<std::string, std::equal_to<std::string>, IsArizonaOrNewJersey >(x, y, updates, postUpdates, removes, inserts); + + std::vector<size_t> expectedUpdates = boost::assign::list_of(3)(0); + std::vector<size_t> expectedPostUpdates = boost::assign::list_of(4)(1); + std::vector<size_t> expectedRemoves = boost::assign::list_of(4)(2); + std::vector<size_t> expectedInserts = boost::assign::list_of(5)(3)(0); + CPPUNIT_ASSERT_EQUAL(expectedUpdates, updates); + CPPUNIT_ASSERT_EQUAL(expectedPostUpdates, postUpdates); + CPPUNIT_ASSERT_EQUAL(expectedRemoves, removes); + CPPUNIT_ASSERT_EQUAL(expectedInserts, inserts); + } + + void testComputeIndexDiff_2() { + std::vector<char> x = boost::assign::list_of('x')('y'); + std::vector<char> y = boost::assign::list_of('x'); + + std::vector<size_t> updates; + std::vector<size_t> postUpdates; + std::vector<size_t> removes; + std::vector<size_t> inserts; + computeIndexDiff<char, std::equal_to<char>, IsBOrC >(x, y, updates, postUpdates, removes, inserts); + + std::vector<size_t> expectedRemoves = boost::assign::list_of(1); + CPPUNIT_ASSERT(updates.empty()); + CPPUNIT_ASSERT(postUpdates.empty()); + CPPUNIT_ASSERT(inserts.empty()); + CPPUNIT_ASSERT_EQUAL(expectedRemoves, removes); + } + + void testComputeIndexDiff_Sequence1Empty() { + std::vector<char> x; + std::vector<char> y = boost::assign::list_of('a')('b')('c'); + + std::vector<size_t> updates; + std::vector<size_t> postUpdates; + std::vector<size_t> removes; + std::vector<size_t> inserts; + computeIndexDiff<char, std::equal_to<char>, IsBOrC >(x, y, updates, postUpdates, removes, inserts); + + std::vector<size_t> expectedInserts = boost::assign::list_of(2)(1)(0); + CPPUNIT_ASSERT(updates.empty()); + CPPUNIT_ASSERT(postUpdates.empty()); + CPPUNIT_ASSERT(removes.empty()); + CPPUNIT_ASSERT_EQUAL(expectedInserts, inserts); + } + + void testComputeIndexDiff_Sequence2Empty() { + std::vector<char> x = boost::assign::list_of('a')('b')('c'); + std::vector<char> y; + + std::vector<size_t> updates; + std::vector<size_t> postUpdates; + std::vector<size_t> removes; + std::vector<size_t> inserts; + computeIndexDiff<char, std::equal_to<char>, IsBOrC >(x, y, updates, postUpdates, removes, inserts); + + std::vector<size_t> expectedRemoves = boost::assign::list_of(2)(1)(0); + CPPUNIT_ASSERT(updates.empty()); + CPPUNIT_ASSERT(postUpdates.empty()); + CPPUNIT_ASSERT_EQUAL(expectedRemoves, removes); + CPPUNIT_ASSERT(inserts.empty()); + } + + void testComputeIndexDiff_BothSequencesEmpty() { + std::vector<char> x; + std::vector<char> y; + + std::vector<size_t> updates; + std::vector<size_t> postUpdates; + std::vector<size_t> removes; + std::vector<size_t> inserts; + computeIndexDiff<char, std::equal_to<char>, IsBOrC >(x, y, updates, postUpdates, removes, inserts); + + CPPUNIT_ASSERT(updates.empty()); + CPPUNIT_ASSERT(postUpdates.empty()); + CPPUNIT_ASSERT(removes.empty()); + CPPUNIT_ASSERT(inserts.empty()); + } + + void testComputeIndexDiff_NoCommonSequence() { + std::vector<char> x = boost::assign::list_of('a')('b')('c'); + std::vector<char> y = boost::assign::list_of('d')('e')('f')('g'); + + std::vector<size_t> updates; + std::vector<size_t> postUpdates; + std::vector<size_t> removes; + std::vector<size_t> inserts; + computeIndexDiff<char, std::equal_to<char>, IsBOrC >(x, y, updates, postUpdates, removes, inserts); + + std::vector<size_t> expectedRemoves = boost::assign::list_of(2)(1)(0); + std::vector<size_t> expectedInserts = boost::assign::list_of(3)(2)(1)(0); + CPPUNIT_ASSERT(updates.empty()); + CPPUNIT_ASSERT(postUpdates.empty()); + CPPUNIT_ASSERT_EQUAL(expectedRemoves, removes); + CPPUNIT_ASSERT_EQUAL(expectedInserts, inserts); + } + + void testComputeIndexDiff_SameSequences() { + std::vector<char> x = boost::assign::list_of('a')('b')('c'); + std::vector<char> y = boost::assign::list_of('a')('b')('c'); + + std::vector<size_t> updates; + std::vector<size_t> postUpdates; + std::vector<size_t> removes; + std::vector<size_t> inserts; + computeIndexDiff<char, std::equal_to<char>, IsBOrC >(x, y, updates, postUpdates, removes, inserts); + + std::vector<size_t> expectedUpdates = boost::assign::list_of(1)(2); + CPPUNIT_ASSERT_EQUAL(expectedUpdates, updates); + CPPUNIT_ASSERT_EQUAL(expectedUpdates, postUpdates); + CPPUNIT_ASSERT(removes.empty()); + CPPUNIT_ASSERT(inserts.empty()); + } + + void testComputeIndexDiff_CommonPrefixAndSuffix() { + std::vector<char> x = boost::assign::list_of('x')('x')('x')('x')('a')('b')('c')('d')('e')('y')('y')('y'); + std::vector<char> y = boost::assign::list_of('x')('x')('x')('x')('e')('a')('b')('f')('d')('g')('y')('y')('y'); + + std::vector<size_t> updates; + std::vector<size_t> postUpdates; + std::vector<size_t> removes; + std::vector<size_t> inserts; + computeIndexDiff<char, std::equal_to<char>, IsXOrY >(x, y, updates, postUpdates, removes, inserts); + + std::vector<size_t> expectedUpdates = boost::assign::list_of(0)(1)(2)(3)(11)(10)(9); + std::vector<size_t> expectedPostUpdates = boost::assign::list_of(0)(1)(2)(3)(12)(11)(10); + std::vector<size_t> expectedRemoves = boost::assign::list_of(8)(6); + std::vector<size_t> expectedInserts = boost::assign::list_of(9)(7)(4); + CPPUNIT_ASSERT_EQUAL(expectedUpdates, updates); + CPPUNIT_ASSERT_EQUAL(expectedPostUpdates, postUpdates); + CPPUNIT_ASSERT_EQUAL(expectedRemoves, removes); + CPPUNIT_ASSERT_EQUAL(expectedInserts, inserts); + } }; CPPUNIT_TEST_SUITE_REGISTRATION(LeastCommonSubsequenceTest); diff --git a/Swift/Controllers/Roster/UnitTest/RosterControllerTest.cpp b/Swift/Controllers/Roster/UnitTest/RosterControllerTest.cpp index b0034e6..0a9ea18 100644 --- a/Swift/Controllers/Roster/UnitTest/RosterControllerTest.cpp +++ b/Swift/Controllers/Roster/UnitTest/RosterControllerTest.cpp @@ -1,346 +1,487 @@ - /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ +#include <memory> + #include <cppunit/extensions/HelperMacros.h> #include <cppunit/extensions/TestFactoryRegistry.h> -#include <Swiften/Base/foreach.h> -#include "Swift/Controllers/Roster/RosterController.h" -#include "Swift/Controllers/UnitTest/MockMainWindowFactory.h" -// #include "Swiften/Elements/Payload.h" -// #include "Swiften/Elements/RosterItemPayload.h" -// #include "Swiften/Elements/RosterPayload.h" -#include "Swiften/Queries/DummyIQChannel.h" -#include "Swiften/Client/DummyStanzaChannel.h" -#include "Swiften/Queries/IQRouter.h" -#include "Swiften/Roster/XMPPRosterImpl.h" -#include "Swift/Controllers/Roster/Roster.h" -#include "Swift/Controllers/Roster/GroupRosterItem.h" -#include "Swift/Controllers/Roster/ContactRosterItem.h" -#include "Swift/Controllers/Settings/DummySettingsProvider.h" -#include "Swiften/Avatars/NullAvatarManager.h" -#include "Swift/Controllers/XMPPEvents/EventController.h" -#include "Swiften/Presence/PresenceOracle.h" -#include "Swiften/Presence/SubscriptionManager.h" -#include "Swiften/Client/NickResolver.h" -#include "Swift/Controllers/UIEvents/UIEventStream.h" -#include "Swift/Controllers/UIEvents/RenameRosterItemUIEvent.h" -#include "Swiften/MUC/MUCRegistry.h" +#include <Swiften/Avatars/NullAvatarManager.h> +#include <Swiften/Base/Algorithm.h> +#include <Swiften/Client/ClientBlockListManager.h> #include <Swiften/Client/DummyNickManager.h> -#include <Swiften/Disco/EntityCapsManager.h> +#include <Swiften/Client/DummyStanzaChannel.h> +#include <Swiften/Client/MemoryStorages.h> +#include <Swiften/Client/NickResolver.h> +#include <Swiften/Crypto/CryptoProvider.h> +#include <Swiften/Crypto/PlatformCryptoProvider.h> +#include <Swiften/Disco/CapsInfoGenerator.h> +#include <Swiften/Disco/CapsManager.h> #include <Swiften/Disco/CapsProvider.h> -#include <Swiften/Jingle/JingleSessionManager.h> -#include <Swiften/FileTransfer/UnitTest/DummyFileTransferManager.h> -#include <Swiften/Base/Algorithm.h> +#include <Swiften/Disco/ClientDiscoManager.h> +#include <Swiften/Disco/EntityCapsManager.h> #include <Swiften/EventLoop/DummyEventLoop.h> -#include <Swiften/Client/ClientBlockListManager.h> +#include <Swiften/Jingle/JingleSessionManager.h> +#include <Swiften/MUC/MUCRegistry.h> +#include <Swiften/Presence/PresenceOracle.h> +#include <Swiften/Presence/SubscriptionManager.h> +#include <Swiften/Queries/DummyIQChannel.h> +#include <Swiften/Queries/IQRouter.h> +#include <Swiften/Roster/XMPPRosterImpl.h> +#include <Swiften/VCards/VCardManager.h> +#include <Swiften/VCards/VCardMemoryStorage.h> -using namespace Swift; +#include <Swift/Controllers/Chat/Chattables.h> +#include <Swift/Controllers/Roster/ContactRosterItem.h> +#include <Swift/Controllers/Roster/GroupRosterItem.h> +#include <Swift/Controllers/Roster/Roster.h> +#include <Swift/Controllers/Roster/RosterController.h> +#include <Swift/Controllers/Settings/DummySettingsProvider.h> +#include <Swift/Controllers/UIEvents/RenameRosterItemUIEvent.h> +#include <Swift/Controllers/UIEvents/UIEventStream.h> +#include <Swift/Controllers/UnitTest/MockMainWindowFactory.h> +#include <Swift/Controllers/XMPPEvents/EventController.h> -#define CHILDREN mainWindow_->roster->getRoot()->getChildren() +using namespace Swift; class DummyCapsProvider : public CapsProvider { - DiscoInfo::ref getCaps(const std::string&) const {return DiscoInfo::ref(new DiscoInfo());} + DiscoInfo::ref getCaps(const std::string&) const {return DiscoInfo::ref(new DiscoInfo());} }; class RosterControllerTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(RosterControllerTest); - CPPUNIT_TEST(testAdd); - CPPUNIT_TEST(testAddSubscription); - CPPUNIT_TEST(testReceiveRename); - CPPUNIT_TEST(testReceiveRegroup); - CPPUNIT_TEST(testSendRename); - CPPUNIT_TEST(testPresence); - CPPUNIT_TEST(testHighestPresence); - CPPUNIT_TEST(testNotHighestPresence); - CPPUNIT_TEST(testUnavailablePresence); - CPPUNIT_TEST_SUITE_END(); - - public: - void setUp() { - jid_ = JID("testjid@swift.im/swift"); - xmppRoster_ = new XMPPRosterImpl(); - avatarManager_ = new NullAvatarManager(); - mainWindowFactory_ = new MockMainWindowFactory(); - mucRegistry_ = new MUCRegistry(); - nickResolver_ = new NickResolver(jid_.toBare(), xmppRoster_, NULL, mucRegistry_); - channel_ = new DummyIQChannel(); - router_ = new IQRouter(channel_); - stanzaChannel_ = new DummyStanzaChannel(); - presenceOracle_ = new PresenceOracle(stanzaChannel_); - subscriptionManager_ = new SubscriptionManager(stanzaChannel_); - eventController_ = new EventController(); - uiEventStream_ = new UIEventStream(); - settings_ = new DummySettingsProvider(); - nickManager_ = new DummyNickManager(); - capsProvider_ = new DummyCapsProvider(); - entityCapsManager_ = new EntityCapsManager(capsProvider_, stanzaChannel_); - jingleSessionManager_ = new JingleSessionManager(router_); - - ftManager_ = new DummyFileTransferManager(); - ftOverview_ = new FileTransferOverview(ftManager_); - clientBlockListManager_ = new ClientBlockListManager(router_); - rosterController_ = new RosterController(jid_, xmppRoster_, avatarManager_, mainWindowFactory_, nickManager_, nickResolver_, presenceOracle_, subscriptionManager_, eventController_, uiEventStream_, router_, settings_, entityCapsManager_, ftOverview_, clientBlockListManager_); - mainWindow_ = mainWindowFactory_->last; - } - - void tearDown() { - delete rosterController_; - delete clientBlockListManager_; - delete ftManager_; - delete jingleSessionManager_; - - delete nickManager_; - delete nickResolver_; - delete mucRegistry_; - delete mainWindowFactory_; - delete avatarManager_; - delete router_; - delete channel_; - delete eventController_; - delete subscriptionManager_; - delete presenceOracle_; - delete stanzaChannel_; - delete uiEventStream_; - delete settings_; - delete xmppRoster_; - } - - GroupRosterItem* groupChild(size_t i) { - return dynamic_cast<GroupRosterItem*>(CHILDREN[i]); - } - - JID withResource(const JID& jid, const std::string& resource) { - return JID(jid.toBare().toString() + "/" + resource); - } - - void testPresence() { - std::vector<std::string> groups; - groups.push_back("testGroup1"); - groups.push_back("testGroup2"); - JID from("test@testdomain.com"); - xmppRoster_->addContact(from, "name", groups, RosterItemPayload::Both); - Presence::ref presence(new Presence()); - presence->setFrom(withResource(from, "bob")); - presence->setPriority(2); - presence->setStatus("So totally here"); - stanzaChannel_->onPresenceReceived(presence); - ContactRosterItem* item = dynamic_cast<ContactRosterItem*>(dynamic_cast<GroupRosterItem*>(CHILDREN[0])->getChildren()[0]); - CPPUNIT_ASSERT(item); - CPPUNIT_ASSERT_EQUAL(presence->getStatus(), item->getStatusText()); - ContactRosterItem* item2 = dynamic_cast<ContactRosterItem*>(dynamic_cast<GroupRosterItem*>(CHILDREN[1])->getChildren()[0]); - CPPUNIT_ASSERT(item2); - CPPUNIT_ASSERT_EQUAL(presence->getStatus(), item2->getStatusText()); - - } - - void testHighestPresence() { - std::vector<std::string> groups; - groups.push_back("testGroup1"); - JID from("test@testdomain.com"); - xmppRoster_->addContact(from, "name", groups, RosterItemPayload::Both); - Presence::ref lowPresence(new Presence()); - lowPresence->setFrom(withResource(from, "bob")); - lowPresence->setPriority(2); - lowPresence->setStatus("Not here"); - Presence::ref highPresence(new Presence()); - highPresence->setFrom(withResource(from, "bert")); - highPresence->setPriority(10); - highPresence->setStatus("So totally here"); - stanzaChannel_->onPresenceReceived(lowPresence); - stanzaChannel_->onPresenceReceived(highPresence); - ContactRosterItem* item = dynamic_cast<ContactRosterItem*>(dynamic_cast<GroupRosterItem*>(CHILDREN[0])->getChildren()[0]); - CPPUNIT_ASSERT(item); - CPPUNIT_ASSERT_EQUAL(highPresence->getStatus(), item->getStatusText()); - } - - void testNotHighestPresence() { - std::vector<std::string> groups; - groups.push_back("testGroup1"); - JID from("test@testdomain.com"); - xmppRoster_->addContact(from, "name", groups, RosterItemPayload::Both); - Presence::ref lowPresence(new Presence()); - lowPresence->setFrom(withResource(from, "bob")); - lowPresence->setPriority(2); - lowPresence->setStatus("Not here"); - Presence::ref highPresence(new Presence()); - highPresence->setFrom(withResource(from, "bert")); - highPresence->setPriority(10); - highPresence->setStatus("So totally here"); - stanzaChannel_->onPresenceReceived(highPresence); - stanzaChannel_->onPresenceReceived(lowPresence); - ContactRosterItem* item = dynamic_cast<ContactRosterItem*>(dynamic_cast<GroupRosterItem*>(CHILDREN[0])->getChildren()[0]); - CPPUNIT_ASSERT(item); - CPPUNIT_ASSERT_EQUAL(highPresence->getStatus(), item->getStatusText()); - } - - void testUnavailablePresence() { - std::vector<std::string> groups; - groups.push_back("testGroup1"); - JID from("test@testdomain.com"); - xmppRoster_->addContact(from, "name", groups, RosterItemPayload::Both); - Presence::ref lowPresence(new Presence()); - lowPresence->setFrom(withResource(from, "bob")); - lowPresence->setPriority(2); - lowPresence->setStatus("Not here"); - Presence::ref highPresence(new Presence()); - highPresence->setFrom(withResource(from, "bert")); - highPresence->setPriority(10); - highPresence->setStatus("So totally here"); - Presence::ref highPresenceOffline(new Presence()); - highPresenceOffline->setFrom(withResource(from, "bert")); - highPresenceOffline->setType(Presence::Unavailable); - Presence::ref lowPresenceOffline(new Presence()); - lowPresenceOffline->setFrom(withResource(from, "bob")); - lowPresenceOffline->setStatus("Signing out"); - lowPresenceOffline->setType(Presence::Unavailable); - stanzaChannel_->onPresenceReceived(lowPresence); - stanzaChannel_->onPresenceReceived(highPresence); - stanzaChannel_->onPresenceReceived(highPresenceOffline); - ContactRosterItem* item = dynamic_cast<ContactRosterItem*>(dynamic_cast<GroupRosterItem*>(CHILDREN[0])->getChildren()[0]); - CPPUNIT_ASSERT(item); - /* A verification that if the test fails, it's the RosterController, not the PresenceOracle. */ - Presence::ref high = presenceOracle_->getHighestPriorityPresence(from); - CPPUNIT_ASSERT_EQUAL(Presence::Available, high->getType()); - CPPUNIT_ASSERT_EQUAL(lowPresence->getStatus(), high->getStatus()); - CPPUNIT_ASSERT_EQUAL(StatusShow::Online, item->getStatusShow()); - CPPUNIT_ASSERT_EQUAL(lowPresence->getStatus(), item->getStatusText()); - stanzaChannel_->onPresenceReceived(lowPresenceOffline); - item = dynamic_cast<ContactRosterItem*>(dynamic_cast<GroupRosterItem*>(CHILDREN[0])->getChildren()[0]); - CPPUNIT_ASSERT(item); - /* A verification that if the test fails, it's the RosterController, not the PresenceOracle. */ - high = presenceOracle_->getHighestPriorityPresence(from); - CPPUNIT_ASSERT_EQUAL(Presence::Unavailable, high->getType()); - CPPUNIT_ASSERT_EQUAL(lowPresenceOffline->getStatus(), high->getStatus()); - CPPUNIT_ASSERT_EQUAL(StatusShow::None, item->getStatusShow()); - CPPUNIT_ASSERT_EQUAL(lowPresenceOffline->getStatus(), item->getStatusText()); - } - - void testAdd() { - std::vector<std::string> groups; - groups.push_back("testGroup1"); - groups.push_back("testGroup2"); - xmppRoster_->addContact(JID("test@testdomain.com/bob"), "name", groups, RosterItemPayload::Both); - - CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(CHILDREN.size())); - //CPPUNIT_ASSERT_EQUAL(std::string("Bob"), xmppRoster_->getNameForJID(JID("foo@bar.com"))); - } - - void testAddSubscription() { - std::vector<std::string> groups; - JID jid("test@testdomain.com"); - xmppRoster_->addContact(jid, "name", groups, RosterItemPayload::None); - - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(CHILDREN.size())); - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(groupChild(0)->getChildren().size())); - xmppRoster_->addContact(jid, "name", groups, RosterItemPayload::To); - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(CHILDREN.size())); - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(groupChild(0)->getChildren().size())); - - xmppRoster_->addContact(jid, "name", groups, RosterItemPayload::Both); - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(CHILDREN.size())); - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(groupChild(0)->getChildren().size())); - - } - - void testReceiveRename() { - std::vector<std::string> groups; - JID jid("test@testdomain.com"); - xmppRoster_->addContact(jid, "name", groups, RosterItemPayload::Both); - - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(CHILDREN.size())); - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(groupChild(0)->getChildren().size())); - CPPUNIT_ASSERT_EQUAL(std::string("name"), groupChild(0)->getChildren()[0]->getDisplayName()); - xmppRoster_->addContact(jid, "NewName", groups, RosterItemPayload::Both); - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(CHILDREN.size())); - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(groupChild(0)->getChildren().size())); - CPPUNIT_ASSERT_EQUAL(std::string("NewName"), groupChild(0)->getChildren()[0]->getDisplayName()); - } - - void testReceiveRegroup() { - std::vector<std::string> oldGroups; - std::vector<std::string> newGroups; - newGroups.push_back("A Group"); - std::vector<std::string> newestGroups; - newestGroups.push_back("Best Group"); - JID jid("test@testdomain.com"); - xmppRoster_->addContact(jid, "", oldGroups, RosterItemPayload::Both); - - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(CHILDREN.size())); - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(groupChild(0)->getChildren().size())); - CPPUNIT_ASSERT_EQUAL(jid.toString(), groupChild(0)->getChildren()[0]->getDisplayName()); - - xmppRoster_->addContact(jid, "new name", newGroups, RosterItemPayload::Both); - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(CHILDREN.size())); - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(groupChild(0)->getChildren().size())); - CPPUNIT_ASSERT_EQUAL(std::string("new name"), groupChild(0)->getChildren()[0]->getDisplayName()); - CPPUNIT_ASSERT_EQUAL(std::string("A Group"), groupChild(0)->getDisplayName()); - - xmppRoster_->addContact(jid, "new name", newestGroups, RosterItemPayload::Both); - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(CHILDREN.size())); - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(groupChild(0)->getChildren().size())); - CPPUNIT_ASSERT_EQUAL(std::string("new name"), groupChild(0)->getChildren()[0]->getDisplayName()); - CPPUNIT_ASSERT_EQUAL(std::string("Best Group"), groupChild(0)->getDisplayName()); - } - - void testSendRename() { - JID jid("testling@wonderland.lit"); - std::vector<std::string> groups; - groups.push_back("Friends"); - groups.push_back("Enemies"); - xmppRoster_->addContact(jid, "Bob", groups, RosterItemPayload::From); - CPPUNIT_ASSERT_EQUAL(groups.size(), xmppRoster_->getGroupsForJID(jid).size()); - uiEventStream_->send(boost::shared_ptr<UIEvent>(new RenameRosterItemUIEvent(jid, "Robert"))); - CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), channel_->iqs_.size()); - CPPUNIT_ASSERT_EQUAL(IQ::Set, channel_->iqs_[0]->getType()); - boost::shared_ptr<RosterPayload> payload = channel_->iqs_[0]->getPayload<RosterPayload>(); - CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), payload->getItems().size()); - RosterItemPayload item = payload->getItems()[0]; - CPPUNIT_ASSERT_EQUAL(jid, item.getJID()); - CPPUNIT_ASSERT_EQUAL(std::string("Robert"), item.getName()); - - CPPUNIT_ASSERT_EQUAL(groups.size(), item.getGroups().size()); - assertVectorsEqual(groups, item.getGroups(), __LINE__); - } - - void assertVectorsEqual(const std::vector<std::string>& v1, const std::vector<std::string>& v2, int line) { - foreach (const std::string& entry, v1) { - if (std::find(v2.begin(), v2.end(), entry) == v2.end()) { - std::stringstream stream; - stream << "Couldn't find " << entry << " in v2 (line " << line << ")"; - CPPUNIT_FAIL(stream.str()); - } - } - } - - private: - JID jid_; - XMPPRosterImpl* xmppRoster_; - MUCRegistry* mucRegistry_; - AvatarManager* avatarManager_; - MockMainWindowFactory* mainWindowFactory_; - NickManager* nickManager_; - NickResolver* nickResolver_; - RosterController* rosterController_; - DummyIQChannel* channel_; - DummyStanzaChannel* stanzaChannel_; - IQRouter* router_; - PresenceOracle* presenceOracle_; - SubscriptionManager* subscriptionManager_; - EventController* eventController_; - UIEventStream* uiEventStream_; - MockMainWindow* mainWindow_; - DummySettingsProvider* settings_; - DummyCapsProvider* capsProvider_; - EntityCapsManager* entityCapsManager_; - JingleSessionManager* jingleSessionManager_; - FileTransferManager* ftManager_; - FileTransferOverview* ftOverview_; - ClientBlockListManager* clientBlockListManager_; + CPPUNIT_TEST_SUITE(RosterControllerTest); + CPPUNIT_TEST(testAdd); + CPPUNIT_TEST(testAddSubscription); + CPPUNIT_TEST(testReceiveRename); + CPPUNIT_TEST(testReceiveRegroup); + CPPUNIT_TEST(testSendRename); + CPPUNIT_TEST(testPresence); + CPPUNIT_TEST(testHighestPresence); + CPPUNIT_TEST(testNotHighestPresence); + CPPUNIT_TEST(testUnavailablePresence); + CPPUNIT_TEST(testRemoveResultsInUnavailablePresence); + CPPUNIT_TEST(testOwnContactInRosterPresence); + CPPUNIT_TEST(testMultiResourceFileTransferFeature); + //FIXME: All needs rewriting for new roster + CPPUNIT_TEST_SUITE_END(); + + public: + void setUp() { + jid_ = JID("testjid@swift.im/swift"); + xmppRoster_ = new XMPPRosterImpl(); + avatarManager_ = new NullAvatarManager(); + mainWindowFactory_ = new MockMainWindowFactory(); + mucRegistry_ = new MUCRegistry(); + crypto_ = PlatformCryptoProvider::create(); + storages_ = std::make_unique<MemoryStorages>(crypto_); + nickResolver_ = new NickResolver(jid_.toBare(), xmppRoster_, nullptr, mucRegistry_); + channel_ = new DummyIQChannel(); + router_ = new IQRouter(channel_); + stanzaChannel_ = new DummyStanzaChannel(); + presenceOracle_ = new PresenceOracle(stanzaChannel_, xmppRoster_); + subscriptionManager_ = new SubscriptionManager(stanzaChannel_); + eventController_ = new EventController(); + uiEventStream_ = new UIEventStream(); + settings_ = new DummySettingsProvider(); + nickManager_ = new DummyNickManager(); + capsManager_ = std::make_unique<CapsManager>(storages_->getCapsStorage(), stanzaChannel_, router_, crypto_); + entityCapsManager_ = new EntityCapsManager(capsManager_.get(), stanzaChannel_); + jingleSessionManager_ = new JingleSessionManager(router_); + + clientBlockListManager_ = new ClientBlockListManager(router_); + vcardStorage_ = new VCardMemoryStorage(crypto_); + vcardManager_ = new VCardManager(jid_, router_, vcardStorage_); + chattables_ = std::make_unique<Chattables>(); + rosterController_ = new RosterController(jid_, xmppRoster_, avatarManager_, mainWindowFactory_, nickManager_, nickResolver_, presenceOracle_, subscriptionManager_, eventController_, uiEventStream_, router_, settings_, entityCapsManager_, clientBlockListManager_, vcardManager_, *chattables_); + mainWindow_ = mainWindowFactory_->last; + capsInfoGenerator_ = std::make_unique<CapsInfoGenerator>("", crypto_); + } + + void tearDown() { + delete rosterController_; + delete vcardManager_; + delete vcardStorage_; + delete crypto_; + delete clientBlockListManager_; + delete jingleSessionManager_; + delete entityCapsManager_; + delete nickManager_; + delete nickResolver_; + delete mucRegistry_; + delete mainWindowFactory_; + delete avatarManager_; + delete router_; + delete channel_; + delete eventController_; + delete subscriptionManager_; + delete presenceOracle_; + delete stanzaChannel_; + delete uiEventStream_; + delete settings_; + delete xmppRoster_; + } + + GroupRosterItem* groupChild(size_t i) { + return dynamic_cast<GroupRosterItem*>(getUIRosterChildren()[i]); + } + + JID withResource(const JID& jid, const std::string& resource) { + return JID(jid.toBare().toString() + "/" + resource); + } + + void testPresence() { + std::vector<std::string> groups; + groups.push_back("testGroup1"); + groups.push_back("testGroup2"); + JID from("test@testdomain.com"); + xmppRoster_->addContact(from, "name", groups, RosterItemPayload::Both); + Presence::ref presence(new Presence()); + presence->setFrom(withResource(from, "bob")); + presence->setPriority(2); + presence->setStatus("So totally here"); + stanzaChannel_->onPresenceReceived(presence); + ContactRosterItem* item = dynamic_cast<ContactRosterItem*>(dynamic_cast<GroupRosterItem*>(getUIRosterChildren()[0])->getChildren()[0]); + CPPUNIT_ASSERT(item); + CPPUNIT_ASSERT_EQUAL(presence->getStatus(), item->getStatusText()); + ContactRosterItem* item2 = dynamic_cast<ContactRosterItem*>(dynamic_cast<GroupRosterItem*>(getUIRosterChildren()[1])->getChildren()[0]); + CPPUNIT_ASSERT(item2); + CPPUNIT_ASSERT_EQUAL(presence->getStatus(), item2->getStatusText()); + } + + void testHighestPresence() { + std::vector<std::string> groups; + groups.push_back("testGroup1"); + JID from("test@testdomain.com"); + xmppRoster_->addContact(from, "name", groups, RosterItemPayload::Both); + Presence::ref lowPresence(new Presence()); + lowPresence->setFrom(withResource(from, "bob")); + lowPresence->setPriority(2); + lowPresence->setStatus("Not here"); + Presence::ref highPresence(new Presence()); + highPresence->setFrom(withResource(from, "bert")); + highPresence->setPriority(10); + highPresence->setStatus("So totally here"); + stanzaChannel_->onPresenceReceived(lowPresence); + stanzaChannel_->onPresenceReceived(highPresence); + ContactRosterItem* item = dynamic_cast<ContactRosterItem*>(dynamic_cast<GroupRosterItem*>(getUIRosterChildren()[0])->getChildren()[0]); + CPPUNIT_ASSERT(item); + CPPUNIT_ASSERT_EQUAL(highPresence->getStatus(), item->getStatusText()); + } + + void testNotHighestPresence() { + std::vector<std::string> groups; + groups.push_back("testGroup1"); + JID from("test@testdomain.com"); + xmppRoster_->addContact(from, "name", groups, RosterItemPayload::Both); + Presence::ref lowPresence(new Presence()); + lowPresence->setFrom(withResource(from, "bob")); + lowPresence->setPriority(2); + lowPresence->setStatus("Not here"); + Presence::ref highPresence(new Presence()); + highPresence->setFrom(withResource(from, "bert")); + highPresence->setPriority(10); + highPresence->setStatus("So totally here"); + stanzaChannel_->onPresenceReceived(highPresence); + stanzaChannel_->onPresenceReceived(lowPresence); + ContactRosterItem* item = dynamic_cast<ContactRosterItem*>(dynamic_cast<GroupRosterItem*>(getUIRosterChildren()[0])->getChildren()[0]); + CPPUNIT_ASSERT(item); + CPPUNIT_ASSERT_EQUAL(highPresence->getStatus(), item->getStatusText()); + } + + void testUnavailablePresence() { + std::vector<std::string> groups; + groups.push_back("testGroup1"); + JID from("test@testdomain.com"); + xmppRoster_->addContact(from, "name", groups, RosterItemPayload::Both); + + Presence::ref lowPresence(new Presence()); + lowPresence->setFrom(withResource(from, "bob")); + lowPresence->setPriority(2); + lowPresence->setShow(StatusShow::Away); + lowPresence->setStatus("Not here"); + Presence::ref lowPresenceOffline(new Presence()); + lowPresenceOffline->setFrom(withResource(from, "bob")); + lowPresenceOffline->setStatus("Signing out"); + lowPresenceOffline->setType(Presence::Unavailable); + + Presence::ref highPresence(new Presence()); + highPresence->setFrom(withResource(from, "bert")); + highPresence->setPriority(10); + highPresence->setStatus("So totally here"); + Presence::ref highPresenceOffline(new Presence()); + highPresenceOffline->setFrom(withResource(from, "bert")); + highPresenceOffline->setType(Presence::Unavailable); + + stanzaChannel_->onPresenceReceived(lowPresence); + Presence::ref accountPresence = presenceOracle_->getAccountPresence(from); + CPPUNIT_ASSERT_EQUAL(StatusShow::Away, accountPresence->getShow()); + + stanzaChannel_->onPresenceReceived(highPresence); + accountPresence = presenceOracle_->getAccountPresence(from); + CPPUNIT_ASSERT_EQUAL(StatusShow::Online, accountPresence->getShow()); + + stanzaChannel_->onPresenceReceived(highPresenceOffline); + + // After this, the roster should show the low presence. + ContactRosterItem* item = dynamic_cast<ContactRosterItem*>(dynamic_cast<GroupRosterItem*>(getUIRosterChildren()[0])->getChildren()[0]); + CPPUNIT_ASSERT(item); + + Presence::ref low = presenceOracle_->getAccountPresence(from); + + CPPUNIT_ASSERT_EQUAL(Presence::Available, low->getType()); + CPPUNIT_ASSERT_EQUAL(lowPresence->getStatus(), low->getStatus()); + CPPUNIT_ASSERT_EQUAL(lowPresence->getShow(), item->getStatusShow()); + CPPUNIT_ASSERT_EQUAL(lowPresence->getStatus(), item->getStatusText()); + stanzaChannel_->onPresenceReceived(lowPresenceOffline); + item = dynamic_cast<ContactRosterItem*>(dynamic_cast<GroupRosterItem*>(getUIRosterChildren()[0])->getChildren()[0]); + CPPUNIT_ASSERT(item); + /* A verification that if the test fails, it's the RosterController, not the PresenceOracle. */ + low = presenceOracle_->getHighestPriorityPresence(from); + CPPUNIT_ASSERT_EQUAL(Presence::Unavailable, low->getType()); + CPPUNIT_ASSERT_EQUAL(lowPresenceOffline->getStatus(), low->getStatus()); + CPPUNIT_ASSERT_EQUAL(StatusShow::None, item->getStatusShow()); + CPPUNIT_ASSERT_EQUAL(lowPresenceOffline->getStatus(), item->getStatusText()); + } + + void testAdd() { + std::vector<std::string> groups; + groups.push_back("testGroup1"); + groups.push_back("testGroup2"); + xmppRoster_->addContact(JID("test@testdomain.com/bob"), "name", groups, RosterItemPayload::Both); + + CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(getUIRosterChildren().size())); + //CPPUNIT_ASSERT_EQUAL(std::string("Bob"), xmppRoster_->getNameForJID(JID("foo@bar.com"))); + } + + void testAddSubscription() { + std::vector<std::string> groups; + JID jid("test@testdomain.com"); + xmppRoster_->addContact(jid, "name", groups, RosterItemPayload::None); + + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(getUIRosterChildren().size())); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(groupChild(0)->getChildren().size())); + xmppRoster_->addContact(jid, "name", groups, RosterItemPayload::To); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(getUIRosterChildren().size())); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(groupChild(0)->getChildren().size())); + + xmppRoster_->addContact(jid, "name", groups, RosterItemPayload::Both); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(getUIRosterChildren().size())); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(groupChild(0)->getChildren().size())); + + } + + void testReceiveRename() { + std::vector<std::string> groups; + JID jid("test@testdomain.com"); + xmppRoster_->addContact(jid, "name", groups, RosterItemPayload::Both); + + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(getUIRosterChildren().size())); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(groupChild(0)->getChildren().size())); + CPPUNIT_ASSERT_EQUAL(std::string("name"), groupChild(0)->getChildren()[0]->getDisplayName()); + xmppRoster_->addContact(jid, "NewName", groups, RosterItemPayload::Both); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(getUIRosterChildren().size())); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(groupChild(0)->getChildren().size())); + CPPUNIT_ASSERT_EQUAL(std::string("NewName"), groupChild(0)->getChildren()[0]->getDisplayName()); + } + + void testReceiveRegroup() { + std::vector<std::string> oldGroups; + std::vector<std::string> newGroups; + newGroups.push_back("A Group"); + std::vector<std::string> newestGroups; + newestGroups.push_back("Best Group"); + JID jid("test@testdomain.com"); + xmppRoster_->addContact(jid, "", oldGroups, RosterItemPayload::Both); + + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(getUIRosterChildren().size())); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(groupChild(0)->getChildren().size())); + CPPUNIT_ASSERT_EQUAL(jid.toString(), groupChild(0)->getChildren()[0]->getDisplayName()); + + xmppRoster_->addContact(jid, "new name", newGroups, RosterItemPayload::Both); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(getUIRosterChildren().size())); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(groupChild(0)->getChildren().size())); + CPPUNIT_ASSERT_EQUAL(std::string("new name"), groupChild(0)->getChildren()[0]->getDisplayName()); + CPPUNIT_ASSERT_EQUAL(std::string("A Group"), groupChild(0)->getDisplayName()); + + xmppRoster_->addContact(jid, "new name", newestGroups, RosterItemPayload::Both); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(getUIRosterChildren().size())); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(groupChild(0)->getChildren().size())); + CPPUNIT_ASSERT_EQUAL(std::string("new name"), groupChild(0)->getChildren()[0]->getDisplayName()); + CPPUNIT_ASSERT_EQUAL(std::string("Best Group"), groupChild(0)->getDisplayName()); + } + + void testSendRename() { + JID jid("testling@wonderland.lit"); + std::vector<std::string> groups; + groups.push_back("Friends"); + groups.push_back("Enemies"); + xmppRoster_->addContact(jid, "Bob", groups, RosterItemPayload::From); + CPPUNIT_ASSERT_EQUAL(groups.size(), xmppRoster_->getGroupsForJID(jid).size()); + uiEventStream_->send(std::make_shared<RenameRosterItemUIEvent>(jid, "Robert")); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), channel_->iqs_.size()); + CPPUNIT_ASSERT_EQUAL(IQ::Set, channel_->iqs_[0]->getType()); + std::shared_ptr<RosterPayload> payload = channel_->iqs_[0]->getPayload<RosterPayload>(); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), payload->getItems().size()); + RosterItemPayload item = payload->getItems()[0]; + CPPUNIT_ASSERT_EQUAL(jid, item.getJID()); + CPPUNIT_ASSERT_EQUAL(std::string("Robert"), item.getName()); + + CPPUNIT_ASSERT_EQUAL(groups.size(), item.getGroups().size()); + assertVectorsEqual(groups, item.getGroups(), __LINE__); + } + + void testRemoveResultsInUnavailablePresence() { + std::vector<std::string> groups; + groups.push_back("testGroup1"); + JID from("test@testdomain.com"); + xmppRoster_->addContact(from, "name", groups, RosterItemPayload::Both); + Presence::ref lowPresence(new Presence()); + lowPresence->setFrom(withResource(from, "bob")); + lowPresence->setPriority(2); + lowPresence->setStatus("Not here"); + Presence::ref highPresence(new Presence()); + highPresence->setFrom(withResource(from, "bert")); + highPresence->setPriority(10); + highPresence->setStatus("So totally here"); + stanzaChannel_->onPresenceReceived(highPresence); + stanzaChannel_->onPresenceReceived(lowPresence); + + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), presenceOracle_->getAllPresence("test@testdomain.com").size()); + + xmppRoster_->onJIDRemoved(JID("test@testdomain.com")); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), presenceOracle_->getAllPresence("test@testdomain.com").size()); + CPPUNIT_ASSERT_EQUAL(Presence::Unavailable, presenceOracle_->getAllPresence("test@testdomain.com")[0]->getType()); + } + + void testOwnContactInRosterPresence() { + std::vector<std::string> groups; + groups.push_back("testGroup1"); + groups.push_back("testGroup2"); + JID from = jid_; + xmppRoster_->addContact(from.toBare(), "name", groups, RosterItemPayload::Both); + Presence::ref presence(new Presence()); + presence->setFrom(from); + presence->setPriority(2); + presence->setStatus("So totally here"); + stanzaChannel_->onPresenceReceived(presence); + ContactRosterItem* item = dynamic_cast<ContactRosterItem*>(dynamic_cast<GroupRosterItem*>(getUIRosterChildren()[0])->getChildren()[0]); + CPPUNIT_ASSERT(item); + CPPUNIT_ASSERT_EQUAL(presence->getStatus(), item->getStatusText()); + ContactRosterItem* item2 = dynamic_cast<ContactRosterItem*>(dynamic_cast<GroupRosterItem*>(getUIRosterChildren()[1])->getChildren()[0]); + CPPUNIT_ASSERT(item2); + CPPUNIT_ASSERT_EQUAL(presence->getStatus(), item2->getStatusText()); + } + + // This tests a scenario of a contact having a resource supporting Jingle File Transfer and + // one resource not supporting it, and the contact features being set correctly. + void testMultiResourceFileTransferFeature() { + JID contact("test@testdomain.com"); + xmppRoster_->addContact(contact, "Name", {}, RosterItemPayload::Both); + + auto sendPresenceAndAnswerCaps = [=](const JID& from, const DiscoInfo& discoInfo) { + auto capsInfo = capsInfoGenerator_->generateCapsInfo(discoInfo); + + auto ftClientPresence = std::make_shared<Presence>(); + ftClientPresence->setFrom(from); + ftClientPresence->setPriority(0); + ftClientPresence->setShow(StatusShow::Online); + ftClientPresence->addPayload(std::make_shared<CapsInfo>(capsInfo)); + stanzaChannel_->onPresenceReceived(ftClientPresence); + + // disco reply + auto discoRequest = channel_->iqs_.back(); + CPPUNIT_ASSERT(discoRequest); + auto discoReply = IQ::createResult(discoRequest->getFrom(), ftClientPresence->getFrom(), discoRequest->getID(), std::make_shared<DiscoInfo>(discoInfo)); + channel_->onIQReceived(discoReply); + }; + + auto ftDiscoInfo = DiscoInfo(); + ftDiscoInfo.addFeature(DiscoInfo::JingleFeature); + ftDiscoInfo.addFeature(DiscoInfo::JingleFTFeature); + ftDiscoInfo.addFeature(DiscoInfo::JingleTransportsIBBFeature); + + sendPresenceAndAnswerCaps(contact.withResource("ft-supported"), ftDiscoInfo); + + auto* item = dynamic_cast<ContactRosterItem*>(dynamic_cast<GroupRosterItem*>(getUIRosterChildren()[0])->getChildren()[0]); + CPPUNIT_ASSERT(item); + CPPUNIT_ASSERT_EQUAL(contact, item->getJID()); + CPPUNIT_ASSERT_EQUAL(true, item->supportsFeature(ContactRosterItem::FileTransferFeature)); + + sendPresenceAndAnswerCaps(contact.withResource("ft-unsupported"), DiscoInfo()); + + item = dynamic_cast<ContactRosterItem*>(dynamic_cast<GroupRosterItem*>(getUIRosterChildren()[0])->getChildren()[0]); + CPPUNIT_ASSERT(item); + CPPUNIT_ASSERT_EQUAL(contact, item->getJID()); + CPPUNIT_ASSERT_EQUAL(true, item->supportsFeature(ContactRosterItem::FileTransferFeature)); + + auto unavailablePresence = std::make_shared<Presence>(); + unavailablePresence->setFrom(contact.withResource("ft-unsupported")); + unavailablePresence->setPriority(0); + unavailablePresence->setType(Presence::Unavailable); + stanzaChannel_->onPresenceReceived(unavailablePresence); + + item = dynamic_cast<ContactRosterItem*>(dynamic_cast<GroupRosterItem*>(getUIRosterChildren()[0])->getChildren()[0]); + CPPUNIT_ASSERT(item); + CPPUNIT_ASSERT_EQUAL(contact, item->getJID()); + CPPUNIT_ASSERT_EQUAL(true, item->supportsFeature(ContactRosterItem::FileTransferFeature)); + + unavailablePresence = std::make_shared<Presence>(); + unavailablePresence->setFrom(contact.withResource("ft-supported")); + unavailablePresence->setPriority(0); + unavailablePresence->setType(Presence::Unavailable); + stanzaChannel_->onPresenceReceived(unavailablePresence); + + item = dynamic_cast<ContactRosterItem*>(dynamic_cast<GroupRosterItem*>(getUIRosterChildren()[0])->getChildren()[0]); + CPPUNIT_ASSERT(item); + CPPUNIT_ASSERT_EQUAL(contact, item->getJID()); + CPPUNIT_ASSERT_EQUAL(false, item->supportsFeature(ContactRosterItem::FileTransferFeature)); + } + + void assertVectorsEqual(const std::vector<std::string>& v1, const std::vector<std::string>& v2, int line) { + for (const auto& entry : v1) { + if (std::find(v2.begin(), v2.end(), entry) == v2.end()) { + std::stringstream stream; + stream << "Couldn't find " << entry << " in v2 (line " << line << ")"; + CPPUNIT_FAIL(stream.str()); + } + } + } + + const std::vector<RosterItem*>& getUIRosterChildren() const { + return mainWindow_->roster->getRoot()->getChildren(); + } + + private: + JID jid_; + std::unique_ptr<MemoryStorages> storages_; + XMPPRosterImpl* xmppRoster_; + MUCRegistry* mucRegistry_; + AvatarManager* avatarManager_; + MockMainWindowFactory* mainWindowFactory_; + NickManager* nickManager_; + NickResolver* nickResolver_; + RosterController* rosterController_; + DummyIQChannel* channel_; + DummyStanzaChannel* stanzaChannel_; + IQRouter* router_; + PresenceOracle* presenceOracle_; + SubscriptionManager* subscriptionManager_; + EventController* eventController_; + UIEventStream* uiEventStream_; + MockMainWindow* mainWindow_; + DummySettingsProvider* settings_; + std::unique_ptr<CapsManager> capsManager_; + EntityCapsManager* entityCapsManager_; + JingleSessionManager* jingleSessionManager_; + ClientBlockListManager* clientBlockListManager_; + CryptoProvider* crypto_; + VCardStorage* vcardStorage_; + VCardManager* vcardManager_; + std::unique_ptr<CapsInfoGenerator> capsInfoGenerator_; + std::unique_ptr<Chattables> chattables_; }; CPPUNIT_TEST_SUITE_REGISTRATION(RosterControllerTest); diff --git a/Swift/Controllers/Roster/UnitTest/RosterTest.cpp b/Swift/Controllers/Roster/UnitTest/RosterTest.cpp index 0c3e769..045a5e4 100644 --- a/Swift/Controllers/Roster/UnitTest/RosterTest.cpp +++ b/Swift/Controllers/Roster/UnitTest/RosterTest.cpp @@ -1,144 +1,141 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ +#include <memory> + #include <cppunit/extensions/HelperMacros.h> #include <cppunit/extensions/TestFactoryRegistry.h> -#include <boost/shared_ptr.hpp> -#include "Swift/Controllers/Roster/Roster.h" -#include "Swift/Controllers/Roster/GroupRosterItem.h" -#include "Swift/Controllers/Roster/SetPresence.h" +#include <Swift/Controllers/Roster/GroupRosterItem.h> +#include <Swift/Controllers/Roster/ItemOperations/SetPresence.h> +#include <Swift/Controllers/Roster/Roster.h> using namespace Swift; class RosterTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(RosterTest); - CPPUNIT_TEST(testGetGroup); - CPPUNIT_TEST(testRemoveContact); - CPPUNIT_TEST(testRemoveSecondContact); - CPPUNIT_TEST(testRemoveSecondContactSameBare); - CPPUNIT_TEST(testApplyPresenceLikeMUC); - CPPUNIT_TEST(testReSortLikeMUC); - CPPUNIT_TEST_SUITE_END(); - - public: - void setUp() { - jid1_ = JID("a@b.c"); - jid2_ = JID("b@c.d"); - jid3_ = JID("c@d.e"); - roster_ = new Roster(); - } - - void tearDown() { - delete roster_; - } - - void testGetGroup() { - roster_->addContact(jid1_, JID(), "Bert", "group1", ""); - roster_->addContact(jid2_, JID(), "Ernie", "group2", ""); - roster_->addContact(jid3_, JID(), "Cookie", "group1", ""); - - CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(roster_->getRoot()->getChildren().size())); - CPPUNIT_ASSERT_EQUAL(std::string("group1"), roster_->getRoot()->getChildren()[0]->getDisplayName()); - CPPUNIT_ASSERT_EQUAL(std::string("group2"), roster_->getRoot()->getChildren()[1]->getDisplayName()); - CPPUNIT_ASSERT_EQUAL(std::string("Bert"), static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren()[0]->getDisplayName()); - CPPUNIT_ASSERT_EQUAL(std::string("Cookie"), static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren()[1]->getDisplayName()); - CPPUNIT_ASSERT_EQUAL(std::string("Ernie"), static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[1])->getChildren()[0]->getDisplayName()); - - } - - void testRemoveContact() { - roster_->addContact(jid1_, jid1_, "Bert", "group1", ""); - CPPUNIT_ASSERT_EQUAL(std::string("Bert"), static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren()[0]->getDisplayName()); - - roster_->removeContact(jid1_); - CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren().size())); - } - - void testRemoveSecondContact() { - roster_->addContact(jid1_, jid1_, "Bert", "group1", ""); - roster_->addContact(jid2_, jid2_, "Cookie", "group1", ""); - CPPUNIT_ASSERT_EQUAL(std::string("Cookie"), static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren()[1]->getDisplayName()); - - roster_->removeContact(jid2_); - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren().size())); - CPPUNIT_ASSERT_EQUAL(std::string("Bert"), static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren()[0]->getDisplayName()); - } - - void testRemoveSecondContactSameBare() { - JID jid4a("a@b/c"); - JID jid4b("a@b/d"); - roster_->addContact(jid4a, JID(), "Bert", "group1", ""); - roster_->addContact(jid4b, JID(), "Cookie", "group1", ""); - CPPUNIT_ASSERT_EQUAL(std::string("Cookie"), static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren()[1]->getDisplayName()); - - roster_->removeContact(jid4b); - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren().size())); - CPPUNIT_ASSERT_EQUAL(std::string("Bert"), static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren()[0]->getDisplayName()); - } - - void testApplyPresenceLikeMUC() { - JID jid4a("a@b/c"); - JID jid4b("a@b/d"); - JID jid4c("a@b/e"); - roster_->addContact(jid4a, JID(), "Bird", "group1", ""); - roster_->addContact(jid4b, JID(), "Cookie", "group1", ""); - roster_->removeContact(jid4b); - roster_->addContact(jid4c, JID(), "Bert", "group1", ""); - roster_->addContact(jid4b, JID(), "Ernie", "group1", ""); - boost::shared_ptr<Presence> presence(new Presence()); - presence->setShow(StatusShow::DND); - presence->setFrom(jid4a); - roster_->applyOnItems(SetPresence(presence, JID::WithResource)); - presence->setFrom(jid4b); - roster_->applyOnItems(SetPresence(presence, JID::WithResource)); - presence->setFrom(jid4c); - roster_->applyOnItems(SetPresence(presence, JID::WithResource)); - - presence = boost::make_shared<Presence>(); - presence->setFrom(jid4b); - presence->setShow(StatusShow::Online); - roster_->applyOnItems(SetPresence(presence, JID::WithResource)); - std::vector<RosterItem*> children = static_cast<GroupRosterItem*>(roster_->getRoot()->getDisplayedChildren()[0])->getDisplayedChildren(); - CPPUNIT_ASSERT_EQUAL(3, static_cast<int>(children.size())); - - /* Check order */ - CPPUNIT_ASSERT_EQUAL(std::string("Ernie"), children[0]->getDisplayName()); - CPPUNIT_ASSERT_EQUAL(std::string("Bert"), children[1]->getDisplayName()); - CPPUNIT_ASSERT_EQUAL(std::string("Bird"), children[2]->getDisplayName()); - - presence = boost::make_shared<Presence>(); - presence->setFrom(jid4c); - presence->setType(Presence::Unavailable); - roster_->removeContact(jid4c); - roster_->applyOnItems(SetPresence(presence, JID::WithResource)); - - } - - void testReSortLikeMUC() { - JID jid4a("a@b/c"); - JID jid4b("a@b/d"); - JID jid4c("a@b/e"); - roster_->addContact(jid4a, JID(), "Bird", "group1", ""); - roster_->addContact(jid4b, JID(), "Cookie", "group2", ""); - roster_->addContact(jid4b, JID(), "Ernie", "group1", ""); - roster_->getGroup("group1")->setManualSort("2"); - roster_->getGroup("group2")->setManualSort("1"); - GroupRosterItem* root = roster_->getRoot(); - const std::vector<RosterItem*> kids = root->getDisplayedChildren(); - CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), kids.size()); - CPPUNIT_ASSERT_EQUAL(std::string("group2"), kids[0]->getDisplayName()); - CPPUNIT_ASSERT_EQUAL(std::string("group1"), kids[1]->getDisplayName()); - } - - private: - Roster *roster_; - JID jid1_; - JID jid2_; - JID jid3_; + CPPUNIT_TEST_SUITE(RosterTest); + CPPUNIT_TEST(testGetGroup); + CPPUNIT_TEST(testRemoveContact); + CPPUNIT_TEST(testRemoveSecondContact); + CPPUNIT_TEST(testRemoveSecondContactSameBare); + CPPUNIT_TEST(testApplyPresenceLikeMUC); + CPPUNIT_TEST(testReSortLikeMUC); + CPPUNIT_TEST_SUITE_END(); + + public: + void setUp() { + jid1_ = JID("a@b.c"); + jid2_ = JID("b@c.d"); + jid3_ = JID("c@d.e"); + roster_ = std::make_unique<Roster>(); + } + + void testGetGroup() { + roster_->addContact(jid1_, JID(), "Bert", "group1", ""); + roster_->addContact(jid2_, JID(), "Ernie", "group2", ""); + roster_->addContact(jid3_, JID(), "Cookie", "group1", ""); + + CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(roster_->getRoot()->getChildren().size())); + CPPUNIT_ASSERT_EQUAL(std::string("group1"), roster_->getRoot()->getChildren()[0]->getDisplayName()); + CPPUNIT_ASSERT_EQUAL(std::string("group2"), roster_->getRoot()->getChildren()[1]->getDisplayName()); + CPPUNIT_ASSERT_EQUAL(std::string("Bert"), static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren()[0]->getDisplayName()); + CPPUNIT_ASSERT_EQUAL(std::string("Cookie"), static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren()[1]->getDisplayName()); + CPPUNIT_ASSERT_EQUAL(std::string("Ernie"), static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[1])->getChildren()[0]->getDisplayName()); + + } + + void testRemoveContact() { + roster_->addContact(jid1_, jid1_, "Bert", "group1", ""); + CPPUNIT_ASSERT_EQUAL(std::string("Bert"), static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren()[0]->getDisplayName()); + + roster_->removeContact(jid1_); + CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren().size())); + } + + void testRemoveSecondContact() { + roster_->addContact(jid1_, jid1_, "Bert", "group1", ""); + roster_->addContact(jid2_, jid2_, "Cookie", "group1", ""); + CPPUNIT_ASSERT_EQUAL(std::string("Cookie"), static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren()[1]->getDisplayName()); + + roster_->removeContact(jid2_); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren().size())); + CPPUNIT_ASSERT_EQUAL(std::string("Bert"), static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren()[0]->getDisplayName()); + } + + void testRemoveSecondContactSameBare() { + JID jid4a("a@b/c"); + JID jid4b("a@b/d"); + roster_->addContact(jid4a, JID(), "Bert", "group1", ""); + roster_->addContact(jid4b, JID(), "Cookie", "group1", ""); + CPPUNIT_ASSERT_EQUAL(std::string("Cookie"), static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren()[1]->getDisplayName()); + + roster_->removeContact(jid4b); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren().size())); + CPPUNIT_ASSERT_EQUAL(std::string("Bert"), static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren()[0]->getDisplayName()); + } + + void testApplyPresenceLikeMUC() { + JID jid4a("a@b/c"); + JID jid4b("a@b/d"); + JID jid4c("a@b/e"); + roster_->addContact(jid4a, JID(), "Bird", "group1", ""); + roster_->addContact(jid4b, JID(), "Cookie", "group1", ""); + roster_->removeContact(jid4b); + roster_->addContact(jid4c, JID(), "Bert", "group1", ""); + roster_->addContact(jid4b, JID(), "Ernie", "group1", ""); + std::shared_ptr<Presence> presence(new Presence()); + presence->setShow(StatusShow::DND); + presence->setFrom(jid4a); + roster_->applyOnItems(SetPresence(presence, JID::WithResource)); + presence->setFrom(jid4b); + roster_->applyOnItems(SetPresence(presence, JID::WithResource)); + presence->setFrom(jid4c); + roster_->applyOnItems(SetPresence(presence, JID::WithResource)); + + presence = std::make_shared<Presence>(); + presence->setFrom(jid4b); + presence->setShow(StatusShow::Online); + roster_->applyOnItems(SetPresence(presence, JID::WithResource)); + std::vector<RosterItem*> children = static_cast<GroupRosterItem*>(roster_->getRoot()->getDisplayedChildren()[0])->getDisplayedChildren(); + CPPUNIT_ASSERT_EQUAL(3, static_cast<int>(children.size())); + + /* Check order */ + CPPUNIT_ASSERT_EQUAL(std::string("Ernie"), children[0]->getDisplayName()); + CPPUNIT_ASSERT_EQUAL(std::string("Bert"), children[1]->getDisplayName()); + CPPUNIT_ASSERT_EQUAL(std::string("Bird"), children[2]->getDisplayName()); + + presence = std::make_shared<Presence>(); + presence->setFrom(jid4c); + presence->setType(Presence::Unavailable); + roster_->removeContact(jid4c); + roster_->applyOnItems(SetPresence(presence, JID::WithResource)); + + } + + void testReSortLikeMUC() { + JID jid4a("a@b/c"); + JID jid4b("a@b/d"); + JID jid4c("a@b/e"); + roster_->addContact(jid4a, JID(), "Bird", "group1", ""); + roster_->addContact(jid4b, JID(), "Cookie", "group2", ""); + roster_->addContact(jid4b, JID(), "Ernie", "group1", ""); + roster_->getGroup("group1")->setManualSort("2"); + roster_->getGroup("group2")->setManualSort("1"); + GroupRosterItem* root = roster_->getRoot(); + const std::vector<RosterItem*> kids = root->getDisplayedChildren(); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), kids.size()); + CPPUNIT_ASSERT_EQUAL(std::string("group2"), kids[0]->getDisplayName()); + CPPUNIT_ASSERT_EQUAL(std::string("group1"), kids[1]->getDisplayName()); + } + + private: + std::unique_ptr<Roster> roster_; + JID jid1_; + JID jid2_; + JID jid3_; }; CPPUNIT_TEST_SUITE_REGISTRATION(RosterTest); diff --git a/Swift/Controllers/Roster/UnitTest/TableRosterTest.cpp b/Swift/Controllers/Roster/UnitTest/TableRosterTest.cpp index db8a2fd..7ebce17 100644 --- a/Swift/Controllers/Roster/UnitTest/TableRosterTest.cpp +++ b/Swift/Controllers/Roster/UnitTest/TableRosterTest.cpp @@ -1,7 +1,7 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #include <Swift/Controllers/Roster/TableRoster.h> @@ -9,84 +9,81 @@ std::ostream& operator<<(std::ostream& os, const Swift::TableRoster::Index& i); std::ostream& operator<<(std::ostream& os, const Swift::TableRoster::Index& i) { - os << "(" << i.section << ", " << i.row << ")"; - return os; + os << "(" << i.section << ", " << i.row << ")"; + return os; } +#include <memory> + #include <cppunit/extensions/HelperMacros.h> #include <cppunit/extensions/TestFactoryRegistry.h> -#include <boost/shared_ptr.hpp> + #include <boost/variant.hpp> #include <Swiften/Network/DummyTimerFactory.h> #include <Swift/Controllers/Roster/Roster.h> #include <Swift/Controllers/Roster/GroupRosterItem.h> -#include <Swift/Controllers/Roster/SetPresence.h> +#include <Swift/Controllers/Roster/ItemOperations/SetPresence.h> using namespace Swift; class TableRosterTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(TableRosterTest); - CPPUNIT_TEST(testAddContact_EmptyRoster); - CPPUNIT_TEST_SUITE_END(); - - public: - void setUp() { - timerFactory = new DummyTimerFactory(); - roster = new Roster(); - jid1 = JID("jid1@example.com"); - jid2 = JID("jid2@example.com"); - } - - void tearDown() { - delete roster; - delete timerFactory; - } - - void testAddContact_EmptyRoster() { - /* - boost::shared_ptr<TableRoster> tableRoster(createTestling()); - - addContact(jid1, "1", "group1"); - - CPPUNIT_ASSERT_EQUAL(4, static_cast<int>(events.size())); - CPPUNIT_ASSERT(boost::get<BeginUpdatesEvent>(&events[0])); - CPPUNIT_ASSERT(boost::get<SectionsInsertedEvent>(&events[1])); - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(boost::get<SectionsInsertedEvent>(events[1]).sections.size())); - CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(boost::get<SectionsInsertedEvent>(events[1]).sections[0])); - CPPUNIT_ASSERT(boost::get<RowsInsertedEvent>(&events[2])); - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(boost::get<RowsInsertedEvent>(events[2]).rows.size())); - CPPUNIT_ASSERT_EQUAL(TableRoster::Index(0, 0), boost::get<RowsInsertedEvent>(events[2]).rows[0]); - CPPUNIT_ASSERT(boost::get<EndUpdatesEvent>(&events[3])); - - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(tableRoster->getNumberOfSections())); - CPPUNIT_ASSERT_EQUAL(std::string("group1"), tableRoster->getSectionTitle(0)); - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(tableRoster->getNumberOfRowsInSection(0))); - CPPUNIT_ASSERT_EQUAL(jid1, tableRoster->getItem(TableRoster::Index(0, 0)).jid); - */ - } - - private: - void addContact(const JID& jid, const std::string& name, const std::string& group) { - roster->addContact(jid, JID(), name, group, ""); - } - - TableRoster* createTestling() { - TableRoster* result = new TableRoster(roster, timerFactory, 10); - result->onUpdate.connect(boost::bind(&TableRosterTest::handleUpdate, this, _1)); - return result; - } - - void handleUpdate(const TableRoster::Update& update) { - updates.push_back(update); - } - - private: - DummyTimerFactory* timerFactory; - Roster* roster; - JID jid1; - JID jid2; - std::vector<TableRoster::Update> updates; + CPPUNIT_TEST_SUITE(TableRosterTest); + CPPUNIT_TEST(testAddContact_EmptyRoster); + CPPUNIT_TEST_SUITE_END(); + + public: + void setUp() { + timerFactory = std::make_unique<DummyTimerFactory>(); + roster = std::make_unique<Roster>(); + jid1 = JID("jid1@example.com"); + jid2 = JID("jid2@example.com"); + } + + void testAddContact_EmptyRoster() { + /* + std::shared_ptr<TableRoster> tableRoster(createTestling()); + + addContact(jid1, "1", "group1"); + + CPPUNIT_ASSERT_EQUAL(4, static_cast<int>(events.size())); + CPPUNIT_ASSERT(boost::get<BeginUpdatesEvent>(&events[0])); + CPPUNIT_ASSERT(boost::get<SectionsInsertedEvent>(&events[1])); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(boost::get<SectionsInsertedEvent>(events[1]).sections.size())); + CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(boost::get<SectionsInsertedEvent>(events[1]).sections[0])); + CPPUNIT_ASSERT(boost::get<RowsInsertedEvent>(&events[2])); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(boost::get<RowsInsertedEvent>(events[2]).rows.size())); + CPPUNIT_ASSERT_EQUAL(TableRoster::Index(0, 0), boost::get<RowsInsertedEvent>(events[2]).rows[0]); + CPPUNIT_ASSERT(boost::get<EndUpdatesEvent>(&events[3])); + + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(tableRoster->getNumberOfSections())); + CPPUNIT_ASSERT_EQUAL(std::string("group1"), tableRoster->getSectionTitle(0)); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(tableRoster->getNumberOfRowsInSection(0))); + CPPUNIT_ASSERT_EQUAL(jid1, tableRoster->getItem(TableRoster::Index(0, 0)).jid); + */ + } + + private: + void addContact(const JID& jid, const std::string& name, const std::string& group) { + roster->addContact(jid, JID(), name, group, ""); + } + + TableRoster* createTestling() { + TableRoster* result = new TableRoster(roster.get(), timerFactory.get(), 10); + result->onUpdate.connect(boost::bind(&TableRosterTest::handleUpdate, this, _1)); + return result; + } + + void handleUpdate(const TableRoster::Update& update) { + updates.push_back(update); + } + + private: + std::unique_ptr<DummyTimerFactory> timerFactory; + std::unique_ptr<Roster> roster; + JID jid1; + JID jid2; + std::vector<TableRoster::Update> updates; }; CPPUNIT_TEST_SUITE_REGISTRATION(TableRosterTest); diff --git a/Swift/Controllers/SConscript b/Swift/Controllers/SConscript index ea52084..3840fbf 100644 --- a/Swift/Controllers/SConscript +++ b/Swift/Controllers/SConscript @@ -6,10 +6,10 @@ import Version ################################################################################ if env["SCONS_STAGE"] == "flags" : - env["SWIFT_CONTROLLERS_FLAGS"] = { - "LIBPATH": [Dir(".")], - "LIBS": ["SwiftControllers"] - } + env["SWIFT_CONTROLLERS_FLAGS"] = { + "LIBPATH": [Dir(".")], + "LIBS": ["SwiftControllers"] + } ################################################################################ @@ -17,91 +17,100 @@ if env["SCONS_STAGE"] == "flags" : ################################################################################ if env["SCONS_STAGE"] == "build" : - myenv = env.Clone() - myenv.BuildVersion("BuildVersion.h", project = "swift") - myenv.UseFlags(env["SWIFTEN_FLAGS"]) - myenv.UseFlags(env["SWIFTEN_DEP_FLAGS"]) - myenv.StaticLibrary("SwiftControllers", [ - "Chat/ChatController.cpp", - "Chat/ChatControllerBase.cpp", - "Chat/ChatsManager.cpp", - "Chat/MUCController.cpp", - "Chat/MUCSearchController.cpp", - "Chat/UserSearchController.cpp", - "Chat/ChatMessageParser.cpp", - "ContactSuggester.cpp", - "MainController.cpp", - "ProfileController.cpp", - "ShowProfileController.cpp", - "ContactEditController.cpp", - "FileTransfer/FileTransferController.cpp", - "FileTransfer/FileTransferOverview.cpp", - "FileTransfer/FileTransferProgressInfo.cpp", - "Roster/RosterController.cpp", - "Roster/RosterGroupExpandinessPersister.cpp", - "Roster/ContactRosterItem.cpp", - "Roster/GroupRosterItem.cpp", - "Roster/RosterItem.cpp", - "Roster/Roster.cpp", - "Roster/TableRoster.cpp", - "EventWindowController.cpp", - "SoundEventController.cpp", - "SystemTrayController.cpp", - "XMLConsoleController.cpp", - "HistoryViewController.cpp", - "HistoryController.cpp", - "FileTransferListController.cpp", - "BlockListController.cpp", - "StatusTracker.cpp", - "PresenceNotifier.cpp", - "EventNotifier.cpp", - "AdHocManager.cpp", - "XMPPEvents/EventController.cpp", - "UIEvents/UIEvent.cpp", - "UIInterfaces/XMLConsoleWidget.cpp", - "UIInterfaces/ChatListWindow.cpp", - "PreviousStatusStore.cpp", - "ProfileSettingsProvider.cpp", - "Settings/SettingsProviderHierachy.cpp", - "Settings/XMLSettingsProvider.cpp", - "Storages/CertificateStorageFactory.cpp", - "Storages/CertificateStorage.cpp", - "Storages/CertificateFileStorage.cpp", - "Storages/CertificateMemoryStorage.cpp", - "Storages/AvatarFileStorage.cpp", - "Storages/FileStorages.cpp", - "Storages/RosterFileStorage.cpp", - "Storages/CapsFileStorage.cpp", - "Storages/VCardFileStorage.cpp", - "StatusUtil.cpp", - "Translator.cpp", - "XMPPURIController.cpp", - "ChatMessageSummarizer.cpp", - "SettingConstants.cpp", - "WhiteboardManager.cpp", - "StatusCache.cpp", - "HighlightAction.cpp", - "HighlightEditorController.cpp", - "HighlightManager.cpp", - "HighlightRule.cpp", - "Highlighter.cpp", - "ContactsFromXMPPRoster.cpp", - "ContactProvider.cpp", - "Contact.cpp" - ]) + myenv = env.Clone() + myenv.BuildVersion("BuildVersion.h", project = "swift") + myenv.UseFlags(env["SWIFTEN_FLAGS"]) + myenv.UseFlags(env["SWIFTEN_DEP_FLAGS"]) + myenv.StaticLibrary("SwiftControllers", [ + "AdHocController.cpp", + "AdHocManager.cpp", + "BlockListController.cpp", + "Chat/Chattables.cpp", + "Chat/ChatController.cpp", + "Chat/ChatControllerBase.cpp", + "Chat/ChatMessageParser.cpp", + "Chat/ChatsManager.cpp", + "Chat/MUCController.cpp", + "Chat/MUCSearchController.cpp", + "Chat/UserSearchController.cpp", + "ChatMessageSummarizer.cpp", + "Contact.cpp", + "ContactEditController.cpp", + "ContactProvider.cpp", + "ContactSuggester.cpp", + "ContactsFromXMPPRoster.cpp", + "EventNotifier.cpp", + "EventWindowController.cpp", + "FdpFormSubmitController.cpp", + "FileTransfer/FileTransferController.cpp", + "FileTransfer/FileTransferOverview.cpp", + "FileTransfer/FileTransferProgressInfo.cpp", + "FileTransferListController.cpp", + "Highlighting/HighlightAction.cpp", + "Highlighting/HighlightEditorController.cpp", + "Highlighting/HighlightManager.cpp", + "Highlighting/Highlighter.cpp", + "HistoryController.cpp", + "HistoryViewController.cpp", + "AccountController.cpp", + "PresenceNotifier.cpp", + "PreviousStatusStore.cpp", + "ProfileController.cpp", + "ProfileSettingsProvider.cpp", + "Roster/ContactRosterItem.cpp", + "Roster/GroupRosterItem.cpp", + "Roster/Roster.cpp", + "Roster/RosterController.cpp", + "Roster/RosterGroupExpandinessPersister.cpp", + "Roster/RosterItem.cpp", + "Roster/RosterVCardProvider.cpp", + "Roster/TableRoster.cpp", + "SettingConstants.cpp", + "Settings/SettingsProviderHierachy.cpp", + "Settings/XMLSettingsProvider.cpp", + "ShowProfileController.cpp", + "SoundEventController.cpp", + "StatusCache.cpp", + "StatusTracker.cpp", + "StatusUtil.cpp", + "Storages/AvatarFileStorage.cpp", + "Storages/CapsFileStorage.cpp", + "Storages/CertificateFileStorage.cpp", + "Storages/CertificateMemoryStorage.cpp", + "Storages/CertificateStorage.cpp", + "Storages/CertificateStorageFactory.cpp", + "Storages/FileStorages.cpp", + "Storages/RosterFileStorage.cpp", + "Storages/VCardFileStorage.cpp", + "SystemTrayController.cpp", + "Translator.cpp", + "UIEvents/UIEvent.cpp", + "UIInterfaces/ChatListWindow.cpp", + "UIInterfaces/FdpFormSubmitWindow.cpp", + "UIInterfaces/HighlightEditorWindow.cpp", + "UIInterfaces/XMLConsoleWidget.cpp", + "WhiteboardManager.cpp", + "XMLConsoleController.cpp", + "XMPPEvents/EventController.cpp", + "XMPPURIController.cpp", + ]) - env.Append(UNITTEST_SOURCES = [ - File("Roster/UnitTest/RosterControllerTest.cpp"), - File("Roster/UnitTest/RosterTest.cpp"), - File("Roster/UnitTest/LeastCommonSubsequenceTest.cpp"), - File("Roster/UnitTest/TableRosterTest.cpp"), - File("UnitTest/PreviousStatusStoreTest.cpp"), - File("UnitTest/PresenceNotifierTest.cpp"), - File("Chat/UnitTest/ChatsManagerTest.cpp"), - File("Chat/UnitTest/MUCControllerTest.cpp"), - File("Chat/UnitTest/ChatMessageParserTest.cpp"), - File("UnitTest/MockChatWindow.cpp"), - File("UnitTest/ChatMessageSummarizerTest.cpp"), - File("Settings/UnitTest/SettingsProviderHierachyTest.cpp"), - File("UnitTest/HighlightRuleTest.cpp"), - ]) + env.Append(UNITTEST_SOURCES = [ + File("Chat/UnitTest/ChatListWindowChatTest.cpp"), + File("Chat/UnitTest/ChatMessageParserTest.cpp"), + File("Chat/UnitTest/ChatsManagerTest.cpp"), + File("Chat/UnitTest/ChatControllerTest.cpp"), + File("Chat/UnitTest/ChattablesTest.cpp"), + File("Chat/UnitTest/MUCControllerTest.cpp"), + File("Roster/UnitTest/LeastCommonSubsequenceTest.cpp"), + File("Roster/UnitTest/RosterControllerTest.cpp"), + File("Roster/UnitTest/RosterTest.cpp"), + File("Roster/UnitTest/TableRosterTest.cpp"), + File("Settings/UnitTest/SettingsProviderHierachyTest.cpp"), + File("UnitTest/ChatMessageSummarizerTest.cpp"), + File("UnitTest/ContactSuggesterTest.cpp"), + File("UnitTest/FdpFormSubmitControllerTest.cpp"), + File("UnitTest/MockChatWindow.cpp"), + File("UnitTest/PresenceNotifierTest.cpp"), + File("UnitTest/PreviousStatusStoreTest.cpp"), + ]) diff --git a/Swift/Controllers/SettingConstants.cpp b/Swift/Controllers/SettingConstants.cpp index a5328a4..8336200 100644 --- a/Swift/Controllers/SettingConstants.cpp +++ b/Swift/Controllers/SettingConstants.cpp @@ -1,7 +1,7 @@ /* - * Copyright (c) 2012 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2012-2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #include <Swift/Controllers/SettingConstants.h> @@ -14,15 +14,14 @@ const SettingsProvider::Setting<bool> SettingConstants::SHOW_NOTIFICATIONS = Set const SettingsProvider::Setting<bool> SettingConstants::REQUEST_DELIVERYRECEIPTS = SettingsProvider::Setting<bool>("requestDeliveryReceipts", false); const SettingsProvider::Setting<bool> SettingConstants::FORGET_PASSWORDS = SettingsProvider::Setting<bool>("forgetPasswords", false); const SettingsProvider::Setting<bool> SettingConstants::REMEMBER_RECENT_CHATS = SettingsProvider::Setting<bool>("rememberRecentChats", true); -const SettingsProvider::Setting<std::string> SettingConstants::LAST_LOGIN_JID = SettingsProvider::Setting<std::string>("lastLoginJID", ""); -const SettingsProvider::Setting<bool> SettingConstants::LOGIN_AUTOMATICALLY = SettingsProvider::Setting<bool>("loginAutomatically", false); const SettingsProvider::Setting<bool> SettingConstants::SHOW_OFFLINE("showOffline", false); const SettingsProvider::Setting<std::string> SettingConstants::EXPANDED_ROSTER_GROUPS("GroupExpandiness", ""); const SettingsProvider::Setting<bool> SettingConstants::PLAY_SOUNDS("playSounds", true); const SettingsProvider::Setting<std::string> SettingConstants::HIGHLIGHT_RULES("highlightRules", "@"); -const SettingsProvider::Setting<bool> SettingConstants::SPELL_CHECKER("spellChecker", false); -const SettingsProvider::Setting<std::string> SettingConstants::DICT_PATH("dictPath", "/usr/share/myspell/dicts/"); -const SettingsProvider::Setting<std::string> SettingConstants::PERSONAL_DICT_PATH("personaldictPath", "/home/"); -const SettingsProvider::Setting<std::string> SettingConstants::DICT_FILE("dictFile", "en_US.dic"); +const SettingsProvider::Setting<std::string> SettingConstants::HIGHLIGHT_RULES_V2("highlightRulesV2", "@"); const SettingsProvider::Setting<std::string> SettingConstants::INVITE_AUTO_ACCEPT_MODE("inviteAutoAcceptMode", "presence"); +const SettingsProvider::Setting<bool> SettingConstants::DISCONNECT_ON_CARD_REMOVAL("disconnectOnCardRemoval", true); +const SettingsProvider::Setting<bool> SettingConstants::MUC_MARKING_ELISION("mucMarkingElision", true); +const SettingsProvider::Setting<bool> SettingConstants::FUTURE("future", false); + } diff --git a/Swift/Controllers/SettingConstants.h b/Swift/Controllers/SettingConstants.h index 4d25bde..68c22b7 100644 --- a/Swift/Controllers/SettingConstants.h +++ b/Swift/Controllers/SettingConstants.h @@ -1,7 +1,7 @@ /* - * Copyright (c) 2012 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2012-2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once @@ -9,24 +9,95 @@ #include <Swift/Controllers/Settings/SettingsProvider.h> namespace Swift { - class SettingConstants { - public: - static const SettingsProvider::Setting<bool> IDLE_GOES_OFFLINE; - static const SettingsProvider::Setting<int> IDLE_TIMEOUT; - static const SettingsProvider::Setting<bool> SHOW_NOTIFICATIONS; - static const SettingsProvider::Setting<bool> REQUEST_DELIVERYRECEIPTS; - static const SettingsProvider::Setting<bool> FORGET_PASSWORDS; - static const SettingsProvider::Setting<bool> REMEMBER_RECENT_CHATS; - static const SettingsProvider::Setting<std::string> LAST_LOGIN_JID; - static const SettingsProvider::Setting<bool> LOGIN_AUTOMATICALLY; - static const SettingsProvider::Setting<bool> SHOW_OFFLINE; - static const SettingsProvider::Setting<std::string> EXPANDED_ROSTER_GROUPS; - static const SettingsProvider::Setting<bool> PLAY_SOUNDS; - static const SettingsProvider::Setting<std::string> HIGHLIGHT_RULES; - static const SettingsProvider::Setting<bool> SPELL_CHECKER; - static const SettingsProvider::Setting<std::string> DICT_PATH; - static const SettingsProvider::Setting<std::string> PERSONAL_DICT_PATH; - static const SettingsProvider::Setting<std::string> DICT_FILE; - static const SettingsProvider::Setting<std::string> INVITE_AUTO_ACCEPT_MODE; - }; + /** + * This class contains the major setting keys for Swift. + */ + class SettingConstants { + public: + /** + * The #IDLE_GOES_OFFLINE setting specifies whether to close the XMPP connection when + * the user went idle. + * + * True for automatic close of the XMPP connection and false for only changing the presence on idle. + */ + static const SettingsProvider::Setting<bool> IDLE_GOES_OFFLINE; + /** + * The #IDLE_TIMEOUT setting specifieds the seconds the user has to be inactive at the + * desktop so the user is regarded as idle. + */ + static const SettingsProvider::Setting<int> IDLE_TIMEOUT; + static const SettingsProvider::Setting<bool> SHOW_NOTIFICATIONS; + /** + * The #REQUEST_DELIVERYRECEIPTS settings specifies whether to request delivery receipts + * for messages to contacts that support message receipts. + */ + static const SettingsProvider::Setting<bool> REQUEST_DELIVERYRECEIPTS; + static const SettingsProvider::Setting<bool> FORGET_PASSWORDS; + static const SettingsProvider::Setting<bool> REMEMBER_RECENT_CHATS; + /** + * The #SHOW_OFFLINE setting specifies whether or not to show offline contacts in the + * roster. + * + * If set true Swift will show offline contacts; else not. + */ + static const SettingsProvider::Setting<bool> SHOW_OFFLINE; + /** + * The #EXPANDED_ROSTER_GROUPS setting specifies the list of groups that are expanded + * in the roster UI. + * + * Its value is a string with group names seperated by newlines. + */ + static const SettingsProvider::Setting<std::string> EXPANDED_ROSTER_GROUPS; + static const SettingsProvider::Setting<bool> PLAY_SOUNDS; + /** + * The #HIGHLIGHT_RULES setting specifies the highlight rules and the associated actions. + * + * Its value is a Boost serialized representation. + */ + static const SettingsProvider::Setting<std::string> HIGHLIGHT_RULES; + /** + * The #HIGHLIGHT_RULES_V2 setting specifies the second version of highlight configuration + * rules, incompatible to old highlight rules. + * + * Its value is a Boost serialized representation. + */ + static const SettingsProvider::Setting<std::string> HIGHLIGHT_RULES_V2; + /** + * The #INVITE_AUTO_ACCEPT_MODE setting specifies how to handle invites to chat rooms. + * + * Supported values are: + * - "no" : It is up to the user whether to accept the invitation and enter a room or not. + * - "presence" : The invitation is automatically accepted if it is from a contact that is + * already allowed to see the user's presence status. + * - "domain" : The invitation is automatically accepted if it is from a contact that is + * already allowed to see the user's presence status or from a contact of user's domain. + */ + static const SettingsProvider::Setting<std::string> INVITE_AUTO_ACCEPT_MODE; + /** + * The #DISCONNECT_ON_CARD_REMOVAL setting + * specifies whether or not to sign out the user when + * the smartcard is removed. + * + * If set true Swift will sign out the user when the + * smart card is removed; else not. + */ + static const SettingsProvider::Setting<bool> DISCONNECT_ON_CARD_REMOVAL; + /** + * The #MUC_MARKING_ELISION setting + * specifies whether or not messages with the default muc + * marking display their marking, and whether unmarked messages + * are marked as such. + * + * If set true, unmarked messages will be marked with the marking + * "unmarked", and messages with the room default marking will + * have their markings stripped. + */ + static const SettingsProvider::Setting<bool> MUC_MARKING_ELISION; + + /** + * The #FUTURE setting enables use of experimental features + * planned for future releases. + */ + static const SettingsProvider::Setting<bool> FUTURE; + }; } diff --git a/Swift/Controllers/Settings/DummySettingsProvider.h b/Swift/Controllers/Settings/DummySettingsProvider.h index 0183dd3..134aadc 100644 --- a/Swift/Controllers/Settings/DummySettingsProvider.h +++ b/Swift/Controllers/Settings/DummySettingsProvider.h @@ -1,56 +1,58 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <Swift/Controllers/Settings/SettingsProvider.h> - #include <map> +#include <set> +#include <string> + +#include <Swift/Controllers/Settings/SettingsProvider.h> namespace Swift { class DummySettingsProvider : public SettingsProvider { - public: - virtual ~DummySettingsProvider() {} - virtual std::string getSetting(const Setting<std::string>& setting) { - return stringValues.find(setting.getKey()) != stringValues.end() ? stringValues[setting.getKey()] : setting.getDefaultValue(); - } - virtual void storeSetting(const Setting<std::string>& setting, const std::string& value) { - stringValues[setting.getKey()] = value; - onSettingChanged(setting.getKey()); - } - virtual bool getSetting(const Setting<bool>& setting) { - return boolValues.find(setting.getKey()) != boolValues.end() ? boolValues[setting.getKey()] : setting.getDefaultValue(); - } - virtual void storeSetting(const Setting<bool>& setting, const bool& value) { - boolValues[setting.getKey()] = value; - onSettingChanged(setting.getKey()); - } - virtual int getSetting(const Setting<int>& setting) { - return intValues.find(setting.getKey()) != intValues.end() ? intValues[setting.getKey()] : setting.getDefaultValue(); - } - virtual void storeSetting(const Setting<int>& setting, const int& value) { - intValues[setting.getKey()] = value; - onSettingChanged(setting.getKey()); - } - virtual std::vector<std::string> getAvailableProfiles() {return std::vector<std::string>();} - virtual void createProfile(const std::string& ) {} - virtual void removeProfile(const std::string& ) {} - virtual bool getIsSettingFinal(const std::string& settingPath) {return finals.count(settingPath);} - void setFinal(const std::string& settingPath) { - finals.insert(settingPath); - } - virtual bool hasSetting(const std::string& key) { - return stringValues.find(key) != stringValues.end() || intValues.find(key) != intValues.end() || boolValues.find(key) != boolValues.end(); - } - private: - std::map<std::string, std::string> stringValues; - std::map<std::string, int> intValues; - std::map<std::string, bool> boolValues; - std::set<std::string> finals; + public: + virtual ~DummySettingsProvider() {} + virtual std::string getSetting(const Setting<std::string>& setting) { + return stringValues.find(setting.getKey()) != stringValues.end() ? stringValues[setting.getKey()] : setting.getDefaultValue(); + } + virtual void storeSetting(const Setting<std::string>& setting, const std::string& value) { + stringValues[setting.getKey()] = value; + onSettingChanged(setting.getKey()); + } + virtual bool getSetting(const Setting<bool>& setting) { + return boolValues.find(setting.getKey()) != boolValues.end() ? boolValues[setting.getKey()] : setting.getDefaultValue(); + } + virtual void storeSetting(const Setting<bool>& setting, const bool& value) { + boolValues[setting.getKey()] = value; + onSettingChanged(setting.getKey()); + } + virtual int getSetting(const Setting<int>& setting) { + return intValues.find(setting.getKey()) != intValues.end() ? intValues[setting.getKey()] : setting.getDefaultValue(); + } + virtual void storeSetting(const Setting<int>& setting, const int& value) { + intValues[setting.getKey()] = value; + onSettingChanged(setting.getKey()); + } + virtual std::vector<std::string> getAvailableProfiles() {return std::vector<std::string>();} + virtual void createProfile(const std::string& ) {} + virtual void removeProfile(const std::string& ) {} + virtual bool getIsSettingFinal(const std::string& settingPath) {return finals.count(settingPath);} + void setFinal(const std::string& settingPath) { + finals.insert(settingPath); + } + virtual bool hasSetting(const std::string& key) { + return stringValues.find(key) != stringValues.end() || intValues.find(key) != intValues.end() || boolValues.find(key) != boolValues.end(); + } + private: + std::map<std::string, std::string> stringValues; + std::map<std::string, int> intValues; + std::map<std::string, bool> boolValues; + std::set<std::string> finals; }; } diff --git a/Swift/Controllers/Settings/SettingsProvider.h b/Swift/Controllers/Settings/SettingsProvider.h index 458653b..1e9805a 100644 --- a/Swift/Controllers/Settings/SettingsProvider.h +++ b/Swift/Controllers/Settings/SettingsProvider.h @@ -1,71 +1,71 @@ /* - * Copyright (c) 2010-2012 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <Swiften/Base/boost_bsignals.h> - #include <string> #include <vector> +#include <boost/signals2.hpp> + namespace Swift { class SettingsProvider { - public: - template <typename T> - class Setting { - public: - Setting(const std::string& key, const T& defaultValue) : key(key), defaultValue(defaultValue) { - - } - - const std::string& getKey() const { - return key; - } - - const T& getDefaultValue() const { - return defaultValue; - } - - private: - std::string key; - T defaultValue; - }; - - public: - virtual ~SettingsProvider() {} - virtual std::string getSetting(const Setting<std::string>& setting) = 0; - virtual void storeSetting(const Setting<std::string>& setting, const std::string& value) = 0; - virtual bool getSetting(const Setting<bool>& setting) = 0; - virtual void storeSetting(const Setting<bool>& setting, const bool& value) = 0; - virtual int getSetting(const Setting<int>& setting) = 0; - virtual void storeSetting(const Setting<int>& setting, const int& value) = 0; - - virtual std::vector<std::string> getAvailableProfiles() = 0; - virtual void createProfile(const std::string& profile) = 0; - virtual void removeProfile(const std::string& profile) = 0; - /** A final setting is one that this settings provider says may not be overriden by lower priority profiles. - * e.g. An Administrator-set configuration to disallow saving user passwords could not be overridden by the user. - */ - template<typename T> - bool getIsSettingFinal(const Setting<T>& setting) { - return getIsSettingFinal(setting.getKey()); - } - virtual bool hasSetting(const std::string& key) = 0; - - friend class SettingsProviderHierachy; - protected: - virtual bool getIsSettingFinal(const std::string& settingPath) = 0; - - public: - /** - * Emitted when a setting is changed. - */ - boost::signal<void (const std::string& /*Setting's Path*/)> onSettingChanged; + public: + template <typename T> + class Setting { + public: + Setting(const std::string& key, const T& defaultValue) : key(key), defaultValue(defaultValue) { + + } + + const std::string& getKey() const { + return key; + } + + const T& getDefaultValue() const { + return defaultValue; + } + + private: + std::string key; + T defaultValue; + }; + + public: + virtual ~SettingsProvider() {} + virtual std::string getSetting(const Setting<std::string>& setting) = 0; + virtual void storeSetting(const Setting<std::string>& setting, const std::string& value) = 0; + virtual bool getSetting(const Setting<bool>& setting) = 0; + virtual void storeSetting(const Setting<bool>& setting, const bool& value) = 0; + virtual int getSetting(const Setting<int>& setting) = 0; + virtual void storeSetting(const Setting<int>& setting, const int& value) = 0; + + virtual std::vector<std::string> getAvailableProfiles() = 0; + virtual void createProfile(const std::string& profile) = 0; + virtual void removeProfile(const std::string& profile) = 0; + /** A final setting is one that this settings provider says may not be overriden by lower priority profiles. + * e.g. An Administrator-set configuration to disallow saving user passwords could not be overridden by the user. + */ + template<typename T> + bool getIsSettingFinal(const Setting<T>& setting) { + return getIsSettingFinal(setting.getKey()); + } + virtual bool hasSetting(const std::string& key) = 0; + + friend class SettingsProviderHierachy; + protected: + virtual bool getIsSettingFinal(const std::string& settingPath) = 0; + + public: + /** + * Emitted when a setting is changed. + */ + boost::signals2::signal<void (const std::string& /*Setting's Path*/)> onSettingChanged; }; } diff --git a/Swift/Controllers/Settings/SettingsProviderHierachy.cpp b/Swift/Controllers/Settings/SettingsProviderHierachy.cpp index 40a9025..a05fabc 100644 --- a/Swift/Controllers/Settings/SettingsProviderHierachy.cpp +++ b/Swift/Controllers/Settings/SettingsProviderHierachy.cpp @@ -1,115 +1,115 @@ /* - * Copyright (c) 2012 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2012-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #include <Swift/Controllers/Settings/SettingsProviderHierachy.h> -#include <Swiften/Base/foreach.h> #include <Swiften/Base/Log.h> + namespace Swift { SettingsProviderHierachy::~SettingsProviderHierachy() { } bool SettingsProviderHierachy::hasSetting(const std::string& key) { - foreach (SettingsProvider* provider, providers_) { - if (provider->hasSetting(key)) { - return true; - } - } - return false; + for (auto provider : providers_) { + if (provider->hasSetting(key)) { + return true; + } + } + return false; } std::string SettingsProviderHierachy::getSetting(const Setting<std::string>& setting) { - std::string value = setting.getDefaultValue(); - foreach (SettingsProvider* provider, providers_) { - std::string providerSetting = provider->getSetting(setting); - if (provider->hasSetting(setting.getKey())) { - value = providerSetting; - } - if (provider->getIsSettingFinal(setting.getKey())) { - return value; - } - } - return value; + std::string value = setting.getDefaultValue(); + for (auto provider : providers_) { + std::string providerSetting = provider->getSetting(setting); + if (provider->hasSetting(setting.getKey())) { + value = providerSetting; + } + if (provider->getIsSettingFinal(setting.getKey())) { + return value; + } + } + return value; } void SettingsProviderHierachy::storeSetting(const Setting<std::string>& setting, const std::string& settingValue) { - if (!getIsSettingFinal(setting.getKey())) { - getWritableProvider()->storeSetting(setting, settingValue); - } + if (!getIsSettingFinal(setting.getKey())) { + getWritableProvider()->storeSetting(setting, settingValue); + } } bool SettingsProviderHierachy::getSetting(const Setting<bool>& setting) { - bool value = setting.getDefaultValue(); - foreach (SettingsProvider* provider, providers_) { - bool providerSetting = provider->getSetting(setting); - if (provider->hasSetting(setting.getKey())) { - value = providerSetting; - if (provider->getIsSettingFinal(setting.getKey())) { - return providerSetting; - } - } - } - return value; + bool value = setting.getDefaultValue(); + for (auto provider : providers_) { + bool providerSetting = provider->getSetting(setting); + if (provider->hasSetting(setting.getKey())) { + value = providerSetting; + if (provider->getIsSettingFinal(setting.getKey())) { + return providerSetting; + } + } + } + return value; } void SettingsProviderHierachy::storeSetting(const Setting<bool>& setting, const bool& settingValue) { - if (!getIsSettingFinal(setting.getKey())) { - getWritableProvider()->storeSetting(setting, settingValue); - } + if (!getIsSettingFinal(setting.getKey())) { + getWritableProvider()->storeSetting(setting, settingValue); + } } int SettingsProviderHierachy::getSetting(const Setting<int>& setting) { - int value = setting.getDefaultValue(); - foreach (SettingsProvider* provider, providers_) { - int providerSetting = provider->getSetting(setting); - if (provider->hasSetting(setting.getKey())) { - value = providerSetting; - if (provider->getIsSettingFinal(setting.getKey())) { - return providerSetting; - } - } - } - return value; + int value = setting.getDefaultValue(); + for (auto provider : providers_) { + int providerSetting = provider->getSetting(setting); + if (provider->hasSetting(setting.getKey())) { + value = providerSetting; + if (provider->getIsSettingFinal(setting.getKey())) { + return providerSetting; + } + } + } + return value; } void SettingsProviderHierachy::storeSetting(const Setting<int>& setting, const int& settingValue) { - if (!getIsSettingFinal(setting.getKey())) { - getWritableProvider()->storeSetting(setting, settingValue); - } + if (!getIsSettingFinal(setting.getKey())) { + getWritableProvider()->storeSetting(setting, settingValue); + } } std::vector<std::string> SettingsProviderHierachy::getAvailableProfiles() { - /* Always pull profiles from the topmost */ - return getWritableProvider()->getAvailableProfiles(); + /* Always pull profiles from the topmost */ + return getWritableProvider()->getAvailableProfiles(); } void SettingsProviderHierachy::createProfile(const std::string& profile) { - return getWritableProvider()->createProfile(profile); + return getWritableProvider()->createProfile(profile); } void SettingsProviderHierachy::removeProfile(const std::string& profile) { - return getWritableProvider()->removeProfile(profile); + return getWritableProvider()->removeProfile(profile); } bool SettingsProviderHierachy::getIsSettingFinal(const std::string& settingPath) { - bool isFinal = false; - foreach (SettingsProvider* provider, providers_) { - isFinal |= provider->getIsSettingFinal(settingPath); - } - return isFinal; + bool isFinal = false; + for (auto provider : providers_) { + isFinal |= provider->getIsSettingFinal(settingPath); + } + return isFinal; } SettingsProvider* SettingsProviderHierachy::getWritableProvider() { - return providers_.back(); + return providers_.back(); } void SettingsProviderHierachy::addProviderToTopOfStack(SettingsProvider* provider) { - providers_.push_back(provider); - provider->onSettingChanged.connect(onSettingChanged); + providers_.push_back(provider); + provider->onSettingChanged.connect(onSettingChanged); } } diff --git a/Swift/Controllers/Settings/SettingsProviderHierachy.h b/Swift/Controllers/Settings/SettingsProviderHierachy.h index b16b33e..a68ff36 100644 --- a/Swift/Controllers/Settings/SettingsProviderHierachy.h +++ b/Swift/Controllers/Settings/SettingsProviderHierachy.h @@ -1,7 +1,7 @@ /* - * Copyright (c) 2012 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2012 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once @@ -11,34 +11,34 @@ namespace Swift { class SettingsProviderHierachy : public SettingsProvider { - public: - virtual ~SettingsProviderHierachy(); - virtual std::string getSetting(const Setting<std::string>& setting); - virtual void storeSetting(const Setting<std::string>& setting, const std::string& value); - virtual bool getSetting(const Setting<bool>& setting); - virtual void storeSetting(const Setting<bool>& setting, const bool& value); - virtual int getSetting(const Setting<int>& setting); - virtual void storeSetting(const Setting<int>& setting, const int& value); - virtual std::vector<std::string> getAvailableProfiles(); - virtual void createProfile(const std::string& profile); - virtual void removeProfile(const std::string& profile); - virtual bool hasSetting(const std::string& key); - protected: - virtual bool getIsSettingFinal(const std::string& settingPath); - - public: - /** - * Adds a provider less significant than any already added. - * This means that if an existing provider has a setting, this provider won't be asked. - * Any settings will be pushed into the topmost (least significant) provider. - * Does not take ownership of provider. - */ - void addProviderToTopOfStack(SettingsProvider* provider); - private: - SettingsProvider* getWritableProvider(); - private: - /* Start/Left is most significant (lowest), left overrides right.*/ - std::vector<SettingsProvider*> providers_; + public: + virtual ~SettingsProviderHierachy(); + virtual std::string getSetting(const Setting<std::string>& setting); + virtual void storeSetting(const Setting<std::string>& setting, const std::string& value); + virtual bool getSetting(const Setting<bool>& setting); + virtual void storeSetting(const Setting<bool>& setting, const bool& value); + virtual int getSetting(const Setting<int>& setting); + virtual void storeSetting(const Setting<int>& setting, const int& value); + virtual std::vector<std::string> getAvailableProfiles(); + virtual void createProfile(const std::string& profile); + virtual void removeProfile(const std::string& profile); + virtual bool hasSetting(const std::string& key); + protected: + virtual bool getIsSettingFinal(const std::string& settingPath); + + public: + /** + * Adds a provider less significant than any already added. + * This means that if an existing provider has a setting, this provider won't be asked. + * Any settings will be pushed into the topmost (least significant) provider. + * Does not take ownership of provider. + */ + void addProviderToTopOfStack(SettingsProvider* provider); + private: + SettingsProvider* getWritableProvider(); + private: + /* Start/Left is most significant (lowest), left overrides right.*/ + std::vector<SettingsProvider*> providers_; }; } diff --git a/Swift/Controllers/Settings/UnitTest/SettingsProviderHierachyTest.cpp b/Swift/Controllers/Settings/UnitTest/SettingsProviderHierachyTest.cpp index 2b637a0..3cfebc7 100644 --- a/Swift/Controllers/Settings/UnitTest/SettingsProviderHierachyTest.cpp +++ b/Swift/Controllers/Settings/UnitTest/SettingsProviderHierachyTest.cpp @@ -1,91 +1,87 @@ /* - * Copyright (c) 2012 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2012-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ +#include <memory> + #include <cppunit/extensions/HelperMacros.h> #include <cppunit/extensions/TestFactoryRegistry.h> -#include <Swift/Controllers/Settings/SettingsProviderHierachy.h> #include <Swift/Controllers/Settings/DummySettingsProvider.h> +#include <Swift/Controllers/Settings/SettingsProviderHierachy.h> #include <Swift/Controllers/Settings/XMLSettingsProvider.h> using namespace Swift; using namespace std; class SettingsProviderHierachyTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(SettingsProviderHierachyTest); - CPPUNIT_TEST(testEmpty); - CPPUNIT_TEST(testTop); - CPPUNIT_TEST(testBottom); - CPPUNIT_TEST(testBoth); - CPPUNIT_TEST(testTopDefault); - CPPUNIT_TEST(testBottomOverrides); - CPPUNIT_TEST(testFinal); - CPPUNIT_TEST_SUITE_END(); + CPPUNIT_TEST_SUITE(SettingsProviderHierachyTest); + CPPUNIT_TEST(testEmpty); + CPPUNIT_TEST(testTop); + CPPUNIT_TEST(testBottom); + CPPUNIT_TEST(testBoth); + CPPUNIT_TEST(testTopDefault); + CPPUNIT_TEST(testBottomOverrides); + CPPUNIT_TEST(testFinal); + CPPUNIT_TEST_SUITE_END(); public: - SettingsProviderHierachyTest() : setting1("somekey", 42) {} - - void setUp() { - bottom = new DummySettingsProvider(); - top = new DummySettingsProvider(); - testling = new SettingsProviderHierachy(); - testling->addProviderToTopOfStack(bottom); - testling->addProviderToTopOfStack(top); - } + SettingsProviderHierachyTest() : setting1("somekey", 42) {} - void tearDown() { - delete testling; - delete top; - delete bottom; - } + void setUp() { + bottom = std::unique_ptr<DummySettingsProvider>(new DummySettingsProvider()); + top = std::unique_ptr<DummySettingsProvider>(new DummySettingsProvider()); + testling = std::unique_ptr<SettingsProviderHierachy>(new SettingsProviderHierachy()); + testling->addProviderToTopOfStack(bottom.get()); + testling->addProviderToTopOfStack(top.get()); + } - void testEmpty() { - CPPUNIT_ASSERT_EQUAL(42, testling->getSetting(setting1)); - } + void testEmpty() { + CPPUNIT_ASSERT_EQUAL(42, testling->getSetting(setting1)); + } - void testTop() { - top->storeSetting(setting1, 37); - CPPUNIT_ASSERT_EQUAL(37, testling->getSetting(setting1)); - } + void testTop() { + top->storeSetting(setting1, 37); + CPPUNIT_ASSERT_EQUAL(37, testling->getSetting(setting1)); + } - void testBottom() { - bottom->storeSetting(setting1, 17); - CPPUNIT_ASSERT_EQUAL(17, testling->getSetting(setting1)); - } + void testBottom() { + bottom->storeSetting(setting1, 17); + CPPUNIT_ASSERT_EQUAL(17, testling->getSetting(setting1)); + } - void testBoth() { - bottom->storeSetting(setting1, 17); - top->storeSetting(setting1, 37); - CPPUNIT_ASSERT_EQUAL(37, testling->getSetting(setting1)); - } + void testBoth() { + bottom->storeSetting(setting1, 17); + top->storeSetting(setting1, 37); + CPPUNIT_ASSERT_EQUAL(37, testling->getSetting(setting1)); + } - void testTopDefault() { - bottom->storeSetting(setting1, 17); - top->storeSetting(setting1, 42); - CPPUNIT_ASSERT_EQUAL(42, testling->getSetting(setting1)); - } + void testTopDefault() { + bottom->storeSetting(setting1, 17); + top->storeSetting(setting1, 42); + CPPUNIT_ASSERT_EQUAL(42, testling->getSetting(setting1)); + } - void testBottomOverrides() { - bottom->storeSetting(setting1, 17); - bottom->setFinal(setting1.getKey()); - top->storeSetting(setting1, 5); - CPPUNIT_ASSERT_EQUAL(17, testling->getSetting(setting1)); - } + void testBottomOverrides() { + bottom->storeSetting(setting1, 17); + bottom->setFinal(setting1.getKey()); + top->storeSetting(setting1, 5); + CPPUNIT_ASSERT_EQUAL(17, testling->getSetting(setting1)); + } - void testFinal() { - bottom->storeSetting(setting1, 17); - bottom->setFinal(setting1.getKey()); - testling->storeSetting(setting1, 5); - CPPUNIT_ASSERT_EQUAL(17, testling->getSetting(setting1)); - } + void testFinal() { + bottom->storeSetting(setting1, 17); + bottom->setFinal(setting1.getKey()); + testling->storeSetting(setting1, 5); + CPPUNIT_ASSERT_EQUAL(17, testling->getSetting(setting1)); + } private: - SettingsProviderHierachy* testling; - DummySettingsProvider* bottom; - DummySettingsProvider* top; - SettingsProvider::Setting<int> setting1; + std::unique_ptr<SettingsProviderHierachy> testling; + std::unique_ptr<DummySettingsProvider> bottom; + std::unique_ptr<DummySettingsProvider> top; + SettingsProvider::Setting<int> setting1; }; CPPUNIT_TEST_SUITE_REGISTRATION(SettingsProviderHierachyTest); diff --git a/Swift/Controllers/Settings/XMLSettingsProvider.cpp b/Swift/Controllers/Settings/XMLSettingsProvider.cpp index e83ed24..a316cef 100644 --- a/Swift/Controllers/Settings/XMLSettingsProvider.cpp +++ b/Swift/Controllers/Settings/XMLSettingsProvider.cpp @@ -1,35 +1,34 @@ /* - * Copyright (c) 2012 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2012-2019 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #include <Swift/Controllers/Settings/XMLSettingsProvider.h> -#include <boost/lexical_cast.hpp> #include <boost/algorithm/string.hpp> +#include <boost/lexical_cast.hpp> +#include <Swiften/Base/Log.h> #include <Swiften/Parser/PlatformXMLParserFactory.h> #include <Swiften/Parser/XMLParser.h> -#include <Swiften/Base/Log.h> namespace Swift { XMLSettingsProvider::XMLSettingsProvider(const std::string& xmlConfig) : level_(0) { - if (!xmlConfig.empty()) { - PlatformXMLParserFactory factory; - XMLParser* parser = factory.createXMLParser(this); - if (parser->parse(xmlConfig)) { - SWIFT_LOG(debug) << "Found and parsed system config" << std::endl; - } - else { - SWIFT_LOG(debug) << "Found invalid system config" << std::endl; - } - delete parser; - } - else { - SWIFT_LOG(debug) << "No system config found" << std::endl; - } + if (!xmlConfig.empty()) { + PlatformXMLParserFactory factory; + auto parser = factory.createXMLParser(this, true); + if (parser->parse(xmlConfig)) { + SWIFT_LOG(debug) << "Found and parsed system config"; + } + else { + SWIFT_LOG(debug) << "Found invalid system config"; + } + } + else { + SWIFT_LOG(debug) << "No system config found"; + } } XMLSettingsProvider::~XMLSettingsProvider() { @@ -37,94 +36,90 @@ XMLSettingsProvider::~XMLSettingsProvider() { } bool XMLSettingsProvider::hasSetting(const std::string& key) { - return (values_.find(key) != values_.end()); + return (values_.find(key) != values_.end()); } std::string XMLSettingsProvider::getSetting(const Setting<std::string>& setting) { - if (values_.find(setting.getKey()) != values_.end()) { - std::string value = values_[setting.getKey()]; - return value; - } - return setting.getDefaultValue(); + if (values_.find(setting.getKey()) != values_.end()) { + std::string value = values_[setting.getKey()]; + return value; + } + return setting.getDefaultValue(); } void XMLSettingsProvider::storeSetting(const Setting<std::string>& /*settingPath*/, const std::string& /*settingValue*/) { - assert(false); + assert(false); } bool XMLSettingsProvider::getSetting(const Setting<bool>& setting) { - if (values_.find(setting.getKey()) != values_.end()) { - std::string value = values_[setting.getKey()]; - return boost::iequals(value, "true") || value == "1"; - } - return setting.getDefaultValue(); + if (values_.find(setting.getKey()) != values_.end()) { + std::string value = values_[setting.getKey()]; + return boost::iequals(value, "true") || value == "1"; + } + return setting.getDefaultValue(); } void XMLSettingsProvider::storeSetting(const Setting<bool>& /*settingPath*/, const bool& /*settingValue*/) { - assert(false); + assert(false); } int XMLSettingsProvider::getSetting(const Setting<int>& setting) { - if (values_.find(setting.getKey()) != values_.end()) { - std::string value = values_[setting.getKey()]; - try { - return value.empty() ? setting.getDefaultValue() : boost::lexical_cast<int>(value);; - } - catch(boost::bad_lexical_cast &) {} - } - return setting.getDefaultValue(); + if (values_.find(setting.getKey()) != values_.end()) { + std::string value = values_[setting.getKey()]; + try { + return value.empty() ? setting.getDefaultValue() : boost::lexical_cast<int>(value); + } + catch(boost::bad_lexical_cast &) {} + } + return setting.getDefaultValue(); } void XMLSettingsProvider::storeSetting(const Setting<int>& /*settingPath*/, const int& /*settingValue*/) { - assert(false); + assert(false); } std::vector<std::string> XMLSettingsProvider::getAvailableProfiles() { - assert(false); - return std::vector<std::string>(); + assert(false); + return std::vector<std::string>(); } void XMLSettingsProvider::createProfile(const std::string& /*profile*/) { - assert(false); + assert(false); } void XMLSettingsProvider::removeProfile(const std::string& /*profile*/) { - assert(false); + assert(false); } bool XMLSettingsProvider::getIsSettingFinal(const std::string& settingPath) { - return finals_.count(settingPath); + return finals_.count(settingPath); } void XMLSettingsProvider::handleStartElement(const std::string& element, const std::string& /*ns*/, const AttributeMap& attributes) { - level_++; - if (level_ == SettingLevel) { - if (attributes.getBoolAttribute("final", false)) { - finals_.insert(element); - } - currentElement_ = element; - currentText_ = ""; - } + level_++; + if (level_ == SettingLevel) { + if (attributes.getBoolAttribute("final", false)) { + finals_.insert(element); + } + currentElement_ = element; + currentText_ = ""; + } } void XMLSettingsProvider::handleEndElement(const std::string& /*element*/, const std::string& /*ns*/) { - if (level_ == SettingLevel) { - values_[currentElement_] = currentText_; - SWIFT_LOG(debug) << "Setting value of " << currentElement_ << " to " << currentText_ << std::endl; - } - level_--; + if (level_ == SettingLevel) { + values_[currentElement_] = currentText_; + SWIFT_LOG(debug) << "Setting value of " << currentElement_ << " to " << currentText_; + } + level_--; } void XMLSettingsProvider::handleCharacterData(const std::string& data) { - if (level_ >= SettingLevel) { - currentText_ += data; - } + if (level_ >= SettingLevel) { + currentText_ += data; + } } } - - - - diff --git a/Swift/Controllers/Settings/XMLSettingsProvider.h b/Swift/Controllers/Settings/XMLSettingsProvider.h index e03d3c1..5fd82cf 100644 --- a/Swift/Controllers/Settings/XMLSettingsProvider.h +++ b/Swift/Controllers/Settings/XMLSettingsProvider.h @@ -1,54 +1,55 @@ /* - * Copyright (c) 2012 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2012-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <Swift/Controllers/Settings/SettingsProvider.h> -#include <Swiften/Parser/XMLParserClient.h> - #include <map> #include <set> +#include <Swiften/Parser/XMLParserClient.h> + +#include <Swift/Controllers/Settings/SettingsProvider.h> + namespace Swift { class XMLSettingsProvider : public SettingsProvider, public XMLParserClient { - public: - XMLSettingsProvider(const std::string& xmlConfig); - virtual ~XMLSettingsProvider(); - virtual std::string getSetting(const Setting<std::string>& setting); - virtual void storeSetting(const Setting<std::string>& setting, const std::string& value); - virtual bool getSetting(const Setting<bool>& setting); - virtual void storeSetting(const Setting<bool>& setting, const bool& value); - virtual int getSetting(const Setting<int>& setting); - virtual void storeSetting(const Setting<int>& setting, const int& value); - virtual std::vector<std::string> getAvailableProfiles(); - virtual void createProfile(const std::string& profile); - virtual void removeProfile(const std::string& profile); - virtual bool hasSetting(const std::string& key); - - - virtual void handleStartElement(const std::string& element, const std::string& ns, const AttributeMap& attributes); - virtual void handleEndElement(const std::string& element, const std::string& ns); - virtual void handleCharacterData(const std::string& data); - - protected: - virtual bool getIsSettingFinal(const std::string& settingPath); - private: - std::map<std::string /*settingPath*/, std::string /*settingValue*/> values_; - /* Settings that are final*/ - std::set<std::string /*settingPath*/> finals_; - - enum Level { - TopLevel = 0, - SettingLevel = 2 - }; - - int level_; - std::string currentElement_; - std::string currentText_; + public: + XMLSettingsProvider(const std::string& xmlConfig); + virtual ~XMLSettingsProvider(); + virtual std::string getSetting(const Setting<std::string>& setting); + virtual void storeSetting(const Setting<std::string>& setting, const std::string& value); + virtual bool getSetting(const Setting<bool>& setting); + virtual void storeSetting(const Setting<bool>& setting, const bool& value); + virtual int getSetting(const Setting<int>& setting); + virtual void storeSetting(const Setting<int>& setting, const int& value); + virtual std::vector<std::string> getAvailableProfiles(); + virtual void createProfile(const std::string& profile); + virtual void removeProfile(const std::string& profile); + virtual bool hasSetting(const std::string& key); + + + virtual void handleStartElement(const std::string& element, const std::string& ns, const AttributeMap& attributes); + virtual void handleEndElement(const std::string& element, const std::string& ns); + virtual void handleCharacterData(const std::string& data); + + protected: + virtual bool getIsSettingFinal(const std::string& settingPath); + private: + std::map<std::string /*settingPath*/, std::string /*settingValue*/> values_; + /* Settings that are final*/ + std::set<std::string /*settingPath*/> finals_; + + enum Level { + TopLevel = 0, + SettingLevel = 2 + }; + + int level_; + std::string currentElement_; + std::string currentText_; }; } diff --git a/Swift/Controllers/ShowProfileController.cpp b/Swift/Controllers/ShowProfileController.cpp index 15b7b26..a7d7d04 100644 --- a/Swift/Controllers/ShowProfileController.cpp +++ b/Swift/Controllers/ShowProfileController.cpp @@ -4,13 +4,20 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ -#include "ShowProfileController.h" +/* + * Copyright (c) 2014-2017 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#include <Swift/Controllers/ShowProfileController.h> #include <boost/bind.hpp> +#include <boost/date_time/posix_time/posix_time.hpp> -#include <Swiften/Base/foreach.h> #include <Swiften/VCards/VCardManager.h> +#include <Swift/Controllers/Intl.h> #include <Swift/Controllers/UIEvents/ShowProfileForRosterItemUIEvent.h> #include <Swift/Controllers/UIEvents/UIEventStream.h> #include <Swift/Controllers/UIInterfaces/ProfileWindowFactory.h> @@ -18,57 +25,70 @@ namespace Swift { ShowProfileController::ShowProfileController(VCardManager* vcardManager, ProfileWindowFactory* profileWindowFactory, UIEventStream* uiEventStream) : vcardManager(vcardManager), profileWindowFactory(profileWindowFactory), uiEventStream(uiEventStream) { - uiEventStream->onUIEvent.connect(boost::bind(&ShowProfileController::handleUIEvent, this, _1)); - vcardManager->onVCardChanged.connect(boost::bind(&ShowProfileController::handleVCardChanged, this, _1, _2)); + uiEventStream->onUIEvent.connect(boost::bind(&ShowProfileController::handleUIEvent, this, _1)); + vcardManager->onVCardChanged.connect(boost::bind(&ShowProfileController::handleVCardChanged, this, _1, _2)); + vcardManager->onVCardRetrievalError.connect(boost::bind(&ShowProfileController::handleVCardRetrievalError, this, _1, _2)); } ShowProfileController::~ShowProfileController() { - typedef std::pair<JID, ProfileWindow*> JIDProfileWindowPair; - foreach(const JIDProfileWindowPair& jidWndPair, openedProfileWindows) { - jidWndPair.second->onWindowAboutToBeClosed.disconnect(boost::bind(&ShowProfileController::handleProfileWindowAboutToBeClosed, this, _1)); - delete jidWndPair.second; - } - - vcardManager->onVCardChanged.disconnect(boost::bind(&ShowProfileController::handleVCardChanged, this, _1, _2)); - uiEventStream->onUIEvent.disconnect(boost::bind(&ShowProfileController::handleUIEvent, this, _1)); + for (const auto& jidWndPair : openedProfileWindows) { + jidWndPair.second->onWindowAboutToBeClosed.disconnect(boost::bind(&ShowProfileController::handleProfileWindowAboutToBeClosed, this, _1)); + delete jidWndPair.second; + } + + vcardManager->onVCardRetrievalError.disconnect(boost::bind(&ShowProfileController::handleVCardRetrievalError, this, _1, _2)); + vcardManager->onVCardChanged.disconnect(boost::bind(&ShowProfileController::handleVCardChanged, this, _1, _2)); + uiEventStream->onUIEvent.disconnect(boost::bind(&ShowProfileController::handleUIEvent, this, _1)); } void ShowProfileController::handleUIEvent(UIEvent::ref event) { - ShowProfileForRosterItemUIEvent::ref showProfileEvent = boost::dynamic_pointer_cast<ShowProfileForRosterItemUIEvent>(event); - if (!showProfileEvent) { - return; - } - - if (openedProfileWindows.find(showProfileEvent->getJID()) == openedProfileWindows.end()) { - ProfileWindow* newProfileWindow = profileWindowFactory->createProfileWindow(); - newProfileWindow->setJID(showProfileEvent->getJID()); - newProfileWindow->onWindowAboutToBeClosed.connect(boost::bind(&ShowProfileController::handleProfileWindowAboutToBeClosed, this, _1)); - openedProfileWindows[showProfileEvent->getJID()] = newProfileWindow; - VCard::ref vcard = vcardManager->getVCardAndRequestWhenNeeded(showProfileEvent->getJID()); - if (vcard) { - newProfileWindow->setVCard(vcard); - } else { - newProfileWindow->setProcessing(true); - } - newProfileWindow->show(); - } else { - openedProfileWindows[showProfileEvent->getJID()]->show(); - } + ShowProfileForRosterItemUIEvent::ref showProfileEvent = std::dynamic_pointer_cast<ShowProfileForRosterItemUIEvent>(event); + if (!showProfileEvent) { + return; + } + + if (openedProfileWindows.find(showProfileEvent->getJID()) == openedProfileWindows.end()) { + ProfileWindow* newProfileWindow = profileWindowFactory->createProfileWindow(); + newProfileWindow->setJID(showProfileEvent->getJID()); + newProfileWindow->onWindowAboutToBeClosed.connect(boost::bind(&ShowProfileController::handleProfileWindowAboutToBeClosed, this, _1)); + openedProfileWindows[showProfileEvent->getJID()] = newProfileWindow; + VCard::ref vcard = vcardManager->getVCardAndRequestWhenNeeded(showProfileEvent->getJID(), boost::posix_time::minutes(5)); + if (vcard) { + newProfileWindow->setVCard(vcard); + } else { + newProfileWindow->setProcessing(true); + } + newProfileWindow->setError(""); + newProfileWindow->show(); + } else { + openedProfileWindows[showProfileEvent->getJID()]->show(); + } } void ShowProfileController::handleVCardChanged(const JID& jid, VCard::ref vcard) { - if (openedProfileWindows.find(jid) == openedProfileWindows.end()) { - return; - } - - ProfileWindow* profileWindow = openedProfileWindows[jid]; - profileWindow->setVCard(vcard); - profileWindow->setProcessing(false); - profileWindow->show(); + if (openedProfileWindows.find(jid) == openedProfileWindows.end()) { + return; + } + + ProfileWindow* profileWindow = openedProfileWindows[jid]; + profileWindow->setVCard(vcard); + profileWindow->setProcessing(false); + profileWindow->show(); } void ShowProfileController::handleProfileWindowAboutToBeClosed(const JID& profileJid) { - openedProfileWindows.erase(profileJid); + openedProfileWindows.erase(profileJid); +} + +void ShowProfileController::handleVCardRetrievalError(const JID& jid, ErrorPayload::ref /* error */) { + if (openedProfileWindows.find(jid) == openedProfileWindows.end()) { + return; + } + + auto profileWindow = openedProfileWindows[jid]; + profileWindow->setError(QT_TRANSLATE_NOOP("", "Failed to retrieve recent profile for user.")); + profileWindow->setProcessing(false); + profileWindow->show(); } } diff --git a/Swift/Controllers/ShowProfileController.h b/Swift/Controllers/ShowProfileController.h index 27a0cf4..0d01ba1 100644 --- a/Swift/Controllers/ShowProfileController.h +++ b/Swift/Controllers/ShowProfileController.h @@ -4,33 +4,41 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ +/* + * Copyright (c) 2016-2017 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + #pragma once -#include <Swiften/JID/JID.h> +#include <Swiften/Elements/ErrorPayload.h> #include <Swiften/Elements/VCard.h> +#include <Swiften/JID/JID.h> #include <Swift/Controllers/UIEvents/UIEvent.h> namespace Swift { - class VCardManager; - class ProfileWindow; - class ProfileWindowFactory; - class UIEventStream; - - class ShowProfileController { - public: - ShowProfileController(VCardManager* vcardManager, ProfileWindowFactory* profileWindowFactory, UIEventStream* uiEventStream); - ~ShowProfileController(); - - private: - void handleUIEvent(UIEvent::ref event); - void handleVCardChanged(const JID&, VCard::ref); - void handleProfileWindowAboutToBeClosed(const JID& profileJid); - - private: - VCardManager* vcardManager; - ProfileWindowFactory* profileWindowFactory; - UIEventStream* uiEventStream; - std::map<JID, ProfileWindow*> openedProfileWindows; - }; + class VCardManager; + class ProfileWindow; + class ProfileWindowFactory; + class UIEventStream; + + class ShowProfileController { + public: + ShowProfileController(VCardManager* vcardManager, ProfileWindowFactory* profileWindowFactory, UIEventStream* uiEventStream); + ~ShowProfileController(); + + private: + void handleUIEvent(UIEvent::ref event); + void handleVCardChanged(const JID&, VCard::ref); + void handleProfileWindowAboutToBeClosed(const JID& profileJid); + void handleVCardRetrievalError(const JID& jid, ErrorPayload::ref error); + + private: + VCardManager* vcardManager; + ProfileWindowFactory* profileWindowFactory; + UIEventStream* uiEventStream; + std::map<JID, ProfileWindow*> openedProfileWindows; + }; } diff --git a/Swift/Controllers/SoundEventController.cpp b/Swift/Controllers/SoundEventController.cpp index a5171e2..2bafcca 100644 --- a/Swift/Controllers/SoundEventController.cpp +++ b/Swift/Controllers/SoundEventController.cpp @@ -1,56 +1,56 @@ /* - * Copyright (c) 2010-2012 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2017 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #include <Swift/Controllers/SoundEventController.h> #include <boost/bind.hpp> -#include <Swift/Controllers/XMPPEvents/EventController.h> +#include <Swift/Controllers/Highlighting/HighlightManager.h> +#include <Swift/Controllers/SettingConstants.h> #include <Swift/Controllers/SoundPlayer.h> #include <Swift/Controllers/UIEvents/UIEventStream.h> -#include <Swift/Controllers/SettingConstants.h> -#include <Swift/Controllers/HighlightManager.h> +#include <Swift/Controllers/XMPPEvents/EventController.h> +#include <Swift/Controllers/XMPPEvents/IncomingFileTransferEvent.h> namespace Swift { SoundEventController::SoundEventController(EventController* eventController, SoundPlayer* soundPlayer, SettingsProvider* settings, HighlightManager* highlightManager) { - settings_ = settings; - eventController_ = eventController; - soundPlayer_ = soundPlayer; - eventController_->onEventQueueEventAdded.connect(boost::bind(&SoundEventController::handleEventQueueEventAdded, this, _1)); - highlightManager_ = highlightManager; - highlightManager_->onHighlight.connect(boost::bind(&SoundEventController::handleHighlight, this, _1)); + settings_ = settings; + eventController_ = eventController; + soundPlayer_ = soundPlayer; + eventController_->onEventQueueEventAdded.connect(boost::bind(&SoundEventController::handleEventQueueEventAdded, this, _1)); + highlightManager_ = highlightManager; + highlightManager_->onHighlight.connect(boost::bind(&SoundEventController::handleHighlight, this, _1)); - settings_->onSettingChanged.connect(boost::bind(&SoundEventController::handleSettingChanged, this, _1)); + settings_->onSettingChanged.connect(boost::bind(&SoundEventController::handleSettingChanged, this, _1)); - playSounds_ = settings->getSetting(SettingConstants::PLAY_SOUNDS); + playSounds_ = settings->getSetting(SettingConstants::PLAY_SOUNDS); } -void SoundEventController::handleEventQueueEventAdded(boost::shared_ptr<StanzaEvent> /*event*/) { - // message received sound is now played via highlighting - //if (playSounds_ && !event->getConcluded()) { - // soundPlayer_->playSound(SoundPlayer::MessageReceived); - //} +void SoundEventController::handleEventQueueEventAdded(std::shared_ptr<StanzaEvent> event) { + if (playSounds_ && std::dynamic_pointer_cast<IncomingFileTransferEvent>(event)) { + soundPlayer_->playSound(SoundPlayer::MessageReceived, ""); + } } void SoundEventController::handleHighlight(const HighlightAction& action) { - if (playSounds_ && action.playSound()) { - soundPlayer_->playSound(SoundPlayer::MessageReceived, action.getSoundFile()); - } + if (playSounds_ && action.getSoundFilePath()) { + soundPlayer_->playSound(SoundPlayer::MessageReceived, action.getSoundFilePath().get_value_or("")); + } } void SoundEventController::setPlaySounds(bool playSounds) { - playSounds_ = playSounds; - settings_->storeSetting(SettingConstants::PLAY_SOUNDS, playSounds); + playSounds_ = playSounds; + settings_->storeSetting(SettingConstants::PLAY_SOUNDS, playSounds); } void SoundEventController::handleSettingChanged(const std::string& settingPath) { - if (SettingConstants::PLAY_SOUNDS.getKey() == settingPath) { - playSounds_ = settings_->getSetting(SettingConstants::PLAY_SOUNDS); - } + if (SettingConstants::PLAY_SOUNDS.getKey() == settingPath) { + playSounds_ = settings_->getSetting(SettingConstants::PLAY_SOUNDS); + } } } diff --git a/Swift/Controllers/SoundEventController.h b/Swift/Controllers/SoundEventController.h index c9dcab4..d612b18 100644 --- a/Swift/Controllers/SoundEventController.h +++ b/Swift/Controllers/SoundEventController.h @@ -1,34 +1,37 @@ /* - * Copyright (c) 2010-2012 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2017 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <boost/shared_ptr.hpp> +#include <memory> -#include <Swift/Controllers/XMPPEvents/StanzaEvent.h> +#include <Swift/Controllers/Highlighting/HighlightAction.h> #include <Swift/Controllers/Settings/SettingsProvider.h> -#include <Swift/Controllers/HighlightAction.h> +#include <Swift/Controllers/XMPPEvents/StanzaEvent.h> namespace Swift { - class EventController; - class SoundPlayer; - class HighlightManager; - class SoundEventController { - public: - SoundEventController(EventController* eventController, SoundPlayer* soundPlayer, SettingsProvider* settings, HighlightManager* highlightManager); - void setPlaySounds(bool playSounds); - bool getSoundEnabled() {return playSounds_;} - private: - void handleSettingChanged(const std::string& settingPath); - void handleEventQueueEventAdded(boost::shared_ptr<StanzaEvent> event); - void handleHighlight(const HighlightAction& action); - EventController* eventController_; - SoundPlayer* soundPlayer_; - bool playSounds_; - SettingsProvider* settings_; - HighlightManager* highlightManager_; - }; + class EventController; + class SoundPlayer; + class HighlightManager; + class SoundEventController { + public: + SoundEventController(EventController* eventController, SoundPlayer* soundPlayer, SettingsProvider* settings, HighlightManager* highlightManager); + void setPlaySounds(bool playSounds); + bool getSoundEnabled() {return playSounds_;} + + private: + void handleSettingChanged(const std::string& settingPath); + void handleEventQueueEventAdded(std::shared_ptr<StanzaEvent> event); + void handleHighlight(const HighlightAction& action); + + private: + EventController* eventController_; + SoundPlayer* soundPlayer_; + bool playSounds_; + SettingsProvider* settings_; + HighlightManager* highlightManager_; + }; } diff --git a/Swift/Controllers/SoundPlayer.h b/Swift/Controllers/SoundPlayer.h index f18a2c0..8525c6c 100644 --- a/Swift/Controllers/SoundPlayer.h +++ b/Swift/Controllers/SoundPlayer.h @@ -1,7 +1,7 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once @@ -9,10 +9,10 @@ #include <string> namespace Swift { - class SoundPlayer { - public: - virtual ~SoundPlayer() {} - enum SoundEffect{MessageReceived}; - virtual void playSound(SoundEffect sound, const std::string& soundResource) = 0; - }; + class SoundPlayer { + public: + virtual ~SoundPlayer() {} + enum SoundEffect{MessageReceived}; + virtual void playSound(SoundEffect sound, const std::string& soundResource) = 0; + }; } diff --git a/Swift/Controllers/StatusCache.cpp b/Swift/Controllers/StatusCache.cpp index 3444189..f9196f6 100644 --- a/Swift/Controllers/StatusCache.cpp +++ b/Swift/Controllers/StatusCache.cpp @@ -1,31 +1,28 @@ /* - * Copyright (c) 2012 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2012-2019 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #include <Swift/Controllers/StatusCache.h> #include <boost/algorithm/string.hpp> -#include <boost/lexical_cast.hpp> #include <boost/filesystem/fstream.hpp> -#include <boost/lambda/lambda.hpp> -#include <boost/lambda/bind.hpp> +#include <boost/lexical_cast.hpp> -#include <Swiften/Base/foreach.h> #include <Swiften/Base/ByteArray.h> -#include <SwifTools/Application/ApplicationPathProvider.h> +#include <Swiften/Base/Log.h> -namespace lambda = boost::lambda; +#include <SwifTools/Application/ApplicationPathProvider.h> namespace Swift { static const size_t MAX_ENTRIES = 200; StatusCache::StatusCache(ApplicationPathProvider* paths) { - paths_ = paths; - path_ = paths_->getDataDir() / "StatusCache"; - loadRecents(); + paths_ = paths; + path_ = paths_->getDataDir() / "StatusCache"; + loadRecents(); } StatusCache::~StatusCache() { @@ -33,71 +30,78 @@ StatusCache::~StatusCache() { } std::vector<StatusCache::PreviousStatus> StatusCache::getMatches(const std::string& substring, size_t maxCount) const { - std::vector<PreviousStatus> matches; - foreach (const PreviousStatus& status, previousStatuses_) { - if (substring.empty() || (boost::algorithm::ifind_first(status.first, substring) && substring != status.first)) { - matches.push_back(status); - if (matches.size() == maxCount) { - break; - } - } - } - return matches; + std::vector<PreviousStatus> matches; + for (const auto& status : previousStatuses_) { + if (substring.empty() || (boost::algorithm::ifind_first(status.first, substring) && substring != status.first)) { + matches.push_back(status); + if (matches.size() == maxCount) { + break; + } + } + } + return matches; } void StatusCache::addRecent(const std::string& text, StatusShow::Type type) { - if (text.empty()) { - return; - } - previousStatuses_.remove_if(lambda::bind(&PreviousStatus::first, lambda::_1) == text && lambda::bind(&PreviousStatus::second, lambda::_1) == type); - previousStatuses_.push_front(PreviousStatus(text, type)); - for (size_t i = previousStatuses_.size(); i > MAX_ENTRIES; i--) { - previousStatuses_.pop_back(); - } - saveRecents(); + if (text.empty()) { + return; + } + previousStatuses_.remove_if([&](const PreviousStatus& previousStatus) { + return previousStatus.first == text && previousStatus.second == type; + }); + previousStatuses_.push_front(PreviousStatus(text, type)); + for (size_t i = previousStatuses_.size(); i > MAX_ENTRIES; i--) { + previousStatuses_.pop_back(); + } + saveRecents(); } void StatusCache::loadRecents() { - try { - if (boost::filesystem::exists(path_)) { - ByteArray data; - readByteArrayFromFile(data, path_); - std::string stringData = byteArrayToString(data); - std::vector<std::string> lines; - boost::split(lines, stringData, boost::is_any_of("\n")); - foreach (const std::string& line, lines) { - std::vector<std::string> bits; - boost::split(bits, line, boost::is_any_of("\t")); - if (bits.size() < 2) { - continue; - } - StatusShow::Type type; - type = static_cast<StatusShow::Type>(boost::lexical_cast<size_t>(bits[0])); - previousStatuses_.push_back(PreviousStatus(boost::trim_copy(bits[1]), type)); - } - } - } - catch (const boost::filesystem::filesystem_error& e) { - std::cerr << "ERROR: " << e.what() << std::endl; - } + try { + if (boost::filesystem::exists(path_)) { + ByteArray data; + readByteArrayFromFile(data, path_); + std::string stringData = byteArrayToString(data); + std::vector<std::string> lines; + boost::split(lines, stringData, boost::is_any_of("\n")); + for (const auto& line : lines) { + std::vector<std::string> bits; + boost::split(bits, line, boost::is_any_of("\t")); + if (bits.size() < 2) { + continue; + } + StatusShow::Type type; + try { + type = static_cast<StatusShow::Type>(boost::lexical_cast<size_t>(bits[0])); + previousStatuses_.push_back(PreviousStatus(boost::trim_copy(bits[1]), type)); + } + catch (const boost::bad_lexical_cast& e) { + SWIFT_LOG(error) << "Failed to load recent status cache entry: " << e.what(); + } + } + } + } + catch (const boost::filesystem::filesystem_error& e) { + SWIFT_LOG(error) << "Failed to load recents: " << e.what(); + } } void StatusCache::saveRecents() { - try { - if (!boost::filesystem::exists(path_.parent_path())) { - boost::filesystem::create_directories(path_.parent_path()); - } - boost::filesystem::ofstream file(path_); - foreach (const PreviousStatus& recent, previousStatuses_) { - std::string message = recent.first; - boost::replace_all(message, "\t", " "); - file << recent.second << "\t" << message << std::endl; - } - file.close(); - } - catch (const boost::filesystem::filesystem_error& e) { - std::cerr << "ERROR: " << e.what() << std::endl; - } + try { + if (!boost::filesystem::exists(path_.parent_path())) { + boost::filesystem::create_directories(path_.parent_path()); + } + boost::filesystem::ofstream file(path_); + for (const auto& recent : previousStatuses_) { + std::string message = recent.first; + boost::replace_all(message, "\t", " "); + file << recent.second << "\t" << message << std::endl; + } + file.close(); + } + catch (const boost::filesystem::filesystem_error& e) { + SWIFT_LOG(error) << "Failed to save recents: " << e.what(); + } } } diff --git a/Swift/Controllers/StatusCache.h b/Swift/Controllers/StatusCache.h index 35b3674..83fd17a 100644 --- a/Swift/Controllers/StatusCache.h +++ b/Swift/Controllers/StatusCache.h @@ -1,40 +1,41 @@ /* - * Copyright (c) 2012 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2012-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once +#include <list> #include <string> #include <utility> -#include <vector> -#include <list> -#include <iostream> #include <boost/filesystem/path.hpp> #include <Swiften/Elements/StatusShow.h> namespace Swift { - class ApplicationPathProvider; - class StatusCache { - public: - typedef std::pair<std::string, StatusShow::Type> PreviousStatus; - public: - StatusCache(ApplicationPathProvider* paths); - ~StatusCache(); - - std::vector<PreviousStatus> getMatches(const std::string& substring, size_t maxCount) const; - void addRecent(const std::string& text, StatusShow::Type type); - private: - void saveRecents(); - void loadRecents(); - private: - boost::filesystem::path path_; - std::list<PreviousStatus> previousStatuses_; - ApplicationPathProvider* paths_; - }; + class ApplicationPathProvider; + + class StatusCache { + public: + typedef std::pair<std::string, StatusShow::Type> PreviousStatus; + public: + StatusCache(ApplicationPathProvider* paths); + ~StatusCache(); + + std::vector<PreviousStatus> getMatches(const std::string& substring, size_t maxCount) const; + void addRecent(const std::string& text, StatusShow::Type type); + + private: + void saveRecents(); + void loadRecents(); + + private: + boost::filesystem::path path_; + std::list<PreviousStatus> previousStatuses_; + ApplicationPathProvider* paths_; + }; } diff --git a/Swift/Controllers/StatusTracker.cpp b/Swift/Controllers/StatusTracker.cpp index 6766c2e..56cd27f 100644 --- a/Swift/Controllers/StatusTracker.cpp +++ b/Swift/Controllers/StatusTracker.cpp @@ -1,58 +1,58 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ -#include "Swift/Controllers/StatusTracker.h" +#include <Swift/Controllers/StatusTracker.h> -#include <boost/smart_ptr/make_shared.hpp> +#include <memory> #include <Swiften/Elements/Idle.h> namespace Swift { StatusTracker::StatusTracker() { - isAutoAway_ = false; - queuedPresence_ = boost::make_shared<Presence>(); + isAutoAway_ = false; + queuedPresence_ = std::make_shared<Presence>(); } -boost::shared_ptr<Presence> StatusTracker::getNextPresence() { - boost::shared_ptr<Presence> presence; - if (isAutoAway_) { - presence = boost::make_shared<Presence>(); - presence->setShow(StatusShow::Away); - presence->setStatus(queuedPresence_->getStatus()); - presence->addPayload(boost::make_shared<Idle>(isAutoAwaySince_)); - } else { - presence = queuedPresence_; - } - return presence; +std::shared_ptr<Presence> StatusTracker::getNextPresence() { + std::shared_ptr<Presence> presence; + if (isAutoAway_) { + presence = std::make_shared<Presence>(); + presence->setShow(StatusShow::Away); + presence->setStatus(queuedPresence_->getStatus()); + presence->addPayload(std::make_shared<Idle>(isAutoAwaySince_)); + } else { + presence = queuedPresence_; + } + return presence; } -void StatusTracker::setRequestedPresence(boost::shared_ptr<Presence> presence) { - isAutoAway_ = false; - queuedPresence_ = presence; -// if (presence->getType() == Presence::Unavailable) { -// queuedPresence_ = boost::make_shared<Presence>(); -// } +void StatusTracker::setRequestedPresence(std::shared_ptr<Presence> presence) { + isAutoAway_ = false; + queuedPresence_ = presence; +// if (presence->getType() == Presence::Unavailable) { +// queuedPresence_ = std::make_shared<Presence>(); +// } } bool StatusTracker::goAutoAway(const int& seconds) { - if (queuedPresence_->getShow() != StatusShow::Online) { - return false; - } - isAutoAway_ = true; - isAutoAwaySince_ = boost::posix_time::second_clock::universal_time() - boost::posix_time::seconds(seconds); - return true; + if (queuedPresence_->getShow() != StatusShow::Online) { + return false; + } + isAutoAway_ = true; + isAutoAwaySince_ = boost::posix_time::second_clock::universal_time() - boost::posix_time::seconds(seconds); + return true; } bool StatusTracker::goAutoUnAway() { - if (!isAutoAway_) { - return false; - } - isAutoAway_ = false; - return true; + if (!isAutoAway_) { + return false; + } + isAutoAway_ = false; + return true; } } diff --git a/Swift/Controllers/StatusTracker.h b/Swift/Controllers/StatusTracker.h index 10a5c0c..a74ab6e 100644 --- a/Swift/Controllers/StatusTracker.h +++ b/Swift/Controllers/StatusTracker.h @@ -1,29 +1,29 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <boost/shared_ptr.hpp> - -#include "Swiften/Elements/Presence.h" +#include <memory> #include <boost/date_time/posix_time/posix_time_types.hpp> +#include <Swiften/Elements/Presence.h> + namespace Swift { - class StatusTracker { - public: - StatusTracker(); - boost::shared_ptr<Presence> getNextPresence(); - void setRequestedPresence(boost::shared_ptr<Presence> presence); - bool goAutoAway(const int& seconds); - bool goAutoUnAway(); - private: - boost::shared_ptr<Presence> queuedPresence_; - bool isAutoAway_; - boost::posix_time::ptime isAutoAwaySince_; - }; + class StatusTracker { + public: + StatusTracker(); + std::shared_ptr<Presence> getNextPresence(); + void setRequestedPresence(std::shared_ptr<Presence> presence); + bool goAutoAway(const int& seconds); + bool goAutoUnAway(); + private: + std::shared_ptr<Presence> queuedPresence_; + bool isAutoAway_; + boost::posix_time::ptime isAutoAwaySince_; + }; } diff --git a/Swift/Controllers/StatusUtil.cpp b/Swift/Controllers/StatusUtil.cpp index a72f340..1034863 100644 --- a/Swift/Controllers/StatusUtil.cpp +++ b/Swift/Controllers/StatusUtil.cpp @@ -1,27 +1,28 @@ /* - * Copyright (c) 2011 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2011-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #include <Swift/Controllers/StatusUtil.h> #include <cassert> + #include <Swift/Controllers/Intl.h> namespace Swift { std::string statusShowTypeToFriendlyName(StatusShow::Type type) { - switch (type) { - case StatusShow::Online: return QT_TRANSLATE_NOOP("", "Available"); - case StatusShow::FFC: return QT_TRANSLATE_NOOP("", "Available"); - case StatusShow::Away: return QT_TRANSLATE_NOOP("", "Away"); - case StatusShow::XA: return QT_TRANSLATE_NOOP("", "Away"); - case StatusShow::DND: return QT_TRANSLATE_NOOP("", "Busy"); - case StatusShow::None: return QT_TRANSLATE_NOOP("", "Offline"); - } - assert(false); - return ""; + switch (type) { + case StatusShow::Online: return QT_TRANSLATE_NOOP("", "Available"); + case StatusShow::FFC: return QT_TRANSLATE_NOOP("", "Available"); + case StatusShow::Away: return QT_TRANSLATE_NOOP("", "Away"); + case StatusShow::XA: return QT_TRANSLATE_NOOP("", "Away"); + case StatusShow::DND: return QT_TRANSLATE_NOOP("", "Busy"); + case StatusShow::None: return QT_TRANSLATE_NOOP("", "Offline"); + } + assert(false); + return ""; } } diff --git a/Swift/Controllers/StatusUtil.h b/Swift/Controllers/StatusUtil.h index dddee92..6872bf7 100644 --- a/Swift/Controllers/StatusUtil.h +++ b/Swift/Controllers/StatusUtil.h @@ -1,7 +1,7 @@ /* - * Copyright (c) 2011 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2011 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once @@ -11,6 +11,6 @@ #include <Swiften/Elements/StatusShow.h> namespace Swift { - std::string statusShowTypeToFriendlyName(StatusShow::Type type); + std::string statusShowTypeToFriendlyName(StatusShow::Type type); } diff --git a/Swift/Controllers/Storages/AvatarFileStorage.cpp b/Swift/Controllers/Storages/AvatarFileStorage.cpp index 671e0cb..808c432 100644 --- a/Swift/Controllers/Storages/AvatarFileStorage.cpp +++ b/Swift/Controllers/Storages/AvatarFileStorage.cpp @@ -1,105 +1,114 @@ /* - * Copyright (c) 2010-2013 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2019 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #include <Swift/Controllers/Storages/AvatarFileStorage.h> -#include <iostream> -#include <boost/filesystem/fstream.hpp> #include <boost/filesystem.hpp> +#include <boost/filesystem/fstream.hpp> -#include <Swiften/Base/foreach.h> +#include <Swiften/Base/Log.h> #include <Swiften/Base/String.h> -#include <Swiften/StringCodecs/Hexify.h> #include <Swiften/Crypto/CryptoProvider.h> +#include <Swiften/StringCodecs/Hexify.h> namespace Swift { AvatarFileStorage::AvatarFileStorage(const boost::filesystem::path& avatarsDir, const boost::filesystem::path& avatarsFile, CryptoProvider* crypto) : avatarsDir(avatarsDir), avatarsFile(avatarsFile), crypto(crypto) { - if (boost::filesystem::exists(avatarsFile)) { - try { - boost::filesystem::ifstream file(avatarsFile); - std::string line; - if (file.is_open()) { - while (!file.eof()) { - getline(file, line); - std::pair<std::string, std::string> r = String::getSplittedAtFirst(line, ' '); - JID jid(r.second); - if (jid.isValid()) { - jidAvatars.insert(std::make_pair(jid, r.first)); - } - else if (!r.first.empty() || !r.second.empty()) { - std::cerr << "Invalid entry in avatars file: " << r.second << std::endl; - } - } - } - } - catch (...) { - std::cerr << "Error reading avatars file" << std::endl; - } - } + if (boost::filesystem::exists(avatarsFile)) { + try { + boost::filesystem::ifstream file(avatarsFile); + std::string line; + if (file.is_open()) { + while (!file.eof()) { + getline(file, line); + std::pair<std::string, std::string> r = String::getSplittedAtFirst(line, ' '); + JID jid(r.second); + if (jid.isValid()) { + jidAvatars.insert(std::make_pair(jid, r.first)); + } + else if (!r.first.empty() || !r.second.empty()) { + SWIFT_LOG(error) << "Invalid entry in avatars file: " << r.second; + } + } + } + } + catch (...) { + SWIFT_LOG(error) << "Error reading avatars file"; + } + } } -bool AvatarFileStorage::hasAvatar(const std::string& hash) const { - return boost::filesystem::exists(getAvatarPath(hash)); +bool AvatarFileStorage::hasAvatar(const std::string& hash) const { + return boost::filesystem::exists(getAvatarPath(hash)); } void AvatarFileStorage::addAvatar(const std::string& hash, const ByteArray& avatar) { - assert(Hexify::hexify(crypto->getSHA1Hash(avatar)) == hash); + assert(Hexify::hexify(crypto->getSHA1Hash(avatar)) == hash); + + boost::filesystem::path avatarPath = getAvatarPath(hash); + if (!boost::filesystem::exists(avatarPath.parent_path())) { + try { + boost::filesystem::create_directories(avatarPath.parent_path()); + } + catch (const boost::filesystem::filesystem_error& e) { + SWIFT_LOG(error) << "filesystem error: " << e.what(); + } + } - boost::filesystem::path avatarPath = getAvatarPath(hash); - if (!boost::filesystem::exists(avatarPath.parent_path())) { - try { - boost::filesystem::create_directories(avatarPath.parent_path()); - } - catch (const boost::filesystem::filesystem_error& e) { - std::cerr << "ERROR: " << e.what() << std::endl; - } - } - boost::filesystem::ofstream file(avatarPath, boost::filesystem::ofstream::binary|boost::filesystem::ofstream::out); - file.write(reinterpret_cast<const char*>(vecptr(avatar)), static_cast<std::streamsize>(avatar.size())); - file.close(); + try { + boost::filesystem::ofstream file(avatarPath, boost::filesystem::ofstream::binary|boost::filesystem::ofstream::out); + file.write(reinterpret_cast<const char*>(vecptr(avatar)), static_cast<std::streamsize>(avatar.size())); + } + catch (const boost::filesystem::filesystem_error& e) { + SWIFT_LOG(error) << "filesystem error: " << e.what(); + } } boost::filesystem::path AvatarFileStorage::getAvatarPath(const std::string& hash) const { - return avatarsDir / hash; + return avatarsDir / hash; } ByteArray AvatarFileStorage::getAvatar(const std::string& hash) const { - ByteArray data; - readByteArrayFromFile(data, getAvatarPath(hash)); - return data; + ByteArray data; + try { + readByteArrayFromFile(data, getAvatarPath(hash)); + } + catch (const boost::filesystem::filesystem_error& e) { + SWIFT_LOG(error) << "filesystem error: " << e.what(); + } + return data; } void AvatarFileStorage::setAvatarForJID(const JID& jid, const std::string& hash) { - std::pair<JIDAvatarMap::iterator, bool> r = jidAvatars.insert(std::make_pair(jid, hash)); - if (r.second) { - saveJIDAvatars(); - } - else if (r.first->second != hash) { - r.first->second = hash; - saveJIDAvatars(); - } + std::pair<JIDAvatarMap::iterator, bool> r = jidAvatars.insert(std::make_pair(jid, hash)); + if (r.second) { + saveJIDAvatars(); + } + else if (r.first->second != hash) { + r.first->second = hash; + saveJIDAvatars(); + } } std::string AvatarFileStorage::getAvatarForJID(const JID& jid) const { - JIDAvatarMap::const_iterator i = jidAvatars.find(jid); - return i == jidAvatars.end() ? "" : i->second; + JIDAvatarMap::const_iterator i = jidAvatars.find(jid); + return i == jidAvatars.end() ? "" : i->second; } void AvatarFileStorage::saveJIDAvatars() { - try { - boost::filesystem::ofstream file(avatarsFile); - for (JIDAvatarMap::const_iterator i = jidAvatars.begin(); i != jidAvatars.end(); ++i) { - file << i->second << " " << i->first.toString() << std::endl; - } - file.close(); - } - catch (...) { - std::cerr << "Error writing avatars file" << std::endl; - } + try { + boost::filesystem::ofstream file(avatarsFile); + for (JIDAvatarMap::const_iterator i = jidAvatars.begin(); i != jidAvatars.end(); ++i) { + file << i->second << " " << i->first.toString() << std::endl; + } + file.close(); + } + catch (...) { + SWIFT_LOG(error) << "Error writing avatars file"; + } } } diff --git a/Swift/Controllers/Storages/AvatarFileStorage.h b/Swift/Controllers/Storages/AvatarFileStorage.h index 85a6463..41c7106 100644 --- a/Swift/Controllers/Storages/AvatarFileStorage.h +++ b/Swift/Controllers/Storages/AvatarFileStorage.h @@ -1,44 +1,45 @@ /* - * Copyright (c) 2010-2013 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once #include <map> #include <string> + #include <boost/filesystem/path.hpp> +#include <Swiften/Avatars/AvatarStorage.h> +#include <Swiften/Base/ByteArray.h> #include <Swiften/JID/JID.h> -#include "Swiften/Base/ByteArray.h" -#include "Swiften/Avatars/AvatarStorage.h" namespace Swift { - class CryptoProvider; + class CryptoProvider; - class AvatarFileStorage : public AvatarStorage { - public: - AvatarFileStorage(const boost::filesystem::path& avatarsDir, const boost::filesystem::path& avatarsFile, CryptoProvider* crypto); + class AvatarFileStorage : public AvatarStorage { + public: + AvatarFileStorage(const boost::filesystem::path& avatarsDir, const boost::filesystem::path& avatarsFile, CryptoProvider* crypto); - virtual bool hasAvatar(const std::string& hash) const; - virtual void addAvatar(const std::string& hash, const ByteArray& avatar); - virtual ByteArray getAvatar(const std::string& hash) const; + virtual bool hasAvatar(const std::string& hash) const; + virtual void addAvatar(const std::string& hash, const ByteArray& avatar); + virtual ByteArray getAvatar(const std::string& hash) const; - virtual boost::filesystem::path getAvatarPath(const std::string& hash) const; + virtual boost::filesystem::path getAvatarPath(const std::string& hash) const; - virtual void setAvatarForJID(const JID& jid, const std::string& hash); - virtual std::string getAvatarForJID(const JID& jid) const; + virtual void setAvatarForJID(const JID& jid, const std::string& hash); + virtual std::string getAvatarForJID(const JID& jid) const; - private: - void saveJIDAvatars(); + private: + void saveJIDAvatars(); - private: - boost::filesystem::path avatarsDir; - boost::filesystem::path avatarsFile; - CryptoProvider* crypto; - typedef std::map<JID, std::string> JIDAvatarMap; - JIDAvatarMap jidAvatars; - }; + private: + boost::filesystem::path avatarsDir; + boost::filesystem::path avatarsFile; + CryptoProvider* crypto; + typedef std::map<JID, std::string> JIDAvatarMap; + JIDAvatarMap jidAvatars; + }; } diff --git a/Swift/Controllers/Storages/CapsFileStorage.cpp b/Swift/Controllers/Storages/CapsFileStorage.cpp index b7593fd..21a99bc 100644 --- a/Swift/Controllers/Storages/CapsFileStorage.cpp +++ b/Swift/Controllers/Storages/CapsFileStorage.cpp @@ -1,16 +1,16 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ -#include "Swift/Controllers/Storages/CapsFileStorage.h" +#include <Swift/Controllers/Storages/CapsFileStorage.h> #include <Swiften/Entity/GenericPayloadPersister.h> -#include "Swiften/Serializer/PayloadSerializers/DiscoInfoSerializer.h" -#include "Swiften/Parser/PayloadParsers/DiscoInfoParser.h" -#include "Swiften/StringCodecs/Hexify.h" -#include "Swiften/StringCodecs/Base64.h" +#include <Swiften/Parser/PayloadParsers/DiscoInfoParser.h> +#include <Swiften/Serializer/PayloadSerializers/DiscoInfoSerializer.h> +#include <Swiften/StringCodecs/Base64.h> +#include <Swiften/StringCodecs/Hexify.h> using namespace Swift; @@ -20,15 +20,15 @@ CapsFileStorage::CapsFileStorage(const boost::filesystem::path& path) : path(pat } DiscoInfo::ref CapsFileStorage::getDiscoInfo(const std::string& hash) const { - return DiscoInfoPersister().loadPayloadGeneric(getCapsPath(hash)); + return DiscoInfoPersister().loadPayloadGeneric(getCapsPath(hash)); } void CapsFileStorage::setDiscoInfo(const std::string& hash, DiscoInfo::ref discoInfo) { - DiscoInfo::ref bareDiscoInfo(new DiscoInfo(*discoInfo.get())); - bareDiscoInfo->setNode(""); - DiscoInfoPersister().savePayload(bareDiscoInfo, getCapsPath(hash)); + DiscoInfo::ref bareDiscoInfo(new DiscoInfo(*discoInfo.get())); + bareDiscoInfo->setNode(""); + DiscoInfoPersister().savePayload(bareDiscoInfo, getCapsPath(hash)); } boost::filesystem::path CapsFileStorage::getCapsPath(const std::string& hash) const { - return path / (Hexify::hexify(Base64::decode(hash)) + ".xml"); + return path / (Hexify::hexify(Base64::decode(hash)) + ".xml"); } diff --git a/Swift/Controllers/Storages/CapsFileStorage.h b/Swift/Controllers/Storages/CapsFileStorage.h index b3757e0..7df23f1 100644 --- a/Swift/Controllers/Storages/CapsFileStorage.h +++ b/Swift/Controllers/Storages/CapsFileStorage.h @@ -1,28 +1,29 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once +#include <string> + #include <boost/filesystem/path.hpp> -#include "Swiften/Disco/CapsStorage.h" -#include <string> +#include <Swiften/Disco/CapsStorage.h> namespace Swift { - class CapsFileStorage : public CapsStorage { - public: - CapsFileStorage(const boost::filesystem::path& path); + class CapsFileStorage : public CapsStorage { + public: + CapsFileStorage(const boost::filesystem::path& path); - virtual DiscoInfo::ref getDiscoInfo(const std::string& hash) const; - virtual void setDiscoInfo(const std::string& hash, DiscoInfo::ref discoInfo); + virtual DiscoInfo::ref getDiscoInfo(const std::string& hash) const; + virtual void setDiscoInfo(const std::string& hash, DiscoInfo::ref discoInfo); - private: - boost::filesystem::path getCapsPath(const std::string& hash) const; + private: + boost::filesystem::path getCapsPath(const std::string& hash) const; - private: - boost::filesystem::path path; - }; + private: + boost::filesystem::path path; + }; } diff --git a/Swift/Controllers/Storages/CertificateFileStorage.cpp b/Swift/Controllers/Storages/CertificateFileStorage.cpp index 34d1f76..a8661df 100644 --- a/Swift/Controllers/Storages/CertificateFileStorage.cpp +++ b/Swift/Controllers/Storages/CertificateFileStorage.cpp @@ -1,19 +1,20 @@ /* - * Copyright (c) 2010-2013 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2019 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #include <Swift/Controllers/Storages/CertificateFileStorage.h> #include <iostream> + #include <boost/filesystem/fstream.hpp> #include <boost/numeric/conversion/cast.hpp> -#include <Swiften/StringCodecs/Hexify.h> -#include <Swiften/TLS/CertificateFactory.h> #include <Swiften/Base/Log.h> #include <Swiften/Crypto/CryptoProvider.h> +#include <Swiften/StringCodecs/Hexify.h> +#include <Swiften/TLS/CertificateFactory.h> namespace Swift { @@ -21,42 +22,47 @@ CertificateFileStorage::CertificateFileStorage(const boost::filesystem::path& pa } bool CertificateFileStorage::hasCertificate(Certificate::ref certificate) const { - boost::filesystem::path certificatePath = getCertificatePath(certificate); - if (boost::filesystem::exists(certificatePath)) { - ByteArray data; - readByteArrayFromFile(data, certificatePath); - Certificate::ref storedCertificate = certificateFactory->createCertificateFromDER(data); - if (storedCertificate && storedCertificate->toDER() == certificate->toDER()) { - return true; - } - else { - SWIFT_LOG(warning) << "Stored certificate does not match received certificate" << std::endl; - return false; - } - } - else { - return false; - } + boost::filesystem::path certificatePath = getCertificatePath(certificate); + if (boost::filesystem::exists(certificatePath)) { + ByteArray data; + readByteArrayFromFile(data, certificatePath); + Certificate::ref storedCertificate(certificateFactory->createCertificateFromDER(data)); + if (storedCertificate && storedCertificate->toDER() == certificate->toDER()) { + return true; + } + else { + SWIFT_LOG(warning) << "Stored certificate does not match received certificate"; + return false; + } + } + else { + return false; + } } void CertificateFileStorage::addCertificate(Certificate::ref certificate) { - boost::filesystem::path certificatePath = getCertificatePath(certificate); - if (!boost::filesystem::exists(certificatePath.parent_path())) { - try { - boost::filesystem::create_directories(certificatePath.parent_path()); - } - catch (const boost::filesystem::filesystem_error& e) { - std::cerr << "ERROR: " << e.what() << std::endl; - } - } - boost::filesystem::ofstream file(certificatePath, boost::filesystem::ofstream::binary|boost::filesystem::ofstream::out); - ByteArray data = certificate->toDER(); - file.write(reinterpret_cast<const char*>(vecptr(data)), boost::numeric_cast<std::streamsize>(data.size())); - file.close(); + boost::filesystem::path certificatePath = getCertificatePath(certificate); + if (!boost::filesystem::exists(certificatePath.parent_path())) { + try { + boost::filesystem::create_directories(certificatePath.parent_path()); + } + catch (const boost::filesystem::filesystem_error& e) { + std::cerr << "ERROR: " << e.what() << std::endl; + } + } + try { + boost::filesystem::ofstream file(certificatePath, boost::filesystem::ofstream::binary|boost::filesystem::ofstream::out); + ByteArray data = certificate->toDER(); + file.write(reinterpret_cast<const char*>(vecptr(data)), boost::numeric_cast<std::streamsize>(data.size())); + file.close(); + } + catch (...) { + SWIFT_LOG(warning) << "Failed to store certificate to " << certificatePath; + } } boost::filesystem::path CertificateFileStorage::getCertificatePath(Certificate::ref certificate) const { - return path / Hexify::hexify(crypto->getSHA1Hash(certificate->toDER())); + return path / Hexify::hexify(crypto->getSHA1Hash(certificate->toDER())); } } diff --git a/Swift/Controllers/Storages/CertificateFileStorage.h b/Swift/Controllers/Storages/CertificateFileStorage.h index 12151d0..d2c228d 100644 --- a/Swift/Controllers/Storages/CertificateFileStorage.h +++ b/Swift/Controllers/Storages/CertificateFileStorage.h @@ -1,33 +1,33 @@ /* - * Copyright (c) 2010-2013 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once #include <boost/filesystem.hpp> -#include "Swift/Controllers/Storages/CertificateStorage.h" +#include <Swift/Controllers/Storages/CertificateStorage.h> namespace Swift { - class CertificateFactory; - class CryptoProvider; + class CertificateFactory; + class CryptoProvider; - class CertificateFileStorage : public CertificateStorage { - public: - CertificateFileStorage(const boost::filesystem::path& path, CertificateFactory* certificateFactory, CryptoProvider* crypto); + class CertificateFileStorage : public CertificateStorage { + public: + CertificateFileStorage(const boost::filesystem::path& path, CertificateFactory* certificateFactory, CryptoProvider* crypto); - virtual bool hasCertificate(Certificate::ref certificate) const; - virtual void addCertificate(Certificate::ref certificate); + virtual bool hasCertificate(Certificate::ref certificate) const; + virtual void addCertificate(Certificate::ref certificate); - private: - boost::filesystem::path getCertificatePath(Certificate::ref certificate) const; + private: + boost::filesystem::path getCertificatePath(Certificate::ref certificate) const; - private: - boost::filesystem::path path; - CertificateFactory* certificateFactory; - CryptoProvider* crypto; - }; + private: + boost::filesystem::path path; + CertificateFactory* certificateFactory; + CryptoProvider* crypto; + }; } diff --git a/Swift/Controllers/Storages/CertificateFileStorageFactory.h b/Swift/Controllers/Storages/CertificateFileStorageFactory.h index 6834619..6f466dc 100644 --- a/Swift/Controllers/Storages/CertificateFileStorageFactory.h +++ b/Swift/Controllers/Storages/CertificateFileStorageFactory.h @@ -1,30 +1,32 @@ /* - * Copyright (c) 2010-2013 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <Swift/Controllers/Storages/CertificateStorageFactory.h> +#include <Swiften/JID/JID.h> + #include <Swift/Controllers/Storages/CertificateFileStorage.h> +#include <Swift/Controllers/Storages/CertificateStorageFactory.h> namespace Swift { - class CertificateFactory; - class CryptoProvider; + class CertificateFactory; + class CryptoProvider; - class CertificateFileStorageFactory : public CertificateStorageFactory { - public: - CertificateFileStorageFactory(const boost::filesystem::path& basePath, CertificateFactory* certificateFactory, CryptoProvider* crypto) : basePath(basePath), certificateFactory(certificateFactory), crypto(crypto) {} + class CertificateFileStorageFactory : public CertificateStorageFactory { + public: + CertificateFileStorageFactory(const boost::filesystem::path& basePath, CertificateFactory* certificateFactory, CryptoProvider* crypto) : basePath(basePath), certificateFactory(certificateFactory), crypto(crypto) {} - virtual CertificateStorage* createCertificateStorage(const JID& profile) const { - boost::filesystem::path profilePath = basePath / profile.toString(); - return new CertificateFileStorage(profilePath / "certificates", certificateFactory, crypto); - } + virtual CertificateStorage* createCertificateStorage(const JID& profile) const { + boost::filesystem::path profilePath = basePath / profile.toString(); + return new CertificateFileStorage(profilePath / "certificates", certificateFactory, crypto); + } - private: - boost::filesystem::path basePath; - CertificateFactory* certificateFactory; - CryptoProvider* crypto; - }; + private: + boost::filesystem::path basePath; + CertificateFactory* certificateFactory; + CryptoProvider* crypto; + }; } diff --git a/Swift/Controllers/Storages/CertificateMemoryStorage.cpp b/Swift/Controllers/Storages/CertificateMemoryStorage.cpp index 71d7c4a..545ca65 100644 --- a/Swift/Controllers/Storages/CertificateMemoryStorage.cpp +++ b/Swift/Controllers/Storages/CertificateMemoryStorage.cpp @@ -1,27 +1,25 @@ /* - * Copyright (c) 2011 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2011-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #include <Swift/Controllers/Storages/CertificateMemoryStorage.h> -#include <Swiften/Base/foreach.h> - using namespace Swift; CertificateMemoryStorage::CertificateMemoryStorage() { } bool CertificateMemoryStorage::hasCertificate(Certificate::ref certificate) const { - foreach(Certificate::ref storedCert, certificates) { - if (storedCert->toDER() == certificate->toDER()) { - return true; - } - } - return false; + for (auto&& storedCert : certificates) { + if (storedCert->toDER() == certificate->toDER()) { + return true; + } + } + return false; } void CertificateMemoryStorage::addCertificate(Certificate::ref certificate) { - certificates.push_back(certificate); + certificates.push_back(certificate); } diff --git a/Swift/Controllers/Storages/CertificateMemoryStorage.h b/Swift/Controllers/Storages/CertificateMemoryStorage.h index 5c0333d..4870385 100644 --- a/Swift/Controllers/Storages/CertificateMemoryStorage.h +++ b/Swift/Controllers/Storages/CertificateMemoryStorage.h @@ -1,7 +1,7 @@ /* - * Copyright (c) 2011 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2011 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once @@ -11,15 +11,15 @@ #include <Swift/Controllers/Storages/CertificateStorage.h> namespace Swift { - class CertificateMemoryStorage : public CertificateStorage { - public: - CertificateMemoryStorage(); + class CertificateMemoryStorage : public CertificateStorage { + public: + CertificateMemoryStorage(); - virtual bool hasCertificate(Certificate::ref certificate) const; - virtual void addCertificate(Certificate::ref certificate); + virtual bool hasCertificate(Certificate::ref certificate) const; + virtual void addCertificate(Certificate::ref certificate); - private: - std::vector<Certificate::ref> certificates; - }; + private: + std::vector<Certificate::ref> certificates; + }; } diff --git a/Swift/Controllers/Storages/CertificateStorage.cpp b/Swift/Controllers/Storages/CertificateStorage.cpp index ee942c0..38ed564 100644 --- a/Swift/Controllers/Storages/CertificateStorage.cpp +++ b/Swift/Controllers/Storages/CertificateStorage.cpp @@ -1,10 +1,10 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ -#include "Swift/Controllers/Storages/CertificateStorage.h" +#include <Swift/Controllers/Storages/CertificateStorage.h> namespace Swift { diff --git a/Swift/Controllers/Storages/CertificateStorage.h b/Swift/Controllers/Storages/CertificateStorage.h index f8c6fb5..87a566b 100644 --- a/Swift/Controllers/Storages/CertificateStorage.h +++ b/Swift/Controllers/Storages/CertificateStorage.h @@ -1,7 +1,7 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once @@ -11,12 +11,12 @@ #include <Swiften/TLS/Certificate.h> namespace Swift { - class CertificateStorage { - public: - virtual ~CertificateStorage(); + class CertificateStorage { + public: + virtual ~CertificateStorage(); - virtual bool hasCertificate(Certificate::ref certificate) const = 0; - virtual void addCertificate(Certificate::ref certificate) = 0; - }; + virtual bool hasCertificate(Certificate::ref certificate) const = 0; + virtual void addCertificate(Certificate::ref certificate) = 0; + }; } diff --git a/Swift/Controllers/Storages/CertificateStorageFactory.cpp b/Swift/Controllers/Storages/CertificateStorageFactory.cpp index ba0179a..1c30f77 100644 --- a/Swift/Controllers/Storages/CertificateStorageFactory.cpp +++ b/Swift/Controllers/Storages/CertificateStorageFactory.cpp @@ -1,7 +1,7 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #include <Swift/Controllers/Storages/CertificateStorageFactory.h> diff --git a/Swift/Controllers/Storages/CertificateStorageFactory.h b/Swift/Controllers/Storages/CertificateStorageFactory.h index 5b85757..25fa232 100644 --- a/Swift/Controllers/Storages/CertificateStorageFactory.h +++ b/Swift/Controllers/Storages/CertificateStorageFactory.h @@ -1,19 +1,19 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once namespace Swift { - class CertificateStorage; - class JID; + class CertificateStorage; + class JID; - class CertificateStorageFactory { - public: - virtual ~CertificateStorageFactory(); + class CertificateStorageFactory { + public: + virtual ~CertificateStorageFactory(); - virtual CertificateStorage* createCertificateStorage(const JID& profile) const = 0; - }; + virtual CertificateStorage* createCertificateStorage(const JID& profile) const = 0; + }; } diff --git a/Swift/Controllers/Storages/CertificateStorageTrustChecker.h b/Swift/Controllers/Storages/CertificateStorageTrustChecker.h index df15575..3c708a3 100644 --- a/Swift/Controllers/Storages/CertificateStorageTrustChecker.h +++ b/Swift/Controllers/Storages/CertificateStorageTrustChecker.h @@ -1,34 +1,35 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once #include <Swiften/TLS/CertificateTrustChecker.h> + #include <Swift/Controllers/Storages/CertificateStorage.h> namespace Swift { - /** - * A certificate trust checker that trusts certificates in a certificate storage. - */ - class CertificateStorageTrustChecker : public CertificateTrustChecker { - public: - CertificateStorageTrustChecker(CertificateStorage* storage) : storage(storage) { - } + /** + * A certificate trust checker that trusts certificates in a certificate storage. + */ + class CertificateStorageTrustChecker : public CertificateTrustChecker { + public: + CertificateStorageTrustChecker(CertificateStorage* storage) : storage(storage) { + } - virtual bool isCertificateTrusted(const std::vector<Certificate::ref>& certificateChain) { - lastCertificateChain = std::vector<Certificate::ref>(certificateChain.begin(), certificateChain.end()); - return certificateChain.empty() ? false : storage->hasCertificate(certificateChain[0]); - } + virtual bool isCertificateTrusted(const std::vector<Certificate::ref>& certificateChain) { + lastCertificateChain = std::vector<Certificate::ref>(certificateChain.begin(), certificateChain.end()); + return certificateChain.empty() ? false : storage->hasCertificate(certificateChain[0]); + } - const std::vector<Certificate::ref>& getLastCertificateChain() const { - return lastCertificateChain; - } + const std::vector<Certificate::ref>& getLastCertificateChain() const { + return lastCertificateChain; + } - private: - CertificateStorage* storage; - std::vector<Certificate::ref> lastCertificateChain; - }; + private: + CertificateStorage* storage; + std::vector<Certificate::ref> lastCertificateChain; + }; } diff --git a/Swift/Controllers/Storages/FileStorages.cpp b/Swift/Controllers/Storages/FileStorages.cpp index 52a5e00..49f9ecf 100644 --- a/Swift/Controllers/Storages/FileStorages.cpp +++ b/Swift/Controllers/Storages/FileStorages.cpp @@ -1,61 +1,63 @@ /* - * Copyright (c) 2010-2013 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ -#include "Swift/Controllers/Storages/FileStorages.h" -#include "Swift/Controllers/Storages/VCardFileStorage.h" -#include "Swift/Controllers/Storages/AvatarFileStorage.h" -#include "Swift/Controllers/Storages/CapsFileStorage.h" -#include "Swift/Controllers/Storages/RosterFileStorage.h" -#include <Swiften/History/SQLiteHistoryStorage.h> +#include <Swift/Controllers/Storages/FileStorages.h> + #include <Swiften/Base/Path.h> +#include <Swiften/History/SQLiteHistoryStorage.h> + +#include <Swift/Controllers/Storages/AvatarFileStorage.h> +#include <Swift/Controllers/Storages/CapsFileStorage.h> +#include <Swift/Controllers/Storages/RosterFileStorage.h> +#include <Swift/Controllers/Storages/VCardFileStorage.h> namespace Swift { FileStorages::FileStorages(const boost::filesystem::path& baseDir, const JID& jid, CryptoProvider* crypto) { - boost::filesystem::path profile = stringToPath(jid.toBare()); - vcardStorage = new VCardFileStorage(baseDir / profile / "vcards", crypto); - capsStorage = new CapsFileStorage(baseDir / "caps"); - avatarStorage = new AvatarFileStorage(baseDir / "avatars", baseDir / profile / "avatars", crypto); - rosterStorage = new RosterFileStorage(baseDir / profile / "roster.xml"); + boost::filesystem::path profile = stringToPath(jid.toBare()); + vcardStorage = new VCardFileStorage(baseDir / profile / "vcards", crypto); + capsStorage = new CapsFileStorage(baseDir / "caps"); + avatarStorage = new AvatarFileStorage(baseDir / "avatars", baseDir / profile / "avatars", crypto); + rosterStorage = new RosterFileStorage(baseDir / profile / "roster.xml"); #ifdef SWIFT_EXPERIMENTAL_HISTORY - historyStorage = new SQLiteHistoryStorage(baseDir / "history.db"); + historyStorage = new SQLiteHistoryStorage(baseDir / "history.db"); +#else + historyStorage = nullptr; #endif } FileStorages::~FileStorages() { - delete rosterStorage; - delete avatarStorage; - delete capsStorage; - delete vcardStorage; -#ifdef SWIFT_EXPERIMENTAL_HISTORY - delete historyStorage; -#endif + delete rosterStorage; + delete avatarStorage; + delete capsStorage; + delete vcardStorage; + delete historyStorage; } VCardStorage* FileStorages::getVCardStorage() const { - return vcardStorage; + return vcardStorage; } CapsStorage* FileStorages::getCapsStorage() const { - return capsStorage; + return capsStorage; } AvatarStorage* FileStorages::getAvatarStorage() const { - return avatarStorage; + return avatarStorage; } RosterStorage* FileStorages::getRosterStorage() const { - return rosterStorage; + return rosterStorage; } HistoryStorage* FileStorages::getHistoryStorage() const { #ifdef SWIFT_EXPERIMENTAL_HISTORY - return historyStorage; + return historyStorage; #else - return NULL; + return nullptr; #endif } diff --git a/Swift/Controllers/Storages/FileStorages.h b/Swift/Controllers/Storages/FileStorages.h index 1e914b9..e71d665 100644 --- a/Swift/Controllers/Storages/FileStorages.h +++ b/Swift/Controllers/Storages/FileStorages.h @@ -1,7 +1,7 @@ /* - * Copyright (c) 2010-2013 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2015 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once @@ -11,47 +11,47 @@ #include <Swiften/Client/Storages.h> namespace Swift { - class VCardFileStorage; - class AvatarFileStorage; - class CapsFileStorage; - class RosterFileStorage; - class HistoryStorage; - class JID; - class CryptoProvider; + class VCardFileStorage; + class AvatarFileStorage; + class CapsFileStorage; + class RosterFileStorage; + class HistoryStorage; + class JID; + class CryptoProvider; - /** - * A storages implementation that stores all controller data on disk. - */ - class FileStorages : public Storages { - public: - /** - * Creates the storages interface. - * - * All data will be stored relative to a base directory, and - * for some controllers, in a subdirectory for the given profile. - * The data is stored in the following places: - * - Avatars: <basedir>/avatars - * - VCards: <basedir>/<profile>/vcards - * - Entity capabilities: <basedir>/caps - * - * \param baseDir the base dir to store data relative to - * \param jid the subdir in which profile-specific data will be stored. - * The bare JID will be used as the subdir name. - */ - FileStorages(const boost::filesystem::path& baseDir, const JID& jid, CryptoProvider*); - ~FileStorages(); + /** + * A storages implementation that stores all controller data on disk. + */ + class FileStorages : public Storages { + public: + /** + * Creates the storages interface. + * + * All data will be stored relative to a base directory, and + * for some controllers, in a subdirectory for the given profile. + * The data is stored in the following places: + * - Avatars: $basedir/avatars + * - VCards: $basedir/$profile/vcards + * - Entity capabilities: $basedir/caps + * + * \param baseDir the base dir to store data relative to + * \param jid the subdir in which profile-specific data will be stored. + * The bare JID will be used as the subdir name. + */ + FileStorages(const boost::filesystem::path& baseDir, const JID& jid, CryptoProvider*); + ~FileStorages(); - virtual VCardStorage* getVCardStorage() const; - virtual AvatarStorage* getAvatarStorage() const; - virtual CapsStorage* getCapsStorage() const; - virtual RosterStorage* getRosterStorage() const; - virtual HistoryStorage* getHistoryStorage() const; + virtual VCardStorage* getVCardStorage() const; + virtual AvatarStorage* getAvatarStorage() const; + virtual CapsStorage* getCapsStorage() const; + virtual RosterStorage* getRosterStorage() const; + virtual HistoryStorage* getHistoryStorage() const; - private: - VCardFileStorage* vcardStorage; - AvatarFileStorage* avatarStorage; - CapsFileStorage* capsStorage; - RosterFileStorage* rosterStorage; - HistoryStorage* historyStorage; - }; + private: + VCardFileStorage* vcardStorage; + AvatarFileStorage* avatarStorage; + CapsFileStorage* capsStorage; + RosterFileStorage* rosterStorage; + HistoryStorage* historyStorage; + }; } diff --git a/Swift/Controllers/Storages/FileStoragesFactory.h b/Swift/Controllers/Storages/FileStoragesFactory.h index c119dcf..ec0106e 100644 --- a/Swift/Controllers/Storages/FileStoragesFactory.h +++ b/Swift/Controllers/Storages/FileStoragesFactory.h @@ -1,27 +1,27 @@ /* - * Copyright (c) 2010-2013 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include "Swift/Controllers/Storages/StoragesFactory.h" -#include "Swift/Controllers/Storages/FileStorages.h" +#include <Swift/Controllers/Storages/FileStorages.h> +#include <Swift/Controllers/Storages/StoragesFactory.h> namespace Swift { - class CryptoProvider; + class CryptoProvider; - class FileStoragesFactory : public StoragesFactory { - public: - FileStoragesFactory(const boost::filesystem::path& basePath, CryptoProvider* crypto) : basePath(basePath), crypto(crypto) {} + class FileStoragesFactory : public StoragesFactory { + public: + FileStoragesFactory(const boost::filesystem::path& basePath, CryptoProvider* crypto) : basePath(basePath), crypto(crypto) {} - virtual Storages* createStorages(const JID& profile) const { - return new FileStorages(basePath, profile, crypto); - } + virtual Storages* createStorages(const JID& profile) const { + return new FileStorages(basePath, profile, crypto); + } - private: - boost::filesystem::path basePath; - CryptoProvider* crypto; - }; + private: + boost::filesystem::path basePath; + CryptoProvider* crypto; + }; } diff --git a/Swift/Controllers/Storages/MemoryStoragesFactory.h b/Swift/Controllers/Storages/MemoryStoragesFactory.h index 0dea349..28e9138 100644 --- a/Swift/Controllers/Storages/MemoryStoragesFactory.h +++ b/Swift/Controllers/Storages/MemoryStoragesFactory.h @@ -1,23 +1,27 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include "Swift/Controllers/Storages/StoragesFactory.h" -#include "Swiften/Client/MemoryStorages.h" +#include <Swiften/Client/MemoryStorages.h> + +#include <Swift/Controllers/Storages/StoragesFactory.h> namespace Swift { - class JID; - - class MemoryStoragesFactory : public StoragesFactory { - public: - MemoryStoragesFactory() {} + class JID; + class CryptoProvider; + + class MemoryStoragesFactory : public StoragesFactory { + public: + MemoryStoragesFactory(CryptoProvider* cryptoProvider) : cryptoProvider_(cryptoProvider) {} - virtual Storages* createStorages(const JID& profile) const { - return new MemoryStorages(); - } - }; + virtual Storages* createStorages(const JID& /*profile*/) const { + return new MemoryStorages(cryptoProvider_); + } + private: + CryptoProvider* cryptoProvider_; + }; } diff --git a/Swift/Controllers/Storages/RosterFileStorage.cpp b/Swift/Controllers/Storages/RosterFileStorage.cpp index 73e582f..1f0a90b 100644 --- a/Swift/Controllers/Storages/RosterFileStorage.cpp +++ b/Swift/Controllers/Storages/RosterFileStorage.cpp @@ -1,14 +1,14 @@ /* - * Copyright (c) 2011 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2011-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #include <Swift/Controllers/Storages/RosterFileStorage.h> #include <Swiften/Entity/GenericPayloadPersister.h> -#include <Swiften/Serializer/PayloadSerializers/RosterSerializer.h> #include <Swiften/Parser/PayloadParsers/RosterParser.h> +#include <Swiften/Serializer/PayloadSerializers/RosterSerializer.h> using namespace Swift; @@ -17,10 +17,10 @@ typedef GenericPayloadPersister<RosterPayload, RosterParser, RosterSerializer> R RosterFileStorage::RosterFileStorage(const boost::filesystem::path& path) : path(path) { } -boost::shared_ptr<RosterPayload> RosterFileStorage::getRoster() const { - return RosterPersister().loadPayloadGeneric(path); +std::shared_ptr<RosterPayload> RosterFileStorage::getRoster() const { + return RosterPersister().loadPayloadGeneric(path); } -void RosterFileStorage::setRoster(boost::shared_ptr<RosterPayload> roster) { - RosterPersister().savePayload(roster, path); +void RosterFileStorage::setRoster(std::shared_ptr<RosterPayload> roster) { + RosterPersister().savePayload(roster, path); } diff --git a/Swift/Controllers/Storages/RosterFileStorage.h b/Swift/Controllers/Storages/RosterFileStorage.h index cb00969..38e26c9 100644 --- a/Swift/Controllers/Storages/RosterFileStorage.h +++ b/Swift/Controllers/Storages/RosterFileStorage.h @@ -1,7 +1,7 @@ /* - * Copyright (c) 2011 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2011-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once @@ -11,14 +11,14 @@ #include <Swiften/Roster/RosterStorage.h> namespace Swift { - class RosterFileStorage : public RosterStorage { - public: - RosterFileStorage(const boost::filesystem::path& path); + class RosterFileStorage : public RosterStorage { + public: + RosterFileStorage(const boost::filesystem::path& path); - virtual boost::shared_ptr<RosterPayload> getRoster() const; - virtual void setRoster(boost::shared_ptr<RosterPayload>); + virtual std::shared_ptr<RosterPayload> getRoster() const; + virtual void setRoster(std::shared_ptr<RosterPayload>); - private: - boost::filesystem::path path; - }; + private: + boost::filesystem::path path; + }; } diff --git a/Swift/Controllers/Storages/StoragesFactory.h b/Swift/Controllers/Storages/StoragesFactory.h index 203f9c9..771230b 100644 --- a/Swift/Controllers/Storages/StoragesFactory.h +++ b/Swift/Controllers/Storages/StoragesFactory.h @@ -1,19 +1,19 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once namespace Swift { - class Storages; - class JID; + class Storages; + class JID; - class StoragesFactory { - public: - virtual ~StoragesFactory() {} + class StoragesFactory { + public: + virtual ~StoragesFactory() {} - virtual Storages* createStorages(const JID& profile) const = 0; - }; + virtual Storages* createStorages(const JID& profile) const = 0; + }; } diff --git a/Swift/Controllers/Storages/VCardFileStorage.cpp b/Swift/Controllers/Storages/VCardFileStorage.cpp index b22e235..2fdadf6 100644 --- a/Swift/Controllers/Storages/VCardFileStorage.cpp +++ b/Swift/Controllers/Storages/VCardFileStorage.cpp @@ -1,116 +1,126 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ -#include "Swift/Controllers/Storages/VCardFileStorage.h" +#include <Swift/Controllers/Storages/VCardFileStorage.h> -#include <boost/filesystem/fstream.hpp> -#include <boost/filesystem.hpp> #include <iostream> -#include <Swiften/Entity/GenericPayloadPersister.h> -#include <Swiften/Base/String.h> -#include <Swiften/StringCodecs/Hexify.h> -#include <Swiften/Base/foreach.h> +#include <boost/filesystem.hpp> +#include <boost/filesystem/fstream.hpp> + #include <Swiften/Base/Path.h> +#include <Swiften/Base/String.h> #include <Swiften/Crypto/CryptoProvider.h> -#include "Swiften/JID/JID.h" -#include "Swiften/Elements/VCard.h" -#include "Swiften/Serializer/PayloadSerializers/VCardSerializer.h" -#include "Swiften/Parser/PayloadParsers/UnitTest/PayloadParserTester.h" -#include "Swiften/Parser/PayloadParsers/VCardParser.h" +#include <Swiften/Elements/VCard.h> +#include <Swiften/Entity/GenericPayloadPersister.h> +#include <Swiften/JID/JID.h> +#include <Swiften/Parser/PayloadParsers/UnitTest/PayloadParserTester.h> +#include <Swiften/Parser/PayloadParsers/VCardParser.h> +#include <Swiften/Serializer/PayloadSerializers/VCardSerializer.h> +#include <Swiften/StringCodecs/Hexify.h> using namespace Swift; typedef GenericPayloadPersister<VCard, VCardParser, VCardSerializer> VCardPersister; VCardFileStorage::VCardFileStorage(boost::filesystem::path dir, CryptoProvider* crypto) : VCardStorage(crypto), vcardsPath(dir), crypto(crypto) { - cacheFile = vcardsPath / "phashes"; - if (boost::filesystem::exists(cacheFile)) { - try { - boost::filesystem::ifstream file(cacheFile); - std::string line; - if (file.is_open()) { - while (!file.eof()) { - getline(file, line); - std::pair<std::string, std::string> r = String::getSplittedAtFirst(line, ' '); - JID jid(r.second); - if (jid.isValid()) { - photoHashes.insert(std::make_pair(jid, r.first)); - } - else if (!r.first.empty() || !r.second.empty()) { - std::cerr << "Invalid entry in phashes file" << std::endl; - } - } - } - } - catch (...) { - std::cerr << "Error reading phashes file" << std::endl; - } - } + cacheFile = vcardsPath / "phashes"; + if (boost::filesystem::exists(cacheFile)) { + try { + boost::filesystem::ifstream file(cacheFile); + std::string line; + if (file.is_open()) { + while (!file.eof()) { + getline(file, line); + std::pair<std::string, std::string> r = String::getSplittedAtFirst(line, ' '); + JID jid(r.second); + if (jid.isValid()) { + photoHashes.insert(std::make_pair(jid, r.first)); + } + else if (!r.first.empty() || !r.second.empty()) { + std::cerr << "Invalid entry in phashes file" << std::endl; + } + } + } + } + catch (...) { + std::cerr << "Error reading phashes file" << std::endl; + } + } +} + +std::shared_ptr<VCard> VCardFileStorage::getVCard(const JID& jid) const { + std::shared_ptr<VCard> result = VCardPersister().loadPayloadGeneric(getVCardPath(jid)); + getAndUpdatePhotoHash(jid, result); + return result; } -boost::shared_ptr<VCard> VCardFileStorage::getVCard(const JID& jid) const { - boost::shared_ptr<VCard> result = VCardPersister().loadPayloadGeneric(getVCardPath(jid)); - getAndUpdatePhotoHash(jid, result); - return result; +boost::posix_time::ptime VCardFileStorage::getVCardWriteTime(const JID& jid) const { + if (vcardWriteTimes.find(jid) == vcardWriteTimes.end()) { + return boost::posix_time::ptime(); + } + else { + return vcardWriteTimes.at(jid); + } } void VCardFileStorage::setVCard(const JID& jid, VCard::ref v) { - VCardPersister().savePayload(v, getVCardPath(jid)); - getAndUpdatePhotoHash(jid, v); + vcardWriteTimes[jid] = boost::posix_time::second_clock::universal_time(); + VCardPersister().savePayload(v, getVCardPath(jid)); + getAndUpdatePhotoHash(jid, v); } boost::filesystem::path VCardFileStorage::getVCardPath(const JID& jid) const { - try { - std::string file(jid.toString()); - String::replaceAll(file, '/', "%2f"); - return boost::filesystem::path(vcardsPath / stringToPath(file + ".xml")); - } - catch (const boost::filesystem::filesystem_error& e) { - std::cerr << "ERROR: " << e.what() << std::endl; - return boost::filesystem::path(); - } + try { + std::string file(jid.toString()); + String::replaceAll(file, '/', "%2f"); + return boost::filesystem::path(vcardsPath / stringToPath(file + ".xml")); + } + catch (const boost::filesystem::filesystem_error& e) { + std::cerr << "ERROR: " << e.what() << std::endl; + return boost::filesystem::path(); + } } std::string VCardFileStorage::getPhotoHash(const JID& jid) const { - PhotoHashMap::const_iterator i = photoHashes.find(jid); - if (i != photoHashes.end()) { - return i->second; - } - else { - VCard::ref vCard = getVCard(jid); - return getAndUpdatePhotoHash(jid, vCard); - } + PhotoHashMap::const_iterator i = photoHashes.find(jid); + if (i != photoHashes.end()) { + return i->second; + } + else { + VCard::ref vCard = getVCard(jid); + return getAndUpdatePhotoHash(jid, vCard); + } } std::string VCardFileStorage::getAndUpdatePhotoHash(const JID& jid, VCard::ref vCard) const { - std::string hash; - if (vCard && !vCard->getPhoto().empty()) { - hash = Hexify::hexify(crypto->getSHA1Hash(vCard->getPhoto())); - } - std::pair<PhotoHashMap::iterator, bool> r = photoHashes.insert(std::make_pair(jid, hash)); - if (r.second) { - savePhotoHashes(); - } - else if (r.first->second != hash) { - r.first->second = hash; - savePhotoHashes(); - } - return hash; + std::string hash; + if (vCard && !vCard->getPhoto().empty()) { + hash = Hexify::hexify(crypto->getSHA1Hash(vCard->getPhoto())); + } + std::pair<PhotoHashMap::iterator, bool> r = photoHashes.insert(std::make_pair(jid, hash)); + if (r.second) { + savePhotoHashes(); + } + else if (r.first->second != hash) { + r.first->second = hash; + savePhotoHashes(); + } + return hash; } void VCardFileStorage::savePhotoHashes() const { - try { - boost::filesystem::ofstream file(cacheFile); - for (PhotoHashMap::const_iterator i = photoHashes.begin(); i != photoHashes.end(); ++i) { - file << i->second << " " << i->first.toString() << std::endl; - } - file.close(); - } - catch (...) { - std::cerr << "Error writing vcards file" << std::endl; - } + try { + boost::filesystem::ofstream file(cacheFile); + for (PhotoHashMap::const_iterator i = photoHashes.begin(); i != photoHashes.end(); ++i) { + file << i->second << " " << i->first.toString() << std::endl; + } + file.close(); + } + catch (...) { + std::cerr << "Error writing vcards file" << std::endl; + } } diff --git a/Swift/Controllers/Storages/VCardFileStorage.h b/Swift/Controllers/Storages/VCardFileStorage.h index 2c3af3d..a91e914 100644 --- a/Swift/Controllers/Storages/VCardFileStorage.h +++ b/Swift/Controllers/Storages/VCardFileStorage.h @@ -1,41 +1,44 @@ /* - * Copyright (c) 2010-2013 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <boost/shared_ptr.hpp> -#include <boost/filesystem/path.hpp> -#include <string> #include <map> +#include <memory> +#include <string> + +#include <boost/filesystem/path.hpp> -#include "Swiften/VCards/VCardStorage.h" +#include <Swiften/VCards/VCardStorage.h> namespace Swift { - class CryptoProvider; + class CryptoProvider; - class VCardFileStorage : public VCardStorage { - public: - VCardFileStorage(boost::filesystem::path dir, CryptoProvider* crypto); + class VCardFileStorage : public VCardStorage { + public: + VCardFileStorage(boost::filesystem::path dir, CryptoProvider* crypto); - virtual VCard::ref getVCard(const JID& jid) const; - virtual void setVCard(const JID& jid, VCard::ref v); + virtual VCard::ref getVCard(const JID& jid) const; + virtual boost::posix_time::ptime getVCardWriteTime(const JID& jid) const; + virtual void setVCard(const JID& jid, VCard::ref v); - virtual std::string getPhotoHash(const JID&) const; + virtual std::string getPhotoHash(const JID&) const; - private: - boost::filesystem::path getVCardPath(const JID&) const; + private: + boost::filesystem::path getVCardPath(const JID&) const; - std::string getAndUpdatePhotoHash(const JID& jid, VCard::ref vcard) const; - void savePhotoHashes() const; + std::string getAndUpdatePhotoHash(const JID& jid, VCard::ref vcard) const; + void savePhotoHashes() const; - private: - boost::filesystem::path vcardsPath; - CryptoProvider* crypto; - boost::filesystem::path cacheFile; - typedef std::map<JID, std::string> PhotoHashMap; - mutable PhotoHashMap photoHashes; - }; + private: + boost::filesystem::path vcardsPath; + CryptoProvider* crypto; + boost::filesystem::path cacheFile; + typedef std::map<JID, std::string> PhotoHashMap; + mutable PhotoHashMap photoHashes; + std::map<JID, boost::posix_time::ptime> vcardWriteTimes; + }; } diff --git a/Swift/Controllers/SystemTray.h b/Swift/Controllers/SystemTray.h index b71a783..094857f 100644 --- a/Swift/Controllers/SystemTray.h +++ b/Swift/Controllers/SystemTray.h @@ -1,19 +1,19 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include "Swiften/Elements/StatusShow.h" +#include <Swiften/Elements/StatusShow.h> namespace Swift { - class SystemTray { - public: - virtual ~SystemTray(){} - virtual void setUnreadMessages(bool some) = 0; - virtual void setStatusType(StatusShow::Type type) = 0; - virtual void setConnecting() = 0; - }; + class SystemTray { + public: + virtual ~SystemTray(){} + virtual void setUnreadMessages(bool some) = 0; + virtual void setStatusType(StatusShow::Type type) = 0; + virtual void setConnecting() = 0; + }; } diff --git a/Swift/Controllers/SystemTrayController.cpp b/Swift/Controllers/SystemTrayController.cpp index 31fd4ff..8d4b2b7 100644 --- a/Swift/Controllers/SystemTrayController.cpp +++ b/Swift/Controllers/SystemTrayController.cpp @@ -1,42 +1,42 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ -#include "Swift/Controllers/SystemTrayController.h" +#include <Swift/Controllers/SystemTrayController.h> #include <boost/bind.hpp> -#include "Swift/Controllers/XMPPEvents/EventController.h" -#include "Swift/Controllers/SystemTray.h" +#include <Swift/Controllers/SystemTray.h> +#include <Swift/Controllers/XMPPEvents/EventController.h> namespace Swift { SystemTrayController::SystemTrayController(EventController* eventController, SystemTray* systemTray) { - systemTray_ = systemTray; - eventController_ = eventController; - eventController_->onEventQueueLengthChange.connect(boost::bind(&SystemTrayController::handleEventQueueLengthChange, this, _1)); + systemTray_ = systemTray; + eventController_ = eventController; + eventController_->onEventQueueLengthChange.connect(boost::bind(&SystemTrayController::handleEventQueueLengthChange, this, _1)); } void SystemTrayController::handleEventQueueLengthChange(int /*length*/) { - EventList events = eventController_->getEvents(); - bool found = false; - for (EventList::iterator it = events.begin(); it != events.end(); ++it) { - if (boost::dynamic_pointer_cast<MessageEvent>(*it)) { - found = true; - break; - } - } - systemTray_->setUnreadMessages(found); + EventList events = eventController_->getEvents(); + bool found = false; + for (auto& event : events) { + if (std::dynamic_pointer_cast<MessageEvent>(event)) { + found = true; + break; + } + } + systemTray_->setUnreadMessages(found); } void SystemTrayController::setMyStatusType(StatusShow::Type type) { - systemTray_->setStatusType(type); + systemTray_->setStatusType(type); } void SystemTrayController::setConnecting() { - systemTray_->setConnecting(); + systemTray_->setConnecting(); } } diff --git a/Swift/Controllers/SystemTrayController.h b/Swift/Controllers/SystemTrayController.h index 3624ea2..850ac71 100644 --- a/Swift/Controllers/SystemTrayController.h +++ b/Swift/Controllers/SystemTrayController.h @@ -1,27 +1,27 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include "Swiften/Elements/StatusShow.h" +#include <Swiften/Elements/StatusShow.h> namespace Swift { - class EventController; - class SystemTray; + class EventController; + class SystemTray; - class SystemTrayController { - public: - SystemTrayController(EventController* eventController, SystemTray* systemTray); - void setMyStatusType(StatusShow::Type type); - void setConnecting(); - private: - void handleEventQueueLengthChange(int length); + class SystemTrayController { + public: + SystemTrayController(EventController* eventController, SystemTray* systemTray); + void setMyStatusType(StatusShow::Type type); + void setConnecting(); + private: + void handleEventQueueLengthChange(int length); - private: - EventController* eventController_; - SystemTray* systemTray_; - }; + private: + EventController* eventController_; + SystemTray* systemTray_; + }; } diff --git a/Swift/Controllers/Translator.cpp b/Swift/Controllers/Translator.cpp index b7766ca..f03c533 100644 --- a/Swift/Controllers/Translator.cpp +++ b/Swift/Controllers/Translator.cpp @@ -1,19 +1,25 @@ /* - * Copyright (c) 2011 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2011-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #include <Swift/Controllers/Translator.h> #include <cassert> +#include <Swiften/Base/DateTime.h> + namespace Swift { static struct DefaultTranslator : public Translator { - virtual std::string translate(const std::string& text, const std::string&) { - return text; - } + virtual std::string translate(const std::string& text, const std::string&) { + return text; + } + + virtual std::string ptimeToHumanReadableString(const boost::posix_time::ptime& time) { + return dateTimeToLocalString(time); + } } defaultTranslator; Translator* Translator::translator = &defaultTranslator; @@ -22,7 +28,7 @@ Translator::~Translator() { } void Translator::setInstance(Translator* t) { - translator = t; + translator = t; } } diff --git a/Swift/Controllers/Translator.h b/Swift/Controllers/Translator.h index ac4407d..f37e059 100644 --- a/Swift/Controllers/Translator.h +++ b/Swift/Controllers/Translator.h @@ -1,27 +1,31 @@ /* - * Copyright (c) 2011 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2011-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once #include <string> +#include <boost/date_time/posix_time/posix_time_types.hpp> + namespace Swift { - class Translator { - public: - virtual ~Translator(); + class Translator { + public: + virtual ~Translator(); + + virtual std::string translate(const std::string& text, const std::string& context) = 0; - virtual std::string translate(const std::string& text, const std::string& context) = 0; + virtual std::string ptimeToHumanReadableString(const boost::posix_time::ptime& time) = 0; - static void setInstance(Translator* translator); + static void setInstance(Translator* translator); - static Translator* getInstance() { - return translator; - } + static Translator* getInstance() { + return translator; + } - private: - static Translator* translator; - }; + private: + static Translator* translator; + }; } diff --git a/Swift/Controllers/UIEvents/AcceptWhiteboardSessionUIEvent.h b/Swift/Controllers/UIEvents/AcceptWhiteboardSessionUIEvent.h index 93cad03..ac76ec4 100644 --- a/Swift/Controllers/UIEvents/AcceptWhiteboardSessionUIEvent.h +++ b/Swift/Controllers/UIEvents/AcceptWhiteboardSessionUIEvent.h @@ -4,21 +4,27 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ +/* + * Copyright (c) 2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + #pragma once -#include <boost/shared_ptr.hpp> +#include <memory> #include <Swiften/JID/JID.h> #include <Swift/Controllers/UIEvents/UIEvent.h> namespace Swift { - class AcceptWhiteboardSessionUIEvent : public UIEvent { - typedef boost::shared_ptr<AcceptWhiteboardSessionUIEvent> ref; - public: - AcceptWhiteboardSessionUIEvent(const JID& jid) : jid_(jid) {} - const JID& getContact() const {return jid_;} - private: - JID jid_; - }; + class AcceptWhiteboardSessionUIEvent : public UIEvent { + typedef std::shared_ptr<AcceptWhiteboardSessionUIEvent> ref; + public: + AcceptWhiteboardSessionUIEvent(const JID& jid) : jid_(jid) {} + const JID& getContact() const {return jid_;} + private: + JID jid_; + }; } diff --git a/Swift/Controllers/UIEvents/AddContactUIEvent.h b/Swift/Controllers/UIEvents/AddContactUIEvent.h index 6b70b76..df5bf36 100644 --- a/Swift/Controllers/UIEvents/AddContactUIEvent.h +++ b/Swift/Controllers/UIEvents/AddContactUIEvent.h @@ -1,36 +1,38 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <string> #include <set> +#include <string> + +#include <Swiften/JID/JID.h> -#include "Swift/Controllers/UIEvents/UIEvent.h" +#include <Swift/Controllers/UIEvents/UIEvent.h> namespace Swift { - class AddContactUIEvent : public UIEvent { - public: - AddContactUIEvent(const JID& jid, const std::string& name, const std::set<std::string>& groups) : jid_(jid), name_(name), groups_(groups) {} - - const std::string& getName() const { - return name_; - } - - const JID& getJID() const { - return jid_; - } - - const std::set<std::string>& getGroups() const { - return groups_; - } - - private: - JID jid_; - std::string name_; - std::set<std::string> groups_; - }; + class AddContactUIEvent : public UIEvent { + public: + AddContactUIEvent(const JID& jid, const std::string& name, const std::set<std::string>& groups) : jid_(jid), name_(name), groups_(groups) {} + + const std::string& getName() const { + return name_; + } + + const JID& getJID() const { + return jid_; + } + + const std::set<std::string>& getGroups() const { + return groups_; + } + + private: + JID jid_; + std::string name_; + std::set<std::string> groups_; + }; } diff --git a/Swift/Controllers/UIEvents/AddMUCBookmarkUIEvent.h b/Swift/Controllers/UIEvents/AddMUCBookmarkUIEvent.h index df01d6c..e1d6744 100644 --- a/Swift/Controllers/UIEvents/AddMUCBookmarkUIEvent.h +++ b/Swift/Controllers/UIEvents/AddMUCBookmarkUIEvent.h @@ -1,23 +1,24 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <boost/shared_ptr.hpp> +#include <memory> -#include "Swift/Controllers/UIEvents/UIEvent.h" -#include "Swiften/MUC/MUCBookmark.h" +#include <Swiften/MUC/MUCBookmark.h> + +#include <Swift/Controllers/UIEvents/UIEvent.h> namespace Swift { - class AddMUCBookmarkUIEvent : public UIEvent { - public: - AddMUCBookmarkUIEvent(const MUCBookmark& bookmark) : bookmark(bookmark) {} - const MUCBookmark& getBookmark() { return bookmark; } + class AddMUCBookmarkUIEvent : public UIEvent { + public: + AddMUCBookmarkUIEvent(const MUCBookmark& bookmark) : bookmark(bookmark) {} + const MUCBookmark& getBookmark() { return bookmark; } - private: - MUCBookmark bookmark; - }; + private: + MUCBookmark bookmark; + }; } diff --git a/Swift/Controllers/UIEvents/CancelWhiteboardSessionUIEvent.h b/Swift/Controllers/UIEvents/CancelWhiteboardSessionUIEvent.h index f5c3b0e..1e9491f 100644 --- a/Swift/Controllers/UIEvents/CancelWhiteboardSessionUIEvent.h +++ b/Swift/Controllers/UIEvents/CancelWhiteboardSessionUIEvent.h @@ -4,21 +4,27 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ +/* + * Copyright (c) 2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + #pragma once -#include <boost/shared_ptr.hpp> +#include <memory> #include <Swiften/JID/JID.h> #include <Swift/Controllers/UIEvents/UIEvent.h> namespace Swift { - class CancelWhiteboardSessionUIEvent : public UIEvent { - typedef boost::shared_ptr<CancelWhiteboardSessionUIEvent> ref; - public: - CancelWhiteboardSessionUIEvent(const JID& jid) : jid_(jid) {} - const JID& getContact() const {return jid_;} - private: - JID jid_; - }; + class CancelWhiteboardSessionUIEvent : public UIEvent { + typedef std::shared_ptr<CancelWhiteboardSessionUIEvent> ref; + public: + CancelWhiteboardSessionUIEvent(const JID& jid) : jid_(jid) {} + const JID& getContact() const {return jid_;} + private: + JID jid_; + }; } diff --git a/Swift/Controllers/UIEvents/CreateImpromptuMUCUIEvent.h b/Swift/Controllers/UIEvents/CreateImpromptuMUCUIEvent.h index 57e181d..ed9fd85 100644 --- a/Swift/Controllers/UIEvents/CreateImpromptuMUCUIEvent.h +++ b/Swift/Controllers/UIEvents/CreateImpromptuMUCUIEvent.h @@ -4,23 +4,44 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ +/* + * Copyright (c) 2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + #pragma once +#include <string> +#include <vector> + +#include <Swiften/JID/JID.h> + #include <Swift/Controllers/UIEvents/UIEvent.h> namespace Swift { class CreateImpromptuMUCUIEvent : public UIEvent { - public: - CreateImpromptuMUCUIEvent(const std::vector<JID>& jids, const JID& roomJID = JID(), const std::string reason = "") : jids_(jids), roomJID_(roomJID), reason_(reason) { } - - std::vector<JID> getJIDs() const { return jids_; } - JID getRoomJID() const { return roomJID_; } - std::string getReason() const { return reason_; } - private: - std::vector<JID> jids_; - JID roomJID_; - std::string reason_; + public: + /** + * @brief CreateImpromptuMUCUIEvent + * @param jids A vector of JIDs that are invited to the imprompto MUC. + * Useful when the event is used to recreate an old impromptu + * chat room. + * @param roomJID The full JID of the impromtu MUC. Useful when the event + * is used to recreate an old impromptu chat room. + * @param reason + */ + CreateImpromptuMUCUIEvent(const std::vector<JID>& jids, const JID& roomJID, const std::string reason) : jids_(jids), roomJID_(roomJID), reason_(reason) { } + + std::vector<JID> getJIDs() const { return jids_; } + JID getRoomJID() const { return roomJID_; } + std::string getReason() const { return reason_; } + + private: + std::vector<JID> jids_; + JID roomJID_; + std::string reason_; }; } diff --git a/Swift/Controllers/UIEvents/EditMUCBookmarkUIEvent.h b/Swift/Controllers/UIEvents/EditMUCBookmarkUIEvent.h index 7723d89..33f38f2 100644 --- a/Swift/Controllers/UIEvents/EditMUCBookmarkUIEvent.h +++ b/Swift/Controllers/UIEvents/EditMUCBookmarkUIEvent.h @@ -1,26 +1,27 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <boost/shared_ptr.hpp> +#include <memory> -#include "Swift/Controllers/UIEvents/UIEvent.h" -#include "Swiften/MUC/MUCBookmark.h" +#include <Swiften/MUC/MUCBookmark.h> + +#include <Swift/Controllers/UIEvents/UIEvent.h> namespace Swift { - class EditMUCBookmarkUIEvent : public UIEvent { - public: - EditMUCBookmarkUIEvent(const MUCBookmark& oldBookmark, const MUCBookmark& newBookmark) : oldBookmark(oldBookmark) , newBookmark(newBookmark) {} + class EditMUCBookmarkUIEvent : public UIEvent { + public: + EditMUCBookmarkUIEvent(const MUCBookmark& oldBookmark, const MUCBookmark& newBookmark) : oldBookmark(oldBookmark) , newBookmark(newBookmark) {} - const MUCBookmark& getOldBookmark() {return oldBookmark;} - const MUCBookmark& getNewBookmark() {return newBookmark;} + const MUCBookmark& getOldBookmark() {return oldBookmark;} + const MUCBookmark& getNewBookmark() {return newBookmark;} - private: - MUCBookmark oldBookmark; - MUCBookmark newBookmark; - }; + private: + MUCBookmark oldBookmark; + MUCBookmark newBookmark; + }; } diff --git a/Swift/Controllers/UIEvents/FdpFormSubmitWindowOpenUIEvent.h b/Swift/Controllers/UIEvents/FdpFormSubmitWindowOpenUIEvent.h new file mode 100644 index 0000000..d540cb2 --- /dev/null +++ b/Swift/Controllers/UIEvents/FdpFormSubmitWindowOpenUIEvent.h @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#pragma once + +#include <Swift/Controllers/UIEvents/UIEvent.h> + +namespace Swift { + class FdpFormSubmitWindowOpenUIEvent : public UIEvent { + public: + FdpFormSubmitWindowOpenUIEvent() { + } + }; +} diff --git a/Swift/Controllers/UIEvents/InviteToMUCUIEvent.h b/Swift/Controllers/UIEvents/InviteToMUCUIEvent.h index cb9d20b..e38eab8 100644 --- a/Swift/Controllers/UIEvents/InviteToMUCUIEvent.h +++ b/Swift/Controllers/UIEvents/InviteToMUCUIEvent.h @@ -4,37 +4,44 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ +/* + * Copyright (c) 2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + #pragma once -#include <boost/shared_ptr.hpp> +#include <memory> #include <vector> -#include <Swift/Controllers/UIEvents/UIEvent.h> #include <Swiften/JID/JID.h> +#include <Swift/Controllers/UIEvents/UIEvent.h> + namespace Swift { - class InviteToMUCUIEvent : public UIEvent { - public: - typedef boost::shared_ptr<InviteToMUCUIEvent> ref; - - InviteToMUCUIEvent(const JID& room, const std::vector<JID>& JIDsToInvite, const std::string& reason) : room_(room), invite_(JIDsToInvite), reason_(reason) { - } - - const JID& getRoom() const { - return room_; - } - - const std::vector<JID> getInvites() const { - return invite_; - } - - const std::string getReason() const { - return reason_; - } - - private: - JID room_; - std::vector<JID> invite_; - std::string reason_; - }; + class InviteToMUCUIEvent : public UIEvent { + public: + typedef std::shared_ptr<InviteToMUCUIEvent> ref; + + InviteToMUCUIEvent(const JID& originator, const std::vector<JID>& JIDsToInvite, const std::string& reason) : originator_(originator), invite_(JIDsToInvite), reason_(reason) { + } + + const JID& getOriginator() const { + return originator_; + } + + const std::vector<JID> getInvites() const { + return invite_; + } + + const std::string getReason() const { + return reason_; + } + + private: + JID originator_; + std::vector<JID> invite_; + std::string reason_; + }; } diff --git a/Swift/Controllers/UIEvents/JoinMUCUIEvent.h b/Swift/Controllers/UIEvents/JoinMUCUIEvent.h index e046942..78103a8 100644 --- a/Swift/Controllers/UIEvents/JoinMUCUIEvent.h +++ b/Swift/Controllers/UIEvents/JoinMUCUIEvent.h @@ -1,39 +1,40 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <boost/optional.hpp> -#include <boost/shared_ptr.hpp> +#include <memory> #include <string> +#include <boost/optional.hpp> + #include <Swiften/JID/JID.h> #include <Swift/Controllers/UIEvents/UIEvent.h> namespace Swift { - class JoinMUCUIEvent : public UIEvent { - public: - typedef boost::shared_ptr<JoinMUCUIEvent> ref; - JoinMUCUIEvent(const JID& jid, const boost::optional<std::string>& password = boost::optional<std::string>(), const boost::optional<std::string>& nick = boost::optional<std::string>(), bool joinAutomaticallyInFuture = false, bool createAsReservedRoomIfNew = false, bool isImpromptu = false, bool isContinuation = false) : jid_(jid), nick_(nick), joinAutomatically_(joinAutomaticallyInFuture), createAsReservedRoomIfNew_(createAsReservedRoomIfNew), password_(password), isImpromptuMUC_(isImpromptu), isContinuation_(isContinuation) {} - const boost::optional<std::string>& getNick() const {return nick_;} - const JID& getJID() const {return jid_;} - bool getShouldJoinAutomatically() const {return joinAutomatically_;} - bool getCreateAsReservedRoomIfNew() const {return createAsReservedRoomIfNew_;} - const boost::optional<std::string>& getPassword() const {return password_;} - bool isImpromptu() const {return isImpromptuMUC_;} - bool isContinuation() const {return isContinuation_;} + class JoinMUCUIEvent : public UIEvent { + public: + typedef std::shared_ptr<JoinMUCUIEvent> ref; + JoinMUCUIEvent(const JID& jid, const boost::optional<std::string>& password = boost::optional<std::string>(), const boost::optional<std::string>& nick = boost::optional<std::string>(), bool createAsReservedRoomIfNew = false, bool isImpromptu = false, bool isContinuation = false) : jid_(jid), nick_(nick), createAsReservedRoomIfNew_(createAsReservedRoomIfNew), password_(password), isImpromptuMUC_(isImpromptu), isContinuation_(isContinuation) {} + const boost::optional<std::string>& getNick() const {return nick_;} + const JID& getJID() const {return jid_;} + bool getShouldJoinAutomatically() const {return joinAutomatically_;} + bool getCreateAsReservedRoomIfNew() const {return createAsReservedRoomIfNew_;} + const boost::optional<std::string>& getPassword() const {return password_;} + bool isImpromptu() const {return isImpromptuMUC_;} + bool isContinuation() const {return isContinuation_;} - private: - JID jid_; - boost::optional<std::string> nick_; - bool joinAutomatically_; - bool createAsReservedRoomIfNew_; - boost::optional<std::string> password_; - bool isImpromptuMUC_; - bool isContinuation_; - }; + private: + JID jid_; + boost::optional<std::string> nick_; + bool joinAutomatically_; + bool createAsReservedRoomIfNew_; + boost::optional<std::string> password_; + bool isImpromptuMUC_; + bool isContinuation_; + }; } diff --git a/Swift/Controllers/UIEvents/RemoveMUCBookmarkUIEvent.h b/Swift/Controllers/UIEvents/RemoveMUCBookmarkUIEvent.h index 0df40f9..b73eda5 100644 --- a/Swift/Controllers/UIEvents/RemoveMUCBookmarkUIEvent.h +++ b/Swift/Controllers/UIEvents/RemoveMUCBookmarkUIEvent.h @@ -1,23 +1,24 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <boost/shared_ptr.hpp> +#include <memory> -#include "Swift/Controllers/UIEvents/UIEvent.h" -#include "Swiften/MUC/MUCBookmark.h" +#include <Swiften/MUC/MUCBookmark.h> + +#include <Swift/Controllers/UIEvents/UIEvent.h> namespace Swift { - class RemoveMUCBookmarkUIEvent : public UIEvent { - public: - RemoveMUCBookmarkUIEvent(const MUCBookmark& bookmark) : bookmark(bookmark) {} - const MUCBookmark& getBookmark() { return bookmark; } + class RemoveMUCBookmarkUIEvent : public UIEvent { + public: + RemoveMUCBookmarkUIEvent(const MUCBookmark& bookmark) : bookmark(bookmark) {} + const MUCBookmark& getBookmark() { return bookmark; } - private: - MUCBookmark bookmark; - }; + private: + MUCBookmark bookmark; + }; } diff --git a/Swift/Controllers/UIEvents/RemoveRosterItemUIEvent.h b/Swift/Controllers/UIEvents/RemoveRosterItemUIEvent.h index 617daf3..0f4a89d 100644 --- a/Swift/Controllers/UIEvents/RemoveRosterItemUIEvent.h +++ b/Swift/Controllers/UIEvents/RemoveRosterItemUIEvent.h @@ -1,23 +1,24 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include "Swiften/JID/JID.h" -#include "Swift/Controllers/UIEvents/UIEvent.h" +#include <Swiften/JID/JID.h> + +#include <Swift/Controllers/UIEvents/UIEvent.h> namespace Swift { class RemoveRosterItemUIEvent : public UIEvent { - public: - RemoveRosterItemUIEvent(const JID& jid) : jid_(jid) {} - virtual ~RemoveRosterItemUIEvent() {} - JID getJID() {return jid_;} - private: - JID jid_; + public: + RemoveRosterItemUIEvent(const JID& jid) : jid_(jid) {} + virtual ~RemoveRosterItemUIEvent() {} + JID getJID() {return jid_;} + private: + JID jid_; }; diff --git a/Swift/Controllers/UIEvents/RenameGroupUIEvent.h b/Swift/Controllers/UIEvents/RenameGroupUIEvent.h index 9773b9e..ea19efe 100644 --- a/Swift/Controllers/UIEvents/RenameGroupUIEvent.h +++ b/Swift/Controllers/UIEvents/RenameGroupUIEvent.h @@ -1,30 +1,31 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <Swift/Controllers/UIEvents/UIEvent.h> #include <string> +#include <Swift/Controllers/UIEvents/UIEvent.h> + namespace Swift { - class RenameGroupUIEvent : public UIEvent { - public: - RenameGroupUIEvent(const std::string& group, const std::string& newName) : group(group), newName(newName) { - } + class RenameGroupUIEvent : public UIEvent { + public: + RenameGroupUIEvent(const std::string& group, const std::string& newName) : group(group), newName(newName) { + } - const std::string& getGroup() const { - return group; - } + const std::string& getGroup() const { + return group; + } - const std::string& getNewName() const { - return newName; - } + const std::string& getNewName() const { + return newName; + } - private: - std::string group; - std::string newName; - }; + private: + std::string group; + std::string newName; + }; } diff --git a/Swift/Controllers/UIEvents/RenameRosterItemUIEvent.h b/Swift/Controllers/UIEvents/RenameRosterItemUIEvent.h index f3542fe..1a71cb4 100644 --- a/Swift/Controllers/UIEvents/RenameRosterItemUIEvent.h +++ b/Swift/Controllers/UIEvents/RenameRosterItemUIEvent.h @@ -1,26 +1,27 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <boost/shared_ptr.hpp> +#include <memory> -#include "Swift/Controllers/UIEvents/UIEvent.h" -#include "Swiften/MUC/MUCBookmark.h" +#include <Swiften/MUC/MUCBookmark.h> + +#include <Swift/Controllers/UIEvents/UIEvent.h> namespace Swift { - class RenameRosterItemUIEvent : public UIEvent { - public: - RenameRosterItemUIEvent(const JID& jid, const std::string& newName) : jid_(jid), newName_(newName) {} + class RenameRosterItemUIEvent : public UIEvent { + public: + RenameRosterItemUIEvent(const JID& jid, const std::string& newName) : jid_(jid), newName_(newName) {} - const JID& getJID() const {return jid_;} - const std::string& getNewName() const {return newName_;} + const JID& getJID() const {return jid_;} + const std::string& getNewName() const {return newName_;} - private: - JID jid_; - std::string newName_; - }; + private: + JID jid_; + std::string newName_; + }; } diff --git a/Swift/Controllers/UIEvents/RequestAdHocUIEvent.h b/Swift/Controllers/UIEvents/RequestAdHocUIEvent.h index a0b51f2..f6fa1c7 100644 --- a/Swift/Controllers/UIEvents/RequestAdHocUIEvent.h +++ b/Swift/Controllers/UIEvents/RequestAdHocUIEvent.h @@ -1,21 +1,20 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <Swift/Controllers/UIInterfaces/MainWindow.h> - #include <Swift/Controllers/UIEvents/UIEvent.h> +#include <Swift/Controllers/UIInterfaces/MainWindow.h> namespace Swift { - class RequestAdHocUIEvent : public UIEvent { - public: - RequestAdHocUIEvent(const DiscoItems::Item& command) : command_(command) {} - const DiscoItems::Item& getCommand() const {return command_;} - private: - DiscoItems::Item command_; - }; + class RequestAdHocUIEvent : public UIEvent { + public: + RequestAdHocUIEvent(const DiscoItems::Item& command) : command_(command) {} + const DiscoItems::Item& getCommand() const {return command_;} + private: + DiscoItems::Item command_; + }; } diff --git a/Swift/Controllers/UIEvents/RequestAdHocWithJIDUIEvent.h b/Swift/Controllers/UIEvents/RequestAdHocWithJIDUIEvent.h new file mode 100644 index 0000000..6fe2342 --- /dev/null +++ b/Swift/Controllers/UIEvents/RequestAdHocWithJIDUIEvent.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2014-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#pragma once + +#include <string> + +#include <Swiften/JID/JID.h> + +#include <Swift/Controllers/UIEvents/UIEvent.h> + +namespace Swift { + class RequestAdHocWithJIDUIEvent : public UIEvent { + public: + RequestAdHocWithJIDUIEvent(const JID& jid, const std::string& node) : jid_(jid), node_(node) {} + JID getJID() const { return jid_; } + std::string getNode() const { return node_; } + private: + JID jid_; + std::string node_; + }; +} diff --git a/Swift/Controllers/UIEvents/RequestAddUserDialogUIEvent.h b/Swift/Controllers/UIEvents/RequestAddUserDialogUIEvent.h index 7fe1926..474d155 100644 --- a/Swift/Controllers/UIEvents/RequestAddUserDialogUIEvent.h +++ b/Swift/Controllers/UIEvents/RequestAddUserDialogUIEvent.h @@ -1,29 +1,30 @@ /* - * Copyright (c) 2010-2012 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include "Swift/Controllers/UIEvents/UIEvent.h" #include <string> + #include <Swiften/JID/JID.h> +#include <Swift/Controllers/UIEvents/UIEvent.h> namespace Swift { - class RequestAddUserDialogUIEvent : public UIEvent { + class RequestAddUserDialogUIEvent : public UIEvent { - public: - RequestAddUserDialogUIEvent(const JID& predefinedJID, const std::string& predefinedName) : preJID_(predefinedJID), preName_(predefinedName) {} - RequestAddUserDialogUIEvent() : preJID_(), preName_() {} + public: + RequestAddUserDialogUIEvent(const JID& predefinedJID, const std::string& predefinedName) : preJID_(predefinedJID), preName_(predefinedName) {} + RequestAddUserDialogUIEvent() : preJID_(), preName_() {} - const JID& getPredefinedJID() const { return preJID_; } - const std::string& getPredefinedName() const { return preName_; } + const JID& getPredefinedJID() const { return preJID_; } + const std::string& getPredefinedName() const { return preName_; } - private: - JID preJID_; - std::string preName_; + private: + JID preJID_; + std::string preName_; - }; + }; } diff --git a/Swift/Controllers/UIEvents/RequestChangeBlockStateUIEvent.h b/Swift/Controllers/UIEvents/RequestChangeBlockStateUIEvent.h index 9b7abcb..4dcf8be 100644 --- a/Swift/Controllers/UIEvents/RequestChangeBlockStateUIEvent.h +++ b/Swift/Controllers/UIEvents/RequestChangeBlockStateUIEvent.h @@ -4,34 +4,40 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ -#pragma once +/* + * Copyright (c) 2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ -#include <Swift/Controllers/UIEvents/UIEvent.h> +#pragma once #include <Swiften/JID/JID.h> +#include <Swift/Controllers/UIEvents/UIEvent.h> + namespace Swift { class RequestChangeBlockStateUIEvent : public UIEvent { - public: - enum BlockState { - Blocked, - Unblocked - }; - - public: - RequestChangeBlockStateUIEvent(BlockState newState, const JID& contact) : state_(newState), contact_(contact) {} - - BlockState getBlockState() const { - return state_; - } - - JID getContact() const { - return contact_; - } - private: - BlockState state_; - JID contact_; + public: + enum BlockState { + Blocked, + Unblocked + }; + + public: + RequestChangeBlockStateUIEvent(BlockState newState, const JID& contact) : state_(newState), contact_(contact) {} + + BlockState getBlockState() const { + return state_; + } + + JID getContact() const { + return contact_; + } + private: + BlockState state_; + JID contact_; }; } diff --git a/Swift/Controllers/UIEvents/RequestChatUIEvent.h b/Swift/Controllers/UIEvents/RequestChatUIEvent.h index 4ef954f..4eca5d4 100644 --- a/Swift/Controllers/UIEvents/RequestChatUIEvent.h +++ b/Swift/Controllers/UIEvents/RequestChatUIEvent.h @@ -1,21 +1,21 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include "Swiften/JID/JID.h" +#include <Swiften/JID/JID.h> -#include "Swift/Controllers/UIEvents/UIEvent.h" +#include <Swift/Controllers/UIEvents/UIEvent.h> namespace Swift { - class RequestChatUIEvent : public UIEvent { - public: - RequestChatUIEvent(const JID& contact) : contact_(contact) {} - JID getContact() {return contact_;} - private: - JID contact_; - }; + class RequestChatUIEvent : public UIEvent { + public: + RequestChatUIEvent(const JID& contact) : contact_(contact) {} + JID getContact() {return contact_;} + private: + JID contact_; + }; } diff --git a/Swift/Controllers/UIEvents/RequestChatWithUserDialogUIEvent.h b/Swift/Controllers/UIEvents/RequestChatWithUserDialogUIEvent.h index 7b967bc..08804f4 100644 --- a/Swift/Controllers/UIEvents/RequestChatWithUserDialogUIEvent.h +++ b/Swift/Controllers/UIEvents/RequestChatWithUserDialogUIEvent.h @@ -1,15 +1,15 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include "Swift/Controllers/UIEvents/UIEvent.h" +#include <Swift/Controllers/UIEvents/UIEvent.h> namespace Swift { - class RequestChatWithUserDialogUIEvent : public UIEvent { + class RequestChatWithUserDialogUIEvent : public UIEvent { - }; + }; } diff --git a/Swift/Controllers/UIEvents/RequestContactEditorUIEvent.h b/Swift/Controllers/UIEvents/RequestContactEditorUIEvent.h index 8d04525..25a5e42 100644 --- a/Swift/Controllers/UIEvents/RequestContactEditorUIEvent.h +++ b/Swift/Controllers/UIEvents/RequestContactEditorUIEvent.h @@ -1,27 +1,28 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once #include <Swiften/JID/JID.h> + #include <Swift/Controllers/UIEvents/UIEvent.h> namespace Swift { - class RequestContactEditorUIEvent : public UIEvent { - public: - typedef boost::shared_ptr<RequestContactEditorUIEvent> ref; + class RequestContactEditorUIEvent : public UIEvent { + public: + typedef std::shared_ptr<RequestContactEditorUIEvent> ref; - RequestContactEditorUIEvent(const JID& jid) : jid(jid) { - } + RequestContactEditorUIEvent(const JID& jid) : jid(jid) { + } - const JID& getJID() const { - return jid; - } + const JID& getJID() const { + return jid; + } - private: - JID jid; - }; + private: + JID jid; + }; } diff --git a/Swift/Controllers/UIEvents/RequestHighlightEditorUIEvent.h b/Swift/Controllers/UIEvents/RequestHighlightEditorUIEvent.h index 42e22a2..0bfa458 100644 --- a/Swift/Controllers/UIEvents/RequestHighlightEditorUIEvent.h +++ b/Swift/Controllers/UIEvents/RequestHighlightEditorUIEvent.h @@ -10,7 +10,7 @@ namespace Swift { - class RequestHighlightEditorUIEvent : public UIEvent { - }; + class RequestHighlightEditorUIEvent : public UIEvent { + }; } diff --git a/Swift/Controllers/UIEvents/RequestHistoryUIEvent.h b/Swift/Controllers/UIEvents/RequestHistoryUIEvent.h index 025e91f..8282204 100644 --- a/Swift/Controllers/UIEvents/RequestHistoryUIEvent.h +++ b/Swift/Controllers/UIEvents/RequestHistoryUIEvent.h @@ -9,6 +9,6 @@ #include <Swift/Controllers/UIEvents/UIEvent.h> namespace Swift { - class RequestHistoryUIEvent : public UIEvent { - }; + class RequestHistoryUIEvent : public UIEvent { + }; } diff --git a/Swift/Controllers/UIEvents/RequestInviteToMUCUIEvent.h b/Swift/Controllers/UIEvents/RequestInviteToMUCUIEvent.h index 69aa0cd..a8e4bb7 100644 --- a/Swift/Controllers/UIEvents/RequestInviteToMUCUIEvent.h +++ b/Swift/Controllers/UIEvents/RequestInviteToMUCUIEvent.h @@ -4,32 +4,60 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ +/* + * Copyright (c) 2014-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + #pragma once -#include <boost/shared_ptr.hpp> +#include <memory> #include <vector> -#include <Swift/Controllers/UIEvents/UIEvent.h> #include <Swiften/JID/JID.h> -namespace Swift { - class RequestInviteToMUCUIEvent : public UIEvent { - public: - typedef boost::shared_ptr<RequestInviteToMUCUIEvent> ref; - - RequestInviteToMUCUIEvent(const JID& room, const std::vector<JID>& JIDsToInvite) : room_(room), invite_(JIDsToInvite) { - } - - const JID& getRoom() const { - return room_; - } - - const std::vector<JID> getInvites() const { - return invite_; - } +#include <Swift/Controllers/UIEvents/UIEvent.h> - private: - JID room_; - std::vector<JID> invite_; - }; +namespace Swift { + class RequestInviteToMUCUIEvent : public UIEvent { + public: + typedef std::shared_ptr<RequestInviteToMUCUIEvent> ref; + + enum ImpromptuMode { + Impromptu, + NotImpromptu + }; + + /** + * @brief RequestInviteToMUCUIEvent + * @param originator This can be a MUC JID if the user wants to invite + * people to an existing MUC, or a contact JID if this is the + * start of an impromptu group chat. + * @param JIDsToInvite This is a std::vector of JIDs which are prefilled + * in the invite dialog. + * @param impromptu This flag indicates whether it is a normal MUC invite + * or an impromptu MUC invite. + */ + RequestInviteToMUCUIEvent(const JID& originator, const std::vector<JID>& JIDsToInvite, ImpromptuMode impromptu) : originator_(originator), invite_(JIDsToInvite) { + isImpromptu_ = impromptu == Impromptu; + } + + const JID& getOriginator() const { + return originator_; + } + + const std::vector<JID> getInvites() const { + return invite_; + } + + bool isImpromptu() const { + return isImpromptu_; + } + + private: + JID originator_; + std::vector<JID> invite_; + bool isImpromptu_; + }; } diff --git a/Swift/Controllers/UIEvents/RequestJoinMUCUIEvent.h b/Swift/Controllers/UIEvents/RequestJoinMUCUIEvent.h index 2c7b105..5e94290 100644 --- a/Swift/Controllers/UIEvents/RequestJoinMUCUIEvent.h +++ b/Swift/Controllers/UIEvents/RequestJoinMUCUIEvent.h @@ -1,30 +1,31 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <boost/shared_ptr.hpp> +#include <memory> #include <string> -#include <Swift/Controllers/UIEvents/UIEvent.h> #include <Swiften/JID/JID.h> +#include <Swift/Controllers/UIEvents/UIEvent.h> + namespace Swift { - class RequestJoinMUCUIEvent : public UIEvent { - public: - typedef boost::shared_ptr<RequestJoinMUCUIEvent> ref; + class RequestJoinMUCUIEvent : public UIEvent { + public: + typedef std::shared_ptr<RequestJoinMUCUIEvent> ref; - RequestJoinMUCUIEvent(const JID& room = JID()) : room(room) { - } + RequestJoinMUCUIEvent(const JID& room = JID()) : room(room) { + } - const JID& getRoom() const { - return room; - } + const JID& getRoom() const { + return room; + } - private: - JID room; - }; + private: + JID room; + }; } diff --git a/Swift/Controllers/UIEvents/RequestProfileEditorUIEvent.h b/Swift/Controllers/UIEvents/RequestProfileEditorUIEvent.h index 107e2e8..1a02af4 100644 --- a/Swift/Controllers/UIEvents/RequestProfileEditorUIEvent.h +++ b/Swift/Controllers/UIEvents/RequestProfileEditorUIEvent.h @@ -1,16 +1,16 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include "Swift/Controllers/UIEvents/UIEvent.h" +#include <Swift/Controllers/UIEvents/UIEvent.h> namespace Swift { - class RequestProfileEditorUIEvent : public UIEvent { - public: - RequestProfileEditorUIEvent() {} - }; + class RequestProfileEditorUIEvent : public UIEvent { + public: + RequestProfileEditorUIEvent() {} + }; } diff --git a/Swift/Controllers/UIEvents/RequestWhiteboardUIEvent.h b/Swift/Controllers/UIEvents/RequestWhiteboardUIEvent.h index 5c44da7..9c2b01d 100644 --- a/Swift/Controllers/UIEvents/RequestWhiteboardUIEvent.h +++ b/Swift/Controllers/UIEvents/RequestWhiteboardUIEvent.h @@ -4,18 +4,24 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ +/* + * Copyright (c) 2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + #pragma once -#include "Swiften/JID/JID.h" +#include <Swiften/JID/JID.h> -#include "Swift/Controllers/UIEvents/UIEvent.h" +#include <Swift/Controllers/UIEvents/UIEvent.h> namespace Swift { - class RequestWhiteboardUIEvent : public UIEvent { - public: - RequestWhiteboardUIEvent(const JID& contact) : contact_(contact) {} - const JID& getContact() const {return contact_;} - private: - JID contact_; - }; + class RequestWhiteboardUIEvent : public UIEvent { + public: + RequestWhiteboardUIEvent(const JID& contact) : contact_(contact) {} + const JID& getContact() const {return contact_;} + private: + JID contact_; + }; } diff --git a/Swift/Controllers/UIEvents/RequestXMLConsoleUIEvent.h b/Swift/Controllers/UIEvents/RequestXMLConsoleUIEvent.h index a1daf29..4d780be 100644 --- a/Swift/Controllers/UIEvents/RequestXMLConsoleUIEvent.h +++ b/Swift/Controllers/UIEvents/RequestXMLConsoleUIEvent.h @@ -1,14 +1,14 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include "Swift/Controllers/UIEvents/UIEvent.h" +#include <Swift/Controllers/UIEvents/UIEvent.h> namespace Swift { - class RequestXMLConsoleUIEvent : public UIEvent { - }; + class RequestXMLConsoleUIEvent : public UIEvent { + }; } diff --git a/Swift/Controllers/UIEvents/SendFileUIEvent.h b/Swift/Controllers/UIEvents/SendFileUIEvent.h index 3bfa69d..26e4940 100644 --- a/Swift/Controllers/UIEvents/SendFileUIEvent.h +++ b/Swift/Controllers/UIEvents/SendFileUIEvent.h @@ -4,31 +4,38 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ +/* + * Copyright (c) 2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + #pragma once #include <string> #include <Swiften/JID/JID.h> + #include <Swift/Controllers/UIEvents/UIEvent.h> namespace Swift { - class SendFileUIEvent : public UIEvent { - public: - typedef boost::shared_ptr<SendFileUIEvent> ref; - - SendFileUIEvent(const JID& jid, const std::string& filename) : jid(jid), filename(filename) { - } - - const JID& getJID() const { - return jid; - } - - const std::string& getFilename() const { - return filename; - } - - private: - JID jid; - std::string filename; - }; + class SendFileUIEvent : public UIEvent { + public: + typedef std::shared_ptr<SendFileUIEvent> ref; + + SendFileUIEvent(const JID& jid, const std::string& filename) : jid(jid), filename(filename) { + } + + const JID& getJID() const { + return jid; + } + + const std::string& getFilename() const { + return filename; + } + + private: + JID jid; + std::string filename; + }; } diff --git a/Swift/Controllers/UIEvents/ShowProfileForRosterItemUIEvent.h b/Swift/Controllers/UIEvents/ShowProfileForRosterItemUIEvent.h index 4a603ea..9b2f60f 100644 --- a/Swift/Controllers/UIEvents/ShowProfileForRosterItemUIEvent.h +++ b/Swift/Controllers/UIEvents/ShowProfileForRosterItemUIEvent.h @@ -4,22 +4,29 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ +/* + * Copyright (c) 2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + #pragma once #include <Swiften/JID/JID.h> + #include <Swift/Controllers/UIEvents/UIEvent.h> namespace Swift { class ShowProfileForRosterItemUIEvent : public UIEvent { - public: - typedef boost::shared_ptr<ShowProfileForRosterItemUIEvent> ref; - public: - ShowProfileForRosterItemUIEvent(const JID& jid) : jid_(jid) {} - virtual ~ShowProfileForRosterItemUIEvent() {} - JID getJID() const {return jid_;} - private: - JID jid_; + public: + typedef std::shared_ptr<ShowProfileForRosterItemUIEvent> ref; + public: + ShowProfileForRosterItemUIEvent(const JID& jid) : jid_(jid) {} + virtual ~ShowProfileForRosterItemUIEvent() {} + JID getJID() const {return jid_;} + private: + JID jid_; }; } diff --git a/Swift/Controllers/UIEvents/ShowWhiteboardUIEvent.h b/Swift/Controllers/UIEvents/ShowWhiteboardUIEvent.h index bb72d9b..a1b6efb 100644 --- a/Swift/Controllers/UIEvents/ShowWhiteboardUIEvent.h +++ b/Swift/Controllers/UIEvents/ShowWhiteboardUIEvent.h @@ -4,19 +4,25 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ +/* + * Copyright (c) 2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + #pragma once -#include "Swiften/JID/JID.h" +#include <Swiften/JID/JID.h> -#include "Swift/Controllers/UIEvents/UIEvent.h" +#include <Swift/Controllers/UIEvents/UIEvent.h> namespace Swift { - class ShowWhiteboardUIEvent : public UIEvent { - public: - ShowWhiteboardUIEvent(const JID& contact) : contact_(contact) {} - const JID& getContact() const {return contact_;} - private: - JID contact_; - }; + class ShowWhiteboardUIEvent : public UIEvent { + public: + ShowWhiteboardUIEvent(const JID& contact) : contact_(contact) {} + const JID& getContact() const {return contact_;} + private: + JID contact_; + }; } diff --git a/Swift/Controllers/UIEvents/UIEvent.cpp b/Swift/Controllers/UIEvents/UIEvent.cpp index d77e1c9..b1e870d 100644 --- a/Swift/Controllers/UIEvents/UIEvent.cpp +++ b/Swift/Controllers/UIEvents/UIEvent.cpp @@ -1,10 +1,10 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ -#include "Swift/Controllers/UIEvents/UIEvent.h" +#include <Swift/Controllers/UIEvents/UIEvent.h> namespace Swift { UIEvent::~UIEvent() { diff --git a/Swift/Controllers/UIEvents/UIEvent.h b/Swift/Controllers/UIEvents/UIEvent.h index e79a4f1..5363a49 100644 --- a/Swift/Controllers/UIEvents/UIEvent.h +++ b/Swift/Controllers/UIEvents/UIEvent.h @@ -1,18 +1,18 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <boost/shared_ptr.hpp> +#include <memory> namespace Swift { - class UIEvent { - public: - typedef boost::shared_ptr<UIEvent> ref; + class UIEvent { + public: + typedef std::shared_ptr<UIEvent> ref; - virtual ~UIEvent(); - }; + virtual ~UIEvent(); + }; } diff --git a/Swift/Controllers/UIEvents/UIEventStream.h b/Swift/Controllers/UIEvents/UIEventStream.h index b1337a2..e6e3f80 100644 --- a/Swift/Controllers/UIEvents/UIEventStream.h +++ b/Swift/Controllers/UIEvents/UIEventStream.h @@ -1,23 +1,24 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include "Swiften/Base/boost_bsignals.h" -#include <boost/shared_ptr.hpp> +#include <memory> -#include "Swift/Controllers/UIEvents/UIEvent.h" +#include <boost/signals2.hpp> + +#include <Swift/Controllers/UIEvents/UIEvent.h> namespace Swift { - class UIEventStream { - public: - boost::signal<void (boost::shared_ptr<UIEvent>)> onUIEvent; + class UIEventStream { + public: + boost::signals2::signal<void (std::shared_ptr<UIEvent>)> onUIEvent; - void send(boost::shared_ptr<UIEvent> event) { - onUIEvent(event); - } - }; + void send(std::shared_ptr<UIEvent> event) { + onUIEvent(event); + } + }; } diff --git a/Swift/Controllers/UIInterfaces/AdHocCommandWindow.h b/Swift/Controllers/UIInterfaces/AdHocCommandWindow.h index 835defe..ceb1531 100644 --- a/Swift/Controllers/UIInterfaces/AdHocCommandWindow.h +++ b/Swift/Controllers/UIInterfaces/AdHocCommandWindow.h @@ -1,14 +1,18 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once +#include <boost/signals2.hpp> + namespace Swift { - class AdHocCommandWindow { - public: - virtual ~AdHocCommandWindow() {} - }; + class AdHocCommandWindow { + public: + virtual ~AdHocCommandWindow() {} + virtual void setOnline(bool /*online*/) {} + boost::signals2::signal<void ()> onClosing; + }; } diff --git a/Swift/Controllers/UIInterfaces/AdHocCommandWindowFactory.h b/Swift/Controllers/UIInterfaces/AdHocCommandWindowFactory.h index ae77180..7b9b6f7 100644 --- a/Swift/Controllers/UIInterfaces/AdHocCommandWindowFactory.h +++ b/Swift/Controllers/UIInterfaces/AdHocCommandWindowFactory.h @@ -1,22 +1,20 @@ /* - * Copyright (c) 2010-2011 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <Swift/Controllers/UIInterfaces/AdHocCommandWindow.h> #include <Swiften/AdHoc/OutgoingAdHocCommandSession.h> +#include <Swift/Controllers/UIInterfaces/AdHocCommandWindow.h> + namespace Swift { - class AdHocCommandWindowFactory { - public: - virtual ~AdHocCommandWindowFactory() {} - /** - * The UI should deal with the lifetime of this window (i.e. DeleteOnClose), - * so the result isn't returned. - */ - virtual void createAdHocCommandWindow(boost::shared_ptr<OutgoingAdHocCommandSession> command) = 0; - }; +class AdHocCommandWindow; + class AdHocCommandWindowFactory { + public: + virtual ~AdHocCommandWindowFactory() {} + virtual AdHocCommandWindow* createAdHocCommandWindow(std::shared_ptr<OutgoingAdHocCommandSession> command) = 0; + }; } diff --git a/Swift/Controllers/UIInterfaces/BlockListEditorWidget.h b/Swift/Controllers/UIInterfaces/BlockListEditorWidget.h index 60a1c11..58b45d0 100644 --- a/Swift/Controllers/UIInterfaces/BlockListEditorWidget.h +++ b/Swift/Controllers/UIInterfaces/BlockListEditorWidget.h @@ -4,29 +4,38 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ +/* + * Copyright (c) 2014-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + #pragma once #include <vector> +#include <boost/signals2.hpp> + #include <Swiften/JID/JID.h> -#include <Swiften/Base/boost_bsignals.h> namespace Swift { - class ClientBlockListManager; + class ClientBlockListManager; - class BlockListEditorWidget { - public: - virtual ~BlockListEditorWidget() {} + class BlockListEditorWidget { + public: + virtual ~BlockListEditorWidget() {} - virtual void show() = 0; + virtual void show() = 0; + virtual void hide() = 0; - virtual void setCurrentBlockList(const std::vector<JID>& blockedJIDs) = 0; - virtual void setBusy(bool isBusy) = 0; + virtual void setCurrentBlockList(const std::vector<JID>& blockedJIDs) = 0; + virtual void setBusy(bool isBusy) = 0; + virtual void setError(const std::string&) = 0; - virtual std::vector<JID> getCurrentBlockList() const = 0; + virtual std::vector<JID> getCurrentBlockList() const = 0; - boost::signal<void (const std::vector<JID>& /* blockedJID */)> onSetNewBlockList; - }; + boost::signals2::signal<void (const std::vector<JID>& /* blockedJID */)> onSetNewBlockList; + }; } diff --git a/Swift/Controllers/UIInterfaces/BlockListEditorWidgetFactory.h b/Swift/Controllers/UIInterfaces/BlockListEditorWidgetFactory.h index eb91ac1..05e7f3a 100644 --- a/Swift/Controllers/UIInterfaces/BlockListEditorWidgetFactory.h +++ b/Swift/Controllers/UIInterfaces/BlockListEditorWidgetFactory.h @@ -8,13 +8,13 @@ namespace Swift { - class BlockListEditorWidget; + class BlockListEditorWidget; - class BlockListEditorWidgetFactory { - public: - virtual ~BlockListEditorWidgetFactory() {} + class BlockListEditorWidgetFactory { + public: + virtual ~BlockListEditorWidgetFactory() {} - virtual BlockListEditorWidget* createBlockListEditorWidget() = 0; - }; + virtual BlockListEditorWidget* createBlockListEditorWidget() = 0; + }; } diff --git a/Swift/Controllers/UIInterfaces/ChatListWindow.cpp b/Swift/Controllers/UIInterfaces/ChatListWindow.cpp index f649b44..5ad40fe 100644 --- a/Swift/Controllers/UIInterfaces/ChatListWindow.cpp +++ b/Swift/Controllers/UIInterfaces/ChatListWindow.cpp @@ -1,10 +1,10 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ -#include "Swift/Controllers/UIInterfaces/ChatListWindow.h" +#include <Swift/Controllers/UIInterfaces/ChatListWindow.h> namespace Swift { diff --git a/Swift/Controllers/UIInterfaces/ChatListWindow.h b/Swift/Controllers/UIInterfaces/ChatListWindow.h index b189e72..6aa729b 100644 --- a/Swift/Controllers/UIInterfaces/ChatListWindow.h +++ b/Swift/Controllers/UIInterfaces/ChatListWindow.h @@ -1,79 +1,119 @@ /* - * Copyright (c) 2010-2011 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once #include <list> -#include <set> #include <map> -#include <boost/shared_ptr.hpp> -#include <Swiften/MUC/MUCBookmark.h> -#include <Swiften/Elements/StatusShow.h> +#include <memory> +#include <set> + + +#include <boost/algorithm/string/join.hpp> #include <boost/filesystem/path.hpp> -#include <Swiften/Base/foreach.h> -#include <Swiften/Base/boost_bsignals.h> +#include <boost/signals2.hpp> + +#include <Swiften/Base/Algorithm.h> +#include <Swiften/Elements/StatusShow.h> +#include <Swiften/MUC/MUCBookmark.h> namespace Swift { - class ChatListWindow { - public: - class Chat { - public: - Chat() : statusType(StatusShow::None), isMUC(false), unreadCount(0) {} - Chat(const JID& jid, const std::string& chatName, const std::string& activity, int unreadCount, StatusShow::Type statusType, const boost::filesystem::path& avatarPath, bool isMUC, const std::string& nick = "") - : jid(jid), chatName(chatName), activity(activity), statusType(statusType), isMUC(isMUC), nick(nick), unreadCount(unreadCount), avatarPath(avatarPath) {} - /** 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; - } - void setStatusType(StatusShow::Type type) { - statusType = type; - } - void setAvatarPath(const boost::filesystem::path& path) { - avatarPath = path; - } - std::string getImpromptuTitle() const { - typedef std::pair<std::string, JID> StringJIDPair; - std::string title; - foreach(StringJIDPair pair, impromptuJIDs) { - if (title.empty()) { - title += pair.first; - } else { - title += ", " + pair.first; - } - } - return title; - } - JID jid; - std::string chatName; - std::string activity; - StatusShow::Type statusType; - bool isMUC; - std::string nick; - int unreadCount; - boost::filesystem::path avatarPath; - std::map<std::string, JID> impromptuJIDs; - }; - virtual ~ChatListWindow(); + class ChatListWindow { + public: + class Chat { + public: + Chat() : statusType(StatusShow::None), isMUC(false), unreadCount(0), isPrivateMessage(false) {} + Chat(const JID& jid, const std::string& chatName, const std::string& activity, size_t unreadCount, StatusShow::Type statusType, const boost::filesystem::path& avatarPath, bool isMUC, bool isPrivateMessage = false, const std::string& nick = "", const boost::optional<std::string> password = boost::optional<std::string>()) + : jid(jid), chatName(chatName), activity(activity), statusType(statusType), isMUC(isMUC), nick(nick), password(password), unreadCount(unreadCount), avatarPath(avatarPath), isPrivateMessage(isPrivateMessage) {} + /** Assume that nicks and other transient features aren't important for equality */ + bool operator==(const Chat& other) const { + if (impromptuJIDs.empty()) { + return jid.toBare() == other.jid.toBare() + && isMUC == other.isMUC; + } + if (chatName == other.chatName) { + return true; + } + else { /* compare the chat occupant lists */ + for (const auto& jid : impromptuJIDs) { + bool found = false; + for (const auto& otherJID : other.impromptuJIDs) { + if (jid.second.toBare() == otherJID.second.toBare()) { + found = true; + break; + } + } + if (!found) { + return false; + } + } + return key_compare(inviteesNames, other.inviteesNames); + } + } + void setUnreadCount(size_t unread) { + unreadCount = unread; + } + void setStatusType(StatusShow::Type type) { + statusType = type; + } + void setAvatarPath(const boost::filesystem::path& path) { + avatarPath = path; + } + std::string getImpromptuTitle() const { + std::set<std::string> participants; + std::map<JID, std::string> participantsMap; + + for (auto& pair : inviteesNames) { + if (!pair.second.empty()) { + participantsMap[pair.first] = pair.second; + } + else { + participantsMap[pair.first] = pair.first.toString(); + } + } + for (auto& pair : impromptuJIDs) { + participantsMap[pair.second] = pair.first; + } + for (auto& participant : participantsMap) { + participants.insert(participant.second); + } + return boost::algorithm::join(participants, ", "); + } + std::string getTitle() const { + std::string title = getImpromptuTitle(); + return title.empty() ? chatName : title; + } + JID jid; + std::string chatName; + std::string activity; + StatusShow::Type statusType; + bool isMUC; + std::string nick; + boost::optional<std::string> password; + size_t unreadCount; + boost::filesystem::path avatarPath; + std::map<std::string, JID> impromptuJIDs; + std::map<JID, std::string> inviteesNames; + bool isPrivateMessage; + }; + virtual ~ChatListWindow(); - virtual void setBookmarksEnabled(bool enabled) = 0; - virtual void addMUCBookmark(const MUCBookmark& bookmark) = 0; - virtual void addWhiteboardSession(const ChatListWindow::Chat& chat) = 0; - virtual void removeWhiteboardSession(const JID& jid) = 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; + virtual void setBookmarksEnabled(bool enabled) = 0; + virtual void addMUCBookmark(const MUCBookmark& bookmark) = 0; + virtual void addWhiteboardSession(const ChatListWindow::Chat& chat) = 0; + virtual void removeWhiteboardSession(const JID& jid) = 0; + virtual void removeMUCBookmark(const MUCBookmark& bookmark) = 0; + virtual void setRecents(const std::list<Chat>& recents) = 0; + virtual void setUnreadCount(size_t unread) = 0; + virtual void clearBookmarks() = 0; + virtual void setOnline(bool isOnline) = 0; - boost::signal<void (const MUCBookmark&)> onMUCBookmarkActivated; - boost::signal<void (const Chat&)> onRecentActivated; - boost::signal<void (const JID&)> onWhiteboardActivated; - boost::signal<void ()> onClearRecentsRequested; - }; + boost::signals2::signal<void (const MUCBookmark&)> onMUCBookmarkActivated; + boost::signals2::signal<void (const Chat&)> onRecentActivated; + boost::signals2::signal<void (const JID&)> onWhiteboardActivated; + boost::signals2::signal<void ()> onClearRecentsRequested; + }; } diff --git a/Swift/Controllers/UIInterfaces/ChatListWindowFactory.h b/Swift/Controllers/UIInterfaces/ChatListWindowFactory.h index cf8409e..3ae1ec6 100644 --- a/Swift/Controllers/UIInterfaces/ChatListWindowFactory.h +++ b/Swift/Controllers/UIInterfaces/ChatListWindowFactory.h @@ -1,18 +1,18 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include "Swift/Controllers/UIInterfaces/ChatListWindow.h" +#include <Swift/Controllers/UIInterfaces/ChatListWindow.h> namespace Swift { - class UIEventStream; - class ChatListWindowFactory { - public: - virtual ~ChatListWindowFactory() {} - virtual ChatListWindow* createChatListWindow(UIEventStream* uiEventStream) = 0; - }; + class UIEventStream; + class ChatListWindowFactory { + public: + virtual ~ChatListWindowFactory() {} + virtual ChatListWindow* createChatListWindow(UIEventStream* uiEventStream) = 0; + }; } diff --git a/Swift/Controllers/UIInterfaces/ChatWindow.h b/Swift/Controllers/UIInterfaces/ChatWindow.h index e6f61ca..1c36ffc 100644 --- a/Swift/Controllers/UIInterfaces/ChatWindow.h +++ b/Swift/Controllers/UIInterfaces/ChatWindow.h @@ -1,207 +1,290 @@ /* - * Copyright (c) 2010-2012 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <vector> +#include <memory> #include <string> +#include <vector> -#include <boost/optional.hpp> -#include <boost/shared_ptr.hpp> -#include <boost/make_shared.hpp> +#include <boost/algorithm/string.hpp> #include <boost/date_time/posix_time/posix_time.hpp> +#include <boost/optional.hpp> +#include <boost/signals2.hpp> -#include <Swiften/Base/boost_bsignals.h> -#include <Swiften/Elements/SecurityLabelsCatalog.h> +#include <Swiften/Base/Tristate.h> #include <Swiften/Elements/ChatState.h> #include <Swiften/Elements/Form.h> #include <Swiften/Elements/MUCOccupant.h> -#include <Swift/Controllers/HighlightManager.h> +#include <Swiften/Elements/SecurityLabelsCatalog.h> +#include <Swiften/MUC/MUCBookmark.h> +#include <Swift/Controllers/Highlighting/HighlightManager.h> namespace Swift { - class AvatarManager; - class TreeWidget; - class Roster; - class TabComplete; - class RosterItem; - class ContactRosterItem; - class FileTransferController; - class UserSearchWindow; - - - class ChatWindow { - public: - class ChatMessagePart { - public: - virtual ~ChatMessagePart() {} - }; - - class ChatMessage { - public: - ChatMessage() {} - ChatMessage(const std::string& text) { - append(boost::make_shared<ChatTextMessagePart>(text)); - } - void append(const boost::shared_ptr<ChatMessagePart>& part) { - parts_.push_back(part); - } - - const std::vector<boost::shared_ptr<ChatMessagePart> >& getParts() const { - return parts_; - } - private: - std::vector<boost::shared_ptr<ChatMessagePart> > parts_; - }; - - class ChatTextMessagePart : public ChatMessagePart { - public: - ChatTextMessagePart(const std::string& text) : text(text) {} - std::string text; - }; - - class ChatURIMessagePart : public ChatMessagePart { - public: - ChatURIMessagePart(const std::string& target) : target(target) {} - std::string target; - }; - - class ChatEmoticonMessagePart : public ChatMessagePart { - public: - std::string imagePath; - std::string alternativeText; - }; - - class ChatHighlightingMessagePart : public ChatMessagePart { - public: - std::string foregroundColor; - std::string backgroundColor; - std::string text; - }; - - - enum AckState {Pending, Received, Failed}; - enum ReceiptState {ReceiptRequested, ReceiptReceived}; - enum Tristate {Yes, No, Maybe}; - enum OccupantAction {Kick, Ban, MakeModerator, MakeParticipant, MakeVisitor, AddContact, ShowProfile}; - enum RoomAction {ChangeSubject, Configure, Affiliations, Destroy, Invite}; - enum FileTransferState {WaitingForAccept, Negotiating, Transferring, Canceled, Finished, FTFailed}; - enum WhiteboardSessionState {WhiteboardAccepted, WhiteboardTerminated, WhiteboardRejected}; - enum BlockingState {BlockingUnsupported, IsBlocked, IsUnblocked}; - enum Direction { UnknownDirection, DefaultDirection }; - - ChatWindow() {} - virtual ~ChatWindow() {} - - /** Add message to window. - * @return id of added message (for acks). - */ - virtual std::string addMessage(const ChatMessage& message, const std::string& senderName, bool senderIsSelf, boost::shared_ptr<SecurityLabel> label, const std::string& avatarPath, const boost::posix_time::ptime& time, const HighlightAction& highlight) = 0; - /** Adds action to window. - * @return id of added message (for acks); - */ - virtual std::string addAction(const ChatMessage& message, const std::string& senderName, bool senderIsSelf, boost::shared_ptr<SecurityLabel> label, const std::string& avatarPath, const boost::posix_time::ptime& time, const HighlightAction& highlight) = 0; - - virtual void addSystemMessage(const ChatMessage& message, Direction direction) = 0; - virtual void addPresenceMessage(const ChatMessage& message, Direction direction) = 0; - - virtual void addErrorMessage(const ChatMessage& message) = 0; - virtual void replaceMessage(const ChatMessage& message, const std::string& id, const boost::posix_time::ptime& time, const HighlightAction& highlight) = 0; - virtual void replaceWithAction(const ChatMessage& message, const std::string& id, const boost::posix_time::ptime& time, const HighlightAction& highlight) = 0; - - // File transfer related stuff - virtual std::string addFileTransfer(const std::string& senderName, bool senderIsSelf, const std::string& filename, const boost::uintmax_t sizeInBytes) = 0; - virtual void setFileTransferProgress(std::string, const int percentageDone) = 0; - virtual void setFileTransferStatus(std::string, const FileTransferState state, const std::string& msg = "") = 0; - virtual void addMUCInvitation(const std::string& senderName, const JID& jid, const std::string& reason, const std::string& password, bool direct = true, bool isImpromptu = false, bool isContinuation = false) = 0; - - virtual std::string addWhiteboardRequest(bool senderIsSelf) = 0; - virtual void setWhiteboardSessionStatus(std::string id, const ChatWindow::WhiteboardSessionState state) = 0; - - // message receipts - virtual void setMessageReceiptState(const std::string& id, ChatWindow::ReceiptState state) = 0; - - virtual void setContactChatState(ChatState::ChatStateType state) = 0; - virtual void setName(const std::string& name) = 0; - virtual void show() = 0; - virtual void activate() = 0; - virtual void setAvailableSecurityLabels(const std::vector<SecurityLabelsCatalog::Item>& labels) = 0; - virtual void setSecurityLabelsEnabled(bool enabled) = 0; - virtual void setCorrectionEnabled(Tristate enabled) = 0; - virtual void setUnreadMessageCount(int count) = 0; - virtual void convertToMUC(bool impromptuMUC = false) = 0; -// virtual TreeWidget *getTreeWidget() = 0; - virtual void setSecurityLabelsError() = 0; - virtual SecurityLabelsCatalog::Item getSelectedSecurityLabel() = 0; - virtual void setInputEnabled(bool enabled) = 0; - virtual void setRosterModel(Roster* model) = 0; - virtual void setTabComplete(TabComplete* completer) = 0; - virtual void replaceLastMessage(const ChatMessage& message) = 0; - virtual void setAckState(const std::string& id, AckState state) = 0; - virtual void flash() = 0; - virtual void setSubject(const std::string& subject) = 0; - virtual void setAffiliations(MUCOccupant::Affiliation, const std::vector<JID>&) = 0; - virtual void setAvailableRoomActions(const std::vector<RoomAction> &actions) = 0; - virtual void setBlockingState(BlockingState state) = 0; - virtual void setCanInitiateImpromptuChats(bool supportsImpromptu) = 0; - /** - * Set an alert on the window. - * @param alertText Description of alert (required). - * @param buttonText Button text to use (optional, no button is shown if empty). - */ - virtual void setAlert(const std::string& alertText, const std::string& buttonText = "") = 0; - /** - * Removes an alert. - */ - virtual void cancelAlert() = 0; - - /** - * Actions that can be performed on the selected occupant. - */ - virtual void setAvailableOccupantActions(const std::vector<OccupantAction>& actions) = 0; - - /** - * A room configuration has been requested, show the form. - * If the form is cancelled, must emit onConfigurationFormCancelled(). - */ - virtual void showRoomConfigurationForm(Form::ref) = 0; - - boost::signal<void ()> onClosed; - boost::signal<void ()> onAllMessagesRead; - boost::signal<void (const std::string&, bool isCorrection)> onSendMessageRequest; - boost::signal<void ()> onSendCorrectionMessageRequest; - boost::signal<void ()> onUserTyping; - boost::signal<void ()> onUserCancelsTyping; - boost::signal<void ()> onAlertButtonClicked; - boost::signal<void (ContactRosterItem*)> onOccupantSelectionChanged; - boost::signal<void (ChatWindow::OccupantAction, ContactRosterItem*)> onOccupantActionSelected; - boost::signal<void (const std::string&)> onChangeSubjectRequest; - boost::signal<void (Form::ref)> onConfigureRequest; - boost::signal<void ()> onDestroyRequest; - boost::signal<void (const std::vector<JID>&)> onInviteToChat; - boost::signal<void ()> onConfigurationFormCancelled; - boost::signal<void ()> onGetAffiliationsRequest; - boost::signal<void (MUCOccupant::Affiliation, const JID&)> onSetAffiliationRequest; - boost::signal<void (const std::vector<std::pair<MUCOccupant::Affiliation, JID> >& changes)> onChangeAffiliationsRequest; - boost::signal<void ()> onLogCleared; - - // File transfer related - boost::signal<void (std::string /* id */)> onFileTransferCancel; - boost::signal<void (std::string /* id */, std::string /* description */)> onFileTransferStart; - boost::signal<void (std::string /* id */, std::string /* path */)> onFileTransferAccept; - boost::signal<void (std::string /* path */)> onSendFileRequest; - - //Whiteboard related - boost::signal<void ()> onWhiteboardSessionAccept; - boost::signal<void ()> onWhiteboardSessionCancel; - boost::signal<void ()> onWhiteboardWindowShow; - - // Blocking Command related - boost::signal<void ()> onBlockUserRequest; - boost::signal<void ()> onUnblockUserRequest; - }; -} + class AvatarManager; + class TreeWidget; + class Roster; + class TabComplete; + class RosterItem; + class ContactRosterItem; + class FileTransferController; + class UserSearchWindow; + class DiscoInfo; + class ErrorPayload; + + + class ChatWindow { + public: + class ChatMessagePart { + public: + virtual ~ChatMessagePart() {} + }; + + class ChatMessage { + public: + ChatMessage() {} + + ChatMessage(const std::string& text) { + append(std::make_shared<ChatTextMessagePart>(text)); + } + + void append(const std::shared_ptr<ChatMessagePart>& part) { + parts_.push_back(part); + } + + const std::vector<std::shared_ptr<ChatMessagePart> >& getParts() const { + return parts_; + } + + void setParts(const std::vector<std::shared_ptr<ChatMessagePart> >& parts) { + parts_ = parts; + } + + void setHighlightActionSender(const HighlightAction& action) { + highlightActionSender_ = action; + } + + const HighlightAction& getHighlightActionSender() const { + return highlightActionSender_; + } + + void setHighlightActionOwnMention(const HighlightAction& action) { + highlightActionOwnMention_ = action; + } + + const HighlightAction& getHighlightActionOwnMention() const { + return highlightActionOwnMention_; + } + + void setHighlightActionGroupMessage(const HighlightAction& action) { + highlightActionGroupMessage_ = action; + } + + const HighlightAction& getHighlightActionGroupMessage() const { + return highlightActionGroupMessage_; + } + + void setHighlightActonDirectMessage(const HighlightAction& action) { + highlightActionDirectMessage_ = action; + } + + const HighlightAction& getHighlightActionDirectMessage() const { + return highlightActionDirectMessage_; + } + + bool isMeCommand() const { + return isMeCommand_; + } + + void setIsMeCommand(bool isMeCommand) { + isMeCommand_ = isMeCommand; + } + private: + std::vector<std::shared_ptr<ChatMessagePart> > parts_; + HighlightAction highlightActionSender_; + HighlightAction highlightActionOwnMention_; + HighlightAction highlightActionGroupMessage_; + HighlightAction highlightActionDirectMessage_; + bool isMeCommand_ = false; + }; + + class ChatTextMessagePart : public ChatMessagePart { + public: + ChatTextMessagePart(const std::string& text) : text(text) {} + std::string text; + }; + + class ChatURIMessagePart : public ChatMessagePart { + public: + ChatURIMessagePart(const std::string& target) : target(target) {} + std::string target; + }; + + class ChatEmoticonMessagePart : public ChatMessagePart { + public: + std::string imagePath; + std::string alternativeText; + }; + + class ChatHighlightingMessagePart : public ChatMessagePart { + public: + HighlightAction action; + std::string text; + }; + + + enum AckState {Pending, Received, Failed}; + enum ReceiptState {ReceiptRequested, ReceiptReceived, ReceiptFailed}; + enum OccupantAction {Kick, Ban, MakeModerator, MakeParticipant, MakeVisitor, AddContact, ShowProfile}; + enum RoomAction {ChangeSubject, Configure, Affiliations, Destroy, Invite, Leave}; + enum FileTransferState { + Initialisation, ///< Collecting information required for sending the request out. + WaitingForAccept, ///< The file transfer request was send out. + Negotiating, ///< The other party accepted the file transfer request and a suitable transfer method is negotiated. + Transferring, ///< The negotiation was successful and the file is currently transferred. + Canceled, ///< Someone actively canceled the transfer. + Finished, ///< The file was transferred successfully. + FTFailed ///< The negotiation, the transfer itself or the verification failed. + }; + enum WhiteboardSessionState {WhiteboardAccepted, WhiteboardTerminated, WhiteboardRejected}; + enum BlockingState {BlockingUnsupported, IsBlocked, IsUnblocked}; + enum Direction { UnknownDirection, DefaultDirection }; + enum MUCType { StandardMUC, ImpromptuMUC }; + enum TimestampBehaviour { KeepTimestamp, UpdateTimestamp }; + enum RoomBookmarkState { RoomNotBookmarked, RoomBookmarked, RoomAutoJoined }; + + ChatWindow() {} + virtual ~ChatWindow() {} + + /** Add message to window. + * @return id of added message (for acks). + */ + virtual std::string addMessage(const ChatMessage& message, const std::string& senderName, bool senderIsSelf, std::shared_ptr<SecurityLabel> label, const std::string& avatarPath, const boost::posix_time::ptime& time) = 0; + /** Adds action to window. + * @return id of added message (for acks); + */ + virtual std::string addAction(const ChatMessage& message, const std::string& senderName, bool senderIsSelf, std::shared_ptr<SecurityLabel> label, const std::string& avatarPath, const boost::posix_time::ptime& time) = 0; + + /** Adds system message to window + * @return id of added message (for replacement) + */ + virtual std::string addSystemMessage(const ChatMessage& message, Direction direction) = 0; + virtual void addPresenceMessage(const ChatMessage& message, Direction direction) = 0; + + virtual void addErrorMessage(const ChatMessage& message) = 0; + virtual void replaceMessage(const ChatMessage& message, const std::string& id, const boost::posix_time::ptime& time) = 0; + virtual void replaceSystemMessage(const ChatMessage& message, const std::string& id, const TimestampBehaviour timestampBehaviour) = 0; + virtual void replaceWithAction(const ChatMessage& message, const std::string& id, const boost::posix_time::ptime& time) = 0; + + // File transfer related stuff + virtual std::string addFileTransfer(const std::string& senderName, const std::string& avatarPath, bool senderIsSelf, const std::string& filename, const boost::uintmax_t sizeInBytes, const std::string& description) = 0; + virtual void setFileTransferProgress(std::string, const int percentageDone) = 0; + virtual void setFileTransferStatus(std::string, const FileTransferState state, const std::string& msg = "") = 0; + virtual void addMUCInvitation(const std::string& senderName, const JID& jid, const std::string& reason, const std::string& password, bool direct = true, bool isImpromptu = false, bool isContinuation = false) = 0; + + virtual std::string addWhiteboardRequest(bool senderIsSelf) = 0; + virtual void setWhiteboardSessionStatus(std::string id, const ChatWindow::WhiteboardSessionState state) = 0; + + // message receipts + virtual void setMessageReceiptState(const std::string& id, ChatWindow::ReceiptState state) = 0; + + virtual void setContactChatState(ChatState::ChatStateType state) = 0; + virtual void setName(const std::string& name) = 0; + virtual void show() = 0; + virtual bool isVisible() const = 0; + virtual void activate() = 0; + virtual void setAvailableSecurityLabels(const std::vector<SecurityLabelsCatalog::Item>& labels) = 0; + virtual void setSecurityLabelsEnabled(bool enabled) = 0; + virtual void setCorrectionEnabled(Tristate enabled) = 0; + virtual void setFileTransferEnabled(Tristate enabled) = 0; + virtual void setUnreadMessageCount(size_t count) = 0; + virtual void convertToMUC(MUCType mucType) = 0; +// virtual TreeWidget *getTreeWidget() = 0; + virtual void setSecurityLabelsError() = 0; + virtual SecurityLabelsCatalog::Item getSelectedSecurityLabel() = 0; + virtual void setOnline(bool online) = 0; + virtual void setRosterModel(Roster* model) = 0; + virtual void setTabComplete(TabComplete* completer) = 0; + virtual void replaceLastMessage(const ChatMessage& message, const TimestampBehaviour timestampBehaviour) = 0; + virtual void setAckState(const std::string& id, AckState state) = 0; + virtual void flash() = 0; + virtual void setSubject(const std::string& subject) = 0; + virtual void setAffiliations(MUCOccupant::Affiliation, const std::vector<JID>&) = 0; + virtual void setAvailableRoomActions(const std::vector<RoomAction> &actions) = 0; + virtual void setBlockingState(BlockingState state) = 0; + virtual void setCanInitiateImpromptuChats(bool supportsImpromptu) = 0; + virtual void showBookmarkWindow(const MUCBookmark& bookmark) = 0; + virtual void setBookmarkState(RoomBookmarkState bookmarkState) = 0; + + virtual void setChatSecurityMarking(const std::string& markingValue, const std::string& markingForegroundColorValue, const std::string& markingBackgroundColorValue) = 0; + virtual void removeChatSecurityMarking() = 0; + + /** + * A handle that uniquely identities an alert message. + */ + typedef int AlertID; + /** + * Set an alert on the window. + * @param alertText Description of alert (required). + * @return A handle to the alert message. + */ + virtual AlertID addAlert(const std::string& alertText) = 0; + /** + * Removes an alert. + * @param id An alert ID previously returned from setAlert + */ + virtual void removeAlert(const AlertID id) = 0; + + /** + * Actions that can be performed on the selected occupant. + */ + virtual void setAvailableOccupantActions(const std::vector<OccupantAction>& actions) = 0; + + /** + * A room configuration has been requested, show the form. + * If the form is cancelled, must emit onConfigurationFormCancelled(). + */ + virtual void showRoomConfigurationForm(Form::ref) = 0; + + boost::signals2::signal<void ()> onClosed; + boost::signals2::signal<void ()> onAllMessagesRead; + boost::signals2::signal<void (const std::string&, bool isCorrection)> onSendMessageRequest; + boost::signals2::signal<void (const std::string&)> onResendMessageRequest; + boost::signals2::signal<void ()> onSendCorrectionMessageRequest; + boost::signals2::signal<void ()> onUserTyping; + boost::signals2::signal<void ()> onUserCancelsTyping; + boost::signals2::signal<void ()> onAlertButtonClicked; + boost::signals2::signal<void (ContactRosterItem*)> onOccupantSelectionChanged; + boost::signals2::signal<void (ChatWindow::OccupantAction, ContactRosterItem*)> onOccupantActionSelected; + boost::signals2::signal<void (const std::string&)> onChangeSubjectRequest; + boost::signals2::signal<void ()> onBookmarkRequest; + boost::signals2::signal<void (Form::ref)> onConfigureRequest; + boost::signals2::signal<void ()> onDestroyRequest; + boost::signals2::signal<void (const std::vector<JID>&)> onInviteToChat; + boost::signals2::signal<void ()> onConfigurationFormCancelled; + boost::signals2::signal<void ()> onGetAffiliationsRequest; + boost::signals2::signal<void (MUCOccupant::Affiliation, const JID&)> onSetAffiliationRequest; + boost::signals2::signal<void (const std::vector<std::pair<MUCOccupant::Affiliation, JID> >& changes)> onChangeAffiliationsRequest; + boost::signals2::signal<void ()> onContinuationsBroken; + + // File transfer related + boost::signals2::signal<void (std::string /* id */)> onFileTransferCancel; + boost::signals2::signal<void (std::string /* id */, std::string /* description */)> onFileTransferStart; + boost::signals2::signal<void (std::string /* id */, std::string /* path */)> onFileTransferAccept; + boost::signals2::signal<void (std::string /* path */)> onSendFileRequest; + + //Whiteboard related + boost::signals2::signal<void ()> onWhiteboardSessionAccept; + boost::signals2::signal<void ()> onWhiteboardSessionCancel; + boost::signals2::signal<void ()> onWhiteboardWindowShow; + + // Blocking Command related + boost::signals2::signal<void ()> onBlockUserRequest; + boost::signals2::signal<void ()> onUnblockUserRequest; + }; +} diff --git a/Swift/Controllers/UIInterfaces/ChatWindowFactory.h b/Swift/Controllers/UIInterfaces/ChatWindowFactory.h index 62e6621..38706ab 100644 --- a/Swift/Controllers/UIInterfaces/ChatWindowFactory.h +++ b/Swift/Controllers/UIInterfaces/ChatWindowFactory.h @@ -1,7 +1,7 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #ifndef SWIFTEN_CHATWINDOWFACTORY_H @@ -10,17 +10,17 @@ #include "Swiften/JID/JID.h" namespace Swift { - class ChatWindow; - class UIEventStream; - class ChatWindowFactory { - public: - virtual ~ChatWindowFactory() {} - /** - * Transfers ownership of result. - */ - virtual ChatWindow* createChatWindow(const JID &contact, UIEventStream* eventStream) = 0; + class ChatWindow; + class UIEventStream; + class ChatWindowFactory { + public: + virtual ~ChatWindowFactory() {} + /** + * Transfers ownership of result. + */ + virtual ChatWindow* createChatWindow(const JID &contact, UIEventStream* eventStream) = 0; - }; + }; } #endif diff --git a/Swift/Controllers/UIInterfaces/ContactEditWindow.h b/Swift/Controllers/UIInterfaces/ContactEditWindow.h index 90f774c..1e311c5 100644 --- a/Swift/Controllers/UIInterfaces/ContactEditWindow.h +++ b/Swift/Controllers/UIInterfaces/ContactEditWindow.h @@ -1,35 +1,35 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <Swiften/Base/boost_bsignals.h> -#include <boost/shared_ptr.hpp> +#include <memory> #include <set> +#include <string> #include <vector> -#include <string> +#include <boost/signals2.hpp> namespace Swift { - class JID; - class VCardManager; + class JID; + class VCardManager; - class ContactEditWindow { - public: - virtual ~ContactEditWindow() {} + class ContactEditWindow { + public: + virtual ~ContactEditWindow() {} - virtual void setEnabled(bool b) = 0; + virtual void setEnabled(bool b) = 0; - virtual void setNameSuggestions(const std::vector<std::string>& suggestions) = 0; - virtual void setContact(const JID& jid, const std::string& name, const std::vector<std::string>& groups, const std::set<std::string>& allGroups) = 0; + virtual void setNameSuggestions(const std::vector<std::string>& suggestions) = 0; + virtual void setContact(const JID& jid, const std::string& name, const std::vector<std::string>& groups, const std::set<std::string>& allGroups) = 0; - virtual void show() = 0; - virtual void hide() = 0; + virtual void show() = 0; + virtual void hide() = 0; - boost::signal<void ()> onRemoveContactRequest; - boost::signal<void (const std::string& /* name */, const std::set<std::string>& /* groups */)> onChangeContactRequest; - }; + boost::signals2::signal<void ()> onRemoveContactRequest; + boost::signals2::signal<void (const std::string& /* name */, const std::set<std::string>& /* groups */)> onChangeContactRequest; + }; } diff --git a/Swift/Controllers/UIInterfaces/ContactEditWindowFactory.h b/Swift/Controllers/UIInterfaces/ContactEditWindowFactory.h index 9d47aef..f5a84d5 100644 --- a/Swift/Controllers/UIInterfaces/ContactEditWindowFactory.h +++ b/Swift/Controllers/UIInterfaces/ContactEditWindowFactory.h @@ -1,7 +1,7 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once @@ -9,10 +9,10 @@ #include <Swift/Controllers/UIInterfaces/ContactEditWindow.h> namespace Swift { - class ContactEditWindowFactory { - public: - virtual ~ContactEditWindowFactory() {} + class ContactEditWindowFactory { + public: + virtual ~ContactEditWindowFactory() {} - virtual ContactEditWindow* createContactEditWindow() = 0; - }; + virtual ContactEditWindow* createContactEditWindow() = 0; + }; } diff --git a/Swift/Controllers/UIInterfaces/EventWindow.h b/Swift/Controllers/UIInterfaces/EventWindow.h index b3af3d3..c05976b 100644 --- a/Swift/Controllers/UIInterfaces/EventWindow.h +++ b/Swift/Controllers/UIInterfaces/EventWindow.h @@ -1,29 +1,29 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <boost/shared_ptr.hpp> +#include <memory> -#include "Swift/Controllers/XMPPEvents/StanzaEvent.h" +#include <Swift/Controllers/XMPPEvents/StanzaEvent.h> namespace Swift { - class EventWindow { - public: - EventWindow(bool candelete = true) : canDelete_(candelete) {} + class EventWindow { + public: + EventWindow(bool candelete = true) : canDelete_(candelete) {} - bool canDelete() const { - return canDelete_; - } + bool canDelete() const { + return canDelete_; + } - virtual ~EventWindow() {} - virtual void addEvent(boost::shared_ptr<StanzaEvent> event, bool active) = 0; - virtual void removeEvent(boost::shared_ptr<StanzaEvent> event) = 0; + virtual ~EventWindow() {} + virtual void addEvent(std::shared_ptr<StanzaEvent> event, bool active) = 0; + virtual void removeEvent(std::shared_ptr<StanzaEvent> event) = 0; - private: - bool canDelete_; - }; + private: + bool canDelete_; + }; } diff --git a/Swift/Controllers/UIInterfaces/EventWindowFactory.h b/Swift/Controllers/UIInterfaces/EventWindowFactory.h index 0b9c28e..76638da 100644 --- a/Swift/Controllers/UIInterfaces/EventWindowFactory.h +++ b/Swift/Controllers/UIInterfaces/EventWindowFactory.h @@ -1,23 +1,23 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once namespace Swift { - class EventWindow; + class EventWindow; - class EventWindowFactory { - public: - virtual ~EventWindowFactory() {} - /** - * Transfers ownership of result. - */ - virtual EventWindow* createEventWindow() = 0; + class EventWindowFactory { + public: + virtual ~EventWindowFactory() {} + /** + * Transfers ownership of result. + */ + virtual EventWindow* createEventWindow() = 0; - }; + }; } diff --git a/Swift/Controllers/UIInterfaces/FdpFormSubmitWindow.cpp b/Swift/Controllers/UIInterfaces/FdpFormSubmitWindow.cpp new file mode 100644 index 0000000..47ef9cd --- /dev/null +++ b/Swift/Controllers/UIInterfaces/FdpFormSubmitWindow.cpp @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#include <Swift/Controllers/UIInterfaces/FdpFormSubmitWindow.h> + +#include <Swift/Controllers/Intl.h> + +namespace Swift { + +std::string FdpFormSubmitWindow::getNodeErrorText(NodeError nodeError) const { + switch(nodeError) { + case NodeError::DomainNotFound: return QT_TRANSLATE_NOOP("", "Error: No pubsub domain found"); + case NodeError::NoFdpNodesInDomain: return QT_TRANSLATE_NOOP("", "Error: Domain does not contain an FDP template node"); + case NodeError::NoError: return ""; + } + return ""; +} + +std::string FdpFormSubmitWindow::getTemplateErrorText(TemplateError templateError) const { + switch(templateError) { + case TemplateError::CannotLocateForm: return QT_TRANSLATE_NOOP("", "Error: Could not locate template form"); + case TemplateError::InvalidPayload: return QT_TRANSLATE_NOOP("", "Error: Invalid payload returned from node"); + case TemplateError::RequestFailed: return QT_TRANSLATE_NOOP("", "Error: Pubsub request failed"); + case TemplateError::NoError: return ""; + } + return ""; +} + +} diff --git a/Swift/Controllers/UIInterfaces/FdpFormSubmitWindow.h b/Swift/Controllers/UIInterfaces/FdpFormSubmitWindow.h new file mode 100644 index 0000000..572f910 --- /dev/null +++ b/Swift/Controllers/UIInterfaces/FdpFormSubmitWindow.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#pragma once + +#include <memory> +#include <string> + +#include <boost/signals2.hpp> + +namespace Swift { + + class Form; + + class FdpFormSubmitWindow { + + public: + enum class NodeError { + DomainNotFound, + NoFdpNodesInDomain, + NoError, + }; + enum class TemplateError { + CannotLocateForm, + InvalidPayload, + RequestFailed, + NoError, + }; + + virtual ~FdpFormSubmitWindow() {} + + virtual void show() = 0; + virtual void raise() = 0; + virtual void addNode(const std::string& node, const std::string& nodeName) = 0; + virtual void clearNodeData() = 0; + virtual void setFormData(const std::shared_ptr<Form>& form) = 0; + virtual void showNodePlaceholder(NodeError nodeError) = 0; + virtual void showFormPlaceholder(TemplateError templateError) = 0; + virtual void handleSubmitServerResponse(bool submissionSuccess) = 0; + + boost::signals2::signal<void ()> onCloseEvent; + boost::signals2::signal<void (const std::string&)> onRequestPubSubNodeData; + boost::signals2::signal<void (const std::string&)> onRequestTemplateForm; + boost::signals2::signal<void (const std::shared_ptr<Form>&)> onSubmitForm; + + protected: + + std::string getNodeErrorText(NodeError nodeError) const; + std::string getTemplateErrorText(TemplateError templateError) const; + }; +} diff --git a/Swift/Controllers/UIInterfaces/FdpFormSubmitWindowFactory.h b/Swift/Controllers/UIInterfaces/FdpFormSubmitWindowFactory.h new file mode 100644 index 0000000..ef11eaf --- /dev/null +++ b/Swift/Controllers/UIInterfaces/FdpFormSubmitWindowFactory.h @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#pragma once + +#include <memory> + +namespace Swift { + class FdpFormSubmitWindow; + + class FdpFormSubmitWindowFactory { + public: + virtual ~FdpFormSubmitWindowFactory() {} + virtual std::unique_ptr<FdpFormSubmitWindow> createFdpFormSubmitWindow() = 0; + }; +} diff --git a/Swift/Controllers/UIInterfaces/FileTransferListWidget.h b/Swift/Controllers/UIInterfaces/FileTransferListWidget.h index 01dcfd3..5a9eaeb 100644 --- a/Swift/Controllers/UIInterfaces/FileTransferListWidget.h +++ b/Swift/Controllers/UIInterfaces/FileTransferListWidget.h @@ -12,12 +12,12 @@ class FileTransferOverview; class FileTransferListWidget { public: - virtual ~FileTransferListWidget() {} + virtual ~FileTransferListWidget() {} - virtual void show() = 0; - virtual void activate() = 0; + virtual void show() = 0; + virtual void activate() = 0; - virtual void setFileTransferOverview(FileTransferOverview*) = 0; + virtual void setFileTransferOverview(FileTransferOverview*) = 0; }; } diff --git a/Swift/Controllers/UIInterfaces/FileTransferListWidgetFactory.h b/Swift/Controllers/UIInterfaces/FileTransferListWidgetFactory.h index 0b08fb3..da9fd37 100644 --- a/Swift/Controllers/UIInterfaces/FileTransferListWidgetFactory.h +++ b/Swift/Controllers/UIInterfaces/FileTransferListWidgetFactory.h @@ -4,17 +4,23 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ +/* + * Copyright (c) 2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + #pragma once -#include "Swift/Controllers/UIInterfaces/FileTransferListWidget.h" +#include <Swift/Controllers/UIInterfaces/FileTransferListWidget.h> namespace Swift { class FileTransferListWidgetFactory { public: - virtual ~FileTransferListWidgetFactory() {} + virtual ~FileTransferListWidgetFactory() {} - virtual FileTransferListWidget* createFileTransferListWidget() = 0; + virtual FileTransferListWidget* createFileTransferListWidget() = 0; }; } diff --git a/Swift/Controllers/UIInterfaces/HighlightEditorWidget.h b/Swift/Controllers/UIInterfaces/HighlightEditorWidget.h index 4745035..ff888e6 100644 --- a/Swift/Controllers/UIInterfaces/HighlightEditorWidget.h +++ b/Swift/Controllers/UIInterfaces/HighlightEditorWidget.h @@ -8,15 +8,15 @@ namespace Swift { - class HighlightManager; + class HighlightManager; - class HighlightEditorWidget { - public: - virtual ~HighlightEditorWidget() {} + class HighlightEditorWidget { + public: + virtual ~HighlightEditorWidget() {} - virtual void show() = 0; + virtual void show() = 0; - virtual void setHighlightManager(HighlightManager* highlightManager) = 0; - }; + virtual void setHighlightManager(HighlightManager* highlightManager) = 0; + }; } diff --git a/Swift/Controllers/UIInterfaces/HighlightEditorWidgetFactory.h b/Swift/Controllers/UIInterfaces/HighlightEditorWidgetFactory.h index ade575b..363e666 100644 --- a/Swift/Controllers/UIInterfaces/HighlightEditorWidgetFactory.h +++ b/Swift/Controllers/UIInterfaces/HighlightEditorWidgetFactory.h @@ -8,13 +8,13 @@ namespace Swift { - class HighlightEditorWidget; + class HighlightEditorWidget; - class HighlightEditorWidgetFactory { - public: - virtual ~HighlightEditorWidgetFactory() {} + class HighlightEditorWidgetFactory { + public: + virtual ~HighlightEditorWidgetFactory() {} - virtual HighlightEditorWidget* createHighlightEditorWidget() = 0; - }; + virtual HighlightEditorWidget* createHighlightEditorWidget() = 0; + }; } diff --git a/Swift/Controllers/UIInterfaces/HighlightEditorWindow.cpp b/Swift/Controllers/UIInterfaces/HighlightEditorWindow.cpp new file mode 100644 index 0000000..ff9b307 --- /dev/null +++ b/Swift/Controllers/UIInterfaces/HighlightEditorWindow.cpp @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2014 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#include <Swift/Controllers/UIInterfaces/HighlightEditorWindow.h> + +namespace Swift { + +HighlightEditorWindow::HighlightEditorWindow() +{ +} + +HighlightEditorWindow::~HighlightEditorWindow() +{ +} + +} diff --git a/Swift/Controllers/UIInterfaces/HighlightEditorWindow.h b/Swift/Controllers/UIInterfaces/HighlightEditorWindow.h new file mode 100644 index 0000000..cae54dc --- /dev/null +++ b/Swift/Controllers/UIInterfaces/HighlightEditorWindow.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2014-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#pragma once + +#include <vector> + +#include <boost/signals2.hpp> + +#include <Swift/Controllers/Contact.h> + +namespace Swift { + +class HighlightManager; + +class HighlightEditorWindow { +public: + HighlightEditorWindow(); + virtual ~HighlightEditorWindow(); + virtual void show() = 0; + virtual void setHighlightManager(HighlightManager *highlightManager) = 0; + virtual void setContactSuggestions(const std::vector<Contact::ref>& suggestions) = 0; + +public: + boost::signals2::signal<void (const std::string&)> onContactSuggestionsRequested; +}; + +} diff --git a/Swift/Controllers/UIInterfaces/HighlightEditorWindowFactory.h b/Swift/Controllers/UIInterfaces/HighlightEditorWindowFactory.h new file mode 100644 index 0000000..ea05425 --- /dev/null +++ b/Swift/Controllers/UIInterfaces/HighlightEditorWindowFactory.h @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2012 Mateusz Piękos + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +/* + * Copyright (c) 2014 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#pragma once + +namespace Swift { + class HighlightEditorWindow; + + class HighlightEditorWindowFactory { + public : + virtual ~HighlightEditorWindowFactory() {} + + virtual HighlightEditorWindow* createHighlightEditorWindow() = 0; + }; +} diff --git a/Swift/Controllers/UIInterfaces/HistoryWindow.h b/Swift/Controllers/UIInterfaces/HistoryWindow.h index 6d50f4b..413d9d6 100644 --- a/Swift/Controllers/UIInterfaces/HistoryWindow.h +++ b/Swift/Controllers/UIInterfaces/HistoryWindow.h @@ -4,31 +4,37 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ +/* + * Copyright (c) 2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + #pragma once #include <Swift/Controllers/Roster/Roster.h> namespace Swift { - class HistoryWindow { - public: - virtual ~HistoryWindow() {} + class HistoryWindow { + public: + virtual ~HistoryWindow() {} - virtual void activate() = 0; - virtual void setRosterModel(Roster*) = 0; - virtual void addMessage(const std::string &message, const std::string &senderName, bool senderIsSelf, const std::string& avatarPath, const boost::posix_time::ptime& time, bool addAtTheTop) = 0; - virtual void resetConversationView() = 0; - 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 void activate() = 0; + virtual void setRosterModel(Roster*) = 0; + virtual void addMessage(const std::string &message, const std::string &senderName, bool senderIsSelf, const std::string& avatarPath, const boost::posix_time::ptime& time, bool addAtTheTop) = 0; + virtual void resetConversationView() = 0; + 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; + 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; - boost::signal<void (const boost::gregorian::date&)> onScrollReachedBottom; - boost::signal<void ()> onPreviousButtonClicked; - boost::signal<void ()> onNextButtonClicked; - boost::signal<void (const boost::gregorian::date&)> onCalendarClicked; - }; + boost::signals2::signal<void (RosterItem*)> onSelectedContactChanged; + boost::signals2::signal<void (const std::string&)> onReturnPressed; + boost::signals2::signal<void (const boost::gregorian::date&)> onScrollReachedTop; + boost::signals2::signal<void (const boost::gregorian::date&)> onScrollReachedBottom; + boost::signals2::signal<void ()> onPreviousButtonClicked; + boost::signals2::signal<void ()> onNextButtonClicked; + boost::signals2::signal<void (const boost::gregorian::date&)> onCalendarClicked; + }; } diff --git a/Swift/Controllers/UIInterfaces/HistoryWindowFactory.h b/Swift/Controllers/UIInterfaces/HistoryWindowFactory.h index 807fec5..ab4cf0d 100644 --- a/Swift/Controllers/UIInterfaces/HistoryWindowFactory.h +++ b/Swift/Controllers/UIInterfaces/HistoryWindowFactory.h @@ -9,10 +9,10 @@ #include <Swift/Controllers/UIInterfaces/HistoryWindow.h> namespace Swift { - class UIEventStream; - class HistoryWindowFactory { - public: - virtual ~HistoryWindowFactory() {} - virtual HistoryWindow* createHistoryWindow(UIEventStream* eventStream) = 0; - }; + class UIEventStream; + class HistoryWindowFactory { + public: + virtual ~HistoryWindowFactory() {} + virtual HistoryWindow* createHistoryWindow(UIEventStream* eventStream) = 0; + }; } diff --git a/Swift/Controllers/UIInterfaces/JoinMUCWindow.h b/Swift/Controllers/UIInterfaces/JoinMUCWindow.h index 56a9587..7a71195 100644 --- a/Swift/Controllers/UIInterfaces/JoinMUCWindow.h +++ b/Swift/Controllers/UIInterfaces/JoinMUCWindow.h @@ -1,26 +1,27 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once +#include <string> #include <vector> -#include <string> -#include <Swiften/Base/boost_bsignals.h> +#include <boost/signals2.hpp> + #include <Swiften/JID/JID.h> namespace Swift { - class JoinMUCWindow { - public: - virtual ~JoinMUCWindow() {} + class JoinMUCWindow { + public: + virtual ~JoinMUCWindow() {} - virtual void setNick(const std::string& nick) = 0; - virtual void setMUC(const std::string& nick) = 0; - virtual void show() = 0; + virtual void setNick(const std::string& nick) = 0; + virtual void setMUC(const std::string& nick) = 0; + virtual void show() = 0; - boost::signal<void ()> onSearchMUC; - }; + boost::signals2::signal<void ()> onSearchMUC; + }; } diff --git a/Swift/Controllers/UIInterfaces/JoinMUCWindowFactory.h b/Swift/Controllers/UIInterfaces/JoinMUCWindowFactory.h index 494b418..5e8b1d0 100644 --- a/Swift/Controllers/UIInterfaces/JoinMUCWindowFactory.h +++ b/Swift/Controllers/UIInterfaces/JoinMUCWindowFactory.h @@ -1,7 +1,7 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once @@ -9,11 +9,11 @@ #include <Swift/Controllers/UIInterfaces/JoinMUCWindow.h> namespace Swift { - class UIEventStream; - class JoinMUCWindowFactory { - public: - virtual ~JoinMUCWindowFactory() {} + class UIEventStream; + class JoinMUCWindowFactory { + public: + virtual ~JoinMUCWindowFactory() {} - virtual JoinMUCWindow* createJoinMUCWindow(UIEventStream* uiEventStream) = 0; - }; + virtual JoinMUCWindow* createJoinMUCWindow(UIEventStream* uiEventStream) = 0; + }; } diff --git a/Swift/Controllers/UIInterfaces/LoginWindow.h b/Swift/Controllers/UIInterfaces/LoginWindow.h index e27c385..d11aadb 100644 --- a/Swift/Controllers/UIInterfaces/LoginWindow.h +++ b/Swift/Controllers/UIInterfaces/LoginWindow.h @@ -1,41 +1,42 @@ /* - * Copyright (c) 2010-2012 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <Swiften/Base/boost_bsignals.h> -#include <boost/shared_ptr.hpp> - +#include <memory> #include <string> + +#include <boost/signals2.hpp> + +#include <Swiften/Client/ClientOptions.h> #include <Swiften/TLS/Certificate.h> #include <Swiften/TLS/CertificateWithKey.h> -#include <Swiften/Client/ClientOptions.h> namespace Swift { - class MainWindow; - class LoginWindow { - public: - virtual ~LoginWindow() {} - virtual void selectUser(const std::string&) = 0; - virtual void morphInto(MainWindow *mainWindow) = 0; - virtual void loggedOut() = 0; - virtual void setShowNotificationToggle(bool) = 0; - virtual void setMessage(const std::string&) = 0; - virtual void setIsLoggingIn(bool loggingIn) = 0; - virtual void addAvailableAccount(const std::string& defaultJID, const std::string& defaultPassword, const std::string& defaultCertificate, const ClientOptions& options) = 0; - virtual void removeAvailableAccount(const std::string& jid) = 0; - /** The certificate is what is used for login, the certificatePath is used for remembering paths to populate the loginwindow with*/ - boost::signal<void (const std::string&, const std::string&, const std::string& /*CertificatePath*/, CertificateWithKey::ref /* clientCertificate */, const ClientOptions& /*options*/, bool /* remember password*/, bool /* login automatically */)> onLoginRequest; - virtual void setLoginAutomatically(bool loginAutomatically) = 0; - virtual void quit() = 0; - /** Blocking request whether a cert should be permanently trusted.*/ - virtual bool askUserToTrustCertificatePermanently(const std::string& message, const std::vector<Certificate::ref>& certificateChain) = 0; + class MainWindow; + class LoginWindow { + public: + virtual ~LoginWindow() {} + virtual void selectUser(const std::string&) = 0; + virtual void morphInto(MainWindow *mainWindow) = 0; + virtual void loggedOut() = 0; + virtual void setShowNotificationToggle(bool) = 0; + virtual void setMessage(const std::string&) = 0; + virtual void setIsLoggingIn(bool loggingIn) = 0; + virtual void addAvailableAccount(const std::string& defaultJID, const std::string& defaultPassword, const std::string& defaultCertificate, const ClientOptions& options) = 0; + virtual void removeAvailableAccount(const std::string& jid) = 0; + /** The certificate is what is used for login, the certificatePath is used for remembering paths to populate the loginwindow with*/ + boost::signals2::signal<void (const std::string&, const std::string&, const std::string& /*CertificatePath*/, CertificateWithKey::ref /* clientCertificate */, const ClientOptions& /*options*/, bool /* remember password*/, bool /* login automatically */)> onLoginRequest; + virtual void setLoginAutomatically(bool loginAutomatically) = 0; + virtual void quit() = 0; + /** Blocking request whether a cert should be permanently trusted.*/ + virtual bool askUserToTrustCertificatePermanently(const std::string& message, const std::vector<Certificate::ref>& certificateChain) = 0; - boost::signal<void ()> onCancelLoginRequest; - boost::signal<void ()> onQuitRequest; - boost::signal<void (const std::string&)> onPurgeSavedLoginRequest; - }; + boost::signals2::signal<void ()> onCancelLoginRequest; + boost::signals2::signal<void ()> onQuitRequest; + boost::signals2::signal<void (const std::string&)> onPurgeSavedLoginRequest; + }; } diff --git a/Swift/Controllers/UIInterfaces/LoginWindowFactory.h b/Swift/Controllers/UIInterfaces/LoginWindowFactory.h index 7b8b7ec..485f975 100644 --- a/Swift/Controllers/UIInterfaces/LoginWindowFactory.h +++ b/Swift/Controllers/UIInterfaces/LoginWindowFactory.h @@ -1,7 +1,7 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #ifndef SWIFTEN_LoginWindowFactory_H @@ -10,20 +10,20 @@ namespace Swift { - class LoginWindow; - - class UIEventStream; - - class LoginWindowFactory { - public: - virtual ~LoginWindowFactory() {} - - /** - * Transfers ownership of result. - */ - virtual LoginWindow* createLoginWindow(UIEventStream* uiEventStream) = 0; - - }; + class LoginWindow; + + class UIEventStream; + + class LoginWindowFactory { + public: + virtual ~LoginWindowFactory() {} + + /** + * Transfers ownership of result. + */ + virtual LoginWindow* createLoginWindow(UIEventStream* uiEventStream) = 0; + + }; } #endif diff --git a/Swift/Controllers/UIInterfaces/MUCSearchWindow.h b/Swift/Controllers/UIInterfaces/MUCSearchWindow.h index 43a61a1..045c2df 100644 --- a/Swift/Controllers/UIInterfaces/MUCSearchWindow.h +++ b/Swift/Controllers/UIInterfaces/MUCSearchWindow.h @@ -1,34 +1,35 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include "Swiften/Base/boost_bsignals.h" - +#include <string> #include <vector> #include <boost/optional.hpp> -#include <string> -#include "Swiften/JID/JID.h" +#include <boost/signals2.hpp> + +#include <Swiften/JID/JID.h> + #include <Swift/Controllers/Chat/MUCSearchController.h> namespace Swift { - class MUCSearchWindow { - public: - virtual ~MUCSearchWindow() {} + class MUCSearchWindow { + public: + virtual ~MUCSearchWindow() {} - virtual void clearList() = 0; - virtual void addService(const MUCService& service) = 0; - virtual void addSavedServices(const std::list<JID>& services) = 0; - virtual void setSearchInProgress(bool searching) = 0; + virtual void clearList() = 0; + virtual void addService(const MUCService& service) = 0; + virtual void addSavedServices(const std::list<JID>& services) = 0; + virtual void setSearchInProgress(bool searching) = 0; - virtual void show() = 0; + virtual void show() = 0; - boost::signal<void (const JID&)> onSearchService; - boost::signal<void (const boost::optional<JID>&)> onFinished; - }; + boost::signals2::signal<void (const JID&)> onSearchService; + boost::signals2::signal<void (const boost::optional<JID>&)> onFinished; + }; } diff --git a/Swift/Controllers/UIInterfaces/MUCSearchWindowFactory.h b/Swift/Controllers/UIInterfaces/MUCSearchWindowFactory.h index 46488eb..6e26ac3 100644 --- a/Swift/Controllers/UIInterfaces/MUCSearchWindowFactory.h +++ b/Swift/Controllers/UIInterfaces/MUCSearchWindowFactory.h @@ -1,19 +1,19 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include "Swift/Controllers/UIInterfaces/MUCSearchWindow.h" +#include <Swift/Controllers/UIInterfaces/MUCSearchWindow.h> namespace Swift { - class UIEventStream; - class MUCSearchWindowFactory { - public: - virtual ~MUCSearchWindowFactory() {} + class UIEventStream; + class MUCSearchWindowFactory { + public: + virtual ~MUCSearchWindowFactory() {} - virtual MUCSearchWindow* createMUCSearchWindow() = 0; - }; + virtual MUCSearchWindow* createMUCSearchWindow() = 0; + }; } diff --git a/Swift/Controllers/UIInterfaces/MainWindow.h b/Swift/Controllers/UIInterfaces/MainWindow.h index 3b10041..e4b4657 100644 --- a/Swift/Controllers/UIInterfaces/MainWindow.h +++ b/Swift/Controllers/UIInterfaces/MainWindow.h @@ -1,49 +1,54 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once +#include <memory> #include <string> -#include "Swiften/JID/JID.h" -#include "Swiften/Elements/StatusShow.h" -#include "Swiften/Elements/DiscoItems.h" -#include "Swiften/TLS/Certificate.h" -#include "Swiften/Base/boost_bsignals.h" -#include <boost/shared_ptr.hpp> + +#include <boost/signals2.hpp> + +#include <Swiften/Elements/DiscoItems.h> +#include <Swiften/Elements/StatusShow.h> +#include <Swiften/TLS/Certificate.h> + +#include <Swift/Controllers/Roster/ContactRosterItem.h> namespace Swift { - class Roster; - - class MainWindow { - public: - MainWindow(bool candelete = true) : canDelete_(candelete) {} - virtual ~MainWindow() {} - - bool canDelete() const { - return canDelete_; - } - - virtual void setMyNick(const std::string& name) = 0; - virtual void setMyJID(const JID& jid) = 0; - virtual void setMyAvatarPath(const std::string& path) = 0; - virtual void setMyStatusText(const std::string& status) = 0; - virtual void setMyStatusType(StatusShow::Type type) = 0; - /** Must be able to cope with NULL to clear the roster */ - virtual void setRosterModel(Roster* roster) = 0; - virtual void setConnecting() = 0; - virtual void setBlockingCommandAvailable(bool isAvailable) = 0; - virtual void setAvailableAdHocCommands(const std::vector<DiscoItems::Item>& commands) = 0; - virtual void setStreamEncryptionStatus(bool tlsInPlaceAndValid) = 0; - virtual void openCertificateDialog(const std::vector<Certificate::ref>& chain) = 0; - - boost::signal<void (StatusShow::Type, const std::string&)> onChangeStatusRequest; - boost::signal<void ()> onSignOutRequest; - boost::signal<void ()> onShowCertificateRequest; - - private: - bool canDelete_; - }; + class JID; + class Roster; + + class MainWindow { + public: + MainWindow(bool candelete = true) : canDelete_(candelete) {} + virtual ~MainWindow() {} + + bool canDelete() const { + return canDelete_; + } + + virtual void setMyNick(const std::string& name) = 0; + virtual void setMyJID(const JID& jid) = 0; + virtual void setMyAvatarPath(const std::string& path) = 0; + virtual void setMyStatusText(const std::string& status) = 0; + virtual void setMyStatusType(StatusShow::Type type) = 0; + virtual void setMyContactRosterItem(std::shared_ptr<ContactRosterItem> contact) = 0; + /** Must be able to cope with NULL to clear the roster */ + virtual void setRosterModel(Roster* roster) = 0; + virtual void setConnecting() = 0; + virtual void setBlockingCommandAvailable(bool isAvailable) = 0; + virtual void setAvailableAdHocCommands(const std::vector<DiscoItems::Item>& commands) = 0; + virtual void setStreamEncryptionStatus(bool tlsInPlaceAndValid) = 0; + virtual void openCertificateDialog(const std::vector<Certificate::ref>& chain) = 0; + + boost::signals2::signal<void (StatusShow::Type, const std::string&)> onChangeStatusRequest; + boost::signals2::signal<void ()> onSignOutRequest; + boost::signals2::signal<void ()> onShowCertificateRequest; + + private: + bool canDelete_; + }; } diff --git a/Swift/Controllers/UIInterfaces/MainWindowFactory.h b/Swift/Controllers/UIInterfaces/MainWindowFactory.h index 6bd34b4..af924e2 100644 --- a/Swift/Controllers/UIInterfaces/MainWindowFactory.h +++ b/Swift/Controllers/UIInterfaces/MainWindowFactory.h @@ -1,27 +1,23 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ -#ifndef SWIFTEN_MainWindowFactory_H -#define SWIFTEN_MainWindowFactory_H - -#include "Swiften/JID/JID.h" -#include "Swift/Controllers/UIEvents/UIEventStream.h" +#pragma once namespace Swift { - class MainWindow; + class Chattables; + class MainWindow; + class UIEventStream; - class MainWindowFactory { - public: - virtual ~MainWindowFactory() {} - /** - * Transfers ownership of result. - */ - virtual MainWindow* createMainWindow(UIEventStream* eventStream) = 0; + class MainWindowFactory { + public: + virtual ~MainWindowFactory() {} + /** + * Transfers ownership of result. + */ + virtual MainWindow* createMainWindow(Chattables&, UIEventStream*) = 0; - }; + }; } -#endif - diff --git a/Swift/Controllers/UIInterfaces/ProfileWindow.h b/Swift/Controllers/UIInterfaces/ProfileWindow.h index 5c158e1..c2bcdae 100644 --- a/Swift/Controllers/UIInterfaces/ProfileWindow.h +++ b/Swift/Controllers/UIInterfaces/ProfileWindow.h @@ -1,35 +1,36 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <Swiften/Base/boost_bsignals.h> -#include <boost/shared_ptr.hpp> +#include <memory> + +#include <boost/signals2.hpp> #include <Swiften/Elements/VCard.h> namespace Swift { - class JID; + class JID; - class ProfileWindow { - public: - virtual ~ProfileWindow() {} + class ProfileWindow { + public: + virtual ~ProfileWindow() {} - virtual void setJID(const JID& jid) = 0; - virtual void setVCard(VCard::ref vcard) = 0; + virtual void setJID(const JID& jid) = 0; + virtual void setVCard(VCard::ref vcard) = 0; - virtual void setEnabled(bool b) = 0; - virtual void setProcessing(bool b) = 0; - virtual void setError(const std::string&) = 0; - virtual void setEditable(bool b) = 0; + virtual void setEnabled(bool b) = 0; + virtual void setProcessing(bool b) = 0; + virtual void setError(const std::string&) = 0; + virtual void setEditable(bool b) = 0; - virtual void show() = 0; - virtual void hide() = 0; + virtual void show() = 0; + virtual void hide() = 0; - boost::signal<void (VCard::ref)> onVCardChangeRequest; - boost::signal<void (const JID&)> onWindowAboutToBeClosed; - }; + boost::signals2::signal<void (VCard::ref)> onVCardChangeRequest; + boost::signals2::signal<void (const JID&)> onWindowAboutToBeClosed; + }; } diff --git a/Swift/Controllers/UIInterfaces/ProfileWindowFactory.h b/Swift/Controllers/UIInterfaces/ProfileWindowFactory.h index 45a340a..6c2c6e5 100644 --- a/Swift/Controllers/UIInterfaces/ProfileWindowFactory.h +++ b/Swift/Controllers/UIInterfaces/ProfileWindowFactory.h @@ -1,7 +1,7 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once @@ -9,10 +9,10 @@ #include <Swift/Controllers/UIInterfaces/ProfileWindow.h> namespace Swift { - class ProfileWindowFactory { - public: - virtual ~ProfileWindowFactory() {} + class ProfileWindowFactory { + public: + virtual ~ProfileWindowFactory() {} - virtual ProfileWindow* createProfileWindow() = 0; - }; + virtual ProfileWindow* createProfileWindow() = 0; + }; } diff --git a/Swift/Controllers/UIInterfaces/UIFactory.h b/Swift/Controllers/UIInterfaces/UIFactory.h index 990dc98..c49364a 100644 --- a/Swift/Controllers/UIInterfaces/UIFactory.h +++ b/Swift/Controllers/UIInterfaces/UIFactory.h @@ -1,49 +1,51 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once +#include <Swift/Controllers/UIInterfaces/AdHocCommandWindowFactory.h> +#include <Swift/Controllers/UIInterfaces/BlockListEditorWidgetFactory.h> #include <Swift/Controllers/UIInterfaces/ChatListWindowFactory.h> #include <Swift/Controllers/UIInterfaces/ChatWindowFactory.h> -#include <Swift/Controllers/UIInterfaces/HistoryWindowFactory.h> +#include <Swift/Controllers/UIInterfaces/ContactEditWindowFactory.h> #include <Swift/Controllers/UIInterfaces/EventWindowFactory.h> +#include <Swift/Controllers/UIInterfaces/FdpFormSubmitWindowFactory.h> +#include <Swift/Controllers/UIInterfaces/FileTransferListWidgetFactory.h> +#include <Swift/Controllers/UIInterfaces/HighlightEditorWindowFactory.h> +#include <Swift/Controllers/UIInterfaces/HistoryWindowFactory.h> +#include <Swift/Controllers/UIInterfaces/JoinMUCWindowFactory.h> #include <Swift/Controllers/UIInterfaces/LoginWindowFactory.h> -#include <Swift/Controllers/UIInterfaces/MainWindowFactory.h> #include <Swift/Controllers/UIInterfaces/MUCSearchWindowFactory.h> -#include <Swift/Controllers/UIInterfaces/JoinMUCWindowFactory.h> -#include <Swift/Controllers/UIInterfaces/UserSearchWindowFactory.h> -#include <Swift/Controllers/UIInterfaces/XMLConsoleWidgetFactory.h> +#include <Swift/Controllers/UIInterfaces/MainWindowFactory.h> #include <Swift/Controllers/UIInterfaces/ProfileWindowFactory.h> -#include <Swift/Controllers/UIInterfaces/ContactEditWindowFactory.h> -#include <Swift/Controllers/UIInterfaces/AdHocCommandWindowFactory.h> -#include <Swift/Controllers/UIInterfaces/FileTransferListWidgetFactory.h> +#include <Swift/Controllers/UIInterfaces/UserSearchWindowFactory.h> #include <Swift/Controllers/UIInterfaces/WhiteboardWindowFactory.h> -#include <Swift/Controllers/UIInterfaces/HighlightEditorWidgetFactory.h> -#include <Swift/Controllers/UIInterfaces/BlockListEditorWidgetFactory.h> +#include <Swift/Controllers/UIInterfaces/XMLConsoleWidgetFactory.h> namespace Swift { - class UIFactory : - public ChatListWindowFactory, - public ChatWindowFactory, - public HistoryWindowFactory, - public EventWindowFactory, - public LoginWindowFactory, - public MainWindowFactory, - public MUCSearchWindowFactory, - public XMLConsoleWidgetFactory, - public UserSearchWindowFactory, - public JoinMUCWindowFactory, - public ProfileWindowFactory, - public ContactEditWindowFactory, - public AdHocCommandWindowFactory, - public FileTransferListWidgetFactory, - public WhiteboardWindowFactory, - public HighlightEditorWidgetFactory, - public BlockListEditorWidgetFactory { - public: - virtual ~UIFactory() {} - }; + class UIFactory : + public ChatListWindowFactory, + public ChatWindowFactory, + public HistoryWindowFactory, + public EventWindowFactory, + public LoginWindowFactory, + public MainWindowFactory, + public MUCSearchWindowFactory, + public XMLConsoleWidgetFactory, + public UserSearchWindowFactory, + public JoinMUCWindowFactory, + public ProfileWindowFactory, + public ContactEditWindowFactory, + public AdHocCommandWindowFactory, + public FileTransferListWidgetFactory, + public WhiteboardWindowFactory, + public HighlightEditorWindowFactory, + public BlockListEditorWidgetFactory, + public FdpFormSubmitWindowFactory { + public: + virtual ~UIFactory() {} + }; } diff --git a/Swift/Controllers/UIInterfaces/UserSearchWindow.h b/Swift/Controllers/UIInterfaces/UserSearchWindow.h index 9dd1811..c13e376 100644 --- a/Swift/Controllers/UIInterfaces/UserSearchWindow.h +++ b/Swift/Controllers/UIInterfaces/UserSearchWindow.h @@ -1,51 +1,58 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2017 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <Swiften/Base/boost_bsignals.h> - -#include <vector> #include <string> +#include <vector> + +#include <boost/signals2.hpp> #include <Swiften/JID/JID.h> + #include <Swift/Controllers/Chat/UserSearchController.h> #include <Swift/Controllers/Contact.h> namespace Swift { - class UserSearchWindow { - public: - enum Type {AddContact, ChatToContact, InviteToChat}; - virtual ~UserSearchWindow() {} - - virtual void clear() = 0; - virtual void setResults(const std::vector<UserSearchResult>& results) = 0; - virtual void setResultsForm(const Form::ref results) = 0; - virtual void addSavedServices(const std::vector<JID>& services) = 0; - virtual void setSelectedService(const JID& service) = 0; - virtual void setServerSupportsSearch(bool support) = 0; - virtual void setSearchError(bool support) = 0; - virtual void setSearchFields(boost::shared_ptr<SearchPayload> fields) = 0; - virtual void setNameSuggestions(const std::vector<std::string>& suggestions) = 0; - virtual void prepopulateJIDAndName(const JID& jid, const std::string& name) = 0; - virtual void setContactSuggestions(const std::vector<Contact>& suggestions) = 0; - virtual void setJIDs(const std::vector<JID>&) = 0; - virtual void setRoomJID(const JID& roomJID) = 0; - virtual std::string getReason() const = 0; - virtual std::vector<JID> getJIDs() const = 0; - virtual void setCanStartImpromptuChats(bool supportsImpromptu) = 0; - virtual void updateContacts(const std::vector<Contact>& contacts) = 0; - - virtual void show() = 0; - - boost::signal<void (const JID&)> onFormRequested; - boost::signal<void (boost::shared_ptr<SearchPayload>, const JID&)> onSearchRequested; - boost::signal<void (const JID&)> onNameSuggestionRequested; - boost::signal<void (const std::string&)> onContactSuggestionsRequested; - boost::signal<void (const std::vector<JID>&)> onJIDUpdateRequested; - }; + class UserSearchWindow { + public: + enum class Type {AddContact, ChatToContact, InviteToChat}; + virtual ~UserSearchWindow() {} + + virtual void clear() = 0; + virtual void setResults(const std::vector<UserSearchResult>& results) = 0; + virtual void setResultsForm(const Form::ref results) = 0; + virtual void addSavedServices(const std::vector<JID>& services) = 0; + virtual void setSelectedService(const JID& service) = 0; + virtual void setServerSupportsSearch(bool support) = 0; + virtual void setSearchError(bool support) = 0; + virtual void setSearchFields(std::shared_ptr<SearchPayload> fields) = 0; + virtual void setNameSuggestions(const std::vector<std::string>& suggestions) = 0; + virtual void prepopulateJIDAndName(const JID& jid, const std::string& name) = 0; + virtual void setContactSuggestions(const std::vector<Contact::ref>& suggestions) = 0; + virtual void setJIDs(const std::vector<JID>&) = 0; + virtual void setOriginator(const JID& originator) = 0; + virtual void setRoomJID(const JID& roomJID) = 0; + virtual std::string getReason() const = 0; + virtual std::vector<JID> getJIDs() const = 0; + virtual void setCanStartImpromptuChats(bool supportsImpromptu) = 0; + virtual void updateContacts(const std::vector<Contact::ref>& contacts) = 0; + virtual void addContacts(const std::vector<Contact::ref>& contacts) = 0; + virtual void setCanSupplyDescription(bool allowed) = 0; + virtual void setWarning(const boost::optional<std::string>& message) = 0; + + virtual void show() = 0; + + boost::signals2::signal<void (const JID&)> onFormRequested; + boost::signals2::signal<void (std::shared_ptr<SearchPayload>, const JID&)> onSearchRequested; + boost::signals2::signal<void (const JID&)> onNameSuggestionRequested; + boost::signals2::signal<void (const std::string&)> onContactSuggestionsRequested; + boost::signals2::signal<void (const std::vector<JID>&)> onJIDUpdateRequested; + boost::signals2::signal<void (const std::vector<JID>&)> onJIDAddRequested; + boost::signals2::signal<void (const JID&)> onJIDEditFieldChanged; + }; } diff --git a/Swift/Controllers/UIInterfaces/UserSearchWindowFactory.h b/Swift/Controllers/UIInterfaces/UserSearchWindowFactory.h index 331d6dd..d5d6135 100644 --- a/Swift/Controllers/UIInterfaces/UserSearchWindowFactory.h +++ b/Swift/Controllers/UIInterfaces/UserSearchWindowFactory.h @@ -1,21 +1,21 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once #include <set> -#include "Swift/Controllers/UIInterfaces/UserSearchWindow.h" +#include <Swift/Controllers/UIInterfaces/UserSearchWindow.h> namespace Swift { - class UIEventStream; - class UserSearchWindowFactory { - public: - virtual ~UserSearchWindowFactory() {} + class UIEventStream; + class UserSearchWindowFactory { + public: + virtual ~UserSearchWindowFactory() {} - virtual UserSearchWindow* createUserSearchWindow(UserSearchWindow::Type type, UIEventStream* eventStream, const std::set<std::string>& groups) = 0; - }; + virtual UserSearchWindow* createUserSearchWindow(UserSearchWindow::Type type, UIEventStream* eventStream, const std::set<std::string>& groups) = 0; + }; } diff --git a/Swift/Controllers/UIInterfaces/WhiteboardWindow.h b/Swift/Controllers/UIInterfaces/WhiteboardWindow.h index a4a9ef0..a904ee1 100644 --- a/Swift/Controllers/UIInterfaces/WhiteboardWindow.h +++ b/Swift/Controllers/UIInterfaces/WhiteboardWindow.h @@ -4,23 +4,29 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ -#pragma once +/* + * Copyright (c) 2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ -#include "Swiften/Base/boost_bsignals.h" +#pragma once #include <string> +#include <boost/signals2.hpp> + namespace Swift { - class WhiteboardSession; - class WhiteboardElement; + class WhiteboardSession; + class WhiteboardElement; - class WhiteboardWindow { - public: - virtual ~WhiteboardWindow() {} + class WhiteboardWindow { + public: + virtual ~WhiteboardWindow() {} - virtual void show() = 0; - virtual void setSession(boost::shared_ptr<WhiteboardSession> session) = 0; - virtual void activateWindow() = 0; - virtual void setName(const std::string& name) = 0; - }; + virtual void show() = 0; + virtual void setSession(std::shared_ptr<WhiteboardSession> session) = 0; + virtual void activateWindow() = 0; + virtual void setName(const std::string& name) = 0; + }; } diff --git a/Swift/Controllers/UIInterfaces/WhiteboardWindowFactory.h b/Swift/Controllers/UIInterfaces/WhiteboardWindowFactory.h index 2be0f9c..9868ceb 100644 --- a/Swift/Controllers/UIInterfaces/WhiteboardWindowFactory.h +++ b/Swift/Controllers/UIInterfaces/WhiteboardWindowFactory.h @@ -4,16 +4,24 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ +/* + * Copyright (c) 2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + #pragma once +#include <memory> + namespace Swift { - class WhiteboardSession; - class WhiteboardWindow; + class WhiteboardSession; + class WhiteboardWindow; - class WhiteboardWindowFactory { - public : - virtual ~WhiteboardWindowFactory() {} + class WhiteboardWindowFactory { + public : + virtual ~WhiteboardWindowFactory() {} - virtual WhiteboardWindow* createWhiteboardWindow(boost::shared_ptr<WhiteboardSession> whiteboardSession) = 0; - }; + virtual WhiteboardWindow* createWhiteboardWindow(std::shared_ptr<WhiteboardSession> whiteboardSession) = 0; + }; } diff --git a/Swift/Controllers/UIInterfaces/XMLConsoleWidget.cpp b/Swift/Controllers/UIInterfaces/XMLConsoleWidget.cpp index 2362a2e..0916d0b 100644 --- a/Swift/Controllers/UIInterfaces/XMLConsoleWidget.cpp +++ b/Swift/Controllers/UIInterfaces/XMLConsoleWidget.cpp @@ -1,10 +1,10 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ -#include "Swift/Controllers/UIInterfaces/XMLConsoleWidget.h" +#include <Swift/Controllers/UIInterfaces/XMLConsoleWidget.h> namespace Swift { diff --git a/Swift/Controllers/UIInterfaces/XMLConsoleWidget.h b/Swift/Controllers/UIInterfaces/XMLConsoleWidget.h index caec38c..872b9de 100644 --- a/Swift/Controllers/UIInterfaces/XMLConsoleWidget.h +++ b/Swift/Controllers/UIInterfaces/XMLConsoleWidget.h @@ -1,7 +1,7 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once @@ -9,14 +9,14 @@ #include <Swiften/Base/SafeByteArray.h> namespace Swift { - class XMLConsoleWidget { - public: - virtual ~XMLConsoleWidget(); + class XMLConsoleWidget { + public: + virtual ~XMLConsoleWidget(); - virtual void handleDataRead(const SafeByteArray& data) = 0; - virtual void handleDataWritten(const SafeByteArray& data) = 0; + virtual void handleDataRead(const SafeByteArray& data) = 0; + virtual void handleDataWritten(const SafeByteArray& data) = 0; - virtual void show() = 0; - virtual void activate() = 0; - }; + virtual void show() = 0; + virtual void activate() = 0; + }; } diff --git a/Swift/Controllers/UIInterfaces/XMLConsoleWidgetFactory.h b/Swift/Controllers/UIInterfaces/XMLConsoleWidgetFactory.h index 3cba597..bbc055e 100644 --- a/Swift/Controllers/UIInterfaces/XMLConsoleWidgetFactory.h +++ b/Swift/Controllers/UIInterfaces/XMLConsoleWidgetFactory.h @@ -1,19 +1,19 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include "Swift/Controllers/UIInterfaces/XMLConsoleWidget.h" +#include <Swift/Controllers/UIInterfaces/XMLConsoleWidget.h> namespace Swift { - class UIEventStream; - class XMLConsoleWidgetFactory { - public: - virtual ~XMLConsoleWidgetFactory() {} + class UIEventStream; + class XMLConsoleWidgetFactory { + public: + virtual ~XMLConsoleWidgetFactory() {} - virtual XMLConsoleWidget* createXMLConsoleWidget() = 0; - }; + virtual XMLConsoleWidget* createXMLConsoleWidget() = 0; + }; } diff --git a/Swift/Controllers/UnitTest/ChatMessageSummarizerTest.cpp b/Swift/Controllers/UnitTest/ChatMessageSummarizerTest.cpp index dd2a8e5..59c57b9 100644 --- a/Swift/Controllers/UnitTest/ChatMessageSummarizerTest.cpp +++ b/Swift/Controllers/UnitTest/ChatMessageSummarizerTest.cpp @@ -1,121 +1,121 @@ /* - * Copyright (c) 2011 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2011-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #include <cppunit/extensions/HelperMacros.h> #include <cppunit/extensions/TestFactoryRegistry.h> -#include "Swift/Controllers/ChatMessageSummarizer.h" +#include <Swift/Controllers/ChatMessageSummarizer.h> using namespace Swift; using namespace std; class ChatMessageSummarizerTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(ChatMessageSummarizerTest); - CPPUNIT_TEST(testEmpty); - CPPUNIT_TEST(testCurrentNone); - CPPUNIT_TEST(testCurrentCount); - CPPUNIT_TEST(testCurrentCountOthersNone); - CPPUNIT_TEST(testCurrentCountOtherCount); - CPPUNIT_TEST(testCurrentNoneOtherCount); - CPPUNIT_TEST(testCurrentCountOthersCount); - CPPUNIT_TEST(testCurrentNoneOthersCount); - CPPUNIT_TEST(testCurrentCountSomeOthersCount); - CPPUNIT_TEST_SUITE_END(); + CPPUNIT_TEST_SUITE(ChatMessageSummarizerTest); + CPPUNIT_TEST(testEmpty); + CPPUNIT_TEST(testCurrentNone); + CPPUNIT_TEST(testCurrentCount); + CPPUNIT_TEST(testCurrentCountOthersNone); + CPPUNIT_TEST(testCurrentCountOtherCount); + CPPUNIT_TEST(testCurrentNoneOtherCount); + CPPUNIT_TEST(testCurrentCountOthersCount); + CPPUNIT_TEST(testCurrentNoneOthersCount); + CPPUNIT_TEST(testCurrentCountSomeOthersCount); + CPPUNIT_TEST_SUITE_END(); public: - ChatMessageSummarizerTest() {} - - void setUp() { - - } - - void testEmpty() { - string current(""); - vector<UnreadPair> unreads; - ChatMessageSummarizer summary; - CPPUNIT_ASSERT_EQUAL(current, summary.getSummary(current, unreads)); - } - - void testCurrentNone() { - string current("Bob"); - vector<UnreadPair> unreads; - unreads.push_back(UnreadPair("Bob", 0)); - ChatMessageSummarizer summary; - CPPUNIT_ASSERT_EQUAL(current, summary.getSummary(current, unreads)); - } - - void testCurrentCount() { - string current("Bob"); - vector<UnreadPair> unreads; - unreads.push_back(UnreadPair("Bob", 3)); - ChatMessageSummarizer summary; - CPPUNIT_ASSERT_EQUAL(string("Bob (3)"), summary.getSummary(current, unreads)); - } - - void testCurrentCountOthersNone() { - string current("Bob"); - vector<UnreadPair> unreads; - unreads.push_back(UnreadPair("Bert", 0)); - unreads.push_back(UnreadPair("Bob", 3)); - unreads.push_back(UnreadPair("Betty", 0)); - ChatMessageSummarizer summary; - CPPUNIT_ASSERT_EQUAL(string("Bob (3)"), summary.getSummary(current, unreads)); - } - - void testCurrentCountOtherCount() { - string current("Bob"); - vector<UnreadPair> unreads; - unreads.push_back(UnreadPair("Bert", 0)); - unreads.push_back(UnreadPair("Bob", 3)); - unreads.push_back(UnreadPair("Betty", 7)); - ChatMessageSummarizer summary; - CPPUNIT_ASSERT_EQUAL(string("Bob (3); Betty (7)"), summary.getSummary(current, unreads)); - } - - void testCurrentNoneOtherCount() { - string current("Bob"); - vector<UnreadPair> unreads; - unreads.push_back(UnreadPair("Bert", 0)); - unreads.push_back(UnreadPair("Bob", 0)); - unreads.push_back(UnreadPair("Betty", 7)); - ChatMessageSummarizer summary; - CPPUNIT_ASSERT_EQUAL(string("Bob; Betty (7)"), summary.getSummary(current, unreads)); - } - - void testCurrentNoneOthersCount() { - string current("Bob"); - vector<UnreadPair> unreads; - unreads.push_back(UnreadPair("Bert", 2)); - unreads.push_back(UnreadPair("Bob", 0)); - unreads.push_back(UnreadPair("Betty", 7)); - ChatMessageSummarizer summary; - CPPUNIT_ASSERT_EQUAL(string("Bob and 2 others (9)"), summary.getSummary(current, unreads)); - } - - void testCurrentCountOthersCount() { - string current("Bob"); - vector<UnreadPair> unreads; - unreads.push_back(UnreadPair("Bert", 2)); - unreads.push_back(UnreadPair("Bob", 11)); - unreads.push_back(UnreadPair("Betty", 7)); - ChatMessageSummarizer summary; - CPPUNIT_ASSERT_EQUAL(string("Bob (11) and 2 others (9)"), summary.getSummary(current, unreads)); - } - - void testCurrentCountSomeOthersCount() { - string current("Bob"); - vector<UnreadPair> unreads; - unreads.push_back(UnreadPair("Bert", 2)); - unreads.push_back(UnreadPair("Beverly", 0)); - unreads.push_back(UnreadPair("Bob", 11)); - unreads.push_back(UnreadPair("Beatrice", 0)); - unreads.push_back(UnreadPair("Betty", 7)); - ChatMessageSummarizer summary; - CPPUNIT_ASSERT_EQUAL(string("Bob (11) and 2 others (9)"), summary.getSummary(current, unreads)); - } + ChatMessageSummarizerTest() {} + + void setUp() { + + } + + void testEmpty() { + string current(""); + vector<UnreadPair> unreads; + ChatMessageSummarizer summary; + CPPUNIT_ASSERT_EQUAL(current, summary.getSummary(current, unreads)); + } + + void testCurrentNone() { + string current("Bob"); + vector<UnreadPair> unreads; + unreads.push_back(UnreadPair("Bob", 0)); + ChatMessageSummarizer summary; + CPPUNIT_ASSERT_EQUAL(current, summary.getSummary(current, unreads)); + } + + void testCurrentCount() { + string current("Bob"); + vector<UnreadPair> unreads; + unreads.push_back(UnreadPair("Bob", 3)); + ChatMessageSummarizer summary; + CPPUNIT_ASSERT_EQUAL(string("Bob (3)"), summary.getSummary(current, unreads)); + } + + void testCurrentCountOthersNone() { + string current("Bob"); + vector<UnreadPair> unreads; + unreads.push_back(UnreadPair("Bert", 0)); + unreads.push_back(UnreadPair("Bob", 3)); + unreads.push_back(UnreadPair("Betty", 0)); + ChatMessageSummarizer summary; + CPPUNIT_ASSERT_EQUAL(string("Bob (3)"), summary.getSummary(current, unreads)); + } + + void testCurrentCountOtherCount() { + string current("Bob"); + vector<UnreadPair> unreads; + unreads.push_back(UnreadPair("Bert", 0)); + unreads.push_back(UnreadPair("Bob", 3)); + unreads.push_back(UnreadPair("Betty", 7)); + ChatMessageSummarizer summary; + CPPUNIT_ASSERT_EQUAL(string("Bob (3); Betty (7)"), summary.getSummary(current, unreads)); + } + + void testCurrentNoneOtherCount() { + string current("Bob"); + vector<UnreadPair> unreads; + unreads.push_back(UnreadPair("Bert", 0)); + unreads.push_back(UnreadPair("Bob", 0)); + unreads.push_back(UnreadPair("Betty", 7)); + ChatMessageSummarizer summary; + CPPUNIT_ASSERT_EQUAL(string("Bob; Betty (7)"), summary.getSummary(current, unreads)); + } + + void testCurrentNoneOthersCount() { + string current("Bob"); + vector<UnreadPair> unreads; + unreads.push_back(UnreadPair("Bert", 2)); + unreads.push_back(UnreadPair("Bob", 0)); + unreads.push_back(UnreadPair("Betty", 7)); + ChatMessageSummarizer summary; + CPPUNIT_ASSERT_EQUAL(string("Bob and 2 others (9)"), summary.getSummary(current, unreads)); + } + + void testCurrentCountOthersCount() { + string current("Bob"); + vector<UnreadPair> unreads; + unreads.push_back(UnreadPair("Bert", 2)); + unreads.push_back(UnreadPair("Bob", 11)); + unreads.push_back(UnreadPair("Betty", 7)); + ChatMessageSummarizer summary; + CPPUNIT_ASSERT_EQUAL(string("Bob (11) and 2 others (9)"), summary.getSummary(current, unreads)); + } + + void testCurrentCountSomeOthersCount() { + string current("Bob"); + vector<UnreadPair> unreads; + unreads.push_back(UnreadPair("Bert", 2)); + unreads.push_back(UnreadPair("Beverly", 0)); + unreads.push_back(UnreadPair("Bob", 11)); + unreads.push_back(UnreadPair("Beatrice", 0)); + unreads.push_back(UnreadPair("Betty", 7)); + ChatMessageSummarizer summary; + CPPUNIT_ASSERT_EQUAL(string("Bob (11) and 2 others (9)"), summary.getSummary(current, unreads)); + } }; diff --git a/Swift/Controllers/UnitTest/ContactSuggesterTest.cpp b/Swift/Controllers/UnitTest/ContactSuggesterTest.cpp new file mode 100644 index 0000000..6ac51b2 --- /dev/null +++ b/Swift/Controllers/UnitTest/ContactSuggesterTest.cpp @@ -0,0 +1,128 @@ +/* + * Copyright (c) 2014-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#include <memory> + +#include <boost/bind.hpp> +#include <boost/function.hpp> + +#include <cppunit/extensions/HelperMacros.h> +#include <cppunit/extensions/TestFactoryRegistry.h> + +#include <Swift/Controllers/ContactSuggester.h> + +using namespace Swift; + +class ContactSuggesterTest : public CppUnit::TestFixture { + CPPUNIT_TEST_SUITE(ContactSuggesterTest); + CPPUNIT_TEST(equalityTest); + CPPUNIT_TEST(lexicographicalSortTest); + CPPUNIT_TEST(sortTest); + CPPUNIT_TEST_SUITE_END(); + +public: + + std::vector<std::string> wordList() { + const std::string words[] = { + "abc", + "ab", + "bc", + "d" + }; + + return std::vector<std::string>(words, words+sizeof(words)/sizeof(*words)); + } + + std::vector<StatusShow::Type> statusList() { + StatusShow::Type types[] = { + StatusShow::Online, + StatusShow::Away, + StatusShow::FFC, + StatusShow::XA, + StatusShow::DND, + StatusShow::None + }; + + return std::vector<StatusShow::Type>(types, types+sizeof(types)/sizeof(*types)); + } + + std::vector<Contact::ref> contactList() { + std::vector<Contact::ref> contacts; + std::vector<std::string> words = wordList(); + std::vector<StatusShow::Type> statuses = statusList(); + for (const auto& name : words) { + for (const auto& jid : words) { + for (const auto& status : statuses) { + contacts.push_back(std::make_shared<Contact>(name, jid, status, "")); + } + } + } + return contacts; + } + + /* a = a */ + bool isReflexive(const boost::function<bool (const Contact::ref&, const Contact::ref&)>& comparitor) { + std::vector<Contact::ref> contacts = contactList(); + for (const auto& a : contacts) { + if (!comparitor(a, a)) { + return false; + } + } + return true; + } + + /* a = b -> b = a */ + bool isSymmetric(const boost::function<bool (const Contact::ref&, const Contact::ref&)>& comparitor) { + std::vector<Contact::ref> contacts = contactList(); + for (const auto& a : contacts) { + for (const auto& b : contacts) { + if (comparitor(a, b)) { + if (!comparitor(b, a)) { + return false; + } + } + } + } + return true; + } + + /* a = b && b = c -> a = c */ + bool isTransitive(const boost::function<bool (const Contact::ref&, const Contact::ref&)>& comparitor) { + std::vector<Contact::ref> contacts = contactList(); + for (const auto& a : contacts) { + for (const auto& b : contacts) { + for (const auto& c : contacts) { + if (comparitor(a, b) && comparitor(b, c)) { + if (!comparitor(a, c)) { + return false; + } + } + } + } + } + return true; + } + + void equalityTest() { + CPPUNIT_ASSERT(isReflexive(Contact::equalityPredicate)); + CPPUNIT_ASSERT(isSymmetric(Contact::equalityPredicate)); + CPPUNIT_ASSERT(isTransitive(Contact::equalityPredicate)); + } + + void lexicographicalSortTest() { + CPPUNIT_ASSERT(isTransitive(Contact::lexicographicalSortPredicate)); + } + + void sortTest() { + std::vector<std::string> words = wordList(); + for (const auto& word : words) { + CPPUNIT_ASSERT(isTransitive(boost::bind(Contact::sortPredicate, _1, _2, word))); + } + } + +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(ContactSuggesterTest); diff --git a/Swift/Controllers/UnitTest/FdpFormSubmitControllerTest.cpp b/Swift/Controllers/UnitTest/FdpFormSubmitControllerTest.cpp new file mode 100644 index 0000000..8dc3442 --- /dev/null +++ b/Swift/Controllers/UnitTest/FdpFormSubmitControllerTest.cpp @@ -0,0 +1,269 @@ +/* + * Copyright (c) 2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#include <memory> +#include <string> + +#include <gtest/gtest.h> + +#include <Swiften/Client/DummyStanzaChannel.h> +#include <Swiften/Elements/DiscoItems.h> +#include <Swiften/Elements/Form.h> +#include <Swiften/Elements/FormField.h> +#include <Swiften/Elements/IQ.h> +#include <Swiften/Elements/Payload.h> +#include <Swiften/Elements/PubSub.h> +#include <Swiften/Elements/PubSubItems.h> +#include <Swiften/Elements/PubSubPublish.h> +#include <Swiften/JID/JID.h> +#include <Swiften/Queries/IQRouter.h> + +#include <Swift/Controllers/FdpFormSubmitController.h> +#include <Swift/Controllers/UIEvents/FdpFormSubmitWindowOpenUIEvent.h> +#include <Swift/Controllers/UIEvents/UIEventStream.h> +#include <Swift/Controllers/UnitTest/MockFdpFormSubmitWindowFactory.h> +#include <Swift/Controllers/UnitTest/MockFdpFormSubmitWindow.h> + +namespace Swift { + +class FdpFormSubmitControllerTest : public ::testing::Test { + + protected: + void SetUp() { + clientJid_ = JID(clientJIDString_); + stanzaChannel_ = std::make_unique<DummyStanzaChannel>(); + iqRouter_ = std::make_unique<IQRouter>(stanzaChannel_.get()); + uiEventStream_ = std::make_unique<UIEventStream>(); + fdpWindowFactory_ = std::make_unique<MockFdpFormSubmitWindowFactory>(); + fdpController_ = std::make_unique<FdpFormSubmitController>(clientJid_, iqRouter_.get(), uiEventStream_.get(), fdpWindowFactory_.get()); + auto fdpWindowOpenUIEvent = std::make_shared<FdpFormSubmitWindowOpenUIEvent>(); + uiEventStream_->send(fdpWindowOpenUIEvent); + fdpWindow_ = fdpWindowFactory_->getMockFdpFormSubmitWindow(); + } + + void TearDown() { + } + + std::shared_ptr<DiscoItems> createDiscoItemsResult(); + std::shared_ptr<PubSub> createTemplatePubSubResult(const std::shared_ptr<Form>& form); + std::shared_ptr<PubSub> createSubmittedPubSubResult(); + + std::string clientJIDString_ = "testjid@test.im/swift"; + JID clientJid_; + std::unique_ptr<DummyStanzaChannel> stanzaChannel_; + std::unique_ptr<IQRouter> iqRouter_; + std::unique_ptr<UIEventStream> uiEventStream_; + std::unique_ptr<MockFdpFormSubmitWindowFactory> fdpWindowFactory_; + std::unique_ptr<FdpFormSubmitController> fdpController_; + MockFdpFormSubmitWindow* fdpWindow_; +}; + +std::shared_ptr<DiscoItems> FdpFormSubmitControllerTest::createDiscoItemsResult() { + auto discoItems = std::make_shared<DiscoItems>(); + discoItems->addItem(DiscoItems::Item("node0", JID(), "fdp/template/testNode0")); + discoItems->addItem(DiscoItems::Item("node1", JID(), "fdp/template/testNode1")); + discoItems->addItem(DiscoItems::Item("node2", JID(), "fdp/template/testNode2")); + return discoItems; +} + +std::shared_ptr<PubSub> FdpFormSubmitControllerTest::createTemplatePubSubResult(const std::shared_ptr<Form>& form) { + auto pubSubItem = std::make_shared<PubSubItem>(); + pubSubItem->addData(form); + auto pubSubItems = std::make_shared<PubSubItems>(); + pubSubItems->addItem(pubSubItem); + auto pubSub = std::make_shared<PubSub>(); + pubSub->setPayload(pubSubItems); + return pubSub; +} + +std::shared_ptr<PubSub> FdpFormSubmitControllerTest::createSubmittedPubSubResult() { + auto pubSubItem = std::make_shared<PubSubItem>(); + pubSubItem->setID("testID"); + auto pubSubPublish = std::make_shared<PubSubPublish>(); + pubSubPublish->addItem(pubSubItem); + pubSubPublish->setNode("fdp/submitted/test"); + auto pubSub = std::make_shared<PubSub>(); + pubSub->setPayload(pubSubPublish); + return pubSub; +} + +TEST_F(FdpFormSubmitControllerTest, testRequestPubSubNodeData) { + std::string domainName = "fdp.example.test"; + fdpWindow_->onRequestPubSubNodeData(domainName); + ASSERT_EQ(1, stanzaChannel_->sentStanzas.size()); + auto requestIq = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[0]); + ASSERT_EQ(domainName, requestIq->getTo()); + auto discoItemsPayloads = requestIq->getPayloads<DiscoItems>(); + ASSERT_EQ(1, discoItemsPayloads.size()); + + auto discoItemsResult = createDiscoItemsResult(); + auto resultIq = IQ::createResult(clientJid_, domainName, requestIq->getID(), discoItemsResult); + stanzaChannel_->onIQReceived(resultIq); + + auto discoItemsList = discoItemsResult->getItems(); + ASSERT_EQ(discoItemsList.size(), fdpWindow_->nodeData.size()); + for (unsigned int i = 0; i < fdpWindow_->nodeData.size(); i++) { + auto nodeItem = fdpWindow_->nodeData[i]; + auto discoItem = discoItemsList[i]; + ASSERT_EQ(discoItem.getNode(), nodeItem.first); + ASSERT_EQ(discoItem.getName(), nodeItem.second); + } + ASSERT_EQ(FdpFormSubmitWindow::NodeError::NoError, fdpWindow_->nodeError_); +} + +TEST_F(FdpFormSubmitControllerTest, testRequestPubSubNodeDataError) { + std::string domainName = "fdp.example.test"; + fdpWindow_->onRequestPubSubNodeData(domainName); + ASSERT_EQ(1, stanzaChannel_->sentStanzas.size()); + auto requestIq = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[0]); + ASSERT_EQ(domainName, requestIq->getTo()); + + auto resultIq = IQ::createError(clientJid_, domainName, requestIq->getID()); + stanzaChannel_->onIQReceived(resultIq); + + ASSERT_EQ(true, fdpWindow_->nodeData.empty()); + ASSERT_EQ(FdpFormSubmitWindow::NodeError::DomainNotFound, fdpWindow_->nodeError_); +} + +TEST_F(FdpFormSubmitControllerTest, testRequestTemplateForm) { + std::string domainName = "fdp.example.test"; + fdpWindow_->onRequestPubSubNodeData(domainName); + auto nodeDataRequestIq = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[0]); + auto discoItemsResult = createDiscoItemsResult(); + auto discoItemsResultIq = IQ::createResult(clientJid_, domainName, nodeDataRequestIq->getID(), discoItemsResult); + stanzaChannel_->onIQReceived(discoItemsResultIq); + + std::string templateNodeName = "fdp/template/test"; + fdpWindow_->onRequestTemplateForm(templateNodeName); + ASSERT_EQ(2, stanzaChannel_->sentStanzas.size()); + auto requestIq = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[1]); + ASSERT_EQ(domainName, requestIq->getTo()); + auto pubSubPayloads = requestIq->getPayloads<PubSub>(); + ASSERT_EQ(1, pubSubPayloads.size()); + + std::string value0("value0"); + std::string value1("value1@example.test"); + auto field0 = std::make_shared<FormField>(FormField::TextSingleType, value0); + auto field1 = std::make_shared<FormField>(FormField::JIDSingleType, value1); + auto form = std::make_shared<Form>(); + form->addField(field0); + form->addField(field1); + auto pubSubResult = createTemplatePubSubResult(form); + auto resultIq = IQ::createResult(clientJid_, domainName, requestIq->getID(), pubSubResult); + stanzaChannel_->onIQReceived(resultIq); + + ASSERT_EQ(form, fdpWindow_->templateForm_); + auto fields = fdpWindow_->templateForm_->getFields(); + ASSERT_EQ(2, fields.size()); + ASSERT_EQ(field0, fields[0]); + ASSERT_EQ(field1, fields[1]); + ASSERT_EQ(value0, fields[0]->getTextSingleValue()); + ASSERT_EQ(value1, fields[1]->getJIDSingleValue()); + ASSERT_EQ(FdpFormSubmitWindow::TemplateError::NoError, fdpWindow_->templateError_); +} + +TEST_F(FdpFormSubmitControllerTest, testRequestTemplateFormError) { + std::string domainName = "fdp.example.test"; + fdpWindow_->onRequestPubSubNodeData(domainName); + auto nodeDataRequestIq = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[0]); + auto discoItemsResult = createDiscoItemsResult(); + auto discoItemsResultIq = IQ::createResult(clientJid_, domainName, nodeDataRequestIq->getID(), discoItemsResult); + stanzaChannel_->onIQReceived(discoItemsResultIq); + + std::string templateNodeName = "fdp/template/test"; + fdpWindow_->onRequestTemplateForm(templateNodeName); + ASSERT_EQ(2, stanzaChannel_->sentStanzas.size()); + auto requestIq = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[1]); + ASSERT_EQ(domainName, requestIq->getTo()); + + auto resultIq = IQ::createError(clientJid_, domainName, requestIq->getID()); + stanzaChannel_->onIQReceived(resultIq); + + ASSERT_EQ(nullptr, fdpWindow_->templateForm_); + ASSERT_EQ(FdpFormSubmitWindow::TemplateError::RequestFailed, fdpWindow_->templateError_); +} + +TEST_F(FdpFormSubmitControllerTest, testSubmitForm) { + std::string domainName = "fdp.example.test"; + std::string templateNodeName = "fdp/template/test"; + fdpWindow_->onRequestPubSubNodeData(domainName); + auto nodeDataRequestIq = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[0]); + auto discoItemsResult = createDiscoItemsResult(); + auto discoItemsResultIq = IQ::createResult(clientJid_, domainName, nodeDataRequestIq->getID(), discoItemsResult); + stanzaChannel_->onIQReceived(discoItemsResultIq); + fdpWindow_->onRequestTemplateForm(templateNodeName); + auto templateFormRequestIq = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[1]); + auto templateForm = std::make_shared<Form>(); + auto templatePubSubResult = createTemplatePubSubResult(templateForm); + auto templatePubSubResultIq = IQ::createResult(clientJid_, domainName, templateFormRequestIq->getID(), templatePubSubResult); + stanzaChannel_->onIQReceived(templatePubSubResultIq); + + std::string value0("value0"); + std::string value1("value1@example.test"); + auto field0 = std::make_shared<FormField>(FormField::TextSingleType, value0); + auto field1 = std::make_shared<FormField>(FormField::JIDSingleType, value1); + auto form = std::make_shared<Form>(); + form->addField(field0); + form->addField(field1); + fdpWindow_->onSubmitForm(form); + + ASSERT_EQ(3, stanzaChannel_->sentStanzas.size()); + auto requestIq = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[2]); + ASSERT_EQ(domainName, requestIq->getTo()); + auto pubSubPayloads = requestIq->getPayloads<PubSub>(); + ASSERT_EQ(1, pubSubPayloads.size()); + auto pubSubPayload = pubSubPayloads[0]; + auto pubSubPublishPayload = std::dynamic_pointer_cast<PubSubPublish>(pubSubPayload->getPayload()); + ASSERT_TRUE(pubSubPublishPayload); + auto pubSubItems = pubSubPublishPayload->getItems(); + ASSERT_EQ(1, pubSubItems.size()); + auto dataList = pubSubItems[0]->getData(); + ASSERT_EQ(1, dataList.size()); + auto submittedForm = std::dynamic_pointer_cast<Form>(dataList[0]); + ASSERT_TRUE(submittedForm); + ASSERT_EQ(form, submittedForm); + auto fields = submittedForm->getFields(); + ASSERT_EQ(2, fields.size()); + ASSERT_EQ(field0, fields[0]); + ASSERT_EQ(field1, fields[1]); + ASSERT_EQ(value0, fields[0]->getTextSingleValue()); + ASSERT_EQ(value1, fields[1]->getJIDSingleValue()); + + auto pubSubResult = createSubmittedPubSubResult(); + auto resultIq = IQ::createResult(clientJid_, domainName, requestIq->getID(), pubSubResult); + stanzaChannel_->onIQReceived(resultIq); + + ASSERT_EQ(true, fdpWindow_->submissionSuccess_); +} + +TEST_F(FdpFormSubmitControllerTest, testSubmitFormError) { + std::string domainName = "fdp.example.test"; + std::string templateNodeName = "fdp/template/test"; + fdpWindow_->onRequestPubSubNodeData(domainName); + auto nodeDataRequestIq = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[0]); + auto discoItemsResult = createDiscoItemsResult(); + auto discoItemsResultIq = IQ::createResult(clientJid_, domainName, nodeDataRequestIq->getID(), discoItemsResult); + stanzaChannel_->onIQReceived(discoItemsResultIq); + fdpWindow_->onRequestTemplateForm(templateNodeName); + auto templateFormRequestIq = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[1]); + auto templateForm = std::make_shared<Form>(); + auto templatePubSubResult = createTemplatePubSubResult(templateForm); + auto templatePubSubResultIq = IQ::createResult(clientJid_, domainName, templateFormRequestIq->getID(), templatePubSubResult); + stanzaChannel_->onIQReceived(templatePubSubResultIq); + + auto form = std::make_shared<Form>(); + fdpWindow_->onSubmitForm(form); + ASSERT_EQ(3, stanzaChannel_->sentStanzas.size()); + auto requestIq = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[2]); + ASSERT_EQ(domainName, requestIq->getTo()); + + auto resultIq = IQ::createError(clientJid_, domainName, requestIq->getID()); + stanzaChannel_->onIQReceived(resultIq); + + ASSERT_EQ(false, fdpWindow_->submissionSuccess_); +} + +} diff --git a/Swift/Controllers/UnitTest/HighlightRuleTest.cpp b/Swift/Controllers/UnitTest/HighlightRuleTest.cpp deleted file mode 100644 index ec81227..0000000 --- a/Swift/Controllers/UnitTest/HighlightRuleTest.cpp +++ /dev/null @@ -1,318 +0,0 @@ -/* - * Copyright (c) 2012 Maciej Niedzielski - * Licensed under the simplified BSD license. - * See Documentation/Licenses/BSD-simplified.txt for more information. - */ - -#include <vector> -#include <string> - -#include <cppunit/extensions/HelperMacros.h> -#include <cppunit/extensions/TestFactoryRegistry.h> - -#include <Swift/Controllers/HighlightRule.h> - -using namespace Swift; - -class HighlightRuleTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(HighlightRuleTest); - CPPUNIT_TEST(testEmptyRuleNeverMatches); - CPPUNIT_TEST(testKeyword); - CPPUNIT_TEST(testNickKeyword); - CPPUNIT_TEST(testNickWithoutOtherKeywords); - CPPUNIT_TEST(testSender); - CPPUNIT_TEST(testSenderAndKeyword); - CPPUNIT_TEST(testWholeWords); - CPPUNIT_TEST(testCase); - CPPUNIT_TEST(testWholeWordsAndCase); - CPPUNIT_TEST(testMUC); - CPPUNIT_TEST_SUITE_END(); - - public: - void setUp() { - std::vector<std::string> keywords; - keywords.push_back("keyword1"); - keywords.push_back("KEYWORD2"); - - std::vector<std::string>senders; - senders.push_back("sender1"); - senders.push_back("SENDER2"); - - emptyRule = new HighlightRule(); - - keywordRule = new HighlightRule(); - keywordRule->setKeywords(keywords); - - keywordChatRule = new HighlightRule(); - keywordChatRule->setKeywords(keywords); - keywordChatRule->setMatchChat(true); - - keywordNickChatRule = new HighlightRule(); - keywordNickChatRule->setKeywords(keywords); - keywordNickChatRule->setNickIsKeyword(true); - keywordNickChatRule->setMatchChat(true); - - nickChatRule = new HighlightRule(); - nickChatRule->setNickIsKeyword(true); - nickChatRule->setMatchChat(true); - - nickRule = new HighlightRule(); - nickRule->setNickIsKeyword(true); - - senderRule = new HighlightRule(); - senderRule->setSenders(senders); - - senderChatRule = new HighlightRule(); - senderChatRule->setSenders(senders); - senderChatRule->setMatchChat(true); - - senderKeywordChatRule = new HighlightRule(); - senderKeywordChatRule->setSenders(senders); - senderKeywordChatRule->setKeywords(keywords); - senderKeywordChatRule->setMatchChat(true); - - senderKeywordNickChatRule = new HighlightRule(); - senderKeywordNickChatRule->setSenders(senders); - senderKeywordNickChatRule->setKeywords(keywords); - senderKeywordNickChatRule->setNickIsKeyword(true); - senderKeywordNickChatRule->setMatchChat(true); - - senderKeywordNickWordChatRule = new HighlightRule(); - senderKeywordNickWordChatRule->setSenders(senders); - senderKeywordNickWordChatRule->setKeywords(keywords); - senderKeywordNickWordChatRule->setNickIsKeyword(true); - senderKeywordNickWordChatRule->setMatchWholeWords(true); - senderKeywordNickWordChatRule->setMatchChat(true); - - senderKeywordNickCaseChatRule = new HighlightRule(); - senderKeywordNickCaseChatRule->setSenders(senders); - senderKeywordNickCaseChatRule->setKeywords(keywords); - senderKeywordNickCaseChatRule->setNickIsKeyword(true); - senderKeywordNickCaseChatRule->setMatchCase(true); - senderKeywordNickCaseChatRule->setMatchChat(true); - - senderKeywordNickCaseWordChatRule = new HighlightRule(); - senderKeywordNickCaseWordChatRule->setSenders(senders); - senderKeywordNickCaseWordChatRule->setKeywords(keywords); - senderKeywordNickCaseWordChatRule->setNickIsKeyword(true); - senderKeywordNickCaseWordChatRule->setMatchCase(true); - senderKeywordNickCaseWordChatRule->setMatchWholeWords(true); - senderKeywordNickCaseWordChatRule->setMatchChat(true); - - senderKeywordNickMUCRule = new HighlightRule(); - senderKeywordNickMUCRule->setSenders(senders); - senderKeywordNickMUCRule->setKeywords(keywords); - senderKeywordNickMUCRule->setNickIsKeyword(true); - senderKeywordNickMUCRule->setMatchMUC(true); - } - - void tearDown() { - delete emptyRule; - - delete keywordRule; - delete keywordChatRule; - delete keywordNickChatRule; - delete nickChatRule; - delete nickRule; - - delete senderRule; - delete senderChatRule; - delete senderKeywordChatRule; - delete senderKeywordNickChatRule; - - delete senderKeywordNickWordChatRule; - delete senderKeywordNickCaseChatRule; - delete senderKeywordNickCaseWordChatRule; - - delete senderKeywordNickMUCRule; - } - - void testEmptyRuleNeverMatches() { - CPPUNIT_ASSERT_EQUAL(emptyRule->isMatch("body", "from", "nick", HighlightRule::ChatMessage), false); - CPPUNIT_ASSERT_EQUAL(emptyRule->isMatch("body", "from", "nick", HighlightRule::MUCMessage), false); - - CPPUNIT_ASSERT_EQUAL(emptyRule->isMatch("body", "from", "", HighlightRule::ChatMessage), false); - CPPUNIT_ASSERT_EQUAL(emptyRule->isMatch("body", "from", "", HighlightRule::MUCMessage), false); - - CPPUNIT_ASSERT_EQUAL(emptyRule->isMatch("body", "", "nick", HighlightRule::ChatMessage), false); - CPPUNIT_ASSERT_EQUAL(emptyRule->isMatch("body", "", "nick", HighlightRule::MUCMessage), false); - - CPPUNIT_ASSERT_EQUAL(emptyRule->isMatch("", "from", "nick", HighlightRule::ChatMessage), false); - CPPUNIT_ASSERT_EQUAL(emptyRule->isMatch("", "from", "nick", HighlightRule::MUCMessage), false); - - CPPUNIT_ASSERT_EQUAL(emptyRule->isMatch("body", "", "", HighlightRule::ChatMessage), false); - CPPUNIT_ASSERT_EQUAL(emptyRule->isMatch("body", "", "", HighlightRule::MUCMessage), false); - - CPPUNIT_ASSERT_EQUAL(emptyRule->isMatch("", "from", "", HighlightRule::ChatMessage), false); - CPPUNIT_ASSERT_EQUAL(emptyRule->isMatch("", "from", "", HighlightRule::MUCMessage), false); - - CPPUNIT_ASSERT_EQUAL(emptyRule->isMatch("", "", "nick", HighlightRule::ChatMessage), false); - CPPUNIT_ASSERT_EQUAL(emptyRule->isMatch("", "", "nick", HighlightRule::MUCMessage), false); - - CPPUNIT_ASSERT_EQUAL(emptyRule->isMatch("", "", "", HighlightRule::ChatMessage), false); - CPPUNIT_ASSERT_EQUAL(emptyRule->isMatch("", "", "", HighlightRule::MUCMessage), false); - } - - void testKeyword() { - CPPUNIT_ASSERT_EQUAL(keywordChatRule->isMatch("body", "from", "nick", HighlightRule::ChatMessage), false); - CPPUNIT_ASSERT_EQUAL(keywordChatRule->isMatch("body", "from", "nick", HighlightRule::MUCMessage), false); - - CPPUNIT_ASSERT_EQUAL(keywordChatRule->isMatch("keyword1", "from", "nick", HighlightRule::ChatMessage), true); - CPPUNIT_ASSERT_EQUAL(keywordChatRule->isMatch("keyword1", "from", "nick", HighlightRule::MUCMessage), false); - CPPUNIT_ASSERT_EQUAL(keywordRule->isMatch("keyword1", "from", "nick", HighlightRule::ChatMessage), false); - - CPPUNIT_ASSERT_EQUAL(keywordChatRule->isMatch("body", "sender contains keyword1", "nick", HighlightRule::ChatMessage), false); - - CPPUNIT_ASSERT_EQUAL(keywordChatRule->isMatch("abc keyword1 xyz", "from", "nick", HighlightRule::ChatMessage), true); - CPPUNIT_ASSERT_EQUAL(keywordChatRule->isMatch("abckeyword1xyz", "from", "nick", HighlightRule::ChatMessage), true); - - CPPUNIT_ASSERT_EQUAL(keywordChatRule->isMatch("KEYword1", "from", "nick", HighlightRule::ChatMessage), true); - CPPUNIT_ASSERT_EQUAL(keywordChatRule->isMatch("abc KEYword1 xyz", "from", "nick", HighlightRule::ChatMessage), true); - CPPUNIT_ASSERT_EQUAL(keywordChatRule->isMatch("abcKEYword1xyz", "from", "nick", HighlightRule::ChatMessage), true); - - CPPUNIT_ASSERT_EQUAL(keywordChatRule->isMatch("keyword2", "from", "nick", HighlightRule::ChatMessage), true); - } - - void testNickKeyword() { - CPPUNIT_ASSERT_EQUAL(keywordNickChatRule->isMatch("body contains nick", "from", "nick", HighlightRule::ChatMessage), true); - CPPUNIT_ASSERT_EQUAL(keywordNickChatRule->isMatch("body contains nick", "from", "nick", HighlightRule::MUCMessage), false); - CPPUNIT_ASSERT_EQUAL(keywordChatRule->isMatch("body contains nick", "from", "nick", HighlightRule::ChatMessage), false); - - CPPUNIT_ASSERT_EQUAL(keywordNickChatRule->isMatch("keyword1", "from", "nick", HighlightRule::ChatMessage), true); - - CPPUNIT_ASSERT_EQUAL(keywordNickChatRule->isMatch("body", "sender contains nick", "nick", HighlightRule::ChatMessage), false); - - CPPUNIT_ASSERT_EQUAL(keywordNickChatRule->isMatch("body contains mixed-case NiCk", "sender", "nick", HighlightRule::ChatMessage), true); - - CPPUNIT_ASSERT_EQUAL(keywordNickChatRule->isMatch("nickname", "from", "nick", HighlightRule::ChatMessage), true); - CPPUNIT_ASSERT_EQUAL(keywordNickChatRule->isMatch("NIckNAME", "from", "nick", HighlightRule::ChatMessage), true); - - CPPUNIT_ASSERT_EQUAL(keywordNickChatRule->isMatch("body", "from", "", HighlightRule::ChatMessage), false); - } - - void testNickWithoutOtherKeywords() { - CPPUNIT_ASSERT_EQUAL(nickChatRule->isMatch("body contains nick", "from", "nick", HighlightRule::ChatMessage), true); - CPPUNIT_ASSERT_EQUAL(nickChatRule->isMatch("body contains nick", "from", "nick", HighlightRule::MUCMessage), false); - CPPUNIT_ASSERT_EQUAL(nickRule->isMatch("body contains nick", "from", "nick", HighlightRule::ChatMessage), false); - - CPPUNIT_ASSERT_EQUAL(nickChatRule->isMatch("keyword1", "from", "nick", HighlightRule::ChatMessage), false); - - CPPUNIT_ASSERT_EQUAL(nickChatRule->isMatch("body", "sender contains nick but it does't matter", "nick", HighlightRule::ChatMessage), false); - - CPPUNIT_ASSERT_EQUAL(nickChatRule->isMatch("body contains mixed-case NiCk", "from", "nick", HighlightRule::ChatMessage), true); - - CPPUNIT_ASSERT_EQUAL(nickChatRule->isMatch("nickname", "from", "nick", HighlightRule::ChatMessage), true); - CPPUNIT_ASSERT_EQUAL(nickChatRule->isMatch("NIckNAME", "from", "nick", HighlightRule::ChatMessage), true); - - // there are no keywords in this rule and empty nick is not treated as a keyword, so we don't check for keywords to get a match - CPPUNIT_ASSERT_EQUAL(nickChatRule->isMatch("body", "from", "", HighlightRule::ChatMessage), true); - } - - void testSender() { - CPPUNIT_ASSERT_EQUAL(senderChatRule->isMatch("body", "from", "nick", HighlightRule::ChatMessage), false); - CPPUNIT_ASSERT_EQUAL(senderChatRule->isMatch("body", "from", "nick", HighlightRule::MUCMessage), false); - - CPPUNIT_ASSERT_EQUAL(senderChatRule->isMatch("body", "sender1", "nick", HighlightRule::ChatMessage), true); - CPPUNIT_ASSERT_EQUAL(senderChatRule->isMatch("body", "sender1", "nick", HighlightRule::MUCMessage), false); - CPPUNIT_ASSERT_EQUAL(senderRule->isMatch("body", "sender1", "nick", HighlightRule::ChatMessage), false); - - CPPUNIT_ASSERT_EQUAL(senderRule->isMatch("body contains sender1", "from", "nick", HighlightRule::ChatMessage), false); - - CPPUNIT_ASSERT_EQUAL(senderChatRule->isMatch("body", "abc sender1 xyz", "nick", HighlightRule::ChatMessage), true); - CPPUNIT_ASSERT_EQUAL(senderChatRule->isMatch("body", "abcsender1xyz", "nick", HighlightRule::ChatMessage), true); - - CPPUNIT_ASSERT_EQUAL(senderChatRule->isMatch("body", "SENDer1", "nick", HighlightRule::ChatMessage), true); - CPPUNIT_ASSERT_EQUAL(senderChatRule->isMatch("body", "abc SENDer1 xyz", "nick", HighlightRule::ChatMessage), true); - CPPUNIT_ASSERT_EQUAL(senderChatRule->isMatch("body", "abcSENDer1xyz", "nick", HighlightRule::ChatMessage), true); - - CPPUNIT_ASSERT_EQUAL(senderChatRule->isMatch("body", "sender2", "nick", HighlightRule::ChatMessage), true); - } - - void testSenderAndKeyword() { - CPPUNIT_ASSERT_EQUAL(senderKeywordChatRule->isMatch("body", "from", "nick", HighlightRule::ChatMessage), false); - CPPUNIT_ASSERT_EQUAL(senderKeywordChatRule->isMatch("keyword1", "from", "nick", HighlightRule::ChatMessage), false); - CPPUNIT_ASSERT_EQUAL(senderKeywordChatRule->isMatch("body", "sender1", "nick", HighlightRule::ChatMessage), false); - CPPUNIT_ASSERT_EQUAL(senderKeywordChatRule->isMatch("keyword1", "sender1", "nick", HighlightRule::ChatMessage), true); - } - - void testWholeWords() { - CPPUNIT_ASSERT_EQUAL(senderKeywordNickWordChatRule->isMatch("body", "from", "nick", HighlightRule::ChatMessage), false); - CPPUNIT_ASSERT_EQUAL(senderKeywordNickWordChatRule->isMatch("keyword1", "from", "nick", HighlightRule::ChatMessage), false); - CPPUNIT_ASSERT_EQUAL(senderKeywordNickWordChatRule->isMatch("body", "sender1", "nick", HighlightRule::ChatMessage), false); - - CPPUNIT_ASSERT_EQUAL(senderKeywordNickWordChatRule->isMatch("keyword1", "sender1", "nick", HighlightRule::ChatMessage), true); - CPPUNIT_ASSERT_EQUAL(senderKeywordNickWordChatRule->isMatch("xkeyword1", "sender1", "nick", HighlightRule::ChatMessage), false); - CPPUNIT_ASSERT_EQUAL(senderKeywordNickWordChatRule->isMatch("keyword1", "xsender1", "nick", HighlightRule::ChatMessage), false); - - CPPUNIT_ASSERT_EQUAL(senderKeywordNickWordChatRule->isMatch("body contains nick", "sender1", "nick", HighlightRule::ChatMessage), true); - CPPUNIT_ASSERT_EQUAL(senderKeywordNickWordChatRule->isMatch("body contains xnick", "sender1", "nick", HighlightRule::ChatMessage), false); - - CPPUNIT_ASSERT_EQUAL(senderKeywordNickWordChatRule->isMatch("KEYword1", "SENDer1", "nick", HighlightRule::ChatMessage), true); - CPPUNIT_ASSERT_EQUAL(senderKeywordNickWordChatRule->isMatch("body contains NiCk", "sender1", "nick", HighlightRule::ChatMessage), true); - } - - void testCase() { - CPPUNIT_ASSERT_EQUAL(senderKeywordNickCaseChatRule->isMatch("body", "from", "nick", HighlightRule::ChatMessage), false); - CPPUNIT_ASSERT_EQUAL(senderKeywordNickCaseChatRule->isMatch("keyword1", "from", "nick", HighlightRule::ChatMessage), false); - CPPUNIT_ASSERT_EQUAL(senderKeywordNickCaseChatRule->isMatch("body", "sender1", "nick", HighlightRule::ChatMessage), false); - - CPPUNIT_ASSERT_EQUAL(senderKeywordNickCaseChatRule->isMatch("keyword1", "sender1", "nick", HighlightRule::ChatMessage), true); - CPPUNIT_ASSERT_EQUAL(senderKeywordNickCaseChatRule->isMatch("xkeyword1", "xsender1", "nick", HighlightRule::ChatMessage), true); - - CPPUNIT_ASSERT_EQUAL(senderKeywordNickCaseChatRule->isMatch("body contains nick", "sender1", "nick", HighlightRule::ChatMessage), true); - CPPUNIT_ASSERT_EQUAL(senderKeywordNickCaseChatRule->isMatch("body contains xnick", "sender1", "nick", HighlightRule::ChatMessage), true); - - CPPUNIT_ASSERT_EQUAL(senderKeywordNickCaseChatRule->isMatch("KEYword1", "SENDer1", "nick", HighlightRule::ChatMessage), false); - CPPUNIT_ASSERT_EQUAL(senderKeywordNickCaseChatRule->isMatch("keyword1", "SENDer1", "nick", HighlightRule::ChatMessage), false); - CPPUNIT_ASSERT_EQUAL(senderKeywordNickCaseChatRule->isMatch("KEYword1", "sender1", "nick", HighlightRule::ChatMessage), false); - CPPUNIT_ASSERT_EQUAL(senderKeywordNickCaseChatRule->isMatch("body contains NiCk", "sender1", "nick", HighlightRule::ChatMessage), false); - } - - void testWholeWordsAndCase() { - CPPUNIT_ASSERT_EQUAL(senderKeywordNickCaseWordChatRule->isMatch("body", "from", "nick", HighlightRule::ChatMessage), false); - CPPUNIT_ASSERT_EQUAL(senderKeywordNickCaseWordChatRule->isMatch("keyword1", "from", "nick", HighlightRule::ChatMessage), false); - CPPUNIT_ASSERT_EQUAL(senderKeywordNickCaseWordChatRule->isMatch("body", "sender1", "nick", HighlightRule::ChatMessage), false); - - CPPUNIT_ASSERT_EQUAL(senderKeywordNickCaseWordChatRule->isMatch("keyword1", "sender1", "nick", HighlightRule::ChatMessage), true); - CPPUNIT_ASSERT_EQUAL(senderKeywordNickCaseWordChatRule->isMatch("xkeyword1", "sender1", "nick", HighlightRule::ChatMessage), false); - CPPUNIT_ASSERT_EQUAL(senderKeywordNickCaseWordChatRule->isMatch("keyword1", "xsender1", "nick", HighlightRule::ChatMessage), false); - - CPPUNIT_ASSERT_EQUAL(senderKeywordNickCaseWordChatRule->isMatch("body contains nick", "sender1", "nick", HighlightRule::ChatMessage), true); - CPPUNIT_ASSERT_EQUAL(senderKeywordNickCaseWordChatRule->isMatch("body contains xnick", "sender1", "nick", HighlightRule::ChatMessage), false); - - CPPUNIT_ASSERT_EQUAL(senderKeywordNickCaseWordChatRule->isMatch("KEYword1", "SENDer1", "nick", HighlightRule::ChatMessage), false); - CPPUNIT_ASSERT_EQUAL(senderKeywordNickCaseWordChatRule->isMatch("keyword1", "SENDer1", "nick", HighlightRule::ChatMessage), false); - CPPUNIT_ASSERT_EQUAL(senderKeywordNickCaseWordChatRule->isMatch("KEYword1", "sender1", "nick", HighlightRule::ChatMessage), false); - CPPUNIT_ASSERT_EQUAL(senderKeywordNickCaseWordChatRule->isMatch("body contains NiCk", "sender1", "nick", HighlightRule::ChatMessage), false); - } - - void testMUC() { - CPPUNIT_ASSERT_EQUAL(senderKeywordNickMUCRule->isMatch("body", "from", "nick", HighlightRule::ChatMessage), false); - - CPPUNIT_ASSERT_EQUAL(senderKeywordNickMUCRule->isMatch("keyword1", "sender1", "nick", HighlightRule::ChatMessage), false); - CPPUNIT_ASSERT_EQUAL(senderKeywordNickMUCRule->isMatch("keyword1", "sender1", "nick", HighlightRule::MUCMessage), true); - CPPUNIT_ASSERT_EQUAL(senderKeywordNickMUCRule->isMatch("body contains nick", "sender1", "nick", HighlightRule::MUCMessage), true); - } - - private: - HighlightRule* emptyRule; - - HighlightRule* keywordRule; - HighlightRule* keywordChatRule; - HighlightRule* keywordNickChatRule; - HighlightRule* nickChatRule; - HighlightRule* nickRule; - - HighlightRule* senderRule; - HighlightRule* senderChatRule; - HighlightRule* senderKeywordChatRule; - HighlightRule* senderKeywordNickChatRule; - - HighlightRule* senderKeywordNickWordChatRule; - HighlightRule* senderKeywordNickCaseChatRule; - HighlightRule* senderKeywordNickCaseWordChatRule; - - HighlightRule* senderKeywordNickMUCRule; -}; - -CPPUNIT_TEST_SUITE_REGISTRATION(HighlightRuleTest); diff --git a/Swift/Controllers/UnitTest/MockChatWindow.cpp b/Swift/Controllers/UnitTest/MockChatWindow.cpp index 494bc46..f0f666a 100644 --- a/Swift/Controllers/UnitTest/MockChatWindow.cpp +++ b/Swift/Controllers/UnitTest/MockChatWindow.cpp @@ -1,10 +1,10 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ -#include "Swift/Controllers/UnitTest/MockChatWindow.h" +#include <Swift/Controllers/UnitTest/MockChatWindow.h> namespace Swift { MockChatWindow::~MockChatWindow() { diff --git a/Swift/Controllers/UnitTest/MockChatWindow.h b/Swift/Controllers/UnitTest/MockChatWindow.h index 43779c5..38b3b1f 100644 --- a/Swift/Controllers/UnitTest/MockChatWindow.h +++ b/Swift/Controllers/UnitTest/MockChatWindow.h @@ -1,91 +1,172 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2017 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <boost/shared_ptr.hpp> +#include <memory> +#include <string> #include <Swift/Controllers/UIInterfaces/ChatWindow.h> -#include <Swiften/Base/foreach.h> - namespace Swift { - class MockChatWindow : public ChatWindow { - public: - MockChatWindow() : labelsEnabled_(false) {} - virtual ~MockChatWindow(); - - virtual std::string addMessage(const ChatMessage& message, const std::string& /*senderName*/, bool /*senderIsSelf*/, boost::shared_ptr<SecurityLabel> /*label*/, const std::string& /*avatarPath*/, const boost::posix_time::ptime& /*time*/, const HighlightAction& /*highlight*/) { - lastMessageBody_ = bodyFromMessage(message); return "id";} - - virtual std::string addAction(const ChatMessage& /*message*/, const std::string& /*senderName*/, bool /*senderIsSelf*/, boost::shared_ptr<SecurityLabel> /*label*/, const std::string& /*avatarPath*/, const boost::posix_time::ptime& /*time*/, const HighlightAction& /*highlight*/) {return "id";} - - virtual void addSystemMessage(const ChatMessage& /*message*/, Direction /*direction*/) {} - virtual void addPresenceMessage(const ChatMessage& /*message*/, Direction /*direction*/) {} - - virtual void addErrorMessage(const ChatMessage& /*message*/) {} - virtual void replaceMessage(const ChatMessage& /*message*/, const std::string& /*id*/, const boost::posix_time::ptime& /*time*/, const HighlightAction& /*highlight*/) {} - virtual void replaceWithAction(const ChatMessage& /*message*/, const std::string& /*id*/, const boost::posix_time::ptime& /*time*/, const HighlightAction& /*highlight*/) {} - virtual void replaceLastMessage(const ChatMessage& /*message*/) {} - - // File transfer related stuff - virtual std::string addFileTransfer(const std::string& /*senderName*/, bool /*senderIsSelf*/,const std::string& /*filename*/, const boost::uintmax_t /*sizeInBytes*/) { return 0; } - virtual void setFileTransferProgress(std::string /*id*/, const int /*alreadyTransferedBytes*/) { } - virtual void setFileTransferStatus(std::string /*id*/, const FileTransferState /*state*/, const std::string& /*msg*/) { } - - virtual void setMessageReceiptState(const std::string &/* id */, ReceiptState /* state */) { } - - virtual void setContactChatState(ChatState::ChatStateType /*state*/) {} - virtual void setName(const std::string& name) {name_ = name;} - virtual void show() {} - virtual void activate() {} - virtual void setAvailableSecurityLabels(const std::vector<SecurityLabelsCatalog::Item>& labels) {labels_ = labels;} - virtual void setSecurityLabelsEnabled(bool enabled) {labelsEnabled_ = enabled;} - virtual void setUnreadMessageCount(int /*count*/) {} - virtual void convertToMUC(bool /*impromptuMUC*/) {} - virtual void setSecurityLabelsError() {} - virtual SecurityLabelsCatalog::Item getSelectedSecurityLabel() {return label_;} - virtual void setInputEnabled(bool /*enabled*/) {} - virtual void setRosterModel(Roster* /*roster*/) {} - virtual void setTabComplete(TabComplete*) {} - - void setAckState(const std::string& /*id*/, AckState /*state*/) {} - virtual void flash() {} - virtual void setAlert(const std::string& /*alertText*/, const std::string& /*buttonText*/) {} - virtual void cancelAlert() {} - virtual void setCorrectionEnabled(Tristate /*enabled*/) {} - void setAvailableOccupantActions(const std::vector<OccupantAction>&/* actions*/) {} - void setSubject(const std::string& /*subject*/) {} - virtual void showRoomConfigurationForm(Form::ref) {} - virtual void addMUCInvitation(const std::string& /*senderName*/, const JID& /*jid*/, const std::string& /*reason*/, const std::string& /*password*/, bool = true, bool = false, bool = false) {} - - virtual std::string addWhiteboardRequest(bool) {return "";} - virtual void setWhiteboardSessionStatus(std::string, const ChatWindow::WhiteboardSessionState){} - - virtual void setAffiliations(MUCOccupant::Affiliation, const std::vector<JID>&) {} - virtual void setAvailableRoomActions(const std::vector<RoomAction> &) {} - - virtual void setBlockingState(BlockingState) {} - virtual void setCanInitiateImpromptuChats(bool /*supportsImpromptu*/) {} - - std::string bodyFromMessage(const ChatMessage& message) { - boost::shared_ptr<ChatTextMessagePart> text; - foreach (boost::shared_ptr<ChatMessagePart> part, message.getParts()) { - if ((text = boost::dynamic_pointer_cast<ChatTextMessagePart>(part))) { - return text->text; - } - } - return ""; - } - - std::string name_; - std::string lastMessageBody_; - std::vector<SecurityLabelsCatalog::Item> labels_; - bool labelsEnabled_; - SecurityLabelsCatalog::Item label_; - }; + class MockChatWindow : public ChatWindow { + public: + MockChatWindow() {} + virtual ~MockChatWindow(); + + virtual std::string addMessage(const ChatMessage& message, const std::string& senderName, bool senderIsSelf, std::shared_ptr<SecurityLabel> label, const std::string& /*avatarPath*/, const boost::posix_time::ptime& /*time*/) { + lastAddedMessage_ = message; + lastAddedMessageSenderName_ = senderName; + lastAddedMessageSenderIsSelf_ = senderIsSelf; + lastAddedMessageSecurityLabel_ = label; + return "id"; + } + + virtual std::string addAction(const ChatMessage& message, const std::string& senderName, bool senderIsSelf, std::shared_ptr<SecurityLabel> /*label*/, const std::string& /*avatarPath*/, const boost::posix_time::ptime& /*time*/) { + lastAddedAction_ = message; + lastAddedActionSenderName_ = senderName; + lastAddedActionSenderIsSelf_ = senderIsSelf; + return "id"; + } + + virtual std::string addSystemMessage(const ChatMessage& message, Direction /*direction*/) { + lastAddedSystemMessage_ = message; + return "id"; + } + + virtual void addPresenceMessage(const ChatMessage& message, Direction /*direction*/) { + lastAddedPresence_ = message; + } + + virtual void addErrorMessage(const ChatMessage& message) { + lastAddedErrorMessage_ = message; + } + + virtual void replaceMessage(const ChatMessage& message, const std::string& /*id*/, const boost::posix_time::ptime& /*time*/) { + lastReplacedMessage_ = message; + } + + virtual void replaceWithAction(const ChatMessage& /*message*/, const std::string& /*id*/, const boost::posix_time::ptime& /*time*/) {} + virtual void replaceLastMessage(const ChatMessage& message, const TimestampBehaviour /*timestampBehaviour*/) { + lastReplacedLastMessage_ = message; + } + virtual void replaceSystemMessage(const ChatMessage& message, const std::string& /*id*/, const TimestampBehaviour /*timestampBehaviour*/) { + lastReplacedSystemMessage_ = message; + } + + // File transfer related stuff + virtual std::string addFileTransfer(const std::string& /*senderName*/, const std::string& /*avatarPath*/, bool /*senderIsSelf*/,const std::string& /*filename*/, const boost::uintmax_t /*sizeInBytes*/, const std::string& /*description*/) { return nullptr; } + virtual void setFileTransferProgress(std::string /*id*/, const int /*alreadyTransferedBytes*/) { } + virtual void setFileTransferStatus(std::string /*id*/, const FileTransferState /*state*/, const std::string& /*msg*/) { } + + virtual void setMessageReceiptState(const std::string & id, ReceiptState state) { + receiptChanges_.emplace_back(id, state); + } + + virtual void setContactChatState(ChatState::ChatStateType /*state*/) {} + virtual void setName(const std::string& name) {name_ = name;} + virtual void show() {} + virtual bool isVisible() const { return true; } + virtual void activate() {} + virtual void setAvailableSecurityLabels(const std::vector<SecurityLabelsCatalog::Item>& labels) {labels_ = labels;} + virtual void setSecurityLabelsEnabled(bool enabled) {labelsEnabled_ = enabled;} + virtual void setUnreadMessageCount(size_t /*count*/) {} + + virtual void convertToMUC(MUCType mucType) { + mucType_ = mucType; + } + + virtual void setSecurityLabelsError() {} + virtual SecurityLabelsCatalog::Item getSelectedSecurityLabel() {return label_;} + virtual void setOnline(bool /*online*/) {} + virtual void setRosterModel(Roster* roster) { roster_ = roster; } + Roster* getRosterModel() { return roster_; } + virtual void setTabComplete(TabComplete*) {} + + void setAckState(const std::string& /*id*/, AckState /*state*/) {} + virtual void flash() {} + virtual AlertID addAlert(const std::string& /*alertText*/) { return 0; } + virtual void removeAlert(const AlertID /*id*/) {} + virtual void setCorrectionEnabled(Tristate /*enabled*/) {} + virtual void setFileTransferEnabled(Tristate /*enabled*/) {} + void setAvailableOccupantActions(const std::vector<OccupantAction>&/* actions*/) {} + void setSubject(const std::string& /*subject*/) {} + virtual void showRoomConfigurationForm(Form::ref) {} + virtual void addMUCInvitation(const std::string& /*senderName*/, const JID& jid, const std::string& /*reason*/, const std::string& /*password*/, bool = true, bool = false, bool = false) { + lastMUCInvitationJID_ = jid; + } + + virtual std::string addWhiteboardRequest(bool) {return "";} + virtual void setWhiteboardSessionStatus(std::string, const ChatWindow::WhiteboardSessionState){} + + virtual void setAffiliations(MUCOccupant::Affiliation, const std::vector<JID>&) {} + virtual void setAvailableRoomActions(const std::vector<RoomAction> &) {} + + virtual void setBlockingState(BlockingState) {} + virtual void setCanInitiateImpromptuChats(bool supportsImpromptu) { + impromptuMUCSupported_ = supportsImpromptu; + } + + virtual void showBookmarkWindow(const MUCBookmark& /*bookmark*/) {} + virtual void setBookmarkState(RoomBookmarkState) {} + + static std::string bodyFromMessage(const ChatMessage& message) { + std::string body; + std::shared_ptr<ChatTextMessagePart> text; + std::shared_ptr<ChatHighlightingMessagePart> highlight; + for (auto &&part : message.getParts()) { + if ((text = std::dynamic_pointer_cast<ChatTextMessagePart>(part))) { + body += text->text; + } + else if ((highlight = std::dynamic_pointer_cast<ChatHighlightingMessagePart>(part))) { + body += highlight->text; + } + } + return body; + } + + void resetLastMessages() { + lastAddedMessage_ = lastAddedAction_ = lastAddedPresence_ = lastReplacedLastMessage_ = lastAddedSystemMessage_ = lastReplacedSystemMessage_ = ChatMessage(); + lastAddedMessageSenderName_ = lastAddedActionSenderName_ = ""; + lastAddedMessageSenderIsSelf_ = lastAddedActionSenderIsSelf_ = false; + } + + void setChatSecurityMarking(const std::string& markingValue, const std::string& markingForegroundColorValue, const std::string& markingBackgroundColorValue) { + markingValue_ = markingValue; + markingForegroundColorValue_ = markingForegroundColorValue; + markingBackgroundColorValue_ = markingBackgroundColorValue; + } + + void removeChatSecurityMarking() {} + + std::string name_; + ChatMessage lastAddedMessage_; + std::string lastAddedMessageSenderName_; + bool lastAddedMessageSenderIsSelf_ = false; + std::shared_ptr<SecurityLabel> lastAddedMessageSecurityLabel_ = nullptr; + ChatMessage lastAddedAction_; + std::string lastAddedActionSenderName_; + bool lastAddedActionSenderIsSelf_ = false; + ChatMessage lastAddedPresence_; + ChatMessage lastReplacedMessage_; + ChatMessage lastReplacedLastMessage_; + ChatMessage lastAddedSystemMessage_; + ChatMessage lastReplacedSystemMessage_; + ChatMessage lastAddedErrorMessage_; + JID lastMUCInvitationJID_; + std::vector<SecurityLabelsCatalog::Item> labels_; + bool labelsEnabled_ = false; + bool impromptuMUCSupported_ = false; + SecurityLabelsCatalog::Item label_; + Roster* roster_ = nullptr; + std::vector<std::pair<std::string, ReceiptState>> receiptChanges_; + boost::optional<MUCType> mucType_; + std::string markingValue_; + std::string markingForegroundColorValue_; + std::string markingBackgroundColorValue_; + }; } diff --git a/Swift/Controllers/UnitTest/MockFdpFormSubmitWindow.h b/Swift/Controllers/UnitTest/MockFdpFormSubmitWindow.h new file mode 100644 index 0000000..28ef35f --- /dev/null +++ b/Swift/Controllers/UnitTest/MockFdpFormSubmitWindow.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#pragma once + +#include <memory> +#include <string> + +#include <Swift/Controllers/UIInterfaces/FdpFormSubmitWindow.h> + +namespace Swift { + + class Form; + + class MockFdpFormSubmitWindow : public FdpFormSubmitWindow { + public: + MockFdpFormSubmitWindow() : FdpFormSubmitWindow() {} + virtual void show() override {} + virtual void raise() override {} + virtual void addNode(const std::string& node, const std::string& nodeName) override { nodeData.push_back(std::pair<std::string, std::string>(node, nodeName)); } + virtual void clearNodeData() override { nodeData.clear(); } + virtual void setFormData(const std::shared_ptr<Form>& form) override { templateForm_ = form; } + virtual void showNodePlaceholder(NodeError nodeError) override { nodeError_ = nodeError; } + virtual void showFormPlaceholder(TemplateError templateError) override { templateError_ = templateError; } + virtual void handleSubmitServerResponse(bool submissionSuccess) override { submissionSuccess_ = submissionSuccess; } + + std::vector<std::pair<std::string, std::string>> nodeData; + std::shared_ptr<Form> templateForm_ = nullptr; + NodeError nodeError_ = NodeError::NoError; + TemplateError templateError_ = TemplateError::NoError; + bool submissionSuccess_ = false; + }; + +} diff --git a/Swift/Controllers/UnitTest/MockFdpFormSubmitWindowFactory.h b/Swift/Controllers/UnitTest/MockFdpFormSubmitWindowFactory.h new file mode 100644 index 0000000..8015419 --- /dev/null +++ b/Swift/Controllers/UnitTest/MockFdpFormSubmitWindowFactory.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#pragma once + +#include <memory> + +#include <Swift/Controllers/UIInterfaces/FdpFormSubmitWindowFactory.h> +#include <Swift/Controllers/UnitTest/MockFdpFormSubmitWindow.h> + +namespace Swift { + + class MockFdpFormSubmitWindowFactory : public FdpFormSubmitWindowFactory { + public: + MockFdpFormSubmitWindowFactory() : FdpFormSubmitWindowFactory() {} + + virtual std::unique_ptr<FdpFormSubmitWindow> createFdpFormSubmitWindow() override { + std::unique_ptr<FdpFormSubmitWindow> fdpFormSubmitWindow = std::make_unique<MockFdpFormSubmitWindow>(); + mockFdpFormSubmitWindow_ = static_cast<MockFdpFormSubmitWindow*>(fdpFormSubmitWindow.get()); + return fdpFormSubmitWindow; + } + + MockFdpFormSubmitWindow* getMockFdpFormSubmitWindow() { return mockFdpFormSubmitWindow_; } + + private: + MockFdpFormSubmitWindow* mockFdpFormSubmitWindow_; + }; +} diff --git a/Swift/Controllers/UnitTest/MockMainWindow.h b/Swift/Controllers/UnitTest/MockMainWindow.h index 69a4e25..9265310 100644 --- a/Swift/Controllers/UnitTest/MockMainWindow.h +++ b/Swift/Controllers/UnitTest/MockMainWindow.h @@ -1,31 +1,33 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include "Swift/Controllers/UIInterfaces/MainWindow.h" +#include <Swift/Controllers/UIInterfaces/MainWindow.h> namespace Swift { - class Roster; - class MockMainWindow : public MainWindow { - public: - MockMainWindow() : roster(NULL) {} - virtual ~MockMainWindow() {} - virtual void setRosterModel(Roster* roster) {this->roster = roster;} - virtual void setMyNick(const std::string& /*name*/) {} - virtual void setMyJID(const JID& /*jid*/) {} - virtual void setMyAvatarPath(const std::string& /*path*/) {} - virtual void setMyStatusText(const std::string& /*status*/) {} - virtual void setMyStatusType(StatusShow::Type /*type*/) {} - virtual void setAvailableAdHocCommands(const std::vector<DiscoItems::Item>& /*commands*/) {} - virtual void setConnecting() {} - virtual void setStreamEncryptionStatus(bool /*tlsInPlaceAndValid*/) {} - virtual void openCertificateDialog(const std::vector<Certificate::ref>& /*chain*/) {} - virtual void setBlockingCommandAvailable(bool /*isAvailable*/) {} - Roster* roster; + class Roster; + class MockMainWindow : public MainWindow { + public: + MockMainWindow() : roster(nullptr) {} + virtual ~MockMainWindow() {} + virtual void setRosterModel(Roster* roster) {this->roster = roster;} + virtual void setMyNick(const std::string& /*name*/) {} + virtual void setMyJID(const JID& /*jid*/) {} + virtual void setMyAvatarPath(const std::string& /*path*/) {} + virtual void setMyStatusText(const std::string& /*status*/) {} + virtual void setMyStatusType(StatusShow::Type /*type*/) {} + virtual void setMyContactRosterItem(std::shared_ptr<ContactRosterItem> /*contact*/) {} + virtual void setAvailableAdHocCommands(const std::vector<DiscoItems::Item>& /*commands*/) {} + virtual void setConnecting() {} + virtual void setStreamEncryptionStatus(bool /*tlsInPlaceAndValid*/) {} + virtual void openCertificateDialog(const std::vector<Certificate::ref>& /*chain*/) {} + virtual void setBlockingCommandAvailable(bool /*isAvailable*/) {} + virtual void openFdpFormSubmitDialog(const JID& /*self*/, IQRouter* /*iqRouter*/) {} + Roster* roster; - }; + }; } diff --git a/Swift/Controllers/UnitTest/MockMainWindowFactory.h b/Swift/Controllers/UnitTest/MockMainWindowFactory.h index 279a6dd..331ca11 100644 --- a/Swift/Controllers/UnitTest/MockMainWindowFactory.h +++ b/Swift/Controllers/UnitTest/MockMainWindowFactory.h @@ -1,28 +1,26 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include "Swift/Controllers/UIInterfaces/MainWindowFactory.h" -#include "Swift/Controllers/UnitTest/MockMainWindow.h" +#include <Swift/Controllers/UIInterfaces/MainWindowFactory.h> +#include <Swift/Controllers/UnitTest/MockMainWindow.h> namespace Swift { - class MockMainWindowFactory : public MainWindowFactory { - public: - MockMainWindowFactory() : last(NULL) {} + class MockMainWindowFactory : public MainWindowFactory { + public: + MockMainWindowFactory() : last(nullptr) {} - virtual ~MockMainWindowFactory() {} + virtual ~MockMainWindowFactory() {} - /** - * Transfers ownership of result. - */ - virtual MainWindow* createMainWindow(UIEventStream*) {last = new MockMainWindow();return last;} - MockMainWindow* last; - }; + /** + * Transfers ownership of result. + */ + virtual MainWindow* createMainWindow(Chattables&, UIEventStream*) {last = new MockMainWindow();return last;} + MockMainWindow* last; + }; } - - diff --git a/Swift/Controllers/UnitTest/PresenceNotifierTest.cpp b/Swift/Controllers/UnitTest/PresenceNotifierTest.cpp index 3e9be13..1375475 100644 --- a/Swift/Controllers/UnitTest/PresenceNotifierTest.cpp +++ b/Swift/Controllers/UnitTest/PresenceNotifierTest.cpp @@ -1,318 +1,322 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ -#include <cppunit/extensions/HelperMacros.h> -#include <cppunit/extensions/TestFactoryRegistry.h> #include <vector> + #include <boost/bind.hpp> -#include "Swift/Controllers/PresenceNotifier.h" -#include "Swiften/Client/NickResolver.h" -#include "SwifTools/Notifier/LoggingNotifier.h" -#include "Swiften/Client/DummyStanzaChannel.h" -#include "Swiften/MUC/MUCRegistry.h" -#include "Swiften/Roster/XMPPRosterImpl.h" -#include "Swiften/Presence/PresenceOracle.h" -#include "Swiften/Avatars/DummyAvatarManager.h" -#include "Swiften/Network/DummyTimerFactory.h" +#include <cppunit/extensions/HelperMacros.h> +#include <cppunit/extensions/TestFactoryRegistry.h> + +#include <Swiften/Avatars/DummyAvatarManager.h> +#include <Swiften/Client/DummyStanzaChannel.h> +#include <Swiften/Client/NickResolver.h> +#include <Swiften/MUC/MUCRegistry.h> +#include <Swiften/Network/DummyTimerFactory.h> +#include <Swiften/Presence/PresenceOracle.h> +#include <Swiften/Roster/XMPPRosterImpl.h> + +#include <Swift/Controllers/PresenceNotifier.h> + +#include <SwifTools/Notifier/LoggingNotifier.h> using namespace Swift; class PresenceNotifierTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(PresenceNotifierTest); - CPPUNIT_TEST(testReceiveFirstPresenceCreatesAvailableNotification); - CPPUNIT_TEST(testReceiveSecondPresenceCreatesStatusChangeNotification); - CPPUNIT_TEST(testReceiveUnavailablePresenceAfterAvailablePresenceCreatesUnavailableNotification); - CPPUNIT_TEST(testReceiveUnavailablePresenceWithoutAvailableDoesNotCreateNotification); - CPPUNIT_TEST(testReceiveAvailablePresenceAfterUnavailableCreatesAvailableNotification); - CPPUNIT_TEST(testReceiveAvailablePresenceAfterReconnectCreatesAvailableNotification); - CPPUNIT_TEST(testReceiveAvailablePresenceFromMUCDoesNotCreateNotification); - CPPUNIT_TEST(testNotificationSubjectContainsNameForJIDInRoster); - CPPUNIT_TEST(testNotificationSubjectContainsJIDForJIDNotInRoster); - CPPUNIT_TEST(testNotificationSubjectContainsStatus); - CPPUNIT_TEST(testNotificationMessageContainsStatusMessage); - CPPUNIT_TEST(testNotificationPicture); - CPPUNIT_TEST(testNotificationActivationEmitsSignal); - CPPUNIT_TEST(testReceiveFirstPresenceWithQuietPeriodDoesNotNotify); - CPPUNIT_TEST(testReceiveFirstPresenceWithQuietPeriodDoesNotCountAsQuietPeriod); - CPPUNIT_TEST(testReceivePresenceDuringQuietPeriodDoesNotNotify); - CPPUNIT_TEST(testReceivePresenceDuringQuietPeriodResetsTimer); - CPPUNIT_TEST(testReceivePresenceAfterQuietPeriodNotifies); - CPPUNIT_TEST(testReceiveFirstPresenceAfterReconnectWithQuietPeriodDoesNotNotify); - CPPUNIT_TEST_SUITE_END(); - - public: - void setUp() { - stanzaChannel = new DummyStanzaChannel(); - notifier = new LoggingNotifier(); - mucRegistry = new MUCRegistry(); - user1 = JID("user1@bar.com/bla"); - user2 = JID("user2@foo.com/baz"); - avatarManager = new DummyAvatarManager(); - roster = new XMPPRosterImpl(); - nickResolver = new NickResolver(JID("foo@bar.com"), roster, NULL, mucRegistry); - presenceOracle = new PresenceOracle(stanzaChannel); - timerFactory = new DummyTimerFactory(); - } - - void tearDown() { - delete timerFactory; - delete presenceOracle; - delete nickResolver; - delete roster; - delete avatarManager; - delete mucRegistry; - delete notifier; - delete stanzaChannel; - } - - void testReceiveFirstPresenceCreatesAvailableNotification() { - boost::shared_ptr<PresenceNotifier> testling = createNotifier(); - - sendPresence(user1, StatusShow::Online); - - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(notifier->notifications.size())); - CPPUNIT_ASSERT_EQUAL(Notifier::ContactAvailable, notifier->notifications[0].type); - } - - void testReceiveSecondPresenceCreatesStatusChangeNotification() { - boost::shared_ptr<PresenceNotifier> testling = createNotifier(); - sendPresence(user1, StatusShow::Away); - notifier->notifications.clear(); - - sendPresence(user1, StatusShow::Online); - - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(notifier->notifications.size())); - CPPUNIT_ASSERT_EQUAL(Notifier::ContactStatusChange, notifier->notifications[0].type); - } - - void testReceiveUnavailablePresenceAfterAvailablePresenceCreatesUnavailableNotification() { - boost::shared_ptr<PresenceNotifier> testling = createNotifier(); - sendPresence(user1, StatusShow::Away); - notifier->notifications.clear(); - - sendUnavailablePresence(user1); - - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(notifier->notifications.size())); - CPPUNIT_ASSERT_EQUAL(Notifier::ContactUnavailable, notifier->notifications[0].type); - } - - void testReceiveUnavailablePresenceWithoutAvailableDoesNotCreateNotification() { - boost::shared_ptr<PresenceNotifier> testling = createNotifier(); - - sendUnavailablePresence(user1); - - CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(notifier->notifications.size())); - } - - void testReceiveAvailablePresenceAfterUnavailableCreatesAvailableNotification() { - boost::shared_ptr<PresenceNotifier> testling = createNotifier(); - sendPresence(user1, StatusShow::Away); - sendUnavailablePresence(user1); - notifier->notifications.clear(); - - sendPresence(user1, StatusShow::Away); - - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(notifier->notifications.size())); - CPPUNIT_ASSERT_EQUAL(Notifier::ContactAvailable, notifier->notifications[0].type); - } - - void testReceiveAvailablePresenceAfterReconnectCreatesAvailableNotification() { - boost::shared_ptr<PresenceNotifier> testling = createNotifier(); - sendPresence(user1, StatusShow::Away); - stanzaChannel->setAvailable(false); - stanzaChannel->setAvailable(true); - notifier->notifications.clear(); - - sendPresence(user1, StatusShow::Away); - - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(notifier->notifications.size())); - CPPUNIT_ASSERT_EQUAL(Notifier::ContactAvailable, notifier->notifications[0].type); - } + CPPUNIT_TEST_SUITE(PresenceNotifierTest); + CPPUNIT_TEST(testReceiveFirstPresenceCreatesAvailableNotification); + CPPUNIT_TEST(testReceiveSecondPresenceCreatesStatusChangeNotification); + CPPUNIT_TEST(testReceiveUnavailablePresenceAfterAvailablePresenceCreatesUnavailableNotification); + CPPUNIT_TEST(testReceiveUnavailablePresenceWithoutAvailableDoesNotCreateNotification); + CPPUNIT_TEST(testReceiveAvailablePresenceAfterUnavailableCreatesAvailableNotification); + CPPUNIT_TEST(testReceiveAvailablePresenceAfterReconnectCreatesAvailableNotification); + CPPUNIT_TEST(testReceiveAvailablePresenceFromMUCDoesNotCreateNotification); + CPPUNIT_TEST(testNotificationSubjectContainsNameForJIDInRoster); + CPPUNIT_TEST(testNotificationSubjectContainsJIDForJIDNotInRoster); + CPPUNIT_TEST(testNotificationSubjectContainsStatus); + CPPUNIT_TEST(testNotificationMessageContainsStatusMessage); + CPPUNIT_TEST(testNotificationPicture); + CPPUNIT_TEST(testNotificationActivationEmitsSignal); + CPPUNIT_TEST(testReceiveFirstPresenceWithQuietPeriodDoesNotNotify); + CPPUNIT_TEST(testReceiveFirstPresenceWithQuietPeriodDoesNotCountAsQuietPeriod); + CPPUNIT_TEST(testReceivePresenceDuringQuietPeriodDoesNotNotify); + CPPUNIT_TEST(testReceivePresenceDuringQuietPeriodResetsTimer); + CPPUNIT_TEST(testReceivePresenceAfterQuietPeriodNotifies); + CPPUNIT_TEST(testReceiveFirstPresenceAfterReconnectWithQuietPeriodDoesNotNotify); + CPPUNIT_TEST_SUITE_END(); + + public: + void setUp() { + stanzaChannel = new DummyStanzaChannel(); + notifier = new LoggingNotifier(); + mucRegistry = new MUCRegistry(); + user1 = JID("user1@bar.com/bla"); + user2 = JID("user2@foo.com/baz"); + avatarManager = new DummyAvatarManager(); + roster = new XMPPRosterImpl(); + nickResolver = new NickResolver(JID("foo@bar.com"), roster, nullptr, mucRegistry); + presenceOracle = new PresenceOracle(stanzaChannel, roster); + timerFactory = new DummyTimerFactory(); + } + + void tearDown() { + delete timerFactory; + delete presenceOracle; + delete nickResolver; + delete roster; + delete avatarManager; + delete mucRegistry; + delete notifier; + delete stanzaChannel; + } + + void testReceiveFirstPresenceCreatesAvailableNotification() { + std::shared_ptr<PresenceNotifier> testling = createNotifier(); + + sendPresence(user1, StatusShow::Online); + + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(notifier->notifications.size())); + CPPUNIT_ASSERT_EQUAL(Notifier::ContactAvailable, notifier->notifications[0].type); + } + + void testReceiveSecondPresenceCreatesStatusChangeNotification() { + std::shared_ptr<PresenceNotifier> testling = createNotifier(); + sendPresence(user1, StatusShow::Away); + notifier->notifications.clear(); + + sendPresence(user1, StatusShow::Online); + + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(notifier->notifications.size())); + CPPUNIT_ASSERT_EQUAL(Notifier::ContactStatusChange, notifier->notifications[0].type); + } + + void testReceiveUnavailablePresenceAfterAvailablePresenceCreatesUnavailableNotification() { + std::shared_ptr<PresenceNotifier> testling = createNotifier(); + sendPresence(user1, StatusShow::Away); + notifier->notifications.clear(); + + sendUnavailablePresence(user1); + + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(notifier->notifications.size())); + CPPUNIT_ASSERT_EQUAL(Notifier::ContactUnavailable, notifier->notifications[0].type); + } + + void testReceiveUnavailablePresenceWithoutAvailableDoesNotCreateNotification() { + std::shared_ptr<PresenceNotifier> testling = createNotifier(); + + sendUnavailablePresence(user1); + + CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(notifier->notifications.size())); + } + + void testReceiveAvailablePresenceAfterUnavailableCreatesAvailableNotification() { + std::shared_ptr<PresenceNotifier> testling = createNotifier(); + sendPresence(user1, StatusShow::Away); + sendUnavailablePresence(user1); + notifier->notifications.clear(); + + sendPresence(user1, StatusShow::Away); + + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(notifier->notifications.size())); + CPPUNIT_ASSERT_EQUAL(Notifier::ContactAvailable, notifier->notifications[0].type); + } + + void testReceiveAvailablePresenceAfterReconnectCreatesAvailableNotification() { + std::shared_ptr<PresenceNotifier> testling = createNotifier(); + sendPresence(user1, StatusShow::Away); + stanzaChannel->setAvailable(false); + stanzaChannel->setAvailable(true); + notifier->notifications.clear(); + + sendPresence(user1, StatusShow::Away); + + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(notifier->notifications.size())); + CPPUNIT_ASSERT_EQUAL(Notifier::ContactAvailable, notifier->notifications[0].type); + } - void testReceiveAvailablePresenceFromMUCDoesNotCreateNotification() { - boost::shared_ptr<PresenceNotifier> testling = createNotifier(); - mucRegistry->addMUC(JID("teaparty@wonderland.lit")); + void testReceiveAvailablePresenceFromMUCDoesNotCreateNotification() { + std::shared_ptr<PresenceNotifier> testling = createNotifier(); + mucRegistry->addMUC(JID("teaparty@wonderland.lit")); - sendPresence(JID("teaparty@wonderland.lit/Alice"), StatusShow::Away); + sendPresence(JID("teaparty@wonderland.lit/Alice"), StatusShow::Away); - CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(notifier->notifications.size())); - } + CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(notifier->notifications.size())); + } - void testNotificationPicture() { - boost::shared_ptr<PresenceNotifier> testling = createNotifier(); - avatarManager->avatars[user1] = createByteArray("abcdef"); + void testNotificationPicture() { + std::shared_ptr<PresenceNotifier> testling = createNotifier(); + avatarManager->avatars[user1] = createByteArray("abcdef"); - sendPresence(user1, StatusShow::Online); + sendPresence(user1, StatusShow::Online); - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(notifier->notifications.size())); - CPPUNIT_ASSERT_EQUAL(boost::filesystem::path("/avatars/user1@bar.com/bla"), notifier->notifications[0].picture); - } + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(notifier->notifications.size())); + CPPUNIT_ASSERT_EQUAL(boost::filesystem::path("/avatars/user1@bar.com/bla"), notifier->notifications[0].picture); + } - void testNotificationActivationEmitsSignal() { - boost::shared_ptr<PresenceNotifier> testling = createNotifier(); + void testNotificationActivationEmitsSignal() { + std::shared_ptr<PresenceNotifier> testling = createNotifier(); - sendPresence(user1, StatusShow::Online); - CPPUNIT_ASSERT(notifier->notifications[0].callback); - notifier->notifications[0].callback(); + sendPresence(user1, StatusShow::Online); + CPPUNIT_ASSERT(notifier->notifications[0].callback); + notifier->notifications[0].callback(); - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(activatedNotifications.size())); - CPPUNIT_ASSERT_EQUAL(user1, activatedNotifications[0]); - } + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(activatedNotifications.size())); + CPPUNIT_ASSERT_EQUAL(user1, activatedNotifications[0]); + } - void testNotificationSubjectContainsNameForJIDInRoster() { - boost::shared_ptr<PresenceNotifier> testling = createNotifier(); - roster->addContact(user1.toBare(), "User 1", std::vector<std::string>(), RosterItemPayload::Both); + void testNotificationSubjectContainsNameForJIDInRoster() { + std::shared_ptr<PresenceNotifier> testling = createNotifier(); + roster->addContact(user1.toBare(), "User 1", std::vector<std::string>(), RosterItemPayload::Both); - sendPresence(user1, StatusShow::Online); + sendPresence(user1, StatusShow::Online); - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(notifier->notifications.size())); - std::string subject = notifier->notifications[0].subject; - CPPUNIT_ASSERT(subject.find("User 1") != std::string::npos); - } + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(notifier->notifications.size())); + std::string subject = notifier->notifications[0].subject; + CPPUNIT_ASSERT(subject.find("User 1") != std::string::npos); + } - void testNotificationSubjectContainsJIDForJIDNotInRoster() { - boost::shared_ptr<PresenceNotifier> testling = createNotifier(); + void testNotificationSubjectContainsJIDForJIDNotInRoster() { + std::shared_ptr<PresenceNotifier> testling = createNotifier(); - sendPresence(user1, StatusShow::Online); + sendPresence(user1, StatusShow::Online); - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(notifier->notifications.size())); - std::string subject = notifier->notifications[0].subject; - CPPUNIT_ASSERT(subject.find(user1.toBare().toString()) != std::string::npos); - } + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(notifier->notifications.size())); + std::string subject = notifier->notifications[0].subject; + CPPUNIT_ASSERT(subject.find(user1.toBare().toString()) != std::string::npos); + } - void testNotificationSubjectContainsStatus() { - boost::shared_ptr<PresenceNotifier> testling = createNotifier(); + void testNotificationSubjectContainsStatus() { + std::shared_ptr<PresenceNotifier> testling = createNotifier(); - sendPresence(user1, StatusShow::Away); + sendPresence(user1, StatusShow::Away); - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(notifier->notifications.size())); - std::string subject = notifier->notifications[0].subject; - CPPUNIT_ASSERT(subject.find("Away") != std::string::npos); - } + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(notifier->notifications.size())); + std::string subject = notifier->notifications[0].subject; + CPPUNIT_ASSERT(subject.find("Away") != std::string::npos); + } - void testNotificationMessageContainsStatusMessage() { - boost::shared_ptr<PresenceNotifier> testling = createNotifier(); + void testNotificationMessageContainsStatusMessage() { + std::shared_ptr<PresenceNotifier> testling = createNotifier(); - sendPresence(user1, StatusShow::Away); + sendPresence(user1, StatusShow::Away); - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(notifier->notifications.size())); - CPPUNIT_ASSERT(notifier->notifications[0].description.find("Status Message") != std::string::npos); - } + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(notifier->notifications.size())); + CPPUNIT_ASSERT(notifier->notifications[0].description.find("Status Message") != std::string::npos); + } - void testReceiveFirstPresenceWithQuietPeriodDoesNotNotify() { - boost::shared_ptr<PresenceNotifier> testling = createNotifier(); - testling->setInitialQuietPeriodMS(10); + void testReceiveFirstPresenceWithQuietPeriodDoesNotNotify() { + std::shared_ptr<PresenceNotifier> testling = createNotifier(); + testling->setInitialQuietPeriodMS(10); - sendPresence(user1, StatusShow::Online); + sendPresence(user1, StatusShow::Online); - CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(notifier->notifications.size())); - } + CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(notifier->notifications.size())); + } - void testReceivePresenceDuringQuietPeriodDoesNotNotify() { - boost::shared_ptr<PresenceNotifier> testling = createNotifier(); - testling->setInitialQuietPeriodMS(10); + void testReceivePresenceDuringQuietPeriodDoesNotNotify() { + std::shared_ptr<PresenceNotifier> testling = createNotifier(); + testling->setInitialQuietPeriodMS(10); - sendPresence(user1, StatusShow::Online); - timerFactory->setTime(1); - sendPresence(user2, StatusShow::Away); + sendPresence(user1, StatusShow::Online); + timerFactory->setTime(1); + sendPresence(user2, StatusShow::Away); - CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(notifier->notifications.size())); - } + CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(notifier->notifications.size())); + } - void testReceivePresenceDuringQuietPeriodResetsTimer() { - boost::shared_ptr<PresenceNotifier> testling = createNotifier(); - testling->setInitialQuietPeriodMS(10); + void testReceivePresenceDuringQuietPeriodResetsTimer() { + std::shared_ptr<PresenceNotifier> testling = createNotifier(); + testling->setInitialQuietPeriodMS(10); - sendPresence(user1, StatusShow::Online); - timerFactory->setTime(9); - sendPresence(user2, StatusShow::Away); - timerFactory->setTime(18); - sendPresence(user1, StatusShow::Away); + sendPresence(user1, StatusShow::Online); + timerFactory->setTime(9); + sendPresence(user2, StatusShow::Away); + timerFactory->setTime(18); + sendPresence(user1, StatusShow::Away); - CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(notifier->notifications.size())); - } + CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(notifier->notifications.size())); + } - void testReceivePresenceAfterQuietPeriodNotifies() { - boost::shared_ptr<PresenceNotifier> testling = createNotifier(); - testling->setInitialQuietPeriodMS(10); - - sendPresence(user1, StatusShow::Online); - timerFactory->setTime(11); - sendPresence(user2, StatusShow::Away); - - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(notifier->notifications.size())); - } - - void testReceiveFirstPresenceWithQuietPeriodDoesNotCountAsQuietPeriod() { - boost::shared_ptr<PresenceNotifier> testling = createNotifier(); - testling->setInitialQuietPeriodMS(10); - - timerFactory->setTime(11); - sendPresence(user1, StatusShow::Away); - - CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(notifier->notifications.size())); - } - - void testReceiveFirstPresenceAfterReconnectWithQuietPeriodDoesNotNotify() { - boost::shared_ptr<PresenceNotifier> testling = createNotifier(); - testling->setInitialQuietPeriodMS(10); - sendPresence(user1, StatusShow::Online); - timerFactory->setTime(15); - notifier->notifications.clear(); - - stanzaChannel->setAvailable(false); - stanzaChannel->setAvailable(true); - sendPresence(user1, StatusShow::Online); - timerFactory->setTime(21); - sendPresence(user2, StatusShow::Online); - - CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(notifier->notifications.size())); - } - - - private: - boost::shared_ptr<PresenceNotifier> createNotifier() { - boost::shared_ptr<PresenceNotifier> result(new PresenceNotifier(stanzaChannel, notifier, mucRegistry, avatarManager, nickResolver, presenceOracle, timerFactory)); - result->onNotificationActivated.connect(boost::bind(&PresenceNotifierTest::handleNotificationActivated, this, _1)); - result->setInitialQuietPeriodMS(0); - return result; - } - - void sendPresence(const JID& jid, StatusShow::Type type) { - boost::shared_ptr<Presence> presence(new Presence()); - presence->setFrom(jid); - presence->setShow(type); - presence->setStatus("Status Message"); - stanzaChannel->onPresenceReceived(presence); - } - - void sendUnavailablePresence(const JID& jid) { - boost::shared_ptr<Presence> presence(new Presence()); - presence->setType(Presence::Unavailable); - presence->setFrom(jid); - stanzaChannel->onPresenceReceived(presence); - } - - void handleNotificationActivated(const JID& j) { - activatedNotifications.push_back(j); - } - - private: - DummyStanzaChannel* stanzaChannel; - LoggingNotifier* notifier; - MUCRegistry* mucRegistry; - DummyAvatarManager* avatarManager; - XMPPRosterImpl* roster; - NickResolver* nickResolver; - PresenceOracle* presenceOracle; - DummyTimerFactory* timerFactory; - JID user1; - JID user2; - std::vector<JID> activatedNotifications; + void testReceivePresenceAfterQuietPeriodNotifies() { + std::shared_ptr<PresenceNotifier> testling = createNotifier(); + testling->setInitialQuietPeriodMS(10); + + sendPresence(user1, StatusShow::Online); + timerFactory->setTime(11); + sendPresence(user2, StatusShow::Away); + + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(notifier->notifications.size())); + } + + void testReceiveFirstPresenceWithQuietPeriodDoesNotCountAsQuietPeriod() { + std::shared_ptr<PresenceNotifier> testling = createNotifier(); + testling->setInitialQuietPeriodMS(10); + + timerFactory->setTime(11); + sendPresence(user1, StatusShow::Away); + + CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(notifier->notifications.size())); + } + + void testReceiveFirstPresenceAfterReconnectWithQuietPeriodDoesNotNotify() { + std::shared_ptr<PresenceNotifier> testling = createNotifier(); + testling->setInitialQuietPeriodMS(10); + sendPresence(user1, StatusShow::Online); + timerFactory->setTime(15); + notifier->notifications.clear(); + + stanzaChannel->setAvailable(false); + stanzaChannel->setAvailable(true); + sendPresence(user1, StatusShow::Online); + timerFactory->setTime(21); + sendPresence(user2, StatusShow::Online); + + CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(notifier->notifications.size())); + } + + + private: + std::shared_ptr<PresenceNotifier> createNotifier() { + std::shared_ptr<PresenceNotifier> result(new PresenceNotifier(stanzaChannel, notifier, mucRegistry, avatarManager, nickResolver, presenceOracle, timerFactory)); + result->onNotificationActivated.connect(boost::bind(&PresenceNotifierTest::handleNotificationActivated, this, _1)); + result->setInitialQuietPeriodMS(0); + return result; + } + + void sendPresence(const JID& jid, StatusShow::Type type) { + std::shared_ptr<Presence> presence(new Presence()); + presence->setFrom(jid); + presence->setShow(type); + presence->setStatus("Status Message"); + stanzaChannel->onPresenceReceived(presence); + } + + void sendUnavailablePresence(const JID& jid) { + std::shared_ptr<Presence> presence(new Presence()); + presence->setType(Presence::Unavailable); + presence->setFrom(jid); + stanzaChannel->onPresenceReceived(presence); + } + + void handleNotificationActivated(const JID& j) { + activatedNotifications.push_back(j); + } + + private: + DummyStanzaChannel* stanzaChannel; + LoggingNotifier* notifier; + MUCRegistry* mucRegistry; + DummyAvatarManager* avatarManager; + XMPPRosterImpl* roster; + NickResolver* nickResolver; + PresenceOracle* presenceOracle; + DummyTimerFactory* timerFactory; + JID user1; + JID user2; + std::vector<JID> activatedNotifications; }; CPPUNIT_TEST_SUITE_REGISTRATION(PresenceNotifierTest); diff --git a/Swift/Controllers/UnitTest/PreviousStatusStoreTest.cpp b/Swift/Controllers/UnitTest/PreviousStatusStoreTest.cpp index 9489e5b..be35468 100644 --- a/Swift/Controllers/UnitTest/PreviousStatusStoreTest.cpp +++ b/Swift/Controllers/UnitTest/PreviousStatusStoreTest.cpp @@ -1,44 +1,44 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #include <cppunit/extensions/HelperMacros.h> #include <cppunit/extensions/TestFactoryRegistry.h> -#include "Swift/Controllers/PreviousStatusStore.h" +#include <Swift/Controllers/PreviousStatusStore.h> using namespace Swift; class PreviousStatusStoreTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(PreviousStatusStoreTest); - CPPUNIT_TEST(testGetAll); - //CPPUNIT_TEST(testGetAllLimited); - //CPPUNIT_TEST(testGetSuggestionsInexact); - //CPPUNIT_TEST(testGetSuggestionsExact); - CPPUNIT_TEST_SUITE_END(); + CPPUNIT_TEST_SUITE(PreviousStatusStoreTest); + CPPUNIT_TEST(testGetAll); + //CPPUNIT_TEST(testGetAllLimited); + //CPPUNIT_TEST(testGetSuggestionsInexact); + //CPPUNIT_TEST(testGetSuggestionsExact); + CPPUNIT_TEST_SUITE_END(); public: - void setUp() { - store_ = new PreviousStatusStore(); - store_->addStatus(StatusShow::Online, "At home in the study"); - store_->addStatus(StatusShow::DND, "In a meeting"); - store_->addStatus(StatusShow::DND, "With a client"); - store_->addStatus(StatusShow::Away, "Walking the elephant"); - store_->addStatus(StatusShow::Online, "In the office, at my desk"); - } + void setUp() { + store_ = new PreviousStatusStore(); + store_->addStatus(StatusShow::Online, "At home in the study"); + store_->addStatus(StatusShow::DND, "In a meeting"); + store_->addStatus(StatusShow::DND, "With a client"); + store_->addStatus(StatusShow::Away, "Walking the elephant"); + store_->addStatus(StatusShow::Online, "In the office, at my desk"); + } - void tearDown() { - - } + void tearDown() { + delete store_; + } - void testGetAll() { + void testGetAll() { - } + } private: - PreviousStatusStore* store_; + PreviousStatusStore* store_; }; CPPUNIT_TEST_SUITE_REGISTRATION(PreviousStatusStoreTest); diff --git a/Swift/Controllers/WhiteboardManager.cpp b/Swift/Controllers/WhiteboardManager.cpp index 50aba6f..37fe8e3 100644 --- a/Swift/Controllers/WhiteboardManager.cpp +++ b/Swift/Controllers/WhiteboardManager.cpp @@ -4,133 +4,139 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ +/* + * Copyright (c) 2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + #include <Swift/Controllers/WhiteboardManager.h> #include <boost/bind.hpp> -#include <Swiften/Base/foreach.h> -#include <Swift/Controllers/UIEvents/RequestWhiteboardUIEvent.h> +#include <Swiften/Client/NickResolver.h> +#include <Swiften/Client/StanzaChannel.h> +#include <Swiften/Whiteboard/WhiteboardSessionManager.h> + #include <Swift/Controllers/UIEvents/AcceptWhiteboardSessionUIEvent.h> #include <Swift/Controllers/UIEvents/CancelWhiteboardSessionUIEvent.h> +#include <Swift/Controllers/UIEvents/RequestWhiteboardUIEvent.h> #include <Swift/Controllers/UIEvents/ShowWhiteboardUIEvent.h> -#include "Swiften/Client/NickResolver.h" -#include <Swiften/Client/StanzaChannel.h> -#include <Swiften/Whiteboard/WhiteboardSessionManager.h> namespace Swift { - typedef std::pair<JID, WhiteboardWindow*> JIDWhiteboardWindowPair; - - WhiteboardManager::WhiteboardManager(WhiteboardWindowFactory* whiteboardWindowFactory, UIEventStream* uiEventStream, NickResolver* nickResolver, WhiteboardSessionManager* whiteboardSessionManager) : whiteboardWindowFactory_(whiteboardWindowFactory), uiEventStream_(uiEventStream), nickResolver_(nickResolver), whiteboardSessionManager_(whiteboardSessionManager) { + WhiteboardManager::WhiteboardManager(WhiteboardWindowFactory* whiteboardWindowFactory, UIEventStream* uiEventStream, NickResolver* nickResolver, WhiteboardSessionManager* whiteboardSessionManager) : whiteboardWindowFactory_(whiteboardWindowFactory), uiEventStream_(uiEventStream), nickResolver_(nickResolver), whiteboardSessionManager_(whiteboardSessionManager) { #ifdef SWIFT_EXPERIMENTAL_WB - whiteboardSessionManager_->onSessionRequest.connect(boost::bind(&WhiteboardManager::handleIncomingSession, this, _1)); + whiteboardSessionManager_->onSessionRequest.connect(boost::bind(&WhiteboardManager::handleIncomingSession, this, _1)); #endif - uiEventConnection_ = uiEventStream_->onUIEvent.connect(boost::bind(&WhiteboardManager::handleUIEvent, this, _1)); - } - - WhiteboardManager::~WhiteboardManager() { - foreach (JIDWhiteboardWindowPair whiteboardWindowPair, whiteboardWindows_) { - delete whiteboardWindowPair.second; - } - } - - WhiteboardWindow* WhiteboardManager::createNewWhiteboardWindow(const JID& contact, WhiteboardSession::ref session) { - WhiteboardWindow *window = whiteboardWindowFactory_->createWhiteboardWindow(session); - window->setName(nickResolver_->jidToNick(contact)); - whiteboardWindows_[contact.toBare()] = window; - return window; - } - - WhiteboardWindow* WhiteboardManager::findWhiteboardWindow(const JID& contact) { - if (whiteboardWindows_.find(contact.toBare()) == whiteboardWindows_.end()) { - return NULL; - } - return whiteboardWindows_[contact.toBare()]; - } - - void WhiteboardManager::handleUIEvent(boost::shared_ptr<UIEvent> event) { - boost::shared_ptr<RequestWhiteboardUIEvent> requestWhiteboardEvent = boost::dynamic_pointer_cast<RequestWhiteboardUIEvent>(event); - if (requestWhiteboardEvent) { - requestSession(requestWhiteboardEvent->getContact()); - } - boost::shared_ptr<AcceptWhiteboardSessionUIEvent> sessionAcceptEvent = boost::dynamic_pointer_cast<AcceptWhiteboardSessionUIEvent>(event); - if (sessionAcceptEvent) { - acceptSession(sessionAcceptEvent->getContact()); - } - boost::shared_ptr<CancelWhiteboardSessionUIEvent> sessionCancelEvent = boost::dynamic_pointer_cast<CancelWhiteboardSessionUIEvent>(event); - if (sessionCancelEvent) { - cancelSession(sessionCancelEvent->getContact()); - } - boost::shared_ptr<ShowWhiteboardUIEvent> showWindowEvent = boost::dynamic_pointer_cast<ShowWhiteboardUIEvent>(event); - if (showWindowEvent) { - WhiteboardWindow* window = findWhiteboardWindow(showWindowEvent->getContact()); - if (window != NULL) { - window->activateWindow(); - } - } - } - - void WhiteboardManager::acceptSession(const JID& from) { - IncomingWhiteboardSession::ref session = boost::dynamic_pointer_cast<IncomingWhiteboardSession>(whiteboardSessionManager_->getSession(from)); - WhiteboardWindow* window = findWhiteboardWindow(from); - if (session && window) { - session->accept(); - window->show(); - } - } - - void WhiteboardManager::requestSession(const JID& contact) { - WhiteboardSession::ref session = whiteboardSessionManager_->requestSession(contact); - session->onSessionTerminated.connect(boost::bind(&WhiteboardManager::handleSessionTerminate, this, _1)); - session->onRequestAccepted.connect(boost::bind(&WhiteboardManager::handleSessionAccept, this, _1)); - session->onRequestRejected.connect(boost::bind(&WhiteboardManager::handleRequestReject, this, _1)); - - WhiteboardWindow* window = findWhiteboardWindow(contact); - if (window == NULL) { - createNewWhiteboardWindow(contact, session); - } else { - window->setSession(session); - } - onSessionRequest(session->getTo(), true); - } - - void WhiteboardManager::cancelSession(const JID& from) { - WhiteboardSession::ref session = whiteboardSessionManager_->getSession(from); - if (session) { - session->cancel(); - } - } - - void WhiteboardManager::handleIncomingSession(IncomingWhiteboardSession::ref session) { - session->onSessionTerminated.connect(boost::bind(&WhiteboardManager::handleSessionTerminate, this, _1)); - session->onRequestAccepted.connect(boost::bind(&WhiteboardManager::handleSessionAccept, this, _1)); - - WhiteboardWindow* window = findWhiteboardWindow(session->getTo()); - if (window == NULL) { - createNewWhiteboardWindow(session->getTo(), session); - } else { - window->setSession(session); - } - - onSessionRequest(session->getTo(), false); - } - - void WhiteboardManager::handleSessionTerminate(const JID& contact) { - onSessionTerminate(contact); - } - - void WhiteboardManager::handleSessionCancel(const JID& contact) { - onSessionTerminate(contact); - } - - void WhiteboardManager::handleSessionAccept(const JID& contact) { - WhiteboardWindow* window = findWhiteboardWindow(contact); - window->show(); - onRequestAccepted(contact); - } - - void WhiteboardManager::handleRequestReject(const JID& contact) { - onRequestRejected(contact); - } + uiEventConnection_ = uiEventStream_->onUIEvent.connect(boost::bind(&WhiteboardManager::handleUIEvent, this, _1)); + } + + WhiteboardManager::~WhiteboardManager() { + for (auto&& whiteboardWindowPair : whiteboardWindows_) { + delete whiteboardWindowPair.second; + } + } + + WhiteboardWindow* WhiteboardManager::createNewWhiteboardWindow(const JID& contact, WhiteboardSession::ref session) { + WhiteboardWindow *window = whiteboardWindowFactory_->createWhiteboardWindow(session); + window->setName(nickResolver_->jidToNick(contact)); + whiteboardWindows_[contact.toBare()] = window; + return window; + } + + WhiteboardWindow* WhiteboardManager::findWhiteboardWindow(const JID& contact) { + if (whiteboardWindows_.find(contact.toBare()) == whiteboardWindows_.end()) { + return nullptr; + } + return whiteboardWindows_[contact.toBare()]; + } + + void WhiteboardManager::handleUIEvent(std::shared_ptr<UIEvent> event) { + std::shared_ptr<RequestWhiteboardUIEvent> requestWhiteboardEvent = std::dynamic_pointer_cast<RequestWhiteboardUIEvent>(event); + if (requestWhiteboardEvent) { + requestSession(requestWhiteboardEvent->getContact()); + } + std::shared_ptr<AcceptWhiteboardSessionUIEvent> sessionAcceptEvent = std::dynamic_pointer_cast<AcceptWhiteboardSessionUIEvent>(event); + if (sessionAcceptEvent) { + acceptSession(sessionAcceptEvent->getContact()); + } + std::shared_ptr<CancelWhiteboardSessionUIEvent> sessionCancelEvent = std::dynamic_pointer_cast<CancelWhiteboardSessionUIEvent>(event); + if (sessionCancelEvent) { + cancelSession(sessionCancelEvent->getContact()); + } + std::shared_ptr<ShowWhiteboardUIEvent> showWindowEvent = std::dynamic_pointer_cast<ShowWhiteboardUIEvent>(event); + if (showWindowEvent) { + WhiteboardWindow* window = findWhiteboardWindow(showWindowEvent->getContact()); + if (window != nullptr) { + window->activateWindow(); + } + } + } + + void WhiteboardManager::acceptSession(const JID& from) { + IncomingWhiteboardSession::ref session = std::dynamic_pointer_cast<IncomingWhiteboardSession>(whiteboardSessionManager_->getSession(from)); + WhiteboardWindow* window = findWhiteboardWindow(from); + if (session && window) { + session->accept(); + window->show(); + } + } + + void WhiteboardManager::requestSession(const JID& contact) { + WhiteboardSession::ref session = whiteboardSessionManager_->requestSession(contact); + session->onSessionTerminated.connect(boost::bind(&WhiteboardManager::handleSessionTerminate, this, _1)); + session->onRequestAccepted.connect(boost::bind(&WhiteboardManager::handleSessionAccept, this, _1)); + session->onRequestRejected.connect(boost::bind(&WhiteboardManager::handleRequestReject, this, _1)); + + WhiteboardWindow* window = findWhiteboardWindow(contact); + if (window == nullptr) { + createNewWhiteboardWindow(contact, session); + } else { + window->setSession(session); + } + onSessionRequest(session->getTo(), true); + } + + void WhiteboardManager::cancelSession(const JID& from) { + WhiteboardSession::ref session = whiteboardSessionManager_->getSession(from); + if (session) { + session->cancel(); + } + } + + void WhiteboardManager::handleIncomingSession(IncomingWhiteboardSession::ref session) { + session->onSessionTerminated.connect(boost::bind(&WhiteboardManager::handleSessionTerminate, this, _1)); + session->onRequestAccepted.connect(boost::bind(&WhiteboardManager::handleSessionAccept, this, _1)); + + WhiteboardWindow* window = findWhiteboardWindow(session->getTo()); + if (window == nullptr) { + createNewWhiteboardWindow(session->getTo(), session); + } else { + window->setSession(session); + } + + onSessionRequest(session->getTo(), false); + } + + void WhiteboardManager::handleSessionTerminate(const JID& contact) { + onSessionTerminate(contact); + } + + void WhiteboardManager::handleSessionCancel(const JID& contact) { + onSessionTerminate(contact); + } + + void WhiteboardManager::handleSessionAccept(const JID& contact) { + WhiteboardWindow* window = findWhiteboardWindow(contact); + if (window != nullptr) { + window->show(); + } + onRequestAccepted(contact); + } + + void WhiteboardManager::handleRequestReject(const JID& contact) { + onRequestRejected(contact); + } } diff --git a/Swift/Controllers/WhiteboardManager.h b/Swift/Controllers/WhiteboardManager.h index 2f5767b..3ef14ab 100644 --- a/Swift/Controllers/WhiteboardManager.h +++ b/Swift/Controllers/WhiteboardManager.h @@ -4,56 +4,61 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ +/* + * Copyright (c) 2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + #pragma once #include <map> - -#include <boost/shared_ptr.hpp> +#include <memory> #include <Swiften/JID/JID.h> +#include <Swiften/Whiteboard/IncomingWhiteboardSession.h> +#include <Swiften/Whiteboard/WhiteboardSession.h> #include <Swift/Controllers/UIEvents/UIEventStream.h> -#include <Swift/Controllers/UIInterfaces/WhiteboardWindowFactory.h> #include <Swift/Controllers/UIInterfaces/WhiteboardWindow.h> -#include <Swiften/Whiteboard/WhiteboardSession.h> -#include <Swiften/Whiteboard/IncomingWhiteboardSession.h> +#include <Swift/Controllers/UIInterfaces/WhiteboardWindowFactory.h> namespace Swift { - class WhiteboardSessionManager; - class NickResolver; - - class WhiteboardManager { - public: - WhiteboardManager(WhiteboardWindowFactory* whiteboardWindowFactory, UIEventStream* uiEventStream, NickResolver* nickResolver, WhiteboardSessionManager* whiteboardSessionManager); - ~WhiteboardManager(); - - WhiteboardWindow* createNewWhiteboardWindow(const JID& contact, WhiteboardSession::ref session); - - public: - boost::signal< void (const JID&, bool senderIsSelf)> onSessionRequest; - boost::signal< void (const JID&)> onSessionTerminate; - boost::signal< void (const JID&)> onRequestAccepted; - boost::signal< void (const JID&)> onRequestRejected; - - private: - void handleUIEvent(boost::shared_ptr<UIEvent> event); - void handleSessionTerminate(const JID& contact); - void handleSessionCancel(const JID& contact); - void handleSessionAccept(const JID& contact); - void handleRequestReject(const JID& contact); - void handleIncomingSession(IncomingWhiteboardSession::ref session); - void acceptSession(const JID& from); - void requestSession(const JID& contact); - void cancelSession(const JID& from); - WhiteboardWindow* findWhiteboardWindow(const JID& contact); - - private: - std::map<JID, WhiteboardWindow*> whiteboardWindows_; - WhiteboardWindowFactory* whiteboardWindowFactory_; - UIEventStream* uiEventStream_; - NickResolver* nickResolver_; - boost::bsignals::scoped_connection uiEventConnection_; - WhiteboardSessionManager* whiteboardSessionManager_; - }; + class WhiteboardSessionManager; + class NickResolver; + + class WhiteboardManager { + public: + WhiteboardManager(WhiteboardWindowFactory* whiteboardWindowFactory, UIEventStream* uiEventStream, NickResolver* nickResolver, WhiteboardSessionManager* whiteboardSessionManager); + ~WhiteboardManager(); + + WhiteboardWindow* createNewWhiteboardWindow(const JID& contact, WhiteboardSession::ref session); + + public: + boost::signals2::signal< void (const JID&, bool senderIsSelf)> onSessionRequest; + boost::signals2::signal< void (const JID&)> onSessionTerminate; + boost::signals2::signal< void (const JID&)> onRequestAccepted; + boost::signals2::signal< void (const JID&)> onRequestRejected; + + private: + void handleUIEvent(std::shared_ptr<UIEvent> event); + void handleSessionTerminate(const JID& contact); + void handleSessionCancel(const JID& contact); + void handleSessionAccept(const JID& contact); + void handleRequestReject(const JID& contact); + void handleIncomingSession(IncomingWhiteboardSession::ref session); + void acceptSession(const JID& from); + void requestSession(const JID& contact); + void cancelSession(const JID& from); + WhiteboardWindow* findWhiteboardWindow(const JID& contact); + + private: + std::map<JID, WhiteboardWindow*> whiteboardWindows_; + WhiteboardWindowFactory* whiteboardWindowFactory_; + UIEventStream* uiEventStream_; + NickResolver* nickResolver_; + boost::signals2::scoped_connection uiEventConnection_; + WhiteboardSessionManager* whiteboardSessionManager_; + }; } diff --git a/Swift/Controllers/XMLConsoleController.cpp b/Swift/Controllers/XMLConsoleController.cpp index d21f312..b72fde3 100644 --- a/Swift/Controllers/XMLConsoleController.cpp +++ b/Swift/Controllers/XMLConsoleController.cpp @@ -1,45 +1,45 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ -#include "Swift/Controllers/XMLConsoleController.h" +#include <Swift/Controllers/XMLConsoleController.h> -#include "Swift/Controllers/UIInterfaces/XMLConsoleWidgetFactory.h" -#include "Swift/Controllers/UIEvents/RequestXMLConsoleUIEvent.h" +#include <Swift/Controllers/UIEvents/RequestXMLConsoleUIEvent.h> +#include <Swift/Controllers/UIInterfaces/XMLConsoleWidgetFactory.h> namespace Swift { -XMLConsoleController::XMLConsoleController(UIEventStream* uiEventStream, XMLConsoleWidgetFactory* xmlConsoleWidgetFactory) : xmlConsoleWidgetFactory(xmlConsoleWidgetFactory), xmlConsoleWidget(NULL) { - uiEventStream->onUIEvent.connect(boost::bind(&XMLConsoleController::handleUIEvent, this, _1)); +XMLConsoleController::XMLConsoleController(UIEventStream* uiEventStream, XMLConsoleWidgetFactory* xmlConsoleWidgetFactory) : xmlConsoleWidgetFactory(xmlConsoleWidgetFactory), xmlConsoleWidget(nullptr) { + uiEventStream->onUIEvent.connect(boost::bind(&XMLConsoleController::handleUIEvent, this, _1)); } XMLConsoleController::~XMLConsoleController() { - delete xmlConsoleWidget; + delete xmlConsoleWidget; } -void XMLConsoleController::handleUIEvent(boost::shared_ptr<UIEvent> rawEvent) { - boost::shared_ptr<RequestXMLConsoleUIEvent> event = boost::dynamic_pointer_cast<RequestXMLConsoleUIEvent>(rawEvent); - if (event != NULL) { - if (xmlConsoleWidget == NULL) { - xmlConsoleWidget = xmlConsoleWidgetFactory->createXMLConsoleWidget(); - } - xmlConsoleWidget->show(); - xmlConsoleWidget->activate(); - } +void XMLConsoleController::handleUIEvent(std::shared_ptr<UIEvent> rawEvent) { + std::shared_ptr<RequestXMLConsoleUIEvent> event = std::dynamic_pointer_cast<RequestXMLConsoleUIEvent>(rawEvent); + if (event != nullptr) { + if (xmlConsoleWidget == nullptr) { + xmlConsoleWidget = xmlConsoleWidgetFactory->createXMLConsoleWidget(); + } + xmlConsoleWidget->show(); + xmlConsoleWidget->activate(); + } } void XMLConsoleController::handleDataRead(const SafeByteArray& data) { - if (xmlConsoleWidget) { - xmlConsoleWidget->handleDataRead(data); - } + if (xmlConsoleWidget) { + xmlConsoleWidget->handleDataRead(data); + } } void XMLConsoleController::handleDataWritten(const SafeByteArray& data) { - if (xmlConsoleWidget) { - xmlConsoleWidget->handleDataWritten(data); - } + if (xmlConsoleWidget) { + xmlConsoleWidget->handleDataWritten(data); + } } } diff --git a/Swift/Controllers/XMLConsoleController.h b/Swift/Controllers/XMLConsoleController.h index 6426a85..56202b4 100644 --- a/Swift/Controllers/XMLConsoleController.h +++ b/Swift/Controllers/XMLConsoleController.h @@ -1,37 +1,39 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include "Swiften/Base/boost_bsignals.h" +#include <memory> + #include <boost/bind.hpp> -#include <boost/shared_ptr.hpp> +#include <boost/signals2.hpp> -#include "Swift/Controllers/UIEvents/UIEventStream.h" #include <Swiften/Base/SafeByteArray.h> +#include <Swift/Controllers/UIEvents/UIEventStream.h> + namespace Swift { - - class XMLConsoleWidgetFactory; - class XMLConsoleWidget; - - class XMLConsoleController { - public: - XMLConsoleController(UIEventStream* uiEventStream, XMLConsoleWidgetFactory* xmlConsoleWidgetFactory); - ~XMLConsoleController(); - - public: - void handleDataRead(const SafeByteArray& data); - void handleDataWritten(const SafeByteArray& data); - - private: - void handleUIEvent(boost::shared_ptr<UIEvent> event); - - private: - XMLConsoleWidgetFactory* xmlConsoleWidgetFactory; - XMLConsoleWidget* xmlConsoleWidget; - }; + + class XMLConsoleWidgetFactory; + class XMLConsoleWidget; + + class XMLConsoleController { + public: + XMLConsoleController(UIEventStream* uiEventStream, XMLConsoleWidgetFactory* xmlConsoleWidgetFactory); + ~XMLConsoleController(); + + public: + void handleDataRead(const SafeByteArray& data); + void handleDataWritten(const SafeByteArray& data); + + private: + void handleUIEvent(std::shared_ptr<UIEvent> event); + + private: + XMLConsoleWidgetFactory* xmlConsoleWidgetFactory; + XMLConsoleWidget* xmlConsoleWidget; + }; } diff --git a/Swift/Controllers/XMPPEvents/ErrorEvent.h b/Swift/Controllers/XMPPEvents/ErrorEvent.h index ac09de9..c0b5e52 100644 --- a/Swift/Controllers/XMPPEvents/ErrorEvent.h +++ b/Swift/Controllers/XMPPEvents/ErrorEvent.h @@ -1,31 +1,32 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once #include <cassert> +#include <memory> +#include <string> -#include "Swiften/Base/boost_bsignals.h" -#include <boost/shared_ptr.hpp> +#include <boost/signals2.hpp> -#include "Swift/Controllers/XMPPEvents/StanzaEvent.h" -#include <string> -#include "Swiften/JID/JID.h" +#include <Swiften/JID/JID.h> + +#include <Swift/Controllers/XMPPEvents/StanzaEvent.h> namespace Swift { - class ErrorEvent : public StanzaEvent { - public: - ErrorEvent(const JID& jid, const std::string& text) : jid_(jid), text_(text){} - virtual ~ErrorEvent(){} - const JID& getJID() const {return jid_;} - const std::string& getText() const {return text_;} - - private: - JID jid_; - std::string text_; - }; + class ErrorEvent : public StanzaEvent { + public: + ErrorEvent(const JID& jid, const std::string& text) : jid_(jid), text_(text){} + virtual ~ErrorEvent(){} + const JID& getJID() const {return jid_;} + const std::string& getText() const {return text_;} + + private: + JID jid_; + std::string text_; + }; } diff --git a/Swift/Controllers/XMPPEvents/EventController.cpp b/Swift/Controllers/XMPPEvents/EventController.cpp index 8cb259b..0e9429d 100644 --- a/Swift/Controllers/XMPPEvents/EventController.cpp +++ b/Swift/Controllers/XMPPEvents/EventController.cpp @@ -1,20 +1,21 @@ /* - * Copyright (c) 2010-2012 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #include <Swift/Controllers/XMPPEvents/EventController.h> +#include <algorithm> + #include <boost/bind.hpp> #include <boost/numeric/conversion/cast.hpp> -#include <algorithm> -#include <Swiften/Base/foreach.h> -#include <Swift/Controllers/XMPPEvents/MessageEvent.h> #include <Swift/Controllers/XMPPEvents/ErrorEvent.h> -#include <Swift/Controllers/XMPPEvents/SubscriptionRequestEvent.h> +#include <Swift/Controllers/XMPPEvents/IncomingFileTransferEvent.h> #include <Swift/Controllers/XMPPEvents/MUCInviteEvent.h> +#include <Swift/Controllers/XMPPEvents/MessageEvent.h> +#include <Swift/Controllers/XMPPEvents/SubscriptionRequestEvent.h> namespace Swift { @@ -22,55 +23,56 @@ EventController::EventController() { } EventController::~EventController() { - foreach(boost::shared_ptr<StanzaEvent> event, events_) { - event->onConclusion.disconnect(boost::bind(&EventController::handleEventConcluded, this, event)); - } + for (auto&& event : events_) { + event->onConclusion.disconnect(boost::bind(&EventController::handleEventConcluded, this, event)); + } } -void EventController::handleIncomingEvent(boost::shared_ptr<StanzaEvent> sourceEvent) { - boost::shared_ptr<MessageEvent> messageEvent = boost::dynamic_pointer_cast<MessageEvent>(sourceEvent); - boost::shared_ptr<SubscriptionRequestEvent> subscriptionEvent = boost::dynamic_pointer_cast<SubscriptionRequestEvent>(sourceEvent); - boost::shared_ptr<ErrorEvent> errorEvent = boost::dynamic_pointer_cast<ErrorEvent>(sourceEvent); - boost::shared_ptr<MUCInviteEvent> mucInviteEvent = boost::dynamic_pointer_cast<MUCInviteEvent>(sourceEvent); +void EventController::handleIncomingEvent(std::shared_ptr<StanzaEvent> sourceEvent) { + std::shared_ptr<MessageEvent> messageEvent = std::dynamic_pointer_cast<MessageEvent>(sourceEvent); + std::shared_ptr<SubscriptionRequestEvent> subscriptionEvent = std::dynamic_pointer_cast<SubscriptionRequestEvent>(sourceEvent); + std::shared_ptr<ErrorEvent> errorEvent = std::dynamic_pointer_cast<ErrorEvent>(sourceEvent); + std::shared_ptr<MUCInviteEvent> mucInviteEvent = std::dynamic_pointer_cast<MUCInviteEvent>(sourceEvent); + std::shared_ptr<IncomingFileTransferEvent> incomingFileTransferEvent = std::dynamic_pointer_cast<IncomingFileTransferEvent>(sourceEvent); - /* If it's a duplicate subscription request, remove the previous request first */ - if (subscriptionEvent) { - EventList existingEvents(events_); - foreach(boost::shared_ptr<StanzaEvent> existingEvent, existingEvents) { - boost::shared_ptr<SubscriptionRequestEvent> existingSubscriptionEvent = boost::dynamic_pointer_cast<SubscriptionRequestEvent>(existingEvent); - if (existingSubscriptionEvent) { - if (existingSubscriptionEvent->getJID() == subscriptionEvent->getJID()) { - existingEvent->conclude(); - } - } - } - } + /* If it's a duplicate subscription request, remove the previous request first */ + if (subscriptionEvent) { + EventList existingEvents(events_); + for (auto&& existingEvent : existingEvents) { + std::shared_ptr<SubscriptionRequestEvent> existingSubscriptionEvent = std::dynamic_pointer_cast<SubscriptionRequestEvent>(existingEvent); + if (existingSubscriptionEvent) { + if (existingSubscriptionEvent->getJID() == subscriptionEvent->getJID()) { + existingEvent->conclude(); + } + } + } + } - if ((messageEvent && messageEvent->isReadable()) || subscriptionEvent || errorEvent || mucInviteEvent) { - events_.push_back(sourceEvent); - sourceEvent->onConclusion.connect(boost::bind(&EventController::handleEventConcluded, this, sourceEvent)); - onEventQueueLengthChange(boost::numeric_cast<int>(events_.size())); - onEventQueueEventAdded(sourceEvent); - if (sourceEvent->getConcluded()) { - handleEventConcluded(sourceEvent); - } - } + if ((messageEvent && messageEvent->isReadable()) || subscriptionEvent || errorEvent || mucInviteEvent || incomingFileTransferEvent) { + events_.push_back(sourceEvent); + sourceEvent->onConclusion.connect(boost::bind(&EventController::handleEventConcluded, this, sourceEvent)); + onEventQueueLengthChange(events_.size()); + onEventQueueEventAdded(sourceEvent); + if (sourceEvent->getConcluded()) { + handleEventConcluded(sourceEvent); + } + } } -void EventController::handleEventConcluded(boost::shared_ptr<StanzaEvent> event) { - event->onConclusion.disconnect(boost::bind(&EventController::handleEventConcluded, this, event)); - events_.erase(std::remove(events_.begin(), events_.end(), event), events_.end()); - onEventQueueLengthChange(boost::numeric_cast<int>(events_.size())); +void EventController::handleEventConcluded(std::shared_ptr<StanzaEvent> event) { + event->onConclusion.disconnect(boost::bind(&EventController::handleEventConcluded, this, event)); + events_.erase(std::remove(events_.begin(), events_.end(), event), events_.end()); + onEventQueueLengthChange(events_.size()); } void EventController::disconnectAll() { - onEventQueueLengthChange.disconnect_all_slots(); - onEventQueueEventAdded.disconnect_all_slots(); + onEventQueueLengthChange.disconnect_all_slots(); + onEventQueueEventAdded.disconnect_all_slots(); } void EventController::clear() { - events_.clear(); - onEventQueueLengthChange(0); + events_.clear(); + onEventQueueLengthChange(0); } } diff --git a/Swift/Controllers/XMPPEvents/EventController.h b/Swift/Controllers/XMPPEvents/EventController.h index 827dce8..5b746e4 100644 --- a/Swift/Controllers/XMPPEvents/EventController.h +++ b/Swift/Controllers/XMPPEvents/EventController.h @@ -1,40 +1,36 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ -#ifndef SWIFTEN_EventController_H -#define SWIFTEN_EventController_H +#pragma once - -#include "Swiften/Base/boost_bsignals.h" -#include <boost/shared_ptr.hpp> +#include <cstddef> +#include <memory> #include <vector> -#include "Swift/Controllers/XMPPEvents/StanzaEvent.h" -#include "Swift/Controllers/XMPPEvents/MessageEvent.h" - -namespace Swift { - typedef std::vector<boost::shared_ptr<StanzaEvent> > EventList; - class EventController { - public: - - EventController(); - ~EventController(); +#include <boost/signals2.hpp> - void handleIncomingEvent(boost::shared_ptr<StanzaEvent> sourceEvent); - boost::signal<void (int)> onEventQueueLengthChange; - boost::signal<void (boost::shared_ptr<StanzaEvent>)> onEventQueueEventAdded; - const EventList& getEvents() const {return events_;} - void disconnectAll(); - void clear(); +#include <Swift/Controllers/XMPPEvents/MessageEvent.h> +#include <Swift/Controllers/XMPPEvents/StanzaEvent.h> - private: - void handleEventConcluded(boost::shared_ptr<StanzaEvent> event); - EventList events_; - }; +namespace Swift { + typedef std::vector<std::shared_ptr<StanzaEvent> > EventList; + class EventController { + public: + EventController(); + ~EventController(); + + void handleIncomingEvent(std::shared_ptr<StanzaEvent> sourceEvent); + boost::signals2::signal<void (size_t)> onEventQueueLengthChange; + boost::signals2::signal<void (std::shared_ptr<StanzaEvent>)> onEventQueueEventAdded; + const EventList& getEvents() const {return events_;} + void disconnectAll(); + void clear(); + + private: + void handleEventConcluded(std::shared_ptr<StanzaEvent> event); + EventList events_; + }; } -#endif - - diff --git a/Swift/Controllers/XMPPEvents/IncomingFileTransferEvent.h b/Swift/Controllers/XMPPEvents/IncomingFileTransferEvent.h new file mode 100644 index 0000000..3d4303d --- /dev/null +++ b/Swift/Controllers/XMPPEvents/IncomingFileTransferEvent.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2015-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#pragma once + +#include <memory> + +#include <Swiften/JID/JID.h> + +#include <Swift/Controllers/XMPPEvents/StanzaEvent.h> + +namespace Swift { + class IncomingFileTransferEvent : public StanzaEvent { + public: + typedef std::shared_ptr<IncomingFileTransferEvent> ref; + + IncomingFileTransferEvent(const JID& sender) : sender_(sender) {} + + const JID& getSender() const { + return sender_; + } + + private: + JID sender_; + }; +} + diff --git a/Swift/Controllers/XMPPEvents/MUCInviteEvent.h b/Swift/Controllers/XMPPEvents/MUCInviteEvent.h index 65ecece..4cdbbff 100644 --- a/Swift/Controllers/XMPPEvents/MUCInviteEvent.h +++ b/Swift/Controllers/XMPPEvents/MUCInviteEvent.h @@ -4,30 +4,43 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ +/* + * Copyright (c) 2015-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + #pragma once +#include <memory> +#include <string> + +#include <Swiften/JID/JID.h> + +#include <Swift/Controllers/XMPPEvents/StanzaEvent.h> + namespace Swift { - class MUCInviteEvent : public StanzaEvent { - public: - typedef boost::shared_ptr<MUCInviteEvent> ref; - - public: - MUCInviteEvent(const JID& inviter, const JID& roomJID, const std::string& reason, const std::string& password, bool direct, bool impromptu) : inviter_(inviter), roomJID_(roomJID), reason_(reason), password_(password), direct_(direct), impromptu_(impromptu) {} - - const JID& getInviter() const { return inviter_; } - const JID& getRoomJID() const { return roomJID_; } - const std::string& getReason() const { return reason_; } - const std::string& getPassword() const { return password_; } - bool getDirect() const { return direct_; } - bool getImpromptu() const { return impromptu_; } - - private: - JID inviter_; - JID roomJID_; - std::string reason_; - std::string password_; - bool direct_; - bool impromptu_; - }; + class MUCInviteEvent : public StanzaEvent { + public: + typedef std::shared_ptr<MUCInviteEvent> ref; + + public: + MUCInviteEvent(const JID& inviter, const JID& roomJID, const std::string& reason, const std::string& password, bool direct, bool impromptu) : inviter_(inviter), roomJID_(roomJID), reason_(reason), password_(password), direct_(direct), impromptu_(impromptu) {} + + const JID& getInviter() const { return inviter_; } + const JID& getRoomJID() const { return roomJID_; } + const std::string& getReason() const { return reason_; } + const std::string& getPassword() const { return password_; } + bool getDirect() const { return direct_; } + bool getImpromptu() const { return impromptu_; } + + private: + JID inviter_; + JID roomJID_; + std::string reason_; + std::string password_; + bool direct_; + bool impromptu_; + }; } diff --git a/Swift/Controllers/XMPPEvents/MessageEvent.h b/Swift/Controllers/XMPPEvents/MessageEvent.h index a9214f5..12f4c48 100644 --- a/Swift/Controllers/XMPPEvents/MessageEvent.h +++ b/Swift/Controllers/XMPPEvents/MessageEvent.h @@ -1,46 +1,67 @@ /* - * Copyright (c) 2010-2012 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2017 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ -#pragma once +#pragma once + #include <cassert> +#include <memory> -#include <boost/shared_ptr.hpp> +#include <Swiften/Elements/Message.h> #include <Swift/Controllers/XMPPEvents/StanzaEvent.h> -#include <Swiften/Elements/Message.h> namespace Swift { - class MessageEvent : public StanzaEvent { - public: - typedef boost::shared_ptr<MessageEvent> ref; + class MessageEvent : public StanzaEvent { + public: + class SystemNotification { + public: + SystemNotification(const std::string& title, const std::string& message) : title(title), message(message) { + } + + public: + std::string title; + std::string message; + }; + + public: + typedef std::shared_ptr<MessageEvent> ref; + + MessageEvent(std::shared_ptr<Message> stanza) : stanza_(stanza), targetsMe_(true) {} + + std::shared_ptr<Message> getStanza() {return stanza_;} - MessageEvent(boost::shared_ptr<Message> stanza) : stanza_(stanza), targetsMe_(true) {} + bool isReadable() { + return getStanza()->isError() || !getStanza()->getBody().get_value_or("").empty(); + } - boost::shared_ptr<Message> getStanza() {return stanza_;} + void addNotification(const std::string& title, const std::string& message) { + systemNotifications_.push_back(SystemNotification(title, message)); + } - bool isReadable() { - return getStanza()->isError() || !getStanza()->getBody().empty(); - } + const std::vector<SystemNotification>& getNotifications() const { + return systemNotifications_; + } - void read() { - assert (isReadable()); - conclude(); - } + void read() { + assert (isReadable()); + conclude(); + } - void setTargetsMe(bool targetsMe) { - targetsMe_ = targetsMe; - } + void setTargetsMe(bool targetsMe) { + targetsMe_ = targetsMe; + } - bool targetsMe() const { - return targetsMe_; - } + bool targetsMe() const { + return targetsMe_; + } - private: - boost::shared_ptr<Message> stanza_; - bool targetsMe_; - }; + private: + std::shared_ptr<Message> stanza_; + std::vector<SystemNotification> systemNotifications_; + bool targetsMe_; + }; } diff --git a/Swift/Controllers/XMPPEvents/StanzaEvent.h b/Swift/Controllers/XMPPEvents/StanzaEvent.h index a15afc1..56c4ea3 100644 --- a/Swift/Controllers/XMPPEvents/StanzaEvent.h +++ b/Swift/Controllers/XMPPEvents/StanzaEvent.h @@ -1,29 +1,29 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <boost/shared_ptr.hpp> -#include <boost/date_time/posix_time/posix_time.hpp> +#include <memory> -#include "Swiften/Base/boost_bsignals.h" +#include <boost/date_time/posix_time/posix_time.hpp> +#include <boost/signals2.hpp> namespace Swift { - class StanzaEvent { - public: - StanzaEvent() : time_(boost::posix_time::microsec_clock::universal_time()) {concluded_ = false;} - virtual ~StanzaEvent() {} - void conclude() {concluded_ = true; onConclusion();} - /** Do not call this directly from outside the class. - * If you connect to this signal, you *must* disconnect from it manually. */ - boost::signal<void()> onConclusion; - bool getConcluded() {return concluded_;} - boost::posix_time::ptime getTime() {return time_;} - private: - bool concluded_; - boost::posix_time::ptime time_; - }; + class StanzaEvent { + public: + StanzaEvent() : time_(boost::posix_time::microsec_clock::universal_time()) {concluded_ = false;} + virtual ~StanzaEvent() {} + void conclude() {concluded_ = true; onConclusion();} + /** Do not call this directly from outside the class. + * If you connect to this signal, you *must* disconnect from it manually. */ + boost::signals2::signal<void()> onConclusion; + bool getConcluded() {return concluded_;} + boost::posix_time::ptime getTime() {return time_;} + private: + bool concluded_; + boost::posix_time::ptime time_; + }; } diff --git a/Swift/Controllers/XMPPEvents/SubscriptionRequestEvent.h b/Swift/Controllers/XMPPEvents/SubscriptionRequestEvent.h index fb7a05e..8e3fd32 100644 --- a/Swift/Controllers/XMPPEvents/SubscriptionRequestEvent.h +++ b/Swift/Controllers/XMPPEvents/SubscriptionRequestEvent.h @@ -1,46 +1,47 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once #include <cassert> +#include <memory> +#include <string> -#include "Swiften/Base/boost_bsignals.h" -#include <boost/shared_ptr.hpp> +#include <boost/signals2.hpp> -#include "Swift/Controllers/XMPPEvents/StanzaEvent.h" -#include <string> -#include "Swiften/JID/JID.h" +#include <Swiften/JID/JID.h> + +#include <Swift/Controllers/XMPPEvents/StanzaEvent.h> namespace Swift { - class SubscriptionRequestEvent : public StanzaEvent { - public: - SubscriptionRequestEvent(const JID& jid, const std::string& reason) : jid_(jid), reason_(reason){} - virtual ~SubscriptionRequestEvent(){} - const JID& getJID() const {return jid_;} - const std::string& getReason() const {return reason_;} - boost::signal<void()> onAccept; - boost::signal<void()> onDecline; - void accept() { - onAccept(); - conclude(); - } - - void decline() { - onDecline(); - conclude(); - } - - void defer() { - conclude(); - } - - private: - JID jid_; - std::string reason_; - }; + class SubscriptionRequestEvent : public StanzaEvent { + public: + SubscriptionRequestEvent(const JID& jid, const std::string& reason) : jid_(jid), reason_(reason){} + virtual ~SubscriptionRequestEvent(){} + const JID& getJID() const {return jid_;} + const std::string& getReason() const {return reason_;} + boost::signals2::signal<void()> onAccept; + boost::signals2::signal<void()> onDecline; + void accept() { + onAccept(); + conclude(); + } + + void decline() { + onDecline(); + conclude(); + } + + void defer() { + conclude(); + } + + private: + JID jid_; + std::string reason_; + }; } diff --git a/Swift/Controllers/XMPPURIController.cpp b/Swift/Controllers/XMPPURIController.cpp index 00759b8..aaebd56 100644 --- a/Swift/Controllers/XMPPURIController.cpp +++ b/Swift/Controllers/XMPPURIController.cpp @@ -1,38 +1,40 @@ /* - * Copyright (c) 2011 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2011-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #include <Swift/Controllers/XMPPURIController.h> +#include <memory> + #include <boost/bind.hpp> -#include <boost/smart_ptr/make_shared.hpp> + +#include <Swift/Controllers/UIEvents/RequestChatUIEvent.h> +#include <Swift/Controllers/UIEvents/RequestJoinMUCUIEvent.h> +#include <Swift/Controllers/UIEvents/UIEventStream.h> #include <SwifTools/URIHandler/URIHandler.h> #include <SwifTools/URIHandler/XMPPURI.h> -#include <Swift/Controllers/UIEvents/UIEventStream.h> -#include <Swift/Controllers/UIEvents/RequestJoinMUCUIEvent.h> -#include <Swift/Controllers/UIEvents/RequestChatUIEvent.h> using namespace Swift; XMPPURIController::XMPPURIController(URIHandler* uriHandler, UIEventStream* uiEventStream) : uriHandler(uriHandler), uiEventStream(uiEventStream) { - uriHandler->onURI.connect(boost::bind(&XMPPURIController::handleURI, this, _1)); + uriHandler->onURI.connect(boost::bind(&XMPPURIController::handleURI, this, _1)); } XMPPURIController::~XMPPURIController() { - uriHandler->onURI.disconnect(boost::bind(&XMPPURIController::handleURI, this, _1)); + uriHandler->onURI.disconnect(boost::bind(&XMPPURIController::handleURI, this, _1)); } void XMPPURIController::handleURI(const std::string& s) { - XMPPURI uri = XMPPURI::fromString(s); - if (!uri.isNull()) { - if (uri.getQueryType() == "join") { - uiEventStream->send(boost::make_shared<RequestJoinMUCUIEvent>(uri.getPath())); - } - else { - uiEventStream->send(boost::make_shared<RequestChatUIEvent>(uri.getPath())); - } - } + XMPPURI uri = XMPPURI::fromString(s); + if (!uri.isNull()) { + if (uri.getQueryType() == "join") { + uiEventStream->send(std::make_shared<RequestJoinMUCUIEvent>(uri.getPath())); + } + else { + uiEventStream->send(std::make_shared<RequestChatUIEvent>(uri.getPath())); + } + } } diff --git a/Swift/Controllers/XMPPURIController.h b/Swift/Controllers/XMPPURIController.h index 54534d4..941441f 100644 --- a/Swift/Controllers/XMPPURIController.h +++ b/Swift/Controllers/XMPPURIController.h @@ -1,32 +1,33 @@ /* - * Copyright (c) 2011 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2011-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once #include <string> -#include <Swiften/Base/boost_bsignals.h> + +#include <boost/signals2.hpp> namespace Swift { - class URIHandler; - class JID; - class UIEventStream; + class URIHandler; + class JID; + class UIEventStream; - class XMPPURIController { - public: - XMPPURIController(URIHandler* uriHandler, UIEventStream* uiEventStream); - ~XMPPURIController(); + class XMPPURIController { + public: + XMPPURIController(URIHandler* uriHandler, UIEventStream* uiEventStream); + ~XMPPURIController(); - boost::signal<void (const JID&)> onStartChat; - boost::signal<void (const JID&)> onJoinMUC; + boost::signals2::signal<void (const JID&)> onStartChat; + boost::signals2::signal<void (const JID&)> onJoinMUC; - private: - void handleURI(const std::string&); + private: + void handleURI(const std::string&); - private: - URIHandler* uriHandler; - UIEventStream* uiEventStream; - }; + private: + URIHandler* uriHandler; + UIEventStream* uiEventStream; + }; } |