LibreOffice Module shell (master) 1
JumpList.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 */
9
10#include <sal/config.h>
11
12#include <algorithm>
13#include <cassert>
14
22#include <osl/file.hxx>
23#include <osl/mutex.hxx>
24#include <osl/process.h>
25#include <sal/log.hxx>
26#include <systools/win32/comtools.hxx>
27
28#include <com/sun/star/lang/IllegalArgumentException.hpp>
29#include <com/sun/star/lang/XServiceInfo.hpp>
30#include <com/sun/star/system/windows/JumpListItem.hpp>
31#include <com/sun/star/system/windows/XJumpList.hpp>
32#include <com/sun/star/uno/XComponentContext.hpp>
33#include <com/sun/star/util/InvalidStateException.hpp>
34
35#include <prewin.h>
36#include <Shlobj.h>
37#include <propkey.h>
38#include <propvarutil.h>
39#include <postwin.h>
40
41using namespace comphelper;
42using namespace cppu;
43using namespace css;
44using namespace css::uno;
45using namespace css::lang;
46using namespace css::system::windows;
47using namespace css::util;
48using namespace osl;
49using namespace sal::systools;
50
51namespace
52{
53class JumpListImpl : public BaseMutex, public WeakComponentImplHelper<XJumpList, XServiceInfo>
54{
55 Reference<XComponentContext> m_xContext;
56 COMReference<ICustomDestinationList> m_aDestinationList;
57 COMReference<IObjectArray> m_aRemoved;
58 bool m_isListOpen;
59
60public:
61 explicit JumpListImpl(const Reference<XComponentContext>& xContext);
62
63 // XJumpList
64 virtual void SAL_CALL beginList(const OUString& sApplication) override;
65 virtual void SAL_CALL appendCategory(const OUString& sCategory,
66 const Sequence<JumpListItem>& aJumpListItems) override;
67 virtual void SAL_CALL addTasks(const Sequence<JumpListItem>& aJumpListItems) override;
68 virtual void SAL_CALL showRecentFiles() override;
69 virtual void SAL_CALL showFrequentFiles() override;
70 virtual void SAL_CALL commitList() override;
71 virtual void SAL_CALL abortList() override;
72 virtual void SAL_CALL deleteList(const OUString& sApplication) override;
73 virtual Sequence<JumpListItem> SAL_CALL getRemovedItems(const OUString& sApplication) override;
74
75 // XServiceInfo
76 virtual OUString SAL_CALL getImplementationName() override;
77 virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) override;
78 virtual Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
79};
80
81JumpListImpl::JumpListImpl(const Reference<XComponentContext>& xContext)
83 , m_xContext(xContext)
84 , m_aDestinationList(CLSID_DestinationList, nullptr, CLSCTX_INPROC_SERVER)
85 , m_isListOpen(false)
86{
87}
88
89// Determines if the provided IShellLinkItem is listed in the array of items that the user has removed
90bool lcl_isItemInArray(COMReference<IShellLinkW> pShellLinkItem,
91 COMReference<IObjectArray> poaRemoved)
92{
93 UINT nItems;
94 ThrowIfFailed(poaRemoved->GetCount(&nItems), "GetCount failed.");
95
96 COMReference<IShellLinkW> pShellLinkItemCompare;
97 for (UINT i = 0; i < nItems; i++)
98 {
99 if (!SUCCEEDED(poaRemoved->GetAt(i, IID_PPV_ARGS(&pShellLinkItemCompare))))
100 continue;
101
102 PROPVARIANT propvar;
103 COMReference<IPropertyStore> pps(pShellLinkItem, COM_QUERY_THROW);
104 ThrowIfFailed(pps->GetValue(PKEY_Title, &propvar), "GetValue failed.");
105 OUString title(o3tl::toU(PropVariantToStringWithDefault(propvar, L"")));
106
107 COMReference<IPropertyStore> ppsCompare(pShellLinkItemCompare, COM_QUERY_THROW);
108 ThrowIfFailed(ppsCompare->GetValue(PKEY_Title, &propvar), "GetValue failed.");
109 OUString titleCompare(o3tl::toU(PropVariantToStringWithDefault(propvar, L"")));
110 PropVariantClear(&propvar);
111
112 if (title == titleCompare)
113 return true;
114 }
115
116 return false;
117}
118}
119
120void SAL_CALL JumpListImpl::beginList(const OUString& sApplication)
121{
122 if (m_isListOpen)
123 throw InvalidStateException("There is already a list open. Close it with 'commitList'");
124
125 if (sApplication != "Writer" && sApplication != "Calc" && sApplication != "Impress"
126 && sApplication != "Draw" && sApplication != "Math" && sApplication != "Base"
127 && sApplication != "Startcenter")
128 {
129 throw IllegalArgumentException(
130 "Parameter 'application' must be one of 'Writer', 'Calc', 'Impress', 'Draw', "
131 "'Math', 'Base', 'Startcenter'.",
132 getXWeak(), 1);
133 }
134 OUString sApplicationID("TheDocumentFoundation.LibreOffice." + sApplication);
135
136 try
137 {
138 m_aDestinationList->SetAppID(o3tl::toW(sApplicationID.getStr()));
139
140 UINT min_slots;
141
142 ThrowIfFailed(m_aDestinationList->BeginList(&min_slots, IID_PPV_ARGS(&m_aRemoved)),
143 "BeginList failed");
144 m_isListOpen = true;
145 }
146 catch (const ComError& e)
147 {
148 SAL_WARN("shell.jumplist", e.what());
149 }
150}
151
152void SAL_CALL JumpListImpl::appendCategory(const OUString& sCategory,
153 const Sequence<JumpListItem>& aJumpListItems)
154{
155 if (!m_isListOpen)
156 throw InvalidStateException("No list open. Open it with 'beginList'");
157
158 if (sCategory.isEmpty())
159 {
160 throw IllegalArgumentException("Parameter 'category' must not be empty", getXWeak(), 1);
161 }
162
163 try
164 {
165 OUString sofficeURL;
166 OUString sofficePath;
167 oslProcessError err = osl_getExecutableFile(&sofficeURL.pData);
168 FileBase::getSystemPathFromFileURL(sofficeURL, sofficePath);
169 if (err != osl_Process_E_None)
170 {
171 SAL_WARN("shell.jumplist", "osl_getExecutableFile failed");
172 return;
173 }
174 // We need to run soffice.exe, not soffice.bin
175 sofficePath = sofficePath.replaceFirst("soffice.bin", "soffice.exe");
176
177 COMReference<IObjectCollection> aCollection(CLSID_EnumerableObjectCollection, nullptr,
178 CLSCTX_INPROC_SERVER);
179
180 for (auto const& item : aJumpListItems)
181 {
182 if (item.name.isEmpty())
183 continue;
184 try
185 {
186 COMReference<IShellLinkW> pShellLinkItem(CLSID_ShellLink, nullptr,
187 CLSCTX_INPROC_SERVER);
188
189 {
190 COMReference<IPropertyStore> pps(pShellLinkItem, COM_QUERY_THROW);
191
192 PROPVARIANT propvar;
193 ThrowIfFailed(
194 InitPropVariantFromString(o3tl::toW(item.name.getStr()), &propvar),
195 "InitPropVariantFromString failed.");
196
197 ThrowIfFailed(pps->SetValue(PKEY_Title, propvar), "SetValue failed.");
198
199 ThrowIfFailed(pps->Commit(), "Commit failed.");
200
201 PropVariantClear(&propvar);
202 }
203 ThrowIfFailed(
204 pShellLinkItem->SetDescription(o3tl::toW(item.description.getStr())),
205 Concat2View("Setting description '" + item.description.toUtf8() + "' failed."));
206
207 ThrowIfFailed(pShellLinkItem->SetPath(o3tl::toW(sofficePath.getStr())),
208 Concat2View("Setting path '" + sofficePath.toUtf8() + "' failed."));
209
210 ThrowIfFailed(
211 pShellLinkItem->SetArguments(o3tl::toW(item.arguments.getStr())),
212 Concat2View("Setting arguments '" + item.arguments.toUtf8() + "' failed."));
213
214 ThrowIfFailed(
215 pShellLinkItem->SetIconLocation(o3tl::toW(item.iconPath.getStr()), 0),
216 Concat2View("Setting icon path '" + item.iconPath.toUtf8() + "' failed."));
217
218 if (lcl_isItemInArray(pShellLinkItem, m_aRemoved))
219 {
220 SAL_INFO("shell.jumplist", "Ignoring item '"
221 << item.name
222 << "' (was removed by user). See output of "
223 "XJumpList::getRemovedItems().");
224 continue;
225 }
226 aCollection->AddObject(pShellLinkItem);
227 }
228 catch (const ComError& e)
229 {
230 SAL_WARN("shell.jumplist", e.what());
231 continue;
232 }
233 }
234
235 COMReference<IObjectArray> pObjectArray(aCollection, COM_QUERY_THROW);
236 UINT nItems;
237 ThrowIfFailed(pObjectArray->GetCount(&nItems), "GetCount failed.");
238 if (nItems == 0)
239 {
240 throw IllegalArgumentException(
241 "No valid items given. `jumpListItems` is either empty, or contains only items "
242 "which were removed by the user. See `XJumpList::getRemovedItems()`.",
243 getXWeak(), 1);
244 }
245
246 ThrowIfFailed(
247 m_aDestinationList->AppendCategory(o3tl::toW(sCategory.getStr()), pObjectArray),
248 "AppendCategory failed.");
249 }
250 catch (const ComError& e)
251 {
252 SAL_WARN("shell.jumplist", e.what());
253 }
254}
255
256void SAL_CALL JumpListImpl::addTasks(const Sequence<JumpListItem>& aJumpListItems)
257{
258 if (!m_isListOpen)
259 throw InvalidStateException("No list open. Open it with 'beginList'");
260
261 try
262 {
263 OUString sofficeURL;
264 OUString sofficePath;
265 oslProcessError err = osl_getExecutableFile(&sofficeURL.pData);
266 FileBase::getSystemPathFromFileURL(sofficeURL, sofficePath);
267 if (err != osl_Process_E_None)
268 {
269 SAL_WARN("shell.jumplist", "osl_getExecutableFile failed");
270 return;
271 }
272 // We need to run soffice.exe, not soffice.bin
273 sofficePath = sofficePath.replaceFirst("soffice.bin", "soffice.exe");
274
275 COMReference<IObjectCollection> aCollection(CLSID_EnumerableObjectCollection, nullptr,
276 CLSCTX_INPROC_SERVER);
277
278 for (auto const& item : aJumpListItems)
279 {
280 if (item.name.isEmpty())
281 continue;
282 try
283 {
284 COMReference<IShellLinkW> pShellLinkItem(CLSID_ShellLink, nullptr,
285 CLSCTX_INPROC_SERVER);
286
287 {
288 COMReference<IPropertyStore> pps(pShellLinkItem, COM_QUERY_THROW);
289
290 PROPVARIANT propvar;
291 ThrowIfFailed(
292 InitPropVariantFromString(o3tl::toW(item.name.getStr()), &propvar),
293 "InitPropVariantFromString failed.");
294
295 ThrowIfFailed(pps->SetValue(PKEY_Title, propvar), "SetValue failed.");
296
297 ThrowIfFailed(pps->Commit(), "Commit failed.");
298
299 PropVariantClear(&propvar);
300 }
301 ThrowIfFailed(
302 pShellLinkItem->SetDescription(o3tl::toW(item.description.getStr())),
303 Concat2View("Setting description '" + item.description.toUtf8() + "' failed."));
304
305 ThrowIfFailed(pShellLinkItem->SetPath(o3tl::toW(sofficePath.getStr())),
306 Concat2View("Setting path '" + sofficePath.toUtf8() + "' failed."));
307
308 ThrowIfFailed(
309 pShellLinkItem->SetArguments(o3tl::toW(item.arguments.getStr())),
310 Concat2View("Setting arguments '" + item.arguments.toUtf8() + "' failed."));
311
312 ThrowIfFailed(
313 pShellLinkItem->SetIconLocation(o3tl::toW(item.iconPath.getStr()), 0),
314 Concat2View("Setting icon path '" + item.iconPath.toUtf8() + "' failed."));
315
316 aCollection->AddObject(pShellLinkItem);
317 }
318 catch (const ComError& e)
319 {
320 SAL_WARN("shell.jumplist", e.what());
321 continue;
322 }
323 }
324
325 COMReference<IObjectArray> pObjectArray(aCollection, COM_QUERY_THROW);
326 UINT nItems;
327 ThrowIfFailed(pObjectArray->GetCount(&nItems), "GetCount failed.");
328 if (nItems == 0)
329 {
330 throw IllegalArgumentException("No valid items given. `jumpListItems` is empty.",
331 getXWeak(), 1);
332 }
333
334 ThrowIfFailed(m_aDestinationList->AddUserTasks(pObjectArray), "AddUserTasks failed.");
335 }
336 catch (const ComError& e)
337 {
338 SAL_WARN("shell.jumplist", e.what());
339 }
340}
341
342void SAL_CALL JumpListImpl::showRecentFiles()
343{
344 if (!m_isListOpen)
345 throw InvalidStateException("No list open. Open it with 'beginList'");
346
347 try
348 {
349 ThrowIfFailed(m_aDestinationList->AppendKnownCategory(KDC_RECENT),
350 "AppendKnownCategory(KDC_RECENT) failed.");
351 }
352 catch (const ComError& e)
353 {
354 SAL_WARN("shell.jumplist", e.what());
355 }
356}
357
358void SAL_CALL JumpListImpl::showFrequentFiles()
359{
360 if (!m_isListOpen)
361 throw InvalidStateException("No list open. Open it with 'beginList'");
362
363 try
364 {
365 ThrowIfFailed(m_aDestinationList->AppendKnownCategory(KDC_FREQUENT),
366 "AppendKnownCategory(KDC_FREQUENT) failed.");
367 }
368 catch (const ComError& e)
369 {
370 SAL_WARN("shell.jumplist", e.what());
371 }
372}
373
374void SAL_CALL JumpListImpl::commitList()
375{
376 if (!m_isListOpen)
377 throw InvalidStateException("No list open. Open it with 'beginList'");
378
379 try
380 {
381 ThrowIfFailed(m_aDestinationList->CommitList(), "CommitList failed.");
382 m_isListOpen = false;
383 }
384 catch (const ComError& e)
385 {
386 SAL_WARN("shell.jumplist", e.what());
387 }
388}
389
390void SAL_CALL JumpListImpl::abortList()
391{
392 if (!m_isListOpen)
393 throw InvalidStateException("No list open.");
394
395 try
396 {
397 ThrowIfFailed(m_aDestinationList->AbortList(), "AbortList failed.");
398 m_isListOpen = false;
399 }
400 catch (const ComError& e)
401 {
402 SAL_WARN("shell.jumplist", e.what());
403 }
404}
405
406void SAL_CALL JumpListImpl::deleteList(const OUString& sApplication)
407{
408 if (m_isListOpen)
409 throw InvalidStateException("You are in a list building session. Close it with "
410 "'commitList', or abort with 'abortList'");
411
412 if (sApplication != "Writer" && sApplication != "Calc" && sApplication != "Impress"
413 && sApplication != "Draw" && sApplication != "Math" && sApplication != "Base"
414 && sApplication != "Startcenter")
415 {
416 throw IllegalArgumentException(
417 "Parameter 'application' must be one of 'Writer', 'Calc', 'Impress', 'Draw', "
418 "'Math', 'Base', 'Startcenter'.",
419 getXWeak(), 1);
420 }
421 OUString sApplicationID("TheDocumentFoundation.LibreOffice." + sApplication);
422
423 try
424 {
425 COMReference<ICustomDestinationList> aDestinationList(CLSID_DestinationList, nullptr,
426 CLSCTX_INPROC_SERVER);
427 aDestinationList->DeleteList(o3tl::toW(sApplicationID.getStr()));
428 }
429 catch (const ComError& e)
430 {
431 SAL_WARN("shell.jumplist", e.what());
432 }
433}
434
435Sequence<JumpListItem> SAL_CALL JumpListImpl::getRemovedItems(const OUString& sApplication)
436{
437 if (sApplication != "Writer" && sApplication != "Calc" && sApplication != "Impress"
438 && sApplication != "Draw" && sApplication != "Math" && sApplication != "Base"
439 && sApplication != "Startcenter")
440 {
441 throw IllegalArgumentException(
442 "Parameter 'application' must be one of 'Writer', 'Calc', 'Impress', 'Draw', "
443 "'Math', 'Base', 'Startcenter'.",
444 getXWeak(), 1);
445 }
446 OUString sApplicationID("TheDocumentFoundation.LibreOffice." + sApplication);
447
448 std::vector<JumpListItem> removedItems;
449 try
450 {
451 COMReference<ICustomDestinationList> aDestinationList(CLSID_DestinationList, nullptr,
452 CLSCTX_INPROC_SERVER);
453
454 aDestinationList->SetAppID(o3tl::toW(sApplicationID.getStr()));
455
456 COMReference<IObjectArray> removed;
457 ThrowIfFailed(aDestinationList->GetRemovedDestinations(IID_PPV_ARGS(&removed)),
458 "GetRemovedDestinations failed");
459
460 UINT removed_count;
461 if (SUCCEEDED(removed->GetCount(&removed_count) && (removed_count > 0)))
462 {
463 JumpListItem item;
464 COMReference<IShellLinkW> pShellLinkItem;
465 for (UINT i = 0; i < removed_count; ++i)
466 {
467 if (SUCCEEDED(removed->GetAt(i, IID_PPV_ARGS(&pShellLinkItem))))
468 {
469 COMReference<IPropertyStore> propertyStore(pShellLinkItem, COM_QUERY_THROW);
470 PROPVARIANT propvar;
471 ThrowIfFailed(propertyStore->GetValue(PKEY_Title, &propvar),
472 "GetValue failed.");
473 item.name = o3tl::toU(PropVariantToStringWithDefault(propvar, L""));
474
475 ThrowIfFailed(propertyStore->GetValue(PKEY_Link_Arguments, &propvar),
476 "GetValue failed.");
477 item.arguments = o3tl::toU(PropVariantToStringWithDefault(propvar, L""));
478 PropVariantClear(&propvar);
479
480 wchar_t itemDesc[MAX_PATH];
481 ThrowIfFailed(pShellLinkItem->GetDescription(
482 itemDesc, std::extent<decltype(itemDesc)>::value),
483 "GetDescription failed.");
484 item.description = o3tl::toU(itemDesc);
485
486 wchar_t path[MAX_PATH];
487 int icon_index;
488 ThrowIfFailed(pShellLinkItem->GetIconLocation(
489 path, std::extent<decltype(path)>::value, &icon_index),
490 "GetIconLocation failed.");
491 item.iconPath = o3tl::toU(path);
492
493 removedItems.emplace_back(item);
494 }
495 }
496 }
497 }
498 catch (const ComError& e)
499 {
500 SAL_WARN("shell.jumplist", e.what());
501 }
502
503 return containerToSequence(removedItems);
504}
505
506// XServiceInfo
507
508OUString SAL_CALL JumpListImpl::getImplementationName()
509{
510 return "com.sun.star.system.windows.JumpListImpl";
511}
512
513sal_Bool SAL_CALL JumpListImpl::supportsService(const OUString& ServiceName)
514{
515 return cppu::supportsService(this, ServiceName);
516}
517
518Sequence<OUString> SAL_CALL JumpListImpl::getSupportedServiceNames()
519{
520 return { "com.sun.star.system.windows.JumpList" };
521}
522
523extern "C" SAL_DLLPUBLIC_EXPORT XInterface*
524shell_JumpListExec_get_implementation(XComponentContext* context, Sequence<Any> const&)
525{
526 return acquire(new JumpListImpl(context));
527}
528
529/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
SAL_DLLPUBLIC_EXPORT XInterface * shell_JumpListExec_get_implementation(XComponentContext *context, Sequence< Any > const &)
Definition: JumpList.cxx:524
Reference< XComponentContext > m_xContext
std::mutex m_aMutex
#define MAX_PATH
void SAL_CALL removed(::sal_Int32 ID) override
#define SAL_WARN(area, stream)
#define SAL_INFO(area, stream)
err
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
int i
unsigned char sal_Bool