Commit ca92de93 authored by Miguel de Icaza's avatar Miguel de Icaza Committed by Arturo Espinosa

Now we have support for registering/unregistering file opening and saving



Now we have support for registering/unregistering file opening
and saving formats.

Miguel.

1998-12-28  Miguel de Icaza  <miguel@nuclecu.unam.mx>

	* plugins/excel/boot.c: New file, this holds the probing and load
	wrapper code for Gnumeric to use as well as the initial format
	registering code.

	* src/xml-io.c (xml_probe): New routine, used to probe if a file
	is a Gnumeric-xml file.

	* src/file.c (file_format_register_open,
	file_format_unregister_open, file_format_register_save,
	file_format_unregister_save): New routines used to register and
	unregister file formats in Gnumeric.

	* src/main.c (main): Boot xml and excel formats, since they now
	need to register themselves with the file.c code
parent 6d642361
1998-12-28 Miguel de Icaza <miguel@nuclecu.unam.mx>
* plugins/excel/boot.c: New file, this holds the probing and load
wrapper code for Gnumeric to use as well as the initial format
registering code.
* src/xml-io.c (xml_probe): New routine, used to probe if a file
is a Gnumeric-xml file.
* src/file.c (file_format_register_open,
file_format_unregister_open, file_format_register_save,
file_format_unregister_save): New routines used to register and
unregister file formats in Gnumeric.
* src/main.c (main): Boot xml and excel formats, since they now
need to register themselves with the file.c code
* src/cell.h: cell->entered_text is back, now only used for typo
correction. Maybe I will use it for saving.
......
1998-12-28 Miguel de Icaza <miguel@nuclecu.unam.mx>
* plugins/excel/boot.c: New file, this holds the probing and load
wrapper code for Gnumeric to use as well as the initial format
registering code.
* src/xml-io.c (xml_probe): New routine, used to probe if a file
is a Gnumeric-xml file.
* src/file.c (file_format_register_open,
file_format_unregister_open, file_format_register_save,
file_format_unregister_save): New routines used to register and
unregister file formats in Gnumeric.
* src/main.c (main): Boot xml and excel formats, since they now
need to register themselves with the file.c code
* src/cell.h: cell->entered_text is back, now only used for typo
correction. Maybe I will use it for saving.
......
1998-12-28 Miguel de Icaza <miguel@nuclecu.unam.mx>
* plugins/excel/boot.c: New file, this holds the probing and load
wrapper code for Gnumeric to use as well as the initial format
registering code.
* src/xml-io.c (xml_probe): New routine, used to probe if a file
is a Gnumeric-xml file.
* src/file.c (file_format_register_open,
file_format_unregister_open, file_format_register_save,
file_format_unregister_save): New routines used to register and
unregister file formats in Gnumeric.
* src/main.c (main): Boot xml and excel formats, since they now
need to register themselves with the file.c code
* src/cell.h: cell->entered_text is back, now only used for typo
correction. Maybe I will use it for saving.
......
1998-12-28 Miguel de Icaza <miguel@nuclecu.unam.mx>
* plugins/excel/boot.c: New file, this holds the probing and load
wrapper code for Gnumeric to use as well as the initial format
registering code.
* src/xml-io.c (xml_probe): New routine, used to probe if a file
is a Gnumeric-xml file.
* src/file.c (file_format_register_open,
file_format_unregister_open, file_format_register_save,
file_format_unregister_save): New routines used to register and
unregister file formats in Gnumeric.
* src/main.c (main): Boot xml and excel formats, since they now
need to register themselves with the file.c code
* src/cell.h: cell->entered_text is back, now only used for typo
correction. Maybe I will use it for saving.
......
1998-12-28 Miguel de Icaza <miguel@nuclecu.unam.mx>
* boot.c (excel_init): New file, adds the excel booting
mechanism.
......@@ -8,6 +8,8 @@ INCLUDES = \
noinst_LIBRARIES = libexcel.a
libexcel_a_SOURCES = \
boot.c \
boot.h \
ms-excel-biff.h \
ms-biff.h \
ms-excel.c \
......
......@@ -41,6 +41,7 @@ GNUMERIC_BASE_SOURCES = \
expr.c \
expr.h \
file.c \
file.h \
formats.h \
formats.c \
func.c \
......
......@@ -124,6 +124,7 @@ void cell_relocate (Cell *cell,
int target_col, int target_row);
void cell_get_span (Cell *cell, int *col1, int *col2);
char *cell_get_text (Cell *cell);
char *cell_get_content (Cell *cell);
char *cell_get_value_as_text (Cell *cell);
void cell_make_value (Cell *cell);
void cell_render_value (Cell *cell);
......
......@@ -9,6 +9,132 @@
#include "gnumeric.h"
#include "dialogs.h"
#include "xml-io.h"
#include "file.h"
GList *gnumeric_file_savers = NULL;
GList *gnumeric_file_openers = NULL;
static gint
file_priority_sort (gconstpointer a, gconstpointer b)
{
FileOpener *fa = (FileOpener *)a;
FileOpener *fb = (FileOpener *)b;
return fb->priority - fa->priority;
}
/**
* file_format_register_open:
* @priority: The priority at which this file open handler is registered
* @desc: a description of this file format
* @probe_fn: A routine that would probe if the file is of a given type.
* @open_fn: A routine that would load the code
*
* The priority is used to give it a higher precendence to a format.
* The higher the priority, the sooner it will be tried, gnumeric registers
* its XML-based format at priority 50.
*/
void
file_format_register_open (int priority, char *desc, FileFormatProbe probe_fn, FileFormatOpen open_fn)
{
FileOpener *fo = g_new (FileOpener, 1);
g_return_if_fail (probe_fn != NULL);
g_return_if_fail (open_fn != NULL);
fo->priority = priority;
fo->format_description = desc;
fo->probe = probe_fn;
fo->open = open_fn;
gnumeric_file_openers = g_list_insert_sorted (gnumeric_file_openers, fo, file_priority_sort);
}
/**
* file_format_unregister_open:
* @probe: The routine that was used to probe
* @open: The routine that was used to open
*
* This function is used to remove a registered format opener from gnumeric
*/
void
file_format_unregister_open (FileFormatProbe probe, FileFormatOpen open)
{
GList *l;
for (l = gnumeric_file_openers; l; l = l->next){
FileOpener *fo = l->data;
if (fo->probe == probe && fo->open == open){
gnumeric_file_openers = g_list_remove_link (gnumeric_file_openers, l);
return;
}
}
}
/**
* file_format_register_save:
* @extension: An extension that is usually used by this format
* @format_description: A description of this format
* @save_fn: A function that should be used to save
*
* This routine registers a file format save routine with Gnumeric
*/
void
file_format_register_save (char *extension, char *format_description, FileFormatSave save_fn)
{
FileSaver *fs = g_new (FileSaver, 1);
g_return_if_fail (save_fn != NULL);
fs->extension = extension;
fs->format_description = format_description;
fs->save = save_fn;
gnumeric_file_savers = g_list_prepend (gnumeric_file_savers, fs);
}
/**
* file_format_unregister_save:
* @probe: The routine that was used to save
*
* This function is used to remove a registered format saver from gnumeric
*/
void
file_format_unregister_save (FileFormatSave save)
{
GList *l;
for (l = gnumeric_file_savers; l; l = l->next){
FileSaver *fs = l->data;
if (fs->save == save){
gnumeric_file_savers = g_list_remove_link (gnumeric_file_savers, l);
return;
}
}
}
Workbook *
workbook_read (const char *filename)
{
GList *l;
g_return_val_if_fail (filename != NULL, NULL);
for (l = gnumeric_file_openers; l; l = l->next){
FileOpener *fo = l->data;
Workbook *w;
if ((*fo->probe) (filename)){
w = (*fo->open) (filename);
if (w)
return w;
}
}
return NULL;
}
static void
set_ok (GtkWidget *widget, gboolean *dialog_result)
......
#ifndef GNUMERIC_FILE_H
#define GNUMERIC_FILE_H
typedef gboolean (*FileFormatProbe)(const char *filename);
typedef Workbook *(*FileFormatOpen) (const char *filename);
typedef int (*FileFormatSave) (Workbook *wb, const char *filename);
typedef struct {
int priority;
char *format_description;
FileFormatProbe probe;
FileFormatOpen open;
} FileOpener;
/* A GList of FileOpener structures */
extern GList *gnumeric_file_openers;
typedef struct {
char *extension;
char *format_description;
FileFormatSave save;
} FileSaver;
/* A GList of FileSaver structures */
extern GList *gnumeric_file_savers;
void file_format_register_open (int priority,
char *format_description,
FileFormatProbe probe_fn,
FileFormatOpen open_fn);
void file_format_unregister_open (FileFormatProbe probe, FileFormatOpen open);
void file_format_register_save (char *extension,
char *format_description,
FileFormatSave save_fn);
void file_format_unregister_save (FileFormatSave save);
#endif /* GNUMERIC_FILE_H */
......@@ -9,6 +9,8 @@
#include "dump.h"
#include "main.h"
#include "../plugins/excel/boot.h"
#ifdef HAVE_GUILE
#include <libguile.h>
#endif
......@@ -44,6 +46,10 @@ gnumeric_main (void *closure, int argc, char *argv [])
functions_init ();
plugins_init ();
/* The statically linked in file formats */
xml_init ();
excel_init ();
if (dump_file_name){
dump_functions (dump_file_name);
exit (1);
......
......@@ -9,6 +9,8 @@
#include "dump.h"
#include "main.h"
#include "../plugins/excel/boot.h"
#ifdef HAVE_GUILE
#include <libguile.h>
#endif
......@@ -44,6 +46,10 @@ gnumeric_main (void *closure, int argc, char *argv [])
functions_init ();
plugins_init ();
/* The statically linked in file formats */
xml_init ();
excel_init ();
if (dump_file_name){
dump_functions (dump_file_name);
exit (1);
......
......@@ -335,7 +335,7 @@ void sheet_fill_selection_with (Sheet *sheet, char *text);
void sheet_show_cursor (Sheet *sheet);
void sheet_hide_cursor (Sheet *sheet);
char *cellref_name (CellRef *cell_ref,
char *cellref_name (CellRef *cell_ref,
Sheet *eval_sheet,
int eval_col,
int eval_row);
......
......@@ -13,13 +13,11 @@
#include "gnumeric-sheet.h"
#include "sheet-object.h"
#include "dialogs.h"
#include "file.h"
#include "xml-io.h"
#include "plugin.h"
#include "pixmaps.h"
#include "../plugins/excel/ms-ole.h"
#include "../plugins/excel/ms-excel.h"
/* The locations within the main table in the workbook */
#define WB_EA_LINE 0
#define WB_EA_SHEETS 1
......@@ -32,25 +30,6 @@ static int workbook_count;
static GList *workbook_list = NULL;
/**
* Wrapper that decides which format to use
**/
Workbook *
workbook_read (const char *filename)
{
/* A slow and possibly buggy check for now. */
MS_OLE *f = ms_ole_new (filename) ;
Workbook *wb;
if (f)
{
wb = ms_excelReadWorkbook(f) ;
free (f) ;
}
else
wb = gnumericReadXmlWorkbook (filename);
return wb ;
}
static void
new_cmd (void)
{
......@@ -66,7 +45,7 @@ open_cmd (GtkWidget *widget, Workbook *wb)
Workbook *new_wb;
if ((new_wb = workbook_read (fname)))
gtk_widget_show (new_wb->toplevel);
gtk_widget_show (new_wb->toplevel);
}
static void
......@@ -651,16 +630,16 @@ about_cmd (GtkWidget *widget, Workbook *wb)
}
static void
filenames_dropped(GtkWidget * widget,
GdkDragContext *context,
gint x,
gint y,
GtkSelectionData *selection_data,
guint info,
guint time)
filenames_dropped (GtkWidget * widget,
GdkDragContext *context,
gint x,
gint y,
GtkSelectionData *selection_data,
guint info,
guint time)
{
GList *names, *tmp_list;
names = gnome_uri_list_extract_filenames ((char *)selection_data->data);
tmp_list = names;
......
......@@ -15,6 +15,7 @@
#include "color.h"
#include "sheet-object.h"
#include "xml-io.h"
#include "file.h"
/*
* A parsing context.
......@@ -1572,3 +1573,38 @@ readXmlWorkbook (parseXmlContextPtr ctxt, xmlNodePtr tree)
return ret;
}
/*
* We parse and do some limited validation of the XML file, if this
* passes, then we return TRUE
*/
static gboolean
xml_probe (const char *filename)
{
xmlDocPtr res;
xmlNsPtr gmr;
res = xmlParseFile (filename);
if (res == NULL)
return FALSE;
if (res->root == NULL)
return FALSE;
gmr = xmlSearchNsByHref (res, res->root, "http://www.gnome.org/gnumeric/");
if (strcmp (res->root->name, "Workbook") || (gmr == NULL)){
xmlFreeDoc (res);
return FALSE;
}
xmlFreeDoc (res);
return TRUE;
}
void
xml_init (void)
{
char *desc = _("Gnumeric XML file format");
file_format_register_open (50, desc, xml_probe, gnumericReadXmlWorkbook);
file_format_register_save (".gnumeric", desc, gnumericWriteXmlWorkbook);
}
......@@ -14,4 +14,6 @@ int gnumericWriteXmlSheet (Sheet *sheet, const char *filename);
Workbook *gnumericReadXmlWorkbook (const char *filename);
int gnumericWriteXmlWorkbook (Workbook *sheet, const char *filename);
void xml_init (void);
#endif /* GNUMERIC_XML_IO_H */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment