aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/cal-client/client-test.c
blob: f8741f4539e7cdbef470102a33ea818585693c70 (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
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
#include <config.h>
#include <bonobo.h>
#include <gnome.h>
#include <cal-client/cal-client.h>
static CalClient *client1;
static CalClient *client2;

/* Prints a message with a client identifier */
static void
cl_printf (CalClient *client, const char *format, ...)
{
    va_list args;

    va_start (args, format);
    printf ("Client %s: ",
        client == client1 ? "1" :
        client == client2 ? "2" :
        "UNKNOWN");
    vprintf (format, args);
    va_end (args);
}

/* Lists the UIDs of objects in a calendar, called as an idle handler */
static gboolean
list_uids (gpointer data)
{
    CalClient *client;
    GList *uids;
    GList *l;

    client = CAL_CLIENT (data);

    uids = cal_client_get_uids (client, CALOBJ_TYPE_ANY);

    cl_printf (client, "UIDs: ");

    if (!uids)
        printf ("none\n");
    else {
        for (l = uids; l; l = l->next) {
            char *uid;

            uid = l->data;
            printf ("`%s' ", uid);
        }

        printf ("\n");

        for (l = uids; l; l = l->next) {
            char *uid;
            iCalObject *ico;
            CalClientGetStatus status;

            uid = l->data;
            status = cal_client_get_object (client, uid, &ico);

            if (status == CAL_CLIENT_GET_SUCCESS) {
                printf ("------------------------------\n");
                dump_icalobject (ico);
                printf ("------------------------------\n");
            } else {
                printf ("FAILED: %d\n", status);
            }

            // cal_client_update_object (client, uid, calobj);
            // g_free (calobj);
        }
    }

    cal_obj_uid_list_free (uids);

    gtk_object_unref (GTK_OBJECT (client));

    return FALSE;
}

/* Callback used when a calendar is loaded */
static void
cal_loaded (CalClient *client, CalClientLoadStatus status, gpointer data)
{
    cl_printf (client, "Load/create %s\n",
           ((status == CAL_CLIENT_LOAD_SUCCESS) ? "success" :
            (status == CAL_CLIENT_LOAD_ERROR) ? "error" :
            (status == CAL_CLIENT_LOAD_IN_USE) ? "in use" :
            "unknown status value"));

    if (status == CAL_CLIENT_LOAD_SUCCESS)
        g_idle_add (list_uids, client);
    else
        gtk_object_unref (GTK_OBJECT (client));
}

/* Callback used when an object is updated */
static void
obj_updated (CalClient *client, const char *uid, gpointer data)
{
    cl_printf (client, "Object updated: %s\n", uid);
}

/* Creates a calendar client and tries to load the specified URI into it */
static CalClient *
create_client (const char *uri, gboolean load)
{
    CalClient *client;
    gboolean result;

    client = cal_client_new ();
    if (!client) {
        g_message ("create_client(): could not create the client");
        exit (1);
    }

    gtk_signal_connect (GTK_OBJECT (client), "cal_loaded",
                GTK_SIGNAL_FUNC (cal_loaded),
                NULL);
    gtk_signal_connect (GTK_OBJECT (client), "obj_updated",
                GTK_SIGNAL_FUNC (obj_updated),
                NULL);

    printf ("Calendar loading `%s'...\n", uri);

    if (load)
        result = cal_client_load_calendar (client, uri);
    else
        result = cal_client_create_calendar (client, uri);

    if (!result) {
        g_message ("create_client(): failure when issuing calendar %s request `%s'",
               load ? "load" : "create",
               uri);
        exit (1);
    }

    return client;
}

/* Callback used when a client is destroyed */
static void
client_destroy_cb (GtkObject *object, gpointer data)
{
    if (CAL_CLIENT (object) == client1)
        client1 = NULL;
    else if (CAL_CLIENT (object) == client2)
        client2 = NULL;
    else
        g_assert_not_reached ();

    if (!client1 && !client2)
        gtk_main_quit ();
}

#ifdef USING_OAF

#include <liboaf/liboaf.h>

static void
init_corba (int *argc, char **argv)
{
    gnome_init ("tl-test", VERSION, *argc, argv);
    oaf_init (*argc, argv);
}

#else

#include <libgnorba/gnorba.h>

static void
init_corba (int *argc, char **argv)
{
    CORBA_Environment ev;

    CORBA_exception_init (&ev);
    gnome_CORBA_init ("tl-test", VERSION, &argc, argv, 0, &ev);
    if (ev._major != CORBA_NO_EXCEPTION) {
        g_message ("main(): could not initialize the ORB");
        CORBA_exception_free (&ev);
        exit (1);
    }
    CORBA_exception_free (&ev);
}

#endif

int
main (int argc, char **argv)
{
    bindtextdomain (PACKAGE, GNOMELOCALEDIR);
    textdomain (PACKAGE);

    init_corba (&argc, argv);

    if (!bonobo_init (CORBA_OBJECT_NIL, CORBA_OBJECT_NIL, CORBA_OBJECT_NIL)) {
        g_message ("main(): could not initialize Bonobo");
        exit (1);
    }

    client1 = create_client ("/cvs/evolution/calendar/gui/test2.vcf", TRUE);
    gtk_signal_connect (GTK_OBJECT (client1), "destroy",
                client_destroy_cb,
                NULL);

    client2 = create_client ("/cvs/evolution/calendar/gui/test2.vcf", FALSE);
    gtk_signal_connect (GTK_OBJECT (client2), "destroy",
                client_destroy_cb,
                NULL);

    bonobo_main ();

    return 0;
}