/* * Copyright (c) 2012 Yoann Blein * Licensed under the simplified BSD license. * See Documentation/Licenses/BSD-simplified.txt for more information. */ #include "ScreenSharingController.h" #include #include #include #include #include #include #include "Swift/Controllers/ScreenSharing/DesktopScreenGrabber.h" #include "Swift/Controllers/UIInterfaces/RemoteScreenWindowFactory.h" #include "Swift/Controllers/UIInterfaces/RemoteScreenWindow.h" #include "Swift/Controllers/UIInterfaces/ChatWindow.h" #include "Swift/Controllers/Intl.h" #include namespace Swift { ScreenSharingController::ScreenSharingController(ScreenSharingManager* screenSharingManager, TimerFactory* timerFactory, DesktopScreenGrabber* desktopScreenGrabber, const JID& to) : screenGrabber(desktopScreenGrabber), grabTimer(timerFactory->createTimer(500)), remoteScreenWindowFactory(0), remoteScreenWindow(0), otherParty(to), incoming(false), chatWindow(0) { OutgoingScreenSharing::ref oss = screenSharingManager->createOutgoingScreenSharing(to); if (oss) { oss->onReady.connect(boost::bind(&ScreenSharingController::handleReady, this)); oss->onFinished.connect(boost::bind(&ScreenSharingController::handleFinished, this)); oss->onStateChange.connect(boost::bind(&ScreenSharingController::handleStateChange, this, _1)); const Image& image = screenGrabber->grab(); oss->start(image.width, image.height); screenSharing = oss; } else { std::cerr << "Screen sharing not supported!" << std::endl; } } ScreenSharingController::ScreenSharingController(boost::shared_ptr screenSharing, RemoteScreenWindowFactory* remoteScreenWindowFactory) : screenGrabber(0), remoteScreenWindowFactory(remoteScreenWindowFactory), remoteScreenWindow(0), screenSharing(screenSharing), otherParty(screenSharing->getSender()), incoming(true), chatWindow(0) { screenSharing->onFinished.connect(boost::bind(&ScreenSharingController::handleFinished, this)); screenSharing->onStateChange.connect(boost::bind(&ScreenSharingController::handleStateChange, this, _1)); } ScreenSharingController::~ScreenSharingController() { screenSharing->onStateChange.disconnect(boost::bind(&ScreenSharingController::handleStateChange, this, _1)); delete remoteScreenWindow; } const JID& ScreenSharingController::getOtherParty() const { return otherParty; } std::string ScreenSharingController::setChatWindow(ChatWindow* wnd, std::string nickname) { chatWindow = wnd; uiID = wnd->addScreenSharing((incoming ? nickname : QT_TRANSLATE_NOOP("", "me")), incoming); return uiID; } void ScreenSharingController::accept() { if (incoming) { if (IncomingScreenSharing::ref iss = boost::dynamic_pointer_cast(screenSharing)) { iss->accept(); remoteScreenWindow = remoteScreenWindowFactory->createRemoteScreenViewer(iss); remoteScreenWindow->onStopRequest.connect(boost::bind(&ScreenSharingController::handleWindowStopRequest, this)); } } } void ScreenSharingController::cancel() { screenSharing->cancel(); } void ScreenSharingController::stop() { screenSharing->stop(); } void ScreenSharingController::handleGrabTimerTick() { if (screenSharing) { if (OutgoingScreenSharing::ref oss = boost::dynamic_pointer_cast(screenSharing)) { grabTimer->start(); oss->addImage(screenGrabber->grab()); } } else { grabTimer->onTick.disconnect(boost::bind(&ScreenSharingController::handleGrabTimerTick, this)); } } void ScreenSharingController::handleReady() { if (OutgoingScreenSharing::ref oss = boost::dynamic_pointer_cast(screenSharing)) { oss->onReady.disconnect(boost::bind(&ScreenSharingController::handleReady, this)); grabTimer->onTick.connect(boost::bind(&ScreenSharingController::handleGrabTimerTick, this)); handleGrabTimerTick(); } } void ScreenSharingController::handleFinished() { screenSharing->onFinished.disconnect(boost::bind(&ScreenSharingController::handleFinished, this)); screenSharing.reset(); delete remoteScreenWindow; remoteScreenWindow = 0; } void ScreenSharingController::handleStateChange(ScreenSharing::SCState state) { if (chatWindow) chatWindow->setScreenSharingStatus(uiID, state); } void ScreenSharingController::handleWindowStopRequest() { remoteScreenWindow->onStopRequest.disconnect(boost::bind(&ScreenSharingController::handleWindowStopRequest, this)); screenSharing->stop(); } }