]> git.saurik.com Git - wxWidgets.git/blob - src/common/fs_inet.cpp
Workaround for #15404: wxRichTextCtrl: caret does not disappear when focus is lost...
[wxWidgets.git] / src / common / fs_inet.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/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 WX_PRECOMP
23 #include "wx/module.h"
24 #endif
25
26 #include "wx/wfstream.h"
27 #include "wx/url.h"
28 #include "wx/filesys.h"
29 #include "wx/fs_inet.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 virtual ~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 (s)
110 {
111 wxString tmpfile =
112 wxFileName::CreateTempFileName(wxT("wxhtml"));
113
114 { // now copy streams content to temporary file:
115 wxFileOutputStream sout(tmpfile);
116 s->Read(sout);
117 }
118 delete s;
119
120 return new wxFSFile(new wxTemporaryFileInputStream(tmpfile),
121 right,
122 content,
123 GetAnchor(location)
124 #if wxUSE_DATETIME
125 , wxDateTime::Now()
126 #endif // wxUSE_DATETIME
127 );
128 }
129 }
130
131 return NULL; // incorrect URL
132 #endif
133 }
134
135
136 class wxFileSystemInternetModule : public wxModule
137 {
138 DECLARE_DYNAMIC_CLASS(wxFileSystemInternetModule)
139
140 public:
141 wxFileSystemInternetModule() :
142 wxModule(),
143 m_handler(NULL)
144 {
145 }
146
147 virtual bool OnInit()
148 {
149 m_handler = new wxInternetFSHandler;
150 wxFileSystem::AddHandler(m_handler);
151 return true;
152 }
153
154 virtual void OnExit()
155 {
156 delete wxFileSystem::RemoveHandler(m_handler);
157 }
158
159 private:
160 wxFileSystemHandler* m_handler;
161 };
162
163 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemInternetModule, wxModule)
164
165 #endif // wxUSE_FILESYSTEM && wxUSE_FS_INET