use of com.zimbra.common.calendar.ICalTimeZone in project zm-mailbox by Zimbra.
the class RecurId method fromXml.
public static RecurId fromXml(Element e, TimeZoneMap tzMap) throws ServiceException {
assert (tzMap != null);
String recurrenceId = e.getAttribute(MailConstants.A_CAL_RECURRENCE_ID, null);
if (recurrenceId == null)
return null;
String tzId = e.getAttribute(MailConstants.A_CAL_TIMEZONE, null);
ICalTimeZone tz = tzId != null ? tzMap.lookupAndAdd(tzId) : null;
String rangeType = e.getAttribute(MailConstants.A_CAL_RECURRENCE_RANGE_TYPE, null);
try {
ParsedDateTime dt = ParsedDateTime.parse(recurrenceId, tzMap, tz, tzMap.getLocalTimeZone());
if (rangeType != null)
return new RecurId(dt, rangeType);
else
return new RecurId(dt, RANGE_NONE);
} catch (ParseException x) {
throw ServiceException.FAILURE("recurId=" + recurrenceId + ", tz=" + tzId, x);
}
}
use of com.zimbra.common.calendar.ICalTimeZone in project zm-mailbox by Zimbra.
the class ContactCSV method populateDateFieldsIfUnambiguous.
/**
*
* @param testY candidate year
* @param testM candidate month in range 1 to 12
* @param testD candidate day in range 1 to 31
* @param leniant succeed even if the day and month might be ambiguous
* @return Formatted date if certain date fields are OK and wouldn't be OK in a different order, else null
*/
private String populateDateFieldsIfUnambiguous(int testY, int testM, int testD, boolean leniant) {
if ((testY < 1600) || (testY > 4500)) {
// Year does not fit within what Outlook allows, probably not what is intended
return null;
}
int firstAllowableDay = leniant ? 1 : 13;
if ((testD < firstAllowableDay) || (testD > 31)) {
// Either an invalid date or could be a month number if guessed date string order wrong
return null;
}
if ((testM < 1) || (testM > 12)) {
// Not a valid month
return null;
}
ICalTimeZone tzUTC = ICalTimeZone.getUTC();
GregorianCalendar cal = new GregorianCalendar(tzUTC);
cal.set(testY, testM - 1, 1, 0, 0);
if (testD > cal.getActualMaximum(Calendar.DAY_OF_MONTH))
return null;
cal.set(Calendar.DAY_OF_MONTH, testD);
SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd");
ymd.setCalendar(cal);
return ymd.format(cal.getTime());
}
use of com.zimbra.common.calendar.ICalTimeZone in project zm-mailbox by Zimbra.
the class CalendarMailSender method createForwardedPrivateInviteMessage.
public static MimeMessage createForwardedPrivateInviteMessage(Account account, Locale lc, String method, List<Invite> invites, String origSenderEmail, String forwarderEmail, String[] forwardTo) throws ServiceException {
if (invites == null || invites.isEmpty())
return null;
List<Address> rcpts = new ArrayList<Address>();
for (String to : forwardTo) {
try {
rcpts.add(new JavaMailInternetAddress(to));
} catch (AddressException e) {
ZimbraLog.calendar.warn("Ignoring invalid address \"" + to + "\" during invite forward");
}
}
if (rcpts.isEmpty())
return null;
String subject = L10nUtil.getMessage(MsgKey.calendarSubjectWithheld, lc);
// Create filtered version of invites.
List<Invite> filteredInvs = new ArrayList<Invite>();
for (Invite inv : invites) {
Invite filtered = inv.newCopy();
filtered.clearAlarms();
filtered.clearPrivateInfo();
filtered.setName(subject);
// Add ATTENDEE for forwarder.
List<ZAttendee> atts = inv.getAttendees();
if (atts != null && forwarderEmail != null) {
for (ZAttendee att : atts) {
if (forwarderEmail.equalsIgnoreCase(att.getAddress())) {
filtered.addAttendee(att);
}
}
}
filteredInvs.add(filtered);
}
MimeMessage mm = null;
try {
mm = new Mime.FixedMimeMessage(JMSession.getSmtpSession(account));
mm.setFrom(new JavaMailInternetAddress(origSenderEmail));
mm.addRecipients(RecipientType.TO, rcpts.toArray(new Address[0]));
// Set special header to indicate the forwarding attendee.
mm.setHeader(CalendarMailSender.X_ZIMBRA_CALENDAR_INTENDED_FOR, forwarderEmail);
mm.setSubject(subject);
StringWriter writer = new StringWriter();
try {
writer.write("BEGIN:VCALENDAR\r\n");
ZProperty prop;
prop = new ZProperty(ICalTok.PRODID, ZCalendar.sZimbraProdID);
prop.toICalendar(writer);
prop = new ZProperty(ICalTok.VERSION, ZCalendar.sIcalVersion);
prop.toICalendar(writer);
prop = new ZProperty(ICalTok.METHOD, method);
prop.toICalendar(writer);
// timezones
Invite firstInv = filteredInvs.get(0);
TimeZoneMap tzmap = new TimeZoneMap(firstInv.getTimeZoneMap().getLocalTimeZone());
for (Invite inv : filteredInvs) {
tzmap.add(inv.getTimeZoneMap());
}
for (Iterator<ICalTimeZone> iter = tzmap.tzIterator(); iter.hasNext(); ) {
ICalTimeZone tz = iter.next();
tz.newToVTimeZone().toICalendar(writer);
}
// VEVENTs/VTODOs
for (Invite inv : filteredInvs) {
ZComponent comp = inv.newToVComponent(false, true);
comp.toICalendar(writer);
}
writer.write("END:VCALENDAR\r\n");
} catch (IOException e) {
throw ServiceException.FAILURE("Error writing iCalendar", e);
} finally {
Closeables.closeQuietly(writer);
}
mm.setText(writer.toString());
ContentType ct = new ContentType(MimeConstants.CT_TEXT_CALENDAR);
ct.setParameter(MimeConstants.P_CHARSET, MimeConstants.P_CHARSET_UTF8);
ct.setParameter("method", method);
mm.setHeader("Content-Type", ct.toString());
} catch (MessagingException e) {
ZimbraLog.calendar.warn("Unable to compose email for invite forwarding", e);
}
return mm;
}
use of com.zimbra.common.calendar.ICalTimeZone in project zm-mailbox by Zimbra.
the class ToXML method encodeTimeZoneMap.
private static void encodeTimeZoneMap(Element parent, TimeZoneMap tzmap) {
assert (tzmap != null);
for (Iterator<ICalTimeZone> iter = tzmap.tzIterator(); iter.hasNext(); ) {
ICalTimeZone tz = iter.next();
Element e = parent.addElement(MailConstants.E_CAL_TZ);
e.addAttribute(MailConstants.A_ID, tz.getID());
e.addAttribute(MailConstants.A_CAL_TZ_STDOFFSET, tz.getStandardOffset() / 60 / 1000);
e.addAttribute(MailConstants.A_CAL_TZ_STDNAME, tz.getStandardTzname());
if (tz.useDaylightTime()) {
SimpleOnset standard = tz.getStandardOnset();
SimpleOnset daylight = tz.getDaylightOnset();
if (standard != null && daylight != null) {
e.addAttribute(MailConstants.A_CAL_TZ_DAYOFFSET, tz.getDaylightOffset() / 60 / 1000);
e.addAttribute(MailConstants.A_CAL_TZ_DAYNAME, tz.getDaylightTzname());
Element std = e.addElement(MailConstants.E_CAL_TZ_STANDARD);
int standardWeek = standard.getWeek();
if (standardWeek != 0) {
std.addAttribute(MailConstants.A_CAL_TZ_WEEK, standardWeek);
std.addAttribute(MailConstants.A_CAL_TZ_DAYOFWEEK, standard.getDayOfWeek());
} else {
std.addAttribute(MailConstants.A_CAL_TZ_DAYOFMONTH, standard.getDayOfMonth());
}
std.addAttribute(MailConstants.A_CAL_TZ_MONTH, standard.getMonth());
std.addAttribute(MailConstants.A_CAL_TZ_HOUR, standard.getHour());
std.addAttribute(MailConstants.A_CAL_TZ_MINUTE, standard.getMinute());
std.addAttribute(MailConstants.A_CAL_TZ_SECOND, standard.getSecond());
Element day = e.addElement(MailConstants.E_CAL_TZ_DAYLIGHT);
int daylightWeek = daylight.getWeek();
if (daylightWeek != 0) {
day.addAttribute(MailConstants.A_CAL_TZ_WEEK, daylightWeek);
day.addAttribute(MailConstants.A_CAL_TZ_DAYOFWEEK, daylight.getDayOfWeek());
} else {
day.addAttribute(MailConstants.A_CAL_TZ_DAYOFMONTH, daylight.getDayOfMonth());
}
day.addAttribute(MailConstants.A_CAL_TZ_MONTH, daylight.getMonth());
day.addAttribute(MailConstants.A_CAL_TZ_HOUR, daylight.getHour());
day.addAttribute(MailConstants.A_CAL_TZ_MINUTE, daylight.getMinute());
day.addAttribute(MailConstants.A_CAL_TZ_SECOND, daylight.getSecond());
}
}
}
}
use of com.zimbra.common.calendar.ICalTimeZone in project zm-mailbox by Zimbra.
the class Mailbox method writeICalendarForCalendarItems.
public void writeICalendarForCalendarItems(Writer writer, OperationContext octxt, Collection<CalendarItem> calItems, Folder f, boolean useOutlookCompatMode, boolean ignoreErrors, boolean needAppleICalHacks, boolean trimCalItemsList, boolean escapeHtmlTags, boolean includeAttaches) throws ServiceException {
lock.lock();
try {
writer.write("BEGIN:VCALENDAR\r\n");
if (f != null) {
writer.write("X-WR-CALNAME:");
writer.write(f.getName());
writer.write("\r\n");
writer.write("X-WR-CALID:");
writer.write(new ItemId(f).toString());
writer.write("\r\n");
}
ZProperty prop;
prop = new ZProperty(ICalTok.PRODID, ZCalendar.sZimbraProdID);
prop.toICalendar(writer, needAppleICalHacks);
prop = new ZProperty(ICalTok.VERSION, ZCalendar.sIcalVersion);
prop.toICalendar(writer, needAppleICalHacks);
prop = new ZProperty(ICalTok.METHOD, ICalTok.PUBLISH.toString());
prop.toICalendar(writer, needAppleICalHacks);
// timezones
ICalTimeZone localTz = Util.getAccountTimeZone(getAccount());
TimeZoneMap tzmap = new TimeZoneMap(localTz);
for (CalendarItem calItem : calItems) {
tzmap.add(calItem.getTimeZoneMap());
}
// iterate the tzmap and add all the VTimeZone's
for (Iterator<ICalTimeZone> iter = tzmap.tzIterator(); iter.hasNext(); ) {
ICalTimeZone tz = iter.next();
tz.newToVTimeZone().toICalendar(writer, needAppleICalHacks);
}
// help keep memory consumption low
tzmap = null;
// build all the event components and add them to the Calendar
for (Iterator<CalendarItem> iter = calItems.iterator(); iter.hasNext(); ) {
CalendarItem calItem = iter.next();
boolean allowPrivateAccess = calItem.isPublic() || calItem.allowPrivateAccess(octxt.getAuthenticatedUser(), octxt.isUsingAdminPrivileges());
if (trimCalItemsList) {
// help keep memory consumption low
iter.remove();
}
Invite[] invites = calItem.getInvites();
if (invites != null && invites.length > 0) {
boolean appleICalExdateHack = LC.calendar_apple_ical_compatible_canceled_instances.booleanValue();
ZComponent[] comps = null;
try {
comps = Invite.toVComponents(invites, allowPrivateAccess, useOutlookCompatMode, appleICalExdateHack, includeAttaches);
} catch (ServiceException e) {
if (ignoreErrors) {
ZimbraLog.calendar.warn("Error retrieving iCalendar data for item %s: %s", calItem.getId(), e.getMessage(), e);
} else {
throw e;
}
}
if (comps != null) {
for (ZComponent comp : comps) {
comp.toICalendar(writer, needAppleICalHacks, escapeHtmlTags);
}
}
}
}
writer.write("END:VCALENDAR\r\n");
} catch (IOException e) {
throw ServiceException.FAILURE("Error writing iCalendar", e);
} finally {
lock.release();
}
}
Aggregations