]> git.saurik.com Git - wxWidgets.git/blob - src/common/webviewarchivehandler.cpp
Improve inotify()-based wxFileSystemWatcher to handle creation/deletion.
[wxWidgets.git] / src / common / webviewarchivehandler.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: webviewfilehandler.cpp
3 // Purpose: Custom webview handler to allow archive browsing
4 // Author: Steven Lamerton
5 // Id: $Id$
6 // Copyright: (c) 2011 Steven Lamerton
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #if wxUSE_WEBVIEW
14
15 #if defined(__BORLANDC__)
16 #pragma hdrstop
17 #endif
18
19 #include "wx/webviewarchivehandler.h"
20 #include "wx/filesys.h"
21
22 //Taken from wx/filesys.cpp
23 static wxString EscapeFileNameCharsInURL(const char *in)
24 {
25 wxString s;
26
27 for ( const unsigned char *p = (const unsigned char*)in; *p; ++p )
28 {
29 const unsigned char c = *p;
30
31 if ( c == '/' || c == '-' || c == '.' || c == '_' || c == '~' ||
32 (c >= '0' && c <= '9') ||
33 (c >= 'a' && c <= 'z') ||
34 (c >= 'A' && c <= 'Z') )
35 {
36 s << c;
37 }
38 else
39 {
40 s << wxString::Format("%%%02x", c);
41 }
42 }
43
44 return s;
45 }
46
47 wxWebViewArchiveHandler::wxWebViewArchiveHandler(const wxString& scheme) :
48 wxWebViewHandler(scheme)
49 {
50 m_fileSystem = new wxFileSystem();
51 }
52
53 wxWebViewArchiveHandler::~wxWebViewArchiveHandler()
54 {
55 wxDELETE(m_fileSystem);
56 }
57
58 wxFSFile* wxWebViewArchiveHandler::GetFile(const wxString &uri)
59 {
60 //If there is a fragment at the end of the path then we need to strip it
61 //off as not all backends do this for us
62 wxString path = uri;
63 size_t hashloc = uri.find('#');
64 if(hashloc != wxString::npos)
65 {
66 path = uri.substr(0, hashloc);
67 }
68
69 //We iterate through the string to see if there is a protocol description
70 size_t start = wxString::npos;
71 for(size_t i = 0; i < path.length(); i++)
72 {
73 if(path[i] == ';' && path.substr(i, 10) == ";protocol=")
74 {
75 start = i;
76 break;
77 }
78 }
79
80 //We do not have a protocol string so we just pass the path withouth the
81 if(start == wxString::npos)
82 {
83 size_t doubleslash = path.find("//");
84 //The path is incorrectly formed without // after the scheme
85 if(doubleslash == wxString::npos)
86 return NULL;
87
88 wxString fspath = "file:" +
89 EscapeFileNameCharsInURL(path.substr(doubleslash + 2).c_str());
90 return m_fileSystem->OpenFile(fspath);
91 }
92 //Otherwise we need to extract the protocol
93 else
94 {
95 size_t end = path.find('/', start);
96 //For the path to be valid there must to a path after the protocol
97 if(end == wxString::npos)
98 {
99 return NULL;
100 }
101 wxString mainpath = path.substr(0, start);
102 wxString archivepath = path.substr(end);
103 wxString protstring = path.substr(start, end - start);
104 wxString protocol = protstring.substr(10);
105 //We can now construct the correct path
106 size_t doubleslash = path.find("//");
107 //The path is incorrectly formed without // after the first protocol
108 if(doubleslash == wxString::npos)
109 return NULL;
110
111 wxString fspath = "file:" +
112 EscapeFileNameCharsInURL(mainpath.substr(doubleslash + 2).c_str())
113 + "#" + protocol +":" + archivepath;
114 return m_fileSystem->OpenFile(fspath);
115 }
116 }
117
118 #endif // wxUSE_WEBVIEW