/* * Implementation of NAIIOProvider::duplicate_data * Add a ref on CappDesktopFile data, so that unreffing origin object in CACT * does not invalid duplicated pointer */ guint cadp_iio_provider_duplicate_data( const NAIIOProvider *provider, NAObjectItem *dest, const NAObjectItem *source, GSList **messages ) { static const gchar *thisfn = "cadp_iio_provider_duplicate_data"; guint ret; CappDesktopProvider *self; CappDesktopFile *ndf; g_debug( "%s: provider=%p (%s), dest=%p (%s), source=%p (%s), messages=%p", thisfn, ( void * ) provider, G_OBJECT_TYPE_NAME( provider ), ( void * ) dest, G_OBJECT_TYPE_NAME( dest ), ( void * ) source, G_OBJECT_TYPE_NAME( source ), ( void * ) messages ); ret = NA_IIO_PROVIDER_CODE_PROGRAM_ERROR; g_return_val_if_fail( NA_IS_IIO_PROVIDER( provider ), ret ); g_return_val_if_fail( CADP_IS_DESKTOP_PROVIDER( provider ), ret ); g_return_val_if_fail( NA_IS_OBJECT_ITEM( dest ), ret ); g_return_val_if_fail( NA_IS_OBJECT_ITEM( source ), ret ); self = CADP_DESKTOP_PROVIDER( provider ); if( self->private->dispose_has_run ){ return( NA_IIO_PROVIDER_CODE_NOT_WILLING_TO_RUN ); } ndf = ( CappDesktopFile * ) na_object_get_provider_data( source ); g_return_val_if_fail( ndf && CADP_IS_DESKTOP_FILE( ndf ), ret ); na_object_set_provider_data( dest, g_object_ref( ndf )); g_object_weak_ref( G_OBJECT( dest ), ( GWeakNotify ) desktop_weak_notify, ndf ); return( NA_IIO_PROVIDER_CODE_OK ); }
/** * nadp_reader_iimporter_import_from_uri: * @instance: the #NAIImporter provider. * @parms: a #NAIImporterUriParms structure. * * Imports an item. * * Returns: the import operation code. * * As soon as we have a valid .desktop file, we are most probably willing * to successfully import an action or a menu of it. * * GLib does not have any primitive to load a key file from an uri. * So we have to load the file into memory, and then try to load the key * file from the memory data. * * Starting with N-A 3.2, we only honor the version 2 of #NAIImporter interface, * thus no more checking here against possible duplicate identifiers. */ guint nadp_reader_iimporter_import_from_uri( const NAIImporter *instance, void *parms_ptr ) { static const gchar *thisfn = "nadp_reader_iimporter_import_from_uri"; guint code; NAIImporterImportFromUriParmsv2 *parms; NadpDesktopFile *ndf; g_debug( "%s: instance=%p, parms=%p", thisfn, ( void * ) instance, parms_ptr ); g_return_val_if_fail( NA_IS_IIMPORTER( instance ), IMPORTER_CODE_PROGRAM_ERROR ); g_return_val_if_fail( NADP_IS_DESKTOP_PROVIDER( instance ), IMPORTER_CODE_PROGRAM_ERROR ); parms = ( NAIImporterImportFromUriParmsv2 * ) parms_ptr; if( !na_core_utils_file_is_loadable( parms->uri )){ code = IMPORTER_CODE_NOT_LOADABLE; return( code ); } code = IMPORTER_CODE_NOT_WILLING_TO; ndf = nadp_desktop_file_new_from_uri( parms->uri ); if( ndf ){ parms->imported = ( NAObjectItem * ) item_from_desktop_file( ( const NadpDesktopProvider * ) NADP_DESKTOP_PROVIDER( instance ), ndf, &parms->messages ); if( parms->imported ){ g_return_val_if_fail( NA_IS_OBJECT_ITEM( parms->imported ), IMPORTER_CODE_NOT_WILLING_TO ); /* remove the weak reference on desktop file set by 'item_from_desktop_file' * as we must consider this #NAObjectItem as a new one */ na_object_set_provider_data( parms->imported, NULL ); g_object_weak_unref( G_OBJECT( parms->imported ), ( GWeakNotify ) desktop_weak_notify, ndf ); g_object_unref( ndf ); /* also remove the 'writable' status' */ na_object_set_readonly( parms->imported, FALSE ); code = IMPORTER_CODE_OK; } } if( code == IMPORTER_CODE_NOT_WILLING_TO ){ na_core_utils_slist_add_message( &parms->messages, ERR_NOT_DESKTOP ); } return( code ); }
/* * Returns a newly allocated NAIFactoryObject-derived object, initialized * from the .desktop file */ static NAIFactoryObject * item_from_desktop_file( const NadpDesktopProvider *provider, NadpDesktopFile *ndf, GSList **messages ) { /*static const gchar *thisfn = "nadp_reader_item_from_desktop_file";*/ NAIFactoryObject *item; gchar *type; NadpReaderData *reader_data; gchar *id; item = NULL; type = nadp_desktop_file_get_file_type( ndf ); if( !strcmp( type, NADP_VALUE_TYPE_ACTION )){ item = NA_IFACTORY_OBJECT( na_object_action_new()); } else if( !strcmp( type, NADP_VALUE_TYPE_MENU )){ item = NA_IFACTORY_OBJECT( na_object_menu_new()); } else { /* i18n: 'type' is the nature of the item: Action or Menu */ na_core_utils_slist_add_message( messages, _( "unknown type: %s" ), type ); } if( item ){ id = nadp_desktop_file_get_id( ndf ); na_object_set_id( item, id ); g_free( id ); reader_data = g_new0( NadpReaderData, 1 ); reader_data->ndf = ndf; na_ifactory_provider_read_item( NA_IFACTORY_PROVIDER( provider ), reader_data, item, messages ); na_object_set_provider_data( item, ndf ); g_object_weak_ref( G_OBJECT( item ), ( GWeakNotify ) desktop_weak_notify, ndf ); g_free( reader_data ); } g_free( type ); return( item ); }
/* * This is implementation of NAIIOProvider::write_item method */ guint cadp_iio_provider_write_item( const NAIIOProvider *provider, const NAObjectItem *item, GSList **messages ) { static const gchar *thisfn = "cadp_iio_provider_write_item"; guint ret; CappDesktopFile *ndf; gchar *path; gchar *userdir; gchar *id; gchar *bname; GSList *subdirs; gchar *fulldir; gboolean dir_ok; ret = NA_IIO_PROVIDER_CODE_PROGRAM_ERROR; g_return_val_if_fail( CADP_IS_DESKTOP_PROVIDER( provider ), ret ); g_return_val_if_fail( NA_IS_OBJECT_ITEM( item ), ret ); if( na_object_is_readonly( item )){ g_warning( "%s: item=%p is read-only", thisfn, ( void * ) item ); return( ret ); } ndf = ( CappDesktopFile * ) na_object_get_provider_data( item ); /* write into the current key file and write it to current path */ if( ndf ){ g_return_val_if_fail( CADP_IS_DESKTOP_FILE( ndf ), ret ); } else { userdir = cadp_xdg_dirs_get_user_data_dir(); subdirs = na_core_utils_slist_from_split( CADP_DESKTOP_PROVIDER_SUBDIRS, G_SEARCHPATH_SEPARATOR_S ); fulldir = g_build_filename( userdir, ( gchar * ) subdirs->data, NULL ); dir_ok = TRUE; if( !g_file_test( fulldir, G_FILE_TEST_IS_DIR )){ if( g_mkdir_with_parents( fulldir, 0750 )){ g_warning( "%s: %s: %s", thisfn, userdir, g_strerror( errno )); dir_ok = FALSE; } else { na_core_utils_dir_list_perms( userdir, thisfn ); } } g_free( userdir ); na_core_utils_slist_free( subdirs ); if( dir_ok ){ id = na_object_get_id( item ); bname = g_strdup_printf( "%s%s", id, CADP_DESKTOP_FILE_SUFFIX ); g_free( id ); path = g_build_filename( fulldir, bname, NULL ); g_free( bname ); } g_free( fulldir ); if( dir_ok ){ ndf = cadp_desktop_file_new_for_write( path ); na_object_set_provider_data( item, ndf ); g_object_weak_ref( G_OBJECT( item ), ( GWeakNotify ) desktop_weak_notify, ndf ); g_free( path ); } } if( ndf ){ ret = write_item( provider, item, ndf, messages ); } return( ret ); }