1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <unotools/moduleoptions.hxx>
22 #include <unotools/dynamicmenuoptions.hxx>
23 #include <unotools/historyoptions.hxx>
24 #include <rtl/ustring.hxx>
25 #include <tools/urlobj.hxx>
27 #include <comphelper/sequenceashashmap.hxx>
28 #include <osl/mutex.hxx>
29 #include <sfx2/app.hxx>
30 #include <sal/macros.h>
32 #define USE_APP_SHORTCUTS
33 #include "shutdownicon.hxx"
35 #include <com/sun/star/util/XStringWidth.hpp>
37 #include <cppuhelper/implbase1.hxx>
43 #include <objc/objc-runtime.h>
44 #include <Cocoa/Cocoa.h>
47 using namespace ::osl;
48 using namespace ::com::sun::star::uno;
49 using namespace ::com::sun::star::task;
50 using namespace ::com::sun::star::lang;
51 using namespace ::com::sun::star::beans;
52 using namespace ::com::sun::star::util;
62 #define MI_STARTMODULE 9
64 @interface QSMenuExecute : NSObject
67 -(void)executeMenuItem: (NSMenuItem*)pItem;
68 -(void)dockIconClicked: (NSObject*)pSender;
71 @implementation QSMenuExecute
72 -(void)executeMenuItem: (NSMenuItem*)pItem
77 ShutdownIcon::FileOpen();
80 ShutdownIcon::OpenURL( OUString( WRITER_URL ), OUString( "_default" ) );
83 ShutdownIcon::OpenURL( OUString( CALC_URL ), OUString( "_default" ) );
86 ShutdownIcon::OpenURL( OUString( IMPRESS_URL ), OUString( "_default" ) );
89 ShutdownIcon::OpenURL( OUString( DRAW_URL ), OUString( "_default" ) );
92 ShutdownIcon::OpenURL( OUString( BASE_URL ), OUString( "_default" ) );
95 ShutdownIcon::OpenURL( OUString( MATH_URL ), OUString( "_default" ) );
98 ShutdownIcon::FromTemplate();
101 ShutdownIcon::OpenURL( OUString( STARTMODULE_URL ), OUString( "_default" ) );
108 -(void)dockIconClicked: (NSObject*)pSender
111 // start start module
112 ShutdownIcon::OpenURL( OUString( STARTMODULE_URL ), OUString( "_default" ) );
117 bool ShutdownIcon::IsQuickstarterInstalled()
122 static NSMenuItem* pDefMenu = nil, *pDockSubMenu = nil;
123 static QSMenuExecute* pExecute = nil;
125 static std::set< OUString > aShortcuts;
127 static NSString* getAutoreleasedString( const rtl::OUString& rStr )
129 return [[[NSString alloc] initWithCharacters: rStr.getStr() length: rStr.getLength()] autorelease];
132 struct RecentMenuEntry
135 rtl::OUString aFilter;
136 rtl::OUString aTitle;
137 rtl::OUString aPassword;
140 class RecentFilesStringLength : public ::cppu::WeakImplHelper1< ::com::sun::star::util::XStringWidth >
143 RecentFilesStringLength() {}
144 virtual ~RecentFilesStringLength() {}
147 sal_Int32 SAL_CALL queryStringWidth( const ::rtl::OUString& aString )
148 throw (::com::sun::star::uno::RuntimeException) SAL_OVERRIDE
150 return aString.getLength();
154 @interface RecentMenuDelegate : NSObject
156 std::vector< RecentMenuEntry >* m_pRecentFilesItems;
160 -(void)menuNeedsUpdate:(NSMenu *)menu;
161 -(void)executeRecentEntry: (NSMenuItem*)item;
164 @implementation RecentMenuDelegate
167 if( (self = [super init]) )
169 m_pRecentFilesItems = new std::vector< RecentMenuEntry >();
176 delete m_pRecentFilesItems;
180 -(void)menuNeedsUpdate:(NSMenu *)menu
183 int nItems = [menu numberOfItems];
185 [menu removeItemAtIndex: 0];
187 // update recent item list
188 Sequence< Sequence< PropertyValue > > aHistoryList( SvtHistoryOptions().GetList( ePICKLIST ) );
190 int nPickListMenuItems = ( aHistoryList.getLength() > 99 ) ? 99 : aHistoryList.getLength();
192 m_pRecentFilesItems->clear();
193 if( ( nPickListMenuItems > 0 ) )
195 for ( int i = 0; i < nPickListMenuItems; i++ )
197 Sequence< PropertyValue >& rPickListEntry = aHistoryList[i];
198 RecentMenuEntry aRecentFile;
200 for ( int j = 0; j < rPickListEntry.getLength(); j++ )
202 Any a = rPickListEntry[j].Value;
204 if ( rPickListEntry[j].Name == HISTORY_PROPERTYNAME_URL )
205 a >>= aRecentFile.aURL;
206 else if ( rPickListEntry[j].Name == HISTORY_PROPERTYNAME_FILTER )
207 a >>= aRecentFile.aFilter;
208 else if ( rPickListEntry[j].Name == HISTORY_PROPERTYNAME_TITLE )
209 a >>= aRecentFile.aTitle;
210 else if ( rPickListEntry[j].Name == HISTORY_PROPERTYNAME_PASSWORD )
211 a >>= aRecentFile.aPassword;
214 m_pRecentFilesItems->push_back( aRecentFile );
218 // insert new recent items
219 for ( sal_uInt32 i = 0; i < m_pRecentFilesItems->size(); i++ )
221 rtl::OUString aMenuTitle;
222 INetURLObject aURL( (*m_pRecentFilesItems)[i].aURL );
224 if ( aURL.GetProtocol() == INetProtocol::File )
226 // Do handle file URL differently => convert it to a system
227 // path and abbreviate it with a special function:
228 ::rtl::OUString aSystemPath( aURL.getFSysPath( INetURLObject::FSYS_DETECT ) );
229 ::rtl::OUString aCompactedSystemPath;
231 oslFileError nError = osl_abbreviateSystemPath( aSystemPath.pData, &aCompactedSystemPath.pData, 46, NULL );
233 aMenuTitle = aCompactedSystemPath;
235 aMenuTitle = aSystemPath;
239 // Use INetURLObject to abbreviate all other URLs
240 Reference< XStringWidth > xStringLength( new RecentFilesStringLength() );
241 aMenuTitle = aURL.getAbbreviated( xStringLength, 46, INetURLObject::DECODE_UNAMBIGUOUS );
244 NSMenuItem* pNewItem = [[NSMenuItem alloc] initWithTitle: getAutoreleasedString( aMenuTitle )
245 action: @selector(executeRecentEntry:)
247 [pNewItem setTag: i];
248 [pNewItem setTarget: self];
249 [pNewItem setEnabled: YES];
250 [menu addItem: pNewItem];
251 [pNewItem autorelease];
255 -(void)executeRecentEntry: (NSMenuItem*)item
257 sal_Int32 nIndex = [item tag];
258 if( ( nIndex >= 0 ) && ( nIndex < static_cast<sal_Int32>( m_pRecentFilesItems->size() ) ) )
260 const RecentMenuEntry& rRecentFile = (*m_pRecentFilesItems)[ nIndex ];
261 int NUM_OF_PICKLIST_ARGS = 3;
262 Sequence< PropertyValue > aArgsList( NUM_OF_PICKLIST_ARGS );
264 aArgsList[0].Name = "Referer";
265 aArgsList[0].Value = makeAny( OUString( "private:user" ) );
267 // documents in the picklist will never be opened as templates
268 aArgsList[1].Name = "AsTemplate";
269 aArgsList[1].Value = makeAny( false );
271 ::rtl::OUString aFilter( rRecentFile.aFilter );
272 sal_Int32 nPos = aFilter.indexOf( '|' );
275 rtl::OUString aFilterOptions;
277 if ( nPos < ( aFilter.getLength() - 1 ) )
278 aFilterOptions = aFilter.copy( nPos+1 );
280 aArgsList[2].Name = "FilterOptions";
281 aArgsList[2].Value = makeAny( aFilterOptions );
283 aFilter = aFilter.copy( 0, nPos-1 );
284 aArgsList.realloc( ++NUM_OF_PICKLIST_ARGS );
287 aArgsList[NUM_OF_PICKLIST_ARGS-1].Name = "FilterName";
288 aArgsList[NUM_OF_PICKLIST_ARGS-1].Value = makeAny( aFilter );
290 ShutdownIcon::OpenURL( rRecentFile.aURL, OUString( "_default" ), aArgsList );
295 static RecentMenuDelegate* pRecentDelegate = nil;
297 static rtl::OUString getShortCut( const rtl::OUString& i_rTitle )
300 rtl::OUString aKeyEquiv;
301 for( sal_Int32 nIndex = 0; nIndex < i_rTitle.getLength(); nIndex++ )
303 rtl::OUString aShortcut( i_rTitle.copy( nIndex, 1 ).toAsciiLowerCase() );
304 if( aShortcuts.find( aShortcut ) == aShortcuts.end() )
306 aShortcuts.insert( aShortcut );
307 aKeyEquiv = aShortcut;
315 static void appendMenuItem( NSMenu* i_pMenu, NSMenu* i_pDockMenu, const rtl::OUString& i_rTitle, int i_nTag, const rtl::OUString& i_rKeyEquiv )
317 if( ! i_rTitle.getLength() )
320 NSMenuItem* pItem = [[NSMenuItem alloc] initWithTitle: getAutoreleasedString( i_rTitle )
321 action: @selector(executeMenuItem:)
322 keyEquivalent: (i_rKeyEquiv.getLength() ? getAutoreleasedString( i_rKeyEquiv ) : @"")
324 [pItem setTag: i_nTag];
325 [pItem setTarget: pExecute];
326 [pItem setEnabled: YES];
327 [i_pMenu addItem: pItem];
331 // create a similar entry in the dock menu
332 pItem = [[NSMenuItem alloc] initWithTitle: getAutoreleasedString( i_rTitle )
333 action: @selector(executeMenuItem:)
336 [pItem setTag: i_nTag];
337 [pItem setTarget: pExecute];
338 [pItem setEnabled: YES];
339 [i_pDockMenu addItem: pItem];
343 static void appendRecentMenu( NSMenu* i_pMenu, NSMenu* i_pDockMenu, const OUString& i_rTitle )
345 if( ! pRecentDelegate )
346 pRecentDelegate = [[RecentMenuDelegate alloc] init];
348 NSMenuItem* pItem = [i_pMenu addItemWithTitle: getAutoreleasedString( i_rTitle )
349 action: @selector(executeMenuItem:)
352 [pItem setEnabled: YES];
353 NSMenu* pRecentMenu = [[NSMenu alloc] initWithTitle: getAutoreleasedString( i_rTitle ) ];
355 // When compiling against 10.6 SDK, we get the warning:
356 // class 'RecentMenuDelegate' does not implement the 'NSMenuDelegate' protocol
358 // No idea if that is a bogus warning, or if the way this is
359 // implemented just is so weird that the compiler gets
360 // confused. Anyway, to avoid warnings, instead of this:
361 // [pRecentMenu setDelegate: pRecentDelegate];
363 objc_msgSend(pRecentMenu, @selector(setDelegate:), pRecentDelegate);
365 [pRecentMenu setAutoenablesItems: NO];
366 [pItem setSubmenu: pRecentMenu];
370 // create a similar entry in the dock menu
371 pItem = [i_pDockMenu addItemWithTitle: getAutoreleasedString( i_rTitle )
372 action: @selector(executeMenuItem:)
375 [pItem setEnabled: YES];
376 pRecentMenu = [[NSMenu alloc] initWithTitle: getAutoreleasedString( i_rTitle ) ];
379 // [pRecentMenu setDelegate: pRecentDelegate];
380 objc_msgSend(pRecentMenu, @selector(setDelegate:), pRecentDelegate);
382 [pRecentMenu setAutoenablesItems: NO];
383 [pItem setSubmenu: pRecentMenu];
391 void aqua_init_systray()
393 SolarMutexGuard aGuard;
395 ShutdownIcon *pShutdownIcon = ShutdownIcon::getInstance();
396 if( ! pShutdownIcon )
400 pShutdownIcon->SetVeto( true );
401 ShutdownIcon::addTerminateListener();
405 if( [NSApp respondsToSelector: @selector(addFallbackMenuItem:)] )
409 pExecute = [[QSMenuExecute alloc] init];
410 pDefMenu = [[NSMenuItem alloc] initWithTitle: getAutoreleasedString( pShutdownIcon->GetResString( STR_QUICKSTART_FILE ) ) action: NULL keyEquivalent: @""];
411 pDockSubMenu = [[NSMenuItem alloc] initWithTitle: getAutoreleasedString( pShutdownIcon->GetResString( STR_QUICKSTART_FILE ) ) action: NULL keyEquivalent: @""];
412 NSMenu* pMenu = [[NSMenu alloc] initWithTitle: getAutoreleasedString( pShutdownIcon->GetResString( STR_QUICKSTART_FILE ) )];
413 [pMenu setAutoenablesItems: NO];
414 NSMenu* pDockMenu = [[NSMenu alloc] initWithTitle: getAutoreleasedString( pShutdownIcon->GetResString( STR_QUICKSTART_FILE ) )];
415 [pDockMenu setAutoenablesItems: NO];
417 // collect the URLs of the entries in the File/New menu
418 SvtModuleOptions aModuleOptions;
419 std::set< rtl::OUString > aFileNewAppsAvailable;
420 SvtDynamicMenuOptions aOpt;
421 Sequence < Sequence < PropertyValue > > aNewMenu = aOpt.GetMenu( E_NEWMENU );
422 const rtl::OUString sURLKey( "URL" );
424 const Sequence< PropertyValue >* pNewMenu = aNewMenu.getConstArray();
425 const Sequence< PropertyValue >* pNewMenuEnd = aNewMenu.getConstArray() + aNewMenu.getLength();
426 for ( ; pNewMenu != pNewMenuEnd; ++pNewMenu )
428 comphelper::SequenceAsHashMap aEntryItems( *pNewMenu );
429 rtl::OUString sURL( aEntryItems.getUnpackedValueOrDefault( sURLKey, rtl::OUString() ) );
430 if ( sURL.getLength() )
431 aFileNewAppsAvailable.insert( sURL );
434 // describe the menu entries for launching the applications
435 struct MenuEntryDescriptor
437 SvtModuleOptions::EModule eModuleIdentifier;
439 const char* pAsciiURLDescription;
442 { SvtModuleOptions::EModule::WRITER, MI_WRITER, WRITER_URL },
443 { SvtModuleOptions::EModule::CALC, MI_CALC, CALC_URL },
444 { SvtModuleOptions::EModule::IMPRESS, MI_IMPRESS, IMPRESS_WIZARD_URL },
445 { SvtModuleOptions::EModule::DRAW, MI_DRAW, DRAW_URL },
446 { SvtModuleOptions::EModule::DATABASE, MI_BASE, BASE_URL },
447 { SvtModuleOptions::EModule::MATH, MI_MATH, MATH_URL }
450 // insert entry for startcenter
451 if( aModuleOptions.IsModuleInstalled( SvtModuleOptions::EModule::STARTMODULE ) )
453 appendMenuItem( pMenu, nil, pShutdownIcon->GetResString( STR_QUICKSTART_STARTCENTER ), MI_STARTMODULE, OUString( "n" ) );
454 if( [NSApp respondsToSelector: @selector(setDockIconClickHandler:)] )
455 [NSApp performSelector:@selector(setDockIconClickHandler:) withObject: pExecute];
457 OSL_FAIL( "setDockIconClickHandler selector failed on NSApp\n" );
461 // insert the menu entries for launching the applications
462 for ( size_t i = 0; i < SAL_N_ELEMENTS( aMenuItems ); ++i )
464 if ( !aModuleOptions.IsModuleInstalled( aMenuItems[i].eModuleIdentifier ) )
465 // the complete application is not even installed
468 rtl::OUString sURL( ::rtl::OUString::createFromAscii( aMenuItems[i].pAsciiURLDescription ) );
470 if ( aFileNewAppsAvailable.find( sURL ) == aFileNewAppsAvailable.end() )
471 // the application is installed, but the entry has been configured to *not* appear in the File/New
472 // menu => also let not appear it in the quickstarter
475 rtl::OUString aKeyEquiv( getShortCut( ShutdownIcon::GetUrlDescription( sURL ) ) );
477 appendMenuItem( pMenu, pDockMenu, ShutdownIcon::GetUrlDescription( sURL ), aMenuItems[i].nMenuTag, aKeyEquiv );
480 // insert the remaining menu entries
483 appendRecentMenu( pMenu, pDockMenu, pShutdownIcon->GetResString( STR_QUICKSTART_RECENTDOC ) );
485 rtl::OUString aTitle( pShutdownIcon->GetResString( STR_QUICKSTART_FROMTEMPLATE ) );
486 rtl::OUString aKeyEquiv( getShortCut( aTitle ) );
487 appendMenuItem( pMenu, pDockMenu, aTitle, MI_TEMPLATE, aKeyEquiv );
488 aTitle = pShutdownIcon->GetResString( STR_QUICKSTART_FILEOPEN );
489 aKeyEquiv = getShortCut( aTitle );
490 appendMenuItem( pMenu, pDockMenu, aTitle, MI_OPEN, aKeyEquiv );
492 [pDefMenu setSubmenu: pMenu];
493 [NSApp performSelector:@selector(addFallbackMenuItem:) withObject: pDefMenu];
495 if( [NSApp respondsToSelector: @selector(addDockMenuItem:)] )
497 [pDockSubMenu setSubmenu: pDockMenu];
498 // insert a separator to the dock menu
499 [NSApp performSelector:@selector(addDockMenuItem:) withObject: [NSMenuItem separatorItem]];
500 // and now add the submenu
501 [NSApp performSelector:@selector(addDockMenuItem:) withObject: pDockSubMenu];
504 OSL_FAIL( "addDockMenuItem selector failed on NSApp\n" );
507 OSL_FAIL( "addFallbackMenuItem selector failed on NSApp\n" );
511 void SAL_DLLPUBLIC_EXPORT aqua_shutdown_systray()
517 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */