Bonobo API Reference Manual
<<< Previous Page | Home | Up | Next Page >>>

# BonoboPersistFile

## Name

BonoboPersistFile -- Interface for anything that can save / load itself from a file.

## Synopsis

    struct      BonoboPersistFile;
    int         (*BonoboPersistFileIOFn)        (BonoboPersistFile *pf,
                                                 const CORBA_char *filename,
                                                 CORBA_Environment *ev,
                                                 void *closure);
    typedef     BonoboPersistFileClass;
    void        bonobo_persist_file_set_dirty   (BonoboPersistFile *ps,
                                                 gboolean dirty);
    BonoboPersistFile* bonobo_persist_file_new  (BonoboPersistFileIOFn load_fn,
                                                 BonoboPersistFileIOFn save_fn,
                                                 void *closure);
    BonoboPersistFile* bonobo_persist_file_construct
                                                (BonoboPersistFile *ps,
                                                 BonoboPersistFileIOFn load_fn,
                                                 BonoboPersistFileIOFn save_fn,
                                                 void *closure);

## Object Hierarchy

      GtkObject
       +----BonoboObject
             +----BonoboXObject
                   +----BonoboPersist
                         +----BonoboPersistFile

## Description

The PersistFile interface is a useful interface for Bonoboizing legacy applications, however, for new / correct applications it is far preferable to implement the BonoboPersistStream interface, since this will not only result in a nice clean to your application architecture, but also allow the transparent use of local, remote, and synthetic streams.

This interface works by connecting callbacks to the methods, in a pretty deprecated fashion, it is probably better nowadays to simply sub-class the BonoboXObject and override the epv methods. Either way, after all the caveats here is an example use:

Example 1. Persist file implementation

    static gint
    load_from_file (BonoboPersistFile *pf,
                    const CORBA_char  *filename,
    		CORBA_Environment *ev,
                    void              *closure)
    {
    	EogImageData *image_data = closure;

    	g_warning ("Load from 's'", filename);

    	return 0; /* Return 0 on success */
    }

    static gint
    save_to_file (BonoboPersistFile *pf,
                  const CORBA_char  *filename,
                  CORBA_Environment *ev,
                  void              *closure)
    {
    	EogImageData *image_data = closure;

    	g_warning ("Save to 's'", filename);

    	return 0; /* Return 0 on success */
    }

Having implemented the callbacks we then have to register them and aggregate the interface to our object:

Example 2. Aggregating a new PersistFile

    EogImageData *
    eog_image_data_construct (EogImageData *image_data)
    {
    	BonoboObject        *retval;
    	BonoboPersistFile   *file;

    	file = bonobo_persist_file_new (
    		load_from_file, save_to_file, image_data);

    	if (file == NULL) {
    		bonobo_object_unref (BONOBO_OBJECT (image_data));
    		return NULL;
    	}

    	bonobo_object_add_interface (BONOBO_OBJECT (image_data),
    				     BONOBO_OBJECT (file));

    	return image_data;
    }

Note again, that you should be writing a BonoboPersistStream interface, however if you have already done this you might like to just have hooks through so that old apps can use the PersistFile interface:

Example 3. Chaining to a PersistStream implementation

    static gint
    load_from_file (BonoboPersistFile *pf,
                    const CORBA_char  *filename,
    		CORBA_Environment *ev,
                    void              *closure)
    {
    	Bonobo_PersistStream ps = closure;
    	BonoboStream *stream;

    	stream = bonobo_stream_open (
    		BONOBO_IO_DRIVER_FS,
    		filename, Bonobo_STORAGE_READ,
    		0);

    	if (!stream)
    		return 0;

    	.. extract content type from file ...

    	Bonobo_PersistStream_load (ps, type, ev);

    	return 0; /* Return 0 on success */
    }

    static gint
    save_to_file (BonoboPersistFile *pf,
                  const CORBA_char  *filename,
                  CORBA_Environment *ev,
                  void              *closure)
    {
    	Bonobo_PersistStream ps = closure;
    	BonoboStream *stream;

    	stream = bonobo_stream_open (
    		BONOBO_IO_DRIVER_FS,
    		filename, Bonobo_STORAGE_WRITE | Bonobo_STORAGE_CREATE,
    		S_IRUSR | S_IWUSR | S_IRGRP);

    	if (!stream)
    		return 0;

    	.. work out content type we want to save ...

    	Bonobo_PersistStream_save (ps, type, ev);

    	return 0; /* Return 0 on success */
    }

The mime type data can be extracted from gnome-vfs or gnome-mime.

## Details

### struct BonoboPersistFile

    struct BonoboPersistFile;

### BonoboPersistFileIOFn ()

    int         (*BonoboPersistFileIOFn)        (BonoboPersistFile *pf,
                                                 const CORBA_char *filename,
                                                 CORBA_Environment *ev,
                                                 void *closure);

`pf` : |
`filename` : |
`ev` : |
`closure` : |
Returns : |

### BonoboPersistFileClass

    typedef struct {
    	BonoboPersistClass parent_class;

    	POA_Bonobo_PersistFile__epv epv;

    	/* methods */
    	int   (*load)             (BonoboPersistFile *ps,
    				   const CORBA_char  *filename,
    				   CORBA_Environment *ev);

    	int   (*save)             (BonoboPersistFile *ps,
    				   const CORBA_char  *filename,
    				   CORBA_Environment *ev);

    	char *(*get_current_file) (BonoboPersistFile *ps,
    				   CORBA_Environment *ev);

    } BonoboPersistFileClass;

### bonobo_persist_file_set_dirty ()

    void        bonobo_persist_file_set_dirty   (BonoboPersistFile *ps,
                                                 gboolean dirty);

`ps` : |
`dirty` : |

### bonobo_persist_file_new ()

    BonoboPersistFile* bonobo_persist_file_new  (BonoboPersistFileIOFn load_fn,
                                                 BonoboPersistFileIOFn save_fn,
                                                 void *closure);

Creates a BonoboPersistFile object. The `load_fn` and `save_fn` parameters might be NULL. If this is the case, the load and save operations are performed by the class load and save methods

`load_fn` : | Loading routine
`save_fn` : | Saving routine
`closure` : | Data passed to IO routines.
Returns : |

### bonobo_persist_file_construct ()

    BonoboPersistFile* bonobo_persist_file_construct
                                                (BonoboPersistFile *ps,
                                                 BonoboPersistFileIOFn load_fn,
                                                 BonoboPersistFileIOFn save_fn,
                                                 void *closure);

Initializes the BonoboPersistFile object. The `load_fn` and `save_fn` parameters might be NULL. If this is the case, the load and save operations are performed by the class load and save methods

`ps` : |
`load_fn` : | Loading routine
`save_fn` : | Saving routine
`closure` : | Data passed to IO routines.
Returns : |

## See Also

BonoboPersistStream, BonoboPersist

<<< Previous Page | Home | Up | Next Page >>>
BonoboPersist | BonoboPersistStream