java.lang.Object
i2p.susi.webmail.MailCache

class MailCacheextends Object
Caches email data for each folder with POP3 mailbox integration. Only DIR_FOLDER has a non-null POP3MailBox.
  • Field Details

    • mailbox

      private final POP3MailBox mailbox
      The mailbox the messages come from.
    • mails

      private final Map<String,Mail> mails
      The cached messages, by id.
    • disk

      private final PersistentMailCache disk
      The on disk copy of the cache.
    • _context

      private final I2PAppContext _context
      The app context.
    • folder

      private final Folder<String> folder
      The folder holding the messages.
    • folderName

      private final String folderName
      The name of the folder.
    • _writeLock

      private final Object _writeLock
      Serializes draft/sent full-mail writes. @since 0.9.71+.
    • _loadInProgress

      private NewMailListener _loadInProgress
      Listener of the load currently running.
    • _isLoaded

      private boolean _isLoaded
      True once the folder was loaded.
    • _isDrafts

      private final boolean _isDrafts
      True if this cache holds the drafts.
    • _log

      private final Log _log
      Logger for the mail cache.
    • FETCH_ALL_SIZE

      private static final int FETCH_ALL_SIZE
      Includes header, headers are generally 1KB to 1.5 KB, and bodies will compress well.
      See Also:
  • Constructor Details

    • MailCache

      MailCache(I2PAppContext ctx, POP3MailBox mailbox, String folderName, String host, int port, String user, String pass) throws IOException
      Does NOT load the mails in. Caller MUST call loadFromDisk(NewMailListener).
      Parameters:
      ctx - the application context supplying the logger and the disk cache
      mailbox - non-null for DIR_FOLDER; null otherwise
      folderName - the folder this cache holds, one of the WebMail.DIR_ names
      host - POP3 host, passed through to the disk cache for reconnects
      port - POP3 port, passed through to the disk cache for reconnects
      user - POP3 account name, passed through to the disk cache
      pass - POP3 password, passed through to the disk cache
      Throws:
      IOException - if the persistent cache directory cannot be created
  • Method Details

    • getFolderName

      public String getFolderName()
      The folder key this cache was created under.
      Returns:
      as passed in
    • getTranslatedName

      public String getTranslatedName()
      The folder name after message-bundle translation, "Inbox" for the inbox folder.
      Returns:
      translation of name passed in
    • getFolder

      public Folder<String> getFolder()
      The sortable view of the UIDLs held in this cache.
      Returns:
      non-null
    • getFullWriteBuffer

      public Buffer getFullWriteBuffer(String uidl)
      For writing a new full mail (NOT headers only) Caller must close. Caller must hold the write lock (see getWriteLock()) for the whole write.
      Parameters:
      uidl - the UIDL identifying the mail being written
      Returns:
      the full write buffer
    • getWriteLock

      public Object getWriteLock()
      The monitor callers must hold across a full-mail write.
      Returns:
      the write lock serializing draft and sent folder writes
    • writeComplete

      public void writeComplete(String uidl, Buffer buffer, boolean success)
      For writing a new full mail.
      Parameters:
      uidl - the UIDL identifying the mail that was written
      buffer - as received from getFullBuffer
      success - whether the write finished without error; on false the mail is not added
    • getAttachmentDir

      public File getAttachmentDir()
      Where attachments for a newly composed draft are written.
      Returns:
      non-null only for Drafts
    • moveTo

      public boolean moveTo(String uidl, MailCache toMC)
      Move a mail to another MailCache, neither may be DIR_DRAFTS.
      Parameters:
      uidl - the UIDL of the mail to move
      toMC - the cache receiving the mail
      Returns:
      true if the mail was moved, false if either cache holds drafts or the mail has no body or its file is missing
    • movedTo

      private void movedTo(Mail mail)
      Moved a mail from another MailCache.
      Parameters:
      mail - the mail moved into this cache, its body is re-read from disk
    • isLoading

      public boolean isLoading()
      Is loadFromDisk in progress?
      Returns:
      true if loadFromDisk() is running
    • isLoaded

      public boolean isLoaded()
      Has loadFromDisk completed?
      Returns:
      true if loadFromDisk() has completed
    • loadFromDisk

      public boolean loadFromDisk(NewMailListener nml)
      Threaded. Returns immediately. This will not access the mailbox. Mailbox need not be ready.
      Parameters:
      nml - notified when the background load completes; not called if a load is already in progress or already finished
      Returns:
      success false if in progress already and nml will NOT be called back, true if nml will be called back
    • blockingLoadFromDisk

      private void blockingLoadFromDisk()
      Blocking. Only call once! This will not access the mailbox. Mailbox need not be ready.
    • getUIDLs

      public String[] getUIDLs()
      The ones known locally, which will include any known on the server, if connected. Will not include any marked for deletion. This will not access the mailbox. Mailbox need not be ready. loadFromDisk() must have been called first.
      Returns:
      non-null
    • getMail

      public Mail getMail(String uidl, MailCache.FetchMode mode)
      Fetch any needed data from pop3 server, unless mode is CACHE_ONLY or HEADER_CACHE_ONLY, or this isn't the Inbox. Blocking for a long time unless mode is CACHE_ONLY or HEADER_CACHE_ONLY.
      Parameters:
      uidl - message id to get
      mode - CACHE_ONLY or HEADER_CACHE_ONLY to not pull from pop server
      Returns:
      An e-mail or null
    • getMail

      public boolean getMail(MailCache.FetchMode mode)
      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. Call only on inbox! Blocking.
      Parameters:
      mode - HEADER or ALL only
      Returns:
      true if any were fetched
    • delete

      public void delete(String uidl)
      Mark mail for deletion locally. Send delete requests to POP3 then quit and reconnect. No success/failure indication is returned. Does not delete from folder.
      Parameters:
      uidl - the UIDL of the mail to delete
    • delete

      public void delete(Collection<String> uidls)
      Mark mail for deletion locally. Send delete requests to POP3 then quit and reconnect. No success/failure indication is returned. Does not delete from folder.
      Parameters:
      uidls - the UIDLs of the mails to delete
    • _t

      private static String _t(String s)
      Looks up a localized string in the message bundle.
      Parameters:
      s - the message key
      Returns:
      the localized string
    • ngettext

      private static String ngettext(String s, String p, int n)
      Looks up the localized plural form for n items.
      Parameters:
      s - the message key of the singular form
      p - the message key of the plural form
      n - the item count, 1 selects the singular form
      Returns:
      the localized form matching the count