LCOV - code coverage report
Current view: top level - framework/source/dispatch - windowcommanddispatch.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 35 73 47.9 %
Date: 2015-06-13 12:38:46 Functions: 7 8 87.5 %
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 <dispatch/windowcommanddispatch.hxx>
      21             : #include <targets.h>
      22             : #include <services.h>
      23             : 
      24             : #include <com/sun/star/frame/XDispatchProvider.hpp>
      25             : #include <com/sun/star/frame/XDispatch.hpp>
      26             : #include <com/sun/star/util/URLTransformer.hpp>
      27             : #include <com/sun/star/util/XURLTransformer.hpp>
      28             : 
      29             : #include <comphelper/processfactory.hxx>
      30             : #include <vcl/window.hxx>
      31             : #include <vcl/svapp.hxx>
      32             : #include <vcl/cmdevt.hxx>
      33             : #include <osl/mutex.hxx>
      34             : #include <toolkit/helper/vclunohelper.hxx>
      35             : 
      36             : namespace framework{
      37             : 
      38        3282 : WindowCommandDispatch::WindowCommandDispatch(const css::uno::Reference< css::uno::XComponentContext >& xContext ,
      39             :                          const css::uno::Reference< css::frame::XFrame >&              xFrame)
      40             :     : m_xContext    (xContext                    )
      41             :     , m_xFrame      (xFrame                      )
      42        3282 :     , m_xWindow     (xFrame->getContainerWindow())
      43             : {
      44        3282 :     impl_startListening();
      45        3282 : }
      46             : 
      47        9819 : WindowCommandDispatch::~WindowCommandDispatch()
      48             : {
      49        3273 :     impl_stopListening();
      50        3273 :     m_xContext.clear();
      51        6546 : }
      52             : 
      53        3282 : void WindowCommandDispatch::impl_startListening()
      54             : {
      55        3282 :     osl::ClearableMutexGuard aReadLock(m_mutex);
      56        6564 :     css::uno::Reference< css::awt::XWindow > xWindow( m_xWindow.get(), css::uno::UNO_QUERY );
      57        3282 :     aReadLock.clear();
      58             : 
      59        3282 :     if ( ! xWindow.is())
      60           0 :         return;
      61             : 
      62             :     {
      63        3282 :         SolarMutexGuard aSolarLock;
      64             : 
      65        3282 :         vcl::Window* pWindow = VCLUnoHelper::GetWindow(xWindow);
      66        3282 :         if ( ! pWindow)
      67           0 :             return;
      68             : 
      69        3282 :         pWindow->AddEventListener( LINK(this, WindowCommandDispatch, impl_notifyCommand) );
      70        3282 :     }
      71             : }
      72             : 
      73        3273 : void WindowCommandDispatch::impl_stopListening()
      74             : {
      75        3273 :     osl::ClearableMutexGuard aReadLock(m_mutex);
      76        6546 :     css::uno::Reference< css::awt::XWindow > xWindow( m_xWindow.get(), css::uno::UNO_QUERY );
      77        3273 :     aReadLock.clear();
      78             : 
      79        3273 :     if (!xWindow.is())
      80           0 :         return;
      81             : 
      82             :     {
      83        3273 :         SolarMutexGuard aSolarLock;
      84             : 
      85        3273 :         vcl::Window* pWindow = VCLUnoHelper::GetWindow(xWindow);
      86        3273 :         if (!pWindow)
      87           0 :             return;
      88             : 
      89        3273 :         pWindow->RemoveEventListener( LINK(this, WindowCommandDispatch, impl_notifyCommand) );
      90             : 
      91        3273 :         m_xWindow.clear();
      92        3273 :     }
      93             : }
      94             : 
      95      153860 : IMPL_LINK(WindowCommandDispatch, impl_notifyCommand, void*, pParam)
      96             : {
      97       76930 :     if ( ! pParam)
      98           0 :         return 0L;
      99             : 
     100       76930 :     const VclWindowEvent* pEvent = static_cast<VclWindowEvent*>(pParam);
     101       76930 :     if (pEvent->GetId() == VCLEVENT_OBJECT_DYING)
     102             :     {
     103           0 :         impl_stopListening();
     104           0 :         return 0L;
     105             :     }
     106       76930 :     if (pEvent->GetId() != VCLEVENT_WINDOW_COMMAND)
     107       76930 :         return 0L;
     108             : 
     109           0 :     const CommandEvent* pCommand = static_cast<CommandEvent*>(pEvent->GetData());
     110           0 :     if (pCommand->GetCommand() != CommandEventId::ShowDialog)
     111           0 :         return 0L;
     112             : 
     113           0 :     const CommandDialogData* pData = pCommand->GetDialogData();
     114           0 :     if ( ! pData)
     115           0 :         return 0L;
     116             : 
     117           0 :     const ShowDialogId nCommand = pData->GetDialogId();
     118           0 :     OUString sCommand;
     119             : 
     120           0 :     switch (nCommand)
     121             :     {
     122             :         case ShowDialogId::Preferences :
     123           0 :                 sCommand = ".uno:OptionsTreeDialog";
     124           0 :                 break;
     125             : 
     126             :         case ShowDialogId::About :
     127           0 :                 sCommand = ".uno:About";
     128           0 :                 break;
     129             : 
     130             :         default :
     131           0 :                 return 0L;
     132             :     }
     133             : 
     134           0 :     impl_dispatchCommand(sCommand);
     135             : 
     136           0 :     return 0L;
     137             : }
     138             : 
     139           0 : void WindowCommandDispatch::impl_dispatchCommand(const OUString& sCommand)
     140             : {
     141             :     // ignore all errors here. It's clicking a menu entry only ...
     142             :     // The user will try it again, in case nothing happens .-)
     143             :     try
     144             :     {
     145             :         // SYNCHRONIZED ->
     146           0 :         osl::ClearableMutexGuard aReadLock(m_mutex);
     147           0 :         css::uno::Reference< css::frame::XDispatchProvider >   xProvider(m_xFrame.get(), css::uno::UNO_QUERY_THROW);
     148           0 :         css::uno::Reference< css::uno::XComponentContext >     xContext    = m_xContext;
     149           0 :         aReadLock.clear();
     150             :         // <- SYNCHRONIZED
     151             : 
     152             :         // check provider ... we know it's weak reference only
     153           0 :         if ( ! xProvider.is())
     154           0 :             return;
     155             : 
     156           0 :         css::uno::Reference< css::util::XURLTransformer > xParser(css::util::URLTransformer::create(xContext));
     157           0 :         css::util::URL aCommand;
     158           0 :         aCommand.Complete = sCommand;
     159           0 :         xParser->parseStrict(aCommand);
     160             : 
     161           0 :         css::uno::Reference< css::frame::XDispatch > xDispatch = xProvider->queryDispatch(aCommand, SPECIALTARGET_SELF, 0);
     162           0 :         if (xDispatch.is())
     163           0 :             xDispatch->dispatch(aCommand, css::uno::Sequence< css::beans::PropertyValue >());
     164             :     }
     165           0 :     catch(const css::uno::Exception&)
     166             :     {}
     167             : }
     168             : 
     169             : } // namespace framework
     170             : 
     171             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11