[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.io.File; |
---|
| 32 | import java.io.FileNotFoundException; |
---|
| 33 | import java.io.FileOutputStream; |
---|
| 34 | import java.io.OutputStream; |
---|
| 35 | import java.text.MessageFormat; |
---|
| 36 | import java.util.ArrayList; |
---|
| 37 | import java.util.Iterator; |
---|
| 38 | |
---|
| 39 | import javax.swing.*; |
---|
| 40 | |
---|
| 41 | import nu.fw.jeti.images.StatusIcons; |
---|
| 42 | import nu.fw.jeti.jabber.Backend; |
---|
| 43 | import nu.fw.jeti.jabber.JIDStatus; |
---|
| 44 | import nu.fw.jeti.jabber.elements.*; |
---|
| 45 | import nu.fw.jeti.util.I18N; |
---|
| 46 | import nu.fw.jeti.util.Popups; |
---|
| 47 | import nu.fw.jeti.util.Preferences; |
---|
| 48 | import nu.fw.jeti.util.Utils; |
---|
| 49 | |
---|
| 50 | //29-okt-2004 |
---|
| 51 | |
---|
| 52 | public class GetFileWindow extends JFrame |
---|
| 53 | { |
---|
| 54 | private JProgressBar progressBar = new JProgressBar(); |
---|
| 55 | private JButton btnDownload = new JButton(); |
---|
| 56 | private JButton btnCancel = new JButton(); |
---|
| 57 | private Backend backend; |
---|
| 58 | private String streamMethod; |
---|
| 59 | private InfoQuery iq; |
---|
| 60 | private OutputStream out; |
---|
| 61 | private Timer timer; |
---|
| 62 | private StreamReceive streamReceive; |
---|
| 63 | private String fromName; |
---|
| 64 | private String fileName; |
---|
| 65 | private long fileSize; |
---|
| 66 | private String description; |
---|
| 67 | |
---|
| 68 | private int ibbMaxSize; |
---|
| 69 | |
---|
| 70 | public GetFileWindow(Backend backend,InfoQuery iq) |
---|
| 71 | { |
---|
| 72 | ibbMaxSize = Preferences.getInteger("filetransfer", "ibbMaxSize", |
---|
| 73 | 4096); |
---|
| 74 | |
---|
| 75 | this.iq=iq; |
---|
| 76 | this.backend = backend; |
---|
| 77 | if (init()) { |
---|
| 78 | jbInit(); |
---|
| 79 | pack(); |
---|
| 80 | setLocationRelativeTo(null); |
---|
| 81 | setVisible(true); |
---|
| 82 | } |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | private boolean init() |
---|
| 86 | { |
---|
| 87 | IQSi iqSi =(IQSi) iq.getIQExtension(); |
---|
| 88 | |
---|
| 89 | ArrayList streamOptions = new ArrayList(); |
---|
| 90 | XData data= iqSi.getXDataForm(); |
---|
| 91 | if(data.hasFields()) |
---|
| 92 | { |
---|
| 93 | for(Iterator i = data.getFields();i.hasNext();) |
---|
| 94 | { |
---|
| 95 | XDataField datafield= (XDataField)i.next(); |
---|
| 96 | if("stream-method".equals(datafield.getVar())) |
---|
| 97 | { |
---|
| 98 | if(datafield.hasOptions()) |
---|
| 99 | { |
---|
| 100 | for(Iterator j = datafield.getOptionsIterator();j.hasNext();) |
---|
| 101 | { |
---|
| 102 | streamOptions.add(((Object[])j.next())[1]); |
---|
| 103 | } |
---|
| 104 | } |
---|
| 105 | } |
---|
| 106 | } |
---|
| 107 | } |
---|
| 108 | String value=null; |
---|
| 109 | |
---|
| 110 | XSiFileTransfer sifi = iqSi.getSiprofile(); |
---|
| 111 | if(sifi!=null) |
---|
| 112 | {//TODO range |
---|
| 113 | if(streamOptions.contains("http://jabber.org/protocol/bytestreams") |
---|
| 114 | && Preferences.getBoolean("filetransfer", "bytestreams.enable", |
---|
| 115 | true)) { |
---|
| 116 | value="http://jabber.org/protocol/bytestreams"; |
---|
| 117 | } |
---|
| 118 | // select inband for files < ibbMaxSize or if bytestreams not possible |
---|
| 119 | if((value==null||(sifi.getSize()<ibbMaxSize)) |
---|
| 120 | && Preferences.getBoolean("filetransfer", "ibb.enable", true) |
---|
| 121 | && streamOptions.contains("http://jabber.org/protocol/ibb")) { |
---|
| 122 | value="http://jabber.org/protocol/ibb"; |
---|
| 123 | } |
---|
| 124 | if(value==null) { |
---|
| 125 | XMPPError error = new XMPPError("cancel",400); |
---|
| 126 | error.addError(new XMPPErrorTag("bad-request")); |
---|
| 127 | error.addError(new XMPPErrorTag("no-valid-streams","http://jabber.org/protocol/si")); |
---|
| 128 | backend.send(new InfoQuery(iq.getFrom(),iq.getID(),error)); |
---|
| 129 | return false; |
---|
| 130 | } |
---|
| 131 | streamMethod = value; |
---|
| 132 | |
---|
| 133 | JIDStatus j = backend.getJIDStatus(iq.getFrom()); |
---|
| 134 | if(j==null) { |
---|
| 135 | fromName = iq.getFrom().toString(); |
---|
| 136 | } else { |
---|
| 137 | fromName = j.getNick(); |
---|
| 138 | } |
---|
| 139 | fileName = sifi.getName(); |
---|
| 140 | fileSize = sifi.getSize(); |
---|
| 141 | progressBar.setMaximum((int)(fileSize/1024)); |
---|
| 142 | description = sifi.getDescription(); |
---|
| 143 | } |
---|
| 144 | return true; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | |
---|
| 148 | private void jbInit() |
---|
| 149 | { |
---|
| 150 | //setIconImage(StatusIcons.getImageIcon("jeti").getImage()); |
---|
| 151 | setTitle(I18N.gettext("filetransfer.File_Transfer")); |
---|
| 152 | |
---|
| 153 | getContentPane().setLayout(new GridBagLayout()); |
---|
| 154 | GridBagConstraints c = new GridBagConstraints(); |
---|
| 155 | c.gridwidth = 1; |
---|
| 156 | c.anchor = GridBagConstraints.LINE_START; |
---|
| 157 | c.fill = GridBagConstraints.BOTH; |
---|
| 158 | c.insets = new Insets(3, 5, 0, 3); |
---|
| 159 | GridBagConstraints c2 = (GridBagConstraints)c.clone(); |
---|
| 160 | c2.gridwidth = GridBagConstraints.REMAINDER; |
---|
| 161 | c2.weightx = 1.0; |
---|
| 162 | |
---|
| 163 | JLabel lbl = new JLabel(I18N.gettext("filetransfer.From")); |
---|
| 164 | getContentPane().add(lbl, c); |
---|
| 165 | lbl = new JLabel(fromName); |
---|
| 166 | getContentPane().add(lbl, c2); |
---|
| 167 | |
---|
| 168 | lbl = new JLabel(I18N.gettext("filetransfer.File_Name")); |
---|
| 169 | getContentPane().add(lbl, c); |
---|
| 170 | lbl = new JLabel(fileName); |
---|
| 171 | getContentPane().add(lbl, c2); |
---|
| 172 | |
---|
| 173 | lbl = new JLabel(I18N.gettext("filetransfer.File_Size")); |
---|
| 174 | getContentPane().add(lbl, c); |
---|
| 175 | lbl = new JLabel(Plugin.getSizeText(fileSize)); |
---|
| 176 | getContentPane().add(lbl, c2); |
---|
| 177 | |
---|
| 178 | if (description != null && description.length() > 0) { |
---|
| 179 | lbl = new JLabel(I18N.gettext("filetransfer.Description")); |
---|
| 180 | getContentPane().add(lbl, c); |
---|
| 181 | JTextArea d = new JTextArea(description); |
---|
| 182 | d.setEditable(false); |
---|
| 183 | getContentPane().add(new JScrollPane(d), c2); |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | c2.anchor = GridBagConstraints.CENTER; |
---|
| 187 | progressBar.setPreferredSize(new Dimension(300, 17)); |
---|
| 188 | getContentPane().add(progressBar, c2); |
---|
| 189 | |
---|
| 190 | c2.fill = GridBagConstraints.NONE; |
---|
| 191 | JPanel panel = new JPanel(); |
---|
| 192 | btnDownload.setText(I18N.gettext("filetransfer.Download")); |
---|
| 193 | getRootPane().setDefaultButton(btnDownload); |
---|
| 194 | btnDownload.addActionListener(new java.awt.event.ActionListener() { |
---|
| 195 | public void actionPerformed(ActionEvent e) { |
---|
| 196 | btnDownload_actionPerformed(e); |
---|
| 197 | } |
---|
| 198 | }); |
---|
| 199 | Action cancelAction = new AbstractAction(I18N.gettext("Cancel")) { |
---|
| 200 | public void actionPerformed(ActionEvent e) { |
---|
| 201 | btnCancel_actionPerformed(e); |
---|
| 202 | } |
---|
| 203 | }; |
---|
| 204 | Utils.addCancelButton(this, btnCancel, cancelAction); |
---|
| 205 | panel.add(btnDownload); |
---|
| 206 | panel.add(btnCancel); |
---|
| 207 | getContentPane().add(panel, c2); |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | void btnDownload_actionPerformed(ActionEvent e) |
---|
| 211 | { |
---|
| 212 | JFileChooser fileChooser = Plugin.getFileChooser(); |
---|
| 213 | fileChooser.setDialogTitle("Save " + fileName); |
---|
| 214 | String dir = Preferences.getString("filetransfer", "downloadDir",null); |
---|
| 215 | if(dir!=null)fileChooser.setCurrentDirectory(new File(dir)); |
---|
| 216 | fileChooser.setSelectedFile(new File(fileName)); |
---|
| 217 | int a = fileChooser.showSaveDialog(backend.getMainWindow()); |
---|
| 218 | if(a != JFileChooser.APPROVE_OPTION) return; |
---|
| 219 | File file = fileChooser.getSelectedFile(); |
---|
| 220 | Preferences.putString("filetransfer", "downloadDir",file.getParent()); |
---|
| 221 | |
---|
| 222 | if(file.exists()) |
---|
| 223 | { |
---|
| 224 | int opt =JOptionPane.showConfirmDialog(this,MessageFormat.format( |
---|
| 225 | I18N.gettext("filetransfer.{0}_already_exist,_overwrite?") |
---|
| 226 | , new String[]{file.toString()}) |
---|
| 227 | ,I18N.gettext("filetransfer.File_Transfer") |
---|
| 228 | ,JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE); |
---|
| 229 | if(opt == JOptionPane.NO_OPTION) return; |
---|
| 230 | } |
---|
| 231 | try{ |
---|
| 232 | out = new FileOutputStream(file); |
---|
| 233 | }catch(FileNotFoundException e2) |
---|
| 234 | { |
---|
| 235 | // timer.stop(); |
---|
| 236 | Popups.errorPopup(MessageFormat.format(I18N.gettext("filetransfer.{0}_could_not_be_openend_in_write_mode"), |
---|
| 237 | new Object[]{file.getAbsolutePath()}), |
---|
| 238 | I18N.gettext("filetransfer.File_Transfer")); |
---|
| 239 | return; |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | XDataBuilder xdb = new XDataBuilder(); |
---|
| 243 | xdb.type ="submit"; |
---|
| 244 | xdb.addField(new XDataField("stream-method",streamMethod)); |
---|
| 245 | XData xdata=null; |
---|
| 246 | try |
---|
| 247 | { |
---|
| 248 | xdata = xdb.build(); |
---|
| 249 | } catch (InstantiationException e1) |
---|
| 250 | { |
---|
| 251 | e1.printStackTrace(); |
---|
| 252 | } |
---|
| 253 | backend.send(new InfoQuery(iq.getFrom(),"result",iq.getID(),new IQSi(xdata,null))); |
---|
| 254 | IQSi iqSi =(IQSi) iq.getIQExtension(); |
---|
| 255 | Plugin.addGetFile(this,iq.getFrom(),iqSi.getId()); |
---|
| 256 | fileName = file.getName(); |
---|
| 257 | btnDownload.setEnabled(false); |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | void btnCancel_actionPerformed(ActionEvent e) |
---|
| 261 | { |
---|
| 262 | if(btnCancel.getText().equals(I18N.gettext("filetransfer.Close"))) |
---|
| 263 | { |
---|
| 264 | dispose(); |
---|
| 265 | } |
---|
| 266 | else if(streamReceive!=null) |
---|
| 267 | { |
---|
| 268 | timer.stop(); |
---|
| 269 | streamReceive.cancel(); |
---|
| 270 | } |
---|
| 271 | else |
---|
| 272 | { |
---|
| 273 | XMPPError error = new XMPPError("cancel",403); |
---|
| 274 | error.addError(new XMPPErrorTag("forbidden")); |
---|
| 275 | //TODO<text xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'>Offer Declined</text> |
---|
| 276 | backend.send(new InfoQuery(iq.getFrom(),iq.getID(),error)); |
---|
| 277 | } |
---|
| 278 | dispose(); |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | public OutputStream getOutputStream() |
---|
| 282 | { |
---|
| 283 | return out; |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | public StreamReceive getStreamReceive() |
---|
| 287 | { |
---|
| 288 | return streamReceive; |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | public void startDownloading(StreamReceive receive) |
---|
| 292 | { |
---|
| 293 | this.streamReceive = receive; |
---|
| 294 | initTimer(); |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | private void initTimer() |
---|
| 298 | { |
---|
| 299 | timer = new javax.swing.Timer(1000,new ActionListener() |
---|
| 300 | { |
---|
| 301 | public void actionPerformed(ActionEvent evt) |
---|
| 302 | { |
---|
| 303 | long bytes = streamReceive.getBytes(); |
---|
| 304 | progressBar.setValue((int)(bytes/1024)); |
---|
| 305 | int percent = (int) (((double)bytes/fileSize)*100); |
---|
| 306 | setTitle(percent + "% " + fileName); |
---|
| 307 | } |
---|
| 308 | } |
---|
| 309 | ); |
---|
| 310 | timer.start(); |
---|
| 311 | } |
---|
| 312 | |
---|
| 313 | public void stopDownloading() |
---|
| 314 | { |
---|
| 315 | if(Preferences.getBoolean("filetransfer", "closeOnComplete", false)) |
---|
| 316 | { |
---|
| 317 | dispose(); |
---|
| 318 | } |
---|
| 319 | else |
---|
| 320 | { |
---|
| 321 | btnCancel.setText(I18N.gettext("filetransfer.Close")); |
---|
| 322 | progressBar.setValue(progressBar.getMaximum()); |
---|
| 323 | setTitle("100% " + fileName); |
---|
| 324 | } |
---|
| 325 | timer.stop(); |
---|
| 326 | } |
---|
| 327 | } |
---|
| 328 | /* |
---|
| 329 | * Overrides for emacs |
---|
| 330 | * Local variables: |
---|
| 331 | * tab-width: 4 |
---|
| 332 | * End: |
---|
| 333 | */ |
---|