[evolution-mapi] Bug #633185 - Can't handle Birthdays before 01/01/1970



commit ef6caf30ff043c3563a1127fb6b37bd33e2eefcf
Author: Milan Crha <mcrha redhat com>
Date:   Tue Nov 2 18:02:54 2010 +0100

    Bug #633185 - Can't handle Birthdays before 01/01/1970

 src/addressbook/e-book-backend-mapi-contacts.c |   18 ++-----
 src/addressbook/e-book-backend-mapi.c          |   20 ++-----
 src/camel/camel-mapi-folder.c                  |   12 +---
 src/camel/camel-mapi-utils.c                   |    5 +--
 src/libexchangemapi/exchange-mapi-cal-utils.c  |   67 +++++++++---------------
 src/libexchangemapi/exchange-mapi-mail-utils.c |    7 +--
 src/libexchangemapi/exchange-mapi-utils.c      |   34 ++++++++++++
 src/libexchangemapi/exchange-mapi-utils.h      |    3 +
 8 files changed, 77 insertions(+), 89 deletions(-)
---
diff --git a/src/addressbook/e-book-backend-mapi-contacts.c b/src/addressbook/e-book-backend-mapi-contacts.c
index 65d604d..4248228 100644
--- a/src/addressbook/e-book-backend-mapi-contacts.c
+++ b/src/addressbook/e-book-backend-mapi-contacts.c
@@ -451,19 +451,14 @@ mapi_book_write_props (ExchangeMapiConnection *conn, mapi_id_t fid, TALLOC_CTX *
 	/* BDAY AND ANNV */
 	if (e_contact_get (mcd->contact, E_CONTACT_BIRTH_DATE)) {
 		EContactDate *date = e_contact_get (mcd->contact, E_CONTACT_BIRTH_DATE);
-		struct tm tmtime;
-		time_t lt;
-		NTTIME nt;
+		struct tm tmtime = { 0 };
 		struct FILETIME t;
 
 		tmtime.tm_mday = date->day;
 		tmtime.tm_mon = date->month - 1;
 		tmtime.tm_year = date->year - 1900;
 
-		lt = mktime (&tmtime);
-		unix_to_nt_time (&nt, lt);
-		t.dwLowDateTime = (nt << 32) >> 32;
-		t.dwHighDateTime = (nt >> 32);
+		exchange_mapi_util_time_t_to_filetime (mktime (&tmtime) + (24 * 60 * 60), &t);
 
 		if (!exchange_mapi_utils_add_spropvalue (mem_ctx, values, n_values, PR_BIRTHDAY, &t))
 			return FALSE;
@@ -471,19 +466,14 @@ mapi_book_write_props (ExchangeMapiConnection *conn, mapi_id_t fid, TALLOC_CTX *
 
 	if (e_contact_get (mcd->contact, E_CONTACT_ANNIVERSARY)) {
 		EContactDate *date = e_contact_get (mcd->contact, E_CONTACT_ANNIVERSARY);
-		struct tm tmtime;
-		time_t lt;
-		NTTIME nt;
+		struct tm tmtime = { 0 };
 		struct FILETIME t;
 
 		tmtime.tm_mday = date->day;
 		tmtime.tm_mon = date->month - 1;
 		tmtime.tm_year = date->year - 1900;
 
-		lt = mktime (&tmtime);
-		unix_to_nt_time (&nt, lt);
-		t.dwLowDateTime = (nt << 32) >> 32;
-		t.dwHighDateTime = (nt >> 32);
+		exchange_mapi_util_time_t_to_filetime (mktime (&tmtime) + (24 * 60 * 60), &t);
 
 		if (!exchange_mapi_utils_add_spropvalue (mem_ctx, values, n_values, PR_WEDDING_ANNIVERSARY, &t))
 			return FALSE;
diff --git a/src/addressbook/e-book-backend-mapi.c b/src/addressbook/e-book-backend-mapi.c
index e161ab7..758e745 100644
--- a/src/addressbook/e-book-backend-mapi.c
+++ b/src/addressbook/e-book-backend-mapi.c
@@ -1914,15 +1914,10 @@ mapi_book_utils_contact_from_props (ExchangeMapiConnection *conn, mapi_id_t fid,
 				e_contact_set (contact, mappings[i].field_id, value);
 		} else if (contact_type == ELEMENT_TYPE_SIMPLE) {
 			if (value && mappings[i].element_type == PT_SYSTIME) {
-				struct FILETIME *t = value;
-				time_t time;
-				NTTIME nt;
+				const struct FILETIME *t = value;
 				gchar buff[129];
+				time_t time = exchange_mapi_util_filetime_to_time_t (t);
 
-				nt = t->dwHighDateTime;
-				nt = nt << 32;
-				nt |= t->dwLowDateTime;
-				time = nt_time_to_unix (nt);
 				e_contact_set (contact, mappings[i].field_id, ctime_r (&time, buff));
 			}
 		} else if (contact_type == ELEMENT_TYPE_COMPLEX) {
@@ -1934,18 +1929,15 @@ mapi_book_utils_contact_from_props (ExchangeMapiConnection *conn, mapi_id_t fid,
 				g_list_free (list);
 			} else if (mappings[i].field_id == E_CONTACT_BIRTH_DATE
 				   || mappings[i].field_id == E_CONTACT_ANNIVERSARY) {
-				struct FILETIME *t = value;
+				const struct FILETIME *t = value;
 				time_t time;
-				NTTIME nt;
 				struct tm * tmtime;
 				if (value) {
 					EContactDate date = {0};
-					nt = t->dwHighDateTime;
-					nt = nt << 32;
-					nt |= t->dwLowDateTime;
-					time = nt_time_to_unix (nt);
+
+					time = exchange_mapi_util_filetime_to_time_t (t);
 					tmtime = gmtime (&time);
-					//FIXME: Move to new libmapi api to get string dates.
+
 					date.day = tmtime->tm_mday;
 					date.month = tmtime->tm_mon + 1;
 					date.year = tmtime->tm_year + 1900;
diff --git a/src/camel/camel-mapi-folder.c b/src/camel/camel-mapi-folder.c
index b64ec91..c81a90a 100644
--- a/src/camel/camel-mapi-folder.c
+++ b/src/camel/camel-mapi-folder.c
@@ -158,7 +158,6 @@ fetch_items_summary_cb (FetchItemsCallbackData *item_data, gpointer data)
 	struct timeval item_modification_time = { 0 };
 	struct timeval fi_data_mod_time = { 0 };
 	guint32 j = 0;
-	NTTIME ntdate;
 
 	MailItem *item = g_new0(MailItem , 1);
 
@@ -197,17 +196,12 @@ fetch_items_summary_cb (FetchItemsCallbackData *item_data, gpointer data)
 	/* item->header.from = camel_internet_address_format_address (from_name, from_email); */
 
 	if (delivery_date) {
-		ntdate = delivery_date->dwHighDateTime;
-		ntdate = ntdate << 32;
-		ntdate |= delivery_date->dwLowDateTime;
-		item->header.recieved_time = nt_time_to_unix(ntdate);
+		item->header.recieved_time = exchange_mapi_util_filetime_to_time_t (delivery_date);
 	}
 
 	if (last_modification_time) {
-		ntdate = last_modification_time->dwHighDateTime;
-		ntdate = ntdate << 32;
-		ntdate |= last_modification_time->dwLowDateTime;
-		nttime_to_timeval (&item_modification_time, ntdate);
+		item_modification_time.tv_sec = exchange_mapi_util_filetime_to_time_t (last_modification_time);
+		item_modification_time.tv_usec = 0;
 	}
 
 	fi_data_mod_time.tv_sec = fi_data->last_modification_time.tv_sec;
diff --git a/src/camel/camel-mapi-utils.c b/src/camel/camel-mapi-utils.c
index 5773f41..4d6a2dd 100644
--- a/src/camel/camel-mapi-utils.c
+++ b/src/camel/camel-mapi-utils.c
@@ -477,11 +477,8 @@ camel_mapi_utils_create_item_build_props (ExchangeMapiConnection *conn, mapi_id_
 
 	if (item->header.recieved_time != 0) {
 		struct FILETIME msg_date = { 0 };
-		NTTIME nttime = 0;
-		unix_to_nt_time (&nttime, item->header.recieved_time);
 
-		msg_date.dwHighDateTime = nttime >> 32;
-		msg_date.dwLowDateTime = nttime & 0xFFFFFFFF;
+		exchange_mapi_util_time_t_to_filetime (item->header.recieved_time, &msg_date);
 
 		set_value (PR_MESSAGE_DELIVERY_TIME, &msg_date);
 	}
diff --git a/src/libexchangemapi/exchange-mapi-cal-utils.c b/src/libexchangemapi/exchange-mapi-cal-utils.c
index f8833f5..bec294b 100644
--- a/src/libexchangemapi/exchange-mapi-cal-utils.c
+++ b/src/libexchangemapi/exchange-mapi-cal-utils.c
@@ -1243,7 +1243,7 @@ exchange_mapi_cal_utils_write_props_cb (ExchangeMapiConnection *conn, mapi_id_t
 	struct icaltimetype dtstart, dtend, utc_dtstart, utc_dtend;
 	const icaltimezone *utc_zone;
 	const gchar *dtstart_tz_location, *dtend_tz_location, *text = NULL;
-	struct timeval t;
+	time_t tt;
 
 	g_return_val_if_fail (conn != NULL, FALSE);
 	g_return_val_if_fail (mem_ctx != NULL, FALSE);
@@ -1279,12 +1279,8 @@ exchange_mapi_cal_utils_write_props_cb (ExchangeMapiConnection *conn, mapi_id_t
 
 	#define set_datetime_value(hex, dtval) G_STMT_START {		\
 		struct FILETIME	filetime;				\
-		NTTIME		nttime;					\
-									\
-		nttime = timeval_to_nttime(dtval);				\
 									\
-		filetime.dwLowDateTime = (nttime << 32) >> 32;		\
-		filetime.dwHighDateTime = nttime >> 32;			\
+		exchange_mapi_util_time_t_to_filetime (dtval, &filetime); \
 									\
 		if (!exchange_mapi_utils_add_spropvalue (mem_ctx, values, n_values, hex, &filetime)) \
 			return FALSE;	\
@@ -1292,12 +1288,8 @@ exchange_mapi_cal_utils_write_props_cb (ExchangeMapiConnection *conn, mapi_id_t
 
 	#define set_named_datetime_value(named_id, dtval) G_STMT_START { \
 		struct FILETIME	filetime;				\
-		NTTIME		nttime;					\
-									\
-		nttime = timeval_to_nttime(dtval);				\
 									\
-		filetime.dwLowDateTime = (nttime << 32) >> 32;		\
-		filetime.dwHighDateTime = nttime >> 32;			\
+		exchange_mapi_util_time_t_to_filetime (dtval, &filetime); \
 									\
 		if (!exchange_mapi_utils_add_spropvalue_named_id (conn, fid, mem_ctx, values, n_values, named_id, &filetime)) \
 			return FALSE;	\
@@ -1397,13 +1389,11 @@ exchange_mapi_cal_utils_write_props_cb (ExchangeMapiConnection *conn, mapi_id_t
 		}
 	set_named_value (PidLidReminderSet, &b);
 	set_named_value (PidLidReminderDelta, &flag32);
-	t.tv_sec = icaltime_as_timet (utc_dtstart);
-	t.tv_usec = 0;
-	set_named_datetime_value (PidLidReminderTime, &t);
-	t.tv_sec = icaltime_as_timet (utc_dtstart) - (flag32 * SECS_IN_MINUTE);
-	t.tv_usec = 0;
+	tt = icaltime_as_timet (utc_dtstart);
+	set_named_datetime_value (PidLidReminderTime, tt);
+	tt = icaltime_as_timet (utc_dtstart) - (flag32 * SECS_IN_MINUTE);
 	/* ReminderNextTime: FIXME for recurrence */
-	set_named_datetime_value (PidLidReminderSignalTime, &t);
+	set_named_datetime_value (PidLidReminderSignalTime, tt);
 
 	/* Sensitivity, Private */
 	flag32 = olNormal;	/* default */
@@ -1416,15 +1406,13 @@ exchange_mapi_cal_utils_write_props_cb (ExchangeMapiConnection *conn, mapi_id_t
 	set_value (PR_SENSITIVITY, &flag32);
 	set_named_value (PidLidPrivate, &b);
 
-	t.tv_sec = icaltime_as_timet (utc_dtstart);
-	t.tv_usec = 0;
-	set_named_datetime_value (PidLidCommonStart, &t);
-	set_datetime_value (PR_START_DATE, &t);
+	tt = icaltime_as_timet (utc_dtstart);
+	set_named_datetime_value (PidLidCommonStart, tt);
+	set_datetime_value (PR_START_DATE, tt);
 
-	t.tv_sec = icaltime_as_timet (utc_dtend);
-	t.tv_usec = 0;
-	set_named_datetime_value (PidLidCommonEnd, &t);
-	set_datetime_value (PR_END_DATE, &t);
+	tt = icaltime_as_timet (utc_dtend);
+	set_named_datetime_value (PidLidCommonEnd, tt);
+	set_datetime_value (PR_END_DATE, tt);
 
 	b = 1;
 	set_value (PR_RESPONSE_REQUESTED, &b);
@@ -1471,11 +1459,10 @@ exchange_mapi_cal_utils_write_props_cb (ExchangeMapiConnection *conn, mapi_id_t
 		set_named_value (PidLidAutoFillLocation, &b);
 
 		/* Start */
-		t.tv_sec = icaltime_as_timet (utc_dtstart);
-		t.tv_usec = 0;
-		set_named_datetime_value (PidLidAppointmentStartWhole, &t);
+		tt = icaltime_as_timet (utc_dtstart);
+		set_named_datetime_value (PidLidAppointmentStartWhole, tt);
 		/* FIXME: for recurrence */
-		set_named_datetime_value (PidLidClipStart, &t);
+		set_named_datetime_value (PidLidClipStart, tt);
 
 		/* Start TZ */
 		mapi_tzid = exchange_mapi_cal_tz_util_get_mapi_equivalent ((dtstart_tz_location && *dtstart_tz_location) ? dtstart_tz_location : "UTC");
@@ -1485,11 +1472,10 @@ exchange_mapi_cal_utils_write_props_cb (ExchangeMapiConnection *conn, mapi_id_t
 		}
 
 		/* End */
-		t.tv_sec = icaltime_as_timet (utc_dtend);
-		t.tv_usec = 0;
-		set_named_datetime_value (PidLidAppointmentEndWhole, &t);
+		tt = icaltime_as_timet (utc_dtend);
+		set_named_datetime_value (PidLidAppointmentEndWhole, tt);
 		/* FIXME: for recurrence */
-		set_named_datetime_value (PidLidClipEnd, &t);
+		set_named_datetime_value (PidLidClipEnd, tt);
 
 		/* End TZ */
 		mapi_tzid = exchange_mapi_cal_tz_util_get_mapi_equivalent ((dtend_tz_location && *dtend_tz_location) ? dtend_tz_location : "UTC");
@@ -1730,25 +1716,22 @@ exchange_mapi_cal_utils_write_props_cb (ExchangeMapiConnection *conn, mapi_id_t
 			completed = icalproperty_get_completed (prop);
 
 			completed.hour = completed.minute = completed.second = 0; completed.is_date = completed.is_utc = 1;
-			t.tv_sec = icaltime_as_timet (completed);
-			t.tv_usec = 0;
-			set_named_datetime_value (PidLidTaskDateCompleted, &t);
+			tt = icaltime_as_timet (completed);
+			set_named_datetime_value (PidLidTaskDateCompleted, tt);
 		}
 
 		/* Start */
 		dtstart.hour = dtstart.minute = dtstart.second = 0; dtstart.is_date = dtstart.is_utc = 1;
-		t.tv_sec = icaltime_as_timet (dtstart);
-		t.tv_usec = 0;
+		tt = icaltime_as_timet (dtstart);
 		if (!icaltime_is_null_time (dtstart)) {
-			set_named_datetime_value (PidLidTaskStartDate, &t);
+			set_named_datetime_value (PidLidTaskStartDate, tt);
 		}
 
 		/* Due */
 		dtend.hour = dtend.minute = dtend.second = 0; dtend.is_date = dtend.is_utc = 1;
-		t.tv_sec = icaltime_as_timet (dtend);
-		t.tv_usec = 0;
+		tt = icaltime_as_timet (dtend);
 		if (!icaltime_is_null_time (dtend)) {
-			set_named_datetime_value (PidLidTaskDueDate, &t);
+			set_named_datetime_value (PidLidTaskDueDate, tt);
 		}
 
 		/* FIXME: Evolution does not support recurring tasks */
diff --git a/src/libexchangemapi/exchange-mapi-mail-utils.c b/src/libexchangemapi/exchange-mapi-mail-utils.c
index fb33376..b7fcc4b 100644
--- a/src/libexchangemapi/exchange-mapi-mail-utils.c
+++ b/src/libexchangemapi/exchange-mapi-mail-utils.c
@@ -135,12 +135,7 @@ fetch_props_to_mail_item_cb (FetchItemsCallbackData *item_data, gpointer data)
 	}
 
 	if (delivery_date) {
-		NTTIME ntdate = { 0 };
-
-		ntdate = delivery_date->dwHighDateTime;
-		ntdate = ntdate << 32;
-		ntdate |= delivery_date->dwLowDateTime;
-		item->header.recieved_time = nt_time_to_unix(ntdate);
+		item->header.recieved_time = exchange_mapi_util_filetime_to_time_t (delivery_date);
 	}
 
 	if (flags && (*flags & MSGFLAG_READ) != 0)
diff --git a/src/libexchangemapi/exchange-mapi-utils.c b/src/libexchangemapi/exchange-mapi-utils.c
index 3197fe4..a5a04d7 100644
--- a/src/libexchangemapi/exchange-mapi-utils.c
+++ b/src/libexchangemapi/exchange-mapi-utils.c
@@ -1136,3 +1136,37 @@ exchange_mapi_util_free_binary_r (struct Binary_r *bin)
 	g_free (bin->lpb);
 	g_free (bin);
 }
+
+time_t
+exchange_mapi_util_filetime_to_time_t (const struct FILETIME *filetime)
+{
+	NTTIME nt;
+
+	if (!filetime)
+		return (time_t) -1;
+
+	nt = filetime->dwHighDateTime;
+	nt = nt << 32;
+	nt |= filetime->dwLowDateTime;
+
+	nt /=  10 * 1000 * 1000;
+	nt -= 11644473600LL;
+
+	return (time_t) nt;
+}
+
+void
+exchange_mapi_util_time_t_to_filetime (const time_t tt, struct FILETIME *filetime)
+{
+	NTTIME nt;
+
+	g_return_if_fail (filetime != NULL);
+
+	nt = tt;
+	nt += 11644473600LL;
+	nt *=  10 * 1000 * 1000;
+
+	filetime->dwLowDateTime = nt & 0xFFFFFFFF;
+	nt = nt >> 32;
+	filetime->dwHighDateTime = nt & 0xFFFFFFFF;
+}
diff --git a/src/libexchangemapi/exchange-mapi-utils.h b/src/libexchangemapi/exchange-mapi-utils.h
index 96c26d2..0c5fc7d 100644
--- a/src/libexchangemapi/exchange-mapi-utils.h
+++ b/src/libexchangemapi/exchange-mapi-utils.h
@@ -83,4 +83,7 @@ uint32_t exchange_mapi_utils_push_crc32 (uint32_t crc32, uint8_t *bytes, uint32_
 struct Binary_r *exchange_mapi_util_copy_binary_r (const struct Binary_r *bin);
 void exchange_mapi_util_free_binary_r (struct Binary_r *bin);
 
+time_t exchange_mapi_util_filetime_to_time_t (const struct FILETIME *filetime);
+void exchange_mapi_util_time_t_to_filetime (const time_t tt, struct FILETIME *filetime);
+
 #endif



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]