]> git.saurik.com Git - wxWidgets.git/blame - src/common/fs_inet.cpp
Commented out call that breaks metal style setting before Create
[wxWidgets.git] / src / common / fs_inet.cpp
CommitLineData
5526e819
VS
1/////////////////////////////////////////////////////////////////////////////
2// Name: fs_inet.cpp
3// Purpose: HTTP and FTP file system
4// Author: Vaclav Slavik
5// Copyright: (c) 1999 Vaclav Slavik
65571936 6// Licence: wxWindows licence
5526e819
VS
7/////////////////////////////////////////////////////////////////////////////
8
3096bd2f 9#include "wx/wxprec.h"
5526e819 10
2b5f62a0 11#ifdef __BORLANDC__
5526e819
VS
12#pragma hdrstop
13#endif
14
31528cd3
VZ
15#if !wxUSE_SOCKETS
16 #undef wxUSE_FS_INET
17 #define wxUSE_FS_INET 0
18#endif
19
24528b0c 20#if wxUSE_FILESYSTEM && wxUSE_FS_INET
e3e717ec 21
5526e819 22#ifndef WXPRECOMP
5526e819
VS
23#endif
24
25#include "wx/wfstream.h"
26#include "wx/url.h"
27#include "wx/filesys.h"
28#include "wx/fs_inet.h"
cf1f0870 29#include "wx/module.h"
5526e819 30
3caa34b7
VS
31// ----------------------------------------------------------------------------
32// Helper classes
33// ----------------------------------------------------------------------------
34
35// This stream deletes the file when destroyed
36class wxTemporaryFileInputStream : public wxFileInputStream
5526e819 37{
3caa34b7
VS
38public:
39 wxTemporaryFileInputStream(const wxString& filename) :
40 wxFileInputStream(filename), m_filename(filename) {}
a62848fd 41
3caa34b7
VS
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 }
a62848fd 51 wxRemoveFile(m_filename);
3caa34b7 52 }
5526e819 53
3caa34b7
VS
54protected:
55 wxString m_filename;
5526e819
VS
56};
57
58
3caa34b7 59// ----------------------------------------------------------------------------
5526e819 60// wxInternetFSHandler
3caa34b7 61// ----------------------------------------------------------------------------
5526e819 62
c3a1c7d2
VS
63static wxString StripProtocolAnchor(const wxString& location)
64{
65 wxString myloc(location.BeforeLast(wxT('#')));
489f6cf7 66 if (myloc.empty()) myloc = location.AfterFirst(wxT(':'));
c3a1c7d2
VS
67 else myloc = myloc.AfterFirst(wxT(':'));
68
69 // fix malformed url:
489f6cf7 70 if (!myloc.Left(2).IsSameAs(wxT("//")))
c3a1c7d2
VS
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
5526e819
VS
81bool wxInternetFSHandler::CanOpen(const wxString& location)
82{
34e0d9f8 83#if wxUSE_URL
5526e819 84 wxString p = GetProtocol(location);
04dbb646 85 if ((p == wxT("http")) || (p == wxT("ftp")))
81915974 86 {
c3a1c7d2 87 wxURL url(p + wxT(":") + StripProtocolAnchor(location));
81915974
VS
88 return (url.GetError() == wxURL_NOERR);
89 }
34e0d9f8 90#endif
a62848fd 91 return false;
5526e819
VS
92}
93
94
3caa34b7
VS
95wxFSFile* wxInternetFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs),
96 const wxString& location)
5526e819 97{
34e0d9f8
JS
98#if !wxUSE_URL
99 return NULL;
100#else
3caa34b7
VS
101 wxString right =
102 GetProtocol(location) + wxT(":") + StripProtocolAnchor(location);
5526e819 103
3caa34b7
VS
104 wxURL url(right);
105 if (url.GetError() == wxURL_NOERR)
324dbfec 106 {
3caa34b7
VS
107 wxInputStream *s = url.GetInputStream();
108 wxString content = url.GetProtocol().GetContentType();
109 if (content == wxEmptyString) content = GetMimeTypeFromExt(location);
110 if (s)
07d4b94c 111 {
3caa34b7
VS
112 wxString tmpfile =
113 wxFileName::CreateTempFileName(wxT("wxhtml"));
5526e819 114
3caa34b7
VS
115 { // now copy streams content to temporary file:
116 wxFileOutputStream sout(tmpfile);
117 s->Read(sout);
118 }
119 delete s;
a62848fd 120
3caa34b7
VS
121 return new wxFSFile(new wxTemporaryFileInputStream(tmpfile),
122 right,
123 content,
124 GetAnchor(location)
e2b87f38 125#if wxUSE_DATETIME
3caa34b7 126 , wxDateTime::Now()
e2b87f38
VZ
127#endif // wxUSE_DATETIME
128 );
3caa34b7 129 }
5526e819 130 }
3caa34b7
VS
131
132 return (wxFSFile*) NULL; // incorrect URL
34e0d9f8 133#endif
5526e819
VS
134}
135
3caa34b7 136
f61815af
GL
137class wxFileSystemInternetModule : public wxModule
138{
139 DECLARE_DYNAMIC_CLASS(wxFileSystemInternetModule)
140
141 public:
142 virtual bool OnInit()
143 {
144 wxFileSystem::AddHandler(new wxInternetFSHandler);
a62848fd 145 return true;
f61815af
GL
146 }
147 virtual void OnExit() {}
148};
149
150IMPLEMENT_DYNAMIC_CLASS(wxFileSystemInternetModule, wxModule)
151
24528b0c 152#endif // wxUSE_FILESYSTEM && wxUSE_FS_INET