xref: /AOO41X/main/framework/source/uielement/recentfilesmenucontroller.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_framework.hxx"
30*cdf0e10cSrcweir #include <uielement/recentfilesmenucontroller.hxx>
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
33*cdf0e10cSrcweir //	my own includes
34*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
35*cdf0e10cSrcweir #include <threadhelp/resetableguard.hxx>
36*cdf0e10cSrcweir #include "services.h"
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir #ifndef __FRAMEWORK_CLASSES_RESOURCE_HRC_
39*cdf0e10cSrcweir #include <classes/resource.hrc>
40*cdf0e10cSrcweir #endif
41*cdf0e10cSrcweir #include <classes/fwkresid.hxx>
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
44*cdf0e10cSrcweir //	interface includes
45*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
46*cdf0e10cSrcweir #include <com/sun/star/awt/XDevice.hpp>
47*cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
48*cdf0e10cSrcweir #include <com/sun/star/awt/MenuItemStyle.hpp>
49*cdf0e10cSrcweir #include <com/sun/star/util/XStringWidth.hpp>
50*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
51*cdf0e10cSrcweir //	includes of other projects
52*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir #ifndef _VCL_MENU_HXX_
55*cdf0e10cSrcweir #include <vcl/menu.hxx>
56*cdf0e10cSrcweir #endif
57*cdf0e10cSrcweir #include <vcl/svapp.hxx>
58*cdf0e10cSrcweir #include <vcl/i18nhelp.hxx>
59*cdf0e10cSrcweir #include <tools/urlobj.hxx>
60*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
61*cdf0e10cSrcweir #include <unotools/historyoptions.hxx>
62*cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
63*cdf0e10cSrcweir #include <osl/file.hxx>
64*cdf0e10cSrcweir //#include <tools/solar.hrc>
65*cdf0e10cSrcweir #include <dispatch/uieventloghelper.hxx>
66*cdf0e10cSrcweir #include <vos/mutex.hxx>
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
69*cdf0e10cSrcweir //	Defines
70*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
71*cdf0e10cSrcweir //
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir using namespace com::sun::star::uno;
74*cdf0e10cSrcweir using namespace com::sun::star::lang;
75*cdf0e10cSrcweir using namespace com::sun::star::frame;
76*cdf0e10cSrcweir using namespace com::sun::star::beans;
77*cdf0e10cSrcweir using namespace com::sun::star::util;
78*cdf0e10cSrcweir using namespace com::sun::star::container;
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir static const char SFX_REFERER_USER[] = "private:user";
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir namespace framework
83*cdf0e10cSrcweir {
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir class RecentFilesStringLength : public ::cppu::WeakImplHelper1< ::com::sun::star::util::XStringWidth >
86*cdf0e10cSrcweir {
87*cdf0e10cSrcweir 	public:
88*cdf0e10cSrcweir 		RecentFilesStringLength() {}
89*cdf0e10cSrcweir 		virtual ~RecentFilesStringLength() {}
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir 		// XStringWidth
92*cdf0e10cSrcweir 		sal_Int32 SAL_CALL queryStringWidth( const ::rtl::OUString& aString )
93*cdf0e10cSrcweir 			throw (::com::sun::star::uno::RuntimeException)
94*cdf0e10cSrcweir 		{
95*cdf0e10cSrcweir 			return aString.getLength();
96*cdf0e10cSrcweir 		}
97*cdf0e10cSrcweir };
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir DEFINE_XSERVICEINFO_MULTISERVICE        (   RecentFilesMenuController                   ,
100*cdf0e10cSrcweir                                             OWeakObject                                 ,
101*cdf0e10cSrcweir                                             SERVICENAME_POPUPMENUCONTROLLER		        ,
102*cdf0e10cSrcweir 											IMPLEMENTATIONNAME_RECENTFILESMENUCONTROLLER
103*cdf0e10cSrcweir 										)
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir DEFINE_INIT_SERVICE                     (   RecentFilesMenuController, {} )
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir RecentFilesMenuController::RecentFilesMenuController( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceManager ) :
108*cdf0e10cSrcweir 	svt::PopupMenuControllerBase( xServiceManager ),
109*cdf0e10cSrcweir     m_bDisabled( sal_False )
110*cdf0e10cSrcweir {
111*cdf0e10cSrcweir }
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir RecentFilesMenuController::~RecentFilesMenuController()
114*cdf0e10cSrcweir {
115*cdf0e10cSrcweir }
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir // private function
118*cdf0e10cSrcweir void RecentFilesMenuController::fillPopupMenu( Reference< css::awt::XPopupMenu >& rPopupMenu )
119*cdf0e10cSrcweir {
120*cdf0e10cSrcweir     VCLXPopupMenu*                                     pPopupMenu        = (VCLXPopupMenu *)VCLXMenu::GetImplementation( rPopupMenu );
121*cdf0e10cSrcweir     PopupMenu*                                         pVCLPopupMenu     = 0;
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir     vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir     resetPopupMenu( rPopupMenu );
126*cdf0e10cSrcweir     if ( pPopupMenu )
127*cdf0e10cSrcweir         pVCLPopupMenu = (PopupMenu *)pPopupMenu->GetMenu();
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir     if ( pVCLPopupMenu )
130*cdf0e10cSrcweir     {
131*cdf0e10cSrcweir 	    Sequence< Sequence< PropertyValue > > aHistoryList = SvtHistoryOptions().GetList( ePICKLIST );
132*cdf0e10cSrcweir 	    Reference< XStringWidth > xStringLength( new RecentFilesStringLength );
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir 	    int nPickListMenuItems = ( aHistoryList.getLength() > 99 ) ? 99 : aHistoryList.getLength();
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir         // New vnd.sun.star.popup: command URL to support direct dispatches
137*cdf0e10cSrcweir         const rtl::OUString aCmdPrefix( RTL_CONSTASCII_USTRINGPARAM( "vnd.sun.star.popup:RecentFileList?entry=" ));
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir         m_aRecentFilesItems.clear();
140*cdf0e10cSrcweir         if (( nPickListMenuItems > 0 ) && !m_bDisabled )
141*cdf0e10cSrcweir         {
142*cdf0e10cSrcweir             for ( int i = 0; i < nPickListMenuItems; i++ )
143*cdf0e10cSrcweir 	        {
144*cdf0e10cSrcweir 		        Sequence< PropertyValue >& rPickListEntry = aHistoryList[i];
145*cdf0e10cSrcweir 		        RecentFile aRecentFile;
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir 		        for ( int j = 0; j < rPickListEntry.getLength(); j++ )
148*cdf0e10cSrcweir 		        {
149*cdf0e10cSrcweir 			        Any a = rPickListEntry[j].Value;
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir 			        if ( rPickListEntry[j].Name == HISTORY_PROPERTYNAME_URL )
152*cdf0e10cSrcweir 				        a >>= aRecentFile.aURL;
153*cdf0e10cSrcweir 			        else if ( rPickListEntry[j].Name == HISTORY_PROPERTYNAME_FILTER )
154*cdf0e10cSrcweir 				        a >>= aRecentFile.aFilter;
155*cdf0e10cSrcweir 			        else if ( rPickListEntry[j].Name == HISTORY_PROPERTYNAME_TITLE )
156*cdf0e10cSrcweir 				        a >>= aRecentFile.aTitle;
157*cdf0e10cSrcweir 			        else if ( rPickListEntry[j].Name == HISTORY_PROPERTYNAME_PASSWORD )
158*cdf0e10cSrcweir 				        a >>= aRecentFile.aPassword;
159*cdf0e10cSrcweir 		        }
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir                 m_aRecentFilesItems.push_back( aRecentFile );
162*cdf0e10cSrcweir 	        }
163*cdf0e10cSrcweir         }
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir 	    if ( !m_aRecentFilesItems.empty() )
166*cdf0e10cSrcweir 	    {
167*cdf0e10cSrcweir 		    URL aTargetURL;
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir             const sal_uInt32 nCount = m_aRecentFilesItems.size();
170*cdf0e10cSrcweir             for ( sal_uInt32 i = 0; i < nCount; i++ )
171*cdf0e10cSrcweir 			{
172*cdf0e10cSrcweir 				char menuShortCut[5] = "~n: ";
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir 				::rtl::OUString aMenuShortCut;
175*cdf0e10cSrcweir 				if ( i <= 9 )
176*cdf0e10cSrcweir 				{
177*cdf0e10cSrcweir 					if ( i == 9 )
178*cdf0e10cSrcweir 						aMenuShortCut = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "1~0: " ));
179*cdf0e10cSrcweir 					else
180*cdf0e10cSrcweir 					{
181*cdf0e10cSrcweir 						menuShortCut[1] = (char)( '1' + i );
182*cdf0e10cSrcweir 						aMenuShortCut = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( menuShortCut ));
183*cdf0e10cSrcweir 					}
184*cdf0e10cSrcweir 				}
185*cdf0e10cSrcweir 				else
186*cdf0e10cSrcweir 				{
187*cdf0e10cSrcweir 					aMenuShortCut = rtl::OUString::valueOf((sal_Int32)( i + 1 ));
188*cdf0e10cSrcweir 					aMenuShortCut += rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ": " ));
189*cdf0e10cSrcweir 				}
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir 				// Abbreviate URL
192*cdf0e10cSrcweir                 rtl::OUString	aURLString( aCmdPrefix + rtl::OUString::valueOf( sal_Int32( i )));
193*cdf0e10cSrcweir 				rtl::OUString	aTipHelpText;
194*cdf0e10cSrcweir 				rtl::OUString	aMenuTitle;
195*cdf0e10cSrcweir 				INetURLObject	aURL( m_aRecentFilesItems[i].aURL );
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir 				if ( aURL.GetProtocol() == INET_PROT_FILE )
198*cdf0e10cSrcweir 				{
199*cdf0e10cSrcweir 					// Do handle file URL differently => convert it to a system
200*cdf0e10cSrcweir 					// path and abbreviate it with a special function:
201*cdf0e10cSrcweir 					String aFileSystemPath( aURL.getFSysPath( INetURLObject::FSYS_DETECT ) );
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir 					::rtl::OUString	aSystemPath( aFileSystemPath );
204*cdf0e10cSrcweir 					::rtl::OUString	aCompactedSystemPath;
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir 					aTipHelpText = aSystemPath;
207*cdf0e10cSrcweir 					oslFileError nError = osl_abbreviateSystemPath( aSystemPath.pData, &aCompactedSystemPath.pData, 46, NULL );
208*cdf0e10cSrcweir 					if ( !nError )
209*cdf0e10cSrcweir 						aMenuTitle = String( aCompactedSystemPath );
210*cdf0e10cSrcweir 					else
211*cdf0e10cSrcweir 						aMenuTitle = aSystemPath;
212*cdf0e10cSrcweir 				}
213*cdf0e10cSrcweir 				else
214*cdf0e10cSrcweir 				{
215*cdf0e10cSrcweir 					// Use INetURLObject to abbreviate all other URLs
216*cdf0e10cSrcweir 					String	aShortURL;
217*cdf0e10cSrcweir 					aShortURL = aURL.getAbbreviated( xStringLength, 46, INetURLObject::DECODE_UNAMBIGUOUS );
218*cdf0e10cSrcweir 					aMenuTitle += aShortURL;
219*cdf0e10cSrcweir 					aTipHelpText = aURLString;
220*cdf0e10cSrcweir 				}
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir 				::rtl::OUString aTitle( aMenuShortCut + aMenuTitle );
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir 				pVCLPopupMenu->InsertItem( sal_uInt16( i+1 ), aTitle );
225*cdf0e10cSrcweir 				pVCLPopupMenu->SetTipHelpText( sal_uInt16( i+1 ), aTipHelpText );
226*cdf0e10cSrcweir                 pVCLPopupMenu->SetItemCommand( sal_uInt16( i+1 ), aURLString );
227*cdf0e10cSrcweir 			}
228*cdf0e10cSrcweir 		}
229*cdf0e10cSrcweir         else
230*cdf0e10cSrcweir         {
231*cdf0e10cSrcweir             // No recent documents => insert "no document" string
232*cdf0e10cSrcweir             String aNoDocumentStr = String( FwkResId( STR_NODOCUMENT ));
233*cdf0e10cSrcweir             pVCLPopupMenu->InsertItem( 1, aNoDocumentStr );
234*cdf0e10cSrcweir             pVCLPopupMenu->EnableItem( 1, sal_False );
235*cdf0e10cSrcweir         }
236*cdf0e10cSrcweir 	}
237*cdf0e10cSrcweir }
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir void RecentFilesMenuController::executeEntry( sal_Int32 nIndex )
240*cdf0e10cSrcweir {
241*cdf0e10cSrcweir     static int NUM_OF_PICKLIST_ARGS = 3;
242*cdf0e10cSrcweir 
243*cdf0e10cSrcweir     Reference< css::awt::XPopupMenu > xPopupMenu;
244*cdf0e10cSrcweir     Reference< XDispatch >            xDispatch;
245*cdf0e10cSrcweir     Reference< XDispatchProvider >    xDispatchProvider;
246*cdf0e10cSrcweir     Reference< XMultiServiceFactory > xServiceManager;
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir     osl::ClearableMutexGuard aLock( m_aMutex );
249*cdf0e10cSrcweir     xPopupMenu          = m_xPopupMenu;
250*cdf0e10cSrcweir     xDispatchProvider   = Reference< XDispatchProvider >( m_xFrame, UNO_QUERY );
251*cdf0e10cSrcweir     xServiceManager     = m_xServiceManager;
252*cdf0e10cSrcweir     aLock.clear();
253*cdf0e10cSrcweir 
254*cdf0e10cSrcweir     css::util::URL            aTargetURL;
255*cdf0e10cSrcweir     Sequence< PropertyValue > aArgsList;
256*cdf0e10cSrcweir 
257*cdf0e10cSrcweir     if (( nIndex >= 0 ) &&
258*cdf0e10cSrcweir         ( nIndex < sal::static_int_cast<sal_Int32>( m_aRecentFilesItems.size() )))
259*cdf0e10cSrcweir     {
260*cdf0e10cSrcweir         const RecentFile& rRecentFile = m_aRecentFilesItems[ nIndex ];
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir         aTargetURL.Complete = rRecentFile.aURL;
263*cdf0e10cSrcweir         m_xURLTransformer->parseStrict( aTargetURL );
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir         aArgsList.realloc( NUM_OF_PICKLIST_ARGS );
266*cdf0e10cSrcweir         aArgsList[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Referer" ));
267*cdf0e10cSrcweir         aArgsList[0].Value = makeAny( ::rtl::OUString::createFromAscii( SFX_REFERER_USER ));
268*cdf0e10cSrcweir 
269*cdf0e10cSrcweir         // documents in the picklist will never be opened as templates
270*cdf0e10cSrcweir         aArgsList[1].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "AsTemplate" ));
271*cdf0e10cSrcweir         aArgsList[1].Value = makeAny( (sal_Bool) sal_False );
272*cdf0e10cSrcweir 
273*cdf0e10cSrcweir         ::rtl::OUString  aFilter( rRecentFile.aFilter );
274*cdf0e10cSrcweir         sal_Int32 nPos = aFilter.indexOf( '|' );
275*cdf0e10cSrcweir         if ( nPos >= 0 )
276*cdf0e10cSrcweir         {
277*cdf0e10cSrcweir 	        ::rtl::OUString aFilterOptions;
278*cdf0e10cSrcweir 
279*cdf0e10cSrcweir 	        if ( nPos < ( aFilter.getLength() - 1 ) )
280*cdf0e10cSrcweir 		        aFilterOptions = aFilter.copy( nPos+1 );
281*cdf0e10cSrcweir 
282*cdf0e10cSrcweir 	        aArgsList[2].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FilterOptions" ));
283*cdf0e10cSrcweir 	        aArgsList[2].Value <<= aFilterOptions;
284*cdf0e10cSrcweir 
285*cdf0e10cSrcweir 	        aFilter = aFilter.copy( 0, nPos-1 );
286*cdf0e10cSrcweir 	        aArgsList.realloc( ++NUM_OF_PICKLIST_ARGS );
287*cdf0e10cSrcweir         }
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir         aArgsList[NUM_OF_PICKLIST_ARGS-1].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FilterName" ));
290*cdf0e10cSrcweir         aArgsList[NUM_OF_PICKLIST_ARGS-1].Value <<= aFilter;
291*cdf0e10cSrcweir 
292*cdf0e10cSrcweir         xDispatch = xDispatchProvider->queryDispatch( aTargetURL, ::rtl::OUString::createFromAscii("_default"), 0 );
293*cdf0e10cSrcweir     }
294*cdf0e10cSrcweir 
295*cdf0e10cSrcweir     if ( xDispatch.is() )
296*cdf0e10cSrcweir     {
297*cdf0e10cSrcweir         // Call dispatch asychronously as we can be destroyed while dispatch is
298*cdf0e10cSrcweir         // executed. VCL is not able to survive this as it wants to call listeners
299*cdf0e10cSrcweir         // after select!!!
300*cdf0e10cSrcweir         LoadRecentFile* pLoadRecentFile = new LoadRecentFile;
301*cdf0e10cSrcweir         pLoadRecentFile->xDispatch  = xDispatch;
302*cdf0e10cSrcweir         pLoadRecentFile->aTargetURL = aTargetURL;
303*cdf0e10cSrcweir         pLoadRecentFile->aArgSeq    = aArgsList;
304*cdf0e10cSrcweir         if(::comphelper::UiEventsLogger::isEnabled()) //#i88653#
305*cdf0e10cSrcweir             UiEventLogHelper(::rtl::OUString::createFromAscii("RecentFilesMenuController")).log(m_xServiceManager, m_xFrame, aTargetURL, aArgsList);
306*cdf0e10cSrcweir         Application::PostUserEvent( STATIC_LINK(0, RecentFilesMenuController, ExecuteHdl_Impl), pLoadRecentFile );
307*cdf0e10cSrcweir     }
308*cdf0e10cSrcweir }
309*cdf0e10cSrcweir 
310*cdf0e10cSrcweir // XEventListener
311*cdf0e10cSrcweir void SAL_CALL RecentFilesMenuController::disposing( const EventObject& ) throw ( RuntimeException )
312*cdf0e10cSrcweir {
313*cdf0e10cSrcweir     Reference< css::awt::XMenuListener > xHolder(( OWeakObject *)this, UNO_QUERY );
314*cdf0e10cSrcweir 
315*cdf0e10cSrcweir     osl::MutexGuard aLock( m_aMutex );
316*cdf0e10cSrcweir     m_xFrame.clear();
317*cdf0e10cSrcweir     m_xDispatch.clear();
318*cdf0e10cSrcweir     m_xServiceManager.clear();
319*cdf0e10cSrcweir 
320*cdf0e10cSrcweir     if ( m_xPopupMenu.is() )
321*cdf0e10cSrcweir         m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(( OWeakObject *)this, UNO_QUERY ));
322*cdf0e10cSrcweir     m_xPopupMenu.clear();
323*cdf0e10cSrcweir }
324*cdf0e10cSrcweir 
325*cdf0e10cSrcweir // XStatusListener
326*cdf0e10cSrcweir void SAL_CALL RecentFilesMenuController::statusChanged( const FeatureStateEvent& Event ) throw ( RuntimeException )
327*cdf0e10cSrcweir {
328*cdf0e10cSrcweir     osl::MutexGuard aLock( m_aMutex );
329*cdf0e10cSrcweir     m_bDisabled = !Event.IsEnabled;
330*cdf0e10cSrcweir }
331*cdf0e10cSrcweir 
332*cdf0e10cSrcweir void SAL_CALL RecentFilesMenuController::select( const css::awt::MenuEvent& rEvent ) throw (RuntimeException)
333*cdf0e10cSrcweir {
334*cdf0e10cSrcweir     Reference< css::awt::XPopupMenu > xPopupMenu;
335*cdf0e10cSrcweir     Reference< XDispatch >            xDispatch;
336*cdf0e10cSrcweir     Reference< XDispatchProvider >    xDispatchProvider;
337*cdf0e10cSrcweir     Reference< XMultiServiceFactory > xServiceManager;
338*cdf0e10cSrcweir 
339*cdf0e10cSrcweir     osl::ClearableMutexGuard aLock( m_aMutex );
340*cdf0e10cSrcweir     xPopupMenu          = m_xPopupMenu;
341*cdf0e10cSrcweir     xDispatchProvider   = Reference< XDispatchProvider >( m_xFrame, UNO_QUERY );
342*cdf0e10cSrcweir     xServiceManager     = m_xServiceManager;
343*cdf0e10cSrcweir     aLock.clear();
344*cdf0e10cSrcweir 
345*cdf0e10cSrcweir     css::util::URL aTargetURL;
346*cdf0e10cSrcweir     Sequence< PropertyValue > aArgsList;
347*cdf0e10cSrcweir 
348*cdf0e10cSrcweir     if ( xPopupMenu.is() && xDispatchProvider.is() )
349*cdf0e10cSrcweir     {
350*cdf0e10cSrcweir         VCLXPopupMenu* pPopupMenu = (VCLXPopupMenu *)VCLXPopupMenu::GetImplementation( xPopupMenu );
351*cdf0e10cSrcweir         if ( pPopupMenu )
352*cdf0e10cSrcweir             executeEntry( rEvent.MenuId-1 );
353*cdf0e10cSrcweir     }
354*cdf0e10cSrcweir }
355*cdf0e10cSrcweir 
356*cdf0e10cSrcweir void SAL_CALL RecentFilesMenuController::activate( const css::awt::MenuEvent& ) throw (RuntimeException)
357*cdf0e10cSrcweir {
358*cdf0e10cSrcweir     osl::MutexGuard aLock( m_aMutex );
359*cdf0e10cSrcweir     impl_setPopupMenu();
360*cdf0e10cSrcweir }
361*cdf0e10cSrcweir 
362*cdf0e10cSrcweir // XPopupMenuController
363*cdf0e10cSrcweir void RecentFilesMenuController::impl_setPopupMenu()
364*cdf0e10cSrcweir {
365*cdf0e10cSrcweir     if ( m_xPopupMenu.is() )
366*cdf0e10cSrcweir         fillPopupMenu( m_xPopupMenu );
367*cdf0e10cSrcweir }
368*cdf0e10cSrcweir 
369*cdf0e10cSrcweir void SAL_CALL RecentFilesMenuController::updatePopupMenu() throw (RuntimeException)
370*cdf0e10cSrcweir {
371*cdf0e10cSrcweir     osl::ClearableMutexGuard aLock( m_aMutex );
372*cdf0e10cSrcweir 
373*cdf0e10cSrcweir     throwIfDisposed();
374*cdf0e10cSrcweir 
375*cdf0e10cSrcweir     Reference< XStatusListener > xStatusListener( static_cast< OWeakObject* >( this ), UNO_QUERY );
376*cdf0e10cSrcweir     Reference< XDispatch > xDispatch( m_xDispatch );
377*cdf0e10cSrcweir     com::sun::star::util::URL aTargetURL;
378*cdf0e10cSrcweir     aTargetURL.Complete = m_aCommandURL;
379*cdf0e10cSrcweir     m_xURLTransformer->parseStrict( aTargetURL );
380*cdf0e10cSrcweir     aLock.clear();
381*cdf0e10cSrcweir 
382*cdf0e10cSrcweir     // Add/remove status listener to get a status update once
383*cdf0e10cSrcweir     if ( xDispatch.is() )
384*cdf0e10cSrcweir     {
385*cdf0e10cSrcweir         xDispatch->addStatusListener( xStatusListener, aTargetURL );
386*cdf0e10cSrcweir         xDispatch->removeStatusListener( xStatusListener, aTargetURL );
387*cdf0e10cSrcweir     }
388*cdf0e10cSrcweir }
389*cdf0e10cSrcweir 
390*cdf0e10cSrcweir // XDispatchProvider
391*cdf0e10cSrcweir Reference< XDispatch > SAL_CALL RecentFilesMenuController::queryDispatch(
392*cdf0e10cSrcweir     const URL& aURL,
393*cdf0e10cSrcweir     const ::rtl::OUString& /*sTarget*/,
394*cdf0e10cSrcweir     sal_Int32 /*nFlags*/ )
395*cdf0e10cSrcweir throw( RuntimeException )
396*cdf0e10cSrcweir {
397*cdf0e10cSrcweir     osl::MutexGuard aLock( m_aMutex );
398*cdf0e10cSrcweir 
399*cdf0e10cSrcweir 	throwIfDisposed();
400*cdf0e10cSrcweir 
401*cdf0e10cSrcweir     if ( aURL.Complete.indexOf( m_aBaseURL ) == 0 )
402*cdf0e10cSrcweir         return Reference< XDispatch >( static_cast< OWeakObject* >( this ), UNO_QUERY );
403*cdf0e10cSrcweir     else
404*cdf0e10cSrcweir         return Reference< XDispatch >();
405*cdf0e10cSrcweir }
406*cdf0e10cSrcweir 
407*cdf0e10cSrcweir // XDispatch
408*cdf0e10cSrcweir void SAL_CALL RecentFilesMenuController::dispatch(
409*cdf0e10cSrcweir     const URL& aURL,
410*cdf0e10cSrcweir     const Sequence< PropertyValue >& /*seqProperties*/ )
411*cdf0e10cSrcweir throw( RuntimeException )
412*cdf0e10cSrcweir {
413*cdf0e10cSrcweir     osl::MutexGuard aLock( m_aMutex );
414*cdf0e10cSrcweir 
415*cdf0e10cSrcweir 	throwIfDisposed();
416*cdf0e10cSrcweir 
417*cdf0e10cSrcweir     if ( aURL.Complete.indexOf( m_aBaseURL ) == 0 )
418*cdf0e10cSrcweir     {
419*cdf0e10cSrcweir         // Parse URL to retrieve entry argument and its value
420*cdf0e10cSrcweir         sal_Int32 nQueryPart = aURL.Complete.indexOf( '?', m_aBaseURL.getLength() );
421*cdf0e10cSrcweir         if ( nQueryPart > 0 )
422*cdf0e10cSrcweir         {
423*cdf0e10cSrcweir             const rtl::OUString aEntryArgStr( RTL_CONSTASCII_USTRINGPARAM( "entry=" ));
424*cdf0e10cSrcweir             sal_Int32 nEntryArg = aURL.Complete.indexOf( aEntryArgStr, nQueryPart );
425*cdf0e10cSrcweir             sal_Int32 nEntryPos = nEntryArg + aEntryArgStr.getLength();
426*cdf0e10cSrcweir             if (( nEntryArg > 0 ) && ( nEntryPos < aURL.Complete.getLength() ))
427*cdf0e10cSrcweir             {
428*cdf0e10cSrcweir                 sal_Int32 nAddArgs = aURL.Complete.indexOf( '&', nEntryPos );
429*cdf0e10cSrcweir                 rtl::OUString aEntryArg;
430*cdf0e10cSrcweir 
431*cdf0e10cSrcweir                 if ( nAddArgs < 0 )
432*cdf0e10cSrcweir                     aEntryArg = aURL.Complete.copy( nEntryPos );
433*cdf0e10cSrcweir                 else
434*cdf0e10cSrcweir                     aEntryArg = aURL.Complete.copy( nEntryPos, nAddArgs-nEntryPos );
435*cdf0e10cSrcweir 
436*cdf0e10cSrcweir                 sal_Int32 nEntry = aEntryArg.toInt32();
437*cdf0e10cSrcweir                 executeEntry( nEntry );
438*cdf0e10cSrcweir             }
439*cdf0e10cSrcweir         }
440*cdf0e10cSrcweir     }
441*cdf0e10cSrcweir }
442*cdf0e10cSrcweir 
443*cdf0e10cSrcweir void SAL_CALL RecentFilesMenuController::addStatusListener(
444*cdf0e10cSrcweir     const Reference< XStatusListener >& xControl,
445*cdf0e10cSrcweir     const URL& aURL )
446*cdf0e10cSrcweir throw( RuntimeException )
447*cdf0e10cSrcweir {
448*cdf0e10cSrcweir     osl::MutexGuard aLock( m_aMutex );
449*cdf0e10cSrcweir 
450*cdf0e10cSrcweir 	throwIfDisposed();
451*cdf0e10cSrcweir 
452*cdf0e10cSrcweir 	svt::PopupMenuControllerBase::addStatusListener( xControl, aURL );
453*cdf0e10cSrcweir }
454*cdf0e10cSrcweir 
455*cdf0e10cSrcweir void SAL_CALL RecentFilesMenuController::removeStatusListener(
456*cdf0e10cSrcweir     const Reference< XStatusListener >& xControl,
457*cdf0e10cSrcweir     const URL& aURL )
458*cdf0e10cSrcweir throw( RuntimeException )
459*cdf0e10cSrcweir {
460*cdf0e10cSrcweir 	svt::PopupMenuControllerBase::removeStatusListener( xControl, aURL );
461*cdf0e10cSrcweir }
462*cdf0e10cSrcweir 
463*cdf0e10cSrcweir IMPL_STATIC_LINK_NOINSTANCE( RecentFilesMenuController, ExecuteHdl_Impl, LoadRecentFile*, pLoadRecentFile )
464*cdf0e10cSrcweir {
465*cdf0e10cSrcweir     try
466*cdf0e10cSrcweir     {
467*cdf0e10cSrcweir         // Asynchronous execution as this can lead to our own destruction!
468*cdf0e10cSrcweir         // Framework can recycle our current frame and the layout manager disposes all user interface
469*cdf0e10cSrcweir         // elements if a component gets detached from its frame!
470*cdf0e10cSrcweir         pLoadRecentFile->xDispatch->dispatch( pLoadRecentFile->aTargetURL, pLoadRecentFile->aArgSeq );
471*cdf0e10cSrcweir     }
472*cdf0e10cSrcweir     catch ( Exception& )
473*cdf0e10cSrcweir     {
474*cdf0e10cSrcweir     }
475*cdf0e10cSrcweir 
476*cdf0e10cSrcweir     delete pLoadRecentFile;
477*cdf0e10cSrcweir     return 0;
478*cdf0e10cSrcweir }
479*cdf0e10cSrcweir 
480*cdf0e10cSrcweir }
481