aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/gui/dialogs/comp-editor-util.c
blob: 9be0a394f0e5485a2c8a39a64580a8fed02c561d (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
/* Evolution calendar - Widget utilities
 *
 * Copyright (C) 2000 Helix Code, Inc.
 *
 * Author: Federico Mena-Quintero <federico@helixcode.com>
 *
 * 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 <string.h>
#include <glib.h>
#include <libgnome/gnome-defs.h>
#include <libgnome/gnome-i18n.h>
#include <e-util/e-time-utils.h>
#include "../calendar-config.h"
#include "comp-editor-util.h"



/**
 * comp_editor_dates:
 * @dates: A structure to be filled out with dates of a component
 * @comp: The component to extract the dates from
 * 
 * Extracts the dates from the calendar component into the
 * CompEditorPageDates structure
 **/
void
comp_editor_dates (CompEditorPageDates *dates, CalComponent *comp)
{
    CalComponentDateTime dt;
    struct icaltimetype *completed;

    dates->start = 0;
    dates->end = 0;
    dates->due = 0;
    dates->complete = 0;
    
    cal_component_get_dtstart (comp, &dt);
    if (dt.value)
        dates->start = icaltime_as_timet (*dt.value);

    cal_component_get_dtend (comp, &dt);
    if (dt.value)
        dates->end = icaltime_as_timet (*dt.value);

    cal_component_get_due (comp, &dt);
    if (dt.value)
        dates->due = icaltime_as_timet (*dt.value);

    cal_component_get_completed (comp, &completed);
    if (completed) {
        dates->complete = icaltime_as_timet (*completed);
        cal_component_free_icaltimetype (completed);
    }
}

static void
write_label_piece (time_t t, char *buffer, int size, char *stext, char *etext)
{
    struct tm *tmp_tm;
    int len;
    
    tmp_tm = localtime (&t);
    if (stext != NULL)
        strcat (buffer, stext);

    len = strlen (buffer);
    e_time_format_date_and_time (tmp_tm,
                     calendar_config_get_24_hour_format (), 
                     FALSE, FALSE,
                     &buffer[len], size - len);
    if (etext != NULL)
        strcat (buffer, etext);
}

/**
 * comp_editor_date_label:
 * @dates: The dates to use in constructing a label
 * @label: The label whose text is to be set
 * 
 * Set the text of a label based on the dates available and the user's
 * formatting preferences
 **/
void
comp_editor_date_label (CompEditorPageDates *dates, GtkWidget *label)
{
    static char buffer[1024];

    buffer[0] = '\0';

    if (dates->start > 0)
        write_label_piece (dates->start, buffer, 1024, NULL, NULL);

    if (dates->end > 0 && dates->start > 0)
        write_label_piece (dates->end, buffer, 1024, _(" to "), NULL);

    if (dates->complete > 0) {
        if (dates->start > 0)
            write_label_piece (dates->complete, buffer, 1024, _(" (Completed "), ")");
        else
            write_label_piece (dates->complete, buffer, 1024, _("Completed "), NULL);
    }
    
    if (dates->due > 0 && dates->complete == 0) {
        if (dates->start > 0)
            write_label_piece (dates->due, buffer, 1024, _(" (Due "), ")");
        else
            write_label_piece (dates->due, buffer, 1024, _("Due "), NULL);
    }

    gtk_label_set_text (GTK_LABEL (label), buffer);
}

/**
 * comp_editor_new_date_edit:
 * @show_date: Whether to show a date picker in the widget.
 * @show_time: Whether to show a time picker in the widget.
 * 
 * Creates a new #EDateEdit widget, configured using the calendar's preferences.
 * 
 * Return value: A newly-created #EDateEdit widget.
 **/
GtkWidget *
comp_editor_new_date_edit (gboolean show_date, gboolean show_time)
{
    EDateEdit *dedit;

    dedit = E_DATE_EDIT (e_date_edit_new ());

    e_date_edit_set_show_date (dedit, show_date);
    e_date_edit_set_show_time (dedit, show_time);

    calendar_config_configure_e_date_edit (dedit);

    return GTK_WIDGET (dedit);
}