1 /**
2 * Whiteboard session manager
3 *
4 * Authors:
5 * David Yip <yipdw@rose-hulman.edu>
6 * Bob Jamison (Pedro port)
7 *
8 * Copyright (c) 2005 Authors
9 *
10 * Released under GNU GPL, read the file 'COPYING' for more information
11 */
13 #include <functional>
14 #include <algorithm>
15 #include <iostream>
16 #include <time.h>
18 #include <gtkmm.h>
19 #include <glibmm/i18n.h>
21 #include "xml/node.h"
22 #include "xml/repr.h"
24 #include "util/ucompose.hpp"
26 #include "xml/node-observer.h"
28 #include "pedro/pedrodom.h"
30 #include "ui/view/view-widget.h"
32 #include "application/application.h"
33 #include "application/editor.h"
35 #include "document-private.h"
36 #include "interface.h"
37 #include "sp-namedview.h"
38 #include "document.h"
39 #include "desktop.h"
40 #include "desktop-handles.h"
42 #include "jabber_whiteboard/invitation-confirm-dialog.h"
43 #include "jabber_whiteboard/message-verifier.h"
44 #include "jabber_whiteboard/session-manager.h"
45 #include "jabber_whiteboard/inkboard-document.h"
46 #include "jabber_whiteboard/defines.h"
48 #include "jabber_whiteboard/dialog/choose-desktop.h"
50 namespace Inkscape {
52 namespace Whiteboard {
54 //#########################################################################
55 //# S E S S I O N M A N A G E R
56 //#########################################################################
58 SessionManager *sessionManagerInstance = NULL;
60 void SessionManager::showClient()
61 {
62 SessionManager::instance().gui.show();
63 }
65 SessionManager&
66 SessionManager::instance()
67 {
68 if (!sessionManagerInstance)
69 sessionManagerInstance = new SessionManager();
70 return *sessionManagerInstance;
71 }
73 SessionManager::SessionManager()
74 {
75 getClient().addXmppEventListener(*this);
77 this->CheckPendingInvitations =
78 Glib::signal_timeout().connect(sigc::mem_fun(
79 *this, &SessionManager::checkInvitationQueue), 50);
80 }
82 SessionManager::~SessionManager()
83 {
84 getClient().removeXmppEventListener(*this);
85 getClient().disconnect();
86 }
88 /**
89 * Initiates a shared session with a user or conference room.
90 *
91 * \param to The recipient to which this desktop will be linked, specified as a JID.
92 * \param type Type of the session; i.e. private message or group chat.
93 */
94 void
95 SessionManager::initialiseSession(Glib::ustring const& to, State::SessionType type)
96 {
98 SPDocument* doc = makeInkboardDocument(g_quark_from_static_string("xml"), "svg:svg", type, to);
99 InkboardDocument* inkdoc = dynamic_cast< InkboardDocument* >(doc->rdoc);
100 if(inkdoc == NULL) return;
102 if(type == State::WHITEBOARD_PEER)
103 {
104 ChooseDesktop dialog;
105 int result = dialog.run();
107 if(result == Gtk::RESPONSE_OK)
108 {
109 SPDesktop *desktop = dialog.getDesktop();
111 if(desktop != NULL)
112 {
113 Inkscape::XML::Document *old_doc =
114 sp_desktop_document(desktop)->rdoc;
115 inkdoc->root()->mergeFrom(old_doc->root(),"id");
116 }
117 }else { return; }
118 }
120 char * sessionId = createSessionId(10);
122 inkdoc->setSessionId(sessionId);
124 makeInkboardDesktop(doc);
125 addSession(WhiteboardRecord(sessionId, inkdoc));
127 inkdoc->startSessionNegotiation();
130 }
132 void
133 SessionManager::terminateSession(Glib::ustring const& sessionId)
134 {
135 WhiteboardList::iterator i = whiteboards.begin();
136 for(; i != whiteboards.end(); ++i) {
137 if ((*i).first == sessionId)
138 break;
139 }
141 if (i != whiteboards.end()) {
142 (*i).second->terminateSession();
143 whiteboards.erase(i);
144 }
145 }
147 void
148 SessionManager::addSession(WhiteboardRecord whiteboard)
149 {
150 whiteboards.push_back(whiteboard);
151 }
153 InkboardDocument*
154 SessionManager::getInkboardSession(Glib::ustring const& sessionId)
155 {
156 WhiteboardList::iterator i = whiteboards.begin();
157 for(; i != whiteboards.end(); ++i) {
158 if ((*i).first == sessionId) {
159 return (*i).second;
160 }
161 }
162 return NULL;
163 }
165 void
166 SessionManager::processXmppEvent(const Pedro::XmppEvent &event)
167 {
168 int type = event.getType();
170 switch (type) {
171 case Pedro::XmppEvent::EVENT_STATUS:
172 {
173 break;
174 }
175 case Pedro::XmppEvent::EVENT_ERROR:
176 {
177 break;
178 }
179 case Pedro::XmppEvent::EVENT_CONNECTED:
180 {
181 break;
182 }
183 case Pedro::XmppEvent::EVENT_DISCONNECTED:
184 {
185 break;
186 }
187 case Pedro::XmppEvent::EVENT_MUC_MESSAGE:
188 case Pedro::XmppEvent::EVENT_MESSAGE:
189 {
190 Pedro::Element *root = event.getDOM();
192 if (root && root->getTagAttribute("wb", "xmlns") == Vars::INKBOARD_XMLNS)
193 processWhiteboardEvent(event);
195 break;
196 }
197 case Pedro::XmppEvent::EVENT_PRESENCE:
198 {
199 break;
200 }
201 case Pedro::XmppEvent::EVENT_MUC_JOIN:
202 {
203 break;
204 }
205 case Pedro::XmppEvent::EVENT_MUC_LEAVE:
206 {
207 break;
208 }
209 case Pedro::XmppEvent::EVENT_MUC_PRESENCE:
210 {
211 break;
212 }
213 default:
214 {
215 break;
216 }
217 }
218 }
220 /**
221 * Handles all incoming messages from pedro within a valid namespace, CONNECT_REQUEST messages
222 * are handled here, as they have no InkboardDocument to be handled from, all other messages
223 * are passed to their appropriate Inkboard document, which is identified by the 'session'
224 * attribute of the 'wb' element
225 *
226 */
227 void
228 SessionManager::processWhiteboardEvent(Pedro::XmppEvent const& event)
229 {
230 Pedro::Element* root = event.getDOM();
231 if (root == NULL) {
232 g_warning("Received null DOM; ignoring message.");
233 return;
234 }
236 Pedro::DOMString session = root->getTagAttribute("wb", "session");
237 Pedro::DOMString type = root->getTagAttribute("message", "type");
238 Pedro::DOMString domwrapper = root->getFirstChild()->getFirstChild()->getFirstChild()->getName();
240 if (session.empty()) {
241 g_warning("Received incomplete Whiteboard message, missing session identifier; ignoring message.");
242 return;
243 }
245 if(root->exists(Message::CONNECT_REQUEST) && type == State::WHITEBOARD_PEER)
246 {
247 handleIncomingInvitation(Invitation(event.getFrom(),session));
249 }else
250 {
251 Message::Wrapper wrapper = static_cast< Message::Wrapper >(domwrapper);
252 InkboardDocument* doc = getInkboardSession(session);
254 if(doc != NULL)
255 doc->recieve(wrapper, root->getFirstChild());
256 }
257 }
259 char*
260 SessionManager::createSessionId(int size)
261 {
262 // Create a random session identifier
263 char * randomString = (char*) malloc (size);
264 for (int n=0; n<size; n++)
265 randomString[n]=rand()%26+'a';
266 randomString[size+1]='\0';
268 return randomString;
269 }
271 /**
272 * Adds an invitation to a queue to be executed in SessionManager::_checkInvitationQueue()
273 * as when this method is called, we're still executing in Pedro's context, which causes
274 * issues when we run a dialog main loop.
275 *
276 */
277 void
278 SessionManager::handleIncomingInvitation(Invitation invitation)
279 {
280 // don't insert duplicate invitations
281 if (std::find(invitations.begin(),invitations.end(),invitation) != invitations.end())
282 return;
284 invitations.push_back(invitation);
286 }
288 bool
289 SessionManager::checkInvitationQueue()
290 {
291 // The user is currently busy with an action. Defer invitation processing
292 // until the user is free.
293 int x, y;
294 Gdk::ModifierType mt;
295 Gdk::Display::get_default()->get_pointer(x, y, mt);
296 if (mt & GDK_BUTTON1_MASK)
297 return true;
299 if (invitations.size() > 0)
300 {
301 // There's an invitation to process; process it.
302 Invitation invitation = invitations.front();
303 Glib::ustring from = invitation.first;
304 Glib::ustring sessionId = invitation.second;
306 Glib::ustring primary =
307 "<span weight=\"bold\" size=\"larger\">" +
308 String::ucompose(_("<b>%1</b> has invited you to a whiteboard session."), from) +
309 "</span>\n\n" +
310 String::ucompose(_("Do you wish to accept <b>%1</b>'s whiteboard session invitation?"), from);
312 InvitationConfirmDialog dialog(primary);
314 dialog.add_button(_("Accept invitation"), Dialog::ACCEPT_INVITATION);
315 dialog.add_button(_("Decline invitation"), Dialog::DECLINE_INVITATION);
317 Dialog::DialogReply reply = static_cast< Dialog::DialogReply >(dialog.run());
320 SPDocument* doc = makeInkboardDocument(g_quark_from_static_string("xml"), "svg:svg", State::WHITEBOARD_PEER, from);
322 InkboardDocument* inkdoc = dynamic_cast< InkboardDocument* >(doc->rdoc);
323 if(inkdoc == NULL) return true;
325 inkdoc->handleState(State::INITIAL,State::CONNECTING);
326 inkdoc->setSessionId(sessionId);
327 addSession(WhiteboardRecord(sessionId, inkdoc));
329 switch (reply) {
331 case Dialog::ACCEPT_INVITATION:{
332 inkdoc->send(from, Message::PROTOCOL,Message::ACCEPT_INVITATION);
333 makeInkboardDesktop(doc);
334 break; }
336 case Dialog::DECLINE_INVITATION: default: {
337 inkdoc->send(from, Message::PROTOCOL,Message::DECLINE_INVITATION);
338 terminateSession(sessionId);
339 break; }
340 }
342 invitations.pop_front();
344 }
346 return true;
347 }
349 //#########################################################################
350 //# HELPER FUNCTIONS
351 //#########################################################################
353 SPDocument*
354 makeInkboardDocument(int code, gchar const* rootname, State::SessionType type, Glib::ustring const& to)
355 {
356 SPDocument* doc;
358 InkboardDocument* rdoc = new InkboardDocument(g_quark_from_static_string("xml"), type, to);
359 rdoc->setAttribute("version", "1.0");
360 rdoc->setAttribute("standalone", "no");
361 XML::Node *comment = rdoc->createComment(" Created with Inkscape (http://www.inkscape.org/) ");
362 rdoc->appendChild(comment);
363 GC::release(comment);
365 XML::Node* root = rdoc->createElement(rootname);
366 rdoc->appendChild(root);
367 GC::release(root);
369 Glib::ustring name = String::ucompose(
370 _("Inkboard session (%1 to %2)"), SessionManager::instance().getClient().getJid(), to);
372 doc = sp_document_create(rdoc, NULL, NULL, name.c_str(), TRUE);
373 g_return_val_if_fail(doc != NULL, NULL);
375 return doc;
376 }
378 // TODO: When the switchover to the new GUI is complete, this function should go away
379 // and be replaced with a call to Inkscape::NSApplication::Editor::createDesktop.
380 // It currently only exists to correctly mimic the desktop creation functionality
381 // in file.cpp.
382 //
383 // \see sp_file_new
384 SPDesktop*
385 makeInkboardDesktop(SPDocument* doc)
386 {
387 SPDesktop* dt;
389 if (NSApplication::Application::getNewGui())
390 dt = NSApplication::Editor::createDesktop(doc);
392 else
393 {
394 SPViewWidget *dtw = sp_desktop_widget_new(sp_document_namedview(doc, NULL));
395 g_return_val_if_fail(dtw != NULL, NULL);
396 sp_document_unref(doc);
398 sp_create_window(dtw, TRUE);
399 dt = static_cast<SPDesktop*>(dtw->view);
400 sp_namedview_window_from_document(dt);
401 sp_namedview_update_layers_from_document(dt);
402 }
404 return dt;
405 }
407 } // namespace Whiteboard
409 } // namespace Inkscape
412 /*
413 Local Variables:
414 mode:c++
415 c-file-style:"stroustrup"
416 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
417 indent-tabs-mode:nil
418 fill-column:99
419 End:
420 */
421 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :