LibreOffice Module framework (master) 1
recentfilesmenucontroller.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <strings.hrc>
21#include <classes/fwkresid.hxx>
22
27#include <osl/mutex.hxx>
28#include <svtools/imagemgr.hxx>
30#include <tools/urlobj.hxx>
34#include <vcl/graph.hxx>
35#include <vcl/settings.hxx>
36#include <vcl/svapp.hxx>
37#include <o3tl/string_view.hxx>
38
39#include <officecfg/Office/Common.hxx>
40
41using namespace css;
42using namespace com::sun::star::uno;
43using namespace com::sun::star::lang;
44using namespace com::sun::star::frame;
45using namespace com::sun::star::beans;
46using namespace com::sun::star::util;
47
48#define MAX_MENU_ITEMS 99
49#define MAX_MENU_ITEMS_PER_MODULE 5
50
51namespace {
52
53constexpr OUStringLiteral CMD_CLEAR_LIST = u".uno:ClearRecentFileList";
54constexpr OUStringLiteral CMD_OPEN_AS_TEMPLATE = u".uno:OpenTemplate";
55constexpr OUStringLiteral CMD_OPEN_REMOTE = u".uno:OpenRemote";
56
57class RecentFilesMenuController : public svt::PopupMenuControllerBase
58{
59 using svt::PopupMenuControllerBase::disposing;
60
61public:
62 RecentFilesMenuController( const uno::Reference< uno::XComponentContext >& xContext,
63 const uno::Sequence< uno::Any >& args );
64
65 // XServiceInfo
66 virtual OUString SAL_CALL getImplementationName() override
67 {
68 return "com.sun.star.comp.framework.RecentFilesMenuController";
69 }
70
71 virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
72 {
74 }
75
76 virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
77 {
78 return {"com.sun.star.frame.PopupMenuController"};
79 }
80
81 // XStatusListener
82 virtual void SAL_CALL statusChanged( const frame::FeatureStateEvent& Event ) override;
83
84 // XMenuListener
85 virtual void SAL_CALL itemSelected( const awt::MenuEvent& rEvent ) override;
86 virtual void SAL_CALL itemActivated( const awt::MenuEvent& rEvent ) override;
87
88 // XDispatchProvider
89 virtual uno::Reference< frame::XDispatch > SAL_CALL queryDispatch( const util::URL& aURL, const OUString& sTarget, sal_Int32 nFlags ) override;
90
91 // XDispatch
92 virtual void SAL_CALL dispatch( const util::URL& aURL, const uno::Sequence< beans::PropertyValue >& seqProperties ) override;
93
94 // XEventListener
95 virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override;
96
97private:
98 virtual void impl_setPopupMenu() override;
99 void fillPopupMenu( css::uno::Reference< css::awt::XPopupMenu > const & rPopupMenu );
100 void executeEntry( sal_Int32 nIndex );
101
102 std::vector<std::pair<OUString, bool>> m_aRecentFilesItems;
103 bool m_bDisabled : 1;
104 bool m_bShowToolbarEntries;
105};
106
107RecentFilesMenuController::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 )
112{
113 css::beans::PropertyValue aPropValue;
114 for ( uno::Any const & arg : args )
115 {
116 arg >>= aPropValue;
117 if ( aPropValue.Name == "InToolbar" )
118 {
119 aPropValue.Value >>= m_bShowToolbarEntries;
120 break;
121 }
122 }
123}
124
125void InsertItem(const css::uno::Reference<css::awt::XPopupMenu>& rPopupMenu,
126 const OUString& rCommand,
127 const css::uno::Reference<css::frame::XFrame>& rFrame)
128{
129 sal_uInt16 nItemId = rPopupMenu->getItemCount() + 1;
130
131 if (rFrame.is())
132 {
133 OUString aModuleName(vcl::CommandInfoProvider::GetModuleIdentifier(rFrame));
136 OUString aTooltip(vcl::CommandInfoProvider::GetTooltipForCommand(rCommand, aProperties, rFrame));
137 css::uno::Reference<css::graphic::XGraphic> xGraphic(vcl::CommandInfoProvider::GetXGraphicForCommand(rCommand, rFrame));
138
139 rPopupMenu->insertItem(nItemId, aLabel, 0, -1);
140 rPopupMenu->setItemImage(nItemId, xGraphic, false);
141 rPopupMenu->setHelpText(nItemId, aTooltip);
142 }
143 else
144 rPopupMenu->insertItem(nItemId, OUString(), 0, -1);
145
146 rPopupMenu->setCommand(nItemId, rCommand);
147}
148
149
150// private function
151void RecentFilesMenuController::fillPopupMenu( Reference< css::awt::XPopupMenu > const & rPopupMenu )
152{
153 SolarMutexGuard aSolarMutexGuard;
154
155 resetPopupMenu( rPopupMenu );
156
157 std::vector< SvtHistoryOptions::HistoryItem > aHistoryList = SvtHistoryOptions::GetList( EHistoryType::PickList );
158
159 int nPickListMenuItems = std::min<sal_Int32>( aHistoryList.size(), MAX_MENU_ITEMS );
160 m_aRecentFilesItems.clear();
161
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();
166
167 size_t nItemPosModule = 0;
168 size_t nItemPosPinned = 0;
169 if (( nPickListMenuItems > 0 ) && !m_bDisabled )
170 {
171 size_t nItemPos = 0;
172
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)
176 {
177 m_aRecentFilesItems.insert(m_aRecentFilesItems.begin() + aInsertPosition,
178 { rPickListEntry.sURL, rPickListEntry.isReadOnly });
179 nItemPos++;
180 };
181
182 if (m_aModuleName != "com.sun.star.frame.StartModule")
183 {
186
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++)
190 {
191 const SvtHistoryOptions::HistoryItem& rPickListEntry = aHistoryList[i];
192
193 // tdf#155699 - insert pinned document at the beginning of the list
194 if (rPickListEntry.isPinned)
195 {
196 insertHistoryItemAtPos(rPickListEntry, nItemPosPinned);
197 nItemPosPinned++;
198 nItemPosModule++;
199 }
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)
205 {
206 insertHistoryItemAtPos(rPickListEntry, nItemPosModule);
207 nItemPosModule++;
208 }
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);
212 }
213 }
214 else
215 {
216 for (int i = 0; i < nPickListMenuItems; i++)
217 {
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);
222 }
223 }
224 }
225
226 if ( !m_aRecentFilesItems.empty() )
227 {
228 const sal_uInt32 nCount = m_aRecentFilesItems.size();
229 StyleSettings aIconSettings;
230 bool bIsIconsAllowed = aIconSettings.GetUseImagesInMenus();
231
232 for ( sal_uInt32 i = 0; i < nCount; i++ )
233 {
234
235 OUStringBuffer aMenuShortCut;
236 if ( i <= 9 )
237 {
238 if ( i == 9 )
239 aMenuShortCut.append( "1~0. " );
240 else
241 {
242 aMenuShortCut.append( "~N. " );
243 aMenuShortCut[ 1 ] = sal_Unicode( i + '1' );
244 }
245 }
246 else
247 {
248 aMenuShortCut.append( OUString::number(sal_Int32( i + 1 ) ) + ". " );
249 }
250
251 OUString aURLString = "vnd.sun.star.popup:RecentFileList?entry=" + OUString::number(i);
252
253 // Abbreviate URL
254 OUString aMenuTitle;
255 INetURLObject const aURL(m_aRecentFilesItems[i].first);
256 OUString aTipHelpText( aURL.getFSysPath( FSysStyle::Detect ) );
257
258 if ( aURL.GetProtocol() == INetProtocol::File )
259 {
260 // Do handle file URL differently: don't show the protocol, just the file name
261 aMenuTitle = aURL.GetLastName(INetURLObject::DecodeMechanism::WithCharset);
262 }
263 else
264 {
265 // In all other URLs show the protocol name before the file name
266 aMenuTitle = INetURLObject::GetSchemeName(aURL.GetProtocol()) + ": " + aURL.getName();
267 }
268
269 aMenuShortCut.append( aMenuTitle );
270
271 rPopupMenu->insertItem(sal_uInt16( i+1 ), aMenuShortCut.makeStringAndClear(), 0, -1);
272
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
277 rPopupMenu->setItemImage(sal_uInt16(i + 1), Graphic(aThumbnail).GetXGraphic(), false);
278 }
279
280 rPopupMenu->setTipHelpText(sal_uInt16(i + 1), aTipHelpText);
281 rPopupMenu->setCommand(sal_uInt16(i + 1), aURLString);
282
283 // tdf#155699 - show a separator after the pinned recent document items
284 if (nItemPosPinned > 0 && i == nItemPosPinned - 1)
285 rPopupMenu->insertSeparator(-1);
286
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);
290 }
291
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));
297
298 // Open remote menu entry
299 if ( m_bShowToolbarEntries )
300 {
301 rPopupMenu->insertSeparator(-1);
302 InsertItem(rPopupMenu, CMD_OPEN_AS_TEMPLATE, m_xFrame);
303 InsertItem(rPopupMenu, CMD_OPEN_REMOTE, m_xFrame);
304 }
305 }
306 else
307 {
308 if ( m_bShowToolbarEntries )
309 {
310 InsertItem(rPopupMenu, CMD_OPEN_AS_TEMPLATE, m_xFrame);
311 InsertItem(rPopupMenu, CMD_OPEN_REMOTE, m_xFrame);
312 }
313 else
314 {
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);
322 }
323 }
324}
325
326void RecentFilesMenuController::executeEntry( sal_Int32 nIndex )
327{
328 if (( nIndex < 0 ) ||
329 ( nIndex >= sal::static_int_cast<sal_Int32>( m_aRecentFilesItems.size() )))
330 return;
331
333 comphelper::makePropertyValue("Referer", OUString( "private:user" )),
334
335 // documents in the picklist will never be opened as templates
336 comphelper::makePropertyValue("AsTemplate", false),
337
338 // Type detection needs to know which app we are opening it from.
339 comphelper::makePropertyValue("DocumentService", m_aModuleName)
340 };
341 if (m_aRecentFilesItems[nIndex].second) // tdf#149170 only add if true
342 {
343 aArgsList.realloc(aArgsList.size()+1);
344 aArgsList.getArray()[aArgsList.size()-1] = comphelper::makePropertyValue("ReadOnly", true);
345 }
346 dispatchCommand(m_aRecentFilesItems[nIndex].first, aArgsList, "_default");
347}
348
349// XEventListener
350void SAL_CALL RecentFilesMenuController::disposing( const EventObject& )
351{
353
354 std::unique_lock aLock( m_aMutex );
355 m_xFrame.clear();
356 m_xDispatch.clear();
357
358 if ( m_xPopupMenu.is() )
359 m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(this) );
360 m_xPopupMenu.clear();
361}
362
363// XStatusListener
364void SAL_CALL RecentFilesMenuController::statusChanged( const FeatureStateEvent& Event )
365{
366 std::unique_lock aLock( m_aMutex );
367 m_bDisabled = !Event.IsEnabled;
368}
369
370void SAL_CALL RecentFilesMenuController::itemSelected( const css::awt::MenuEvent& rEvent )
371{
373
374 {
375 std::unique_lock aLock(m_aMutex);
376 xPopupMenu = m_xPopupMenu;
377 }
378
379 if ( !xPopupMenu.is() )
380 return;
381
382 const OUString aCommand( xPopupMenu->getCommand( rEvent.MenuId ) );
383
384 if ( aCommand == CMD_CLEAR_LIST )
385 {
386 SvtHistoryOptions::Clear( EHistoryType::PickList );
388 "vnd.org.libreoffice.recentdocs:ClearRecentFileList",
389 css::uno::Sequence< css::beans::PropertyValue >() );
390 }
391 else if ( aCommand == CMD_OPEN_REMOTE )
392 {
393 Sequence< PropertyValue > aArgsList( 0 );
394 dispatchCommand( CMD_OPEN_REMOTE, aArgsList );
395 }
396 else if ( aCommand == CMD_OPEN_AS_TEMPLATE )
397 {
398 Sequence< PropertyValue > aArgsList( 0 );
399 dispatchCommand( CMD_OPEN_AS_TEMPLATE, aArgsList );
400 }
401 else
402 executeEntry( rEvent.MenuId-1 );
403}
404
405void SAL_CALL RecentFilesMenuController::itemActivated( const css::awt::MenuEvent& )
406{
407 std::unique_lock aLock( m_aMutex );
408 impl_setPopupMenu();
409}
410
411// XPopupMenuController
412void RecentFilesMenuController::impl_setPopupMenu()
413{
414 if ( m_xPopupMenu.is() )
415 fillPopupMenu( m_xPopupMenu );
416}
417
418// XDispatchProvider
419Reference< XDispatch > SAL_CALL RecentFilesMenuController::queryDispatch(
420 const URL& aURL,
421 const OUString& /*sTarget*/,
422 sal_Int32 /*nFlags*/ )
423{
424 std::unique_lock aLock( m_aMutex );
425
426 throwIfDisposed(aLock);
427
428 if ( aURL.Complete.startsWith( m_aBaseURL ) )
429 return Reference< XDispatch >( this );
430 else
431 return Reference< XDispatch >();
432}
433
434// XDispatch
435void SAL_CALL RecentFilesMenuController::dispatch(
436 const URL& aURL,
437 const Sequence< PropertyValue >& /*seqProperties*/ )
438{
439 std::unique_lock aLock( m_aMutex );
440
441 throwIfDisposed(aLock);
442
443 if ( !aURL.Complete.startsWith( m_aBaseURL ) )
444 return;
445
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;
450
451 static constexpr OUStringLiteral aEntryArgStr( u"entry=" );
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;
456
457 sal_Int32 nAddArgs = aURL.Complete.indexOf( '&', nEntryPos );
458 std::u16string_view aEntryArg;
459
460 if ( nAddArgs < 0 )
461 aEntryArg = aURL.Complete.subView( nEntryPos );
462 else
463 aEntryArg = aURL.Complete.subView( nEntryPos, nAddArgs-nEntryPos );
464
465 sal_Int32 nEntry = o3tl::toInt32(aEntryArg);
466 executeEntry( nEntry );
467}
468
469}
470
471extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
473 css::uno::XComponentContext *context,
474 css::uno::Sequence<css::uno::Any> const &args)
475{
476 return cppu::acquire(new RecentFilesMenuController(context, args));
477}
478
479/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
PropertiesInfo aProperties
css::uno::Reference< css::lang::XComponent > m_xFrame
css::uno::Reference< css::frame::XDispatch > m_xDispatch
static const OUString & GetSchemeName(INetProtocol eTheScheme)
bool GetUseImagesInMenus() const
static SVT_DLLPUBLIC OUString GetFileImageId(const INetURLObject &rURL)
OUString GetDocServiceNameFromFilter(const OUString &aFilterName)
int nCount
URL aURL
float u
OUString FwkResId(TranslateId aId)
Definition: fwkresid.cxx:22
sal_Int32 nIndex
void SAL_CALL itemActivated(const css::awt::MenuEvent &rEvent) override
void SAL_CALL itemSelected(const css::awt::MenuEvent &rEvent) override
void Clear(EHistoryType eHistory)
std::vector< HistoryItem > GetList(EHistoryType eHistory)
bool dispatchCommand(const OUString &rCommand, const uno::Reference< css::frame::XFrame > &rFrame, const css::uno::Sequence< css::beans::PropertyValue > &rArguments, const uno::Reference< css::frame::XDispatchResultListener > &rListener)
Reference< XComponentContext > getProcessComponentContext()
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
int i
constexpr OUStringLiteral first
sal_Int32 toInt32(std::u16string_view str, sal_Int16 radix=10)
args
Sequence< beans::PropertyValue > GetCommandProperties(const OUString &rsCommandName, const OUString &rsModuleName)
OUString GetTooltipForCommand(const OUString &rsCommandName, const css::uno::Sequence< css::beans::PropertyValue > &rProperties, const Reference< frame::XFrame > &rxFrame)
Reference< graphic::XGraphic > GetXGraphicForCommand(const OUString &rsCommandName, const Reference< frame::XFrame > &rxFrame, vcl::ImageType eImageType)
OUString GetModuleIdentifier(const Reference< frame::XFrame > &rxFrame)
OUString GetPopupLabelForCommand(const css::uno::Sequence< css::beans::PropertyValue > &rProperties)
#define MAX_MENU_ITEMS_PER_MODULE
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_framework_RecentFilesMenuController_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &args)
#define MAX_MENU_ITEMS
OUString aCommand
unsigned char sal_Bool
sal_uInt16 sal_Unicode
std::mutex m_aMutex
OUString aLabel