FTP.hpp

00001 
00002 //
00003 // SFML - Simple and Fast Multimedia Library
00004 // Copyright (C) 2007 Laurent Gomila (laurent.gom@gmail.com)
00005 //
00006 // This software is provided 'as-is', without any express or implied warranty.
00007 // In no event will the authors be held liable for any damages arising from the use of this software.
00008 //
00009 // Permission is granted to anyone to use this software for any purpose,
00010 // including commercial applications, and to alter it and redistribute it freely,
00011 // subject to the following restrictions:
00012 //
00013 // 1. The origin of this software must not be misrepresented;
00014 //    you must not claim that you wrote the original software.
00015 //    If you use this software in a product, an acknowledgment
00016 //    in the product documentation would be appreciated but is not required.
00017 //
00018 // 2. Altered source versions must be plainly marked as such,
00019 //    and must not be misrepresented as being the original software.
00020 //
00021 // 3. This notice may not be removed or altered from any source distribution.
00022 //
00024 
00025 #ifndef SFML_FTP_HPP
00026 #define SFML_FTP_HPP
00027 
00029 // Headers
00031 #include <SFML/Network/IPAddress.hpp>
00032 #include <SFML/Network/SocketTCP.hpp>
00033 #include <SFML/System/NonCopyable.hpp>
00034 #include <vector>
00035 
00036 
00041 class SFML_API sfFTP : sfNonCopyable
00042 {
00043 public :
00044 
00049     class Listener
00050     {
00051     public :
00052 
00057         virtual ~Listener() {}
00058 
00059     private :
00060 
00061         friend class sfFTP;
00062 
00070         virtual void OnResponse(unsigned int Code, const std::string& Message) = 0;
00071     };
00072 
00076     enum TransferMode
00077     {
00078         Binary,
00079         Ascii,
00080         Ebcdic
00081     };
00082 
00089     sfFTP(Listener* FTPListener = NULL);
00090 
00095     ~sfFTP();
00096 
00106     bool Connect(const sfIPAddress& Server, unsigned short Port = 21);
00107 
00115     bool Login(const std::string& UserName, const std::string& Password);
00116 
00123     bool Disconnect();
00124 
00131     bool KeepAlive();
00132 
00141     bool GetWorkingDirectory(std::string& Directory);
00142 
00153     bool GetDirectoryListing(std::vector<std::string>& Listing, const std::string& Directory = "");
00154 
00163     bool ChangeDirectory(const std::string& Directory);
00164 
00171     bool ParentDirectory();
00172 
00181     bool MakeDirectory(const std::string& Name);
00182 
00191     bool DeleteDirectory(const std::string& Name);
00192 
00202     bool RenameFile(const std::string& File, const std::string& NewName);
00203 
00212     bool RemoveFile(const std::string& Name);
00213 
00224     bool Download(const std::string& DistantFile, const std::string& DestPath, TransferMode Mode = Binary);
00225 
00236     bool Upload(const std::string& LocalFile, const std::string& DestPath, TransferMode Mode = Binary);
00237 
00238 private :
00239 
00249     bool SendCommand(const std::string& Command, const std::string& Parameter = "");
00250 
00258     bool GetResponse();
00259 
00264     class DataChannel : sfNonCopyable
00265     {
00266     public :
00267 
00274         DataChannel(sfFTP& FTP);
00275 
00280         ~DataChannel();
00281 
00291         bool Open(TransferMode Mode, unsigned short Port = 20);
00292 
00299         void Send(const std::vector<char>& Data);
00300 
00307         void Receive(std::vector<char>& Data);
00308 
00309     private :
00310 
00312         // Member data
00314         sfFTP&      myFTP;        
00315         sfSocketTCP myDataSocket; 
00316     };
00317 
00318     friend class DataChannel;
00319 
00321     // Member data
00323     sfSocketTCP  myCommandSocket; 
00324     std::string  myLastMessage;   
00325     Listener*    myListener;      
00326 };
00327 
00328 
00329 #endif // SFML_FTP_HPP