source: trunk/third/gnome-vfs/test/test-mime-handlers-set.c @ 15497

Revision 15497, 5.1 KB checked in by ghudson, 24 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r15496, which included commits to RCS files with non-trunk default branches.
Line 
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#ifdef HAVE_CONFIG_H
25#include <config.h>
26#endif
27
28#include "gnome-vfs.h"
29#include "gnome-vfs-application-registry.h"
30#include "gnome-vfs-mime-handlers.h"
31
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35
36
37static void
38usage (const char *name)
39{
40                fprintf (stderr, "Usage: %s mime_type field value\n", name);
41                fprintf (stderr, "Valid field values are: \n");
42                fprintf (stderr, "\tdefault_action_type\n");
43                fprintf (stderr, "\tdefault_application\n");
44                fprintf (stderr, "\tdefault_component\n");
45                fprintf (stderr, "\tshort_list_applicationss\n");
46                fprintf (stderr, "\tshort_list_components\n");
47                fprintf (stderr, "\tadd_to_all_applicationss\n");
48                fprintf (stderr, "\tremove_from_all_applications\n");
49                fprintf (stderr, "\tdefine_application\n");
50                exit (1);
51}
52
53static GnomeVFSMimeActionType
54str_to_action_type (const char *str)
55{
56        if (strcasecmp (str, "component") == 0) {
57                return GNOME_VFS_MIME_ACTION_TYPE_COMPONENT;
58        } else if (strcasecmp (str, "application") == 0) {
59                return GNOME_VFS_MIME_ACTION_TYPE_APPLICATION;
60        } else {
61                return GNOME_VFS_MIME_ACTION_TYPE_NONE;
62        }
63}
64
65static char **
66strsplit_handle_null (const char *str, const char *delim, int max)
67{
68        return g_strsplit ((str == NULL ? "" : str), delim, max);
69}
70
71
72static GList *gnome_vfs_strsplit_to_list (const char *str, const char *delim, int max)
73{
74        char **strv;
75        GList *retval;
76        int i;
77
78        strv = strsplit_handle_null (str, delim, max);
79
80        retval = NULL;
81
82        for (i = 0; strv[i] != NULL; i++) {
83                retval = g_list_prepend (retval, strv[i]);
84        }
85
86        retval = g_list_reverse (retval);
87        /* Don't strfreev, since we didn't copy the individual strings. */
88        g_free (strv);
89
90        return retval;
91}
92
93static GList *comma_separated_str_to_str_list (const char *str)
94{
95        return gnome_vfs_strsplit_to_list (str, ",", 0);
96}
97
98
99static gboolean
100str_to_bool (const char *str)
101{
102        return ((str != NULL) &&
103                ((strcasecmp (str, "true") == 0) ||
104                 (strcasecmp (str, "yes") == 0)));
105}
106
107
108int
109main (int argc, char **argv)
110{
111        const char *type; 
112        const char *field;
113        const char *value;
114
115        oaf_init (argc, argv);
116        gnome_vfs_init ();
117
118        if (argc < 3) {
119                usage (argv[0]);
120        }
121
122        type = argv[1];
123        field = argv[2];
124        value = argv[3];
125
126        if (strcmp (field, "default_action_type") == 0) {
127                puts ("default_action_type");
128                gnome_vfs_mime_set_default_action_type (type, str_to_action_type (value));
129        } else if (strcmp (field, "default_application") == 0) {
130                puts ("default_application");
131                gnome_vfs_mime_set_default_application (type, value);
132        } else if (strcmp (field, "default_component") == 0) {
133                puts ("default_component");
134                gnome_vfs_mime_set_default_component (type, value);
135        } else if (strcmp (field, "short_list_applicationss") == 0) {
136                puts ("short_list_applications");
137                gnome_vfs_mime_set_short_list_applications (type,
138                                                            comma_separated_str_to_str_list (value));
139        } else if (strcmp (field, "short_list_components") == 0) {
140                puts ("short_list_components");
141                gnome_vfs_mime_set_short_list_components (type,
142                                                           comma_separated_str_to_str_list (value));
143        } else if (strcmp (field, "add_to_all_applicationss") == 0) {
144                puts ("add_to_all_applications");
145                gnome_vfs_mime_extend_all_applications (type,
146                                                        comma_separated_str_to_str_list (value));
147        } else if (strcmp (field, "remove_from_all_applications") == 0) {
148                puts ("remove_from_all_applications");
149                gnome_vfs_mime_remove_from_all_applications (type,
150                                                             comma_separated_str_to_str_list (value));
151
152        } else if (strcmp (field, "define_application") == 0) {
153                GList *stuff;
154                GnomeVFSMimeApplication app;
155
156                stuff = comma_separated_str_to_str_list (value);
157
158                app.id = g_list_nth (stuff, 0)->data;
159                app.name = g_list_nth (stuff, 1)->data;
160                app.command = g_list_nth (stuff, 2)->data;
161                app.can_open_multiple_files = str_to_bool (g_list_nth (stuff, 3)->data);
162                app.can_open_uris = str_to_bool (g_list_nth (stuff, 4)->data);
163                app.requires_terminal = str_to_bool (g_list_nth (stuff, 5)->data);
164               
165                gnome_vfs_application_registry_save_mime_application (&app);
166                gnome_vfs_application_registry_add_mime_type (app.id, type);
167                gnome_vfs_application_registry_sync ();
168        } else {
169                usage (argv[0]);
170        }
171
172        return 0;
173}
Note: See TracBrowser for help on using the repository browser.