1 /*
2  *  nautilus-file-info.c - Information about a file
3  *
4  *  Copyright (C) 2003 Novell, Inc.
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Library General Public
8  *  License as published by the Free Software Foundation; either
9  *  version 2 of the License, or (at your option) any later version.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Library General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Library General Public
17  *  License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #include "nautilus-file-info.h"
22 
23 #include "nautilus-extension-private.h"
24 
25 G_DEFINE_INTERFACE (NautilusFileInfo, nautilus_file_info, G_TYPE_OBJECT)
26 
27 NautilusFileInfo * (*nautilus_file_info_getter)(GFile   *location,
28                                                 gboolean create);
29 
30 GList *
nautilus_file_info_list_copy(GList * files)31 nautilus_file_info_list_copy (GList *files)
32 {
33     GList *ret;
34     GList *l;
35 
36     ret = g_list_copy (files);
37     for (l = ret; l != NULL; l = l->next)
38     {
39         g_object_ref (G_OBJECT (l->data));
40     }
41 
42     return ret;
43 }
44 
45 void
nautilus_file_info_list_free(GList * files)46 nautilus_file_info_list_free (GList *files)
47 {
48     GList *l;
49 
50     for (l = files; l != NULL; l = l->next)
51     {
52         g_object_unref (G_OBJECT (l->data));
53     }
54 
55     g_list_free (files);
56 }
57 
58 static void
nautilus_file_info_default_init(NautilusFileInfoInterface * klass)59 nautilus_file_info_default_init (NautilusFileInfoInterface *klass)
60 {
61 }
62 
63 gboolean
nautilus_file_info_is_gone(NautilusFileInfo * self)64 nautilus_file_info_is_gone (NautilusFileInfo *self)
65 {
66     NautilusFileInfoInterface *iface;
67 
68     g_return_val_if_fail (NAUTILUS_IS_FILE_INFO (self), FALSE);
69 
70     iface = NAUTILUS_FILE_INFO_GET_IFACE (self);
71 
72     g_return_val_if_fail (iface->is_gone != NULL, FALSE);
73 
74     return iface->is_gone (self);
75 }
76 
77 GFileType
nautilus_file_info_get_file_type(NautilusFileInfo * self)78 nautilus_file_info_get_file_type (NautilusFileInfo *self)
79 {
80     NautilusFileInfoInterface *iface;
81 
82     g_return_val_if_fail (NAUTILUS_IS_FILE_INFO (self), G_FILE_TYPE_UNKNOWN);
83 
84     iface = NAUTILUS_FILE_INFO_GET_IFACE (self);
85 
86     g_return_val_if_fail (iface->get_file_type != NULL, G_FILE_TYPE_UNKNOWN);
87 
88     return iface->get_file_type (self);
89 }
90 
91 char *
nautilus_file_info_get_name(NautilusFileInfo * self)92 nautilus_file_info_get_name (NautilusFileInfo *self)
93 {
94     NautilusFileInfoInterface *iface;
95 
96     g_return_val_if_fail (NAUTILUS_IS_FILE_INFO (self), NULL);
97 
98     iface = NAUTILUS_FILE_INFO_GET_IFACE (self);
99 
100     g_return_val_if_fail (iface->get_name != NULL, NULL);
101 
102     return iface->get_name (self);
103 }
104 
105 GFile *
nautilus_file_info_get_location(NautilusFileInfo * self)106 nautilus_file_info_get_location (NautilusFileInfo *self)
107 {
108     NautilusFileInfoInterface *iface;
109 
110     g_return_val_if_fail (NAUTILUS_IS_FILE_INFO (self), NULL);
111 
112     iface = NAUTILUS_FILE_INFO_GET_IFACE (self);
113 
114     g_return_val_if_fail (iface->get_location != NULL, NULL);
115 
116     return iface->get_location (self);
117 }
118 char *
nautilus_file_info_get_uri(NautilusFileInfo * self)119 nautilus_file_info_get_uri (NautilusFileInfo *self)
120 {
121     NautilusFileInfoInterface *iface;
122 
123     g_return_val_if_fail (NAUTILUS_IS_FILE_INFO (self), NULL);
124 
125     iface = NAUTILUS_FILE_INFO_GET_IFACE (self);
126 
127     g_return_val_if_fail (iface->get_uri != NULL, NULL);
128 
129     return iface->get_uri (self);
130 }
131 
132 char *
nautilus_file_info_get_activation_uri(NautilusFileInfo * self)133 nautilus_file_info_get_activation_uri (NautilusFileInfo *self)
134 {
135     NautilusFileInfoInterface *iface;
136 
137     g_return_val_if_fail (NAUTILUS_IS_FILE_INFO (self), NULL);
138 
139     iface = NAUTILUS_FILE_INFO_GET_IFACE (self);
140 
141     g_return_val_if_fail (iface->get_activation_uri != NULL, NULL);
142 
143     return iface->get_activation_uri (self);
144 }
145 
146 GFile *
nautilus_file_info_get_parent_location(NautilusFileInfo * self)147 nautilus_file_info_get_parent_location (NautilusFileInfo *self)
148 {
149     NautilusFileInfoInterface *iface;
150 
151     g_return_val_if_fail (NAUTILUS_IS_FILE_INFO (self), NULL);
152 
153     iface = NAUTILUS_FILE_INFO_GET_IFACE (self);
154 
155     g_return_val_if_fail (iface->get_parent_location != NULL, NULL);
156 
157     return iface->get_parent_location (self);
158 }
159 
160 char *
nautilus_file_info_get_parent_uri(NautilusFileInfo * self)161 nautilus_file_info_get_parent_uri (NautilusFileInfo *self)
162 {
163     NautilusFileInfoInterface *iface;
164 
165     g_return_val_if_fail (NAUTILUS_IS_FILE_INFO (self), NULL);
166 
167     iface = NAUTILUS_FILE_INFO_GET_IFACE (self);
168 
169     g_return_val_if_fail (iface->get_parent_uri != NULL, NULL);
170 
171     return iface->get_parent_uri (self);
172 }
173 
174 NautilusFileInfo *
nautilus_file_info_get_parent_info(NautilusFileInfo * self)175 nautilus_file_info_get_parent_info (NautilusFileInfo *self)
176 {
177     NautilusFileInfoInterface *iface;
178 
179     g_return_val_if_fail (NAUTILUS_IS_FILE_INFO (self), NULL);
180 
181     iface = NAUTILUS_FILE_INFO_GET_IFACE (self);
182 
183     g_return_val_if_fail (iface->get_parent_info != NULL, NULL);
184 
185     return iface->get_parent_info (self);
186 }
187 
188 GMount *
nautilus_file_info_get_mount(NautilusFileInfo * self)189 nautilus_file_info_get_mount (NautilusFileInfo *self)
190 {
191     NautilusFileInfoInterface *iface;
192 
193     g_return_val_if_fail (NAUTILUS_IS_FILE_INFO (self), NULL);
194 
195     iface = NAUTILUS_FILE_INFO_GET_IFACE (self);
196 
197     g_return_val_if_fail (iface->get_mount != NULL, NULL);
198 
199     return iface->get_mount (self);
200 }
201 
202 char *
nautilus_file_info_get_uri_scheme(NautilusFileInfo * self)203 nautilus_file_info_get_uri_scheme (NautilusFileInfo *self)
204 {
205     NautilusFileInfoInterface *iface;
206 
207     g_return_val_if_fail (NAUTILUS_IS_FILE_INFO (self), NULL);
208 
209     iface = NAUTILUS_FILE_INFO_GET_IFACE (self);
210 
211     g_return_val_if_fail (iface->get_uri_scheme != NULL, NULL);
212 
213     return iface->get_uri_scheme (self);
214 }
215 
216 char *
nautilus_file_info_get_mime_type(NautilusFileInfo * self)217 nautilus_file_info_get_mime_type (NautilusFileInfo *self)
218 {
219     NautilusFileInfoInterface *iface;
220 
221     g_return_val_if_fail (NAUTILUS_IS_FILE_INFO (self), NULL);
222 
223     iface = NAUTILUS_FILE_INFO_GET_IFACE (self);
224 
225     g_return_val_if_fail (iface->get_mime_type != NULL, NULL);
226 
227     return iface->get_mime_type (self);
228 }
229 
230 gboolean
nautilus_file_info_is_mime_type(NautilusFileInfo * self,const char * mime_type)231 nautilus_file_info_is_mime_type (NautilusFileInfo *self,
232                                  const char       *mime_type)
233 {
234     NautilusFileInfoInterface *iface;
235 
236     g_return_val_if_fail (NAUTILUS_IS_FILE_INFO (self), FALSE);
237     g_return_val_if_fail (mime_type != NULL, FALSE);
238 
239     iface = NAUTILUS_FILE_INFO_GET_IFACE (self);
240 
241     g_return_val_if_fail (iface->is_mime_type != NULL, FALSE);
242 
243     return iface->is_mime_type (self, mime_type);
244 }
245 
246 gboolean
nautilus_file_info_is_directory(NautilusFileInfo * self)247 nautilus_file_info_is_directory (NautilusFileInfo *self)
248 {
249     NautilusFileInfoInterface *iface;
250 
251     g_return_val_if_fail (NAUTILUS_IS_FILE_INFO (self), FALSE);
252 
253     iface = NAUTILUS_FILE_INFO_GET_IFACE (self);
254 
255     g_return_val_if_fail (iface->is_directory != NULL, FALSE);
256 
257     return iface->is_directory (self);
258 }
259 
260 gboolean
nautilus_file_info_can_write(NautilusFileInfo * self)261 nautilus_file_info_can_write (NautilusFileInfo *self)
262 {
263     NautilusFileInfoInterface *iface;
264 
265     g_return_val_if_fail (NAUTILUS_IS_FILE_INFO (self), FALSE);
266 
267     iface = NAUTILUS_FILE_INFO_GET_IFACE (self);
268 
269     g_return_val_if_fail (iface->can_write != NULL, FALSE);
270 
271     return iface->can_write (self);
272 }
273 
274 void
nautilus_file_info_add_emblem(NautilusFileInfo * self,const char * emblem_name)275 nautilus_file_info_add_emblem (NautilusFileInfo *self,
276                                const char       *emblem_name)
277 {
278     NautilusFileInfoInterface *iface;
279 
280     g_return_if_fail (NAUTILUS_IS_FILE_INFO (self));
281 
282     iface = NAUTILUS_FILE_INFO_GET_IFACE (self);
283 
284     g_return_if_fail (iface->add_emblem != NULL);
285 
286     iface->add_emblem (self, emblem_name);
287 }
288 
289 char *
nautilus_file_info_get_string_attribute(NautilusFileInfo * self,const char * attribute_name)290 nautilus_file_info_get_string_attribute (NautilusFileInfo *self,
291                                          const char       *attribute_name)
292 {
293     NautilusFileInfoInterface *iface;
294 
295     g_return_val_if_fail (NAUTILUS_IS_FILE_INFO (self), NULL);
296     g_return_val_if_fail (attribute_name != NULL, NULL);
297 
298     iface = NAUTILUS_FILE_INFO_GET_IFACE (self);
299 
300     g_return_val_if_fail (iface->get_string_attribute != NULL, NULL);
301 
302     return iface->get_string_attribute (self, attribute_name);
303 }
304 
305 void
nautilus_file_info_add_string_attribute(NautilusFileInfo * self,const char * attribute_name,const char * value)306 nautilus_file_info_add_string_attribute (NautilusFileInfo *self,
307                                          const char       *attribute_name,
308                                          const char       *value)
309 {
310     NautilusFileInfoInterface *iface;
311 
312     g_return_if_fail (NAUTILUS_IS_FILE_INFO (self));
313     g_return_if_fail (attribute_name != NULL);
314     g_return_if_fail (value != NULL);
315 
316     iface = NAUTILUS_FILE_INFO_GET_IFACE (self);
317 
318     g_return_if_fail (iface->add_string_attribute != NULL);
319 
320     iface->add_string_attribute (self, attribute_name, value);
321 }
322 
323 void
nautilus_file_info_invalidate_extension_info(NautilusFileInfo * self)324 nautilus_file_info_invalidate_extension_info (NautilusFileInfo *self)
325 {
326     NautilusFileInfoInterface *iface;
327 
328     g_return_if_fail (NAUTILUS_IS_FILE_INFO (self));
329 
330     iface = NAUTILUS_FILE_INFO_GET_IFACE (self);
331 
332     g_return_if_fail (iface->invalidate_extension_info != NULL);
333 
334     iface->invalidate_extension_info (self);
335 }
336 
337 NautilusFileInfo *
nautilus_file_info_lookup(GFile * location)338 nautilus_file_info_lookup (GFile *location)
339 {
340     g_return_val_if_fail (G_IS_FILE (location), NULL);
341 
342     return nautilus_file_info_getter (location, FALSE);
343 }
344 
345 NautilusFileInfo *
nautilus_file_info_create(GFile * location)346 nautilus_file_info_create (GFile *location)
347 {
348     g_return_val_if_fail (G_IS_FILE (location), NULL);
349 
350     return nautilus_file_info_getter (location, TRUE);
351 }
352 
353 NautilusFileInfo *
nautilus_file_info_lookup_for_uri(const char * uri)354 nautilus_file_info_lookup_for_uri (const char *uri)
355 {
356     g_autoptr (GFile) location = NULL;
357 
358     g_return_val_if_fail (uri != NULL, NULL);
359 
360     location = g_file_new_for_uri (uri);
361 
362     return nautilus_file_info_lookup (location);
363 }
364 
365 NautilusFileInfo *
nautilus_file_info_create_for_uri(const char * uri)366 nautilus_file_info_create_for_uri (const char *uri)
367 {
368     g_autoptr (GFile) location = NULL;
369 
370     g_return_val_if_fail (uri != NULL, NULL);
371 
372     location = g_file_new_for_uri (uri);
373 
374     return nautilus_file_info_create (location);
375 }
376