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"
28 * @addtogroup IrrecoWebdbCache
29 * @ingroup IrrecoWebdb
31 * Caches XML-RPC calls, enables error handling and retries failed calls.
38 * Source file of @ref IrrecoWebdbCache.
43 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
45 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
47 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
48 /* Construction & Destruction */
49 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
52 * @name Construction & Destruction
57 * Create new IrrecoWebdb Object
59 IrrecoWebdbCache
*irreco_webdb_cache_new()
61 IrrecoWebdbCache
*self
;
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
);
83 * Free resources of IrrecoWebdbCache Object.
85 void irreco_webdb_cache_free(IrrecoWebdbCache
*self
)
88 g_string_free(self
->error_msg
, TRUE
);
89 self
->error_msg
= NULL
;
90 irreco_retry_loop_free(self
->loop
);
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);
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
;
110 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
111 /* Private Functions */
112 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
115 * @name Private Functions
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
;
137 if (self
->test_ok
) IRRECO_RETURN_BOOL(self
->test_ok
);
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
);
149 if (sum
== num_a
+ num_b
) {
152 g_string_printf(self
->error_msg
,
153 "Got invalid result from "
154 "sum method. %i + %i = %i. "
156 num_a
, num_b
, (gint
) sum
,
158 IRRECO_PRINTF("%s\n", self
->error_msg
->str
);
159 self
->test_ok
= FALSE
;
162 irreco_webdb_client_get_error_msg(
163 client
, self
->error_msg
);
165 IRRECO_RETRY_LOOP_END(self
->loop
)
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
)
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.
193 irreco_webdb_cache_verify_manufacturer(IrrecoWebdbCache
*self
,
194 const gchar
*category
,
195 IrrecoStringTable
**manufactures
)
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
)
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
,
237 IrrecoStringTable
**configs
)
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
)
251 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
253 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
256 * @name Public Functions
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
,
271 IrrecoWebdbClient
*client
;
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
);
289 /* irreco_webdb_client_add_user failed, get err msg */
290 irreco_webdb_client_get_error_msg(client
,
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
,
311 const gchar
*password
,
315 IrrecoWebdbClient
*client
;
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
,
335 IRRECO_RETURN_BOOL(TRUE
);
337 /* irreco_webdb_client_upload_configuration failed,
339 irreco_webdb_client_get_error_msg(client
,
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
)
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
)
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
);
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;
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
);
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
;
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
);
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
,
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;
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
);
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
;
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
);
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
,
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
,
560 IrrecoStringTable
**configs
)
562 IrrecoStringTable
* config_list
;
563 IrrecoStringTable
* model_list
;
564 IrrecoStringTable
* manufacturer_list
;
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
,
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
);
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
,
607 IrrecoWebdbConf
**config
)
609 IrrecoWebdbConf
* configuration
;
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
);
622 irreco_webdb_client_get_error_msg(client
,
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
,
649 gboolean success
= FALSE
;
650 IrrecoWebdbClient
*client
;
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
);
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
);
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
,
679 gboolean
*user_exists
)
681 IrrecoWebdbClient
*client
;
682 gboolean success
= FALSE
;
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
,
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
;
712 client
= (IrrecoWebdbClient
*) self
->private;
713 IRRECO_RETURN_INT(irreco_webdb_client_get_max_image_size(client
));
719 gint
irreco_webdb_cache_create_theme(IrrecoWebdbCache
*self
,
721 const gchar
*comment
,
722 const gchar
*preview_button
,
725 const gchar
*password
)
727 IrrecoWebdbClient
*client
;
729 client
= (IrrecoWebdbClient
*) self
->private;
730 IRRECO_RETURN_INT(irreco_webdb_client_create_theme(
741 * Set theme downloadable
743 gboolean
irreco_webdb_cache_set_theme_downloadable(IrrecoWebdbCache
*self
,
745 gboolean downloadable
,
747 const gchar
*password
)
749 IrrecoWebdbClient
*client
;
751 client
= (IrrecoWebdbClient
*) self
->private;
752 IRRECO_RETURN_BOOL(irreco_webdb_client_set_theme_downloadable(
763 * @return TRUE if login is succesful, FALSE otherwise.
765 gboolean
irreco_webdb_cache_login(IrrecoWebdbCache
*self
,
767 const gchar
*password
)
769 IrrecoWebdbClient
*client
;
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
);
788 /* irreco_webdb_client_login failed,
790 irreco_webdb_client_get_error_msg(client
,
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;
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
);
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
,
819 IrrecoWebdbTheme
**theme
)
821 IrrecoWebdbTheme
*get_theme
;
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
);
836 irreco_webdb_client_get_error_msg(client
,
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
,
856 IrrecoWebdbTheme
**theme
)
858 gboolean success
= TRUE
;
859 IrrecoWebdbClient
*client
= (IrrecoWebdbClient
*) self
->private;
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
,
867 if (success
== FALSE
) {
868 irreco_webdb_client_get_error_msg(client
, self
->error_msg
);
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
);
884 irreco_webdb_client_get_error_msg(client
, self
->error_msg
);
885 IRRECO_RETRY_LOOP_END(self
->loop
)
890 IRRECO_RETURN_BOOL(success
)
896 gint
irreco_webdb_cache_add_bg_to_theme(IrrecoWebdbCache
*self
,
898 const gchar
*image_hash
,
899 const gchar
*image_name
,
905 const gchar
*password
)
907 IrrecoWebdbClient
*client
;
909 client
= (IrrecoWebdbClient
*) self
->private;
910 IRRECO_RETURN_INT(irreco_webdb_client_add_bg_to_theme(
923 gboolean
irreco_webdb_cache_get_backgrounds(IrrecoWebdbCache
*self
,
925 IrrecoStringTable
**bg_list
){
926 IrrecoWebdbClient
*client
;
927 gboolean success
= TRUE
;
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
,
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
,
945 const char *theme_bg_dir
)
947 IrrecoWebdbClient
*client
= (IrrecoWebdbClient
*) self
->private;
948 gboolean success
= TRUE
;
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
,
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
,
968 const gchar
*text_format_up
,
969 const gchar
*text_format_down
,
973 const gchar
*image_up_hash
,
974 const gchar
*image_up_name
,
975 const guchar
*image_up
,
977 const gchar
*image_down_hash
,
978 const gchar
*image_down_name
,
979 const guchar
*image_down
,
984 const gchar
*password
)
986 IrrecoWebdbClient
*client
;
988 client
= (IrrecoWebdbClient
*) self
->private;
989 IRRECO_RETURN_INT(irreco_webdb_client_add_button_to_theme(
1012 gboolean
irreco_webdb_cache_get_buttons(IrrecoWebdbCache
*self
,
1014 IrrecoStringTable
**button_list
)
1016 IrrecoWebdbClient
*client
= (IrrecoWebdbClient
*) self
->private;
1017 gboolean success
= TRUE
;
1020 IRRECO_RETRY_LOOP_START(self
->loop
)
1021 if (irreco_webdb_cache_test(self
) == FALSE
) break;
1022 success
= irreco_webdb_client_get_buttons(client
,
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
,
1033 const char *theme_button_dir
)
1035 IrrecoWebdbClient
*client
= (IrrecoWebdbClient
*) self
->private;
1036 gboolean success
= TRUE
;
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
,
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
)
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
);
1074 irreco_webdb_client_get_error_msg(self
->private,
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
;
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
);
1124 irreco_webdb_client_get_error_msg(self
->private,
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
,
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
;
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
,
1181 irreco_webdb_client_get_error_msg(self
->private,
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
,
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
,
1208 IrrecoStringTable
**creators
)
1210 IrrecoStringTable
* model_list
;
1211 IrrecoStringTable
* creator_list
;
1214 if (!irreco_webdb_cache_get_remote_models(self
, category
, manufacturer
,
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
);
1237 irreco_webdb_client_get_error_msg(self
->private,
1239 IRRECO_RETRY_LOOP_END(self
->loop
)
1241 if (success
== FALSE
) IRRECO_RETURN_BOOL(FALSE
);
1243 irreco_string_table_change_data(model_list
, model
,
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
,
1262 const gchar
*creator
,
1263 GList
**remote_list
)
1265 IrrecoStringTable
* creator_list
;
1268 if (!irreco_webdb_cache_get_remote_creators(self
, category
,
1269 manufacturer
, model
,
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
);
1293 irreco_webdb_client_get_error_msg(self
->private,
1295 IRRECO_RETRY_LOOP_END(self
->loop
)
1297 if (success
== FALSE
) IRRECO_RETURN_BOOL(FALSE
);
1299 irreco_string_table_change_data(creator_list
, creator
,
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
,
1317 IrrecoWebdbRemote
**remote
)
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
);
1331 irreco_webdb_client_get_error_msg(self
->private,
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
,
1359 IrrecoWebdbRemote
*remote
;
1360 GList
*configuration_list
= NULL
;
1361 gboolean success
= FALSE
;
1364 if (!irreco_webdb_cache_get_remote_by_id(self
, remote_id
, &remote
)) {
1365 IRRECO_RETURN_BOOL(FALSE
)
1369 if (remote
->configurations
!= NULL
) {
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
);
1380 irreco_webdb_client_get_error_msg(self
->private,
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
);
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
,
1411 IrrecoWebdbRemote
*remote
;
1412 GList
*theme_list
= NULL
;
1413 gboolean success
= FALSE
;
1416 if (!irreco_webdb_cache_get_remote_by_id(self
, remote_id
, &remote
)) {
1417 IRRECO_RETURN_BOOL(FALSE
)
1420 if (remote
->themes
!= NULL
) {
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
,
1431 irreco_webdb_client_get_error_msg(self
->private,
1433 IRRECO_RETRY_LOOP_END(self
->loop
)
1435 if (success
== FALSE
) IRRECO_RETURN_BOOL(FALSE
);
1437 theme_list
= g_list_first(theme_list
);
1439 irreco_webdb_remote_add_theme_id(remote
, GPOINTER_TO_INT(
1441 theme_list
= theme_list
->next
;
1443 g_list_free(theme_list
);
1446 *themes
= remote
->themes
;
1448 IRRECO_RETURN_BOOL(TRUE
)
1453 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
1454 /* Events and Callbacks */
1455 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/