1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* nautilus-mime-actions.c - uri-specific versions of mime action functions
Copyright (C) 2000, 2001 Eazel, Inc.
The Gnome Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The Gnome Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the Gnome Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
Authors: Maciej Stachowiak <mjs@eazel.com>
*/
#include <config.h>
#include "nautilus-mime-actions.h"
#include "nautilus-file-attributes.h"
#include "nautilus-file.h"
#include "nautilus-metadata.h"
#include <bonobo-activation/bonobo-activation-activate.h>
#include <eel/eel-glib-extensions.h>
#include <eel/eel-string.h>
#include <libgnomevfs/gnome-vfs-application-registry.h>
#include <libgnomevfs/gnome-vfs-mime-handlers.h>
#include <stdio.h>
static int gnome_vfs_mime_application_has_id (GnomeVFSMimeApplication *application,
const char *id);
static gboolean application_supports_uri_scheme (gpointer data,
gpointer uri_scheme);
static gboolean
nautilus_mime_actions_check_if_minimum_attributes_ready (NautilusFile *file)
{
NautilusFileAttributes attributes;
gboolean ready;
attributes = nautilus_mime_actions_get_minimum_file_attributes ();
ready = nautilus_file_check_if_ready (file, attributes);
return ready;
}
NautilusFileAttributes
nautilus_mime_actions_get_minimum_file_attributes (void)
{
return NAUTILUS_FILE_ATTRIBUTE_VOLUMES |
NAUTILUS_FILE_ATTRIBUTE_ACTIVATION_URI |
NAUTILUS_FILE_ATTRIBUTE_METADATA |
NAUTILUS_FILE_ATTRIBUTE_MIME_TYPE |
NAUTILUS_FILE_ATTRIBUTE_SLOW_MIME_TYPE;
}
static NautilusFileAttributes
nautilus_mime_actions_get_open_with_file_attributes (void)
{
return NAUTILUS_FILE_ATTRIBUTE_VOLUMES |
NAUTILUS_FILE_ATTRIBUTE_ACTIVATION_URI |
NAUTILUS_FILE_ATTRIBUTE_METADATA |
NAUTILUS_FILE_ATTRIBUTE_MIME_TYPE;
}
static gboolean
nautilus_mime_actions_check_if_open_with_attributes_ready (NautilusFile *file)
{
NautilusFileAttributes attributes;
gboolean ready;
attributes = nautilus_mime_actions_get_open_with_file_attributes ();
ready = nautilus_file_check_if_ready (file, attributes);
return ready;
}
NautilusFileAttributes
nautilus_mime_actions_get_full_file_attributes (void)
{
return nautilus_mime_actions_get_minimum_file_attributes () |
NAUTILUS_FILE_ATTRIBUTE_DIRECTORY_ITEM_MIME_TYPES;
}
GnomeVFSMimeApplication *
nautilus_mime_get_default_application_for_file (NautilusFile *file)
{
char *mime_type;
GnomeVFSMimeApplication *result;
char *uri_scheme;
if (!nautilus_mime_actions_check_if_open_with_attributes_ready (file)) {
return NULL;
}
uri_scheme = nautilus_file_get_uri_scheme (file);
/* TODO: this should maybe use gnome_vfs_mime_get_default_application_for_scheme,
but thats not public atm */
mime_type = nautilus_file_get_mime_type (file);
result = gnome_vfs_mime_get_default_application (mime_type);
if (result != NULL && !application_supports_uri_scheme (result, uri_scheme)) {
result = NULL;
}
if (result == NULL) {
GList *all_applications, *l;
all_applications = nautilus_mime_get_open_with_applications_for_file (file);
for (l = all_applications; l != NULL; l = l->next) {
result = gnome_vfs_mime_application_copy (l->data);
if (result != NULL && !application_supports_uri_scheme (result, uri_scheme)) {
gnome_vfs_mime_application_free (result);
result = NULL;
}
if (result != NULL)
break;
}
gnome_vfs_mime_application_list_free (all_applications);
}
g_free (mime_type);
g_free (uri_scheme);
return result;
}
static GList *
get_open_with_mime_applications (NautilusFile *file)
{
char *guessed_mime_type;
char *mime_type;
GList *result;
guessed_mime_type = nautilus_file_get_guessed_mime_type (file);
mime_type = nautilus_file_get_mime_type (file);
result = gnome_vfs_mime_get_all_applications (mime_type);
if (strcmp (guessed_mime_type, mime_type) != 0) {
GList *result_2;
GList *l;
result_2 = gnome_vfs_mime_get_all_applications (guessed_mime_type);
for (l = result_2; l != NULL; l = l->next) {
if (!g_list_find_custom (result,
((GnomeVFSMimeApplication*)l->data)->id,
(GCompareFunc) gnome_vfs_mime_application_has_id)) {
result = g_list_prepend (result, l->data);
}
}
g_list_free (result_2);
}
g_free (mime_type);
g_free (guessed_mime_type);
return result;
}
/* Get a list of applications for the Open With menu. This is
* different than nautilus_mime_get_applications_for_file()
* because this function will merge the lists of the fast and slow
* mime types for the file */
GList *
nautilus_mime_get_open_with_applications_for_file (NautilusFile *file)
{
char *uri_scheme;
GList *result;
GList *removed;
if (!nautilus_mime_actions_check_if_open_with_attributes_ready (file)) {
return NULL;
}
result = get_open_with_mime_applications (file);
/* First remove applications that cannot support this location */
uri_scheme = nautilus_file_get_uri_scheme (file);
g_assert (uri_scheme != NULL);
result = eel_g_list_partition (result, application_supports_uri_scheme,
uri_scheme, &removed);
gnome_vfs_mime_application_list_free (removed);
g_free (uri_scheme);
return g_list_reverse (result);
}
GList *
nautilus_mime_get_applications_for_file (NautilusFile *file)
{
char *mime_type;
if (!nautilus_mime_actions_check_if_minimum_attributes_ready (file)) {
return NULL;
}
mime_type = nautilus_file_get_mime_type (file);
return gnome_vfs_mime_get_all_applications (mime_type);
}
static int
application_supports_uri_scheme_strcmp_style (gconstpointer application_data,
gconstpointer uri_scheme)
{
return application_supports_uri_scheme
((gpointer) application_data,
(gpointer) uri_scheme) ? 0 : 1;
}
gboolean
nautilus_mime_has_any_applications_for_file (NautilusFile *file)
{
GList *all_applications_for_mime_type, *application_that_can_access_uri;
char *uri_scheme;
gboolean result;
all_applications_for_mime_type = nautilus_mime_get_applications_for_file (file);
uri_scheme = nautilus_file_get_uri_scheme (file);
application_that_can_access_uri = g_list_find_custom
(all_applications_for_mime_type,
uri_scheme,
application_supports_uri_scheme_strcmp_style);
g_free (uri_scheme);
result = application_that_can_access_uri != NULL;
gnome_vfs_mime_application_list_free (all_applications_for_mime_type);
return result;
}
static int
gnome_vfs_mime_application_has_id (GnomeVFSMimeApplication *application,
const char *id)
{
return strcmp (application->id, id);
}
static gboolean
application_supports_uri_scheme (gpointer data,
gpointer uri_scheme)
{
GnomeVFSMimeApplication *application;
g_assert (data != NULL);
application = (GnomeVFSMimeApplication *) data;
/* The default supported uri scheme is "file" */
if (application->supported_uri_schemes == NULL
&& g_ascii_strcasecmp ((const char *) uri_scheme, "file") == 0) {
return TRUE;
}
return g_list_find_custom (application->supported_uri_schemes,
uri_scheme,
eel_strcasecmp_compare_func) != NULL;
}
|