IrrecoWebdbCache manages remotes.
[irreco.git] / irreco / src / webdb / irreco_webdb_cache.c
blob2c076aa736866ce0df19abce1022d8ad3bdf0ef8
1 /*
2 * irreco - Ir Remote Control
3 * Copyright (C) 2007,2008 Arto Karppinen (arto.karppinen@iki.fi),
4 * Joni Kokko (t5kojo01@students.oamk.fi),
5 * Sami Mäki (kasmra@xob.kapsi.fi),
6 * Harri Vattulainen (t5vaha01@students.oamk.fi),
7 * Pekka Gehör (pegu6@msn.com)
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include "irreco_webdb_cache.h"
25 #include "irreco_webdb_client.h"
27 /**
28 * @addtogroup IrrecoWebdbCache
29 * @ingroup IrrecoWebdb
31 * Caches XML-RPC calls, enables error handling and retries failed calls.
33 * @{
36 /**
37 * @file
38 * Source file of @ref IrrecoWebdbCache.
43 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
44 /* Prototypes */
45 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
47 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
48 /* Construction & Destruction */
49 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
51 /**
52 * @name Construction & Destruction
53 * @{
56 /**
57 * Create new IrrecoWebdb Object
59 IrrecoWebdbCache *irreco_webdb_cache_new()
61 IrrecoWebdbCache *self;
62 IRRECO_ENTER
64 self = g_slice_new0(IrrecoWebdbCache);
65 self->private = irreco_webdb_client_new();
66 self->remote_categories = NULL;
67 self->error_msg = g_string_new("");
68 self->loop = irreco_retry_loop_new(IRRECO_SECONDS_TO_USEC(0.3),
69 IRRECO_SECONDS_TO_USEC(3));
70 self->conf_hash = g_hash_table_new_full(g_int_hash, g_int_equal, NULL,
71 (GDestroyNotify) irreco_webdb_conf_free);
73 self->theme_id_hash = g_hash_table_new_full(g_int_hash, g_int_equal, NULL,
74 (GDestroyNotify) irreco_webdb_theme_free);
76 self->remote_id_hash = g_hash_table_new_full(g_int_hash, g_int_equal,
77 NULL, (GDestroyNotify) irreco_webdb_remote_free);
79 IRRECO_RETURN_PTR(self);
82 /**
83 * Free resources of IrrecoWebdbCache Object.
85 void irreco_webdb_cache_free(IrrecoWebdbCache *self)
87 IRRECO_ENTER
88 g_string_free(self->error_msg, TRUE);
89 self->error_msg = NULL;
90 irreco_retry_loop_free(self->loop);
91 self->loop = NULL;
92 if (self->remote_categories != NULL) {
93 irreco_string_table_free(self->remote_categories);
94 self->remote_categories = NULL;
96 irreco_webdb_client_free(self->private);
97 self->private = NULL;
98 g_slice_free(IrrecoWebdbCache, self);
99 g_hash_table_destroy(self->conf_hash);
100 self->conf_hash = NULL;
101 g_hash_table_destroy(self->theme_id_hash);
102 self->theme_id_hash = NULL;
103 g_hash_table_destroy(self->remote_id_hash);
104 self->remote_id_hash = NULL;
105 IRRECO_RETURN
108 /** @} */
110 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
111 /* Private Functions */
112 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
115 * @name Private Functions
116 * @{
120 * A simple test on Irreco WebDB to test if WebDB returns correct and
121 * meaningfull results.
123 * This function generates two numbers and asks WebDB to sum them. If the sum
124 * if correct, then our connection to the WebDB seems to be working.
126 * @return TRUE on success, FALSE otherwise.
128 static gboolean irreco_webdb_cache_test(IrrecoWebdbCache *self)
130 IrrecoWebdbClient *client;
131 GRand *rand;
132 gint32 num_a;
133 gint32 num_b;
134 gint sum;
135 IRRECO_ENTER
137 if (self->test_ok) IRRECO_RETURN_BOOL(self->test_ok);
139 rand = g_rand_new();
140 client = (IrrecoWebdbClient *) self->private;
142 IRRECO_RETRY_LOOP_START(self->loop)
143 num_a = g_rand_int_range(rand, 1, 1000);
144 num_b = g_rand_int_range(rand, 1, 1000);
145 self->test_ok = irreco_webdb_client_sum(
146 client, num_a, num_b, &sum);
148 if (self->test_ok) {
149 if (sum == num_a + num_b) {
150 break;
151 } else {
152 g_string_printf(self->error_msg,
153 "Got invalid result from "
154 "sum method. %i + %i = %i. "
155 "Expected %i.",
156 num_a, num_b, sum,
157 num_a + num_b);
158 IRRECO_PRINTF("%s\n", self->error_msg->str);
159 self->test_ok = FALSE;
161 } else {
162 irreco_webdb_client_get_error_msg(
163 client, self->error_msg);
165 IRRECO_RETRY_LOOP_END(self->loop)
167 g_rand_free(rand);
168 IRRECO_RETURN_BOOL(self->test_ok);
172 * Shows error message in case categories list is empty.
174 * @return TRUE if categories is not empty, FALSE otherwise.
176 static gboolean irreco_webdb_cache_verify_category(IrrecoWebdbCache *self)
178 IRRECO_ENTER
179 if (self->categories == NULL) {
180 g_string_printf(self->error_msg, "list of categories is NULL");
181 IRRECO_PRINTF("%s\n", self->error_msg->str);
182 IRRECO_RETURN_BOOL(FALSE)
184 IRRECO_RETURN_BOOL(TRUE)
188 * Shows error message in case manufacturers list is empty.
190 * @return TRUE if manufacturers list contains data, FALSE otherwise.
192 static gboolean
193 irreco_webdb_cache_verify_manufacturer(IrrecoWebdbCache *self,
194 const gchar *category,
195 IrrecoStringTable **manufactures)
197 IRRECO_ENTER
198 if (!irreco_string_table_get(self->categories, category,
199 (gpointer*) manufactures)) {
200 g_string_printf(self->error_msg,
201 "list of manufacturers is NULL");
202 IRRECO_PRINTF("%s\n", self->error_msg->str);
203 IRRECO_RETURN_BOOL(FALSE)
205 IRRECO_RETURN_BOOL(TRUE)
210 * Shows error message in case models list is empty.
212 * @return TRUE if models list contains data, FALSE otherwise.
214 static gboolean irreco_webdb_cache_verify_model(IrrecoWebdbCache *self,
215 IrrecoStringTable *manuf_list,
216 const gchar *manufacturer,
217 IrrecoStringTable **models)
219 IRRECO_ENTER
220 if (!irreco_string_table_get(manuf_list, manufacturer,
221 (gpointer*) models)) {
222 g_string_printf(self->error_msg, "list of models is NULL");
223 IRRECO_PRINTF("%s\n", self->error_msg->str);
224 IRRECO_RETURN_BOOL(FALSE)
226 IRRECO_RETURN_BOOL(TRUE)
230 * Shows error message in case configurations list is empty.
232 * @return TRUE if configurations list contains data, FALSE otherwise.
234 static gboolean irreco_webdb_cache_verify_config(IrrecoWebdbCache *self,
235 IrrecoStringTable *models,
236 const gchar *model,
237 IrrecoStringTable **configs)
239 IRRECO_ENTER
240 if (!irreco_string_table_get(models, model, (gpointer*) configs)) {
241 g_string_printf(self->error_msg,
242 "list of configurations is NULL");
243 IRRECO_PRINTF("%s\n", self->error_msg->str);
244 IRRECO_RETURN_BOOL(FALSE)
246 IRRECO_RETURN_BOOL(TRUE)
249 /** @} */
251 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
252 /* Functions */
253 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
256 * @name Public Functions
257 * @{
262 * Adds new user (name, email, password) to database.
264 * @return TRUE if user is added succesfully, FALSE otherwise.
266 gboolean irreco_webdb_cache_add_user(IrrecoWebdbCache *self,
267 const gchar *name,
268 const gchar *email,
269 const gchar *passwd)
271 IrrecoWebdbClient *client;
273 IRRECO_ENTER
275 client = (IrrecoWebdbClient *) self->private;
277 IRRECO_RETRY_LOOP_START(self->loop)
278 /* test connection to webdb */
279 if (irreco_webdb_cache_test(self) == FALSE){
280 g_string_printf(self->error_msg,
281 "Failed cache self test.");
282 IRRECO_PRINTF("%s\n", self->error_msg->str);
283 IRRECO_RETURN_BOOL(FALSE);
286 if(irreco_webdb_client_add_user(client, name, email, passwd)) {
287 IRRECO_RETURN_BOOL(TRUE);
288 } else {
289 /* irreco_webdb_client_add_user failed, get err msg */
290 irreco_webdb_client_get_error_msg(client,
291 self->error_msg);
292 IRRECO_RETURN_BOOL(FALSE);
294 IRRECO_RETRY_LOOP_END(self->loop)
296 IRRECO_RETURN_BOOL(FALSE);
300 * Adds new configuration to database.
302 * @return TRUE if configuration is uploaded succesfully, FALSE otherwise.
304 gboolean irreco_webdb_cache_upload_configuration(IrrecoWebdbCache *self,
305 const gchar *backend,
306 const gchar *category,
307 const gchar *file_hash,
308 const gchar *file_name,
309 const gchar *manufacturer,
310 const gchar *model,
311 const gchar *password,
312 const gchar *user,
313 const gchar *data)
315 IrrecoWebdbClient *client;
317 IRRECO_ENTER
319 client = (IrrecoWebdbClient *) self->private;
321 IRRECO_RETRY_LOOP_START(self->loop)
322 /* test connection to webdb */
323 if (irreco_webdb_cache_test(self) == FALSE){
324 g_string_printf(self->error_msg,
325 "Failed cache self test.");
326 IRRECO_PRINTF("%s\n", self->error_msg->str);
327 IRRECO_RETURN_BOOL(FALSE);
330 if(irreco_webdb_client_upload_configuration(client, backend,
331 category, file_hash, file_name,
332 manufacturer, model, password, user,
333 data)) {
335 IRRECO_RETURN_BOOL(TRUE);
336 } else {
337 /* irreco_webdb_client_upload_configuration failed,
338 get err msg */
339 irreco_webdb_client_get_error_msg(client,
340 self->error_msg);
341 IRRECO_RETURN_BOOL(FALSE);
343 IRRECO_RETRY_LOOP_END(self->loop)
345 IRRECO_RETURN_BOOL(FALSE);
349 * Extracts error message from IrrecoWebdbCache object.
351 * @return error message as string.
353 const gchar *irreco_webdb_cache_get_error(IrrecoWebdbCache *self)
355 IRRECO_ENTER
356 IRRECO_RETURN_STR(self->error_msg->str);
360 * Fetches categories from DB
362 * @return TRUE if categories are fetched succesfully, FALSE otherwise.
364 gboolean irreco_webdb_cache_get_categories(IrrecoWebdbCache *self,
365 IrrecoStringTable **categories)
367 IRRECO_ENTER
369 if (self->categories == NULL) {
370 gboolean success = FALSE;
371 IrrecoWebdbClient *client = (IrrecoWebdbClient *) self->private;
373 IRRECO_RETRY_LOOP_START(self->loop)
374 if (irreco_webdb_cache_test(self) == FALSE) break;
375 success = irreco_webdb_client_get_categories(
376 client, &self->categories);
377 if (success) {
378 break;
380 irreco_webdb_client_get_error_msg(
381 client, self->error_msg);
382 IRRECO_RETRY_LOOP_END(self->loop)
384 if (success == FALSE) IRRECO_RETURN_BOOL(FALSE);
386 irreco_string_table_sort_abc (self->categories);
389 *categories = self->categories;
390 IRRECO_RETURN_BOOL(TRUE)
394 * Fetches all categories from DB.
396 * @param all_categories must be freed if return value is TRUE.
397 * @return TRUE if categories are fetched succesfully, FALSE otherwise.
399 gboolean irreco_webdb_cache_get_all_categories(IrrecoWebdbCache *self,
400 IrrecoStringTable **all_categories)
402 gboolean success = FALSE;
403 IrrecoWebdbClient *client = (IrrecoWebdbClient *) self->private;
404 IRRECO_ENTER
407 IRRECO_RETRY_LOOP_START(self->loop)
408 if (irreco_webdb_cache_test(self) == FALSE) break;
409 success = irreco_webdb_client_get_all_categories(
410 client, all_categories);
411 if (success) {
412 break;
414 irreco_webdb_client_get_error_msg(
415 client, self->error_msg);
416 IRRECO_RETRY_LOOP_END(self->loop)
418 if (success == FALSE) IRRECO_RETURN_BOOL(FALSE);
420 irreco_string_table_sort_abc(*all_categories);
422 IRRECO_RETURN_BOOL(TRUE)
426 * Fetches manufacturers from DB.
428 * @param category contains category in which type manufacturers are returned.
429 * @return TRUE if manufacturers are fetched succesfully, FALSE otherwise.
431 gboolean irreco_webdb_cache_get_manufacturers(IrrecoWebdbCache *self,
432 const gchar *category,
433 IrrecoStringTable **manufacturers)
435 IrrecoStringTable *manufacturer_list;
436 IRRECO_ENTER
438 if (!irreco_webdb_cache_verify_category(self) ||
439 !irreco_webdb_cache_verify_manufacturer(self, category,
440 &manufacturer_list)) {
441 IRRECO_RETURN_BOOL(FALSE)
444 if(manufacturer_list == NULL) {
445 gboolean success = FALSE;
446 IrrecoWebdbClient *client = (IrrecoWebdbClient *) self->private;
448 IRRECO_RETRY_LOOP_START(self->loop)
449 if (irreco_webdb_cache_test(self) == FALSE) break;
450 success = irreco_webdb_client_get_manufacturers(
451 client, category, &manufacturer_list);
453 if (success) break;
454 irreco_webdb_client_get_error_msg(
455 client, self->error_msg);
456 IRRECO_RETRY_LOOP_END(self->loop)
458 if (success == FALSE) IRRECO_RETURN_BOOL(FALSE);
460 irreco_string_table_sort_abc (manufacturer_list);
461 irreco_string_table_change_data(self->categories, category,
462 manufacturer_list);
465 irreco_string_table_get(self->categories, category,
466 (gpointer *) manufacturers);
467 IRRECO_RETURN_BOOL(TRUE)
471 * Fetches all manufacturers from DB.
473 * @param all_manufacturers must be released if function returns TRUE.
474 * @return TRUE if manufacturers are fetched succesfully, FALSE otherwise.
476 gboolean irreco_webdb_cache_get_all_manufacturers(IrrecoWebdbCache *self,
477 IrrecoStringTable **all_manufacturers)
479 gboolean success = FALSE;
480 IrrecoWebdbClient *client = (IrrecoWebdbClient *) self->private;
481 IRRECO_ENTER
484 IRRECO_RETRY_LOOP_START(self->loop)
485 if (irreco_webdb_cache_test(self) == FALSE) break;
486 success = irreco_webdb_client_get_all_manufacturers(
487 client, all_manufacturers);
488 if (success) {
489 break;
491 irreco_webdb_client_get_error_msg(
492 client, self->error_msg);
493 IRRECO_RETRY_LOOP_END(self->loop)
495 if (success == FALSE) IRRECO_RETURN_BOOL(FALSE);
497 irreco_string_table_sort_abc(*all_manufacturers);
499 IRRECO_RETURN_BOOL(TRUE)
503 * Fetches models belonging given category and manufacturer.
505 * @param models must be freed if function returns TRUE.
506 * @return TRUE if models are fetched succesfully, FALSE otherwise.
508 gboolean irreco_webdb_cache_get_models(IrrecoWebdbCache *self,
509 const gchar *category,
510 const gchar *manufacturer,
511 IrrecoStringTable **models)
513 IrrecoStringTable *model_list;
514 IrrecoStringTable *manufacturer_list;
515 IRRECO_ENTER
517 if (!irreco_webdb_cache_verify_category(self) ||
518 !irreco_webdb_cache_verify_manufacturer(self, category,
519 &manufacturer_list) ||
520 !irreco_webdb_cache_verify_model(self, manufacturer_list,
521 manufacturer, &model_list)) {
522 IRRECO_RETURN_BOOL(FALSE)
525 if(model_list == NULL) {
526 gboolean success = FALSE;
527 IrrecoWebdbClient *client = (IrrecoWebdbClient *) self->private;
529 IRRECO_RETRY_LOOP_START(self->loop)
530 if (irreco_webdb_cache_test(self) == FALSE) break;
531 success = irreco_webdb_client_get_models(
532 client, category, manufacturer, &model_list);
534 if (success) break;
535 irreco_webdb_client_get_error_msg(
536 client, self->error_msg);
537 IRRECO_RETRY_LOOP_END(self->loop)
539 if (success == FALSE) IRRECO_RETURN_BOOL(FALSE);
541 irreco_string_table_sort_abc (model_list);
542 irreco_string_table_change_data(manufacturer_list, manufacturer,
543 model_list);
546 irreco_string_table_get(manufacturer_list, manufacturer,
547 (gpointer *) models);
548 IRRECO_RETURN_BOOL(TRUE)
552 * Fetches configs belonging given category, manufacturer and model.
554 * @return TRUE if configs are fetched succesfully, FALSE otherwise.
556 gboolean irreco_webdb_cache_get_configs(IrrecoWebdbCache *self,
557 const gchar *category,
558 const gchar *manufacturer,
559 const gchar *model,
560 IrrecoStringTable **configs)
562 IrrecoStringTable * config_list;
563 IrrecoStringTable * model_list;
564 IrrecoStringTable * manufacturer_list;
565 IRRECO_ENTER
567 if (!irreco_webdb_cache_verify_category(self) ||
568 !irreco_webdb_cache_verify_manufacturer(self, category,
569 &manufacturer_list) ||
570 !irreco_webdb_cache_verify_model(self, manufacturer_list,
571 manufacturer, &model_list) ||
572 !irreco_webdb_cache_verify_config(self, model_list, model,
573 &config_list)) {
574 IRRECO_RETURN_BOOL(FALSE)
577 if(config_list == NULL) {
578 gboolean success = FALSE;
579 IrrecoWebdbClient *client = (IrrecoWebdbClient *) self->private;
581 IRRECO_RETRY_LOOP_START(self->loop)
582 if (irreco_webdb_cache_test(self) == FALSE) break;
583 success = irreco_webdb_client_get_configs(client,
584 category, manufacturer, model, &config_list);
586 if (success) break;
587 irreco_webdb_client_get_error_msg(
588 client, self->error_msg);
589 IRRECO_RETRY_LOOP_END(self->loop)
591 if (success == FALSE) IRRECO_RETURN_BOOL(FALSE);
593 irreco_string_table_change_data(model_list, model, config_list);
596 irreco_string_table_get(model_list, model, (gpointer *) configs);
597 IRRECO_RETURN_BOOL(TRUE)
601 * Fetches configuration by given ID.
603 * @return TRUE if configuration is fetched succesfully, FALSE otherwise.
605 gboolean irreco_webdb_cache_get_configuration(IrrecoWebdbCache *self,
606 gint id,
607 IrrecoWebdbConf **config)
609 IrrecoWebdbConf * configuration;
610 IRRECO_ENTER
612 if (g_hash_table_lookup(self->conf_hash, (gconstpointer) &id) == NULL) {
613 gboolean success = FALSE;
614 IrrecoWebdbClient *client = (IrrecoWebdbClient *) self->private;
616 IRRECO_RETRY_LOOP_START(self->loop)
617 if (irreco_webdb_cache_test(self) == FALSE) break;
618 success = irreco_webdb_client_get_configuration(
619 client, id, &configuration);
621 if (success) break;
622 irreco_webdb_client_get_error_msg(client,
623 self->error_msg);
624 IRRECO_RETRY_LOOP_END(self->loop)
626 if (success == FALSE) IRRECO_RETURN_BOOL(FALSE);
628 g_hash_table_insert(self->conf_hash,
629 (gpointer) &configuration->id,
630 (gpointer) configuration);
633 *config = g_hash_table_lookup(self->conf_hash, (gconstpointer) &id);
635 IRRECO_RETURN_BOOL(TRUE)
639 * Fetches file by given hash and name.
641 * @return TRUE if configuration is fetched succesfully, FALSE otherwise.
643 gboolean irreco_webdb_cache_get_file(IrrecoWebdbCache *self,
644 const gchar *file_hash,
645 const gchar *file_name,
646 GString **file_data)
648 GString *file;
649 gboolean success = FALSE;
650 IrrecoWebdbClient *client;
651 IRRECO_ENTER
653 client = (IrrecoWebdbClient *) self->private;
655 IRRECO_RETRY_LOOP_START(self->loop)
656 if (irreco_webdb_cache_test(self) == FALSE) break;
657 success = irreco_webdb_client_get_file(
658 client, file_hash, file_name, &file);
660 if (success) break;
661 irreco_webdb_client_get_error_msg(client, self->error_msg);
662 IRRECO_RETRY_LOOP_END(self->loop)
664 if (success == FALSE) IRRECO_RETURN_BOOL(FALSE);
666 *file_data = file;
668 IRRECO_RETURN_BOOL(TRUE)
672 * Check if user with given name is already at DB.
674 *.@param user_exists will contain info wether user name was at DB.
675 * @return TRUE if test is done succesfully, FALSE otherwise.
677 gboolean irreco_webdb_cache_get_user_exists(IrrecoWebdbCache *self,
678 const gchar *name,
679 gboolean *user_exists)
681 IrrecoWebdbClient *client;
682 gboolean success = FALSE;
684 IRRECO_ENTER
685 client = (IrrecoWebdbClient *) self->private;
686 IRRECO_RETRY_LOOP_START(self->loop)
687 /* test connection to webdb */
688 if (irreco_webdb_cache_test(self) == FALSE) break;
690 success = irreco_webdb_client_get_user_exists(client,
691 name,
692 user_exists);
694 if (success) break;
696 /* irreco_webdb_client_get_user_exists failed, get err msg */
697 irreco_webdb_client_get_error_msg(client, self->error_msg);
698 IRRECO_RETRY_LOOP_END(self->loop)
700 if (success == FALSE) IRRECO_RETURN_BOOL(FALSE);
702 IRRECO_RETURN_BOOL(TRUE)
706 * Login to database.
708 * @return TRUE if login is succesful, FALSE otherwise.
710 gboolean irreco_webdb_cache_login(IrrecoWebdbCache *self,
711 const gchar *user,
712 const gchar *password)
714 IrrecoWebdbClient *client;
716 IRRECO_ENTER
718 client = (IrrecoWebdbClient *) self->private;
720 IRRECO_RETRY_LOOP_START(self->loop)
721 /* test connection to webdb */
722 if (irreco_webdb_cache_test(self) == FALSE){
723 g_string_printf(self->error_msg,
724 "Failed cache self test.");
725 IRRECO_PRINTF("%s\n", self->error_msg->str);
726 IRRECO_RETURN_BOOL(FALSE);
729 if(irreco_webdb_client_login(client, user, password)) {
731 IRRECO_RETURN_BOOL(TRUE);
732 } else {
733 /* irreco_webdb_client_login failed,
734 get err msg */
735 irreco_webdb_client_get_error_msg(client,
736 self->error_msg);
737 IRRECO_RETURN_BOOL(FALSE);
739 IRRECO_RETRY_LOOP_END(self->loop)
741 IRRECO_RETURN_BOOL(FALSE);
744 gboolean irreco_webdb_cache_get_themes(IrrecoWebdbCache *self,
745 IrrecoStringTable **theme)
747 gboolean success = FALSE;
748 IrrecoWebdbClient *client = (IrrecoWebdbClient *) self->private;
749 IRRECO_ENTER
751 IRRECO_RETRY_LOOP_START(self->loop)
752 if (irreco_webdb_cache_test(self) == FALSE) break;
753 success = irreco_webdb_client_get_themes(client, theme);
755 if (success) break;
756 irreco_webdb_client_get_error_msg(client, self->error_msg);
757 IRRECO_RETRY_LOOP_END(self->loop)
759 IRRECO_RETURN_BOOL(success)
762 gboolean irreco_webdb_cache_get_theme_by_id(IrrecoWebdbCache *self,
763 gint theme_id,
764 IrrecoWebdbTheme **theme)
766 IrrecoWebdbTheme *get_theme;
767 IRRECO_ENTER
770 if (g_hash_table_lookup(self->theme_id_hash,
771 (gconstpointer) &theme_id) == NULL) {
772 gboolean success = FALSE;
773 IrrecoWebdbClient *client = (IrrecoWebdbClient *) self->private;
775 IRRECO_RETRY_LOOP_START(self->loop)
776 if (irreco_webdb_cache_test(self) == FALSE) break;
777 success = irreco_webdb_client_get_theme_by_id(
778 client, theme_id, &get_theme);
780 if (success) break;
781 irreco_webdb_client_get_error_msg(client,
782 self->error_msg);
783 IRRECO_RETRY_LOOP_END(self->loop)
785 if (success == FALSE) IRRECO_RETURN_BOOL(FALSE);
787 g_hash_table_insert(self->theme_id_hash,
788 (gpointer) &get_theme->id,
789 (gpointer) get_theme);
792 *theme = g_hash_table_lookup(self->theme_id_hash, (gconstpointer) &theme_id);
794 IRRECO_RETURN_BOOL(TRUE)
799 gboolean irreco_webdb_cache_get_preview_button(IrrecoWebdbCache *self,
800 gint theme_id,
801 IrrecoWebdbTheme **theme)
803 gboolean success = TRUE;
804 IrrecoWebdbClient *client = (IrrecoWebdbClient *) self->private;
805 IRRECO_ENTER
807 *theme = g_hash_table_lookup(self->theme_id_hash,
808 (gconstpointer) &theme_id);
809 if (*theme == NULL) {
810 success = irreco_webdb_cache_get_theme_by_id(self, theme_id,
811 theme);
812 if (success == FALSE) {
813 irreco_webdb_client_get_error_msg(client, self->error_msg);
814 goto end;
818 if ((*theme)->preview_button == NULL)
820 IRRECO_RETRY_LOOP_START(self->loop)
822 if (irreco_webdb_cache_test(self) == FALSE) break;
824 success = irreco_webdb_client_get_preview_button(client,
825 theme_id, &(*theme)->preview_button);
827 if (success) break;
829 irreco_webdb_client_get_error_msg(client, self->error_msg);
830 IRRECO_RETRY_LOOP_END(self->loop)
833 end:
835 IRRECO_RETURN_BOOL(success)
838 gboolean irreco_webdb_cache_get_backgrounds(IrrecoWebdbCache *self,
839 gint theme_id,
840 IrrecoStringTable **bg_list){
841 IrrecoWebdbClient *client;
842 gboolean success = TRUE;
843 IRRECO_ENTER
845 client = (IrrecoWebdbClient *) self->private;
846 IRRECO_RETRY_LOOP_START(self->loop)
847 if (irreco_webdb_cache_test(self) == FALSE) break;
848 success = irreco_webdb_client_get_backgrounds(client,
849 theme_id,
850 bg_list);
851 if (success) break;
852 irreco_webdb_client_get_error_msg(client, self->error_msg);
853 IRRECO_RETRY_LOOP_END(self->loop)
855 IRRECO_RETURN_BOOL(success)
858 gboolean irreco_webdb_cache_get_bg_by_id(IrrecoWebdbCache *self,
859 gint bg_id,
860 const char *theme_bg_dir)
862 IrrecoWebdbClient *client = (IrrecoWebdbClient *) self->private;
863 gboolean success = TRUE;
864 IRRECO_ENTER
866 IRRECO_RETRY_LOOP_START(self->loop)
867 if (irreco_webdb_cache_test(self) == FALSE) break;
868 success = irreco_webdb_client_get_bg_by_id(client,
869 bg_id,
870 theme_bg_dir);
871 if (success) break;
872 irreco_webdb_client_get_error_msg(client, self->error_msg);
873 IRRECO_RETRY_LOOP_END(self->loop)
874 IRRECO_RETURN_BOOL(success)
877 gboolean irreco_webdb_cache_get_buttons(IrrecoWebdbCache *self,
878 gint theme_id,
879 IrrecoStringTable **button_list)
881 IrrecoWebdbClient *client = (IrrecoWebdbClient *) self->private;
882 gboolean success = TRUE;
883 IRRECO_ENTER
885 IRRECO_RETRY_LOOP_START(self->loop)
886 if (irreco_webdb_cache_test(self) == FALSE) break;
887 success = irreco_webdb_client_get_buttons(client,
888 theme_id,
889 button_list);
890 if (success) break;
891 irreco_webdb_client_get_error_msg(client, self->error_msg);
892 IRRECO_RETRY_LOOP_END(self->loop)
893 IRRECO_RETURN_BOOL(success)
896 gboolean irreco_webdb_cache_get_button_by_id(IrrecoWebdbCache *self,
897 gint button_id,
898 const char *theme_button_dir)
900 IrrecoWebdbClient *client = (IrrecoWebdbClient *) self->private;
901 gboolean success = TRUE;
902 IRRECO_ENTER
904 IRRECO_RETRY_LOOP_START(self->loop)
905 if (irreco_webdb_cache_test(self) == FALSE) break;
906 success = irreco_webdb_client_get_button_by_id(client,
907 button_id,
908 theme_button_dir);
909 if (success) break;
910 irreco_webdb_client_get_error_msg(client, self->error_msg);
911 IRRECO_RETRY_LOOP_END(self->loop)
913 IRRECO_RETURN_BOOL(success)
918 * Fetches remote categories from DB
920 * @param categories points to internally allocated storage in the cache
921 * and must not be freed
922 * @return TRUE if categories are fetched succesfully, FALSE otherwise.
924 gboolean irreco_webdb_cache_get_remote_categories(IrrecoWebdbCache *self,
925 IrrecoStringTable **categories)
927 IRRECO_ENTER
929 if (self->remote_categories == NULL) {
930 gboolean success = FALSE;
932 IRRECO_RETRY_LOOP_START(self->loop)
933 if (irreco_webdb_cache_test(self) == FALSE) break;
934 success = irreco_webdb_client_get_remote_categories(
935 self->private, &self->remote_categories);
936 if (success) {
937 break;
939 irreco_webdb_client_get_error_msg(self->private,
940 self->error_msg);
941 IRRECO_RETRY_LOOP_END(self->loop)
943 if (success == FALSE) IRRECO_RETURN_BOOL(FALSE);
945 irreco_string_table_sort_abc(self->remote_categories);
948 *categories = self->remote_categories;
949 IRRECO_RETURN_BOOL(TRUE)
953 * Fetches remote manufacturers from DB.
955 * @param category contains category in which type manufacturers are returned.
956 * @param manufacturers points to internally allocated storage in the cache
957 * and must not be freed
958 * @return TRUE if manufacturers are fetched succesfully, FALSE otherwise.
960 gboolean irreco_webdb_cache_get_remote_manufacturers(IrrecoWebdbCache *self,
961 const gchar *category,
962 IrrecoStringTable **manufacturers)
964 IrrecoStringTable *categories;
965 IrrecoStringTable *manufacturer_list;
966 IRRECO_ENTER
968 if (!irreco_webdb_cache_get_remote_categories(self, &categories)) {
969 IRRECO_RETURN_BOOL(FALSE)
972 if (!irreco_string_table_exists(categories, category)) {
973 g_string_printf(self->error_msg, "%s",
974 "Can't find category");
975 IRRECO_RETURN_BOOL(FALSE)
978 irreco_string_table_get(categories, category,
979 (gpointer *) &manufacturer_list);
981 if (manufacturer_list == NULL) {
982 gboolean success = FALSE;
983 IRRECO_RETRY_LOOP_START(self->loop)
984 if (irreco_webdb_cache_test(self) == FALSE) break;
985 success = irreco_webdb_client_get_remote_manufacturers(
986 self->private, category, &manufacturer_list);
988 if (success) break;
989 irreco_webdb_client_get_error_msg(self->private,
990 self->error_msg);
991 IRRECO_RETRY_LOOP_END(self->loop)
993 if (success == FALSE) IRRECO_RETURN_BOOL(FALSE);
995 irreco_string_table_sort_abc (manufacturer_list);
996 irreco_string_table_change_data(categories, category,
997 manufacturer_list);
1000 irreco_string_table_get(categories, category,
1001 (gpointer *) manufacturers);
1003 IRRECO_RETURN_BOOL(TRUE)
1007 * Fetches remote models belonging given category and manufacturer.
1009 * @param models points to internally allocated storage in the cache
1010 * and must not be freed
1011 * @return TRUE if models are fetched succesfully, FALSE otherwise.
1013 gboolean irreco_webdb_cache_get_remote_models(IrrecoWebdbCache *self,
1014 const gchar *category,
1015 const gchar *manufacturer,
1016 IrrecoStringTable **models)
1018 IrrecoStringTable *manufacturer_list;
1019 IrrecoStringTable *model_list;
1020 IRRECO_ENTER
1022 if (!irreco_webdb_cache_get_remote_manufacturers(self, category,
1023 &manufacturer_list)) {
1024 IRRECO_RETURN_BOOL(FALSE)
1027 if (!irreco_string_table_exists(manufacturer_list, manufacturer)) {
1028 g_string_printf(self->error_msg, "%s",
1029 "Can't find manufacturer");
1030 IRRECO_RETURN_BOOL(FALSE)
1033 irreco_string_table_get(manufacturer_list, manufacturer,
1034 (gpointer *) &model_list);
1036 if(model_list == NULL) {
1037 gboolean success = FALSE;
1039 IRRECO_RETRY_LOOP_START(self->loop)
1040 if (irreco_webdb_cache_test(self) == FALSE) break;
1041 success = irreco_webdb_client_get_remote_models(
1042 self->private, category, manufacturer,
1043 &model_list);
1045 if (success) break;
1046 irreco_webdb_client_get_error_msg(self->private,
1047 self->error_msg);
1048 IRRECO_RETRY_LOOP_END(self->loop)
1050 if (success == FALSE) IRRECO_RETURN_BOOL(FALSE);
1052 irreco_string_table_sort_abc (model_list);
1053 irreco_string_table_change_data(manufacturer_list, manufacturer,
1054 model_list);
1057 irreco_string_table_get(manufacturer_list, manufacturer,
1058 (gpointer *) models);
1059 IRRECO_RETURN_BOOL(TRUE)
1063 * Fetches creators belonging given category, manufacturer and model.
1065 * @param creators points to internally allocated storage in the cache
1066 * and must not be freed
1067 * @return TRUE if configs are fetched succesfully, FALSE otherwise.
1069 gboolean irreco_webdb_cache_get_remote_creators(IrrecoWebdbCache *self,
1070 const gchar *category,
1071 const gchar *manufacturer,
1072 const gchar *model,
1073 IrrecoStringTable **creators)
1075 IrrecoStringTable * model_list;
1076 IrrecoStringTable * creator_list;
1077 IRRECO_ENTER
1079 if (!irreco_webdb_cache_get_remote_models(self, category, manufacturer,
1080 &model_list)) {
1081 IRRECO_RETURN_BOOL(FALSE)
1084 if (!irreco_string_table_exists(model_list, model)) {
1085 g_string_printf(self->error_msg, "%s",
1086 "Can't find model");
1087 IRRECO_RETURN_BOOL(FALSE)
1090 irreco_string_table_get(model_list, model, (gpointer *) &creator_list);
1092 if(creator_list == NULL) {
1093 gboolean success = FALSE;
1095 IRRECO_RETRY_LOOP_START(self->loop)
1096 if (irreco_webdb_cache_test(self) == FALSE) break;
1097 success = irreco_webdb_client_get_remote_creators(
1098 self->private, category, manufacturer,
1099 model, &creator_list);
1101 if (success) break;
1102 irreco_webdb_client_get_error_msg(self->private,
1103 self->error_msg);
1104 IRRECO_RETRY_LOOP_END(self->loop)
1106 if (success == FALSE) IRRECO_RETURN_BOOL(FALSE);
1108 irreco_string_table_change_data(model_list, model,
1109 creator_list);
1112 irreco_string_table_get(model_list, model, (gpointer *) creators);
1113 IRRECO_RETURN_BOOL(TRUE)
1117 * Fetches creators belonging given category, manufacturer and model.
1119 * @param creators points to internally allocated storage in the cache
1120 * and must not be freed
1121 * @return TRUE if configs are fetched succesfully, FALSE otherwise.
1123 gboolean irreco_webdb_cache_get_remotes(IrrecoWebdbCache *self,
1124 const gchar *category,
1125 const gchar *manufacturer,
1126 const gchar *model,
1127 const gchar *creator,
1128 GList **remote_list)
1130 IrrecoStringTable * creator_list;
1131 IRRECO_ENTER
1133 if (!irreco_webdb_cache_get_remote_creators(self, category,
1134 manufacturer, model,
1135 &creator_list)) {
1136 IRRECO_RETURN_BOOL(FALSE)
1139 if (!irreco_string_table_exists(creator_list, creator)) {
1140 g_string_printf(self->error_msg, "%s",
1141 "Can't find creator");
1142 IRRECO_RETURN_BOOL(FALSE)
1145 irreco_string_table_get(creator_list, creator,
1146 (gpointer *) remote_list);
1148 if(*remote_list == NULL) {
1149 gboolean success = FALSE;
1151 IRRECO_RETRY_LOOP_START(self->loop)
1152 if (irreco_webdb_cache_test(self) == FALSE) break;
1153 success = irreco_webdb_client_get_remotes(self->private,
1154 category, manufacturer, model,
1155 creator, remote_list);
1157 if (success) break;
1158 irreco_webdb_client_get_error_msg(self->private,
1159 self->error_msg);
1160 IRRECO_RETRY_LOOP_END(self->loop)
1162 if (success == FALSE) IRRECO_RETURN_BOOL(FALSE);
1164 irreco_string_table_change_data(creator_list, creator,
1165 *remote_list);
1168 irreco_string_table_get(creator_list, creator,
1169 (gpointer *) remote_list);
1170 IRRECO_RETURN_BOOL(TRUE)
1174 * Fetches remote by given ID.
1176 * @param remote points to internally allocated storage in the cache
1177 * and must not be freed
1178 * @return TRUE if remote is fetched succesfully, FALSE otherwise.
1180 gboolean irreco_webdb_cache_get_remote_by_id(IrrecoWebdbCache *self,
1181 gint id,
1182 IrrecoWebdbRemote **remote)
1184 IRRECO_ENTER
1186 if (g_hash_table_lookup(self->remote_id_hash,
1187 (gconstpointer) &id) == NULL) {
1188 gboolean success = FALSE;
1190 IRRECO_RETRY_LOOP_START(self->loop)
1191 if (irreco_webdb_cache_test(self) == FALSE) break;
1192 success = irreco_webdb_client_get_remote_by_id(
1193 self->private, id, remote);
1195 if (success) break;
1196 irreco_webdb_client_get_error_msg(self->private,
1197 self->error_msg);
1198 IRRECO_RETRY_LOOP_END(self->loop)
1200 if (success == FALSE) IRRECO_RETURN_BOOL(FALSE);
1202 g_hash_table_insert(self->remote_id_hash,
1203 (gpointer) &((*remote)->id),
1204 (gpointer) *remote);
1207 *remote = g_hash_table_lookup(self->remote_id_hash,
1208 (gconstpointer) &id);
1210 IRRECO_RETURN_BOOL(TRUE)
1213 /** @} */
1215 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
1216 /* Events and Callbacks */
1217 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
1219 /** @} */