gint changed to glong
[irreco.git] / irreco / src / webdb / irreco_webdb_cache.c
blob740552167837ac15633a41f148ec1395440197e2
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 glong 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, (gint) 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 * Get maximum image size accepted by database
708 gint irreco_webdb_cache_get_max_image_size(IrrecoWebdbCache *self)
710 IrrecoWebdbClient *client;
711 IRRECO_ENTER
712 client = (IrrecoWebdbClient *) self->private;
713 IRRECO_RETURN_INT(irreco_webdb_client_get_max_image_size(client));
717 * Create theme
719 gint irreco_webdb_cache_create_theme(IrrecoWebdbCache *self,
720 const gchar *name,
721 const gchar *comment,
722 const gchar *preview_button,
723 const gchar *folder,
724 const gchar *user,
725 const gchar *password)
727 IrrecoWebdbClient *client;
728 IRRECO_ENTER
729 client = (IrrecoWebdbClient *) self->private;
730 IRRECO_RETURN_INT(irreco_webdb_client_create_theme(
731 client,
732 name,
733 comment,
734 preview_button,
735 folder,
736 user,
737 password));
741 * Set theme downloadable
743 gboolean irreco_webdb_cache_set_theme_downloadable(IrrecoWebdbCache *self,
744 gint id,
745 gboolean downloadable,
746 const gchar *user,
747 const gchar *password)
749 IrrecoWebdbClient *client;
750 IRRECO_ENTER
751 client = (IrrecoWebdbClient *) self->private;
752 IRRECO_RETURN_BOOL(irreco_webdb_client_set_theme_downloadable(
753 client,
755 downloadable,
756 user,
757 password));
761 * Login to database.
763 * @return TRUE if login is succesful, FALSE otherwise.
765 gboolean irreco_webdb_cache_login(IrrecoWebdbCache *self,
766 const gchar *user,
767 const gchar *password)
769 IrrecoWebdbClient *client;
771 IRRECO_ENTER
773 client = (IrrecoWebdbClient *) self->private;
775 IRRECO_RETRY_LOOP_START(self->loop)
776 /* test connection to webdb */
777 if (irreco_webdb_cache_test(self) == FALSE){
778 g_string_printf(self->error_msg,
779 "Failed cache self test.");
780 IRRECO_PRINTF("%s\n", self->error_msg->str);
781 IRRECO_RETURN_BOOL(FALSE);
784 if(irreco_webdb_client_login(client, user, password)) {
786 IRRECO_RETURN_BOOL(TRUE);
787 } else {
788 /* irreco_webdb_client_login failed,
789 get err msg */
790 irreco_webdb_client_get_error_msg(client,
791 self->error_msg);
792 IRRECO_RETURN_BOOL(FALSE);
794 IRRECO_RETRY_LOOP_END(self->loop)
796 IRRECO_RETURN_BOOL(FALSE);
799 gboolean irreco_webdb_cache_get_themes(IrrecoWebdbCache *self,
800 IrrecoStringTable **theme)
802 gboolean success = FALSE;
803 IrrecoWebdbClient *client = (IrrecoWebdbClient *) self->private;
804 IRRECO_ENTER
806 IRRECO_RETRY_LOOP_START(self->loop)
807 if (irreco_webdb_cache_test(self) == FALSE) break;
808 success = irreco_webdb_client_get_themes(client, theme);
810 if (success) break;
811 irreco_webdb_client_get_error_msg(client, self->error_msg);
812 IRRECO_RETRY_LOOP_END(self->loop)
814 IRRECO_RETURN_BOOL(success)
817 gboolean irreco_webdb_cache_get_theme_by_id(IrrecoWebdbCache *self,
818 gint theme_id,
819 IrrecoWebdbTheme **theme)
821 IrrecoWebdbTheme *get_theme;
822 IRRECO_ENTER
825 if (g_hash_table_lookup(self->theme_id_hash,
826 (gconstpointer) &theme_id) == NULL) {
827 gboolean success = FALSE;
828 IrrecoWebdbClient *client = (IrrecoWebdbClient *) self->private;
830 IRRECO_RETRY_LOOP_START(self->loop)
831 if (irreco_webdb_cache_test(self) == FALSE) break;
832 success = irreco_webdb_client_get_theme_by_id(
833 client, theme_id, &get_theme);
835 if (success) break;
836 irreco_webdb_client_get_error_msg(client,
837 self->error_msg);
838 IRRECO_RETRY_LOOP_END(self->loop)
840 if (success == FALSE) IRRECO_RETURN_BOOL(FALSE);
842 g_hash_table_insert(self->theme_id_hash,
843 (gpointer) &get_theme->id,
844 (gpointer) get_theme);
847 *theme = g_hash_table_lookup(self->theme_id_hash, (gconstpointer) &theme_id);
849 IRRECO_RETURN_BOOL(TRUE)
854 gboolean irreco_webdb_cache_get_preview_button(IrrecoWebdbCache *self,
855 gint theme_id,
856 IrrecoWebdbTheme **theme)
858 gboolean success = TRUE;
859 IrrecoWebdbClient *client = (IrrecoWebdbClient *) self->private;
860 IRRECO_ENTER
862 *theme = g_hash_table_lookup(self->theme_id_hash,
863 (gconstpointer) &theme_id);
864 if (*theme == NULL) {
865 success = irreco_webdb_cache_get_theme_by_id(self, theme_id,
866 theme);
867 if (success == FALSE) {
868 irreco_webdb_client_get_error_msg(client, self->error_msg);
869 goto end;
873 if ((*theme)->preview_button == NULL)
875 IRRECO_RETRY_LOOP_START(self->loop)
877 if (irreco_webdb_cache_test(self) == FALSE) break;
879 success = irreco_webdb_client_get_preview_button(client,
880 theme_id, &(*theme)->preview_button);
882 if (success) break;
884 irreco_webdb_client_get_error_msg(client, self->error_msg);
885 IRRECO_RETRY_LOOP_END(self->loop)
888 end:
890 IRRECO_RETURN_BOOL(success)
894 * Add bg to theme
896 gint irreco_webdb_cache_add_bg_to_theme(IrrecoWebdbCache *self,
897 const gchar *name,
898 const gchar *image_hash,
899 const gchar *image_name,
900 const guchar *image,
901 gint image_len,
902 const gchar *folder,
903 gint theme_id,
904 const gchar *user,
905 const gchar *password)
907 IrrecoWebdbClient *client;
908 IRRECO_ENTER
909 client = (IrrecoWebdbClient *) self->private;
910 IRRECO_RETURN_INT(irreco_webdb_client_add_bg_to_theme(
911 client,
912 name,
913 image_hash,
914 image_name,
915 image,
916 image_len,
917 folder,
918 theme_id,
919 user,
920 password));
923 gboolean irreco_webdb_cache_get_backgrounds(IrrecoWebdbCache *self,
924 gint theme_id,
925 IrrecoStringTable **bg_list){
926 IrrecoWebdbClient *client;
927 gboolean success = TRUE;
928 IRRECO_ENTER
930 client = (IrrecoWebdbClient *) self->private;
931 IRRECO_RETRY_LOOP_START(self->loop)
932 if (irreco_webdb_cache_test(self) == FALSE) break;
933 success = irreco_webdb_client_get_backgrounds(client,
934 theme_id,
935 bg_list);
936 if (success) break;
937 irreco_webdb_client_get_error_msg(client, self->error_msg);
938 IRRECO_RETRY_LOOP_END(self->loop)
940 IRRECO_RETURN_BOOL(success)
943 gboolean irreco_webdb_cache_get_bg_by_id(IrrecoWebdbCache *self,
944 gint bg_id,
945 const char *theme_bg_dir)
947 IrrecoWebdbClient *client = (IrrecoWebdbClient *) self->private;
948 gboolean success = TRUE;
949 IRRECO_ENTER
951 IRRECO_RETRY_LOOP_START(self->loop)
952 if (irreco_webdb_cache_test(self) == FALSE) break;
953 success = irreco_webdb_client_get_bg_by_id(client,
954 bg_id,
955 theme_bg_dir);
956 if (success) break;
957 irreco_webdb_client_get_error_msg(client, self->error_msg);
958 IRRECO_RETRY_LOOP_END(self->loop)
959 IRRECO_RETURN_BOOL(success)
963 * Add button to theme
965 gint irreco_webdb_cache_add_button_to_theme(IrrecoWebdbCache *self,
966 const gchar *name,
967 gboolean allow_text,
968 const gchar *text_format_up,
969 const gchar *text_format_down,
970 gint text_padding,
971 gfloat text_h_align,
972 gfloat text_v_align,
973 const gchar *image_up_hash,
974 const gchar *image_up_name,
975 const guchar *image_up,
976 gint image_up_len,
977 const gchar *image_down_hash,
978 const gchar *image_down_name,
979 const guchar *image_down,
980 gint image_down_len,
981 const gchar *folder,
982 gint theme_id,
983 const gchar *user,
984 const gchar *password)
986 IrrecoWebdbClient *client;
987 IRRECO_ENTER
988 client = (IrrecoWebdbClient *) self->private;
989 IRRECO_RETURN_INT(irreco_webdb_client_add_button_to_theme(
990 client,
991 name,
992 allow_text,
993 text_format_up,
994 text_format_down,
995 text_padding,
996 text_h_align,
997 text_v_align,
998 image_up_hash,
999 image_up_name,
1000 image_up,
1001 image_up_len,
1002 image_down_hash,
1003 image_down_name,
1004 image_down,
1005 image_down_len,
1006 folder,
1007 theme_id,
1008 user,
1009 password));
1012 gboolean irreco_webdb_cache_get_buttons(IrrecoWebdbCache *self,
1013 gint theme_id,
1014 IrrecoStringTable **button_list)
1016 IrrecoWebdbClient *client = (IrrecoWebdbClient *) self->private;
1017 gboolean success = TRUE;
1018 IRRECO_ENTER
1020 IRRECO_RETRY_LOOP_START(self->loop)
1021 if (irreco_webdb_cache_test(self) == FALSE) break;
1022 success = irreco_webdb_client_get_buttons(client,
1023 theme_id,
1024 button_list);
1025 if (success) break;
1026 irreco_webdb_client_get_error_msg(client, self->error_msg);
1027 IRRECO_RETRY_LOOP_END(self->loop)
1028 IRRECO_RETURN_BOOL(success)
1031 gboolean irreco_webdb_cache_get_button_by_id(IrrecoWebdbCache *self,
1032 gint button_id,
1033 const char *theme_button_dir)
1035 IrrecoWebdbClient *client = (IrrecoWebdbClient *) self->private;
1036 gboolean success = TRUE;
1037 IRRECO_ENTER
1039 IRRECO_RETRY_LOOP_START(self->loop)
1040 if (irreco_webdb_cache_test(self) == FALSE) break;
1041 success = irreco_webdb_client_get_button_by_id(client,
1042 button_id,
1043 theme_button_dir);
1044 if (success) break;
1045 irreco_webdb_client_get_error_msg(client, self->error_msg);
1046 IRRECO_RETRY_LOOP_END(self->loop)
1048 IRRECO_RETURN_BOOL(success)
1053 * Fetches remote categories from DB
1055 * @param categories points to internally allocated storage in the cache
1056 * and must not be freed
1057 * @return TRUE if categories are fetched succesfully, FALSE otherwise.
1059 gboolean irreco_webdb_cache_get_remote_categories(IrrecoWebdbCache *self,
1060 IrrecoStringTable **categories)
1062 IRRECO_ENTER
1064 if (self->remote_categories == NULL) {
1065 gboolean success = FALSE;
1067 IRRECO_RETRY_LOOP_START(self->loop)
1068 if (irreco_webdb_cache_test(self) == FALSE) break;
1069 success = irreco_webdb_client_get_remote_categories(
1070 self->private, &self->remote_categories);
1071 if (success) {
1072 break;
1074 irreco_webdb_client_get_error_msg(self->private,
1075 self->error_msg);
1076 IRRECO_RETRY_LOOP_END(self->loop)
1078 if (success == FALSE) IRRECO_RETURN_BOOL(FALSE);
1080 irreco_string_table_sort_abc(self->remote_categories);
1083 *categories = self->remote_categories;
1084 IRRECO_RETURN_BOOL(TRUE)
1088 * Fetches remote manufacturers from DB.
1090 * @param category contains category in which type manufacturers are returned.
1091 * @param manufacturers points to internally allocated storage in the cache
1092 * and must not be freed
1093 * @return TRUE if manufacturers are fetched succesfully, FALSE otherwise.
1095 gboolean irreco_webdb_cache_get_remote_manufacturers(IrrecoWebdbCache *self,
1096 const gchar *category,
1097 IrrecoStringTable **manufacturers)
1099 IrrecoStringTable *categories;
1100 IrrecoStringTable *manufacturer_list;
1101 IRRECO_ENTER
1103 if (!irreco_webdb_cache_get_remote_categories(self, &categories)) {
1104 IRRECO_RETURN_BOOL(FALSE)
1107 if (!irreco_string_table_exists(categories, category)) {
1108 g_string_printf(self->error_msg, "%s",
1109 "Can't find category");
1110 IRRECO_RETURN_BOOL(FALSE)
1113 irreco_string_table_get(categories, category,
1114 (gpointer *) &manufacturer_list);
1116 if (manufacturer_list == NULL) {
1117 gboolean success = FALSE;
1118 IRRECO_RETRY_LOOP_START(self->loop)
1119 if (irreco_webdb_cache_test(self) == FALSE) break;
1120 success = irreco_webdb_client_get_remote_manufacturers(
1121 self->private, category, &manufacturer_list);
1123 if (success) break;
1124 irreco_webdb_client_get_error_msg(self->private,
1125 self->error_msg);
1126 IRRECO_RETRY_LOOP_END(self->loop)
1128 if (success == FALSE) IRRECO_RETURN_BOOL(FALSE);
1130 irreco_string_table_sort_abc (manufacturer_list);
1131 irreco_string_table_change_data(categories, category,
1132 manufacturer_list);
1135 irreco_string_table_get(categories, category,
1136 (gpointer *) manufacturers);
1138 IRRECO_RETURN_BOOL(TRUE)
1142 * Fetches remote models belonging given category and manufacturer.
1144 * @param models points to internally allocated storage in the cache
1145 * and must not be freed
1146 * @return TRUE if models are fetched succesfully, FALSE otherwise.
1148 gboolean irreco_webdb_cache_get_remote_models(IrrecoWebdbCache *self,
1149 const gchar *category,
1150 const gchar *manufacturer,
1151 IrrecoStringTable **models)
1153 IrrecoStringTable *manufacturer_list;
1154 IrrecoStringTable *model_list;
1155 IRRECO_ENTER
1157 if (!irreco_webdb_cache_get_remote_manufacturers(self, category,
1158 &manufacturer_list)) {
1159 IRRECO_RETURN_BOOL(FALSE)
1162 if (!irreco_string_table_exists(manufacturer_list, manufacturer)) {
1163 g_string_printf(self->error_msg, "%s",
1164 "Can't find manufacturer");
1165 IRRECO_RETURN_BOOL(FALSE)
1168 irreco_string_table_get(manufacturer_list, manufacturer,
1169 (gpointer *) &model_list);
1171 if(model_list == NULL) {
1172 gboolean success = FALSE;
1174 IRRECO_RETRY_LOOP_START(self->loop)
1175 if (irreco_webdb_cache_test(self) == FALSE) break;
1176 success = irreco_webdb_client_get_remote_models(
1177 self->private, category, manufacturer,
1178 &model_list);
1180 if (success) break;
1181 irreco_webdb_client_get_error_msg(self->private,
1182 self->error_msg);
1183 IRRECO_RETRY_LOOP_END(self->loop)
1185 if (success == FALSE) IRRECO_RETURN_BOOL(FALSE);
1187 irreco_string_table_sort_abc (model_list);
1188 irreco_string_table_change_data(manufacturer_list, manufacturer,
1189 model_list);
1192 irreco_string_table_get(manufacturer_list, manufacturer,
1193 (gpointer *) models);
1194 IRRECO_RETURN_BOOL(TRUE)
1198 * Fetches creators belonging given category, manufacturer and model.
1200 * @param creators points to internally allocated storage in the cache
1201 * and must not be freed
1202 * @return TRUE if configs are fetched succesfully, FALSE otherwise.
1204 gboolean irreco_webdb_cache_get_remote_creators(IrrecoWebdbCache *self,
1205 const gchar *category,
1206 const gchar *manufacturer,
1207 const gchar *model,
1208 IrrecoStringTable **creators)
1210 IrrecoStringTable * model_list;
1211 IrrecoStringTable * creator_list;
1212 IRRECO_ENTER
1214 if (!irreco_webdb_cache_get_remote_models(self, category, manufacturer,
1215 &model_list)) {
1216 IRRECO_RETURN_BOOL(FALSE)
1219 if (!irreco_string_table_exists(model_list, model)) {
1220 g_string_printf(self->error_msg, "%s",
1221 "Can't find model");
1222 IRRECO_RETURN_BOOL(FALSE)
1225 irreco_string_table_get(model_list, model, (gpointer *) &creator_list);
1227 if(creator_list == NULL) {
1228 gboolean success = FALSE;
1230 IRRECO_RETRY_LOOP_START(self->loop)
1231 if (irreco_webdb_cache_test(self) == FALSE) break;
1232 success = irreco_webdb_client_get_remote_creators(
1233 self->private, category, manufacturer,
1234 model, &creator_list);
1236 if (success) break;
1237 irreco_webdb_client_get_error_msg(self->private,
1238 self->error_msg);
1239 IRRECO_RETRY_LOOP_END(self->loop)
1241 if (success == FALSE) IRRECO_RETURN_BOOL(FALSE);
1243 irreco_string_table_change_data(model_list, model,
1244 creator_list);
1247 irreco_string_table_get(model_list, model, (gpointer *) creators);
1248 IRRECO_RETURN_BOOL(TRUE)
1252 * Fetches creators belonging given category, manufacturer and model.
1254 * @param creators points to internally allocated storage in the cache
1255 * and must not be freed
1256 * @return TRUE if configs are fetched succesfully, FALSE otherwise.
1258 gboolean irreco_webdb_cache_get_remotes(IrrecoWebdbCache *self,
1259 const gchar *category,
1260 const gchar *manufacturer,
1261 const gchar *model,
1262 const gchar *creator,
1263 GList **remote_list)
1265 IrrecoStringTable * creator_list;
1266 IRRECO_ENTER
1268 if (!irreco_webdb_cache_get_remote_creators(self, category,
1269 manufacturer, model,
1270 &creator_list)) {
1271 IRRECO_RETURN_BOOL(FALSE)
1274 if (!irreco_string_table_exists(creator_list, creator)) {
1275 g_string_printf(self->error_msg, "%s",
1276 "Can't find creator");
1277 IRRECO_RETURN_BOOL(FALSE)
1280 irreco_string_table_get(creator_list, creator,
1281 (gpointer *) remote_list);
1283 if(*remote_list == NULL) {
1284 gboolean success = FALSE;
1286 IRRECO_RETRY_LOOP_START(self->loop)
1287 if (irreco_webdb_cache_test(self) == FALSE) break;
1288 success = irreco_webdb_client_get_remotes(self->private,
1289 category, manufacturer, model,
1290 creator, remote_list);
1292 if (success) break;
1293 irreco_webdb_client_get_error_msg(self->private,
1294 self->error_msg);
1295 IRRECO_RETRY_LOOP_END(self->loop)
1297 if (success == FALSE) IRRECO_RETURN_BOOL(FALSE);
1299 irreco_string_table_change_data(creator_list, creator,
1300 *remote_list);
1303 irreco_string_table_get(creator_list, creator,
1304 (gpointer *) remote_list);
1305 IRRECO_RETURN_BOOL(TRUE)
1309 * Fetches remote by given ID.
1311 * @param remote points to internally allocated storage in the cache
1312 * and must not be freed
1313 * @return TRUE if remote is fetched succesfully, FALSE otherwise.
1315 gboolean irreco_webdb_cache_get_remote_by_id(IrrecoWebdbCache *self,
1316 gint id,
1317 IrrecoWebdbRemote **remote)
1319 IRRECO_ENTER
1321 if (g_hash_table_lookup(self->remote_id_hash,
1322 (gconstpointer) &id) == NULL) {
1323 gboolean success = FALSE;
1325 IRRECO_RETRY_LOOP_START(self->loop)
1326 if (irreco_webdb_cache_test(self) == FALSE) break;
1327 success = irreco_webdb_client_get_remote_by_id(
1328 self->private, id, remote);
1330 if (success) break;
1331 irreco_webdb_client_get_error_msg(self->private,
1332 self->error_msg);
1333 IRRECO_RETRY_LOOP_END(self->loop)
1335 if (success == FALSE) IRRECO_RETURN_BOOL(FALSE);
1337 g_hash_table_insert(self->remote_id_hash,
1338 (gpointer) &((*remote)->id),
1339 (gpointer) *remote);
1342 *remote = g_hash_table_lookup(self->remote_id_hash,
1343 (gconstpointer) &id);
1345 IRRECO_RETURN_BOOL(TRUE)
1349 * Fetches configurations belonging given remote_id.
1351 * @param configs points to internally allocated storage in the cache
1352 * and must not be freed
1353 * @return TRUE if remote is fetched succesfully, FALSE otherwise.
1355 gboolean irreco_webdb_cache_get_configurations_of_remote(IrrecoWebdbCache *self,
1356 gint remote_id,
1357 GList **configs)
1359 IrrecoWebdbRemote *remote;
1360 GList *configuration_list = NULL;
1361 gboolean success = FALSE;
1362 IRRECO_ENTER
1364 if (!irreco_webdb_cache_get_remote_by_id(self, remote_id, &remote)) {
1365 IRRECO_RETURN_BOOL(FALSE)
1369 if (remote->configurations != NULL) {
1370 goto end;
1373 IRRECO_RETRY_LOOP_START(self->loop)
1374 if (irreco_webdb_cache_test(self) == FALSE) break;
1375 success = irreco_webdb_client_get_configurations_of_remote(
1376 self->private, remote_id,
1377 &configuration_list);
1379 if (success) break;
1380 irreco_webdb_client_get_error_msg(self->private,
1381 self->error_msg);
1382 IRRECO_RETRY_LOOP_END(self->loop)
1384 if (success == FALSE) IRRECO_RETURN_BOOL(FALSE);
1386 configuration_list = g_list_first(configuration_list);
1387 while(configuration_list) {
1388 irreco_webdb_remote_add_configuration_id(remote,
1389 GPOINTER_TO_INT(configuration_list->data));
1390 configuration_list = configuration_list->next;
1392 g_list_free(configuration_list);
1394 end:
1395 *configs = remote->configurations;
1397 IRRECO_RETURN_BOOL(TRUE)
1401 * Fetches themes belonging given remote_id.
1403 * @param themes points to internally allocated storage in the cache
1404 * and must not be freed
1405 * @return TRUE if remote is fetched succesfully, FALSE otherwise.
1407 gboolean irreco_webdb_cache_get_themes_of_remote(IrrecoWebdbCache *self,
1408 gint remote_id,
1409 GList **themes)
1411 IrrecoWebdbRemote *remote;
1412 GList *theme_list = NULL;
1413 gboolean success = FALSE;
1414 IRRECO_ENTER
1416 if (!irreco_webdb_cache_get_remote_by_id(self, remote_id, &remote)) {
1417 IRRECO_RETURN_BOOL(FALSE)
1420 if (remote->themes != NULL) {
1421 goto end;
1424 IRRECO_RETRY_LOOP_START(self->loop)
1425 if (irreco_webdb_cache_test(self) == FALSE) break;
1426 success = irreco_webdb_client_get_themes_of_remote(
1427 self->private, remote_id,
1428 &theme_list);
1430 if (success) break;
1431 irreco_webdb_client_get_error_msg(self->private,
1432 self->error_msg);
1433 IRRECO_RETRY_LOOP_END(self->loop)
1435 if (success == FALSE) IRRECO_RETURN_BOOL(FALSE);
1437 theme_list = g_list_first(theme_list);
1438 while(theme_list) {
1439 irreco_webdb_remote_add_theme_id(remote, GPOINTER_TO_INT(
1440 theme_list->data));
1441 theme_list = theme_list->next;
1443 g_list_free(theme_list);
1445 end:
1446 *themes = remote->themes;
1448 IRRECO_RETURN_BOOL(TRUE)
1451 /** @} */
1453 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
1454 /* Events and Callbacks */
1455 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
1457 /** @} */