Minor fixes mainly to get stuff compiled using the mingw32 (cross) compiler
[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 #if wxUSE_FS_INET
33
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 == _T("http")) || (p == _T("ftp"));
68 }
69
70
71 wxFSFile* wxInternetFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location)
72 {
73 wxString right = GetProtocol(location) + _T(":") + 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 {
83 wxURL url(right);
84 s = url.GetInputStream();
85 content = url.GetProtocol().GetContentType();
86 if (content == wxEmptyString) content = GetMimeTypeFromExt(location);
87 if (s)
88 {
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
102 {
103 return (wxFSFile*) NULL; // we can't open the URL
104 }
105 }
106
107 // Load item from cache:
108 s = new wxFileInputStream(info->GetTemp());
109 if (s)
110 {
111 return new wxFSFile(s,
112 right,
113 info->GetMime(),
114 GetAnchor(location));
115 }
116 else return (wxFSFile*) NULL;
117 }
118
119
120
121 wxInternetFSHandler::~wxInternetFSHandler()
122 {
123 wxNode *n;
124 wxInetCacheNode *n2;
125
126 m_Cache.BeginFind();
127 while ((n = m_Cache.Next()) != NULL)
128 {
129 n2 = (wxInetCacheNode*) n->GetData();
130 wxRemoveFile(n2->GetTemp());
131 delete n2;
132 }
133 }
134
135 #endif // wxUSE_FS_INET