Add Friulian translation
[glib.git] / gio / gio-tool-monitor.c
blob9e43d99a64fb5bfb40eb6750bcdb8c24f5bd58aa
1 /*
2 * Copyright 2015 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the licence, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 * Author: Matthias Clasen <mclasen@redhat.com>
20 #include "config.h"
22 #include <gio/gio.h>
23 #include <gi18n.h>
25 #include "gio-tool.h"
27 static gchar **watch_dirs;
28 static gchar **watch_files;
29 static gchar **watch_direct;
30 static gchar **watch_silent;
31 static gchar **watch_default;
32 static gboolean no_moves;
33 static gboolean mounts;
35 static const GOptionEntry entries[] = {
36 { "dir", 'd', 0, G_OPTION_ARG_FILENAME_ARRAY, &watch_dirs,
37 N_("Monitor a directory (default: depends on type)"), N_("LOCATION") },
38 { "file", 'f', 0, G_OPTION_ARG_FILENAME_ARRAY, &watch_files,
39 N_("Monitor a file (default: depends on type)"), N_("LOCATION") },
40 { "direct", 'D', 0, G_OPTION_ARG_FILENAME_ARRAY, &watch_direct,
41 N_("Monitor a file directly (notices changes made via hardlinks)"), N_("LOCATION") },
42 { "silent", 's', 0, G_OPTION_ARG_FILENAME_ARRAY, &watch_silent,
43 N_("Monitors a file directly, but doesn’t report changes"), N_("LOCATION") },
44 { "no-moves", 'n', 0, G_OPTION_ARG_NONE, &no_moves,
45 N_("Report moves and renames as simple deleted/created events"), NULL },
46 { "mounts", 'm', 0, G_OPTION_ARG_NONE, &mounts,
47 N_("Watch for mount events"), NULL },
48 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &watch_default },
49 { NULL }
52 static void
53 watch_callback (GFileMonitor *monitor,
54 GFile *child,
55 GFile *other,
56 GFileMonitorEvent event_type,
57 gpointer user_data)
59 gchar *child_str;
60 gchar *other_str;
62 g_assert (child);
64 if (g_file_is_native (child))
65 child_str = g_file_get_path (child);
66 else
67 child_str = g_file_get_uri (child);
69 if (other)
71 if (g_file_is_native (other))
72 other_str = g_file_get_path (other);
73 else
74 other_str = g_file_get_uri (other);
76 else
77 other_str = g_strdup ("(none)");
79 g_print ("%s: ", (gchar *) user_data);
80 switch (event_type)
82 case G_FILE_MONITOR_EVENT_CHANGED:
83 g_assert (!other);
84 g_print ("%s: changed", child_str);
85 break;
86 case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
87 g_assert (!other);
88 g_print ("%s: changes done", child_str);
89 break;
90 case G_FILE_MONITOR_EVENT_DELETED:
91 g_assert (!other);
92 g_print ("%s: deleted", child_str);
93 break;
94 case G_FILE_MONITOR_EVENT_CREATED:
95 g_assert (!other);
96 g_print ("%s: created", child_str);
97 break;
98 case G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED:
99 g_assert (!other);
100 g_print ("%s: attributes changed", child_str);
101 break;
102 case G_FILE_MONITOR_EVENT_PRE_UNMOUNT:
103 g_assert (!other);
104 g_print ("%s: pre-unmount", child_str);
105 break;
106 case G_FILE_MONITOR_EVENT_UNMOUNTED:
107 g_assert (!other);
108 g_print ("%s: unmounted", child_str);
109 break;
110 case G_FILE_MONITOR_EVENT_MOVED_IN:
111 g_print ("%s: moved in", child_str);
112 if (other)
113 g_print (" (from %s)", other_str);
114 break;
115 case G_FILE_MONITOR_EVENT_MOVED_OUT:
116 g_print ("%s: moved out", child_str);
117 if (other)
118 g_print (" (to %s)", other_str);
119 break;
120 case G_FILE_MONITOR_EVENT_RENAMED:
121 g_assert (other);
122 g_print ("%s: renamed to %s\n", child_str, other_str);
123 break;
125 case G_FILE_MONITOR_EVENT_MOVED:
126 default:
127 g_assert_not_reached ();
130 g_free (child_str);
131 g_free (other_str);
132 g_print ("\n");
135 typedef enum
137 WATCH_DIR,
138 WATCH_FILE,
139 WATCH_AUTO
140 } WatchType;
142 static gboolean
143 add_watch (const gchar *cmdline,
144 WatchType watch_type,
145 GFileMonitorFlags flags,
146 gboolean connect_handler)
148 GFileMonitor *monitor = NULL;
149 GError *error = NULL;
150 GFile *file;
152 file = g_file_new_for_commandline_arg (cmdline);
154 if (watch_type == WATCH_AUTO)
156 GFileInfo *info;
157 guint32 type;
159 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, G_FILE_QUERY_INFO_NONE, NULL, &error);
160 if (!info)
161 goto err;
163 type = g_file_info_get_attribute_uint32 (info, G_FILE_ATTRIBUTE_STANDARD_TYPE);
164 watch_type = (type == G_FILE_TYPE_DIRECTORY) ? WATCH_DIR : WATCH_FILE;
167 if (watch_type == WATCH_DIR)
168 monitor = g_file_monitor_directory (file, flags, NULL, &error);
169 else
170 monitor = g_file_monitor (file, flags, NULL, &error);
172 if (!monitor)
173 goto err;
175 if (connect_handler)
176 g_signal_connect (monitor, "changed", G_CALLBACK (watch_callback), g_strdup (cmdline));
178 monitor = NULL; /* leak */
180 return TRUE;
182 err:
183 g_printerr ("error: %s: %s", cmdline, error->message);
184 g_error_free (error);
186 return FALSE;
190 handle_monitor (int argc, gchar *argv[], gboolean do_help)
192 GOptionContext *context;
193 gchar *param;
194 GError *error = NULL;
195 GFileMonitorFlags flags;
196 guint total = 0;
197 guint i;
199 g_set_prgname ("gio monitor");
201 /* Translators: commandline placeholder */
202 param = g_strdup_printf ("[%s...]", _("LOCATION"));
203 context = g_option_context_new (param);
204 g_free (param);
205 g_option_context_set_help_enabled (context, FALSE);
206 g_option_context_set_summary (context,
207 _("Monitor files or directories for changes."));
208 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
210 if (do_help)
212 show_help (context, NULL);
213 return 0;
216 if (!g_option_context_parse (context, &argc, &argv, &error))
218 show_help (context, error->message);
219 g_error_free (error);
220 return 1;
223 g_option_context_free (context);
225 flags = (no_moves ? 0 : G_FILE_MONITOR_WATCH_MOVES) |
226 (mounts ? G_FILE_MONITOR_WATCH_MOUNTS : 0);
228 if (watch_dirs)
230 for (i = 0; watch_dirs[i]; i++)
231 if (!add_watch (watch_dirs[i], WATCH_DIR, flags, TRUE))
232 return 1;
233 total++;
236 if (watch_files)
238 for (i = 0; watch_files[i]; i++)
239 if (!add_watch (watch_files[i], WATCH_FILE, flags, TRUE))
240 return 1;
241 total++;
244 if (watch_direct)
246 for (i = 0; watch_direct[i]; i++)
247 if (!add_watch (watch_direct[i], WATCH_FILE, flags | G_FILE_MONITOR_WATCH_HARD_LINKS, TRUE))
248 return 1;
249 total++;
252 if (watch_silent)
254 for (i = 0; watch_silent[i]; i++)
255 if (!add_watch (watch_silent[i], WATCH_FILE, flags | G_FILE_MONITOR_WATCH_HARD_LINKS, FALSE))
256 return 1;
257 total++;
260 if (watch_default)
262 for (i = 0; watch_default[i]; i++)
263 if (!add_watch (watch_default[i], WATCH_AUTO, flags, TRUE))
264 return 1;
265 total++;
268 if (!total)
270 g_printerr ("gio: Must give at least one file to monitor\n");
271 return 1;
274 while (TRUE)
275 g_main_context_iteration (NULL, TRUE);
277 return 0;