/** * cadp_writer_iexporter_export_to_file: * @instance: this #NAIExporter instance. * @parms: a #NAIExporterFileParmsv2 structure. * * Export the specified 'item' to a newly created file. */ guint cadp_writer_iexporter_export_to_file( const NAIExporter *instance, NAIExporterFileParmsv2 *parms ) { static const gchar *thisfn = "cadp_writer_iexporter_export_to_file"; guint code, write_code; gchar *id, *folder_path, *dest_path; ExportFormatFn *fmt; CappDesktopFile *ndf; g_debug( "%s: instance=%p, parms=%p", thisfn, ( void * ) instance, ( void * ) parms ); parms->basename = NULL; code = NA_IEXPORTER_CODE_OK; if( !parms->exported || !NA_IS_OBJECT_ITEM( parms->exported )){ code = NA_IEXPORTER_CODE_INVALID_ITEM; } if( code == NA_IEXPORTER_CODE_OK ){ #ifdef NA_ENABLE_DEPRECATED if( parms->version == 1 ){ fmt = find_export_format_fn_from_quark((( NAIExporterFileParms * ) parms )->format ); } else { fmt = find_export_format_fn( parms->format ); } #else fmt = find_export_format_fn( parms->format ); #endif if( !fmt ){ code = NA_IEXPORTER_CODE_INVALID_FORMAT; } else { id = na_object_get_id( parms->exported ); parms->basename = g_strdup_printf( "%s%s", id, CADP_DESKTOP_FILE_SUFFIX ); g_free( id ); folder_path = g_filename_from_uri( parms->folder, NULL, NULL ); dest_path = g_strdup_printf( "%s/%s", folder_path, parms->basename ); g_free( folder_path ); ndf = cadp_desktop_file_new_for_write( dest_path ); write_code = na_ifactory_provider_write_item( NA_IFACTORY_PROVIDER( instance ), ndf, NA_IFACTORY_OBJECT( parms->exported ), &parms->messages ); if( write_code != NA_IIO_PROVIDER_CODE_OK ){ code = NA_IEXPORTER_CODE_ERROR; } else if( !cadp_desktop_file_write( ndf )){ code = NA_IEXPORTER_CODE_UNABLE_TO_WRITE; } g_free( dest_path ); g_object_unref( ndf ); } } g_debug( "%s: returning code=%u", thisfn, code ); return( code ); }
/* * First check here for duplicates inside of imported population, * then delegates to the caller-provided check function the rest of work... */ static NAObjectItem * is_importing_already_exists( NAImporterParms *parms, GList *results, NAImporterResult *result ) { static const gchar *thisfn = "na_importer_is_importing_already_exists"; NAObjectItem *exists; GList *ip; exists = NULL; gchar *importing_id = na_object_get_id( result->imported ); g_debug( "%s: importing=%p, id=%s", thisfn, ( void * ) result->imported, importing_id ); /* is the importing item already in the current importation list ? * (only tries previous items of the list) */ for( ip = results ; ip && !exists && ip->data != result ; ip = ip->next ){ NAImporterResult *try_result = ( NAImporterResult * ) ip->data; if( try_result->imported ){ g_return_val_if_fail( NA_IS_OBJECT_ITEM( try_result->imported ), NULL ); gchar *id = na_object_get_id( try_result->imported ); if( !strcmp( importing_id, id )){ exists = NA_OBJECT_ITEM( try_result->imported ); } g_free( id ); } } g_free( importing_id ); /* if not found in our current importation list, * then check the existence via provided function and data */ if( !exists ){ exists = parms->check_fn( result->imported, parms->check_fn_data ); } return( exists ); }
/* * when writing to .desktop file a profile which has both a path and parameters, * then concatenate these two fields to the 'Exec' key */ guint cadp_writer_ifactory_provider_write_data( const NAIFactoryProvider *provider, void *writer_data, const NAIFactoryObject *object, const NADataBoxed *boxed, GSList **messages ) { static const gchar *thisfn = "cadp_writer_ifactory_provider_write_data"; CappDesktopFile *ndf; guint code; const NADataDef *def; gchar *profile_id; gchar *group_name; gchar *str_value; gboolean bool_value; GSList *slist_value; guint uint_value; gchar *parms, *tmp; g_return_val_if_fail( CADP_IS_DESKTOP_FILE( writer_data ), NA_IIO_PROVIDER_CODE_PROGRAM_ERROR ); /*g_debug( "%s: object=%p (%s)", thisfn, ( void * ) object, G_OBJECT_TYPE_NAME( object ));*/ code = NA_IIO_PROVIDER_CODE_OK; ndf = CADP_DESKTOP_FILE( writer_data ); def = na_data_boxed_get_data_def( boxed ); if( def->desktop_entry && strlen( def->desktop_entry )){ if( NA_IS_OBJECT_PROFILE( object )){ profile_id = na_object_get_id( object ); group_name = g_strdup_printf( "%s %s", CADP_GROUP_PROFILE, profile_id ); g_free( profile_id ); } else { group_name = g_strdup( CADP_GROUP_DESKTOP ); } if( !na_data_boxed_is_default( boxed ) || def->write_if_default ){ switch( def->type ){ case NA_DATA_TYPE_STRING: str_value = na_boxed_get_string( NA_BOXED( boxed )); if( !strcmp( def->name, NAFO_DATA_PATH )){ parms = na_object_get_parameters( object ); tmp = g_strdup_printf( "%s %s", str_value, parms ); g_free( str_value ); g_free( parms ); str_value = tmp; } cadp_desktop_file_set_string( ndf, group_name, def->desktop_entry, str_value ); g_free( str_value ); break; case NA_DATA_TYPE_LOCALE_STRING: str_value = na_boxed_get_string( NA_BOXED( boxed )); cadp_desktop_file_set_locale_string( ndf, group_name, def->desktop_entry, str_value ); g_free( str_value ); break; case NA_DATA_TYPE_BOOLEAN: bool_value = GPOINTER_TO_UINT( na_boxed_get_as_void( NA_BOXED( boxed ))); cadp_desktop_file_set_boolean( ndf, group_name, def->desktop_entry, bool_value ); break; case NA_DATA_TYPE_STRING_LIST: slist_value = ( GSList * ) na_boxed_get_as_void( NA_BOXED( boxed )); cadp_desktop_file_set_string_list( ndf, group_name, def->desktop_entry, slist_value ); na_core_utils_slist_free( slist_value ); break; case NA_DATA_TYPE_UINT: uint_value = GPOINTER_TO_UINT( na_boxed_get_as_void( NA_BOXED( boxed ))); cadp_desktop_file_set_uint( ndf, group_name, def->desktop_entry, uint_value ); break; default: g_warning( "%s: unknown type=%u for %s", thisfn, def->type, def->name ); code = NA_IIO_PROVIDER_CODE_PROGRAM_ERROR; } } else { cadp_desktop_file_remove_key( ndf, group_name, def->desktop_entry ); } g_free( group_name ); } return( code ); }
/* * 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 ); }
/* * reading any data from a desktop file requires: * - a NadpDesktopFile object which has been initialized with the .desktop file * -> has been attached to the NAObjectItem in get_item() above * - the data type (+ reading default value) * - group and key names * * Returns: NULL if the key has not been found * letting the caller deal with default values */ NADataBoxed * nadp_reader_ifactory_provider_read_data( const NAIFactoryProvider *reader, void *reader_data, const NAIFactoryObject *object, const NADataDef *def, GSList **messages ) { static const gchar *thisfn = "nadp_reader_ifactory_provider_read_data"; NADataBoxed *boxed; gboolean found; NadpReaderData *nrd; gchar *group, *id; gchar *msg; gchar *str_value; gboolean bool_value; GSList *slist_value; guint uint_value; g_return_val_if_fail( NA_IS_IFACTORY_PROVIDER( reader ), NULL ); g_return_val_if_fail( NADP_IS_DESKTOP_PROVIDER( reader ), NULL ); g_return_val_if_fail( NA_IS_IFACTORY_OBJECT( object ), NULL ); boxed = NULL; if( !NADP_DESKTOP_PROVIDER( reader )->private->dispose_has_run ){ nrd = ( NadpReaderData * ) reader_data; g_return_val_if_fail( NADP_IS_DESKTOP_FILE( nrd->ndf ), NULL ); if( def->desktop_entry ){ if( NA_IS_OBJECT_ITEM( object )){ group = g_strdup( NADP_GROUP_DESKTOP ); } else { g_return_val_if_fail( NA_IS_OBJECT_PROFILE( object ), NULL ); id = na_object_get_id( object ); group = g_strdup_printf( "%s %s", NADP_GROUP_PROFILE, id ); g_free( id ); } switch( def->type ){ case NA_DATA_TYPE_LOCALE_STRING: str_value = nadp_desktop_file_get_locale_string( nrd->ndf, group, def->desktop_entry, &found, def->default_value ); if( found ){ boxed = na_data_boxed_new( def ); na_boxed_set_from_void( NA_BOXED( boxed ), str_value ); } g_free( str_value ); break; case NA_DATA_TYPE_STRING: str_value = nadp_desktop_file_get_string( nrd->ndf, group, def->desktop_entry, &found, def->default_value ); if( found ){ boxed = na_data_boxed_new( def ); na_boxed_set_from_void( NA_BOXED( boxed ), str_value ); } g_free( str_value ); break; case NA_DATA_TYPE_BOOLEAN: bool_value = nadp_desktop_file_get_boolean( nrd->ndf, group, def->desktop_entry, &found, na_core_utils_boolean_from_string( def->default_value )); if( found ){ boxed = na_data_boxed_new( def ); na_boxed_set_from_void( NA_BOXED( boxed ), GUINT_TO_POINTER( bool_value )); } break; case NA_DATA_TYPE_STRING_LIST: slist_value = nadp_desktop_file_get_string_list( nrd->ndf, group, def->desktop_entry, &found, def->default_value ); if( found ){ boxed = na_data_boxed_new( def ); na_boxed_set_from_void( NA_BOXED( boxed ), slist_value ); } na_core_utils_slist_free( slist_value ); break; case NA_DATA_TYPE_UINT: uint_value = nadp_desktop_file_get_uint( nrd->ndf, group, def->desktop_entry, &found, atoi( def->default_value )); if( found ){ boxed = na_data_boxed_new( def ); na_boxed_set_from_void( NA_BOXED( boxed ), GUINT_TO_POINTER( uint_value )); } break; default: msg = g_strdup_printf( "%s: %d: invalid data type.", thisfn, def->type ); g_warning( "%s", msg ); *messages = g_slist_append( *messages, msg ); } g_free( group ); } } return( boxed ); }
/* * check for existence of the imported item * ask for the user if needed */ static void manage_import_mode( NAImporterParms *parms, GList *results, NAImporterAskUserParms *ask_parms, NAImporterResult *result ) { static const gchar *thisfn = "na_importer_manage_import_mode"; NAObjectItem *exists; guint mode; gchar *id; exists = NULL; result->exist = FALSE; result->mode = parms->preferred_mode; mode = 0; /* if no check function is provided, then we systematically allocate * a new identifier to the imported item */ if( !parms->check_fn ){ renumber_label_item( result->imported ); na_core_utils_slist_add_message( &result->messages, "%s", _( "Item was renumbered because the caller did not provide any check function." )); result->mode = IMPORTER_MODE_RENUMBER; } else { exists = is_importing_already_exists( parms, results, result ); } g_debug( "%s: exists=%p", thisfn, exists ); if( exists ){ result->exist = TRUE; if( parms->preferred_mode == IMPORTER_MODE_ASK ){ mode = ask_user_for_mode( result->imported, exists, ask_parms ); } else { mode = parms->preferred_mode; } } /* mode is only set if asked mode was "ask me" and an ask function was provided * or if asked mode was not "ask me" */ if( mode ){ result->mode = mode; switch( mode ){ case IMPORTER_MODE_RENUMBER: renumber_label_item( result->imported ); if( parms->preferred_mode == IMPORTER_MODE_ASK ){ na_core_utils_slist_add_message( &result->messages, "%s", _( "Item was renumbered due to user request." )); } break; case IMPORTER_MODE_OVERRIDE: if( parms->preferred_mode == IMPORTER_MODE_ASK ){ na_core_utils_slist_add_message( &result->messages, "%s", _( "Existing item was overriden due to user request." )); } break; case IMPORTER_MODE_NO_IMPORT: default: id = na_object_get_id( result->imported ); na_core_utils_slist_add_message( &result->messages, _( "Item %s already exists." ), id ); if( parms->preferred_mode == IMPORTER_MODE_ASK ){ na_core_utils_slist_add_message( &result->messages, "%s", _( "Import was canceled due to user request." )); } g_free( id ); g_object_unref( result->imported ); result->imported = NULL; } } }