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