1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3  * anjuta
4  * Copyright (C) James Liggett 2008 <jrliggett@cox.net>
5  *
6  * anjuta is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * anjuta is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "anjuta-async-notify.h"
21 
22 /**
23  * SECTION: anjuta-async-notify
24  * @short_description: Mechanism used by interfaces that run asynchronously to
25  *					   notify clients of finished tasks and to report errors.
26  *
27  * #AnjutaAsyncNotify is a way to allow Anjuta interfaces that run
28  * asynchronously, such as #IAnjutaVCS, to notify clients that a method has
29  * completed. #AnjutaAsyncNotify also reports errors to the user.
30  *
31  * All clients need to do is create an instance of #AnjutaAsyncNotify, connect
32  * to the finished signal, and pass it in to the interface method to be called.
33  */
34 
35 enum
36 {
37 	FINISHED,
38 
39 	LAST_SIGNAL
40 };
41 
42 
43 static guint async_notify_signals[LAST_SIGNAL] = { 0 };
44 
45 struct _AnjutaAsyncNotifyPriv
46 {
47 	GError *error;
48 };
49 
50 G_DEFINE_TYPE (AnjutaAsyncNotify, anjuta_async_notify, G_TYPE_OBJECT);
51 
52 static void
anjuta_async_notify_init(AnjutaAsyncNotify * self)53 anjuta_async_notify_init (AnjutaAsyncNotify *self)
54 {
55 	self->priv = g_new0 (AnjutaAsyncNotifyPriv, 1);
56 }
57 
58 static void
anjuta_async_notify_finalize(GObject * object)59 anjuta_async_notify_finalize (GObject *object)
60 {
61 	AnjutaAsyncNotify *self;
62 
63 	self = ANJUTA_ASYNC_NOTIFY (object);
64 
65 	g_error_free (self->priv->error);
66 	g_free (self->priv);
67 
68 	G_OBJECT_CLASS (anjuta_async_notify_parent_class)->finalize (object);
69 }
70 
71 static void
anjuta_async_notify_finished(AnjutaAsyncNotify * self)72 anjuta_async_notify_finished (AnjutaAsyncNotify *self)
73 {
74 
75 }
76 
77 static void
anjuta_async_notify_class_init(AnjutaAsyncNotifyClass * klass)78 anjuta_async_notify_class_init (AnjutaAsyncNotifyClass *klass)
79 {
80 	GObjectClass* object_class = G_OBJECT_CLASS (klass);
81 
82 	object_class->finalize = anjuta_async_notify_finalize;
83 
84 	klass->finished = anjuta_async_notify_finished;
85 
86 	/**
87 	 * AnjutaAsyncNotify::finished:
88 	 * @notify: AnjutaAsyncNotify object.
89 	 *
90 	 * Notifies clients that the requested task has finished.
91 	 */
92 	async_notify_signals[FINISHED] =
93 		g_signal_new ("finished",
94 		              G_OBJECT_CLASS_TYPE (klass),
95 		              0,
96 		              G_STRUCT_OFFSET (AnjutaAsyncNotifyClass, finished),
97 		              NULL, NULL,
98 		              g_cclosure_marshal_VOID__VOID,
99 		              G_TYPE_NONE, 0,
100 		              NULL);
101 }
102 
103 /**
104  * anjuta_async_notify_new:
105  *
106  * Creates a new #AnjutaAsyncNotify object.
107  *
108  * Return value: a new #AnjutaAsyncNotify instance
109  */
110 AnjutaAsyncNotify *
anjuta_async_notify_new(void)111 anjuta_async_notify_new (void)
112 {
113 	return g_object_new (ANJUTA_TYPE_ASYNC_NOTIFY, NULL);
114 }
115 
116 /**
117  * anjuta_async_notify_get_error:
118  * @self: An #AnjutaAsyncNotify object
119  * @error: Return location for the error set by the called interface to which
120  *		   this object was passed. If no error is set, @error is set to NULL.
121  *
122  * Gets the error set on @self.
123  */
124 void
anjuta_async_notify_get_error(AnjutaAsyncNotify * self,GError ** error)125 anjuta_async_notify_get_error (AnjutaAsyncNotify *self, GError **error)
126 {
127 	if (self->priv->error)
128 		*error = g_error_copy (self->priv->error);
129 }
130 
131 /**
132  * anjuta_async_notify_set_error:
133  * @self: An #AnjutaAsyncNotify object
134  * @error: Error to set
135  *
136  * Sets the error for an interface call. This method should only be used by
137  * interface implementations themselves, not by clients.
138  */
139 void
anjuta_async_notify_set_error(AnjutaAsyncNotify * self,GError * error)140 anjuta_async_notify_set_error (AnjutaAsyncNotify *self, GError *error)
141 {
142 	if (self->priv->error)
143 		g_error_free (self->priv->error);
144 
145 	self->priv->error = g_error_copy (error);
146 }
147 
148 /**
149  * anjuta_async_notify_notify_finished:
150  * @self: An #AnjutaAsyncNotify object
151  *
152  * Emits the finished signal. This method should only be used by
153  * interface methods themselves, not by clients.
154  */
155 void
anjuta_async_notify_notify_finished(AnjutaAsyncNotify * self)156 anjuta_async_notify_notify_finished (AnjutaAsyncNotify *self)
157 {
158 	g_signal_emit_by_name (self, "finished", NULL);
159 }
160