Debian Codegraph @

src/hex-file-monitor.c

raw symbols SCIP
1 /* vim: ts=4 sw=4 colorcolumn=80
2 */
3 /* hex-file-monitor.c - Implementation of file monitor object
4 *
5 * Copyright © 2025 Jordan Christiansen
6 *
7 * GHex is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
11 *
12 * GHex is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with GHex; see the file COPYING.
19 * If not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 * Original GHex Author: Jaka Mocnik
23 */
24
25 #include <stdio.h>
26
27 #include "hex-file-monitor.h"
28
29 #include <glib-object.h>
30 #include <glib.h>
31
32 struct _HexFileMonitor
33 {
34 GObject parent;
35 GFile *file;
36 GFileMonitor *monitor;
37 gboolean changed;
38 };
39 G_DEFINE_TYPE (HexFileMonitor, hex_file_monitor, G_TYPE_OBJECT)
40
41 typedef enum {
42 PROP_CHANGED = 1,
43 PROP_LAST,
44 } HexFileMonitorProperty;
45
46 static GParamSpec *properties[PROP_LAST];
47
48 static void
49 hex_file_monitor_changed_cb (HexFileMonitor *self,
50 GFile *file,
51 GFile *other_file,
52 GFileMonitorEvent event,
53 GFileMonitor *monitor)
54 {
55 if (event == G_FILE_MONITOR_EVENT_CHANGED) {
56 g_debug ("%s: File %s changed", __func__, g_file_peek_path(file));
57 self->changed = TRUE;
58 g_object_notify_by_pspec(G_OBJECT (self), properties[PROP_CHANGED]);
59 }
60 }
61
62 HexFileMonitor *
63 hex_file_monitor_new (GFile *file)
64 {
65 HexFileMonitor *self;
66
67 g_return_val_if_fail (G_IS_FILE (file), NULL);
68
69 self = g_object_new (HEX_TYPE_FILE_MONITOR, NULL);
70 g_return_val_if_fail (self, NULL);
71
72 self->file = g_object_ref (file);
73
74 self->monitor = g_file_monitor_file (self->file, G_FILE_MONITOR_WATCH_MOVES, NULL, NULL);
75
76 if (self->monitor == NULL)
77 {
78 g_warning ("%s: Failed to set up file monitor.", __func__);
79 g_clear_object (&self);
80 return self;
81 }
82
83 g_file_monitor_set_rate_limit (self->monitor, 500);
84 g_signal_connect_object (
85 self->monitor,
86 "changed",
87 G_CALLBACK (hex_file_monitor_changed_cb),
88 self,
89 G_CONNECT_SWAPPED
90 );
91
92 g_debug ("%s: Set up watch on %s", __func__, g_file_peek_path(file));
93
94 return self;
95 }
96
97 /**
98 * hex_file_monitor_get_changed:
99 * @self: a [[email protected]] object
100 *
101 * Check whether the monitored file has changed.
102 *
103 * Returns: %TRUE if the file has changed; %FALSE otherwise
104 */
105 gboolean
106 hex_file_monitor_get_changed (HexFileMonitor *self)
107 {
108 g_return_val_if_fail (HEX_IS_FILE_MONITOR (self), FALSE);
109
110 return self->changed;
111 }
112
113 /**
114 * hex_file_monitor_reset:
115 * @self: a [[email protected]] object
116 *
117 * Clear any previously seen file change events and begin watching for changes again.
118 */
119 void
120 hex_file_monitor_reset (HexFileMonitor *self)
121 {
122 g_return_if_fail (HEX_IS_FILE_MONITOR (self));
123
124 self->changed = FALSE;
125 }
126
127 static void
128 hex_file_monitor_init (HexFileMonitor *self)
129 {
130 }
131
132 static void
133 hex_file_monitor_set_property (GObject *object,
134 guint property_id,
135 const GValue *value,
136 GParamSpec *pspec)
137 {
138 HexFileMonitor *monitor = HEX_FILE_MONITOR (object);
139
140 switch (property_id)
141 {
142 default:
143 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
144 break;
145 }
146 }
147
148 static void
149 hex_file_monitor_get_property (GObject *object,
150 guint property_id,
151 GValue *value,
152 GParamSpec *pspec)
153 {
154 HexFileMonitor *monitor = HEX_FILE_MONITOR (object);
155
156 switch (property_id)
157 {
158 case PROP_CHANGED:
159 g_value_set_boolean(value, hex_file_monitor_get_changed(monitor));
160 break;
161
162 default:
163 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
164 break;
165 }
166 }
167
168 static void
169 hex_file_monitor_dispose (GObject *object)
170 {
171 HexFileMonitor *monitor = HEX_FILE_MONITOR (object);
172
173 g_clear_object (&monitor->file);
174 g_clear_object (&monitor->monitor);
175
176 G_OBJECT_CLASS (hex_file_monitor_parent_class)->dispose (object);
177 }
178
179 static void
180 hex_file_monitor_finalize (GObject *object)
181 {
182 G_OBJECT_CLASS (hex_file_monitor_parent_class)->finalize (object);
183 }
184
185 static void
186 hex_file_monitor_class_init (HexFileMonitorClass *klass)
187 {
188 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
189
190 gobject_class->set_property = hex_file_monitor_set_property;
191 gobject_class->get_property = hex_file_monitor_get_property;
192 gobject_class->dispose = hex_file_monitor_dispose;
193 gobject_class->finalize = hex_file_monitor_finalize;
194
195 properties [PROP_CHANGED] =
196 g_param_spec_boolean ("changed",
197 "Changed",
198 "If the open file changed on disk since it was last read",
199 FALSE,
200 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
201
202 g_object_class_install_properties (gobject_class, PROP_LAST, properties);
203 }
204