1 /*
2  *      This program is free software; you can redistribute it and/or modify
3  *      it under the terms of the GNU General Public License as published by
4  *      the Free Software Foundation; either version 2 of the License, or
5  *      (at your option) any later version.
6  *
7  *      This program is distributed in the hope that it will be useful,
8  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *      GNU General Public License for more details.
11  *
12  *      You should have received a copy of the GNU General Public License
13  *      along with this program; if not, write to the Free Software
14  *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 #include <gtk/gtk.h>
17 #include "def.h"
18 
19 /* free_generic_list : takes care
20  * of freeing memory of a entire list.
21  * Don't forget to free the data
22  * slot first.
23  */
free_generic_list(GList ** ptrlist)24 void free_generic_list(GList **ptrlist)
25 {
26 	GList *ptr;
27 
28 	for(ptr = *ptrlist; ptr; ptr = ptr->next)
29 	{
30 		g_free(ptr->data);
31 		ptr->data = NULL;
32 	}
33 
34 	if(*ptrlist)
35 		g_list_free(*ptrlist);
36 	*ptrlist = NULL;
37 }
38 
39 
free_cell_list(GList ** ptrlist)40 void free_cell_list(GList **ptrlist)
41 {
42 	GList *ptr;
43 
44 	GtkWidget *cell;
45 
46 	for(ptr = *ptrlist; ptr; ptr = ptr->next)
47 	{
48 		cell = (GtkWidget*)ptr->data;
49 		gtk_widget_destroy(GTK_WIDGET(cell));
50 		ptr->data = NULL;
51 	}
52 
53 	if(*ptrlist)
54 	g_list_free(*ptrlist);
55 	*ptrlist = NULL;
56 }
57 /* free_list : this is like only
58  * usefull for the GList called
59  * "all", which is defined in the
60  * struct _prog (see file def.h
61  * for more details).
62  * That's because we need to
63  * unref the data pointer on
64  * each element of the list
65  * before freeing it.
66  */
free_list(GList ** ptrlist)67 void free_list(GList **ptrlist)
68 {
69 	GList *ptr;
70 	struct _card *data_ptr;
71 
72 	for(ptr = *ptrlist; ptr; ptr = ptr->next)
73 	{
74 		data_ptr = ptr->data;
75 		if(data_ptr->original)
76   			g_object_unref(data_ptr->original);
77 
78   		if(data_ptr->pixmap)
79 			g_object_unref(data_ptr->pixmap);
80 
81   		if(data_ptr->pixmask)
82 			g_object_unref(data_ptr->pixmask);
83 
84 		g_free(data_ptr);
85 		ptr->data = NULL;
86 	}
87 
88 	if(*ptrlist)
89 	g_list_free(*ptrlist);
90 	*ptrlist = NULL;
91 }
92 /*
93 void free_blink(GList *lst)
94 {
95 	struct _card *data;
96 	while(lst)
97 	{
98 		data =(struct _card*) lst->data;
99 
100 		if(data->blink == TRUE)
101 		{
102 		  if(data->img != data->original && data->original)
103 		    g_object_unref(data->original);
104 
105 			g_object_unref(data->img);
106 			data->img = NULL;
107 		}
108 		lst = lst->next;
109 	}
110 }
111 */
112 /* free_clear : called when we need to
113  * restart a game. That's related to
114  * what is allocated during or before
115  * a round.
116  *
117  * That's just composed by many
118  * g_list_free to clear some GList.
119  *
120  * The only exception is the
121  * movecard at the end. That correspond
122  * to the struct _movingcard (see def.h
123  * file for more details).
124  */
free_clear(struct _prog * prog_data)125 void free_clear(struct _prog *prog_data)
126 {
127 	int i;
128 
129 	if(prog_data->pile)
130 		g_list_free(prog_data->pile);
131 	prog_data->pile = NULL;
132 
133 	if(prog_data->waiting)
134 	{
135 		g_list_free(prog_data->waiting);
136 	}
137 	prog_data->waiting = NULL;
138 
139 	if(prog_data->players != NULL)
140 	{
141 		for(i = 0; i < 4; i++)
142 		{
143 			if(prog_data->players[i])
144 				g_list_free(prog_data->players[i]);
145 
146 			prog_data->players[i] = NULL;
147 		}
148 	}
149 	if(prog_data->teams != NULL)
150 	{
151 		for(i = 0; i < 2; i++)
152 		{
153 			if(prog_data->teams[i])
154 			{
155 				g_list_free(prog_data->teams[i]);
156 			}
157 			prog_data->teams[i] = NULL;
158 		}
159 	}
160 
161 	if(prog_data->movecard)
162 		g_free(prog_data->movecard);
163 
164 	prog_data->movecard = NULL;
165 }
166 
167 /* free_prog : clear everything,
168  * we just want to quit the program.
169  */
free_prog(struct _prog * prog_data)170 void free_prog(struct _prog *prog_data)
171 {
172 	if(prog_data->output)
173 		fprintf(prog_data->output, "Quit..\n");
174 	free_clear(prog_data);
175 
176 	g_free(prog_data->allwidgets);
177 
178 	g_free(prog_data->players);
179 
180 	g_free(prog_data->teams);
181 
182 	if(prog_data->cells)
183 		g_list_free(prog_data->cells);
184 
185 	if(prog_data->icons != NULL)
186 	{
187 		int i;
188 		for(i = 0; i< 5; i++)
189 		{
190 			if(prog_data->icons[i] != NULL)
191 			g_object_unref(prog_data->icons[i]);
192 		}
193 
194 		g_free(prog_data->icons);
195 	}
196 
197 	if(prog_data->back_original)
198 		g_object_unref(prog_data->back_original);
199 
200 	if(prog_data->back)
201 		g_object_unref(prog_data->back);
202 
203 	if(prog_data->backmask)
204 		g_object_unref(prog_data->backmask);
205 
206   if(prog_data->card_face)
207     g_free(prog_data->card_face);
208 
209   if(prog_data->card_type)
210     g_free(prog_data->card_type);
211 
212 
213   if(prog_data->bg_img_file)
214     g_free(prog_data->bg_img_file);
215 
216   if(prog_data->bg_pixbuf)
217     g_object_unref(prog_data->bg_pixbuf);
218 
219   if(prog_data->bg_scaled)
220     g_object_unref(prog_data->bg_scaled);
221 
222 	if(prog_data->player_names)
223 		g_strfreev(prog_data->player_names);
224 
225 	free_list(&prog_data->all);
226 
227 	free_generic_list(&prog_data->dropping);
228 }
229