File: synchronization-commands.cpp

package info (click to toggle)
gobby-infinote 0.5.0-4
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,756 kB
  • ctags: 2,474
  • sloc: cpp: 19,816; sh: 4,337; xml: 456; makefile: 395
file content (252 lines) | stat: -rw-r--r-- 7,965 bytes parent folder | download | duplicates (2)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/* Gobby - GTK-based collaborative text editor
 * Copyright (C) 2008-2014 Armin Burgmeier <armin@arbur.net>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "commands/synchronization-commands.hpp"

#include "util/i18n.hpp"

namespace
{
	inline const gchar* _(const gchar* msgid) { return Gobby::_(msgid); }

	void set_progress_text(Gobby::SessionView& view,
	                       gdouble percentage)
	{
		view.set_info(
			Glib::ustring::compose(
				_("Synchronization in progress... %1%%"),
				static_cast<unsigned int>(percentage * 100)),
			false);
	}

	void set_error_text(Gobby::SessionView& view,
			    const Glib::ustring& initial_text)
	{
		Glib::ustring type_text;

		// Document cannot be used if an error happened
		// during synchronization.
		type_text = _("This document cannot be used.");

		const Glib::ustring info_text =
			_("If you have an idea what could have caused the "
			  "problem, then you may attempt to solve it and "
			  "try again (after having closed this document). "
			  "Otherwise it is most likely a bug in the "
			  "software. In that case, please file a bug report "
			  "at http://gobby.0x539.de/trac/newticket and "
			  "provide as much information as you can, including "
			  "what you did when the problem occurred and how to "
			  "reproduce the problem (if possible) so that we "
			  "can fix the problem in a later version. "
			  "Thank you.");

		view.set_info(
			Glib::ustring::compose(
				_("Synchronization failed: %1"),
				initial_text) +
			"\n\n" + type_text + "\n\n" + info_text, true);
	}
}

class Gobby::SynchronizationCommands::SyncInfo
{
public:
	SyncInfo(SynchronizationCommands& sync_commands,
	         SessionView& view);
	~SyncInfo();

	SessionView& get_session_view() { return m_view; }

private:
	static void
	on_synchronization_failed_static(InfSession* session,
	                                 InfXmlConnection* conn,
	                                 const GError* error,
	                                 gpointer user_data)
	{
		static_cast<SynchronizationCommands*>(user_data)->
			on_synchronization_failed(session, conn, error);
	}

	static void
	on_synchronization_complete_static(InfSession* session,
	                                   InfXmlConnection* conn,
	                                   gpointer user_data)
	{
		static_cast<SynchronizationCommands*>(user_data)->
			on_synchronization_complete(session, conn);
	}

	static void
	on_synchronization_progress_static(InfSession* session,
	                                   InfXmlConnection* conn,
	                                   gdouble percentage,
	                                   gpointer user_data)
	{
		static_cast<SyncInfo*>(user_data)->
			on_synchronization_progress(conn, percentage);
	}

	void on_synchronization_progress(InfXmlConnection* conn,
	                                 gdouble percentage);

	SessionView& m_view;

	gulong m_synchronization_complete_handler;
	gulong m_synchronization_failed_handler;
	gulong m_synchronization_progress_handler;
};

Gobby::SynchronizationCommands::SyncInfo::
	SyncInfo(SynchronizationCommands& commands, SessionView& view):
	m_view(view)
{
	InfSession* session = m_view.get_session();

	m_synchronization_complete_handler = g_signal_connect(
		G_OBJECT(session), "synchronization-complete",
		G_CALLBACK(on_synchronization_complete_static), &commands);
	m_synchronization_failed_handler = g_signal_connect(
		G_OBJECT(session), "synchronization-failed",
		G_CALLBACK(on_synchronization_failed_static), &commands);
	m_synchronization_progress_handler = g_signal_connect(
		G_OBJECT(session), "synchronization-progress",
		G_CALLBACK(on_synchronization_progress_static), this);
}

Gobby::SynchronizationCommands::SyncInfo::~SyncInfo()
{
	InfSession* session = m_view.get_session();

	g_signal_handler_disconnect(G_OBJECT(session),
	                            m_synchronization_complete_handler);
	g_signal_handler_disconnect(G_OBJECT(session),
	                            m_synchronization_failed_handler);
	g_signal_handler_disconnect(G_OBJECT(session),
	                            m_synchronization_progress_handler);
}

void Gobby::SynchronizationCommands::SyncInfo::
	on_synchronization_progress(InfXmlConnection* conn,
	                            gdouble percentage)
{
	set_progress_text(m_view, percentage);
}

Gobby::SynchronizationCommands::
	SynchronizationCommands(const Folder& text_folder,
	                        const Folder& chat_folder)
{
	text_folder.signal_document_added().connect(
		sigc::mem_fun(
			*this,
			&SynchronizationCommands::on_document_added));
	chat_folder.signal_document_added().connect(
		sigc::mem_fun(
			*this,
			&SynchronizationCommands::on_document_added));

	text_folder.signal_document_removed().connect(
		sigc::mem_fun(
			*this,
			&SynchronizationCommands::on_document_removed));

	text_folder.signal_document_removed().connect(
		sigc::mem_fun(
			*this,
			&SynchronizationCommands::on_document_removed));
}

Gobby::SynchronizationCommands::~SynchronizationCommands()
{
	for(SyncMap::iterator iter = m_sync_map.begin();
	    iter != m_sync_map.end(); ++iter)
	{
		delete iter->second;
	}
}

void
Gobby::SynchronizationCommands::on_document_added(SessionView& view)
{
	InfSession* session = view.get_session();
	if(inf_session_get_status(session) == INF_SESSION_SYNCHRONIZING)
	{
		InfXmlConnection* connection;
		g_object_get(G_OBJECT(session),
		             "sync-connection", &connection, NULL);
		gdouble percentage = inf_session_get_synchronization_progress(
			session, connection);
		g_object_unref(connection);

		g_assert(m_sync_map.find(session) == m_sync_map.end());
		m_sync_map[session] = new SyncInfo(*this, view);
		set_progress_text(view, percentage);
	}
}

void
Gobby::SynchronizationCommands::on_document_removed(SessionView& view)
{
	InfSession* session = view.get_session();
	SyncMap::iterator iter = m_sync_map.find(session);
	if(iter != m_sync_map.end())
	{
		delete iter->second;
		m_sync_map.erase(iter);
	}
}

void
Gobby::SynchronizationCommands::
	on_synchronization_failed(InfSession* session,
                                  InfXmlConnection* c,
                                  const GError* error)
{
	SyncMap::iterator iter = m_sync_map.find(session);
	g_assert(iter != m_sync_map.end());

	set_error_text(iter->second->get_session_view(), error->message);

	// The document will be of no use anyway, so consider it as not
	// being modified.
	InfBuffer* buffer = inf_session_get_buffer(session);
	inf_buffer_set_modified(buffer, FALSE);

	delete iter->second;
	m_sync_map.erase(iter);
}

void
Gobby::SynchronizationCommands::
	on_synchronization_complete(InfSession* session,
                                    InfXmlConnection* c)
{
	SyncMap::iterator iter = m_sync_map.find(session);
	g_assert(iter != m_sync_map.end());

	// TODO: Actually we should always set the modified flag, except the
	// document is either empty, or known in the document info storage
	// and the version on disk is the same as the one we got
	// synchronized. We could store a hash and modification time in the
	// documentinfo storage for this.
	InfBuffer* buffer = inf_session_get_buffer(session);
	inf_buffer_set_modified(buffer, FALSE);

	delete iter->second;
	m_sync_map.erase(iter);
}