1 import java.util.Timer ; 2 import java.util.TimerTask ; 3 import org.jivesoftware.smack.packet.Presence; 4 5 7 public final class Idle{ 8 9 private static final int ONE_MINUTE=60000; private static long lastAction=0; 11 private static final Timer timer=new Timer (true); private static final Task task=new Task (); 13 private static long timeout; 14 private static boolean setToAway=false; private static String oldStatus=""; 16 17 private Idle(){ } 20 21 22 public static void init(){ 23 setTimeout(); 24 lastAction=System.currentTimeMillis(); 25 timer.scheduleAtFixedRate(task,0,ONE_MINUTE); 26 } 27 28 30 public static void setTimeout(){ 31 int to=10; 32 try{ 33 to=Integer.parseInt(WhisperIM.UserPref.getProperty("idle_time")); 34 } 35 catch(NumberFormatException nfe){ 36 to=10; 37 } 38 timeout=to*ONE_MINUTE; } 40 41 42 public static void action(){ 43 lastAction=System.currentTimeMillis(); 44 if(setToAway){ 45 setToAway=false; 46 javax.swing.SwingUtilities.invokeLater(new Runnable (){ 48 public void run(){ 49 if(WhisperIM.MainWindow.currentPresence.equals("away")){ 50 WhisperIM.MainWindow.currentPresence=oldStatus; 51 if(oldStatus.equals("online")){ 52 setStatus(Presence.Mode.AVAILABLE); 53 } 54 else{ 55 setStatus(Presence.Mode.CHAT); 56 } 57 } 58 } 59 }); 60 } 61 } 62 63 64 public static void stop(){ 65 timer.cancel(); 66 task.cancel(); 67 } 68 69 70 private static void doCheck(){ 71 long now=System.currentTimeMillis(); 72 if((now-lastAction)<=timeout){ 73 return; 74 } 75 javax.swing.SwingUtilities.invokeLater(new Runnable (){ 77 public void run(){ 78 if(WhisperIM.UserPref.getProperty("auto_away").equals("true") && (WhisperIM.MainWindow.currentPresence.equals("online") || WhisperIM.MainWindow.currentPresence.equals("available"))){ 79 setToAway=true; 80 oldStatus=WhisperIM.MainWindow.currentPresence; 81 WhisperIM.MainWindow.currentPresence="away"; 82 setStatus(Presence.Mode.AWAY); 83 } 84 } 85 }); 86 if(WhisperIM.UserPref.getProperty("clear_keypair").equals("true") && WhisperIM.KEYPAIR!=null){ 89 javax.swing.SwingUtilities.invokeLater(new Runnable (){ 90 public void run(){ 91 if(WhisperIM.MainWindow.secureChannelPanel!=null){ 92 WhisperIM.MainWindow.secureChannelPanel.doClose(); 93 } 94 WhisperIM.KEYPAIR.privateKey().wipe(); 95 whisper.Util.wipe(WhisperIM.MainWindow.keycachekey); 96 WhisperIM.KEYPAIR=null; 97 (new PassphraseDialog()).show(); 99 } 100 }); 101 } 102 } 103 104 106 private static void setStatus(Presence.Mode pm){ 107 if(WhisperIM.MainWindow.Conn!=null && WhisperIM.MainWindow.Conn.isConnected()){ 108 try{ 109 Presence p=new Presence(Presence.Type.AVAILABLE); 110 p.setFrom(WhisperIM.MainWindow.Conn.getUser()); 111 p.setMode(pm); 112 WhisperIM.MainWindow.Conn.sendPacket(p); 113 } 114 catch(Exception e){ 115 e.printStackTrace(); 116 return; 117 } 118 WhisperIM.MainWindow.setTitle("Whisper IM "+Lang.gs("status_"+WhisperIM.MainWindow.currentPresence)); 119 WhisperIM.MainWindow.myStatus.setToolTipText(Lang.gs("menu_status_tt")+Lang.gs("status_"+WhisperIM.MainWindow.currentPresence)); 120 WhisperIM.MainWindow.myStatus.setIcon(new javax.swing.ImageIcon ("img/status_"+WhisperIM.MainWindow.currentPresence+".png")); 121 } 122 } 123 124 final static class Task extends TimerTask { 125 public void run(){ 126 doCheck(); 127 } 128 } 129 } | Popular Tags |