~ubuntu-branches/ubuntu/natty/evolution-data-server/natty

« back to all changes in this revision

Viewing changes to .pc/199-git-backport-2.32.1-to-b08a6a1.patch/calendar/libecal/e-cal-system-timezone.c

  • Committer: Bazaar Package Importer
  • Author(s): Robert Ancell
  • Date: 2011-02-07 14:11:56 UTC
  • mfrom: (1.1.84 upstream)
  • Revision ID: james.westby@ubuntu.com-20110207141156-jakojbbkzee62447
Tags: 2.32.2-0ubuntu1
* New upstream release
* debian/patches/01_various_linking_issues.patch:
* debian/patches/02_fix_sources_migration.patch:
* debian/patches/199-git-backport-2.32.1-b08a6a1_to_2d0f4a3.patch:
* debian/patches/199-git-backport-2.32.1-to-b08a6a1.patch:
* debian/patches/199-git-skip-empty-cache-files-ee21a86.patch:
  - Fixed upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Evolution calendar system timezone functions
2
 
 * Based on gnome-panel's clock-applet system-timezone.c file.
3
 
 *
4
 
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
5
 
 *
6
 
 * This program is free software; you can redistribute it and/or
7
 
 * modify it under the terms of version 2 of the GNU Lesser General Public
8
 
 * License as published by the Free Software Foundation.
9
 
 *
10
 
 * This program is distributed in the hope that it will be useful,
11
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 * GNU Lesser General Public License for more details.
14
 
 *
15
 
 * You should have received a copy of the GNU Lesser General Public License
16
 
 * along with this program; if not, write to the Free Software
17
 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
 
 */
19
 
 
20
 
#include <glib.h>
21
 
#include <glib/gstdio.h>
22
 
#include <gio/gio.h>
23
 
#include <string.h>
24
 
#include "e-cal-system-timezone.h"
25
 
 
26
 
#ifndef G_OS_WIN32
27
 
 
28
 
#ifdef HAVE_SOLARIS
29
 
#define SYSTEM_ZONEINFODIR "/usr/share/lib/zoneinfo/tab"
30
 
#else
31
 
#define SYSTEM_ZONEINFODIR "/usr/share/zoneinfo"
32
 
#endif
33
 
 
34
 
#define ETC_TIMEZONE        "/etc/timezone"
35
 
#define ETC_TIMEZONE_MAJ    "/etc/TIMEZONE"
36
 
#define ETC_RC_CONF         "/etc/rc.conf"
37
 
#define ETC_SYSCONFIG_CLOCK "/etc/sysconfig/clock"
38
 
#define ETC_CONF_D_CLOCK    "/etc/conf.d/clock"
39
 
#define ETC_LOCALTIME       "/etc/localtime"
40
 
 
41
 
#define TZ_MAGIC "TZif"
42
 
 
43
 
static gchar *
44
 
system_timezone_strip_path_if_valid (const gchar *filename)
45
 
{
46
 
        gint skip;
47
 
 
48
 
        if (!filename || !g_str_has_prefix (filename, SYSTEM_ZONEINFODIR"/"))
49
 
                return NULL;
50
 
 
51
 
        /* Timezone data files also live under posix/ and right/ for some
52
 
         * reason.
53
 
         * FIXME: make sure accepting those files is valid. I think "posix" is
54
 
         * okay, not sure about "right" */
55
 
        if (g_str_has_prefix (filename, SYSTEM_ZONEINFODIR"/posix/"))
56
 
                skip = strlen (SYSTEM_ZONEINFODIR"/posix/");
57
 
        else if (g_str_has_prefix (filename, SYSTEM_ZONEINFODIR"/right/"))
58
 
                skip = strlen (SYSTEM_ZONEINFODIR"/right/");
59
 
        else
60
 
                skip = strlen (SYSTEM_ZONEINFODIR"/");
61
 
 
62
 
        return g_strdup (filename + skip);
63
 
}
64
 
 
65
 
/* Read the soft symlink from /etc/localtime */
66
 
static gchar *
67
 
system_timezone_read_etc_localtime_softlink (void)
68
 
{
69
 
        gchar *file;
70
 
        gchar *tz;
71
 
 
72
 
        if (!g_file_test (ETC_LOCALTIME, G_FILE_TEST_IS_SYMLINK))
73
 
                return NULL;
74
 
 
75
 
        file = g_file_read_link (ETC_LOCALTIME, NULL);
76
 
        tz = system_timezone_strip_path_if_valid (file);
77
 
        g_free (file);
78
 
 
79
 
        return tz;
80
 
}
81
 
 
82
 
static gchar *
83
 
system_timezone_read_etc_timezone (void)
84
 
{
85
 
        FILE    *etc_timezone;
86
 
        GString *reading;
87
 
        gint      c;
88
 
 
89
 
        etc_timezone = g_fopen (ETC_TIMEZONE, "r");
90
 
        if (!etc_timezone)
91
 
                return NULL;
92
 
 
93
 
        reading = g_string_new ("");
94
 
 
95
 
        c = fgetc (etc_timezone);
96
 
        /* only get the first line, we'll validate the value later */
97
 
        while (c != EOF && !g_ascii_isspace (c)) {
98
 
                reading = g_string_append_c (reading, c);
99
 
                c = fgetc (etc_timezone);
100
 
        }
101
 
 
102
 
        fclose (etc_timezone);
103
 
 
104
 
        if (reading->str && reading->str[0] != '\0')
105
 
                return g_string_free (reading, FALSE);
106
 
        else
107
 
                g_string_free (reading, TRUE);
108
 
 
109
 
        return NULL;
110
 
}
111
 
 
112
 
/* Read a file that looks like a key-file (but there's no need for groups)
113
 
 * and get the last value for a specific key */
114
 
static gchar *
115
 
system_timezone_read_key_file (const gchar *filename,
116
 
                               const gchar *key)
117
 
{
118
 
        GIOChannel *channel;
119
 
        gchar       *key_eq;
120
 
        gchar       *line;
121
 
        gchar       *retval;
122
 
 
123
 
        if (!g_file_test (filename, G_FILE_TEST_IS_REGULAR))
124
 
                return NULL;
125
 
 
126
 
        channel = g_io_channel_new_file (filename, "r", NULL);
127
 
        if (!channel)
128
 
                return NULL;
129
 
 
130
 
        key_eq = g_strdup_printf ("%s=", key);
131
 
        retval = NULL;
132
 
 
133
 
        while (g_io_channel_read_line (channel, &line, NULL,
134
 
                                       NULL, NULL) == G_IO_STATUS_NORMAL) {
135
 
                if (g_str_has_prefix (line, key_eq)) {
136
 
                        gchar *value;
137
 
                        gint   len;
138
 
 
139
 
                        value = line + strlen (key_eq);
140
 
                        g_strstrip (value);
141
 
 
142
 
                        len = strlen (value);
143
 
 
144
 
                        if (value[0] == '\"') {
145
 
                                if (value[len - 1] == '\"') {
146
 
                                        if (retval)
147
 
                                                g_free (retval);
148
 
 
149
 
                                        retval = g_strndup (value + 1,
150
 
                                                            len - 2);
151
 
                                }
152
 
                        } else {
153
 
                                if (retval)
154
 
                                        g_free (retval);
155
 
 
156
 
                                retval = g_strdup (line + strlen (key_eq));
157
 
                        }
158
 
 
159
 
                        g_strstrip (retval);
160
 
                }
161
 
 
162
 
                g_free (line);
163
 
        }
164
 
 
165
 
        g_free (key_eq);
166
 
        g_io_channel_unref (channel);
167
 
 
168
 
        return retval;
169
 
}
170
 
 
171
 
/* This works for Fedora and Mandriva */
172
 
static gchar *
173
 
system_timezone_read_etc_sysconfig_clock (void)
174
 
{
175
 
        return system_timezone_read_key_file (ETC_SYSCONFIG_CLOCK,
176
 
                                              "ZONE");
177
 
}
178
 
 
179
 
/* This works for openSUSE */
180
 
static gchar *
181
 
system_timezone_read_etc_sysconfig_clock_alt (void)
182
 
{
183
 
        return system_timezone_read_key_file (ETC_SYSCONFIG_CLOCK,
184
 
                                              "TIMEZONE");
185
 
}
186
 
 
187
 
/* This works for Solaris/OpenSolaris */
188
 
static gchar *
189
 
system_timezone_read_etc_TIMEZONE (void)
190
 
{
191
 
        return system_timezone_read_key_file (ETC_TIMEZONE_MAJ,
192
 
                                              "TZ");
193
 
}
194
 
 
195
 
/* This works for Arch Linux */
196
 
static gchar *
197
 
system_timezone_read_etc_rc_conf (void)
198
 
{
199
 
        return system_timezone_read_key_file (ETC_RC_CONF,
200
 
                                              "TIMEZONE");
201
 
}
202
 
 
203
 
/* This works for old Gentoo */
204
 
static gchar *
205
 
system_timezone_read_etc_conf_d_clock (void)
206
 
{
207
 
        return system_timezone_read_key_file (ETC_CONF_D_CLOCK,
208
 
                                              "TIMEZONE");
209
 
}
210
 
 
211
 
typedef gboolean (*CompareFiles) (struct stat *a_stat,
212
 
                                  struct stat *b_stat,
213
 
                                  const gchar  *a_content,
214
 
                                  gsize a_content_len,
215
 
                                  const gchar  *b_filename);
216
 
 
217
 
static gchar *
218
 
recursive_compare (struct stat  *localtime_stat,
219
 
                   const gchar   *localtime_content,
220
 
                   gsize         localtime_content_len,
221
 
                   const gchar  *file,
222
 
                   CompareFiles  compare_func)
223
 
{
224
 
        struct stat file_stat;
225
 
 
226
 
        if (g_stat (file, &file_stat) != 0)
227
 
                return NULL;
228
 
 
229
 
        if (S_ISREG (file_stat.st_mode)) {
230
 
                if (compare_func (localtime_stat,
231
 
                                  &file_stat,
232
 
                                  localtime_content,
233
 
                                  localtime_content_len,
234
 
                                  file))
235
 
                        return system_timezone_strip_path_if_valid (file);
236
 
                else
237
 
                        return NULL;
238
 
        } else if (S_ISDIR (file_stat.st_mode)) {
239
 
                GDir       *dir = NULL;
240
 
                gchar       *ret = NULL;
241
 
                const gchar *subfile = NULL;
242
 
                gchar       *subpath = NULL;
243
 
 
244
 
                dir = g_dir_open (file, 0, NULL);
245
 
                if (dir == NULL)
246
 
                        return NULL;
247
 
 
248
 
                while ((subfile = g_dir_read_name (dir)) != NULL) {
249
 
                        subpath = g_build_filename (file, subfile, NULL);
250
 
 
251
 
                        ret = recursive_compare (localtime_stat,
252
 
                                                 localtime_content,
253
 
                                                 localtime_content_len,
254
 
                                                 subpath,
255
 
                                                 compare_func);
256
 
 
257
 
                        g_free (subpath);
258
 
 
259
 
                        if (ret != NULL)
260
 
                                break;
261
 
                }
262
 
 
263
 
                g_dir_close (dir);
264
 
 
265
 
                return ret;
266
 
        }
267
 
 
268
 
        return NULL;
269
 
}
270
 
 
271
 
static gboolean
272
 
files_are_identical_inode (struct stat *a_stat,
273
 
                           struct stat *b_stat,
274
 
                           const gchar  *a_content,
275
 
                           gsize        a_content_len,
276
 
                           const gchar  *b_filename)
277
 
{
278
 
        gboolean res = a_stat->st_ino == b_stat->st_ino;
279
 
 
280
 
        if (res) {
281
 
                const gchar *filename;
282
 
 
283
 
                filename = strrchr (b_filename, '/');
284
 
                if (filename)
285
 
                        filename++;
286
 
                else
287
 
                        filename = b_filename;
288
 
 
289
 
                /* There is a 'localtime' soft link to /etc/localtime in the zoneinfo
290
 
                   directory on Slackware, thus rather skip this file. */
291
 
                res = !g_str_equal (filename, "localtime");
292
 
        }
293
 
 
294
 
        return res;
295
 
}
296
 
 
297
 
/* Determine if /etc/localtime is a hard link to some file, by looking at
298
 
 * the inodes */
299
 
static gchar *
300
 
system_timezone_read_etc_localtime_hardlink (void)
301
 
{
302
 
        struct stat stat_localtime;
303
 
 
304
 
        if (g_stat (ETC_LOCALTIME, &stat_localtime) != 0)
305
 
                return NULL;
306
 
 
307
 
        if (!S_ISREG (stat_localtime.st_mode))
308
 
                return NULL;
309
 
 
310
 
        return recursive_compare (&stat_localtime,
311
 
                                  NULL,
312
 
                                  0,
313
 
                                  SYSTEM_ZONEINFODIR,
314
 
                                  files_are_identical_inode);
315
 
}
316
 
 
317
 
static gboolean
318
 
files_are_identical_content (struct stat *a_stat,
319
 
                             struct stat *b_stat,
320
 
                             const gchar  *a_content,
321
 
                             gsize        a_content_len,
322
 
                             const gchar  *b_filename)
323
 
{
324
 
        gchar  *b_content = NULL;
325
 
        gsize  b_content_len = -1;
326
 
        gint    cmp;
327
 
 
328
 
        if (a_stat->st_size != b_stat->st_size)
329
 
                return FALSE;
330
 
 
331
 
        if (!g_file_get_contents (b_filename,
332
 
                                 &b_content, &b_content_len, NULL))
333
 
                return FALSE;
334
 
 
335
 
        if (a_content_len != b_content_len) {
336
 
                g_free (b_content);
337
 
                return FALSE;
338
 
        }
339
 
 
340
 
        cmp = memcmp (a_content, b_content, a_content_len);
341
 
        g_free (b_content);
342
 
 
343
 
        return (cmp == 0);
344
 
}
345
 
 
346
 
/* Determine if /etc/localtime is a copy of a timezone file */
347
 
static gchar *
348
 
system_timezone_read_etc_localtime_content (void)
349
 
{
350
 
        struct stat  stat_localtime;
351
 
        gchar   *localtime_content = NULL;
352
 
        gsize   localtime_content_len = -1;
353
 
        gchar   *retval;
354
 
 
355
 
        if (g_stat (ETC_LOCALTIME, &stat_localtime) != 0)
356
 
                return NULL;
357
 
 
358
 
        if (!S_ISREG (stat_localtime.st_mode))
359
 
                return NULL;
360
 
 
361
 
        if (!g_file_get_contents (ETC_LOCALTIME,
362
 
                                  &localtime_content,
363
 
                                  &localtime_content_len,
364
 
                                  NULL))
365
 
                return NULL;
366
 
 
367
 
        retval = recursive_compare (&stat_localtime,
368
 
                                   localtime_content,
369
 
                                   localtime_content_len,
370
 
                                   SYSTEM_ZONEINFODIR,
371
 
                                   files_are_identical_content);
372
 
 
373
 
        g_free (localtime_content);
374
 
 
375
 
        return retval;
376
 
}
377
 
 
378
 
typedef gchar * (*GetSystemTimezone) (void);
379
 
/* The order of the functions here define the priority of the methods used
380
 
 * to find the timezone. First method has higher priority. */
381
 
static GetSystemTimezone get_system_timezone_methods[] = {
382
 
        /* cheap and "more correct" than data from a config file */
383
 
        system_timezone_read_etc_localtime_softlink,
384
 
        /* reading various config files */
385
 
        system_timezone_read_etc_timezone,
386
 
        system_timezone_read_etc_sysconfig_clock,
387
 
        system_timezone_read_etc_sysconfig_clock_alt,
388
 
        system_timezone_read_etc_TIMEZONE,
389
 
        system_timezone_read_etc_rc_conf,
390
 
        /* reading deprecated config files */
391
 
        system_timezone_read_etc_conf_d_clock,
392
 
        /* reading /etc/timezone directly. Expensive since we have to stat
393
 
         * many files */
394
 
        system_timezone_read_etc_localtime_hardlink,
395
 
        system_timezone_read_etc_localtime_content,
396
 
        NULL
397
 
};
398
 
 
399
 
static gboolean
400
 
system_timezone_is_valid (const gchar *tz)
401
 
{
402
 
        const gchar *c;
403
 
 
404
 
        if (!tz)
405
 
                return FALSE;
406
 
 
407
 
        for (c = tz; *c != '\0'; c++) {
408
 
                if (!(g_ascii_isalnum (*c) ||
409
 
                    *c == '/' || *c == '-' || *c == '_'))
410
 
                        return FALSE;
411
 
        }
412
 
 
413
 
        return TRUE;
414
 
}
415
 
 
416
 
static gchar *
417
 
system_timezone_find (void)
418
 
{
419
 
        gchar *tz;
420
 
        gint   i;
421
 
 
422
 
        for (i = 0; get_system_timezone_methods[i] != NULL; i++) {
423
 
                tz = get_system_timezone_methods[i] ();
424
 
 
425
 
                if (system_timezone_is_valid (tz))
426
 
                        return tz;
427
 
 
428
 
                g_free (tz);
429
 
        }
430
 
 
431
 
        return NULL;
432
 
}
433
 
 
434
 
