]> git.saurik.com Git - wxWidgets.git/blame - src/common/fs_inet.cpp
some conflicts resolved
[wxWidgets.git] / src / common / fs_inet.cpp
CommitLineData
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 11REMARKS :
5526e819
VS
12
13This FS creates local cache (in /tmp directory). The cache is freed
14on program exit.
15
16Size of cache is limited to cca 1000 items (due to GetTempFileName
17limitation)
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"
cf1f0870 47#include "wx/module.h"
5526e819
VS
48
49class wxInetCacheNode : public wxObject
50{
51 private:
52 wxString m_Temp;
53 wxString m_Mime;
54
55 public:
56 wxInetCacheNode(const wxString& l, const wxString& m) : wxObject() {m_Temp = l; m_Mime = m;}
57 const wxString& GetTemp() const {return m_Temp;}
58 const wxString& GetMime() const {return m_Mime;}
59};
60
61
62
63
64
65//--------------------------------------------------------------------------------
66// wxInternetFSHandler
67//--------------------------------------------------------------------------------
68
69
70bool wxInternetFSHandler::CanOpen(const wxString& location)
71{
72 wxString p = GetProtocol(location);
223d09f6 73 return (p == wxT("http")) || (p == wxT("ftp"));
5526e819
VS
74}
75
76
5526e819
VS
77wxFSFile* wxInternetFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location)
78{
223d09f6 79 wxString right = GetProtocol(location) + wxT(":") + GetRightLocation(location);
5526e819
VS
80 wxInputStream *s;
81 wxString content;
82 wxInetCacheNode *info;
83
84 info = (wxInetCacheNode*) m_Cache.Get(right);
85
86 // Add item into cache:
f61815af 87 if (info == NULL)
324dbfec 88 {
5526e819
VS
89 wxURL url(right);
90 s = url.GetInputStream();
91 content = url.GetProtocol().GetContentType();
92 if (content == wxEmptyString) content = GetMimeTypeFromExt(location);
913df6f2 93 if (s)
07d4b94c 94 {
e7fc59f4 95 wxChar buf[256];
5526e819 96
223d09f6 97 wxGetTempFileName( wxT("wxhtml"), buf);
5526e819
VS
98 info = new wxInetCacheNode(buf, content);
99 m_Cache.Put(right, info);
100
101 { // ok, now copy it:
45c94d78 102 wxFileOutputStream sout((wxString)buf);
5526e819
VS
103 s -> Read(sout); // copy the stream
104 }
105 delete s;
106 }
324dbfec 107 else
913df6f2
DW
108 {
109 return (wxFSFile*) NULL; // we can't open the URL
110 }
5526e819
VS
111 }
112
113 // Load item from cache:
324dbfec 114 s = new wxFileInputStream(info->GetTemp());
913df6f2 115 if (s)
324dbfec 116 {
5526e819
VS
117 return new wxFSFile(s,
118 right,
324dbfec 119 info->GetMime(),
5526e819
VS
120 GetAnchor(location));
121 }
324dbfec 122 else return (wxFSFile*) NULL;
5526e819
VS
123}
124
125
126
127wxInternetFSHandler::~wxInternetFSHandler()
128{
129 wxNode *n;
130 wxInetCacheNode *n2;
131
132 m_Cache.BeginFind();
913df6f2 133 while ((n = m_Cache.Next()) != NULL)
324dbfec
RR
134 {
135 n2 = (wxInetCacheNode*) n->GetData();
136 wxRemoveFile(n2->GetTemp());
5526e819
VS
137 delete n2;
138 }
139}
140
f61815af
GL
141class wxFileSystemInternetModule : public wxModule
142{
143 DECLARE_DYNAMIC_CLASS(wxFileSystemInternetModule)
144
145 public:
146 virtual bool OnInit()
147 {
148 wxFileSystem::AddHandler(new wxInternetFSHandler);
149 return TRUE;
150 }
151 virtual void OnExit() {}
152};
153
154IMPLEMENT_DYNAMIC_CLASS(wxFileSystemInternetModule, wxModule)
155
e3e717ec 156#endif // wxUSE_FS_INET