File: note.c

package info (click to toggle)
xfce4-notes-plugin 1.11.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,292 kB
  • sloc: ansic: 13,235; sh: 5,064; makefile: 303; sed: 16
file content (411 lines) | stat: -rw-r--r-- 13,163 bytes parent folder | download
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
/* note.c generated by valac 0.56.17, the Vala compiler
 * generated from note.vala, do not modify */

/*
 *  Notes - panel plugin for Xfce Desktop Environment
 *  Copyright (c) 2009-2010  Mike Massonnet <mmassonnet@xfce.org>
 *
 *  This program 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.
 *
 *  This program 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 Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "libnotes.h"
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <glib-object.h>
#include <gtk/gtk.h>
#include <gtksourceview/gtksource.h>

#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif

enum  {
	XNP_NOTE_0_PROPERTY,
	XNP_NOTE_NAME_PROPERTY,
	XNP_NOTE_TEXT_PROPERTY,
	XNP_NOTE_DIRTY_PROPERTY,
	XNP_NOTE_NUM_PROPERTIES
};
static GParamSpec* xnp_note_properties[XNP_NOTE_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
enum  {
	XNP_NOTE_SAVE_DATA_SIGNAL,
	XNP_NOTE_NUM_SIGNALS
};
static guint xnp_note_signals[XNP_NOTE_NUM_SIGNALS] = {0};

struct _XnpNotePrivate {
	gchar* _name;
	guint save_timeout;
	gboolean _dirty;
};

static gint XnpNote_private_offset;
static gpointer xnp_note_parent_class = NULL;

static void xnp_note_buffer_changed_cb (XnpNote* self);
static void _xnp_note_buffer_changed_cb_gtk_text_buffer_changed (GtkTextBuffer* _sender,
                                                          gpointer self);
static gboolean xnp_note_save_cb (XnpNote* self);
static gboolean _xnp_note_save_cb_gsource_func (gpointer self);
static void xnp_note_finalize (GObject * obj);
static GType xnp_note_get_type_once (void);
static void _vala_xnp_note_get_property (GObject * object,
                                  guint property_id,
                                  GValue * value,
                                  GParamSpec * pspec);
static void _vala_xnp_note_set_property (GObject * object,
                                  guint property_id,
                                  const GValue * value,
                                  GParamSpec * pspec);

static inline gpointer
xnp_note_get_instance_private (XnpNote* self)
{
	return G_STRUCT_MEMBER_P (self, XnpNote_private_offset);
}

void
xnp_note_save (XnpNote* self)
{
	gboolean _tmp0_;
	gboolean _tmp1_;
	g_return_if_fail (self != NULL);
	_tmp0_ = xnp_note_get_dirty (self);
	_tmp1_ = _tmp0_;
	if (_tmp1_) {
		g_signal_emit (self, xnp_note_signals[XNP_NOTE_SAVE_DATA_SIGNAL], 0);
	}
}

static gpointer
_g_object_ref0 (gpointer self)
{
	return self ? g_object_ref (self) : NULL;
}

static void
_xnp_note_buffer_changed_cb_gtk_text_buffer_changed (GtkTextBuffer* _sender,
                                                     gpointer self)
{
	xnp_note_buffer_changed_cb ((XnpNote*) self);
}

XnpNote*
xnp_note_construct (GType object_type,
                    const gchar* name)
{
	XnpNote * self = NULL;
	XnpHypertextView* _tmp0_;
	XnpHypertextView* _tmp1_;
	XnpHypertextView* _tmp2_;
	XnpHypertextView* _tmp3_;
	XnpHypertextView* _tmp4_;
	XnpHypertextView* _tmp5_;
	XnpHypertextView* _tmp6_;
	XnpHypertextView* _tmp7_;
	GtkTextBuffer* buffer = NULL;
	XnpHypertextView* _tmp8_;
	GtkTextBuffer* _tmp9_;
	GtkTextBuffer* _tmp10_;
	g_return_val_if_fail (name != NULL, NULL);
	self = (XnpNote*) g_object_new (object_type, NULL);
	xnp_note_set_name (self, name);
	gtk_scrolled_window_set_policy ((GtkScrolledWindow*) self, GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
	_tmp0_ = xnp_hypertext_view_new ();
	g_object_ref_sink (_tmp0_);
	_g_object_unref0 (self->text_view);
	self->text_view = _tmp0_;
	_tmp1_ = self->text_view;
	gtk_widget_show ((GtkWidget*) _tmp1_);
	_tmp2_ = self->text_view;
	gtk_text_view_set_wrap_mode ((GtkTextView*) _tmp2_, GTK_WRAP_WORD);
	_tmp3_ = self->text_view;
	gtk_text_view_set_left_margin ((GtkTextView*) _tmp3_, 2);
	_tmp4_ = self->text_view;
	gtk_text_view_set_right_margin ((GtkTextView*) _tmp4_, 2);
	_tmp5_ = self->text_view;
	gtk_text_view_set_pixels_above_lines ((GtkTextView*) _tmp5_, 1);
	_tmp6_ = self->text_view;
	gtk_text_view_set_pixels_below_lines ((GtkTextView*) _tmp6_, 1);
	_tmp7_ = self->text_view;
	gtk_container_add ((GtkContainer*) self, (GtkWidget*) _tmp7_);
	_tmp8_ = self->text_view;
	_tmp9_ = gtk_text_view_get_buffer ((GtkTextView*) _tmp8_);
	_tmp10_ = _g_object_ref0 (_tmp9_);
	buffer = _tmp10_;
	g_signal_connect_object (buffer, "changed", (GCallback) _xnp_note_buffer_changed_cb_gtk_text_buffer_changed, self, 0);
	_g_object_unref0 (buffer);
	return self;
}

XnpNote*
xnp_note_new (const gchar* name)
{
	return xnp_note_construct (XNP_TYPE_NOTE, name);
}

/**
 * buffer_changed_cb:
 *
 * Reset the save_timeout as long as the buffer is under constant
 * changes and send the save-data signal.
 */
