1*6d739b60SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*6d739b60SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*6d739b60SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*6d739b60SAndrew Rist  * distributed with this work for additional information
6*6d739b60SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*6d739b60SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*6d739b60SAndrew Rist  * "License"); you may not use this file except in compliance
9*6d739b60SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*6d739b60SAndrew Rist  *
11*6d739b60SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*6d739b60SAndrew Rist  *
13*6d739b60SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*6d739b60SAndrew Rist  * software distributed under the License is distributed on an
15*6d739b60SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*6d739b60SAndrew Rist  * KIND, either express or implied.  See the License for the
17*6d739b60SAndrew Rist  * specific language governing permissions and limitations
18*6d739b60SAndrew Rist  * under the License.
19*6d739b60SAndrew Rist  *
20*6d739b60SAndrew Rist  *************************************************************/
21*6d739b60SAndrew Rist 
22*6d739b60SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_framework.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir //_________________________________________________________________________________________________________________
28cdf0e10cSrcweir //	my own includes
29cdf0e10cSrcweir //_________________________________________________________________________________________________________________
30cdf0e10cSrcweir #include <dispatch/dispatchinformationprovider.hxx>
31cdf0e10cSrcweir #include <dispatch/closedispatcher.hxx>
32cdf0e10cSrcweir #include <threadhelp/readguard.hxx>
33cdf0e10cSrcweir #include <threadhelp/writeguard.hxx>
34cdf0e10cSrcweir #include <stdtypes.h>
35cdf0e10cSrcweir #include <services.h>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir //_________________________________________________________________________________________________________________
38cdf0e10cSrcweir //	interface includes
39cdf0e10cSrcweir //_________________________________________________________________________________________________________________
40cdf0e10cSrcweir #include <com/sun/star/frame/CommandGroup.hpp>
41cdf0e10cSrcweir 
42cdf0e10cSrcweir //_________________________________________________________________________________________________________________
43cdf0e10cSrcweir //	includes of other projects
44cdf0e10cSrcweir //_________________________________________________________________________________________________________________
45cdf0e10cSrcweir #include <comphelper/sequenceasvector.hxx>
46cdf0e10cSrcweir 
47cdf0e10cSrcweir //_________________________________________________________________________________________________________________
48cdf0e10cSrcweir //	namespace
49cdf0e10cSrcweir //_________________________________________________________________________________________________________________
50cdf0e10cSrcweir 
51cdf0e10cSrcweir namespace framework{
52cdf0e10cSrcweir 
53cdf0e10cSrcweir namespace css = ::com::sun::star;
54cdf0e10cSrcweir 
55cdf0e10cSrcweir //_________________________________________________________________________________________________________________
56cdf0e10cSrcweir //	declarations
57cdf0e10cSrcweir //_________________________________________________________________________________________________________________
58cdf0e10cSrcweir DEFINE_XINTERFACE_1(DispatchInformationProvider                               ,
59cdf0e10cSrcweir                     OWeakObject                                               ,
60cdf0e10cSrcweir                     DIRECT_INTERFACE(css::frame::XDispatchInformationProvider))
61cdf0e10cSrcweir 
62cdf0e10cSrcweir //_________________________________________________________________________________________________________________
63cdf0e10cSrcweir DispatchInformationProvider::DispatchInformationProvider(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR ,
64cdf0e10cSrcweir                                                          const css::uno::Reference< css::frame::XFrame >&              xFrame)
65cdf0e10cSrcweir     : ThreadHelpBase(&Application::GetSolarMutex())
66cdf0e10cSrcweir     , m_xSMGR       (xSMGR                        )
67cdf0e10cSrcweir     , m_xFrame      (xFrame                       )
68cdf0e10cSrcweir {
69cdf0e10cSrcweir }
70cdf0e10cSrcweir 
71cdf0e10cSrcweir //_________________________________________________________________________________________________________________
72cdf0e10cSrcweir DispatchInformationProvider::~DispatchInformationProvider()
73cdf0e10cSrcweir {
74cdf0e10cSrcweir }
75cdf0e10cSrcweir 
76cdf0e10cSrcweir //_________________________________________________________________________________________________________________
77cdf0e10cSrcweir css::uno::Sequence< sal_Int16 > SAL_CALL DispatchInformationProvider::getSupportedCommandGroups()
78cdf0e10cSrcweir     throw (css::uno::RuntimeException)
79cdf0e10cSrcweir {
80cdf0e10cSrcweir     css::uno::Sequence< css::uno::Reference< css::frame::XDispatchInformationProvider > > lProvider = implts_getAllSubProvider();
81cdf0e10cSrcweir     sal_Int32                                                                             c1        = lProvider.getLength();
82cdf0e10cSrcweir     sal_Int32                                                                             i1        = 0;
83cdf0e10cSrcweir 
84cdf0e10cSrcweir     ::comphelper::SequenceAsVector< sal_Int16 > lGroups;
85cdf0e10cSrcweir 
86cdf0e10cSrcweir     for (i1=0; i1<c1; ++i1)
87cdf0e10cSrcweir     {
88cdf0e10cSrcweir         // ignore controller, which doesnt implement the right interface
89cdf0e10cSrcweir         css::uno::Reference< css::frame::XDispatchInformationProvider > xProvider = lProvider[i1];
90cdf0e10cSrcweir         if (!xProvider.is())
91cdf0e10cSrcweir             continue;
92cdf0e10cSrcweir 
93cdf0e10cSrcweir         const css::uno::Sequence< sal_Int16 > lProviderGroups = xProvider->getSupportedCommandGroups();
94cdf0e10cSrcweir               sal_Int32                       c2              = lProviderGroups.getLength();
95cdf0e10cSrcweir               sal_Int32                       i2              = 0;
96cdf0e10cSrcweir         for (i2=0; i2<c2; ++i2)
97cdf0e10cSrcweir         {
98cdf0e10cSrcweir             const sal_Int16&                                                  rGroup = lProviderGroups[i2];
99cdf0e10cSrcweir                   ::comphelper::SequenceAsVector< sal_Int16 >::const_iterator pGroup = ::std::find(lGroups.begin(), lGroups.end(), rGroup);
100cdf0e10cSrcweir             if (pGroup == lGroups.end())
101cdf0e10cSrcweir                 lGroups.push_back(rGroup);
102cdf0e10cSrcweir         }
103cdf0e10cSrcweir     }
104cdf0e10cSrcweir 
105cdf0e10cSrcweir     return lGroups.getAsConstList();
106cdf0e10cSrcweir }
107cdf0e10cSrcweir 
108cdf0e10cSrcweir //_________________________________________________________________________________________________________________
109cdf0e10cSrcweir css::uno::Sequence< css::frame::DispatchInformation > SAL_CALL DispatchInformationProvider::getConfigurableDispatchInformation(sal_Int16 nCommandGroup)
110cdf0e10cSrcweir     throw (css::uno::RuntimeException)
111cdf0e10cSrcweir {
112cdf0e10cSrcweir     css::uno::Sequence< css::uno::Reference< css::frame::XDispatchInformationProvider > > lProvider = implts_getAllSubProvider();
113cdf0e10cSrcweir     sal_Int32                                                                             c1        = lProvider.getLength();
114cdf0e10cSrcweir     sal_Int32                                                                             i1        = 0;
115cdf0e10cSrcweir 
116cdf0e10cSrcweir     BaseHash< css::frame::DispatchInformation > lInfos;
117cdf0e10cSrcweir 
118cdf0e10cSrcweir     for (i1=0; i1<c1; ++i1)
119cdf0e10cSrcweir     {
120cdf0e10cSrcweir         try
121cdf0e10cSrcweir         {
122cdf0e10cSrcweir             // ignore controller, which doesnt implement the right interface
123cdf0e10cSrcweir             css::uno::Reference< css::frame::XDispatchInformationProvider > xProvider = lProvider[i1];
124cdf0e10cSrcweir             if (!xProvider.is())
125cdf0e10cSrcweir                 continue;
126cdf0e10cSrcweir 
127cdf0e10cSrcweir             const css::uno::Sequence< css::frame::DispatchInformation > lProviderInfos = xProvider->getConfigurableDispatchInformation(nCommandGroup);
128cdf0e10cSrcweir                   sal_Int32                                             c2             = lProviderInfos.getLength();
129cdf0e10cSrcweir                   sal_Int32                                             i2             = 0;
130cdf0e10cSrcweir             for (i2=0; i2<c2; ++i2)
131cdf0e10cSrcweir             {
132cdf0e10cSrcweir                 const css::frame::DispatchInformation&                            rInfo = lProviderInfos[i2];
133cdf0e10cSrcweir                       BaseHash< css::frame::DispatchInformation >::const_iterator pInfo = lInfos.find(rInfo.Command);
134cdf0e10cSrcweir                 if (pInfo == lInfos.end())
135cdf0e10cSrcweir                     lInfos[rInfo.Command] = rInfo;
136cdf0e10cSrcweir             }
137cdf0e10cSrcweir         }
138cdf0e10cSrcweir         catch(const css::uno::RuntimeException& exRun)
139cdf0e10cSrcweir             { throw exRun; }
140cdf0e10cSrcweir         catch(const css::uno::Exception&)
141cdf0e10cSrcweir             { continue; }
142cdf0e10cSrcweir     }
143cdf0e10cSrcweir 
144cdf0e10cSrcweir     c1 = (sal_Int32)lInfos.size();
145cdf0e10cSrcweir     i1 = 0;
146cdf0e10cSrcweir 
147cdf0e10cSrcweir     css::uno::Sequence< css::frame::DispatchInformation >       lReturn(c1);
148cdf0e10cSrcweir     BaseHash< css::frame::DispatchInformation >::const_iterator pStepp ;
149cdf0e10cSrcweir     for (  pStepp  = lInfos.begin()          ;
150cdf0e10cSrcweir            pStepp != lInfos.end  () && i1<c1 ;
151cdf0e10cSrcweir          ++pStepp, ++i1                      )
152cdf0e10cSrcweir     {
153cdf0e10cSrcweir         lReturn[i1] = pStepp->second;
154cdf0e10cSrcweir     }
155cdf0e10cSrcweir     return lReturn;
156cdf0e10cSrcweir }
157cdf0e10cSrcweir 
158cdf0e10cSrcweir //_________________________________________________________________________________________________________________
159cdf0e10cSrcweir css::uno::Sequence< css::uno::Reference< css::frame::XDispatchInformationProvider > > DispatchInformationProvider::implts_getAllSubProvider()
160cdf0e10cSrcweir {
161cdf0e10cSrcweir     // SAFE -> ----------------------------------
162cdf0e10cSrcweir     ReadGuard aReadLock(m_aLock);
163cdf0e10cSrcweir     css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR = m_xSMGR;
164cdf0e10cSrcweir     css::uno::Reference< css::frame::XFrame >              xFrame(m_xFrame.get(), css::uno::UNO_QUERY);
165cdf0e10cSrcweir     aReadLock.unlock();
166cdf0e10cSrcweir     // <- SAFE ----------------------------------
167cdf0e10cSrcweir 
168cdf0e10cSrcweir     if (!xFrame.is())
169cdf0e10cSrcweir         return css::uno::Sequence< css::uno::Reference< css::frame::XDispatchInformationProvider > >();
170cdf0e10cSrcweir 
171cdf0e10cSrcweir     CloseDispatcher* pCloser = new CloseDispatcher(xSMGR, xFrame, ::rtl::OUString::createFromAscii("_self")); // explicit "_self" ... not "" ... see implementation of close dispatcher itself!
172cdf0e10cSrcweir     css::uno::Reference< css::uno::XInterface > xCloser(static_cast< css::frame::XDispatch* >(pCloser), css::uno::UNO_QUERY);
173cdf0e10cSrcweir 
174cdf0e10cSrcweir     css::uno::Reference< css::frame::XDispatchInformationProvider > xCloseDispatch(xCloser                                                      , css::uno::UNO_QUERY);
175cdf0e10cSrcweir     css::uno::Reference< css::frame::XDispatchInformationProvider > xController   (xFrame->getController()                                      , css::uno::UNO_QUERY);
176cdf0e10cSrcweir     css::uno::Reference< css::frame::XDispatchInformationProvider > xAppDispatcher(xSMGR->createInstance(IMPLEMENTATIONNAME_APPDISPATCHPROVIDER), css::uno::UNO_QUERY);
177cdf0e10cSrcweir 
178cdf0e10cSrcweir     css::uno::Sequence< css::uno::Reference< css::frame::XDispatchInformationProvider > > lProvider(3);
179cdf0e10cSrcweir     lProvider[0] = xController   ;
180cdf0e10cSrcweir     lProvider[1] = xCloseDispatch;
181cdf0e10cSrcweir     lProvider[2] = xAppDispatcher;
182cdf0e10cSrcweir 
183cdf0e10cSrcweir     return lProvider;
184cdf0e10cSrcweir }
185cdf0e10cSrcweir 
186cdf0e10cSrcweir } // namespace framework
187