2009-07-21 04:39:18 -04:00
|
|
|
/*
|
|
|
|
* DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
|
|
|
|
* Copyright (C) 2009 Mickael Guessant
|
|
|
|
*
|
|
|
|
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
2009-04-03 03:38:31 -04:00
|
|
|
package davmail.ui.tray;
|
2007-05-09 19:37:24 -04:00
|
|
|
|
2008-01-31 18:40:26 -05:00
|
|
|
import davmail.Settings;
|
2009-04-17 05:20:11 -04:00
|
|
|
import davmail.BundleMessage;
|
2008-11-24 07:35:58 -05:00
|
|
|
import davmail.exchange.NetworkDownException;
|
2008-01-31 18:40:26 -05:00
|
|
|
import org.apache.log4j.Logger;
|
2009-06-22 09:37:47 -04:00
|
|
|
import org.apache.log4j.Level;
|
2007-05-09 19:37:24 -04:00
|
|
|
|
2008-11-24 07:35:58 -05:00
|
|
|
import javax.imageio.ImageIO;
|
2007-10-20 14:35:02 -04:00
|
|
|
import java.awt.*;
|
2008-11-24 07:35:58 -05:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.URL;
|
2007-10-09 08:01:58 -04:00
|
|
|
|
2007-05-09 19:37:24 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tray icon handler
|
|
|
|
*/
|
2009-09-21 17:34:13 -04:00
|
|
|
public final class DavGatewayTray {
|
2009-09-24 05:18:57 -04:00
|
|
|
private static final Logger LOGGER = Logger.getLogger("davmail");
|
|
|
|
private static final long ICON_SWITCH_MINIMUM_DELAY = 250;
|
|
|
|
private static long lastIconSwitch;
|
2008-01-31 18:40:26 -05:00
|
|
|
|
2009-04-16 17:52:17 -04:00
|
|
|
private DavGatewayTray() {
|
2007-05-09 19:37:24 -04:00
|
|
|
}
|
|
|
|
|
2008-10-31 13:12:30 -04:00
|
|
|
static DavGatewayTrayInterface davGatewayTray;
|
2007-05-09 19:37:24 -04:00
|
|
|
|
2009-09-04 04:46:39 -04:00
|
|
|
/**
|
|
|
|
* Return AWT Image icon for frame title.
|
|
|
|
*
|
|
|
|
* @return frame icon
|
|
|
|
*/
|
2008-11-03 05:56:57 -05:00
|
|
|
public static Image getFrameIcon() {
|
|
|
|
Image icon = null;
|
|
|
|
if (davGatewayTray != null) {
|
|
|
|
icon = davGatewayTray.getFrameIcon();
|
|
|
|
}
|
|
|
|
return icon;
|
|
|
|
}
|
|
|
|
|
2009-09-04 04:46:39 -04:00
|
|
|
/**
|
|
|
|
* Switch tray icon between active and standby icon.
|
|
|
|
*/
|
2007-05-09 19:37:24 -04:00
|
|
|
public static void switchIcon() {
|
2008-01-31 18:40:26 -05:00
|
|
|
if (davGatewayTray != null) {
|
2009-09-24 05:18:57 -04:00
|
|
|
if (System.currentTimeMillis() - lastIconSwitch > ICON_SWITCH_MINIMUM_DELAY) {
|
|
|
|
davGatewayTray.switchIcon();
|
|
|
|
lastIconSwitch = System.currentTimeMillis();
|
|
|
|
}
|
2007-05-09 19:37:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-04 04:46:39 -04:00
|
|
|
/**
|
|
|
|
* Set tray icon to inactive (network down)
|
|
|
|
*/
|
2007-05-09 19:37:24 -04:00
|
|
|
public static void resetIcon() {
|
2008-11-24 07:35:58 -05:00
|
|
|
if (davGatewayTray != null && isActive()) {
|
2008-01-31 18:40:26 -05:00
|
|
|
davGatewayTray.resetIcon();
|
2007-05-09 19:37:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-04 04:46:39 -04:00
|
|
|
/**
|
|
|
|
* Check if current tray status is inactive (network down).
|
|
|
|
*
|
|
|
|
* @return true if inactive
|
|
|
|
*/
|
2008-11-24 07:35:58 -05:00
|
|
|
public static boolean isActive() {
|
|
|
|
return davGatewayTray == null || davGatewayTray.isActive();
|
|
|
|
}
|
|
|
|
|
2009-09-04 04:46:39 -04:00
|
|
|
/**
|
|
|
|
* Log and display balloon message according to log level.
|
|
|
|
*
|
|
|
|
* @param message text message
|
|
|
|
* @param level log level
|
|
|
|
*/
|
2009-09-24 05:18:57 -04:00
|
|
|
private static void displayMessage(BundleMessage message, Level level) {
|
2009-06-22 09:37:47 -04:00
|
|
|
LOGGER.log(level, message.formatLog());
|
2008-01-31 18:40:26 -05:00
|
|
|
if (davGatewayTray != null) {
|
2009-06-22 09:37:47 -04:00
|
|
|
davGatewayTray.displayMessage(message.format(), level);
|
2008-01-31 18:40:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-04 04:46:39 -04:00
|
|
|
/**
|
|
|
|
* Log and display balloon message and exception according to log level.
|
|
|
|
*
|
|
|
|
* @param message text message
|
|
|
|
* @param e exception
|
|
|
|
* @param level log level
|
|
|
|
*/
|
2009-09-24 05:18:57 -04:00
|
|
|
private static void displayMessage(BundleMessage message, Exception e, Level level) {
|
2009-07-15 18:47:31 -04:00
|
|
|
if (e instanceof NetworkDownException) {
|
|
|
|
LOGGER.log(level, BundleMessage.getExceptionLogMessage(message, e));
|
|
|
|
} else {
|
|
|
|
LOGGER.log(level, BundleMessage.getExceptionLogMessage(message, e), e);
|
|
|
|
}
|
2009-04-15 11:20:06 -04:00
|
|
|
if (davGatewayTray != null
|
2009-05-10 19:10:48 -04:00
|
|
|
&& (!(e instanceof NetworkDownException))) {
|
2009-06-22 09:37:47 -04:00
|
|
|
davGatewayTray.displayMessage(BundleMessage.getExceptionMessage(message, e), level);
|
2008-11-24 07:35:58 -05:00
|
|
|
}
|
|
|
|
if (davGatewayTray != null && e instanceof NetworkDownException) {
|
|
|
|
davGatewayTray.inactiveIcon();
|
2007-05-09 19:37:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-04 04:46:39 -04:00
|
|
|
/**
|
|
|
|
* Log message at level DEBUG.
|
|
|
|
*
|
|
|
|
* @param message bundle message
|
|
|
|
*/
|
2009-04-17 05:20:11 -04:00
|
|
|
public static void debug(BundleMessage message) {
|
2009-06-22 09:37:47 -04:00
|
|
|
displayMessage(message, Level.DEBUG);
|
2009-04-17 05:20:11 -04:00
|
|
|
}
|
2007-05-09 19:37:24 -04:00
|
|
|
|
2009-09-04 04:46:39 -04:00
|
|
|
/**
|
|
|
|
* Log message at level INFO.
|
|
|
|
*
|
|
|
|
* @param message bundle message
|
|
|
|
*/
|
2009-04-17 05:20:11 -04:00
|
|
|
public static void info(BundleMessage message) {
|
2009-06-22 09:37:47 -04:00
|
|
|
displayMessage(message, Level.INFO);
|
2009-04-17 05:20:11 -04:00
|
|
|
}
|
2007-05-09 19:37:24 -04:00
|
|
|
|
2009-09-04 04:46:39 -04:00
|
|
|
/**
|
|
|
|
* Log message at level WARN.
|
|
|
|
*
|
|
|
|
* @param message bundle message
|
|
|
|
*/
|
2009-04-17 05:20:11 -04:00
|
|
|
public static void warn(BundleMessage message) {
|
2009-06-22 09:37:47 -04:00
|
|
|
displayMessage(message, Level.WARN);
|
2009-04-17 05:20:11 -04:00
|
|
|
}
|
2007-05-09 19:37:24 -04:00
|
|
|
|
2009-09-04 04:46:39 -04:00
|
|
|
/**
|
|
|
|
* Log exception at level WARN.
|
|
|
|
*
|
|
|
|
* @param e exception
|
|
|
|
*/
|
2009-07-15 18:47:31 -04:00
|
|
|
public static void warn(Exception e) {
|
|
|
|
displayMessage(null, e, Level.WARN);
|
|
|
|
}
|
|
|
|
|
2009-09-04 04:46:39 -04:00
|
|
|
/**
|
|
|
|
* Log message at level ERROR.
|
|
|
|
*
|
|
|
|
* @param message bundle message
|
|
|
|
*/
|
2009-04-17 06:32:11 -04:00
|
|
|
public static void error(BundleMessage message) {
|
2009-06-22 09:37:47 -04:00
|
|
|
displayMessage(message, Level.ERROR);
|
2009-04-17 06:32:11 -04:00
|
|
|
}
|
|
|
|
|
2009-09-04 04:46:39 -04:00
|
|
|
/**
|
|
|
|
* Log exception at level WARN for NetworkDownException,
|
|
|
|
* ERROR for other exceptions.
|
|
|
|
*
|
|
|
|
* @param e exception
|
|
|
|
*/
|
2009-07-15 18:47:31 -04:00
|
|
|
public static void log(Exception e) {
|
|
|
|
// only warn on network down
|
|
|
|
if (e instanceof NetworkDownException) {
|
|
|
|
warn(e);
|
|
|
|
} else {
|
|
|
|
error(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-04 04:46:39 -04:00
|
|
|
/**
|
|
|
|
* Log exception at level ERROR.
|
|
|
|
*
|
|
|
|
* @param e exception
|
|
|
|
*/
|
2008-11-24 07:35:58 -05:00
|
|
|
public static void error(Exception e) {
|
2009-06-22 09:37:47 -04:00
|
|
|
displayMessage(null, e, Level.ERROR);
|
2009-04-17 06:32:11 -04:00
|
|
|
}
|
|
|
|
|
2009-09-04 04:46:39 -04:00
|
|
|
/**
|
|
|
|
* Log message and exception at level DEBUG.
|
|
|
|
*
|
|
|
|
* @param message bundle message
|
|
|
|
* @param e exception
|
|
|
|
*/
|
2009-04-17 06:32:11 -04:00
|
|
|
public static void debug(BundleMessage message, Exception e) {
|
2009-06-22 09:37:47 -04:00
|
|
|
displayMessage(message, e, Level.DEBUG);
|
2007-05-09 19:37:24 -04:00
|
|
|
}
|
|
|
|
|
2009-09-04 04:46:39 -04:00
|
|
|
/**
|
|
|
|
* Log message and exception at level WARN.
|
|
|
|
*
|
|
|
|
* @param message bundle message
|
|
|
|
* @param e exception
|
|
|
|
*/
|
2009-04-17 05:20:11 -04:00
|
|
|
public static void warn(BundleMessage message, Exception e) {
|
2009-06-22 09:37:47 -04:00
|
|
|
displayMessage(message, e, Level.WARN);
|
2009-04-17 05:20:11 -04:00
|
|
|
}
|
2007-05-09 19:37:24 -04:00
|
|
|
|
2009-09-04 04:46:39 -04:00
|
|
|
/**
|
|
|
|
* Log message and exception at level ERROR.
|
|
|
|
*
|
|
|
|
* @param message bundle message
|
|
|
|
* @param e exception
|
|
|
|
*/
|
2009-04-17 05:20:11 -04:00
|
|
|
public static void error(BundleMessage message, Exception e) {
|
2009-06-22 09:37:47 -04:00
|
|
|
displayMessage(message, e, Level.ERROR);
|
2009-04-17 05:20:11 -04:00
|
|
|
}
|
|
|
|
|
2009-09-04 04:46:39 -04:00
|
|
|
/**
|
|
|
|
* Create tray icon and register frame listeners.
|
|
|
|
*/
|
2007-05-09 19:37:24 -04:00
|
|
|
public static void init() {
|
2008-01-31 18:40:26 -05:00
|
|
|
if (!Settings.getBooleanProperty("davmail.server")) {
|
|
|
|
ClassLoader classloader = DavGatewayTray.class.getClassLoader();
|
|
|
|
// first try to load SWT
|
2007-10-09 05:48:59 -04:00
|
|
|
try {
|
2008-01-31 18:40:26 -05:00
|
|
|
// trigger ClassNotFoundException
|
|
|
|
classloader.loadClass("org.eclipse.swt.SWT");
|
|
|
|
// SWT available, create tray
|
|
|
|
davGatewayTray = new SwtGatewayTray();
|
|
|
|
davGatewayTray.init();
|
|
|
|
} catch (ClassNotFoundException e) {
|
2009-04-17 05:20:11 -04:00
|
|
|
DavGatewayTray.info(new BundleMessage("LOG_SWT_NOT_AVAILABLE"));
|
2008-01-31 18:40:26 -05:00
|
|
|
}
|
|
|
|
// try java6 tray support
|
|
|
|
if (davGatewayTray == null) {
|
|
|
|
try {
|
|
|
|
if (SystemTray.isSupported()) {
|
2009-04-02 11:08:34 -04:00
|
|
|
if (isOSX()) {
|
|
|
|
davGatewayTray = new OSXAwtGatewayTray();
|
|
|
|
} else {
|
|
|
|
davGatewayTray = new AwtGatewayTray();
|
|
|
|
}
|
2008-01-31 18:40:26 -05:00
|
|
|
davGatewayTray.init();
|
|
|
|
}
|
|
|
|
} catch (NoClassDefFoundError e) {
|
2009-04-17 05:20:11 -04:00
|
|
|
DavGatewayTray.info(new BundleMessage("LOG_SYSTEM_TRAY_NOT_AVAILABLE"));
|
2007-08-02 09:34:10 -04:00
|
|
|
}
|
2007-05-09 19:37:24 -04:00
|
|
|
}
|
2008-01-31 18:40:26 -05:00
|
|
|
if (davGatewayTray == null) {
|
2009-04-02 11:08:34 -04:00
|
|
|
if (isOSX()) {
|
2009-02-11 17:38:46 -05:00
|
|
|
// MacOS
|
|
|
|
davGatewayTray = new OSXFrameGatewayTray();
|
|
|
|
} else {
|
|
|
|
davGatewayTray = new FrameGatewayTray();
|
|
|
|
}
|
2009-02-11 10:33:58 -05:00
|
|
|
davGatewayTray.init();
|
2008-01-31 18:40:26 -05:00
|
|
|
}
|
2007-10-09 05:48:59 -04:00
|
|
|
}
|
2007-05-09 19:37:24 -04:00
|
|
|
}
|
2008-11-24 07:35:58 -05:00
|
|
|
|
2009-04-02 11:08:34 -04:00
|
|
|
/**
|
|
|
|
* Test if running on OSX
|
|
|
|
*
|
|
|
|
* @return true on Mac OS X
|
|
|
|
*/
|
2009-09-24 05:18:57 -04:00
|
|
|
private static boolean isOSX() {
|
2009-04-02 11:08:34 -04:00
|
|
|
return System.getProperty("os.name").toLowerCase().startsWith("mac os x");
|
|
|
|
}
|
|
|
|
|
2008-11-24 07:35:58 -05:00
|
|
|
/**
|
|
|
|
* Load image with current class loader.
|
|
|
|
*
|
|
|
|
* @param fileName image resource file name
|
|
|
|
* @return image
|
|
|
|
*/
|
|
|
|
public static Image loadImage(String fileName) {
|
|
|
|
Image result = null;
|
|
|
|
try {
|
|
|
|
ClassLoader classloader = DavGatewayTray.class.getClassLoader();
|
|
|
|
URL imageUrl = classloader.getResource(fileName);
|
|
|
|
result = ImageIO.read(imageUrl);
|
|
|
|
} catch (IOException e) {
|
2009-04-17 05:20:11 -04:00
|
|
|
DavGatewayTray.warn(new BundleMessage("LOG_UNABLE_TO_LOAD_IMAGE"), e);
|
2008-11-24 07:35:58 -05:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2007-05-09 19:37:24 -04:00
|
|
|
}
|