1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <uielement/newmenucontroller.hxx>
#include <menuconfiguration.hxx>

#include <services.h>

#include <com/sun/star/beans/PropertyValue.hpp>
#include <com/sun/star/ui/theModuleUIConfigurationManagerSupplier.hpp>
#include <com/sun/star/ui/XUIConfigurationManagerSupplier.hpp>
#include <com/sun/star/ui/GlobalAcceleratorConfiguration.hpp>
#include <com/sun/star/frame/ModuleManager.hpp>
#include <com/sun/star/frame/XFrame.hpp>
#include <com/sun/star/util/XURLTransformer.hpp>

#include <vcl/svapp.hxx>
#include <vcl/settings.hxx>
#include <vcl/commandinfoprovider.hxx>
#include <svtools/acceleratorexecute.hxx>
#include <svtools/imagemgr.hxx>
#include <toolkit/awt/vclxmenu.hxx>
#include <tools/urlobj.hxx>
#include <unotools/dynamicmenuoptions.hxx>
#include <unotools/moduleoptions.hxx>
#include <osl/mutex.hxx>

//  Defines
#define aSlotNewDocDirect ".uno:AddDirect"
#define aSlotAutoPilot ".uno:AutoPilotMenu"

using namespace com::sun::star::uno;
using namespace com::sun::star::lang;
using namespace com::sun::star::frame;
using namespace com::sun::star::beans;
using namespace com::sun::star::util;
using namespace com::sun::star::container;
using namespace com::sun::star::ui;

namespace framework
{

DEFINE_XSERVICEINFO_MULTISERVICE_2      (   NewMenuController                           ,<--- There is an unknown macro here somewhere. Configuration is required. If DEFINE_XSERVICEINFO_MULTISERVICE_2 is a macro then please configure it.
                                            OWeakObject                                 ,
                                            SERVICENAME_POPUPMENUCONTROLLER             ,
                                            IMPLEMENTATIONNAME_NEWMENUCONTROLLER
                                        )

DEFINE_INIT_SERVICE                     (   NewMenuController, {} )

void NewMenuController::setMenuImages( PopupMenu* pPopupMenu, bool bSetImages )
{
    sal_uInt16 nItemCount = pPopupMenu->GetItemCount();
    Reference< XFrame > xFrame( m_xFrame );

    for ( sal_uInt16 i = 0; i < nItemCount; i++ )
    {
        sal_uInt16 nItemId = pPopupMenu->GetItemId( i );
        if ( nItemId != 0 )
        {
            if ( bSetImages )
            {
                OUString aImageId;
                OUString aCmd( pPopupMenu->GetItemCommand( nItemId ) );
                void* nAttributePtr = pPopupMenu->GetUserValue( nItemId );
                MenuAttributes* pAttributes = static_cast<MenuAttributes *>(nAttributePtr);
                if (pAttributes)
                    aImageId = pAttributes->aImageId;

                INetURLObject aURLObj( aImageId.isEmpty() ? aCmd : aImageId );
                Image aImage = SvFileInformationManager::GetImageNoDefault( aURLObj );
                if ( !aImage )
                    aImage = vcl::CommandInfoProvider::GetImageForCommand(aCmd, xFrame);

                if ( !!aImage )
                    pPopupMenu->SetItemImage( nItemId, aImage );
            }
            else
                pPopupMenu->SetItemImage( nItemId, Image() );
        }
    }
}

void NewMenuController::determineAndSetNewDocAccel( PopupMenu* pPopupMenu, const vcl::KeyCode& rKeyCode )
{
    sal_uInt16        nCount( pPopupMenu->GetItemCount() );
    sal_uInt16        nId( 0 );
    bool      bFound( false );
    OUString aCommand;

    if ( !m_aEmptyDocURL.isEmpty() )
    {
        // Search for the empty document URL

        for ( sal_uInt16 i = 0; i < nCount; i++ )
        {
            if ( pPopupMenu->GetItemType( i ) != MenuItemType::SEPARATOR )
            {
                nId = pPopupMenu->GetItemId( i );
                aCommand = pPopupMenu->GetItemCommand( nId );
                if ( aCommand.startsWith( m_aEmptyDocURL ) )
                {
                    pPopupMenu->SetAccelKey( nId, rKeyCode );
                    bFound = true;
                    break;
                }
            }
        }
    }

    if ( bFound )
        return;

    // Search for the default module name
    OUString aDefaultModuleName( SvtModuleOptions().GetDefaultModuleName() );
    if ( aDefaultModuleName.isEmpty() )
        return;

    for ( sal_uInt16 i = 0; i < nCount; i++ )
    {
        if ( pPopupMenu->GetItemType( i ) != MenuItemType::SEPARATOR )
        {
            nId = pPopupMenu->GetItemId( i );
            aCommand = pPopupMenu->GetItemCommand( nId );
            if ( aCommand.indexOf( aDefaultModuleName ) >= 0 )
            {
                pPopupMenu->SetAccelKey( nId, rKeyCode );
                break;
            }
        }
    }
}

void NewMenuController::setAccelerators( PopupMenu* pPopupMenu )
{
    if ( !m_bModuleIdentified )
        return;

    Reference< XAcceleratorConfiguration > xDocAccelCfg( m_xDocAcceleratorManager );
    Reference< XAcceleratorConfiguration > xModuleAccelCfg( m_xModuleAcceleratorManager );
    Reference< XAcceleratorConfiguration > xGlobalAccelCfg( m_xGlobalAcceleratorManager );

    if ( !m_bAcceleratorCfg )
    {
        // Retrieve references on demand
        m_bAcceleratorCfg = true;
        if ( !xDocAccelCfg.is() )
        {
            Reference< XController > xController = m_xFrame->getController();
            Reference< XModel > xModel;
            if ( xController.is() )
            {
                xModel = xController->getModel();
                if ( xModel.is() )
                {
                    Reference< XUIConfigurationManagerSupplier > xSupplier( xModel, UNO_QUERY );
                    if ( xSupplier.is() )
                    {
                        Reference< XUIConfigurationManager > xDocUICfgMgr = xSupplier->getUIConfigurationManager();
                        if ( xDocUICfgMgr.is() )
                        {
                            xDocAccelCfg = xDocUICfgMgr->getShortCutManager();
                            m_xDocAcceleratorManager = xDocAccelCfg;
                        }
                    }
                }
            }
        }

        if ( !xModuleAccelCfg.is() )
        {
            Reference< XModuleUIConfigurationManagerSupplier > xModuleCfgMgrSupplier =
                theModuleUIConfigurationManagerSupplier::get( m_xContext );
            Reference< XUIConfigurationManager > xUICfgMgr = xModuleCfgMgrSupplier->getUIConfigurationManager( m_aModuleIdentifier );
            if ( xUICfgMgr.is() )
            {
                xModuleAccelCfg = xUICfgMgr->getShortCutManager();
                m_xModuleAcceleratorManager = xModuleAccelCfg;
            }
        }

        if ( !xGlobalAccelCfg.is() )
        {
            xGlobalAccelCfg = GlobalAcceleratorConfiguration::create( m_xContext );
            m_xGlobalAcceleratorManager = xGlobalAccelCfg;
        }
    }

    vcl::KeyCode                    aEmptyKeyCode;
    sal_uInt16                      nItemCount( pPopupMenu->GetItemCount() );
    std::vector< vcl::KeyCode >     aMenuShortCuts;
    std::vector< OUString >    aCmds;
    std::vector< sal_uInt16 >       aIds;
    for ( sal_uInt16 i = 0; i < nItemCount; i++ )
    {
        if ( pPopupMenu->GetItemType( i ) != MenuItemType::SEPARATOR )
        {
            sal_uInt16 nId( pPopupMenu->GetItemId( i ));
            aIds.push_back( nId );
            aMenuShortCuts.push_back( aEmptyKeyCode );
            aCmds.push_back( pPopupMenu->GetItemCommand( nId ));
        }
    }

    sal_uInt32 nSeqCount( aIds.size() );

    if ( m_bNewMenu )
        nSeqCount+=1;

    Sequence< OUString > aSeq( nSeqCount );

    // Add a special command for our "New" menu.
    if ( m_bNewMenu )
    {
        aSeq[nSeqCount-1] = m_aCommandURL;
        aMenuShortCuts.push_back( aEmptyKeyCode );
    }

    const sal_uInt32 nCount = aCmds.size();
    for ( sal_uInt32 i = 0; i < nCount; i++ )
        aSeq[i] = aCmds[i];

    if ( m_xGlobalAcceleratorManager.is() )
        retrieveShortcutsFromConfiguration( xGlobalAccelCfg, aSeq, aMenuShortCuts );
    if ( m_xModuleAcceleratorManager.is() )
        retrieveShortcutsFromConfiguration( xModuleAccelCfg, aSeq, aMenuShortCuts );
    if ( m_xDocAcceleratorManager.is() )
        retrieveShortcutsFromConfiguration( xDocAccelCfg, aSeq, aMenuShortCuts );

    const sal_uInt32 nCount2 = aIds.size();
    for ( sal_uInt32 i = 0; i < nCount2; i++ )
        pPopupMenu->SetAccelKey( aIds[i], aMenuShortCuts[i] );

    // Special handling for "New" menu short-cut should be set at the
    // document which will be opened using it.
    if ( m_bNewMenu )
    {
        if ( aMenuShortCuts[nSeqCount-1] != aEmptyKeyCode )
            determineAndSetNewDocAccel( pPopupMenu, aMenuShortCuts[nSeqCount-1] );
    }
}

void NewMenuController::retrieveShortcutsFromConfiguration(
    const Reference< XAcceleratorConfiguration >& rAccelCfg,
    const Sequence< OUString >& rCommands,
    std::vector< vcl::KeyCode >& aMenuShortCuts )
{
    if ( !rAccelCfg.is() )
        return;

    try
    {
        css::awt::KeyEvent aKeyEvent;
        Sequence< Any > aSeqKeyCode = rAccelCfg->getPreferredKeyEventsForCommandList( rCommands );
        for ( sal_Int32 i = 0; i < aSeqKeyCode.getLength(); i++ )
        {
            if ( aSeqKeyCode[i] >>= aKeyEvent )
                aMenuShortCuts[i] = svt::AcceleratorExecute::st_AWTKey2VCLKey( aKeyEvent );
        }
    }
    catch ( const IllegalArgumentException& )
    {
    }
}

NewMenuController::NewMenuController( const css::uno::Reference< css::uno::XComponentContext >& xContext ) :
    svt::PopupMenuControllerBase( xContext ),
    m_bShowImages( true ),
    m_bNewMenu( false ),
    m_bModuleIdentified( false ),
    m_bAcceleratorCfg( false ),
    m_aTargetFrame( "_default" ),
    m_xContext( xContext )
{
}

NewMenuController::~NewMenuController()
{
}

// private function
void NewMenuController::fillPopupMenu( Reference< css::awt::XPopupMenu > const & rPopupMenu )
{
    VCLXPopupMenu* pPopupMenu    = static_cast<VCLXPopupMenu *>(comphelper::getUnoTunnelImplementation<VCLXMenu>( rPopupMenu ));
    PopupMenu*     pVCLPopupMenu = nullptr;

    SolarMutexGuard aSolarMutexGuard;

    resetPopupMenu( rPopupMenu );
    if ( pPopupMenu )
        pVCLPopupMenu = static_cast<PopupMenu *>(pPopupMenu->GetMenu());

    if ( !pVCLPopupMenu )
        return;

    Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
    URL aTargetURL;
    aTargetURL.Complete = OUString::createFromAscii(m_bNewMenu ? aSlotNewDocDirect : aSlotAutoPilot);
    m_xURLTransformer->parseStrict( aTargetURL );
    Reference< XDispatch > xMenuItemDispatch = xDispatchProvider->queryDispatch( aTargetURL, OUString(), 0 );
    if(xMenuItemDispatch == nullptr)
        return;

    const css::uno::Sequence< css::uno::Sequence< css::beans::PropertyValue > > aDynamicMenuEntries =
        SvtDynamicMenuOptions().GetMenu( m_bNewMenu ? EDynamicMenuType::NewMenu : EDynamicMenuType::WizardMenu );

    OUString aTitle;
    OUString aURL;
    OUString aTargetFrame;
    OUString aImageId;
    sal_uInt16 nItemId = 1;

    for ( const auto& aDynamicMenuEntry : aDynamicMenuEntries )
    {
        for ( const auto& aProperty : aDynamicMenuEntry )
        {
            if ( aProperty.Name == DYNAMICMENU_PROPERTYNAME_URL )
                aProperty.Value >>= aURL;
            else if ( aProperty.Name == DYNAMICMENU_PROPERTYNAME_TITLE )
                aProperty.Value >>= aTitle;
            else if ( aProperty.Name == DYNAMICMENU_PROPERTYNAME_IMAGEIDENTIFIER )
                aProperty.Value >>= aImageId;
            else if ( aProperty.Name == DYNAMICMENU_PROPERTYNAME_TARGETNAME )
                aProperty.Value >>= aTargetFrame;
        }

        if ( aTitle.isEmpty() && aURL.isEmpty() )
            continue;

        if ( aURL == "private:separator" )
            pVCLPopupMenu->InsertSeparator();
        else
        {
            pVCLPopupMenu->InsertItem( nItemId, aTitle );
            pVCLPopupMenu->SetItemCommand( nItemId, aURL );

            void* nAttributePtr = MenuAttributes::CreateAttribute( aTargetFrame, aImageId );
            pVCLPopupMenu->SetUserValue( nItemId, nAttributePtr, MenuAttributes::ReleaseAttribute );

            nItemId++;
        }
    }

    if ( m_bShowImages )
        setMenuImages( pVCLPopupMenu, m_bShowImages );
}

// XEventListener
void SAL_CALL NewMenuController::disposing( const EventObject& )
{
    Reference< css::awt::XMenuListener > xHolder(static_cast<OWeakObject *>(this), UNO_QUERY );

    osl::MutexGuard aLock( m_aMutex );
    m_xFrame.clear();
    m_xDispatch.clear();
    m_xContext.clear();

    if ( m_xPopupMenu.is() )
        m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(static_cast<OWeakObject *>(this), UNO_QUERY ));
    m_xPopupMenu.clear();
}

// XStatusListener
void SAL_CALL NewMenuController::statusChanged( const FeatureStateEvent& )
{
}

// XMenuListener
void SAL_CALL NewMenuController::itemSelected( const css::awt::MenuEvent& rEvent )
{
    Reference< css::awt::XPopupMenu > xPopupMenu;
    Reference< XComponentContext >    xContext;

    {
        osl::MutexGuard aLock(m_aMutex);
        xPopupMenu = m_xPopupMenu;
        xContext = m_xContext;
    }

    if ( !xPopupMenu.is() )
        return;

    VCLXPopupMenu* pPopupMenu = static_cast<VCLXPopupMenu *>(comphelper::getUnoTunnelImplementation<VCLXMenu>( xPopupMenu ));
    if ( !pPopupMenu )
        return;

    OUString aURL;
    OUString aTargetFrame( m_aTargetFrame );

    {
        SolarMutexGuard aSolarMutexGuard;
        PopupMenu* pVCLPopupMenu = static_cast<PopupMenu *>(pPopupMenu->GetMenu());
        aURL = pVCLPopupMenu->GetItemCommand(rEvent.MenuId);
        void* nAttributePtr = pVCLPopupMenu->GetUserValue(rEvent.MenuId);
        MenuAttributes* pAttributes = static_cast<MenuAttributes *>(nAttributePtr);
        if (pAttributes)
            aTargetFrame = pAttributes->aTargetFrame;
    }

    Sequence< PropertyValue > aArgsList( 1 );
    aArgsList[0].Name = "Referer";
    aArgsList[0].Value <<= OUString( "private:user" );

    dispatchCommand( aURL, aArgsList, aTargetFrame );
}

