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

📄 inbox.java

📁 手机邮箱撒的方式方式方式的
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
package mujmail;

/*
MujMail - Simple mail client for J2ME
Copyright (C) 2006 Nguyen Son Tung <n.sontung@gmail.com>
Copyright (C) 2008 David Hauzar <david.hauzar.mujmail@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Timer;
import java.util.TimerTask;

import java.util.Vector;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;

import javax.microedition.lcdui.Displayable;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import mujmail.Lang;
import mujmail.MessageHeader;
import mujmail.MujMail;
import mujmail.PersistentBox;
import mujmail.account.MailAccount;
import mujmail.account.MailAccountPrimary;
import mujmail.ordering.ComparatorStrategy;
import mujmail.ordering.Criterion;
import mujmail.ordering.Ordering;
import mujmail.protocols.InProtocol;
import mujmail.tasks.StoppableProgress;
//#ifdef MUJMAIL_USR_FOLDERS
import mujmail.account.MailAccountDerived;
//#endif

/**
 * Stores mails downloaded from email accounts.
 */
public class InBox extends PersistentBox {
    
    /** The name of this source file */
    private static final String SOURCE_FILE = "InBox";

    /** Flag signals if we want to print debug prints */
    private static final boolean DEBUG = false;

    public final Command retrieve;
    public final Command redownload;

    /** to map stored mails in the vector storage */
    private Hashtable/*<String, MessageHeader>*/ msgIDs;
    
    boolean syncOK = true; //a flag that during Servers->InBox sync everything went OK 

    //mailsOnServers counts how many mails are currently on servers. 
    //-1 indicates that the state is unknown or synchronization is not run yet
    int mailsOnServers = -1;

    /** Flag indicates whether in box have to be sorted again */
    private boolean needResort;
    private Timer pollTimer;
    private int unreadMails = 0; /// Number of mail not readed by user
    private MessageHeader lastSafeMail;
    private boolean pushActive = false;

//    private ThreadedEmails storage = new ThreadedEmails();

    /** Holds list of (active) accounts to be retrieved into this folder */
    private Vector/*<MailAccount>*/ accounts = new Vector/*<MailAccount>*/();

    //#ifdef MUJMAIL_USR_FOLDERS    
    /** 
     *  Holds index in recordstore userMailBox database, 
     *    where info about this InBox instance take place.
     *  Note: Only for BoxList class purposes.
     *  Note: Used only for user mail boxes.
     */
    private int userBoxListDB_recordID = -1;
    //#endif
    
    /**
     * Creates inbox.
     * 
     * @param DBFile the identifier of RMS database where the mails of this box
     *  will be stored.
     * @param mMail the main object in the application
     * @param name the name of the box
     */
    public InBox(String DBFile, String name) {
        super(DBFile, MujMail.mujmail, name);
        
        //msgIDs = new Hashtable();
        retrieve = new Command(Lang.get(Lang.BTN_RTV_NEW_MAILS), Command.ITEM, 8);
        redownload = new Command(Lang.get(Lang.BTN_TB_REDOWNLOAD), Command.ITEM, 6);
        addCommand(redownload);
        addCommand(retrieve);
        
        //#ifdef MUJMAIL_USR_FOLDERS
        // Load accounts for this folder
        Thread t = new RetrieveBoxAccountsTask(RetrieveBoxAccountsTask.ACCOUNTS_LOAD);
        t.start();
        //#endif
    }

    /**
     * Returns true if the synchronization is running.
     * @return true if the synchronization is running.
     */
    public boolean isSyncRunning() {
        //mailsOnServers counts how many mails are currently on servers. 
        //-1 indicates that the state is unknown or synchronization is not run yet
        return (mailsOnServers != -1);
    }

    public void showBox() {
        // Show us only if paint can show progress with loading DB or inbox content
        if (getMailDB().getDBLoadingTask() != null) {
            super.showBox();
        }
        setCurFirstUnread();
    }

    public boolean isNeedResort() {
        return needResort;
    }

    public void setNeedResort(boolean needResort) {
        this.needResort = needResort;
    }
    
    private class Polling extends TimerTask {

        public void run() {
            for (Enumeration e = getMujMail().getMailAccounts().elements(); e.hasMoreElements();) {
                MailAccount account = (MailAccount) e.nextElement();
                if (account.isPOP3() && account.isActive()) {
                    account.getProtocol().poll(InBox.this);
                }
            }
        }
    }
    
    /*
     * Pushing Thread: Starts new push mail IMAP session for the specified account
     */
    private class Pushing extends Thread{
        private final MailAccount pushAccount;

        public Pushing(MailAccount pushAccount) {
            this.pushAccount = pushAccount;
        }
        
        
        public synchronized void run(){  
            pushAccount.startPushing();
            //pushActive = false;				WHY??? This way pushing won't ever start  
        }
    }
    
    /**
     * If some error during synchronization occurs, set this.
     */
    public void synchronizationError() {
        syncOK = false;
    }
    


    public void exit() {
        if (Settings.delOnExit) {
            deleteMarkedFromBoxAndDB();
        }
        
        super.exit();
    }
    
    

    protected boolean isBusy() {
        if (getMailDB().isBusy() || InProtocol.isBusyGlobal()) {
            return true;
        }

        return false;
    }

    /**
     * If there is polling not active, enables polling, if it is active, 
     * cancels polling.
     */
    public void poll() {
        if (pollTimer == null) {
            pollTimer = new Timer();
            pollTimer.scheduleAtFixedRate(new Polling(), 0, Settings.pollInvl >= 5 ? Settings.pollInvl * 1000 : 5 * 1000);
        } else {
            pollTimer.cancel();
            pollTimer = null;
        }
    }

    /**
     * Increases the number of unread messages.
     * 
     * @param val value to increase number of unread e-mails
     */
    public synchronized void changeUnreadMails(int val) {
        unreadMails += val;
    }

    //init the hash of mails in the vector storage
    public void initHash() {
        if (DEBUG) { System.out.println("DEBUG InBox.initHash() - " + this.getName()); }
        msgIDs = new Hashtable(storage.getSize());
        MessageHeader header;
        Enumeration messages = storage.getEnumeration();
        while (  messages.hasMoreElements() ) {
            header = (MessageHeader) messages.nextElement();
            msgIDs.put(header.getAccountID() + "@" + header.getMessageID(), header);
        }
    }

    /** Checks if we see this many sometime or it's new comming email
     *  @param ID Identification of email
     *  @return true if seen mail
     */
    public boolean wasOnceDownloaded(String accountID, String messageID) {
        if (DEBUG) { System.out.println("DEBUG InBox.wasOnceDownloaded(String, String)"); }
        if (msgIDs.containsKey(accountID + "@" + messageID)) { //if it is in the storage
            return true;
        }
        return mujMail.getMailDBSeen().wasMailSeen(accountID, messageID);
    }

    //increase mailsOnServer counter
    public synchronized void newMailOnServer() {
        if (mailsOnServers != -1) //do it only when synchronization is running
        {
            ++mailsOnServers;
        }
    }

    // TODO (Betlista): integrate threads here
//    public synchronized void addToStorage(MessageHeader message) {
//        if (message != null && storage != null) {
//            storage.addElement(message);
//        }
//    }

    public synchronized void addToMsgIDs(MessageHeader message) {
        if (message != null && msgIDs != null) {
            msgIDs.put(message.getAccountID() + "@" + message.getMessageID(), message);
        }
    }

    public synchronized void addToOnceDownloaded(MessageHeader header) {
        try { //lets mark it as seen
            mujMail.getMailDBSeen().addSeen(header);
        } catch (MyException ex) {
            report(Lang.get(Lang.ALRT_SAVING) + Lang.get(Lang.MSGIDS_CACHE) + Lang.get(Lang.FAILED), SOURCE_FILE);
        }
    }

    //synchronizes content of inbox exactly to the active servers. 
    //so mails that were deleted on the server or mails of inactive accounts will be deleted in the inbox as well
    public void serversSync(StoppableProgress progress) {
        //if something gone wrong during checking mails that are on the server
        if (!syncOK) {
            syncOK = true;
            mailsOnServers = -1; //defaults the value, indicating that sync is not running
            stop(); //stop all operation on servers
            getMujMail().alert.setAlert(this, null, Lang.get(Lang.ALRT_SYNCHRONIZING) + Lang.get(Lang.FAILED) + Lang.get(Lang.ALRT_SYS_EXCEPTION_AROSED), MyAlert.DEFAULT, AlertType.ERROR);
            return;
        }

        if (mailsOnServers == -1 && !hasAccountToRetrieve()) {
            getMujMail().alert.setAlert(this, null, Lang.get(Lang.ALRT_AS_NO_ACCOUNT_SET_ACTIVE), MyAlert.DEFAULT, AlertType.WARNING);
        } else {
            //first run of serversSync. download new mails

⌨️ 快捷键说明

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