Mercurial > ~mikael > mcabber > hg
comparison mcabber/src/events.c @ 745:413e95f3051a
Introduce user "events" list
Not used yet.
The events list (evs_list) will be used to queue events for user approval,
for example subscription requests, file tranfers, etc.
The evs stuff is actually almost the same as the iqs stuff, a lot of code
is duplicated... :-\
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Mon, 13 Mar 2006 17:28:24 +0100 |
parents | |
children | 3a76c2d73606 |
comparison
equal
deleted
inserted
replaced
744:c3b76a1a07cb | 745:413e95f3051a |
---|---|
1 /* | |
2 * events.c -- Events fonctions | |
3 * | |
4 * Copyright (C) 2006 Mikael Berthe <bmikael@lists.lilotux.net> | |
5 * | |
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. | |
10 * | |
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. | |
15 * | |
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 | |
19 * USA | |
20 */ | |
21 | |
22 #include <glib.h> | |
23 #include "events.h" | |
24 #include "logprint.h" | |
25 | |
26 static GSList *evs_list; // Events list | |
27 | |
28 | |
29 // evs_new(type, timeout) | |
30 // Create an events structure. | |
31 eviqs *evs_new(guint8 type, time_t timeout) | |
32 { | |
33 static guint evs_idn; | |
34 eviqs *new_evs; | |
35 time_t now_t; | |
36 | |
37 if (!++evs_idn) | |
38 evs_idn = 1; | |
39 /* TODO: check for wrapping, we shouldn't reuse ids */ | |
40 | |
41 new_evs = g_new0(eviqs, 1); | |
42 time(&now_t); | |
43 new_evs->ts_create = now_t; | |
44 if (timeout) | |
45 new_evs->ts_expire = now_t + timeout; | |
46 new_evs->type = type; | |
47 new_evs->id = g_strdup_printf("%d", evs_idn); | |
48 | |
49 evs_list = g_slist_append(evs_list, new_evs); | |
50 return new_evs; | |
51 } | |
52 | |
53 int evs_del(const char *evid) | |
54 { | |
55 GSList *p; | |
56 eviqs *i; | |
57 | |
58 if (!evid) return 1; | |
59 | |
60 for (p = evs_list; p; p = g_slist_next(p)) { | |
61 i = p->data; | |
62 if (!strcmp(evid, i->id)) | |
63 break; | |
64 } | |
65 if (p) { | |
66 g_free(i->id); | |
67 if (i->xmldata) xmlnode_free(i->xmldata); | |
68 // XXX Should we free i->data? | |
69 g_free(i); | |
70 evs_list = g_slist_remove(evs_list, p->data); | |
71 return 0; // Ok, deleted | |
72 } | |
73 return -1; // Not found | |
74 } | |
75 | |
76 static eviqs *evs_find(const char *evid) | |
77 { | |
78 GSList *p; | |
79 eviqs *i; | |
80 | |
81 if (!evid) return NULL; | |
82 | |
83 for (p = evs_list; p; p = g_slist_next(p)) { | |
84 i = p->data; | |
85 if (!strcmp(evid, i->id)) | |
86 return i; | |
87 } | |
88 return NULL; | |
89 } | |
90 | |
91 // evs_callback(evid, evcontext) | |
92 // Callback processing for the specified event. | |
93 // Return 0 in case of success, -1 if the evid hasn't been found. | |
94 int evs_callback(const char *evid, guint evcontext) | |
95 { | |
96 eviqs *i; | |
97 | |
98 i = evs_find(evid); | |
99 if (!i) return -1; | |
100 | |
101 // IQ processing | |
102 // Note: If xml_result is NULL, this is a timeout | |
103 if (i->callback) | |
104 (*i->callback)(i, evcontext); | |
105 | |
106 evs_del(evid); | |
107 return 0; | |
108 } | |
109 | |
110 void evs_check_timeout(time_t now_t) | |
111 { | |
112 GSList *p; | |
113 eviqs *i; | |
114 | |
115 p = evs_list; | |
116 while (p) { | |
117 i = p->data; | |
118 // We must get next IQ eviqs element now because the current one | |
119 // could be freed. | |
120 p = g_slist_next(p); | |
121 | |
122 if ((!i->ts_expire && now_t > i->ts_create + EVS_MAX_TIMEOUT) || | |
123 (i->ts_expire && now_t > i->ts_expire)) { | |
124 evs_callback(i->id, EVS_CONTEXT_TIMEOUT); | |
125 } | |
126 } | |
127 } | |
128 | |
129 void evs_display_list(void) | |
130 { | |
131 GSList *p; | |
132 eviqs *i; | |
133 | |
134 scr_LogPrint(LPRINT_LOGNORM, "Events list:"); | |
135 for (p = evs_list; p; p = g_slist_next(p)) { | |
136 i = p->data; | |
137 scr_LogPrint(LPRINT_LOGNORM, "Id: %s", i->id); | |
138 } | |
139 scr_LogPrint(LPRINT_LOGNORM, "End of events list."); | |
140 } | |
141 | |
142 /* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */ |