]>
Commit | Line | Data |
---|---|---|
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 | |
6 | // Licence: wxWindows Licence | |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
9 | /* | |
10 | ||
e3e717ec | 11 | REMARKS : |
5526e819 VS |
12 | |
13 | This FS creates local cache (in /tmp directory). The cache is freed | |
14 | on program exit. | |
15 | ||
16 | Size of cache is limited to cca 1000 items (due to GetTempFileName | |
17 | limitation) | |
18 | ||
19 | ||
20 | */ | |
21 | ||
22 | #ifdef __GNUG__ | |
23 | #pragma implementation | |
24 | #endif | |
25 | ||
26 | #include <wx/wxprec.h> | |
27 | ||
28 | #ifdef __BORDLANDC__ | |
29 | #pragma hdrstop | |
30 | #endif | |
31 | ||
31528cd3 VZ |
32 | #if !wxUSE_SOCKETS |
33 | #undef wxUSE_FS_INET | |
34 | #define wxUSE_FS_INET 0 | |
35 | #endif | |
36 | ||
e3e717ec VZ |
37 | #if wxUSE_FS_INET |
38 | ||
5526e819 VS |
39 | #ifndef WXPRECOMP |
40 | #include <wx/wx.h> | |
41 | #endif | |
42 | ||
43 | #include "wx/wfstream.h" | |
44 | #include "wx/url.h" | |
45 | #include "wx/filesys.h" | |
46 | #include "wx/fs_inet.h" | |
47 | ||
48 | class wxInetCacheNode : public wxObject | |
49 | { | |
50 | private: | |
51 | wxString m_Temp; | |
52 | wxString m_Mime; | |
53 | ||
54 | public: | |
55 | wxInetCacheNode(const wxString& l, const wxString& m) : wxObject() {m_Temp = l; m_Mime = m;} | |
56 | const wxString& GetTemp() const {return m_Temp;} | |
57 | const wxString& GetMime() const {return m_Mime;} | |
58 | }; | |
59 | ||
60 | ||
61 | ||
62 | ||
63 | ||
64 | //-------------------------------------------------------------------------------- | |
65 | // wxInternetFSHandler | |
66 | //-------------------------------------------------------------------------------- | |
67 | ||
68 | ||
69 | bool wxInternetFSHandler::CanOpen(const wxString& location) | |
70 | { | |
71 | wxString p = GetProtocol(location); | |
324dbfec | 72 | return (p == _T("http")) || (p == _T("ftp")); |
5526e819 VS |
73 | } |
74 | ||
75 | ||
5526e819 VS |
76 | wxFSFile* wxInternetFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location) |
77 | { | |
324dbfec | 78 | wxString right = GetProtocol(location) + _T(":") + GetRightLocation(location); |
5526e819 VS |
79 | wxInputStream *s; |
80 | wxString content; | |
81 | wxInetCacheNode *info; | |
82 | ||
83 | info = (wxInetCacheNode*) m_Cache.Get(right); | |
84 | ||
85 | // Add item into cache: | |
f61815af | 86 | if (info == NULL) |
324dbfec | 87 | { |
5526e819 VS |
88 | wxURL url(right); |
89 | s = url.GetInputStream(); | |
90 | content = url.GetProtocol().GetContentType(); | |
91 | if (content == wxEmptyString) content = GetMimeTypeFromExt(location); | |
913df6f2 | 92 | if (s) |
324dbfec | 93 | { |
e7fc59f4 | 94 | wxChar buf[256]; |
5526e819 | 95 | |
e7fc59f4 | 96 | wxGetTempFileName( _T("wxhtml"), buf); |
5526e819 VS |
97 | info = new wxInetCacheNode(buf, content); |
98 | m_Cache.Put(right, info); | |
99 | ||
100 | { // ok, now copy it: | |
913df6f2 DW |
101 | #if defined(__VISAGECPP__) |
102 | // VA thinks this is an ambiguous call | |
103 | wxFileOutputStream sout((wxString)buf); | |
104 | #else | |
5526e819 | 105 | wxFileOutputStream sout(buf); |
913df6f2 | 106 | #endif |
5526e819 VS |
107 | s -> Read(sout); // copy the stream |
108 | } | |
109 | delete s; | |
110 | } | |
324dbfec | 111 | else |
913df6f2 DW |
112 | { |
113 | return (wxFSFile*) NULL; // we can't open the URL | |
114 | } | |
5526e819 VS |
115 | } |
116 | ||
117 | // Load item from cache: | |
324dbfec | 118 | s = new wxFileInputStream(info->GetTemp()); |
913df6f2 | 119 | if (s) |
324dbfec | 120 | { |
5526e819 VS |
121 | return new wxFSFile(s, |
122 | right, | |
324dbfec | 123 | info->GetMime(), |
5526e819 VS |
124 | GetAnchor(location)); |
125 | } | |
324dbfec | 126 | else return (wxFSFile*) NULL; |
5526e819 VS |
127 | } |
128 | ||
129 | ||
130 | ||
131 | wxInternetFSHandler::~wxInternetFSHandler() | |
132 | { | |
133 | wxNode *n; | |
134 | wxInetCacheNode *n2; | |
135 | ||
136 | m_Cache.BeginFind(); | |
913df6f2 | 137 | while ((n = m_Cache.Next()) != NULL) |
324dbfec RR |
138 | { |
139 | n2 = (wxInetCacheNode*) n->GetData(); | |
140 | wxRemoveFile(n2->GetTemp()); | |
5526e819 VS |
141 | delete n2; |
142 | } | |
143 | } | |
144 | ||
f61815af GL |
145 | class wxFileSystemInternetModule : public wxModule |
146 | { | |
147 | DECLARE_DYNAMIC_CLASS(wxFileSystemInternetModule) | |
148 | ||
149 | public: | |
150 | virtual bool OnInit() | |
151 | { | |
152 | wxFileSystem::AddHandler(new wxInternetFSHandler); | |
153 | return TRUE; | |
154 | } | |
155 | virtual void OnExit() {} | |
156 | }; | |
157 | ||
158 | IMPLEMENT_DYNAMIC_CLASS(wxFileSystemInternetModule, wxModule) | |
159 | ||
e3e717ec | 160 | #endif // wxUSE_FS_INET |