static void
xnp_note_buffer_changed_cb (XnpNote* self)
{
	g_return_if_fail (self != NULL);
	xnp_note_set_dirty (self, TRUE);
}

/**
 * save_cb:
 *
 * Send save-data signal.
 */
static gboolean
xnp_note_save_cb (XnpNote* self)
{
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	self->priv->save_timeout = (guint) 0;
	g_signal_emit (self, xnp_note_signals[XNP_NOTE_SAVE_DATA_SIGNAL], 0);
	result = FALSE;
	return result;
}

const gchar*
xnp_note_get_name (XnpNote* self)
{
	const gchar* result;
	const gchar* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_name;
	result = _tmp0_;
	return result;
}

void
xnp_note_set_name (XnpNote* self,
                   const gchar* value)
{
	gchar* old_value;
	g_return_if_fail (self != NULL);
	old_value = xnp_note_get_name (self);
	if (g_strcmp0 (value, old_value) != 0) {
		gchar* _tmp0_;
		_tmp0_ = g_strdup (value);
		_g_free0 (self->priv->_name);
		self->priv->_name = _tmp0_;
		g_object_notify_by_pspec ((GObject *) self, xnp_note_properties[XNP_NOTE_NAME_PROPERTY]);
	}
}

gchar*
xnp_note_get_text (XnpNote* self)
{
	gchar* result;
	XnpHypertextView* _tmp0_;
	GtkTextBuffer* _tmp1_;
	GtkTextBuffer* _tmp2_;
	gchar* _tmp3_;
	gchar* _tmp4_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->text_view;
	_tmp1_ = gtk_text_view_get_buffer ((GtkTextView*) _tmp0_);
	_tmp2_ = _tmp1_;
	g_object_get (_tmp2_, "text", &_tmp3_, NULL);
	_tmp4_ = _tmp3_;
	result = _tmp4_;
	return result;
}

void
xnp_note_set_text (XnpNote* self,
                   const gchar* value)
{
	GtkSourceBuffer* buffer = NULL;
	XnpHypertextView* _tmp0_;
	GtkTextBuffer* _tmp1_;
	GtkTextBuffer* _tmp2_;
	GtkSourceBuffer* _tmp3_;
	XnpHypertextView* _tmp4_;
	GtkTextBuffer* _tmp5_;
	GtkTextBuffer* _tmp6_;
	XnpHypertextView* _tmp7_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->text_view;
	_tmp1_ = gtk_text_view_get_buffer ((GtkTextView*) _tmp0_);
	_tmp2_ = _tmp1_;
	_tmp3_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_TYPE (_tmp2_, gtk_source_buffer_get_type ()) ? ((GtkSourceBuffer*) _tmp2_) : NULL);
	buffer = _tmp3_;
	gtk_source_buffer_begin_not_undoable_action (buffer);
	_tmp4_ = self->text_view;
	_tmp5_ = gtk_text_view_get_buffer ((GtkTextView*) _tmp4_);
	_tmp6_ = _tmp5_;
	g_object_set (_tmp6_, "text", value, NULL);
	_tmp7_ = self->text_view;
	xnp_hypertext_view_update_tags (_tmp7_);
	gtk_source_buffer_end_not_undoable_action (buffer);
	xnp_note_set_dirty (self, FALSE);
	_g_object_unref0 (buffer);
	g_object_notify_by_pspec ((GObject *) self, xnp_note_properties[XNP_NOTE_TEXT_PROPERTY]);
}

gboolean
xnp_note_get_dirty (XnpNote* self)
{
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	result = self->priv->_dirty;
	return result;
}

