Add some more cases to the app-id unit tests
[glib.git] / gio / gio-tool.c
blob2937d8e71f2858b8b6fcffc0accd5e4d543cbba7
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>
24 #include <locale.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <errno.h>
30 #include "gio-tool.h"
33 void
34 print_file_error (GFile *file, const char *message)
36 g_printerr ("gio: %s: %s\n", g_file_get_uri (file), message);
39 void
40 show_help (GOptionContext *context, const char *message)
42 char *help;
44 if (message)
45 g_printerr ("gio: %s\n\n", message);
47 help = g_option_context_get_help (context, TRUE, NULL);
48 g_printerr ("%s", help);
49 g_free (help);
52 const char *
53 file_type_to_string (GFileType type)
55 switch (type)
57 case G_FILE_TYPE_UNKNOWN:
58 return "unknown";
59 case G_FILE_TYPE_REGULAR:
60 return "regular";
61 case G_FILE_TYPE_DIRECTORY:
62 return "directory";
63 case G_FILE_TYPE_SYMBOLIC_LINK:
64 return "symlink";
65 case G_FILE_TYPE_SPECIAL:
66 return "special";
67 case G_FILE_TYPE_SHORTCUT:
68 return "shortcut";
69 case G_FILE_TYPE_MOUNTABLE:
70 return "mountable";
71 default:
72 return "invalid type";
76 const char *
77 attribute_type_to_string (GFileAttributeType type)
79 switch (type)
81 case G_FILE_ATTRIBUTE_TYPE_INVALID:
82 return "invalid";
83 case G_FILE_ATTRIBUTE_TYPE_STRING:
84 return "string";
85 case G_FILE_ATTRIBUTE_TYPE_BYTE_STRING:
86 return "bytestring";
87 case G_FILE_ATTRIBUTE_TYPE_BOOLEAN:
88 return "boolean";
89 case G_FILE_ATTRIBUTE_TYPE_UINT32:
90 return "uint32";
91 case G_FILE_ATTRIBUTE_TYPE_INT32:
92 return "int32";
93 case G_FILE_ATTRIBUTE_TYPE_UINT64:
94 return "uint64";
95 case G_FILE_ATTRIBUTE_TYPE_INT64:
96 return "int64";
97 case G_FILE_ATTRIBUTE_TYPE_OBJECT:
98 return "object";
99 default:
100 return "uknown type";
104 GFileAttributeType
105 attribute_type_from_string (const char *str)
107 if (strcmp (str, "string") == 0)
108 return G_FILE_ATTRIBUTE_TYPE_STRING;
109 if (strcmp (str, "stringv") == 0)
110 return G_FILE_ATTRIBUTE_TYPE_STRINGV;
111 if (strcmp (str, "bytestring") == 0)
112 return G_FILE_ATTRIBUTE_TYPE_BYTE_STRING;
113 if (strcmp (str, "boolean") == 0)
114 return G_FILE_ATTRIBUTE_TYPE_BOOLEAN;
115 if (strcmp (str, "uint32") == 0)
116 return G_FILE_ATTRIBUTE_TYPE_UINT32;
117 if (strcmp (str, "int32") == 0)
118 return G_FILE_ATTRIBUTE_TYPE_INT32;
119 if (strcmp (str, "uint64") == 0)
120 return G_FILE_ATTRIBUTE_TYPE_UINT64;
121 if (strcmp (str, "int64") == 0)
122 return G_FILE_ATTRIBUTE_TYPE_INT64;
123 if (strcmp (str, "object") == 0)
124 return G_FILE_ATTRIBUTE_TYPE_OBJECT;
125 if (strcmp (str, "unset") == 0)
126 return G_FILE_ATTRIBUTE_TYPE_INVALID;
127 return -1;
130 char *
131 attribute_flags_to_string (GFileAttributeInfoFlags flags)
133 GString *s;
134 int i;
135 gboolean first;
136 struct {
137 guint32 mask;
138 char *descr;
139 } flag_descr[] = {
141 G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE,
142 N_("Copy with file")
145 G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED,
146 N_("Keep with file when moved")
150 first = TRUE;
152 s = g_string_new ("");
153 for (i = 0; i < G_N_ELEMENTS (flag_descr); i++)
155 if (flags & flag_descr[i].mask)
157 if (!first)
158 g_string_append (s, ", ");
159 g_string_append (s, gettext (flag_descr[i].descr));
160 first = FALSE;
164 return g_string_free (s, FALSE);
167 gboolean
168 file_is_dir (GFile *file)
170 GFileInfo *info;
171 gboolean res;
173 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, 0, NULL, NULL);
174 res = info && g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY;
175 if (info)
176 g_object_unref (info);
177 return res;
181 static int
182 handle_version (int argc, char *argv[], gboolean do_help)
184 if (do_help || argc > 1)
186 if (!do_help)
187 g_printerr ("gio: %s\n\n", _("“version” takes no arguments"));
189 g_printerr ("%s\n", _("Usage:"));
190 g_printerr (" gio version\n");
191 g_printerr ("\n");
192 g_printerr ("%s\n", _("Print version information and exit."));
194 return do_help ? 0 : 2;
197 g_print ("%d.%d.%d\n", glib_major_version, glib_minor_version, glib_micro_version);
199 return 0;
202 static void
203 usage (void)
205 g_printerr ("%s\n", _("Usage:"));
206 g_printerr (" gio %s %s\n", _("COMMAND"), _("[ARGS...]"));
207 g_printerr ("\n");
208 g_printerr ("%s\n", _("Commands:"));
209 g_printerr (" help %s\n", _("Print help"));
210 g_printerr (" version %s\n", _("Print version"));
211 g_printerr (" cat %s\n", _("Concatenate files to standard output"));
212 g_printerr (" copy %s\n", _("Copy one or more files"));
213 g_printerr (" info %s\n", _("Show information about locations"));
214 g_printerr (" list %s\n", _("List the contents of locations"));
215 g_printerr (" mime %s\n", _("Get or set the handler for a mimetype"));
216 g_printerr (" mkdir %s\n", _("Create directories"));
217 g_printerr (" monitor %s\n", _("Monitor files and directories for changes"));
218 g_printerr (" mount %s\n", _("Mount or unmount the locations"));
219 g_printerr (" move %s\n", _("Move one or more files"));
220 g_printerr (" open %s\n", _("Open files with the default application"));
221 g_printerr (" rename %s\n", _("Rename a file"));
222 g_printerr (" remove %s\n", _("Delete one or more files"));
223 g_printerr (" save %s\n", _("Read from standard input and save"));
224 g_printerr (" set %s\n", _("Set a file attribute"));
225 g_printerr (" trash %s\n", _("Move files or directories to the trash"));
226 g_printerr (" tree %s\n", _("Lists the contents of locations in a tree"));
227 g_printerr ("\n");
228 g_printerr (_("Use %s to get detailed help.\n"), "“gio help COMMAND”");
229 exit (1);
233 main (int argc, char **argv)
235 const char *command;
236 gboolean do_help;
238 setlocale (LC_ALL, "");
239 textdomain (GETTEXT_PACKAGE);
240 bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
242 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
243 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
244 #endif
246 if (argc < 2)
248 usage ();
249 return 1;
252 command = argv[1];
253 argc -= 1;
254 argv += 1;
256 do_help = FALSE;
257 if (g_str_equal (command, "help"))
259 if (argc == 1)
261 usage ();
262 return 0;
264 else
266 command = argv[1];
267 do_help = TRUE;
270 else if (g_str_equal (command, "--help"))
272 usage ();
273 return 0;
275 else if (g_str_equal (command, "--version"))
276 command = "version";
278 if (g_str_equal (command, "version"))
279 return handle_version (argc, argv, do_help);
280 else if (g_str_equal (command, "cat"))
281 return handle_cat (argc, argv, do_help);
282 else if (g_str_equal (command, "copy"))
283 return handle_copy (argc, argv, do_help);
284 else if (g_str_equal (command, "info"))
285 return handle_info (argc, argv, do_help);
286 else if (g_str_equal (command, "list"))
287 return handle_list (argc, argv, do_help);
288 else if (g_str_equal (command, "mime"))
289 return handle_mime (argc, argv, do_help);
290 else if (g_str_equal (command, "mkdir"))
291 return handle_mkdir (argc, argv, do_help);
292 else if (g_str_equal (command, "monitor"))
293 return handle_monitor (argc, argv, do_help);
294 else if (g_str_equal (command, "mount"))
295 return handle_mount (argc, argv, do_help);
296 else if (g_str_equal (command, "move"))
297 return handle_move (argc, argv, do_help);
298 else if (g_str_equal (command, "open"))
299 return handle_open (argc, argv, do_help);
300 else if (g_str_equal (command, "rename"))
301 return handle_rename (argc, argv, do_help);
302 else if (g_str_equal (command, "remove"))
303 return handle_remove (argc, argv, do_help);
304 else if (g_str_equal (command, "save"))
305 return handle_save (argc, argv, do_help);
306 else if (g_str_equal (command, "set"))
307 return handle_set (argc, argv, do_help);
308 else if (g_str_equal (command, "trash"))
309 return handle_trash (argc, argv, do_help);
310 else if (g_str_equal (command, "tree"))
311 return handle_tree (argc, argv, do_help);
312 else
313 usage ();
315 return 1;