1 /**
2 * Whiteboard session file playback mechanism
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 */
13 #include <glibmm.h>
14 #include <glibmm/i18n.h>
16 #include <gtkmm/textbuffer.h>
18 #include "desktop-handles.h"
19 #include "document.h"
21 #include "jabber_whiteboard/defines.h"
22 #include "jabber_whiteboard/session-file.h"
23 #include "jabber_whiteboard/session-file-player.h"
24 #include "jabber_whiteboard/session-manager.h"
26 namespace Inkscape {
28 namespace Whiteboard {
30 SessionFilePlayer::SessionFilePlayer(unsigned int bufsz, SessionManager* sm)
31 {
32 this->_sm = sm;
33 this->_delay = 100;
34 this->_playing = false;
35 this->_curdir = FORWARD;
37 this->_current = this->_next = 0;
38 this->_sf = NULL;
40 if (sm->session_file() != NULL) {
41 this->_sf = sm->session_file();
42 } else {
43 g_warning("Cannot operate on NULL SessionFile.");
44 }
45 }
47 SessionFilePlayer::~SessionFilePlayer()
48 {
50 }
52 void
53 SessionFilePlayer::load(SessionFile* sm)
54 {
55 this->stop();
56 if (this->_sm != NULL) {
57 this->_sf = sm;
58 this->_current = this->_next = 0;
59 this->_visited.clear();
60 }
61 }
63 SessionFile*
64 SessionFilePlayer::unload()
65 {
66 SessionFile* sf = this->_sf;
67 this->stop();
68 this->_sf = NULL;
69 return sf;
70 }
72 Glib::ustring const&
73 SessionFilePlayer::filename()
74 {
75 return this->_sf->filename();
76 }
78 Glib::ustring const&
79 SessionFilePlayer::curmsg()
80 {
81 return this->_curmsg;
82 }
84 void
85 SessionFilePlayer::start()
86 {
87 this->_playback_dispatcher = Glib::signal_timeout().connect(sigc::bind< 0 > (sigc::mem_fun(*(this), &SessionFilePlayer::step), this->_curdir), this->_delay);
88 this->_playing = true;
89 }
91 void
92 SessionFilePlayer::stop()
93 {
94 this->_playback_dispatcher.disconnect();
95 this->_playing = false;
96 }
98 void
99 SessionFilePlayer::setDelay(unsigned int delay)
100 {
101 this->_delay = delay;
102 if (this->_playing) {
103 this->stop();
104 this->start();
105 }
106 }
108 void
109 SessionFilePlayer::setMessageOutputWidget(Glib::RefPtr<Gtk::TextBuffer> const& widgetptr)
110 {
111 this->_outputwidget = widgetptr;
112 }
114 bool
115 SessionFilePlayer::step(unsigned short dir)
116 {
117 switch (dir) {
118 case FORWARD: {
119 // Glib::ustring buf;
120 this->_current = this->_next;
121 this->_next = this->_sf->nextMessageFrom(this->_current, this->_curmsg);
122 if (this->_next == this->_current) {
123 return false;
124 } else {
125 this->_visited.push_front(std::make_pair< gint64, gint64 >(this->_current, this->_curmsg.bytes()));
126 this->_outputMessageToWidget();
127 this->_sm->receiveChange(this->_curmsg);
128 sp_document_done(SP_DT_DOCUMENT(this->_sm->desktop()));
129 this->_curdir = FORWARD;
130 return true;
131 }
132 break;
133 }
134 case BACKWARD:
135 if (this->_current == 0) {
136 return false;
137 } else {
138 this->_visited.pop_front();
139 std::pair< gint64, gint64 > last = this->_visited.front();
140 this->_current = last.first;
141 this->_next = last.first + last.second;
142 this->_sf->nextMessageFrom(this->_current, this->_curmsg);
143 this->_outputMessageToWidget();
144 sp_document_undo(SP_DT_DOCUMENT(this->_sm->desktop()));
145 this->_curdir = BACKWARD;
146 return true;
147 }
148 break;
149 default:
150 return true;
151 break;
152 }
153 }
155 void
156 SessionFilePlayer::_outputMessageToWidget()
157 {
158 if (this->_outputwidget) {
159 this->_outputwidget->set_text(this->_curmsg);
160 }
161 }
163 }
165 }
167 /*
168 Local Variables:
169 mode:c++
170 c-file-style:"stroustrup"
171 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
172 indent-tabs-mode:nil
173 fill-column:99
174 End:
175 */
176 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :