Use encodings on save too, show nicer error messages

master
Yevgen Muntyan 2006-12-17 10:58:17 -06:00
parent 1c3bfbfa93
commit 4941eb05ea
9 changed files with 316 additions and 324 deletions

View File

@ -16,6 +16,7 @@
#include "mooedit/mooedit-private.h" #include "mooedit/mooedit-private.h"
#include "mooedit/mooeditprefs.h" #include "mooedit/mooeditprefs.h"
#include "mooedit/mootextfind-glade.h" #include "mooedit/mootextfind-glade.h"
#include "mooedit/mooeditfileops.h"
#include "mooedit/mooeditsavemultiple-glade.h" #include "mooedit/mooeditsavemultiple-glade.h"
#include "mooutils/moodialogs.h" #include "mooutils/moodialogs.h"
#include "mooutils/moostock.h" #include "mooutils/moostock.h"
@ -466,7 +467,7 @@ _moo_edit_save_multiple_changes_dialog (GSList *docs,
void void
_moo_edit_save_error_dialog (GtkWidget *widget, _moo_edit_save_error_dialog (GtkWidget *widget,
const char *filename, const char *filename,
const char *err_msg) GError *error)
{ {
char *filename_utf8, *msg = NULL; char *filename_utf8, *msg = NULL;
@ -483,19 +484,54 @@ _moo_edit_save_error_dialog (GtkWidget *widget,
else else
msg = g_strdup (_("Could not save file")); msg = g_strdup (_("Could not save file"));
moo_error_dialog (widget, msg, err_msg); moo_error_dialog (widget, msg, error ? error->message : NULL);
g_free (msg); g_free (msg);
g_free (filename_utf8); g_free (filename_utf8);
} }
void
_moo_edit_save_error_enc_dialog (GtkWidget *widget,
const char *filename,
const char *encoding)
{
char *filename_utf8, *msg = NULL;
char *secondary;
g_return_if_fail (filename != NULL);
g_return_if_fail (encoding != NULL);
filename_utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
if (!filename_utf8)
g_critical ("%s: could not convert filename '%s' to utf8", G_STRLOC, filename);
if (filename_utf8)
/* Could not save file foo.txt */
msg = g_strdup_printf (_("Error saving file\n%s"), filename_utf8);
else
msg = g_strdup (_("Error saving file"));
secondary = g_strdup_printf (_("Could not convert file to requested character "
"encoding %s. File was saved in UTF-8 encoding."),
encoding);
moo_error_dialog (widget, msg, secondary);
g_free (msg);
g_free (secondary);
g_free (filename_utf8);
}
void void
_moo_edit_open_error_dialog (GtkWidget *widget, _moo_edit_open_error_dialog (GtkWidget *widget,
const char *filename, const char *filename,
const char *err_msg) const char *encoding,
GError *error)
{ {
char *filename_utf8, *msg = NULL; char *filename_utf8, *msg = NULL;
char *secondary;
g_return_if_fail (filename != NULL); g_return_if_fail (filename != NULL);
@ -510,16 +546,34 @@ _moo_edit_open_error_dialog (GtkWidget *widget,
else else
msg = g_strdup (_("Could not open file")); msg = g_strdup (_("Could not open file"));
moo_error_dialog (widget, msg, err_msg); if (error && error->domain == MOO_EDIT_FILE_ERROR &&
error->code == MOO_EDIT_FILE_ERROR_ENCODING)
{
if (encoding)
secondary = g_strdup_printf (_("Could not open file using character encoding %s. "
"The file may be binary or encoding may be specified "
"incorrectly."), encoding);
else
secondary = g_strdup_printf (_("Could not detect file character encoding. "
"Please make sure the file is not binary and try to select "
"encoding in the Open dialog."));
}
else
{
secondary = error ? g_strdup (error->message) : NULL;
}
moo_error_dialog (widget, msg, secondary);
g_free (msg); g_free (msg);
g_free (secondary);
g_free (filename_utf8); g_free (filename_utf8);
} }
void void
_moo_edit_reload_error_dialog (MooEdit *doc, _moo_edit_reload_error_dialog (MooEdit *doc,
const char *err_msg) GError *error)
{ {
const char *filename; const char *filename;
char *msg = NULL; char *msg = NULL;
@ -536,7 +590,8 @@ _moo_edit_reload_error_dialog (MooEdit *doc,
/* Could not reload file foo.txt */ /* Could not reload file foo.txt */
msg = g_strdup_printf (_("Could not reload file\n%s"), filename); msg = g_strdup_printf (_("Could not reload file\n%s"), filename);
moo_error_dialog (GTK_WIDGET (doc), msg, err_msg); /* XXX */
moo_error_dialog (GTK_WIDGET (doc), msg, error ? error->message : NULL);
g_free (msg); g_free (msg);
} }

View File

@ -42,12 +42,16 @@ gboolean _moo_edit_overwrite_modified_dialog (MooEdit *edit);
void _moo_edit_save_error_dialog (GtkWidget *widget, void _moo_edit_save_error_dialog (GtkWidget *widget,
const char *filename, const char *filename,
const char *err_msg); GError *error);
void _moo_edit_save_error_enc_dialog (GtkWidget *widget,
const char *filename,
const char *encoding);
void _moo_edit_open_error_dialog (GtkWidget *widget, void _moo_edit_open_error_dialog (GtkWidget *widget,
const char *filename, const char *filename,
const char *err_msg); const char *encoding,
GError *error);
void _moo_edit_reload_error_dialog (MooEdit *doc, void _moo_edit_reload_error_dialog (MooEdit *doc,
const char *err_msg); GError *error);
gboolean _moo_text_search_from_start_dialog (GtkWidget *parent, gboolean _moo_text_search_from_start_dialog (GtkWidget *parent,

View File

@ -23,6 +23,7 @@
#include "mooedit/mootextbuffer.h" #include "mooedit/mootextbuffer.h"
#include "mooutils/moocompat.h" #include "mooutils/moocompat.h"
#include "mooutils/moofilewatch.h" #include "mooutils/moofilewatch.h"
#include "mooutils/mooencodings.h"
#include <string.h> #include <string.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -38,32 +39,9 @@
#undef LOAD_BINARY #undef LOAD_BINARY
static MooEditLoader *default_loader = NULL;
static MooEditSaver *default_saver = NULL;
static GSList *UNTITLED = NULL; static GSList *UNTITLED = NULL;
static GHashTable *UNTITLED_NO = NULL; static GHashTable *UNTITLED_NO = NULL;
static gboolean moo_edit_load_default (MooEditLoader *loader,
MooEdit *edit,
const char *file,
const char *encoding,
GError **error);
static gboolean moo_edit_reload_default (MooEditLoader *loader,
MooEdit *edit,
GError **error);
static gboolean moo_edit_save_default (MooEditSaver *saver,
MooEdit *edit,
const char *file,
const char *encoding,
MooEditSaveFlags flags,
GError **error);
static gboolean moo_edit_save_copy_default (MooEditSaver *saver,
MooEdit *edit,
const char *file,
const char *encoding,
GError **error);
static void block_buffer_signals (MooEdit *edit); static void block_buffer_signals (MooEdit *edit);
static void unblock_buffer_signals (MooEdit *edit); static void unblock_buffer_signals (MooEdit *edit);
static gboolean focus_in_cb (MooEdit *edit); static gboolean focus_in_cb (MooEdit *edit);
@ -74,84 +52,57 @@ static void file_deleted (MooEdit *edit);
static void add_status (MooEdit *edit, static void add_status (MooEdit *edit,
MooEditStatus s); MooEditStatus s);
static gboolean moo_edit_load_local (MooEdit *edit,
const char *file,
const char *encoding,
GError **error);
static gboolean moo_edit_reload_local (MooEdit *edit,
GError **error);
static gboolean moo_edit_save_local (MooEdit *edit,
const char *filename,
const char *encoding,
MooEditSaveFlags flags,
GError **error);
static gboolean moo_edit_save_copy_local (MooEdit *edit,
const char *filename,
const char *encoding,
GError **error);
MooEditLoader*
_moo_edit_loader_get_default (void)
{
if (!default_loader)
{
default_loader = g_new0 (MooEditLoader, 1);
default_loader->ref_count = 1;
default_loader->load = moo_edit_load_default;
default_loader->reload = moo_edit_reload_default;
}
return default_loader; GQuark
_moo_edit_file_error_quark (void)
{
static GQuark q;
if (!q)
q = g_quark_from_static_string ("MooEditFileErrorQuark");
return q;
} }
MooEditSaver* static const char *
_moo_edit_saver_get_default (void) normalize_encoding (const char *encoding,
gboolean for_save)
{ {
if (!default_saver) if (encoding)
{ {
default_saver = g_new0 (MooEditSaver, 1); if (!encoding[0] || !strcmp (encoding, MOO_ENCODING_AUTO))
default_saver->ref_count = 1; encoding = for_save ? MOO_ENCODING_UTF8 : NULL;
default_saver->save = moo_edit_save_default; else if (!strcmp (encoding, MOO_ENCODING_LOCALE))
default_saver->save_copy = moo_edit_save_copy_default; encoding = _moo_encoding_locale ();
}
else
{
encoding = for_save ? MOO_ENCODING_UTF8 : NULL;
} }
return default_saver; return encoding;
}
MooEditLoader*
_moo_edit_loader_ref (MooEditLoader *loader)
{
g_return_val_if_fail (loader != NULL, NULL);
loader->ref_count++;
return loader;
}
MooEditSaver*
_moo_edit_saver_ref (MooEditSaver *saver)
{
g_return_val_if_fail (saver != NULL, NULL);
saver->ref_count++;
return saver;
}
void
_moo_edit_loader_unref (MooEditLoader *loader)
{
if (!loader || --loader->ref_count)
return;
g_free (loader);
if (loader == default_loader)
default_loader = NULL;
}
void
_moo_edit_saver_unref (MooEditSaver *saver)
{
if (!saver || --saver->ref_count)
return;
g_free (saver);
if (saver == default_saver)
default_saver = NULL;
} }
gboolean gboolean
_moo_edit_loader_load (MooEditLoader *loader, _moo_edit_load_file (MooEdit *edit,
MooEdit *edit,
const char *filename, const char *filename,
const char *encoding, const char *encoding,
GError **error) GError **error)
@ -160,14 +111,13 @@ _moo_edit_loader_load (MooEditLoader *loader,
gboolean result; gboolean result;
GError *error_here = NULL; GError *error_here = NULL;
g_return_val_if_fail (loader != NULL, 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 (filename != NULL, FALSE); g_return_val_if_fail (filename != NULL, FALSE);
filename_copy = g_strdup (filename); filename_copy = g_strdup (filename);
encoding_copy = g_strdup (encoding); encoding_copy = g_strdup (normalize_encoding (encoding, FALSE));
result = loader->load (loader, edit, filename_copy, encoding_copy, &error_here); result = moo_edit_load_local (edit, filename_copy, encoding_copy, &error_here);
if (error_here) if (error_here)
g_propagate_error (error, error_here); g_propagate_error (error, error_here);
@ -179,17 +129,15 @@ _moo_edit_loader_load (MooEditLoader *loader,
gboolean gboolean
_moo_edit_loader_reload (MooEditLoader *loader, _moo_edit_reload_file (MooEdit *edit,
MooEdit *edit,
GError **error) GError **error)
{ {
GError *error_here = NULL; GError *error_here = NULL;
gboolean result; gboolean result;
g_return_val_if_fail (loader != NULL, FALSE);
g_return_val_if_fail (MOO_IS_EDIT (edit), FALSE); g_return_val_if_fail (MOO_IS_EDIT (edit), FALSE);
result = loader->reload (loader, edit, &error_here); result = moo_edit_reload_local (edit, &error_here);
if (error_here) if (error_here)
g_propagate_error (error, error_here); g_propagate_error (error, error_here);
@ -199,8 +147,7 @@ _moo_edit_loader_reload (MooEditLoader *loader,
gboolean gboolean
_moo_edit_saver_save (MooEditSaver *saver, _moo_edit_save_file (MooEdit *edit,
MooEdit *edit,
const char *filename, const char *filename,
const char *encoding, const char *encoding,
MooEditSaveFlags flags, MooEditSaveFlags flags,
@ -210,14 +157,13 @@ _moo_edit_saver_save (MooEditSaver *saver,
char *filename_copy, *encoding_copy; char *filename_copy, *encoding_copy;
gboolean result; gboolean result;
g_return_val_if_fail (saver != NULL, 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 (filename != NULL, FALSE); g_return_val_if_fail (filename != NULL, FALSE);
filename_copy = g_strdup (filename); filename_copy = g_strdup (filename);
encoding_copy = g_strdup (encoding); encoding_copy = g_strdup (normalize_encoding (encoding, TRUE));
result = saver->save (saver, edit, filename_copy, encoding_copy, flags, &error_here); result = moo_edit_save_local (edit, filename_copy, encoding_copy, flags, &error_here);
if (error_here) if (error_here)
g_propagate_error (error, error_here); g_propagate_error (error, error_here);
@ -229,8 +175,7 @@ _moo_edit_saver_save (MooEditSaver *saver,
gboolean gboolean
_moo_edit_saver_save_copy (MooEditSaver *saver, _moo_edit_save_file_copy (MooEdit *edit,
MooEdit *edit,
const char *filename, const char *filename,
const char *encoding, const char *encoding,
GError **error) GError **error)
@ -238,14 +183,13 @@ _moo_edit_saver_save_copy (MooEditSaver *saver,
char *filename_copy, *encoding_copy; char *filename_copy, *encoding_copy;
gboolean result; gboolean result;
g_return_val_if_fail (saver != NULL, 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 (filename != NULL, FALSE); g_return_val_if_fail (filename != NULL, FALSE);
filename_copy = g_strdup (filename); filename_copy = g_strdup (filename);
encoding_copy = g_strdup (encoding); encoding_copy = g_strdup (normalize_encoding (encoding, TRUE));
result = saver->save_copy (saver, edit, filename_copy, encoding_copy, error); result = moo_edit_save_copy_local (edit, filename_copy, encoding_copy, error);
g_free (filename_copy); g_free (filename_copy);
g_free (encoding_copy); g_free (encoding_copy);
@ -253,6 +197,18 @@ _moo_edit_saver_save_copy (MooEditSaver *saver,
} }
static void
set_encoding_error (GError **error)
{
GError *tmp_error = NULL;
g_set_error (&tmp_error, MOO_EDIT_FILE_ERROR,
MOO_EDIT_FILE_ERROR_ENCODING,
"%s", *error ? (*error)->message : "ERROR");
g_clear_error (error);
g_propagate_error (error, tmp_error);
}
/***************************************************************************/ /***************************************************************************/
/* File loading /* File loading
*/ */
@ -305,8 +261,7 @@ try_load (MooEdit *edit,
static gboolean static gboolean
moo_edit_load_default (G_GNUC_UNUSED MooEditLoader *loader, moo_edit_load_local (MooEdit *edit,
MooEdit *edit,
const char *file, const char *file,
const char *encoding, const char *encoding,
GError **error) GError **error)
@ -382,6 +337,9 @@ moo_edit_load_default (G_GNUC_UNUSED MooEditLoader *loader,
} }
#endif #endif
if (result == ERROR_ENCODING)
set_encoding_error (error);
if (result != SUCCESS) if (result != SUCCESS)
gtk_text_buffer_set_text (buffer, "", 0); gtk_text_buffer_set_text (buffer, "", 0);
@ -664,8 +622,7 @@ error_out:
/* XXX */ /* XXX */
static gboolean static gboolean
moo_edit_reload_default (MooEditLoader *loader, moo_edit_reload_local (MooEdit *edit,
MooEdit *edit,
GError **error) GError **error)
{ {
GtkTextIter start, end; GtkTextIter start, end;
@ -684,7 +641,7 @@ moo_edit_reload_default (MooEditLoader *loader,
g_object_get (edit, "enable-highlight", &enable_highlight, NULL); g_object_get (edit, "enable-highlight", &enable_highlight, NULL);
g_object_set (edit, "enable-highlight", FALSE, NULL); g_object_set (edit, "enable-highlight", FALSE, NULL);
result = _moo_edit_loader_load (loader, edit, edit->priv->filename, result = _moo_edit_load_file (edit, edit->priv->filename,
edit->priv->encoding, error); edit->priv->encoding, error);
g_object_set (edit, "enable-highlight", enable_highlight, NULL); g_object_set (edit, "enable-highlight", enable_highlight, NULL);
@ -722,41 +679,72 @@ static gboolean do_write (MooEdit *edit,
static gboolean static gboolean
moo_edit_save_default (G_GNUC_UNUSED MooEditSaver *saver, do_save_local (MooEdit *edit,
MooEdit *edit,
const char *filename, const char *filename,
const char *encoding, const char *encoding,
MooEditSaveFlags flags, GError **error,
GError **error) gboolean *retval)
{ {
g_return_val_if_fail (MOO_IS_EDIT (edit), FALSE); *retval = TRUE;
g_return_val_if_fail (filename && filename[0], FALSE);
if (!encoding)
encoding = "UTF-8";
if (!do_write (edit, filename, encoding, error)) if (!do_write (edit, filename, encoding, error))
return FALSE; {
if ((*error)->domain != G_CONVERT_ERROR ||
_moo_encodings_equal (encoding, MOO_ENCODING_UTF8))
{
g_clear_error (error);
edit->priv->status = 0; if (!do_write (edit, filename, MOO_ENCODING_UTF8, error))
_moo_edit_set_filename (edit, filename, encoding); {
moo_edit_set_modified (edit, FALSE); *retval = FALSE;
_moo_edit_start_file_watch (edit); return FALSE;
}
}
*retval = FALSE;
set_encoding_error (error);
}
return TRUE; return TRUE;
} }
static gboolean static gboolean
moo_edit_save_copy_default (G_GNUC_UNUSED MooEditSaver *saver, moo_edit_save_local (MooEdit *edit,
MooEdit *edit, const char *filename,
const char *encoding,
MooEditSaveFlags flags,
GError **error)
{
gboolean result;
g_return_val_if_fail (MOO_IS_EDIT (edit), FALSE);
g_return_val_if_fail (filename && filename[0], FALSE);
if (do_save_local (edit, filename, encoding, error, &result))
{
edit->priv->status = 0;
_moo_edit_set_filename (edit, filename,
result ? encoding : MOO_ENCODING_UTF8);
moo_edit_set_modified (edit, FALSE);
_moo_edit_start_file_watch (edit);
}
return result;
}
static gboolean
moo_edit_save_copy_local (MooEdit *edit,
const char *filename, const char *filename,
const char *encoding, const char *encoding,
GError **error) GError **error)
{ {
gboolean result;
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);
return do_write (edit, filename, encoding ? encoding : "UTF-8", error); do_save_local (edit, filename, encoding, error, &result);
return result;
} }

View File

@ -23,69 +23,27 @@
G_BEGIN_DECLS G_BEGIN_DECLS
typedef struct _MooEditLoader MooEditLoader;
typedef struct _MooEditSaver MooEditSaver;
typedef enum { typedef enum {
MOO_EDIT_SAVE_BACKUP = 1 << 0 MOO_EDIT_SAVE_BACKUP = 1 << 0
} MooEditSaveFlags; } MooEditSaveFlags;
struct _MooEditLoader #define MOO_EDIT_FILE_ERROR (_moo_edit_file_error_quark ())
{ #define MOO_EDIT_FILE_ERROR_ENCODING 0
guint ref_count;
gboolean (*load) (MooEditLoader *loader, GQuark _moo_edit_file_error_quark (void) G_GNUC_CONST;
MooEdit *edit,
gboolean _moo_edit_load_file (MooEdit *edit,
const char *file, const char *file,
const char *encoding, const char *encoding,
GError **error); GError **error);
gboolean (*reload) (MooEditLoader *loader, gboolean _moo_edit_reload_file (MooEdit *edit,
MooEdit *edit,
GError **error); GError **error);
}; gboolean _moo_edit_save_file (MooEdit *edit,
struct _MooEditSaver
{
guint ref_count;
gboolean (*save) (MooEditSaver *saver,
MooEdit *edit,
const char *file, const char *file,
const char *encoding, const char *encoding,
MooEditSaveFlags flags, MooEditSaveFlags flags,
GError **error); GError **error);
gboolean (*save_copy) (MooEditSaver *saver, gboolean _moo_edit_save_file_copy (MooEdit *edit,
MooEdit *edit,
const char *file,
const char *encoding,
GError **error);
};
MooEditLoader *_moo_edit_loader_get_default(void);
MooEditSaver *_moo_edit_saver_get_default (void);
MooEditLoader *_moo_edit_loader_ref (MooEditLoader *loader);
void _moo_edit_loader_unref (MooEditLoader *loader);
MooEditSaver *_moo_edit_saver_ref (MooEditSaver *saver);
void _moo_edit_saver_unref (MooEditSaver *saver);
gboolean _moo_edit_loader_load (MooEditLoader *loader,
MooEdit *edit,
const char *file,
const char *encoding,
GError **error);
gboolean _moo_edit_loader_reload (MooEditLoader *loader,
MooEdit *edit,
GError **error);
gboolean _moo_edit_saver_save (MooEditSaver *saver,
MooEdit *edit,
const char *file,
const char *encoding,
MooEditSaveFlags flags,
GError **error);
gboolean _moo_edit_saver_save_copy (MooEditSaver *saver,
MooEdit *edit,
const char *file, const char *file,
const char *encoding, const char *encoding,
GError **error); GError **error);

View File

@ -70,9 +70,7 @@ static GtkAction *create_recent_action (MooEditWindow *window);
static MooEditWindow *create_window (MooEditor *editor); static MooEditWindow *create_window (MooEditor *editor);
static void moo_editor_add_doc (MooEditor *editor, static void moo_editor_add_doc (MooEditor *editor,
MooEditWindow *window, MooEditWindow *window,
MooEdit *doc, MooEdit *doc);
MooEditLoader *loader,
MooEditSaver *saver);
static void do_close_window (MooEditor *editor, static void do_close_window (MooEditor *editor,
MooEditWindow *window); MooEditWindow *window);
static void do_close_doc (MooEditor *editor, static void do_close_doc (MooEditor *editor,
@ -81,10 +79,6 @@ static gboolean close_docs_real (MooEditor *editor,
GSList *docs, GSList *docs,
gboolean ask_confirm); gboolean ask_confirm);
static GSList *find_modified (GSList *docs); static GSList *find_modified (GSList *docs);
static MooEditLoader*get_loader (MooEditor *editor,
MooEdit *doc);
static MooEditSaver *get_saver (MooEditor *editor,
MooEdit *doc);
static void activate_history_item (MooEditor *editor, static void activate_history_item (MooEditor *editor,
MooHistoryItem *item, MooHistoryItem *item,
@ -108,8 +102,6 @@ struct _MooEditorPrivate {
GSList *messages; GSList *messages;
WindowInfo *windowless; WindowInfo *windowless;
GSList *windows; /* WindowInfo* */ GSList *windows; /* WindowInfo* */
GHashTable *loaders;
GHashTable *savers;
char *app_name; char *app_name;
MooUIXML *ui_xml; MooUIXML *ui_xml;
MooFilterMgr *filter_mgr; MooFilterMgr *filter_mgr;
@ -303,13 +295,6 @@ moo_editor_init (MooEditor *editor)
editor->priv->windows = NULL; editor->priv->windows = NULL;
editor->priv->windowless = window_info_new (NULL); editor->priv->windowless = window_info_new (NULL);
editor->priv->loaders =
g_hash_table_new_full (g_direct_hash, g_direct_equal,
NULL, (GDestroyNotify) _moo_edit_loader_unref);
editor->priv->savers =
g_hash_table_new_full (g_direct_hash, g_direct_equal,
NULL, (GDestroyNotify) _moo_edit_saver_unref);
moo_prefs_new_key_string (moo_edit_setting (MOO_EDIT_PREFS_DEFAULT_LANG), moo_prefs_new_key_string (moo_edit_setting (MOO_EDIT_PREFS_DEFAULT_LANG),
MOO_LANG_NONE); MOO_LANG_NONE);
@ -470,9 +455,6 @@ moo_editor_finalize (GObject *object)
window_info_free (editor->priv->windowless); window_info_free (editor->priv->windowless);
g_hash_table_destroy (editor->priv->loaders);
g_hash_table_destroy (editor->priv->savers);
g_free (editor->priv->default_lang); g_free (editor->priv->default_lang);
g_slist_foreach (editor->priv->messages, (GFunc) message_free, NULL); g_slist_foreach (editor->priv->messages, (GFunc) message_free, NULL);
@ -986,9 +968,7 @@ create_window (MooEditor *editor)
static void static void
moo_editor_add_doc (MooEditor *editor, moo_editor_add_doc (MooEditor *editor,
MooEditWindow *window, MooEditWindow *window,
MooEdit *doc, MooEdit *doc)
MooEditLoader *loader,
MooEditSaver *saver)
{ {
WindowInfo *info; WindowInfo *info;
@ -1006,9 +986,6 @@ moo_editor_add_doc (MooEditor *editor,
window_info_add (info, doc); window_info_add (info, doc);
g_hash_table_insert (editor->priv->loaders, doc, _moo_edit_loader_ref (loader));
g_hash_table_insert (editor->priv->savers, doc, _moo_edit_saver_ref (saver));
if (!moo_edit_get_filename (doc) && if (!moo_edit_get_filename (doc) &&
!moo_edit_config_get_string (doc->config, "lang") && !moo_edit_config_get_string (doc->config, "lang") &&
editor->priv->default_lang) editor->priv->default_lang)
@ -1035,9 +1012,7 @@ moo_editor_new_window (MooEditor *editor)
{ {
doc = g_object_new (get_doc_type (editor), "editor", editor, NULL); doc = g_object_new (get_doc_type (editor), "editor", editor, NULL);
_moo_edit_window_insert_doc (window, doc, -1); _moo_edit_window_insert_doc (window, doc, -1);
moo_editor_add_doc (editor, window, doc, moo_editor_add_doc (editor, window, doc);
_moo_edit_loader_get_default (),
_moo_edit_saver_get_default ());
} }
return window; return window;
@ -1052,23 +1027,19 @@ moo_editor_create_doc (MooEditor *editor,
GError **error) GError **error)
{ {
MooEdit *doc; MooEdit *doc;
MooEditLoader *loader;
MooEditSaver *saver;
g_return_val_if_fail (MOO_IS_EDITOR (editor), NULL); g_return_val_if_fail (MOO_IS_EDITOR (editor), NULL);
doc = g_object_new (get_doc_type (editor), "editor", editor, NULL); doc = g_object_new (get_doc_type (editor), "editor", editor, NULL);
loader = _moo_edit_loader_get_default ();
saver = _moo_edit_saver_get_default ();
if (filename && !_moo_edit_loader_load (loader, doc, filename, encoding, error)) if (filename && !_moo_edit_load_file (doc, filename, encoding, error))
{ {
gtk_object_sink (g_object_ref (doc)); gtk_object_sink (g_object_ref (doc));
g_object_unref (doc); g_object_unref (doc);
return NULL; return NULL;
} }
moo_editor_add_doc (editor, NULL, doc, loader, saver); moo_editor_add_doc (editor, NULL, doc);
_moo_doc_attach_plugins (NULL, doc); _moo_doc_attach_plugins (NULL, doc);
return doc; return doc;
@ -1095,9 +1066,7 @@ moo_editor_new_doc (MooEditor *editor,
doc = g_object_new (get_doc_type (editor), "editor", editor, NULL); doc = g_object_new (get_doc_type (editor), "editor", editor, NULL);
_moo_edit_window_insert_doc (window, doc, -1); _moo_edit_window_insert_doc (window, doc, -1);
moo_editor_add_doc (editor, window, doc, moo_editor_add_doc (editor, window, doc);
_moo_edit_loader_get_default (),
_moo_edit_saver_get_default ());
return doc; return doc;
} }
@ -1110,8 +1079,6 @@ moo_editor_open (MooEditor *editor,
GSList *files) GSList *files)
{ {
GSList *l; GSList *l;
MooEditLoader *loader;
MooEditSaver *saver;
MooEdit *bring_to_front = NULL; MooEdit *bring_to_front = NULL;
gboolean result = TRUE; gboolean result = TRUE;
@ -1136,9 +1103,6 @@ moo_editor_open (MooEditor *editor,
return result; return result;
} }
loader = _moo_edit_loader_get_default ();
saver = _moo_edit_saver_get_default ();
for (l = files; l != NULL; l = l->next) for (l = files; l != NULL; l = l->next)
{ {
MooEditFileInfo *info = l->data; MooEditFileInfo *info = l->data;
@ -1177,11 +1141,10 @@ moo_editor_open (MooEditor *editor,
} }
/* XXX open_single */ /* XXX open_single */
if (!_moo_edit_loader_load (loader, doc, filename, info->encoding, &error)) if (!_moo_edit_load_file (doc, filename, info->encoding, &error))
{ {
if (!editor->priv->silent) if (!editor->priv->silent)
_moo_edit_open_error_dialog (parent, filename, _moo_edit_open_error_dialog (parent, filename, info->encoding, error);
error ? error->message : NULL);
g_error_free (error); g_error_free (error);
result = FALSE; result = FALSE;
} }
@ -1195,7 +1158,7 @@ moo_editor_open (MooEditor *editor,
if (new_doc) if (new_doc)
{ {
_moo_edit_window_insert_doc (window, doc, -1); _moo_edit_window_insert_doc (window, doc, -1);
moo_editor_add_doc (editor, window, doc, loader, saver); moo_editor_add_doc (editor, window, doc);
} }
else else
{ {
@ -1435,9 +1398,6 @@ do_close_doc (MooEditor *editor,
_moo_edit_window_remove_doc (info->window, doc); _moo_edit_window_remove_doc (info->window, doc);
else else
_moo_doc_detach_plugins (NULL, doc); _moo_doc_detach_plugins (NULL, doc);
g_hash_table_remove (editor->priv->loaders, doc);
g_hash_table_remove (editor->priv->savers, doc);
} }
@ -1498,9 +1458,7 @@ moo_editor_close_docs (MooEditor *editor,
MooEdit *doc = g_object_new (get_doc_type (editor), MooEdit *doc = g_object_new (get_doc_type (editor),
"editor", editor, NULL); "editor", editor, NULL);
_moo_edit_window_insert_doc (info->window, doc, -1); _moo_edit_window_insert_doc (info->window, doc, -1);
moo_editor_add_doc (editor, info->window, doc, moo_editor_add_doc (editor, info->window, doc);
_moo_edit_loader_get_default (),
_moo_edit_saver_get_default ());
} }
return TRUE; return TRUE;
@ -1830,7 +1788,6 @@ _moo_editor_reload (MooEditor *editor,
GError **error) GError **error)
{ {
WindowInfo *info; WindowInfo *info;
MooEditLoader *loader;
GError *error_here = NULL; GError *error_here = NULL;
int cursor_line, cursor_offset; int cursor_line, cursor_offset;
GtkTextIter iter; GtkTextIter iter;
@ -1843,9 +1800,6 @@ _moo_editor_reload (MooEditor *editor,
info = window_list_find_doc (editor, doc); info = window_list_find_doc (editor, doc);
g_return_if_fail (info != NULL); g_return_if_fail (info != NULL);
loader = get_loader (editor, doc);
g_return_if_fail (loader != NULL);
/* XXX */ /* XXX */
g_return_if_fail (moo_edit_get_filename (doc) != NULL); g_return_if_fail (moo_edit_get_filename (doc) != NULL);
@ -1859,10 +1813,10 @@ _moo_editor_reload (MooEditor *editor,
cursor_line = gtk_text_iter_get_line (&iter); cursor_line = gtk_text_iter_get_line (&iter);
cursor_offset = moo_text_iter_get_visual_line_offset (&iter, 8); cursor_offset = moo_text_iter_get_visual_line_offset (&iter, 8);
if (!_moo_edit_loader_reload (loader, doc, &error_here)) if (!_moo_edit_reload_file (doc, &error_here))
{ {
if (!editor->priv->silent) if (!editor->priv->silent)
_moo_edit_reload_error_dialog (doc, error_here->message); _moo_edit_reload_error_dialog (doc, error_here);
else else
g_propagate_error (error, error_here); g_propagate_error (error, error_here);
@ -1889,7 +1843,6 @@ moo_editor_get_save_flags (MooEditor *editor)
static gboolean static gboolean
do_save (MooEditor *editor, do_save (MooEditor *editor,
MooEditSaver *saver,
MooEdit *doc, MooEdit *doc,
const char *filename, const char *filename,
const char *encoding, const char *encoding,
@ -1904,8 +1857,9 @@ do_save (MooEditor *editor,
moo_text_view_strip_whitespace (MOO_TEXT_VIEW (doc)); moo_text_view_strip_whitespace (MOO_TEXT_VIEW (doc));
g_signal_emit_by_name (doc, "save-before"); g_signal_emit_by_name (doc, "save-before");
result = _moo_edit_saver_save (saver, doc, filename, encoding, result = _moo_edit_save_file (doc, filename, encoding,
moo_editor_get_save_flags (editor), error); moo_editor_get_save_flags (editor),
error);
g_signal_emit_by_name (doc, "save-after"); g_signal_emit_by_name (doc, "save-after");
return result; return result;
@ -1918,7 +1872,6 @@ _moo_editor_save (MooEditor *editor,
GError **error) GError **error)
{ {
WindowInfo *info; WindowInfo *info;
MooEditSaver *saver;
GError *error_here = NULL; GError *error_here = NULL;
char *filename; char *filename;
char *encoding; char *encoding;
@ -1932,9 +1885,6 @@ _moo_editor_save (MooEditor *editor,
info = window_list_find_doc (editor, doc); info = window_list_find_doc (editor, doc);
g_return_val_if_fail (info != NULL, FALSE); g_return_val_if_fail (info != NULL, FALSE);
saver = get_saver (editor, doc);
g_return_val_if_fail (saver != NULL, FALSE);
if (!moo_edit_get_filename (doc)) if (!moo_edit_get_filename (doc))
return _moo_editor_save_as (editor, doc, NULL, NULL, error); return _moo_editor_save_as (editor, doc, NULL, NULL, error);
@ -1946,16 +1896,21 @@ _moo_editor_save (MooEditor *editor,
!_moo_edit_overwrite_modified_dialog (doc)) !_moo_edit_overwrite_modified_dialog (doc))
goto out; goto out;
if (!do_save (editor, saver, doc, filename, encoding, &error_here)) if (!do_save (editor, doc, filename, encoding, &error_here))
{ {
if (!editor->priv->silent) if (!editor->priv->silent)
{ {
_moo_edit_save_error_dialog (GTK_WIDGET (doc), filename, gboolean saved_utf8 = error_here->domain == MOO_EDIT_FILE_ERROR &&
error_here->message); error_here->code == MOO_EDIT_FILE_ERROR_ENCODING;
if (saved_utf8)
_moo_edit_save_error_enc_dialog (GTK_WIDGET (doc), filename, encoding);
else
_moo_edit_save_error_dialog (GTK_WIDGET (doc), filename, error_here);
g_error_free (error_here); g_error_free (error_here);
} }
else else
{ {
/* XXX */
g_propagate_error (error, error_here); g_propagate_error (error, error_here);
} }
@ -1981,7 +1936,6 @@ _moo_editor_save_as (MooEditor *editor,
GError **error) GError **error)
{ {
WindowInfo *info; WindowInfo *info;
MooEditSaver *saver;
GError *error_here = NULL; GError *error_here = NULL;
MooEditFileInfo *file_info = NULL; MooEditFileInfo *file_info = NULL;
gboolean result = FALSE; gboolean result = FALSE;
@ -1994,9 +1948,6 @@ _moo_editor_save_as (MooEditor *editor,
info = window_list_find_doc (editor, doc); info = window_list_find_doc (editor, doc);
g_return_val_if_fail (info != NULL, FALSE); g_return_val_if_fail (info != NULL, FALSE);
saver = get_saver (editor, doc);
g_return_val_if_fail (saver != NULL, FALSE);
if (!filename) if (!filename)
{ {
file_info = _moo_edit_save_as_dialog (doc, editor->priv->filter_mgr, file_info = _moo_edit_save_as_dialog (doc, editor->priv->filter_mgr,
@ -2010,17 +1961,25 @@ _moo_editor_save_as (MooEditor *editor,
file_info = moo_edit_file_info_new (filename, encoding); file_info = moo_edit_file_info_new (filename, encoding);
} }
if (!do_save (editor, saver, doc, file_info->filename, file_info->encoding, &error_here)) if (!do_save (editor, doc, file_info->filename, file_info->encoding, &error_here))
{ {
if (!editor->priv->silent) if (!editor->priv->silent)
{ {
gboolean saved_utf8 = error_here->domain == MOO_EDIT_FILE_ERROR &&
error_here->code == MOO_EDIT_FILE_ERROR_ENCODING;
if (saved_utf8)
_moo_edit_save_error_enc_dialog (GTK_WIDGET (doc),
file_info->filename,
file_info->encoding);
else
_moo_edit_save_error_dialog (GTK_WIDGET (doc), _moo_edit_save_error_dialog (GTK_WIDGET (doc),
file_info->filename, file_info->filename,
error_here->message); error_here);
g_error_free (error_here); g_error_free (error_here);
} }
else else
{ {
/* XXX */
g_propagate_error (error, error_here); g_propagate_error (error, error_here);
} }
@ -2045,7 +2004,6 @@ moo_editor_save_copy (MooEditor *editor,
GError **error) GError **error)
{ {
WindowInfo *info; WindowInfo *info;
MooEditSaver *saver;
g_return_val_if_fail (MOO_IS_EDITOR (editor), FALSE); g_return_val_if_fail (MOO_IS_EDITOR (editor), FALSE);
g_return_val_if_fail (filename != NULL, FALSE); g_return_val_if_fail (filename != NULL, FALSE);
@ -2053,26 +2011,7 @@ moo_editor_save_copy (MooEditor *editor,
info = window_list_find_doc (editor, doc); info = window_list_find_doc (editor, doc);
g_return_val_if_fail (info != NULL, FALSE); g_return_val_if_fail (info != NULL, FALSE);
saver = get_saver (editor, doc); return _moo_edit_save_file_copy (doc, filename, encoding, error);
g_return_val_if_fail (saver != NULL, FALSE);
return _moo_edit_saver_save_copy (saver, doc, filename, encoding, error);
}
static MooEditLoader*
get_loader (MooEditor *editor,
MooEdit *doc)
{
return g_hash_table_lookup (editor->priv->loaders, doc);
}
static MooEditSaver*
get_saver (MooEditor *editor,
MooEdit *doc)
{
return g_hash_table_lookup (editor->priv->savers, doc);
} }

View File

@ -59,28 +59,41 @@ static void combo_changed (GtkComboBox *combo,
static Encoding * static Encoding *
find_encoding (EncodingsManager *mgr, lookup_encoding (EncodingsManager *mgr,
const char *name) const char *name)
{ {
Encoding *enc;
char *upper; char *upper;
Encoding *enc;
g_return_val_if_fail (name != NULL, NULL); g_return_val_if_fail (name != NULL, NULL);
upper = g_ascii_strup (name, -1); upper = g_ascii_strup (name, -1);
enc = g_hash_table_lookup (mgr->encodings, upper); enc = g_hash_table_lookup (mgr->encodings, upper);
g_free (upper);
return enc;
}
static Encoding *
get_encoding (EncodingsManager *mgr,
const char *name)
{
Encoding *enc;
g_return_val_if_fail (name != NULL, NULL);
enc = lookup_encoding (mgr, name);
if (!enc) if (!enc)
{ {
enc = g_new0 (Encoding, 1); enc = g_new0 (Encoding, 1);
enc->name = g_strdup (upper); enc->name = g_ascii_strup (name, -1);
enc->short_display_name = enc->name; enc->short_display_name = enc->name;
enc->display_name = g_strdup (name); enc->display_name = g_strdup (name);
mgr->user_defined = g_slist_prepend (mgr->user_defined, enc); mgr->user_defined = g_slist_prepend (mgr->user_defined, enc);
g_hash_table_insert (mgr->encodings, (char*) enc->name, enc); g_hash_table_insert (mgr->encodings, (char*) enc->name, enc);
} }
g_free (upper);
return enc; return enc;
} }
@ -235,7 +248,7 @@ get_enc_mgr (void)
if (!g_get_charset (&locale_charset)) if (!g_get_charset (&locale_charset))
locale_charset = MOO_ENCODING_UTF8; locale_charset = MOO_ENCODING_UTF8;
mgr->locale_encoding = find_encoding (mgr, locale_charset); mgr->locale_encoding = get_encoding (mgr, locale_charset);
} }
return mgr; return mgr;
@ -387,7 +400,7 @@ encoding_combo_set_active (GtkComboBox *combo,
goto out; goto out;
} }
new_enc = find_encoding (mgr, enc_name); new_enc = get_encoding (mgr, enc_name);
g_return_if_fail (new_enc != NULL); g_return_if_fail (new_enc != NULL);
set_last (mgr, new_enc->name, save_mode); set_last (mgr, new_enc->name, save_mode);
@ -675,6 +688,38 @@ _moo_encodings_combo_get (GtkWidget *dialog,
if (!strcmp (enc_name, MOO_ENCODING_LOCALE)) if (!strcmp (enc_name, MOO_ENCODING_LOCALE))
enc_name = mgr->locale_encoding->name; enc_name = mgr->locale_encoding->name;
g_print ("encoding: %s\n", enc_name);
return enc_name; return enc_name;
} }
const char *
_moo_encoding_locale (void)
{
EncodingsManager *mgr;
mgr = get_enc_mgr ();
return mgr->locale_encoding->name;
}
gboolean
_moo_encodings_equal (const char *enc1_name,
const char *enc2_name)
{
Encoding *enc1, *enc2;
EncodingsManager *mgr;
enc1_name = enc1_name && enc1_name[0] ? enc1_name : MOO_ENCODING_UTF8;
enc2_name = enc2_name && enc2_name[0] ? enc2_name : MOO_ENCODING_UTF8;
mgr = get_enc_mgr ();
enc1 = lookup_encoding (mgr, enc1_name);
enc2 = lookup_encoding (mgr, enc2_name);
if (!enc1 || !enc2)
return !strcmp (enc1_name, enc2_name);
return enc1 == enc2;
}

View File

@ -32,6 +32,10 @@ void _moo_encodings_sync_combo (GtkWidget *dialog,
const char *_moo_encodings_combo_get (GtkWidget *dialog, const char *_moo_encodings_combo_get (GtkWidget *dialog,
gboolean save_mode); gboolean save_mode);
const char *_moo_encoding_locale (void);
gboolean _moo_encodings_equal (const char *enc1,
const char *enc2);
G_END_DECLS G_END_DECLS

View File

@ -593,7 +593,6 @@ moo_file_dialog_run (MooFileDialog *dialog)
set_filename (dialog, NULL); set_filename (dialog, NULL);
set_filenames (dialog, NULL); set_filenames (dialog, NULL);
set_encoding (dialog, NULL);
filechooser = moo_file_dialog_create_widget (dialog); filechooser = moo_file_dialog_create_widget (dialog);