Code

empty messages are passed onto all listeners in pedro, gui ignores such messages
[inkscape.git] / src / jabber_whiteboard / session-manager.cpp
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();
132 void
133 SessionManager::terminateSession(Glib::ustring const& sessionId)
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     }
147 void
148 SessionManager::addSession(WhiteboardRecord whiteboard)
150     whiteboards.push_back(whiteboard);
153 InkboardDocument*
154 SessionManager::getInkboardSession(Glib::ustring const& sessionId)
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;
165 void
166 SessionManager::processXmppEvent(const Pedro::XmppEvent &event)
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     }
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)
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 domwrapper = root->getFirstChild()->getFirstChild()->getFirstChild()->getName();
239     if (session.empty()) {
240         g_warning("Received incomplete Whiteboard message, missing session identifier; ignoring message.");
241         return;
242     }
244     if(root->exists(Message::CONNECT_REQUEST))
245         handleIncomingInvitation(Invitation(event.getFrom(),session));
247     else
248     { 
249         Message::Wrapper wrapper = static_cast< Message::Wrapper >("protocol");
250         InkboardDocument* doc = getInkboardSession(session);
251         doc->processInkboardEvent(wrapper, root->getFirstChild());
252     }
255 char*
256 SessionManager::createSessionId(int size)
258     // Create a random session identifier
259     char * randomString = (char*) malloc (size);
260     for (int n=0; n<size; n++)
261         randomString[n]=rand()%26+'a';
262     randomString[size+1]='\0';
264     return randomString;
267 /**
268  * Adds an invitation to a queue to be executed in SessionManager::_checkInvitationQueue()
269  * as when this method is called, we're still executing in Pedro's context, which causes 
270  * issues when we run a dialog main loop.
271  *
272  */
273 void
274 SessionManager::handleIncomingInvitation(Invitation invitation)
276     // don't insert duplicate invitations
277     if (std::find(invitations.begin(),invitations.end(),invitation) != invitations.end())
278         return;
280     invitations.push_back(invitation);
284 bool
285 SessionManager::checkInvitationQueue()
287     // The user is currently busy with an action.  Defer invitation processing 
288     // until the user is free.
289     int x, y;
290     Gdk::ModifierType mt;
291     Gdk::Display::get_default()->get_pointer(x, y, mt);
292     if (mt & GDK_BUTTON1_MASK) 
293         return true;
295     if (invitations.size() > 0) 
296     {
297         // There's an invitation to process; process it.
298         Invitation invitation = invitations.front();
299         Glib::ustring from = invitation.first;
300         Glib::ustring sessionId = invitation.second;
302         Glib::ustring primary = 
303             "<span weight=\"bold\" size=\"larger\">" + 
304             String::ucompose(_("<b>%1</b> has invited you to a whiteboard session."), from) + 
305             "</span>\n\n" + 
306             String::ucompose(_("Do you wish to accept <b>%1</b>'s whiteboard session invitation?"), from);
308         InvitationConfirmDialog dialog(primary);
310         dialog.add_button(_("Accept invitation"), Dialog::ACCEPT_INVITATION);
311         dialog.add_button(_("Decline invitation"), Dialog::DECLINE_INVITATION);
313         Dialog::DialogReply reply = static_cast< Dialog::DialogReply >(dialog.run());
316         SPDocument* doc = makeInkboardDocument(g_quark_from_static_string("xml"), "svg:svg", State::WHITEBOARD_PEER, from);
318         InkboardDocument* inkdoc = dynamic_cast< InkboardDocument* >(doc->rdoc);
319         if(inkdoc == NULL) return true;
321         inkdoc->handleState(State::INITIAL,State::CONNECTING);
322         inkdoc->setSessionId(sessionId);
323         addSession(WhiteboardRecord(sessionId, inkdoc));
325         switch (reply) {
327             case Dialog::ACCEPT_INVITATION:{
328                 inkdoc->sendProtocol(from, Message::PROTOCOL,Message::ACCEPT_INVITATION);
329                 makeInkboardDesktop(doc);
330                 break; }
332             case Dialog::DECLINE_INVITATION: default: {
333                 inkdoc->sendProtocol(from, Message::PROTOCOL,Message::DECLINE_INVITATION);
334                 terminateSession(sessionId);
335                 break; }
336         }
338         invitations.pop_front();
340     }
342     return true;
345 //#########################################################################
346 //# HELPER FUNCTIONS
347 //#########################################################################
349 SPDocument*
350 makeInkboardDocument(int code, gchar const* rootname, State::SessionType type, Glib::ustring const& to)
352     SPDocument* doc;
354     InkboardDocument* rdoc = new InkboardDocument(g_quark_from_static_string("xml"), type, to);
355     rdoc->setAttribute("version", "1.0");
356     rdoc->setAttribute("standalone", "no");
357     XML::Node *comment = sp_repr_new_comment(" Created with Inkscape (http://www.inkscape.org/) ");
358     rdoc->appendChild(comment);
359     GC::release(comment);
361     XML::Node* root = sp_repr_new(rootname);
362     rdoc->appendChild(root);
363     GC::release(root);
365     Glib::ustring name = String::ucompose(
366         _("Inkboard session (%1 to %2)"), SessionManager::instance().getClient().getJid(), to);
368     doc = sp_document_create(rdoc, NULL, NULL, name.c_str(), TRUE);
369     g_return_val_if_fail(doc != NULL, NULL);
371     return doc;
374 // TODO: When the switchover to the new GUI is complete, this function should go away
375 // and be replaced with a call to Inkscape::NSApplication::Editor::createDesktop.  
376 // It currently only exists to correctly mimic the desktop creation functionality
377 // in file.cpp.
378 //
379 // \see sp_file_new
380 SPDesktop*
381 makeInkboardDesktop(SPDocument* doc)
383     SPDesktop* dt;
385     if (NSApplication::Application::getNewGui()) 
386         dt = NSApplication::Editor::createDesktop(doc);
388     else 
389     {
390         SPViewWidget *dtw = sp_desktop_widget_new(sp_document_namedview(doc, NULL));
391         g_return_val_if_fail(dtw != NULL, NULL);
392         sp_document_unref(doc);
394         sp_create_window(dtw, TRUE);
395         dt = static_cast<SPDesktop*>(dtw->view);
396         sp_namedview_window_from_document(dt);
397     }
399     return dt;
402 }  // namespace Whiteboard
403  
404 }  // namespace Inkscape
407 /*
408   Local Variables:
409   mode:c++
410   c-file-style:"stroustrup"
411   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
412   indent-tabs-mode:nil
413   fill-column:99
414   End:
415 */
416 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :