1 #include <string.h>
2 #include <stdlib.h>
3 #include "urlgfe_parser.h"
4 #include "global_setting.h"
5 #include "category_store.h"
6 
7 
common_parser_end_element(ConfParser * cp,const gchar * element_name,gpointer data,GError ** error)8 void common_parser_end_element (ConfParser*   cp,
9                                 const gchar*  element_name,
10                                 gpointer      data,
11                                 GError**      error)
12 {
13 	conf_parser_pop (cp);
14 }
15 
16 // proxy setting parser ---------------------------------------------
17 
proxy_parser_start_element(ConfParser * cp,const gchar * element_name,const gchar ** attr_names,const gchar ** attr_values,ProxySetting * setting,GError ** error)18 void proxy_parser_start_element (ConfParser*          cp,
19                                  const gchar*         element_name,
20                                  const gchar**        attr_names,
21                                  const gchar**        attr_values,
22                                  ProxySetting*        setting,
23                                  GError**             error)
24 {
25 	int ii;
26 
27 	for (ii=0; attr_names[ii]; ii++) {
28 		if (strcmp (attr_names[ii], "type")==0)
29 			setting->type = atoi (attr_values[ii]);
30 		else if (strcmp (attr_names[ii], "authentication")==0)
31 			setting->authentication = atoi (attr_values[ii]);
32 		else if (strcmp (attr_names[ii], "port")==0)
33 			setting->port = atoi (attr_values[ii]);
34 		else if (strcmp (attr_names[ii], "address")==0)
35 			setting_set_string (&setting->address, attr_values[ii]);
36 		else if (strcmp (attr_names[ii], "username")==0)
37 			setting_set_string (&setting->username, attr_values[ii]);
38 		else if (strcmp (attr_names[ii], "password")==0)
39 			setting_set_string (&setting->password, attr_values[ii]);
40 	}
41 }
42 
43 static ConfParserEntry proxy_parser = {
44 	(conf_parser_start_element_func) proxy_parser_start_element,
45 	(conf_parser_end_element_func)   common_parser_end_element,
46 	(conf_parser_text_func)          NULL,
47 	(conf_parser_passthrough_func)   NULL,
48 	(conf_parser_error_func)         NULL
49 };
50 
urlgfe_get_proxy_parser()51 ConfParserEntry* urlgfe_get_proxy_parser ()
52 {
53 	return &proxy_parser;
54 }
55 
56 // downloaded setting parser ----------------------------------------
57 
downloaded_parser_parse_attr(const gchar ** attr_names,const gchar ** attr_values,DownloadSetting * setting)58 void downloaded_parser_parse_attr (const gchar**        attr_names,
59                                    const gchar**        attr_values,
60                                    DownloadSetting*     setting)
61 {
62 	int ii;
63 
64 	for (ii=0; attr_names[ii]; ii++) {
65 		if (strcmp (attr_names[ii], "parts")==0)
66 			setting->n_parts = atoi (attr_values[ii]);
67 		if (strcmp (attr_names[ii], "redirect")==0)
68 			setting->n_redirect = atoi (attr_values[ii]);
69 		if (strcmp (attr_names[ii], "retries")==0)
70 			setting->n_retries = atoi (attr_values[ii]);
71 		if (strcmp (attr_names[ii], "retry_delay")==0)
72 			setting->retry_delay = atoi (attr_values[ii]);
73 		if (strcmp (attr_names[ii], "multiple")==0)
74 			setting->multiple = atoi (attr_values[ii]);
75 		if (strcmp (attr_names[ii], "start_mode")==0)
76 			setting->start_mode = atoi (attr_values[ii]);
77 		if (strcmp (attr_names[ii], "state")==0)
78 			setting->state = atoi (attr_values[ii]);
79 	}
80 }
81 
downloaded_parser_text(ConfParser * cp,const gchar * text,gsize text_len,gchar ** dest_str,GError ** error)82 void downloaded_parser_text (ConfParser*          cp,
83                              const gchar*         text,
84                              gsize                text_len,
85                              gchar**              dest_str,
86                              GError**             error)
87 {
88 	setting_set_string_len (dest_str, text, text_len);
89 	conf_parser_pop_data (cp);
90 
91 	cp->parser->text = NULL;
92 }
93 
downloaded_parser_start_element(ConfParser * cp,const gchar * element_name,const gchar ** attr_names,const gchar ** attr_values,DownloadSetting * setting,GError ** error)94 void downloaded_parser_start_element (ConfParser*          cp,
95                                       const gchar*         element_name,
96                                       const gchar**        attr_names,
97                                       const gchar**        attr_values,
98                                       DownloadSetting*     setting,
99                                       GError**             error)
100 {
101 	ConfParserEntry* parser = cp->parser;
102 
103 	if (strcmp (element_name, PROXY_SETTING_TAG)==0) {
104 		conf_parser_push (cp, urlgfe_get_proxy_parser (), setting);
105 		proxy_parser_start_element (cp, element_name, attr_names,
106 		                            attr_values, PROXY_SETTING(setting), error);
107 	}
108 	else if (strcmp (element_name, "url")==0) {
109 		parser->text = (conf_parser_text_func)downloaded_parser_text;
110 		conf_parser_push_data (cp, &setting->url);
111 	}
112 	else if (strcmp (element_name, "directory")==0) {
113 		parser->text = (conf_parser_text_func)downloaded_parser_text;
114 		conf_parser_push_data (cp, &setting->directory);
115 	}
116 	else if (strcmp (element_name, "filename")==0) {
117 		parser->text = (conf_parser_text_func)downloaded_parser_text;
118 		conf_parser_push_data (cp, &setting->filename);
119 	}
120 	else if (strcmp (element_name, "referer")==0) {
121 		parser->text = (conf_parser_text_func)downloaded_parser_text;
122 		conf_parser_push_data (cp, &setting->referer);
123 	}
124 	else if (strcmp (element_name, DOWNLOAD_SETTING_TAG)==0) {
125 		downloaded_parser_parse_attr (attr_names, attr_values, setting);
126 	}
127 }
128 
downloaded_parser_end_element(ConfParser * cp,const gchar * element_name,gpointer data,GError ** error)129 void downloaded_parser_end_element (ConfParser*   cp,
130                                     const gchar*  element_name,
131                                     gpointer      data,
132                                     GError**      error)
133 {
134 	if (strcmp (element_name, DOWNLOAD_SETTING_TAG)==0)
135 		conf_parser_pop (cp);
136 }
137 
138 static ConfParserEntry downloaded_parser = {
139 	(conf_parser_start_element_func) downloaded_parser_start_element,
140 	(conf_parser_end_element_func)   downloaded_parser_end_element,
141 	(conf_parser_text_func)          NULL,
142 	(conf_parser_passthrough_func)   NULL,
143 	(conf_parser_error_func)         NULL
144 };
145 
urlgfe_get_downloaded_parser()146 ConfParserEntry* urlgfe_get_downloaded_parser ()
147 {
148 	return &downloaded_parser;
149 }
150 
151 // category setting parser ------------------------------------------
152 
category_parser_parse_attr(const gchar ** attr_names,const gchar ** attr_values,CategorySetting * setting)153 void category_parser_parse_attr (const gchar**        attr_names,
154                                  const gchar**        attr_values,
155                                  CategorySetting*     setting)
156 {
157 	int ii;
158 
159 	for (ii=0; attr_names[ii]; ii++) {
160 		if (strcmp (attr_names[ii], "name")==0)
161 			setting_set_string (&setting->name, attr_values[ii]);
162 		if (strcmp (attr_names[ii], "state")==0)
163 			setting->state = atoi (attr_values[ii]);
164 		if (strcmp (attr_names[ii], "multiple")==0)
165 			setting->multiple = atoi (attr_values[ii]);
166 		if (strcmp (attr_names[ii], "n_active_download")==0)
167 			setting->n_active_download = atoi (attr_values[ii]);
168 
169 		if (strcmp (attr_names[ii], "completed_capacity")==0)
170 			setting->completed_capacity = atoi (attr_values[ii]);
171 		if (strcmp (attr_names[ii], "recycled_capacity")==0)
172 			setting->recycled_capacity = atoi (attr_values[ii]);
173 
174 		if (strcmp (attr_names[ii], "col_completed")==0)
175 			setting->col_completed = atoi (attr_values[ii]);
176 		if (strcmp (attr_names[ii], "col_total")==0)
177 			setting->col_total = atoi (attr_values[ii]);
178 		if (strcmp (attr_names[ii], "col_speed")==0)
179 			setting->col_speed = atoi (attr_values[ii]);
180 		if (strcmp (attr_names[ii], "col_retry")==0)
181 			setting->col_retry = atoi (attr_values[ii]);
182 		if (strcmp (attr_names[ii], "col_url")==0)
183 			setting->col_url = atoi (attr_values[ii]);
184 
185 		if (strcmp (attr_names[ii], "text_filename")==0)
186 			setting->text_filename = atoi (attr_values[ii]);
187 		if (strcmp (attr_names[ii], "text_directory")==0)
188 			setting->text_directory = atoi (attr_values[ii]);
189 		if (strcmp (attr_names[ii], "text_url")==0)
190 			setting->text_url = atoi (attr_values[ii]);
191 		if (strcmp (attr_names[ii], "text_start_mode")==0)
192 			setting->text_start_mode = atoi (attr_values[ii]);
193 		if (strcmp (attr_names[ii], "text_message")==0)
194 			setting->text_message = atoi (attr_values[ii]);
195 	}
196 }
197 
category_parser_start_element(ConfParser * cp,const gchar * element_name,const gchar ** attr_names,const gchar ** attr_values,CategorySetting * setting,GError ** error)198 void category_parser_start_element (ConfParser*          cp,
199                                     const gchar*         element_name,
200                                     const gchar**        attr_names,
201                                     const gchar**        attr_values,
202                                     CategorySetting*     setting,
203                                     GError**             error)
204 {
205 	if (strcmp (element_name, DOWNLOAD_SETTING_TAG)==0) {
206 		conf_parser_push (cp, urlgfe_get_downloaded_parser (), setting);
207 		downloaded_parser_parse_attr (attr_names, attr_values,
208 		                              DOWNLOAD_SETTING (setting));
209 	}
210 	else if (strcmp (element_name, CATEGORY_SETTING_TAG)==0) {
211 		category_parser_parse_attr (attr_names, attr_values, setting);
212 	}
213 }
214 
category_parser_end_element(ConfParser * cp,const gchar * element_name,gpointer data,GError ** error)215 void category_parser_end_element (ConfParser*   cp,
216                                   const gchar*  element_name,
217                                   gpointer      data,
218                                   GError**      error)
219 {
220 	if (strcmp (element_name, CATEGORY_SETTING_TAG)==0)
221 		conf_parser_pop (cp);
222 }
223 
224 static ConfParserEntry category_parser = {
225 	(conf_parser_start_element_func) category_parser_start_element,
226 	(conf_parser_end_element_func)   category_parser_end_element,
227 	(conf_parser_text_func)          NULL,
228 	(conf_parser_passthrough_func)   NULL,
229 	(conf_parser_error_func)         NULL
230 };
231 
urlgfe_get_category_parser()232 ConfParserEntry* urlgfe_get_category_parser ()
233 {
234 	return &category_parser;
235 }
236 
237 // global setting parser --------------------------------------------
238 
global_parser_parse_attr(const gchar ** attr_names,const gchar ** attr_values,GlobalSetting * setting)239 void global_parser_parse_attr (const gchar**        attr_names,
240                                const gchar**        attr_values,
241                                GlobalSetting*       setting)
242 {
243 	int ii;
244 
245 	for (ii=0; attr_names[ii]; ii++) {
246 		if (strcmp (attr_names[ii], "n_active_category")==0)
247 			setting->n_active_category = atoi (attr_values[ii]);
248 		if (strcmp (attr_names[ii], "header_category_visible")==0)
249 			setting->header_category_visible = atoi (attr_values[ii]);
250 		if (strcmp (attr_names[ii], "monitoring_clipboard")==0)
251 			setting->monitoring_clipboard = atoi (attr_values[ii]);
252 	}
253 }
254 
global_parser_directory_text(ConfParser * cp,const gchar * text,gsize text_len,GlobalSetting * setting,GError ** error)255 void global_parser_directory_text  (ConfParser*          cp,
256                                     const gchar*         text,
257                                     gsize                text_len,
258                                     GlobalSetting*       setting,
259                                     GError**             error)
260 {
261 	gchar* str = g_strndup (text, text_len);
262 
263 	global_setting_add_directory (setting, str);
264 	g_free (str);
265 
266 	cp->parser->text = NULL;
267 }
268 
global_parser_extention_text(ConfParser * cp,const gchar * text,gsize text_len,GlobalSetting * setting,GError ** error)269 void global_parser_extention_text  (ConfParser*          cp,
270                                     const gchar*         text,
271                                     gsize                text_len,
272                                     GlobalSetting*       setting,
273                                     GError**             error)
274 {
275 	gchar* str = g_strndup (text, text_len);
276 
277 	global_setting_set_extention (setting, str);
278 	g_free (str);
279 
280 	cp->parser->text = NULL;
281 }
282 
global_parser_start_element(ConfParser * cp,const gchar * element_name,const gchar ** attr_names,const gchar ** attr_values,GlobalSetting * setting,GError ** error)283 void global_parser_start_element (ConfParser*          cp,
284                                   const gchar*         element_name,
285                                   const gchar**        attr_names,
286                                   const gchar**        attr_values,
287                                   GlobalSetting*       setting,
288                                   GError**             error)
289 {
290 	if (strcmp (element_name, CATEGORY_SETTING_TAG)==0) {
291 		conf_parser_push (cp, urlgfe_get_category_parser (), setting);
292 		category_parser_parse_attr (attr_names, attr_values, CATEGORY_SETTING (setting));
293 	}
294 	else if (strcmp (element_name, "directory")==0) {
295 		cp->parser->text = (conf_parser_text_func)global_parser_directory_text;
296 	}
297 	else if (strcmp (element_name, "extention")==0) {
298 		cp->parser->text = (conf_parser_text_func)global_parser_extention_text;
299 	}
300 	else {
301 		global_parser_parse_attr (attr_names, attr_values, setting);
302 	}
303 }
304 
global_parser_end_element(ConfParser * cp,const gchar * element_name,gpointer data,GError ** error)305 void global_parser_end_element (ConfParser*   cp,
306                                 const gchar*  element_name,
307                                 gpointer      data,
308                                 GError**      error)
309 {
310 	if (strcmp (element_name, GLOBAL_SETTING_TAG)==0)
311 		conf_parser_pop (cp);
312 }
313 
314 static ConfParserEntry global_parser = {
315 	(conf_parser_start_element_func) global_parser_start_element,
316 	(conf_parser_end_element_func)   global_parser_end_element,
317 	(conf_parser_text_func)          NULL,
318 	(conf_parser_passthrough_func)   NULL,
319 	(conf_parser_error_func)         NULL
320 };
321 
urlgfe_get_global_parser()322 ConfParserEntry* urlgfe_get_global_parser ()
323 {
324 	return &global_parser;
325 }
326 
327 // downloaded store parser ------------------------------------------
328 
downloaded_store_parser_start_element(ConfParser * cp,const gchar * element_name,const gchar ** attr_names,const gchar ** attr_values,DownloadStore * store,GError ** error)329 void downloaded_store_parser_start_element (ConfParser*          cp,
330                                             const gchar*         element_name,
331                                             const gchar**        attr_names,
332                                             const gchar**        attr_values,
333                                             DownloadStore*       store,
334                                             GError**             error)
335 {
336 	Download* dl_data;
337 
338 	if (strcmp (element_name, DOWNLOAD_SETTING_TAG)==0)
339 	{
340 		dl_data = download_new ();
341 		download_store_append_allocated (store, dl_data);
342 
343 		conf_parser_push (cp, urlgfe_get_downloaded_parser (), dl_data);
344 		downloaded_parser_parse_attr (attr_names, attr_values,
345 		                              DOWNLOAD_SETTING (dl_data));
346 	}
347 }
348 
349 static ConfParserEntry downloaded_store_parser = {
350 	(conf_parser_start_element_func) downloaded_store_parser_start_element,
351 	(conf_parser_end_element_func)   common_parser_end_element,
352 	(conf_parser_text_func)          NULL,
353 	(conf_parser_passthrough_func)   NULL,
354 	(conf_parser_error_func)         NULL
355 };
356 
357 // sub category parser ----------------------------------------------
358 
sub_category_parser_start_element(ConfParser * cp,const gchar * element_name,const gchar ** attr_names,const gchar ** attr_values,Category * category,GError ** error)359 void sub_category_parser_start_element (ConfParser*          cp,
360                                         const gchar*         element_name,
361                                         const gchar**        attr_names,
362                                         const gchar**        attr_values,
363                                         Category*            category,
364                                         GError**             error)
365 {
366 	DownloadStore*   download_store;
367 
368 	if (strcmp (element_name, CATEGORY_SETTING_TAG)==0) {
369 		conf_parser_push (cp, urlgfe_get_category_parser (), category);
370 		category_parser_parse_attr (attr_names, attr_values,
371 		                            CATEGORY_SETTING (category));
372 	}
373 	else {
374 		if (strcmp (element_name, CATEGORY_WAITING_STORE_TAG)==0 )
375 			download_store = &category->waiting_store;
376 		else if (strcmp (element_name, CATEGORY_COMPLETED_STORE_TAG)==0 )
377 			download_store = &category->completed_store;
378 		else if (strcmp (element_name, CATEGORY_RECYCLED_STORE_TAG)==0 )
379 			download_store = &category->recycled_store;
380 
381 		conf_parser_push (cp, &downloaded_store_parser, download_store);
382 	}
383 }
384 
385 static ConfParserEntry sub_category_parser = {
386 	(conf_parser_start_element_func) sub_category_parser_start_element,
387 	(conf_parser_end_element_func)   common_parser_end_element,
388 	(conf_parser_text_func)          NULL,
389 	(conf_parser_passthrough_func)   NULL,
390 	(conf_parser_error_func)         NULL
391 };
392 
393 // category store parser --------------------------------------------
394 
category_store_parser_start_element(ConfParser * cp,const gchar * element_name,const gchar ** attr_names,const gchar ** attr_values,GList ** list,GError ** error)395 void category_store_parser_start_element (ConfParser*          cp,
396                                           const gchar*         element_name,
397                                           const gchar**        attr_names,
398                                           const gchar**        attr_values,
399                                           GList**              list,
400                                           GError**             error)
401 {
402 	Category*        category;
403 
404 	if (strcmp (element_name, CATEGORY_TAG)==0) {
405 		category = category_new ();
406 		*list = g_list_append (*list, category);
407 
408 		conf_parser_push (cp, &sub_category_parser, category);
409 	}
410 }
411 
412 static ConfParserEntry category_store_parser = {
413 	(conf_parser_start_element_func) category_store_parser_start_element,
414 	(conf_parser_end_element_func)   common_parser_end_element,
415 	(conf_parser_text_func)          NULL,
416 	(conf_parser_passthrough_func)   NULL,
417 	(conf_parser_error_func)         NULL
418 };
419 
420 // category/setting file reader -------------------------------------
421 
urlgfe_parser_read_category(ConfParser * cp,const gchar * filename,GError ** error)422 GList* urlgfe_parser_read_category (ConfParser*  cp,
423                                     const gchar* filename,
424                                     GError** error)
425 {
426 	GList* list = NULL;
427 	gboolean read_ok;
428 
429 	conf_parser_push (cp, &category_store_parser, &list);
430 	read_ok = conf_parser_read_file (cp, filename, error);
431 
432 	return list;
433 }
434 
urlgfe_parser_read_setting(ConfParser * cp,const gchar * filename,GError ** error)435 GlobalSetting* urlgfe_parser_read_setting (ConfParser*  cp,
436                                            const gchar* filename,
437                                            GError** error)
438 {
439 	GlobalSetting* setting = global_setting_new ();
440 
441 	conf_parser_push (cp, urlgfe_get_global_parser (), setting);
442 	conf_parser_read_file (cp, filename, error);
443 
444 	return setting;
445 }
446 
447