void SAL_CALL NewMenuController::itemActivated( const css::awt::MenuEvent& )
{
    SolarMutexGuard aSolarMutexGuard;
    if ( !(m_xFrame.is() && m_xPopupMenu.is()) )
        return;

    VCLXPopupMenu* pPopupMenu = static_cast<VCLXPopupMenu *>(comphelper::getUnoTunnelImplementation<VCLXMenu>( m_xPopupMenu ));
    if ( !pPopupMenu )
        return;

    const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
    bool bShowImages( rSettings.GetUseImagesInMenus() );
    OUString aIconTheme( rSettings.DetermineIconTheme() );

    PopupMenu* pVCLPopupMenu = static_cast<PopupMenu *>(pPopupMenu->GetMenu());

    if ( m_bShowImages != bShowImages || m_aIconTheme != aIconTheme )
    {
        m_bShowImages = bShowImages;
        m_aIconTheme = aIconTheme;
        setMenuImages( pVCLPopupMenu, m_bShowImages );
    }

    setAccelerators( pVCLPopupMenu );
}

// XPopupMenuController
void NewMenuController::impl_setPopupMenu()
{

    if ( m_xPopupMenu.is() )
        fillPopupMenu( m_xPopupMenu );

    // Identify module that we are attach to. It's our context that we need to know.
    Reference< XModuleManager2 > xModuleManager = ModuleManager::create( m_xContext );
    try
    {
        m_aModuleIdentifier = xModuleManager->identify( m_xFrame );
        m_bModuleIdentified = true;

        if ( !m_aModuleIdentifier.isEmpty() )
        {
            Sequence< PropertyValue > aSeq;

            if ( xModuleManager->getByName( m_aModuleIdentifier ) >>= aSeq )
            {
                for ( PropertyValue const & prop : std::as_const(aSeq) )
                {
                    if ( prop.Name == "ooSetupFactoryEmptyDocumentURL" )
                    {
                        prop.Value >>= m_aEmptyDocURL;
                        break;
                    }
                }
            }
        }
    }
    catch ( const RuntimeException& )
    {
        throw;
    }
    catch ( const Exception& )
    {
    }
}

// XInitialization
void SAL_CALL NewMenuController::initialize( const Sequence< Any >& aArguments )
{
    osl::MutexGuard aLock( m_aMutex );

    bool bInitalized( m_bInitialized );
    if ( bInitalized )
        return;

    svt::PopupMenuControllerBase::initialize( aArguments );

    if ( m_bInitialized )
    {
        const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();

        m_bShowImages   = rSettings.GetUseImagesInMenus();
        m_aIconTheme    = rSettings.DetermineIconTheme();
        m_bNewMenu      = m_aCommandURL == aSlotNewDocDirect;
    }
}

}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */