nautilus-3.6.3/libnautilus-extension/nautilus-file-info.c

No issues found

  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, write to the Free
 18  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 19  *
 20  */
 21 
 22 #include <config.h>
 23 #include "nautilus-file-info.h"
 24 #include "nautilus-extension-private.h"
 25 
 26 NautilusFileInfo *(*nautilus_file_info_getter) (GFile *location, gboolean create);
 27 
 28 /**
 29  * nautilus_file_info_list_copy:
 30  * @files: (element-type NautilusFileInfo): the files to copy
 31  *
 32  * Returns: (element-type NautilusFileInfo) (transfer full): a copy of @files.
 33  *  Use #nautilus_file_info_list_free to free the list and unref its contents.
 34  */
 35 GList *
 36 nautilus_file_info_list_copy (GList *files)
 37 {
 38 	GList *ret;
 39 	GList *l;
 40 	
 41 	ret = g_list_copy (files);
 42 	for (l = ret; l != NULL; l = l->next) {
 43 		g_object_ref (G_OBJECT (l->data));
 44 	}
 45 
 46 	return ret;
 47 }
 48 
 49 /**
 50  * nautilus_file_info_list_free:
 51  * @files: (element-type NautilusFileInfo): a list created with
 52  *   #nautilus_file_info_list_copy
 53  *
 54  */
 55 void              
 56 nautilus_file_info_list_free (GList *files)
 57 {
 58 	GList *l;
 59 	
 60 	for (l = files; l != NULL; l = l->next) {
 61 		g_object_unref (G_OBJECT (l->data));
 62 	}
 63 	
 64 	g_list_free (files);
 65 }
 66 
 67 static void
 68 nautilus_file_info_base_init (gpointer g_class)
 69 {
 70 }
 71 
 72 GType                   
 73 nautilus_file_info_get_type (void)
 74 {
 75 	static GType type = 0;
 76 
 77 	if (!type) {
 78 		const GTypeInfo info = {
 79 			sizeof (NautilusFileInfoIface),
 80 			nautilus_file_info_base_init,
 81 			NULL,
 82 			NULL,
 83 			NULL,
 84 			NULL,
 85 			0,
 86 			0,
 87 			NULL
 88 		};
 89 		
 90 		type = g_type_register_static (G_TYPE_INTERFACE, 
 91 					       "NautilusFileInfo",
 92 					       &info, 0);
 93 		g_type_interface_add_prerequisite (type, G_TYPE_OBJECT);
 94 	}
 95 
 96 	return type;
 97 }
 98 
 99 gboolean
