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