]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/fs_inet.cpp
added wxAuiNotebook::Split() documentation
[wxWidgets.git] / src / common / fs_inet.cpp
... / ...
CommitLineData
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// RCS-ID: $Id$
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10#include "wx/wxprec.h"
11
12#ifdef __BORLANDC__
13 #pragma hdrstop
14#endif
15
16#if !wxUSE_SOCKETS
17 #undef wxUSE_FS_INET
18 #define wxUSE_FS_INET 0
19#endif
20
21#if wxUSE_FILESYSTEM && wxUSE_FS_INET
22
23#ifndef WXPRECOMP
24 #include "wx/module.h"
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
32// ----------------------------------------------------------------------------
33// Helper classes
34// ----------------------------------------------------------------------------
35
36// This stream deletes the file when destroyed
37class wxTemporaryFileInputStream : public wxFileInputStream
38{
39public:
40 wxTemporaryFileInputStream(const wxString& filename) :
41 wxFileInputStream(filename), m_filename(filename) {}
42
43 virtual ~wxTemporaryFileInputStream()
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 }
52 wxRemoveFile(m_filename);
53 }
54
55protected:
56 wxString m_filename;
57};
58
59
60// ----------------------------------------------------------------------------
61// wxInternetFSHandler
62// ----------------------------------------------------------------------------
63
64static wxString StripProtocolAnchor(const wxString& location)
65{
66 wxString myloc(location.BeforeLast(wxT('#')));
67 if (myloc.empty()) myloc = location.AfterFirst(wxT(':'));
68 else myloc = myloc.AfterFirst(wxT(':'));
69
70 // fix malformed url:
71 if (!myloc.Left(2).IsSameAs(wxT("//")))
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
82bool wxInternetFSHandler::CanOpen(const wxString& location)
83{
84#if wxUSE_URL
85 wxString p = GetProtocol(location);
86 if ((p == wxT("http")) || (p == wxT("ftp")))
87 {
88 wxURL url(p + wxT(":") + StripProtocolAnchor(location));
89 return (url.GetError() == wxURL_NOERR);
90 }
91#endif
92 return false;
93}
94
95
96wxFSFile* wxInternetFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs),
97 const wxString& location)
98{
99#if !wxUSE_URL
100 return NULL;
101#else
102 wxString right =
103 GetProtocol(location) + wxT(":") + StripProtocolAnchor(location);
104
105 wxURL url(right);
106 if (url.GetError() == wxURL_NOERR)
107 {
108 wxInputStream *s = url.GetInputStream();
109 wxString content = url.GetProtocol().GetContentType();
110 if (content == wxEmptyString) content = GetMimeTypeFromExt(location);
111 if (s)
112 {
113 wxString tmpfile =
114 wxFileName::CreateTempFileName(wxT("wxhtml"));
115
116 { // now copy streams content to temporary file:
117 wxFileOutputStream sout(tmpfile);
118 s->Read(sout);
119 }
120 delete s;
121
122 return new wxFSFile(new wxTemporaryFileInputStream(tmpfile),
123 right,
124 content,
125 GetAnchor(location)
126#if wxUSE_DATETIME
127 , wxDateTime::Now()
128#endif // wxUSE_DATETIME
129 );
130 }
131 }
132
133 return (wxFSFile*) NULL; // incorrect URL
134#endif
135}
136
137
138class wxFileSystemInternetModule : public wxModule
139{
140 DECLARE_DYNAMIC_CLASS(wxFileSystemInternetModule)
141
142 public:
143 wxFileSystemInternetModule() :
144 wxModule(),
145 m_handler(NULL)
146 {
147 }
148
149 virtual bool OnInit()
150 {
151 m_handler = new wxInternetFSHandler;
152 wxFileSystem::AddHandler(m_handler);
153 return true;
154 }
155
156 virtual void OnExit()
157 {
158 delete wxFileSystem::RemoveHandler(m_handler);
159 }
160
161 private:
162 wxFileSystemHandler* m_handler;
163};
164
165IMPLEMENT_DYNAMIC_CLASS(wxFileSystemInternetModule, wxModule)
166
167#endif // wxUSE_FILESYSTEM && wxUSE_FS_INET