1
|
1 |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
|
2 |
/* |
|
3 |
* Copyright (C) 2003 Mikael Hallendal <micke@codefactory.se> |
|
4 |
* Copyright (C) 2003 CodeFactory AB. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU Lesser General Public License as |
|
8 |
* published by the Free Software Foundation; either version 2 of the |
|
9 |
* License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 |
* Lesser General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU Lesser General Public |
|
17 |
* License along with this program; if not, write to the |
|
18 |
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
|
19 |
* Boston, MA 02111-1307, USA. |
|
20 |
*/ |
|
21 |
|
|
22 |
#include "lm-internals.h" |
|
23 |
#include "lm-message-handler.h" |
|
24 |
|
|
25 |
struct LmMessageHandler { |
|
26 |
gint ref_count; |
|
27 |
LmHandleMessageFunction function; |
|
28 |
gpointer user_data; |
|
29 |
GDestroyNotify notify; |
|
30 |
}; |
|
31 |
|
|
32 |
LmHandlerResult |
|
33 |
_lm_message_handler_handle_message (LmMessageHandler *handler, |
|
34 |
LmConnection *connection, |
|
35 |
LmMessage *message) |
|
36 |
{ |
|
37 |
g_return_val_if_fail (handler != NULL, |
|
38 |
LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS); |
|
39 |
|
|
40 |
if (handler->function) { |
|
41 |
return (* handler->function) (handler, connection, |
|
42 |
message, handler->user_data); |
|
43 |
} |
|
44 |
|
|
45 |
return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS; |
|
46 |
} |
|
47 |
|
|
48 |
/** |
|
49 |
* lm_message_handler_new: |
|
50 |
* @function: a callback |
|
51 |
* @user_data: user data passed to function |
|
52 |
* @notify: function called when the message handler is freed |
|
53 |
* |
|
54 |
* Creates a new message handler. This can be set to handle incoming messages |
|
55 |
* and when a message of the type the handler is registered to handle is |
|
56 |
* received @function will be called and @user_data will be passed to it. |
|
57 |
* @notify is called when the message handler is freed, that way any memory |
|
58 |
* allocated by @user_data can be freed. |
|
59 |
* |
|
60 |
* Return value: a newly created message handler |
|
61 |
**/ |
|
62 |
LmMessageHandler * |
|
63 |
lm_message_handler_new (LmHandleMessageFunction function, |
|
64 |
gpointer user_data, |
|
65 |
GDestroyNotify notify) |
|
66 |
{ |
|
67 |
LmMessageHandler *handler; |
|
68 |
|
|
69 |
handler = g_new0 (LmMessageHandler, 1); |
|
70 |
|
|
71 |
if (handler == NULL) { |
|
72 |
return NULL; |
|
73 |
} |
|
74 |
|
|
75 |
handler->ref_count = 1; |
|
76 |
handler->function = function; |
|
77 |
handler->user_data = user_data; |
|
78 |
handler->notify = notify; |
|
79 |
|
|
80 |
return handler; |
|
81 |
} |
|
82 |
|
|
83 |
/** |
|
84 |
* lm_message_handler_ref: |
|
85 |
* @handler: an #LmMessageHandler |
|
86 |
* |
|
87 |
* Adds a reference to @node. |
|
88 |
* |
|
89 |
* Return value: the message handler |
|
90 |
**/ |
|
91 |
LmMessageHandler * |
|
92 |
lm_message_handler_ref (LmMessageHandler *handler) |
|
93 |
{ |
|
94 |
g_return_val_if_fail (handler != NULL, NULL); |
|
95 |
|
|
96 |
handler->ref_count++; |
|
97 |
|
|
98 |
return handler; |
|
99 |
} |
|
100 |
|
|
101 |
/** |
|
102 |
* lm_message_handler_unref: |
|
103 |
* @handler: an #LmMessagHandler |
|
104 |
* |
|
105 |
* Removes a reference from @handler. When no more references are present the |
|
106 |
* handler is freed. |
|
107 |
**/ |
|
108 |
void |
|
109 |
lm_message_handler_unref (LmMessageHandler *handler) |
|
110 |
{ |
|
111 |
g_return_if_fail (handler != NULL); |
|
112 |
|
|
113 |
handler->ref_count --; |
|
114 |
|
|
115 |
if (handler->ref_count == 0) { |
|
116 |
if (handler->notify) { |
|
117 |
(* handler->notify) (handler->user_data); |
|
118 |
} |
|
119 |
g_free (handler); |
|
120 |
} |
|
121 |
} |
|
122 |
|