[3102] | 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 | package nu.fw.jeti.plugins.filetransfer.ibb; |
---|
| 23 | |
---|
| 24 | import java.io.File; |
---|
| 25 | import java.io.FileInputStream; |
---|
| 26 | import java.io.IOException; |
---|
| 27 | import java.io.InputStream; |
---|
| 28 | |
---|
| 29 | import nu.fw.jeti.events.IQResultListener; |
---|
| 30 | import nu.fw.jeti.jabber.Backend; |
---|
| 31 | import nu.fw.jeti.jabber.JID; |
---|
| 32 | import nu.fw.jeti.jabber.elements.InfoQuery; |
---|
| 33 | import nu.fw.jeti.jabber.elements.Message; |
---|
| 34 | import nu.fw.jeti.plugins.filetransfer.StreamSend; |
---|
| 35 | import nu.fw.jeti.plugins.filetransfer.SendFileProgress; |
---|
| 36 | import nu.fw.jeti.util.Base64; |
---|
| 37 | import nu.fw.jeti.util.I18N; |
---|
| 38 | import nu.fw.jeti.util.Popups; |
---|
| 39 | |
---|
| 40 | public class IBBSend extends Thread implements StreamSend |
---|
| 41 | { |
---|
| 42 | final static int BUF_SIZE = 4096; |
---|
| 43 | private File file; |
---|
| 44 | |
---|
| 45 | private SendFileProgress window; |
---|
| 46 | private long bytes; |
---|
| 47 | private int sequence; |
---|
| 48 | private Backend backend; |
---|
| 49 | private JID to; |
---|
| 50 | private String sid; |
---|
| 51 | /* buffer to use for requests */ |
---|
| 52 | byte[] buf; |
---|
| 53 | |
---|
| 54 | public IBBSend(File file,Backend backend,SendFileProgress ws,JID to,String id) { |
---|
| 55 | buf = new byte[BUF_SIZE]; |
---|
| 56 | this.file = file; |
---|
| 57 | this.backend = backend; |
---|
| 58 | window =ws; |
---|
| 59 | this.to = to; |
---|
| 60 | sid = id; |
---|
| 61 | setPriority(NORM_PRIORITY); |
---|
| 62 | backend.send(new InfoQuery(to,"set","ibb" + backend.getIdentifier(), new IBBExtension(sid,BUF_SIZE)),new IQResultListener() |
---|
| 63 | { |
---|
| 64 | public void iqResult(InfoQuery iq) |
---|
| 65 | { |
---|
| 66 | if(iq.getType().equals("result")) |
---|
| 67 | { |
---|
| 68 | start(); |
---|
| 69 | } |
---|
| 70 | else |
---|
| 71 | {//TODO add refuse error |
---|
| 72 | Popups.messagePopup(I18N.gettext("filetransfer.Problem_during_file_transfer,_transfer_aborted"),I18N.gettext("filetransfer.File_Transfer")); |
---|
| 73 | window.stop(); |
---|
| 74 | } |
---|
| 75 | } |
---|
| 76 | },0); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | public void run() |
---|
| 80 | { |
---|
| 81 | InputStream is = null; |
---|
| 82 | try { |
---|
| 83 | is = new FileInputStream(file.getAbsolutePath()); |
---|
| 84 | for(int readed = is.read(buf);readed > 0;readed =is.read(buf)) |
---|
| 85 | { |
---|
| 86 | String encoded = Base64.encodeBytes(buf,0,readed); |
---|
| 87 | backend.send(new Message(null,to,new IBBExtension(sid,sequence,encoded))); |
---|
| 88 | //TODO add message resultlistener instead of iqresultlistener |
---|
| 89 | sequence++; |
---|
| 90 | bytes+=readed; |
---|
| 91 | if (Thread.interrupted()) |
---|
| 92 | { |
---|
| 93 | sendStreamClose(); |
---|
| 94 | is.close(); |
---|
| 95 | window.stop(); |
---|
| 96 | return; |
---|
| 97 | } |
---|
| 98 | Thread.yield(); |
---|
| 99 | } |
---|
| 100 | backend.send(new InfoQuery(to,"set",new IBBExtension(sid))); |
---|
| 101 | }catch(IOException e) |
---|
| 102 | { |
---|
| 103 | sendStreamClose(); |
---|
| 104 | Popups.messagePopup(I18N.gettext("filetransfer.Problem_during_file_transfer,_transfer_aborted"),I18N.gettext("filetransfer.File_Transfer")); |
---|
| 105 | window.stop(); |
---|
| 106 | } |
---|
| 107 | finally { |
---|
| 108 | try{ |
---|
| 109 | is.close(); |
---|
| 110 | }catch(IOException e){e.printStackTrace();} |
---|
| 111 | } |
---|
| 112 | window.done(); |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | private void sendStreamClose() |
---|
| 116 | { |
---|
| 117 | backend.send(new InfoQuery(to,"set","ibb" + backend.getIdentifier(), new IBBExtension(sid))); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | public long getBytes() |
---|
| 121 | { |
---|
| 122 | return bytes; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | public void cancel() |
---|
| 126 | { |
---|
| 127 | interrupt(); |
---|
| 128 | } |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | |
---|
| 132 | |
---|
| 133 | |
---|
| 134 | |
---|
| 135 | /* |
---|
| 136 | * Overrides for emacs |
---|
| 137 | * Local variables: |
---|
| 138 | * tab-width: 4 |
---|
| 139 | * End: |
---|
| 140 | */ |
---|