[3030] | 1 | /* |
---|
| 2 | * Jeti, a Java Jabber client, Copyright (C) 2004 E.S. de Boer |
---|
| 3 | * |
---|
| 4 | * This program is free software; you can redistribute it and/or modify |
---|
| 5 | * it under the terms of the GNU General Public License as published by |
---|
| 6 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | * (at your option) any later version. |
---|
| 8 | * |
---|
| 9 | * This program is distributed in the hope that it will be useful, |
---|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 12 | * GNU General Public License for more details. |
---|
| 13 | * |
---|
| 14 | * You should have received a copy of the GNU General Public License |
---|
| 15 | * along with this program; if not, write to the Free Software |
---|
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 17 | * |
---|
| 18 | * For questions, comments etc, |
---|
| 19 | * use the website at http://jeti.jabberstudio.org |
---|
| 20 | * or mail me at eric@jeti.tk |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | package nu.fw.jeti.plugins.filetransfer; |
---|
| 24 | |
---|
| 25 | import java.awt.Dimension; |
---|
| 26 | import java.awt.GridBagConstraints; |
---|
| 27 | import java.awt.GridBagLayout; |
---|
| 28 | import java.awt.Insets; |
---|
| 29 | import java.awt.event.ActionEvent; |
---|
| 30 | import java.awt.event.ActionListener; |
---|
| 31 | import java.awt.event.KeyEvent; |
---|
| 32 | import java.io.File; |
---|
| 33 | import java.text.MessageFormat; |
---|
| 34 | import java.util.Iterator; |
---|
| 35 | |
---|
| 36 | import javax.swing.AbstractAction; |
---|
| 37 | import javax.swing.Action; |
---|
| 38 | import javax.swing.JButton; |
---|
| 39 | import javax.swing.JComponent; |
---|
| 40 | import javax.swing.JFrame; |
---|
| 41 | import javax.swing.JLabel; |
---|
| 42 | import javax.swing.JLayeredPane; |
---|
| 43 | import javax.swing.JPanel; |
---|
| 44 | import javax.swing.JProgressBar; |
---|
| 45 | import javax.swing.KeyStroke; |
---|
| 46 | import javax.swing.SwingConstants; |
---|
| 47 | |
---|
| 48 | import nu.fw.jeti.backend.roster.Roster; |
---|
| 49 | import nu.fw.jeti.events.ErrorListener; |
---|
| 50 | import nu.fw.jeti.events.IQResultListener; |
---|
| 51 | import nu.fw.jeti.jabber.Backend; |
---|
| 52 | import nu.fw.jeti.jabber.JID; |
---|
| 53 | import nu.fw.jeti.jabber.JIDStatus; |
---|
| 54 | import nu.fw.jeti.jabber.elements.InfoQuery; |
---|
| 55 | import nu.fw.jeti.jabber.elements.XData; |
---|
| 56 | import nu.fw.jeti.jabber.elements.XDataBuilder; |
---|
| 57 | import nu.fw.jeti.jabber.elements.XDataField; |
---|
| 58 | import nu.fw.jeti.jabber.elements.XDataFieldBuilder; |
---|
| 59 | import nu.fw.jeti.plugins.filetransfer.ibb.IBBSend; |
---|
| 60 | import nu.fw.jeti.plugins.filetransfer.socks5.Socks5Send; |
---|
| 61 | import nu.fw.jeti.util.I18N; |
---|
| 62 | import nu.fw.jeti.util.Popups; |
---|
| 63 | import nu.fw.jeti.util.Preferences; |
---|
| 64 | |
---|
| 65 | //24-0kt-2004 |
---|
| 66 | public class SendFileProgress extends JFrame implements IQResultListener |
---|
| 67 | { |
---|
| 68 | private Backend backend; |
---|
| 69 | private JID to; |
---|
| 70 | private StreamSend streamSend; |
---|
| 71 | private javax.swing.Timer timer; |
---|
| 72 | private String id; |
---|
| 73 | private File file; |
---|
| 74 | private boolean begun = false; |
---|
| 75 | |
---|
| 76 | private JButton btnAbort = new JButton(); |
---|
| 77 | private JLabel toTxt = new JLabel(); |
---|
| 78 | private JLabel fileTxt = new JLabel(); |
---|
| 79 | private JLabel sizeTxt = new JLabel(); |
---|
| 80 | private JLabel statusTxt = new JLabel(); |
---|
| 81 | private JProgressBar progressBar = new JProgressBar(); |
---|
| 82 | |
---|
| 83 | public SendFileProgress(Backend backend, JID to, File file, |
---|
| 84 | String description) { |
---|
| 85 | this.backend = backend; |
---|
| 86 | this.to = to; |
---|
| 87 | this.file = file; |
---|
| 88 | try { |
---|
| 89 | jbInit(); |
---|
| 90 | } catch(Exception e) { |
---|
| 91 | e.printStackTrace(); |
---|
| 92 | } |
---|
| 93 | JIDStatus js = Roster.getJIDStatus(to); |
---|
| 94 | if(js!=null)toTxt.setText(js.getNick()); |
---|
| 95 | else toTxt.setText(to.toString()); |
---|
| 96 | |
---|
| 97 | fileTxt.setText(file.toString()); |
---|
| 98 | sizeTxt.setText(Plugin.getSizeText(file.length())); |
---|
| 99 | statusTxt.setText(I18N.gettext("filetransfer.Opening_connection") + "..."); |
---|
| 100 | |
---|
| 101 | pack(); |
---|
| 102 | setLocationRelativeTo(null); |
---|
| 103 | |
---|
| 104 | progressBar.setMaximum((int)(file.length()/1024)); |
---|
| 105 | |
---|
| 106 | id = "file"+backend.getIdentifier(); |
---|
| 107 | XDataBuilder xdb = new XDataBuilder(); |
---|
| 108 | xdb.type ="form"; |
---|
| 109 | XDataFieldBuilder xdfb = new XDataFieldBuilder(); |
---|
| 110 | xdfb.var="stream-method"; |
---|
| 111 | xdfb.type="list-single"; |
---|
| 112 | |
---|
| 113 | if (Preferences.getBoolean("filetransfer","bytestreams.enable",true)) |
---|
| 114 | { |
---|
| 115 | xdfb.addOption("http://jabber.org/protocol/bytestreams"); |
---|
| 116 | } |
---|
| 117 | if (Preferences.getBoolean("filetransfer", "ibb.enable", true)) |
---|
| 118 | { |
---|
| 119 | xdfb.addOption("http://jabber.org/protocol/ibb"); |
---|
| 120 | } |
---|
| 121 | xdb.addField(xdfb.build()); |
---|
| 122 | XData xdata=null; |
---|
| 123 | try { |
---|
| 124 | xdata = xdb.build(); |
---|
| 125 | } catch (InstantiationException e1) { |
---|
| 126 | e1.printStackTrace(); |
---|
| 127 | } |
---|
| 128 | IQSi si = new IQSi( |
---|
| 129 | id, "http://jabber.org/protocol/si/profile/file-transfer",null, |
---|
| 130 | xdata, |
---|
| 131 | new XSiFileTransfer(file.getName(), null, null, file.length(), |
---|
| 132 | description, 0, 0)); |
---|
| 133 | backend.send( |
---|
| 134 | new InfoQuery(to,"set","filetransfer"+backend.getIdentifier(),si), |
---|
| 135 | this, 0); |
---|
| 136 | |
---|
| 137 | backend.addListener(ErrorListener.class,this); |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | private void jbInit() throws Exception |
---|
| 141 | { |
---|
| 142 | setIconImage( |
---|
| 143 | nu.fw.jeti.images.StatusIcons.getImageIcon("jeti").getImage()); |
---|
| 144 | setTitle(I18N.gettext("filetransfer.File_Transfer")); |
---|
| 145 | |
---|
| 146 | getContentPane().setLayout(new GridBagLayout()); |
---|
| 147 | GridBagConstraints c = new GridBagConstraints(); |
---|
| 148 | c.gridwidth = 1; |
---|
| 149 | c.anchor = GridBagConstraints.LINE_START; |
---|
| 150 | c.fill = GridBagConstraints.BOTH; |
---|
| 151 | c.insets = new Insets(3, 5, 0, 3); |
---|
| 152 | GridBagConstraints c2 = (GridBagConstraints)c.clone(); |
---|
| 153 | c2.gridwidth = GridBagConstraints.REMAINDER; |
---|
| 154 | c2.weightx = 1.0; |
---|
| 155 | |
---|
| 156 | JLabel lbl = new JLabel(I18N.gettext("filetransfer.To")); |
---|
| 157 | getContentPane().add(lbl, c); |
---|
| 158 | getContentPane().add(toTxt, c2); |
---|
| 159 | |
---|
| 160 | lbl = new JLabel(I18N.gettext("filetransfer.File_Name")); |
---|
| 161 | getContentPane().add(lbl, c); |
---|
| 162 | getContentPane().add(fileTxt, c2); |
---|
| 163 | |
---|
| 164 | lbl = new JLabel(I18N.gettext("filetransfer.File_Size")); |
---|
| 165 | getContentPane().add(lbl, c); |
---|
| 166 | getContentPane().add(sizeTxt, c2); |
---|
| 167 | |
---|
| 168 | c.gridwidth = GridBagConstraints.REMAINDER; |
---|
| 169 | c.insets = new Insets(5, 5, 0, 5); |
---|
| 170 | c.anchor = GridBagConstraints.CENTER; |
---|
| 171 | c.weightx = 1.0; |
---|
| 172 | c.fill = GridBagConstraints.NONE; |
---|
| 173 | getContentPane().add(statusTxt, c); |
---|
| 174 | |
---|
| 175 | progressBar.setPreferredSize(new Dimension(300, 17)); |
---|
| 176 | c.fill = GridBagConstraints.BOTH; |
---|
| 177 | c.insets = new Insets(0, 5, 0, 5); |
---|
| 178 | getContentPane().add(progressBar, c); |
---|
| 179 | |
---|
| 180 | getRootPane().setDefaultButton(btnAbort); |
---|
| 181 | c.fill = GridBagConstraints.NONE; |
---|
| 182 | c.insets = new Insets(5, 5, 5, 5); |
---|
| 183 | getContentPane().add(btnAbort, c); |
---|
| 184 | |
---|
| 185 | Action abortAction = |
---|
| 186 | new AbstractAction(I18N.gettext("filetransfer.Abort")) { |
---|
| 187 | public void actionPerformed(ActionEvent e) { |
---|
| 188 | btnAbort_actionPerformed(e); |
---|
| 189 | } |
---|
| 190 | }; |
---|
| 191 | btnAbort.setAction(abortAction); |
---|
| 192 | |
---|
| 193 | KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0); |
---|
| 194 | JLayeredPane layeredPane = getLayeredPane(); |
---|
| 195 | layeredPane.getActionMap().put("cancel", abortAction); |
---|
| 196 | layeredPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(stroke, "abort"); |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | void btnAbort_actionPerformed(ActionEvent e) |
---|
| 200 | { |
---|
| 201 | if(streamSend!=null) |
---|
| 202 | { |
---|
| 203 | timer.stop(); |
---|
| 204 | streamSend.cancel(); |
---|
| 205 | } |
---|
| 206 | this.dispose(); |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | public void iqResult(InfoQuery query) |
---|
| 210 | { |
---|
| 211 | |
---|
| 212 | if(query.getType().equals("result")) |
---|
| 213 | { |
---|
| 214 | IQSi iqSi =(IQSi) query.getIQExtension(); |
---|
| 215 | XSiFileTransfer sifi = iqSi.getSiprofile(); |
---|
| 216 | if( sifi != null ) |
---|
| 217 | {//TODO range |
---|
| 218 | |
---|
| 219 | } |
---|
| 220 | String streamMethod=null; |
---|
| 221 | XData data= iqSi.getXDataForm(); |
---|
| 222 | if(data.hasFields()) |
---|
| 223 | { |
---|
| 224 | for(Iterator i = data.getFields();i.hasNext();) |
---|
| 225 | { |
---|
| 226 | XDataField datafield= (XDataField)i.next(); |
---|
| 227 | if("stream-method".equals(datafield.getVar())) |
---|
| 228 | { |
---|
| 229 | streamMethod = datafield.getValue(); |
---|
| 230 | } |
---|
| 231 | } |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | /** |
---|
| 235 | * @Author(s): Alexandre Correia |
---|
| 236 | * @Date : 10/03/2009 |
---|
| 237 | * @Brief : Modificado para o funcionamento dentro do applet |
---|
| 238 | */ |
---|
| 239 | |
---|
| 240 | if("http://jabber.org/protocol/ibb".equals(streamMethod) || "http://jabber.org/protocol/bytestreams".equals(streamMethod)) |
---|
| 241 | { |
---|
| 242 | streamSend = new Socks5Send(file,backend,this,to,id); |
---|
| 243 | } |
---|
| 244 | else |
---|
| 245 | { |
---|
| 246 | Popups.messagePopup(I18N.gettext("filetransfer.Problem_during_file_transfer,_transfer_aborted"),I18N.gettext("filetransfer.File_Transfer")); |
---|
| 247 | this.dispose(); |
---|
| 248 | return; |
---|
| 249 | } |
---|
| 250 | initTimer(); |
---|
| 251 | } |
---|
| 252 | else if(query.getType().equals("error")) |
---|
| 253 | { |
---|
| 254 | //FIXME jidstatus can be null (groupchat) |
---|
| 255 | String msg; |
---|
| 256 | if( query.getErrorCode()== 403 ) |
---|
| 257 | { |
---|
| 258 | msg = MessageFormat.format( |
---|
| 259 | I18N.gettext("filetransfer.{0}_did_not_accept_the_file"), |
---|
| 260 | new Object[] { Roster.getJIDStatus(to).getNick() }); |
---|
| 261 | } |
---|
| 262 | else if ( query.getErrorCode() == 404 ) |
---|
| 263 | { |
---|
| 264 | msg = MessageFormat.format( |
---|
| 265 | I18N.gettext("filetransfer.{0}_is_not_online"), |
---|
| 266 | new Object[] { Roster.getJIDStatus(to).getNick() }); |
---|
| 267 | } |
---|
| 268 | else if ( query.getErrorCode()== 400 ) |
---|
| 269 | { |
---|
| 270 | msg = I18N.gettext("filetransfer.No_compatible_transfer_protocols_found"); |
---|
| 271 | } |
---|
| 272 | else if (query.getErrorCode()== 501) |
---|
| 273 | { |
---|
| 274 | msg = MessageFormat.format( |
---|
| 275 | I18N.gettext("filetransfer.{0}_does_not_support_file_transfer_or_is_offline"), |
---|
| 276 | new Object[] { Roster.getJIDStatus(to).getNick() }); |
---|
| 277 | } |
---|
| 278 | else |
---|
| 279 | { |
---|
| 280 | msg = I18N.gettext("filetransfer.Problem_during_file_transfer,_transfer_aborted"); |
---|
| 281 | } |
---|
| 282 | Popups.messagePopup(msg, file.getName()); |
---|
| 283 | this.dispose(); |
---|
| 284 | } |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | private void updateProgress() { |
---|
| 288 | int bytes=0; |
---|
| 289 | bytes = (int)streamSend.getBytes()/1024; |
---|
| 290 | if (bytes >0 && !begun) { |
---|
| 291 | statusTxt.setText(I18N.gettext("filetransfer.Transferring") + "..."); |
---|
| 292 | begun = true; |
---|
| 293 | } |
---|
| 294 | progressBar.setValue(bytes); |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | private void initTimer() |
---|
| 298 | { |
---|
| 299 | timer = new javax.swing.Timer(1000,new ActionListener() { |
---|
| 300 | public void actionPerformed(ActionEvent evt) { |
---|
| 301 | updateProgress(); |
---|
| 302 | } |
---|
| 303 | }); |
---|
| 304 | timer.start(); |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | public void stop() |
---|
| 308 | { |
---|
| 309 | timer.stop(); |
---|
| 310 | dispose(); |
---|
| 311 | } |
---|
| 312 | |
---|
| 313 | public void done() |
---|
| 314 | { |
---|
| 315 | timer.stop(); |
---|
| 316 | updateProgress(); |
---|
| 317 | streamSend = null; |
---|
| 318 | if(Preferences.getBoolean("filetransfer", "closeOnComplete", false)) |
---|
| 319 | { |
---|
| 320 | dispose(); |
---|
| 321 | } |
---|
| 322 | else |
---|
| 323 | { |
---|
| 324 | statusTxt.setText(I18N.gettext("filetransfer.Transfer_complete")); |
---|
| 325 | btnAbort.setText(I18N.gettext("filetransfer.Close")); |
---|
| 326 | progressBar.setValue(progressBar.getMaximum()); |
---|
| 327 | } |
---|
| 328 | } |
---|
| 329 | |
---|
| 330 | } |
---|
| 331 | /* |
---|
| 332 | * Overrides for emacs |
---|
| 333 | * Local variables: |
---|
| 334 | * tab-width: 4 |
---|
| 335 | * End: |
---|
| 336 | */ |
---|