Search in sources :

Example 71 with MailItem

use of com.zimbra.cs.mailbox.MailItem in project zm-mailbox by Zimbra.

the class IcsFormatter method formatCallback.

@Override
public void formatCallback(UserServletContext context) throws IOException, ServiceException {
    Iterator<? extends MailItem> iterator = null;
    List<CalendarItem> calItems = new ArrayList<CalendarItem>();
    //ZimbraLog.mailbox.info("end = "+new Date(context.getEndTime()));
    try {
        long start = context.getStartTime();
        long end = context.getEndTime();
        boolean hasTimeRange = start != TIME_UNSPECIFIED && end != TIME_UNSPECIFIED;
        iterator = getMailItems(context, start, end, Integer.MAX_VALUE);
        // this is lame
        while (iterator.hasNext()) {
            MailItem item = iterator.next();
            if (item instanceof CalendarItem) {
                CalendarItem calItem = (CalendarItem) item;
                if (hasTimeRange) {
                    Collection<Instance> instances = calItem.expandInstances(start, end, false);
                    if (!instances.isEmpty())
                        calItems.add(calItem);
                } else {
                    calItems.add(calItem);
                }
            }
        }
    } finally {
        if (iterator instanceof QueryResultIterator)
            ((QueryResultIterator) iterator).finished();
    }
    // todo: get from folder name
    String filename = context.itemPath;
    if (mayAttach(context)) {
        if (filename == null || filename.length() == 0)
            filename = "contacts";
        // Let the client specify the filename to save as
        String requestFilename = context.req.getParameter("filename");
        if (requestFilename != null)
            filename = requestFilename;
        else
            // Trim off leading non-word characters (e.g. forward slash)
            filename = filename.replaceAll("^\\W", "");
        if (filename.toLowerCase().endsWith(".ics") == false)
            filename = filename + ".ics";
        String cd = HttpUtil.createContentDisposition(context.req, Part.ATTACHMENT, filename);
        context.resp.addHeader("Content-Disposition", cd);
    }
    Browser browser = HttpUtil.guessBrowser(context.req);
    boolean useOutlookCompatMode = Browser.IE.equals(browser);
    // bug 15549
    boolean needAppleICalHacks = Browser.APPLE_ICAL.equals(browser);
    // Use only htmlFormat when the file isn't supposed to be downloaded (ie. it's supposed to be shown in the browser). Mangles the code so it can be displayed correctly, especially by IE
    boolean htmlFormat = !mayAttach(context) && Browser.IE.equals(browser);
    context.resp.setCharacterEncoding(MimeConstants.P_CHARSET_UTF8);
    String contentType;
    if (htmlFormat) {
        contentType = MimeConstants.CT_TEXT_HTML;
    } else if (mayAttach(context)) {
        contentType = MimeConstants.CT_TEXT_CALENDAR;
    } else {
        contentType = MimeConstants.CT_TEXT_PLAIN;
    }
    context.resp.setContentType(contentType);
    OperationContext octxt = new OperationContext(context.getAuthAccount(), context.isUsingAdminPrivileges());
    FileBufferedWriter fileBufferedWriter = new FileBufferedWriter(context.resp.getWriter(), LC.calendar_ics_export_buffer_size.intValueWithinRange(0, FileBufferedWriter.MAX_BUFFER_SIZE));
    try {
        if (htmlFormat)
            fileBufferedWriter.write("<html><body><pre>");
        context.targetMailbox.writeICalendarForCalendarItems(fileBufferedWriter, octxt, calItems, (context.target != null && context.target instanceof Folder) ? (Folder) context.target : null, useOutlookCompatMode, true, needAppleICalHacks, true, htmlFormat, includeInlineAttaches(context));
        if (htmlFormat)
            fileBufferedWriter.write("</pre></body></html>");
    } finally {
        fileBufferedWriter.finish();
    }
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Instance(com.zimbra.cs.mailbox.CalendarItem.Instance) ArrayList(java.util.ArrayList) FileBufferedWriter(com.zimbra.common.util.FileBufferedWriter) Folder(com.zimbra.cs.mailbox.Folder) CalendarItem(com.zimbra.cs.mailbox.CalendarItem) MailItem(com.zimbra.cs.mailbox.MailItem) Browser(com.zimbra.common.util.HttpUtil.Browser)

Example 72 with MailItem

use of com.zimbra.cs.mailbox.MailItem in project zm-mailbox by Zimbra.

the class ContactFolderFormatter method formatCallback.

@Override
public void formatCallback(UserServletContext context) throws UserServletException, ServiceException, IOException, ServletException {
    if (!(context.target instanceof Folder))
        throw UserServletException.notImplemented("can only handle Folders");
    Folder f = (Folder) context.target;
    if (f.getDefaultView() != MailItem.Type.CONTACT) {
        throw UserServletException.notImplemented("can only handle Contact Folders");
    }
    String v = context.params.get("t");
    Delimiter d = Delimiter.Field;
    if (v != null && v.equals("2"))
        d = Delimiter.Contact;
    v = context.params.get("all");
    boolean allContacts = false;
    if (v != null)
        allContacts = true;
    ItemIdFormatter ifmt = new ItemIdFormatter(context.getAuthAccount(), context.targetAccount, false);
    OutputStream out = new BufferedOutputStream(context.resp.getOutputStream());
    Iterator<? extends MailItem> contacts = null;
    contacts = this.getMailItems(context, 0, 0, 0);
    while (contacts.hasNext()) printContact(contacts.next(), out, ifmt, d);
    if (allContacts) {
        for (Folder folder : context.targetMailbox.getFolderList(context.opContext, SortBy.NONE)) {
            // local contact folders only
            if (folder == context.target || folder.getType() == MailItem.Type.MOUNTPOINT || folder.getDefaultView() != MailItem.Type.CONTACT) {
                continue;
            }
            for (MailItem item : this.getMailItemsFromFolder(context, folder, 0, 0, 0)) {
                printContact(item, out, ifmt, d);
            }
        }
    }
    out.flush();
}
Also used : MailItem(com.zimbra.cs.mailbox.MailItem) ItemIdFormatter(com.zimbra.cs.service.util.ItemIdFormatter) OutputStream(java.io.OutputStream) BufferedOutputStream(java.io.BufferedOutputStream) Folder(com.zimbra.cs.mailbox.Folder) BufferedOutputStream(java.io.BufferedOutputStream)

Example 73 with MailItem

use of com.zimbra.cs.mailbox.MailItem in project zm-mailbox by Zimbra.

the class SoapSession method notifyPendingChanges.

/** Handles the set of changes from a single Mailbox transaction.
     *  <p>
     *  Takes a set of new mailbox changes and caches it locally.  This is
     *  currently initiated from inside the Mailbox transaction commit, but we
     *  still shouldn't assume that execution of this method is synchronized
     *  on the Mailbox.
     *  <p>
     *  *All* changes are currently cached, regardless of the client's state/views.
     * @param pms       A set of new change notifications from our Mailbox.
     * @param changeId  The change ID of the change.
     * @param source    The (optional) Session which initiated these changes. */
@Override
public void notifyPendingChanges(PendingModifications pms, int changeId, Session source) {
    Mailbox mbox = mailbox;
    if (pms == null || mbox == null || !pms.hasNotifications()) {
        return;
    }
    if (source == this) {
        updateLastWrite(mbox);
    } else {
        // keep track of "recent" message count: all present before the session started, plus all received during the session
        if (pms.created != null) {
            for (MailItem item : pms.created.values()) {
                if (item instanceof Message) {
                    boolean isReceived = true;
                    if (item.getFolderId() == Mailbox.ID_FOLDER_SPAM || item.getFolderId() == Mailbox.ID_FOLDER_TRASH) {
                        isReceived = false;
                    } else if ((item.getFlagBitmask() & Mailbox.NON_DELIVERY_FLAGS) != 0) {
                        isReceived = false;
                    } else if (source != null) {
                        isReceived = false;
                    }
                    if (isReceived) {
                        recentMessages++;
                        ZimbraLog.session.debug("incrementing session recent count to %d", recentMessages);
                    }
                }
            }
        }
    }
    handleNotifications(pms, source == this);
}
Also used : MailItem(com.zimbra.cs.mailbox.MailItem) Mailbox(com.zimbra.cs.mailbox.Mailbox) Message(com.zimbra.cs.mailbox.Message)

Example 74 with MailItem

use of com.zimbra.cs.mailbox.MailItem in project zm-mailbox by Zimbra.

the class PendingModifications method recordDeleted.

public void recordDeleted(MailItem itemSnapshot) {
    MailItem.Type type = itemSnapshot.getType();
    changedTypes.add(type);
    delete(new ModificationKey(itemSnapshot), type, itemSnapshot);
}
Also used : MailItem(com.zimbra.cs.mailbox.MailItem) Type(com.zimbra.cs.mailbox.MailItem.Type)

Aggregations

MailItem (com.zimbra.cs.mailbox.MailItem)74 Mailbox (com.zimbra.cs.mailbox.Mailbox)36 ServiceException (com.zimbra.common.service.ServiceException)30 Folder (com.zimbra.cs.mailbox.Folder)23 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)23 Message (com.zimbra.cs.mailbox.Message)19 Mountpoint (com.zimbra.cs.mailbox.Mountpoint)17 ArrayList (java.util.ArrayList)17 IOException (java.io.IOException)16 OperationContext (com.zimbra.cs.mailbox.OperationContext)15 NoSuchItemException (com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException)14 Element (com.zimbra.common.soap.Element)13 CalendarItem (com.zimbra.cs.mailbox.CalendarItem)13 Account (com.zimbra.cs.account.Account)12 Document (com.zimbra.cs.mailbox.Document)12 ItemId (com.zimbra.cs.service.util.ItemId)11 HashMap (java.util.HashMap)11 ZMailbox (com.zimbra.client.ZMailbox)9 Contact (com.zimbra.cs.mailbox.Contact)9 HashSet (java.util.HashSet)9