]>
Commit | Line | Data |
---|---|---|
f4ada568 GL |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: ftp.h | |
3 | // Purpose: FTP protocol | |
4 | // Author: Vadim Zeitlin | |
2e907fab | 5 | // Modified by: Mark Johnson, wxWindows@mj10777.de |
77ffb593 | 6 | // 20000917 : RmDir, GetLastResult, GetList |
f4ada568 GL |
7 | // Created: 07/07/1997 |
8 | // RCS-ID: $Id$ | |
9 | // Copyright: (c) 1997, 1998 Guilhem Lavaux | |
65571936 | 10 | // Licence: wxWindows licence |
f4ada568 | 11 | ///////////////////////////////////////////////////////////////////////////// |
8e907a13 | 12 | |
f4ada568 GL |
13 | #ifndef __WX_FTP_H__ |
14 | #define __WX_FTP_H__ | |
15 | ||
a5d46b73 VZ |
16 | #include "wx/defs.h" |
17 | ||
18 | #if wxUSE_PROTOCOL_FTP | |
19 | ||
f4ada568 GL |
20 | #include "wx/sckaddr.h" |
21 | #include "wx/protocol/protocol.h" | |
22 | #include "wx/url.h" | |
23 | ||
7c4728f6 | 24 | class WXDLLIMPEXP_NET wxFTP : public wxProtocol |
8e907a13 | 25 | { |
f4ada568 | 26 | public: |
2e907fab VZ |
27 | enum TransferMode |
28 | { | |
b92fd37c | 29 | NONE, // not set by user explicitly |
2e907fab VZ |
30 | ASCII, |
31 | BINARY | |
32 | }; | |
33 | ||
34 | wxFTP(); | |
35 | virtual ~wxFTP(); | |
36 | ||
37 | // Connecting and disconnecting | |
38 | void SetUser(const wxString& user) { m_user = user; } | |
39 | void SetPassword(const wxString& passwd) { m_passwd = passwd; } | |
40 | ||
e2454588 | 41 | bool Connect(wxSockAddress& addr, bool wait = true); |
2e907fab VZ |
42 | bool Connect(const wxString& host); |
43 | ||
44 | // disconnect | |
45 | virtual bool Close(); | |
46 | ||
47 | // Parameters set up | |
48 | ||
49 | // set transfer mode now | |
47b378bd | 50 | void SetPassive(bool pasv) { m_bPassive = pasv; } |
e2454588 | 51 | void SetDefaultTimeout(wxUint32 Value); |
2e907fab VZ |
52 | bool SetBinary() { return SetTransferMode(BINARY); } |
53 | bool SetAscii() { return SetTransferMode(ASCII); } | |
54 | bool SetTransferMode(TransferMode mode); | |
55 | ||
56 | // Generic FTP interface | |
57 | ||
58 | // the error code | |
59 | virtual wxProtocolError GetError() { return m_lastError; } | |
60 | ||
61 | // the last FTP server reply | |
62 | const wxString& GetLastResult() { return m_lastResult; } | |
63 | ||
64 | // send any FTP command (should be full FTP command line but without | |
65 | // trailing "\r\n") and return its return code | |
66 | char SendCommand(const wxString& command); | |
67 | ||
68 | // check that the command returned the given code | |
69 | bool CheckCommand(const wxString& command, char expectedReturn) | |
70 | { | |
71 | return SendCommand(command) == expectedReturn; | |
72 | } | |
73 | ||
74 | // Filesystem commands | |
75 | bool ChDir(const wxString& dir); | |
76 | bool MkDir(const wxString& dir); | |
77 | bool RmDir(const wxString& dir); | |
78 | wxString Pwd(); | |
79 | bool Rename(const wxString& src, const wxString& dst); | |
80 | bool RmFile(const wxString& path); | |
81 | ||
3f2bcf34 VZ |
82 | // Get the size of a file in the current dir. |
83 | // this function tries its best to deliver the size in bytes using BINARY | |
84 | // (the SIZE command reports different sizes depending on whether | |
85 | // type is set to ASCII or BINARY) | |
86 | // returns -1 if file is non-existant or size could not be found | |
87 | int GetFileSize(const wxString& fileName); | |
b92fd37c VZ |
88 | |
89 | // Check to see if a file exists in the current dir | |
3f2bcf34 | 90 | bool FileExists(const wxString& fileName); |
b92fd37c | 91 | |
2e907fab VZ |
92 | // Download methods |
93 | bool Abort(); | |
94 | ||
95 | virtual wxInputStream *GetInputStream(const wxString& path); | |
96 | virtual wxOutputStream *GetOutputStream(const wxString& path); | |
97 | ||
98 | // Directory listing | |
99 | ||
100 | // get the list of full filenames, the format is fixed: one file name per | |
101 | // line | |
102 | bool GetFilesList(wxArrayString& files, | |
103 | const wxString& wildcard = wxEmptyString) | |
104 | { | |
e2454588 | 105 | return GetList(files, wildcard, false); |
2e907fab VZ |
106 | } |
107 | ||
108 | // get a directory list in server dependent format - this can be shown | |
109 | // directly to the user | |
110 | bool GetDirList(wxArrayString& files, | |
111 | const wxString& wildcard = wxEmptyString) | |
112 | { | |
e2454588 | 113 | return GetList(files, wildcard, true); |
2e907fab VZ |
114 | } |
115 | ||
116 | // equivalent to either GetFilesList() (default) or GetDirList() | |
117 | bool GetList(wxArrayString& files, | |
118 | const wxString& wildcard = wxEmptyString, | |
e2454588 | 119 | bool details = false); |
2e907fab | 120 | |
2e907fab VZ |
121 | protected: |
122 | // this executes a simple ftp command with the given argument and returns | |
e2454588 | 123 | // true if it its return code starts with '2' |
2e907fab VZ |
124 | bool DoSimpleCommand(const wxChar *command, |
125 | const wxString& arg = wxEmptyString); | |
f4ada568 | 126 | |
2e907fab | 127 | // get the server reply, return the first character of the reply code, |
3103e8a9 | 128 | // '1'..'5' for normal FTP replies, 0 (*not* '0') if an error occurred |
2e907fab | 129 | char GetResult(); |
f4ada568 | 130 | |
2e907fab VZ |
131 | // check that the result is equal to expected value |
132 | bool CheckResult(char ch) { return GetResult() == ch; } | |
f4ada568 | 133 | |
e2454588 VZ |
134 | // return the socket to be used, Passive/Active versions are used only by |
135 | // GetPort() | |
136 | wxSocketBase *GetPort(); | |
137 | wxSocketBase *GetPassivePort(); | |
138 | wxSocketBase *GetActivePort(); | |
139 | ||
140 | // helper for GetPort() | |
fbfb8bcc | 141 | wxString GetPortCmdArgument(const wxIPV4address& Local, const wxIPV4address& New); |
e2454588 VZ |
142 | |
143 | // accept connection from server in active mode, returns the same socket as | |
144 | // passed in in passive mode | |
145 | wxSocketBase *AcceptIfActive(wxSocketBase *sock); | |
146 | ||
8e907a13 | 147 | |
2e907fab VZ |
148 | wxString m_user, |
149 | m_passwd; | |
8e907a13 | 150 | |
2e907fab VZ |
151 | wxString m_lastResult; |
152 | wxProtocolError m_lastError; | |
f4ada568 | 153 | |
2e907fab VZ |
154 | // true if there is an FTP transfer going on |
155 | bool m_streaming; | |
f4ada568 | 156 | |
b92fd37c VZ |
157 | // although this should be set to ASCII by default according to STD9, |
158 | // we will use BINARY transfer mode by default for backwards compatibility | |
159 | TransferMode m_currentTransfermode; | |
f4ada568 | 160 | |
2e907fab VZ |
161 | friend class wxInputFTPStream; |
162 | friend class wxOutputFTPStream; | |
8e907a13 | 163 | |
e2454588 VZ |
164 | bool m_bPassive; |
165 | wxUint32 m_uiDefaultTimeout; | |
166 | ||
167 | // following is true when a read or write times out, we then assume | |
168 | // the connection is dead and abort. we avoid additional delays this way | |
169 | bool m_bEncounteredError; | |
170 | ||
171 | ||
fc7a2a60 | 172 | DECLARE_DYNAMIC_CLASS_NO_COPY(wxFTP) |
2e907fab | 173 | DECLARE_PROTOCOL(wxFTP) |
f4ada568 GL |
174 | }; |
175 | ||
b92fd37c VZ |
176 | // the trace mask used by assorted wxLogTrace() in ftp code, do |
177 | // wxLog::AddTraceMask(FTP_TRACE_MASK) to see them in output | |
178 | #define FTP_TRACE_MASK _T("ftp") | |
179 | ||
a5d46b73 VZ |
180 | #endif // wxUSE_PROTOCOL_FTP |
181 | ||
8e907a13 | 182 | #endif // __WX_FTP_H__ |