/plugins/FTP/tags/release-0-2-1/FtpPlugin.java

# · Java · 174 lines · 117 code · 23 blank · 34 comment · 32 complexity · 2f71a4b0c56bbc6eb6054a6ed9004b76 MD5 · raw file

  1. /*
  2. * FtpPlugin.java - Main class of virtual filesystem
  3. * Copyright (C) 2000 Slava Pestov
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. import java.awt.Component;
  20. import java.util.Hashtable;
  21. import java.util.Vector;
  22. import org.gjt.sp.jedit.browser.VFSBrowser;
  23. import org.gjt.sp.jedit.io.*;
  24. import org.gjt.sp.jedit.msg.*;
  25. import org.gjt.sp.jedit.*;
  26. public class FtpPlugin extends EBPlugin
  27. {
  28. public void start()
  29. {
  30. loginHash = new Hashtable();
  31. VFSManager.registerVFS(FtpVFS.PROTOCOL,new FtpVFS());
  32. }
  33. public void createMenuItems(Vector menuItems)
  34. {
  35. menuItems.addElement(GUIUtilities.loadMenu("ftp"));
  36. }
  37. public void handleMessage(EBMessage msg)
  38. {
  39. if(msg instanceof EditorExiting)
  40. {
  41. // Clear cached directory listings
  42. DirectoryCache.clearAllCachedDirectories();
  43. }
  44. else if(msg instanceof VFSUpdate)
  45. {
  46. VFSUpdate vmsg = (VFSUpdate)msg;
  47. DirectoryCache.clearCachedDirectory(vmsg.getPath());
  48. }
  49. }
  50. public static void showOpenFTPDialog(View view)
  51. {
  52. String path = ((FtpVFS)VFSManager.getVFSForProtocol("ftp"))
  53. .showBrowseDialog(new Object[1],view);
  54. if(path != null)
  55. {
  56. String[] files = GUIUtilities.showVFSFileDialog(
  57. view,path,VFSBrowser.OPEN_DIALOG,true);
  58. if(files == null)
  59. return;
  60. Buffer buffer = null;
  61. for(int i = 0; i < files.length; i++)
  62. {
  63. Buffer _buffer = jEdit.openFile(null,files[i]);
  64. if(_buffer != null)
  65. buffer = buffer;
  66. }
  67. if(buffer != null)
  68. view.setBuffer(buffer);
  69. }
  70. }
  71. public static void showSaveFTPDialog(View view)
  72. {
  73. String path = ((FtpVFS)VFSManager.getVFSForProtocol("ftp"))
  74. .showBrowseDialog(new Object[1],view);
  75. if(path != null)
  76. {
  77. String[] files = GUIUtilities.showVFSFileDialog(
  78. view,path,VFSBrowser.SAVE_DIALOG,false);
  79. if(files == null)
  80. return;
  81. view.getBuffer().save(view,files[0],true);
  82. }
  83. }
  84. /**
  85. * If a password has been saved for the host and user name in the
  86. * session, it sets the value of the session's PASSWORD_KEY value
  87. * accordingly. Otherwise, a login dialog box is displayed.
  88. * @param session The VFS session
  89. * @param comp The component that will parent the login dialog box
  90. * @return True if everything is ok, false if the user cancelled the
  91. * operation
  92. */
  93. public static boolean showLoginDialog(Object _session, Component comp)
  94. {
  95. VFSSession session = (VFSSession)_session;
  96. String host = (String)session.get(VFSSession.HOSTNAME_KEY);
  97. String user = (String)session.get(VFSSession.USERNAME_KEY);
  98. String password = (String)session.get(VFSSession.PASSWORD_KEY);
  99. if(host != null)
  100. {
  101. LoginInfo login = (LoginInfo)loginHash.get(host);
  102. if(login != null && (user == null || login.user.equals(user)))
  103. {
  104. if(user == null)
  105. {
  106. user = login.user;
  107. session.put(VFSSession.USERNAME_KEY,user);
  108. }
  109. if(password == null)
  110. {
  111. password = login.password;
  112. session.put(VFSSession.PASSWORD_KEY,password);
  113. }
  114. }
  115. if(user != null && password != null)
  116. return true;
  117. }
  118. /* since this can be called at startup time,
  119. * we need to hide the splash screen. */
  120. GUIUtilities.hideSplashScreen();
  121. LoginDialog dialog = new LoginDialog(comp,host,user,password);
  122. if(!dialog.isOK())
  123. return false;
  124. host = dialog.getHost();
  125. user = dialog.getUser();
  126. password = dialog.getPassword();
  127. session.put(VFSSession.HOSTNAME_KEY,host);
  128. session.put(VFSSession.USERNAME_KEY,user);
  129. session.put(VFSSession.PASSWORD_KEY,password);
  130. loginHash.put(host,new LoginInfo(user,password));
  131. return true;
  132. }
  133. /**
  134. * Forgets all saved passwords.
  135. * @since jEdit 2.6pre3
  136. */
  137. public static void forgetPasswords()
  138. {
  139. loginHash.clear();
  140. }
  141. static class LoginInfo
  142. {
  143. String user, password;
  144. LoginInfo(String user, String password)
  145. {
  146. this.user = user;
  147. this.password = password;
  148. }
  149. }
  150. private static Hashtable loginHash;
  151. }