Code

e9363ecd95c02a2728ce54627e9f7e71b21bda9f
[inkscape.git] / src / jabber_whiteboard / inkboard-document.cpp
1 /**
2  * Inkscape::Whiteboard::InkboardDocument - Inkboard document implementation
3  *
4  * Authors:
5  * David Yip <yipdw@rose-hulman.edu>
6  *
7  * Copyright (c) 2005 Authors
8  *
9  * Released under GNU GPL, read the file 'COPYING' for more information
10  */
12 #include <glib.h>
13 #include <glibmm.h>
15 #include "jabber_whiteboard/inkboard-document.h"
17 #include "xml/simple-session.h"
18 #include "jabber_whiteboard/inkboard-session.h"
19 #include "jabber_whiteboard/defines.h"
20 #include "jabber_whiteboard/session-manager.h"
22 namespace Inkscape {
24 namespace Whiteboard {
26 InkboardDocument::InkboardDocument(int code, State::SessionType type, Glib::ustring const& to) :
27         XML::SimpleNode(code), _type(type), _recipient(to)
28 {
29     _initBindings();
30 }
32 void
33 InkboardDocument::_initBindings()
34 {
35     this->_sm = &SessionManager::instance();
36     this->state = State::INITIAL;
37     _bindDocument(*this);
38     _bindLogger(*(new XML::SimpleSession()));
39 }
41 void
42 InkboardDocument::setRecipient(Glib::ustring const& val)
43 {
44     this->_recipient = val;
45 }
47 Glib::ustring 
48 InkboardDocument::getRecipient() const
49 {
50     return this->_recipient;
51 }
53 void
54 InkboardDocument::setSessionId(Glib::ustring const& val)
55 {
56     this->_session = val;
57 }
59 Glib::ustring 
60 InkboardDocument::getSessionId() const
61 {
62     return this->_session;
63 }
65 void
66 InkboardDocument::startSessionNegotiation()
67 {
68     if(_type == State::WHITEBOARD_PEER)
69         sendProtocol(_recipient, Message::PROTOCOL,Message::CONNECT_REQUEST);
71     else if(_type == State::WHITEBOARD_MUC)
72     {
73         // Check that the MUC room is whiteboard enabled, if not no need to send 
74         // anything, just set the room to be whiteboard enabled
75     }
76 }
78 void
79 InkboardDocument::terminateSession()
80 {
82 }
84 void
85 InkboardDocument::processInkboardEvent(Message::Wrapper &wrapper, Pedro::Element* data)
86 {
87     if(this->handleIncomingState(wrapper,data))
88     {
89         if(wrapper == Message::PROTOCOL)
90         {
91             Glib::ustring message = data->getFirstChild()->getFirstChild()->getFirstChild()->getName();
93             if(message == Message::CONNECT_REQUEST)
94             {
95                 // An MUC member requesting document
97             }else if(message == Message::ACCEPT_INVITATION)
98             {
99                 // TODO : Would be nice to create the desktop here
101                 sendProtocol(getRecipient(),Message::PROTOCOL, Message::CONNECTED);
102                 sendProtocol(getRecipient(),Message::PROTOCOL, Message::DOCUMENT_BEGIN);
104                 // Send the Document
106                 sendProtocol(getRecipient(),Message::PROTOCOL, Message::DOCUMENT_END);
108             }else if(message == Message::DECLINE_INVITATION)
109             {
110                 this->_sm->terminateSession(this->getSessionId());
111             }
112         }
113     }else{
114         g_warning("Recieved Message in invalid state = %d", this->state);
115         data->print();
116     }
119 bool
120 InkboardDocument::sendProtocol(const Glib::ustring &destJid, Message::Wrapper &wrapper,
121      Message::Message &message)
123     if(this->handleOutgoingState(wrapper,message))
124     {
125         char *fmt=
126             "<message type='%s' from='%s' to='%s'>"
127                 "<wb xmlns='%s' session='%s'>"
128                     "<%s>"
129                         "<%s />"
130                     "</%s>"
131                 "</wb>"
132             "</message>";
134         if (!_sm->getClient().write(fmt,
135                 _type.c_str(),_sm->getClient().getJid().c_str(),destJid.c_str(),Vars::INKBOARD_XMLNS.c_str(),
136                 this->getSessionId().c_str(),wrapper.c_str(),message.c_str(),wrapper.c_str())) 
137             { return false; }
139         else 
140             { return true; }
142     }else 
143     { 
144         g_warning("Sending Message in invalid state message=%s , state=%d",message.c_str(),this->state);
145         return false; 
146     }
149 bool
150 InkboardDocument::handleOutgoingState(Message::Wrapper &wrapper, Glib::ustring const& message)
152     if(wrapper == Message::PROTOCOL) 
153     {
154         if(message == Message::CONNECT_REQUEST) 
155             return this->handleState(State::INITIAL,State::AWAITING_INVITATION_REPLY);
157         else if(message == Message::ACCEPT_INVITATION)
158             return this->handleState(State::CONNECTING,State::AWAITING_CONNECTED);
160         else if(message == Message::CONNECTED)
161             return this->handleState(State::INVITATION_RECIEVED,State::CONNECTED);
163         else if(message == Message::DOCUMENT_BEGIN)
164             return this->handleState(State::CONNECTED,State::SYNCHRONISING);
166         else if(message == Message::DOCUMENT_END) { 
167             return this->handleState(State::SYNCHRONISING,State::IN_WHITEBOARD);
168         }
170         else 
171             return false;
173     } else 
174         if(this->state == State::SYNCHRONISING && wrapper == Message::NEW)
175             return true;
177     return this->state == State::IN_WHITEBOARD;
180 bool
181 InkboardDocument::handleIncomingState(Message::Wrapper &wrapper, Pedro::Element* data)
183     if(wrapper == Message::PROTOCOL) 
184     {
185         Glib::ustring message = data->getFirstChild()->getFirstChild()->getFirstChild()->getName();
187         if(message == Message::CONNECT_REQUEST)
188             return this->handleState(State::INITIAL,State::CONNECTING);
189         if(message == Message::ACCEPT_INVITATION)
190             return this->handleState(State::AWAITING_INVITATION_REPLY,State::INVITATION_RECIEVED);
192         else if(message == Message::CONNECTED)
193             return this->handleState(State::AWAITING_CONNECTED,State::AWAITING_DOCUMENT_BEGIN);
195         else if(message == Message::DOCUMENT_BEGIN)
196             return this->handleState(State::AWAITING_DOCUMENT_BEGIN,State::SYNCHRONISING);
198         else if(message == Message::DOCUMENT_END)
199             return this->handleState(State::SYNCHRONISING,State::IN_WHITEBOARD);
201         else 
202             return false;
204     } else 
205         if(this->state == State::SYNCHRONISING && wrapper == Message::NEW)
206             return true;
208     return this->state == State::IN_WHITEBOARD;
211 bool 
212 InkboardDocument::handleState(State::SessionState expectedState, State::SessionState newState)
214     if(this->state == expectedState)
215     {
216         this->state = newState;
217         return true;
218     }
220     return false;
224 } // namespace Whiteboard
225 } // namespace Inkscape
228 /*
229   Local Variables:
230   mode:c++
231   c-file-style:"stroustrup"
232   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
233   indent-tabs-mode:nil
234   fill-column:99
235   End:
236 */
237 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :