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-info.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-init.h>
28 #include <libgnomevfs/gnome-vfs-mime-handlers.h>
29 #include <libgnomevfs/gnome-vfs-mime-info.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 int
35 main (int argc, char **argv)
36 {
37         const char *value;
38
39         gnome_vfs_init ();
40
41         /* test reading of gnome-vfs.keys file */
42         value = gnome_vfs_mime_get_value ("x-directory/normal", "description");
43         if (value == NULL || strcmp (value, "folder") != 0) {
44                 printf ("Error: get_value failed on \"description\".\n");
45                 exit (1);
46         }
47         value = gnome_vfs_mime_get_value ("x-directory/normal", "default_action_type");
48         if (value == NULL || strcmp (value, "component") != 0) {
49                 printf ("Error: get_value failed on \"default_action_type\".\n");
50                 exit (1);
51         }
52         /* default_component_iid doesn't appear anymore in gnome-vfs.keys */
53 #if 0
54         value = gnome_vfs_mime_get_value ("x-directory/normal", "default_component_iid");
55         if (value == NULL || strcmp (value, "OAFIID:nautilus_file_manager_icon_view:42681b21-d5ca-4837-87d2-394d88ecc058") != 0) {
56                 printf ("Error: get_value failed on \"default_component_iid\".\n");
57                 exit (1);
58         }
59 #endif
60         value = gnome_vfs_mime_get_value ("x-directory/normal", "short_list_component_iids_for_novice_user_level");
61         if (value == NULL || strcmp (value,
62                                 "OAFIID:nautilus_file_manager_icon_view:42681b21-d5ca-4837-87d2-394d88ecc058,"
63                                 "OAFIID:nautilus_file_manager_list_view:521e489d-0662-4ad7-ac3a-832deabe111c,"
64                                 "OAFIID:nautilus_music_view:9456b5d2-60a8-407f-a56e-d561e1821391:OAFIID") != 0) {
65                 printf ("Error: get_value failed on \"short_list_component_iids_for_novice_user_level\".\n");
66                 exit (1);
67         }
68         value = gnome_vfs_mime_get_value ("x-directory/normal", "short_list_component_iids_for_intermediate_user_level");
69         if (value == NULL || strcmp (value, "OAFIID:nautilus_file_manager_icon_view:42681b21-d5ca-4837-87d2-394d88ecc058,"
70                     "OAFIID:nautilus_file_manager_list_view:521e489d-0662-4ad7-ac3a-832deabe111c,"
71                     "OAFIID:nautilus_music_view:9456b5d2-60a8-407f-a56e-d561e1821391") != 0) {
72                 printf ("Error: get_value failed on \"short_list_component_iids_for_intermediate_user_level\".\n");
73                 exit (1);
74         }
75         value = gnome_vfs_mime_get_value ("x-directory/normal", "short_list_component_iids_for_advanced_user_level");
76         if (value == NULL || strcmp (value, "OAFIID:nautilus_file_manager_icon_view:42681b21-d5ca-4837-87d2-394d88ecc058,"
77                     "OAFIID:nautilus_file_manager_list_view:521e489d-0662-4ad7-ac3a-832deabe111c,"
78                     "OAFIID:nautilus_music_view:9456b5d2-60a8-407f-a56e-d561e1821391") != 0) {
79                 printf ("Error: get_value failed on \"short_list_component_iids_for_advanced_user_level\".\n");
80                 exit (1);
81         }
82         value = gnome_vfs_mime_get_description ("application/mime-type-test");
83         if (value == NULL || strcmp (value, "mon test a moi") != 0) {
84                 printf ("Error: description failed on \"application/mime-type-test\".\n");
85                 exit (1);
86         }
87
88
89         /* test reading of gnome-vfs.mime file */
90         {
91                 GList *list, *temp;
92                 char *extensions[] = {"ps", "eps"};
93                 char **extension;
94                 char *ext;
95                 list = gnome_vfs_mime_get_extensions_list ("application/postscript");
96                 if (list == NULL) {
97                         printf ("Error: get_extensions_list failed on \"application/postscript\" list NULL.\n");
98                         exit (1);
99                 }
100                 for (temp = list, extension = extensions; temp != NULL; temp = temp->next, extension++) {
101                         if (strcmp (*extension, (char *)temp->data) != 0) {
102                                 printf ("Error: get_extensions_list failed on \"application/postscript\".\n");
103                                 printf ("Wrong value received was %s instead of %s\n", (char *) temp->data, *extension);
104                                 exit (1);
105                         }
106                 }
107                 gnome_vfs_mime_extensions_list_free (list);
108                 
109                 ext = gnome_vfs_mime_get_extensions_string ("application/postscript");
110                 if (ext == NULL || strcmp (ext, "eps ps") != 0) {
111                         printf ("Error: get_extensions_string failed on \"application/postscript\".\n");
112                         exit (1);
113                 }
114                 ext = gnome_vfs_mime_get_extensions_pretty_string ("application/postscript");
115                 if (ext == NULL || strcmp (ext, ".ps, .eps") != 0) {
116                         printf ("Error: get_extensions_pretty_string failed on \"application/postscript\".\n");
117                         exit (1);
118                 }
119         }
120
121         /* test writing the users' user.keys file. It should overide the system default */
122         {
123                 const char *value;
124                 gnome_vfs_mime_set_value ("application/postscript",
125                                               "foo",
126                                               "bar");
127                 value = gnome_vfs_mime_get_value ("application/postscript",
128                                                       "foo");
129                 if (value == NULL || strcmp (value, "bar") != 0) {
130                         printf ("%s\n", value);
131                         printf ("Error: set_value failed on \"application/postscript\" / \"foo\".\n");
132                         exit (1);                       
133                 }
134
135                 /* test freeze/thaw for this stuff */
136                 gnome_vfs_mime_freeze ();
137                 gnome_vfs_mime_set_value ("application/postscript",
138                                               "bar",
139                                               "foo");
140                 value = gnome_vfs_mime_get_value ("application/postscript",
141                                                       "bar");
142                 if (value == NULL || strcmp (value, "foo") != 0) {
143                         printf ("%s\n", value);
144                         printf ("Error: set_value failed on \"application/postscript\" / \"bar\".\n");
145                         exit (1);                       
146                 }
147                 gnome_vfs_mime_thaw ();
148                 value = gnome_vfs_mime_get_value ("application/postscript",
149                                                       "bar");
150                 if (value == NULL || strcmp (value, "foo") != 0) {
151                         printf ("%s\n", value);
152                         printf ("Error: set_value failed on \"application/postscript\" / \"bar\".\n");
153                         exit (1);                       
154                 }
155                 
156                 /* try to overide system settings.*/
157                 gnome_vfs_mime_set_value ("application/postscript",
158                                               "description",
159                                               "bar");
160                 value = gnome_vfs_mime_get_value ("application/postscript",
161                                                       "description");
162                 if (value == NULL || strcmp (value, "bar") != 0) {
163                         printf ("%s\n", value);
164                         printf ("Error: set_value failed on \"application/postscript\" / \"description\".\n");
165                         exit (1);                       
166                 }
167
168         }
169
170
171         /* test to try to modify the user.mime file */
172         {
173                 char *value, *save;
174
175                 save = gnome_vfs_mime_get_extensions_string ("application/postscript");
176                 gnome_vfs_mime_set_registered_type_key ("application/postscript", "ext", "foo");
177
178                 value = gnome_vfs_mime_get_extensions_string ("application/postscript");
179
180                 if (strstr (value, "foo") == NULL) {
181                         printf ("Error: cannot set mime type new extension.\n");
182                         exit (1);
183                 }
184                 if (strstr (value, "ps") != NULL) {
185                         printf ("Error: didn't delete default mime type extension.\n");
186                         exit (1);
187                 }
188                 gnome_vfs_mime_set_registered_type_key ("application/postscript", "ext", save);
189         }
190
191         {
192                 GList *mime_types_list;
193                 char *deleted_mime_type;
194                 GList *temp;
195                 gboolean found_mime_type;
196
197                 mime_types_list = gnome_vfs_get_registered_mime_types ();
198
199                 if (mime_types_list->data != NULL) {
200                         /* try to delete a mime type */
201                         deleted_mime_type = mime_types_list->data;
202                         gnome_vfs_mime_registered_mime_type_delete (deleted_mime_type);
203                         gnome_vfs_mime_registered_mime_type_list_free (mime_types_list);
204                         mime_types_list = gnome_vfs_get_registered_mime_types ();
205                         for (temp = mime_types_list; temp != NULL; temp = temp->next) {
206                                 if (strcmp (deleted_mime_type, (char *)temp->data) == 0) {
207                                         printf ("Error: could not delete mime type.\n");
208                                         exit (1);
209                                 }
210                         }
211                         gnome_vfs_mime_registered_mime_type_list_free (mime_types_list);
212                         
213                         /* reset to system defaults */
214                         gnome_vfs_mime_reset ();
215
216                         /* try to find the original mime type again */
217                         mime_types_list = gnome_vfs_get_registered_mime_types ();
218                         found_mime_type = FALSE;
219                         for (temp = mime_types_list; temp != NULL; temp = temp->next) {
220                                 if (strcmp (deleted_mime_type, (char *)temp->data) == 0) {
221                                         found_mime_type = TRUE;
222                                         break;
223                                 }
224                         }
225                         if (!found_mime_type) {
226                                 printf ("Error: lost a mime type.\n");
227                                 exit (1);
228                         }
229                         
230                 }
231                 
232         }
233
234         /* do hard stuff on the API */
235
236         printf ("all mime-info-related tests succeeded\n");
237
238         return 0;
239 }
240
241
242