Code

lessen the overkill nature of whiteboard's Quit
[inkscape.git] / src / jabber_whiteboard / inkboard-session.cpp
1 /**
2  * Inkscape::Whiteboard::InkboardSession - Whiteboard implementation of XML::Session
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 <glibmm.h>
13 #include <glib/gmessages.h>
14 #include <glib/gquark.h>
16 #include "jabber_whiteboard/inkboard-session.h"
17 #include "jabber_whiteboard/inkboard-document.h"
18 #include "jabber_whiteboard/defines.h"
20 #include "xml/node.h"
21 #include "xml/event.h"
22 #include "xml/element-node.h"
23 #include "xml/text-node.h"
24 #include "xml/comment-node.h"
26 #include "util/share.h"
27 #include "util/ucompose.hpp"
29 namespace Inkscape {
31 namespace Whiteboard {
33 using XML::Node;
35 void
36 InkboardSession::beginTransaction()
37 {
38     g_assert(!_in_transaction);
39     _in_transaction = true;
40 }
42 void
43 InkboardSession::rollback()
44 {
45     g_assert(_in_transaction);
46     _in_transaction = false;
47 }
49 void 
50 InkboardSession::commit()
51 {
52     g_assert(_in_transaction);
53     _in_transaction = false;
54 }
56 XML::Event*
57 InkboardSession::commitUndoable()
58 {
59     g_assert(_in_transaction);
60     _in_transaction = false;
61     return NULL;
62 }
64 XML::Node*
65 InkboardSession::createElementNode(char const* name)
66 {
67     return new XML::ElementNode(g_quark_from_string(name));
68 }
70 XML::Node*
71 InkboardSession::createTextNode(char const* content)
72 {
73     return new XML::TextNode(Util::share_string(content));
74 }
76 XML::Node*
77 InkboardSession::createCommentNode(char const* content)
78 {
79     return new XML::CommentNode(Util::share_string(content));
80 }
83 void InkboardSession::notifyChildAdded(Node &parent,
84                                      Node &child,
85                                      Node *prev)
86 {
87     if (_in_transaction && doc->state == State::IN_WHITEBOARD) {
89         XML::Node *node = (XML::Node *)&child;
91         if(this->doc->tracker->get(node) == "")
92         {
93             this->doc->addNodeToTracker(node);
94             Message::Message message = this->doc->composeNewMessage(node);
96             this->doc->send(this->doc->getRecipient(),Message::NEW,message);
97         }
98     }
99 }
101 void InkboardSession::notifyChildRemoved(Node &parent,
102                                        Node &child,
103                                        Node *prev)
105     if (_in_transaction && doc->state == State::IN_WHITEBOARD) 
106     {
107         XML::Node *element = (XML::Node *)&child;
109         Message::Message message = String::ucompose(Vars::REMOVE_MESSAGE,
110             this->doc->tracker->get(element));
112         this->doc->send(this->doc->getRecipient(),Message::REMOVE,message);
113    }
116 void InkboardSession::notifyChildOrderChanged(Node &parent,
117                                             Node &child,
118                                             Node *old_prev,
119                                             Node *new_prev)
121     if (_in_transaction && doc->state == State::IN_WHITEBOARD) 
122     {
123         XML::Node *element = (XML::Node *)&child;
124         XML::Node *parentElement = (XML::Node *)&parent;
126         unsigned int index = parentElement->_childPosition(*element);
128         Message::Message message = String::ucompose(Vars::MOVE_MESSAGE,
129                 this->doc->tracker->get(element),index);
131         this->doc->send(this->doc->getRecipient(),Message::MOVE,message);
132     }
135 void InkboardSession::notifyContentChanged(Node &node,
136                                          Util::ptr_shared<char> old_content,
137                                          Util::ptr_shared<char> new_content)
139     if (_in_transaction && doc->state == State::IN_WHITEBOARD) 
140     {
141         XML::Node *element = (XML::Node *)&node;
143         Glib::ustring value(new_content.pointer());
145         Glib::ustring change = this->doc->tracker->getLastHistory(element,"text");
147         if(change.size() > 0 && change == value)
148             return;
150         if(new_content.pointer())
151         {
152             unsigned int version = this->doc->tracker->incrementVersion(element);
154             Message::Message message = String::ucompose(Vars::CONFIGURE_TEXT_MESSAGE,
155                 this->doc->tracker->get(element),version,new_content.pointer());
157             this->doc->send(this->doc->getRecipient(),Message::CONFIGURE,message);
158         }
159     }
162 void InkboardSession::notifyAttributeChanged(Node &node,
163                                            GQuark name,
164                                            Util::ptr_shared<char> old_value,
165                                            Util::ptr_shared<char> new_value)
167     if (_in_transaction && doc->state == State::IN_WHITEBOARD) 
168     {
169         XML::Node *element = (XML::Node *)&node;
171         Glib::ustring value(new_value.pointer());
172         Glib::ustring attribute(g_quark_to_string(name));
174         Glib::ustring change = this->doc->tracker->getLastHistory(element,attribute);
176         if(change.size() > 0 && change == value)
177             return;
179         if(attribute.size() > 0 && value.size() > 0)
180         {
181             unsigned int version = this->doc->tracker->incrementVersion(element);
183             Message::Message message = String::ucompose(Vars::CONFIGURE_MESSAGE,
184                 this->doc->tracker->get(element),version,attribute.c_str(),value.c_str());
186             this->doc->send(this->doc->getRecipient(),Message::CONFIGURE,message);
187         }
188     }