summaryrefslogtreecommitdiffstats
path: root/src/jabber_whiteboard/undo-stack-observer.cpp
blob: 7750dc73c9799545bae904a93c61d35423ad3bf8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
 * Undo / redo / undo log commit listener
 *
 * Authors:
 * David Yip <yipdw@rose-hulman.edu>
 *
 * Copyright (c) 2005 Authors
 *
 * Released under GNU GPL, read the file 'COPYING' for more information
 */

#include "document.h"
#include "document-private.h"

#include "xml/event.h"
#include "xml/event-fns.h"
#include "undo-stack-observer.h"

#include "util/list.h"
#include "util/reverse-list.h"

#include "jabber_whiteboard/defines.h"
#include "jabber_whiteboard/session-manager.h"
#include "jabber_whiteboard/node-tracker.h"
#include "jabber_whiteboard/serializer.h"
#include "jabber_whiteboard/message-utilities.h"
#include "jabber_whiteboard/message-aggregator.h"
#include "jabber_whiteboard/message-tags.h"


#include <glibmm.h>

namespace Inkscape {

namespace Whiteboard {

UndoStackObserver::UndoStackObserver(SessionManager* sm) : _sm(sm), _undoSendEventLocks(0), _redoSendEventLocks(0), _undoCommitSendEventLocks(0) { 
}

UndoStackObserver::~UndoStackObserver() { }

void
UndoStackObserver::notifyUndoEvent(XML::Event* log)
{
	if (this->_undoSendEventLocks == 0) {
		bool chatroom = this->_sm->session_data->status.test(IN_CHATROOM);
		Glib::ustring commit = MessageUtilities::makeTagWithContent(MESSAGE_UNDO, "");
		this->_sm->sendChange(commit, CHANGE_COMMIT, "", chatroom);
	}

	// Retrieve and process added/deleted nodes in the undo log
	// TODO: re-enable; right now it doesn't work because we can't recover the names
	// of deleted nodes (although perhaps creating a subclass of XML::Event that stored
	// names of serialized nodes along with all other Event information would fix this)
	/*
	KeyToNodeActionMap node_actions = this->_action_observer.getNodeActionMap();
	this->_sm->node_tracker()->process(node_actions);
	this->_action_observer.clearNodeBuffers();
	*/
	
}

void
UndoStackObserver::notifyRedoEvent(XML::Event* log)
{
	if (this->_redoSendEventLocks == 0) {
		bool chatroom = this->_sm->session_data->status.test(IN_CHATROOM);
		Glib::ustring commit = MessageUtilities::makeTagWithContent(MESSAGE_REDO, "");
		this->_sm->sendChange(commit, CHANGE_COMMIT, "", chatroom);
	}

	// Retrieve and process added/deleted nodes in the redo log
	/*
	KeyToNodeActionMap node_actions = this->_action_observer.getNodeActionMap();
	this->_sm->node_tracker()->process(node_actions);
	this->_action_observer.clearNodeBuffers();
	*/
}

void
UndoStackObserver::notifyUndoCommitEvent(XML::Event* log)
{
	if (this->_undoCommitSendEventLocks == 0) {
		this->_doAction(log);
	}
}

void
UndoStackObserver::lockObserverFromSending(ObserverType type)
{
	switch (type) {
		case UNDO_EVENT:
			++this->_undoSendEventLocks;
			break;
		case REDO_EVENT:
			++this->_redoSendEventLocks;
			break;
		case UNDO_COMMIT_EVENT:
			++this->_undoCommitSendEventLocks;
			break;
		default:
			break;
	}
}

void
UndoStackObserver::unlockObserverFromSending(ObserverType type)
{
	switch(type) {
		case UNDO_EVENT:
			if (this->_undoSendEventLocks) {
				--this->_undoSendEventLocks;
			}
			break;
		case REDO_EVENT:
			if (this->_redoSendEventLocks) {
				--this->_redoSendEventLocks;
			}
			break;
		case UNDO_COMMIT_EVENT:
			if (this->_undoCommitSendEventLocks) {
				--this->_undoCommitSendEventLocks;
			}
			break;
		default:
			break;
	}
}

void
UndoStackObserver::_doAction(XML::Event* log)
{
	if (this->_sm->serializer()) {
		bool chatroom = this->_sm->session_data->status.test(IN_CHATROOM);
		XML::replay_log_to_observer(log, *this->_sm->serializer());

		this->_sm->serializer()->synthesizeChildNodeAddEvents();

		SerializedEventList& events = this->_sm->serializer()->getEventList();

		SerializedEventList::iterator i = events.begin();
		MessageAggregator& agg = MessageAggregator::instance();
		Glib::ustring msgbuf;

		while(i != events.end()) {
			while(agg.addOne(*i++, msgbuf)) {
				if (i == events.end()) {
					break;
				}
			}

			if (i != events.end()) {
				i--;
			}

			this->_sm->sendChange(msgbuf, CHANGE_REPEATABLE, "", chatroom);
			msgbuf.clear();
		}

		KeyToNodeActionList& node_actions = this->_sm->serializer()->getNodeTrackerActions();
		this->_sm->node_tracker()->process(node_actions);
		this->_sm->serializer()->reset();
		Glib::ustring commit = MessageUtilities::makeTagWithContent(MESSAGE_COMMIT, "");
		this->_sm->sendChange(commit, CHANGE_COMMIT, "", chatroom);
	}
}

}

}

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :