]> git.saurik.com Git - wxWidgets.git/blob - include/wx/filesys.h
Catching dclick event from a spinbutton or spin control
[wxWidgets.git] / include / wx / filesys.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: filesys.h
3 // Purpose: class for opening files - virtual file system
4 // Author: Vaclav Slavik
5 // Copyright: (c) 1999 Vaclav Slavik
6 // RCS-ID: $Id$
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifndef __FILESYS_H__
11 #define __FILESYS_H__
12
13 #if defined(__GNUG__) && !defined(__APPLE__)
14 #pragma interface "filesys.h"
15 #endif
16
17 #include "wx/setup.h"
18
19 #if !wxUSE_STREAMS
20 #error You cannot compile virtual file systems without wxUSE_STREAMS
21 #endif
22
23 #if wxUSE_HTML && !wxUSE_FILESYSTEM
24 #error You cannot compile wxHTML without virtual file systems
25 #endif
26
27 #if wxUSE_FILESYSTEM
28
29 #include "wx/stream.h"
30 #include "wx/url.h"
31 #include "wx/datetime.h"
32
33 class wxFSFile;
34 class wxFileSystemHandler;
35 class wxFileSystem;
36
37 //--------------------------------------------------------------------------------
38 // wxFSFile
39 // This class is a file opened using wxFileSystem. It consists of
40 // input stream, location, mime type & optional anchor
41 // (in 'index.htm#chapter2', 'chapter2' is anchor)
42 //--------------------------------------------------------------------------------
43
44 class WXDLLEXPORT wxFSFile : public wxObject
45 {
46 public:
47 wxFSFile(wxInputStream *stream, const wxString& loc,
48 const wxString& mimetype, const wxString& anchor,
49 wxDateTime modif)
50 {
51 m_Stream = stream;
52 m_Location = loc;
53 m_MimeType = mimetype; m_MimeType.MakeLower();
54 m_Anchor = anchor;
55 m_Modif = modif;
56 }
57 virtual ~wxFSFile() { if (m_Stream) delete m_Stream; }
58
59 // returns stream. This doesn't _create_ stream, it only returns
60 // pointer to it!!
61 wxInputStream *GetStream() const {return m_Stream;}
62
63 // returns file's mime type
64 const wxString& GetMimeType() const {return m_MimeType;}
65
66 // returns the original location (aka filename) of the file
67 const wxString& GetLocation() const {return m_Location;}
68
69 const wxString& GetAnchor() const {return m_Anchor;}
70
71 wxDateTime GetModificationTime() const {return m_Modif;}
72
73 private:
74 wxInputStream *m_Stream;
75 wxString m_Location;
76 wxString m_MimeType;
77 wxString m_Anchor;
78 wxDateTime m_Modif;
79
80 DECLARE_ABSTRACT_CLASS(wxFSFile)
81 };
82
83
84
85
86
87 //--------------------------------------------------------------------------------
88 // wxFileSystemHandler
89 // This class is FS handler for wxFileSystem. It provides
90 // interface to access certain
91 // kinds of files (HTPP, FTP, local, tar.gz etc..)
92 //--------------------------------------------------------------------------------
93
94 class WXDLLEXPORT wxFileSystemHandler : public wxObject
95 {
96 public:
97 wxFileSystemHandler() : wxObject() {}
98
99 // returns TRUE if this handler is able to open given location
100 virtual bool CanOpen(const wxString& location) = 0;
101
102 // opens given file and returns pointer to input stream.
103 // Returns NULL if opening failed.
104 // The location is always absolute path.
105 virtual wxFSFile* OpenFile(wxFileSystem& fs, const wxString& location) = 0;
106
107 // Finds first/next file that matches spec wildcard. flags can be wxDIR for restricting
108 // the query to directories or wxFILE for files only or 0 for either.
109 // Returns filename or empty string if no more matching file exists
110 virtual wxString FindFirst(const wxString& spec, int flags = 0);
111 virtual wxString FindNext();
112
113 protected:
114 // returns protocol ("file", "http", "tar" etc.) The last (most right)
115 // protocol is used:
116 // {it returns "tar" for "file:subdir/archive.tar.gz#tar:/README.txt"}
117 wxString GetProtocol(const wxString& location) const;
118
119 // returns left part of address:
120 // {it returns "file:subdir/archive.tar.gz" for "file:subdir/archive.tar.gz#tar:/README.txt"}
121 wxString GetLeftLocation(const wxString& location) const;
122
123 // returns anchor part of address:
124 // {it returns "anchor" for "file:subdir/archive.tar.gz#tar:/README.txt#anchor"}
125 // NOTE: anchor is NOT a part of GetLeftLocation()'s return value
126 wxString GetAnchor(const wxString& location) const;
127
128 // returns right part of address:
129 // {it returns "/README.txt" for "file:subdir/archive.tar.gz#tar:/README.txt"}
130 wxString GetRightLocation(const wxString& location) const;
131
132 // Returns MIME type of the file - w/o need to open it
133 // (default behaviour is that it returns type based on extension)
134 wxString GetMimeTypeFromExt(const wxString& location);
135
136 DECLARE_ABSTRACT_CLASS(wxFileSystemHandler)
137 };
138
139
140
141
142 //--------------------------------------------------------------------------------
143 // wxFileSystem
144 // This class provides simple interface for opening various
145 // kinds of files (HTPP, FTP, local, tar.gz etc..)
146 //--------------------------------------------------------------------------------
147
148 class WXDLLEXPORT wxFileSystem : public wxObject
149 {
150 public:
151 wxFileSystem() : wxObject() {m_Path = m_LastName = wxEmptyString; m_Handlers.DeleteContents(TRUE); m_FindFileHandler = NULL;}
152
153 // sets the current location. Every call to OpenFile is
154 // relative to this location.
155 // NOTE !!
156 // unless is_dir = TRUE 'location' is *not* the directory but
157 // file contained in this directory
158 // (so ChangePathTo("dir/subdir/xh.htm") sets m_Path to "dir/subdir/")
159 void ChangePathTo(const wxString& location, bool is_dir = FALSE);
160
161 wxString GetPath() const {return m_Path;}
162
163 // opens given file and returns pointer to input stream.
164 // Returns NULL if opening failed.
165 // It first tries to open the file in relative scope
166 // (based on ChangePathTo()'s value) and then as an absolute
167 // path.
168 wxFSFile* OpenFile(const wxString& location);
169
170 // Finds first/next file that matches spec wildcard. flags can be wxDIR for restricting
171 // the query to directories or wxFILE for files only or 0 for either.
172 // Returns filename or empty string if no more matching file exists
173 wxString FindFirst(const wxString& spec, int flags = 0);
174 wxString FindNext();
175
176 // Adds FS handler.
177 // In fact, this class is only front-end to the FS hanlers :-)
178 static void AddHandler(wxFileSystemHandler *handler);
179
180 // remove all items from the m_Handlers list
181 static void CleanUpHandlers();
182
183 // Returns the native path for a file URL
184 static wxString URLToNativePath( const wxString& url );
185
186 // Returns the file URL for a native path
187 static wxString NativePathToURL( const wxString& path );
188
189
190 protected:
191 wxString m_Path;
192 // the path (location) we are currently in
193 // this is path, not file!
194 // (so if you opened test/demo.htm, it is
195 // "test/", not "test/demo.htm")
196 wxString m_LastName;
197 // name of last opened file (full path)
198 static wxList m_Handlers;
199 // list of FS handlers
200 wxFileSystemHandler *m_FindFileHandler;
201 // handler that succeed in FindFirst query
202
203 DECLARE_DYNAMIC_CLASS(wxFileSystem)
204 };
205
206
207 /*
208
209 'location' syntax:
210
211 To determine FS type, we're using standard KDE notation:
212 file:/absolute/path/file.htm
213 file:relative_path/xxxxx.html
214 /some/path/x.file ('file:' is default)
215 http://www.gnome.org
216 file:subdir/archive.tar.gz#tar:/README.txt
217
218 special characters :
219 ':' - FS identificator is before this char
220 '#' - separator. It can be either HTML anchor ("index.html#news")
221 (in case there is no ':' in the string to the right from it)
222 or FS separator
223 (example : http://www.wxhtml.org/wxhtml-0.1.tar.gz#tar:/include/wxhtml/filesys.h"
224 this would access tgz archive stored on web)
225 '/' - directory (path) separator. It is used to determine upper-level path.
226 HEY! Don't use \ even if you're on Windows!
227
228 */
229
230
231 class wxLocalFSHandler : public wxFileSystemHandler
232 {
233 public:
234 virtual bool CanOpen(const wxString& location);
235 virtual wxFSFile* OpenFile(wxFileSystem& fs, const wxString& location);
236 virtual wxString FindFirst(const wxString& spec, int flags = 0);
237 virtual wxString FindNext();
238
239 // wxLocalFSHandler will prefix all filenames with 'root' before accessing
240 // files on disk. This effectively makes 'root' the top-level directory
241 // and prevents access to files outside this directory.
242 // (This is similar to Unix command 'chroot'.)
243 static void Chroot(const wxString& root) { ms_root = root; }
244
245 protected:
246 static wxString ms_root;
247 };
248
249
250
251 #endif
252 // wxUSE_FILESYSTEM
253
254 #endif
255 // __FILESYS_H__