aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/gui/calendar-summary.c
blob: 3544fbbc38fea36c639da841255628b34bc467ef (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
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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* calendar-summary.c
 *
 * Authors: Iain Holmes <iain@helixcode.com>
 *
 * Copyright (C) 2000  Helix Code, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * 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.
 */

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

#include <time.h>

#include <bonobo.h>

#include <gnome.h>
#include <liboaf/liboaf.h>

#include <evolution-services/executive-summary-component.h>
#include <evolution-services/executive-summary-html-view.h>

#include "cal-util/cal-component.h"
#include "cal-util/timeutil.h"
#include "calendar-model.h"

#include "calendar-summary.h"

typedef struct {
    ExecutiveSummaryComponent *component;
    ExecutiveSummaryHtmlView *view;
    CalClient *client;
    gboolean cal_loaded;

    char *title;
    char *icon;
    
    guint32 idle;
} CalSummary;

enum {
    PROPERTY_TITLE,
    PROPERTY_ICON
};

extern gchar *evolution_dir;

static int running_views = 0;
static BonoboGenericFactory *factory;
#define CALENDAR_SUMMARY_ID "OAFIID:GNOME_Evolution_Calendar_Summary_ComponentFactory"

static gboolean
generate_html_summary (CalSummary *summary)
{
    time_t t, day_begin, day_end;
    struct tm *timeptr;
    GList *uids, *l;
    char *ret_html, *tmp, *datestr;
    
    t = time (NULL);
    day_begin = time_day_begin (t);
    day_end = time_day_end (t);

    datestr = g_new (char, 256);
    timeptr = localtime (&t);
    strftime (datestr, 255, _("%A, %e %B %Y"), timeptr);
    ret_html = g_strdup_printf ("<b>%s</b><br> <ul>", datestr);
    g_free (datestr);

    uids = cal_client_get_objects_in_range (summary->client, 
                        CALOBJ_TYPE_EVENT, day_begin,
                        day_end);
    for (l = uids; l; l = l->next){
        CalComponent *comp;
        CalComponentText text;
        CalClientGetStatus status;
        CalComponentDateTime start, end;
        struct icaltimetype *start_time, *end_time;
        char *start_str, *end_str;
        char *uid;
        char *tmp2;
        
        uid = l->data;
        status = cal_client_get_object (summary->client, uid, &comp);
        if (status != CAL_CLIENT_GET_SUCCESS)
            continue;
        
        cal_component_get_summary (comp, &text);
        cal_component_get_dtstart (comp, &start);
        cal_component_get_dtend (comp, &end);

        g_print ("text.value: %s\n", text.value);
        start_time = start.value;
        end_time = end.value;

        start_str = g_strdup_printf ("%d.%d", start_time->hour, start_time->minute);
        if (end_time) {
            end_str = g_strdup_printf ("%d.%d", end_time->hour, end_time->minute);
        } else {
            end_str = g_strdup (" ");
        }

        tmp2 = g_strdup_printf ("<li>%s:%s -> %s</li>", text.value, start_str, end_str);
        g_free (start_str);
        g_free (end_str);

        tmp = ret_html;
        ret_html = g_strconcat (ret_html, tmp2, NULL);
        g_free (tmp);
        g_free (tmp2);
    }
    
    cal_obj_uid_list_free (uids);
    
    tmp = ret_html;
    ret_html = g_strconcat (ret_html, "</ul>", NULL);
    g_free (tmp);
    
    executive_summary_html_view_set_html (summary->view, ret_html);
    g_free (ret_html);

    summary->idle = 0;
    return FALSE;
}

static void
get_property (BonoboPropertyBag *bag,
          BonoboArg *arg,
          guint arg_id,
          gpointer data)
{
    CalSummary *summary = (CalSummary *) data;

    switch (arg_id) {
    case PROPERTY_TITLE:
        BONOBO_ARG_SET_STRING (arg, summary->title);
        break;

    case PROPERTY_ICON:
        BONOBO_ARG_SET_STRING (arg, summary->icon);
        break;

    default:
        break;
    }
}

static void
component_destroyed (GtkObject *object,
             gpointer data)
{
    CalSummary *summary = (CalSummary *) data;

    g_free (summary->title);
    g_free (summary->icon);
    gtk_object_destroy (GTK_OBJECT (summary->client));

    g_free (summary);

    running_views--;

    if (running_views <= 0) {
        bonobo_object_unref (BONOBO_OBJECT (factory));
    }
}

static void
obj_updated_cb (CalClient *client,
        const char *uid,
        CalSummary *summary)
{
    /* FIXME: Maybe cache the uid's in the summary and only call this if
       uid is in this cache??? */

    if (summary->idle != 0) 
        return;

    summary->idle = g_idle_add (generate_html_summary, summary);
}

static void
obj_removed_cb (CalClient *client,
        const char *uid,
        CalSummary *summary)
{
    /* See FIXME: above */
    if (summary->idle != 0)
        return;

    summary->idle = g_idle_add (generate_html_summary, summary);
}
static void
cal_loaded_cb (CalClient *client,
           CalClientLoadStatus status,
           CalSummary *summary)
{
    switch (status) {
    case CAL_CLIENT_LOAD_SUCCESS:
        summary->cal_loaded = TRUE;

        if (summary->idle != 0)
            return;

        summary->idle = g_idle_add (generate_html_summary, summary);
        break;

    case CAL_CLIENT_LOAD_ERROR:
        executive_summary_html_view_set_html (summary->view,
                              _("<b>Error loading calendar</b>"));
        break;

    case CAL_CLIENT_LOAD_IN_USE:
        executive_summary_html_view_set_html (summary->view,
                              _("<b>Error loading calendar:<br>Calendar in use."));
        
        break;

    case CAL_CLIENT_LOAD_METHOD_NOT_SUPPORTED:
        executive_summary_html_view_set_html (summary->view,
                              _("<b>Error loading calendar:<br>Method not supported"));
        break;

    default:
        break;
    }
}

BonoboObject *
create_summary_view (ExecutiveSummaryComponentFactory *_factory,
             void *closure)
{
    BonoboObject *component, *view;
    BonoboPersistStream *stream;
    BonoboPropertyBag *bag;
    CalSummary *summary;
    char *html, *file;

    file = g_concat_dir_and_file (evolution_dir, "local/Calendar/calendar.ics");

    /* Create the component object */
    component = executive_summary_component_new ();

    summary = g_new (CalSummary, 1);
    summary->component = component;
    summary->icon = g_strdup ("evolution-calendar.png");
    summary->title = g_strdup ("Things to do");
    summary->client = cal_client_new ();
    summary->cal_loaded = FALSE;
    summary->idle = 0;

    /* Check for calendar files */
    if (!g_file_exists (file)) {
        cal_client_create_calendar (summary->client, file);
    }

    /* Load calendar */
    cal_client_load_calendar (summary->client, file);
    g_free (file);

    gtk_signal_connect (GTK_OBJECT (summary->client), "cal-loaded",
                GTK_SIGNAL_FUNC (cal_loaded_cb), summary);
    gtk_signal_connect (GTK_OBJECT (summary->client), "obj-updated",
                GTK_SIGNAL_FUNC (obj_updated_cb), summary);
    gtk_signal_connect (GTK_OBJECT (summary->client), "obj-removed",
                GTK_SIGNAL_FUNC (obj_removed_cb), summary);
        
    gtk_signal_connect (GTK_OBJECT (component), "destroy",
                GTK_SIGNAL_FUNC (component_destroyed), summary);

    /* HTML view */
    view = executive_summary_html_view_new ();
    summary->view = view;

    executive_summary_html_view_set_html (EXECUTIVE_SUMMARY_HTML_VIEW (view),
                          _("Loading Calendar"));
    bonobo_object_add_interface (component, view);

    /* BonoboPropertyBag */
    bag = bonobo_property_bag_new (get_property, NULL, summary);
    bonobo_property_bag_add (bag, "window_title", PROPERTY_TITLE,
                 BONOBO_ARG_STRING, NULL, 
                 "The title of this component's window", 0);
    bonobo_property_bag_add (bag, "window_icon", PROPERTY_ICON,
                 BONOBO_ARG_STRING, NULL, 
                 "The icon for this component's window", 0);
    bonobo_object_add_interface (component, BONOBO_OBJECT (bag));

    running_views++;

    return component;
}

static BonoboObject *
factory_fn (BonoboGenericFactory *generic_factory,
        void *closure)
{
    BonoboObject *_factory;

    _factory = executive_summary_component_factory_new (create_summary_view,
                                NULL);
    return _factory;
}

BonoboGenericFactory *
calendar_summary_factory_init (void)
{
    if (factory != NULL)
        return factory;

    factory = bonobo_generic_factory_new (CALENDAR_SUMMARY_ID, factory_fn,
                          NULL);

    if (factory == NULL) {
        g_warning ("Cannot initialise calendar summary factory");
        return NULL;
    }

    return factory;
}