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/.
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>
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
53 constexpr OUString CMD_CLEAR_LIST
= u
".uno:ClearRecentFileList"_ustr
;
54 constexpr OUString CMD_TOGGLE_CURRENTMODULE
= u
".uno:ToggleCurrentModule"_ustr
;
55 constexpr OUString CMD_OPEN_AS_TEMPLATE
= u
".uno:OpenTemplate"_ustr
;
56 constexpr OUString CMD_OPEN_REMOTE
= u
".uno:OpenRemote"_ustr
;
58 class RecentFilesMenuController
: public svt::PopupMenuControllerBase
60 using svt::PopupMenuControllerBase::disposing
;
63 RecentFilesMenuController( const uno::Reference
< uno::XComponentContext
>& xContext
,
64 const uno::Sequence
< uno::Any
>& args
);
67 virtual OUString SAL_CALL
getImplementationName() override
69 return u
"com.sun.star.comp.framework.RecentFilesMenuController"_ustr
;
72 virtual sal_Bool SAL_CALL
supportsService(OUString
const & ServiceName
) override
74 return cppu::supportsService(this, ServiceName
);
77 virtual css::uno::Sequence
<OUString
> SAL_CALL
getSupportedServiceNames() override
79 return {u
"com.sun.star.frame.PopupMenuController"_ustr
};
83 virtual void SAL_CALL
statusChanged( const frame::FeatureStateEvent
& Event
) override
;
86 virtual void SAL_CALL
itemSelected( const awt::MenuEvent
& rEvent
) override
;
87 virtual void SAL_CALL
itemActivated( const awt::MenuEvent
& rEvent
) override
;
90 virtual uno::Reference
< frame::XDispatch
> SAL_CALL
queryDispatch( const util::URL
& aURL
, const OUString
& sTarget
, sal_Int32 nFlags
) override
;
93 virtual void SAL_CALL
dispatch( const util::URL
& aURL
, const uno::Sequence
< beans::PropertyValue
>& seqProperties
) override
;
96 virtual void SAL_CALL
disposing( const css::lang::EventObject
& Source
) override
;
99 virtual void impl_setPopupMenu(std::unique_lock
<std::mutex
>& rGuard
) override
;
100 void fillPopupMenu(std::unique_lock
<std::mutex
>& rGuard
, css::uno::Reference
<css::awt::XPopupMenu
> const & rPopupMenu
);
101 void executeEntry( sal_Int32 nIndex
);
102 void executeEntryImpl(std::unique_lock
<std::mutex
>& rGuard
, sal_Int32 nIndex
);
104 std::vector
<std::pair
<OUString
, bool>> m_aRecentFilesItems
;
105 bool m_bDisabled
: 1;
106 bool m_bShowToolbarEntries
;
109 RecentFilesMenuController::RecentFilesMenuController( const uno::Reference
< uno::XComponentContext
>& xContext
,
110 const uno::Sequence
< uno::Any
>& args
) :
111 svt::PopupMenuControllerBase( xContext
),
112 m_bDisabled( false ),
113 m_bShowToolbarEntries( false )
115 css::beans::PropertyValue aPropValue
;
116 for ( uno::Any
const & arg
: args
)
119 if ( aPropValue
.Name
== "InToolbar" )
121 aPropValue
.Value
>>= m_bShowToolbarEntries
;
127 void InsertItem(const css::uno::Reference
<css::awt::XPopupMenu
>& rPopupMenu
,
128 const OUString
& rCommand
,
129 const css::uno::Reference
<css::frame::XFrame
>& rFrame
)
131 sal_uInt16 nItemId
= rPopupMenu
->getItemCount() + 1;
135 OUString
aModuleName(vcl::CommandInfoProvider::GetModuleIdentifier(rFrame
));
136 auto aProperties
= vcl::CommandInfoProvider::GetCommandProperties(rCommand
, aModuleName
);
137 OUString
aLabel(vcl::CommandInfoProvider::GetPopupLabelForCommand(aProperties
));
138 OUString
aTooltip(vcl::CommandInfoProvider::GetTooltipForCommand(rCommand
, aProperties
, rFrame
));
139 css::uno::Reference
<css::graphic::XGraphic
> xGraphic(vcl::CommandInfoProvider::GetXGraphicForCommand(rCommand
, rFrame
));
141 rPopupMenu
->insertItem(nItemId
, aLabel
, 0, -1);
142 rPopupMenu
->setItemImage(nItemId
, xGraphic
, false);
143 rPopupMenu
->setHelpText(nItemId
, aTooltip
);
146 rPopupMenu
->insertItem(nItemId
, OUString(), 0, -1);
148 rPopupMenu
->setCommand(nItemId
, rCommand
);
153 void RecentFilesMenuController::fillPopupMenu(std::unique_lock
<std::mutex
>& /*rGuard*/, Reference
<css::awt::XPopupMenu
> const & rPopupMenu
)
155 SolarMutexGuard aSolarMutexGuard
;
157 resetPopupMenu( rPopupMenu
);
159 std::vector
< SvtHistoryOptions::HistoryItem
> aHistoryList
= SvtHistoryOptions::GetList( EHistoryType::PickList
);
161 int nPickListMenuItems
= std::min
<sal_Int32
>( aHistoryList
.size(), MAX_MENU_ITEMS
);
162 m_aRecentFilesItems
.clear();
164 // tdf#56696 - retrieve expert configuration option if the recent document
165 // list should show only files that can be handled by the current module
166 const bool bShowCurrentModuleOnly
167 = officecfg::Office::Common::History::ShowCurrentModuleOnly::get();
169 size_t nItemPosModule
= 0;
170 size_t nItemPosPinned
= 0;
171 if (( nPickListMenuItems
> 0 ) && !m_bDisabled
)
175 // tdf#155699 - create a lambda to insert a recent document item at a specified position
176 auto insertHistoryItemAtPos
=
177 [&](const SvtHistoryOptions::HistoryItem
& rPickListEntry
, const size_t aInsertPosition
)
179 m_aRecentFilesItems
.insert(m_aRecentFilesItems
.begin() + aInsertPosition
,
180 { rPickListEntry
.sURL
, rPickListEntry
.isReadOnly
});
184 if (m_aModuleName
!= "com.sun.star.frame.StartModule")
186 ::comphelper::MimeConfigurationHelper
aConfigHelper(
187 comphelper::getProcessComponentContext());
189 // Show the first MAX_MENU_ITEMS_PER_MODULE items of the current module
190 // on top of the recent document list.
191 for (int i
= 0; i
< nPickListMenuItems
; i
++)
193 const SvtHistoryOptions::HistoryItem
& rPickListEntry
= aHistoryList
[i
];
195 // tdf#155699 - insert pinned document at the beginning of the list
196 if (rPickListEntry
.isPinned
)
198 insertHistoryItemAtPos(rPickListEntry
, nItemPosPinned
);
202 // tdf#56696 - insert documents of the current module
203 else if ((bShowCurrentModuleOnly
204 || (nItemPosModule
- nItemPosPinned
) < MAX_MENU_ITEMS_PER_MODULE
)
205 && aConfigHelper
.GetDocServiceNameFromFilter(rPickListEntry
.sFilter
)
208 insertHistoryItemAtPos(rPickListEntry
, nItemPosModule
);
211 // Insert all other documents at the end of the list if the expert option is not set
212 else if (!bShowCurrentModuleOnly
)
213 insertHistoryItemAtPos(rPickListEntry
, nItemPos
);
218 for (int i
= 0; i
< nPickListMenuItems
; i
++)
220 const SvtHistoryOptions::HistoryItem
& rPickListEntry
= aHistoryList
[i
];
221 // tdf#155699 - insert pinned document at the beginning of the list
222 insertHistoryItemAtPos(rPickListEntry
,
223 rPickListEntry
.isPinned
? nItemPosModule
++ : nItemPos
);
228 if ( !m_aRecentFilesItems
.empty() )
230 const sal_uInt32 nCount
= m_aRecentFilesItems
.size();
231 StyleSettings aIconSettings
;
232 bool bIsIconsAllowed
= aIconSettings
.GetUseImagesInMenus();
234 for ( sal_uInt32 i
= 0; i
< nCount
; i
++ )
237 OUStringBuffer aMenuShortCut
;
241 aMenuShortCut
.append( "1~0. " );
244 aMenuShortCut
.append( "~N. " );
245 aMenuShortCut
[ 1 ] = sal_Unicode( i
+ '1' );
250 aMenuShortCut
.append( OUString::number(sal_Int32( i
+ 1 ) ) + ". " );
253 OUString aURLString
= "vnd.sun.star.popup:RecentFileList?entry=" + OUString::number(i
);
257 INetURLObject
const aURL(m_aRecentFilesItems
[i
].first
);
258 OUString
aTipHelpText( aURL
.getFSysPath( FSysStyle::Detect
) );
260 if ( aURL
.GetProtocol() == INetProtocol::File
)
262 // Do handle file URL differently: don't show the protocol, just the file name
263 aMenuTitle
= aURL
.GetLastName(INetURLObject::DecodeMechanism::WithCharset
);
267 // In all other URLs show the protocol name before the file name
268 aMenuTitle
= INetURLObject::GetSchemeName(aURL
.GetProtocol()) + ": " + aURL
.getName();
271 aMenuShortCut
.append( aMenuTitle
);
273 rPopupMenu
->insertItem(sal_uInt16( i
+1 ), aMenuShortCut
.makeStringAndClear(), 0, -1);
275 if ( bIsIconsAllowed
) {
276 // tdf#146219: don't use SvFileInformationManager::GetImageId,
277 // which needs to access the URL to detect if it's a directory
278 BitmapEx
aThumbnail(SvFileInformationManager::GetFileImageId(aURL
));
279 rPopupMenu
->setItemImage(sal_uInt16(i
+ 1), Graphic(aThumbnail
).GetXGraphic(), false);
282 rPopupMenu
->setTipHelpText(sal_uInt16(i
+ 1), aTipHelpText
);
283 rPopupMenu
->setCommand(sal_uInt16(i
+ 1), aURLString
);
285 // tdf#155699 - show a separator after the pinned recent document items
286 if (nItemPosPinned
> 0 && i
== nItemPosPinned
- 1)
287 rPopupMenu
->insertSeparator(-1);
289 // Show a separator after the MAX_MENU_ITEMS_PER_MODULE recent document items
290 if (nItemPosModule
> 0 && i
== nItemPosModule
- 1)
291 rPopupMenu
->insertSeparator(-1);
294 rPopupMenu
->insertSeparator(-1);
295 // Clear List menu entry
296 rPopupMenu
->insertItem(sal_uInt16(nCount
+ 1), FwkResId(STR_CLEAR_RECENT_FILES
), 0, -1);
297 rPopupMenu
->setCommand(sal_uInt16(nCount
+ 1), CMD_CLEAR_LIST
);
298 rPopupMenu
->setHelpText(sal_uInt16(nCount
+ 1), FwkResId(STR_CLEAR_RECENT_FILES_HELP
));
300 // Toggle current module only
301 rPopupMenu
->insertItem(sal_uInt16(nCount
+ 2), FwkResId(STR_TOGGLE_CURRENT_MODULE
), 0, -1);
302 rPopupMenu
->checkItem(sal_uInt16(nCount
+ 2), bShowCurrentModuleOnly
);
303 rPopupMenu
->setCommand(sal_uInt16(nCount
+ 2), CMD_TOGGLE_CURRENTMODULE
);
304 rPopupMenu
->setHelpText(sal_uInt16(nCount
+ 2), FwkResId(STR_TOGGLE_CURRENT_MODULE_HELP
));
306 // Open remote menu entry
307 if ( m_bShowToolbarEntries
)
309 rPopupMenu
->insertSeparator(-1);
310 InsertItem(rPopupMenu
, CMD_OPEN_AS_TEMPLATE
, m_xFrame
);
311 InsertItem(rPopupMenu
, CMD_OPEN_REMOTE
, m_xFrame
);
316 if ( m_bShowToolbarEntries
)
318 InsertItem(rPopupMenu
, CMD_OPEN_AS_TEMPLATE
, m_xFrame
);
319 InsertItem(rPopupMenu
, CMD_OPEN_REMOTE
, m_xFrame
);
323 // Add InsertSeparator(), otherwise it will display
324 // the first item icon of recent files instead of displaying no icon.
325 rPopupMenu
->insertSeparator(-1);
326 // No recent documents => insert "no documents" string
327 // Do not disable it, otherwise the Toolbar controller and MenuButton
328 // will display SV_RESID_STRING_NOSELECTIONPOSSIBLE instead of STR_NODOCUMENT
329 rPopupMenu
->insertItem(1, FwkResId(STR_NODOCUMENT
), static_cast<sal_Int16
>(MenuItemBits::NOSELECT
), -1);
334 void RecentFilesMenuController::executeEntryImpl(std::unique_lock
<std::mutex
>& rGuard
, sal_Int32 nIndex
)
336 if (( nIndex
< 0 ) ||
337 ( nIndex
>= sal::static_int_cast
<sal_Int32
>( m_aRecentFilesItems
.size() )))
340 Sequence
< PropertyValue
> aArgsList
{
341 comphelper::makePropertyValue(u
"Referer"_ustr
, u
"private:user"_ustr
),
343 // documents in the picklist will never be opened as templates
344 comphelper::makePropertyValue(u
"AsTemplate"_ustr
, false),
346 // Type detection needs to know which app we are opening it from.
347 comphelper::makePropertyValue(u
"DocumentService"_ustr
, m_aModuleName
)
349 if (m_aRecentFilesItems
[nIndex
].second
) // tdf#149170 only add if true
351 aArgsList
.realloc(aArgsList
.size()+1);
352 aArgsList
.getArray()[aArgsList
.size()-1] = comphelper::makePropertyValue(u
"ReadOnly"_ustr
, true);
354 dispatchCommandImpl(rGuard
, m_aRecentFilesItems
[nIndex
].first
, aArgsList
, u
"_default"_ustr
);
357 void RecentFilesMenuController::executeEntry( sal_Int32 nIndex
)
359 std::unique_lock
aLock(m_aMutex
);
360 executeEntryImpl(aLock
, nIndex
);
364 void SAL_CALL
RecentFilesMenuController::disposing( const EventObject
& )
366 Reference
< css::awt::XMenuListener
> xHolder(this);
368 std::unique_lock
aLock( m_aMutex
);
372 if ( m_xPopupMenu
.is() )
373 m_xPopupMenu
->removeMenuListener( Reference
< css::awt::XMenuListener
>(this) );
374 m_xPopupMenu
.clear();
378 void SAL_CALL
RecentFilesMenuController::statusChanged( const FeatureStateEvent
& Event
)
380 std::unique_lock
aLock( m_aMutex
);
381 m_bDisabled
= !Event
.IsEnabled
;
384 void SAL_CALL
RecentFilesMenuController::itemSelected( const css::awt::MenuEvent
& rEvent
)
386 Reference
< css::awt::XPopupMenu
> xPopupMenu
;
389 std::unique_lock
aLock(m_aMutex
);
390 xPopupMenu
= m_xPopupMenu
;
393 if ( !xPopupMenu
.is() )
396 const OUString
aCommand( xPopupMenu
->getCommand( rEvent
.MenuId
) );
398 if ( aCommand
== CMD_CLEAR_LIST
)
400 SvtHistoryOptions::Clear( EHistoryType::PickList
, false );
402 u
"vnd.org.libreoffice.recentdocs:ClearRecentFileList"_ustr
,
403 css::uno::Sequence
< css::beans::PropertyValue
>() );
405 if ( aCommand
== CMD_TOGGLE_CURRENTMODULE
)
407 bool bIsExclusive
= officecfg::Office::Common::History::ShowCurrentModuleOnly::get();
408 std::shared_ptr
<comphelper::ConfigurationChanges
> batch(comphelper::ConfigurationChanges::create());
409 officecfg::Office::Common::History::ShowCurrentModuleOnly::set(!bIsExclusive
, batch
);
412 else if ( aCommand
== CMD_OPEN_REMOTE
)
414 Sequence
< PropertyValue
> aArgsList( 0 );
415 dispatchCommand( CMD_OPEN_REMOTE
, aArgsList
);
417 else if ( aCommand
== CMD_OPEN_AS_TEMPLATE
)
419 Sequence
< PropertyValue
> aArgsList( 0 );
420 dispatchCommand( CMD_OPEN_AS_TEMPLATE
, aArgsList
);
423 executeEntry( rEvent
.MenuId
-1 );
426 void SAL_CALL
RecentFilesMenuController::itemActivated( const css::awt::MenuEvent
& )
428 std::unique_lock
aLock( m_aMutex
);
429 impl_setPopupMenu(aLock
);
432 // XPopupMenuController
433 void RecentFilesMenuController::impl_setPopupMenu(std::unique_lock
<std::mutex
>& rGuard
)
435 if ( m_xPopupMenu
.is() )
436 fillPopupMenu(rGuard
, m_xPopupMenu
);
440 Reference
< XDispatch
> SAL_CALL
RecentFilesMenuController::queryDispatch(
442 const OUString
& /*sTarget*/,
443 sal_Int32
/*nFlags*/ )
445 std::unique_lock
aLock( m_aMutex
);
447 throwIfDisposed(aLock
);
449 if ( aURL
.Complete
.startsWith( m_aBaseURL
) )
450 return Reference
< XDispatch
>( this );
452 return Reference
< XDispatch
>();
456 void SAL_CALL
RecentFilesMenuController::dispatch(
458 const Sequence
< PropertyValue
>& /*seqProperties*/ )
460 std::unique_lock
aLock( m_aMutex
);
462 throwIfDisposed(aLock
);
464 if ( !aURL
.Complete
.startsWith( m_aBaseURL
) )
467 // Parse URL to retrieve entry argument and its value
468 sal_Int32 nQueryPart
= aURL
.Complete
.indexOf( '?', m_aBaseURL
.getLength() );
469 if ( nQueryPart
<= 0 )
472 static constexpr OUString
aEntryArgStr( u
"entry="_ustr
);
473 sal_Int32 nEntryArg
= aURL
.Complete
.indexOf( aEntryArgStr
, nQueryPart
);
474 sal_Int32 nEntryPos
= nEntryArg
+ aEntryArgStr
.getLength();
475 if (( nEntryArg
<= 0 ) || ( nEntryPos
>= aURL
.Complete
.getLength() ))
478 sal_Int32 nAddArgs
= aURL
.Complete
.indexOf( '&', nEntryPos
);
479 std::u16string_view aEntryArg
;
482 aEntryArg
= aURL
.Complete
.subView( nEntryPos
);
484 aEntryArg
= aURL
.Complete
.subView( nEntryPos
, nAddArgs
-nEntryPos
);
486 sal_Int32 nEntry
= o3tl::toInt32(aEntryArg
);
487 executeEntryImpl(aLock
, nEntry
);
492 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
493 com_sun_star_comp_framework_RecentFilesMenuController_get_implementation(
494 css::uno::XComponentContext
*context
,
495 css::uno::Sequence
<css::uno::Any
> const &args
)
497 return cppu::acquire(new RecentFilesMenuController(context
, args
));
500 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */