aboutsummaryrefslogtreecommitdiffstats
path: root/addressbook/tools/evolution-addressbook-abuse.c
blob: 7801ed7e9dbed5a32fb47a31fd6c51b5bf68a0ee (plain) (blame)
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

#include <config.h>

#include <bonobo-activation/bonobo-activation.h>
#include <bonobo/bonobo-main.h>

#include <backend/ebook/e-book-async.h>
#include <gnome.h>

static int contacts_to_add_total = 1000;
static int contacts_to_add = 50;
static int call_count = 0;

static gchar *
make_random_string (void)
{
    const gchar *elements = " abcdefghijklmnopqrstuvwxyz1234567890  ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    gint len = strlen (elements);
    gint i, N = 5 + (random () % 10);
    gchar *str = g_malloc (N+1);

    for (i = 0; i < N; ++i) {
        str[i] = elements[random () % len];
    }
    str[i] = '\0';

    return str;
}

static gchar *
make_random_vcard (void)
{
    gchar *fa = make_random_string ();
    gchar *name = make_random_string ();
    gchar *email = make_random_string ();
    gchar *org = make_random_string ();

    gchar *vcard;

    vcard = g_strdup_printf ("BEGIN:VCARD\n"
                 "X-EVOLUTION-FILE-AS:%s\n"
                 "N:%s\n"
                 "EMAIL;INTERNET:%s\n"
                 "ORG:%s\n"
                 "END:VCARD",
                 fa, name, email, org);
    g_free (fa);
    g_free (name);
    g_free (email);
    g_free (org);

    return vcard;
}

/* ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** */

static void
add_cb (EBook *book, EBookStatus status, const char *id, gpointer closure)
{
    switch (status) {
    case E_BOOK_ERROR_OK:
        --contacts_to_add_total;
        g_message ("succesful add! (%d remaining)", contacts_to_add_total);
        if (contacts_to_add_total <= 0)
            g_main_loop_quit (NULL);
        break;
    default:
        g_message ("something went wrong...");
        g_main_loop_quit (NULL);
        break;
    }
}

static void
use_addressbook (EBook *book, EBookStatus status, gpointer closure)
{
    gint i;

    if (book == NULL || status != E_BOOK_ERROR_OK)
        g_error (_("Error loading default addressbook."));

    for (i = 0; i < contacts_to_add; ++i) {
        gchar *vcard = make_random_vcard ();
        EContact *contact = e_contact_new_from_vcard (vcard);
        g_message ("adding %d", i);
        e_book_async_add_contact (book, contact, add_cb, NULL);
        g_free (vcard);
        g_object_unref (contact);
    }

    g_object_unref (book);
}

static gint
abuse_timeout (gpointer foo)
{
    e_book_async_get_default_addressbook (use_addressbook, NULL);

    ++call_count;
    g_message ("timeout!");
    return call_count < contacts_to_add_total / contacts_to_add;
}

int
main (int argc, char *argv[])
{
    if (getenv ("ABUSE_THE_WOMBAT") == NULL) {
        g_print ("You probably don't want to use this program.\n"
             "It isn't very nice.\n");
        exit(0);
    }

    bindtextdomain (GETTEXT_PACKAGE, EVOLUTION_LOCALEDIR);
    textdomain (GETTEXT_PACKAGE);

    gnome_program_init ("evolution-addressbook-abuse", VERSION,
                LIBGNOMEUI_MODULE, argc, argv, 
                GNOME_PROGRAM_STANDARD_PROPERTIES,
                NULL);

    g_timeout_add (20, abuse_timeout, NULL);

    bonobo_main ();

    return 0;
}