Debian Codegraph @

src/job/fm-simple-job.c

raw
1 /*
2 * fm-simple-job.c
3 *
4 * Copyright 2010 Hong Jen Yee (PCMan) <[email protected]>
5 *
6 * This file is a part of the Libfm library.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * SECTION:fm-simple-job
25 * @short_description: Job to run a function asynchronously.
26 * @title: FmSimpleJob
27 *
28 * @include: libfm/fm.h
29 *
30 * The #FmJob can be used to create asynchronous job which just run some
31 * simple function with provided data.
32 */
33
34 #include "fm-simple-job.h"
35
36 #define FM_TYPE_SIMPLE_JOB (fm_simple_job_get_type())
37 #define FM_SIMPLE_JOB(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),\
38 FM_TYPE_SIMPLE_JOB, FmSimpleJob))
39 #define FM_SIMPLE_JOB_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),\
40 FM_TYPE_SIMPLE_JOB, FmSimpleJobClass))
41 #define FM_IS_SIMPLE_JOB(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),\
42 FM_TYPE_SIMPLE_JOB))
43 #define FM_IS_SIMPLE_JOB_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),\
44 FM_TYPE_SIMPLE_JOB))
45
46 typedef struct _FmSimpleJob FmSimpleJob;
47 typedef struct _FmSimpleJobClass FmSimpleJobClass;
48
49 struct _FmSimpleJob
50 {
51 FmJob parent;
52 FmSimpleJobFunc func;
53 gpointer user_data;
54 GDestroyNotify destroy_data;
55 };
56
57 struct _FmSimpleJobClass
58 {
59 FmJobClass parent_class;
60 };
61
62 G_DEFINE_TYPE(FmSimpleJob, fm_simple_job, FM_TYPE_JOB);
63
64 static void fm_simple_job_finalize (GObject *object);
65 static gboolean fm_simple_job_run(FmJob *job);
66
67
68 static void fm_simple_job_class_init(FmSimpleJobClass *klass)
69 {
70 GObjectClass *g_object_class;
71 FmJobClass* job_class = FM_JOB_CLASS(klass);
72 g_object_class = G_OBJECT_CLASS(klass);
73 g_object_class->finalize = fm_simple_job_finalize;
74
75 job_class->run = fm_simple_job_run;
76 }
77
78
79 static void fm_simple_job_finalize(GObject *object)
80 {
81 FmSimpleJob *self;
82
83 g_return_if_fail(object != NULL);
84 g_return_if_fail(FM_IS_SIMPLE_JOB(object));
85
86 self = (FmSimpleJob*)object;
87 if(self->user_data && self->destroy_data)
88 self->destroy_data(self->user_data);
89
90 G_OBJECT_CLASS(fm_simple_job_parent_class)->finalize(object);
91 }
92
93
94 static void fm_simple_job_init(FmSimpleJob *self)
95 {
96
97 }
98
99 /**
100 * fm_simple_job_new
101 * @func: user function to run asynchronously
102 * @user_data: user data provided for @func
103 * @destroy_data: user function to free data after job finished
104 *
105 * Creates a new simple #FmJob for user task.
106 *
107 * Returns: (transfer full): a new #FmJob object.
108 *
109 * Since: 0.1.0
110 */
111 FmJob* fm_simple_job_new(FmSimpleJobFunc func, gpointer user_data, GDestroyNotify destroy_data)
112 {
113 FmSimpleJob* job = (FmSimpleJob*)g_object_new(FM_TYPE_SIMPLE_JOB, NULL);
114 job->func = func;
115 job->user_data = user_data;
116 job->destroy_data = destroy_data;
117 return (FmJob*)job;
118 }
119
120 static gboolean fm_simple_job_run(FmJob *job)
121 {
122 FmSimpleJob* sjob = FM_SIMPLE_JOB(job);
123 return sjob->func ? sjob->func(job, sjob->user_data) : FALSE;
124 }