/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ /* e-meeting_attendee.h * * Copyright (C) 2001 Ximian, Inc. * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU 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 General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. * * Author: JP Rosevear */ #ifndef _E_MEETING_ATTENDEE_H_ #define _E_MEETING_ATTENDEE_H_ #ifdef HAVE_CONFIG_H #include #endif #include #include #include "e-meeting-types.h" #ifdef __cplusplus extern "C" { #pragma } #endif /* __cplusplus */ #define E_TYPE_MEETING_ATTENDEE (e_meeting_attendee_get_type ()) #define E_MEETING_ATTENDEE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), E_TYPE_MEETING_ATTENDEE, EMeetingAttendee)) #define E_MEETING_ATTENDEE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), E_TYPE_MEETING_ATTENDEE, EMeetingAttendeeClass)) #define E_IS_MEETING_ATTENDEE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), E_TYPE_MEETING_ATTENDEE)) #define E_IS_MEETING_ATTENDEE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((obj), E_TYPE_MEETING_ATTENDEE)) typedef struct _EMeetingAttendee EMeetingAttendee; typedef struct _EMeetingAttendeePrivate EMeetingAttendeePrivate; typedef struct _EMeetingAttendeeClass EMeetingAttendeeClass; /* These specify the type of attendee. Either a person or a resource (e.g. a meeting room). These are used for the Autopick options, where the user can ask for a time when, for example, all people and one resource are free. The default is E_MEETING_ATTENDEE_REQUIRED_PERSON. */ typedef enum { E_MEETING_ATTENDEE_REQUIRED_PERSON, E_MEETING_ATTENDEE_OPTIONAL_PERSON, E_MEETING_ATTENDEE_RESOURCE } EMeetingAttendeeType; typedef enum { E_MEETING_ATTENDEE_EDIT_FULL, E_MEETING_ATTENDEE_EDIT_STATUS, E_MEETING_ATTENDEE_EDIT_NONE } EMeetingAttendeeEditLevel; struct _EMeetingAttendee { GtkObject parent; EMeetingAttendeePrivate *priv; }; struct _EMeetingAttendeeClass { GtkObjectClass parent_class; void (* changed) (EMeetingAttendee *ia); }; GType e_meeting_attendee_get_type (void); GObject *e_meeting_attendee_new (void); GObject *e_meeting_attendee_new_from_e_cal_component_attendee (ECalComponentAttendee *ca); ECalComponentAttendee *e_meeting_attendee_as_e_cal_component_attendee (EMeetingAttendee *ia); const gchar *e_meeting_attendee_get_address (EMeetingAttendee *ia); void e_meeting_attendee_set_address (EMeetingAttendee *ia, gchar *address); gboolean e_meeting_attendee_is_set_address (EMeetingAttendee *ia); const gchar *e_meeting_attendee_get_member (EMeetingAttendee *ia); void e_meeting_attendee_set_member (EMeetingAttendee *ia, gchar *member); gboolean e_meeting_attendee_is_set_member (EMeetingAttendee *ia); icalparameter_cutype e_meeting_attendee_get_cutype (EMeetingAttendee *ia); void e_meeting_attendee_set_cutype (EMeetingAttendee *ia, icalparameter_cutype cutype); icalparameter_role e_meeting_attendee_get_role (EMeetingAttendee *ia); void e_meeting_attendee_set_role (EMeetingAttendee *ia, icalparameter_role role); gboolean e_meeting_attendee_get_rsvp (EMeetingAttendee *ia); void e_meeting_attendee_set_rsvp (EMeetingAttendee *ia, gboolean rsvp); const gchar *e_meeting_attendee_get_delto (EMeetingAttendee *ia); void e_meeting_attendee_set_delto (EMeetingAttendee *ia, gchar *delto); gboolean e_meeting_attendee_is_set_delto (EMeetingAttendee *ia); const gchar *e_meeting_attendee_get_delfrom (EMeetingAttendee *ia); void e_meeting_attendee_set_delfrom (EMeetingAttendee *ia, gchar *delfrom); gboolean e_meeting_attendee_is_set_delfrom (EMeetingAttendee *ia); icalparameter_partstat e_meeting_attendee_get_status (EMeetingAttendee *ia); void e_meeting_attendee_set_status (EMeetingAttendee *ia, icalparameter_partstat status); const gchar *e_meeting_attendee_get_sentby (EMeetingAttendee *ia); void e_meeting_attendee_set_sentby (EMeetingAttendee *ia, gchar *sentby); gboolean e_meeting_attendee_is_set_sentby (EMeetingAttendee *ia); const gchar *e_meeting_attendee_get_cn (EMeetingAttendee *ia); void e_meeting_attendee_set_cn (EMeetingAttendee *ia, gchar *cn); gboolean e_meeting_attendee_is_set_cn (EMeetingAttendee *ia); const gchar *e_meeting_attendee_get_language (EMeetingAttendee *ia); void e_meeting_attendee_set_language (EMeetingAttendee *ia, gchar *language); gboolean e_meeting_attendee_is_set_language (EMeetingAttendee *ia); EMeetingAttendeeType e_meeting_attendee_get_atype (EMeetingAttendee *ia); EMeetingAttendeeEditLevel e_meeting_attendee_get_edit_level (EMeetingAttendee *ia); void e_meeting_attendee_set_edit_level (EMeetingAttendee *ia, EMeetingAttendeeEditLevel level); gboolean e_meeting_attendee_get_has_calendar_info (EMeetingAttendee *ia); void e_meeting_attendee_set_has_calendar_info (EMeetingAttendee *ia, gboolean has_calendar_info); const GArray *e_meeting_attendee_get_busy_periods (EMeetingAttendee *ia); gint e_meeting_attendee_find_first_busy_period (EMeetingAttendee *ia, GDate *date); gboolean e_meeting_attendee_add_busy_period (EMeetingAttendee *ia, gint start_year, gint start_month, gint start_day, gint start_hour, gint start_minute, gint end_year, gint end_month, gint end_day, gint end_hour, gint end_minute, EMeetingFreeBusyType busy_type); EMeetingTime e_meeting_attendee_get_start_busy_range (EMeetingAttendee *ia); EMeetingTime e_meeting_attendee_get_end_busy_range (EMeetingAttendee *ia); gboolean e_meeting_attendee_set_start_busy_range (EMeetingAttendee *ia, gint start_year, gint start_month, gint start_day, gint start_hour, gint start_minute); gboolean e_meeting_attendee_set_end_busy_range (EMeetingAttendee *ia, gint end_year, gint end_month, gint end_day, gint end_hour, gint end_minute); void e_meeting_attendee_clear_busy_periods (EMeetingAttendee *ia); #ifdef __cplusplus } #endif /* __cplusplus */ #endif /* _E_MEETING_ATTENDEE_H_ */ '#n71'>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
/*
 *  Routines to copy information from a Gaim buddy list into an
 *  Evolution addressbook.
 *
 *  I currently copy IM account names and buddy icons, provided you
 *  don't already have a buddy icon defined for a person.
 *
 *  This works today (25 October 2004), but is pretty sure to break
 *  later on as the Gaim buddylist file format shifts.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 *
 * Authors:
 *      Nat Friedman <nat@novell.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <libxml/tree.h>
#include <libxml/parser.h>
#include <libxml/xmlmemory.h>
#include <e-util/e-xml-utils.h>

#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <string.h>

#include <libebook/e-book-client.h>
#include <libebook/e-book-query.h>
#include <libedataserverui/e-source-combo-box.h>

#include <sys/time.h>
#include <sys/stat.h>

#include <e-util/e-config.h>

#include "bbdb.h"

typedef struct {
    gchar *account_name;
    gchar *proto;
    gchar *alias;
    gchar *icon;
} GaimBuddy;

/* Forward declarations for this file. */
static gboolean bbdb_merge_buddy_to_contact (EBookClient *client, GaimBuddy *b, EContact *c);
static GList *bbdb_get_gaim_buddy_list (void);
static gchar *get_node_text (xmlNodePtr node);
static gchar *get_buddy_icon_from_setting (xmlNodePtr setting);
static void free_buddy_list (GList *blist);
static void parse_buddy_group (xmlNodePtr group, GList **buddies, GSList *blocked);
static EContactField proto_to_contact_field (const gchar *proto);

static gchar *
get_buddy_filename (void)
{
    return g_build_path ("/", g_get_home_dir (), ".purple/blist.xml", NULL);
}

static gchar *
get_md5_as_string (const gchar *filename)
{
    GMappedFile *mapped_file;
    const gchar *contents;
    gchar *digest;
    gsize length;
    GError *error = NULL;

    g_return_val_if_fail (filename != NULL, NULL);

    mapped_file = g_mapped_file_new (filename, FALSE, &error);
    if (mapped_file == NULL) {
        g_warning ("%s", error->message);
        return NULL;
    }

    contents = g_mapped_file_get_contents (mapped_file);
    length = g_mapped_file_get_length (mapped_file);

    digest = g_compute_checksum_for_data (
        G_CHECKSUM_MD5, (guchar *) contents, length);

    g_mapped_file_unref (mapped_file);

    return digest;
}

void
bbdb_sync_buddy_list_check (void)
{
    GConfClient *gconf;
    struct stat statbuf;
    time_t last_sync_time;
    gchar *md5;
    gchar *blist_path;
    gchar *last_sync_str;

    blist_path = get_buddy_filename ();
    if (stat (blist_path, &statbuf) < 0) {
        g_free (blist_path);
        return;
    }

    gconf = gconf_client_get_default ();

    /* Reprocess the buddy list if it's been updated. */
    last_sync_str = gconf_client_get_string (
        gconf, GCONF_KEY_GAIM_LAST_SYNC_TIME, NULL);
    if (last_sync_str == NULL || !strcmp ((const gchar *)last_sync_str, ""))
        last_sync_time = (time_t) 0;
    else
        last_sync_time = (time_t) g_ascii_strtoull (last_sync_str, NULL, 10);

    g_free (last_sync_str);

    if (statbuf.st_mtime <= last_sync_time) {
        g_object_unref (G_OBJECT (gconf));
        g_free (blist_path);
        return;
    }

    last_sync_str = gconf_client_get_string (
        gconf, GCONF_KEY_GAIM_LAST_SYNC_MD5, NULL);

    g_object_unref (gconf);

    md5 = get_md5_as_string (blist_path);

    if (!last_sync_str || !*last_sync_str || !g_str_equal (md5, last_sync_str)) {
        fprintf (stderr, "bbdb: Buddy list has changed since last sync.\n");

        bbdb_sync_buddy_list ();
    }

    g_free (last_sync_str);
    g_free (blist_path);
    g_free (md5);
}

static gboolean
store_last_sync_idle_cb (gpointer data)
{
    GConfClient *gconf;
    gchar *md5;
    gchar *blist_path = get_buddy_filename ();
    time_t last_sync;
    gchar *last_sync_time;

    time (&last_sync);
    last_sync_time = g_strdup_printf ("%ld", (glong) last_sync);

    md5 = get_md5_as_string (blist_path);

    gconf = gconf_client_get_default ();
    gconf_client_set_string (
        gconf, GCONF_KEY_GAIM_LAST_SYNC_TIME, last_sync_time, NULL);
    gconf_client_set_string (
        gconf, GCONF_KEY_GAIM_LAST_SYNC_MD5, md5, NULL);

    g_object_unref (G_OBJECT (gconf));

    g_free (last_sync_time);
    g_free (blist_path);
    g_free (md5);

    return FALSE;
}

static gboolean syncing = FALSE;
G_LOCK_DEFINE_STATIC (syncing);

struct sync_thread_data
{
    GList *blist;
    EBookClient *client;
};

static gpointer
bbdb_sync_buddy_list_in_thread (gpointer data)
{
    GList *l;
    struct sync_thread_data *std = data;

    g_return_val_if_fail (std != NULL, NULL);

    if (!bbdb_open_book_client (std->client)) {
        /* client got freed in bbdb_open_book_client on a failure */
        free_buddy_list (std->blist);
        g_free (std);

        G_LOCK (syncing);
        syncing = FALSE;
        G_UNLOCK (syncing);

        return NULL;
    }

    printf ("bbdb: Synchronizing buddy list to contacts...\n");
    /* Walk the buddy list */
    for (l = std->blist; l != NULL; l = l->next) {
        GaimBuddy *b = l->data;
        EBookQuery *query;
        gchar *query_string, *uid;
        GSList *contacts = NULL;
        GError *error = NULL;
        EContact *c;

        if (b->alias == NULL || strlen (b->alias) == 0) {
            g_free (b->alias);
            b->alias = g_strdup (b->account_name);
        }

        /* Look for an exact match full name == buddy alias */
        query = e_book_query_field_test (E_CONTACT_FULL_NAME, E_BOOK_QUERY_IS, b->alias);
        query_string = e_book_query_to_string (query);
        e_book_query_unref (query);
        if (!e_book_client_get_contacts_sync (std->client, query_string, &contacts, NULL, NULL)) {
            g_free (query_string);
            continue;
        }

        g_free (query_string);

        if (contacts != NULL) {

            /* FIXME: If there's more than one contact with this
               name, just give up; we're not smart enough for
               this. */
            if (contacts->next != NULL) {
                e_client_util_free_object_slist (contacts);
                continue;
            }

            c = E_CONTACT (contacts->data);

            if (!bbdb_merge_buddy_to_contact (std->client, b, c)) {
                e_client_util_free_object_slist (contacts);
                continue;
            }

            /* Write it out to the addressbook */
            if (!e_book_client_modify_contact_sync (std->client, c, NULL, &error)) {
                g_warning ("bbdb: Could not modify contact: %s", error->message);
                g_error_free (error);
            }
            e_client_util_free_object_slist (contacts);
            continue;
        }

        /* Otherwise, create a new contact. */
        c = e_contact_new ();
        e_contact_set (c, E_CONTACT_FULL_NAME, (gpointer) b->alias);
        if (!bbdb_merge_buddy_to_contact (std->client, b, c)) {
            g_object_unref (c);
            continue;
        }

        uid = NULL;
        if (!e_book_client_add_contact_sync (std->client, c, &uid, NULL, &error)) {
            g_warning ("bbdb: Failed to add new contact: %s", error->message);
            g_error_free (error);
            goto finish;
        }

        g_object_unref (c);
        g_free (uid);
    }

    g_idle_add (store_last_sync_idle_cb, NULL);

 finish:
    printf ("bbdb: Done syncing buddy list to contacts.\n");

    g_object_unref (std->client);
    free_buddy_list (std->blist);
    g_free (std);

    G_LOCK (syncing);
    syncing = FALSE;
    G_UNLOCK (syncing);

    return NULL;
}

void
bbdb_sync_buddy_list (void)
{
    GList *blist;
    GError *error = NULL;
    EBookClient *client = NULL;
    struct sync_thread_data *std;

    G_LOCK (syncing);
    if (syncing) {
        G_UNLOCK (syncing);
        printf ("bbdb: Already syncing buddy list, skipping this call\n");
        return;
    }

    /* Get the Gaim buddy list */
    blist = bbdb_get_gaim_buddy_list ();
    if (blist == NULL) {
        G_UNLOCK (syncing);
        return;
    }

    /* Open the addressbook */
    client = bbdb_create_book_client (GAIM_ADDRESSBOOK);
    if (client == NULL) {
        free_buddy_list (blist);
        G_UNLOCK (syncing);
        return;
    }

    std = g_new0 (struct sync_thread_data, 1);
    std->blist = blist;
    std->client = client;

    syncing = TRUE;

    g_thread_create (bbdb_sync_buddy_list_in_thread, std, FALSE, &error);
    if (error) {
        g_warning (
            "%s: Creation of the thread failed with error: %s",
            G_STRFUNC, error->message);
        g_error_free (error);

        G_UNLOCK (syncing);
        bbdb_sync_buddy_list_in_thread (std);
        G_LOCK (syncing);
    }

    G_UNLOCK (syncing);
}

static gboolean
im_list_contains_buddy (GList *ims, GaimBuddy *b)
{
    GList *l;

    for (l = ims; l != NULL; l = l->next) {
        gchar *im = (gchar *) l->data;

        if (!strcmp (im, b->account_name))
            return TRUE;
    }

    return FALSE;
}

static gboolean
bbdb_merge_buddy_to_contact (EBookClient *client, GaimBuddy *b, EContact *c)
{
    EContactField field;
    GList *ims;
    gboolean dirty = FALSE;

    EContactPhoto *photo = NULL;

    GError *error = NULL;

    /* Set the IM account */
    field = proto_to_contact_field (b->proto);
    ims = e_contact_get (c, field);
    if (!im_list_contains_buddy (ims, b)) {
        ims = g_list_append (ims, g_strdup (b->account_name));
        e_contact_set (c, field, (gpointer) ims);
        dirty = TRUE;
    }

    g_list_foreach (ims, (GFunc) g_free, NULL);
    g_list_free (ims);
    ims = NULL;

        /* Set the photo if it's not set */
    if (b->icon != NULL) {
        photo = e_contact_get (c, E_CONTACT_PHOTO);
        if (photo == NULL) {
            gchar *contents = NULL;

            photo = g_new0 (EContactPhoto, 1);
            photo->type = E_CONTACT_PHOTO_TYPE_INLINED;

            if (!g_file_get_contents (
                b->icon, &contents,
                &photo->data.inlined.length, &error)) {
                g_warning ("bbdb: Could not read buddy icon: %s\n", error->message);
                g_error_free (error);
                return dirty;
            }

            photo->data.inlined.data = (guchar *) contents;
            e_contact_set (c, E_CONTACT_PHOTO, (gpointer) photo);
            dirty = TRUE;
        }
    }

    /* Clean up */
    if (photo != NULL)
        e_contact_photo_free (photo);

    return dirty;
}

static EContactField
proto_to_contact_field (const gchar *proto)
{
    if (!strcmp (proto,  "prpl-oscar"))
        return E_CONTACT_IM_AIM;
    if (!strcmp (proto, "prpl-novell"))
        return E_CONTACT_IM_GROUPWISE;
    if (!strcmp (proto, "prpl-msn"))
        return E_CONTACT_IM_MSN;
    if (!strcmp (proto, "prpl-icq"))
        return E_CONTACT_IM_ICQ;
    if (!strcmp (proto, "prpl-yahoo"))
        return E_CONTACT_IM_YAHOO;
    if (!strcmp (proto, "prpl-jabber"))
        return E_CONTACT_IM_JABBER;
    if (!strcmp (proto, "prpl-gg"))
        return E_CONTACT_IM_GADUGADU;

    return E_CONTACT_IM_AIM;
}

static void
get_all_blocked (xmlNodePtr node, GSList **blocked)
{
    xmlNodePtr child;

    if (!node || !blocked)
        return;

    for (child = node->children; child; child = child->next) {
        if (child->children)
            get_all_blocked (child, blocked);

        if (!strcmp ((const gchar *)child->name, "block")) {
            gchar *name = get_node_text (child);

            if (name)
                *blocked = g_slist_prepend (*blocked, name);