📄 gmailer4j.java
字号:
connect();
}else if (command == "Open Browser"){
openBrowser(GMConstants.GM_LNK_GMAIL);
}else if (command == "Logout") {
disconnect();
}else if (command == "Check Mail"){
fetchBox("Inbox", 0);
}else {
JOptionPane.showConfirmDialog(mainframe,
"Function not implemented.",
"Information",
JOptionPane.CLOSED_OPTION);
}
}
public void iconLeftDoubleClicked( SysTrayMenuEvent e ) {
// ignore event if main program is not loaded
if (!loaded) return;
// if the frame is not visible, show it
if( !mainframe.isVisible()) {
mainframe.setState(Frame.NORMAL);
mainframe.show();
}
mainframe.toFront();
if (aboutDlg.isVisible()) aboutDlg.toFront();
}
};
systrayCtrl.addSysTrayMenuListener(systrayAction);
configOption.addActionListener(getActionListener());
}
/**
*
*/
public static void openBrowser(String url) {
try {
Browser.displayURL(url);
} catch (IOException e) {
e.printStackTrace();
}
}
private void setupViews(){
aboutDlg.setModal(true);
if (conn.isConnected()){
setLogonStatus(GMailer4j.LOGON_ON);
}else{
setLogonStatus(GMailer4j.LOGON_OFF);
}
mainCtrl.expandTree();
updateLAF();
}
/**
* set the message of status bar and progress bar
* also log the emssage to logger
* @param shortMsg String
* @param longMsg String
*/
private void setMessage(final String shortMsg, final String longMsg){
mainCtrl.setMessages(shortMsg, longMsg);
logger.info(longMsg);
}
private void setupModels() throws NullPointerException, IOException {
// load models from data store if possible
try{
gminfo = (TreeMap) storage.get("info");
gmlabel = (TreeMap) storage.get("label");
}catch(ClassCastException cce){
logger.warning("Error loading storage, format unexpected ... " + cce);
}catch(IOException ie){
logger.info("Cannot open storage for info and/or labels");
}catch(RuntimeException re){
logger.warning("Unexpected error ... " + re);
}
try{
gmsearch = (TreeMap) storage.get("search");
mailboxModel.updateSearches(gmsearch);
mailboxModel.updateStdBox(gminfo);
mailboxModel.updateLabel(gmlabel);
}catch(ClassCastException cce){
logger.warning("Error loading storage, format unexpected ... " + cce);
}catch(IOException ie){
logger.info("Cannot open storage for searches");
}catch(RuntimeException re){
logger.warning("Unexpected error ... " + re);
}
try{
mailboxes = (HashMap) storage.get("mailboxes");
}catch(ClassCastException cce){
logger.warning("Error loading storage, format unexpected ... " + cce);
}catch(IOException ie){
logger.info("Cannot open storage for mailboxes ");
}catch(RuntimeException re){
logger.warning("Unexpected error ... " + re);
}
// if failed loading, initialize them with new variables
if (gminfo == null){
gminfo = new TreeMap();
}
if (gmlabel == null){
gmlabel = new TreeMap();
}
if (gmsearch == null){
gmsearch = new TreeMap();
}
if (mailboxes == null){
mailboxes = new HashMap();
}else{
threadsModel.setMessageThreads("Inbox", (ArrayList)mailboxes.get("Inbox"));
threadsModel.setCurrentBox("Inbox");
}
if (converation == null){
converation = new HashMap();
}
mailboxModel.updateStdBox(gminfo);
mailboxModel.updateLabel(gmlabel);
mainCtrl.setTreeModel(mailboxModel);
mainCtrl.setTableModel(threadsModel);
// setup the console
Interpreter interpreter =new Interpreter(mainCtrl.getConsole());
try{
interpreter.set("gmailer", this);
interpreter.set("connection", conn);
interpreter.set("gminfo", gminfo);
interpreter.set("gmlabel", gmlabel);
interpreter.set("gmsearch", gmsearch);
interpreter.set("storage", storage);
interpreter.set("pref", pref);
}catch(bsh.EvalError be){}
new Thread( interpreter ).start();
}
/**
* Shutdown the application
* Release all resources and shutdown gracefully
*/
private void shutdown(){
logger.info("Shutdown ...");
setMessage("Shutdown", "Shutdown in progress ... ");
// logout and close mail checker
final Runnable logoutTask = new Thread() {
public void run() {
try{
try{
mailChecker.interrupt();
}catch(Exception e){}
if (conn.isConnected()) {
setMessage("Shutdown", "Disconnecting ... ");
conn.disconnect();
}
// close data store
try {
setMessage("Shutdown", "Saving preference ... ");
pref.flush();
}
catch (java.util.prefs.BackingStoreException be) {
logger.info("Failed saving preference " + be);
}
try{
setMessage("Shutdown", "Saving mailboxes ... ");
storage.put("mailboxes", mailboxes);
storage.flush();
storage.close();
}catch(Exception e){}
// shutdown controllers
mainCtrl.dispose();
System.exit(0);
}
catch (Exception e) {
}
}
};
try{
ThreadUtils.execute(logoutTask);
}catch(InterruptedException ie){
logger.info("logout task interrupted! "+ ie);
}
// shutdown all threads in pool
ThreadUtils.getPool().shutdownAfterProcessingCurrentlyQueuedTasks();
}
/**
* connect to gmail
* checking user name, password and proxy setting before login
* @todo add error handler
*/
public void connect(){
JFrame frame = mainCtrl.getFrame();
// retrive preference
String username = pref.node("Accounts").get("username", "Username");
String password = pref.node("Accounts").get("password", "");
String pserver = pref.node("Proxy").get("server", "");
String pport = pref.node("Proxy").get("port", "");
boolean useProxy = pref.node("Proxy").getBoolean("enabled", false);
// add connection info
if (!username.equals("Username") && !password.equals("")){
conn.setUser(username);
conn.setPasswd(password);
}else{
// possibly error message
logger.warning("Username or password empty!");
JOptionPane.showMessageDialog(frame,
"Logon info is not entered, please enter it in Tools > Option > Account!",
"Missing information",
JOptionPane.WARNING_MESSAGE);
return;
}
// add proxy info
if (useProxy) {
if (pserver.equals("") || pport.equals("")){
// no valid input
logger.warning("Proxy server not setup correctly!");
return;
}else{
try{
// add port data in connection
conn.setProxy(pserver, Integer.parseInt(pport));
}catch(NumberFormatException ne){
// handle number format error
logger.warning("Proxy server not setup correctly!");
return;
}
}
}
Runnable co = new Runnable(){
public void run(){
Boolean connected;
GMResponse resp = null;
try{
setMessage("Connecting", "Login in progress ...");
systrayCtrl.setToolTip("Login in progress ...");
setLogonStatus(GMailer4j.LOGGING);
connected = new Boolean(conn.connect());
if (connected.equals(Boolean.TRUE)){
// refresh inbox and select it
setMessage("Online", "Downloading message list from Inbox ... ");
systrayCtrl.setToolTip("Conencted, checking mailbox ... ");
resp = conn.request(GMConstants.GM_REQ_STANDARD, "Inbox",
Integer.toString(0));
}
if (resp != null){
updateThreadsModel("Inbox", resp);
updateBoxModel("Inbox", resp);
setLogonStatus(GMailer4j.LOGON_ON);
}else{
// handle error
setMessage("Offline", "Logon failure.");
logger.info("Logon failure.");
setLogonStatus(GMailer4j.LOGON_OFF);
}
}catch(IllegalStateException ise){
setMessage("N/A", "IllegalStateException: " + ise.getMessage());
logger.warning("IllegalStateException: " + ise.getMessage());
setLogonStatus(GMailer4j.LOGON_OFF);
}catch (ParsePacketException ppe) {
setMessage("Unknow Format", "Cannot parse mail: " + ppe.getMessage());
logger.warning("Cannot parse mail: " + ppe.getMessage());
setLogonStatus(GMailer4j.LOGON_OFF);
JOptionPane.showMessageDialog(null,
"Cannot login to gmail! GMail is not responding or protocol changed!",
"Login Failed",
JOptionPane.ERROR_MESSAGE);
}catch (IOException ie) {
setMessage("N/A", "Connection Failure: " + ie.getCause());
logger.warning("Connection Failure: " + ie.getCause());
setLogonStatus(GMailer4j.LOGON_OFF);
JOptionPane.showMessageDialog(null,
"Cannot login to gmail! Please check your internet connection and try again later.",
"Login Failed",
JOptionPane.ERROR_MESSAGE);
}
}
};
try{
ThreadUtils.execute(co);
}catch(InterruptedException ie){
setMessage("Interrupted", "Connection Interrupted!");
setLogonStatus(GMailer4j.LOGON_OFF);
}
}
/**
* disconnect from gmail
* @todo add error handler
*/
public void disconnect(){
Runnable co = new Runnable(){
public void run(){
logger.info("Logging out ...");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -