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