1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #include <sal/config.h>
16 #include <COMOpenDocuments.hpp>
17 #include <spsuppServ.hpp>
22 template<class... Args
>
23 HRESULT
LOStart(Args
... args
)
25 auto quote
= [](const std::wstring
& s
) { return L
"\"" + s
+ L
"\""; };
26 std::wstring
sCmdLine((quote(GetHelperExe()) + ... + (L
" " + quote(args
))));
27 LPWSTR pCmdLine
= const_cast<LPWSTR
>(sCmdLine
.c_str());
31 si
.dwFlags
= STARTF_USESHOWWINDOW
;
32 si
.wShowWindow
= SW_SHOW
;
33 PROCESS_INFORMATION pi
= {};
34 if (!CreateProcessW(nullptr, pCmdLine
, nullptr, nullptr, FALSE
, 0, nullptr, nullptr, &si
, &pi
))
35 return HRESULT_FROM_WIN32(GetLastError());
37 WaitForSingleObject(pi
.hProcess
, INFINITE
);
39 const bool bGotExitCode
= GetExitCodeProcess(pi
.hProcess
, &nExitCode
);
40 const DWORD nGetExitCodeError
= GetLastError();
42 CloseHandle(pi
.hProcess
);
43 CloseHandle(pi
.hThread
);
46 return HRESULT_FROM_WIN32(nGetExitCodeError
);
54 VARIANT_BOOL
toVBool(bool b
) { return b
? VARIANT_TRUE
: VARIANT_FALSE
; }
56 HRESULT
ImplCreateNewDocument(IDispatch
* /*pdisp*/, BSTR bstrTemplateLocation
,
57 BSTR bstrDefaultSaveLocation
, VARIANT_BOOL
* pbResult
)
59 HRESULT hr
= LOStart(L
"CreateNewDocument", bstrTemplateLocation
, bstrDefaultSaveLocation
);
60 *pbResult
= toVBool(hr
== S_OK
);
64 HRESULT
ImplEditDocument(IDispatch
* /*pdisp*/, BSTR bstrDocumentLocation
,
65 VARIANT_BOOL fUseLocalCopy
, const VARIANT
& varProgID
,
66 VARIANT_BOOL
* pbResult
)
68 const wchar_t* sUseLocalCopy
= (fUseLocalCopy
== VARIANT_FALSE
) ? L
"0" : L
"1";
69 const wchar_t* sProgId
= (varProgID
.vt
== VT_BSTR
) ? varProgID
.bstrVal
: L
"";
70 HRESULT hr
= LOStart(L
"EditDocument", bstrDocumentLocation
, sUseLocalCopy
, sProgId
);
71 *pbResult
= toVBool(hr
== S_OK
);
75 HRESULT
ImplViewDocument(IDispatch
* /*pdisp*/, BSTR bstrDocumentLocation
, int OpenType
,
76 const VARIANT
& varProgID
, VARIANT_BOOL
* pbResult
)
78 wchar_t sOpenType
[16]{};
79 swprintf(sOpenType
, L
"%d", OpenType
);
80 const wchar_t* sProgId
= (varProgID
.vt
== VT_BSTR
) ? varProgID
.bstrVal
: L
"";
81 HRESULT hr
= LOStart(L
"ViewDocument", bstrDocumentLocation
, sOpenType
, sProgId
);
82 *pbResult
= toVBool(hr
== S_OK
);
87 LONG
COMOpenDocuments::m_nObjCount
= 0;
88 ITypeInfo
* COMOpenDocuments::m_pTypeInfo
= nullptr;
90 COMOpenDocuments::COMOpenDocuments()
92 ::InterlockedIncrement(&m_nObjCount
);
93 if (m_pTypeInfo
== nullptr)
95 ITypeLib
* pITypeLib
= GetTypeLib();
96 HRESULT hr
= pITypeLib
->GetTypeInfoOfGuid(__uuidof(IOWSNewDocument3
), &m_pTypeInfo
);
102 COMOpenDocuments::~COMOpenDocuments()
104 if (::InterlockedDecrement(&m_nObjCount
) == 0 && m_pTypeInfo
)
106 m_pTypeInfo
->Release();
107 m_pTypeInfo
= nullptr;
113 STDMETHODIMP
COMOpenDocuments::QueryInterface(REFIID riid
, void **ppvObject
)
115 *ppvObject
= nullptr;
116 if (IsEqualIID(riid
, __uuidof(IUnknown
)) ||
117 IsEqualIID(riid
, __uuidof(IDispatch
)) ||
118 IsEqualIID(riid
, __uuidof(IOWSNewDocument
)) ||
119 IsEqualIID(riid
, __uuidof(IOWSNewDocument2
)) ||
120 IsEqualIID(riid
, __uuidof(IOWSNewDocument3
)))
122 *ppvObject
= static_cast<IOWSNewDocument3
*>(this);
124 else if (IsEqualIID(riid
, __uuidof(IObjectSafety
)))
126 *ppvObject
= static_cast<IObjectSafety
*>(this);
130 return E_NOINTERFACE
;
133 static_cast<IUnknown
*>(*ppvObject
)->AddRef();
139 STDMETHODIMP
COMOpenDocuments::GetTypeInfoCount(UINT
*pctinfo
)
141 if (pctinfo
== nullptr)
148 STDMETHODIMP
COMOpenDocuments::GetTypeInfo(UINT iTInfo
, LCID
/*lcid*/, ITypeInfo
**ppTInfo
)
150 if (ppTInfo
== nullptr)
155 return DISP_E_BADINDEX
;
157 (*ppTInfo
= m_pTypeInfo
)->AddRef();
161 STDMETHODIMP
COMOpenDocuments::GetIDsOfNames(
168 return m_pTypeInfo
->GetIDsOfNames(rgszNames
, cNames
, rgDispId
);
171 STDMETHODIMP
COMOpenDocuments::Invoke(
173 REFIID
/*riid*/, // IID_NULL (see https://msdn.microsoft.com/en-us/library/windows/desktop/ms221479)
176 DISPPARAMS
*pDispParams
,
178 EXCEPINFO
*pExcepInfo
,
181 return DispInvoke(this, m_pTypeInfo
, dispIdMember
, wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
184 // IOWSNewDocument methods
186 // Creates a document based on the specified document template
187 STDMETHODIMP
COMOpenDocuments::CreateNewDocument(
188 BSTR bstrTemplateLocation
, // A string that contains the URL of the document template from which the document is created, or the programmatic identifier (progID) of the application to invoke when creating the document
189 BSTR bstrDefaultSaveLocation
, // A string that contains the path that specifies a suggested default location for saving the new document
190 VARIANT_BOOL
*pbResult
) // true if the document creation succeeds; otherwise false
192 return CreateNewDocument2(nullptr, bstrTemplateLocation
, bstrDefaultSaveLocation
, pbResult
);
195 // Opens the specified document for editing with its associated application
196 // or with the specified editor
197 STDMETHODIMP
COMOpenDocuments::EditDocument(
198 BSTR bstrDocumentLocation
, // A string that contains the URL of the document to open for editing
199 VARIANT varProgID
, // An optional string that contains the ProgID of the application with which to edit the document. If this argument is omitted, the default editor for the document is used
200 VARIANT_BOOL
*pbResult
) // true if the document was successfully opened; otherwise false
202 return EditDocument3(nullptr, bstrDocumentLocation
, FALSE
, varProgID
, pbResult
);
205 // IOWSNewDocument2 methods
207 // Opens the document for reading instead of editing, so that the document is not locked on the server
209 // Use the ViewDocument method to open a document in its appropriate application,
210 // instead of inside of an application instance embedded within the browser
211 STDMETHODIMP
COMOpenDocuments::ViewDocument(
212 BSTR bstrDocumentLocation
, // A string that contains the URL of the document to open for reading
213 VARIANT varProgID
, // An optional string that contains the ProgID of the application with which to open the document. If this argument is omitted, the default viewer for the document is used
214 VARIANT_BOOL
*pbResult
) // true if the document was successfully opened; otherwise false
216 return ViewDocument3(nullptr, bstrDocumentLocation
, 0, varProgID
, pbResult
);
219 // Opens the document for reading instead of editing, so that the document
220 // is not locked on the server and in a specified window
222 // Use the ViewDocument method to open a document in its appropriate application,
223 // instead of inside of an application instance embedded within the browser
224 STDMETHODIMP
COMOpenDocuments::ViewDocument2(
225 IDispatch
*pdisp
, // An Object that represents the window from which the ViewDocument2 method is being activated
226 BSTR bstrDocumentLocation
, // A string that contains the URL of the document to open for reading
227 VARIANT varProgID
, // An optional string that contains the ProgID of the application with which to open the document. If this argument is omitted, the default viewer for the document is used
228 VARIANT_BOOL
*pbResult
) // true if the document was successfully opened; otherwise false
230 return ViewDocument3(pdisp
, bstrDocumentLocation
, 0, varProgID
, pbResult
);
233 // Opens the specified document for editing with its associated application
234 // or with the specified editor based on the specified window object
235 STDMETHODIMP
COMOpenDocuments::EditDocument2(
236 IDispatch
*pdisp
, // An Object that represents the window from which the EditDocument2 method is being activated
237 BSTR bstrDocumentLocation
, // A string that contains the URL of the document to open for editing
238 VARIANT varProgID
, // An optional string that contains the ProgID of the application with which to edit the document. If this argument is omitted, the default editor for the document is used
239 VARIANT_BOOL
*pbResult
) // true if the document was successfully opened; otherwise false
241 return EditDocument3(pdisp
, bstrDocumentLocation
, FALSE
, varProgID
, pbResult
);
244 // Creates a document based on the specified document template and window object
245 STDMETHODIMP
COMOpenDocuments::CreateNewDocument2(
246 IDispatch
* pdisp
, // An Object that represents the window from which the CreateNewDocument2 method is being activated
247 BSTR bstrTemplateLocation
, // A string that contains the URL of the document template from which the document is created, or the programmatic identifier (progID) of the application to invoke when creating the document
248 BSTR bstrDefaultSaveLocation
, // A string that contains the path that specifies a suggested default location for saving the new document
249 VARIANT_BOOL
* pbResult
) // true if the document creation succeeds; otherwise false
253 // TODO: resolve the program from varProgID (nullptr -> default?)
254 return ImplCreateNewDocument(pdisp
, bstrTemplateLocation
, bstrDefaultSaveLocation
, pbResult
);
257 // Used with the OpenDocuments.CreateNewDocument2 method to determine
258 // whether the security dialog box that appears when a document is opened has already appeared
260 // If the PromptedOnLastOpen method returns true, the window containing the document library view
261 // refreshes itself the next time it receives focus. One refresh can occur after the new document
262 // is saved to the server
263 STDMETHODIMP
COMOpenDocuments::PromptedOnLastOpen(
264 VARIANT_BOOL
* pbResult
) // true if the security dialog box that appears when a document is opened has already appeared; otherwise false
266 // This method is used by SharePoint e.g. after calling ViewDocument3. Needs to be implemented,
267 // otherwise IE would show download bar ("Do you want to open Foo.xls?"), as if opening with
268 // LibreOffice failed, even if actually it succeeded.
271 // Returning true makes SharePoint library to refresh only when focused next time; false makes
272 // it refresh instantly. The JavaScript code involved is this:
273 // var fRefreshOnNextFocus=stsOpen.PromptedOnLastOpen();
274 // if (fRefreshOnNextFocus)
275 // window.onfocus=RefreshOnNextFocus;
277 // SetWindowRefreshOnFocus();
278 // It seems to be no reason to require immediate refresh, so just return true.
279 *pbResult
= VARIANT_TRUE
;
283 // IOWSNewDocument3 methods
285 // Opens the document for reading instead of editing, so that the document
286 // is not locked on the server in a specified window, and with a specified type
288 // The following table shows possible values for OpenType
290 // 0 When checked out, or when the document library does not require check out, the user can read or edit the document
291 // 1 When another user has checked it out, the user can only read the document
292 // 2 When the current user has checked it out, the user can only edit the document
293 // 3 When the document is not checked out and the document library requires that documents be checked out to be edited, the user can only read the document, or check it out and edit it
294 // 4 When the current user has checked it out, the user can only edit the local copy of the document
295 STDMETHODIMP
COMOpenDocuments::ViewDocument3(
296 IDispatch
* pdisp
, // An Object that represents the window from which the ViewDocument3 method is being activated
297 BSTR bstrDocumentLocation
, // A string that contains the URL of the document to open for reading
298 int OpenType
, // A Long integer that specifies the rights for opening the document
299 VARIANT varProgID
, // An optional string that contains the ProgID of the application with which to open the document. If this argument is omitted, the default viewer for the document is used
300 VARIANT_BOOL
*pbResult
) // true if the document was successfully opened; otherwise false
304 return ImplViewDocument(pdisp
, bstrDocumentLocation
, OpenType
, varProgID
, pbResult
);
307 // Checks in the specified document to a library
308 STDMETHODIMP
COMOpenDocuments::CheckinDocument(
309 BSTR
/*bstrDocumentLocation*/, // A string that contains the URL of the document to check in
310 int /*CheckinType*/, // A Long that specifies the type of check-in, where 0 = minor check-in, 1 = major check-in, and 2 = overwrite check-in
311 BSTR
/*CheckinComment*/, // A string that contains a comment for checking in the document
312 VARIANT_BOOL
/*bKeepCheckout*/, // Optional. true to check in changes that have been made to the document yet keep the document checked out; otherwise, false. The default value is false
313 VARIANT_BOOL
* /*pbResult*/) // true if the document is successfully checked in; otherwise, false
319 // Discards the check out of a document to the client computer and deletes the local draft
320 STDMETHODIMP
COMOpenDocuments::DiscardLocalCheckout(
321 BSTR
/*bstrDocumentLocationRaw*/, // A string that contains the URL of the document
322 VARIANT_BOOL
* /*pbResult*/) // true if the operation to discard the local checkout of the document is successful; otherwise, false
328 // Deprecated. Returns E_NOTIMPL
329 STDMETHODIMP
COMOpenDocuments::ViewInExcel(
337 VARIANT
/*varProgID*/)
342 // Checks out a document from a library
343 STDMETHODIMP
COMOpenDocuments::CheckoutDocumentPrompt(
344 BSTR
/*bstrDocumentLocationRaw*/, // A string that contains the URL of the document to check out
345 VARIANT_BOOL
/*fEditAfterCheckout*/, // true to open the document in an editing application; otherwise, false
346 VARIANT
/*varProgID*/, // An optional string that contains the ProgID of the application that is used to work with the document. If this argument is omitted, the default application for the document is used
347 VARIANT_BOOL
* /*pbResult*/) // true if the document is successfully checked out; otherwise, false
353 // Opens the specified document for editing with its associated application
354 // or with the specified editor based on the specified window object,
355 // and specifies whether to use a local copy
356 STDMETHODIMP
COMOpenDocuments::EditDocument3(
357 IDispatch
* pdisp
, // An Object that represents the window from which the EditDocument3 method is being activated
358 BSTR bstrDocumentLocation
, // A string that contains the URL of the document to open for editing
359 VARIANT_BOOL fUseLocalCopy
, // true to use a local copy; otherwise false
360 VARIANT varProgID
, // An optional string that contains the ProgID of the application with which to edit the document. If this argument is omitted, the default editor for the document is used
361 VARIANT_BOOL
*pbResult
) // true if the document was successfully opened; otherwise false
365 // TODO: resolve the program from varProgID (nullptr -> default?)
366 return ImplEditDocument(pdisp
, bstrDocumentLocation
, fUseLocalCopy
, varProgID
, pbResult
);
369 // Creates a new blog post in the editing application
370 STDMETHODIMP
COMOpenDocuments::NewBlogPost(
371 BSTR
/*bstrProviderId*/, // A string that contains the GUID of the blog provider
372 BSTR
/*bstrBlogUrl*/, // A string that contains the absolute URL of the blog site
373 BSTR
/*bstrBlogName*/) // A string that contains the GUID of the blog site and the GUID of the post list separated by the pound sign (#)
378 // IObjectSafety methods
380 HRESULT STDMETHODCALLTYPE
COMOpenDocuments::GetInterfaceSafetyOptions(
382 DWORD
*pdwSupportedOptions
,
383 DWORD
*pdwEnabledOptions
)
386 HRESULT hr
= QueryInterface(riid
, reinterpret_cast<void**>(&pUnk
));
392 // We know about it; release reference and return required information
394 *pdwSupportedOptions
= iSupportedOptionsMask
;
395 *pdwEnabledOptions
= m_iEnabledOptions
;
399 HRESULT STDMETHODCALLTYPE
COMOpenDocuments::SetInterfaceSafetyOptions(
401 DWORD dwOptionSetMask
,
402 DWORD dwEnabledOptions
)
405 HRESULT hr
= QueryInterface(riid
, reinterpret_cast<void**>(&pUnk
));
412 // Are there unsupported options in mask?
413 if (dwOptionSetMask
& ~iSupportedOptionsMask
)
416 m_iEnabledOptions
= (m_iEnabledOptions
& ~dwOptionSetMask
) | (dwOptionSetMask
& dwEnabledOptions
);
422 LONG
COMOpenDocuments::GetObjectCount() { return m_nObjCount
; }
424 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */