Stop sharing requirement_unit_state_ereq().
[freeciv.git] / client / voteinfo.c
blobff5cc6ecc7817cf57ea53e3dbc8bd039ad114d2a
1 /***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 ***********************************************************************/
14 #ifdef HAVE_CONFIG_H
15 #include <fc_config.h>
16 #endif
18 #include <time.h>
20 /* utility */
21 #include "log.h"
23 /* common */
24 #include "packets.h"
26 /* client/include */
27 #include "voteinfo_bar_g.h"
29 /* client */
30 #include "client_main.h"
31 #include "clinet.h"
32 #include "options.h"
34 #include "voteinfo.h"
37 /* Define struct voteinfo_list type. */
38 #define SPECLIST_TAG voteinfo
39 #define SPECLIST_TYPE struct voteinfo
40 #include "speclist.h"
41 #define voteinfo_list_iterate(alist, pitem)\
42 TYPED_LIST_ITERATE(struct voteinfo, alist, pitem)
43 #define voteinfo_list_iterate_end LIST_ITERATE_END
45 static struct voteinfo_list *voteinfo_queue = NULL;
46 static int voteinfo_queue_current_index = 0;
49 /**************************************************************************
50 Remove the vote with number 'vote_no' after a small amount of time so
51 that the user can see that it was removed.
52 **************************************************************************/
53 void voteinfo_queue_delayed_remove(int vote_no)
55 struct voteinfo *vi;
57 fc_assert_ret_msg(NULL != voteinfo_queue,
58 "%s() called before votinfo_queue_init()!",
59 __FUNCTION__);
61 vi = voteinfo_queue_find(vote_no);
62 if (vi == NULL) {
63 return;
65 vi->remove_time = time(NULL);
68 /**************************************************************************
69 Check for old votes that should be removed from the queue. This function
70 should be called periodically from a timer callback.
71 **************************************************************************/
72 void voteinfo_queue_check_removed(void)
74 time_t now;
75 struct voteinfo_list *removed;
77 if (voteinfo_queue == NULL) {
78 return;
81 now = time(NULL);
82 removed = voteinfo_list_new();
83 voteinfo_list_iterate(voteinfo_queue, vi) {
84 if (vi != NULL && vi->remove_time > 0 && now - vi->remove_time > 2) {
85 voteinfo_list_append(removed, vi);
87 } voteinfo_list_iterate_end;
89 voteinfo_list_iterate(removed, vi) {
90 voteinfo_queue_remove(vi->vote_no);
91 } voteinfo_list_iterate_end;
93 if (voteinfo_list_size(removed) > 0) {
94 voteinfo_gui_update();
97 voteinfo_list_destroy(removed);
100 /**************************************************************************
101 Remove the given vote from the queue immediately.
102 **************************************************************************/
103 void voteinfo_queue_remove(int vote_no)
105 struct voteinfo *vi;
107 fc_assert_ret_msg(NULL != voteinfo_queue,
108 "%s() called before votinfo_queue_init()!",
109 __FUNCTION__);
111 vi = voteinfo_queue_find(vote_no);
112 if (vi == NULL) {
113 return;
116 voteinfo_list_remove(voteinfo_queue, vi);
117 free(vi);
120 /**************************************************************************
121 Create a new voteinfo record and place it in the queue.
122 **************************************************************************/
123 void voteinfo_queue_add(int vote_no, const char *user, const char *desc,
124 int percent_required, int flags)
126 struct voteinfo *vi;
128 fc_assert_ret_msg(NULL != voteinfo_queue,
129 "%s() called before votinfo_queue_init()!",
130 __FUNCTION__);
132 vi = fc_calloc(1, sizeof(struct voteinfo));
133 vi->vote_no = vote_no;
134 sz_strlcpy(vi->user, user);
135 sz_strlcpy(vi->desc, desc);
136 vi->percent_required = percent_required;
137 vi->flags = flags;
139 if (gui_options.voteinfo_bar_new_at_front) {
140 voteinfo_list_prepend(voteinfo_queue, vi);
141 voteinfo_queue_current_index = 0;
142 } else {
143 voteinfo_list_append(voteinfo_queue, vi);
147 /**************************************************************************
148 Find the voteinfo record corresponding to the given vote number.
149 **************************************************************************/
150 struct voteinfo *voteinfo_queue_find(int vote_no)
152 fc_assert_ret_val_msg(NULL != voteinfo_queue, NULL,
153 "%s() called before votinfo_queue_init()!",
154 __FUNCTION__);
156 voteinfo_list_iterate(voteinfo_queue, vi) {
157 if (vi->vote_no == vote_no) {
158 return vi;
160 } voteinfo_list_iterate_end;
161 return NULL;
164 /**************************************************************************
165 Initialize data structures used by this module.
166 **************************************************************************/
167 void voteinfo_queue_init(void)
169 if (voteinfo_queue != NULL) {
170 voteinfo_queue_free();
172 voteinfo_queue = voteinfo_list_new();
173 voteinfo_queue_current_index = 0;
176 /**************************************************************************
177 Free memory allocated by this module.
178 **************************************************************************/
179 void voteinfo_queue_free(void)
181 if (voteinfo_queue == NULL) {
182 return;
185 voteinfo_list_iterate(voteinfo_queue, vi) {
186 if (vi != NULL) {
187 free(vi);
189 } voteinfo_list_iterate_end;
191 voteinfo_list_destroy(voteinfo_queue);
192 voteinfo_queue = NULL;
193 voteinfo_queue_current_index = 0;
196 /**************************************************************************
197 Get the voteinfo record at the start of the vote queue. If 'pindex' is
198 non-NULL, it is set to queue index of that record. This function is
199 used in conjunction with voteinfo_queue_next().
200 **************************************************************************/
201 struct voteinfo *voteinfo_queue_get_current(int *pindex)
203 struct voteinfo *vi;
204 int size;
206 if (voteinfo_queue == NULL) {
207 return NULL;
210 size = voteinfo_list_size(voteinfo_queue);
212 if (size <= 0) {
213 return NULL;
216 if (!(0 <= voteinfo_queue_current_index
217 && voteinfo_queue_current_index < size)) {
218 voteinfo_queue_next();
221 vi = voteinfo_list_get(voteinfo_queue, voteinfo_queue_current_index);
223 if (vi != NULL && pindex != NULL) {
224 *pindex = voteinfo_queue_current_index;
227 return vi;
230 /**************************************************************************
231 Convenience function for submitting a vote to the server.
232 NB: Only to be used if the server has the "voteinfo" capability.
233 **************************************************************************/
234 void voteinfo_do_vote(int vote_no, enum client_vote_type vote)
236 struct voteinfo *vi;
237 struct packet_vote_submit packet;
239 if (!can_client_control()) {
240 return;
243 vi = voteinfo_queue_find(vote_no);
244 if (vi == NULL) {
245 return;
248 packet.vote_no = vi->vote_no;
250 switch (vote) {
251 case CVT_YES:
252 packet.value = 1;
253 break;
254 case CVT_NO:
255 packet.value = -1;
256 break;
257 case CVT_ABSTAIN:
258 packet.value = 0;
259 break;
260 default:
261 return;
262 break;
265 send_packet_vote_submit(&client.conn, &packet);
266 vi->client_vote = vote;
269 /**************************************************************************
270 Cycle through the votes in the queue.
271 **************************************************************************/
272 void voteinfo_queue_next(void)
274 int size;
276 if (voteinfo_queue == NULL) {
277 return;
280 size = voteinfo_list_size(voteinfo_queue);
282 voteinfo_queue_current_index++;
283 if (voteinfo_queue_current_index >= size) {
284 voteinfo_queue_current_index = 0;
288 /**************************************************************************
289 Returns the number of pending votes.
290 **************************************************************************/
291 int voteinfo_queue_size(void)
293 return (NULL != voteinfo_queue ? voteinfo_list_size(voteinfo_queue) : 0);
296 /**************************************************************************
297 Returns whether the voteinfo bar should be displayed or not.
298 **************************************************************************/
299 bool voteinfo_bar_can_be_shown(void)
301 return (NULL != voteinfo_queue
302 && gui_options.voteinfo_bar_use
303 && (gui_options.voteinfo_bar_always_show
304 || (0 < voteinfo_list_size(voteinfo_queue)
305 && NULL != voteinfo_queue_get_current(NULL)))
306 && (!gui_options.voteinfo_bar_hide_when_not_player
307 || (client_has_player()
308 && !client_is_observer())));