Avoid potential negative array index access to cached text.
[LibreOffice.git] / framework / source / uielement / recentfilesmenucontroller.cxx
blob4355069c683d0310f4e61c4e2fe05009ef1cfab5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <strings.hrc>
21 #include <classes/fwkresid.hxx>
23 #include <comphelper/mimeconfighelper.hxx>
24 #include <comphelper/processfactory.hxx>
25 #include <comphelper/propertyvalue.hxx>
26 #include <cppuhelper/supportsservice.hxx>
27 #include <osl/mutex.hxx>
28 #include <svtools/imagemgr.hxx>
29 #include <svtools/popupmenucontrollerbase.hxx>
30 #include <tools/urlobj.hxx>
31 #include <toolkit/awt/vclxmenu.hxx>
32 #include <unotools/historyoptions.hxx>
33 #include <vcl/commandinfoprovider.hxx>
34 #include <vcl/graph.hxx>
35 #include <vcl/settings.hxx>
36 #include <vcl/svapp.hxx>
37 #include <o3tl/string_view.hxx>
39 #include <officecfg/Office/Common.hxx>
41 using namespace css;
42 using namespace com::sun::star::uno;
43 using namespace com::sun::star::lang;
44 using namespace com::sun::star::frame;
45 using namespace com::sun::star::beans;
46 using namespace com::sun::star::util;
48 #define MAX_MENU_ITEMS 99
49 #define MAX_MENU_ITEMS_PER_MODULE 5
51 namespace {
53 constexpr OUString CMD_CLEAR_LIST = u".uno:ClearRecentFileList"_ustr;
54 constexpr OUString CMD_OPEN_AS_TEMPLATE = u".uno:OpenTemplate"_ustr;
55 constexpr OUString CMD_OPEN_REMOTE = u".uno:OpenRemote"_ustr;
57 class RecentFilesMenuController : public svt::PopupMenuControllerBase
59 using svt::PopupMenuControllerBase::disposing;
61 public:
62 RecentFilesMenuController( const uno::Reference< uno::XComponentContext >& xContext,
63 const uno::Sequence< uno::Any >& args );
65 // XServiceInfo
66 virtual OUString SAL_CALL getImplementationName() override
68 return "com.sun.star.comp.framework.RecentFilesMenuController";
71 virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
73 return cppu::supportsService(this, ServiceName);
76 virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
78 return {"com.sun.star.frame.PopupMenuController"};
81 // XStatusListener
82 virtual void SAL_CALL statusChanged( const frame::FeatureStateEvent& Event ) override;
84 // XMenuListener
85 virtual void SAL_CALL itemSelected( const awt::MenuEvent& rEvent ) override;
86 virtual void SAL_CALL itemActivated( const awt::MenuEvent& rEvent ) override;
88 // XDispatchProvider
89 virtual uno::Reference< frame::XDispatch > SAL_CALL queryDispatch( const util::URL& aURL, const OUString& sTarget, sal_Int32 nFlags ) override;
91 // XDispatch
92 virtual void SAL_CALL dispatch( const util::URL& aURL, const uno::Sequence< beans::PropertyValue >& seqProperties ) override;
94 // XEventListener
95 virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override;
97 private:
98 virtual void impl_setPopupMenu() override;
99 void fillPopupMenu( css::uno::Reference< css::awt::XPopupMenu > const & rPopupMenu );
100 void executeEntry( sal_Int32 nIndex );
102 std::vector<std::pair<OUString, bool>> m_aRecentFilesItems;
103 bool m_bDisabled : 1;
104 bool m_bShowToolbarEntries;
107 RecentFilesMenuController::RecentFilesMenuController( const uno::Reference< uno::XComponentContext >& xContext,
108 const uno::Sequence< uno::Any >& args ) :
109 svt::PopupMenuControllerBase( xContext ),
110 m_bDisabled( false ),
111 m_bShowToolbarEntries( false )
113 css::beans::PropertyValue aPropValue;
114 for ( uno::Any const & arg : args )
116 arg >>= aPropValue;
117 if ( aPropValue.Name == "InToolbar" )
119 aPropValue.Value >>= m_bShowToolbarEntries;
120 break;
125 void InsertItem(const css::uno::Reference<css::awt::XPopupMenu>& rPopupMenu,
126 const OUString& rCommand,
127 const css::uno::Reference<css::frame::XFrame>& rFrame)
129 sal_uInt16 nItemId = rPopupMenu->getItemCount() + 1;
131 if (rFrame.is())
133 OUString aModuleName(vcl::CommandInfoProvider::GetModuleIdentifier(rFrame));
134 auto aProperties = vcl::CommandInfoProvider::GetCommandProperties(rCommand, aModuleName);
135 OUString aLabel(vcl::CommandInfoProvider::GetPopupLabelForCommand(aProperties));
136 OUString aTooltip(vcl::CommandInfoProvider::GetTooltipForCommand(rCommand, aProperties, rFrame));
137 css::uno::Reference<css::graphic::XGraphic> xGraphic(vcl::CommandInfoProvider::GetXGraphicForCommand(rCommand, rFrame));
139 rPopupMenu->insertItem(nItemId, aLabel, 0, -1);
140 rPopupMenu->setItemImage(nItemId, xGraphic, false);
141 rPopupMenu->setHelpText(nItemId, aTooltip);
143 else
144 rPopupMenu->insertItem(nItemId, OUString(), 0, -1);
146 rPopupMenu->setCommand(nItemId, rCommand);
150 // private function
151 void RecentFilesMenuController::fillPopupMenu( Reference< css::awt::XPopupMenu > const & rPopupMenu )
153 SolarMutexGuard aSolarMutexGuard;
155 resetPopupMenu( rPopupMenu );
157 std::vector< SvtHistoryOptions::HistoryItem > aHistoryList = SvtHistoryOptions::GetList( EHistoryType::PickList );
159 int nPickListMenuItems = std::min<sal_Int32>( aHistoryList.size(), MAX_MENU_ITEMS );
160 m_aRecentFilesItems.clear();
162 // tdf#56696 - retrieve expert configuration option if the recent document
163 // list should show only files that can be handled by the current module
164 const bool bShowCurrentModuleOnly
165 = officecfg::Office::Common::History::ShowCurrentModuleOnly::get();
167 size_t nItemPosModule = 0;
168 size_t nItemPosPinned = 0;
169 if (( nPickListMenuItems > 0 ) && !m_bDisabled )
171 size_t nItemPos = 0;
173 // tdf#155699 - create a lambda to insert a recent document item at a specified position
174 auto insertHistoryItemAtPos =
175 [&](const SvtHistoryOptions::HistoryItem& rPickListEntry, const size_t aInsertPosition)
177 m_aRecentFilesItems.insert(m_aRecentFilesItems.begin() + aInsertPosition,
178 { rPickListEntry.sURL, rPickListEntry.isReadOnly });
179 nItemPos++;
182 if (m_aModuleName != "com.sun.star.frame.StartModule")
184 ::comphelper::MimeConfigurationHelper aConfigHelper(
185 comphelper::getProcessComponentContext());
187 // Show the first MAX_MENU_ITEMS_PER_MODULE items of the current module
188 // on top of the recent document list.
189 for (int i = 0; i < nPickListMenuItems; i++)
191 const SvtHistoryOptions::HistoryItem& rPickListEntry = aHistoryList[i];
193 // tdf#155699 - insert pinned document at the beginning of the list
194 if (rPickListEntry.isPinned)
196 insertHistoryItemAtPos(rPickListEntry, nItemPosPinned);
197 nItemPosPinned++;
198 nItemPosModule++;
200 // tdf#56696 - insert documents of the current module
201 else if ((bShowCurrentModuleOnly
202 || (nItemPosModule - nItemPosPinned) < MAX_MENU_ITEMS_PER_MODULE)
203 && aConfigHelper.GetDocServiceNameFromFilter(rPickListEntry.sFilter)
204 == m_aModuleName)
206 insertHistoryItemAtPos(rPickListEntry, nItemPosModule);
207 nItemPosModule++;
209 // Insert all other documents at the end of the list if the expert option is not set
210 else if (!bShowCurrentModuleOnly)
211 insertHistoryItemAtPos(rPickListEntry, nItemPos);
214 else
216 for (int i = 0; i < nPickListMenuItems; i++)
218 const SvtHistoryOptions::HistoryItem& rPickListEntry = aHistoryList[i];
219 // tdf#155699 - insert pinned document at the beginning of the list
220 insertHistoryItemAtPos(rPickListEntry,
221 rPickListEntry.isPinned ? nItemPosModule++ : nItemPos);
226 if ( !m_aRecentFilesItems.empty() )
228 const sal_uInt32 nCount = m_aRecentFilesItems.size();
229 StyleSettings aIconSettings;
230 bool bIsIconsAllowed = aIconSettings.GetUseImagesInMenus();
232 for ( sal_uInt32 i = 0; i < nCount; i++ )
235 OUStringBuffer aMenuShortCut;
236 if ( i <= 9 )
238 if ( i == 9 )
239 aMenuShortCut.append( "1~0. " );
240 else
242 aMenuShortCut.append( "~N. " );
243 aMenuShortCut[ 1 ] = sal_Unicode( i + '1' );
246 else
248 aMenuShortCut.append( OUString::number(sal_Int32( i + 1 ) ) + ". " );
251 OUString aURLString = "vnd.sun.star.popup:RecentFileList?entry=" + OUString::number(i);
253 // Abbreviate URL
254 OUString aMenuTitle;
255 INetURLObject const aURL(m_aRecentFilesItems[i].first);
256 OUString aTipHelpText( aURL.getFSysPath( FSysStyle::Detect ) );
258 if ( aURL.GetProtocol() == INetProtocol::File )
260 // Do handle file URL differently: don't show the protocol, just the file name
261 aMenuTitle = aURL.GetLastName(INetURLObject::DecodeMechanism::WithCharset);
263 else
265 // In all other URLs show the protocol name before the file name
266 aMenuTitle = INetURLObject::GetSchemeName(aURL.GetProtocol()) + ": " + aURL.getName();
269 aMenuShortCut.append( aMenuTitle );
271 rPopupMenu->insertItem(sal_uInt16( i+1 ), aMenuShortCut.makeStringAndClear(), 0, -1);
273 if ( bIsIconsAllowed ) {
274 // tdf#146219: don't use SvFileInformationManager::GetImageId,
275 // which needs to access the URL to detect if it's a directory
276 BitmapEx aThumbnail(SvFileInformationManager::GetFileImageId(aURL));
277 rPopupMenu->setItemImage(sal_uInt16(i + 1), Graphic(aThumbnail).GetXGraphic(), false);
280 rPopupMenu->setTipHelpText(sal_uInt16(i + 1), aTipHelpText);
281 rPopupMenu->setCommand(sal_uInt16(i + 1), aURLString);
283 // tdf#155699 - show a separator after the pinned recent document items
284 if (nItemPosPinned > 0 && i == nItemPosPinned - 1)
285 rPopupMenu->insertSeparator(-1);
287 // Show a separator after the MAX_MENU_ITEMS_PER_MODULE recent document items
288 if (nItemPosModule > 0 && i == nItemPosModule - 1)
289 rPopupMenu->insertSeparator(-1);
292 rPopupMenu->insertSeparator(-1);
293 // Clear List menu entry
294 rPopupMenu->insertItem(sal_uInt16(nCount + 1), FwkResId(STR_CLEAR_RECENT_FILES), 0, -1);
295 rPopupMenu->setCommand(sal_uInt16(nCount + 1), CMD_CLEAR_LIST);
296 rPopupMenu->setHelpText(sal_uInt16(nCount + 1), FwkResId(STR_CLEAR_RECENT_FILES_HELP));
298 // Open remote menu entry
299 if ( m_bShowToolbarEntries )
301 rPopupMenu->insertSeparator(-1);
302 InsertItem(rPopupMenu, CMD_OPEN_AS_TEMPLATE, m_xFrame);
303 InsertItem(rPopupMenu, CMD_OPEN_REMOTE, m_xFrame);
306 else
308 if ( m_bShowToolbarEntries )
310 InsertItem(rPopupMenu, CMD_OPEN_AS_TEMPLATE, m_xFrame);
311 InsertItem(rPopupMenu, CMD_OPEN_REMOTE, m_xFrame);
313 else
315 // Add InsertSeparator(), otherwise it will display
316 // the first item icon of recent files instead of displaying no icon.
317 rPopupMenu->insertSeparator(-1);
318 // No recent documents => insert "no documents" string
319 // Do not disable it, otherwise the Toolbar controller and MenuButton
320 // will display SV_RESID_STRING_NOSELECTIONPOSSIBLE instead of STR_NODOCUMENT
321 rPopupMenu->insertItem(1, FwkResId(STR_NODOCUMENT), static_cast<sal_Int16>(MenuItemBits::NOSELECT), -1);
326 void RecentFilesMenuController::executeEntry( sal_Int32 nIndex )
328 if (( nIndex < 0 ) ||
329 ( nIndex >= sal::static_int_cast<sal_Int32>( m_aRecentFilesItems.size() )))
330 return;
332 Sequence< PropertyValue > aArgsList{
333 comphelper::makePropertyValue("Referer", OUString( "private:user" )),
335 // documents in the picklist will never be opened as templates
336 comphelper::makePropertyValue("AsTemplate", false),
338 // Type detection needs to know which app we are opening it from.
339 comphelper::makePropertyValue("DocumentService", m_aModuleName)
341 if (m_aRecentFilesItems[nIndex].second) // tdf#149170 only add if true
343 aArgsList.realloc(aArgsList.size()+1);
344 aArgsList.getArray()[aArgsList.size()-1] = comphelper::makePropertyValue("ReadOnly", true);
346 dispatchCommand(m_aRecentFilesItems[nIndex].first, aArgsList, "_default");
349 // XEventListener
350 void SAL_CALL RecentFilesMenuController::disposing( const EventObject& )
352 Reference< css::awt::XMenuListener > xHolder(this);
354 std::unique_lock aLock( m_aMutex );
355 m_xFrame.clear();
356 m_xDispatch.clear();
358 if ( m_xPopupMenu.is() )
359 m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(this) );
360 m_xPopupMenu.clear();
363 // XStatusListener
364 void SAL_CALL RecentFilesMenuController::statusChanged( const FeatureStateEvent& Event )
366 std::unique_lock aLock( m_aMutex );
367 m_bDisabled = !Event.IsEnabled;
370 void SAL_CALL RecentFilesMenuController::itemSelected( const css::awt::MenuEvent& rEvent )
372 Reference< css::awt::XPopupMenu > xPopupMenu;
375 std::unique_lock aLock(m_aMutex);
376 xPopupMenu = m_xPopupMenu;
379 if ( !xPopupMenu.is() )
380 return;
382 const OUString aCommand( xPopupMenu->getCommand( rEvent.MenuId ) );
384 if ( aCommand == CMD_CLEAR_LIST )
386 SvtHistoryOptions::Clear( EHistoryType::PickList, false );
387 dispatchCommand(
388 "vnd.org.libreoffice.recentdocs:ClearRecentFileList",
389 css::uno::Sequence< css::beans::PropertyValue >() );
391 else if ( aCommand == CMD_OPEN_REMOTE )
393 Sequence< PropertyValue > aArgsList( 0 );
394 dispatchCommand( CMD_OPEN_REMOTE, aArgsList );
396 else if ( aCommand == CMD_OPEN_AS_TEMPLATE )
398 Sequence< PropertyValue > aArgsList( 0 );
399 dispatchCommand( CMD_OPEN_AS_TEMPLATE, aArgsList );
401 else
402 executeEntry( rEvent.MenuId-1 );
405 void SAL_CALL RecentFilesMenuController::itemActivated( const css::awt::MenuEvent& )
407 std::unique_lock aLock( m_aMutex );
408 impl_setPopupMenu();
411 // XPopupMenuController
412 void RecentFilesMenuController::impl_setPopupMenu()
414 if ( m_xPopupMenu.is() )
415 fillPopupMenu( m_xPopupMenu );
418 // XDispatchProvider
419 Reference< XDispatch > SAL_CALL RecentFilesMenuController::queryDispatch(
420 const URL& aURL,
421 const OUString& /*sTarget*/,
422 sal_Int32 /*nFlags*/ )
424 std::unique_lock aLock( m_aMutex );
426 throwIfDisposed(aLock);
428 if ( aURL.Complete.startsWith( m_aBaseURL ) )
429 return Reference< XDispatch >( this );
430 else
431 return Reference< XDispatch >();
434 // XDispatch
435 void SAL_CALL RecentFilesMenuController::dispatch(
436 const URL& aURL,
437 const Sequence< PropertyValue >& /*seqProperties*/ )
439 std::unique_lock aLock( m_aMutex );
441 throwIfDisposed(aLock);
443 if ( !aURL.Complete.startsWith( m_aBaseURL ) )
444 return;
446 // Parse URL to retrieve entry argument and its value
447 sal_Int32 nQueryPart = aURL.Complete.indexOf( '?', m_aBaseURL.getLength() );
448 if ( nQueryPart <= 0 )
449 return;
451 static constexpr OUString aEntryArgStr( u"entry="_ustr );
452 sal_Int32 nEntryArg = aURL.Complete.indexOf( aEntryArgStr, nQueryPart );
453 sal_Int32 nEntryPos = nEntryArg + aEntryArgStr.getLength();
454 if (( nEntryArg <= 0 ) || ( nEntryPos >= aURL.Complete.getLength() ))
455 return;
457 sal_Int32 nAddArgs = aURL.Complete.indexOf( '&', nEntryPos );
458 std::u16string_view aEntryArg;
460 if ( nAddArgs < 0 )
461 aEntryArg = aURL.Complete.subView( nEntryPos );
462 else
463 aEntryArg = aURL.Complete.subView( nEntryPos, nAddArgs-nEntryPos );
465 sal_Int32 nEntry = o3tl::toInt32(aEntryArg);
466 executeEntry( nEntry );
471 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
472 com_sun_star_comp_framework_RecentFilesMenuController_get_implementation(
473 css::uno::XComponentContext *context,
474 css::uno::Sequence<css::uno::Any> const &args)
476 return cppu::acquire(new RecentFilesMenuController(context, args));
479 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */