Added a bit of encodings stuff - just a band aid for now

This commit is contained in:
Yevgen Muntyan 2005-09-16 23:45:55 +00:00
parent 36e0a1c491
commit 1acf66ac30
2 changed files with 98 additions and 55 deletions

View File

@ -192,17 +192,46 @@ static gboolean do_load (MooEdit *edit,
const char *encoding, const char *encoding,
GError **error); GError **error);
static const char *common_encodings[] = {
"UTF8",
"ISO_8859-15",
"ISO_8859-1"
};
static gboolean moo_edit_load_default (G_GNUC_UNUSED MooEditLoader *loader,
MooEdit *edit, static gboolean
const char *file, try_load (MooEdit *edit,
const char *encoding, const char *file,
GError **error) const char *encoding,
GError **error)
{ {
GtkTextIter start, end; GtkTextIter start, end;
GtkTextBuffer *buffer; GtkTextBuffer *buffer;
MooEditFileInfo *info = NULL;
g_return_val_if_fail (MOO_IS_EDIT (edit), FALSE);
g_return_val_if_fail (file && file[0], FALSE);
g_return_val_if_fail (encoding && encoding[0], FALSE);
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (edit));
gtk_text_buffer_get_bounds (buffer, &start, &end);
gtk_text_buffer_delete (buffer, &start, &end);
return do_load (edit, file, encoding, error);
}
static gboolean
moo_edit_load_default (G_GNUC_UNUSED MooEditLoader *loader,
MooEdit *edit,
const char *file,
const char *encoding,
GError **error)
{
GtkTextIter start;
GtkTextBuffer *buffer;
gboolean undo; gboolean undo;
gboolean success = FALSE;
g_return_val_if_fail (MOO_IS_EDIT (edit), FALSE); g_return_val_if_fail (MOO_IS_EDIT (edit), FALSE);
g_return_val_if_fail (file && file[0], FALSE); g_return_val_if_fail (file && file[0], FALSE);
@ -221,59 +250,77 @@ static gboolean moo_edit_load_default (G_GNUC_UNUSED MooEditLoader *loader,
else else
gtk_source_buffer_begin_not_undoable_action (GTK_SOURCE_BUFFER (buffer)); gtk_source_buffer_begin_not_undoable_action (GTK_SOURCE_BUFFER (buffer));
gtk_text_buffer_get_bounds (buffer, &start, &end); if (!encoding)
gtk_text_buffer_delete (buffer, &start, &end);
if (!do_load (edit, file, encoding, error))
{ {
moo_edit_file_info_free (info); guint i;
edit->priv->status = 0;
stop_file_watch (edit);
unblock_buffer_signals (edit);
if (undo) for (i = 0; i < G_N_ELEMENTS (common_encodings); ++i)
gtk_text_buffer_end_user_action (buffer); {
else g_clear_error (error);
gtk_source_buffer_end_not_undoable_action (GTK_SOURCE_BUFFER (buffer)); encoding = common_encodings[i];
success = try_load (edit, file, encoding, error);
if (success)
break;
}
moo_edit_set_modified (edit, FALSE); if (!success)
return FALSE; {
const char *locale_charset;
if (!g_get_charset (&locale_charset))
{
g_clear_error (error);
encoding = locale_charset;
success = try_load (edit, file, encoding, error);
}
}
}
else
{
success = try_load (edit, file, encoding, error);
} }
gtk_text_buffer_get_start_iter (buffer, &start);
gtk_text_buffer_place_cursor (buffer, &start);
unblock_buffer_signals (edit); unblock_buffer_signals (edit);
edit->priv->status = 0;
_moo_edit_set_filename (edit, file, encoding);
moo_edit_set_modified (edit, FALSE);
start_file_watch (edit);
moo_edit_file_info_free (info); if (success)
{
gtk_text_buffer_get_start_iter (buffer, &start);
gtk_text_buffer_place_cursor (buffer, &start);
_moo_edit_set_filename (edit, file, encoding);
start_file_watch (edit);
}
else
{
stop_file_watch (edit);
}
if (undo) if (undo)
gtk_text_buffer_end_user_action (buffer); gtk_text_buffer_end_user_action (buffer);
else else
gtk_source_buffer_end_not_undoable_action (GTK_SOURCE_BUFFER (buffer)); gtk_source_buffer_end_not_undoable_action (GTK_SOURCE_BUFFER (buffer));
return TRUE; edit->priv->status = 0;
moo_edit_set_modified (edit, FALSE);
return success;
} }
static gboolean do_load (MooEdit *edit, static gboolean
const char *filename, do_load (MooEdit *edit,
const char *encoding, const char *filename,
GError **error) const char *encoding,
GError **error)
{ {
GIOChannel *file = NULL; GIOChannel *file = NULL;
GIOStatus status; GIOStatus status;
GtkTextBuffer *buffer; GtkTextBuffer *buffer;
g_return_val_if_fail (filename != NULL, FALSE); g_return_val_if_fail (filename != NULL, FALSE);
g_return_val_if_fail (encoding != NULL, FALSE);
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (edit)); buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (edit));
if (!encoding || !strcmp (encoding, "UTF-8") || !strcmp (encoding, "UTF8")) if (!strcmp (encoding, "UTF-8") || !strcmp (encoding, "UTF8"))
{ {
char *contents; char *contents;
gsize len; gsize len;
@ -294,8 +341,6 @@ static gboolean do_load (MooEdit *edit,
} }
gtk_text_buffer_insert_at_cursor (buffer, contents, len); gtk_text_buffer_insert_at_cursor (buffer, contents, len);
g_free (edit->priv->encoding);
edit->priv->encoding = NULL;
g_mapped_file_free (mapped_file); g_mapped_file_free (mapped_file);
return TRUE; return TRUE;
@ -313,9 +358,6 @@ static gboolean do_load (MooEdit *edit,
if (!file) if (!file)
return FALSE; return FALSE;
if (!encoding)
encoding = "UTF8";
if (g_io_channel_set_encoding (file, encoding, error) != G_IO_STATUS_NORMAL) if (g_io_channel_set_encoding (file, encoding, error) != G_IO_STATUS_NORMAL)
{ {
g_io_channel_shutdown (file, TRUE, NULL); g_io_channel_shutdown (file, TRUE, NULL);
@ -330,6 +372,14 @@ static gboolean do_load (MooEdit *edit,
status = g_io_channel_read_line (file, &line, &len, NULL, error); status = g_io_channel_read_line (file, &line, &len, NULL, error);
if (status != G_IO_STATUS_NORMAL && status != G_IO_STATUS_EOF)
{
g_io_channel_shutdown (file, TRUE, NULL);
g_io_channel_unref (file);
g_free (line);
return FALSE;
}
if (line) if (line)
{ {
if (!g_utf8_validate (line, len, NULL)) if (!g_utf8_validate (line, len, NULL))
@ -347,26 +397,14 @@ static gboolean do_load (MooEdit *edit,
g_free (line); g_free (line);
} }
/* XXX */ if (status == G_IO_STATUS_EOF)
if (status != G_IO_STATUS_NORMAL)
{ {
if (status != G_IO_STATUS_EOF) g_io_channel_shutdown (file, TRUE, NULL);
{ g_io_channel_unref (file);
g_io_channel_shutdown (file, TRUE, NULL); break;
g_io_channel_unref (file);
return FALSE;
}
else
{
g_io_channel_shutdown (file, TRUE, NULL);
g_io_channel_unref (file);
break;
}
} }
} }
g_free (edit->priv->encoding);
edit->priv->encoding = g_strdup (encoding);
g_clear_error (error); g_clear_error (error);
return TRUE; return TRUE;
} }
@ -432,6 +470,9 @@ static gboolean moo_edit_save_default (G_GNUC_UNUSED MooEditSaver *saver,
g_return_val_if_fail (MOO_IS_EDIT (edit), FALSE); g_return_val_if_fail (MOO_IS_EDIT (edit), FALSE);
g_return_val_if_fail (filename && filename[0], FALSE); g_return_val_if_fail (filename && filename[0], FALSE);
if (!encoding)
encoding = "UTF8";
if (!do_write (edit, filename, encoding, error)) if (!do_write (edit, filename, encoding, error))
return FALSE; return FALSE;

View File

@ -754,6 +754,8 @@ void moo_editor_open (MooEditor *editor,
if (!moo_edit_load (loader, doc, info->filename, info->encoding, &error)) if (!moo_edit_load (loader, doc, info->filename, info->encoding, &error))
{ {
moo_edit_open_error_dialog (parent, error ? error->message : NULL); moo_edit_open_error_dialog (parent, error ? error->message : NULL);
if (error)
g_error_free (error);
} }
else else
{ {