]>
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 | ||
e3e717ec VZ |
32 | #if wxUSE_FS_INET |
33 | ||
5526e819 VS |
34 | #ifndef WXPRECOMP |
35 | #include <wx/wx.h> | |
36 | #endif | |
37 | ||
38 | #include "wx/wfstream.h" | |
39 | #include "wx/url.h" | |
40 | #include "wx/filesys.h" | |
41 | #include "wx/fs_inet.h" | |
42 | ||
43 | class wxInetCacheNode : public wxObject | |
44 | { | |
45 | private: | |
46 | wxString m_Temp; | |
47 | wxString m_Mime; | |
48 | ||
49 | public: | |
50 | wxInetCacheNode(const wxString& l, const wxString& m) : wxObject() {m_Temp = l; m_Mime = m;} | |
51 | const wxString& GetTemp() const {return m_Temp;} | |
52 | const wxString& GetMime() const {return m_Mime;} | |
53 | }; | |
54 | ||
55 | ||
56 | ||
57 | ||
58 | ||
59 | //-------------------------------------------------------------------------------- | |
60 | // wxInternetFSHandler | |
61 | //-------------------------------------------------------------------------------- | |
62 | ||
63 | ||
64 | bool wxInternetFSHandler::CanOpen(const wxString& location) | |
65 | { | |
66 | wxString p = GetProtocol(location); | |
324dbfec | 67 | return (p == _T("http")) || (p == _T("ftp")); |
5526e819 VS |
68 | } |
69 | ||
70 | ||
5526e819 VS |
71 | wxFSFile* wxInternetFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location) |
72 | { | |
324dbfec | 73 | wxString right = GetProtocol(location) + _T(":") + GetRightLocation(location); |
5526e819 VS |
74 | wxInputStream *s; |
75 | wxString content; | |
76 | wxInetCacheNode *info; | |
77 | ||
78 | info = (wxInetCacheNode*) m_Cache.Get(right); | |
79 | ||
80 | // Add item into cache: | |
324dbfec RR |
81 | if (!info == NULL) |
82 | { | |
5526e819 VS |
83 | wxURL url(right); |
84 | s = url.GetInputStream(); | |
85 | content = url.GetProtocol().GetContentType(); | |
86 | if (content == wxEmptyString) content = GetMimeTypeFromExt(location); | |
324dbfec RR |
87 | if (s) |
88 | { | |
5526e819 VS |
89 | char buf[256]; |
90 | ||
324dbfec | 91 | wxGetTempFileName( "wxhtml", buf); |
5526e819 VS |
92 | info = new wxInetCacheNode(buf, content); |
93 | m_Cache.Put(right, info); | |
94 | ||
95 | { // ok, now copy it: | |
96 | wxFileOutputStream sout(buf); | |
97 | s -> Read(sout); // copy the stream | |
98 | } | |
99 | delete s; | |
100 | } | |
324dbfec RR |
101 | else |
102 | { | |
103 | return (wxFSFile*) NULL; // we can't open the URL | |
104 | } | |
5526e819 VS |
105 | } |
106 | ||
107 | // Load item from cache: | |
324dbfec RR |
108 | s = new wxFileInputStream(info->GetTemp()); |
109 | if (s) | |
110 | { | |
5526e819 VS |
111 | return new wxFSFile(s, |
112 | right, | |
324dbfec | 113 | info->GetMime(), |
5526e819 VS |
114 | GetAnchor(location)); |
115 | } | |
324dbfec | 116 | else return (wxFSFile*) NULL; |
5526e819 VS |
117 | } |
118 | ||
119 | ||
120 | ||
121 | wxInternetFSHandler::~wxInternetFSHandler() | |
122 | { | |
123 | wxNode *n; | |
124 | wxInetCacheNode *n2; | |
125 | ||
126 | m_Cache.BeginFind(); | |
324dbfec RR |
127 | while ((n = m_Cache.Next()) != NULL) |
128 | { | |
129 | n2 = (wxInetCacheNode*) n->GetData(); | |
130 | wxRemoveFile(n2->GetTemp()); | |
5526e819 VS |
131 | delete n2; |
132 | } | |
133 | } | |
134 | ||
e3e717ec | 135 | #endif // wxUSE_FS_INET |