aboutsummaryrefslogtreecommitdiffstats
path: root/filter/filter-datespec.c
blob: 21435c37be884a1a96906f33821f3da164589d57 (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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  Copyright (C) 2000-2002 Ximian Inc.
 *
 *  Authors: Not Zed <notzed@lostzed.mmc.com.au>
 *           Jeffrey Stedfast <fejj@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.
 */


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

#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

#include <gtk/gtk.h>
#include <libgnome/gnome-i18n.h>
#include <glade/glade.h>

#include "filter-datespec.h"
#include "e-util/e-sexp.h"

#define d(x)

static gboolean validate (FilterElement *fe);
static int date_eq (FilterElement *fe, FilterElement *cm);
static void xml_create (FilterElement *fe, xmlNodePtr node);
static xmlNodePtr xml_encode (FilterElement *fe);
static int xml_decode (FilterElement *fe, xmlNodePtr node);
static GtkWidget *get_widget (FilterElement *fe);
static void build_code (FilterElement *fe, GString *out, struct _FilterPart *fds);
static void format_sexp (FilterElement *, GString *);

static void filter_datespec_class_init (FilterDatespecClass *klass);
static void filter_datespec_init (FilterDatespec *fd);
static void filter_datespec_finalise (GObject *obj);

#define PRIV(x) (((FilterDatespec *)(x))->priv)

typedef struct _timespan {
    guint32 seconds;
    const char *singular;
    const char *plural;
    float max;
} timespan;

static const timespan timespans[] = {
    { 1, N_("second"), N_("seconds"), 59.0 },
    { 60, N_("minute"), N_("minutes"), 59.0 },
    { 3600, N_("hour"), N_("hours"), 23.0 },
    { 86400, N_("day"), N_("days"), 31.0 },
    { 604800, N_("week"), N_("weeks"), 52.0 },
    { 2419200, N_("month"), N_("months"), 12.0 },
    { 31557600, N_("year"), N_("years"), 1000.0 },
};

#define DAY_INDEX 3
#define N_TIMESPANS (sizeof (timespans) / sizeof (timespans[0]))

struct _FilterDatespecPrivate {
    GtkWidget *label_button;
    GtkWidget *notebook_type, *option_type, *calendar_specify, *spin_relative, *option_relative;
    FilterDatespec_type type;
    int span;
};

static FilterElementClass *parent_class;

GType
filter_datespec_get_type (void)
{
    static GType type = 0;
    
    if (!type) {
        static const GTypeInfo info = {
            sizeof (FilterDatespecClass),
            NULL, /* base_class_init */
            NULL, /* base_class_finalize */
            (GClassInitFunc) filter_datespec_class_init,
            NULL, /* class_finalize */
            NULL, /* class_data */
            sizeof (FilterDatespec),
            0,    /* n_preallocs */
            (GInstanceInitFunc) filter_datespec_init,
        };
        
        type = g_type_register_static (FILTER_TYPE_ELEMENT, "FilterDatespec", &info, 0);
    }
    
    return type;
}

static void
filter_datespec_class_init (FilterDatespecClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);
    FilterElementClass *fe_class = FILTER_ELEMENT_CLASS (klass);
    
    parent_class = g_type_class_ref (FILTER_TYPE_ELEMENT);
    
    object_class->finalize = filter_datespec_finalise;
    
    /* override methods */
    fe_class->validate = validate;
    fe_class->eq = date_eq;
    fe_class->xml_create = xml_create;
    fe_class->xml_encode = xml_encode;
    fe_class->xml_decode = xml_decode;
    fe_class->get_widget = get_widget;
    fe_class->build_code = build_code;
    fe_class->format_sexp = format_sexp;
}

static void
filter_datespec_init (FilterDatespec *fd)
{
    fd->priv = g_malloc0 (sizeof (*fd->priv));
    fd->type = FDST_UNKNOWN;
}

static void
filter_datespec_finalise (GObject *obj)
{
    FilterDatespec *fd = (FilterDatespec *) obj;
    
    g_free (fd->priv);
    
        G_OBJECT_CLASS (parent_class)->finalize (obj);
}

/**
 * filter_datespec_new:
 *
 * Create a new FilterDatespec object.
 * 
 * Return value: A new #FilterDatespec object.
 **/
FilterDatespec *
filter_datespec_new (void)
{
    return (FilterDatespec *) g_object_new (FILTER_TYPE_DATESPEC, NULL, NULL);
}

static gboolean
validate (FilterElement *fe)
{
    FilterDatespec *fds = (FilterDatespec *) fe;
    GtkWidget *dialog;
    gboolean valid;
    
    valid = fds->type != FDST_UNKNOWN;
    if (!valid) {
        /* FIXME: FilterElement should probably have a
                   GtkWidget member pointing to the value gotten with
                   ::get_widget() so that we can get the parent window
                   here. */
        dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_DESTROY_WITH_PARENT,
                         GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
                         "%s", _("You must choose a date."));
        
        gtk_dialog_run ((GtkDialog *) dialog);
        gtk_widget_destroy (dialog);
    }
    
    return valid;
}

