Changeset 264

Show
Ignore:
Timestamp:
08/03/08 02:00:51 PM (4 months ago)
Author:
octorian
Message:

More UID FETCH code

Location:
trunk/LogicMail/src/org/logicprobe/LogicMail
Files:
8 modified

Legend:

Unmodified
Added
Removed
  • trunk/LogicMail/src/org/logicprobe/LogicMail/mail/AbstractMailStore.java

    r259 r264  
    151151     *  
    152152     * @param folder The folder to request a message listing for. 
    153      * @param count The maximum number of messages to get headers for. 
    154      */ 
    155     public abstract void requestFolderMessagesRecent(FolderTreeItem folder, int count); 
     153     */ 
     154    public abstract void requestFolderMessagesRecent(FolderTreeItem folder); 
    156155     
    157156    /** 
  • trunk/LogicMail/src/org/logicprobe/LogicMail/mail/imap/ImapClient.java

    r262 r264  
    9191    private FolderTreeItem activeMailbox = null; 
    9292 
     93    /** 
     94     * Seen mailboxes, used to track whether fetching new 
     95     * messages should be based on a limited range, or 
     96     * the UIDNEXT parameter. 
     97     */ 
     98    private Hashtable seenMailboxes = new Hashtable(); 
     99     
     100    /** 
     101     * Known mailboxes, used to track protocol-specific 
     102     * information on mailboxes that is not exposed 
     103     * to other classes. 
     104     */ 
     105    private Hashtable knownMailboxes = new Hashtable(); 
     106     
    93107    public ImapClient(GlobalConfig globalConfig, ImapConfig accountConfig) { 
    94108        this.accountConfig = accountConfig; 
     
    361375        this.activeMailbox = mailbox; 
    362376        activeMailbox.setMsgCount(response.exists); 
     377        knownMailboxes.put(activeMailbox, response); 
    363378         
    364379        // ideally, this should parse out the message counts 
     
    375390                imapProtocol.executeFetchEnvelope(firstIndex, lastIndex); 
    376391         
     392        return prepareFolderMessages(response); 
     393    } 
     394 
     395    public FolderMessage[] getNewFolderMessages() throws IOException, MailException { 
     396        // Sanity check 
     397        if(activeMailbox == null) { 
     398                throw new MailException("Mailbox not selected"); 
     399        } 
     400         
     401        FolderMessage[] result; 
     402        if(!seenMailboxes.containsKey(activeMailbox)) { 
     403                int count = MailSettings.getInstance().getGlobalConfig().getRetMsgCount(); 
     404                        int msgCount = activeMailbox.getMsgCount(); 
     405                int firstIndex = Math.max(1, msgCount - count); 
     406                result = getFolderMessages(firstIndex, activeMailbox.getMsgCount()); 
     407                seenMailboxes.put(activeMailbox, new Object()); 
     408        } 
     409        else { 
     410                int uidNext = ((ImapProtocol.SelectResponse)knownMailboxes.get(activeMailbox)).uidNext; 
     411                ImapProtocol.FetchEnvelopeResponse[] response = 
     412                        imapProtocol.executeFetchEnvelopeUid(uidNext); 
     413                result = prepareFolderMessages(response); 
     414                 
     415                if(result.length > 0) { 
     416                        uidNext = result[result.length-1].getUid() + 1; 
     417                        ((ImapProtocol.SelectResponse)knownMailboxes.get(activeMailbox)).uidNext = uidNext; 
     418                } 
     419        } 
     420        return result; 
     421    } 
     422 
     423    private FolderMessage[] prepareFolderMessages(ImapProtocol.FetchEnvelopeResponse[] response) { 
    377424        FolderMessage[] folderMessages = new FolderMessage[response.length]; 
    378425        for(int i=0;i<response.length;i++) { 
     
    386433            folderMessages[i].setJunk(response[i].flags.junk); 
    387434        } 
    388          
    389         return folderMessages; 
    390     } 
    391  
     435        return folderMessages; 
     436    } 
     437     
    392438    public Message getMessage(FolderMessage folderMessage) throws IOException, MailException { 
    393439        ImapParser.MessageSection structure = getMessageStructure(folderMessage.getUid()); 
  • trunk/LogicMail/src/org/logicprobe/LogicMail/mail/imap/ImapProtocol.java

    r262 r264  
    477477                                   Integer.toString(lastIndex) + 
    478478                                   " (FLAGS UID ENVELOPE)"); 
    479          
    480         FetchEnvelopeResponse[] envResponses = new FetchEnvelopeResponse[(lastIndex - firstIndex)+1]; 
    481          
    482         // Pre-process the returned text to clean up mid-field line breaks 
     479        return prepareFetchEnvelopeResponse(rawList); 
     480    } 
     481 
     482    /** 
     483     * Execute the "UID FETCH (FLAGS UID ENVELOPE)" command 
     484     * @param uidNext Unique ID of the next message 
     485     * @return Array of FetchEnvelopeResponse objects 
     486     */ 
     487    public FetchEnvelopeResponse[] executeFetchEnvelopeUid(int uidNext) throws IOException, MailException { 
     488        if(EventLogger.getMinimumLevel() >= EventLogger.DEBUG_INFO) { 
     489            EventLogger.logEvent( 
     490            AppInfo.GUID, 
     491            ("ImapProtocol.executeFetchEnvelopeUid("+uidNext+")").getBytes(), 
     492            EventLogger.DEBUG_INFO); 
     493        } 
     494         
     495        String[] rawList = execute("UID FETCH", 
     496                Integer.toString(uidNext) + ":*" + 
     497                " (FLAGS UID ENVELOPE)"); 
     498         
     499        return prepareFetchEnvelopeResponse(rawList); 
     500    } 
     501     
     502    private FetchEnvelopeResponse[] prepareFetchEnvelopeResponse(String[] rawList) throws IOException, MailException { 
     503        // Preprocess the returned text to clean up mid-field line breaks 
    483504        // This should all become unnecessary once execute() 
    484505        // becomes more intelligent in how it handles replies 
     
    500521        } 
    501522         
    502         int index = 0; 
     523        Vector envResponses = new Vector(); 
    503524        int size = rawList2.size(); 
    504525        for(int i=0;i<size;i++) { 
     
    558579                envRespItem.index = midx; 
    559580                envRespItem.envelope = env; 
    560                 envResponses[index++] = envRespItem; 
     581                envResponses.addElement(envRespItem); 
    561582            } catch (Exception exp) { 
    562583                System.err.println("Parse error: " + exp); 
     
    564585        } 
    565586 
    566         return envResponses; 
    567     } 
    568  
     587        FetchEnvelopeResponse[] result = new FetchEnvelopeResponse[envResponses.size()]; 
     588        envResponses.copyInto(result); 
     589        return result; 
     590    } 
     591     
    569592    /** 
    570593     * Execute the "FETCH (BODYSTRUCTURE)" command 
  • trunk/LogicMail/src/org/logicprobe/LogicMail/mail/IncomingMailClient.java

    r259 r264  
    129129     
    130130    /** 
     131     * Get a list of new messages in the selected folder. 
     132     * On the first invocation, it will return the most recent messages 
     133     * up to the limit configured in the global configuration. 
     134     * On subsequent invocations, if the underlying protocol supports it, 
     135     * this method should return all new messages that have entered the 
     136     * mailbox since.  Otherwise, it will behave the same as it 
     137     * did on the first invocation. 
     138     *  
     139     * @return List of message envelopes 
     140     * @throw IOException on I/O errors 
     141     * @throw MailException on protocol errors 
     142     */ 
     143    public abstract FolderMessage[] getNewFolderMessages() throws IOException, MailException; 
     144     
     145    /** 
    131146     * Get a particular message from the selected folder. 
    132147     * The details of message retrieval should be constrained by 
  • trunk/LogicMail/src/org/logicprobe/LogicMail/mail/IncomingMailConnectionHandler.java

    r259 r264  
    9090                case REQUEST_FOLDER_MESSAGES_RECENT: 
    9191                        handleRequestFolderMessagesRecent( 
    92                                         (FolderTreeItem)params[0], 
    93                                         ((Integer)params[1]).intValue()); 
     92                                        (FolderTreeItem)params[0]); 
    9493                        break; 
    9594                case REQUEST_MESSAGE: 
     
    158157        } 
    159158         
    160         private void handleRequestFolderMessagesRecent(FolderTreeItem folder, int count) throws IOException, MailException { 
    161                 checkActiveFolder(folder); 
    162                 int msgCount = incomingClient.getActiveFolder().getMsgCount(); 
    163         int firstIndex = Math.max(1, msgCount - count); 
     159        private void handleRequestFolderMessagesRecent(FolderTreeItem folder) throws IOException, MailException { 
     160                checkActiveFolder(folder); 
    164161         
    165                 FolderMessage[] messages = incomingClient.getFolderMessages(firstIndex, msgCount); 
     162                FolderMessage[] messages = incomingClient.getNewFolderMessages(); 
    166163                 
    167164                MailConnectionHandlerListener listener = getListener(); 
  • trunk/LogicMail/src/org/logicprobe/LogicMail/mail/NetworkMailStore.java

    r259 r264  
    123123        } 
    124124         
    125         public void requestFolderMessagesRecent(FolderTreeItem folder, int count) { 
     125        public void requestFolderMessagesRecent(FolderTreeItem folder) { 
    126126                connectionHandler.addRequest( 
    127127                                IncomingMailConnectionHandler.REQUEST_FOLDER_MESSAGES_RECENT, 
    128                                 new Object[] { folder, new Integer(count) }); 
     128                                new Object[] { folder }); 
    129129        } 
    130130         
  • trunk/LogicMail/src/org/logicprobe/LogicMail/mail/pop/PopClient.java

    r262 r264  
    246246    } 
    247247 
     248    public FolderMessage[] getNewFolderMessages() throws IOException, MailException { 
     249        int count = MailSettings.getInstance().getGlobalConfig().getRetMsgCount(); 
     250                int msgCount = activeMailbox.getMsgCount(); 
     251        int firstIndex = Math.max(1, msgCount - count); 
     252        return getFolderMessages(firstIndex, activeMailbox.getMsgCount()); 
     253    } 
     254 
    248255    public Message getMessage(FolderMessage folderMessage) throws IOException, MailException { 
    249256        // Figure out the max number of lines 
  • trunk/LogicMail/src/org/logicprobe/LogicMail/model/MailboxNode.java

    r259 r264  
    3737import java.util.Vector; 
    3838 
    39 import org.logicprobe.LogicMail.conf.MailSettings; 
    4039import org.logicprobe.LogicMail.mail.FolderTreeItem; 
    4140import org.logicprobe.LogicMail.util.EventListenerList; 
     
    434433     */ 
    435434    public void refreshMessages() { 
    436         parentAccount.getMailStore().requestFolderMessagesRecent( 
    437                         this.folderTreeItem, 
    438                         MailSettings.getInstance().getGlobalConfig().getRetMsgCount()); 
     435        parentAccount.getMailStore().requestFolderMessagesRecent(this.folderTreeItem); 
    439436    } 
    440437