Code

quick g_message UndoStackObserver for tracing calls to the undo system
[inkscape.git] / src / jabber_whiteboard / session-file.cpp
1 /**
2  * Whiteboard session file object
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 <glibmm/i18n.h>
15 #include "util/list-container.h"
17 #include "jabber_whiteboard/message-utilities.h"
18 #include "jabber_whiteboard/node-utilities.h"
19 #include "jabber_whiteboard/typedefs.h"
21 #include "jabber_whiteboard/session-file.h"
24 namespace Inkscape {
26 namespace Whiteboard {
28 SessionFile::SessionFile(Glib::ustring const& filename, bool reading, bool compress) : _filename(filename), _compress(compress), _reading(reading)
29 {
30         try {
32                 if (!reading) {
33                         this->fptr = Glib::IOChannel::create_from_file(filename, "w+");
34                 } else {
35                         this->fptr = Glib::IOChannel::create_from_file(filename, "r");
36                 }
37                 this->_ateof = false;
38         } catch (Glib::FileError) {
39                 throw;
40         }
41 }
43 SessionFile::~SessionFile() 
44 {
45         if (!this->_reading) {
46                 this->commit();
47         }
48         this->close();
49 }
51 gint64
52 SessionFile::nextMessageFrom(gint64 from, Glib::ustring& buf)
53 {
54         try {
55                 Glib::ustring line;
56                 Glib::IOStatus st;
57                 Node part;
59                 gint64 accum = from;
60                 buf = "";
61                 this->fptr->seek(accum, Glib::SEEK_TYPE_SET);
63                 while(part.tag != MESSAGE_COMMIT) {
64                         st = this->fptr->read_line(line);
65                         if (st == Glib::IO_STATUS_EOF) {
66                                 break;
67                         } else {
68                                 accum += line.bytes();
69                                 this->fptr->seek(accum);
70                                 MessageUtilities::getFirstMessageTag(part, line);
71                                 buf += line;
72                                 line.clear();
73                         }
74                 } 
75                 
76                 if (st == Glib::IO_STATUS_NORMAL) {
77                         // reset eof flag if successful
78                         this->_ateof = false;
79                         return from + buf.bytes();
80                 } else {
81                         if (st == Glib::IO_STATUS_EOF) {
82                                 this->_ateof = true;
83                         }
84                         return from;
85                 }
86         } catch (Glib::IOChannelError e) {
87                 g_warning("Could not read next message due to I/O error (error: %s)!", e.what().data());
88         } catch (Glib::ConvertError e) {
89                 g_warning("Could not read next message due to charset conversion error (error: %s)!", e.what().data());
90         }
92         return from;
93 }
95 void
96 SessionFile::addMessage(Glib::ustring const& message)
97 {
98         Glib::ustring msg = message;
99         this->changes.push_back(msg);
102 void
103 SessionFile::commit()
105         if (!this->_reading) {
106                 SessionQueue::iterator i = changes.begin();
107                 for(; i != changes.end(); i++) {
108                         try {
109                                 fptr->write(*i);
110                                 changes.erase(i);
111                         } catch (Glib::IOChannelError e) {
112                                 g_warning("Caught I/O exception (error string: %s) while committing change to session file %s.  Attempting to commit remaining changes; session file will be inconsistent with whiteboard session history.", e.what().c_str(), this->_filename.c_str());
113                         } catch (Glib::ConvertError e) {
114                                 g_warning("Caught character set conversion error (error string: %s) while committing change to session file %s.  Attempting to commit remaining changes; session file will be inconsistent with whiteboard session history.", e.what().c_str(), this->_filename.c_str());
115                         }
116                 }
117                 fptr->write("\n");
118         }
121 void
122 SessionFile::close()
124         fptr->close(true);
131 /*
132   Local Variables:
133   mode:c++
134   c-file-style:"stroustrup"
135   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
136   indent-tabs-mode:nil
137   fill-column:99
138   End:
139 */
140 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :