source: trunk/jabberit_messenger/java_source/src/nu/fw/jeti/plugins/filetransfer/Plugin.java @ 1001

Revision 1001, 10.5 KB checked in by alexandrecorreia, 15 years ago (diff)

Ticket #552 - Inclusão do projeto Java referente ao applet do módulo.

Line 
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 jeti@jabber.org
21 */
22
23package nu.fw.jeti.plugins.filetransfer;
24
25import java.awt.datatransfer.DataFlavor;
26import java.awt.datatransfer.Transferable;
27import java.awt.datatransfer.UnsupportedFlavorException;
28import java.io.File;
29import java.io.IOException;
30import java.net.Inet4Address;
31import java.net.InetAddress;
32import java.net.NetworkInterface;
33import java.net.SocketException;
34import java.security.MessageDigest;
35import java.security.NoSuchAlgorithmException;
36import java.text.NumberFormat;
37import java.util.*;
38
39import javax.swing.JComponent;
40import javax.swing.JFileChooser;
41import javax.swing.JTree;
42import javax.swing.TransferHandler;
43
44import nu.fw.jeti.backend.roster.PrimaryJIDStatus;
45import nu.fw.jeti.jabber.Backend;
46import nu.fw.jeti.jabber.JID;
47import nu.fw.jeti.jabber.JIDStatus;
48import nu.fw.jeti.jabber.elements.Extension;
49import nu.fw.jeti.jabber.elements.XData;
50import nu.fw.jeti.jabber.handlers.ExtensionHandler;
51import nu.fw.jeti.plugins.Plugins;
52import nu.fw.jeti.plugins.RosterMenuListener;
53import nu.fw.jeti.plugins.filetransfer.ibb.IBBHandler;
54import nu.fw.jeti.plugins.filetransfer.socks5.Socks5Handler;
55import nu.fw.jeti.plugins.filetransfer.socks5.Socks5Send;
56import nu.fw.jeti.util.I18N;
57import nu.fw.jeti.util.Popups;
58import nu.fw.jeti.util.Preferences;
59import nu.fw.jeti.util.Utils;
60
61import org.xml.sax.Attributes;
62
63/**
64 * @author E.S. de Boer
65 
66 */
67//Created on 20-okt-2004
68public class Plugin implements Plugins
69{
70        private static Backend backend;
71        private static Plugin plugin;
72    private static JFileChooser fileChooser = new JFileChooser();
73        private Map fileWindows = new HashMap(10);
74        public final static String VERSION = "0.1";
75        public final static String DESCRIPTION = "filetransfer.File_Transfer";
76        public final static String MIN_JETI_VERSION = "0.6.1";
77        public final static String NAME = "filetransfer";
78        public final static String ABOUT = "by E.S. de Boer";
79       
80       
81        public static void init(final Backend backend)
82    {
83               
84                backend.getMain().addToOnlineRosterMenu(I18N.gettext("filetransfer.Transfer_File")+ "...",new RosterMenuListener()
85                {
86                        public void actionPerformed(JIDStatus jidStatus,nu.fw.jeti.backend.roster.JIDStatusGroup group)
87                        {
88                                if(Socks5Send.isProxyAvailable() ||
89                                                Preferences.getBoolean("filetransfer", "useLocalIP", true))
90                                {
91                                        new SendFileWindow(backend,jidStatus.getCompleteJID()).setVisible(true);
92                                }
93                                else
94                                {
95                                        Popups.errorPopup(I18N.gettext("filetransfer.File_Transfer_not_available,_because_no_filetransfer_proxy_is_available"),I18N.gettext("filetransfer.File_Transfer"));
96                                }
97                        }
98                });
99
100                plugin = new Plugin();
101                Plugin.backend = backend;
102                backend.addExtensionHandler("http://jabber.org/protocol/si",new IQSiHandler());
103                backend.addExtensionHandler("http://jabber.org/protocol/si/profile/file-transfer",new XSiFileTransferHandler());
104                backend.addCapability("si", "http://jabber.org/protocol/bytestreams");
105                backend.addCapability("si", "http://jabber.org/protocol/ibb");
106                backend.addCapability("si", "http://jabber.org/protocol/si");
107                backend.addCapability("si", "http://jabber.org/protocol/si/profile/file-transfer");
108                backend.addExtensionHandler("http://jabber.org/protocol/feature-neg",new ExtensionHandler()
109                {
110                        private XData data;
111                       
112                        public void startHandling(Attributes attr)
113                        {
114                                data=null;
115                        }
116                       
117                        public void addExtension(Extension extension)
118                        {
119                                if(extension instanceof XData) data = (XData)extension;
120                        }
121
122                        public Extension build() throws InstantiationException
123                        {
124                                return data;
125                        }
126                });
127                backend.addExtensionHandler("http://jabber.org/protocol/ibb",new IBBHandler());
128                backend.addExtensionHandler("http://jabber.org/protocol/bytestreams",new Socks5Handler());
129                Socks5Send.initProxies(backend);
130        backend.getMain().getOnlinePanel().setTransferHandler(new FileTransferhandler());
131    }
132
133        protected static Backend getBackend()
134        {
135                return backend;
136        }
137       
138        public static void addGetFile(GetFileWindow w, JID jid,String id)
139        {
140                //FIXME add sid to getfile to know the precise file
141                plugin.fileWindows.put(jid,w);
142                //plugin.fileWindows.put(new JIDSID(jid,id),w);
143        }
144       
145        public static GetFileWindow getGetFile(JID jid,String sid)
146        {
147                //FIXME add sid to getfile to know the precise file
148                return (GetFileWindow)plugin.fileWindows.remove(jid);
149                //return (GetFileWindow)plugin.fileWindows.remove(new JIDSID(jid,sid));
150        }
151       
152        public static void unload(Backend backend)
153        {
154                backend.getMain().removeFromRosterMenu(I18N.gettext("filetransfer.Transfer_File")+ "...");
155                //backend.getMain().removeFromMenu(plugin.menuItem);
156                backend.removeExtensionHandler("http://jabber.org/protocol/ibb");
157                backend.removeExtensionHandler("http://jabber.org/protocol/si");
158                backend.removeExtensionHandler("http://jabber.org/protocol/si/profile/file-transfer");
159                backend.removeExtensionHandler("http://jabber.org/protocol/feature-neg");
160                backend.removeCapability("si", "http://jabber.org/protocol/bytestreams");
161                backend.removeCapability("si", "http://jabber.org/protocol/ibb");
162                backend.removeCapability("si", "http://jabber.org/protocol/si");
163                backend.removeCapability("si", "http://jabber.org/protocol/si/profile/file-transfer");
164               
165                plugin = null;
166        }
167       
168        public void unload() {}
169       
170        public static String sha(String sid,JID initiator,JID target)
171        {
172                //System.out.println(sid.toString());
173                //System.out.println(initiator.toString());
174                //System.out.println(target.toString());
175               
176                MessageDigest sha=null;
177                try
178                {
179                        sha = MessageDigest.getInstance("SHA");
180                } catch (NoSuchAlgorithmException e)
181                {
182                        // TODO Auto-generated catch block
183                        e.printStackTrace();
184                }
185                sha.update(sid.getBytes());
186                sha.update(stringPrep(initiator).getBytes());
187                return Utils.toString(sha.digest(stringPrep(target).getBytes()));
188        }
189       
190        private static String stringPrep(JID jid)
191        {//TODO make real stringprep
192                StringBuffer j = new StringBuffer();
193                if (jid.getUser() != null){
194                        j.append(jid.getUser().toLowerCase());
195                        j.append("@");
196                }
197                j.append(jid.getDomain().toLowerCase());
198                if (jid.getResource() != null){
199                        j.append("/");
200                        j.append(jid.getResource());
201                }
202                return j.toString();
203        }
204               
205        public static String getIP()
206        {
207                Enumeration interfaceEnum;
208                try
209                {
210                        interfaceEnum = NetworkInterface.getNetworkInterfaces();
211                        while (interfaceEnum.hasMoreElements())
212                        {
213                                NetworkInterface n = (NetworkInterface)
214                                                                        interfaceEnum.nextElement();
215                                //System.out.println(n.getDisplayName() + ":");
216                                Enumeration ipEnum = n.getInetAddresses();
217                                while (ipEnum.hasMoreElements())
218                                {
219                                        InetAddress ip = (InetAddress) ipEnum.nextElement();
220                                        if(ip instanceof Inet4Address)
221                                        {
222                                                if (!ip.isLoopbackAddress())
223                                                {
224                                                        return ip.getHostAddress();
225                                                }
226                                        }
227                                }
228                        }
229                } catch (SocketException e)
230                {
231                        e.printStackTrace();
232                }
233
234                return null;
235        }
236
237    protected static JFileChooser getFileChooser() {
238        return fileChooser;
239    }
240
241        public static String getSizeText(long size)
242        {
243                if(size<1024) return size + " bytes";
244                double s;
245                NumberFormat n = NumberFormat.getNumberInstance();
246                n.setMaximumFractionDigits(2);
247                s = size/1024d;
248                if(s<1024) return n.format(s) + " kB";
249                s = s/1024d;
250                if(s<1024) return n.format(s) + " MB";
251                s = s/1024d;
252                return n.format(s) + " GB";
253        }
254
255        static private class FileTransferhandler extends TransferHandler {
256
257        public boolean canImport(JComponent comp, DataFlavor[] flavors) {
258            for (int i = 0; i < flavors.length; i++) {
259                if (flavors[i].equals(DataFlavor.javaFileListFlavor)) {
260                    return true;
261                }
262            }
263            return false;
264        }
265
266        public boolean importData(JComponent comp, Transferable t) {
267            Object o = ((JTree)comp).getLastSelectedPathComponent();
268            JID to;
269
270            if(!Socks5Send.isProxyAvailable() &&
271                                        !Preferences.getBoolean("filetransfer", "useLocalIP", true))
272                        {
273                Popups.errorPopup(I18N.gettext("filetransfer.File_Transfer_not_available,_because_no_filetransfer_proxy_is_available"),I18N.gettext("filetransfer.File_Transfer"));
274                return false;
275                        }
276           
277                        if (o instanceof JIDStatus) {
278                to = ((JIDStatus)o).getCompleteJID();
279            } else if (o instanceof PrimaryJIDStatus) {
280                PrimaryJIDStatus p = (PrimaryJIDStatus)o;
281                to = p.getJIDPrimaryStatus().getCompleteJID();
282            } else {
283                return false;
284            }
285
286            if (!t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
287                return false;
288            }
289            try {
290                Collection files = (Collection) t.getTransferData(
291                    DataFlavor.javaFileListFlavor);
292
293                if (files == null || files.size() == 0) {
294                    return false;
295                }
296
297                // Check that everything is regular files
298                for (Iterator i = files.iterator(); i.hasNext(); ) {
299                    File file = (File) i.next();
300                    if (!file.isFile()) {
301                        Popups.errorPopup(
302                            I18N.gettext("filetransfer.Can_not_transfer_directories"),
303                            I18N.gettext("main.error.Error"));
304                        return false;
305                    }
306                }
307
308                for (Iterator i = files.iterator(); i.hasNext(); ) {
309                    File file = (File) i.next();
310                    new SendFileWindow(backend, to, file).setVisible(true);
311                }
312
313                return true;
314            } catch (UnsupportedFlavorException e) {
315                System.out.println("unsupported data");
316            } catch (IOException e) {
317                System.out.println("IOException");
318            }
319            return false;
320        }
321    }
322}
323
324/*
325 * Overrides for emacs
326 * Local variables:
327 * tab-width: 4
328 * End:
329 */
Note: See TracBrowser for help on using the repository browser.