Commit 2b5db3dc authored by Morten Welinder's avatar Morten Welinder Committed by Morten Welinder

Make filename handling UTF-8 safe. Ditto. (wb_view_save): Ditto.

2003-06-10  Morten Welinder  <terra@gnome.org>

	* src/stf.c (stf_read_workbook): Make filename handling UTF-8
	safe.
	* src/workbook-view.c (wb_view_attach_control): Ditto.
	(wb_view_save): Ditto.
	(workbook_view_new): Ditto.
	(wbv_save_to_file): Ditto.
	* src/workbook.c (workbook_set_filename): Ditto.
	* src/print-info.c (render_file): Ditto.
	* src/gui-file.c (can_try_save_to): Ditto.

	* src/workbook.c (workbook_get_filename_utf8): New function.

	* src/gutils.c (gnumeric_valid_filename): Delete.  No longer useful.
parent 55977989
2003-06-10 Morten Welinder <terra@gnome.org>
* src/stf.c (stf_read_workbook): Make filename handling UTF-8
safe.
* src/workbook-view.c (wb_view_attach_control): Ditto.
(wb_view_save): Ditto.
(workbook_view_new): Ditto.
(wbv_save_to_file): Ditto.
* src/workbook.c (workbook_set_filename): Ditto.
* src/print-info.c (render_file): Ditto.
* src/gui-file.c (can_try_save_to): Ditto.
* src/workbook.c (workbook_get_filename_utf8): New function.
* src/gutils.c (gnumeric_valid_filename): Delete. No longer useful.
2003-06-10 Abel Cheung <maddog@linux.org.hk>
* configure.in: Removed en_CA and en_AU from ALL_LINGUAS. They are
......
......@@ -12,6 +12,7 @@ Morten:
* Hopefully fix NAN issues on Alphas.
* Plug autoformat leak.
* Import all gal widgets we use.
* Fix lots of UTF-8/filename problems.
--------------------------------------------------------------------------
......
2003-06-10 Morten Welinder <terra@gnome.org>
* src/stf.c (stf_read_workbook): Make filename handling UTF-8
safe.
* src/workbook-view.c (wb_view_attach_control): Ditto.
(wb_view_save): Ditto.
(workbook_view_new): Ditto.
(wbv_save_to_file): Ditto.
* src/workbook.c (workbook_set_filename): Ditto.
* src/print-info.c (render_file): Ditto.
* src/gui-file.c (can_try_save_to): Ditto.
* src/workbook.c (workbook_get_filename_utf8): New function.
* src/gutils.c (gnumeric_valid_filename): Delete. No longer useful.
2003-06-10 Abel Cheung <maddog@linux.org.hk>
* configure.in: Removed en_CA and en_AU from ALL_LINGUAS. They are
......
2003-06-10 Morten Welinder <terra@gnome.org>
* src/stf.c (stf_read_workbook): Make filename handling UTF-8
safe.
* src/workbook-view.c (wb_view_attach_control): Ditto.
(wb_view_save): Ditto.
(workbook_view_new): Ditto.
(wbv_save_to_file): Ditto.
* src/workbook.c (workbook_set_filename): Ditto.
* src/print-info.c (render_file): Ditto.
* src/gui-file.c (can_try_save_to): Ditto.
* src/workbook.c (workbook_get_filename_utf8): New function.
* src/gutils.c (gnumeric_valid_filename): Delete. No longer useful.
2003-06-10 Abel Cheung <maddog@linux.org.hk>
* configure.in: Removed en_CA and en_AU from ALL_LINGUAS. They are
......
......@@ -72,6 +72,7 @@ applix_write_header (ApplixWriteState const *state)
gsf_output_printf (state->sink, "Num ExtLinks: 0\n");
gsf_output_printf (state->sink,
"Spreadsheet Dump Rev 4.42 Line Length 80\n");
#warning "FIXME: filename is fs encoded; that's not right, but neither is UTF-8."
gsf_output_printf (state->sink,
"Current Doc Real Name: %s",
workbook_get_filename (state->wb));
......
2003-06-10 Morten Welinder <terra@gnome.org>
* functions.c (gnumeric_cell): UTF-8 fix.
2003-06-07 Jody Goldberg <jody@gnome.org>
* Release 1.1.19
......
......@@ -239,12 +239,12 @@ gnumeric_cell (FunctionEvalInfo *ei, Value **argv)
* the worksheet name, but they can't make any other changes to CELL?!
*/
} else if (!g_ascii_strcasecmp (info_type, "filename")) {
char const *name = workbook_get_filename (ei->pos->sheet->workbook);
char const *name = workbook_get_filename_utf8 (ei->pos->sheet->workbook, FALSE);
if (name == NULL)
return value_new_string ("");
else
return value_new_string (name);
return value_new_string_nocopy (name);
/* from CELL */
/* Backwards compatibility w/123 - unnecessary */
......
......@@ -222,37 +222,45 @@ gui_file_open (WorkbookControlGUI *wbcg)
*
* FIXME: The message boxes should really be children of the file selector,
* not the workbook.
*
* Note: filename is filesys, not UTF-8 encoded.
*/
static gboolean
can_try_save_to (WorkbookControlGUI *wbcg, char const *name)
{
gboolean result = TRUE;
gchar *msg;
char *filename_utf8 = name
? g_filename_to_utf8 (name, -1, NULL, NULL, NULL)
: NULL;
if (name == NULL || name[0] == '\0') {
result = FALSE;
} else if (filename_utf8 == NULL) {
result = FALSE;
} else if (name [strlen (name) - 1] == '/' ||
g_file_test (name, G_FILE_TEST_IS_DIR)) {
msg = g_strdup_printf (_("%s\nis a directory name"), name);
msg = g_strdup_printf (_("%s\nis a directory name"), filename_utf8);
gnumeric_notice (wbcg, GTK_MESSAGE_ERROR, msg);
g_free (msg);
result = FALSE;
} else if (access (name, W_OK) != 0 && errno != ENOENT) {
msg = g_strdup_printf (
_("You do not have permission to save to\n%s"),
name);
filename_utf8);
gnumeric_notice (wbcg, GTK_MESSAGE_ERROR, msg);
g_free (msg);
result = FALSE;
} else if (g_file_test (name, G_FILE_TEST_EXISTS)) {
msg = g_strdup_printf (
_("Workbook %s already exists.\n"
"Do you want to save over it?"), name);
"Do you want to save over it?"), filename_utf8);
result = gnumeric_dialog_question_yes_no (
wbcg, msg, gnm_app_prefs->file_overwrite_default_answer);
g_free (msg);
}
g_free (filename_utf8);
return result;
}
......@@ -281,6 +289,9 @@ check_multiple_sheet_support_if_needed (GnmFileSaver *fs,
return (ret_val);
}
/*
* Note: filename is filesys, not UTF-8 encoded.
*/
static gboolean
do_save_as (WorkbookControlGUI *wbcg, WorkbookView *wb_view,
GnmFileSaver *fs, char const *name)
......@@ -288,7 +299,7 @@ do_save_as (WorkbookControlGUI *wbcg, WorkbookView *wb_view,
char *filename = NULL;
gboolean success = FALSE;
if (*name == 0 || name [strlen (name) - 1] == '/') {
if (*name == 0 || name[strlen (name) - 1] == '/') {
gnumeric_notice (wbcg, GTK_MESSAGE_ERROR,
_("Please enter a file name,\nnot a directory"));
return FALSE;
......
......@@ -593,25 +593,6 @@ gnumeric_utf8_strcapital (const char *p, ssize_t len)
/* ------------------------------------------------------------------------- */
gboolean
gnumeric_valid_filename (const char *filename)
{
GError *err = NULL;
char *utf8name;
gboolean res;
g_return_val_if_fail (filename != NULL, FALSE);
utf8name = g_filename_to_utf8 (filename, -1, NULL, NULL, &err);
res = utf8name && !err;
g_free (utf8name);
g_clear_error (&err);
return res;
}
/* ------------------------------------------------------------------------- */
#undef DEBUG_CHUNK_ALLOCATOR
typedef struct _gnm_mem_chunk_freeblock gnm_mem_chunk_freeblock;
......
......@@ -84,8 +84,6 @@ const char *gnm_strunescape (GString *target, const char *string);
char * gnumeric_utf8_strcapital (const char *p, ssize_t len);
gboolean gnumeric_valid_filename (const char *filename);
gnm_mem_chunk *gnm_mem_chunk_new (const char *, size_t, size_t);
void gnm_mem_chunk_destroy (gnm_mem_chunk *, gboolean);
gpointer gnm_mem_chunk_alloc (gnm_mem_chunk *);
......
......@@ -497,9 +497,11 @@ render_time (GString *target, HFRenderInfo *info, char const *args)
static void
render_file (GString *target, HFRenderInfo *info, char const *args)
{
if (info->sheet != NULL && info->sheet->workbook != NULL)
g_string_append (target,
workbook_get_filename (info->sheet->workbook));
if (info->sheet != NULL && info->sheet->workbook != NULL) {
char *filename_utf8 = workbook_get_filename_utf8 (info->sheet->workbook, FALSE);
g_string_append (target, filename_utf8);
g_free (filename_utf8);
}
}
static struct {
......
......@@ -158,27 +158,35 @@ stf_read_workbook (GnmFileOpener const *fo, gchar const *enc,
IOContext *context, WorkbookView *wbv, GsfInput *input)
{
DialogStfResult_t *dialogresult = NULL;
char *name;
char *name, *nameutf8;
char *data;
Sheet *sheet;
Workbook *book;
data = stf_preparse (COMMAND_CONTEXT (context), input, enc);
if (!data)
return;
/* FIXME : how to do this cleanly ? */
if (!IS_WORKBOOK_CONTROL_GUI (context->impl))
return;
/* Add Sheet */
name = g_path_get_basename (gsf_input_name (input));
nameutf8 = g_filename_to_utf8 (name, -1, NULL, NULL, NULL);
g_free (name);
if (!nameutf8) {
g_warning ("Failed to convert filename to UTF-8. This shouldn't happen here.");
return;
}
data = stf_preparse (COMMAND_CONTEXT (context), input, enc);
if (!data) {
g_free (nameutf8);
return;
}
/* Add Sheet */
book = wb_view_workbook (wbv);
sheet = sheet_new (book, name);
sheet = sheet_new (book, nameutf8);
workbook_sheet_attach (book, sheet, NULL);
dialogresult = stf_dialog (WORKBOOK_CONTROL_GUI (context->impl), enc, name, data);
g_free (name);
dialogresult = stf_dialog (WORKBOOK_CONTROL_GUI (context->impl), enc, nameutf8, data);
if (dialogresult != NULL && stf_store_results (dialogresult, sheet, 0, 0)) {
workbook_recalc (book);
sheet_queue_respan (sheet, 0, SHEET_MAX_ROWS-1);
......@@ -194,6 +202,7 @@ stf_read_workbook (GnmFileOpener const *fo, gchar const *enc,
}
g_free (data);
g_free (nameutf8);
if (dialogresult != NULL)
stf_dialog_result_free (dialogresult);
}
......
......@@ -1956,6 +1956,7 @@ cb_file_send (GtkWidget *widget, WorkbookControlGUI *wbcg)
attachment_data, &ev);
CORBA_free (info);
#else
#error "FIXME filename is not UTF-8."
filename = workbook_get_filename (wb_view_workbook (wb_control_view (
WORKBOOK_CONTROL (wbcg))));
GNOME_Evolution_Composer_attachData (composer, "application/x-gnumeric",
......
......@@ -411,9 +411,13 @@ wb_view_attach_control (WorkbookView *wbv, WorkbookControl *wbc)
if (wbv->wb != NULL) {
/* Set the title of the newly connected control */
char *base_name = g_path_get_basename (workbook_get_filename (wbv->wb));
wb_control_title_set (wbc, base_name);
g_free (base_name);
char *base_name_utf8 = workbook_get_filename_utf8 (wbv->wb, TRUE);
if (base_name_utf8) {
wb_control_title_set (wbc, base_name_utf8);
g_free (base_name_utf8);
} else {
g_warning ("Failed to convert filename to UTF-8. This shouldn't happen here.");
}
}
}
......@@ -489,7 +493,7 @@ WorkbookView *
workbook_view_new (Workbook *wb)
{
WorkbookView *wbv = g_object_new (WORKBOOK_VIEW_TYPE, NULL);
char *base_name;
char *base_name_utf8;
int i;
if (wb == NULL)
......@@ -521,38 +525,44 @@ workbook_view_new (Workbook *wb)
wb_view_sheet_add (wbv, workbook_sheet_by_index (wb, i));
/* Set the titles of the newly connected view's controls */
base_name = g_path_get_basename (workbook_get_filename (wbv->wb));
base_name_utf8 = workbook_get_filename_utf8 (wbv->wb, TRUE);
WORKBOOK_VIEW_FOREACH_CONTROL (wbv, wbc,
wb_control_title_set (wbc, base_name););
g_free (base_name);
wb_control_title_set (wbc, base_name_utf8););
g_free (base_name_utf8);
return wbv;
}
/*
* Filename is fs encoded, not UTF-8.
*/
static void
wbv_save_to_file (WorkbookView *wbv, GnmFileSaver const *fs,
gchar const *file_name, IOContext *io_context)
gchar const *filename, IOContext *io_context)
{
char *msg = NULL;
char *filename_utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
if (gnumeric_valid_filename (file_name)) {
if (filename_utf8) {
GError *err = NULL;
GsfOutputStdio *output;
output = gsf_output_stdio_new (file_name, &err);
output = gsf_output_stdio_new (filename, &err);
if (output == NULL) {
char *str = g_strdup_printf (_("Can't open '%s' : %s"),
file_name, err->message);
char *str = g_strdup_printf (_("Can't open '%s' for writing: %s"),
filename_utf8, err->message);
gnumeric_error_save (COMMAND_CONTEXT (io_context), str);
g_error_free (err);
g_free (str);
g_free (filename_utf8);
return;
}
puts (file_name);
puts (filename);
if (output != NULL) {
gnm_file_saver_save (fs, io_context, wbv, GSF_OUTPUT (output));
g_object_unref (G_OBJECT (output));
g_free (filename_utf8);
return;
}
......@@ -564,23 +574,24 @@ wbv_save_to_file (WorkbookView *wbv, GnmFileSaver const *fs,
if (msg == NULL)
msg = g_strdup_printf (_("An unexplained error happened while saving %s"),
file_name);
filename_utf8);
} else {
/*
* This should be quite rare. To provoke, use
* gnumeric `echo -e '\377\376'`
*/
msg = g_strdup (_("The filename given is not valid."));
msg = g_strdup (_("The filename given is not valid in the current encoding."));
}
gnumeric_error_save (COMMAND_CONTEXT (io_context), msg);
g_free (msg);
g_free (filename_utf8);
}
/**
* wb_view_save_as:
* @wbv : Workbook View
* @fs : GnmFileSaver object
* @file_name : File name
* @file_name : File name (fs encoded, not UTF-8)
* @context :
*
* Saves @wbv and workbook it's attached to into @file_name file using
......@@ -655,9 +666,10 @@ wb_view_save (WorkbookView *wbv, CommandContext *context)
if (fs == NULL)
gnumeric_error_save (COMMAND_CONTEXT (io_context),
_("Default file saver is not available."));
else
wbv_save_to_file (wbv, fs, workbook_get_filename (wb),
io_context);
else {
const char *filename = workbook_get_filename (wb);
wbv_save_to_file (wbv, fs, filename, io_context);
}
has_error = gnumeric_io_error_occurred (io_context);
has_warning = gnumeric_io_warning_occurred (io_context);
......@@ -753,45 +765,47 @@ wb_view_new_from_input (GsfInput *input,
/**
* wb_view_new_from_file :
* @file_name : File name
* @filename : File name
* @optional_fmt : Optional GnmFileOpener
* @io_context : Optional context to display errors.
* @optional_enc : Optional encoding for GnmFileOpener that understand it
*
* Reads @file_name file using given file opener @optional_fmt, or probes for a valid
* Reads @filename file using given file opener @optional_fmt, or probes for a valid
* possibility if @optional_fmt is NULL. Reports problems to @io_context.
*
* Return value: TRUE if file was successfully read and FALSE otherwise.
*/
WorkbookView *
wb_view_new_from_file (char const *file_name,
wb_view_new_from_file (char const *filename,
GnmFileOpener const *optional_fmt,
IOContext *io_context,
gchar const *optional_enc)
{
char *msg = NULL;
char *filename_utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
if (gnumeric_valid_filename (file_name)) {
if (filename_utf8) {
GError *err = NULL;
GsfInputMemory *in_mem;
GsfInputStdio *in_stdio;
GsfInput *input = NULL;
/* Only report error if stdio fails too */
in_mem = gsf_input_mmap_new (file_name, NULL);
in_mem = gsf_input_mmap_new (filename, NULL);
if (in_mem == NULL) {
in_stdio = gsf_input_stdio_new (file_name, &err);
in_stdio = gsf_input_stdio_new (filename, &err);
if (in_stdio != NULL)
input = GSF_INPUT (in_stdio);
} else
input = GSF_INPUT (in_mem);
puts (file_name);
puts (filename);
if (input != NULL) {
WorkbookView *res = wb_view_new_from_input (input,
optional_fmt, io_context,
optional_enc);
g_object_unref (G_OBJECT (input));
g_free (filename_utf8);
return res;
}
......@@ -803,16 +817,17 @@ wb_view_new_from_file (char const *file_name,
if (msg == NULL)
msg = g_strdup_printf (_("An unexplained error happened while opening %s"),
file_name);
filename_utf8);
} else {
/*
* This should be quite rare. To provoke, use
* gnumeric `echo -e '\377\376'`
*/
msg = g_strdup (_("The filename given is not valid."));
msg = g_strdup (_("The filename given is not valid in the current encoding."));
}
gnumeric_error_read (COMMAND_CONTEXT (io_context), msg);
g_free (msg);
g_free (filename_utf8);
return NULL;
}
......@@ -395,9 +395,19 @@ workbook_new (void)
/* Assign a default name */
do {
char *name = g_strdup_printf (_("Book%d.%s"), ++count, extension);
char *name, *nameutf8;
count++;
nameutf8 = g_strdup_printf (_("Book%d.%s"), count, extension);
name = g_filename_from_utf8 (nameutf8, -1, NULL, NULL, NULL);
if (!name) {
name = g_strdup_printf ("Book%d.%s", count, extension);
}
is_unique = workbook_set_filename (wb, name);
g_free (name);
g_free (nameutf8);
} while (!is_unique);
return wb;
}
......@@ -477,7 +487,8 @@ workbook_new_with_sheets (int sheet_count)
gboolean
workbook_set_filename (Workbook *wb, char const *name)
{
char *base_name;
char *base_name, *base_name_utf8;
g_return_val_if_fail (wb != NULL, FALSE);
g_return_val_if_fail (name != NULL, FALSE);
......@@ -486,9 +497,11 @@ workbook_set_filename (Workbook *wb, char const *name)
wb->filename = g_strdup (name);
base_name = g_path_get_basename (name);
base_name_utf8 = g_filename_to_utf8 (base_name, -1, NULL, NULL, NULL);
WORKBOOK_FOREACH_CONTROL (wb, view, control,
wb_control_title_set (control, base_name););
wb_control_title_set (control, base_name_utf8););
g_free (base_name_utf8);
g_free (base_name);
g_signal_emit (G_OBJECT (wb), signals [FILENAME_CHANGED], 0);
......@@ -503,6 +516,20 @@ workbook_get_filename (Workbook *wb)
return wb->filename;
}
char *
workbook_get_filename_utf8 (Workbook *wb, gboolean basename)
{
const char *filename = workbook_get_filename (wb);
if (filename == NULL)
return NULL;
if (basename)
filename = g_path_get_basename (filename);
return g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
}
void
workbook_add_summary_info (Workbook *wb, SummaryItem *sit)
{
......
......@@ -44,8 +44,10 @@ gboolean workbook_sheet_reorganize (Workbook *wb,
CommandContext *cc);
/* IO Routines */
gboolean workbook_set_filename (Workbook *wb, char const *);
char const *workbook_get_filename (Workbook *wb);
gboolean workbook_set_filename (Workbook *wb, char const *); /* Filesys name, not UTF-8. */
char const *workbook_get_filename (Workbook *wb); /* Filesys name, not UTF-8. */
char *workbook_get_filename_utf8 (Workbook *wb, gboolean basename);
gboolean workbook_set_saveinfo (Workbook *wb,
FileFormatLevel, GnmFileSaver *);
GnmFileSaver *workbook_get_file_saver (Workbook *wb);
......
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