]> git.saurik.com Git - wxWidgets.git/blob - src/common/fs_inet.cpp
fixing focus handling for generic controls in carbon-cfm
[wxWidgets.git] / src / common / fs_inet.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: fs_inet.cpp
3 // Purpose: HTTP and FTP file system
4 // Author: Vaclav Slavik
5 // Copyright: (c) 1999 Vaclav Slavik
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8
9 #include "wx/wxprec.h"
10
11 #ifdef __BORLANDC__
12 #pragma hdrstop
13 #endif
14
15 #if !wxUSE_SOCKETS
16 #undef wxUSE_FS_INET
17 #define wxUSE_FS_INET 0
18 #endif
19
20 #if wxUSE_FILESYSTEM && wxUSE_FS_INET
21
22 #ifndef WXPRECOMP
23 #endif
24
25 #include "wx/wfstream.h"
26 #include "wx/url.h"
27 #include "wx/filesys.h"
28 #include "wx/fs_inet.h"
29 #include "wx/module.h"
30
31 // ----------------------------------------------------------------------------
32 // Helper classes
33 // ----------------------------------------------------------------------------
34
35 // This stream deletes the file when destroyed
36 class wxTemporaryFileInputStream : public wxFileInputStream
37 {
38 public:
39 wxTemporaryFileInputStream(const wxString& filename) :
40 wxFileInputStream(filename), m_filename(filename) {}
41
42 ~wxTemporaryFileInputStream()
43 {
44 // NB: copied from wxFileInputStream dtor, we need to do it before
45 // wxRemoveFile
46 if (m_file_destroy)
47 {
48 delete m_file;
49 m_file_destroy = false;
50 }
51 wxRemoveFile(m_filename);
52 }
53
54 protected:
55 wxString m_filename;
56 };
57
58
59 // ----------------------------------------------------------------------------
60 // wxInternetFSHandler
61 // ----------------------------------------------------------------------------
62
63 static wxString StripProtocolAnchor(const wxString& location)
64 {
65 wxString myloc(location.BeforeLast(wxT('#')));
66 if (myloc.empty()) myloc = location.AfterFirst(wxT(':'));
67 else myloc = myloc.AfterFirst(wxT(':'));
68
69 // fix malformed url:
70 if (!myloc.Left(2).IsSameAs(wxT("//")))
71 {
72 if (myloc.GetChar(0) != wxT('/')) myloc = wxT("//") + myloc;
73 else myloc = wxT("/") + myloc;
74 }
75 if (myloc.Mid(2).Find(wxT('/')) == wxNOT_FOUND) myloc << wxT('/');
76
77 return myloc;
78 }
79
80
81 bool wxInternetFSHandler::CanOpen(const wxString& location)
82 {
83 #if wxUSE_URL
84 wxString p = GetProtocol(location);
85 if ((p == wxT("http")) || (p == wxT("ftp")))
86 {
87 wxURL url(p + wxT(":") + StripProtocolAnchor(location));
88 return (url.GetError() == wxURL_NOERR);
89 }
90 #endif
91 return false;
92 }
93
94
95 wxFSFile* wxInternetFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs),
96 const wxString& location)
97 {
98 #if !wxUSE_URL
99 return NULL;
100 #else
101 wxString right =
102 GetProtocol(location) + wxT(":") + StripProtocolAnchor(location);
103
104 wxURL url(right);
105 if (url.GetError() == wxURL_NOERR)
106 {
107 wxInputStream *s = url.GetInputStream();
108 wxString content = url.GetProtocol().GetContentType();
109 if (content == wxEmptyString) content = GetMimeTypeFromExt(location);
110 if (s)
111 {
112 wxString tmpfile =
113 wxFileName::CreateTempFileName(wxT("wxhtml"));
114
115 { // now copy streams content to temporary file:
116 wxFileOutputStream sout(tmpfile);
117 s->Read(sout);
118 }
119 delete s;
120
121 return new wxFSFile(new wxTemporaryFileInputStream(tmpfile),
122 right,
123 content,
124 GetAnchor(location)
125 #if wxUSE_DATETIME
126 , wxDateTime::Now()
127 #endif // wxUSE_DATETIME
128 );
129 }
130 }
131
132 return (wxFSFile*) NULL; // incorrect URL
133 #endif
134 }
135
136
137 class wxFileSystemInternetModule : public wxModule
138 {
139 DECLARE_DYNAMIC_CLASS(wxFileSystemInternetModule)
140
141 public:
142 virtual bool OnInit()
143 {
144 wxFileSystem::AddHandler(new wxInternetFSHandler);
145 return true;
146 }
147 virtual void OnExit() {}
148 };
149
150 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemInternetModule, wxModule)
151
152 #endif // wxUSE_FILESYSTEM && wxUSE_FS_INET