File: jana-ecal-utils.c

package info (click to toggle)
jana 0.0.0%2Bgit20091215.9ec1da8a-4
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 1,972 kB
  • ctags: 1,943
  • sloc: ansic: 17,575; makefile: 290; xml: 263; sh: 1
file content (263 lines) | stat: -rw-r--r-- 7,104 bytes parent folder | download | duplicates (2)
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
/*
 * Author: Chris Lord <chris@linux.intel.com>
 * Copyright (c) 2007 OpenedHand Ltd
 * Copyright (C) 2008 - 2009 Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU Lesser General Public License,
 * version 2.1, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope 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 this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 */


/**
 * SECTION:jana-ecal-utils
 * @short_description: A set of utility functions for libjana-ecal
 *
 * jana-ecal-utils contains a set of libjana-ecal related utility functions.
 */

#define HANDLE_LIBICAL_MEMORY 1

#include <string.h>
#include <libical/icaltime.h>
#include <libjana/jana-utils.h>
#include <libecal/libecal.h>
#include <libjana-ecal/jana-ecal-time.h>
#include <gconf/gconf-client.h>
#include "jana-ecal-utils.h"

/**
 * jana_ecal_utils_time_now:
 * @location: A full timezone name
 *
 * Creates a new #JanaEcalTime with the current time of the given location.
 *
 * Returns: A new #JanaEcalTime, cast as a #JanaTime.
 */
JanaTime *
jana_ecal_utils_time_now (const gchar *location)
{
	icaltimetype ical_time;
	const icaltimezone *zone;
	
	zone = (const icaltimezone *)icaltimezone_get_builtin_timezone (
		location);
	ical_time = icaltime_current_time_with_zone (zone);
	icaltime_set_timezone (&ical_time, zone);

	return jana_ecal_time_new_from_icaltime (&ical_time);
}

/**
 * jana_ecal_utils_time_now_local:
 *
 * Creates a new #JanaEcalTime with the current time of the guessed location. 
 * See jana_ecal_utils_guess_location().
 *
 * Returns: A new #JanaEcalTime, cast as a #JanaTime.
 */
JanaTime *
jana_ecal_utils_time_now_local ()
{
	gchar *location = jana_ecal_utils_guess_location ();
	JanaTime *time = jana_ecal_utils_time_now (location);
	g_free (location);
	return time;
}

/**
 * jana_ecal_utils_time_today:
 * @location: A full timezone name
 *
 * Creates a new #JanaEcalTime with the current date of the given location.
 *
 * Returns: A new #JanaEcalTime, cast as a #JanaTime.
 */
JanaTime *
jana_ecal_utils_time_today (const gchar *location)
{
	JanaTime *time;
	icaltimetype ical_time;
	const icaltimezone *zone;
	
	zone = (const icaltimezone *)icaltimezone_get_builtin_timezone (
		location);
	ical_time = icaltime_current_time_with_zone (zone);
	icaltime_set_timezone (&ical_time, zone);

	time = jana_ecal_time_new_from_icaltime (&ical_time);
	jana_time_set_isdate (time, TRUE);

	return time;
}

/**
 * jana_ecal_utils_guess_location:
 *
 * Tries to guess the location by checking for common system settings and 
 * files, and finally falling back on the first location that matches the
 * current system timezone. See also #JANA_ECAL_LOCATION_KEY.
 *
 * Returns: A newly allocated string with the guessed location.
 */
gchar *
jana_ecal_utils_guess_location ()
{
	gint i;
	FILE *file;
	gchar *tzname;
	gchar string[128];
	icaltimetype today;
	icalarray *builtin;
	time_t now_t = time (NULL);
	struct tm *now = localtime (&now_t);

	/* Check the Evolution timezone key first */
	tzname = gconf_client_get_string (gconf_client_get_default (),
		JANA_ECAL_LOCATION_KEY, NULL);
	if (tzname && icaltimezone_get_builtin_timezone (tzname)) return tzname;
	g_free (tzname);
	
	/* Debian systems have /etc/timezone */
	if ((file = fopen ("/etc/timezone", "r"))) {
		if ((fgets (string, 128, file)) && (string[0] != '\0')) {
			gint c;
			for (c = 0; (string[c] != '\0') &&
			     (string[c] != '\n'); c++);
			string[c] = '\0';
			if (icaltimezone_get_builtin_timezone (string)) {
				fclose (file);
				return g_strdup (string);
			}
		}
		fclose (file);
	}
	
#if GLIB_CHECK_VERSION(2,14,0)
	/* OpenSuSE (and RH?) systems have /etc/sysconfig/clock */
	if ((file = fopen ("/etc/sysconfig/clock", "r"))) {
		GRegex *regex;
		GError *error = NULL;

		regex = g_regex_new ("ZONE *= *\".*\"",
			G_REGEX_OPTIMIZE, 0, &error);
		if (!regex) {
			g_warning ("Failed to create regex: %s",
				error->message);
			g_error_free (error);
		} else while (fgets (string, 128, file)) {
			GMatchInfo *match_info;
			if (g_regex_match (regex, string, 0, &match_info)) {
				gchar *zone;
				gchar *match = g_match_info_fetch (
					match_info, 0);
				
				g_match_info_free (match_info);
				g_regex_unref (regex);
				regex = NULL;
				
				if (match[strlen (match) - 2] == '\n')
					match[strlen (match)-2] = '\0';
				else
					match[strlen (match)-1] = '\0';
				zone = g_strdup (strchr (match, '\"') + 1);
				g_free (match);
				
				if (icaltimezone_get_builtin_timezone (zone)) {
					fclose (file);
					return zone;
				} else {
					g_free (zone);
					break;
				}
			}
			g_match_info_free (match_info);
		}
		if (regex) g_regex_unref (regex);
		fclose (file);
	}
#endif
	
	/* Check to see if the /etc/localtime symlink exists */
	if (g_file_test ("/etc/localtime", G_FILE_TEST_IS_SYMLINK)) {
		gchar *link;
		if ((link = g_file_read_link ("/etc/localtime", NULL))) {
			tzname = g_strrstr (link, "zoneinfo/");
			if (tzname &&
			    icaltimezone_get_builtin_timezone (tzname + 9)) {
				tzname = g_strdup (tzname + 9);
				g_free (link);
				return tzname;
			}
			g_free (link);
		}
	}
	
	/* Fallback to first location that matches libc timezone and the
	 * offset for the current time.
	 */
	today = icaltime_today ();
	tzname = jana_utils_get_local_tzname ();
	if (strcmp (tzname, "UTC") == 0) return tzname;
	
	builtin = icaltimezone_get_builtin_timezones ();
	for (i = 0; i < builtin->num_elements; i++) {
		icaltimezone *zone = (icaltimezone *)icalarray_element_at (
			builtin, i);
		gchar *zone_tz;
		int offset;
		
		offset = icaltimezone_get_utc_offset (zone, &today, NULL);
		zone_tz = icaltimezone_get_tznames (zone);

		if (zone_tz && (strcasecmp (tzname, zone_tz) == 0) &&
		    (offset == now->tm_gmtoff)) {
			g_free (tzname);
			return g_strdup (icaltimezone_get_display_name (zone));
		}
	}
	g_free (tzname);
	
	/* No matching timezone found, fall back to UTC */
	return g_strdup ("UTC");
}

/**
 * jana_ecal_utils_get_locations:
 *
 * Retrieves the built-in list of timezone locations from libecal.
 *
 * Returns: A newly allocated string array, to be freed with g_strfreev().
 */
gchar **
jana_ecal_utils_get_locations ()
{
	gint i, z;
	icalarray *builtin;
	gchar **locations;
	
	builtin = icaltimezone_get_builtin_timezones ();
	locations = g_new0 (gchar *, builtin->num_elements + 1);
	for (i = 0, z = 0; i < builtin->num_elements; i++) {
		icaltimezone *zone = (icaltimezone *)icalarray_element_at (
			builtin, i);

		/* Don't return zones that don't have a tzname */
		if (icaltimezone_get_tznames (zone)) {
			locations[z] = g_strdup (
				icaltimezone_get_display_name (zone));
			z++;
		}
	}
	
	return locations;
}