Code

c46546ebc47822ac4d5f32f310c6c9c8e8a36ef2
[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     sendProtocol(_recipient, Message::PROTOCOL,Message::CONNECT_REQUEST);
69 }
71 void
72 InkboardDocument::terminateSession()
73 {
75 }
77 void
78 InkboardDocument::processInkboardEvent(Message::Wrapper &wrapper, Pedro::Element* data)
79 {
80     if(this->handleIncomingState(wrapper,data))
81     {
82         if(wrapper == Message::PROTOCOL)
83         {
84             if(data->exists(Message::ACCEPT_INVITATION)); 
85             {
86                 // TODO : Would be nice to create the desktop here
88                 sendProtocol(getRecipient(),Message::PROTOCOL, Message::CONNECTED);
89                 sendProtocol(getRecipient(),Message::PROTOCOL, Message::DOCUMENT_BEGIN);
90                 sendProtocol(getRecipient(),Message::PROTOCOL, Message::DOCUMENT_END);
91             }
92         }
93     }else{
94         g_warning("Recieved Message in invalid state = %d", this->state);
95         data->print();
96     }
97 }
99 bool
100 InkboardDocument::sendProtocol(const Glib::ustring &destJid, Message::Wrapper &wrapper,
101      Message::Message message)
103     if(this->handleOutgoingState(wrapper,message))
104     {
105         char *fmt=
106             "<message type='%s' from='%s' to='%s'>"
107                 "<wb xmlns='%s' session='%s'>"
108                     "<%s>"
109                         "<%s />"
110                     "</%s>"
111                 "</wb>"
112                 "<body> </body>"
113             "</message>";
115         if (!_sm->getClient().write(fmt,
116                 _type,_sm->getClient().getJid().c_str(),destJid.c_str(),Vars::INKBOARD_XMLNS,
117                 this->getSessionId().c_str(),wrapper,message,wrapper)) 
118             { return false; }
120         else 
121             { return true; }
123     }else 
124     { 
125         g_warning("Sending Message in invalid state message=%s , state=%d",message,this->state);
126         return false; 
127     }
130 bool
131 InkboardDocument::handleOutgoingState(Message::Wrapper &wrapper, Glib::ustring const& message)
133     if(wrapper == Message::PROTOCOL) 
134     {
135         if(message == Message::CONNECT_REQUEST) 
136             return this->handleState(State::INITIAL,State::AWAITING_INVITATION_REPLY);
138         else if(message == Message::ACCEPT_INVITATION)
139             return this->handleState(State::CONNECTING,State::AWAITING_CONNECTED);
141         else if(message == Message::CONNECTED)
142             return this->handleState(State::INVITATION_RECIEVED,State::CONNECTED);
144         else if(message == Message::DOCUMENT_BEGIN)
145             return this->handleState(State::CONNECTED,State::SYNCHRONISING);
147         else if(message == Message::DOCUMENT_END) { 
148             return this->handleState(State::SYNCHRONISING,State::IN_WHITEBOARD);
149         }
151         else 
152             return false;
154     } else 
155         if(this->state == State::SYNCHRONISING && wrapper == Message::NEW)
156             return true;
158     return this->state == State::IN_WHITEBOARD;
161 bool
162 InkboardDocument::handleIncomingState(Message::Wrapper &wrapper, Pedro::Element* data)
164     if(wrapper == Message::PROTOCOL) 
165     {
166         Glib::ustring message = data->getFirstChild()->getFirstChild()->getFirstChild()->getName();
168         // Connect Requests are handled in SessionManager
169         if(message == Message::ACCEPT_INVITATION)
170             return this->handleState(State::AWAITING_INVITATION_REPLY,State::INVITATION_RECIEVED);
172         else if(message == Message::CONNECTED)
173             return this->handleState(State::AWAITING_CONNECTED,State::AWAITING_DOCUMENT_BEGIN);
175         else if(message == Message::DOCUMENT_BEGIN)
176             return this->handleState(State::AWAITING_DOCUMENT_BEGIN,State::SYNCHRONISING);
178         else if(message == Message::DOCUMENT_END)
179             return this->handleState(State::SYNCHRONISING,State::IN_WHITEBOARD);
181         else 
182             return false;
184     } else 
185         if(this->state == State::SYNCHRONISING && wrapper == Message::NEW)
186             return true;
188     return this->state == State::IN_WHITEBOARD;
191 bool 
192 InkboardDocument::handleState(State::SessionState expectedState, State::SessionState newState)
194     if(this->state == expectedState)
195     {
196         this->state = newState;
197         return true;
198     }
200     return false;
204 } // namespace Whiteboard
205 } // namespace Inkscape
208 /*
209   Local Variables:
210   mode:c++
211   c-file-style:"stroustrup"
212   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
213   indent-tabs-mode:nil
214   fill-column:99
215   End:
216 */
217 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :