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)
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 ***********************************************************************/
15 #include <fc_config.h>
27 #include "voteinfo_bar_g.h"
30 #include "client_main.h"
37 /* Define struct voteinfo_list type. */
38 #define SPECLIST_TAG voteinfo
39 #define SPECLIST_TYPE struct voteinfo
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
)
57 fc_assert_ret_msg(NULL
!= voteinfo_queue
,
58 "%s() called before votinfo_queue_init()!",
61 vi
= voteinfo_queue_find(vote_no
);
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)
75 struct voteinfo_list
*removed
;
77 if (voteinfo_queue
== 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
)
107 fc_assert_ret_msg(NULL
!= voteinfo_queue
,
108 "%s() called before votinfo_queue_init()!",
111 vi
= voteinfo_queue_find(vote_no
);
116 voteinfo_list_remove(voteinfo_queue
, 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
)
128 fc_assert_ret_msg(NULL
!= voteinfo_queue
,
129 "%s() called before votinfo_queue_init()!",
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
;
139 if (gui_options
.voteinfo_bar_new_at_front
) {
140 voteinfo_list_prepend(voteinfo_queue
, vi
);
141 voteinfo_queue_current_index
= 0;
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()!",
156 voteinfo_list_iterate(voteinfo_queue
, vi
) {
157 if (vi
->vote_no
== vote_no
) {
160 } voteinfo_list_iterate_end
;
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
) {
185 voteinfo_list_iterate(voteinfo_queue
, 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
)
206 if (voteinfo_queue
== NULL
) {
210 size
= voteinfo_list_size(voteinfo_queue
);
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
;
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
)
237 struct packet_vote_submit packet
;
239 if (!can_client_control()) {
243 vi
= voteinfo_queue_find(vote_no
);
248 packet
.vote_no
= vi
->vote_no
;
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)
276 if (voteinfo_queue
== NULL
) {
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())));