Commit bd3e3ea1 authored by Christian Hergert's avatar Christian Hergert
Browse files

style: use g_autoptr and g_steal_pointer with GError

This changes all our uses of g_task_return_error() to use
g_steal_pointer() and g_autoptr(GError). This is how I want to
see new code written.

It also cleans up things here and there as I came across it.
parent 89ea4331
......@@ -715,7 +715,7 @@ ide_application_get_worker_cb (GObject *object,
{
IdeWorkerManager *worker_manager = (IdeWorkerManager *)object;
g_autoptr(GTask) task = user_data;
GError *error = NULL;
g_autoptr(GError) error = NULL;
GDBusProxy *proxy;
g_assert (IDE_IS_WORKER_MANAGER (worker_manager));
......@@ -723,7 +723,7 @@ ide_application_get_worker_cb (GObject *object,
proxy = ide_worker_manager_get_worker_finish (worker_manager, result, &error);
if (proxy == NULL)
g_task_return_error (task, error);
g_task_return_error (task, g_steal_pointer (&error));
else
g_task_return_pointer (task, proxy, g_object_unref);
}
......
......@@ -586,6 +586,8 @@ ide_buffer_manager_load_file__load_cb (GObject *object,
g_task_return_error (task, g_steal_pointer (&error));
IDE_EXIT;
}
g_clear_error (&error);
}
gtk_text_buffer_set_modified (GTK_TEXT_BUFFER (state->buffer), FALSE);
......@@ -641,8 +643,8 @@ ide_buffer_manager__load_file_query_info_cb (GObject *object,
GFile *file = (GFile *)object;
g_autoptr(GTask) task = user_data;
g_autoptr(GFileInfo) file_info = NULL;
g_autoptr(GError) error = NULL;
LoadState *state;
GError *error = NULL;
gsize size = 0;
gboolean create_new_view = FALSE;
......@@ -660,13 +662,14 @@ ide_buffer_manager__load_file_query_info_cb (GObject *object,
file_info = g_file_query_info_finish (file, result, &error);
if (!file_info)
if (file_info == NULL)
{
if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
{
g_task_return_error (task, error);
g_task_return_error (task, g_steal_pointer (&error));
IDE_EXIT;
}
g_clear_error (&error);
}
else
{
......@@ -988,6 +991,7 @@ ide_buffer_manager_save_file__save_cb (GObject *object,
gpointer user_data)
{
g_autoptr(GTask) task = user_data;
g_autoptr(GError) error = NULL;
GtkSourceFileSaver *saver = (GtkSourceFileSaver *)object;
IdeBufferManager *self;
IdeUnsavedFiles *unsaved_files;
......@@ -997,7 +1001,6 @@ ide_buffer_manager_save_file__save_cb (GObject *object,
GFile *gfile;
GFile *old_gfile;
SaveState *state;
GError *error = NULL;
g_assert (GTK_SOURCE_IS_FILE_SAVER (saver));
g_assert (G_IS_TASK (task));
......@@ -1014,7 +1017,7 @@ ide_buffer_manager_save_file__save_cb (GObject *object,
/* Complete the save request */
if (!gtk_source_file_saver_save_finish (saver, result, &error))
{
g_task_return_error (task, error);
g_task_return_error (task, g_steal_pointer (&error));
return;
}
......@@ -1065,7 +1068,7 @@ ide_buffer_manager_save_file__load_settings_cb (GObject *object,
GtkSourceNewlineType newline_type;
const GtkSourceEncoding *encoding;
const gchar *charset;
GError *error = NULL;
g_autoptr(GError) error = NULL;
IDE_ENTRY;
......@@ -1076,7 +1079,7 @@ ide_buffer_manager_save_file__load_settings_cb (GObject *object,
if (!file_settings)
{
g_task_return_error (task, error);
g_task_return_error (task, g_steal_pointer (&error));
IDE_EXIT;
}
......@@ -2180,6 +2183,7 @@ ide_buffer_manager_apply_edits_buffer_loaded (GObject *object,
g_task_return_error (task, g_steal_pointer (&error));
return;
}
g_clear_error (&error);
}
/* Nothing to do if we already failed */
......
......@@ -233,12 +233,9 @@ ide_unsaved_files_save_worker (GTask *task,
}
if (!g_file_set_contents (manifest_path, manifest->str, manifest->len, &write_error))
{
g_task_return_error (task, write_error);
IDE_EXIT;
}
g_task_return_boolean (task, TRUE);
g_task_return_error (task, g_steal_pointer (&write_error));
else
g_task_return_boolean (task, TRUE);
IDE_EXIT;
}
......
......@@ -61,14 +61,14 @@ ide_buildconfig_configuration_provider_save_cb (GObject *object,
gpointer user_data)
{
g_autoptr(GTask) task = user_data;
GError *error = NULL;
g_autoptr(GError) error = NULL;
GFile *file = (GFile *)object;
g_assert (G_IS_FILE (file));
g_assert (G_IS_ASYNC_RESULT (result));
if (!g_file_replace_contents_finish (file, result, NULL, &error))
g_task_return_error (task, error);
g_task_return_error (task, g_steal_pointer (&error));
else
g_task_return_boolean (task, TRUE);
}
......@@ -85,13 +85,12 @@ ide_buildconfig_configuration_provider_save_async (IdeConfigurationProvider *pro
g_auto(GStrv) groups = NULL;
g_autoptr(GFile) file = NULL;
g_autoptr(GBytes) bytes = NULL;
g_autoptr(GError) error = NULL;
gchar *data;
gsize length;
gsize length = 0;
IdeContext *context;
IdeVcs *vcs;
GFile *workdir;
GError *error = NULL;
guint i;
IDE_ENTRY;
......@@ -130,7 +129,7 @@ ide_buildconfig_configuration_provider_save_async (IdeConfigurationProvider *pro
group_names = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
for (i = 0; i < self->configurations->len; i++)
for (guint i = 0; i < self->configurations->len; i++)
{
IdeConfiguration *configuration = g_ptr_array_index (self->configurations, i);
IdeEnvironment *environment;
......@@ -209,7 +208,7 @@ ide_buildconfig_configuration_provider_save_async (IdeConfigurationProvider *pro
*/
if (NULL != (groups = g_key_file_get_groups (self->key_file, NULL)))
{
for (i = 0; groups [i]; i++)
for (guint i = 0; groups [i]; i++)
{
if (!g_hash_table_contains (group_names, groups [i]))
g_key_file_remove_group (self->key_file, groups [i], NULL);
......@@ -218,7 +217,7 @@ ide_buildconfig_configuration_provider_save_async (IdeConfigurationProvider *pro
if (NULL == (data = g_key_file_to_data (self->key_file, &length, &error)))
{
g_task_return_error (task, error);
g_task_return_error (task, g_steal_pointer (&error));
IDE_EXIT;
}
......@@ -231,7 +230,7 @@ ide_buildconfig_configuration_provider_save_async (IdeConfigurationProvider *pro
G_FILE_CREATE_NONE,
cancellable,
ide_buildconfig_configuration_provider_save_cb,
g_object_ref (task));
g_steal_pointer (&task));
IDE_EXIT;
}
......
......@@ -176,7 +176,7 @@ ide_build_tool_new_context_cb (GObject *object,
IdeBuildManager *build_manager;
IdeBuildPipeline *pipeline;
GCancellable *cancellable;
GError *error = NULL;
g_autoptr(GError) error = NULL;
g_assert (G_IS_TASK (task));
......@@ -186,7 +186,7 @@ ide_build_tool_new_context_cb (GObject *object,
if (context == NULL)
{
g_task_return_error (task, error);
g_task_return_error (task, g_steal_pointer (&error));
return;
}
......@@ -260,8 +260,8 @@ ide_build_tool_run_async (IdeApplicationTool *tool,
g_autoptr(GFile) project_file = NULL;
g_autoptr(GOptionContext) opt_context = NULL;
g_auto(GStrv) strv = NULL;
g_autoptr(GError) error = NULL;
gboolean clean = FALSE;
GError *error = NULL;
const GOptionEntry entries[] = {
{ "clean", 'c', 0, G_OPTION_ARG_NONE, &clean,
N_("Clean the project") },
......@@ -295,7 +295,7 @@ ide_build_tool_run_async (IdeApplicationTool *tool,
if (!g_option_context_parse_strv (opt_context, &strv, &error))
{
g_task_return_error (task, error);
g_task_return_error (task, g_steal_pointer (&error));
return;
}
......
......@@ -162,8 +162,8 @@ ide_directory_vcs_init_worker (GTask *task,
{
IdeDirectoryVcs *self = source_object;
g_autoptr(GFileInfo) file_info = NULL;
g_autoptr(GError) error = NULL;
GFile *file = task_data;
GError *error = NULL;
GFileType file_type;
g_assert (IDE_IS_DIRECTORY_VCS (self));
......@@ -177,7 +177,7 @@ ide_directory_vcs_init_worker (GTask *task,
if (file_info == NULL)
{
g_task_return_error (task, error);
g_task_return_error (task, g_steal_pointer (&error));
return;
}
......
......@@ -323,7 +323,7 @@ ide_editor_workbench_addin_open_cb (GObject *object,
IdeEditorWorkbenchAddin *self;
g_autoptr(IdeBuffer) buffer = NULL;
g_autoptr(GTask) task = user_data;
GError *error = NULL;
g_autoptr(GError) error = NULL;
const gchar *fragment;
OpenFileTaskData *open_file_task_data;
IdeUri *uri;
......@@ -341,7 +341,7 @@ ide_editor_workbench_addin_open_cb (GObject *object,
if (buffer == NULL)
{
IDE_TRACE_MSG ("%s", error->message);
g_task_return_error (task, error);
g_task_return_error (task, g_steal_pointer (&error));
return;
}
......
......@@ -57,10 +57,10 @@ ide_editorconfig_file_settings_init_worker (GTask *task,
GCancellable *cancellable)
{
GFile *file = task_data;
g_autoptr(GError) error = NULL;
GHashTableIter iter;
GHashTable *ht;
gpointer k, v;
GError *error = NULL;
g_assert (G_IS_TASK (task));
g_assert (IDE_IS_EDITORCONFIG_FILE_SETTINGS (source_object));
......@@ -71,7 +71,7 @@ ide_editorconfig_file_settings_init_worker (GTask *task,
if (!ht)
{
g_task_return_error (task, error);
g_task_return_error (task, g_steal_pointer (&error));
return;
}
......
......@@ -216,10 +216,10 @@ ide_language_defaults_init_worker (GTask *task,
g_autofree gchar *version_dir = NULL;
g_autoptr(GBytes) defaults = NULL;
g_autoptr(GKeyFile) key_file = NULL;
g_autoptr(GError) error = NULL;
gint global_version;
gboolean ret;
GError *error = NULL;
gint current_version;
gboolean ret;
IDE_ENTRY;
......@@ -237,7 +237,7 @@ ide_language_defaults_init_worker (GTask *task,
if (current_version < 0)
{
g_task_return_error (task, error);
g_task_return_error (task, g_steal_pointer (&error));
goto failure;
}
......@@ -245,7 +245,7 @@ ide_language_defaults_init_worker (GTask *task,
if (!defaults)
{
g_task_return_error (task, error);
g_task_return_error (task, g_steal_pointer (&error));
goto failure;
}
......@@ -258,7 +258,7 @@ ide_language_defaults_init_worker (GTask *task,
if (!ret)
{
g_task_return_error (task, error);
g_task_return_error (task, g_steal_pointer (&error));
goto failure;
}
......@@ -274,17 +274,19 @@ ide_language_defaults_init_worker (GTask *task,
global_version = g_key_file_get_integer (key_file, "global", "version", &error);
if ((global_version == 0) && error)
if (global_version == 0 && error != NULL)
{
g_task_return_error (task, error);
g_task_return_error (task, g_steal_pointer (&error));
goto failure;
}
g_clear_error (&error);
if (global_version > current_version)
{
if (!ide_language_defaults_migrate (key_file, current_version, global_version, &error))
{
g_task_return_error (task, error);
g_task_return_error (task, g_steal_pointer (&error));
goto failure;
}
......@@ -308,7 +310,7 @@ ide_language_defaults_init_worker (GTask *task,
if (!g_file_set_contents (version_path, version_contents, -1, &error))
{
g_task_return_error (task, error);
g_task_return_error (task, g_steal_pointer (&error));
goto failure;
}
}
......
......@@ -466,8 +466,8 @@ rename_file_free (gpointer data)
if (op != NULL)
{
g_object_unref (op->new_file);
g_object_unref (op->orig_file);
g_clear_object (&op->new_file);
g_clear_object (&op->orig_file);
g_slice_free (RenameFile, op);
}
}
......@@ -475,7 +475,7 @@ rename_file_free (gpointer data)
static gboolean
emit_file_renamed (gpointer data)
{
g_autoptr(GTask) task = data;
GTask *task = data;
IdeProject *project;
RenameFile *rf;
......@@ -510,7 +510,7 @@ ide_project_rename_file_worker (GTask *task,
IdeContext *context;
IdeVcs *vcs;
RenameFile *op = task_data;
GError *error = NULL;
g_autoptr(GError) error = NULL;
GFile *workdir;
g_assert (IDE_IS_PROJECT (self));
......@@ -526,11 +526,9 @@ ide_project_rename_file_worker (GTask *task,
#ifdef IDE_ENABLE_TRACE
{
gchar *old_path = g_file_get_uri (op->orig_file);
gchar *new_path = g_file_get_uri (op->new_file);
g_autofree gchar *old_path = g_file_get_uri (op->orig_file);
g_autofree gchar *new_path = g_file_get_uri (op->new_file);
IDE_TRACE_MSG ("Renaming %s to %s", old_path, new_path);
g_free (old_path);
g_free (new_path);
}
#endif
......@@ -548,7 +546,7 @@ ide_project_rename_file_worker (GTask *task,
if (!g_file_query_exists (parent, cancellable) &&
!g_file_make_directory_with_parents (parent, cancellable, &error))
{
g_task_return_error (task, error);
g_task_return_error (task, g_steal_pointer (&error));
return;
}
......@@ -560,11 +558,14 @@ ide_project_rename_file_worker (GTask *task,
NULL,
&error))
{
g_task_return_error (task, error);
g_task_return_error (task, g_steal_pointer (&error));
return;
}
g_timeout_add (0, emit_file_renamed, g_object_ref (task));
g_idle_add_full (G_PRIORITY_LOW,
emit_file_renamed,
g_object_ref (task),
g_object_unref);
g_task_return_boolean (task, TRUE);
}
......@@ -738,15 +739,12 @@ ide_project_trash_file_async (IdeProject *self,
subprocess = ide_subprocess_launcher_spawn (launcher, cancellable, &error);
if (subprocess == NULL)
{
g_task_return_error (task, g_steal_pointer (&error));
IDE_EXIT;
}
ide_subprocess_wait_check_async (subprocess,
cancellable,
ide_project_trash_file__wait_check_cb,
g_steal_pointer (&task));
g_task_return_error (task, g_steal_pointer (&error));
else
ide_subprocess_wait_check_async (subprocess,
cancellable,
ide_project_trash_file__wait_check_cb,
g_steal_pointer (&task));
IDE_EXIT;
}
......
......@@ -135,7 +135,7 @@ ide_runner_run_wait_cb (GObject *object,
IdeSubprocess *subprocess = (IdeSubprocess *)object;
IdeRunnerPrivate *priv;
g_autoptr(GTask) task = user_data;
GError *error = NULL;
g_autoptr(GError) error = NULL;
IdeRunner *self;
IDE_ENTRY;
......@@ -155,7 +155,7 @@ ide_runner_run_wait_cb (GObject *object,
if (!ide_subprocess_wait_finish (subprocess, result, &error))
{
g_task_return_error (task, error);
g_task_return_error (task, g_steal_pointer (&error));
IDE_EXIT;
}
......@@ -221,7 +221,7 @@ ide_runner_real_run_async (IdeRunner *self,
const gchar *identifier;
IdeContext *context;
IdeRuntime *runtime;
GError *error = NULL;
g_autoptr(GError) error = NULL;
IDE_ENTRY;
......@@ -304,7 +304,7 @@ ide_runner_real_run_async (IdeRunner *self,
if (subprocess == NULL)
{
g_task_return_error (task, error);
g_task_return_error (task, g_steal_pointer (&error));
IDE_GOTO (failure);
}
......@@ -880,18 +880,15 @@ ide_runner_posthook_cb (GObject *object,
{
IdeRunnerAddin *addin = (IdeRunnerAddin *)object;
g_autoptr(GTask) task = user_data;
GError *error = NULL;
g_autoptr(GError) error = NULL;
g_assert (IDE_IS_RUNNER_ADDIN (addin));
g_assert (G_IS_ASYNC_RESULT (result));
if (!ide_runner_addin_posthook_finish (addin, result, &error))
{
g_task_return_error (task, error);
return;
}
ide_runner_tick_posthook (task);
g_task_return_error (task, g_steal_pointer (&error));
else
ide_runner_tick_posthook (task);
}
static void
......@@ -934,12 +931,9 @@ ide_runner_run_cb (GObject *object,
g_assert (G_IS_TASK (task));
if (!IDE_RUNNER_GET_CLASS (self)->run_finish (self, result, &error))
{
g_task_return_error (task, g_steal_pointer (&error));
IDE_EXIT;
}
ide_runner_tick_posthook (task);
g_task_return_error (task, g_steal_pointer (&error));
else
ide_runner_tick_posthook (task);
IDE_EXIT;
}
......@@ -979,12 +973,9 @@ ide_runner_prehook_cb (GObject *object,
g_assert (G_IS_TASK (task));
if (!ide_runner_addin_prehook_finish (addin, result, &error))
{
g_task_return_error (task, g_steal_pointer (&error));
IDE_EXIT;
}
ide_runner_tick_prehook (task);
g_task_return_error (task, g_steal_pointer (&error));
else
ide_runner_tick_prehook (task);
IDE_EXIT;
}
......
......@@ -671,15 +671,14 @@ ide_subprocess_communicate_made_progress (GObject *source_object,
{
CommunicateState *state;
IdeBreakoutSubprocess *subprocess;
GError *error = NULL;
g_autoptr(GError) error = NULL;
g_autoptr(GTask) task = user_data;
gpointer source;
GTask *task;
IDE_ENTRY;
g_assert (source_object != NULL);
task = user_data;
subprocess = g_task_get_source_object (task);
state = g_task_get_task_data (task);
source = source_object;
......@@ -690,18 +689,17 @@ ide_subprocess_communicate_made_progress (GObject *source_object,
source == state->stdout_buf ||
source == state->stderr_buf)
{
if (g_output_stream_splice_finish ((GOutputStream*) source, result, &error) == -1)
if (g_output_stream_splice_finish (source, result, &error) == -1)
goto out;
if (source == state->stdout_buf ||
source == state->stderr_buf)
if (source == state->stdout_buf || source == state->stderr_buf)
{
/* This is a memory stream, so it can't be cancelled or return
* an error really.
*/
if (state->add_nul)
{
gsize bytes_written;
gsize bytes_written = 0;
if (!g_output_stream_write_all (source, "\0", 1, &bytes_written, NULL, &error))
goto out;
}
......@@ -717,7 +715,7 @@ ide_subprocess_communicate_made_progress (GObject *source_object,
g_assert_not_reached ();
out:
if (error)
if (error != NULL)
{
/* Only report the first error we see.
*
......@@ -728,19 +726,14 @@ ide_subprocess_communicate_made_progress (GObject *source_object,
{
state->reported_error = TRUE;
g_cancellable_cancel (state->cancellable);
ide_g_task_return_error_from_main (task, error);
ide_g_task_return_error_from_main (task, g_steal_pointer (&error));
}
else
g_error_free (error);
}
else if (state->outstanding_ops == 0)
{
ide_g_task_return_boolean_from_main (task, TRUE);
}
/* And drop the original ref */
g_object_unref (task);
IDE_EXIT;
}
......
......@@ -69,29 +69,25 @@ ide_template_base_mkdirs_worker (GTask *task,
{