-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTiddlyIE.cpp
More file actions
298 lines (250 loc) · 7.3 KB
/
TiddlyIE.cpp
File metadata and controls
298 lines (250 loc) · 7.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// TiddlyIE.cpp : Implementation of CTiddlyIE
#include "stdafx.h"
#include "atlpath.h"
#include "atlfile.h"
#include "Iepmapi.h"
#include "TiddlyIE.h"
// extension property name
#define EXTENSION_PROPERTY_NAME L"TiddlyIE"
// content validation tokens
#define DOCTYPE L"<!doctype html>"
#define MOTW L"<!-- saved from url=(0021)http://tiddlywiki.com -->\r"
#define HTML_ELEMENT L"<html>"
// number of lines of content to search for validation tokens
#define LINES_TO_SEARCH 10
// CTiddlyIE
STDMETHODIMP CTiddlyIE::SetSite(IUnknown* pUnkSite)
{
if (pUnkSite != NULL)
{
// Cache the pointer to IWebBrowser2.
HRESULT hr = pUnkSite->QueryInterface(IID_IWebBrowser2, (void **)&m_spWebBrowser);
if (SUCCEEDED(hr))
{
// Register to sink events from DWebBrowserEvents2.
hr = DispEventAdvise(m_spWebBrowser);
if (SUCCEEDED(hr))
{
m_fAdvised = TRUE;
}
}
}
else
{
// Unregister event sink.
if (m_fAdvised)
{
DispEventUnadvise(m_spWebBrowser);
m_fAdvised = FALSE;
}
// Release cached pointers and other resources here.
m_spWebBrowser.Release();
}
// Return the base class implementation
return IObjectWithSiteImpl<CTiddlyIE>::SetSite(pUnkSite);
}
///
/// OnDocumentComplete - attach our custom object
///
void STDMETHODCALLTYPE CTiddlyIE::OnDocumentComplete(IDispatch *pDisp, VARIANT *pvarURL)
{
// attach the extension COM object
AddCustomObject((IDispatch*)this, EXTENSION_PROPERTY_NAME);
}
///
/// OnDownloadComplete - also have to check here to trap F5/Refresh
///
void STDMETHODCALLTYPE CTiddlyIE::OnDownloadComplete()
{
// attach the extension COM object
AddCustomObject((IDispatch*)this, EXTENSION_PROPERTY_NAME);
}
///
/// AddCustomObject - Adds a custom COM object to the DOM window object as a named property
///
/// Note: only adds it if necessary and protocol is "file:"
///
HRESULT CTiddlyIE::AddCustomObject(IDispatch *custObj, TCHAR* name)
{
// spBrowser is of type IHTMLWindow2
CComPtr<IDispatch> spDoc;
HRESULT hr = m_spWebBrowser->get_Document(&spDoc);
if (FAILED(hr))
{
return hr;
}
CComQIPtr<IHTMLDocument2> spHTMLDoc(spDoc);
if (!spHTMLDoc)
{
return E_NOINTERFACE;
}
// only for local documents
CComPtr<IHTMLLocation> spLocation;
hr = spHTMLDoc->get_location(&spLocation);
if (FAILED(hr))
{
return hr;
}
CComBSTR protocol;
hr = spLocation->get_protocol(&protocol);
if (FAILED(hr))
{
return hr;
}
else if (StrCmpIW(protocol, L"file:") != 0)
{
return S_FALSE;
}
CComPtr<IHTMLWindow2> spWindow;
hr = spHTMLDoc->get_parentWindow(&spWindow);
if (FAILED(hr))
{
return hr;
}
CComQIPtr<IDispatchEx> spDispatchEx(spWindow);
if (!spDispatchEx)
{
return E_NOINTERFACE;
}
// check for the named property so we only create it once
DISPID dispid;
hr = spDispatchEx->GetDispID(CComBSTR(name), fdexNameCaseSensitive, &dispid);
if (FAILED(hr))
{
// create the named property if it does not exist
hr = spDispatchEx->GetDispID(CComBSTR(name), fdexNameEnsure, &dispid);
if (FAILED(hr))
{
return hr;
}
// now get the named property
hr = spDispatchEx->GetDispID(CComBSTR(name), fdexNameCaseSensitive, &dispid);
if (FAILED(hr))
{
return hr;
}
// attach the COM object to the property
CComVariant var((IDispatch*)this);
DISPID putid = DISPID_PROPERTYPUT;
DISPPARAMS params;
params.cArgs = 1;
params.rgvarg = &var;
params.cNamedArgs = 1;
params.rgdispidNamedArgs = &putid;;
hr = spDispatchEx->InvokeEx(dispid, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYPUTREF, ¶ms, NULL, NULL, NULL);
if (FAILED(hr))
{
return hr;
}
}
return S_OK;
}
STDMETHODIMP CTiddlyIE::save(BSTR bstrFilespec, BSTR bstrContent)
{
if (bstrFilespec == NULL || bstrContent == NULL)
{
return E_INVALIDARG;
}
// search for required content within first 10 lines or until we find the <html> element
CStringW strContent = bstrContent;
int iLineCount = 0;
int iStart = 0;
bool bFoundDoctype = false;
bool bFoundMOTW = false;
while (iStart != -1 && iLineCount++ < LINES_TO_SEARCH)
{
CStringW strLine = strContent.Tokenize(L"\n", iStart).TrimLeft().MakeLower();
if (strLine.Find(HTML_ELEMENT) != -1) break;
if (strLine.Find(DOCTYPE) != -1) bFoundDoctype = true;
if (strLine.Find(MOTW) != -1) bFoundMOTW = true;
if (bFoundDoctype && bFoundMOTW) break;
}
if (!bFoundDoctype || !bFoundMOTW)
{
return HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
}
HWND hwnd;
HRESULT hr = m_spWebBrowser->get_HWND((LONG_PTR*)&hwnd);
if (FAILED(hr))
{
return hr;
}
// unescape filespec
WCHAR pwszFilespec[MAX_PATH * sizeof(WCHAR)] = { 0 };
DWORD dw = MAX_PATH * sizeof(WCHAR);
hr = UrlUnescapeW(bstrFilespec, pwszFilespec, &dw, 0);
if (SUCCEEDED(hr))
{
// crack filespec
CPathW path = pwszFilespec;
int fileidx = path.FindFileName();
if (fileidx == -1)
{
return HRESULT_FROM_WIN32(ERROR_INVALID_NAME); // no file name specified
}
CStringW filename = path.m_strPath.Mid(fileidx);
path.RemoveFileSpec();
// Get a filespec from the user (must use IE user consent API)
HANDLE hState;
LPWSTR pwszSelectedFilename = NULL;
const DWORD dwSaveFlags = OFN_ENABLESIZING | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST;
hr = IEShowSaveFileDialog(hwnd, filename, path,
L"TiddleyWiki Files|*.html|", L"html", 1,
dwSaveFlags, &pwszSelectedFilename, &hState);
// did user cancel?
if (S_OK != hr)
return hr;
CPath SelectedFilespec = pwszSelectedFilename;
CoTaskMemFree(pwszSelectedFilename);
// safety check - enforce .html extension
if (!SelectedFilespec.MatchSpec(L"*.html"))
{
// we can't actually change the path so all we can do is fail
return HRESULT_FROM_WIN32(ERROR_INVALID_NAME);
}
// Get the path to the IE cache dir, which is a dir that we're allowed
// to write to in protected mode
LPWSTR pwszCacheDir = NULL;
hr = IEGetWriteableFolderPath(FOLDERID_InternetCache, &pwszCacheDir);
if (SUCCEEDED(hr))
{
// Get a temp file name in that dir
TCHAR szTempFile[MAX_PATH] = { 0 };
GetTempFileName(CW2CT(pwszCacheDir), _T("tw"), 0, szTempFile);
CoTaskMemFree(pwszCacheDir);
// Write our data to that temp file
CAtlFile file;
hr = file.Create(szTempFile, GENERIC_WRITE, 0, CREATE_ALWAYS);
if (SUCCEEDED(hr))
{
// UTF-8 encode content
int cNeeded = WideCharToMultiByte(CP_UTF8, 0, bstrContent, -1, NULL, 0, NULL, NULL);
CStringA strContent;
LPSTR lpstrBuffer = strContent.GetBufferSetLength(cNeeded);
cNeeded = WideCharToMultiByte(CP_UTF8, 0, bstrContent, -1, lpstrBuffer, cNeeded, NULL, NULL);
strContent.ReleaseBuffer();
if(cNeeded == 0)
{
// UTF-8 conversion failed
return HRESULT_FROM_WIN32(GetLastError());
}
hr = file.Write(strContent, strContent.GetLength());
file.Close();
if (SUCCEEDED(hr))
{
// If we wrote the file successfully, have IE save that data to
// the path that the user chose
hr = IESaveFile(hState, T2CW(szTempFile));
// Clean up our temp file.
DeleteFile(szTempFile);
}
else
{
// We couldn't complete the save operation, so cancel it
IECancelSaveFile(hState);
}
}
}
}
return hr;
}