]>
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); | |
67 | return (p == "http") || (p == "ftp"); | |
68 | } | |
69 | ||
70 | ||
71 | ||
72 | ||
73 | wxFSFile* wxInternetFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location) | |
74 | { | |
75 | wxString right = GetProtocol(location) + ":" + GetRightLocation(location); | |
76 | wxInputStream *s; | |
77 | wxString content; | |
78 | wxInetCacheNode *info; | |
79 | ||
80 | info = (wxInetCacheNode*) m_Cache.Get(right); | |
81 | ||
82 | // Add item into cache: | |
83 | if (info == NULL) { | |
84 | wxURL url(right); | |
85 | s = url.GetInputStream(); | |
86 | content = url.GetProtocol().GetContentType(); | |
87 | if (content == wxEmptyString) content = GetMimeTypeFromExt(location); | |
88 | if (s) { | |
89 | char buf[256]; | |
90 | ||
91 | wxGetTempFileName("wxhtml", buf); | |
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 | } | |
101 | else return NULL; //we can't open the URL | |
102 | } | |
103 | ||
104 | // Load item from cache: | |
105 | s = new wxFileInputStream(info -> GetTemp()); | |
106 | if (s) { | |
107 | return new wxFSFile(s, | |
108 | right, | |
109 | info -> GetMime(), | |
110 | GetAnchor(location)); | |
111 | } | |
112 | else return NULL; | |
113 | } | |
114 | ||
115 | ||
116 | ||
117 | wxInternetFSHandler::~wxInternetFSHandler() | |
118 | { | |
119 | wxNode *n; | |
120 | wxInetCacheNode *n2; | |
121 | ||
122 | m_Cache.BeginFind(); | |
123 | while ((n = m_Cache.Next()) != NULL) { | |
124 | n2 = (wxInetCacheNode*) n -> GetData(); | |
125 | wxRemoveFile(n2 -> GetTemp()); | |
126 | delete n2; | |
127 | } | |
128 | } | |
129 | ||
e3e717ec | 130 | #endif // wxUSE_FS_INET |