#else /* G_OS_WIN32 */
435
 
#include <windows.h>
436
 
 
437
 
struct timezone_map_entry
438
 
{
439
 
        const gchar *windows_string;
440
 
        const gchar *olson_string;
441
 
};
442
 
 
443
 
static gchar *
444
 
windows_timezone_string_to_olson(const gchar * windows_tz)
445
 
{
446
 
        /* source: http://www.chronos-st.org/Windows-to-Olson.txt */
447
 
        static const struct timezone_map_entry timezone_map[] = {
448
 
                { "Afghanistan", "Asia/Kabul" },
449
 
                { "Afghanistan Standard Time", "Asia/Kabul" },
450
 
                { "Alaskan", "America/Anchorage" },
451
 
                { "Alaskan Standard Time", "America/Anchorage" },
452
 
                { "Arab", "Asia/Riyadh" },
453
 
                { "Arab Standard Time", "Asia/Riyadh" },
454
 
                { "Arabian", "Asia/Muscat" },
455
 
                { "Arabian Standard Time", "Asia/Muscat" },
456
 
                { "Arabic Standard Time", "Asia/Baghdad" },
457
 
                { "Atlantic", "America/Halifax" },
458
 
                { "Atlantic Standard Time", "America/Halifax" },
459
 
                { "AUS Central", "Australia/Darwin" },
460
 
                { "AUS Central Standard Time", "Australia/Darwin" },
461
 
                { "AUS Eastern", "Australia/Sydney" },
462
 
                { "AUS Eastern Standard Time", "Australia/Sydney" },
463
 
                { "Azerbaijan Standard Time", "Asia/Baku" },
464
 
                { "Azores", "Atlantic/Azores" },
465
 
                { "Azores Standard Time", "Atlantic/Azores" },
466
 
                { "Bangkok", "Asia/Bangkok" },
467
 
                { "Bangkok Standard Time", "Asia/Bangkok" },
468
 
                { "Beijing", "Asia/Shanghai" },
469
 
                { "Canada Central", "America/Regina" },
470
 
                { "Canada Central Standard Time", "America/Regina" },
471
 
                { "Cape Verde Standard Time", "Atlantic/Cape_Verde" },
472
 
                { "Caucasus", "Asia/Yerevan" },
473
 
                { "Caucasus Standard Time", "Asia/Yerevan" },
474
 
                { "Cen. Australia", "Australia/Adelaide" },
475
 
                { "Cen. Australia Standard Time", "Australia/Adelaide" },
476
 
                { "Central", "America/Chicago" },
477
 
                { "Central America Standard Time", "America/Regina" },
478
 
                { "Central Asia", "Asia/Dhaka" },
479
 
                { "Central Asia Standard Time", "Asia/Dhaka" },
480
 
                { "Central Brazilian Standard Time", "America/Manaus" },
481
 
                { "Central Europe", "Europe/Prague" },
482
 
                { "Central Europe Standard Time", "Europe/Prague" },
483
 
                { "Central European", "Europe/Belgrade" },
484
 
                { "Central European Standard Time", "Europe/Belgrade" },
485
 
                { "Central Pacific", "Pacific/Guadalcanal" },
486
 
                { "Central Pacific Standard Time", "Pacific/Guadalcanal" },
487
 
                { "Central Standard Time", "America/Chicago" },
488
 
                { "Central Standard Time (Mexico)", "America/Mexico_City" },
489
 
                { "China", "Asia/Shanghai" },
490
 
                { "China Standard Time", "Asia/Shanghai" },
491
 
                { "Dateline", "GMT-1200" },
492
 
                { "Dateline Standard Time", "GMT-1200" },
493
 
                { "E. Africa", "Africa/Nairobi" },
494
 
                { "E. Africa Standard Time", "Africa/Nairobi" },
495
 
                { "E. Australia", "Australia/Brisbane" },
496
 
                { "E. Australia Standard Time", "Australia/Brisbane" },
497
 
                { "E. Europe", "Europe/Minsk" },
498
 
                { "E. Europe Standard Time", "Europe/Minsk" },
499
 
                { "E. South America", "America/Sao_Paulo" },
500
 
                { "E. South America Standard Time", "America/Sao_Paulo" },
501
 
                { "Eastern", "America/New_York" },
502
 
                { "Eastern Standard Time", "America/New_York" },
503
 
                { "Egypt", "Africa/Cairo" },
504
 
                { "Egypt Standard Time", "Africa/Cairo" },
505
 
                { "Ekaterinburg", "Asia/Yekaterinburg" },
506
 
                { "Ekaterinburg Standard Time", "Asia/Yekaterinburg" },
507
 
                { "Fiji", "Pacific/Fiji" },
508
 
                { "Fiji Standard Time", "Pacific/Fiji" },
509
 
                { "FLE", "Europe/Helsinki" },
510
 
                { "FLE Standard Time", "Europe/Helsinki" },
511
 
                { "Georgian Standard Time", "Asia/Tbilisi" },
512
 
                { "GFT", "Europe/Athens" },
513
 
                { "GFT Standard Time", "Europe/Athens" },
514
 
                { "GMT", "Europe/London" },
515
 
                { "GMT Standard Time", "Europe/London" },
516
 
                { "GMT Standard Time", "GMT" },
517
 
                { "Greenland Standard Time", "America/Godthab" },
518
 
                { "Greenwich", "GMT" },
519
 
                { "Greenwich Standard Time", "GMT" },
520
 
                { "GTB", "Europe/Athens" },
521
 
                { "GTB Standard Time", "Europe/Athens" },
522
 
                { "Hawaiian", "Pacific/Honolulu" },
523
 
                { "Hawaiian Standard Time", "Pacific/Honolulu" },
524
 
                { "India", "Asia/Calcutta" },
525
 
                { "India Standard Time", "Asia/Calcutta" },
526
 
                { "Iran", "Asia/Tehran" },
527
 
                { "Iran Standard Time", "Asia/Tehran" },
528
 
                { "Israel", "Asia/Jerusalem" },
529
 
                { "Israel Standard Time", "Asia/Jerusalem" },
530
 
                { "Jordan Standard Time", "Asia/Amman" },
531
 
                { "Korea", "Asia/Seoul" },
532
 
                { "Korea Standard Time", "Asia/Seoul" },
533
 
                { "Mexico", "America/Mexico_City" },
534
 
                { "Mexico Standard Time", "America/Mexico_City" },
535
 
                { "Mexico Standard Time 2", "America/Chihuahua" },
536
 
                { "Mid-Atlantic", "Atlantic/South_Georgia" },
537
 
                { "Mid-Atlantic Standard Time", "Atlantic/South_Georgia" },
538
 
                { "Middle East Standard Time", "Asia/Beirut" },
539
 
                { "Mountain", "America/Denver" },
540
 
                { "Mountain Standard Time", "America/Denver" },
541
 
                { "Mountain Standard Time (Mexico)", "America/Chihuahua" },
542
 
                { "Myanmar Standard Time", "Asia/Rangoon" },
543
 
                { "N. Central Asia Standard Time", "Asia/Novosibirsk" },
544
 
                { "Namibia Standard Time", "Africa/Windhoek" },
545
 
                { "Nepal Standard Time", "Asia/Katmandu" },
546
 
                { "New Zealand", "Pacific/Auckland" },
547
 
                { "New Zealand Standard Time", "Pacific/Auckland" },
548
 
                { "Newfoundland", "America/St_Johns" },
549
 
                { "Newfoundland Standard Time", "America/St_Johns" },
550
 
                { "North Asia East Standard Time", "Asia/Ulaanbaatar" },
551
 
                { "North Asia Standard Time", "Asia/Krasnoyarsk" },
552
 
                { "Pacific", "America/Los_Angeles" },
553
 
                { "Pacific SA", "America/Santiago" },
554
 
                { "Pacific SA Standard Time", "America/Santiago" },
555
 
                { "Pacific Standard Time", "America/Los_Angeles" },
556
 
                { "Pacific Standard Time (Mexico)", "America/Tijuana" },
557
 
                { "Prague Bratislava", "Europe/Prague" },
558
 
                { "Romance", "Europe/Paris" },
559
 
                { "Romance Standard Time", "Europe/Paris" },
560
 
                { "Russian", "Europe/Moscow" },
561
 
                { "Russian Standard Time", "Europe/Moscow" },
562
 
                { "SA Eastern", "America/Buenos_Aires" },
563
 
                { "SA Eastern Standard Time", "America/Buenos_Aires" },
564
 
                { "SA Pacific", "America/Bogota" },
565
 
                { "SA Pacific Standard Time", "America/Bogota" },
566
 
                { "SA Western", "America/Caracas" },
567
 
                { "SA Western Standard Time", "America/Caracas" },
568
 
                { "Samoa", "Pacific/Apia" },
569
 
                { "Samoa Standard Time", "Pacific/Apia" },
570
 
                { "Saudi Arabia", "Asia/Riyadh" },
571
 
                { "Saudi Arabia Standard Time", "Asia/Riyadh" },
572
 
                { "SE Asia", "Asia/Bangkok" },
573
 
                { "SE Asia Standard Time", "Asia/Bangkok" },
574
 
                { "Singapore", "Asia/Singapore" },
575
 
                { "Singapore Standard Time", "Asia/Singapore" },
576
 
                { "South Africa", "Africa/Harare" },
577
 
                { "South Africa Standard Time", "Africa/Harare" },
578
 
                { "Sri Lanka", "Asia/Colombo" },
579
 
                { "Sri Lanka Standard Time", "Asia/Colombo" },
580
 
                { "Sydney Standard Time", "Australia/Sydney" },
581
 
                { "Taipei", "Asia/Taipei" },
582
 
                { "Taipei Standard Time", "Asia/Taipei" },
583
 
                { "Tasmania", "Australia/Hobart" },
584
 
                { "Tasmania Standard Time", "Australia/Hobart" },
585
 
                { "Tasmania Standard Time", "Australia/Hobart" },
586
 
                { "Tokyo", "Asia/Tokyo" },
587
 
                { "Tokyo Standard Time", "Asia/Tokyo" },
588
 
                { "Tonga Standard Time", "Pacific/Tongatapu" },
589
 
                { "US Eastern", "America/Indianapolis" },
590
 
                { "US Eastern Standard Time", "America/Indianapolis" },
591
 
                { "US Mountain", "America/Phoenix" },
592
 
                { "US Mountain Standard Time", "America/Phoenix" },
593
 
                { "Vladivostok", "Asia/Vladivostok" },
594
 
                { "Vladivostok Standard Time", "Asia/Vladivostok" },
595
 
                { "W. Australia", "Australia/Perth" },
596
 
                { "W. Australia Standard Time", "Australia/Perth" },
597
 
                { "W. Central Africa Standard Time", "Africa/Luanda" },
598
 
                { "W. Europe", "Europe/Berlin" },
599
 
                { "W. Europe Standard Time", "Europe/Berlin" },
600
 
                { "Warsaw", "Europe/Warsaw" },
601
 
                { "West Asia", "Asia/Karachi" },
602
 
                { "West Asia Standard Time", "Asia/Karachi" },
603
 
                { "West Pacific", "Pacific/Guam" },
604
 
                { "West Pacific Standard Time", "Pacific/Guam" },
605
 
                { "Western Brazilian Standard Time", "America/Rio_Branco" },
606
 
                { "Yakutsk", "Asia/Yakutsk" },
607
 
                { "Yakutsk Standard Time", "Asia/Yakutsk" },
608
 
                { 0, 0 } // end marker
609
 
        };
610
 
 
611
 
        gint i;
612
 
 
613
 
        for (i=0; timezone_map[i].windows_string && windows_tz; i++) {
614
 
                gint res = strcmp( timezone_map[i].windows_string, windows_tz);
615
 
                if (res > 0)
616
 
                        return NULL;
617
 
                if (res == 0) {
618
 
                        return g_strdup(timezone_map[i].olson_string);
619
 
                }
620
 
        }
621
 
 
622
 
        return NULL;
623
 
}
624
 
 
625
 
