⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mailsyncsource.java

📁 moblie syncml mail javame
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            } 
            
            if (Store.INBOX.equals(folder)) {
                MessageFlags newMask = FlagQueue.get(key);
                
                if (newMask!=null&&!newMask.isSet(MessageFlags.DELETED)) {
                //    &&(newMask.isSet(MessageFlags.ANSWERED)
                //    ||newMask.isSet(MessageFlags.FORWARDED))) {
                    Folder inbox = store.getFolder(Store.INBOX);
                    Message msg = mm.getMessage(inbox, key);
                    
                    if (msg!=null) {
                        Log.debug("MSS - Msg is not null: Changing fwd/re flags");
                        // we've received a 200 for our update of the flags, 
                        // we've sent the mask stored in the flagqueue so we
                        // can commit this flags
                        msg.setFlags(newMask);
                        inbox.updateMessage(msg);
                        // Notification to the UI is sent by the listener
                    } 
                    //return;
                }
            }
            
            FlagQueue.remove(key);
            
        } catch (MailException me) {
            me.printStackTrace();
            Log.error("setItemStatus: error setting the status for item:"+key);
        }
    }
    
    /**
     * Send incomingMsgNumber event when the engine announce the number of
     * changes: it is the first Sync tag from server.
     */
    public void setServerItemsNumber(int number) {
        super.setServerItemsNumber(number);
        receiving = true;
    }

   
    //------------------------------------------------------- Protected methods
    
    /**
     * Init the allItems list used in the slow sync with the messages
     * in the outbox only.
     */
    protected void initAllItems() throws SyncException {
        //Sends the inbox messages headers list to the server in order to
        //receive only the new items during the slow sync
        /*try {
            // Get the keys of the messages in the Outbox only.
            MessageManager mm = MessageManager.getInstance();
            Message[] headers = mm.getMessagesInFolder(Store.INBOX);
            int count = headers.length;
            
            if (count > 0) {
                allItems = new SyncItem[count];
                for(int i=0; i<count; i++) {
                    Message msg = headers[i];
                    String luid = msg.getKey();
                    Log.debug("**** " + luid);
                    allItems[i] = new SyncItem(luid, EMAIL_TYPE,
                                               SyncItem.STATE_UPDATED,
                                               Store.INBOX, null);
                }
            } else {
                allItems = null;
            }
        } catch(MailException me) {
            Log.error("[MailSyncSource.initNewItems]: " + me.toString());
            me.printStackTrace();
            throw new SyncException(SyncException.CLIENT_ERROR, me.toString());
        }*/
        
        // The code below is commented: we're waiting for connector 
        // implementation in sending messages during slow sync
        // This Initialize all OUTBOX Items in order them to be sent 
        // when a slow sync is forced by server for any reason
        /*try {
            // Get the keys of the messages in the Outbox only.
            MessageManager mm = MessageManager.getInstance();
            Message[] headers = mm.getMessagesInFolder(Store.OUTBOX);
            int count = headers.length;
            
            if (count > 0) {
                allItems = new SyncItem[count];
                for(int i=0; i<count; i++) {
                    Message msg = headers[i];
                    String luid = msg.getKey();
                    Log.debug("**** " + luid);
                    allItems[i] = new SyncItem(luid, EMAIL_TYPE,
                                               SyncItem.STATE_NEW,
                                               Store.OUTBOX, null);
                }
            } else {
                allItems = null;
            }
        } catch(MailException me) {
            Log.error("[MailSyncSource.initNewItems]: " + me.toString());
            me.printStackTrace();
            throw new SyncException(SyncException.CLIENT_ERROR, me.toString());
        }*/
    }
    
    /**
     * Init the newItems list used in the fast sync with the messages
     * in the outbox only.
     * TODO: check the state of the message
     */
    protected void initNewItems() throws SyncException {
        try {
            // Get the keys of the messages in the Outbox only.
            MessageManager mm = MessageManager.getInstance();
            Message[] headers = mm.getMessagesInFolder(Store.OUTBOX);
            int count = headers.length;
            
            if (count > 0) {
                newItems = new SyncItem[count];
                for(int i=0; i<count; i++) {
                    Message msg = headers[i];
                    String luid = msg.getKey();
                    newItems[i] = new SyncItem(luid, EMAIL_TYPE,
                                               SyncItem.STATE_NEW,
                                               Store.OUTBOX, null);
                }
            } else {
                newItems = null;
            }
        } catch(MailException me) {
            Log.error("[MailSyncSource.initNewItems]: " + me.toString());
            me.printStackTrace();
            throw new SyncException(SyncException.CLIENT_ERROR, me.toString());
        }
    }
    
    /**
     * Not used now. It will be implemented if the Draft folder
     * has to be synchronized.
     */
    protected void initUpdItems() throws SyncException {
        Log.debug("Initializating Updated Items of folder INBOX");
        int count = 0;
        updItems = null;
        
        Hashtable updatedItemsList = FlagQueue.getUpdatedItemsList();
        Log.debug(updatedItemsList.toString());
        if (updatedItemsList==null) {
            return;
        }
        count = updatedItemsList.size();
        
        if (count > 0) {
            int i=0;

            updItems = new SyncItem[count];
            Enumeration itemKeys = updatedItemsList.keys();
            while (itemKeys.hasMoreElements()) {
                String key = (String) itemKeys.nextElement();
                String folder = MessageManager.getInstance().folderFromKey(key);
                updItems[i] = new SyncItem(key, EMAIL_TYPE,
                                           SyncItem.STATE_UPDATED,folder,null);
                i++;
            }
        }
    }
    
    /**
     * Init the delItems list used in the fast sync
     * TODO: check the state of the message
     */
    protected void initDelItems() throws SyncException {
        Log.debug("Initializating Deleted Items");
        int count=0;
        delItems = null;
        Hashtable deletedItemsList = FlagQueue.getDeletedItemsList();
            
        if (deletedItemsList==null) {
            return;
        }
        count = deletedItemsList.size();
        if (count > 0) {
            delItems = new SyncItem[count];
            Enumeration itemKeys = deletedItemsList.keys();
            int i=0;
            while (itemKeys.hasMoreElements()) {
                
                String key = (String) itemKeys.nextElement();
                String folder = MessageManager.getInstance().folderFromKey(key);
                Log.debug("Folder to find: " + folder);
                delItems[i] = new SyncItem(key, EMAIL_TYPE,
                                           SyncItem.STATE_DELETED,folder,null);
                i++;
            }
        }
    }
    
    /**
     * This function gets the item content in the backend database and
     * returns a complete item. The parameter item is marked final because
     * should not be used for the filled item: it is a reference to the
     * array entry, and filling it would cause the array to keep all the
     * filled items at the end (the gc will not dispose them). <p>
     * The content of the item depends also from the encoding of this
     * SyncSource:
     * <li> if the encoding is <i>none</i>, it must be a String, converted
     *      with getBytes(), so the engine will send it unchanged.
     * <li> if the encoding is <i>b64</i>, the content can be binary, and the
     *      type should be set accordingly, so that the receiving source
     *      can handle it. In this way, the binary content is transferred
     *      encoded in the SyncML message. This encoding can be applied to
     *      a text item too, to avoid problems with charset or other, like
     *      what is done with the SIF format.
     */
    protected SyncItem getItemContent(final SyncItem item) throws SyncException {
        Log.debug("Current Item State: " + item.getState());
        try {
            SyncItem ret = null;
            switch(item.getState()){
                case SyncItem.STATE_UPDATED:
                    Folder folder = store.getFolder(item.getParent());
                    if (folder.getName().equals(Store.INBOX)) {
                        //Last result to be achieved:
                        MessageManager mm = MessageManager.getInstance();
                        
                        byte format[];
                        
                        if (syncMode != SyncML.ALERT_CODE_SLOW) {
                            //format the message flags to propagate on the 
                            //server for fast sync: refers to the flagqueue
                            format = mm.formatFlags(FlagQueue.get(item.getKey()));
                        } else {
                            //Avoid NPE during slow sync: when performing a slow 
                            //sync, the Flagqueue is empty and should return 
                            //null values for Msg flags. This branch give the 
                            //availability to send inbox message flags that are
                            //reported on any queue
                            Message m = mm.getMessage(
                                   store.getFolder(Store.INBOX), item.getKey());
                            format = mm.format(m);
                        }
                        
                        
                        ret = new SyncItem(item);
                        ret.setContent(format);
                        Log.debug("[getItemContent]: " + new String(format));
                        return ret;
                    }
                    break;
                    
                case SyncItem.STATE_NEW:
                    if(item.getType().equals(EMAIL_TYPE)) {
                        // For type email, the content has to be filled,
                        // for folders, the item is already complete.
                        // for deleted items
                        Folder f = store.getFolder(item.getParent());
                        if (f.getName().equals(Store.OUTBOX)) {
                            MessageManager mm = MessageManager.getInstance();
                            Message msg = mm.getMessage(f, item.getKey());
                            //Sent real sent date
                            Date newSentDate = new Date();
                            mm.updateMessageDates(msg, newSentDate, newSentDate);

                            byte format[] = mm.format(msg);
                            ret = new SyncItem(item);
                            ret.setContent(format);
                            Log.debug("getItemContent: returning filled item.");
                            
                            // Return the filled item
                            return ret;
                        }
                    }
                    break;
                default:
                    break;
            }
            
        } catch (MailException me) {
            Log.error("[MailSyncSource.getItemContent] Error filling item:"
                    + item.getKey());
            me.printStackTrace();
            throw new SyncException(SyncException.CLIENT_ERROR, me.toString());
        }

        // If no changes, return the original item
        Log.debug("getItemContent: returning original item.");
        return item;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -