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