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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
 * lapiz-document-output-stream.c
 * This file is part of lapiz
 *
 * Copyright (C) 2010 - Ignacio Casal Quinteiro
 *
 * lapiz is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * lapiz is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with lapiz; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

#include "config.h"

#include <string.h>
#include <glib.h>
#include <glib/gi18n.h>
#include <gio/gio.h>
#include "lapiz-document-output-stream.h"

/* NOTE: never use async methods on this stream, the stream is just
 * a wrapper around CtkTextBuffer api so that we can use GIO Stream
 * methods, but the undelying code operates on a CtkTextBuffer, so
 * there is no I/O involved and should be accessed only by the main
 * thread */

#define MAX_UNICHAR_LEN 6

struct _LapizDocumentOutputStreamPrivate
{
	LapizDocument *doc;
	CtkTextIter    pos;

	gchar *buffer;
	gsize buflen;

	guint is_initialized : 1;
	guint is_closed : 1;
};

enum
{
	PROP_0,
	PROP_DOCUMENT
};

G_DEFINE_TYPE_WITH_PRIVATE (LapizDocumentOutputStream, lapiz_document_output_stream, G_TYPE_OUTPUT_STREAM)<--- There is an unknown macro here somewhere. Configuration is required. If G_DEFINE_TYPE_WITH_PRIVATE is a macro then please configure it.

static gssize	lapiz_document_output_stream_write (GOutputStream            *stream,
						    const void               *buffer,
						    gsize                     count,
						    GCancellable             *cancellable,
						    GError                  **error);

static gboolean lapiz_document_output_stream_flush (GOutputStream *stream,
                                                    GCancellable  *cancellable,
                                                    GError        **error);

static gboolean	lapiz_document_output_stream_close (GOutputStream     *stream,
						    GCancellable      *cancellable,
						    GError           **error);

static void
lapiz_document_output_stream_set_property (GObject      *object,
					   guint         prop_id,
					   const GValue *value,
					   GParamSpec   *pspec)
{
	LapizDocumentOutputStream *stream = LAPIZ_DOCUMENT_OUTPUT_STREAM (object);

	switch (prop_id)
	{
		case PROP_DOCUMENT:
			stream->priv->doc = LAPIZ_DOCUMENT (g_value_get_object (value));
			break;

		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
			break;
	}
}

static void
lapiz_document_output_stream_get_property (GObject    *object,
					   guint       prop_id,
					   GValue     *value,
					   GParamSpec *pspec)
{
	LapizDocumentOutputStream *stream = LAPIZ_DOCUMENT_OUTPUT_STREAM (object);

	switch (prop_id)
	{
		case PROP_DOCUMENT:
			g_value_set_object (value, stream->priv->doc);
			break;

		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
			break;
	}
}

static void
lapiz_document_output_stream_finalize (GObject *object)
{
	LapizDocumentOutputStream *stream = LAPIZ_DOCUMENT_OUTPUT_STREAM (object);

	g_free (stream->priv->buffer);

	G_OBJECT_CLASS (lapiz_document_output_stream_parent_class)->finalize (object);
}

static void
lapiz_document_output_stream_constructed (GObject *object)
{
	LapizDocumentOutputStream *stream = LAPIZ_DOCUMENT_OUTPUT_STREAM (object);

	if (!stream->priv->doc)
	{
		g_critical ("This should never happen, a problem happened constructing the Document Output Stream!");
		return;
	}

	/* Init the undoable action */
	ctk_source_buffer_begin_not_undoable_action (CTK_SOURCE_BUFFER (stream->priv->doc));
	/* clear the buffer */
	ctk_text_buffer_set_text (CTK_TEXT_BUFFER (stream->priv->doc),
				  "", 0);
	ctk_text_buffer_set_modified (CTK_TEXT_BUFFER (stream->priv->doc),
				      FALSE);

	ctk_source_buffer_end_not_undoable_action (CTK_SOURCE_BUFFER (stream->priv->doc));
}

static void
lapiz_document_output_stream_class_init (LapizDocumentOutputStreamClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);

	object_class->get_property = lapiz_document_output_stream_get_property;
	object_class->set_property = lapiz_document_output_stream_set_property;
	object_class->finalize = lapiz_document_output_stream_finalize;
	object_class->constructed = lapiz_document_output_stream_constructed;

	stream_class->write_fn = lapiz_document_output_stream_write;
	stream_class->flush = lapiz_document_output_stream_flush;
	stream_class->close_fn = lapiz_document_output_stream_close;

	g_object_class_install_property (object_class,
					 PROP_DOCUMENT,
					 g_param_spec_object ("document",
							      "Document",
							      "The document which is written",
							      LAPIZ_TYPE_DOCUMENT,
							      G_PARAM_READWRITE |
							      G_PARAM_CONSTRUCT_ONLY));
}

static void
lapiz_document_output_stream_init (LapizDocumentOutputStream *stream)
{
	stream->priv = lapiz_document_output_stream_get_instance_private (stream);

	stream->priv->buffer = NULL;
	stream->priv->buflen = 0;

	stream->priv->is_initialized = FALSE;
	stream->priv->is_closed = FALSE;
}

static LapizDocumentNewlineType
get_newline_type (CtkTextIter *end)
{
	LapizDocumentNewlineType res;
	CtkTextIter copy;
	gunichar c;

	copy = *end;
	c = ctk_text_iter_get_char (&copy);

	if (g_unichar_break_type (c) == G_UNICODE_BREAK_CARRIAGE_RETURN)
	{
		if (ctk_text_iter_forward_char (&copy) &&
		    g_unichar_break_type (ctk_text_iter_get_char (&copy)) == G_UNICODE_BREAK_LINE_FEED)
		{
			res = LAPIZ_DOCUMENT_NEWLINE_TYPE_CR_LF;
		}
		else
		{
			res = LAPIZ_DOCUMENT_NEWLINE_TYPE_CR;
		}
	}
	else
	{
		res = LAPIZ_DOCUMENT_NEWLINE_TYPE_LF;
	}

	return res;
}

GOutputStream *
lapiz_document_output_stream_new (LapizDocument *doc)
{
	return G_OUTPUT_STREAM (g_object_new (LAPIZ_TYPE_DOCUMENT_OUTPUT_STREAM,
					      "document", doc, NULL));
}

LapizDocumentNewlineType
lapiz_document_output_stream_detect_newline_type (LapizDocumentOutputStream *stream)
{
	LapizDocumentNewlineType type;
	CtkTextIter iter;

	g_return_val_if_fail (LAPIZ_IS_DOCUMENT_OUTPUT_STREAM (stream),
			      LAPIZ_DOCUMENT_NEWLINE_TYPE_DEFAULT);

	type = LAPIZ_DOCUMENT_NEWLINE_TYPE_DEFAULT;

	ctk_text_buffer_get_start_iter (CTK_TEXT_BUFFER (stream->priv->doc),
					&iter);

	if (ctk_text_iter_ends_line (&iter) || ctk_text_iter_forward_to_line_end (&iter))
	{
		type = get_newline_type (&iter);
	}

	return type;
}

/* If the last char is a newline, remove it from the buffer (otherwise
   CtkTextView shows it as an empty line). See bug #324942. */
static void
remove_ending_newline (LapizDocumentOutputStream *stream)
{
	CtkTextIter end;
	CtkTextIter start;

	ctk_text_buffer_get_end_iter (CTK_TEXT_BUFFER (stream->priv->doc), &end);
	start = end;

	ctk_text_iter_set_line_offset (&start, 0);

	if (ctk_text_iter_ends_line (&start) &&
	    ctk_text_iter_backward_line (&start))
	{
		if (!ctk_text_iter_ends_line (&start))
		{
			ctk_text_iter_forward_to_line_end (&start);
		}

		/* Delete the empty line which is from 'start' to 'end' */
		ctk_text_buffer_delete (CTK_TEXT_BUFFER (stream->priv->doc),
		                        &start,
		                        &end);
	}
}

