]> git.saurik.com Git - wxWidgets.git/blob - src/common/filesys.cpp
fixed code in OpenURL() to actually use the result of OnOpeningURL()
[wxWidgets.git] / src / common / filesys.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: filesys.cpp
3 // Purpose: wxFileSystem class - interface for opening files
4 // Author: Vaclav Slavik
5 // Copyright: (c) 1999 Vaclav Slavik
6 // CVS-ID: $Id$
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation
12 #endif
13
14 #include "wx/wxprec.h"
15
16 #ifdef __BORDLANDC__
17 #pragma hdrstop
18 #endif
19
20
21 #if wxUSE_FILESYSTEM
22
23 #include "wx/wfstream.h"
24 #include "wx/module.h"
25 #include "wx/filesys.h"
26 #include "wx/mimetype.h"
27 #include "wx/filename.h"
28
29
30
31 //--------------------------------------------------------------------------------
32 // wxFileSystemHandler
33 //--------------------------------------------------------------------------------
34
35 IMPLEMENT_ABSTRACT_CLASS(wxFileSystemHandler, wxObject)
36
37
38 #if wxUSE_MIMETYPE
39 static wxFileTypeInfo *gs_FSMimeFallbacks = NULL;
40 #endif
41
42 wxString wxFileSystemHandler::GetMimeTypeFromExt(const wxString& location)
43 {
44 wxString ext, mime;
45 wxString loc = GetRightLocation(location);
46 char c;
47 int l = loc.Length(), l2;
48
49 l2 = l;
50 for (int i = l-1; i >= 0; i--)
51 {
52 c = loc[(unsigned int) i];
53 if ( c == wxT('#') )
54 l2 = i + 1;
55 if ( c == wxT('.') )
56 {
57 ext = loc.Right(l2-i-1);
58 break;
59 }
60 if ( (c == wxT('/')) || (c == wxT('\\')) || (c == wxT(':')) )
61 return wxEmptyString;
62 }
63
64 #if wxUSE_MIMETYPE
65 static bool s_MinimalMimeEnsured = FALSE;
66 if (!s_MinimalMimeEnsured) {
67 wxTheMimeTypesManager->AddFallbacks(gs_FSMimeFallbacks);
68 s_MinimalMimeEnsured = TRUE;
69 }
70
71 wxFileType *ft = wxTheMimeTypesManager->GetFileTypeFromExtension(ext);
72 if ( !ft || !ft -> GetMimeType(&mime) )
73 {
74 mime = wxEmptyString;
75 }
76
77 delete ft;
78
79 return mime;
80 #else
81 if ( ext.IsSameAs(wxT("htm"), FALSE) || ext.IsSameAs(_T("html"), FALSE) )
82 return wxT("text/html");
83 if ( ext.IsSameAs(wxT("jpg"), FALSE) || ext.IsSameAs(_T("jpeg"), FALSE) )
84 return wxT("image/jpeg");
85 if ( ext.IsSameAs(wxT("gif"), FALSE) )
86 return wxT("image/gif");
87 if ( ext.IsSameAs(wxT("png"), FALSE) )
88 return wxT("image/png");
89 if ( ext.IsSameAs(wxT("bmp"), FALSE) )
90 return wxT("image/bmp");
91 return wxEmptyString;
92 #endif
93 }
94
95
96
97 wxString wxFileSystemHandler::GetProtocol(const wxString& location) const
98 {
99 wxString s = wxEmptyString;
100 int i, l = location.Length();
101 bool fnd;
102
103 fnd = FALSE;
104 for (i = l-1; (i >= 0) && ((location[i] != wxT('#')) || (!fnd)); i--) {
105 if ((location[i] == wxT(':')) && (i != 1 /*win: C:\path*/)) fnd = TRUE;
106 }
107 if (!fnd) return wxT("file");
108 for (++i; (i < l) && (location[i] != wxT(':')); i++) s << location[i];
109 return s;
110 }
111
112
113 wxString wxFileSystemHandler::GetLeftLocation(const wxString& location) const
114 {
115 int i;
116 bool fnd;
117
118 fnd = FALSE;
119 for (i = location.Length()-1; i >= 0; i--) {
120 if ((location[i] == wxT(':')) && (i != 1 /*win: C:\path*/)) fnd = TRUE;
121 else if (fnd && (location[i] == wxT('#'))) return location.Left(i);
122 }
123 return wxEmptyString;
124 }
125
126 wxString wxFileSystemHandler::GetRightLocation(const wxString& location) const
127 {
128 int i, l = location.Length();
129 int l2 = l + 1;
130 for (i = l-1; (i >= 0) && ((location[i] != wxT(':')) || (i == 1) || (location[i-2] == wxT(':'))); i--) {if (location[i] == wxT('#')) l2 = i + 1;}
131 if (i == 0) return wxEmptyString;
132 else return location.Mid(i + 1, l2 - i - 2);
133 }
134
135 wxString wxFileSystemHandler::GetAnchor(const wxString& location) const
136 {
137 char c;
138 int l = location.Length();
139
140 for (int i = l-1; i >= 0; i--) {
141 c = location[i];
142 if (c == wxT('#')) return location.Right(l-i-1);
143 else if ((c == wxT('.')) || (c == wxT('/')) || (c == wxT('\\')) || (c == wxT(':'))) return wxEmptyString;
144 }
145 return wxEmptyString;
146 }
147
148
149 wxString wxFileSystemHandler::FindFirst(const wxString& WXUNUSED(spec),
150 int WXUNUSED(flags))
151 {
152 return wxEmptyString;
153 }
154
155 wxString wxFileSystemHandler::FindNext()
156 {
157 return wxEmptyString;
158 }
159
160 //--------------------------------------------------------------------------------
161 // wxLocalFSHandler
162 //--------------------------------------------------------------------------------
163
164
165 wxString wxLocalFSHandler::ms_root;
166
167 bool wxLocalFSHandler::CanOpen(const wxString& location)
168 {
169 return GetProtocol(location) == wxT("file");
170 }
171
172 wxFSFile* wxLocalFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location)
173 {
174 // location has Unix path separators
175 wxString right = ms_root + GetRightLocation(location);
176 wxFileName fn(right, wxPATH_UNIX);
177
178 if (!wxFileExists(fn.GetFullPath()))
179 return (wxFSFile*) NULL;
180
181 return new wxFSFile(new wxFileInputStream(fn.GetFullPath()),
182 right,
183 GetMimeTypeFromExt(location),
184 GetAnchor(location),
185 wxDateTime(wxFileModificationTime(fn.GetFullPath())));
186
187 }
188
189 wxString wxLocalFSHandler::FindFirst(const wxString& spec, int flags)
190 {
191 wxString right = ms_root + GetRightLocation(spec);
192 return wxFindFirstFile(right, flags);
193 }
194
195 wxString wxLocalFSHandler::FindNext()
196 {
197 return wxFindNextFile();
198 }
199
200
201
202 //-----------------------------------------------------------------------------
203 // wxFileSystem
204 //-----------------------------------------------------------------------------
205
206 IMPLEMENT_DYNAMIC_CLASS(wxFileSystem, wxObject)
207
208
209 wxList wxFileSystem::m_Handlers;
210
211
212 static wxString MakeCorrectPath(const wxString& path)
213 {
214 wxString p(path);
215 wxString r;
216 int i, j, cnt;
217
218 cnt = p.Length();
219 for (i = 0; i < cnt; i++)
220 if (p.GetChar(i) == wxT('\\')) p.GetWritableChar(i) = wxT('/'); // Want to be windows-safe
221
222 if (p.Left(2) == wxT("./")) { p = p.Mid(2); cnt -= 2; }
223
224 if (cnt < 3) return p;
225
226 r << p.GetChar(0) << p.GetChar(1);
227
228 // skip trailing ../.., if any
229 for (i = 2; i < cnt && (p.GetChar(i) == wxT('/') || p.GetChar(i) == wxT('.')); i++) r << p.GetChar(i);
230
231 // remove back references: translate dir1/../dir2 to dir2
232 for (; i < cnt; i++)
233 {
234 r << p.GetChar(i);
235 if (p.GetChar(i) == wxT('/') && p.GetChar(i-1) == wxT('.') && p.GetChar(i-2) == wxT('.'))
236 {
237 for (j = r.Length() - 2; j >= 0 && r.GetChar(j) != wxT('/') && r.GetChar(j) != wxT(':'); j--) {}
238 if (j >= 0 && r.GetChar(j) != wxT(':'))
239 {
240 for (j = j - 1; j >= 0 && r.GetChar(j) != wxT('/') && r.GetChar(j) != wxT(':'); j--) {}
241 r.Remove(j + 1);
242 }
243 }
244 }
245
246 for (; i < cnt; i++) r << p.GetChar(i);
247
248 return r;
249 }
250
251
252 void wxFileSystem::ChangePathTo(const wxString& location, bool is_dir)
253 {
254 int i, pathpos = -1;
255
256 m_Path = MakeCorrectPath(location);
257
258 if (is_dir)
259 {
260 if (m_Path.Length() > 0 && m_Path.Last() != wxT('/') && m_Path.Last() != wxT(':'))
261 m_Path << wxT('/');
262 }
263
264 else
265 {
266 for (i = m_Path.Length()-1; i >= 0; i--)
267 {
268 if (m_Path[(unsigned int) i] == wxT('/'))
269 {
270 if ((i > 1) && (m_Path[(unsigned int) (i-1)] == wxT('/')) && (m_Path[(unsigned int) (i-2)] == wxT(':')))
271 {
272 i -= 2;
273 continue;
274 }
275 else
276 {
277 pathpos = i;
278 break;
279 }
280 }
281 else if (m_Path[(unsigned int) i] == wxT(':')) {
282 pathpos = i;
283 break;
284 }
285 }
286 if (pathpos == -1)
287 {
288 for (i = 0; i < (int) m_Path.Length(); i++)
289 {
290 if (m_Path[(unsigned int) i] == wxT(':'))
291 {
292 m_Path.Remove(i+1);
293 break;
294 }
295 }
296 if (i == (int) m_Path.Length())
297 m_Path = wxEmptyString;
298 }
299 else
300 {
301 m_Path.Remove(pathpos+1);
302 }
303 }
304 }
305
306
307
308 wxFSFile* wxFileSystem::OpenFile(const wxString& location)
309 {
310 wxString loc = MakeCorrectPath(location);
311 unsigned i, ln;
312 char meta;
313 wxFSFile *s = NULL;
314 wxNode *node;
315
316 ln = loc.Length();
317 meta = 0;
318 for (i = 0; i < ln; i++)
319 {
320 switch (loc[i])
321 {
322 case wxT('/') : case wxT(':') : case wxT('#') :
323 meta = loc[i];
324 break;
325 }
326 if (meta != 0) break;
327 }
328 m_LastName = wxEmptyString;
329
330 // try relative paths first :
331 if (meta != wxT(':'))
332 {
333 node = m_Handlers.GetFirst();
334 while (node)
335 {
336 wxFileSystemHandler *h = (wxFileSystemHandler*) node -> GetData();
337 if (h->CanOpen(m_Path + loc))
338 {
339 s = h->OpenFile(*this, m_Path + loc);
340 if (s) { m_LastName = m_Path + loc; break; }
341 }
342 node = node->GetNext();
343 }
344 }
345
346 // if failed, try absolute paths :
347 if (s == NULL)
348 {
349 node = m_Handlers.GetFirst();
350 while (node)
351 {
352 wxFileSystemHandler *h = (wxFileSystemHandler*) node->GetData();
353 if (h->CanOpen(loc))
354 {
355 s = h->OpenFile(*this, loc);
356 if (s) { m_LastName = loc; break; }
357 }
358 node = node->GetNext();
359 }
360 }
361 return (s);
362 }
363
364
365
366 wxString wxFileSystem::FindFirst(const wxString& spec, int flags)
367 {
368 wxNode *node;
369 wxString spec2(spec);
370
371 m_FindFileHandler = NULL;
372
373 for (int i = spec2.Length()-1; i >= 0; i--)
374 if (spec2[(unsigned int) i] == wxT('\\')) spec2.GetWritableChar(i) = wxT('/'); // Want to be windows-safe
375
376 node = m_Handlers.GetFirst();
377 while (node)
378 {
379 m_FindFileHandler = (wxFileSystemHandler*) node -> GetData();
380 if (m_FindFileHandler -> CanOpen(m_Path + spec2))
381 return m_FindFileHandler -> FindFirst(m_Path + spec2, flags);
382 node = node->GetNext();
383 }
384
385 node = m_Handlers.GetFirst();
386 while (node)
387 {
388 m_FindFileHandler = (wxFileSystemHandler*) node -> GetData();
389 if (m_FindFileHandler -> CanOpen(spec2))
390 return m_FindFileHandler -> FindFirst(spec2, flags);
391 node = node->GetNext();
392 }
393
394 return wxEmptyString;
395 }
396
397
398
399 wxString wxFileSystem::FindNext()
400 {
401 if (m_FindFileHandler == NULL) return wxEmptyString;
402 else return m_FindFileHandler -> FindNext();
403 }
404
405
406
407 void wxFileSystem::AddHandler(wxFileSystemHandler *handler)
408 {
409 m_Handlers.Append(handler);
410 }
411
412
413 void wxFileSystem::CleanUpHandlers()
414 {
415 m_Handlers.DeleteContents(TRUE);
416 m_Handlers.Clear();
417 }
418
419
420
421
422 ///// Module:
423
424 class wxFileSystemModule : public wxModule
425 {
426 DECLARE_DYNAMIC_CLASS(wxFileSystemModule)
427
428 public:
429 virtual bool OnInit()
430 {
431 wxFileSystem::AddHandler(new wxLocalFSHandler);
432
433 #if wxUSE_MIMETYPE
434 gs_FSMimeFallbacks = new wxFileTypeInfo[6];
435 gs_FSMimeFallbacks[0] =
436 wxFileTypeInfo("image/jpeg",
437 "",
438 "",
439 "JPEG image (from fallback)",
440 "jpg", "jpeg", NULL);
441 gs_FSMimeFallbacks[1] =
442 wxFileTypeInfo("image/gif",
443 "",
444 "",
445 "GIF image (from fallback)",
446 "gif", NULL);
447 gs_FSMimeFallbacks[2] =
448 wxFileTypeInfo("image/png",
449 "",
450 "",
451 "PNG image (from fallback)",
452 "png", NULL);
453 gs_FSMimeFallbacks[3] =
454 wxFileTypeInfo("image/bmp",
455 "",
456 "",
457 "windows bitmap image (from fallback)",
458 "bmp", NULL);
459 gs_FSMimeFallbacks[4] =
460 wxFileTypeInfo("text/html",
461 "",
462 "",
463 "HTML document (from fallback)",
464 "htm", "html", NULL);
465 gs_FSMimeFallbacks[5] =
466 // must terminate the table with this!
467 wxFileTypeInfo();
468 #endif
469 return TRUE;
470 }
471 virtual void OnExit()
472 {
473 #if wxUSE_MIMETYPE
474 delete [] gs_FSMimeFallbacks;
475 #endif
476 wxFileSystem::CleanUpHandlers();
477 }
478 };
479
480 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule, wxModule)
481
482 #endif
483 // wxUSE_FILESYSTEM
484
485
486