static int
date_eq (FilterElement *fe, FilterElement *cm)
{
    FilterDatespec *fd = (FilterDatespec *)fe, *cd = (FilterDatespec *)cm;
    
        return FILTER_ELEMENT_CLASS (parent_class)->eq(fe, cm)
        && (fd->type == cd->type)
        && (fd->value == cd->value);
}

static void
xml_create (FilterElement *fe, xmlNodePtr node)
{
    /* parent implementation */
        FILTER_ELEMENT_CLASS (parent_class)->xml_create (fe, node);
}

static xmlNodePtr
xml_encode (FilterElement *fe)
{
    xmlNodePtr value, work;
    FilterDatespec *fds = (FilterDatespec *)fe;
    char str[32];
    
    d(printf ("Encoding datespec as xml\n"));
    
    value = xmlNewNode (NULL, "value");
    xmlSetProp (value, "name", fe->name);
    xmlSetProp (value, "type", "datespec");
    
    work = xmlNewChild (value, NULL, "datespec", NULL);
    sprintf (str, "%d", fds->type);
    xmlSetProp (work, "type", str);
    sprintf (str, "%d", (int)fds->value);
    xmlSetProp (work, "value", str);
    
    return value;
}

static int
xml_decode (FilterElement *fe, xmlNodePtr node)
{
    FilterDatespec *fds = (FilterDatespec *)fe;
    xmlNodePtr n;
    char *val;
    
    d(printf ("Decoding datespec from xml %p\n", fe));
    
    xmlFree (fe->name);
    fe->name = xmlGetProp (node, "name");
    
    n = node->children;
    while (n) {
        if (!strcmp (n->name, "datespec")) {
            val = xmlGetProp (n, "type");
            fds->type = atoi (val);
            xmlFree (val);
            val = xmlGetProp (n, "value");
            fds->value = atoi (val);
            xmlFree (val);
            break;
        }
        n = n->next;
    }
    
    return 0;
}

static int
get_best_span (time_t val)
{
    int i;
    
    for (i=N_TIMESPANS-1;i>=0;i--) {
        if (val % timespans[i].seconds == 0)
            return i;
    }
    
    return 0;
}

/* sets button label */
static void
set_button (FilterDatespec *fds)
{
    char buf[128];
    char *label = buf;
    
    switch (fds->type) {
    case FDST_UNKNOWN:
        label = _("<click here to select a date>");
        break;
    case FDST_NOW:
        label = _("now");
        break;
    case FDST_SPECIFIED: {
        struct tm tm;
        
        localtime_r(&fds->value, &tm);
        /* strftime for date filter display, only needs to show a day date (i.e. no time) */
        strftime(buf, sizeof(buf), _("%d-%b-%Y"), &tm);
        break; }
    case FDST_X_AGO:
        if (fds->value == 0)
            label = _("now");
        else {
            int span, count;
            
            span = get_best_span(fds->value);
            count = fds->value / timespans[span].seconds;
            
            if (count == 1)
                /* 1 (minute|day|...) ago (singular time ago) */
                sprintf(buf, _("%d %s ago"), count, timespans[span].singular);
            else
                /* N (minutes|days|...) ago (plural time ago) */
                sprintf(buf, _("%d %s ago"), count, timespans[span].plural);
        }
        break;
    }
    
    gtk_label_set_text((GtkLabel *)fds->priv->label_button, label);
}

static void
get_values (FilterDatespec *fds)
{
    struct _FilterDatespecPrivate *p = PRIV(fds);
    
    switch(fds->priv->type) {
    case FDST_SPECIFIED: {
        guint year, month, day;
        struct tm tm;
        
        gtk_calendar_get_date((GtkCalendar *)p->calendar_specify, &year, &month, &day);
        memset(&tm, 0, sizeof(tm));
        tm.tm_mday = day;
        tm.tm_year = year - 1900;
        tm.tm_mon = month;
        fds->value = mktime(&tm);
        /* what about timezone? */
        break; }
    case FDST_X_AGO: {
        int val;
        
        val = gtk_spin_button_get_value_as_int((GtkSpinButton *)p->spin_relative);
        fds->value = timespans[p->span].seconds * val;
        break; }
    case FDST_NOW:
    default:
        break;
    }
    
    fds->type = p->type;
}

static void
set_values (FilterDatespec *fds)
{
    struct _FilterDatespecPrivate *p = PRIV(fds);
    
    p->type = fds->type==FDST_UNKNOWN ? FDST_NOW : fds->type;
    
    switch (p->type) {
    case FDST_NOW:
    case FDST_UNKNOWN:
        /* noop */
        break;
    case FDST_SPECIFIED:
    {
        struct tm tm;
        
        localtime_r(&fds->value, &tm);
        gtk_calendar_select_month((GtkCalendar*)p->calendar_specify, tm.tm_mon, tm.tm_year + 1900);
        gtk_calendar_select_day((GtkCalendar*)p->calendar_specify, tm.tm_mday);
        break;
    }
    case FDST_X_AGO:
        p->span = get_best_span(fds->value);
        gtk_spin_button_set_value((GtkSpinButton*)p->spin_relative, fds->value/timespans[p->span].seconds);
        gtk_option_menu_set_history((GtkOptionMenu*)p->option_relative, p->span);
        break;
    }
    
    gtk_notebook_set_current_page ((GtkNotebook*) p->notebook_type, p->type);
    gtk_option_menu_set_history ((GtkOptionMenu*) p->option_type, p->type);
}


static void
set_option_type (GtkMenu *menu, FilterDatespec *fds)
{
    GtkWidget *w;

    /* ugh, no other way to 'get_history' */
    w = gtk_menu_get_active (menu);
    fds->priv->type = g_list_index (GTK_MENU_SHELL (menu)->children, w);
    gtk_notebook_set_current_page ((GtkNotebook*) fds->priv->notebook_type, fds->priv->type);
}

static void
set_option_relative (GtkMenu *menu, FilterDatespec *fds)
{
    GtkWidget *w;
    
    w = gtk_menu_get_active (menu);
    fds->priv->span = g_list_index (GTK_MENU_SHELL (menu)->children, w);
}

static void
dialog_response (GtkWidget *dialog, int button, FilterDatespec *fds)
{
    if (button == GTK_RESPONSE_ACCEPT) {
        get_values (fds);
        set_button (fds);
    }
    
    gtk_widget_destroy (dialog);
}

static void
button_clicked (GtkButton *button, FilterDatespec *fds)
{
    struct _FilterDatespecPrivate *p = PRIV(fds);
    GtkWidget *toplevel;
    GtkDialog *dialog;
    GladeXML *gui;
    
    gui = glade_xml_new (FILTER_GLADEDIR "/filter.glade", "filter_datespec", NULL);
    toplevel = glade_xml_get_widget (gui, "filter_datespec");
    
    dialog = (GtkDialog *) gtk_dialog_new ();
    gtk_window_set_title ((GtkWindow *) dialog, _("Select a time to compare against"));
    gtk_dialog_add_buttons (dialog,
                GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
                GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
                NULL);
    
    p->notebook_type = glade_xml_get_widget (gui, "notebook_type");
    p->option_type = glade_xml_get_widget (gui, "option_type");
    p->calendar_specify = glade_xml_get_widget (gui, "calendar_specify");
    p->spin_relative = glade_xml_get_widget (gui, "spin_relative");
    p->option_relative = glade_xml_get_widget (gui, "option_relative");
    
    set_values (fds);
    
    g_signal_connect (GTK_OPTION_MENU (p->option_type)->menu, "deactivate",
              G_CALLBACK (set_option_type), fds);
    g_signal_connect (GTK_OPTION_MENU (p->option_relative)->menu, "deactivate",
              G_CALLBACK (set_option_relative), fds);
    
    gtk_box_pack_start ((GtkBox *) dialog->vbox, toplevel, TRUE, TRUE, 3);
    
    g_signal_connect (dialog, "response", G_CALLBACK (dialog_response), fds);
    
    gtk_dialog_run (dialog);
}

static GtkWidget *
get_widget (FilterElement *fe)
{
    FilterDatespec *fds = (FilterDatespec *)fe;
    GtkWidget *button;
    
    fds->priv->label_button = gtk_label_new ("");
    gtk_misc_set_alignment (GTK_MISC (fds->priv->label_button), 0.5, 0.5);
    set_button(fds);
    
    button = gtk_button_new();
    gtk_container_add (GTK_CONTAINER (button), fds->priv->label_button);
    g_signal_connect (button, "clicked", G_CALLBACK (button_clicked), fds);
    
    gtk_widget_show (button);
    gtk_widget_show (fds->priv->label_button);
    
    return button;
}

static void 
build_code (FilterElement *fe, GString *out, struct _FilterPart *fp)
{
    return;
}

static void 
format_sexp (FilterElement *fe, GString *out)
{
    FilterDatespec *fds = (FilterDatespec *)fe;
    
    switch (fds->type) {
    case FDST_UNKNOWN:
        g_warning ("user hasn't selected a datespec yet!");
        /* fall through */
    case FDST_NOW:
        g_string_append (out, "(get-current-date)");
        break;
    case FDST_SPECIFIED:
        g_string_append_printf (out, "%d", (int) fds->value);
        break;
    case FDST_X_AGO:
        g_string_append_printf (out, "(- (get-current-date) %d)", (int) fds->value);
        break;
    }
}