📄 maileventthread.java
字号:
/*** $Id: MailEventThread.java,v 1.3 2001/05/07 12:37:22 kunugi Exp $**** Copyright (c) 2000-2001 Jeff Gay** on behalf of ICEMail.org <http://www.icemail.org>** Copyright (c) 1998-2000 by Timothy Gerard Endres** ** This program is free software.** ** You may redistribute it and/or modify it under the terms of the GNU** General Public License as published by the Free Software Foundation.** Version 2 of the license should be included with this distribution in** the file LICENSE, as well as License.html. If the license is not** included with this distribution, you may find a copy at the FSF web** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.**** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR** REDISTRIBUTION OF THIS SOFTWARE. */package org.icemail.mail;import java.util.Vector;import javax.mail.Folder;import javax.mail.MessagingException;import javax.mail.Store;import javax.swing.JButton;import javax.swing.ImageIcon;import org.icemail.util.UserProperties;/** */public class MailEventThread extends Thread{ static private MailEventThread Instance_ = null;// FIX ME; blinking of the notify button should be in a separate thread which starts and stops private JButton notifyButton = null; private ImageIcon[] notifyIcons_ = null; private int notifyIconIndex = 0; private boolean incomingNotifyOn = false; private boolean toggleIncomingNotify = false; private Folder monitorFolder = null; private long monitorMillis; private long lastMonitorMillis = 0; private Vector stores = new Vector(); private long tickleMillis; private long lastTickleMillis = 0; /** * Default constructor, private to disable multiple instantiations. */ private MailEventThread() { super(); this.monitorMillis = UserProperties.getProperty( "folderMonitorMillis", (long)(7 * 1000) ); this.tickleMillis = UserProperties.getProperty( "storeTickleMillis", (long)(180 * 1000) ); } /** * Get the SINGLE instance of the thread, creating it if necessary. * * @return the SINGLE instance of the thread */ public static synchronized MailEventThread getInstance() { if ( MailEventThread.Instance_ == null ) { MailEventThread.Instance_ = new MailEventThread(); } return MailEventThread.Instance_; }// used by FolderTableModel public void setMonitorFolder( Folder folder ) { monitorFolder = folder; }// used by MainPanel.java public void setNotifyButton( JButton button ) { notifyButton = button; }// used by MainPanel.java public void setNotifyIcons( ImageIcon[] icons ) { notifyIcons_ = icons; }// used by MailUtilities.java public void addStore( Store store ) { synchronized( stores ) { if ( ! stores.contains( store ) ) { stores.addElement( store ); } } }// used by FolderTableModel public void notifyIncomingMail( Folder folder, boolean turnOn ) { synchronized ( this ) { if ( turnOn ) { if ( ! incomingNotifyOn ) { toggleIncomingNotify = true; } } else { if ( incomingNotifyOn ) { toggleIncomingNotify = true; } } } }//............................................................ /** * <p> * Implementation of java.lang.Thread.run() */ public void run() { for ( ; ; ) { try { sleep( 731 ); } catch ( InterruptedException ex ) { // System.err.println( "MailEventThread INTERRUPTED." ); } long currentMillis = System.currentTimeMillis(); synchronized ( this ) { // Check for Store tickling if ( (currentMillis - this.lastTickleMillis) > this.tickleMillis ) { tickleStores(); lastTickleMillis = System.currentTimeMillis(); } // Check for folder monitoring if ( (currentMillis - this.lastMonitorMillis) > this.monitorMillis ) { monitorFolder(); lastMonitorMillis = System.currentTimeMillis(); } } // Check for folder monitor icon blinking boolean xrotateIcon = false; synchronized ( this ) { if ( this.toggleIncomingNotify ) { xrotateIcon = true; this.toggleIncomingNotify = false; if ( this.incomingNotifyOn ) { this.notifyIconIndex = 0; MainFrame.getInstance().toggleTitleHilite( false ); } else { MainFrame.getInstance().toggleTitleHilite( true ); } this.incomingNotifyOn = ! this.incomingNotifyOn; if ( this.incomingNotifyOn ) { ICEMailMedia.playIncomingSound(); } } else if ( this.incomingNotifyOn ) { if ( notifyIcons_ != null && notifyIcons_.length > 0 ) { xrotateIcon = true; if ( ++this.notifyIconIndex >= notifyIcons_.length ) { this.notifyIconIndex = 0; } } } if ( xrotateIcon && this.notifyButton != null ) { this.notifyButton.setIcon( notifyIcons_[ this.notifyIconIndex ] ); this.notifyButton.repaint( 100 ); } } } } private synchronized void tickleStores() { int cnt = this.stores.size(); for ( int i = 0 ; i < cnt ; ++i ) { Store s = (Store)this.stores.elementAt(i); // This seems like a TREMENDOUS HACK, but I do // not know of any other means to keep those // Stores open. try { Folder f = s.getFolder( "RandomFolderName" ); f.exists(); } catch ( MessagingException ex ) { try { if ( ! s.isConnected() ) { s.connect(); } } catch ( MessagingException ex2 ) { } } } } private synchronized void monitorFolder() { if ( this.monitorFolder != null ) { String state = "begin"; try { state = "connect"; MailUtilities.setStoreConnected( this.monitorFolder.getStore() ); state = "exists"; if ( this.monitorFolder.exists() ) { state = "open"; MailUtilities.setFolderOpenAndReady( this.monitorFolder, Folder.READ_WRITE ); state = "getcount"; this.monitorFolder.getMessageCount(); } } catch ( MessagingException ex ) { ex.printStackTrace( System.err ); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -