NadpDesktopFile: do not try to import empty or not DES-EMA files
[nautilus-actions.git] / src / io-desktop / nadp-utils.c
blob9bff43f99f24e55747996d29ad0dbed353e010c8
1 /*
2 * Nautilus-Actions
3 * A Nautilus extension which offers configurable context menu actions.
5 * Copyright (C) 2005 The GNOME Foundation
6 * Copyright (C) 2006, 2007, 2008 Frederic Ruaudel and others (see AUTHORS)
7 * Copyright (C) 2009, 2010, 2011 Pierre Wieser and others (see AUTHORS)
9 * This Program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
14 * This Program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public
20 * License along with this Library; see the file COPYING. If not,
21 * write to the Free Software Foundation, Inc., 59 Temple Place,
22 * Suite 330, Boston, MA 02111-1307, USA.
24 * Authors:
25 * Frederic Ruaudel <grumz@grumz.net>
26 * Rodrigo Moya <rodrigo@gnome-db.org>
27 * Pierre Wieser <pwieser@trychlos.org>
28 * ... and many others (see AUTHORS)
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
35 #include <errno.h>
36 #include <gio/gio.h>
37 #include <glib/gstdio.h>
38 #include <string.h>
40 #include <api/na-core-utils.h>
42 #include "nadp-desktop-provider.h"
43 #include "nadp-utils.h"
45 /**
46 * nadp_utils_gslist_remove_from:
47 * @list: the #GSList from which remove the @string.
48 * @string: the string to be removed.
50 * Removes a @string from a string list, then frees the removed @string.
52 GSList *
53 nadp_utils_gslist_remove_from( GSList *list, const gchar *string )
55 GSList *is;
57 for( is = list ; is ; is = is->next ){
58 const gchar *istr = ( const gchar * ) is->data;
59 if( !na_core_utils_str_collate( string, istr )){
60 g_free( is->data );
61 list = g_slist_delete_link( list, is );
62 break;
66 return( list );
69 /**
72 gboolean
73 nadp_utils_uri_delete( const gchar *uri )
75 gboolean deleted;
76 gchar *scheme;
77 gchar *path;
79 deleted = FALSE;
80 scheme = g_uri_parse_scheme( uri );
82 if( !strcmp( scheme, "file" )){
83 path = g_filename_from_uri( uri, NULL, NULL );
85 if( path ){
86 deleted = na_core_utils_file_delete( path );
87 g_free( path );
91 g_free( scheme );
93 return( deleted );
96 /**
97 * nadp_utils_uri_is_writable:
98 * @uri: the URI of the file to be tested.
100 * Returns: %TRUE if the file is writable, %FALSE else.
102 * Please note that this type of test is subject to race conditions,
103 * as the file may become unwritable after a successful test,
104 * but before the caller has been able to actually write into it.
106 * There is no "super-test". Just try...
108 gboolean
109 nadp_utils_uri_is_writable( const gchar *uri )
111 static const gchar *thisfn = "nadp_utils_uri_is_writable";
112 GFile *file;
113 GError *error = NULL;
114 GFileInfo *info;
115 gboolean writable;
117 if( !uri || !g_utf8_strlen( uri, -1 )){
118 return( FALSE );
121 file = g_file_new_for_uri( uri );
122 info = g_file_query_info( file,
123 G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE "," G_FILE_ATTRIBUTE_STANDARD_TYPE,
124 G_FILE_QUERY_INFO_NONE, NULL, &error );
126 if( error ){
127 g_warning( "%s: g_file_query_info error: %s", thisfn, error->message );
128 g_error_free( error );
129 g_object_unref( file );
130 return( FALSE );
133 writable = g_file_info_get_attribute_boolean( info, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE );
134 if( !writable ){
135 g_debug( "%s: %s is not writable", thisfn, uri );
138 g_object_unref( info );
140 return( writable );