Commit | Line | Data |
---|---|---|
9e3d4a32 SL |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: webviewfilehandler.cpp | |
7d8d6163 | 3 | // Purpose: Custom webview handler to allow archive browsing |
9e3d4a32 SL |
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 | ||
88cc66f7 | 13 | #if wxUSE_WEBVIEW |
9e3d4a32 SL |
14 | |
15 | #if defined(__BORLANDC__) | |
16 | #pragma hdrstop | |
17 | #endif | |
18 | ||
7d8d6163 | 19 | #include "wx/webviewarchivehandler.h" |
9e3d4a32 | 20 | #include "wx/filesys.h" |
9e3d4a32 SL |
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 | ||
7d8d6163 SL |
47 | wxWebViewArchiveHandler::wxWebViewArchiveHandler(const wxString& scheme) : |
48 | wxWebViewHandler(scheme) | |
9e3d4a32 | 49 | { |
9e3d4a32 SL |
50 | m_fileSystem = new wxFileSystem(); |
51 | } | |
52 | ||
f2ae0da1 SL |
53 | wxWebViewArchiveHandler::~wxWebViewArchiveHandler() |
54 | { | |
55 | wxDELETE(m_fileSystem); | |
56 | } | |
57 | ||
7d8d6163 | 58 | wxFSFile* wxWebViewArchiveHandler::GetFile(const wxString &uri) |
9e3d4a32 | 59 | { |
e6db07c3 SL |
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 | ||
b7d3a622 | 69 | //We iterate through the string to see if there is a protocol description |
e6db07c3 SL |
70 | size_t start = wxString::npos; |
71 | for(size_t i = 0; i < path.length(); i++) | |
b7d3a622 | 72 | { |
e6db07c3 | 73 | if(path[i] == ';' && path.substr(i, 10) == ";protocol=") |
b7d3a622 SL |
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 | |
e6db07c3 | 81 | if(start == wxString::npos) |
9e3d4a32 | 82 | { |
e6db07c3 | 83 | size_t doubleslash = path.find("//"); |
b7d3a622 | 84 | //The path is incorrectly formed without // after the scheme |
9e3d4a32 SL |
85 | if(doubleslash == wxString::npos) |
86 | return NULL; | |
87 | ||
88 | wxString fspath = "file:" + | |
2bcada5f | 89 | EscapeFileNameCharsInURL(path.substr(doubleslash + 2).c_str()); |
9e3d4a32 SL |
90 | return m_fileSystem->OpenFile(fspath); |
91 | } | |
b7d3a622 SL |
92 | //Otherwise we need to extract the protocol |
93 | else | |
94 | { | |
e6db07c3 | 95 | size_t end = path.find('/', start); |
b7d3a622 SL |
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 | } | |
e6db07c3 SL |
101 | wxString mainpath = path.substr(0, start); |
102 | wxString archivepath = path.substr(end); | |
103 | wxString protstring = path.substr(start, end - start); | |
b7d3a622 SL |
104 | wxString protocol = protstring.substr(10); |
105 | //We can now construct the correct path | |
e6db07c3 | 106 | size_t doubleslash = path.find("//"); |
9e3d4a32 SL |
107 | //The path is incorrectly formed without // after the first protocol |
108 | if(doubleslash == wxString::npos) | |
109 | return NULL; | |
110 | ||
111 | wxString fspath = "file:" + | |
2bcada5f | 112 | EscapeFileNameCharsInURL(mainpath.substr(doubleslash + 2).c_str()) |
b7d3a622 | 113 | + "#" + protocol +":" + archivepath; |
9e3d4a32 SL |
114 | return m_fileSystem->OpenFile(fspath); |
115 | } | |
116 | } | |
117 | ||
88cc66f7 | 118 | #endif // wxUSE_WEBVIEW |