Add some more cases to the app-id unit tests
[glib.git] / gio / gio-tool-info.c
blob8b86293d7b08c47445391a646153270174165a1a
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"
28 static gboolean writable = FALSE;
29 static gboolean filesystem = FALSE;
30 static char *attributes = NULL;
31 static gboolean nofollow_symlinks = FALSE;
33 static const GOptionEntry entries[] = {
34 { "query-writable", 'w', 0, G_OPTION_ARG_NONE, &writable, N_("List writable attributes"), NULL },
35 { "filesystem", 'f', 0, G_OPTION_ARG_NONE, &filesystem, N_("Get file system info"), NULL },
36 { "attributes", 'a', 0, G_OPTION_ARG_STRING, &attributes, N_("The attributes to get"), N_("ATTRIBUTES") },
37 { "nofollow-symlinks", 'n', 0, G_OPTION_ARG_NONE, &nofollow_symlinks, N_("Don’t follow symbolic links"), NULL },
38 { NULL }
41 static char *
42 escape_string (const char *in)
44 GString *str;
45 static char *hex_digits = "0123456789abcdef";
46 unsigned char c;
49 str = g_string_new ("");
51 while ((c = *in++) != 0)
53 if (c >= 32 && c <= 126 && c != '\\')
54 g_string_append_c (str, c);
55 else
57 g_string_append (str, "\\x");
58 g_string_append_c (str, hex_digits[(c >> 4) & 0xf]);
59 g_string_append_c (str, hex_digits[c & 0xf]);
63 return g_string_free (str, FALSE);
66 static void
67 show_attributes (GFileInfo *info)
69 char **attributes;
70 char *s;
71 int i;
73 attributes = g_file_info_list_attributes (info, NULL);
75 g_print (_("attributes:\n"));
76 for (i = 0; attributes[i] != NULL; i++)
78 /* list the icons in order rather than displaying "GThemedIcon:0x8df7200" */
79 if (strcmp (attributes[i], "standard::icon") == 0 ||
80 strcmp (attributes[i], "standard::symbolic-icon") == 0)
82 GIcon *icon;
83 int j;
84 const char * const *names = NULL;
86 if (strcmp (attributes[i], "standard::symbolic-icon") == 0)
87 icon = g_file_info_get_symbolic_icon (info);
88 else
89 icon = g_file_info_get_icon (info);
91 /* only look up names if GThemedIcon */
92 if (G_IS_THEMED_ICON(icon))
94 names = g_themed_icon_get_names (G_THEMED_ICON (icon));
95 g_print (" %s: ", attributes[i]);
96 for (j = 0; names[j] != NULL; j++)
97 g_print ("%s%s", names[j], (names[j+1] == NULL)?"":", ");
98 g_print ("\n");
100 else
102 s = g_file_info_get_attribute_as_string (info, attributes[i]);
103 g_print (" %s: %s\n", attributes[i], s);
104 g_free (s);
107 else
109 s = g_file_info_get_attribute_as_string (info, attributes[i]);
110 g_print (" %s: %s\n", attributes[i], s);
111 g_free (s);
114 g_strfreev (attributes);
117 static void
118 show_info (GFile *file, GFileInfo *info)
120 const char *name, *type;
121 char *escaped, *uri;
122 goffset size;
124 name = g_file_info_get_display_name (info);
125 if (name)
126 /* Translators: This is a noun and represents and attribute of a file */
127 g_print (_("display name: %s\n"), name);
129 name = g_file_info_get_edit_name (info);
130 if (name)
131 /* Translators: This is a noun and represents and attribute of a file */
132 g_print (_("edit name: %s\n"), name);
134 name = g_file_info_get_name (info);
135 if (name)
137 escaped = escape_string (name);
138 g_print (_("name: %s\n"), escaped);
139 g_free (escaped);
142 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
144 type = file_type_to_string (g_file_info_get_file_type (info));
145 g_print (_("type: %s\n"), type);
148 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
150 size = g_file_info_get_size (info);
151 g_print (_("size: "));
152 g_print (" %"G_GUINT64_FORMAT"\n", (guint64)size);
155 if (g_file_info_get_is_hidden (info))
156 g_print (_("hidden\n"));
158 uri = g_file_get_uri (file);
159 g_print (_("uri: %s\n"), uri);
160 g_free (uri);
162 show_attributes (info);
165 static gboolean
166 query_info (GFile *file)
168 GFileQueryInfoFlags flags;
169 GFileInfo *info;
170 GError *error;
172 if (file == NULL)
173 return FALSE;
175 if (attributes == NULL)
176 attributes = "*";
178 flags = 0;
179 if (nofollow_symlinks)
180 flags |= G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS;
182 error = NULL;
183 if (filesystem)
184 info = g_file_query_filesystem_info (file, attributes, NULL, &error);
185 else
186 info = g_file_query_info (file, attributes, flags, NULL, &error);
188 if (info == NULL)
190 g_printerr ("Error getting info: %s\n", error->message);
191 g_error_free (error);
192 return FALSE;
195 if (filesystem)
196 show_attributes (info);
197 else
198 show_info (file, info);
200 g_object_unref (info);
202 return TRUE;
205 static gboolean
206 get_writable_info (GFile *file)
208 GFileAttributeInfoList *list;
209 GError *error;
210 int i;
211 char *flags;
213 if (file == NULL)
214 return FALSE;
216 error = NULL;
218 list = g_file_query_settable_attributes (file, NULL, &error);
219 if (list == NULL)
221 g_printerr (_("Error getting writable attributes: %s\n"), error->message);
222 g_error_free (error);
223 return FALSE;
226 g_print (_("Settable attributes:\n"));
227 for (i = 0; i < list->n_infos; i++)
229 flags = attribute_flags_to_string (list->infos[i].flags);
230 g_print (" %s (%s%s%s)\n",
231 list->infos[i].name,
232 attribute_type_to_string (list->infos[i].type),
233 (*flags != 0)?", ":"", flags);
234 g_free (flags);
237 g_file_attribute_info_list_unref (list);
239 list = g_file_query_writable_namespaces (file, NULL, &error);
240 if (list == NULL)
242 g_printerr ("Error getting writable namespaces: %s\n", error->message);
243 g_error_free (error);
244 return FALSE;
247 if (list->n_infos > 0)
249 g_print (_("Writable attribute namespaces:\n"));
250 for (i = 0; i < list->n_infos; i++)
252 flags = attribute_flags_to_string (list->infos[i].flags);
253 g_print (" %s (%s%s%s)\n",
254 list->infos[i].name,
255 attribute_type_to_string (list->infos[i].type),
256 (*flags != 0)?", ":"", flags);
260 g_file_attribute_info_list_unref (list);
262 return TRUE;
266 handle_info (int argc, char *argv[], gboolean do_help)
268 GOptionContext *context;
269 gchar *param;
270 GError *error = NULL;
271 gboolean res;
272 gint i;
273 GFile *file;
275 g_set_prgname ("gio info");
277 /* Translators: commandline placeholder */
278 param = g_strdup_printf ("%s...", _("LOCATION"));
279 context = g_option_context_new (param);
280 g_free (param);
281 g_option_context_set_help_enabled (context, FALSE);
282 g_option_context_set_summary (context,
283 _("Show information about locations."));
284 g_option_context_set_description (context,
285 _("gio info is similar to the traditional ls utility, but using GIO\n"
286 "locations instead of local files: for example, you can use something\n"
287 "like smb://server/resource/file.txt as location. File attributes can\n"
288 "be specified with their GIO name, e.g. standard::icon, or just by\n"
289 "namespace, e.g. unix, or by “*”, which matches all attributes"));
290 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
292 if (do_help)
294 show_help (context, NULL);
295 return 0;
298 if (!g_option_context_parse (context, &argc, &argv, &error))
300 show_help (context, error->message);
301 g_error_free (error);
302 return 1;
305 if (argc < 2)
307 show_help (context, _("No locations given"));
308 return 1;
311 g_option_context_free (context);
313 res = TRUE;
314 for (i = 1; i < argc; i++)
316 file = g_file_new_for_commandline_arg (argv[i]);
317 if (writable)
318 res &= get_writable_info (file);
319 else
320 res &= query_info (file);
321 g_object_unref (file);
324 return res ? 0 : 2;