]> git.saurik.com Git - wxWidgets.git/blame - interface/wx/protocol/ftp.h
Move code removing "-psn_xxx" command line arguments to common code.
[wxWidgets.git] / interface / wx / protocol / ftp.h
CommitLineData
23324ae1
FM
1/////////////////////////////////////////////////////////////////////////////
2// Name: protocol/ftp.h
e54c96f1 3// Purpose: interface of wxFTP
23324ae1 4// Author: wxWidgets team
526954c5 5// Licence: wxWindows licence
23324ae1
FM
6/////////////////////////////////////////////////////////////////////////////
7
8/**
9 @class wxFTP
7c913512 10
23324ae1 11 wxFTP can be used to establish a connection to an FTP server and perform all the
730b772b
FM
12 usual operations. Please consult the RFC 959 (http://www.w3.org/Protocols/rfc959/)
13 for more details about the FTP protocol.
14
15 wxFTP can thus be used to create a (basic) FTP @b client.
7c913512 16
a30b5ab9 17 To use a command which doesn't involve file transfer (i.e. directory oriented
23324ae1 18 commands) you just need to call a corresponding member function or use the
a30b5ab9
FM
19 generic wxFTP::SendCommand() method.
20 However to actually transfer files you just get or give a stream to or from this
21 class and the actual data are read or written using the usual stream methods.
7c913512 22
23324ae1 23 Example of using wxFTP for file downloading:
7c913512 24
23324ae1 25 @code
a30b5ab9 26 wxFTP ftp;
7c913512 27
23324ae1
FM
28 // if you don't use these lines anonymous login will be used
29 ftp.SetUser("user");
30 ftp.SetPassword("password");
7c913512 31
730b772b 32 if ( !ftp.Connect("ftp.wxwidgets.org") )
23324ae1
FM
33 {
34 wxLogError("Couldn't connect");
35 return;
36 }
7c913512 37
634ad772 38 ftp.ChDir("/pub/2.8.9");
cf7f7d5d
VZ
39 const char *filename = "wxWidgets-2.8.9.tar.bz2";
40 int size = ftp.GetFileSize(filename);
41 if ( size == -1 )
42 {
43 wxLogError("Couldn't get the file size for \"%s\"", filename);
44 }
45
28c2cd42 46 wxInputStream *in = ftp.GetInputStream(filename);
23324ae1
FM
47 if ( !in )
48 {
634ad772 49 wxLogError("Couldn't get the file");
23324ae1
FM
50 }
51 else
52 {
23324ae1 53 char *data = new char[size];
a30b5ab9 54 if ( !in->Read(data, size) )
23324ae1 55 {
634ad772 56 wxLogError("Read error: %d", ftp.GetError());
23324ae1
FM
57 }
58 else
59 {
60 // file data is in the buffer
61 ...
62 }
7c913512 63
23324ae1
FM
64 delete [] data;
65 delete in;
66 }
730b772b
FM
67
68 // gracefully close the connection to the server
69 ftp.Close();
23324ae1 70 @endcode
7c913512 71
23324ae1
FM
72 To upload a file you would do (assuming the connection to the server was opened
73 successfully):
7c913512 74
23324ae1 75 @code
a30b5ab9
FM
76 wxOutputStream *out = ftp.GetOutputStream("filename");
77 if ( out )
78 {
79 out->Write(...); // your data
80 delete out;
81 }
23324ae1 82 @endcode
7c913512 83
23324ae1
FM
84 @library{wxnet}
85 @category{net}
7c913512 86
e54c96f1 87 @see wxSocketBase
23324ae1
FM
88*/
89class wxFTP : public wxProtocol
90{
91public:
0e1cd9be
VZ
92 /**
93 Transfer modes used by wxFTP.
94 */
95 enum TransferMode
96 {
97 NONE, //!< not set by user explicitly.
98 ASCII,
99 BINARY
100 };
101
23324ae1
FM
102 /**
103 Default constructor.
104 */
4cc4bfaf 105 wxFTP();
23324ae1
FM
106
107 /**
108 Destructor will close the connection if connected.
109 */
adaaa686 110 virtual ~wxFTP();
23324ae1 111
730b772b
FM
112
113
9655ec02
VZ
114 //@{
115 /**
116 Connect to the FTP server to default port (21) of the specified @a host.
117 */
118 virtual bool Connect(const wxString& host);
119
120 /**
121 Connect to the FTP server to any port of the specified @a host.
122 By default (@a port = 0), connection is made to default FTP port (21)
123 of the specified @a host.
124
125 @since 2.9.1
126 */
127 virtual bool Connect(const wxString& host, unsigned short port);
128 //@}
129
730b772b
FM
130 /**
131 @name Functions for managing the FTP connection
132 */
133 //@{
134
23324ae1 135 /**
7c913512 136 Aborts the download currently in process, returns @true if ok, @false
23324ae1
FM
137 if an error occurred.
138 */
adaaa686 139 virtual bool Abort();
23324ae1
FM
140
141 /**
730b772b 142 Gracefully closes the connection with the server.
23324ae1 143 */
730b772b 144 virtual bool Close();
23324ae1
FM
145
146 /**
4cc4bfaf 147 Send the specified @a command to the FTP server. @a ret specifies
23324ae1 148 the expected result.
a30b5ab9 149
d29a9a8a 150 @return @true if the command has been sent successfully, else @false.
23324ae1
FM
151 */
152 bool CheckCommand(const wxString& command, char ret);
153
730b772b
FM
154 /**
155 Returns the last command result, i.e. the full server reply for the last command.
156 */
157 const wxString& GetLastResult();
158
159 /**
160 Send the specified @a command to the FTP server and return the first
161 character of the return code.
162 */
163 char SendCommand(const wxString& command);
164
165 /**
166 Sets the transfer mode to ASCII. It will be used for the next transfer.
167 */
168 bool SetAscii();
169
170 /**
171 Sets the transfer mode to binary. It will be used for the next transfer.
172 */
173 bool SetBinary();
174
175 /**
176 If @a pasv is @true, passive connection to the FTP server is used.
177
178 This is the default as it works with practically all firewalls.
179 If the server doesn't support passive mode, you may call this function
180 with @false as argument to use an active connection.
181 */
182 void SetPassive(bool pasv);
183
184 /**
185 Sets the password to be sent to the FTP server to be allowed to log in.
186 */
187 virtual void SetPassword(const wxString& passwd);
188
189 /**
190 Sets the transfer mode to the specified one. It will be used for the next
191 transfer.
192
193 If this function is never called, binary transfer mode is used by default.
194 */
195 bool SetTransferMode(TransferMode mode);
196
197 /**
198 Sets the user name to be sent to the FTP server to be allowed to log in.
199 */
200 virtual void SetUser(const wxString& user);
201
202 //@}
203
204
205
206 /**
207 @name Filesystem commands
208 */
209 //@{
210
211 /**
212 Change the current FTP working directory.
213 Returns @true if successful.
214 */
215 bool ChDir(const wxString& dir);
216
217 /**
218 Create the specified directory in the current FTP working directory.
219 Returns @true if successful.
220 */
221 bool MkDir(const wxString& dir);
222
223 /**
224 Returns the current FTP working directory.
225 */
226 wxString Pwd();
227
228 /**
229 Rename the specified @a src element to @e dst. Returns @true if successful.
230 */
231 bool Rename(const wxString& src, const wxString& dst);
232
233 /**
234 Remove the specified directory from the current FTP working directory.
235 Returns @true if successful.
236 */
237 bool RmDir(const wxString& dir);
238
239 /**
240 Delete the file specified by @e path. Returns @true if successful.
241 */
242 bool RmFile(const wxString& path);
243
23324ae1
FM
244 /**
245 Returns @true if the given remote file exists, @false otherwise.
246 */
247 bool FileExists(const wxString& filename);
248
249 /**
a30b5ab9 250 The GetList() function is quite low-level. It returns the list of the files in
4cc4bfaf 251 the current directory. The list can be filtered using the @a wildcard string.
a30b5ab9 252
4cc4bfaf 253 If @a wildcard is empty (default), it will return all files in directory.
23324ae1
FM
254 The form of the list can change from one peer system to another. For example,
255 for a UNIX peer system, it will look like this:
a30b5ab9
FM
256
257 @verbatim
258 -r--r--r-- 1 guilhem lavaux 12738 Jan 16 20:17 cmndata.cpp
259 -r--r--r-- 1 guilhem lavaux 10866 Jan 24 16:41 config.cpp
260 -rw-rw-rw- 1 guilhem lavaux 29967 Dec 21 19:17 cwlex_yy.c
261 -rw-rw-rw- 1 guilhem lavaux 14342 Jan 22 19:51 cwy_tab.c
262 -r--r--r-- 1 guilhem lavaux 13890 Jan 29 19:18 date.cpp
263 -r--r--r-- 1 guilhem lavaux 3989 Feb 8 19:18 datstrm.cpp
264 @endverbatim
265
23324ae1 266 But on Windows system, it will look like this:
a30b5ab9
FM
267
268 @verbatim
269 winamp~1 exe 520196 02-25-1999 19:28 winamp204.exe
270 1 file(s) 520 196 bytes
271 @endverbatim
272
273 @return @true if the file list was successfully retrieved, @false otherwise.
274
4cc4bfaf 275 @see GetFilesList()
23324ae1
FM
276 */
277 bool GetDirList(wxArrayString& files,
43c48e1e 278 const wxString& wildcard = wxEmptyString);
23324ae1
FM
279
280 /**
281 Returns the file size in bytes or -1 if the file doesn't exist or the size
a30b5ab9
FM
282 couldn't be determined.
283
284 Notice that this size can be approximative size only and shouldn't be used
285 for allocating the buffer in which the remote file is copied, for example.
23324ae1
FM
286 */
287 int GetFileSize(const wxString& filename);
288
289 /**
290 This function returns the computer-parsable list of the files in the current
291 directory (optionally only of the files matching the @e wildcard, all files
a30b5ab9
FM
292 by default).
293
294 This list always has the same format and contains one full (including the
295 directory path) file name per line.
296
d29a9a8a 297 @return @true if the file list was successfully retrieved, @false otherwise.
a30b5ab9
FM
298
299 @see GetDirList()
23324ae1
FM
300 */
301 bool GetFilesList(wxArrayString& files,
43c48e1e 302 const wxString& wildcard = wxEmptyString);
23324ae1 303
730b772b
FM
304 //@}
305
306
307 /**
308 @name Download and upload functions
309 */
310
311 //@{
23324ae1 312 /**
a30b5ab9
FM
313 Creates a new input stream on the specified path.
314
315 You can use all but the seek functionality of wxStreamBase.
316 wxStreamBase::Seek() isn't available on all streams. For example, HTTP or FTP
317 streams do not deal with it. Other functions like wxStreamBase::Tell() are
318 not available for this sort of stream, at present.
319
23324ae1 320 You will be notified when the EOF is reached by an error.
a30b5ab9 321
d29a9a8a 322 @return Returns @NULL if an error occurred (it could be a network failure
730b772b 323 or the fact that the file doesn't exist).
23324ae1 324 */
adaaa686 325 virtual wxInputStream* GetInputStream(const wxString& path);
23324ae1
FM
326
327 /**
730b772b 328 Initializes an output stream to the specified @a file.
a30b5ab9
FM
329
330 The returned stream has all but the seek functionality of wxStreams.
331 When the user finishes writing data, he has to delete the stream to close it.
332
d29a9a8a 333 @return An initialized write-only stream.
730b772b
FM
334 Returns @NULL if an error occurred (it could be a network failure
335 or the fact that the file doesn't exist).
23324ae1 336 */
adaaa686 337 virtual wxOutputStream* GetOutputStream(const wxString& file);
23324ae1 338
730b772b 339 //@}
23324ae1 340};
e54c96f1 341