Added an (grey) inactive icon on network down detection

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@178 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2008-11-24 12:35:58 +00:00
parent b9763f8028
commit 20ef4a9da0
10 changed files with 201 additions and 134 deletions

View File

@ -34,7 +34,7 @@ public class ExchangeSessionFactory {
if (checkNetwork()) { if (checkNetwork()) {
throw e; throw e;
} else { } else {
throw new IOException("All network interfaces down !"); throw new NetworkDownException("All network interfaces down !");
} }
} }
} }
@ -64,12 +64,16 @@ public class ExchangeSessionFactory {
String message = "DavMail configuration exception: \n"; String message = "DavMail configuration exception: \n";
if (checkNetwork()) { if (checkNetwork()) {
message += "Unknown host " + exc.getMessage(); message += "Unknown host " + exc.getMessage();
ExchangeSession.LOGGER.error(message, exc);
throw new IOException(message);
} else { } else {
message += "All network interfaces down !"; message += "All network interfaces down !";
ExchangeSession.LOGGER.error(message, exc);
throw new NetworkDownException(message);
} }
ExchangeSession.LOGGER.error(message, exc); } catch (NetworkDownException exc) {
throw new IOException(message); throw exc;
} catch (Exception exc) { } catch (Exception exc) {
ExchangeSession.LOGGER.error("DavMail configuration exception: \n" + exc.getMessage(), exc); ExchangeSession.LOGGER.error("DavMail configuration exception: \n" + exc.getMessage(), exc);
throw new IOException("DavMail configuration exception: \n" + exc.getMessage()); throw new IOException("DavMail configuration exception: \n" + exc.getMessage());

View File

@ -0,0 +1,12 @@
package davmail.exchange;
import java.io.IOException;
/**
* Custom exception to mark network down case.
*/
public class NetworkDownException extends IOException {
public NetworkDownException(String message) {
super(message);
}
}

View File

@ -56,7 +56,7 @@ public class ImapConnection extends AbstractConnection {
sendClient(commandId + " OK Authenticated"); sendClient(commandId + " OK Authenticated");
state = AUTHENTICATED; state = AUTHENTICATED;
} catch (Exception e) { } catch (Exception e) {
DavGatewayTray.error(e.getMessage()); DavGatewayTray.error(e);
sendClient(commandId + " NO LOGIN failed"); sendClient(commandId + " NO LOGIN failed");
state = INITIAL; state = INITIAL;
} }

View File

@ -75,7 +75,6 @@ public class PopConnection extends AbstractConnection {
break; break;
} }
tokens = new StringTokenizer(line); tokens = new StringTokenizer(line);
if (tokens.hasMoreTokens()) { if (tokens.hasMoreTokens()) {
String command = tokens.nextToken(); String command = tokens.nextToken();
@ -117,12 +116,8 @@ public class PopConnection extends AbstractConnection {
// can not send error to client after a socket exception // can not send error to client after a socket exception
DavGatewayTray.warn("Client closed connection ", e); DavGatewayTray.warn("Client closed connection ", e);
} catch (Exception e) { } catch (Exception e) {
String message = e.getMessage(); DavGatewayTray.error(e);
if (message == null) { sendERR(e);
message = "Authentication failed: "+e.toString();
}
DavGatewayTray.error(message);
sendERR(message);
} }
} }
} else if ("CAPA".equalsIgnoreCase(command)) { } else if ("CAPA".equalsIgnoreCase(command)) {
@ -223,7 +218,7 @@ public class PopConnection extends AbstractConnection {
os.flush(); os.flush();
} }
} catch (IOException e) { } catch (IOException e) {
DavGatewayTray.error(e.getMessage()); DavGatewayTray.error(e);
try { try {
sendERR(e.getMessage()); sendERR(e.getMessage());
} catch (IOException e2) { } catch (IOException e2) {
@ -239,6 +234,14 @@ public class PopConnection extends AbstractConnection {
sendClient("+OK ", message); sendClient("+OK ", message);
} }
public void sendERR(Exception e) throws IOException {
String message = e.getMessage();
if (message == null) {
message = e.toString();
}
sendERR(message);
}
public void sendERR(String message) throws IOException { public void sendERR(String message) throws IOException {
sendClient("-ERR ", message.replaceAll("\\n", " ")); sendClient("-ERR ", message.replaceAll("\\n", " "));
} }

View File

@ -3,8 +3,6 @@ package davmail.smtp;
import davmail.AbstractConnection; import davmail.AbstractConnection;
import davmail.exchange.ExchangeSessionFactory; import davmail.exchange.ExchangeSessionFactory;
import davmail.tray.DavGatewayTray; import davmail.tray.DavGatewayTray;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.io.IOException; import java.io.IOException;
import java.net.Socket; import java.net.Socket;
@ -21,7 +19,7 @@ public class SmtpConnection extends AbstractConnection {
protected static final int RECIPIENT = 3; protected static final int RECIPIENT = 3;
protected static final int MAILDATA = 4; protected static final int MAILDATA = 4;
protected static final int LOGIN = 5; protected static final int LOGIN = 5;
protected static final int PASSWORD = 6; public static final int PASSWORD = 6;
// Initialize the streams and start the thread // Initialize the streams and start the thread
public SmtpConnection(Socket clientSocket) { public SmtpConnection(Socket clientSocket) {
@ -147,11 +145,11 @@ public class SmtpConnection extends AbstractConnection {
sendClient("235 OK Authenticated"); sendClient("235 OK Authenticated");
state = AUTHENTICATED; state = AUTHENTICATED;
} catch (Exception e) { } catch (Exception e) {
DavGatewayTray.error(e);
String message = e.getMessage(); String message = e.getMessage();
if (message == null) { if (message == null) {
message = e.toString(); message = e.toString();
} }
DavGatewayTray.error(message);
message = message.replaceAll("\\n", " "); message = message.replaceAll("\\n", " ");
sendClient("554 Authenticated failed " + message); sendClient("554 Authenticated failed " + message);
state = INITIAL; state = INITIAL;
@ -159,16 +157,6 @@ public class SmtpConnection extends AbstractConnection {
} }
protected String base64Encode(String value) {
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(value.getBytes());
}
protected String base64Decode(String value) throws IOException {
BASE64Decoder decoder = new BASE64Decoder();
return new String(decoder.decodeBuffer(value));
}
/** /**
* Decode SMTP credentials * Decode SMTP credentials
* *

View File

@ -10,12 +10,9 @@ import org.apache.log4j.lf5.LogLevel;
import org.apache.log4j.lf5.viewer.LogBrokerMonitor; import org.apache.log4j.lf5.viewer.LogBrokerMonitor;
import javax.swing.*; import javax.swing.*;
import javax.imageio.ImageIO;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.net.URL;
import java.io.IOException;
/** /**
* Tray icon handler based on java 1.6 * Tray icon handler based on java 1.6
@ -24,35 +21,53 @@ public class AwtGatewayTray implements DavGatewayTrayInterface {
protected AwtGatewayTray() { protected AwtGatewayTray() {
} }
// LOCK for synchronized block
protected static final Object LOCK = new Object();
private static TrayIcon trayIcon = null; private static TrayIcon trayIcon = null;
private static Image image = null; private static Image image = null;
private static Image image2 = null; private static Image image2 = null;
private static Image inactiveImage = null;
private boolean isActive = true;
public Image getFrameIcon() { public Image getFrameIcon() {
return image; return image;
} }
public void switchIcon() { public void switchIcon() {
synchronized (LOCK) { isActive = true;
if (trayIcon.getImage() == image) { SwingUtilities.invokeLater(new Runnable() {
trayIcon.setImage(image2); public void run() {
} else { if (trayIcon.getImage() == image) {
trayIcon.setImage(image); trayIcon.setImage(image2);
} else {
trayIcon.setImage(image);
}
} }
} });
} }
public void resetIcon() { public void resetIcon() {
synchronized (LOCK) { SwingUtilities.invokeLater(new Runnable() {
trayIcon.setImage(image); public void run() {
} trayIcon.setImage(image);
}
});
} }
public void displayMessage(String message, Priority priority) { public void inactiveIcon() {
synchronized (LOCK) { isActive = false;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
trayIcon.setImage(inactiveImage);
}
});
}
public boolean isActive() {
return isActive;
}
public void displayMessage(final String message, final Priority priority) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
if (trayIcon != null) { if (trayIcon != null) {
TrayIcon.MessageType messageType = null; TrayIcon.MessageType messageType = null;
if (priority == Priority.INFO) { if (priority == Priority.INFO) {
@ -67,8 +82,8 @@ public class AwtGatewayTray implements DavGatewayTrayInterface {
} }
trayIcon.setToolTip("DavMail gateway \n" + message); trayIcon.setToolTip("DavMail gateway \n" + message);
} }
} }
});
} }
public void init() { public void init() {
@ -79,7 +94,9 @@ public class AwtGatewayTray implements DavGatewayTrayInterface {
}); });
} }
public void createAndShowGUI() {
protected void createAndShowGUI() {
// set native look and feel // set native look and feel
try { try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
@ -89,21 +106,9 @@ public class AwtGatewayTray implements DavGatewayTrayInterface {
// get the SystemTray instance // get the SystemTray instance
SystemTray tray = SystemTray.getSystemTray(); SystemTray tray = SystemTray.getSystemTray();
// load an image image = DavGatewayTray.loadImage("tray.png");
ClassLoader classloader = DavGatewayTray.class.getClassLoader(); image2 = DavGatewayTray.loadImage("tray.png");
try { inactiveImage = DavGatewayTray.loadImage("trayinactive.png");
URL imageUrl = classloader.getResource("tray.png");
image = ImageIO.read(imageUrl);
} catch (IOException e) {
DavGatewayTray.warn("Unable to load image", e);
}
try {
URL imageUrl2 = classloader.getResource("tray2.png");
image2 = ImageIO.read(imageUrl2);
} catch (IOException e) {
DavGatewayTray.warn("Unable to load image", e);
}
// create a popup menu // create a popup menu
PopupMenu popup = new PopupMenu(); PopupMenu popup = new PopupMenu();

View File

@ -1,10 +1,14 @@
package davmail.tray; package davmail.tray;
import davmail.Settings; import davmail.Settings;
import davmail.exchange.NetworkDownException;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.apache.log4j.Priority; import org.apache.log4j.Priority;
import javax.imageio.ImageIO;
import java.awt.*; import java.awt.*;
import java.io.IOException;
import java.net.URL;
/** /**
@ -33,11 +37,21 @@ public class DavGatewayTray {
} }
public static void resetIcon() { public static void resetIcon() {
if (davGatewayTray != null) { if (davGatewayTray != null && isActive()) {
davGatewayTray.resetIcon(); davGatewayTray.resetIcon();
} }
} }
public static void inactiveIcon() {
if (davGatewayTray != null) {
davGatewayTray.inactiveIcon();
}
}
public static boolean isActive() {
return davGatewayTray == null || davGatewayTray.isActive();
}
protected static void displayMessage(String message, Priority priority) { protected static void displayMessage(String message, Priority priority) {
LOGGER.log(priority, message); LOGGER.log(priority, message);
if (davGatewayTray != null) { if (davGatewayTray != null) {
@ -47,8 +61,21 @@ public class DavGatewayTray {
protected static void displayMessage(String message, Exception e, Priority priority) { protected static void displayMessage(String message, Exception e, Priority priority) {
LOGGER.log(priority, message, e); LOGGER.log(priority, message, e);
if (davGatewayTray != null) { if (davGatewayTray != null
davGatewayTray.displayMessage(message + " " + e + " " + e.getMessage(), priority); && (!(e instanceof NetworkDownException) || isActive())) {
StringBuilder buffer = new StringBuilder();
if (message != null) {
buffer.append(message).append(" ");
}
if (e.getMessage() != null) {
buffer.append(e.getMessage());
} else {
buffer.append(e.toString());
}
davGatewayTray.displayMessage(buffer.toString(), priority);
}
if (davGatewayTray != null && e instanceof NetworkDownException) {
davGatewayTray.inactiveIcon();
} }
} }
@ -68,6 +95,10 @@ public class DavGatewayTray {
displayMessage(message, Priority.ERROR); displayMessage(message, Priority.ERROR);
} }
public static void error(Exception e) {
displayMessage(null, e, Priority.ERROR);
}
public static void debug(String message, Exception e) { public static void debug(String message, Exception e) {
displayMessage(message, e, Priority.DEBUG); displayMessage(message, e, Priority.DEBUG);
} }
@ -113,4 +144,22 @@ public class DavGatewayTray {
} }
} }
} }
/**
* 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) {
DavGatewayTray.warn("Unable to load image", e);
}
return result;
}
} }

View File

@ -10,6 +10,8 @@ import java.awt.*;
public interface DavGatewayTrayInterface { public interface DavGatewayTrayInterface {
void switchIcon(); void switchIcon();
void resetIcon(); void resetIcon();
void inactiveIcon();
boolean isActive();
Image getFrameIcon(); Image getFrameIcon();
void displayMessage(String message, Priority priority); void displayMessage(String message, Priority priority);
void init(); void init();

View File

@ -10,17 +10,8 @@ import org.apache.log4j.lf5.LogLevel;
import org.apache.log4j.lf5.viewer.LogBrokerMonitor; import org.apache.log4j.lf5.viewer.LogBrokerMonitor;
import org.eclipse.swt.SWT; import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.*;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolTip;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;
import javax.imageio.ImageIO;
import javax.swing.*; import javax.swing.*;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
@ -32,71 +23,96 @@ public class SwtGatewayTray implements DavGatewayTrayInterface {
protected SwtGatewayTray() { protected SwtGatewayTray() {
} }
// LOCK for synchronized block
protected static final Object LOCK = new Object();
private static TrayItem trayItem = null; private static TrayItem trayItem = null;
private static java.awt.Image awtImage = null; private static java.awt.Image awtImage = null;
private static Image image = null; private static Image image = null;
private static Image image2 = null; private static Image image2 = null;
private static Image inactiveImage = null;
private static Display display; private static Display display;
private static Shell shell; private static Shell shell;
private boolean isActive = true;
public java.awt.Image getFrameIcon() { public java.awt.Image getFrameIcon() {
return awtImage; return awtImage;
} }
public void switchIcon() { public void switchIcon() {
display.syncExec( isActive = true;
new Runnable() { display.syncExec(new Runnable() {
public void run() { public void run() {
if (trayItem.getImage() == image) { if (trayItem.getImage() == image) {
trayItem.setImage(image2); trayItem.setImage(image2);
} else { } else {
trayItem.setImage(image); trayItem.setImage(image);
} }
} }
}); });
} }
public void resetIcon() { public void resetIcon() {
display.syncExec( display.syncExec(new Runnable() {
new Runnable() { public void run() {
public void run() { trayItem.setImage(image);
trayItem.setImage(image); }
} });
}); }
public void inactiveIcon() {
isActive = false;
display.syncExec(new Runnable() {
public void run() {
trayItem.setImage(inactiveImage);
}
});
}
public boolean isActive() {
return isActive;
} }
public void displayMessage(final String message, final Priority priority) { public void displayMessage(final String message, final Priority priority) {
synchronized (LOCK) { if (trayItem != null) {
if (trayItem != null) { display.asyncExec(new Runnable() {
display.asyncExec( public void run() {
new Runnable() { int messageType = 0;
public void run() { if (priority == Priority.INFO) {
int messageType = 0; messageType = SWT.ICON_INFORMATION;
if (priority == Priority.INFO) { } else if (priority == Priority.WARN) {
messageType = SWT.ICON_INFORMATION; messageType = SWT.ICON_WARNING;
} else if (priority == Priority.WARN) { } else if (priority == Priority.ERROR) {
messageType = SWT.ICON_WARNING; messageType = SWT.ICON_ERROR;
} else if (priority == Priority.ERROR) { }
messageType = SWT.ICON_ERROR; if (messageType == 0) {
} trayItem.setToolTipText("DavMail gateway \n" + message);
if (messageType == 0) { } else {
trayItem.setToolTipText("DavMail gateway \n" + message); final ToolTip toolTip = new ToolTip(shell, SWT.BALLOON | messageType);
} else { toolTip.setText("DavMail gateway");
final ToolTip toolTip = new ToolTip(shell, SWT.BALLOON | messageType); toolTip.setMessage(message);
toolTip.setText("DavMail gateway"); trayItem.setToolTip(toolTip);
toolTip.setMessage(message); toolTip.setVisible(true);
trayItem.setToolTip(toolTip); }
toolTip.setVisible(true); }
} });
}
});
}
} }
}
/**
* Load image with current class loader.
*
* @param fileName image resource file name
* @return image
*/
public static Image loadSwtImage(String fileName) {
Image result = null;
try {
ClassLoader classloader = DavGatewayTray.class.getClassLoader();
URL imageUrl = classloader.getResource(fileName);
result = new Image(display, imageUrl.openStream());
} catch (IOException e) {
DavGatewayTray.warn("Unable to load image", e);
}
return result;
} }
public void init() { public void init() {
@ -123,22 +139,10 @@ public class SwtGatewayTray implements DavGatewayTrayInterface {
trayItem = new TrayItem(tray, SWT.NONE); trayItem = new TrayItem(tray, SWT.NONE);
trayItem.setToolTipText("DavMail gateway"); trayItem.setToolTipText("DavMail gateway");
// load an image awtImage = DavGatewayTray.loadImage("tray.png");
ClassLoader classloader = DavGatewayTray.class.getClassLoader(); image = loadSwtImage("tray.png");
try { image2 = loadSwtImage("tray2.png");
URL imageUrl = classloader.getResource("tray.png"); inactiveImage = loadSwtImage("trayinactive.png");
image = new Image(display, imageUrl.openStream());
awtImage = ImageIO.read(imageUrl);
} catch (IOException e) {
DavGatewayTray.warn("Unable to load image", e);
}
try {
URL imageUrl2 = classloader.getResource("tray2.png");
image2 = new Image(display, imageUrl2.openStream());
} catch (IOException e) {
DavGatewayTray.warn("Unable to load image", e);
}
trayItem.setImage(image); trayItem.setImage(image);

BIN
src/java/trayinactive.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 660 B