1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
|
// SPDX-License-Identifier: GPL-2.0-or-later
/* this file is part of papers, a gnome document viewer
*
* Copyright (C) 2008 Carlos Garcia Campos <carlosgc@gnome.org>
* Copyright (C) 2005 Red Hat, Inc
*/
#include <config.h>
#include "pps-debug.h"
#include "pps-job.h"
#ifdef G_LOG_DOMAIN
#undef G_LOG_DOMAIN
#endif
#define G_LOG_DOMAIN "PpsJobs"
enum {
PROP_0,
PROP_DOCUMENT,
};
enum {
CANCELLED,
FINISHED,
LAST_SIGNAL
};
static guint job_signals[LAST_SIGNAL] = { 0 };
typedef struct _PpsJobPrivate {
PpsDocument *document;
guint finished : 1;
guint failed : 1;
GError *error;
GCancellable *cancellable;
guint idle_finished_id;
guint idle_cancelled_id;
} PpsJobPrivate;
#define GET_PRIVATE(o) pps_job_get_instance_private (o)
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (PpsJob, pps_job, G_TYPE_OBJECT)
static void
pps_job_init (PpsJob *job)
{
PpsJobPrivate *priv = GET_PRIVATE (job);
priv->cancellable = g_cancellable_new ();
}
static void
pps_job_dispose (GObject *object)
{
PpsJob *job = PPS_JOB (object);
PpsJobPrivate *priv = GET_PRIVATE (job);
g_debug ("disposing %s (%p)", PPS_GET_TYPE_NAME (job), job);
g_clear_object (&priv->document);
g_clear_object (&priv->cancellable);
g_clear_error (&priv->error);
(*G_OBJECT_CLASS (pps_job_parent_class)->dispose) (object);
}
static void
pps_job_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
PpsJob *job = PPS_JOB (object);
PpsJobPrivate *priv = GET_PRIVATE (job);
switch (prop_id) {
case PROP_DOCUMENT:
priv->document = g_value_dup_object (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
pps_job_class_init (PpsJobClass *klass)
{
GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
g_object_class->dispose = pps_job_dispose;
g_object_class->set_property = pps_job_set_property;
job_signals[CANCELLED] =
g_signal_new ("cancelled",
PPS_TYPE_JOB,
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (PpsJobClass, cancelled),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
job_signals[FINISHED] =
g_signal_new ("finished",
PPS_TYPE_JOB,
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (PpsJobClass, finished),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
g_object_class_install_property (g_object_class,
PROP_DOCUMENT,
g_param_spec_object ("document",
"Document",
"The document",
PPS_TYPE_DOCUMENT,
G_PARAM_WRITABLE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
}
static void
emit_finished (PpsJob *job)
{
PpsJobPrivate *priv = GET_PRIVATE (job);
g_debug ("emit finished for %s (%p)", PPS_GET_TYPE_NAME (job), job);
priv->idle_finished_id = 0;
if (g_cancellable_is_cancelled (priv->cancellable))
g_debug ("%s (%p) job was cancelled, do not emit finished", PPS_GET_TYPE_NAME (job), job);
else
g_signal_emit (job, job_signals[FINISHED], 0);
g_object_unref (job);
}
static void
pps_job_emit_finished (PpsJob *job)
{
PpsJobPrivate *priv = GET_PRIVATE (job);
if (g_cancellable_is_cancelled (priv->cancellable)) {
g_debug ("%s (%p) job was cancelled, returning", PPS_GET_TYPE_NAME (job), job);
return;
}
priv->finished = TRUE;
priv->idle_finished_id =
g_idle_add_once ((GSourceOnceFunc) emit_finished,
g_object_ref (job));
}
/**
* pps_job_run:
* @job: A #PpsJob object to run.
*
* Starts or executes the specified job.
*/
void
pps_job_run (PpsJob *job)
{
PpsJobClass *class = PPS_JOB_GET_CLASS (job);
PPS_PROFILER_START (PPS_GET_TYPE_NAME (job), g_strdup ("running"));
class->run (job);
PPS_PROFILER_STOP ();
}
/**
* pps_job_cancel:
* @job: A #PpsJob object to cancel.
*
* Cancels the specified job if it has not already been cancelled.
*
* This function updates the job's state to indicate it has been cancelled,
* cancels any associated cancellable object, and emits a CANCELLED signal.
* It does nothing if the job is already cancelled or finished.
*
* This function should not be called from a thread.
*/
void
pps_job_cancel (PpsJob *job)
{
PpsJobPrivate *priv = GET_PRIVATE (job);
if (g_cancellable_is_cancelled (priv->cancellable))
return;
g_debug ("job %s (%p) cancelled", PPS_GET_TYPE_NAME (job), job);
/* This should never be called from a thread */
g_cancellable_cancel (priv->cancellable);
if (priv->finished && priv->idle_finished_id == 0)
return;
g_signal_emit (job, job_signals[CANCELLED], 0);
}
/**
* pps_job_failed:
* @job: A #PpsJob object.
* @domain: The error domain (a #GQuark).
* @code: The error code (an integer).
* @format: A printf-style format string.
*
* Marks the specified job as failed and sets an error with the provided details.
*
* This function creates a new #GError using the provided domain, code, and message,
* and updates the job's state to indicate failure. It emits a finished signal when done.
* It does nothing if the job is already marked as failed or finished.
*/
void
pps_job_failed (PpsJob *job,
GQuark domain,
gint code,
const gchar *format,
...)
{
PpsJobPrivate *priv = GET_PRIVATE (job);
va_list args;
gchar *message;
if (priv->failed || priv->finished)
return;
g_debug ("job %s (%p) failed", PPS_GET_TYPE_NAME (job), job);
priv->failed = TRUE;
va_start (args, format);
message = g_strdup_vprintf (format, args);
va_end (args);
priv->error = g_error_new_literal (domain, code, message);
g_free (message);
pps_job_emit_finished (job);
}
/**
* pps_job_failed_from_error: (rename-to pps_job_failed)
* @job: A #PpsJob object.
* @error: A #GError containing details about the failure.
*
* Marks the specified job as failed and sets the error associated with the failure.
*
* This function updates the job's state to indicate it has failed, stores the error,
* and emits a finished signal. It does nothing if the job is already finished or marked as failed.
*/
void
pps_job_failed_from_error (PpsJob *job,
GError *error)
{
PpsJobPrivate *priv = GET_PRIVATE (job);
if (priv->failed || priv->finished)
return;
g_debug ("job %s (%p) failed", PPS_GET_TYPE_NAME (job), job);
priv->failed = TRUE;
priv->error = g_error_copy (error);
pps_job_emit_finished (job);
}
/**
* pps_job_succeeded:
* @job: A #PpsJob object that has completed.
*
* Marks the specified job as succeeded.
*
* This function should be called when the job finishes successfully.
*/
void
pps_job_succeeded (PpsJob *job)
{
PpsJobPrivate *priv = GET_PRIVATE (job);
if (priv->finished)
return;
g_debug ("job %s (%p) succeeded", PPS_GET_TYPE_NAME (job), job);
priv->failed = FALSE;
pps_job_emit_finished (job);
}
/**
* pps_job_is_finished:
* @job: A #PpsJob object.
*
* Checks if the specified job has finished execution.
*
* Returns: TRUE if the job is finished, FALSE otherwise.
*/
gboolean
pps_job_is_finished (PpsJob *job)
{
PpsJobPrivate *priv = GET_PRIVATE (job);
return priv->finished;
}
/**
* pps_job_is_succeeded:
* @job: A #PpsJob object.
* @error: (nullable) (transfer full): the error to set if the job fails.
*
* Determines the finish status of the specified job.
*
* Returns: TRUE if the job succeeded, FALSE otherwise.
*/
gboolean
pps_job_is_succeeded (PpsJob *job, GError **error)
{
PpsJobPrivate *priv = GET_PRIVATE (job);
g_return_val_if_fail (job != NULL, FALSE);
if (priv->failed && error)
*error = g_error_copy (priv->error);
return !priv->failed;
}
/**
* pps_job_get_document:
* @job: A #PpsJob object.
*
* Retrieves the #PpsDocument associated with the specified job.
*
* Returns: (transfer none): The #PpsDocument object associated with the job,
* or NULL if the job is invalid or does not have an associated document.
*/
PpsDocument *
pps_job_get_document (PpsJob *job)
{
PpsJobPrivate *priv = GET_PRIVATE (job);
g_return_val_if_fail (PPS_IS_JOB (job), NULL);
return priv->document;
}
/**
* pps_job_get_cancellable:
* @job: A #PpsJob object.
*
* Retrieves the #GCancellable object associated with the specified job.
*
* Returns: (transfer none): The #GCancellable object of the job,
* or NULL if the job is invalid or does not have an associated cancellable.
*/
GCancellable *
pps_job_get_cancellable (PpsJob *job)
{
PpsJobPrivate *priv = GET_PRIVATE (job);
g_return_val_if_fail (PPS_IS_JOB (job), NULL);
return priv->cancellable;
}
/**
* pps_job_reset:
* @job: A #PpsJob object to reset.
*
* Resets the state of the specified job to its initial state.
*
* This function clears any error, marks the job as not finished, and sets
* the job's failure status to FALSE, effectively resetting its progress.
*/
void
pps_job_reset (PpsJob *job)
{
PpsJobPrivate *priv = GET_PRIVATE (job);
priv->failed = FALSE;
priv->finished = FALSE;
g_clear_error (&priv->error);
}
|