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