2 * events.c -- Events fonctions
4 * Copyright (C) 2006-2009 Mikael Berthe <mikael@lilotux.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
11 * This program 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. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
27 static GSList
*evs_list
; // Events list
29 static eviqs
*evs_find(const char *evid
);
31 // evs_new(type, timeout)
32 // Create an events structure.
33 eviqs
*evs_new(guint8 type
, time_t timeout
)
42 /* Check for wrapping, we shouldn't reuse ids */
43 stridn
= g_strdup_printf("%d", evs_idn
);
44 if (evs_find(stridn
)) {
46 // We could try another id but for now giving up should be fine...
50 new_evs
= g_new0(eviqs
, 1);
52 new_evs
->ts_create
= now_t
;
54 new_evs
->ts_expire
= now_t
+ timeout
;
58 if(!g_slist_length(evs_list
))
59 g_timeout_add_seconds(20, evs_check_timeout
, NULL
);
60 evs_list
= g_slist_append(evs_list
, new_evs
);
64 int evs_del(const char *evid
)
71 for (p
= evs_list
; p
; p
= g_slist_next(p
)) {
73 if (!strcmp(evid
, i
->id
))
81 evs_list
= g_slist_remove(evs_list
, p
->data
);
82 return 0; // Ok, deleted
84 return -1; // Not found
87 static eviqs
*evs_find(const char *evid
)
92 if (!evid
) return NULL
;
94 for (p
= evs_list
; p
; p
= g_slist_next(p
)) {
96 if (!strcmp(evid
, i
->id
))
102 // evs_callback(evid, evcontext)
103 // Callback processing for the specified event.
104 // Return 0 in case of success, -1 if the evid hasn't been found.
105 int evs_callback(const char *evid
, guint evcontext
)
113 // Note: If xml_result is NULL, this is a timeout
115 (void)(*i
->callback
)(i
, evcontext
);
121 gboolean
evs_check_timeout()
133 // We must get next IQ eviqs element now because the current one
137 if ((!i
->ts_expire
&& now_t
> i
->ts_create
+ EVS_MAX_TIMEOUT
) ||
138 (i
->ts_expire
&& now_t
> i
->ts_expire
)) {
139 evs_callback(i
->id
, EVS_CONTEXT_TIMEOUT
);
145 void evs_display_list(void)
150 scr_LogPrint(LPRINT_LOGNORM
, "Events list:");
151 for (p
= evs_list
; p
; p
= g_slist_next(p
)) {
153 scr_LogPrint(LPRINT_LOGNORM
,
154 "Id: %-3s %s", i
->id
, (i
->desc
? i
->desc
: ""));
156 scr_LogPrint(LPRINT_LOGNORM
, "End of events list.");
159 // evs_geteventslist(bool comp)
160 // Return a singly-linked-list of events ids, for the completion system.
161 // If comp is true, the string "list" is added (it's a completion argument).
162 // Note: the caller should free the list (and data) after use.
163 GSList
*evs_geteventslist(int compl)
165 GSList
*evidlist
= NULL
, *p
;
168 for (p
= evs_list
; p
; p
= g_slist_next(p
)) {
170 evidlist
= g_slist_append(evidlist
, g_strdup(i
->id
));
174 // Last item is the "list" subcommand.
175 evidlist
= g_slist_append(evidlist
, g_strdup("list"));
180 /* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */