aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/gui/gnome-cal.c
blob: e1c300071dcf419842959c08c9f8a33ff389f9dd (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
/*
 * GnomeCalendar widget
 * Copyright (C) 1998 the Free Software Foundation
 *
 * Author: Miguel de Icaza (miguel@kernel.org)
 */
#include <config.h>
#include <gnome.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include "calendar.h"
#include "gnome-cal.h"
#include "gncal-day-panel.h"
#include "gncal-week-view.h"
#include "gncal-year-view.h"
#include "timeutil.h"
#include "views.h"
#include "main.h"

GnomeApp *parent_class;

guint
gnome_calendar_get_type (void)
{
    static guint gnome_calendar_type = 0;
    if(!gnome_calendar_type) {
        GtkTypeInfo gnome_calendar_info = {
            "GnomeCalendar",
            sizeof(GnomeCalendar),
            sizeof(GnomeCalendarClass),
            (GtkClassInitFunc) NULL,
            (GtkObjectInitFunc) NULL,
            (GtkArgSetFunc) NULL,
            (GtkArgGetFunc) NULL,
        };
        gnome_calendar_type = gtk_type_unique(gnome_app_get_type(), &gnome_calendar_info);
        parent_class = gtk_type_class (gnome_app_get_type());
    }
    return gnome_calendar_type;
}

static void
setup_widgets (GnomeCalendar *gcal)
{
    time_t now;

    now = time (NULL);
    
    gcal->notebook  = gtk_notebook_new ();
    gcal->day_view  = gncal_day_panel_new (gcal, now);
    gcal->week_view = gncal_week_view_new (gcal, now);
    gcal->year_view = gncal_year_view_new (gcal, now);
    gcal->task_view = tasks_create (gcal);

    gtk_notebook_append_page (GTK_NOTEBOOK (gcal->notebook), gcal->day_view,  gtk_label_new (_("Day View")));
    gtk_notebook_append_page (GTK_NOTEBOOK (gcal->notebook), gcal->week_view, gtk_label_new (_("Week View")));
    gtk_notebook_append_page (GTK_NOTEBOOK (gcal->notebook), gcal->year_view, gtk_label_new (_("Year View")));
/*  gtk_notebook_append_page (GTK_NOTEBOOK (gcal->notebook), gcal->task_view, gtk_label_new (_("Todo"))); */

    gtk_widget_show_all (gcal->notebook);
    
    gnome_app_set_contents (GNOME_APP (gcal), gcal->notebook);
    
}

static GtkWidget *
get_current_page (GnomeCalendar *gcal)
{
    return GTK_NOTEBOOK (gcal->notebook)->cur_page->child;
}

void
gnome_calendar_goto (GnomeCalendar *gcal, time_t new_time)
{
    GtkWidget *current = get_current_page (gcal);

    g_return_if_fail (new_time != -1);

    if (current == gcal->day_view)
        gncal_day_panel_set (GNCAL_DAY_PANEL (gcal->day_view), new_time);
    else if (current == gcal->week_view)
        gncal_week_view_set (GNCAL_WEEK_VIEW (gcal->week_view), new_time);
    else if (current == gcal->year_view)
        gncal_year_view_set (GNCAL_YEAR_VIEW (gcal->year_view), new_time);
    else {
        g_warning ("My penguin is gone!");
        g_assert_not_reached ();
    }

    gcal->current_display = new_time;
}

static void
gnome_calendar_direction (GnomeCalendar *gcal, int direction)
{
    GtkWidget *cp = get_current_page (gcal);
    time_t new_time;
    
    if (cp == gcal->day_view)
        new_time = time_add_day (gcal->current_display, 1 * direction);
    else if (cp == gcal->week_view)
        new_time = time_add_day (gcal->current_display, 7 * direction);
    else if (cp == gcal->year_view)
        new_time = time_add_year (gcal->current_display, 1 * direction);
    else {
        g_warning ("Weee!  Where did the penguin go?");
        g_assert_not_reached ();
    }

    gnome_calendar_goto (gcal, new_time);
}

void
gnome_calendar_next (GnomeCalendar *gcal)
{
    gnome_calendar_direction (gcal, 1);
}

void
gnome_calendar_previous (GnomeCalendar *gcal)
{
    gnome_calendar_direction (gcal, -1);
}

void
gnome_calendar_dayjump (GnomeCalendar *gcal, time_t time)
{
    gtk_notebook_set_page (GTK_NOTEBOOK (gcal->notebook), 0);
    gnome_calendar_goto (gcal, time);
}

GtkWidget *
gnome_calendar_new (char *title)
{
    GtkWidget      *retval;
    GnomeCalendar  *gcal;
    GnomeApp       *app;
        
    retval = gtk_type_new (gnome_calendar_get_type ());
    app = GNOME_APP (retval);
    gcal = GNOME_CALENDAR (retval);
    
    app->name = g_strdup ("calendar");
    app->prefix = g_copy_strings ("/", app->name, "/", NULL);
    
    gtk_window_set_title(GTK_WINDOW(retval), title);

    gcal->current_display = time (NULL);
    gcal->cal = calendar_new (title);
    setup_widgets (gcal);
    return retval;
}

static void
gnome_calendar_update_all (GnomeCalendar *cal, iCalObject *object, int flags)
{
    gncal_day_panel_update (GNCAL_DAY_PANEL (cal->day_view), object, flags);
    gncal_week_view_update (GNCAL_WEEK_VIEW (cal->week_view), object, flags);
    gncal_year_view_update (GNCAL_YEAR_VIEW (cal->year_view), object, flags);
}

int
gnome_calendar_load (GnomeCalendar *gcal, char *file)
{
    char *r;
    
    if ((r = calendar_load (gcal->cal, file)) != NULL){
        printf ("Error loading calendar: %s\n", r);
        return 0;
    }
    gnome_calendar_update_all (gcal, NULL, 0);
    return 1;
}

void
gnome_calendar_add_object (GnomeCalendar *gcal, iCalObject *obj)
{
    calendar_add_object (gcal->cal, obj);
    gnome_calendar_update_all (gcal, obj, CHANGE_NEW);
}

void
gnome_calendar_remove_object (GnomeCalendar *gcal, iCalObject *obj)
{
    calendar_remove_object (gcal->cal, obj);
    gnome_calendar_update_all (gcal, obj, CHANGE_ALL);
}

void
gnome_calendar_object_changed (GnomeCalendar *gcal, iCalObject *obj, int flags)
{
    g_return_if_fail (gcal != NULL);
    g_return_if_fail (GNOME_IS_CALENDAR (gcal));
    g_return_if_fail (obj != NULL);

    /* FIXME: for now we only update the views.  Most likely we
     * need to do something else like set the last_mod field on
     * the iCalObject and such - Federico
     */

    gcal->cal->modified = TRUE;

    gnome_calendar_update_all (gcal, obj, flags);
}

static int
max_open_files (void)
{
        static int files;

        if (files)
                return files;

        files = sysconf (_SC_OPEN_MAX);
        if (files != -1)
                return files;
#ifdef OPEN_MAX
        return files = OPEN_MAX;
#else
        return files = 256;
#endif
}

static void
execute (char *command, int close_standard)
{
    struct sigaction ignore, save_intr, save_quit;
    int status = 0, i;
    pid_t pid;
    
    ignore.sa_handler = SIG_IGN;
    sigemptyset (&ignore.sa_mask);
    ignore.sa_flags = 0;
    
    sigaction (SIGINT, &ignore, &save_intr);    
    sigaction (SIGQUIT, &ignore, &save_quit);

    if ((pid = fork ()) < 0){
        fprintf (stderr, "\n\nfork () = -1\n");
        return;
    }
    if (pid == 0){
        pid = fork ();
        if (pid == 0){
            const int top = max_open_files ();
            sigaction (SIGINT,  &save_intr, NULL);
            sigaction (SIGQUIT, &save_quit, NULL);
            
            for (i = (close_standard ? 0 : 3); i < top; i++)
                close (i);
            
            /* FIXME: As an excercise to the reader, copy the
             * code from mc to setup shell properly instead of
             * /bin/sh.  Yes, this comment is larger than a cut and paste.
             */
            execl ("/bin/sh", "/bin/sh", "-c", command, (char *) 0);
            
            exit (127);
        } else {
            exit (127);
        }
    }
    wait (&status);
    sigaction (SIGINT,  &save_intr, NULL);
    sigaction (SIGQUIT, &save_quit, NULL);
}

void
calendar_notify (time_t time, void *data)
{
    iCalObject *ico = data;

    if (ico->aalarm.enabled && ico->aalarm.trigger == time){
        printf ("bip\n");
        return;
    }

        if (ico->palarm.enabled && ico->palarm.trigger == time){
        execute (ico->palarm.data, 0);
        return;
    }

    if (ico->malarm.enabled && ico->malarm.trigger == time){
        char *command;
        time_t app = ico->malarm.trigger + ico->malarm.offset;

        command = g_copy_strings ("mail -s '",
                      _("Reminder of your appointment at "),
                      ctime (&app), "' '",
                      ico->malarm.data, "' ",
                      NULL);
        execute (command, 1);

        g_free (command);
        return;
    }

    if (ico->dalarm.enabled && ico->dalarm.trigger == time){
        time_t app = ico->dalarm.trigger + ico->dalarm.offset;
        GtkWidget *w;
        char *msg;

        msg = g_copy_strings (_("Reminder of your appointment at "),
                    ctime (&app), "`",
                    ico->summary, "'", NULL);
        w = gnome_message_box_new (msg, GNOME_MESSAGE_BOX_INFO, "Ok", NULL);
        gtk_widget_show (w);
        return;
    }
}