[3952] | 1 | package nu.fw.jeti.jabber.elements; |
---|
| 2 | |
---|
| 3 | import java.text.DateFormat; |
---|
| 4 | import java.text.MessageFormat; |
---|
| 5 | import java.text.ParseException; |
---|
| 6 | import java.util.Calendar; |
---|
| 7 | import java.util.Date; |
---|
| 8 | import java.util.Locale; |
---|
| 9 | import java.util.TimeZone; |
---|
| 10 | |
---|
| 11 | import javax.swing.JOptionPane; |
---|
| 12 | |
---|
| 13 | import nu.fw.jeti.jabber.Backend; |
---|
| 14 | import nu.fw.jeti.jabber.JID; |
---|
| 15 | import nu.fw.jeti.util.I18N; |
---|
| 16 | import nu.fw.jeti.util.Popups; |
---|
| 17 | import nu.fw.jeti.util.Preferences; |
---|
| 18 | |
---|
| 19 | /** |
---|
| 20 | * @author E.S. de Boer |
---|
| 21 | * @version 1.0 |
---|
| 22 | */ |
---|
| 23 | |
---|
| 24 | public class IQTime extends Extension implements IQExtension |
---|
| 25 | { |
---|
| 26 | private String tz; |
---|
| 27 | private String utc; |
---|
| 28 | private String display; |
---|
| 29 | |
---|
| 30 | public IQTime(){} |
---|
| 31 | |
---|
| 32 | public IQTime(String utc, String tz,String display) |
---|
| 33 | { |
---|
| 34 | this.tz = tz; |
---|
| 35 | this.utc = utc; |
---|
| 36 | this.display = display; |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | public String getTZ(){return tz;} |
---|
| 40 | |
---|
| 41 | public String getUTC(){return utc;} |
---|
| 42 | |
---|
| 43 | public String getDisplay(){return display;} |
---|
| 44 | |
---|
| 45 | public void execute(InfoQuery iq,Backend backend) |
---|
| 46 | { |
---|
| 47 | String type = iq.getType(); |
---|
| 48 | if (type.equals("get")) { |
---|
| 49 | if(Preferences.getBoolean("jeti", "privacy", false)) return; |
---|
| 50 | sendTime(iq.getFrom(), iq.getID(),backend); |
---|
| 51 | } else if (type.equals("result")) { |
---|
| 52 | timePopup(iq.getFrom().toString(), getDisplay(), |
---|
| 53 | getUTC(),getTZ()); |
---|
| 54 | } else if (type.equals("error")) { |
---|
| 55 | Popups.errorPopup(iq.getErrorDescription(), I18N.gettext("main.error.Time_Error")); |
---|
| 56 | } |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | private void timePopup(String jid, String display, String time, |
---|
| 60 | String timezone) { |
---|
| 61 | String info = MessageFormat.format( |
---|
| 62 | I18N.gettext("main.popup.Local_time_for_{0}_is"), |
---|
| 63 | new Object[]{jid}); |
---|
| 64 | String timeFormatted = null; |
---|
| 65 | if (time.length() > 0 && timezone.length() > 0) { |
---|
| 66 | try { |
---|
| 67 | DateFormat peerFormat = |
---|
| 68 | new java.text.SimpleDateFormat("yyyyMMdd'T'HH:mm:ss"); |
---|
| 69 | peerFormat.setTimeZone(TimeZone.getTimeZone("UTC")); |
---|
| 70 | Date peerDate = peerFormat.parse(time); |
---|
| 71 | DateFormat localFormat = DateFormat.getDateTimeInstance(); |
---|
| 72 | localFormat.setTimeZone(TimeZone.getTimeZone(timezone)); |
---|
| 73 | timeFormatted = localFormat.format(peerDate); |
---|
| 74 | } catch (ParseException e) { |
---|
| 75 | // Do Nothing |
---|
| 76 | } |
---|
| 77 | } |
---|
| 78 | if (timeFormatted == null) { |
---|
| 79 | timeFormatted = display; |
---|
| 80 | } |
---|
| 81 | if (timezone.length() > 0) { |
---|
| 82 | timeFormatted += "\n" + |
---|
| 83 | MessageFormat.format( |
---|
| 84 | I18N.gettext("main.popup.{0}_is_in_timezone_{1}"), |
---|
| 85 | new Object[]{jid,timezone}); |
---|
| 86 | } |
---|
| 87 | Popups.popup(info + "\n" + timeFormatted, |
---|
| 88 | jid + ' ' + I18N.gettext("main.popup.Time"), |
---|
| 89 | JOptionPane.INFORMATION_MESSAGE); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | private void sendTime(JID to, String id,Backend backend) |
---|
| 93 | { |
---|
| 94 | Date date = new Date(); |
---|
| 95 | DateFormat dateFormat = |
---|
| 96 | new java.text.SimpleDateFormat("yyyyMMdd'T'HH:mm:ss"); |
---|
| 97 | TimeZone tz = dateFormat.getTimeZone(); |
---|
| 98 | String tzName = dateFormat.getTimeZone().getDisplayName( |
---|
| 99 | tz.inDaylightTime(date), TimeZone.SHORT); |
---|
| 100 | dateFormat.setCalendar( |
---|
| 101 | Calendar.getInstance(TimeZone.getTimeZone("UTC"))); |
---|
| 102 | IQTime time = |
---|
| 103 | new IQTime(dateFormat.format(date), tzName, date.toString()); |
---|
| 104 | backend.send(new InfoQuery(to,"result",id,time)); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | public void appendToXML(StringBuffer xml) |
---|
| 108 | { |
---|
| 109 | xml.append("<query xmlns=\"jabber:iq:time\""); |
---|
| 110 | if(tz == null && utc == null && display ==null) |
---|
| 111 | { //short cut |
---|
| 112 | xml.append("/>"); |
---|
| 113 | return; |
---|
| 114 | } |
---|
| 115 | xml.append('>'); |
---|
| 116 | appendElement(xml,"utc",utc); |
---|
| 117 | appendElement(xml,"tz",tz); |
---|
| 118 | appendElement(xml,"display",display); |
---|
| 119 | xml.append("</query>"); |
---|
| 120 | } |
---|
| 121 | } |
---|
| 122 | /* |
---|
| 123 | * Overrides for emacs |
---|
| 124 | * Local variables: |
---|
| 125 | * tab-width: 4 |
---|
| 126 | * End: |
---|
| 127 | */ |
---|