Search in sources :

Example 1 with FetchRequest

use of i2p.susi.webmail.pop3.POP3MailBox.FetchRequest in project i2p.i2p by i2p.

the class MailCache method getMail.

/**
 * Fetch any needed data from pop3 server.
 * Mail objects are inserted into the requests.
 * After this, call getUIDLs() to get all known mail UIDLs.
 * MUST already be connected, otherwise returns false.
 *
 * Blocking.
 *
 * @param mode HEADER or ALL only
 * @return true if any were fetched
 * @since 0.9.13
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public boolean getMail(FetchMode mode) {
    if (mode == FetchMode.CACHE_ONLY)
        throw new IllegalArgumentException();
    boolean hOnly = mode == FetchMode.HEADER;
    Collection<String> popKnown = mailbox.getUIDLs();
    if (popKnown == null)
        return false;
    List<POP3Request> fetches = new ArrayList<POP3Request>();
    // requests.to send off
    for (String uidl : popKnown) {
        Mail mail = null, newMail = null;
        boolean headerOnly = hOnly;
        /*
			 * synchronize update to hashtable
			 */
        synchronized (mails) {
            mail = mails.get(uidl);
            if (mail == null) {
                newMail = new Mail(uidl);
                mails.put(uidl, newMail);
            }
        }
        if (mail == null) {
            mail = newMail;
            mail.setSize(mailbox.getSize(uidl));
        }
        if (mail.markForDeletion)
            continue;
        long sz = mail.getSize();
        if (sz > 0 && sz <= FETCH_ALL_SIZE)
            headerOnly = false;
        if (headerOnly) {
            if (!mail.hasHeader()) {
                if (disk != null) {
                    if (disk.getMail(mail, true)) {
                        Debug.debug(Debug.DEBUG, "Loaded header from disk cache: " + uidl);
                        // note that disk loaded the full body if it had it
                        if (mail.hasBody() && !Boolean.parseBoolean(Config.getProperty(WebMail.CONFIG_LEAVE_ON_SERVER))) {
                            // we already have it, send delete
                            mailbox.queueForDeletion(mail.uidl);
                        }
                        // found on disk, woo
                        continue;
                    }
                }
                POP3Request pr = new POP3Request(mail, true, new MemoryBuffer(1024));
                fetches.add(pr);
            } else {
                if (mail.hasBody() && !Boolean.parseBoolean(Config.getProperty(WebMail.CONFIG_LEAVE_ON_SERVER))) {
                    // we already have it, send delete
                    mailbox.queueForDeletion(mail.uidl);
                }
            }
        } else {
            if (!mail.hasBody()) {
                if (disk != null) {
                    if (disk.getMail(mail, false)) {
                        Debug.debug(Debug.DEBUG, "Loaded body from disk cache: " + uidl);
                        // note that disk loaded the full body if it had it
                        if (!Boolean.parseBoolean(Config.getProperty(WebMail.CONFIG_LEAVE_ON_SERVER))) {
                            // we already have it, send delete
                            mailbox.queueForDeletion(mail.uidl);
                        }
                        // found on disk, woo
                        continue;
                    }
                }
                File file = new File(_context.getTempDir(), "susimail-new-" + _context.random().nextLong());
                POP3Request pr = new POP3Request(mail, false, new FileBuffer(file));
                fetches.add(pr);
            } else {
                if (!Boolean.parseBoolean(Config.getProperty(WebMail.CONFIG_LEAVE_ON_SERVER))) {
                    // we already have it, send delete
                    mailbox.queueForDeletion(mail.uidl);
                }
            }
        }
    }
    boolean rv = false;
    if (!fetches.isEmpty()) {
        // Send off the fetches
        // gaah compiler
        List foo = fetches;
        List<FetchRequest> bar = foo;
        mailbox.getBodies(bar);
        // Process results
        for (POP3Request pr : fetches) {
            if (pr.getSuccess()) {
                Mail mail = pr.mail;
                if (!mail.hasHeader())
                    mail.setNew(true);
                if (pr.getHeaderOnly()) {
                    mail.setHeader(pr.getBuffer());
                } else {
                    mail.setBody(pr.getBuffer());
                }
                rv = true;
                if (disk != null) {
                    if (disk.saveMail(mail) && mail.hasBody() && !Boolean.parseBoolean(Config.getProperty(WebMail.CONFIG_LEAVE_ON_SERVER))) {
                        mailbox.queueForDeletion(mail.uidl);
                    }
                }
            }
        }
    }
    return rv;
}
Also used : FileBuffer(i2p.susi.util.FileBuffer) ArrayList(java.util.ArrayList) MemoryBuffer(i2p.susi.util.MemoryBuffer) FetchRequest(i2p.susi.webmail.pop3.POP3MailBox.FetchRequest) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File)

Aggregations

FileBuffer (i2p.susi.util.FileBuffer)1 MemoryBuffer (i2p.susi.util.MemoryBuffer)1 FetchRequest (i2p.susi.webmail.pop3.POP3MailBox.FetchRequest)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1