static gboolean
_xnp_note_save_cb_gsource_func (gpointer self)
{
	gboolean result;
	result = xnp_note_save_cb ((XnpNote*) self);
	return result;
}

void
xnp_note_set_dirty (XnpNote* self,
                    gboolean value)
{
	g_return_if_fail (self != NULL);
	self->priv->_dirty = value;
	if (self->priv->save_timeout > ((guint) 0)) {
		g_source_remove (self->priv->save_timeout);
		self->priv->save_timeout = (guint) 0;
	}
	if (value) {
		self->priv->save_timeout = g_timeout_add_seconds_full (G_PRIORITY_DEFAULT, (guint) 60, _xnp_note_save_cb_gsource_func, g_object_ref (self), g_object_unref);
	}
	g_object_notify_by_pspec ((GObject *) self, xnp_note_properties[XNP_NOTE_DIRTY_PROPERTY]);
}

static void
xnp_note_class_init (XnpNoteClass * klass,
                     gpointer klass_data)
{
	xnp_note_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &XnpNote_private_offset);
	G_OBJECT_CLASS (klass)->get_property = _vala_xnp_note_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_xnp_note_set_property;
	G_OBJECT_CLASS (klass)->finalize = xnp_note_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), XNP_NOTE_NAME_PROPERTY, xnp_note_properties[XNP_NOTE_NAME_PROPERTY] = g_param_spec_string ("name", "name", "name", NULL, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE));
	g_object_class_install_property (G_OBJECT_CLASS (klass), XNP_NOTE_TEXT_PROPERTY, xnp_note_properties[XNP_NOTE_TEXT_PROPERTY] = g_param_spec_string ("text", "text", "text", NULL, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE));
	g_object_class_install_property (G_OBJECT_CLASS (klass), XNP_NOTE_DIRTY_PROPERTY, xnp_note_properties[XNP_NOTE_DIRTY_PROPERTY] = g_param_spec_boolean ("dirty", "dirty", "dirty", FALSE, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE));
	xnp_note_signals[XNP_NOTE_SAVE_DATA_SIGNAL] = g_signal_new ("save-data", XNP_TYPE_NOTE, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
}

static void
xnp_note_instance_init (XnpNote * self,
                        gpointer klass)
{
	self->priv = xnp_note_get_instance_private (self);
	self->backed = TRUE;
	self->priv->_dirty = FALSE;
}

static void
xnp_note_finalize (GObject * obj)
{
	XnpNote * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, XNP_TYPE_NOTE, XnpNote);
	xnp_note_set_dirty (self, FALSE);
	_g_object_unref0 (self->text_view);
	_g_free0 (self->priv->_name);
	G_OBJECT_CLASS (xnp_note_parent_class)->finalize (obj);
}

static GType
xnp_note_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (XnpNoteClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) xnp_note_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (XnpNote), 0, (GInstanceInitFunc) xnp_note_instance_init, NULL };
	GType xnp_note_type_id;
	xnp_note_type_id = g_type_register_static (gtk_scrolled_window_get_type (), "XnpNote", &g_define_type_info, 0);
	XnpNote_private_offset = g_type_add_instance_private (xnp_note_type_id, sizeof (XnpNotePrivate));
	return xnp_note_type_id;
}

GType
xnp_note_get_type (void)
{
	static volatile gsize xnp_note_type_id__once = 0;
	if (g_once_init_enter (&xnp_note_type_id__once)) {
		GType xnp_note_type_id;
		xnp_note_type_id = xnp_note_get_type_once ();
		g_once_init_leave (&xnp_note_type_id__once, xnp_note_type_id);
	}
	return xnp_note_type_id__once;
}

static void
_vala_xnp_note_get_property (GObject * object,
                             guint property_id,
                             GValue * value,
                             GParamSpec * pspec)
{
	XnpNote * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, XNP_TYPE_NOTE, XnpNote);
	switch (property_id) {
		case XNP_NOTE_NAME_PROPERTY:
		g_value_set_string (value, xnp_note_get_name (self));
		break;
		case XNP_NOTE_TEXT_PROPERTY:
		g_value_take_string (value, xnp_note_get_text (self));
		break;
		case XNP_NOTE_DIRTY_PROPERTY:
		g_value_set_boolean (value, xnp_note_get_dirty (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_xnp_note_set_property (GObject * object,
                             guint property_id,
                             const GValue * value,
                             GParamSpec * pspec)
{
	XnpNote * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, XNP_TYPE_NOTE, XnpNote);
	switch (property_id) {
		case XNP_NOTE_NAME_PROPERTY:
		xnp_note_set_name (self, g_value_get_string (value));
		break;
		case XNP_NOTE_TEXT_PROPERTY:
		xnp_note_set_text (self, g_value_get_string (value));
		break;
		case XNP_NOTE_DIRTY_PROPERTY:
		xnp_note_set_dirty (self, g_value_get_boolean (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}