LCOV - code coverage report
Current view: top level - framework/source/uielement - recentfilesmenucontroller.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 1 174 0.6 %
Date: 2014-11-03 Functions: 2 25 8.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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 <classes/resource.hrc>
      21             : #include <classes/fwkresid.hxx>
      22             : 
      23             : #include <cppuhelper/implbase1.hxx>
      24             : #include <cppuhelper/supportsservice.hxx>
      25             : #include <osl/file.hxx>
      26             : #include <osl/mutex.hxx>
      27             : #include <rtl/ref.hxx>
      28             : #include <svtools/popupmenucontrollerbase.hxx>
      29             : #include <tools/urlobj.hxx>
      30             : #include <unotools/historyoptions.hxx>
      31             : #include <vcl/menu.hxx>
      32             : #include <vcl/svapp.hxx>
      33             : 
      34             : using namespace css;
      35             : using namespace com::sun::star::uno;
      36             : using namespace com::sun::star::lang;
      37             : using namespace com::sun::star::frame;
      38             : using namespace com::sun::star::beans;
      39             : using namespace com::sun::star::util;
      40             : using namespace framework;
      41             : 
      42             : #define MAX_MENU_ITEMS  99
      43             : 
      44             : namespace {
      45             : 
      46             : static const char CMD_CLEAR_LIST[]   = ".uno:ClearRecentFileList";
      47             : static const char CMD_PREFIX[]       = "vnd.sun.star.popup:RecentFileList?entry=";
      48             : static const char MENU_SHORTCUT[]     = "~N. ";
      49             : 
      50           0 : struct LoadRecentFile
      51             : {
      52             :     util::URL                               aTargetURL;
      53             :     uno::Sequence< beans::PropertyValue >   aArgSeq;
      54             :     uno::Reference< frame::XDispatch >      xDispatch;
      55             : };
      56             : 
      57             : class RecentFilesMenuController :  public svt::PopupMenuControllerBase
      58             : {
      59             :     using svt::PopupMenuControllerBase::disposing;
      60             : 
      61             : public:
      62             :     RecentFilesMenuController( const uno::Reference< uno::XComponentContext >& xContext );
      63             :     virtual ~RecentFilesMenuController();
      64             : 
      65             :     // XServiceInfo
      66           0 :     virtual OUString SAL_CALL getImplementationName()
      67             :         throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
      68             :     {
      69           0 :         return OUString("com.sun.star.comp.framework.RecentFilesMenuController");
      70             :     }
      71             : 
      72           0 :     virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName)
      73             :         throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
      74             :     {
      75           0 :         return cppu::supportsService(this, ServiceName);
      76             :     }
      77             : 
      78           0 :     virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames()
      79             :         throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
      80             :     {
      81           0 :         css::uno::Sequence< OUString > aSeq(1);
      82           0 :         aSeq[0] = OUString("com.sun.star.frame.PopupMenuController");
      83           0 :         return aSeq;
      84             :     }
      85             : 
      86             :     // XStatusListener
      87             :     virtual void SAL_CALL statusChanged( const frame::FeatureStateEvent& Event ) throw ( uno::RuntimeException, std::exception ) SAL_OVERRIDE;
      88             : 
      89             :     // XMenuListener
      90             :     virtual void SAL_CALL itemSelected( const awt::MenuEvent& rEvent ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE;
      91             :     virtual void SAL_CALL itemActivated( const awt::MenuEvent& rEvent ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE;
      92             : 
      93             :     // XDispatchProvider
      94             :     virtual uno::Reference< frame::XDispatch > SAL_CALL queryDispatch( const util::URL& aURL, const OUString& sTarget, sal_Int32 nFlags ) throw( uno::RuntimeException, std::exception ) SAL_OVERRIDE;
      95             : 
      96             :     // XDispatch
      97             :     virtual void SAL_CALL dispatch( const util::URL& aURL, const uno::Sequence< beans::PropertyValue >& seqProperties ) throw( uno::RuntimeException, std::exception ) SAL_OVERRIDE;
      98             : 
      99             :     // XEventListener
     100             :     virtual void SAL_CALL disposing( const com::sun::star::lang::EventObject& Source ) throw ( uno::RuntimeException, std::exception ) SAL_OVERRIDE;
     101             : 
     102             :     DECL_STATIC_LINK( RecentFilesMenuController, ExecuteHdl_Impl, LoadRecentFile* );
     103             : 
     104             : private:
     105             :     virtual void impl_setPopupMenu() SAL_OVERRIDE;
     106           0 :     struct RecentFile
     107             :     {
     108             :         OUString aURL;
     109             :         OUString aTitle;
     110             :     };
     111             : 
     112             :     void fillPopupMenu( com::sun::star::uno::Reference< com::sun::star::awt::XPopupMenu >& rPopupMenu );
     113             :     void executeEntry( sal_Int32 nIndex );
     114             : 
     115             :     std::vector< RecentFile > m_aRecentFilesItems;
     116             :     bool                  m_bDisabled : 1;
     117             : };
     118             : 
     119           0 : RecentFilesMenuController::RecentFilesMenuController( const uno::Reference< uno::XComponentContext >& xContext ) :
     120             :     svt::PopupMenuControllerBase( xContext ),
     121           0 :     m_bDisabled( false )
     122             : {
     123           0 : }
     124             : 
     125           0 : RecentFilesMenuController::~RecentFilesMenuController()
     126             : {
     127           0 : }
     128             : 
     129             : // private function
     130           0 : void RecentFilesMenuController::fillPopupMenu( Reference< css::awt::XPopupMenu >& rPopupMenu )
     131             : {
     132           0 :     VCLXPopupMenu* pPopupMenu    = static_cast<VCLXPopupMenu *>(VCLXMenu::GetImplementation( rPopupMenu ));
     133           0 :     PopupMenu*     pVCLPopupMenu = 0;
     134             : 
     135           0 :     SolarMutexGuard aSolarMutexGuard;
     136             : 
     137           0 :     resetPopupMenu( rPopupMenu );
     138           0 :     if ( pPopupMenu )
     139           0 :         pVCLPopupMenu = static_cast<PopupMenu *>(pPopupMenu->GetMenu());
     140             : 
     141           0 :     if ( pVCLPopupMenu )
     142             :     {
     143           0 :         Sequence< Sequence< PropertyValue > > aHistoryList = SvtHistoryOptions().GetList( ePICKLIST );
     144             : 
     145           0 :         int nPickListMenuItems = ( aHistoryList.getLength() > MAX_MENU_ITEMS ) ? MAX_MENU_ITEMS : aHistoryList.getLength();
     146           0 :         m_aRecentFilesItems.clear();
     147           0 :         if (( nPickListMenuItems > 0 ) && !m_bDisabled )
     148             :         {
     149           0 :             for ( int i = 0; i < nPickListMenuItems; i++ )
     150             :             {
     151           0 :                 Sequence< PropertyValue >& rPickListEntry = aHistoryList[i];
     152           0 :                 RecentFile aRecentFile;
     153             : 
     154           0 :                 for ( int j = 0; j < rPickListEntry.getLength(); j++ )
     155             :                 {
     156           0 :                     Any a = rPickListEntry[j].Value;
     157             : 
     158           0 :                     if ( rPickListEntry[j].Name == HISTORY_PROPERTYNAME_URL )
     159           0 :                         a >>= aRecentFile.aURL;
     160           0 :                     else if ( rPickListEntry[j].Name == HISTORY_PROPERTYNAME_TITLE )
     161           0 :                         a >>= aRecentFile.aTitle;
     162           0 :                 }
     163             : 
     164           0 :                 m_aRecentFilesItems.push_back( aRecentFile );
     165           0 :             }
     166             :         }
     167             : 
     168           0 :         if ( !m_aRecentFilesItems.empty() )
     169             :         {
     170           0 :             const sal_uInt32 nCount = m_aRecentFilesItems.size();
     171           0 :             for ( sal_uInt32 i = 0; i < nCount; i++ )
     172             :             {
     173             : 
     174           0 :                 OUStringBuffer aMenuShortCut;
     175           0 :                 if ( i <= 9 )
     176             :                 {
     177           0 :                     if ( i == 9 )
     178           0 :                         aMenuShortCut.append( "1~0. " );
     179             :                     else
     180             :                     {
     181           0 :                         aMenuShortCut.append( MENU_SHORTCUT );
     182           0 :                         aMenuShortCut[ 1 ] = sal_Unicode( i + '1' );
     183             :                     }
     184             :                 }
     185             :                 else
     186             :                 {
     187           0 :                     aMenuShortCut.append( sal_Int32( i + 1 ) );
     188           0 :                     aMenuShortCut.append( ". " );
     189             :                 }
     190             : 
     191           0 :                 OUStringBuffer aStrBuffer;
     192           0 :                 aStrBuffer.append( CMD_PREFIX );
     193           0 :                 aStrBuffer.append( sal_Int32( i ) );
     194           0 :                 OUString  aURLString( aStrBuffer.makeStringAndClear() );
     195             : 
     196             :                 // Abbreviate URL
     197           0 :                 OUString   aMenuTitle;
     198           0 :                 INetURLObject   aURL( m_aRecentFilesItems[i].aURL );
     199           0 :                 OUString aTipHelpText( aURL.getFSysPath( INetURLObject::FSYS_DETECT ) );
     200             : 
     201           0 :                 if ( aURL.GetProtocol() == INET_PROT_FILE )
     202             :                 {
     203             :                     // Do handle file URL differently: don't show the protocol, just the file name
     204           0 :                     aMenuTitle = aURL.GetLastName(INetURLObject::DECODE_WITH_CHARSET, RTL_TEXTENCODING_UTF8);
     205             :                 }
     206             :                 else
     207             :                 {
     208             :                     // In all other URLs show the protocol name before the file name
     209           0 :                     aMenuTitle   = INetURLObject::GetSchemeName(aURL.GetProtocol()) + ": " + aURL.getName();
     210             :                 }
     211             : 
     212           0 :                 aMenuShortCut.append( aMenuTitle );
     213             : 
     214           0 :                 pVCLPopupMenu->InsertItem( sal_uInt16( i+1 ), aMenuShortCut.makeStringAndClear() );
     215           0 :                 pVCLPopupMenu->SetTipHelpText( sal_uInt16( i+1 ), aTipHelpText );
     216           0 :                 pVCLPopupMenu->SetItemCommand( sal_uInt16( i+1 ), aURLString );
     217           0 :             }
     218             : 
     219           0 :             pVCLPopupMenu->InsertSeparator();
     220             :             // Clear List menu entry
     221             :             pVCLPopupMenu->InsertItem( sal_uInt16( nCount + 1 ),
     222           0 :                                        FWK_RESSTR(STR_CLEAR_RECENT_FILES) );
     223             :             pVCLPopupMenu->SetItemCommand( sal_uInt16( nCount + 1 ),
     224           0 :                                            OUString( CMD_CLEAR_LIST ) );
     225             :             pVCLPopupMenu->SetHelpText( sal_uInt16( nCount + 1 ),
     226           0 :                                         FWK_RESSTR(STR_CLEAR_RECENT_FILES_HELP) );
     227             :         }
     228             :         else
     229             :         {
     230             :             // No recent documents => insert "no document" string
     231           0 :             pVCLPopupMenu->InsertItem( 1, FWK_RESSTR(STR_NODOCUMENT) );
     232             :             // Do not disable it, otherwise the Toolbar controller and MenuButton
     233             :             // will display SV_RESID_STRING_NOSELECTIONPOSSIBLE instead of STR_NODOCUMENT
     234           0 :             pVCLPopupMenu->SetItemBits( 1, pVCLPopupMenu->GetItemBits( 1 ) | MenuItemBits::NOSELECT );
     235           0 :         }
     236           0 :     }
     237           0 : }
     238             : 
     239           0 : void RecentFilesMenuController::executeEntry( sal_Int32 nIndex )
     240             : {
     241           0 :     Reference< XDispatch >            xDispatch;
     242           0 :     Reference< XDispatchProvider >    xDispatchProvider;
     243           0 :     css::util::URL                    aTargetURL;
     244           0 :     Sequence< PropertyValue >         aArgsList;
     245             : 
     246           0 :     osl::ClearableMutexGuard aLock( m_aMutex );
     247           0 :     xDispatchProvider = Reference< XDispatchProvider >( m_xFrame, UNO_QUERY );
     248           0 :     aLock.clear();
     249             : 
     250           0 :     if (( nIndex >= 0 ) &&
     251           0 :         ( nIndex < sal::static_int_cast<sal_Int32>( m_aRecentFilesItems.size() )))
     252             :     {
     253           0 :         const RecentFile& rRecentFile = m_aRecentFilesItems[ nIndex ];
     254             : 
     255           0 :         aTargetURL.Complete = rRecentFile.aURL;
     256           0 :         m_xURLTransformer->parseStrict( aTargetURL );
     257             : 
     258           0 :         sal_Int32 nSize = 2;
     259           0 :         aArgsList.realloc( nSize );
     260           0 :         aArgsList[0].Name = "Referer";
     261           0 :         aArgsList[0].Value = makeAny( OUString( "private:user" ) );
     262             : 
     263             :         // documents in the picklist will never be opened as templates
     264           0 :         aArgsList[1].Name = "AsTemplate";
     265           0 :         aArgsList[1].Value = makeAny( sal_False );
     266             : 
     267           0 :         if (!m_aModuleName.isEmpty())
     268             :         {
     269             :             // Type detection needs to know which app we are opening it from.
     270           0 :             aArgsList.realloc(++nSize);
     271           0 :             aArgsList[nSize-1].Name = "DocumentService";
     272           0 :             aArgsList[nSize-1].Value <<= m_aModuleName;
     273             :         }
     274             : 
     275           0 :         xDispatch = xDispatchProvider->queryDispatch( aTargetURL, "_default", 0 );
     276             :     }
     277             : 
     278           0 :     if ( xDispatch.is() )
     279             :     {
     280             :         // Call dispatch asychronously as we can be destroyed while dispatch is
     281             :         // executed. VCL is not able to survive this as it wants to call listeners
     282             :         // after select!!!
     283           0 :         LoadRecentFile* pLoadRecentFile = new LoadRecentFile;
     284           0 :         pLoadRecentFile->xDispatch  = xDispatch;
     285           0 :         pLoadRecentFile->aTargetURL = aTargetURL;
     286           0 :         pLoadRecentFile->aArgSeq    = aArgsList;
     287             : 
     288           0 :         Application::PostUserEvent( STATIC_LINK(0, RecentFilesMenuController, ExecuteHdl_Impl), pLoadRecentFile );
     289           0 :     }
     290           0 : }
     291             : 
     292             : // XEventListener
     293           0 : void SAL_CALL RecentFilesMenuController::disposing( const EventObject& ) throw ( RuntimeException, std::exception )
     294             : {
     295           0 :     Reference< css::awt::XMenuListener > xHolder(( OWeakObject *)this, UNO_QUERY );
     296             : 
     297           0 :     osl::MutexGuard aLock( m_aMutex );
     298           0 :     m_xFrame.clear();
     299           0 :     m_xDispatch.clear();
     300             : 
     301           0 :     if ( m_xPopupMenu.is() )
     302           0 :         m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(( OWeakObject *)this, UNO_QUERY ));
     303           0 :     m_xPopupMenu.clear();
     304           0 : }
     305             : 
     306             : // XStatusListener
     307           0 : void SAL_CALL RecentFilesMenuController::statusChanged( const FeatureStateEvent& Event ) throw ( RuntimeException, std::exception )
     308             : {
     309           0 :     osl::MutexGuard aLock( m_aMutex );
     310           0 :     m_bDisabled = !Event.IsEnabled;
     311           0 : }
     312             : 
     313           0 : void SAL_CALL RecentFilesMenuController::itemSelected( const css::awt::MenuEvent& rEvent ) throw (RuntimeException, std::exception)
     314             : {
     315           0 :     Reference< css::awt::XPopupMenu > xPopupMenu;
     316             : 
     317           0 :     osl::ClearableMutexGuard aLock( m_aMutex );
     318           0 :     xPopupMenu = m_xPopupMenu;
     319           0 :     aLock.clear();
     320             : 
     321           0 :     if ( xPopupMenu.is() )
     322             :     {
     323           0 :         const OUString aCommand( xPopupMenu->getCommand( rEvent.MenuId ) );
     324             :         OSL_TRACE( "RecentFilesMenuController::itemSelected() - Command : %s",
     325             :                    OUStringToOString( aCommand, RTL_TEXTENCODING_UTF8 ).getStr() );
     326             : 
     327           0 :         if ( aCommand == CMD_CLEAR_LIST )
     328             :         {
     329           0 :             SvtHistoryOptions().Clear( ePICKLIST );
     330             :             dispatchCommand(
     331             :                 "vnd.org.libreoffice.recentdocs:ClearRecentFileList",
     332           0 :                 ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >() );
     333             :         }
     334             :         else
     335           0 :             executeEntry( rEvent.MenuId-1 );
     336           0 :     }
     337           0 : }
     338             : 
     339           0 : void SAL_CALL RecentFilesMenuController::itemActivated( const css::awt::MenuEvent& ) throw (RuntimeException, std::exception)
     340             : {
     341           0 :     osl::MutexGuard aLock( m_aMutex );
     342           0 :     impl_setPopupMenu();
     343           0 : }
     344             : 
     345             : // XPopupMenuController
     346           0 : void RecentFilesMenuController::impl_setPopupMenu()
     347             : {
     348           0 :     if ( m_xPopupMenu.is() )
     349           0 :         fillPopupMenu( m_xPopupMenu );
     350           0 : }
     351             : 
     352             : // XDispatchProvider
     353           0 : Reference< XDispatch > SAL_CALL RecentFilesMenuController::queryDispatch(
     354             :     const URL& aURL,
     355             :     const OUString& /*sTarget*/,
     356             :     sal_Int32 /*nFlags*/ )
     357             : throw( RuntimeException, std::exception )
     358             : {
     359           0 :     osl::MutexGuard aLock( m_aMutex );
     360             : 
     361           0 :     throwIfDisposed();
     362             : 
     363           0 :     if ( aURL.Complete.startsWith( m_aBaseURL ) )
     364           0 :         return Reference< XDispatch >( static_cast< OWeakObject* >( this ), UNO_QUERY );
     365             :     else
     366           0 :         return Reference< XDispatch >();
     367             : }
     368             : 
     369             : // XDispatch
     370           0 : void SAL_CALL RecentFilesMenuController::dispatch(
     371             :     const URL& aURL,
     372             :     const Sequence< PropertyValue >& /*seqProperties*/ )
     373             : throw( RuntimeException, std::exception )
     374             : {
     375           0 :     osl::MutexGuard aLock( m_aMutex );
     376             : 
     377           0 :     throwIfDisposed();
     378             : 
     379           0 :     if ( aURL.Complete.startsWith( m_aBaseURL ) )
     380             :     {
     381             :         // Parse URL to retrieve entry argument and its value
     382           0 :         sal_Int32 nQueryPart = aURL.Complete.indexOf( '?', m_aBaseURL.getLength() );
     383           0 :         if ( nQueryPart > 0 )
     384             :         {
     385           0 :             const OUString aEntryArgStr( "entry=" );
     386           0 :             sal_Int32 nEntryArg = aURL.Complete.indexOf( aEntryArgStr, nQueryPart );
     387           0 :             sal_Int32 nEntryPos = nEntryArg + aEntryArgStr.getLength();
     388           0 :             if (( nEntryArg > 0 ) && ( nEntryPos < aURL.Complete.getLength() ))
     389             :             {
     390           0 :                 sal_Int32 nAddArgs = aURL.Complete.indexOf( '&', nEntryPos );
     391           0 :                 OUString aEntryArg;
     392             : 
     393           0 :                 if ( nAddArgs < 0 )
     394           0 :                     aEntryArg = aURL.Complete.copy( nEntryPos );
     395             :                 else
     396           0 :                     aEntryArg = aURL.Complete.copy( nEntryPos, nAddArgs-nEntryPos );
     397             : 
     398           0 :                 sal_Int32 nEntry = aEntryArg.toInt32();
     399           0 :                 executeEntry( nEntry );
     400           0 :             }
     401             :         }
     402           0 :     }
     403           0 : }
     404             : 
     405           0 : IMPL_STATIC_LINK_NOINSTANCE( RecentFilesMenuController, ExecuteHdl_Impl, LoadRecentFile*, pLoadRecentFile )
     406             : {
     407             :     try
     408             :     {
     409             :         // Asynchronous execution as this can lead to our own destruction!
     410             :         // Framework can recycle our current frame and the layout manager disposes all user interface
     411             :         // elements if a component gets detached from its frame!
     412           0 :         pLoadRecentFile->xDispatch->dispatch( pLoadRecentFile->aTargetURL, pLoadRecentFile->aArgSeq );
     413             :     }
     414           0 :     catch ( const Exception& )
     415             :     {
     416             :     }
     417             : 
     418           0 :     delete pLoadRecentFile;
     419           0 :     return 0;
     420             : }
     421             : 
     422             : }
     423             : 
     424             : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
     425           0 : com_sun_star_comp_framework_RecentFilesMenuController_get_implementation(
     426             :     css::uno::XComponentContext *context,
     427             :     css::uno::Sequence<css::uno::Any> const &)
     428             : {
     429           0 :     return cppu::acquire(new RecentFilesMenuController(context));
     430         951 : }
     431             : 
     432             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10