| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: (C) 2020 Red Hat (www.redhat.com) |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "evolution-ews-config.h" |
| 7 |
|
| 8 |
#include <libxml/parser.h> |
| 9 |
#include <libxml/tree.h> |
| 10 |
#include <libxml/xpath.h> |
| 11 |
#include <libxml/xpathInternals.h> |
| 12 |
|
| 13 |
#include <gio/gio.h> |
| 14 |
|
| 15 |
#include "e-ews-common-utils.h" |
| 16 |
|
| 17 |
#include "e-m365-tz-utils.h" |
| 18 |
|
| 19 |
/* |
| 20 |
* A bunch of global variables used to map the ICalTimezone to MSDN[0] format. |
| 21 |
* Also, some auxiliar functions to translate from one tz type to another. |
| 22 |
* |
| 23 |
* [0]: http://msdn.microsoft.com/en-us/library/ms912391(v=winembedded.11).aspx |
| 24 |
*/ |
| 25 |
static GRecMutex tz_mutex; |
| 26 |
static GHashTable *ical_to_msdn = NULL; |
| 27 |
static GHashTable *msdn_to_ical = NULL; |
| 28 |
static guint tables_counter = 0; |
| 29 |
|
| 30 |
void |
| 31 |
e_m365_tz_utils_ref_windows_zones (void) |
| 32 |
{ |
| 33 |
const gchar *xpath_eval_exp; |
| 34 |
gchar *filename = NULL; |
| 35 |
xmlDocPtr doc; |
| 36 |
xmlXPathContextPtr xpath_ctxt; |
| 37 |
xmlXPathObjectPtr xpath_obj; |
| 38 |
xmlNodeSetPtr nodes; |
| 39 |
gint i, len; |
| 40 |
|
| 41 |
g_rec_mutex_lock (&tz_mutex); |
| 42 |
if (ical_to_msdn != NULL && msdn_to_ical != NULL) { |
| 43 |
g_hash_table_ref (ical_to_msdn); |
| 44 |
g_hash_table_ref (msdn_to_ical); |
| 45 |
tables_counter++; |
| 46 |
|
| 47 |
g_rec_mutex_unlock (&tz_mutex); |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
filename = g_build_filename (M365_DATADIR, "windowsZones.xml", NULL); |
| 52 |
|
| 53 |
doc = xmlReadFile (filename, NULL, 0); |
| 54 |
|
| 55 |
if (doc == NULL) { |
| 56 |
g_warning (G_STRLOC "Could not map %s file.", filename); |
| 57 |
g_free (filename); |
| 58 |
|
| 59 |
g_rec_mutex_unlock (&tz_mutex); |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
xpath_eval_exp = "/supplementalData/windowsZones/mapTimezones/mapZone"; |
| 64 |
|
| 65 |
xpath_ctxt = xmlXPathNewContext (doc); |
| 66 |
xpath_obj = xmlXPathEvalExpression (BAD_CAST xpath_eval_exp, xpath_ctxt); |
| 67 |
|
| 68 |
if (xpath_obj == NULL) { |
| 69 |
g_warning (G_STRLOC "Unable to evaluate xpath expression \"%s\".", xpath_eval_exp); |
| 70 |
xmlXPathFreeContext (xpath_ctxt); |
| 71 |
xmlFreeDoc (doc); |
| 72 |
g_free (filename); |
| 73 |
|
| 74 |
g_rec_mutex_unlock (&tz_mutex); |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
nodes = xpath_obj->nodesetval; |
| 79 |
len = nodes->nodeNr; |
| 80 |
|
| 81 |
msdn_to_ical = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); |
| 82 |
ical_to_msdn = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); |
| 83 |
tables_counter++; |
| 84 |
|
| 85 |
for (i = 0; i < len; i++) { |
| 86 |
xmlChar *msdn = xmlGetProp (nodes->nodeTab[i], BAD_CAST "other"); |
| 87 |
xmlChar *ical = xmlGetProp (nodes->nodeTab[i], BAD_CAST "type"); |
| 88 |
gchar **tokens; |
| 89 |
gint tokens_len; |
| 90 |
|
| 91 |
tokens = g_strsplit ((gchar *) ical, " ", 0); |
| 92 |
tokens_len = g_strv_length (tokens); |
| 93 |
if (tokens_len == 1) { |
| 94 |
if (!g_hash_table_lookup (msdn_to_ical, msdn)) |
| 95 |
g_hash_table_insert (msdn_to_ical, g_strdup ((gchar *) msdn), g_strdup ((gchar *) ical)); |
| 96 |
|
| 97 |
if (!g_hash_table_lookup (ical_to_msdn, ical)) |
| 98 |
g_hash_table_insert (ical_to_msdn, g_strdup ((gchar *) ical), g_strdup ((gchar *) msdn)); |
| 99 |
} else { |
| 100 |
gint j; |
| 101 |
for (j = 0; j < tokens_len; j++) { |
| 102 |
if (!g_hash_table_lookup (msdn_to_ical, msdn)) |
| 103 |
g_hash_table_insert (msdn_to_ical, g_strdup ((gchar *) msdn), g_strdup (tokens[j])); |
| 104 |
|
| 105 |
if (!g_hash_table_lookup (ical_to_msdn, tokens[j])) |
| 106 |
g_hash_table_insert (ical_to_msdn, g_strdup (tokens[j]), g_strdup ((gchar *) msdn)); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
g_strfreev (tokens); |
| 111 |
xmlFree (ical); |
| 112 |
xmlFree (msdn); |
| 113 |
} |
| 114 |
|
| 115 |
xmlXPathFreeObject (xpath_obj); |
| 116 |
xmlXPathFreeContext (xpath_ctxt); |
| 117 |
xmlFreeDoc (doc); |
| 118 |
g_free (filename); |
| 119 |
|
| 120 |
g_rec_mutex_unlock (&tz_mutex); |
| 121 |
} |
| 122 |
|
| 123 |
void |
| 124 |
e_m365_tz_utils_unref_windows_zones (void) |
| 125 |
{ |
| 126 |
g_rec_mutex_lock (&tz_mutex); |
| 127 |
if (ical_to_msdn != NULL) |
| 128 |
g_hash_table_unref (ical_to_msdn); |
| 129 |
|
| 130 |
if (msdn_to_ical != NULL) |
| 131 |
g_hash_table_unref (msdn_to_ical); |
| 132 |
|
| 133 |
if (tables_counter > 0) { |
| 134 |
tables_counter--; |
| 135 |
|
| 136 |
if (tables_counter == 0) { |
| 137 |
ical_to_msdn = NULL; |
| 138 |
msdn_to_ical = NULL; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
g_rec_mutex_unlock (&tz_mutex); |
| 143 |
} |
| 144 |
|
| 145 |
const gchar * |
| 146 |
e_m365_tz_utils_get_msdn_equivalent (const gchar *ical_tz_location) |
| 147 |
{ |
| 148 |
const gchar *msdn_tz_location = NULL; |
| 149 |
|
| 150 |
if (!ical_tz_location || !*ical_tz_location) |
| 151 |
return NULL; |
| 152 |
|
| 153 |
g_rec_mutex_lock (&tz_mutex); |
| 154 |
if (ical_to_msdn == NULL) { |
| 155 |
g_rec_mutex_unlock (&tz_mutex); |
| 156 |
|
| 157 |
g_warn_if_reached (); |
| 158 |
return NULL; |
| 159 |
} |
| 160 |
|
| 161 |
msdn_tz_location = g_hash_table_lookup (ical_to_msdn, ical_tz_location); |
| 162 |
g_rec_mutex_unlock (&tz_mutex); |
| 163 |
|
| 164 |
return msdn_tz_location; |
| 165 |
} |
| 166 |
|
| 167 |
const gchar * |
| 168 |
e_m365_tz_utils_get_ical_equivalent (const gchar *msdn_tz_location) |
| 169 |
{ |
| 170 |
const gchar *ical_tz_location = NULL; |
| 171 |
|
| 172 |
if (!msdn_tz_location || !*msdn_tz_location) |
| 173 |
return NULL; |
| 174 |
|
| 175 |
g_rec_mutex_lock (&tz_mutex); |
| 176 |
if (msdn_to_ical == NULL) { |
| 177 |
g_rec_mutex_unlock (&tz_mutex); |
| 178 |
|
| 179 |
g_warn_if_reached (); |
| 180 |
return NULL; |
| 181 |
} |
| 182 |
|
| 183 |
ical_tz_location = g_hash_table_lookup (msdn_to_ical, msdn_tz_location); |
| 184 |
g_rec_mutex_unlock (&tz_mutex); |
| 185 |
|
| 186 |
return ical_tz_location; |
| 187 |
} |
| 188 |
|
| 189 |
ICalTimezone * |
| 190 |
e_m365_tz_utils_get_user_timezone (void) |
| 191 |
{ |
| 192 |
gchar *location = NULL; |
| 193 |
ICalTimezone *zone = NULL; |
| 194 |
|
| 195 |
if (e_ews_common_utils_gsettings_schema_exists ("org.gnome.evolution.calendar")) { |
| 196 |
GSettings *settings; |
| 197 |
|
| 198 |
settings = g_settings_new ("org.gnome.evolution.calendar"); |
| 199 |
|
| 200 |
if (g_settings_get_boolean (settings, "use-system-timezone")) |
| 201 |
location = e_cal_util_get_system_timezone_location (); |
| 202 |
else |
| 203 |
location = g_settings_get_string (settings, "timezone"); |
| 204 |
|
| 205 |
g_object_unref (settings); |
| 206 |
} |
| 207 |
|
| 208 |
if (!location) |
| 209 |
location = e_cal_util_get_system_timezone_location (); |
| 210 |
|
| 211 |
if (location) |
| 212 |
zone = i_cal_timezone_get_builtin_timezone (location); |
| 213 |
|
| 214 |
g_free (location); |
| 215 |
|
| 216 |
return zone; |
| 217 |
} |