100 nautilus_file_info_is_gone (NautilusFileInfo *file)
101 {
102 	g_return_val_if_fail (NAUTILUS_IS_FILE_INFO (file), FALSE);
103 	g_return_val_if_fail (NAUTILUS_FILE_INFO_GET_IFACE (file)->is_gone != NULL, FALSE);
104 	
105 	return NAUTILUS_FILE_INFO_GET_IFACE (file)->is_gone (file);
106 }
107 
108 GFileType
109 nautilus_file_info_get_file_type (NautilusFileInfo *file)
110 {
111 	g_return_val_if_fail (NAUTILUS_IS_FILE_INFO (file), G_FILE_TYPE_UNKNOWN);
112 	g_return_val_if_fail (NAUTILUS_FILE_INFO_GET_IFACE (file)->get_file_type != NULL, G_FILE_TYPE_UNKNOWN);
113 
114 	return NAUTILUS_FILE_INFO_GET_IFACE (file)->get_file_type (file);
115 }
116 
117 char *
118 nautilus_file_info_get_name (NautilusFileInfo *file)
119 {
120 	g_return_val_if_fail (NAUTILUS_IS_FILE_INFO (file), NULL);
121 	g_return_val_if_fail (NAUTILUS_FILE_INFO_GET_IFACE (file)->get_name != NULL, NULL);
122 
123 	return NAUTILUS_FILE_INFO_GET_IFACE (file)->get_name (file);
124 }
125 
126 /**
127  * nautilus_file_info_get_location:
128  * @file: a #NautilusFileInfo
129  *
130  * Returns: (transfer full): a #GFile for the location of @file
131  */
132 GFile *
133 nautilus_file_info_get_location (NautilusFileInfo *file)
134 {
135 	g_return_val_if_fail (NAUTILUS_IS_FILE_INFO (file), NULL);
136 	g_return_val_if_fail (NAUTILUS_FILE_INFO_GET_IFACE (file)->get_location != NULL, NULL);
137 
138 	return NAUTILUS_FILE_INFO_GET_IFACE (file)->get_location (file);
139 }
140 char *
141 nautilus_file_info_get_uri (NautilusFileInfo *file)
142 {
143 	g_return_val_if_fail (NAUTILUS_IS_FILE_INFO (file), NULL);
144 	g_return_val_if_fail (NAUTILUS_FILE_INFO_GET_IFACE (file)->get_uri != NULL, NULL);
145 
146 	return NAUTILUS_FILE_INFO_GET_IFACE (file)->get_uri (file);
147 }
148 
149 char *
150 nautilus_file_info_get_activation_uri (NautilusFileInfo *file)
151 {
152 	g_return_val_if_fail (NAUTILUS_IS_FILE_INFO (file), NULL);
153 	g_return_val_if_fail (NAUTILUS_FILE_INFO_GET_IFACE (file)->get_activation_uri != NULL, NULL);
154 
155 	return NAUTILUS_FILE_INFO_GET_IFACE (file)->get_activation_uri (file);
156 }
157 
158 /**
159  * nautilus_file_info_get_parent_location:
160  * @file: a #NautilusFileInfo
161  *
162  * Returns: (allow-none) (transfer full): a #GFile for the parent location of @file, 
163  *   or %NULL if @file has no parent
164  */
165 GFile *
166 nautilus_file_info_get_parent_location (NautilusFileInfo *file)
167 {
168 	g_return_val_if_fail (NAUTILUS_IS_FILE_INFO (file), NULL);
169 	g_return_val_if_fail (NAUTILUS_FILE_INFO_GET_IFACE (file)->get_parent_location != NULL, NULL);
170 
171 	return NAUTILUS_FILE_INFO_GET_IFACE (file)->get_parent_location (file);
172 }
173 
174 char *
175 nautilus_file_info_get_parent_uri (NautilusFileInfo *file)
176 {
177 	g_return_val_if_fail (NAUTILUS_IS_FILE_INFO (file), NULL);
178 	g_return_val_if_fail (NAUTILUS_FILE_INFO_GET_IFACE (file)->get_parent_uri != NULL, NULL);
179 
180 	return NAUTILUS_FILE_INFO_GET_IFACE (file)->get_parent_uri (file);
181 }
182 
183 /**
184  * nautilus_file_info_get_parent_info:
185  * @file: a #NautilusFileInfo
186  *
187  * Returns: (allow-none) (transfer full): a #NautilusFileInfo for the parent of @file, 
188  *   or %NULL if @file has no parent
189  */
190 NautilusFileInfo *
191 nautilus_file_info_get_parent_info (NautilusFileInfo *file)
192 {
193 	g_return_val_if_fail (NAUTILUS_IS_FILE_INFO (file), NULL);
194 	g_return_val_if_fail (NAUTILUS_FILE_INFO_GET_IFACE (file)->get_parent_info != NULL, NULL);
195 
196 	return NAUTILUS_FILE_INFO_GET_IFACE (file)->get_parent_info (file);
197 }
198 
199 /**
200  * nautilus_file_info_get_mount:
201  * @file: a #NautilusFileInfo
202  *
203  * Returns: (allow-none) (transfer full): a #GMount for the mount of @file, 
204  *   or %NULL if @file has no mount
205  */
206 GMount *
207 nautilus_file_info_get_mount (NautilusFileInfo *file)
208 {
209 	g_return_val_if_fail (NAUTILUS_IS_FILE_INFO (file), NULL);
210 	g_return_val_if_fail (NAUTILUS_FILE_INFO_GET_IFACE (file)->get_mount != NULL, NULL);
211     
212 	return NAUTILUS_FILE_INFO_GET_IFACE (file)->get_mount (file);
213 }
214 
215 char *
216 nautilus_file_info_get_uri_scheme (NautilusFileInfo *file)
217 {
218 	g_return_val_if_fail (NAUTILUS_IS_FILE_INFO (file), NULL);
219 	g_return_val_if_fail (NAUTILUS_FILE_INFO_GET_IFACE (file)->get_uri_scheme != NULL, NULL);
220 
221 	return NAUTILUS_FILE_INFO_GET_IFACE (file)->get_uri_scheme (file);
222 }
223 
224 char *
225 nautilus_file_info_get_mime_type (NautilusFileInfo *file)
226 {
227 	g_return_val_if_fail (NAUTILUS_IS_FILE_INFO (file), NULL);
228 	g_return_val_if_fail (NAUTILUS_FILE_INFO_GET_IFACE (file)->get_mime_type != NULL, NULL);
229 
230 	return NAUTILUS_FILE_INFO_GET_IFACE (file)->get_mime_type (file);
231 }
232 
233 gboolean
234 nautilus_file_info_is_mime_type (NautilusFileInfo *file,
235 				 const char *mime_type)
236 {
237 	g_return_val_if_fail (NAUTILUS_IS_FILE_INFO (file), FALSE);
238 	g_return_val_if_fail (mime_type != NULL, FALSE);
239 	g_return_val_if_fail (NAUTILUS_FILE_INFO_GET_IFACE (file)->is_mime_type != NULL, FALSE);
240 
241 	return NAUTILUS_FILE_INFO_GET_IFACE (file)->is_mime_type (file,
242 								  mime_type);
243 }
244 
245 gboolean
246 nautilus_file_info_is_directory (NautilusFileInfo *file)
247 {
248 	g_return_val_if_fail (NAUTILUS_IS_FILE_INFO (file), FALSE);
249 	g_return_val_if_fail (NAUTILUS_FILE_INFO_GET_IFACE (file)->is_directory != NULL, FALSE);
250 
251 	return NAUTILUS_FILE_INFO_GET_IFACE (file)->is_directory (file);
252 }
253 
254 gboolean
255 nautilus_file_info_can_write (NautilusFileInfo *file)
256 {
257 	g_return_val_if_fail (NAUTILUS_IS_FILE_INFO (file), FALSE);
258 	g_return_val_if_fail (NAUTILUS_FILE_INFO_GET_IFACE (file)->can_write != NULL, FALSE);
259 
260 	return NAUTILUS_FILE_INFO_GET_IFACE (file)->can_write (file);
261 }
262 
263 void
264 nautilus_file_info_add_emblem (NautilusFileInfo *file,
265 			       const char *emblem_name)
266 {
267 	g_return_if_fail (NAUTILUS_IS_FILE_INFO (file));
268 	g_return_if_fail (NAUTILUS_FILE_INFO_GET_IFACE (file)->add_emblem != NULL);
269 
270 	NAUTILUS_FILE_INFO_GET_IFACE (file)->add_emblem (file, emblem_name);
271 }
272 
273 char *
274 nautilus_file_info_get_string_attribute (NautilusFileInfo *file,
275 					 const char *attribute_name)
276 {
277 	g_return_val_if_fail (NAUTILUS_IS_FILE_INFO (file), NULL);
278 	g_return_val_if_fail (NAUTILUS_FILE_INFO_GET_IFACE (file)->get_string_attribute != NULL, NULL);
279 	g_return_val_if_fail (attribute_name != NULL, NULL);
280 
281 	return NAUTILUS_FILE_INFO_GET_IFACE (file)->get_string_attribute 
282 		(file, attribute_name);
283 }
284 
285 void
286 nautilus_file_info_add_string_attribute (NautilusFileInfo *file,
287 					 const char *attribute_name,
288 					 const char *value)
289 {
290 	g_return_if_fail (NAUTILUS_IS_FILE_INFO (file));
291 	g_return_if_fail (NAUTILUS_FILE_INFO_GET_IFACE (file)->add_string_attribute != NULL);
292 	g_return_if_fail (attribute_name != NULL);
293 	g_return_if_fail (value != NULL);
294 	
295 	NAUTILUS_FILE_INFO_GET_IFACE (file)->add_string_attribute 
296 		(file, attribute_name, value);
297 }
298 
299 void
300 nautilus_file_info_invalidate_extension_info (NautilusFileInfo *file)
301 {
302 	g_return_if_fail (NAUTILUS_IS_FILE_INFO (file));
303 	g_return_if_fail (NAUTILUS_FILE_INFO_GET_IFACE (file)->invalidate_extension_info != NULL);
304 	
305 	NAUTILUS_FILE_INFO_GET_IFACE (file)->invalidate_extension_info (file);
306 }
307 
308 /**
309  * nautilus_file_info_lookup:
310  * @location: the location to lookup the file info for
311  *
312  * Returns: (transfer full): a #NautilusFileInfo
313  */
314 NautilusFileInfo *
315 nautilus_file_info_lookup (GFile *location)
316 {
317 	return nautilus_file_info_getter (location, FALSE);
318 }
319 
320 /**
321  * nautilus_file_info_create:
322  * @location: the location to create the file info for
323  *
324  * Returns: (transfer full): a #NautilusFileInfo
325  */
326 NautilusFileInfo *
327 nautilus_file_info_create (GFile *location)
328 {
329 	return nautilus_file_info_getter (location, TRUE);
330 }
331 
332 /**
333  * nautilus_file_info_lookup_for_uri:
334  * @uri: the URI to lookup the file info for
335  *
336  * Returns: (transfer full): a #NautilusFileInfo
337  */
338 NautilusFileInfo *
339 nautilus_file_info_lookup_for_uri (const char *uri)
340 {
341 	GFile *location;
342 	NautilusFile *file;
343 
344 	location = g_file_new_for_uri (uri);
345 	file = nautilus_file_info_lookup (location);
346 	g_object_unref (location);
347 
348 	return file;
349 }
350 
351 /**
352  * nautilus_file_info_create_for_uri:
353  * @uri: the URI to lookup the file info for
354  *
355  * Returns: (transfer full): a #NautilusFileInfo
356  */
357 NautilusFileInfo *
358 nautilus_file_info_create_for_uri (const char *uri)
359 {
360 	GFile *location;
361 	NautilusFile *file;
362 
363 	location = g_file_new_for_uri (uri);
364 	file = nautilus_file_info_create (location);
365 	g_object_unref (location);
366 
367 	return file;
368 }