#define MAX_VALUE_NAME 4096
626
 
 
627
 
static gchar *
628
 
system_timezone_win32_query_registry (void)
629
 
{
630
 
        DWORD type;
631
 
        DWORD size;
632
 
        LONG res;
633
 
        DWORD i;
634
 
 
635
 
        static HKEY reg_key = (HKEY) INVALID_HANDLE_VALUE;
636
 
        static HKEY reg_subkey = (HKEY) INVALID_HANDLE_VALUE;
637
 
        gchar timeZone[MAX_VALUE_NAME] = "";
638
 
        gchar timeZoneStd[MAX_VALUE_NAME] = "";
639
 
        gchar subKey[MAX_VALUE_NAME] = "";
640
 
 
641
 
        res = RegOpenKeyExA (HKEY_LOCAL_MACHINE,
642
 
                "SYSTEM\\CurrentControlSet\\Control\\TimeZoneInformation", 0, KEY_READ, &reg_key);
643
 
        if (res != ERROR_SUCCESS) {
644
 
                g_debug("Could not find system timezone! (1)\n");
645
 
                return NULL;
646
 
        }
647
 
 
648
 
        /* On Windows Vista, Windows Server 2008 and later, the Windows timezone name is the value of 'TimeZoneKeyName' */
649
 
 
650
 
        size = MAX_VALUE_NAME;
651
 
        res = RegQueryValueExA (reg_key, "TimeZoneKeyName", 0, &type, (LPBYTE) timeZone, &size);
652
 
 
653
 
        if (type == REG_SZ && res == ERROR_SUCCESS) {
654
 
                RegCloseKey (reg_key);
655
 
                g_debug ("Windows Timezone String (1): %s\n", timeZone);
656
 
                return g_strdup (timeZone);
657
 
        }
658
 
 
659
 
        /* On older Windows, we must first find the value of 'StandardName' */
660
 
 
661
 
        res = RegQueryValueExA (reg_key, "StandardName", 0, &type, (LPBYTE) timeZone, &size);
662
 
 
663
 
        if (type != REG_SZ || res != ERROR_SUCCESS) {
664
 
                RegCloseKey (reg_key);
665
 
                g_debug ("Could not find system timezone! (2)\n");
666
 
                return NULL;
667
 
        }
668
 
 
669
 
        RegCloseKey (reg_key);
670
 
 
671
 
        /* Windows NT and its family */
672
 
        res = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
673
 
                "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones",
674
 
                0, KEY_READ, &reg_key);
675
 
        if (res != ERROR_SUCCESS) {
676
 
                g_debug ("Could not find the timezone! (3)\n");
677
 
                return NULL;
678
 
        }
679
 
 
680
 
        for (i=0, res = ERROR_SUCCESS; res != ERROR_NO_MORE_ITEMS; i++) {
681
 
                size = MAX_VALUE_NAME;
682
 
                res = RegEnumKeyEx (reg_key, i, subKey, &size, NULL, NULL, NULL, NULL);
683
 
                if (res == ERROR_SUCCESS) {
684
 
                        res = RegOpenKeyExA (reg_key, subKey, 0, KEY_READ, &reg_subkey);
685
 
                        if (res != ERROR_SUCCESS)
686
 
                                continue;
687
 
                        size = MAX_VALUE_NAME;
688
 
                        res = RegQueryValueExA (reg_subkey, "Std", 0, &type,
689
 
                                (LPBYTE) timeZoneStd, &size);
690
 
                        RegCloseKey (reg_subkey);
691
 
                        if (type != REG_SZ || res != ERROR_SUCCESS) {
692
 
                                continue;
693
 
                        }
694
 
                        if (g_strcmp0 (timeZone,timeZoneStd) == 0) {
695
 
                                RegCloseKey (reg_key);
696
 
                                g_debug ("Windows Timezone String (2): %s\n", subKey);
697
 
                                return g_strdup (subKey);
698
 
                        }
699
 
                }
700
 
        }
