File: autosave-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 (344 lines) | stat: -rw-r--r-- 9,155 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/* 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/autosave-commands.hpp"

#include "operations/operation-save.hpp"

#include "core/sessionuserview.hpp"

#include <glibmm/main.h>

#include <ctime>

class Gobby::AutosaveCommands::Info
{
public:
	Info(AutosaveCommands& commands, TextSessionView& view):
		m_commands(commands), m_view(view), m_save_op(NULL)
	{
		GtkSourceBuffer* buffer = m_view.get_text_buffer();

		m_modified_changed_handler = g_signal_connect_after(
			G_OBJECT(buffer), "modified-changed",
			G_CALLBACK(on_modified_changed_static), this);

		// We can't get this correct, so we assume the document was
		// synchronized to disk at the current time. If it has been
		// modified, we schedule a first autosave.
		m_sync_time = std::time(NULL);

		Operations& operations = m_commands.m_operations;
		OperationSave* save_op =
			operations.get_save_operation_for_document(view);
		if(save_op != NULL)
			begin_save_operation(save_op);
		else if(gtk_text_buffer_get_modified(GTK_TEXT_BUFFER(buffer)))
			schedule();
	}

	~Info()
	{
		GtkSourceBuffer* buffer = m_view.get_text_buffer();

		g_signal_handler_disconnect(G_OBJECT(buffer),
		                            m_modified_changed_handler);

		m_timeout_handler.disconnect();
	}

	// Called by AutosaveCommands when the timeout interval has changed.
	// Just reschedule the timeout.
	void reschedule()
	{
		if(m_timeout_handler.connected())
		{
			m_timeout_handler.disconnect();
			schedule();
		}
	}

	// Called by AutosaveCommands
	void begin_save_operation(OperationSave* save_op)
	{
		g_assert(m_save_op == NULL);

		// The document is already being saved, so we don't
		// need to autosave anymore. Reschedule autosave when save
		// operation finished.
		if(m_timeout_handler.connected())
			m_timeout_handler.disconnect();

		m_save_op = save_op;

		m_save_op->signal_finished().connect(
			sigc::mem_fun(
				*this,
				&Info::on_save_operation_finished));
	}

protected:
	void on_modified_changed()
	{
		if(m_save_op == NULL)
		{
			GtkTextBuffer* buffer =
				GTK_TEXT_BUFFER(m_view.get_text_buffer());

			if(!gtk_text_buffer_get_modified(buffer))
				m_timeout_handler.disconnect();
			else
			{
				// Until now the document on disk is in sync
				// with the in-memory buffer. Now the document
				// has changed, so schedule an autosave.
				m_sync_time = std::time(NULL);
				schedule();
			}
		}
	}

	void schedule()
	{
		g_assert(!m_timeout_handler.connected());
		g_assert(m_save_op == NULL);

		// Don't schedule a timeout in case the document has no entry
		// in the document info storage. This means we don't have an
		// uri yet where to save the document. However, we
		// automatically retry when the document is assigned an URI,
		// since the modification flag will change with this anyway.
		const std::string& key = m_view.get_info_storage_key();
		const DocumentInfoStorage::Info* info =
			m_commands.m_info_storage.get_info(key);
		if(!info || info->uri.empty())
			return;

		guint elapsed_seconds = std::time(NULL) - m_sync_time;
		guint autosave_interval = 60 *
			m_commands.m_preferences.editor.autosave_interval;

		if(elapsed_seconds > autosave_interval)
		{
			on_timeout();
		}
		else
		{
			m_timeout_handler =
				Glib::signal_timeout().connect_seconds(
					sigc::mem_fun(
						*this, &Info::on_timeout),
					autosave_interval - elapsed_seconds);
		}
	}

	void on_save_operation_finished(bool success)
	{
		GtkTextBuffer* buffer =
			GTK_TEXT_BUFFER(m_view.get_text_buffer());

		if(success)
			m_sync_time = m_save_op->get_start_time();

		m_save_op = NULL;

		// Schedule the next save operation in case the buffer has
		// been modified since the save operation was started.
		if(gtk_text_buffer_get_modified(buffer))
			schedule();
	}

	bool on_timeout()
	{
		const std::string& key = m_view.get_info_storage_key();
		const DocumentInfoStorage::Info* info =
			m_commands.m_info_storage.get_info(key);

		// Might have been removed from info in the meanwhile, so
		// don't assert here.
		if(info != NULL)
		{
			m_commands.m_operations.save_document(
				m_view, info->uri,
				info->encoding, info->eol_style);

			g_assert(m_save_op != NULL);

			// Set sync time to operation's start time even though
			// we don't know yet whether the operation will fail,
			// because otherwise we would try to save the
			// document the whole time. This way, we simply retry
			// when the timeout triggers again.
			m_sync_time = m_save_op->get_start_time();
		}

		return false;
	}

private:
	static void on_modified_changed_static(GtkTextBuffer* buffer,
	                                       gpointer user_data)
	{
		static_cast<Info*>(user_data)->on_modified_changed();
	}

	AutosaveCommands& m_commands;
	TextSessionView& m_view;

	gulong m_modified_changed_handler;

	sigc::connection m_timeout_handler;
	OperationSave* m_save_op;

	std::time_t m_sync_time;
};

Gobby::AutosaveCommands::AutosaveCommands(const Folder& folder,
                                          Operations& operations,
                                          const DocumentInfoStorage& storage,
					  const Preferences& preferences):
	m_folder(folder), m_operations(operations),
	m_info_storage(storage), m_preferences(preferences)
{
	m_folder.signal_document_added().connect(
		sigc::mem_fun(*this, &AutosaveCommands::on_document_added));
	m_folder.signal_document_removed().connect(
		sigc::mem_fun(*this, &AutosaveCommands::on_document_removed));

	m_operations.signal_begin_save_operation().connect(
		sigc::mem_fun(
			*this,
			&AutosaveCommands::on_begin_save_operation));

	m_preferences.editor.autosave_enabled.signal_changed().connect(
		sigc::mem_fun(
			*this,
			&AutosaveCommands::on_autosave_enabled_changed));

	m_preferences.editor.autosave_interval.signal_changed().connect(
		sigc::mem_fun(
			*this,
			&AutosaveCommands::on_autosave_interval_changed));

	// Create autosave infos for initial documents
	on_autosave_enabled_changed();
}

Gobby::AutosaveCommands::~AutosaveCommands()
{
	for(InfoMap::iterator iter = m_info_map.begin();
	    iter != m_info_map.end(); ++ iter)
	{
		delete iter->second;
	}
}

void Gobby::AutosaveCommands::on_document_added(SessionView& view)
{
	if(m_preferences.editor.autosave_enabled)
	{
		// We can only save text views:
		TextSessionView* text_view =
			dynamic_cast<TextSessionView*>(&view);
		if(text_view)
		{
			g_assert(m_info_map.find(text_view) ==
			         m_info_map.end());
			m_info_map[text_view] = new Info(*this, *text_view);
		}
	}
}

void Gobby::AutosaveCommands::on_document_removed(SessionView& view)
{
	if(m_preferences.editor.autosave_enabled)
	{
		TextSessionView* text_view =
			dynamic_cast<TextSessionView*>(&view);

		if(text_view)
		{
			InfoMap::iterator iter = m_info_map.find(text_view);
			g_assert(iter != m_info_map.end());
			delete iter->second;
			m_info_map.erase(iter);
		}
	}
}

void Gobby::AutosaveCommands::on_begin_save_operation(
	OperationSave* operation)
{
	TextSessionView* view = operation->get_view();
	// Save operation just started, document must be present
	g_assert(view != NULL);

	if(m_preferences.editor.autosave_enabled)
	{
		InfoMap::iterator iter = m_info_map.find(view);
		g_assert(iter != m_info_map.end());

		iter->second->begin_save_operation(operation);
	}
}

void Gobby::AutosaveCommands::on_autosave_enabled_changed()
{
	if(m_preferences.editor.autosave_enabled)
	{
		for(unsigned int i = 0;
		    i < static_cast<unsigned int>(m_folder.get_n_pages());
		    ++i)
		{
			// TODO: Add convenience API to folder, so that we
			// don't need to know here that it actually contains
			// SessionUserViews.
			const SessionUserView* userview =
				static_cast<const SessionUserView*>(
					m_folder.get_nth_page(i));

			SessionView& view = userview->get_session_view();
			TextSessionView* text_view =
				dynamic_cast<TextSessionView*>(&view);

			if(text_view)
			{
				m_info_map[text_view] =
					new Info(*this, *text_view);
			}
		}
	}
	else
	{
		for(InfoMap::iterator iter = m_info_map.begin();
		    iter != m_info_map.end(); ++ iter)
		{
			delete iter->second;
		}

		m_info_map.clear();
	}
}

void Gobby::AutosaveCommands::on_autosave_interval_changed()
{
	// Propagate to infos
	for(InfoMap::iterator iter = m_info_map.begin();
	    iter != m_info_map.end(); ++ iter)
	{
		return iter->second->reschedule();
	}
}