1 19 package org.lucane.applications.sendfile; 20 21 import org.lucane.client.*; 22 import org.lucane.client.widgets.*; 23 import org.lucane.common.*; 24 import org.lucane.common.net.ObjectConnection; 25 26 import java.awt.*; 27 import java.io.*; 28 29 import javax.swing.*; 30 31 32 public class SendFile 33 extends Plugin 34 { 35 private ObjectConnection connection; 36 private ConnectInfo friend; 37 private String fileName; 38 39 private SendFileDialog dialog; 40 41 public SendFile() 42 { 43 } 44 45 47 public Plugin newInstance(ConnectInfo[] friends) 48 { 49 if(friends.length > 0) 50 return new SendFile(friends[0]); 51 else 52 return new SendFile(null); 53 } 54 55 public SendFile(ConnectInfo friend) 56 { 57 this.friend = friend; 58 } 59 60 public void load(ObjectConnection friend, ConnectInfo who, String filename) 61 { 62 this.connection = friend; 63 this.friend = who; 64 this.fileName = filename; 65 } 66 67 public void start() 68 { 69 70 if(this.friend == null) 72 { 73 DialogBox.info(tr("noselect")); 74 exit(); 75 return; 76 } 77 78 dialog = new SendFileDialog(this, getTitle() + "> " + friend.getName(), true); 79 dialog.setExitPluginOnClose(true); 80 selectFile(dialog); 81 dialog.show(); 82 } 83 84 public void follow() 85 { 86 dialog = new SendFileDialog(this, getTitle() + " < " + friend.getName(), false); 87 dialog.setPreferredSize(new Dimension(400, 300)); 88 dialog.setFilePath(fileName); 89 90 try { 91 String comment = connection.readString(); 92 dialog.setComment(comment); 93 } catch (Exception e) { 94 dialog.setComment(tr("err.comment")); 95 } 96 97 dialog.show(); 98 } 99 100 102 public void selectFile(SendFileDialog dialog) 103 { 104 JFileChooser fc = new JFileChooser(); 105 int returnVal = fc.showOpenDialog(null); 106 if(returnVal == JFileChooser.APPROVE_OPTION) 107 dialog.setFilePath(fc.getSelectedFile().getPath()); 108 } 109 110 public void askForAccept(String filePath, String comment) 111 { 112 dialog.dispose(); 113 114 filePath = filePath.replace('\\', '/'); 115 int index = filePath.lastIndexOf('/'); 116 if(index > 0) 117 this.fileName = filePath.substring(index+1); 118 else 119 this.fileName = filePath; 120 121 try { 122 connection = Communicator.getInstance().sendMessageTo(friend, getName(), fileName); 123 connection.write(comment); 124 125 Boolean acceptation = Boolean.FALSE; 126 127 try { 128 acceptation = (Boolean )connection.read(); 129 } catch(Exception e) { 130 } 132 133 if(acceptation.booleanValue()) 134 sendFile(filePath); 135 else 136 { 137 String reject = tr("msg.rejectFile"); 138 reject = reject.replaceAll("%1", friend.getName()); 139 reject = reject.replaceAll("%2", fileName); 140 DialogBox.error(reject); 141 } 142 } 143 catch(Exception e) 144 { 145 DialogBox.error(tr("sendError")); 146 e.printStackTrace(); 147 } 148 149 connection.close(); 150 exit(); 151 } 152 153 public void saveFile() 154 { 155 JFileChooser fc = new JFileChooser(); 156 fc.setSelectedFile(new File(fileName)); 157 int returnVal = fc.showSaveDialog(null); 158 if(returnVal == JFileChooser.APPROVE_OPTION) 159 { 160 try { 161 connection.write(Boolean.TRUE); 162 } catch (IOException e) { 163 e.printStackTrace(); 164 } 165 166 File file = fc.getSelectedFile(); 167 try { 168 downloadFile(file.getAbsolutePath()); 169 String get = tr("msg.get").replaceAll("%1", fileName); 170 DialogBox.info(get); 171 } catch (Exception e) { 172 DialogBox.error(tr("getError")); 173 } 174 exit(); 175 } 176 else 177 rejectFile(); 178 } 179 180 public void openFile() 181 { 182 try { 183 String extension = getFileExtension(); 184 File file = File.createTempFile(fileName, "." + extension); 185 186 String command = getCommand(); 187 if(command == null) 188 command = selectCommand(); 189 190 if(command == null) 192 { 193 boolean doSave = DialogBox.question( 194 tr("msg.selectCommandFor").replaceAll("%1", extension), 195 tr("msg.saveInsteadOfOpen")); 196 197 if(doSave) 198 saveFile(); 199 else 200 rejectFile(); 201 202 return; 203 } 204 205 connection.write(Boolean.TRUE); 206 downloadFile(file.getAbsolutePath()); 207 208 try { 209 Runtime.getRuntime().exec(command + " " + file.getAbsolutePath()); 210 } catch (IOException ioe) { 211 String err = tr("err.commandFailed").replaceAll("%1", command); 212 DialogBox.error(err); 213 } 214 } catch(Exception e) { 215 DialogBox.error(tr("getError")); 216 e.printStackTrace(); 217 } 218 exit(); 219 } 220 221 public void rejectFile() 222 { 223 try { 224 connection.write(Boolean.FALSE); 225 } catch (IOException e) { 226 } 228 Logging.getLogger().finer("SendFile::REJECT"); 229 connection.close(); 230 dialog.dispose(); 231 exit(); 232 } 233 234 public void sendFile(String filePath) 235 throws Exception 236 { 237 File file = new File(filePath); 238 connection.write(new Long (file.length())); 239 240 DataInputStream dis = null; 241 try { 242 dis = new DataInputStream(new FileInputStream(filePath)); 243 while(dis.available() > 0) 244 { 245 byte[] buffer = new byte[10240]; 246 int bytes = dis.read(buffer); 247 if(bytes != buffer.length); 248 { 249 byte[] temp = new byte[bytes]; 250 System.arraycopy(buffer, 0, temp, 0, bytes); 251 buffer = temp; 252 } 253 connection.write(buffer); 254 } 255 } finally { 256 if(dis != null) 257 dis.close(); 258 connection.close(); 259 } 260 261 String sent = tr("msg.sent").replaceAll("%1", fileName); 262 DialogBox.info(sent); 263 } 264 265 public void downloadFile(String destination) 266 throws IOException, ClassNotFoundException 267 { 268 connection.write(Boolean.TRUE); 269 Logging.getLogger().finer("SendFile::ACCEPT"); 270 dialog.dispose(); 271 272 DataOutputStream dos = new DataOutputStream(new FileOutputStream(destination)); 273 long length = ((Long )connection.read()).longValue(); 274 long bytes = 0; 275 while(bytes < length) 276 { 277 byte[] buffer = (byte[])connection.read(); 278 dos.write(buffer); 279 bytes += buffer.length; 280 } 281 282 connection.close(); 283 } 284 285 public String selectCommand() 286 { 287 String command = null; 288 String oldCommand = getCommand(); 289 String extension = getFileExtension(); 290 291 JFileChooser fc = new JFileChooser(); 292 fc.setDialogTitle(tr("msg.selectCommandFor").replaceAll("%1", extension)); 293 294 if(oldCommand != null) 295 fc.setSelectedFile(new File(oldCommand)); 296 297 int returnVal = fc.showOpenDialog(null); 298 if(returnVal == JFileChooser.APPROVE_OPTION) 299 { 300 command = fc.getSelectedFile().getAbsolutePath(); 301 getLocalConfig().set("cmd." + extension, command); 302 } 303 304 return command; 305 } 306 307 309 public String getFileExtension() 310 { 311 int index = fileName.lastIndexOf('.'); 312 if(index < 0) 313 return ""; 314 315 return fileName.substring(index+1).toLowerCase(); 316 } 317 318 public String getCommand() 319 { 320 return getLocalConfig().get("cmd." + getFileExtension()); 321 } 322 323 public String getShortCommand() 324 { 325 String command = getCommand(); 326 if(command != null) 327 { 328 command.replace('\\', '/'); 329 int index = command.lastIndexOf('/'); 330 if(index > 0) 331 command = command.substring(index+1); 332 } 333 334 return command; 335 } 336 } 337 | Popular Tags |