1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* test-mime.c - Test for the mime handler detection features of the GNOME
3 Virtual File System Library
5 Copyright (C) 2000 Eazel
7 The Gnome Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library 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.
12 The Gnome Library 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 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with the Gnome Library; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
22 Author: Maciej Stachowiak <mjs@eazel.com>
27 #include <libgnomevfs/gnome-vfs-application-registry.h>
28 #include <libgnomevfs/gnome-vfs-init.h>
29 #include <libgnomevfs/gnome-vfs-mime-handlers.h>
36 usage (const char *name)
38 fprintf (stderr, "Usage: %s mime_type field value\n", name);
39 fprintf (stderr, "Valid field values are: \n");
40 fprintf (stderr, "\tdefault_action_type\n");
41 fprintf (stderr, "\tdefault_application\n");
42 fprintf (stderr, "\tdefault_component\n");
43 fprintf (stderr, "\tshort_list_applicationss\n");
44 fprintf (stderr, "\tshort_list_components\n");
45 fprintf (stderr, "\tadd_to_all_applicationss\n");
46 fprintf (stderr, "\tremove_from_all_applications\n");
47 fprintf (stderr, "\tdefine_application\n");
51 static GnomeVFSMimeActionType
52 str_to_action_type (const char *str)
54 if (g_ascii_strcasecmp (str, "component") == 0) {
55 return GNOME_VFS_MIME_ACTION_TYPE_COMPONENT;
56 } else if (g_ascii_strcasecmp (str, "application") == 0) {
57 return GNOME_VFS_MIME_ACTION_TYPE_APPLICATION;
59 return GNOME_VFS_MIME_ACTION_TYPE_NONE;
64 strsplit_handle_null (const char *str, const char *delim, int max)
66 return g_strsplit ((str == NULL ? "" : str), delim, max);
70 static GList *gnome_vfs_strsplit_to_list (const char *str, const char *delim, int max)
76 strv = strsplit_handle_null (str, delim, max);
80 for (i = 0; strv[i] != NULL; i++) {
81 retval = g_list_prepend (retval, strv[i]);
84 retval = g_list_reverse (retval);
85 /* Don't strfreev, since we didn't copy the individual strings. */
91 static GList *comma_separated_str_to_str_list (const char *str)
93 return gnome_vfs_strsplit_to_list (str, ",", 0);
98 str_to_bool (const char *str)
100 return ((str != NULL) &&
101 ((g_ascii_strcasecmp (str, "true") == 0) ||
102 (g_ascii_strcasecmp (str, "yes") == 0)));
107 main (int argc, char **argv)
123 if (strcmp (field, "default_action_type") == 0) {
124 puts ("default_action_type");
125 gnome_vfs_mime_set_default_action_type (type, str_to_action_type (value));
126 } else if (strcmp (field, "default_application") == 0) {
127 puts ("default_application");
128 gnome_vfs_mime_set_default_application (type, value);
129 } else if (strcmp (field, "default_component") == 0) {
130 puts ("default_component");
131 gnome_vfs_mime_set_default_component (type, value);
132 } else if (strcmp (field, "short_list_applicationss") == 0) {
133 puts ("short_list_applications");
134 gnome_vfs_mime_set_short_list_applications (type,
135 comma_separated_str_to_str_list (value));
136 } else if (strcmp (field, "short_list_components") == 0) {
137 puts ("short_list_components");
138 gnome_vfs_mime_set_short_list_components (type,
139 comma_separated_str_to_str_list (value));
140 } else if (strcmp (field, "add_to_all_applicationss") == 0) {
141 puts ("add_to_all_applications");
142 gnome_vfs_mime_extend_all_applications (type,
143 comma_separated_str_to_str_list (value));
144 } else if (strcmp (field, "remove_from_all_applications") == 0) {
145 puts ("remove_from_all_applications");
146 gnome_vfs_mime_remove_from_all_applications (type,
147 comma_separated_str_to_str_list (value));
149 } else if (strcmp (field, "define_application") == 0) {
151 GnomeVFSMimeApplication app;
153 stuff = comma_separated_str_to_str_list (value);
155 app.id = g_list_nth (stuff, 0)->data;
156 app.name = g_list_nth (stuff, 1)->data;
157 app.command = g_list_nth (stuff, 2)->data;
158 app.can_open_multiple_files = str_to_bool (g_list_nth (stuff, 3)->data);
159 app.expects_uris = str_to_bool (g_list_nth (stuff, 4)->data);
160 app.requires_terminal = str_to_bool (g_list_nth (stuff, 5)->data);
162 gnome_vfs_application_registry_save_mime_application (&app);
163 gnome_vfs_application_registry_add_mime_type (app.id, type);
164 gnome_vfs_application_registry_sync ();