701
 
 
702
 
        g_debug ("Could not find system timezone! (3)\n");
703
 
        RegCloseKey (reg_key);
704
 
        return NULL;
705
 
}
706
 
 
707
 
#endif /* G_OS_WIN32 */
708
 
 
709
 
/**
710
 
 * e_cal_system_timezone_get_location:
711
 
 *
712
 
 * Returns system timezone location string, NULL on an error.
713
 
 * Returned pointer should be freed with g_free().
714
 
 *
715
 
 * Since: 2.28
716
 
 **/
717
 
gchar *
718
 
e_cal_system_timezone_get_location (void)
719
 
{
720
 
#ifndef G_OS_WIN32
721
 
        return system_timezone_find ();
722
 
#else
723
 
        gchar *windows_timezone_string = NULL;
724
 
        gchar *olson_timezone_string = NULL;
725
 
 
726
 
        if (!(windows_timezone_string = system_timezone_win32_query_registry ()))
727
 
                return NULL;
728
 
        olson_timezone_string = windows_timezone_string_to_olson (windows_timezone_string);
729
 
        g_free (windows_timezone_string);
730
 
        if (!olson_timezone_string)
731
 
                return NULL;
732
 
        g_debug("Olson Timezone String: %s\n", olson_timezone_string);
733
 
        return olson_timezone_string;
734
 
#endif
735
 
}