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 "framework/addonmenu.hxx"
21 : #include "framework/addonsoptions.hxx"
22 : #include <general.h>
23 : #include <framework/imageproducer.hxx>
24 : #include <framework/menuconfiguration.hxx>
25 :
26 : #include <com/sun/star/uno/Reference.hxx>
27 : #include <com/sun/star/util/URL.hpp>
28 : #include <com/sun/star/util/XURLTransformer.hpp>
29 : #include <com/sun/star/lang/XServiceInfo.hpp>
30 :
31 : #include <tools/config.hxx>
32 : #include <vcl/svapp.hxx>
33 : #include <svtools/menuoptions.hxx>
34 : #include <svl/solar.hrc>
35 :
36 : using namespace ::com::sun::star::uno;
37 : using namespace ::com::sun::star::lang;
38 : using namespace ::com::sun::star::frame;
39 : using namespace ::com::sun::star::beans;
40 :
41 : // Please look at sfx2/inc/sfxsids.hrc the values are defined there. Due to build dependencies
42 : // we cannot include the header file.
43 : const sal_uInt16 SID_HELPMENU = (SID_SFX_START + 410);
44 :
45 : namespace framework
46 : {
47 :
48 0 : AddonMenu::AddonMenu( const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& rFrame ) :
49 0 : m_xFrame( rFrame )
50 : {
51 0 : }
52 :
53 0 : AddonMenu::~AddonMenu()
54 : {
55 0 : for ( sal_uInt16 i = 0; i < GetItemCount(); i++ )
56 : {
57 0 : if ( GetItemType( i ) != MENUITEM_SEPARATOR )
58 : {
59 : // delete user attributes created with new!
60 0 : sal_uInt16 nId = GetItemId( i );
61 0 : MenuConfiguration::Attributes* pUserAttributes = (MenuConfiguration::Attributes*)GetUserValue( nId );
62 0 : delete pUserAttributes;
63 0 : delete GetPopupMenu( nId );
64 : }
65 : }
66 0 : }
67 :
68 : // Check if command URL string has the unique prefix to identify addon popup menus
69 0 : bool AddonPopupMenu::IsCommandURLPrefix( const OUString& aCmdURL )
70 : {
71 0 : const char aPrefixCharBuf[] = ADDONSPOPUPMENU_URL_PREFIX_STR;
72 :
73 0 : return aCmdURL.matchAsciiL( aPrefixCharBuf, sizeof( aPrefixCharBuf )-1, 0 );
74 : }
75 :
76 0 : AddonPopupMenu::AddonPopupMenu( const com::sun::star::uno::Reference< com::sun::star::frame::XFrame >& rFrame ) :
77 0 : AddonMenu( rFrame )
78 : {
79 0 : }
80 :
81 0 : AddonPopupMenu::~AddonPopupMenu()
82 : {
83 0 : }
84 :
85 0 : static Reference< XModel > GetModelFromFrame( const Reference< XFrame >& rFrame )
86 : {
87 : // Query for the model to get check the context information
88 0 : Reference< XModel > xModel;
89 0 : if ( rFrame.is() )
90 : {
91 0 : Reference< XController > xController( rFrame->getController(), UNO_QUERY );
92 0 : if ( xController.is() )
93 0 : xModel = xController->getModel();
94 : }
95 :
96 0 : return xModel;
97 : }
98 :
99 0 : bool AddonMenuManager::HasAddonMenuElements()
100 : {
101 0 : return AddonsOptions().HasAddonsMenu();
102 : }
103 :
104 : // Factory method to create different Add-On menu types
105 0 : PopupMenu* AddonMenuManager::CreatePopupMenuType( MenuType eMenuType, const Reference< XFrame >& rFrame )
106 : {
107 0 : if ( eMenuType == ADDON_MENU )
108 0 : return new AddonMenu( rFrame );
109 0 : else if ( eMenuType == ADDON_POPUPMENU )
110 0 : return new AddonPopupMenu( rFrame );
111 : else
112 0 : return NULL;
113 : }
114 :
115 : // Create the Add-Ons menu
116 0 : AddonMenu* AddonMenuManager::CreateAddonMenu( const Reference< XFrame >& rFrame )
117 : {
118 0 : AddonsOptions aOptions;
119 0 : AddonMenu* pAddonMenu = NULL;
120 0 : sal_uInt16 nUniqueMenuId = ADDONMENU_ITEMID_START;
121 :
122 0 : const Sequence< Sequence< PropertyValue > >& rAddonMenuEntries = aOptions.GetAddonsMenu();
123 0 : if ( rAddonMenuEntries.getLength() > 0 )
124 : {
125 0 : pAddonMenu = (AddonMenu *)AddonMenuManager::CreatePopupMenuType( ADDON_MENU, rFrame );
126 0 : Reference< XModel > xModel = GetModelFromFrame( rFrame );
127 0 : AddonMenuManager::BuildMenu( pAddonMenu, ADDON_MENU, MENU_APPEND, nUniqueMenuId, rAddonMenuEntries, rFrame, xModel );
128 :
129 : // Don't return an empty Add-On menu
130 0 : if ( pAddonMenu->GetItemCount() == 0 )
131 : {
132 0 : delete pAddonMenu;
133 0 : pAddonMenu = NULL;
134 0 : }
135 : }
136 :
137 0 : return pAddonMenu;
138 : }
139 :
140 : // Returns the next insert position from nPos.
141 0 : sal_uInt16 AddonMenuManager::GetNextPos( sal_uInt16 nPos )
142 : {
143 0 : return ( nPos == MENU_APPEND ) ? MENU_APPEND : ( nPos+1 );
144 : }
145 :
146 0 : static sal_uInt16 FindMenuId( Menu* pMenu, const OUString& aCommand )
147 : {
148 0 : sal_uInt16 nPos = 0;
149 0 : OUString aCmd;
150 0 : for ( nPos = 0; nPos < pMenu->GetItemCount(); nPos++ )
151 : {
152 0 : sal_uInt16 nId = pMenu->GetItemId( nPos );
153 0 : aCmd = pMenu->GetItemCommand( nId );
154 0 : if ( aCmd == aCommand )
155 0 : return nId;
156 : }
157 :
158 0 : return USHRT_MAX;
159 : }
160 :
161 : // Merge the Add-Ons help menu items into the given menu bar at a defined pos
162 0 : void AddonMenuManager::MergeAddonHelpMenu( const Reference< XFrame >& rFrame, MenuBar* pMergeMenuBar )
163 : {
164 0 : if ( pMergeMenuBar )
165 : {
166 0 : PopupMenu* pHelpMenu = pMergeMenuBar->GetPopupMenu( SID_HELPMENU );
167 0 : if ( !pHelpMenu )
168 : {
169 0 : sal_uInt16 nId = FindMenuId(pMergeMenuBar, OUString(".uno:HelpMenu"));
170 0 : if ( nId != USHRT_MAX )
171 0 : pHelpMenu = pMergeMenuBar->GetPopupMenu( nId );
172 : }
173 :
174 0 : if ( pHelpMenu )
175 : {
176 : // Add-Ons help menu items should be inserted after the "registration" menu item
177 0 : sal_uInt16 nItemCount = pHelpMenu->GetItemCount();
178 0 : sal_uInt16 nInsSepAfterPos = MENU_APPEND;
179 0 : sal_uInt16 nUniqueMenuId = ADDONMENU_ITEMID_START;
180 0 : AddonsOptions aOptions;
181 :
182 : // try to detect the about menu item with the command URL
183 0 : sal_uInt16 nId = FindMenuId(pHelpMenu, OUString(".uno:About"));
184 0 : sal_uInt16 nInsPos = pHelpMenu->GetItemPos( nId );
185 :
186 0 : const Sequence< Sequence< PropertyValue > >& rAddonHelpMenuEntries = aOptions.GetAddonsHelpMenu();
187 :
188 0 : if ( nInsPos < nItemCount && pHelpMenu->GetItemType( nInsPos ) != MENUITEM_SEPARATOR )
189 0 : nInsSepAfterPos = nInsPos;
190 :
191 0 : Reference< XModel > xModel = GetModelFromFrame( rFrame );
192 0 : AddonMenuManager::BuildMenu( pHelpMenu, ADDON_MENU, nInsPos, nUniqueMenuId, rAddonHelpMenuEntries, rFrame, xModel );
193 :
194 0 : if ( pHelpMenu->GetItemCount() > nItemCount )
195 : {
196 0 : if ( nInsSepAfterPos < MENU_APPEND )
197 : {
198 0 : nInsSepAfterPos += ( pHelpMenu->GetItemCount() - nItemCount );
199 0 : if ( pHelpMenu->GetItemType( nInsSepAfterPos ) != MENUITEM_SEPARATOR )
200 0 : pHelpMenu->InsertSeparator(OString(), nInsSepAfterPos);
201 : }
202 0 : pHelpMenu->InsertSeparator(OString(), nItemCount);
203 0 : }
204 : }
205 : }
206 0 : }
207 :
208 : // Merge the addon popup menus into the given menu bar at the provided pos.
209 0 : void AddonMenuManager::MergeAddonPopupMenus( const Reference< XFrame >& rFrame,
210 : const Reference< XModel >& rModel,
211 : sal_uInt16 nMergeAtPos,
212 : MenuBar* pMergeMenuBar )
213 : {
214 0 : if ( pMergeMenuBar )
215 : {
216 0 : AddonsOptions aAddonsOptions;
217 0 : sal_uInt16 nInsertPos = nMergeAtPos;
218 :
219 0 : OUString aTitle;
220 0 : OUString aURL;
221 0 : OUString aTarget;
222 0 : OUString aImageId;
223 0 : OUString aContext;
224 0 : Sequence< Sequence< PropertyValue > > aAddonSubMenu;
225 0 : sal_uInt16 nUniqueMenuId = ADDONMENU_ITEMID_START;
226 :
227 0 : const Sequence< Sequence< PropertyValue > >& rAddonMenuEntries = aAddonsOptions.GetAddonsMenuBarPart();
228 0 : for ( sal_Int32 i = 0; i < rAddonMenuEntries.getLength(); i++ )
229 : {
230 0 : AddonMenuManager::GetMenuEntry( rAddonMenuEntries[i],
231 : aTitle,
232 : aURL,
233 : aTarget,
234 : aImageId,
235 : aContext,
236 0 : aAddonSubMenu );
237 0 : if ( !aTitle.isEmpty() &&
238 0 : !aURL.isEmpty() &&
239 0 : aAddonSubMenu.getLength() > 0 &&
240 0 : AddonMenuManager::IsCorrectContext( rModel, aContext ))
241 : {
242 0 : sal_uInt16 nId = nUniqueMenuId++;
243 0 : AddonPopupMenu* pAddonPopupMenu = (AddonPopupMenu *)AddonMenuManager::CreatePopupMenuType( ADDON_POPUPMENU, rFrame );
244 :
245 0 : AddonMenuManager::BuildMenu( pAddonPopupMenu, ADDON_MENU, MENU_APPEND, nUniqueMenuId, aAddonSubMenu, rFrame, rModel );
246 :
247 0 : if ( pAddonPopupMenu->GetItemCount() > 0 )
248 : {
249 0 : pAddonPopupMenu->SetCommandURL( aURL );
250 0 : pMergeMenuBar->InsertItem( nId, aTitle, 0, OString(), nInsertPos++ );
251 0 : pMergeMenuBar->SetPopupMenu( nId, pAddonPopupMenu );
252 :
253 : // Store the command URL into the VCL menu bar for later identification
254 0 : pMergeMenuBar->SetItemCommand( nId, aURL );
255 : }
256 : else
257 0 : delete pAddonPopupMenu;
258 : }
259 0 : }
260 : }
261 0 : }
262 :
263 : // Insert the menu and sub menu entries into pCurrentMenu with the aAddonMenuDefinition provided
264 0 : void AddonMenuManager::BuildMenu( PopupMenu* pCurrentMenu,
265 : MenuType nSubMenuType,
266 : sal_uInt16 nInsPos,
267 : sal_uInt16& nUniqueMenuId,
268 : Sequence< Sequence< PropertyValue > > aAddonMenuDefinition,
269 : const Reference< XFrame >& rFrame,
270 : const Reference< XModel >& rModel )
271 : {
272 0 : Sequence< Sequence< PropertyValue > > aAddonSubMenu;
273 0 : bool bInsertSeparator = false;
274 0 : sal_uInt32 i = 0;
275 0 : sal_uInt32 nElements = 0;
276 0 : sal_uInt32 nCount = aAddonMenuDefinition.getLength();
277 0 : AddonsOptions aAddonsOptions;
278 :
279 0 : OUString aTitle;
280 0 : OUString aURL;
281 0 : OUString aTarget;
282 0 : OUString aImageId;
283 0 : OUString aContext;
284 :
285 0 : for ( i = 0; i < nCount; ++i )
286 : {
287 0 : GetMenuEntry( aAddonMenuDefinition[i], aTitle, aURL, aTarget, aImageId, aContext, aAddonSubMenu );
288 :
289 0 : if ( !IsCorrectContext( rModel, aContext ) || ( aTitle.isEmpty() && aURL.isEmpty() ))
290 0 : continue;
291 :
292 0 : if ( aURL == "private:separator" )
293 0 : bInsertSeparator = true;
294 : else
295 : {
296 0 : PopupMenu* pSubMenu = NULL;
297 0 : if ( aAddonSubMenu.getLength() > 0 )
298 : {
299 0 : pSubMenu = AddonMenuManager::CreatePopupMenuType( nSubMenuType, rFrame );
300 0 : AddonMenuManager::BuildMenu( pSubMenu, nSubMenuType, MENU_APPEND, nUniqueMenuId, aAddonSubMenu, rFrame, rModel );
301 :
302 : // Don't create a menu item for an empty sub menu
303 0 : if ( pSubMenu->GetItemCount() == 0 )
304 : {
305 0 : delete pSubMenu;
306 0 : pSubMenu = NULL;
307 0 : continue;
308 : }
309 : }
310 :
311 0 : if ( bInsertSeparator && nElements > 0 )
312 : {
313 : // Insert a separator only when we insert a new element afterwards and we
314 : // have already one before us
315 0 : nElements = 0;
316 0 : bInsertSeparator = false;
317 0 : pCurrentMenu->InsertSeparator(OString(), nInsPos);
318 0 : nInsPos = AddonMenuManager::GetNextPos( nInsPos );
319 : }
320 :
321 0 : sal_uInt16 nId = nUniqueMenuId++;
322 0 : pCurrentMenu->InsertItem(nId, aTitle, 0, OString(), nInsPos);
323 0 : nInsPos = AddonMenuManager::GetNextPos( nInsPos );
324 :
325 0 : ++nElements;
326 :
327 : // Store values from configuration to the New and Wizard menu entries to enable
328 : // sfx2 based code to support high contrast mode correctly!
329 0 : pCurrentMenu->SetUserValue( nId, sal_uIntPtr( new MenuConfiguration::Attributes( aTarget, aImageId )) );
330 0 : pCurrentMenu->SetItemCommand( nId, aURL );
331 :
332 0 : if ( pSubMenu )
333 0 : pCurrentMenu->SetPopupMenu( nId, pSubMenu );
334 : }
335 0 : }
336 0 : }
337 :
338 : // Retrieve the menu entry property values from a sequence
339 0 : void AddonMenuManager::GetMenuEntry( const Sequence< PropertyValue >& rAddonMenuEntry,
340 : OUString& rTitle,
341 : OUString& rURL,
342 : OUString& rTarget,
343 : OUString& rImageId,
344 : OUString& rContext,
345 : Sequence< Sequence< PropertyValue > >& rAddonSubMenu )
346 : {
347 : // Reset submenu parameter
348 0 : rAddonSubMenu = Sequence< Sequence< PropertyValue > >();
349 :
350 0 : for ( int i = 0; i < rAddonMenuEntry.getLength(); i++ )
351 : {
352 0 : OUString aMenuEntryPropName = rAddonMenuEntry[i].Name;
353 0 : if ( aMenuEntryPropName == ADDONSMENUITEM_PROPERTYNAME_URL )
354 0 : rAddonMenuEntry[i].Value >>= rURL;
355 0 : else if ( aMenuEntryPropName == ADDONSMENUITEM_PROPERTYNAME_TITLE )
356 0 : rAddonMenuEntry[i].Value >>= rTitle;
357 0 : else if ( aMenuEntryPropName == ADDONSMENUITEM_PROPERTYNAME_TARGET )
358 0 : rAddonMenuEntry[i].Value >>= rTarget;
359 0 : else if ( aMenuEntryPropName == ADDONSMENUITEM_PROPERTYNAME_IMAGEIDENTIFIER )
360 0 : rAddonMenuEntry[i].Value >>= rImageId;
361 0 : else if ( aMenuEntryPropName == ADDONSMENUITEM_PROPERTYNAME_SUBMENU )
362 0 : rAddonMenuEntry[i].Value >>= rAddonSubMenu;
363 0 : else if ( aMenuEntryPropName == ADDONSMENUITEM_PROPERTYNAME_CONTEXT )
364 0 : rAddonMenuEntry[i].Value >>= rContext;
365 0 : }
366 0 : }
367 :
368 : // Check if the context string matches the provided xModel context
369 0 : bool AddonMenuManager::IsCorrectContext( const Reference< XModel >& rModel, const OUString& aContext )
370 : {
371 0 : if ( rModel.is() )
372 : {
373 0 : Reference< com::sun::star::lang::XServiceInfo > xServiceInfo( rModel, UNO_QUERY );
374 0 : if ( xServiceInfo.is() )
375 : {
376 0 : sal_Int32 nIndex = 0;
377 0 : do
378 : {
379 0 : OUString aToken = aContext.getToken( 0, ',', nIndex );
380 :
381 0 : if ( xServiceInfo->supportsService( aToken ))
382 0 : return true;
383 : }
384 0 : while ( nIndex >= 0 );
385 0 : }
386 : }
387 :
388 0 : return ( aContext.isEmpty() );
389 : }
390 :
391 : }
392 :
393 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|