aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/gui/alarm-notify/alarm-notify-dialog.c
blob: 0eefb533c178a51fd412ece628f08986362dcb1c (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
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
/* Evolution calendar - alarm notification dialog
 *
 * Copyright (C) 2000 Ximian, Inc.
 * Copyright (C) 2001 Ximian, Inc.
 *
 * Author: Federico Mena-Quintero <federico@ximian.com>
 *
 * 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.
 */

#include <config.h>
#include <stdio.h>
#include <string.h>
#include <gtk/gtklabel.h>
#include <gtk/gtkspinbutton.h>
#include <gtk/gtksignal.h>
#include <gtk/gtkscrolledwindow.h>
#include <gtk/gtkwindow.h>
#include <libgnome/gnome-i18n.h>
#if 0 
#  include <libgnomeui/gnome-winhints.h>
#endif
#include <libgnomeui/gnome-window-icon.h>
#include <glade/glade.h>
#include <e-util/e-time-utils.h>
#include <gal/widgets/e-unicode.h>
#include <gtkhtml/gtkhtml.h>
#include <gtkhtml/gtkhtml-stream.h>
#include "cal-util/timeutil.h"
#include "alarm-notify-dialog.h"
#include "config-data.h"


GtkWidget *make_html_display (gchar *widget_name, char *s1, char *s2, int scroll, int shadow);

/* The useful contents of the alarm notify dialog */
typedef struct {
    GladeXML *xml;

    GtkWidget *dialog;
    GtkWidget *close;
    GtkWidget *snooze;
    GtkWidget *edit;
    GtkWidget *heading;
    GtkWidget *message;
    GtkWidget *snooze_time;
    GtkWidget *html;

    AlarmNotifyFunc func;
    gpointer func_data;
} AlarmNotify;



/* Callback used when the notify dialog is destroyed */
static void
dialog_destroy_cb (GtkObject *object, gpointer data)
{
    AlarmNotify *an;

    an = data;
    g_object_unref (an->xml);
    g_free (an);
}

/* Delete_event handler for the alarm notify dialog */
static gint
delete_event_cb (GtkWidget *widget, GdkEvent *event, gpointer data)
{
    AlarmNotify *an;

    an = data;
    g_assert (an->func != NULL);

    (* an->func) (ALARM_NOTIFY_CLOSE, -1, an->func_data);

    gtk_widget_destroy (widget);
    return TRUE;
}

/* Callback for the close button */
static void
close_clicked_cb (GtkWidget *widget, gpointer data)
{
    AlarmNotify *an;

    an = data;
    g_assert (an->func != NULL);

    (* an->func) (ALARM_NOTIFY_CLOSE, -1, an->func_data);

    gtk_widget_destroy (an->dialog);
}

/* Callback for the snooze button */
static void
snooze_clicked_cb (GtkWidget *widget, gpointer data)
{
    AlarmNotify *an;
    int snooze_time;

    an = data;
    g_assert (an->func != NULL);

    snooze_time = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (an->snooze_time));
    (* an->func) (ALARM_NOTIFY_SNOOZE, snooze_time, an->func_data);

    gtk_widget_destroy (an->dialog);
}

/* Callback for the edit button */
static void
edit_clicked_cb (GtkWidget *widget, gpointer data)
{
    AlarmNotify *an;

    an = data;
    g_assert (an->func != NULL);

    (* an->func) (ALARM_NOTIFY_EDIT, -1, an->func_data);

    gtk_widget_destroy (an->dialog);
}

static void
url_requested_cb (GtkHTML *html, const char *url, GtkHTMLStream *stream, gpointer data)
{

    if (!strncmp ("file:///", url, strlen ("file:///"))) {
        FILE *fp;
        const char *filename = url + strlen ("file://");
        char buf[4096];
        size_t len;

        fp = fopen (filename, "r");

        if (fp == NULL) {
            g_warning ("Error opening image: %s\n", url);
            gtk_html_stream_close (stream, GTK_HTML_STREAM_ERROR);
            return;
        }

        while ((len = fread (buf, 1, sizeof(buf), fp)) > 0)
            gtk_html_stream_write (stream, buf, len);

        if (feof (fp)) {
            fclose (fp);
            gtk_html_stream_close (stream, GTK_HTML_STREAM_OK);
            return;
        }

        fclose (fp);
    }

    g_warning ("Error loading image");
    gtk_html_stream_close (stream, GTK_HTML_STREAM_ERROR);
    return;
}

GtkWidget *
make_html_display (gchar *widget_name, char *s1, char *s2, int scroll, int shadow)
{
    GtkWidget *html, *scrolled_window;

    gtk_widget_push_colormap (gdk_rgb_get_colormap ());

    html = gtk_html_new();

    gtk_html_set_default_content_type (GTK_HTML (html),
                       "charset=utf-8");
    gtk_html_load_empty (GTK_HTML (html));

    g_signal_connect (html, "url_requested",
              G_CALLBACK (url_requested_cb),
              NULL);

    gtk_widget_pop_colormap();

    scrolled_window = gtk_scrolled_window_new(NULL, NULL);

    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
                       GTK_POLICY_AUTOMATIC,
                       GTK_POLICY_AUTOMATIC);


    gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window),
                         GTK_SHADOW_IN);

    gtk_widget_set_size_request (scrolled_window, 300, 200);

    gtk_container_add(GTK_CONTAINER (scrolled_window), html);

    gtk_widget_show_all(scrolled_window);

    g_object_set_data (G_OBJECT (scrolled_window), "html", html);
    return scrolled_window;
}

static void
write_times (GtkHTMLStream *stream, char *start, char *end)
{
    if (start)
        gtk_html_stream_printf (stream, "<b>%s</b> %s<br>", _("Starting:"), start);
    if (end)
        gtk_html_stream_printf (stream, "<b>%s</b> %s<br>", _("Ending:"), end);

}

/* Converts a time_t to a string, relative to the specified timezone */
static char *
timet_to_str_with_zone (time_t t, icaltimezone *zone)
{
    struct icaltimetype itt;
    struct tm tm;
    char buf[256];

    if (t == -1)
        return g_strdup (_("invalid time"));

    itt = icaltime_from_timet_with_zone (t, FALSE, zone);
    tm = icaltimetype_to_tm (&itt);

    e_time_format_date_and_time (&tm, config_data_get_24_hour_format (),
                     FALSE, FALSE, buf, sizeof (buf));
    return g_strdup (buf);
}

/* Creates a heading for the alarm notification dialog */
static void
write_html_heading (GtkHTMLStream *stream, const char *message,
            CalComponentVType vtype, time_t occur_start, time_t occur_end)
{
    char *buf;
    char *start, *end;
    char *bg_path = "file://" EVOLUTION_ICONSDIR "/bcg.png";
    char *image_path = "file://" EVOLUTION_ICONSDIR "/alarm.png";
    icaltimezone *current_zone;

    /* Stringize the times */

    current_zone = config_data_get_timezone ();

    buf = timet_to_str_with_zone (occur_start, current_zone);
    start = e_utf8_from_locale_string (buf);
    g_free (buf);

    buf = timet_to_str_with_zone (occur_end, current_zone);
    end = e_utf8_from_locale_string (buf);
    g_free (buf);

    /* Write the header */

    gtk_html_stream_printf (stream,
                "<HTML><BODY background=\"%s\">"
                "<TABLE WIDTH=\"100%%\">"
                "<TR>"
                "<TD><IMG SRC=\"%s\" ALIGN=\"top\" BORDER=\"0\"></TD>"
                "<TD><H1>%s</H1></TD>"
                "</TR>"
                "</TABLE>",
                bg_path,
                image_path,
                _("Evolution Alarm"));

    gtk_html_stream_printf (stream, "<br><br><font size=\"+2\">%s</font><br><br>", message);

    /* Write the times */

    switch (vtype) {
    case CAL_COMPONENT_EVENT:
        write_times (stream, start, end);
        break;

    case CAL_COMPONENT_TODO:
        write_times (stream, start, end);
        break;

    default:
        /* Only VEVENTs and VTODOs can have alarms */
        g_assert_not_reached ();
        break;
    }

    g_free (start);
    g_free (end);
}

/**
 * alarm_notify_dialog:
 * @trigger: Trigger time for the alarm.
 * @occur_start: Start of occurrence time for the event.
 * @occur_end: End of occurrence time for the event.
 * @vtype: Type of the component which corresponds to the alarm.
 * @message; Message to display in the dialog; usually comes from the component.
 * @func: Function to be called when a dialog action is invoked.
 * @func_data: Closure data for @func.
 *
 * Runs the alarm notification dialog.  The specified @func will be used to
 * notify the client about result of the actions in the dialog.
 *
 * Return value: TRUE on success, FALSE if the dialog could not be created.
 **/
gboolean
alarm_notify_dialog (time_t trigger, time_t occur_start, time_t occur_end,
             CalComponentVType vtype, const char *message,
             AlarmNotifyFunc func, gpointer func_data)
{
    AlarmNotify *an;
    GtkHTMLStream *stream;
    icaltimezone *current_zone;
    char *buf, *title;

    g_return_val_if_fail (trigger != -1, FALSE);

    /* Only VEVENTs or VTODOs can have alarms */
    g_return_val_if_fail (vtype == CAL_COMPONENT_EVENT || vtype == CAL_COMPONENT_TODO, FALSE);
    g_return_val_if_fail (message != NULL, FALSE);
    g_return_val_if_fail (func != NULL, FALSE);

    an = g_new0 (AlarmNotify, 1);

    an->func = func;
    an->func_data = func_data;

    an->xml = glade_xml_new (EVOLUTION_GLADEDIR "/alarm-notify.glade", NULL, NULL);
    if (!an->xml) {
        g_message ("alarm_notify_dialog(): Could not load the Glade XML file!");
        g_free (an);
        return FALSE;
    }

    an->dialog = glade_xml_get_widget (an->xml, "alarm-notify");
    an->close = glade_xml_get_widget (an->xml, "close");
    an->snooze = glade_xml_get_widget (an->xml, "snooze");
    an->edit = glade_xml_get_widget (an->xml, "edit");
    an->heading = glade_xml_get_widget (an->xml, "heading");
    an->message = glade_xml_get_widget (an->xml, "message");
    an->snooze_time = glade_xml_get_widget (an->xml, "snooze-time");
    an->html = g_object_get_data (G_OBJECT (glade_xml_get_widget (an->xml, "frame")), "html");

    if (!(an->dialog && an->close && an->snooze && an->edit && an->heading && an->message
          && an->snooze_time)) {
        g_message ("alarm_notify_dialog(): Could not find all widgets in Glade file!");
        g_object_unref (an->xml);
        g_free (an);
        return FALSE;
    }

    g_signal_connect (an->dialog, "destroy",
              G_CALLBACK (dialog_destroy_cb),
              an);

    /* Title */

    current_zone = config_data_get_timezone ();

    buf = timet_to_str_with_zone (trigger, current_zone);
    title = g_strdup_printf (_("Alarm on %s"), buf);
    g_free (buf);

    gtk_window_set_title (GTK_WINDOW (an->dialog), title);
    g_free (title);

    /* html heading */
    stream = gtk_html_begin (GTK_HTML (an->html));
    write_html_heading (stream, message, vtype, occur_start, occur_end);
    gtk_html_stream_close (stream, GTK_HTML_STREAM_OK);

    /* Connect actions */

    g_signal_connect (an->dialog, "delete_event",
              G_CALLBACK (delete_event_cb),
              an);

    g_signal_connect (an->close, "clicked",
              G_CALLBACK (close_clicked_cb),
              an);

    g_signal_connect (an->snooze, "clicked",
              G_CALLBACK (snooze_clicked_cb),
              an);

    g_signal_connect (an->edit, "clicked",
              G_CALLBACK (edit_clicked_cb),
              an);

    /* Run! */

    if (!GTK_WIDGET_REALIZED (an->dialog))
        gtk_widget_realize (an->dialog);

#if 0
    gnome_win_hints_set_state (an->dialog, WIN_STATE_STICKY);
    gnome_win_hints_set_layer (an->dialog, WIN_LAYER_ONTOP);
    gnome_window_icon_set_from_file (GTK_WINDOW (an->dialog), EVOLUTION_ICONSDIR "/alarm.png");
#endif

    gtk_widget_show (an->dialog);
    return TRUE;
}