aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/gui/e-meeting-utils.c
blob: 733052674cf6a4382a7f06e19b006410d86d53e6 (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
/*
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 *
 * Authors:
 *      JP Rosevear <jpr@novell.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

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

#include <string.h>
#include <libedataserver/libedataserver.h>

#include "e-meeting-utils.h"

gint
e_meeting_time_compare_times (EMeetingTime *time1,
                              EMeetingTime *time2)
{
    gint day_comparison;

    day_comparison = g_date_compare (&time1->date, &time2->date);

    if (day_comparison != 0)
        return day_comparison;

    if (time1->hour < time2->hour)
        return -1;
    if (time1->hour > time2->hour)
        return 1;

    if (time1->minute < time2->minute)
        return -1;
    if (time1->minute > time2->minute)
        return 1;

    /* The start times are exactly the same. */
    return 0;
}

void
e_meeting_xfb_data_init (EMeetingXfbData *xfb)
{
    g_return_if_fail (xfb != NULL);

    xfb->summary = NULL;
    xfb->location = NULL;
}

void
e_meeting_xfb_data_set (EMeetingXfbData *xfb,
                        const gchar *summary,
                        const gchar *location)
{
    g_return_if_fail (xfb != NULL);

    e_meeting_xfb_data_clear (xfb);
    xfb->summary = g_strdup (summary);
    xfb->location = g_strdup (location);
}

void
e_meeting_xfb_data_clear (EMeetingXfbData *xfb)
{
    g_return_if_fail (xfb != NULL);

    /* clearing the contents of xfb,
     * but not the xfb structure itself
     */

    if (xfb->summary != NULL) {
        g_free (xfb->summary);
        xfb->summary = NULL;
    }
    if (xfb->location != NULL) {
        g_free (xfb->location);
        xfb->location = NULL;
    }
}

/* Creates an XFB string from a string property of a vfreebusy
 * icalproperty. The ical string we read may be base64 encoded, but
 * we get no reliable indication whether it really is. So we
 * try to base64-decode, and failing that, assume the string
 * is plain. The result is validated for UTF-8. We try to convert
 * to UTF-8 from locale if the input is no valid UTF-8, and failing
 * that, force the result into valid UTF-8. We also limit the
 * length of the resulting string, since it gets displayed as a
 * tooltip text in the meeting time selector.
 */
gchar *
e_meeting_xfb_utf8_string_new_from_ical (const gchar *icalstring,
                                         gsize max_len)
{
    gchar *tmp = NULL;
    gchar *utf8s = NULL;
    gsize in_len = 0;
    gsize out_len = 0;
    GError *tmp_err = NULL;

    g_return_val_if_fail (max_len > 4, NULL);

    if (icalstring == NULL)
        return NULL;

    /* ical does not carry charset hints, so we
     * try UTF-8 first, then conversion using
     * system locale info.
     */

    /* if we have valid UTF-8, we're done converting */
    if (g_utf8_validate (icalstring, -1, NULL))
        goto valid;

    /* no valid UTF-8, trying to convert to it
     * according to system locale
     */
    tmp = g_locale_to_utf8 (
        icalstring, -1, &in_len, &out_len, &tmp_err);

    if (tmp_err == NULL)
        goto valid;

    g_warning ("%s: %s", G_STRFUNC, tmp_err->message);
    g_error_free (tmp_err);
    g_free (tmp);

    /* still no success, forcing it into UTF-8, using
     * replacement chars to replace invalid ones
     */
    tmp = e_util_utf8_data_make_valid (
        icalstring, strlen (icalstring));
 valid:
    if (tmp == NULL)
        tmp = g_strdup (icalstring);

    /* now that we're (forcibly) valid UTF-8, we can
     * limit the size of the UTF-8 string for display
     */

    if (g_utf8_strlen (tmp, -1) > (glong) max_len) {
        /* insert NULL termination to where we want to
         * clip, take care to hit UTF-8 character boundary
         */
        utf8s = g_utf8_offset_to_pointer (tmp, (glong) max_len - 4);
        *utf8s = '\0';
        /* create shortened UTF-8 string */
        utf8s = g_strdup_printf ("%s ...", tmp);
        g_free (tmp);
    } else {
        utf8s = tmp;
    }

    return utf8s;
}