4 * Purple is the legal property of its developers, whose names are too numerous
5 * to list here. Please refer to the COPYRIGHT file distributed with this
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program 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
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
25 #include "glibcompat.h"
26 #include "whiteboard.h"
29 typedef struct _PurpleWhiteboardPrivate PurpleWhiteboardPrivate
;
31 /* Private data for a whiteboard */
32 struct _PurpleWhiteboardPrivate
34 int state
; /* State of whiteboard session */
36 PurpleAccount
*account
; /* Account associated with this session */
37 char *who
; /* Name of the remote user */
39 /* TODO Remove this and use protocol-specific subclasses. */
40 void *proto_data
; /* Protocol specific data */
42 PurpleWhiteboardOps
*protocol_ops
; /* Protocol operations */
44 GList
*draw_list
; /* List of drawing elements/deltas to
48 /* GObject Property enums */
59 /******************************************************************************
61 *****************************************************************************/
62 static GParamSpec
*properties
[PROP_LAST
];
64 G_DEFINE_TYPE_WITH_PRIVATE(PurpleWhiteboard
, purple_whiteboard
, G_TYPE_OBJECT
);
66 static PurpleWhiteboardUiOps
*whiteboard_ui_ops
= NULL
;
67 /* static PurpleWhiteboardOps *whiteboard_protocol_ops = NULL; */
69 static GList
*wb_list
= NULL
;
71 /*static gboolean auto_accept = TRUE; */
73 /******************************************************************************
75 *****************************************************************************/
76 static PurpleWhiteboardUiOps
*
77 purple_whiteboard_ui_ops_copy(PurpleWhiteboardUiOps
*ops
)
79 PurpleWhiteboardUiOps
*ops_new
;
81 g_return_val_if_fail(ops
!= NULL
, NULL
);
83 ops_new
= g_new(PurpleWhiteboardUiOps
, 1);
90 purple_whiteboard_ui_ops_get_type(void)
92 static GType type
= 0;
95 type
= g_boxed_type_register_static("PurpleWhiteboardUiOps",
96 (GBoxedCopyFunc
)purple_whiteboard_ui_ops_copy
,
97 (GBoxedFreeFunc
)g_free
);
103 void purple_whiteboard_set_ui_ops(PurpleWhiteboardUiOps
*ops
)
105 whiteboard_ui_ops
= ops
;
108 void purple_whiteboard_set_protocol_ops(PurpleWhiteboard
*wb
, PurpleWhiteboardOps
*ops
)
110 PurpleWhiteboardPrivate
*priv
= NULL
;
112 g_return_if_fail(PURPLE_IS_WHITEBOARD(wb
));
114 priv
= purple_whiteboard_get_instance_private(wb
);
115 priv
->protocol_ops
= ops
;
118 PurpleAccount
*purple_whiteboard_get_account(PurpleWhiteboard
*wb
)
120 PurpleWhiteboardPrivate
*priv
= NULL
;
122 g_return_val_if_fail(PURPLE_IS_WHITEBOARD(wb
), NULL
);
124 priv
= purple_whiteboard_get_instance_private(wb
);
125 return priv
->account
;
128 const char *purple_whiteboard_get_who(PurpleWhiteboard
*wb
)
130 PurpleWhiteboardPrivate
*priv
= NULL
;
132 g_return_val_if_fail(PURPLE_IS_WHITEBOARD(wb
), NULL
);
134 priv
= purple_whiteboard_get_instance_private(wb
);
138 void purple_whiteboard_set_state(PurpleWhiteboard
*wb
, int state
)
140 PurpleWhiteboardPrivate
*priv
= NULL
;
142 g_return_if_fail(PURPLE_IS_WHITEBOARD(wb
));
144 priv
= purple_whiteboard_get_instance_private(wb
);
147 g_object_notify_by_pspec(G_OBJECT(wb
), properties
[PROP_STATE
]);
150 int purple_whiteboard_get_state(PurpleWhiteboard
*wb
)
152 PurpleWhiteboardPrivate
*priv
= NULL
;
154 g_return_val_if_fail(PURPLE_IS_WHITEBOARD(wb
), -1);
156 priv
= purple_whiteboard_get_instance_private(wb
);
160 void purple_whiteboard_start(PurpleWhiteboard
*wb
)
162 /* Create frontend for whiteboard */
163 if(whiteboard_ui_ops
&& whiteboard_ui_ops
->create
)
164 whiteboard_ui_ops
->create(wb
);
167 /* Looks through the list of whiteboard sessions for one that is between
168 * usernames 'me' and 'who'. Returns a pointer to a matching whiteboard
169 * session; if none match, it returns NULL.
171 PurpleWhiteboard
*purple_whiteboard_get_session(const PurpleAccount
*account
, const char *who
)
173 PurpleWhiteboard
*wb
;
174 PurpleWhiteboardPrivate
*priv
;
178 /* Look for a whiteboard session between the local user and the remote user
183 priv
= purple_whiteboard_get_instance_private(wb
);
185 if(priv
->account
== account
&& purple_strequal(priv
->who
, who
))
194 void purple_whiteboard_draw_list_destroy(GList
*draw_list
)
196 g_list_free(draw_list
);
199 gboolean
purple_whiteboard_get_dimensions(PurpleWhiteboard
*wb
, int *width
, int *height
)
201 PurpleWhiteboardPrivate
*priv
= NULL
;
202 PurpleWhiteboardOps
*protocol_ops
;
204 g_return_val_if_fail(PURPLE_IS_WHITEBOARD(wb
), FALSE
);
206 priv
= purple_whiteboard_get_instance_private(wb
);
207 protocol_ops
= priv
->protocol_ops
;
209 if (protocol_ops
&& protocol_ops
->get_dimensions
)
211 protocol_ops
->get_dimensions(wb
, width
, height
);
218 void purple_whiteboard_set_dimensions(PurpleWhiteboard
*wb
, int width
, int height
)
220 if(whiteboard_ui_ops
&& whiteboard_ui_ops
->set_dimensions
)
221 whiteboard_ui_ops
->set_dimensions(wb
, width
, height
);
224 void purple_whiteboard_send_draw_list(PurpleWhiteboard
*wb
, GList
*list
)
226 PurpleWhiteboardPrivate
*priv
= NULL
;
227 PurpleWhiteboardOps
*protocol_ops
;
229 g_return_if_fail(PURPLE_IS_WHITEBOARD(wb
));
231 priv
= purple_whiteboard_get_instance_private(wb
);
232 protocol_ops
= priv
->protocol_ops
;
234 if (protocol_ops
&& protocol_ops
->send_draw_list
)
235 protocol_ops
->send_draw_list(wb
, list
);
238 void purple_whiteboard_draw_point(PurpleWhiteboard
*wb
, int x
, int y
, int color
, int size
)
240 if(whiteboard_ui_ops
&& whiteboard_ui_ops
->draw_point
)
241 whiteboard_ui_ops
->draw_point(wb
, x
, y
, color
, size
);
244 void purple_whiteboard_draw_line(PurpleWhiteboard
*wb
, int x1
, int y1
, int x2
, int y2
, int color
, int size
)
246 if(whiteboard_ui_ops
&& whiteboard_ui_ops
->draw_line
)
247 whiteboard_ui_ops
->draw_line(wb
, x1
, y1
, x2
, y2
, color
, size
);
250 void purple_whiteboard_clear(PurpleWhiteboard
*wb
)
252 if(whiteboard_ui_ops
&& whiteboard_ui_ops
->clear
)
253 whiteboard_ui_ops
->clear(wb
);
256 void purple_whiteboard_send_clear(PurpleWhiteboard
*wb
)
258 PurpleWhiteboardPrivate
*priv
= NULL
;
259 PurpleWhiteboardOps
*protocol_ops
;
261 g_return_if_fail(PURPLE_IS_WHITEBOARD(wb
));
263 priv
= purple_whiteboard_get_instance_private(wb
);
264 protocol_ops
= priv
->protocol_ops
;
266 if (protocol_ops
&& protocol_ops
->clear
)
267 protocol_ops
->clear(wb
);
270 void purple_whiteboard_send_brush(PurpleWhiteboard
*wb
, int size
, int color
)
272 PurpleWhiteboardPrivate
*priv
= NULL
;
273 PurpleWhiteboardOps
*protocol_ops
;
275 g_return_if_fail(PURPLE_IS_WHITEBOARD(wb
));
277 priv
= purple_whiteboard_get_instance_private(wb
);
278 protocol_ops
= priv
->protocol_ops
;
280 if (protocol_ops
&& protocol_ops
->set_brush
)
281 protocol_ops
->set_brush(wb
, size
, color
);
284 gboolean
purple_whiteboard_get_brush(PurpleWhiteboard
*wb
, int *size
, int *color
)
286 PurpleWhiteboardPrivate
*priv
= NULL
;
287 PurpleWhiteboardOps
*protocol_ops
;
289 g_return_val_if_fail(PURPLE_IS_WHITEBOARD(wb
), FALSE
);
291 priv
= purple_whiteboard_get_instance_private(wb
);
292 protocol_ops
= priv
->protocol_ops
;
294 if (protocol_ops
&& protocol_ops
->get_brush
)
296 protocol_ops
->get_brush(wb
, size
, color
);
302 void purple_whiteboard_set_brush(PurpleWhiteboard
*wb
, int size
, int color
)
304 if (whiteboard_ui_ops
&& whiteboard_ui_ops
->set_brush
)
305 whiteboard_ui_ops
->set_brush(wb
, size
, color
);
308 GList
*purple_whiteboard_get_draw_list(PurpleWhiteboard
*wb
)
310 PurpleWhiteboardPrivate
*priv
= NULL
;
312 g_return_val_if_fail(PURPLE_IS_WHITEBOARD(wb
), NULL
);
314 priv
= purple_whiteboard_get_instance_private(wb
);
315 return priv
->draw_list
;
318 void purple_whiteboard_set_draw_list(PurpleWhiteboard
*wb
, GList
* draw_list
)
320 PurpleWhiteboardPrivate
*priv
= NULL
;
322 g_return_if_fail(PURPLE_IS_WHITEBOARD(wb
));
324 priv
= purple_whiteboard_get_instance_private(wb
);
325 priv
->draw_list
= draw_list
;
327 g_object_notify_by_pspec(G_OBJECT(wb
), properties
[PROP_DRAW_LIST
]);
330 void purple_whiteboard_set_protocol_data(PurpleWhiteboard
*wb
, gpointer proto_data
)
332 PurpleWhiteboardPrivate
*priv
= NULL
;
334 g_return_if_fail(PURPLE_IS_WHITEBOARD(wb
));
336 priv
= purple_whiteboard_get_instance_private(wb
);
337 priv
->proto_data
= proto_data
;
340 gpointer
purple_whiteboard_get_protocol_data(PurpleWhiteboard
*wb
)
342 PurpleWhiteboardPrivate
*priv
= NULL
;
344 g_return_val_if_fail(PURPLE_IS_WHITEBOARD(wb
), NULL
);
346 priv
= purple_whiteboard_get_instance_private(wb
);
347 return priv
->proto_data
;
350 void purple_whiteboard_set_ui_data(PurpleWhiteboard
*wb
, gpointer ui_data
)
352 g_return_if_fail(PURPLE_IS_WHITEBOARD(wb
));
354 wb
->ui_data
= ui_data
;
357 gpointer
purple_whiteboard_get_ui_data(PurpleWhiteboard
*wb
)
359 g_return_val_if_fail(PURPLE_IS_WHITEBOARD(wb
), NULL
);
364 /******************************************************************************
366 *****************************************************************************/
367 /* Set method for GObject properties */
369 purple_whiteboard_set_property(GObject
*obj
, guint param_id
, const GValue
*value
,
372 PurpleWhiteboard
*wb
= PURPLE_WHITEBOARD(obj
);
373 PurpleWhiteboardPrivate
*priv
=
374 purple_whiteboard_get_instance_private(wb
);
378 purple_whiteboard_set_state(wb
, g_value_get_int(value
));
381 priv
->account
= g_value_get_object(value
);
384 priv
->who
= g_value_dup_string(value
);
387 purple_whiteboard_set_draw_list(wb
, g_value_get_pointer(value
));
390 G_OBJECT_WARN_INVALID_PROPERTY_ID(obj
, param_id
, pspec
);
395 /* Get method for GObject properties */
397 purple_whiteboard_get_property(GObject
*obj
, guint param_id
, GValue
*value
,
400 PurpleWhiteboard
*wb
= PURPLE_WHITEBOARD(obj
);
404 g_value_set_int(value
, purple_whiteboard_get_state(wb
));
407 g_value_set_object(value
, purple_whiteboard_get_account(wb
));
410 g_value_set_string(value
, purple_whiteboard_get_who(wb
));
413 g_value_set_pointer(value
, purple_whiteboard_get_draw_list(wb
));
416 G_OBJECT_WARN_INVALID_PROPERTY_ID(obj
, param_id
, pspec
);
422 purple_whiteboard_init(PurpleWhiteboard
*wb
)
426 /* Called when done constructing */
428 purple_whiteboard_constructed(GObject
*object
)
430 PurpleWhiteboard
*wb
= PURPLE_WHITEBOARD(object
);
431 PurpleWhiteboardPrivate
*priv
=
432 purple_whiteboard_get_instance_private(wb
);
433 PurpleProtocol
*protocol
;
435 G_OBJECT_CLASS(purple_whiteboard_parent_class
)->constructed(object
);
437 protocol
= purple_connection_get_protocol(
438 purple_account_get_connection(priv
->account
));
439 purple_whiteboard_set_protocol_ops(wb
,
440 purple_protocol_get_whiteboard_ops(protocol
));
442 /* Start up protocol specifics */
443 if(priv
->protocol_ops
&& priv
->protocol_ops
->start
)
444 priv
->protocol_ops
->start(wb
);
446 wb_list
= g_list_append(wb_list
, wb
);
449 /* GObject finalize function */
451 purple_whiteboard_finalize(GObject
*object
)
453 PurpleWhiteboard
*wb
= PURPLE_WHITEBOARD(object
);
454 PurpleWhiteboardPrivate
*priv
=
455 purple_whiteboard_get_instance_private(wb
);
459 /* Destroy frontend */
460 if(whiteboard_ui_ops
&& whiteboard_ui_ops
->destroy
)
461 whiteboard_ui_ops
->destroy(wb
);
464 /* Do protocol specific session ending procedures */
465 if(priv
->protocol_ops
&& priv
->protocol_ops
->end
)
466 priv
->protocol_ops
->end(wb
);
468 wb_list
= g_list_remove(wb_list
, wb
);
472 G_OBJECT_CLASS(purple_whiteboard_parent_class
)->finalize(object
);
475 /* Class initializer function */
477 purple_whiteboard_class_init(PurpleWhiteboardClass
*klass
)
479 GObjectClass
*obj_class
= G_OBJECT_CLASS(klass
);
481 obj_class
->finalize
= purple_whiteboard_finalize
;
482 obj_class
->constructed
= purple_whiteboard_constructed
;
484 /* Setup properties */
485 obj_class
->get_property
= purple_whiteboard_get_property
;
486 obj_class
->set_property
= purple_whiteboard_set_property
;
488 properties
[PROP_STATE
] = g_param_spec_int("state", "State",
489 "State of the whiteboard.",
490 G_MININT
, G_MAXINT
, 0,
491 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT
| G_PARAM_STATIC_STRINGS
);
493 properties
[PROP_ACCOUNT
] = g_param_spec_object("account", "Account",
494 "The whiteboard's account.", PURPLE_TYPE_ACCOUNT
,
495 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT_ONLY
|
496 G_PARAM_STATIC_STRINGS
);
498 properties
[PROP_WHO
] = g_param_spec_string("who", "Who",
499 "Who you're drawing with.", NULL
,
500 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT_ONLY
|
501 G_PARAM_STATIC_STRINGS
);
503 properties
[PROP_DRAW_LIST
] = g_param_spec_pointer("draw-list", "Draw list",
504 "A list of points to draw to the buddy.",
505 G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
);
507 g_object_class_install_properties(obj_class
, PROP_LAST
, properties
);
510 PurpleWhiteboard
*purple_whiteboard_new(PurpleAccount
*account
, const char *who
, int state
)
512 PurpleWhiteboard
*wb
;
513 PurpleProtocol
*protocol
;
515 g_return_val_if_fail(PURPLE_IS_ACCOUNT(account
), NULL
);
516 g_return_val_if_fail(who
!= NULL
, NULL
);
518 protocol
= purple_protocols_find(purple_account_get_protocol_id(account
));
520 g_return_val_if_fail(PURPLE_IS_PROTOCOL(protocol
), NULL
);
522 if (PURPLE_PROTOCOL_IMPLEMENTS(protocol
, FACTORY
, whiteboard_new
))
523 wb
= purple_protocol_factory_iface_whiteboard_new(protocol
, account
,
526 wb
= g_object_new(PURPLE_TYPE_WHITEBOARD
,
533 g_return_val_if_fail(wb
!= NULL
, NULL
);