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  *  see <http://www.gnu.org/licenses/>.
20  *
21  *  Author: Maciej Stachowiak <mjs@eazel.com>
22  */
23 
24 #include <config.h>
25 
26 #include <gtk/gtk.h>
27 #include <src/nautilus-mime-actions.h>
28 #include <stdio.h>
29 
30 static gboolean ready = FALSE;
31 
32 static void
usage(const char * name)33 usage (const char *name)
34 {
35     fprintf (stderr, "Usage: %s uri field value\n", name);
36     fprintf (stderr, "Valid field values are: \n");
37     fprintf (stderr, "\tdefault_action_type\n");
38     fprintf (stderr, "\tdefault_application\n");
39     fprintf (stderr, "\tdefault_component\n");
40     fprintf (stderr, "\tshort_list_applicationss\n");
41     fprintf (stderr, "\tshort_list_components\n");
42     fprintf (stderr, "\tadd_to_all_applicationss\n");
43     fprintf (stderr, "\tremove_from_all_applications\n");
44     exit (1);
45 }
46 
47 static GnomeVFSMimeActionType
str_to_action_type(const char * str)48 str_to_action_type (const char *str)
49 {
50     if (g_ascii_strcasecmp (str, "component") == 0)
51     {
52         return GNOME_VFS_MIME_ACTION_TYPE_COMPONENT;
53     }
54     else if (g_ascii_strcasecmp (str, "application") == 0)
55     {
56         return GNOME_VFS_MIME_ACTION_TYPE_APPLICATION;
57     }
58     else
59     {
60         return GNOME_VFS_MIME_ACTION_TYPE_NONE;
61     }
62 }
63 
64 static char **
strsplit_handle_null(const char * str,const char * delim,int max)65 strsplit_handle_null (const char *str,
66                       const char *delim,
67                       int         max)
68 {
69     return g_strsplit ((str == NULL ? "" : str), delim, max);
70 }
71 
72 
73 static GList *
strsplit_to_list(const char * str,const char * delim,int max)74 strsplit_to_list (const char *str,
75                   const char *delim,
76                   int         max)
77 {
78     char **strv;
79     GList *retval;
80     int i;
81 
82     strv = strsplit_handle_null (str, delim, max);
83 
84     retval = NULL;
85 
86     for (i = 0; strv[i] != NULL; i++)
87     {
88         retval = g_list_prepend (retval, strv[i]);
89     }
90 
91     retval = g_list_reverse (retval);
92     /* Don't strfreev, since we didn't copy the individual strings. */
93     g_free (strv);
94 
95     return retval;
96 }
97 
98 static GList *
comma_separated_str_to_str_list(const char * str)99 comma_separated_str_to_str_list (const char *str)
100 {
101     return strsplit_to_list (str, ",", 0);
102 }
103 
104 static void
ready_callback(NautilusFile * file,gpointer callback_data)105 ready_callback (NautilusFile *file,
106                 gpointer      callback_data)
107 {
108     ready = TRUE;
109 }
110 
111 int
main(int argc,char ** argv)112 main (int    argc,
113       char **argv)
114 {
115     const char *uri;
116     const char *field;
117     const char *value;
118     NautilusFile *file;
119     NautilusFileAttributes attributes;
120 
121     gtk_init (&argc, &argv);
122 
123     if (argc < 3)
124     {
125         usage (argv[0]);
126     }
127 
128     uri = argv[1];
129     field = argv[2];
130     value = argv[3];
131 
132     file = nautilus_file_get_by_uri (uri);
133 
134     attributes = nautilus_mime_actions_get_full_file_attributes ();
135     nautilus_file_call_when_ready (file, attributes, ready_callback, NULL);
136 
137     while (!ready)
138     {
139         gtk_main_iteration ();
140     }
141 
142     if (strcmp (field, "default_action_type") == 0)
143     {
144         puts ("default_action_type");
145         nautilus_mime_set_default_action_type_for_file (file, str_to_action_type (value));
146     }
147     else if (strcmp (field, "default_application") == 0)
148     {
149         puts ("default_application");
150         nautilus_mime_set_default_application_for_file (file, value);
151     }
152     else if (strcmp (field, "default_component") == 0)
153     {
154         puts ("default_component");
155         nautilus_mime_set_default_component_for_file (file, value);
156     }
157     else if (strcmp (field, "short_list_applicationss") == 0)
158     {
159         puts ("short_list_applications");
160         nautilus_mime_set_short_list_applications_for_file
161             (file, comma_separated_str_to_str_list (value));
162     }
163     else if (strcmp (field, "short_list_components") == 0)
164     {
165         puts ("short_list_components");
166         nautilus_mime_set_short_list_components_for_file
167             (file, comma_separated_str_to_str_list (value));
168     }
169     else if (strcmp (field, "add_to_all_applicationss") == 0)
170     {
171         puts ("add_to_all_applications");
172         nautilus_mime_extend_all_applications_for_file
173             (file, comma_separated_str_to_str_list (value));
174     }
175     else if (strcmp (field, "remove_from_all_applications") == 0)
176     {
177         puts ("remove_from_all_applications");
178         nautilus_mime_remove_from_all_applications_for_file
179             (file, comma_separated_str_to_str_list (value));
180     }
181     else
182     {
183         usage (argv[0]);
184     }
185 
186     return 0;
187 }
188