summaryrefslogtreecommitdiffstats
blob: b932765f5a75d956616c92ae27a8b3328404aa94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * Copyright (c) 2012 Yoann Blein
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

#include "ScreenSharingController.h"

#include <Swiften/ScreenSharing/ScreenSharingManager.h>
#include <Swiften/ScreenSharing/IncomingScreenSharing.h>
#include <Swiften/ScreenSharing/OutgoingScreenSharing.h>
#include <Swiften/ScreenSharing/Image.h>
#include <Swiften/Network/TimerFactory.h>
#include <Swiften/Network/Timer.h>
#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 <boost/bind.hpp>

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<IncomingScreenSharing> 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<IncomingScreenSharing>(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<OutgoingScreenSharing>(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<OutgoingScreenSharing>(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();
}

}