Update copyright message
[nautilus-actions.git] / src / io-desktop / nadp-desktop-file.c
blob58def40785b5db28efadba5b90936caa05e812a7
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 <string.h>
37 #include <gio/gio.h>
39 #include <api/na-core-utils.h>
41 #include "nadp-desktop-file.h"
42 #include "nadp-keys.h"
44 /* private class data
46 struct NadpDesktopFileClassPrivate {
47 void *empty; /* so that gcc -pedantic is happy */
50 /* private instance data
52 struct NadpDesktopFilePrivate {
53 gboolean dispose_has_run;
54 gchar *id;
55 gchar *uri;
56 GKeyFile *key_file;
59 static GObjectClass *st_parent_class = NULL;
61 static GType register_type( void );
62 static void class_init( NadpDesktopFileClass *klass );
63 static void instance_init( GTypeInstance *instance, gpointer klass );
64 static void instance_dispose( GObject *object );
65 static void instance_finalize( GObject *object );
67 static NadpDesktopFile *ndf_new( const gchar *uri );
68 static gchar *path2id( const gchar *path );
69 static gchar *uri2id( const gchar *uri );
70 static gboolean check_key_file( NadpDesktopFile *ndf );
71 static void remove_encoding_part( NadpDesktopFile *ndf );
73 GType
74 nadp_desktop_file_get_type( void )
76 static GType class_type = 0;
78 if( !class_type ){
79 class_type = register_type();
82 return( class_type );
85 static GType
86 register_type( void )
88 static const gchar *thisfn = "nadp_desktop_file_register_type";
89 GType type;
91 static GTypeInfo info = {
92 sizeof( NadpDesktopFileClass ),
93 NULL,
94 NULL,
95 ( GClassInitFunc ) class_init,
96 NULL,
97 NULL,
98 sizeof( NadpDesktopFile ),
100 ( GInstanceInitFunc ) instance_init
103 g_debug( "%s", thisfn );
105 type = g_type_register_static( G_TYPE_OBJECT, "NadpDesktopFile", &info, 0 );
107 return( type );
110 static void
111 class_init( NadpDesktopFileClass *klass )
113 static const gchar *thisfn = "nadp_desktop_file_class_init";
114 GObjectClass *object_class;
116 g_debug( "%s: klass=%p", thisfn, ( void * ) klass );
118 st_parent_class = g_type_class_peek_parent( klass );
120 object_class = G_OBJECT_CLASS( klass );
121 object_class->dispose = instance_dispose;
122 object_class->finalize = instance_finalize;
124 klass->private = g_new0( NadpDesktopFileClassPrivate, 1 );
127 static void
128 instance_init( GTypeInstance *instance, gpointer klass )
130 static const gchar *thisfn = "nadp_desktop_file_instance_init";
131 NadpDesktopFile *self;
133 g_debug( "%s: instance=%p (%s), klass=%p",
134 thisfn, ( void * ) instance, G_OBJECT_TYPE_NAME( instance ), ( void * ) klass );
135 g_return_if_fail( NADP_IS_DESKTOP_FILE( instance ));
136 self = NADP_DESKTOP_FILE( instance );
138 self->private = g_new0( NadpDesktopFilePrivate, 1 );
140 self->private->dispose_has_run = FALSE;
141 self->private->key_file = g_key_file_new();
144 static void
145 instance_dispose( GObject *object )
147 static const gchar *thisfn = "nadp_desktop_file_instance_dispose";
148 NadpDesktopFile *self;
150 g_debug( "%s: object=%p (%s)", thisfn, ( void * ) object, G_OBJECT_TYPE_NAME( object ));
151 g_return_if_fail( NADP_IS_DESKTOP_FILE( object ));
152 self = NADP_DESKTOP_FILE( object );
154 if( !self->private->dispose_has_run ){
156 self->private->dispose_has_run = TRUE;
158 /* chain up to the parent class */
159 if( G_OBJECT_CLASS( st_parent_class )->dispose ){
160 G_OBJECT_CLASS( st_parent_class )->dispose( object );
165 static void
166 instance_finalize( GObject *object )
168 NadpDesktopFile *self;
170 g_assert( NADP_IS_DESKTOP_FILE( object ));
171 self = NADP_DESKTOP_FILE( object );
173 g_free( self->private->id );
174 g_free( self->private->uri );
176 if( self->private->key_file ){
177 g_key_file_free( self->private->key_file );
180 g_free( self->private );
182 /* chain call to parent class */
183 if( G_OBJECT_CLASS( st_parent_class )->finalize ){
184 G_OBJECT_CLASS( st_parent_class )->finalize( object );
189 * nadp_desktop_file_new:
191 * Retuns: a newly allocated #NadpDesktopFile object.
193 NadpDesktopFile *
194 nadp_desktop_file_new( void )
196 NadpDesktopFile *ndf;
198 ndf = g_object_new( NADP_DESKTOP_FILE_TYPE, NULL );
200 return( ndf );
204 * nadp_desktop_file_new_from_path:
205 * @path: the full pathname of a .desktop file.
207 * Retuns: a newly allocated #NadpDesktopFile object.
209 * Key file has been loaded, and first validity checks made.
211 NadpDesktopFile *
212 nadp_desktop_file_new_from_path( const gchar *path )
214 static const gchar *thisfn = "nadp_desktop_file_new_from_path";
215 NadpDesktopFile *ndf;
216 GError *error;
217 gchar *uri;
219 ndf = NULL;
220 g_debug( "%s: path=%s", thisfn, path );
221 g_return_val_if_fail( path && g_utf8_strlen( path, -1 ) && g_path_is_absolute( path ), ndf );
223 error = NULL;
224 uri = g_filename_to_uri( path, NULL, &error );
225 if( !uri || error ){
226 g_warning( "%s: %s: %s", thisfn, path, error->message );
227 g_error_free( error );
228 g_free( uri );
229 return( NULL );
232 ndf = ndf_new( uri );
234 g_free( uri );
236 g_key_file_load_from_file( ndf->private->key_file, path, G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &error );
237 if( error ){
238 g_warning( "%s: %s: %s", thisfn, path, error->message );
239 g_error_free( error );
240 g_object_unref( ndf );
241 return( NULL );
244 if( !check_key_file( ndf )){
245 g_object_unref( ndf );
246 return( NULL );
249 return( ndf );
253 * nadp_desktop_file_new_from_uri:
254 * @uri: the URI the desktop file should be loaded from.
256 * Retuns: a newly allocated #NadpDesktopFile object, or %NULL.
258 * Key file has been loaded, and first validity checks made.
260 * Note: This function is in particular used when importing a file.
261 * So it is rather common that the file not be a .desktop one.
262 * Do not warns when file is malformed.
264 NadpDesktopFile *
265 nadp_desktop_file_new_from_uri( const gchar *uri )
267 static const gchar *thisfn = "nadp_desktop_file_new_from_uri";
268 NadpDesktopFile *ndf;
269 GError *error;
270 gchar *data;
271 gsize length;
273 ndf = NULL;
274 g_debug( "%s: uri=%s", thisfn, uri );
275 g_return_val_if_fail( uri && g_utf8_strlen( uri, -1 ), ndf );
277 ndf = ndf_new( uri );
278 data = na_core_utils_file_load_from_uri( uri, &length );
280 error = NULL;
281 g_key_file_load_from_data( ndf->private->key_file, data, length, G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &error );
282 if( error ){
283 if( error->code != G_KEY_FILE_ERROR_GROUP_NOT_FOUND ){
284 g_warning( "%s: %s", thisfn, error->message );
286 g_error_free( error );
287 g_object_unref( ndf );
288 g_free( data );
289 return( NULL );
292 g_free( data );
294 if( !check_key_file( ndf )){
295 g_object_unref( ndf );
296 return( NULL );
299 return( ndf );
303 * nadp_desktop_file_new_for_write:
304 * @path: the full pathname of a .desktop file.
306 * Retuns: a newly allocated #NadpDesktopFile object.
308 NadpDesktopFile *
309 nadp_desktop_file_new_for_write( const gchar *path )
311 static const gchar *thisfn = "nadp_desktop_file_new_for_write";
312 NadpDesktopFile *ndf;
313 GError *error;
314 gchar *uri;
316 ndf = NULL;
317 g_debug( "%s: path=%s", thisfn, path );
318 g_return_val_if_fail( path && g_utf8_strlen( path, -1 ) && g_path_is_absolute( path ), ndf );
320 error = NULL;
321 uri = g_filename_to_uri( path, NULL, &error );
322 if( !uri || error ){
323 g_warning( "%s: %s: %s", thisfn, path, error->message );
324 g_error_free( error );
325 g_free( uri );
326 return( NULL );
329 ndf = ndf_new( uri );
331 g_free( uri );
333 return( ndf );
337 * nadp_desktop_file_get_key_file:
338 * @ndf: the #NadpDesktopFile instance.
340 * Returns: a pointer to the internal #GKeyFile.
342 GKeyFile *
343 nadp_desktop_file_get_key_file( const NadpDesktopFile *ndf )
345 GKeyFile *key_file;
347 g_return_val_if_fail( NADP_IS_DESKTOP_FILE( ndf ), NULL );
349 key_file = NULL;
351 if( !ndf->private->dispose_has_run ){
353 key_file = ndf->private->key_file;
356 return( key_file );
360 * nadp_desktop_file_get_key_file_uri:
361 * @ndf: the #NadpDesktopFile instance.
363 * Returns: the URI of the key file, as a newly allocated
364 * string which should be g_free() by the caller.
366 gchar *
367 nadp_desktop_file_get_key_file_uri( const NadpDesktopFile *ndf )
369 gchar *uri;
371 g_return_val_if_fail( NADP_IS_DESKTOP_FILE( ndf ), NULL );
373 uri = NULL;
375 if( !ndf->private->dispose_has_run ){
377 uri = g_strdup( ndf->private->uri );
380 return( uri );
383 static NadpDesktopFile *
384 ndf_new( const gchar *uri )
386 NadpDesktopFile *ndf;
388 ndf = g_object_new( NADP_DESKTOP_FILE_TYPE, NULL );
390 ndf->private->id = uri2id( uri );
391 ndf->private->uri = g_strdup( uri );
393 return( ndf );
397 * The id of the file is equal to the basename, minus the suffix.
399 static gchar *
400 path2id( const gchar *path )
402 gchar *bname;
403 gchar *id;
405 bname = g_path_get_basename( path );
406 id = na_core_utils_str_remove_suffix( bname, NADP_DESKTOP_FILE_SUFFIX );
407 g_free( bname );
409 return( id );
412 static gchar *
413 uri2id( const gchar *uri )
415 gchar *path;
416 gchar *id;
418 id = NULL;
419 path = g_filename_from_uri( uri, NULL, NULL );
421 if( path ){
422 id = path2id( path );
423 g_free( path );
426 return( id );
429 static gboolean
430 check_key_file( NadpDesktopFile *ndf )
432 static const gchar *thisfn = "nadp_desktop_file_check_key_file";
433 gboolean ret;
434 gchar *start_group;
435 gboolean has_key;
436 gboolean hidden;
437 GError *error;
439 ret = TRUE;
440 error = NULL;
442 /* start group must be [Desktop Entry] */
443 start_group = g_key_file_get_start_group( ndf->private->key_file );
444 if( strcmp( start_group, NADP_GROUP_DESKTOP )){
445 g_warning( "%s: %s: invalid start group, found %s, waited for %s",
446 thisfn, ndf->private->uri, start_group, NADP_GROUP_DESKTOP );
447 ret = FALSE;
450 /* must not have Hidden=true value */
451 if( ret ){
452 has_key = g_key_file_has_key( ndf->private->key_file, start_group, NADP_KEY_HIDDEN, &error );
453 if( error ){
454 g_warning( "%s: %s: %s", thisfn, ndf->private->uri, error->message );
455 ret = FALSE;
457 } else if( has_key ){
458 hidden = g_key_file_get_boolean( ndf->private->key_file, start_group, NADP_KEY_HIDDEN, &error );
459 if( error ){
460 g_warning( "%s: %s: %s", thisfn, ndf->private->uri, error->message );
461 ret = FALSE;
463 } else if( hidden ){
464 g_debug( "%s: %s: Hidden=true", thisfn, ndf->private->uri );
465 ret = FALSE;
470 g_free( start_group );
472 return( ret );
476 * nadp_desktop_file_get_type:
477 * @ndf: the #NadpDesktopFile instance.
479 * Returns: the value for the Type entry as a newly allocated string which
480 * should be g_free() by the caller.
482 gchar *
483 nadp_desktop_file_get_file_type( const NadpDesktopFile *ndf )
485 static const gchar *thisfn = "nadp_desktop_file_get_file_type";
486 gchar *type;
487 gboolean has_key;
488 GError *error;
490 g_return_val_if_fail( NADP_IS_DESKTOP_FILE( ndf ), NULL );
492 type = NULL;
494 if( !ndf->private->dispose_has_run ){
496 error = NULL;
498 has_key = g_key_file_has_key( ndf->private->key_file, NADP_GROUP_DESKTOP, NADP_KEY_TYPE, &error );
499 if( error ){
500 g_warning( "%s: %s", thisfn, error->message );
501 g_error_free( error );
503 } else if( has_key ){
504 type = g_key_file_get_string( ndf->private->key_file, NADP_GROUP_DESKTOP, NADP_KEY_TYPE, &error );
505 if( error ){
506 g_warning( "%s: %s", thisfn, error->message );
507 g_error_free( error );
508 g_free( type );
509 type = NULL;
514 return( type );
518 * nadp_desktop_file_get_id:
519 * @ndf: the #NadpDesktopFile instance.
521 * Returns: a newly allocated string which holds the id of the Desktop
522 * File.
524 gchar *
525 nadp_desktop_file_get_id( const NadpDesktopFile *ndf )
527 gchar *value;
529 g_return_val_if_fail( NADP_IS_DESKTOP_FILE( ndf ), NULL );
531 value = NULL;
533 if( !ndf->private->dispose_has_run ){
535 value = g_strdup( ndf->private->id );
538 return( value );
542 * nadp_desktop_file_get_profiles:
543 * @ndf: the #NadpDesktopFile instance.
545 * Returns: the list of profiles in the file, as a newly allocated GSList
546 * which must be na_core_utils_slist_free() by the caller.
548 * Silently ignore unknown groups.
550 GSList *
551 nadp_desktop_file_get_profiles( const NadpDesktopFile *ndf )
553 GSList *list;
554 gchar **groups, **ig;
555 gchar *profile_pfx;
556 gchar *profile_id;
557 guint pfx_len;
559 g_return_val_if_fail( NADP_IS_DESKTOP_FILE( ndf ), NULL );
561 list = NULL;
563 if( !ndf->private->dispose_has_run ){
565 groups = g_key_file_get_groups( ndf->private->key_file, NULL );
566 if( groups ){
567 ig = groups;
568 profile_pfx = g_strdup_printf( "%s ", NADP_GROUP_PROFILE );
569 pfx_len = strlen( profile_pfx );
571 while( *ig ){
572 if( !strncmp( *ig, profile_pfx, pfx_len )){
573 profile_id = g_strdup( *ig+pfx_len );
574 list = g_slist_prepend( list, profile_id );
577 ig++;
580 g_strfreev( groups );
581 g_free( profile_pfx );
585 return( list );
589 * nadp_desktop_file_remove_key:
590 * @ndf: this #NadpDesktopFile instance.
591 * @group: the group.
592 * @key: the key.
594 * Removes the specified key.
596 * Note that this doesn't work very well for localized keys, as we only
597 * remove a key which has the exact same label that the provided one.
598 * So we'd have to remove:
599 * - key
600 * - key[en_US.UTF-8]
601 * - key[en_US]
602 * - key[en]
604 void
605 nadp_desktop_file_remove_key( const NadpDesktopFile *ndf, const gchar *group, const gchar *key )
607 char **locales;
608 char **iloc;
609 gchar *locale_key;
611 g_return_if_fail( NADP_IS_DESKTOP_FILE( ndf ));
613 if( !ndf->private->dispose_has_run ){
615 g_key_file_remove_key( ndf->private->key_file, group, key, NULL );
617 locales = ( char ** ) g_get_language_names();
618 iloc = locales;
620 while( *iloc ){
621 locale_key = g_strdup_printf( "%s[%s]", key, *iloc );
622 g_key_file_remove_key( ndf->private->key_file, group, locale_key, NULL );
623 g_free( locale_key );
624 iloc++;
630 * nadp_desktop_file_remove_profile:
631 * @ndf: this #NadpDesktopFile instance.
632 * @profile_id: the id of the profile.
634 * Removes the group which describes the specified profile.
636 void
637 nadp_desktop_file_remove_profile( const NadpDesktopFile *ndf, const gchar *profile_id )
639 gchar *group_name;
641 g_return_if_fail( NADP_IS_DESKTOP_FILE( ndf ));
643 if( !ndf->private->dispose_has_run ){
645 group_name = g_strdup_printf( "%s %s", NADP_GROUP_PROFILE, profile_id );
646 g_key_file_remove_group( ndf->private->key_file, group_name, NULL );
647 g_free( group_name );
652 * nadp_desktop_file_get_boolean:
653 * @ndf: this #NadpDesktopFile instance.
654 * @group: the searched group.
655 * @entry: the searched entry.
656 * @key_found: set to %TRUE if the key has been found, to %FALSE else.
657 * @default_value: value to be set if key has not been found.
659 * Returns: the readen value, or the default value if the entry has not
660 * been found in the given group.
662 gboolean
663 nadp_desktop_file_get_boolean( const NadpDesktopFile *ndf, const gchar *group, const gchar *entry, gboolean *key_found, gboolean default_value )
665 static const gchar *thisfn = "nadp_desktop_file_get_boolean";
666 gboolean value;
667 gboolean read_value;
668 gboolean has_entry;
669 GError *error;
671 value = default_value;
672 *key_found = FALSE;
674 g_return_val_if_fail( NADP_IS_DESKTOP_FILE( ndf ), FALSE );
676 if( !ndf->private->dispose_has_run ){
678 error = NULL;
679 has_entry = g_key_file_has_key( ndf->private->key_file, group, entry, &error );
680 if( error ){
681 g_warning( "%s: %s", thisfn, error->message );
682 g_error_free( error );
684 } else if( has_entry ){
685 read_value = g_key_file_get_boolean( ndf->private->key_file, group, entry, &error );
686 if( error ){
687 g_warning( "%s: %s", thisfn, error->message );
688 g_error_free( error );
690 } else {
691 value = read_value;
692 *key_found = TRUE;
697 return( value );
701 * nadp_desktop_file_get_locale_string:
702 * @ndf: this #NadpDesktopFile instance.
703 * @group: the searched group.
704 * @entry: the searched entry.
705 * @key_found: set to %TRUE if the key has been found, to %FALSE else.
706 * @default_value: value to be set if key has not been found.
708 * Returns: the readen value, or the default value if the entry has not
709 * been found in the given group.
711 gchar *
712 nadp_desktop_file_get_locale_string( const NadpDesktopFile *ndf, const gchar *group, const gchar *entry, gboolean *key_found, const gchar *default_value )
714 static const gchar *thisfn = "nadp_desktop_file_get_locale_string";
715 gchar *value;
716 gchar *read_value;
717 GError *error;
719 value = g_strdup( default_value );
720 *key_found = FALSE;
722 g_return_val_if_fail( NADP_IS_DESKTOP_FILE( ndf ), NULL );
724 if( !ndf->private->dispose_has_run ){
726 error = NULL;
728 read_value = g_key_file_get_locale_string( ndf->private->key_file, group, entry, NULL, &error );
729 if( !read_value || error ){
730 if( error->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND ){
731 g_warning( "%s: %s", thisfn, error->message );
732 g_error_free( error );
733 g_free( read_value );
736 } else {
737 g_free( value );
738 value = read_value;
739 *key_found = TRUE;
743 return( value );
747 * nadp_desktop_file_get_string:
748 * @ndf: this #NadpDesktopFile instance.
749 * @group: the searched group.
750 * @entry: the searched entry.
751 * @key_found: set to %TRUE if the key has been found, to %FALSE else.
752 * @default_value: value to be set if key has not been found.
754 * Returns: the readen value, or the default value if the entry has not
755 * been found in the given group.
757 gchar *
758 nadp_desktop_file_get_string( const NadpDesktopFile *ndf, const gchar *group, const gchar *entry, gboolean *key_found, const gchar *default_value )
760 static const gchar *thisfn = "nadp_desktop_file_get_string";
761 gchar *value;
762 gchar *read_value;
763 gboolean has_entry;
764 GError *error;
766 value = g_strdup( default_value );
767 *key_found = FALSE;
769 g_return_val_if_fail( NADP_IS_DESKTOP_FILE( ndf ), NULL );
771 if( !ndf->private->dispose_has_run ){
773 error = NULL;
774 has_entry = g_key_file_has_key( ndf->private->key_file, group, entry, &error );
775 if( error ){
776 g_warning( "%s: %s", thisfn, error->message );
777 g_error_free( error );
779 } else if( has_entry ){
780 read_value = g_key_file_get_string( ndf->private->key_file, group, entry, &error );
781 if( error ){
782 g_warning( "%s: %s", thisfn, error->message );
783 g_error_free( error );
784 g_free( read_value );
786 } else {
787 g_free( value );
788 value = read_value;
789 *key_found = TRUE;
794 return( value );
798 * nadp_desktop_file_get_string_list:
799 * @ndf: this #NadpDesktopFile instance.
800 * @group: the searched group.
801 * @entry: the searched entry.
802 * @key_found: set to %TRUE if the key has been found, to %FALSE else.
803 * @default_value: value to be set if key has not been found.
805 * Returns: the readen value, or the default value if the entry has not
806 * been found in the given group.
808 GSList *
809 nadp_desktop_file_get_string_list( const NadpDesktopFile *ndf, const gchar *group, const gchar *entry, gboolean *key_found, const gchar *default_value )
811 static const gchar *thisfn = "nadp_desktop_file_get_string_list";
812 GSList *value;
813 gchar **read_array;
814 gboolean has_entry;
815 GError *error;
817 value = g_slist_append( NULL, g_strdup( default_value ));
818 *key_found = FALSE;
820 g_return_val_if_fail( NADP_IS_DESKTOP_FILE( ndf ), NULL );
822 if( !ndf->private->dispose_has_run ){
824 error = NULL;
825 has_entry = g_key_file_has_key( ndf->private->key_file, group, entry, &error );
826 if( error ){
827 g_warning( "%s: %s", thisfn, error->message );
828 g_error_free( error );
830 } else if( has_entry ){
831 read_array = g_key_file_get_string_list( ndf->private->key_file, group, entry, NULL, &error );
832 if( error ){
833 g_warning( "%s: %s", thisfn, error->message );
834 g_error_free( error );
836 } else {
837 na_core_utils_slist_free( value );
838 value = na_core_utils_slist_from_array(( const gchar ** ) read_array );
839 *key_found = TRUE;
842 g_strfreev( read_array );
846 return( value );
850 * nadp_desktop_file_get_uint:
851 * @ndf: this #NadpDesktopFile instance.
852 * @group: the searched group.
853 * @entry: the searched entry.
854 * @key_found: set to %TRUE if the key has been found, to %FALSE else.
855 * @default_value: value to be set if key has not been found.
857 * Returns: the readen value, or the default value if the entry has not
858 * been found in the given group.
860 guint
861 nadp_desktop_file_get_uint( const NadpDesktopFile *ndf, const gchar *group, const gchar *entry, gboolean *key_found, guint default_value )
863 static const gchar *thisfn = "nadp_desktop_file_get_uint";
864 guint value;
865 gboolean has_entry;
866 GError *error;
868 value = default_value;
869 *key_found = FALSE;
871 g_return_val_if_fail( NADP_IS_DESKTOP_FILE( ndf ), 0 );
873 if( !ndf->private->dispose_has_run ){
875 error = NULL;
876 has_entry = g_key_file_has_key( ndf->private->key_file, group, entry, &error );
877 if( error ){
878 g_warning( "%s: %s", thisfn, error->message );
879 g_error_free( error );
881 } else if( has_entry ){
882 value = ( guint ) g_key_file_get_integer( ndf->private->key_file, group, entry, &error );
883 if( error ){
884 g_warning( "%s: %s", thisfn, error->message );
885 g_error_free( error );
887 } else {
888 *key_found = TRUE;
893 return( value );
897 * nadp_desktop_file_set_boolean:
898 * @ndf: this #NadpDesktopFile object.
899 * @group: the name of the group.
900 * @key: the name of the key.
901 * @value: the value to be written.
903 * Write the given boolean value.
905 void
906 nadp_desktop_file_set_boolean( const NadpDesktopFile *ndf, const gchar *group, const gchar *key, gboolean value )
908 g_return_if_fail( NADP_IS_DESKTOP_FILE( ndf ));
910 if( !ndf->private->dispose_has_run ){
912 g_key_file_set_boolean( ndf->private->key_file, group, key, value );
917 * nadp_desktop_file_set_locale_string:
918 * @ndf: this #NadpDesktopFile object.
919 * @group: the name of the group.
920 * @key: the name of the key.
921 * @value: the value to be written.
923 * Write the given string value.
925 * Until v 3.0.4, locale strings used to be written for all available locales
926 * e.g. 'en_US.UTF-8', 'en_US', 'en.UTF-8', 'en', 'C'.
928 * Starting with v 3.0.4, encoding part of the locale is no more written.
930 * The better solution would be to include in the UI a listbox with all
931 * available locales, letting the user choose himself which locale he wish
932 * modify.
934 * A first fallback would be to set some sort of user preferences: whether
935 * to write all available locales, whether to write all but C locales, ...
937 * As of v 3.0.4, we choose:
938 * - always write the first locale, which should obviously be the user locale;
939 * - also write all locales derived from the first (e.g. en_US, en_GB, en);
940 * - when the prefix of the locale changes, stop to write other locales unless
941 * the first prefix was 'en', as we suppose that the C locale will always be
942 * in english.
944 * The locale prefix is identified by '_' or '@' character.
946 void
947 nadp_desktop_file_set_locale_string( const NadpDesktopFile *ndf, const gchar *group, const gchar *key, const gchar *value )
949 char **locales;
950 guint i;
951 gchar *prefix;
952 gboolean write;
954 g_return_if_fail( NADP_IS_DESKTOP_FILE( ndf ));
956 if( !ndf->private->dispose_has_run ){
958 locales = ( char ** ) g_get_language_names();
960 en_US.UTF-8
961 en_US
962 en.UTF-8
967 prefix = g_strdup( locales[0] );
968 for( i = 0 ; prefix[i] ; ++i ){
969 if( prefix[i] == '_' || prefix[i] == '@' || prefix[i] == '.' ){
970 prefix[i] = '\0';
971 break;
975 /* using locales[0] writes a string with, e.g. Label[en_US.UTF-8]
976 * after that trying to read the same key with another locale, even en_US.utf-8,
977 * fails ans returns an empty string.
978 * so write all available locales for the string, so that there is a chance at
979 * least one of these will be used as default
981 * pwi 2010-12-30 v 3.0.4
982 * no more write encoding part of the locale as desktop files are supposed to
983 * be UTF-8 encoded.
985 for( i = 0 ; i < g_strv_length( locales ) ; ++i ){
986 write = FALSE;
988 if( g_strstr_len( locales[i], -1, "." )){
989 continue;
992 /* write the locale string for all locales derived from the first one */
993 if( !strncmp( locales[i], prefix, strlen( prefix ))){
994 write = TRUE;
996 /* also write other locales if first was a 'en'-derivative */
997 } else if( !strcmp( prefix, "en" )){
998 write = TRUE;
1001 if( write ){
1002 g_key_file_set_locale_string( ndf->private->key_file, group, key, locales[i], value );
1006 g_free( prefix );
1011 * nadp_desktop_file_set_string:
1012 * @ndf: this #NadpDesktopFile object.
1013 * @group: the name of the group.
1014 * @key: the name of the key.
1015 * @value: the value to be written.
1017 * Write the given string value.
1019 void
1020 nadp_desktop_file_set_string( const NadpDesktopFile *ndf, const gchar *group, const gchar *key, const gchar *value )
1022 g_return_if_fail( NADP_IS_DESKTOP_FILE( ndf ));
1024 if( !ndf->private->dispose_has_run ){
1026 g_key_file_set_string( ndf->private->key_file, group, key, value );
1031 * nadp_desktop_file_set_string_list:
1032 * @ndf: this #NadpDesktopFile object.
1033 * @group: the name of the group.
1034 * @key: the name of the key.
1035 * @value: the value to be written.
1037 * Write the given list value.
1039 void
1040 nadp_desktop_file_set_string_list( const NadpDesktopFile *ndf, const gchar *group, const gchar *key, GSList *value )
1042 gchar **array;
1044 g_return_if_fail( NADP_IS_DESKTOP_FILE( ndf ));
1046 if( !ndf->private->dispose_has_run ){
1048 array = na_core_utils_slist_to_array( value );
1049 g_key_file_set_string_list( ndf->private->key_file, group, key, ( const gchar * const * ) array, g_slist_length( value ));
1050 g_strfreev( array );
1055 * nadp_desktop_file_set_uint:
1056 * @ndf: this #NadpDesktopFile object.
1057 * @group: the name of the group.
1058 * @key: the name of the key.
1059 * @value: the value to be written.
1061 * Write the given uint value.
1063 void
1064 nadp_desktop_file_set_uint( const NadpDesktopFile *ndf, const gchar *group, const gchar *key, guint value )
1066 g_return_if_fail( NADP_IS_DESKTOP_FILE( ndf ));
1068 if( !ndf->private->dispose_has_run ){
1070 g_key_file_set_integer( ndf->private->key_file, group, key, value );
1075 * nadp_desktop_file_write:
1076 * @ndf: the #NadpDesktopFile instance.
1078 * Writes the key file to the disk.
1080 * Returns: %TRUE if write is ok, %FALSE else.
1082 * Starting with v 3.0.4, locale strings whose identifier include an
1083 * encoding part are removed from the desktop file when rewriting it
1084 * (these were wrongly written between v 2.99 and 3.0.3).
1086 gboolean
1087 nadp_desktop_file_write( NadpDesktopFile *ndf )
1089 static const gchar *thisfn = "nadp_desktop_file_write";
1090 gboolean ret;
1091 gchar *data;
1092 GFile *file;
1093 GFileOutputStream *stream;
1094 GError *error;
1095 gsize length;
1097 ret = FALSE;
1098 error = NULL;
1099 g_return_val_if_fail( NADP_IS_DESKTOP_FILE( ndf ), ret );
1101 if( !ndf->private->dispose_has_run ){
1103 if( ndf->private->key_file ){
1104 remove_encoding_part( ndf );
1107 data = g_key_file_to_data( ndf->private->key_file, &length, NULL );
1108 file = g_file_new_for_uri( ndf->private->uri );
1109 g_debug( "%s: uri=%s", thisfn, ndf->private->uri );
1111 stream = g_file_replace( file, NULL, FALSE, G_FILE_CREATE_NONE, NULL, &error );
1112 if( error ){
1113 g_warning( "%s: g_file_replace: %s", thisfn, error->message );
1114 g_error_free( error );
1115 if( stream ){
1116 g_object_unref( stream );
1118 g_object_unref( file );
1119 g_free( data );
1120 return( FALSE );
1123 g_output_stream_write( G_OUTPUT_STREAM( stream ), data, length, NULL, &error );
1124 if( error ){
1125 g_warning( "%s: g_output_stream_write: %s", thisfn, error->message );
1126 g_error_free( error );
1127 g_object_unref( stream );
1128 g_object_unref( file );
1129 g_free( data );
1130 return( FALSE );
1133 g_output_stream_close( G_OUTPUT_STREAM( stream ), NULL, &error );
1134 if( error ){
1135 g_warning( "%s: g_output_stream_close: %s", thisfn, error->message );
1136 g_error_free( error );
1137 g_object_unref( stream );
1138 g_object_unref( file );
1139 g_free( data );
1140 return( FALSE );
1143 g_object_unref( stream );
1144 g_object_unref( file );
1145 g_free( data );
1147 return( TRUE );
1150 return( FALSE );
1153 static void
1154 remove_encoding_part( NadpDesktopFile *ndf )
1156 static const gchar *thisfn = "nadp_desktop_file_remove_encoding_part";
1157 gchar **groups;
1158 gchar **keys;
1159 guint ig, ik;
1160 GRegex *regex;
1161 GMatchInfo *info;
1162 GError *error;
1164 error = NULL;
1165 regex = g_regex_new( "\\[(.*)\\.(.*)\\]$", G_REGEX_CASELESS | G_REGEX_UNGREEDY, G_REGEX_MATCH_NOTEMPTY, &error );
1166 if( error ){
1167 g_warning( "%s: %s", thisfn, error->message );
1168 g_error_free( error );
1170 } else {
1171 groups = g_key_file_get_groups( ndf->private->key_file, NULL );
1173 for( ig = 0 ; ig < g_strv_length( groups ) ; ++ig ){
1174 keys = g_key_file_get_keys( ndf->private->key_file, groups[ig], NULL, NULL );
1176 for( ik = 0 ; ik < g_strv_length( keys ) ; ++ik ){
1178 if( g_regex_match( regex, keys[ik], 0, &info )){
1179 g_key_file_remove_key( ndf->private->key_file, groups[ig], keys[ik], &error );
1180 if( error ){
1181 g_warning( "%s: %s", thisfn, error->message );
1182 g_error_free( error );
1183 error = NULL;
1187 g_match_info_free( info );
1190 g_strfreev( keys );
1193 g_strfreev( groups );
1194 g_regex_unref( regex );