Commit 505f88db authored by Jesse van den Kieboom's avatar Jesse van den Kieboom

Fixed proper use of gio errors

        * gedit-tab.c:
        * gedit-local-document-saver.c:
        * gedit-gio-document-saver.c:
        * gedit-io-error-message-area.c:
        * gedit-gio-document-loader.c:
        * gedit-mmap-document-loader.c:
        * gedit-document.c:
        * gedit-document.h:
        * gedit-document-saver.c:

        Fixed proper use of gio errors

svn path=/trunk/; revision=6419
parent e9aa4725
2008-08-16 Jesse van den Kieboom <jesse@icecrew.nl>
* gedit-tab.c:
* gedit-local-document-saver.c:
* gedit-gio-document-saver.c:
* gedit-io-error-message-area.c:
* gedit-gio-document-loader.c:
* gedit-mmap-document-loader.c:
* gedit-document.c:
* gedit-document.h:
* gedit-document-saver.c:
Fixed proper use of gio errors
2008-08-16 Jesse van den Kieboom <jesse@icecrew.nl>
* gedit/gedit-document.c: apply some settings in the constructor
......
......@@ -386,7 +386,7 @@ gedit_document_saver_write_document_contents (GeditDocumentSaver *saver,
if (!res)
{
g_set_error (error,
GEDIT_DOCUMENT_ERROR,
G_IO_ERROR,
g_io_error_from_errno (errno),
g_strerror (errno));
}
......
......@@ -1064,7 +1064,7 @@ document_loader_loaded (GeditDocumentLoader *loader,
/* special case creating a named new doc */
else if (doc->priv->create &&
(error->code == G_IO_ERROR_NOT_FOUND) &&
(error->domain == G_IO_ERROR && error->code == G_IO_ERROR_NOT_FOUND) &&
(gedit_utils_uri_has_file_scheme (doc->priv->uri)))
{
reset_temp_loading_data (doc);
......
......@@ -139,8 +139,7 @@ struct _GeditDocumentClass
enum
{
/* we use GIO error codes, so start at some high number? */
GEDIT_DOCUMENT_ERROR_EXTERNALLY_MODIFIED = 1 << 10,
GEDIT_DOCUMENT_ERROR_EXTERNALLY_MODIFIED,
GEDIT_DOCUMENT_ERROR_CANT_CREATE_BACKUP,
GEDIT_DOCUMENT_ERROR_TOO_BIG,
GEDIT_DOCUMENT_NUM_ERRORS
......
......@@ -188,6 +188,15 @@ remote_load_completed_or_failed (GeditGioDocumentLoader *gvloader, AsyncData *as
/* prototype, because they call each other... isn't C lovely */
static void read_file_chunk (AsyncData *async);
static void
async_failed (AsyncData *async, GError *error)
{
g_propagate_error (&async->loader->priv->error, error);
g_error_free (error);
remote_load_completed_or_failed (async->loader, async);
}
static void
async_read_cb (GInputStream *stream,
GAsyncResult *res,
......@@ -212,13 +221,7 @@ async_read_cb (GInputStream *stream,
/* error occurred */
if (bytes_read == -1)
{
g_set_error (&gvloader->priv->error,
GEDIT_DOCUMENT_ERROR,
error->code,
error->message);
remote_load_completed_or_failed (gvloader, async);
async_failed (async, error);
return;
}
......@@ -228,7 +231,7 @@ async_read_cb (GInputStream *stream,
g_set_error (&gvloader->priv->error,
GEDIT_DOCUMENT_ERROR,
GEDIT_DOCUMENT_ERROR_TOO_BIG,
_("File too big"));
"File too big");
remote_load_completed_or_failed (gvloader, async);
......@@ -294,9 +297,9 @@ finish_query_info (AsyncData *async)
g_file_info_get_file_type (gvloader->priv->info) != G_FILE_TYPE_REGULAR)
{
g_set_error (&gvloader->priv->error,
GEDIT_DOCUMENT_ERROR,
G_IO_ERROR,
G_IO_ERROR_NOT_REGULAR_FILE,
_("Not a regular file"));
"Not a regular file");
remote_load_completed_or_failed (gvloader, async);
......@@ -334,15 +337,7 @@ remote_get_file_info_cb (GFile *source,
if (!gvloader->priv->info)
{
/* propagate the error and clean up */
g_set_error (&gvloader->priv->error,
GEDIT_DOCUMENT_ERROR,
error->code,
error->message);
g_error_free (error);
remote_load_completed_or_failed (gvloader, async);
async_failed (async, error);
return;
}
......@@ -391,15 +386,7 @@ remote_get_info_cb (GFileInputStream *source,
}
/* propagate the error and clean up */
g_set_error (&gvloader->priv->error,
GEDIT_DOCUMENT_ERROR,
error->code,
error->message);
g_error_free (error);
remote_load_completed_or_failed (gvloader, async);
async_failed (async, error);
return;
}
......@@ -425,13 +412,7 @@ mount_ready_callback (GFile *file,
if (!mounted)
{
g_set_error (&async->loader->priv->error,
GEDIT_DOCUMENT_ERROR,
error->code,
error->message);
g_error_free (error);
remote_load_completed_or_failed (async->loader, async);
async_failed (async, error);
}
else
{
......@@ -491,12 +472,8 @@ async_read_ready_callback (GObject *source,
return;
}
/* Propagate error using GEDIT_DOCUMENT_ERROR for the domain */
g_set_error (&gvloader->priv->error,
GEDIT_DOCUMENT_ERROR,
error->code,
error->message);
/* Propagate error */
g_propagate_error (&gvloader->priv->error, error);
gedit_document_loader_loading (GEDIT_DOCUMENT_LOADER (gvloader),
TRUE,
gvloader->priv->error);
......@@ -617,7 +594,7 @@ gedit_gio_document_loader_cancel (GeditDocumentLoader *loader)
g_cancellable_cancel (gvloader->priv->cancellable);
g_set_error (&gvloader->priv->error,
GEDIT_DOCUMENT_ERROR,
G_IO_ERROR,
G_IO_ERROR_CANCELLED,
"Operation cancelled");
......
......@@ -158,7 +158,8 @@ gedit_gio_document_saver_init (GeditGioDocumentSaver *gvsaver)
}
static void
remote_save_completed_or_failed (GeditGioDocumentSaver *gvsaver, AsyncData *async)
remote_save_completed_or_failed (GeditGioDocumentSaver *gvsaver,
AsyncData *async)
{
if (async)
async_data_free (async);
......@@ -168,6 +169,16 @@ remote_save_completed_or_failed (GeditGioDocumentSaver *gvsaver, AsyncData *asyn
gvsaver->priv->error);
}
static void
async_failed (AsyncData *async,
GError *error)
{
g_propagate_error (&async->saver->priv->error, error);
g_error_free (error);
remote_save_completed_or_failed (async->saver, async);
}
/* prototype, because they call each other... isn't C lovely */
static void write_file_chunk (AsyncData *async);
......@@ -225,11 +236,7 @@ remote_reget_info_cb (GFile *source,
else
{
gedit_debug_message (DEBUG_SAVER, "Query info failed: %s", error->message);
g_set_error (&saver->priv->error,
GEDIT_DOCUMENT_ERROR,
error->code,
error->message);
g_propagate_error (&saver->priv->error, error);
g_error_free (error);
}
......@@ -256,13 +263,7 @@ close_async_ready_get_info_cb (GOutputStream *stream,
{
gedit_debug_message (DEBUG_SAVER, "Closing stream error: %s", error->message);
g_set_error (&async->saver->priv->error,
GEDIT_DOCUMENT_ERROR,
error->code,
error->message);
g_error_free (error);
remote_save_completed_or_failed (async->saver, async);
async_failed (async, error);
return;
}
......@@ -292,10 +293,7 @@ close_async_ready_cb (GOutputStream *stream,
if (!g_output_stream_close_finish (stream, res, &error))
{
g_set_error (&async->saver->priv->error,
GEDIT_DOCUMENT_ERROR,
error->code,
error->message);
g_propagate_error (&async->saver->priv->error, error);
g_error_free (error);
}
......@@ -341,10 +339,7 @@ remote_get_info_cb (GFileOutputStream *stream,
else
{
gedit_debug_message (DEBUG_SAVER, "Query info failed: %s", error->message);
g_set_error (&saver->priv->error,
GEDIT_DOCUMENT_ERROR,
error->code,
error->message);
g_propagate_error (&saver->priv->error, error);
g_error_free (error);
next_callback = (GAsyncReadyCallback) close_async_ready_cb;
......@@ -409,13 +404,7 @@ async_write_cb (GOutputStream *stream,
if (bytes_written == -1)
{
gedit_debug_message (DEBUG_SAVER, "Write error: %s", error->message);
g_set_error (&gvsaver->priv->error,
GEDIT_DOCUMENT_ERROR,
error->code,
error->message);
remote_save_completed_or_failed (gvsaver, async);
async_failed (async, error);
return;
}
......@@ -481,12 +470,7 @@ async_replace_ready_callback (GFile *source,
if (!gvsaver->priv->stream)
{
gedit_debug_message (DEBUG_SAVER, "Opening file failed: %s", error->message);
g_set_error (&gvsaver->priv->error,
GEDIT_DOCUMENT_ERROR,
error->code,
error->message);
remote_save_completed_or_failed (gvsaver, async);
async_failed (async, error);
return;
}
......@@ -561,12 +545,7 @@ begin_write (AsyncData *async)
if (!buffer)
{
g_set_error (&gvsaver->priv->error,
GEDIT_DOCUMENT_ERROR,
error->code,
error->message);
remote_save_completed_or_failed (gvsaver, async);
async_failed (async, error);
return;
}
......@@ -605,13 +584,7 @@ mount_ready_callback (GFile *file,
if (!mounted)
{
g_set_error (&async->saver->priv->error,
GEDIT_DOCUMENT_ERROR,
error->code,
error->message);
g_error_free (error);
remote_save_completed_or_failed (async->saver, async);
async_failed (async, error);
}
else
{
......@@ -670,13 +643,8 @@ check_modification_callback (GFile *source,
}
gedit_debug_message (DEBUG_SAVER, "Error getting modification: %s", error->message);
g_set_error (&gvsaver->priv->error,
GEDIT_DOCUMENT_ERROR,
error->code,
error->message);
remote_save_completed_or_failed (gvsaver, async);
async_failed (async, error);
return;
}
......
......@@ -57,7 +57,7 @@ is_recoverable_error (const GError *error)
{
gboolean is_recoverable = FALSE;
if (error->domain == GEDIT_DOCUMENT_ERROR)
if (error->domain == G_IO_ERROR)
{
switch (error->code) {
case G_IO_ERROR_PERMISSION_DENIED:
......@@ -74,6 +74,13 @@ is_recoverable_error (const GError *error)
return is_recoverable;
}
static gboolean
is_gio_error (const GError *error,
gint code)
{
return error->domain == G_IO_ERROR && error->code == code;
}
static void
set_message_area_text_and_icon (GeditMessageArea *message_area,
const gchar *icon_stock_id,
......@@ -148,14 +155,16 @@ create_io_loading_error_message_area (const gchar *primary_text,
return message_area;
}
static void
parse_error (const GError *error,
gchar **error_message,
gchar **message_details,
const gchar *uri,
const gchar *uri_for_display)
static gboolean
parse_gio_error (gint code,
gchar **error_message,
gchar **message_details,
const gchar *uri,
const gchar *uri_for_display)
{
switch (error->code)
gboolean ret = TRUE;
switch (code)
{
case G_IO_ERROR_NOT_FOUND:
*error_message = g_strdup_printf (_("Could not find the file %s."),
......@@ -188,10 +197,6 @@ parse_error (const GError *error,
}
break;
case GEDIT_DOCUMENT_ERROR_TOO_BIG:
*message_details = g_strdup (_("The file is too big."));
break;
case G_IO_ERROR_NOT_MOUNTABLE_FILE:
*message_details = g_strdup (_("The location of the file cannot be mounted."));
break;
......@@ -264,15 +269,69 @@ parse_error (const GError *error,
*message_details = g_strdup (_("Connection timed out. Please try again."));
break;
case G_IO_ERROR_FAILED:
default:
ret = FALSE;
break;
}
return ret;
}
static gboolean
parse_gedit_error (gint code,
gchar **error_message,
gchar **message_details,
const gchar *uri,
const gchar *uri_for_display)
{
gboolean ret = TRUE;
switch (code)
{
case GEDIT_DOCUMENT_ERROR_TOO_BIG:
*message_details = g_strdup (_("The file is too big."));
break;
default:
ret = FALSE;
break;
}
return ret;
}
static void
parse_error (const GError *error,
gchar **error_message,
gchar **message_details,
const gchar *uri,
const gchar *uri_for_display)
{
gboolean ret = FALSE;
if (error->domain == G_IO_ERROR)
{
ret = parse_gio_error (error->code,
error_message,
message_details,
uri,
uri_for_display);
}
else if (error->domain == GEDIT_DOCUMENT_ERROR)
{
ret = parse_gedit_error (error->code,
error_message,
message_details,
uri,
uri_for_display);
}
if (!ret)
{
g_warning ("Hit unhandled case %d (%s) in %s.",
error->code, error->message, G_STRFUNC);
*message_details = g_strdup_printf (_("Unexpected error: %s"),
error->message);
break;
}
}
......@@ -304,19 +363,17 @@ gedit_io_loading_error_message_area_new (const gchar *uri,
uri_for_display = g_markup_printf_escaped ("<i>%s</i>", temp_uri_for_display);
g_free (temp_uri_for_display);
switch (error->code)
if (is_gio_error (error, G_IO_ERROR_TOO_MANY_LINKS))
{
case G_IO_ERROR_TOO_MANY_LINKS:
message_details = g_strdup (_("The number of followed links is limited and the actual file could not be found within this limit."));
break;
case G_IO_ERROR_PERMISSION_DENIED:
}
else if (is_gio_error (error, G_IO_ERROR_PERMISSION_DENIED))
{
message_details = g_strdup (_("You do not have the permissions necessary to open the file."));
break;
default:
}
else
{
parse_error (error, &error_message, &message_details, uri, uri_for_display);
break;
}
if (error_message == NULL)
......@@ -369,16 +426,14 @@ gedit_unrecoverable_reverting_error_message_area_new (const gchar *uri,
uri_for_display = g_markup_printf_escaped ("<i>%s</i>", temp_uri_for_display);
g_free (temp_uri_for_display);
switch (error->code)
if (is_gio_error (error, G_IO_ERROR_NOT_FOUND))
{
case G_IO_ERROR_NOT_FOUND:
message_details = g_strdup (_("gedit cannot find the file. "
"Perhaps it has recently been deleted."));
break;
default:
}
else
{
parse_error (error, &error_message, &message_details, uri, uri_for_display);
break;
}
if (error_message == NULL)
......@@ -948,81 +1003,79 @@ gedit_unrecoverable_saving_error_message_area_new (const gchar *uri,
uri_for_display = g_markup_printf_escaped ("<i>%s</i>", temp_uri_for_display);
g_free (temp_uri_for_display);
switch (error->code)
if (is_gio_error (error, G_IO_ERROR_NOT_SUPPORTED))
{
case G_IO_ERROR_NOT_SUPPORTED:
scheme_string = g_uri_parse_scheme (uri);
if ((scheme_string != NULL) && g_utf8_validate (scheme_string, -1, NULL))
{
scheme_markup = g_markup_printf_escaped ("<i>%s:</i>", scheme_string);
/* Translators: %s is a URI scheme (like for example http:, ftp:, etc.) */
message_details = g_strdup_printf (_("gedit cannot handle %s locations in write mode. "
"Please check that you typed the "
"location correctly and try again."),
scheme_markup);
g_free (scheme_markup);
}
else
{
message_details = g_strdup (_("gedit cannot handle this location in write mode. "
"Please check that you typed the "
"location correctly and try again."));
}
g_free (scheme_string);
break;
case GEDIT_DOCUMENT_ERROR_TOO_BIG:
message_details = g_strdup (_("The disk where you are trying to save the file has "
"a limitation on file sizes. Please try saving "
"a smaller file or saving it to a disk that does not "
"have this limitation."));
break;
case G_IO_ERROR_INVALID_FILENAME:
message_details = g_strdup (_("%s is not a valid location. "
"Please check that you typed the "
"location correctly and try again."));
break;
scheme_string = g_uri_parse_scheme (uri);
case G_IO_ERROR_PERMISSION_DENIED:
message_details = g_strdup (_("You do not have the permissions necessary to save the file. "
if ((scheme_string != NULL) && g_utf8_validate (scheme_string, -1, NULL))
{
scheme_markup = g_markup_printf_escaped ("<i>%s:</i>", scheme_string);
/* Translators: %s is a URI scheme (like for example http:, ftp:, etc.) */
message_details = g_strdup_printf (_("gedit cannot handle %s locations in write mode. "
"Please check that you typed the "
"location correctly and try again."),
scheme_markup);
g_free (scheme_markup);
}
else
{
message_details = g_strdup (_("gedit cannot handle this location in write mode. "
"Please check that you typed the "
"location correctly and try again."));
}
break;
case G_IO_ERROR_NO_SPACE:
message_details = g_strdup (_("There is not enough disk space to save the file. "
"Please free some disk space and try again."));
break;
case G_IO_ERROR_READ_ONLY:
message_details = g_strdup (_("You are trying to save the file on a read-only disk. "
"Please check that you typed the location "
"correctly and try again."));
break;
case G_IO_ERROR_EXISTS:
message_details = g_strdup (_("A file with the same name already exists. "
"Please use a different name."));
break;
case G_IO_ERROR_FILENAME_TOO_LONG:
message_details = g_strdup (_("The disk where you are trying to save the file has "
"a limitation on length of the file names. "
"Please use a shorter name."));
break;
default:
parse_error (error,
&error_message,
&message_details,
uri,
uri_for_display);
break;
g_free (scheme_string);
}
else if (is_gio_error (error, G_IO_ERROR_INVALID_FILENAME))
{
message_details = g_strdup (_("%s is not a valid location. "
"Please check that you typed the "
"location correctly and try again."));
}
else if (is_gio_error (error, G_IO_ERROR_PERMISSION_DENIED))
{
message_details = g_strdup (_("You do not have the permissions necessary to save the file. "
"Please check that you typed the "
"location correctly and try again."));
}
else if (is_gio_error (error, G_IO_ERROR_NO_SPACE))
{
message_details = g_strdup (_("There is not enough disk space to save the file. "
"Please free some disk space and try again."));
}
else if (is_gio_error (error, G_IO_ERROR_READ_ONLY))
{
message_details = g_strdup (_("You are trying to save the file on a read-only disk. "
"Please check that you typed the location "
"correctly and try again."));
}
else if (is_gio_error (error, G_IO_ERROR_EXISTS))
{
message_details = g_strdup (_("A file with the same name already exists. "
"Please use a different name."));
}
else if (is_gio_error (error, G_IO_ERROR_FILENAME_TOO_LONG))
{
message_details = g_strdup (_("The disk where you are trying to save the file has "
"a limitation on length of the file names. "
"Please use a shorter name."));
}
else if (error->domain == GEDIT_DOCUMENT_ERROR &&
error->code == GEDIT_DOCUMENT_ERROR_TOO_BIG)
{
message_details = g_strdup (_("The disk where you are trying to save the file has "
"a limitation on file sizes. Please try saving "
"a smaller file or saving it to a disk that does not "
"have this limitation."));
}
else
{
parse_error (error,
&error_message,
&message_details,
uri,
uri_for_display);
}
if (error_message == NULL)
......
......@@ -197,7 +197,7 @@ copy_file_data (gint sfd,
if (bytes_read == -1)
{
g_set_error (error,
GEDIT_DOCUMENT_ERROR,
G_IO_ERROR,
g_io_error_from_errno (errno),
g_strerror (errno));
......@@ -218,7 +218,7 @@ copy_file_data (gint sfd,
continue;
g_set_error (error,
GEDIT_DOCUMENT_ERROR,
G_IO_ERROR,
g_io_error_from_errno (errno),
g_strerror (errno));
......@@ -297,7 +297,7 @@ save_existing_local_file (GeditLocalDocumentSaver *lsaver)
if (fstat (lsaver->priv->fd, &statbuf) != 0)
{
g_set_error (&lsaver->priv->error,
GEDIT_DOCUMENT_ERROR,
G_IO_ERROR,
g_io_error_from_errno (errno),
g_strerror (errno));
......@@ -310,14 +310,14 @@ save_existing_local_file (GeditLocalDocumentSaver *lsaver)
if (S_ISDIR (statbuf.st_mode))
{
g_set_error (&lsaver->priv->error,
GEDIT_DOCUMENT_ERROR,
G_IO_ERROR,
G_IO_ERROR_IS_DIRECTORY,
"Is a directory");
}
else
{
g_set_error (&lsaver->priv->error,
GEDIT_DOCUMENT_ERROR,
G_IO_ERROR,
G_IO_ERROR_NOT_REGULAR_FILE,
"Not a regular file");
}
......@@ -329,7 +329,7 @@ save_existing_local_file (GeditLocalDocumentSaver *lsaver)
if ((statbuf.st_mode & 0222) == 0) //FIXME... check better what else vim does
{
g_set_error (&lsaver->priv->error,
GEDIT_DOCUMENT_ERROR,
G_IO_ERROR,
G_IO_ERROR_READ_ONLY,
"File is read only");
......@@ -360,7 +360,7 @@ save_existing_local_file (GeditLocalDocumentSaver *lsaver)
g_warning (_("Could not obtain backup filename"));
g_set_error (&lsaver->priv->error,
GEDIT_DOCUMENT_ERROR,
G_IO_ERROR,
G_IO_ERROR_FAILED,
"Failure");
......@@ -468,7 +468,7 @@ save_existing_local_file (GeditLocalDocumentSaver *lsaver)
gedit_debug_message (DEBUG_SAVER, "could not rename original -> backup");
g_set_error (&lsaver->priv->error,
GEDIT_DOCUMENT_ERROR,
G_IO_ERROR,
g_io_error_from_errno (errno),
g_strerror (errno));
......@@ -485,7 +485,7 @@ save_existing_local_file (GeditLocalDocumentSaver *lsaver)
gedit_debug_message (DEBUG_SAVER, "could not rename tmp -> original");
g_set_error (&lsaver->priv->error,
GEDIT_DOCUMENT_ERROR,
G_IO_ERROR,
g_io_error_from_errno (errno),
g_strerror (errno));
......@@ -505,7 +505,7 @@ save_existing_local_file (GeditLocalDocumentSaver *lsaver)
if (fstat (tmpfd, &new_statbuf) != 0)
{
g_set_error (&lsaver->priv->error,
GEDIT_DOCUMENT_ERROR,
G_IO_ERROR,
g_io_error_from_errno (errno),
g_strerror (errno));
......@@ -691,7 +691,7 @@ save_existing_local_file (GeditLocalDocumentSaver *lsaver)
if (fstat (lsaver->priv->fd, &new_statbuf) != 0)
{
g_set_error (&lsaver->priv->error,
GEDIT_DOCUMENT_ERROR,
G_IO_ERROR,
g_io_error_from_errno (errno),
g_strerror (errno));
......@@ -707,7 +707,7 @@ save_existing_local_file (GeditLocalDocumentSaver *lsaver)
if (close (lsaver->priv->fd))
g_warning ("File '%s' has not been correctly closed: %s",
saver->uri,
strerror (errno));
g_strerror (errno));
lsaver->priv->fd = -1;
g_free (backup_filename);
......@@ -737,7 +737,7 @@ save_new_local_file (GeditLocalDocumentSaver *lsaver)