static void
end_append_text_to_document (LapizDocumentOutputStream *stream)
{
	remove_ending_newline (stream);

	ctk_text_buffer_set_modified (CTK_TEXT_BUFFER (stream->priv->doc),
				      FALSE);

	ctk_source_buffer_end_not_undoable_action (CTK_SOURCE_BUFFER (stream->priv->doc));
}

static gssize
lapiz_document_output_stream_write (GOutputStream            *stream,
				    const void               *buffer,
				    gsize                     count,
				    GCancellable             *cancellable,
				    GError                  **error)
{
	LapizDocumentOutputStream *ostream;
	gchar *text;
	gsize len;
	gboolean freetext = FALSE;
	const gchar *end;
	gboolean valid;

	if (g_cancellable_set_error_if_cancelled (cancellable, error))
		return -1;

	ostream = LAPIZ_DOCUMENT_OUTPUT_STREAM (stream);

	if (!ostream->priv->is_initialized)
	{
		/* Init the undoable action */
		ctk_source_buffer_begin_not_undoable_action (CTK_SOURCE_BUFFER (ostream->priv->doc));

		ctk_text_buffer_get_start_iter (CTK_TEXT_BUFFER (ostream->priv->doc),
						&ostream->priv->pos);
		ostream->priv->is_initialized = TRUE;
	}

	if (ostream->priv->buflen > 0)
	{
		len = ostream->priv->buflen + count;
		text = g_new (gchar , len + 1);
		memcpy (text, ostream->priv->buffer, ostream->priv->buflen);
		memcpy (text + ostream->priv->buflen, buffer, count);
		text[len] = '\0';
		g_free (ostream->priv->buffer);
		ostream->priv->buffer = NULL;
		ostream->priv->buflen = 0;
		freetext = TRUE;
	}
	else
	{
		text = (gchar *) buffer;
		len = count;
	}

	/* validate */
	valid = g_utf8_validate (text, len, &end);

	/* Avoid keeping a CRLF across two buffers. */
	if (valid && len > 1 && end[-1] == '\r')
	{
		valid = FALSE;
		end--;
	}

	if (!valid)
	{
		gsize nvalid = end - text;
		gsize remainder = len - nvalid;
		gunichar ch;

		if ((remainder < MAX_UNICHAR_LEN) &&
		    ((ch = g_utf8_get_char_validated (text + nvalid, remainder)) == (gunichar)-2 ||
		     ch == (gunichar)'\r'))
		{
			ostream->priv->buffer = g_strndup (end, remainder);
			ostream->priv->buflen = remainder;
			len -= remainder;
		}
		else
		{
			/* TODO: we could escape invalid text and tag it in red
			 * and make the doc readonly.
			 */
			g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA,
				     _("Invalid UTF-8 sequence in input"));

			if (freetext)
				g_free (text);

			return -1;
		}
	}

	ctk_text_buffer_insert (CTK_TEXT_BUFFER (ostream->priv->doc),
				&ostream->priv->pos, text, len);

	if (freetext)
		g_free (text);

	return count;
}

static gboolean
lapiz_document_output_stream_flush (GOutputStream *stream,
                                    GCancellable  *cancellable,
                                    GError        **error)
{
	LapizDocumentOutputStream *ostream = LAPIZ_DOCUMENT_OUTPUT_STREAM (stream);

	/* Flush deferred data if some. */
	if (!ostream->priv->is_closed && ostream->priv->is_initialized &&
	    ostream->priv->buflen > 0 &&
	    lapiz_document_output_stream_write (stream, "", 0, cancellable,
						error) == -1)
		return FALSE;

	return TRUE;
}

static gboolean
lapiz_document_output_stream_close (GOutputStream     *stream,
				    GCancellable      *cancellable G_GNUC_UNUSED,
				    GError           **error)
{
	LapizDocumentOutputStream *ostream = LAPIZ_DOCUMENT_OUTPUT_STREAM (stream);

	if (!ostream->priv->is_closed && ostream->priv->is_initialized)
	{
		end_append_text_to_document (ostream);
		ostream->priv->is_closed = TRUE;
	}

	if (ostream->priv->buflen > 0)
	{
		g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA,
			     _("Incomplete UTF-8 sequence in input"));
		return FALSE;
	}

	return TRUE;
}