ftp://ftp.redhat.com/pub/redhat/linux/rawhide/SRPMS/SRPMS/gnome-vfs2-2.3.8-1.src.rpm
[gnome-vfs-httpcaptive.git] / test / test-mime-handlers-set.c
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
4
5    Copyright (C) 2000 Eazel
6
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.
11
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.
16
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.
21
22    Author: Maciej Stachowiak <mjs@eazel.com>
23 */
24
25 #include <config.h>
26
27 #include <libgnomevfs/gnome-vfs-application-registry.h>
28 #include <libgnomevfs/gnome-vfs-init.h>
29 #include <libgnomevfs/gnome-vfs-mime-handlers.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34
35 static void
36 usage (const char *name)
37 {
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");
48                 exit (1);
49 }
50
51 static GnomeVFSMimeActionType
52 str_to_action_type (const char *str)
53 {
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;
58         } else {
59                 return GNOME_VFS_MIME_ACTION_TYPE_NONE;
60         }
61 }
62
63 static char **
64 strsplit_handle_null (const char *str, const char *delim, int max)
65 {
66         return g_strsplit ((str == NULL ? "" : str), delim, max);
67 }
68
69
70 static GList *gnome_vfs_strsplit_to_list (const char *str, const char *delim, int max)
71 {
72         char **strv;
73         GList *retval;
74         int i;
75
76         strv = strsplit_handle_null (str, delim, max);
77
78         retval = NULL;
79
80         for (i = 0; strv[i] != NULL; i++) {
81                 retval = g_list_prepend (retval, strv[i]);
82         }
83
84         retval = g_list_reverse (retval);
85         /* Don't strfreev, since we didn't copy the individual strings. */
86         g_free (strv);
87
88         return retval;
89 }
90
91 static GList *comma_separated_str_to_str_list (const char *str)
92 {
93         return gnome_vfs_strsplit_to_list (str, ",", 0);
94 }
95
96
97 static gboolean
98 str_to_bool (const char *str)
99 {
100         return ((str != NULL) &&
101                 ((g_ascii_strcasecmp (str, "true") == 0) || 
102                  (g_ascii_strcasecmp (str, "yes") == 0)));
103 }
104
105
106 int
107 main (int argc, char **argv)
108 {
109         const char *type;  
110         const char *field;
111         const char *value;
112
113         gnome_vfs_init ();
114
115         if (argc < 3) {
116                 usage (argv[0]);
117         }
118
119         type = argv[1];
120         field = argv[2];
121         value = argv[3];
122
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));
148
149         } else if (strcmp (field, "define_application") == 0) {
150                 GList *stuff;
151                 GnomeVFSMimeApplication app;
152
153                 stuff = comma_separated_str_to_str_list (value);
154
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);
161                 
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 ();
165         } else {
166                 usage (argv[0]);
167         }
168
169         return 0;
170 }