1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_dbaccess.hxx"
26 
27 #ifndef DBACCESS_SOURCE_UI_INC_OPENDOCCONTROLS_HXX
28 #include "opendoccontrols.hxx"
29 #endif
30 
31 /** === begin UNO includes === **/
32 #ifndef _COM_SUN_STAR_UNO_SEQUENCE_HXX_
33 #include <com/sun/star/uno/Sequence.hxx>
34 #endif
35 #ifndef _COM_SUN_STAR_BEANS_PROPERTYVALUE_HPP_
36 #include <com/sun/star/beans/PropertyValue.hpp>
37 #endif
38 #ifndef _COM_SUN_STAR_LANG_XMULTISERVICEFACTORY_HPP_
39 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
40 #endif
41 #ifndef _COM_SUN_STAR_CONTAINER_XNAMEACCESS_HPP_
42 #include <com/sun/star/container/XNameAccess.hpp>
43 #endif
44 #ifndef _COM_SUN_STAR_UI_XMODULEUICONFIGURATIONMANAGERSUPPLIER_HPP_
45 #include <com/sun/star/ui/XModuleUIConfigurationManagerSupplier.hpp>
46 #endif
47 #ifndef _COM_SUN_STAR_UI_XUICONFIGURATIONMANAGER_HPP_
48 #include <com/sun/star/ui/XUIConfigurationManager.hpp>
49 #endif
50 #ifndef _COM_SUN_STAR_GRAPHIC_XGRAPHIC_HPP_
51 #include <com/sun/star/graphic/XGraphic.hpp>
52 #endif
53 #ifndef _COM_SUN_STAR_UI_XIMAGEMANAGER_HPP_
54 #include <com/sun/star/ui/XImageManager.hpp>
55 #endif
56 /** === end UNO includes === **/
57 
58 #ifndef _COMPHELPER_PROCESSFACTORY_HXX_
59 #include <comphelper/processfactory.hxx>
60 #endif
61 #ifndef _SV_GRAPH_HXX
62 #include <vcl/graph.hxx>
63 #endif
64 #ifndef _SV_HELP_HXX
65 #include <vcl/help.hxx>
66 #endif
67 #ifndef INCLUDED_SVTOOLS_HISTORYOPTIONS_HXX
68 #include <unotools/historyoptions.hxx>
69 #endif
70 #ifndef _COMPHELPER_SEQUENCEASHASHMAP_HXX_
71 #include <comphelper/sequenceashashmap.hxx>
72 #endif
73 #ifndef _URLOBJ_HXX
74 #include <tools/urlobj.hxx>
75 #endif
76 #ifndef SVTOOLS_FILENOTATION_HXX
77 #include <svl/filenotation.hxx>
78 #endif
79 
80 //........................................................................
81 namespace dbaui
82 {
83 //........................................................................
84 
85     namespace
86     {
87         using ::com::sun::star::uno::Reference;
88         using ::com::sun::star::uno::Exception;
89         using ::com::sun::star::uno::Sequence;
90         using ::com::sun::star::uno::UNO_QUERY_THROW;
91         using ::com::sun::star::container::XNameAccess;
92         using ::com::sun::star::lang::XMultiServiceFactory;
93         using ::com::sun::star::beans::PropertyValue;
94         using ::com::sun::star::ui::XModuleUIConfigurationManagerSupplier;
95         using ::com::sun::star::ui::XUIConfigurationManager;
96         using ::com::sun::star::ui::XImageManager;
97         using ::com::sun::star::graphic::XGraphic;
98 
99         String GetCommandText( const sal_Char* _pCommandURL, const ::rtl::OUString& _rModuleName )
100         {
101             ::rtl::OUString sLabel;
102             if ( !_pCommandURL || !*_pCommandURL )
103                 return sLabel;
104 
105             Reference< XNameAccess > xUICommandLabels;
106             ::rtl::OUString sCommandURL = ::rtl::OUString::createFromAscii( _pCommandURL );
107 
108             try
109             {
110                 do
111                 {
112                     // Retrieve popup menu labels
113                     Reference< XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory() );
114                     if ( !xFactory.is() )
115                         break;
116 
117                     Reference< XNameAccess> xNameAccess;
118                     xNameAccess = xNameAccess.query( xFactory->createInstance(
119                         ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.UICommandDescription" ) )
120                     ) );
121                     if ( !xNameAccess.is() )
122                         break;
123 
124                     xNameAccess->getByName( _rModuleName ) >>= xUICommandLabels;
125                     if ( !xUICommandLabels.is() )
126                         break;
127 
128                     Sequence< PropertyValue > aProperties;
129                     if ( !( xUICommandLabels->getByName(sCommandURL) >>= aProperties ) )
130                         break;
131 
132                     sal_Int32 nCount( aProperties.getLength() );
133                     for ( sal_Int32 i=0; i<nCount; ++i )
134                     {
135                         ::rtl::OUString sPropertyName( aProperties[i].Name );
136                         if ( sPropertyName.equalsAscii("Label" ) )
137                         {
138                             aProperties[i].Value >>= sLabel;
139                             break;
140                         }
141                     }
142                 }
143                 while ( false );
144             }
145             catch( Exception& rException )
146             {
147                 (void)rException;
148             }
149 
150             return sLabel;
151         }
152 
153         Image GetCommandIcon( const sal_Char* _pCommandURL, const ::rtl::OUString& _rModuleName )
154         {
155             Image aIcon;
156             if ( !_pCommandURL || !*_pCommandURL )
157                 return aIcon;
158 
159             Reference< XNameAccess > xUICommandLabels;
160             ::rtl::OUString sCommandURL = ::rtl::OUString::createFromAscii( _pCommandURL );
161             try
162             {
163                 do
164                 {
165                     // Retrieve popup menu labels
166                     Reference< XMultiServiceFactory> xFactory( ::comphelper::getProcessServiceFactory() );
167                     if ( !xFactory.is() )
168                         break;
169 
170                     Reference< XModuleUIConfigurationManagerSupplier > xSupplier(
171                         xFactory->createInstance( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
172                             "com.sun.star.ui.ModuleUIConfigurationManagerSupplier" ) ) ),
173                         UNO_QUERY_THROW );
174 
175                     Reference< XUIConfigurationManager > xManager( xSupplier->getUIConfigurationManager( _rModuleName ) );
176                     Reference< XImageManager > xImageManager;
177                     if ( xManager.is() )
178                         xImageManager = xImageManager.query( xManager->getImageManager() );
179                     if ( !xImageManager.is() )
180                         break;
181 
182                     Sequence< ::rtl::OUString > aCommandList( &sCommandURL, 1 );
183                     Sequence<Reference< XGraphic> > xIconList( xImageManager->getImages( 0, aCommandList ) );
184                     if ( !xIconList.hasElements() )
185                         break;
186 
187                     aIcon = Graphic( xIconList[0] ).GetBitmapEx();
188                 }
189                 while ( false );
190             }
191             catch ( Exception& rException )
192             {
193                 (void)rException;
194             }
195 
196             return aIcon;
197         }
198 
199 
200     }
201 
202 	//====================================================================
203 	//= OpenButton
204 	//====================================================================
205 	//--------------------------------------------------------------------
206     OpenDocumentButton::OpenDocumentButton( Window* _pParent, const sal_Char* _pAsciiModuleName, const ResId& _rResId )
207         :PushButton( _pParent, _rResId )
208     {
209         impl_init( _pAsciiModuleName );
210     }
211 
212     //--------------------------------------------------------------------
213     void OpenDocumentButton::impl_init( const sal_Char* _pAsciiModuleName )
214     {
215         DBG_ASSERT( _pAsciiModuleName, "OpenDocumentButton::impl_init: invalid module name!" );
216         m_sModule = ::rtl::OUString::createFromAscii( _pAsciiModuleName );
217 
218         // our label should equal the UI text of the "Open" command
219         String sLabel( GetCommandText( ".uno:Open", m_sModule ) );
220         sLabel.SearchAndReplaceAllAscii( "~", String() );
221         sLabel.Insert( (sal_Unicode)' ', 0 );
222         SetText( sLabel );
223 
224         // Place icon left of text and both centered in the button.
225         SetModeImage( GetCommandIcon( ".uno:Open", m_sModule ), BMP_COLOR_NORMAL );
226         EnableImageDisplay( sal_True );
227         EnableTextDisplay( sal_True );
228         SetImageAlign( IMAGEALIGN_LEFT );
229         SetStyle( GetStyle() | WB_CENTER );
230     }
231 
232 	//====================================================================
233 	//= OpenDocumentListBox
234 	//====================================================================
235     //--------------------------------------------------------------------
236     OpenDocumentListBox::OpenDocumentListBox( Window* _pParent, const sal_Char* _pAsciiModuleName, const ResId& _rResId )
237         :ListBox( _pParent, _rResId )
238     {
239         impl_init( _pAsciiModuleName );
240     }
241 
242     //--------------------------------------------------------------------
243     void OpenDocumentListBox::impl_init( const sal_Char* _pAsciiModuleName )
244     {
245         DBG_ASSERT( _pAsciiModuleName, "OpenDocumentListBox::impl_init: invalid module name!" );
246 
247         Sequence< Sequence< PropertyValue> > aHistory = SvtHistoryOptions().GetList( ePICKLIST );
248         Reference< XNameAccess > xFilterFactory;
249         xFilterFactory = xFilterFactory.query( ::comphelper::getProcessServiceFactory()->createInstance(
250             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.FilterFactory" ) ) ) );
251 
252         sal_uInt32 nCount = aHistory.getLength();
253         for ( sal_uInt32 nItem = 0; nItem < nCount; ++nItem )
254         {
255             try
256             {
257                 //  Get the current history item's properties.
258                 ::comphelper::SequenceAsHashMap aItemProperties( aHistory[ nItem ] );
259                 ::rtl::OUString sURL = aItemProperties.getUnpackedValueOrDefault( HISTORY_PROPERTYNAME_URL, ::rtl::OUString() );
260                 ::rtl::OUString sFilter = aItemProperties.getUnpackedValueOrDefault( HISTORY_PROPERTYNAME_FILTER, ::rtl::OUString() );
261                 String          sTitle = aItemProperties.getUnpackedValueOrDefault( HISTORY_PROPERTYNAME_TITLE, ::rtl::OUString() );
262                 ::rtl::OUString sPassword = aItemProperties.getUnpackedValueOrDefault( HISTORY_PROPERTYNAME_PASSWORD, ::rtl::OUString() );
263 
264                 //  If the entry is an impress file then insert it into the
265                 //  history list and the list box.
266                 Sequence< PropertyValue > aProps;
267                 xFilterFactory->getByName( sFilter ) >>= aProps;
268 
269                 ::comphelper::SequenceAsHashMap aFilterProperties( aProps );
270                 ::rtl::OUString sDocumentService = aFilterProperties.getUnpackedValueOrDefault(
271                     ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DocumentService" ) ), ::rtl::OUString() );
272                 if ( sDocumentService.equalsAscii( _pAsciiModuleName ) )
273                 {
274                     // yes, it's a Base document
275                     INetURLObject aURL;
276                     aURL.SetSmartURL( sURL );
277                     // The password is set only when it is not empty.
278                     if ( sPassword.getLength() > 0 )
279                         aURL.SetPass( sPassword );
280 
281                     if ( !sTitle.Len() )
282                         sTitle = aURL.getBase( INetURLObject::LAST_SEGMENT, true, INetURLObject::DECODE_UNAMBIGUOUS );
283 
284                     String sDecodedURL = aURL.GetMainURL( INetURLObject::NO_DECODE );
285 
286                     sal_uInt16 nPos = InsertEntry( sTitle );
287                     m_aURLs.insert( MapIndexToStringPair::value_type( nPos, StringPair( sDecodedURL, sFilter ) ) );
288                 }
289             }
290             catch( Exception& rException )
291             {
292                 (void)rException;
293             }
294         }
295     }
296 
297     //--------------------------------------------------------------------
298     String OpenDocumentListBox::GetSelectedDocumentURL() const
299     {
300         String sURL;
301         sal_uInt16 nSelected = GetSelectEntryPos();
302         if ( LISTBOX_ENTRY_NOTFOUND != GetSelectEntryPos() )
303             sURL = impl_getDocumentAtIndex( nSelected ).first;
304         return sURL;
305     }
306 
307     //--------------------------------------------------------------------
308     String OpenDocumentListBox::GetSelectedDocumentFilter() const
309     {
310         String sFilter;
311         sal_uInt16 nSelected = GetSelectEntryPos();
312         if ( LISTBOX_ENTRY_NOTFOUND != GetSelectEntryPos() )
313             sFilter = impl_getDocumentAtIndex( nSelected ).second;
314         return sFilter;
315     }
316 
317     //--------------------------------------------------------------------
318     OpenDocumentListBox::StringPair OpenDocumentListBox::impl_getDocumentAtIndex( sal_uInt16 _nListIndex, bool _bSystemNotation ) const
319     {
320         MapIndexToStringPair::const_iterator pos = m_aURLs.find( _nListIndex );
321         DBG_ASSERT( pos != m_aURLs.end(), "OpenDocumentListBox::impl_getDocumentAtIndex: invalid index!" );
322 
323         StringPair aDocumentDescriptor;
324         if ( pos != m_aURLs.end() )
325         {
326             aDocumentDescriptor = pos->second;
327             if ( _bSystemNotation && aDocumentDescriptor.first.Len() )
328             {
329                 ::svt::OFileNotation aNotation( aDocumentDescriptor.first );
330                 aDocumentDescriptor.first = aNotation.get( ::svt::OFileNotation::N_SYSTEM );
331             }
332         }
333         return aDocumentDescriptor;
334     }
335 
336     //--------------------------------------------------------------------
337     void  OpenDocumentListBox::RequestHelp( const HelpEvent& _rHEvt )
338     {
339 	    if( !( _rHEvt.GetMode() & HELPMODE_QUICK ) )
340             return;
341         if ( !IsEnabled() )
342             return;
343 
344         Point aRequestPos( ScreenToOutputPixel( _rHEvt.GetMousePosPixel() ) );
345         sal_uInt16 nItemIndex = LISTBOX_ENTRY_NOTFOUND;
346         if ( GetIndexForPoint( aRequestPos, nItemIndex ) != -1 )
347         {
348             Rectangle aItemRect( GetBoundingRectangle( nItemIndex ) );
349             aItemRect = Rectangle(
350                 OutputToScreenPixel( aItemRect.TopLeft() ),
351                 OutputToScreenPixel( aItemRect.BottomRight() ) );
352             String sHelpText = impl_getDocumentAtIndex( nItemIndex, true ).first;
353             Help::ShowQuickHelp( this, aItemRect, sHelpText, QUICKHELP_LEFT | QUICKHELP_VCENTER );
354         }
355     }
356 
357 //........................................................................
358 } // namespace dbaui
359 //........................................................................
360