1 /*
2  * Xiphos Bible Study Tool
3  * lists.c - glists of module names and descriptions, bible books etc.
4  *
5  * Copyright (C) 2000-2020 Xiphos Developer Team
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20  */
21 
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 
26 #include <gtk/gtk.h>
27 #include <string.h>
28 
29 #include "main/lists.h"
30 #include "main/sword.h"
31 #include "main/settings.h"
32 #include "main/xml.h"
33 #include "backend/sword_main.hh"
34 
35 /******************************************************************************
36  *  lists to keep for the life of the program
37  */
38 static MOD_LISTS *mod_lists;
39 static MOD_LISTS mods;
40 
get_list(gint type)41 GList *get_list(gint type)
42 {
43 	switch (type) {
44 	case TEXT_LIST:
45 		return mod_lists->biblemods;
46 		break;
47 	case TEXT_DESC_LIST:
48 		return mod_lists->text_descriptions;
49 		break;
50 	case COMM_LIST:
51 		return mod_lists->commentarymods;
52 		break;
53 	case COMM_DESC_LIST:
54 		return mod_lists->comm_descriptions;
55 		break;
56 	case DICT_LIST:
57 		return mod_lists->dictionarymods;
58 		break;
59 	case DICT_DESC_LIST:
60 		return mod_lists->dict_descriptions;
61 		break;
62 	case GBS_LIST:
63 		return mod_lists->bookmods;
64 		break;
65 	case GBS_DESC_LIST:
66 		return mod_lists->book_descriptions;
67 		break;
68 	case PERCOMM_LIST:
69 		return mod_lists->percommods;
70 		break;
71 	case OPTIONS_LIST:
72 		return mod_lists->options;
73 		break;
74 	case DEVOTION_LIST:
75 		return mod_lists->devotionmods;
76 		break;
77 	case MAP_LIST:
78 		return mod_lists->mapmods;
79 		break;
80 	case IMAGE_LIST:
81 		return mod_lists->imagemods;
82 		break;
83 	case PRAYER_LIST:
84 		return mod_lists->prayermods;
85 		break;
86 	}
87 	return NULL;
88 }
89 
main_init_lists(void)90 void main_init_lists(void)
91 {
92 	gboolean start_backend = FALSE;
93 	mod_lists = &mods;
94 
95 	/* set glist to null */
96 	mods.biblemods = NULL;
97 	mods.commentarymods = NULL;
98 	mods.dictionarymods = NULL;
99 	mods.percommods = NULL;
100 	mods.bookmods = NULL;
101 	mods.devotionmods = NULL;
102 	mods.prayermods = NULL;
103 	mods.mapmods = NULL;
104 	mods.imagemods = NULL;
105 	mods.options = NULL;
106 	mods.text_descriptions = NULL;
107 	mods.comm_descriptions = NULL;
108 	mods.dict_descriptions = NULL;
109 	mods.book_descriptions = NULL;
110 	//mods.prayer_descriptions = NULL;
111 
112 	settings.havebible = FALSE;
113 	settings.havecomm = FALSE;
114 	settings.havedict = FALSE;
115 	settings.havebook = FALSE;
116 	settings.havepercomm = FALSE;
117 	settings.haveprayerlist = FALSE;
118 
119 	if (backend) {
120 		mods.options = backend->get_module_options();
121 	} else {
122 		start_backend = TRUE;
123 		backend = new BackEnd();
124 	}
125 	backend->init_lists(mod_lists);
126 	if (start_backend) {
127 		delete backend;
128 		backend = NULL;
129 	}
130 
131 	settings.havebible = g_list_length(mods.biblemods);
132 
133 	settings.havecomm = g_list_length(mods.commentarymods);
134 
135 	settings.havedict = g_list_length(mods.dictionarymods);
136 
137 	settings.havebook = g_list_length(mods.bookmods);
138 
139 	settings.havepercomm = g_list_length(mods.percommods);
140 
141 	settings.haveprayerlist = g_list_length(mods.prayermods);
142 
143 	if (g_list_length(mods.devotionmods) == 1) {
144 		xml_set_value("Xiphos", "modules", "devotional", (char *)mods.devotionmods->data);
145 		settings.devotionalmod = xml_get_value("modules", "devotional");
146 	}
147 
148 	XI_print(("%s = %d\n", "Number of Text modules", settings.havebible));
149 	XI_print(("%s = %d\n", "Number of Commentary modules", settings.havecomm));
150 	XI_print(("%s = %d\n", "Number of Dict/lex modules", settings.havedict));
151 	XI_print(("%s = %d\n", "Number of Book modules", settings.havebook));
152 	XI_print(("%s = %d\n", "Number of Percomm modules", settings.havepercomm));
153 	XI_print(("%s = %d\n", "Number of Devotion modules",
154 		  g_list_length(mods.devotionmods)));
155 	XI_print(("%s = %d\n\n", "Number of Prayer modules",
156 		  settings.haveprayerlist));
157 }
158 
main_shutdown_list(void)159 void main_shutdown_list(void)
160 {
161 	/* free lists */
162 	if (!mod_lists)
163 		return;
164 
165 	while (mod_lists->options != NULL) {
166 		g_free((char *)mod_lists->options->data);
167 		mod_lists->options = g_list_next(mod_lists->options);
168 	}
169 	g_list_free(mod_lists->options);
170 
171 	while (mod_lists->biblemods != NULL) {
172 		g_free((char *)mod_lists->biblemods->data);
173 		mod_lists->biblemods =
174 		    g_list_next(mod_lists->biblemods);
175 	}
176 	g_list_free(mod_lists->biblemods);
177 
178 	while (mod_lists->commentarymods != NULL) {
179 		g_free((char *)mod_lists->commentarymods->data);
180 		mod_lists->commentarymods =
181 		    g_list_next(mod_lists->commentarymods);
182 	}
183 	g_list_free(mod_lists->commentarymods);
184 
185 	while (mod_lists->dictionarymods != NULL) {
186 		g_free((char *)mod_lists->dictionarymods->data);
187 		mod_lists->dictionarymods =
188 		    g_list_next(mod_lists->dictionarymods);
189 	}
190 	g_list_free(mod_lists->dictionarymods);
191 
192 	while (mod_lists->bookmods != NULL) {
193 		g_free((char *)mod_lists->bookmods->data);
194 		mod_lists->bookmods = g_list_next(mod_lists->bookmods);
195 	}
196 	g_list_free(mod_lists->bookmods);
197 
198 	while (mod_lists->percommods != NULL) {
199 		g_free((char *)mod_lists->percommods->data);
200 		mod_lists->percommods =
201 		    g_list_next(mod_lists->percommods);
202 	}
203 	g_list_free(mod_lists->percommods);
204 	while (mod_lists->prayermods != NULL) {
205 		g_free((char *)mod_lists->prayermods->data);
206 		mod_lists->prayermods =
207 		    g_list_next(mod_lists->prayermods);
208 	}
209 	g_list_free(mod_lists->prayermods);
210 	while (mod_lists->text_descriptions != NULL) {
211 		g_free((char *)mod_lists->text_descriptions->data);
212 		mod_lists->text_descriptions =
213 		    g_list_next(mod_lists->text_descriptions);
214 	}
215 	g_list_free(mod_lists->text_descriptions);
216 
217 	while (mod_lists->dict_descriptions != NULL) {
218 		g_free((char *)mod_lists->dict_descriptions->data);
219 		mod_lists->dict_descriptions =
220 		    g_list_next(mod_lists->dict_descriptions);
221 	}
222 	g_list_free(mod_lists->dict_descriptions);
223 
224 	while (mod_lists->comm_descriptions != NULL) {
225 		g_free((char *)mod_lists->comm_descriptions->data);
226 		mod_lists->comm_descriptions =
227 		    g_list_next(mod_lists->comm_descriptions);
228 	}
229 	g_list_free(mod_lists->comm_descriptions);
230 
231 	while (mod_lists->book_descriptions != NULL) {
232 		g_free((char *)mod_lists->book_descriptions->data);
233 		mod_lists->book_descriptions =
234 		    g_list_next(mod_lists->book_descriptions);
235 	}
236 	g_list_free(mod_lists->book_descriptions);
237 
238 	while (mod_lists->devotionmods != NULL) {
239 		g_free((char *)mod_lists->devotionmods->data);
240 		mod_lists->devotionmods =
241 		    g_list_next(mod_lists->devotionmods);
242 	}
243 	g_list_free(mod_lists->devotionmods);
244 	mod_lists = NULL;
245 }
246