From 907b71635b7a5e929f5770c07db1a45b320fdf57 Mon Sep 17 00:00:00 2001 From: Georges Basile Stavracas Neto Date: Fri, 26 Apr 2019 19:47:24 -0300 Subject: [PATCH] manager: Move threading functions to utils --- src/gcal-manager.c | 88 +---------------------------- src/meson.build | 1 + src/utils/gcal-thread-utils.c | 101 ++++++++++++++++++++++++++++++++++ src/utils/gcal-thread-utils.h | 33 +++++++++++ src/utils/gcal-utils.h | 1 + 5 files changed, 139 insertions(+), 85 deletions(-) create mode 100644 src/utils/gcal-thread-utils.c create mode 100644 src/utils/gcal-thread-utils.h diff --git a/src/gcal-manager.c b/src/gcal-manager.c index c3e79bfb..0d782f24 100644 --- a/src/gcal-manager.c +++ b/src/gcal-manager.c @@ -115,88 +115,6 @@ enum static guint signals[NUM_SIGNALS] = { 0, }; static GParamSpec *properties[NUM_PROPS] = { NULL, }; -/* -- start: threading related code provided by Milan Crha */ -typedef struct { - EThreadJobFunc func; - gpointer user_data; - GDestroyNotify free_user_data; - - GCancellable *cancellable; - GError *error; -} ThreadJobData; - -static void -thread_job_data_free (gpointer ptr) -{ - ThreadJobData *tjd = ptr; - - if (tjd != NULL) - { - /* This should go to UI with more info/description, - * if it is not G_IOI_ERROR_CANCELLED */ - if (tjd->error != NULL && !g_error_matches (tjd->error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) - g_warning ("Job failed: %s\n", tjd->error->message); - - if (tjd->free_user_data != NULL) - tjd->free_user_data (tjd->user_data); - - g_clear_object (&tjd->cancellable); - g_clear_error (&tjd->error); - g_free (tjd); - } -} - -static gpointer -thread_job_thread (gpointer user_data) -{ - ThreadJobData *tjd = user_data; - - g_return_val_if_fail (tjd != NULL, NULL); - - if (tjd->func != NULL) - tjd->func (tjd->user_data, tjd->cancellable, &tjd->error); - - thread_job_data_free (tjd); - - return g_thread_self (); -} - -static GCancellable* -submit_thread_job (EThreadJobFunc func, - gpointer user_data, - GDestroyNotify free_user_data) -{ - ThreadJobData *tjd; - GThread *thread; - GCancellable *cancellable; - - cancellable = g_cancellable_new (); - - tjd = g_new0 (ThreadJobData, 1); - tjd->func = func; - tjd->user_data = user_data; - tjd->free_user_data = free_user_data; - /* user should be able to cancel this cancellable somehow */ - tjd->cancellable = g_object_ref (cancellable); - tjd->error = NULL; - - thread = g_thread_try_new (NULL, - thread_job_thread, tjd, - &tjd->error); - if (thread != NULL) - { - g_thread_unref (thread); - } - else - { - thread_job_data_free (tjd); - g_clear_object (&cancellable); - } - - return cancellable; -} -/* -- end: threading related code provided by Milan Crha -- */ - static void free_async_ops_data (AsyncOpsData *data) { @@ -1027,7 +945,7 @@ gcal_manager_setup_shell_search (GcalManager *self, if (self->shell_search_data_model) return; - self->shell_search_data_model = e_cal_data_model_new (submit_thread_job); + self->shell_search_data_model = e_cal_data_model_new (gcal_thread_submit_job); g_signal_connect_swapped (self->shell_search_data_model, "view-state-changed", G_CALLBACK (model_state_changed), @@ -1902,8 +1820,8 @@ gcal_manager_startup (GcalManager *self) g_signal_connect_swapped (self->source_registry, "source-changed", G_CALLBACK (source_changed), self); /* create data model */ - self->e_data_model = e_cal_data_model_new (submit_thread_job); - self->search_data_model = e_cal_data_model_new (submit_thread_job); + self->e_data_model = e_cal_data_model_new (gcal_thread_submit_job); + self->search_data_model = e_cal_data_model_new (gcal_thread_submit_job); e_cal_data_model_set_expand_recurrences (self->e_data_model, TRUE); e_cal_data_model_set_timezone (self->e_data_model, e_cal_util_get_system_timezone ()); diff --git a/src/meson.build b/src/meson.build index d7ddc6de..eefa81d3 100644 --- a/src/meson.build +++ b/src/meson.build @@ -22,6 +22,7 @@ gcal_deps = [ sources = files( 'search/gcal-search-button.c', 'utils/gcal-date-time-utils.c', + 'utils/gcal-thread-utils.c', 'utils/gcal-utils.c', 'views/gcal-month-cell.c', 'views/gcal-month-popover.c', diff --git a/src/utils/gcal-thread-utils.c b/src/utils/gcal-thread-utils.c new file mode 100644 index 00000000..2a940ab0 --- /dev/null +++ b/src/utils/gcal-thread-utils.c @@ -0,0 +1,101 @@ +/* gcal-thread-utils.c + * + * Copyright 2019 Georges Basile Stavracas Neto + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include "gcal-thread-utils.h" + +/* Code kindly provided by Milan Crha */ + +typedef struct { + EThreadJobFunc func; + gpointer user_data; + GDestroyNotify free_user_data; + + GCancellable *cancellable; + GError *error; +} ThreadJobData; + +static void +thread_job_data_free (gpointer ptr) +{ + ThreadJobData *tjd = ptr; + + if (tjd != NULL) + { + /* This should go to UI with more info/description, + * if it is not G_IOI_ERROR_CANCELLED */ + if (tjd->error != NULL && !g_error_matches (tjd->error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) + g_warning ("Job failed: %s\n", tjd->error->message); + + if (tjd->free_user_data != NULL) + tjd->free_user_data (tjd->user_data); + + g_clear_object (&tjd->cancellable); + g_clear_error (&tjd->error); + g_free (tjd); + } +} + +static gpointer +thread_job_thread (gpointer user_data) +{ + ThreadJobData *tjd = user_data; + + g_return_val_if_fail (tjd != NULL, NULL); + + if (tjd->func != NULL) + tjd->func (tjd->user_data, tjd->cancellable, &tjd->error); + + thread_job_data_free (tjd); + + return g_thread_self (); +} + +GCancellable* +gcal_thread_submit_job (EThreadJobFunc func, + gpointer user_data, + GDestroyNotify free_user_data) +{ + ThreadJobData *tjd; + GThread *thread; + GCancellable *cancellable; + + cancellable = g_cancellable_new (); + + tjd = g_new0 (ThreadJobData, 1); + tjd->func = func; + tjd->user_data = user_data; + tjd->free_user_data = free_user_data; + /* user should be able to cancel this cancellable somehow */ + tjd->cancellable = g_object_ref (cancellable); + tjd->error = NULL; + + thread = g_thread_try_new (NULL, thread_job_thread, tjd, &tjd->error); + if (thread != NULL) + { + g_thread_unref (thread); + } + else + { + thread_job_data_free (tjd); + g_clear_object (&cancellable); + } + + return cancellable; +} diff --git a/src/utils/gcal-thread-utils.h b/src/utils/gcal-thread-utils.h new file mode 100644 index 00000000..646b7885 --- /dev/null +++ b/src/utils/gcal-thread-utils.h @@ -0,0 +1,33 @@ +/* gcal-thread-utils.h + * + * Copyright 2019 Georges Basile Stavracas Neto + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#pragma once + +#include "e-cal-data-model.h" + +#include + +G_BEGIN_DECLS + +GCancellable* gcal_thread_submit_job (EThreadJobFunc func, + gpointer user_data, + GDestroyNotify free_user_data); + +G_END_DECLS diff --git a/src/utils/gcal-utils.h b/src/utils/gcal-utils.h index fb831430..5f8427bf 100644 --- a/src/utils/gcal-utils.h +++ b/src/utils/gcal-utils.h @@ -20,6 +20,7 @@ #define __GCAL_UTILS_H__ #include "gcal-date-time-utils.h" +#include "gcal-thread-utils.h" #include "gcal-manager.h" #include