1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* e-source-list.c
*
* Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU Lesser General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
* Author: Ettore Perazzoli <ettore@ximian.com>
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
#include "e-source-list.h"
#define E_SOURCE_LIST_GET_PRIVATE(obj) \
(G_TYPE_INSTANCE_GET_PRIVATE \
((obj), E_TYPE_SOURCE_LIST, ESourceListPrivate))
struct _ESourceListPrivate {
GConfClient *gconf_client;
gchar *gconf_path;
gint gconf_notify_id;
GSList *groups;
gboolean ignore_group_changed;
gint sync_idle_id;
};
/* Signals. */
enum {
CHANGED,
GROUP_REMOVED,
GROUP_ADDED,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0 };
/* Forward declarations. */
static gboolean sync_idle_callback (ESourceList *list);
static void group_changed_callback (ESourceGroup *group,
ESourceList *list);
static void conf_changed_callback (GConfClient *client,
guint connection_id,
GConfEntry *entry,
ESourceList *list);
/* Utility functions. */
static void
load_from_gconf (ESourceList *list)
{
GSList *conf_list, *p, *q;
GSList *new_groups_list;
GHashTable *new_groups_hash;
gboolean changed = FALSE;
gint pos;
conf_list = gconf_client_get_list (list->priv->gconf_client,
list->priv->gconf_path,
GCONF_VALUE_STRING, NULL);
new_groups_list = NULL;
new_groups_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
for (p = conf_list, pos = 0; p != NULL; p = p->next, pos++) {
const xmlChar *xml = p->data;
xmlDocPtr xmldoc;
gchar *group_uid;
ESourceGroup *existing_group;
xmldoc = xmlParseDoc (xml);
if (xmldoc == NULL)
continue;
group_uid = e_source_group_uid_from_xmldoc (xmldoc);
if (group_uid == NULL) {
xmlFreeDoc (xmldoc);
continue;
}
existing_group = e_source_list_peek_group_by_uid (list, group_uid);
if (g_hash_table_lookup (new_groups_hash, existing_group) != NULL) {
xmlFreeDoc (xmldoc);
g_free (group_uid);
continue;
}
if (existing_group == NULL) {
ESourceGroup *new_group = e_source_group_new_from_xmldoc (xmldoc);
if (new_group != NULL) {
g_signal_connect (new_group, "changed", G_CALLBACK (group_changed_callback), list);
new_groups_list = g_slist_prepend (new_groups_list, new_group);
g_hash_table_insert (new_groups_hash, new_group, new_group);
g_signal_emit (list, signals[GROUP_ADDED], 0, new_group);
changed = TRUE;
}
} else {
gboolean group_changed;
list->priv->ignore_group_changed++;
if (e_source_group_update_from_xmldoc (existing_group, xmldoc, &group_changed)) {
new_groups_list = g_slist_prepend (new_groups_list, existing_group);
g_object_ref (existing_group);
g_hash_table_insert (new_groups_hash, existing_group, existing_group);
if (group_changed)
changed = TRUE;
}
list->priv->ignore_group_changed--;
}
xmlFreeDoc (xmldoc);
g_free (group_uid);
}
new_groups_list = g_slist_reverse (new_groups_list);
g_slist_foreach (conf_list, (GFunc) g_free, NULL);
g_slist_free (conf_list);
/* Emit "group_removed" and disconnect the "changed" signal for all the
* groups that we haven't found in the new list. Also, check if the
* order has changed. */
q = new_groups_list;
for (p = list->priv->groups; p != NULL; p = p->next) {
ESourceGroup *group = E_SOURCE_GROUP (p->data);
if (g_hash_table_lookup (new_groups_hash, group) == NULL) {
changed = TRUE;
g_signal_emit (list, signals[GROUP_REMOVED], 0, group);
g_signal_handlers_disconnect_by_func (group, group_changed_callback, list);
}
if (!changed && q != NULL) {
if (q->data != p->data)
changed = TRUE;
q = q->next;
}
}
g_hash_table_destroy (new_groups_hash);
/* Replace the original group list with the new one. */
g_slist_foreach (list->priv->groups, (GFunc) g_object_unref, NULL);
g_slist_free (list->priv->groups);
list->priv->groups = new_groups_list;
/* FIXME if the order changes, the function doesn't notice. */
if (changed)
g_signal_emit (list, signals[CHANGED], 0);
}
static void
remove_group (ESourceList *list,
ESourceGroup *group)
{
list->priv->groups = g_slist_remove (list->priv->groups, group);
g_signal_emit (list, signals[GROUP_REMOVED], 0, group);
g_object_unref (group);
g_signal_emit (list, signals[CHANGED], 0);
}
/* Callbacks. */
static gboolean
sync_idle_callback (ESourceList *list)
{
GError *error = NULL;
g_return_val_if_fail (E_IS_SOURCE_LIST (list), FALSE);
g_object_ref (list);
if (!e_source_list_sync (list, &error)) {
g_warning ("Cannot update \"%s\": %s", list->priv->gconf_path, error ? error->message : "Unknown error");
g_error_free (error);
}
list->priv->sync_idle_id= 0;
g_object_unref (list);
return FALSE;
}
static void
group_changed_callback (ESourceGroup *group,
ESourceList *list)
{
g_return_if_fail (E_IS_SOURCE_LIST (list));
g_object_ref (list);
if (!list->priv->ignore_group_changed)
g_signal_emit (list, signals[CHANGED], 0);
if (list->priv->sync_idle_id == 0)
list->priv->sync_idle_id = g_idle_add ((GSourceFunc) sync_idle_callback, list);
g_object_unref (list);
}
static void
conf_changed_callback (GConfClient *client,
guint connection_id,
GConfEntry *entry,
ESourceList *list)
{
g_return_if_fail (E_IS_SOURCE_LIST (list));
g_object_ref (list);
load_from_gconf (list);
g_object_unref (list);
}
/* GObject methods. */
G_DEFINE_TYPE (ESourceList, e_source_list, G_TYPE_OBJECT)
static void
source_list_dispose (GObject *object)
{
ESourceListPrivate *priv;
priv = E_SOURCE_LIST_GET_PRIVATE (object);
if (priv->gconf_client != NULL && priv->gconf_notify_id != 0) {
gconf_client_notify_remove (priv->gconf_client, priv->gconf_notify_id);
priv->gconf_notify_id = 0;
}
if (priv->sync_idle_id != 0) {
GError *error = NULL;
g_source_remove (priv->sync_idle_id);
priv->sync_idle_id = 0;
if (!e_source_list_sync (E_SOURCE_LIST (object), &error))
g_warning ("Could not update \"%s\": %s",
priv->gconf_path, error->message);
}
if (priv->groups != NULL) {
GSList *p;
for (p = priv->groups; p != NULL; p = p->next)
g_object_unref (p->data);
g_slist_free (priv->groups);
priv->groups = NULL;
}
if (priv->gconf_client != NULL) {
g_object_unref (priv->gconf_client);
priv->gconf_client = NULL;
}
/* Chain up to parent's dispose() method. */
G_OBJECT_CLASS (e_source_list_parent_class)->dispose (object);
}
static void
source_list_finalize (GObject *object)
{
ESourceListPrivate *priv;
priv = E_SOURCE_LIST_GET_PRIVATE (object);
g_free (priv->gconf_path);
/* Chain up to parent's finalize() method. */
G_OBJECT_CLASS (e_source_list_parent_class)->finalize (object);
}
/* Initialization. */
static void
e_source_list_class_init (ESourceListClass *class)
{
GObjectClass *object_class;
g_type_class_add_private (class, sizeof (ESourceListPrivate));
object_class = G_OBJECT_CLASS (class);
object_class->dispose = source_list_dispose;
object_class->finalize = source_list_finalize;
signals[CHANGED] = g_signal_new (
"changed",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (ESourceListClass, changed),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
signals[GROUP_REMOVED] = g_signal_new (
"group_removed",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (ESourceListClass, group_removed),
NULL, NULL,
g_cclosure_marshal_VOID__OBJECT,
G_TYPE_NONE, 1,
E_TYPE_SOURCE_GROUP);
signals[GROUP_ADDED] = g_signal_new (
"group_added",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (ESourceListClass, group_added),
NULL, NULL,
g_cclosure_marshal_VOID__OBJECT,
G_TYPE_NONE, 1,
E_TYPE_SOURCE_GROUP);
}
static void
e_source_list_init (ESourceList *source_list)
{
source_list->priv = E_SOURCE_LIST_GET_PRIVATE (source_list);
}
/* Public methods. */
ESourceList *
e_source_list_new (void)
{
ESourceList *list = g_object_new (e_source_list_get_type (), NULL);
return list;
}
ESourceList *
e_source_list_new_for_gconf (GConfClient *client,
const gchar *path)
{
ESourceList *list;
g_return_val_if_fail (GCONF_IS_CLIENT (client), NULL);
g_return_val_if_fail (path != NULL, NULL);
list = g_object_new (e_source_list_get_type (), NULL);
list->priv->gconf_path = g_strdup (path);
list->priv->gconf_client = client;
g_object_ref (client);
gconf_client_add_dir (client, path, GCONF_CLIENT_PRELOAD_ONELEVEL, NULL);
list->priv->gconf_notify_id
= gconf_client_notify_add (client, path,
(GConfClientNotifyFunc) conf_changed_callback, list,
NULL, NULL);
load_from_gconf (list);
return list;
}
ESourceList *
e_source_list_new_for_gconf_default (const gchar *path)
{
ESourceList *list;
g_return_val_if_fail (path != NULL, NULL);
list = g_object_new (e_source_list_get_type (), NULL);
list->priv->gconf_path = g_strdup (path);
list->priv->gconf_client = gconf_client_get_default ();
gconf_client_add_dir (list->priv->gconf_client, path, GCONF_CLIENT_PRELOAD_ONELEVEL, NULL);
list->priv->gconf_notify_id
= gconf_client_notify_add (list->priv->gconf_client, path,
(GConfClientNotifyFunc) conf_changed_callback, list,
NULL, NULL);
load_from_gconf (list);
return list;
}
/**
* e_source_list_peek_groups:
*
* Return value: (transfer none) (element-type ESourceGroup):
*/
GSList *
e_source_list_peek_groups (ESourceList *list)
{
g_return_val_if_fail (E_IS_SOURCE_LIST (list), NULL);
return list->priv->groups;
}
/**
* e_source_list_peek_group_by_uid:
*
* Return value: (transfer none): the #ESourceGroup
*/
ESourceGroup *
e_source_list_peek_group_by_uid (ESourceList *list,
const gchar *uid)
{
GSList *p;
g_return_val_if_fail (E_IS_SOURCE_LIST (list), NULL);
g_return_val_if_fail (uid != NULL, NULL);
for (p = list->priv->groups; p != NULL; p = p->next) {
ESourceGroup *group = E_SOURCE_GROUP (p->data);
if (strcmp (e_source_group_peek_uid (group), uid) == 0)
return group;
}
return NULL;
}
/**
* e_source_list_peek_group_by_base_uri:
* @list: an #ESourceList
* @base_uri: a group base URI
*
* Returns the first #ESourceGroup having the given base URI.
* The base URI is usually just the URI scheme, such as "http://".
* If no such group is present in @list, the function returns %NULL.
*
* Returns: (transfer none): an #ESourceGroup with a matching base URI, or %NULL
*
* Since: 2.28
**/
ESourceGroup *
e_source_list_peek_group_by_base_uri (ESourceList *list,
const gchar *base_uri)
{
GSList *p;
gint len;
g_return_val_if_fail (E_IS_SOURCE_LIST (list), NULL);
g_return_val_if_fail (base_uri != NULL, NULL);
len = strlen (base_uri);
for (p = list->priv->groups; p != NULL; p = p->next) {
ESourceGroup *group = E_SOURCE_GROUP (p->data);
const gchar *buri = e_source_group_peek_base_uri (group);
if (buri && g_ascii_strncasecmp (buri, base_uri, len) == 0)
return group;
}
return NULL;
}
struct property_check_struct {
ESourceGroup *group;
gboolean same;
};
static void
check_group_property (const gchar *property_name,
const gchar *property_value,
struct property_check_struct *pcs)
{
gchar *value;
g_return_if_fail (property_name != NULL);
g_return_if_fail (property_value != NULL);
g_return_if_fail (pcs != NULL);
g_return_if_fail (pcs->group != NULL);
value = e_source_group_get_property (pcs->group, property_name);
pcs->same = pcs->same && value && g_ascii_strcasecmp (property_value, value) == 0;
g_free (value);
}
/**
* e_source_list_peek_group_by_properties:
*
* Peeks group by its properties. Parameters are pairs of strings
* property_name, property_value, terminated by NULL! ESourceGroup
* is returned only if matches all the properties. Values are compared
* case insensitively.
*
* Returns: (transfer none): the #ESourceGroup
*
* Since: 2.28
**/
ESourceGroup *
e_source_list_peek_group_by_properties (ESourceList *list,
const gchar *property_name,
...)
{
GSList *p;
va_list ap;
GHashTable *props;
g_return_val_if_fail (E_IS_SOURCE_LIST (list), NULL);
g_return_val_if_fail (property_name != NULL, NULL);
props = g_hash_table_new (g_str_hash, g_str_equal);
va_start (ap, property_name);
while (property_name) {
const gchar *value = va_arg (ap, const gchar *);
if (!value)
break;
g_hash_table_insert (props, (gpointer) property_name, (gpointer) value);
property_name = va_arg (ap, const gchar *);
}
va_end (ap);
for (p = list->priv->groups; p != NULL; p = p->next) {
struct property_check_struct pcs;
pcs.group = E_SOURCE_GROUP (p->data);
pcs.same = TRUE;
g_hash_table_foreach (props, (GHFunc) check_group_property, &pcs);
if (pcs.same) {
g_hash_table_unref (props);
return pcs.group;
}
}
g_hash_table_unref (props);
return NULL;
}
/**
* e_source_list_peek_source_by_uid:
*
* Return value: (transfer none): the #ESource
*/
ESource *
e_source_list_peek_source_by_uid (ESourceList *list,
const gchar *uid)
{
GSList *p;
g_return_val_if_fail (E_IS_SOURCE_LIST (list), NULL);
g_return_val_if_fail (uid != NULL, NULL);
for (p = list->priv->groups; p != NULL; p = p->next) {
ESourceGroup *group = E_SOURCE_GROUP (p->data);
ESource *source;
source = e_source_group_peek_source_by_uid (group, uid);
if (source)
return source;
}
return NULL;
}
/**
* e_source_list_peek_source_any:
*
* Returns: (transfer none): the #ESource
*/
ESource *
e_source_list_peek_source_any (ESourceList *list)
{
GSList *p;
g_return_val_if_fail (E_IS_SOURCE_LIST (list), NULL);
for (p = list->priv->groups; p != NULL; p = p->next) {
ESourceGroup *group = E_SOURCE_GROUP (p->data);
GSList *sources;
sources = e_source_group_peek_sources (group);
if (sources && sources->data)
return E_SOURCE (sources->data);
}
return NULL;
}
/**
* e_source_list_peek_default_source:
* @list: an #ESourceList
*
* Attempts to find a default #ESource in @list by looking for
* a source with a property named "default", or else a source with a
* property named "system". If no such #ESource exists, the function
* returns %NULL.
*
* Returns: (transfer none) (allow-none): the default #ESource in @list, or %NULL
*
* Since: 2.32
**/
ESource *
e_source_list_peek_default_source (ESourceList *list)
{
ESource *system_source = NULL;
GSList *groups;
GSList *iter1;
g_return_val_if_fail (E_IS_SOURCE_LIST (list), NULL);
groups = e_source_list_peek_groups (list);
for (iter1 = groups; iter1 != NULL; iter1 = iter1->next) {
ESourceGroup *source_group;
GSList *sources;
GSList *iter2;
gboolean is_local_group;
source_group = E_SOURCE_GROUP (iter1->data);
sources = e_source_group_peek_sources (source_group);
is_local_group = e_source_group_peek_base_uri (source_group)
&& g_str_equal (e_source_group_peek_base_uri (source_group), "local:");
for (iter2 = sources; iter2 != NULL; iter2 = iter2->next) {
ESource *source;
source = E_SOURCE (iter2->data);
/* If we find the default source, we're done. */
if (e_source_get_property (source, "default"))
return source;
/* Make a note of the system source. If we fail
* to find a default source we fall back to this. */
if (e_source_get_property (source, "system") ||
(is_local_group && !system_source && e_source_peek_relative_uri (source)
&& g_str_equal (e_source_peek_relative_uri (source), "system")))
system_source = source;
}
}
return system_source;
}
gboolean
e_source_list_add_group (ESourceList *list,
ESourceGroup *group,
gint position)
{
const gchar *uid;
g_return_val_if_fail (E_IS_SOURCE_LIST (list), FALSE);
g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE);
uid = e_source_group_peek_uid (group);
if (e_source_list_peek_group_by_uid (list, uid) != NULL)
return FALSE;
list->priv->groups = g_slist_insert (
list->priv->groups, g_object_ref (group), position);
g_signal_connect (
group, "changed",
G_CALLBACK (group_changed_callback), list);
g_signal_emit (list, signals[GROUP_ADDED], 0, group);
g_signal_emit (list, signals[CHANGED], 0);
return TRUE;
}
/**
* e_source_list_remove_group:
* @list: an #ESourceList
* @group: an #ESourceGroup
*
* Removes the first #ESourceGroup with a unique ID matching @group
* (possibly @group itself) from @list. The function returns %TRUE if a
* matching group was found, otherwise %FALSE.
*
* Returns: %TRUE if an #ESourceGroup was removed, %FALSE otherwise
**/
gboolean
e_source_list_remove_group (ESourceList *list,
ESourceGroup *group)
{
const gchar *uid;
g_return_val_if_fail (E_IS_SOURCE_LIST (list), FALSE);
g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE);
uid = e_source_group_peek_uid (group);
if (e_source_list_peek_group_by_uid (list, uid) == NULL)
return FALSE;
remove_group (list, group);
return TRUE;
}
/**
* e_source_list_remove_group_by_uid:
* @list: an #ESourceList
* @uid: the unique ID of an #ESourceGroup
*
* Removes the first #ESourceGroup with the given unique ID from @list.
* The function returns %TRUE if a matching group was found, otherwise
* %FALSE.
*
* Returns: %TRUE if an #ESourceGroup was removed, %FALSE otherwise
**/
gboolean
e_source_list_remove_group_by_uid (ESourceList *list,
const gchar *uid)
{
ESourceGroup *group;
g_return_val_if_fail (E_IS_SOURCE_LIST (list), FALSE);
g_return_val_if_fail (uid != NULL, FALSE);
group = e_source_list_peek_group_by_uid (list, uid);
if (group== NULL)
return FALSE;
remove_group (list, group);
return TRUE;
}
/**
* e_source_list_ensure_group:
* @list: an #ESourceList
* @name: a localized group name
* @base_uri: a group base URI
* @ret_it: whether to return the group
*
* Ensures an #ESourceGroup with the given base URI exists in @list, and
* renames its to the given name. If @ret_it is %TRUE, the matching group
* will be returned and should be unreferenced with g_object_unref().
*
* Returns: (transfer full): the matching #ESourceGroup if @ret_it is %TRUE,
* otherwise %NULL
*
* Since: 2.28
**/
ESourceGroup *
e_source_list_ensure_group (ESourceList *list,
const gchar *name,
const gchar *base_uri,
gboolean ret_it)
{
ESourceGroup *group;
g_return_val_if_fail (E_IS_SOURCE_LIST (list), NULL);
g_return_val_if_fail (name != NULL, NULL);
g_return_val_if_fail (base_uri != NULL, NULL);
group = e_source_list_peek_group_by_base_uri (list, base_uri);
if (group) {
e_source_group_set_name (group, name);
if (ret_it)
g_object_ref (group);
else
group = NULL;
} else {
group = e_source_group_new (name, base_uri);
if (!e_source_list_add_group (list, group, -1)) {
g_warning ("Could not add source group %s with base uri %s to a source list", name, base_uri);
g_object_unref (group);
group = NULL;
} else {
/* save it now */
e_source_list_sync (list, NULL);
if (!ret_it) {
g_object_unref (group);
group = NULL;
}
}
}
return group;
}
/**
* e_source_list_remove_group_by_base_uri:
* @list: an #ESourceList
* @base_uri: a group base URI
*
* Removes the first #ESourceGroup having the given base URI from @list.
* The base URI is usually just the URI scheme, such as "http://". The
* function returns %TRUE if a matching group was found, otherwise %FALSE.
*
* Returns: %TRUE if an #ESourceGroup was removed, %FALSE otherwise
*
* Since: 2.28
**/
gboolean
e_source_list_remove_group_by_base_uri (ESourceList *list,
const gchar *base_uri)
{
ESourceGroup *group;
g_return_val_if_fail (E_IS_SOURCE_LIST (list), FALSE);
g_return_val_if_fail (base_uri != NULL, FALSE);
group = e_source_list_peek_group_by_base_uri (list, base_uri);
if (group == NULL)
return FALSE;
remove_group (list, group);
return TRUE;
}
/**
* e_source_list_remove_source_by_uid:
* @list: an #ESourceList
* @uid: the unique ID of an #ESource
*
* Removes the first #ESource with the given unique ID from @list. The
* function returns %TRUE if a matching source was found, otherwise %FALSE.
*
* Returns: %TRUE if an #ESource was removed, %FALSE otherwise
**/
gboolean
e_source_list_remove_source_by_uid (ESourceList *list,
const gchar *uid)
{
GSList *p;
g_return_val_if_fail (E_IS_SOURCE_LIST (list), FALSE);
g_return_val_if_fail (uid != NULL, FALSE);
for (p = list->priv->groups; p != NULL; p = p->next) {
ESourceGroup *group = E_SOURCE_GROUP (p->data);
ESource *source;
source = e_source_group_peek_source_by_uid (group, uid);
if (source)
return e_source_group_remove_source_by_uid (group, uid);
}
return FALSE;
}
/**
* e_source_list_sync:
* @list: an #ESourceList
* @error: return location for a #GError, or %NULL
*
* Writes the contents of @list to GConf. If an error occurs, such as
* the GConf daemon not responding, the function sets @error and returns
* %FALSE.
*
* Returns: %TRUE on success, %FALSE on failure
**/
gboolean
e_source_list_sync (ESourceList *list,
GError **error)
{
GSList *conf_list;
GSList *p;
gboolean retval;
g_return_val_if_fail (E_IS_SOURCE_LIST (list), FALSE);
conf_list = NULL;
for (p = list->priv->groups; p != NULL; p = p->next)
conf_list = g_slist_prepend (
conf_list, e_source_group_to_xml (
E_SOURCE_GROUP (p->data)));
conf_list = g_slist_reverse (conf_list);
if (!e_source_list_is_gconf_updated (list))
retval = gconf_client_set_list (
list->priv->gconf_client,
list->priv->gconf_path,
GCONF_VALUE_STRING,
conf_list, error);
else
retval = TRUE;
g_slist_foreach (conf_list, (GFunc) g_free, NULL);
g_slist_free (conf_list);
return retval;
}
/**
* e_source_list_is_gconf_updated:
* @list: an #ESourceList
*
* Returns %TRUE if the GConf data for @list is up-to-date, %FALSE if
* e_source_list_sync() should be called.
*
* Returns: %TRUE if the GConf data for @list is up-to-date
**/
gboolean
e_source_list_is_gconf_updated (ESourceList *list)
{
gchar *source_group_xml = NULL;
gchar *gconf_xml = NULL;
gchar *group_uid = NULL;
GSList *conf_list = NULL, *temp = NULL, *p = NULL;
xmlDocPtr xmldoc;
ESourceGroup *group = NULL;
GSList *groups = NULL;
gboolean conf_to_list = TRUE, list_to_conf = TRUE;
g_return_val_if_fail (list != NULL, FALSE);
conf_list = gconf_client_get_list (list->priv->gconf_client,
list->priv->gconf_path,
GCONF_VALUE_STRING, NULL);
/* From conf to list */
for (temp = conf_list; temp != NULL; temp = temp->next) {
gconf_xml = (gchar *) temp->data;
xmldoc = xmlParseDoc ((const xmlChar *) gconf_xml);
if (xmldoc == NULL)
continue;
group_uid = e_source_group_uid_from_xmldoc (xmldoc);
group = e_source_list_peek_group_by_uid (list, group_uid);
g_free (group_uid);
xmlFreeDoc (xmldoc);
if (group) {
source_group_xml = e_source_group_to_xml (group);
if (e_source_group_xmlstr_equal (gconf_xml, source_group_xml)) {
g_free (source_group_xml);
continue;
}
else {
conf_to_list = FALSE;
g_free (source_group_xml);
break;
}
} else {
conf_to_list = FALSE;
break;
}
}
/* If there is mismatch, free the conf_list and return FALSE */
if (!conf_to_list) {
for (p = conf_list; p != NULL; p = p->next) {
gconf_xml = (gchar *) p->data;
g_free (gconf_xml);
}
g_slist_free (conf_list);
return FALSE;
}
groups = e_source_list_peek_groups (list);
/* From list to conf */
for (p = groups; p != NULL; p = p->next) {
group = E_SOURCE_GROUP (p->data);
source_group_xml = e_source_group_to_xml (group);
for (temp = conf_list; temp != NULL; temp = temp->next) {
gconf_xml = (gchar *) temp->data;
if (!e_source_group_xmlstr_equal (gconf_xml, source_group_xml))
continue;
else
break;
}
g_free (source_group_xml);
if (!temp) {
list_to_conf = FALSE;
break;
}
}
for (p = conf_list; p != NULL; p = p->next) {
gconf_xml = (gchar *) p->data;
g_free (gconf_xml);
}
g_slist_free (conf_list);
/* If there is mismatch return FALSE */
if (!list_to_conf)
return FALSE;
return TRUE;
}
|