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 :
21 : #include "opendoccontrols.hxx"
22 :
23 : #include <com/sun/star/uno/Sequence.hxx>
24 : #include <com/sun/star/beans/PropertyValue.hpp>
25 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
26 : #include <com/sun/star/container/XNameAccess.hpp>
27 : #include <com/sun/star/ui/ModuleUIConfigurationManagerSupplier.hpp>
28 : #include <com/sun/star/frame/UICommandDescription.hpp>
29 : #include <com/sun/star/ui/XUIConfigurationManager.hpp>
30 : #include <com/sun/star/graphic/XGraphic.hpp>
31 : #include <com/sun/star/ui/XImageManager.hpp>
32 :
33 : #include <comphelper/processfactory.hxx>
34 : #include <vcl/graph.hxx>
35 : #include <vcl/help.hxx>
36 : #include <unotools/historyoptions.hxx>
37 : #include <comphelper/sequenceashashmap.hxx>
38 : #include <tools/urlobj.hxx>
39 : #include <svl/filenotation.hxx>
40 : #include <osl/diagnose.h>
41 : #include <vcl/builder.hxx>
42 :
43 : //........................................................................
44 : namespace dbaui
45 : {
46 : //........................................................................
47 :
48 : namespace
49 : {
50 : using ::com::sun::star::uno::Reference;
51 : using ::com::sun::star::uno::Exception;
52 : using ::com::sun::star::uno::Sequence;
53 : using ::com::sun::star::uno::UNO_QUERY_THROW;
54 : using ::com::sun::star::uno::XComponentContext;
55 : using ::com::sun::star::container::XNameAccess;
56 : using ::com::sun::star::lang::XMultiServiceFactory;
57 : using ::com::sun::star::beans::PropertyValue;
58 : using ::com::sun::star::ui::ModuleUIConfigurationManagerSupplier;
59 : using ::com::sun::star::ui::XModuleUIConfigurationManagerSupplier;
60 : using ::com::sun::star::ui::XUIConfigurationManager;
61 : using ::com::sun::star::ui::XImageManager;
62 : using ::com::sun::star::frame::UICommandDescription;
63 : using ::com::sun::star::graphic::XGraphic;
64 :
65 0 : String GetCommandText( const sal_Char* _pCommandURL, const OUString& _rModuleName )
66 : {
67 0 : OUString sLabel;
68 0 : if ( !_pCommandURL || !*_pCommandURL )
69 0 : return sLabel;
70 :
71 0 : Reference< XNameAccess > xUICommandLabels;
72 0 : OUString sCommandURL = OUString::createFromAscii( _pCommandURL );
73 :
74 : try
75 : {
76 : do
77 : {
78 : // Retrieve popup menu labels
79 0 : Reference< XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
80 0 : if ( !xContext.is() )
81 0 : break;
82 :
83 0 : Reference< XNameAccess> xNameAccess( UICommandDescription::create(xContext) );
84 :
85 0 : xNameAccess->getByName( _rModuleName ) >>= xUICommandLabels;
86 0 : if ( !xUICommandLabels.is() )
87 0 : break;
88 :
89 0 : Sequence< PropertyValue > aProperties;
90 0 : if ( !( xUICommandLabels->getByName(sCommandURL) >>= aProperties ) )
91 0 : break;
92 :
93 0 : sal_Int32 nCount( aProperties.getLength() );
94 0 : for ( sal_Int32 i=0; i<nCount; ++i )
95 : {
96 0 : OUString sPropertyName( aProperties[i].Name );
97 0 : if ( sPropertyName == "Label" )
98 : {
99 0 : aProperties[i].Value >>= sLabel;
100 0 : break;
101 : }
102 0 : }
103 : }
104 : while ( false );
105 : }
106 0 : catch( Exception& rException )
107 : {
108 : (void)rException;
109 : }
110 :
111 0 : return sLabel;
112 : }
113 :
114 0 : Image GetCommandIcon( const sal_Char* _pCommandURL, const OUString& _rModuleName )
115 : {
116 0 : Image aIcon;
117 0 : if ( !_pCommandURL || !*_pCommandURL )
118 0 : return aIcon;
119 :
120 0 : Reference< XNameAccess > xUICommandLabels;
121 0 : OUString sCommandURL = OUString::createFromAscii( _pCommandURL );
122 : try
123 : {
124 : do
125 : {
126 : // Retrieve popup menu labels
127 0 : Reference< com::sun::star::uno::XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
128 0 : if ( !xContext.is() )
129 0 : break;
130 :
131 : Reference< XModuleUIConfigurationManagerSupplier > xSupplier(
132 0 : ModuleUIConfigurationManagerSupplier::create(xContext) );
133 :
134 0 : Reference< XUIConfigurationManager > xManager( xSupplier->getUIConfigurationManager( _rModuleName ) );
135 0 : Reference< XImageManager > xImageManager;
136 0 : if ( xManager.is() )
137 0 : xImageManager = xImageManager.query( xManager->getImageManager() );
138 0 : if ( !xImageManager.is() )
139 0 : break;
140 :
141 0 : Sequence< OUString > aCommandList( &sCommandURL, 1 );
142 0 : Sequence<Reference< XGraphic> > xIconList( xImageManager->getImages( 0, aCommandList ) );
143 0 : if ( !xIconList.hasElements() )
144 0 : break;
145 :
146 0 : aIcon = Graphic( xIconList[0] ).GetBitmapEx();
147 : }
148 : while ( false );
149 : }
150 0 : catch ( Exception& rException )
151 : {
152 : (void)rException;
153 : }
154 :
155 0 : return aIcon;
156 : }
157 :
158 :
159 : }
160 :
161 : //====================================================================
162 : //= OpenButton
163 : //====================================================================
164 :
165 : //--------------------------------------------------------------------
166 0 : OpenDocumentButton::OpenDocumentButton( Window* _pParent, const sal_Char* _pAsciiModuleName )
167 0 : :PushButton( _pParent )
168 : {
169 0 : impl_init( _pAsciiModuleName );
170 0 : }
171 :
172 : //--------------------------------------------------------------------
173 0 : extern "C" SAL_DLLPUBLIC_EXPORT Window* SAL_CALL makeOpenDocumentButton( Window *pParent, VclBuilder::stringmap & )
174 : {
175 0 : return new OpenDocumentButton( pParent, "com.sun.star.sdb.OfficeDatabaseDocument" );
176 : }
177 :
178 : //--------------------------------------------------------------------
179 0 : void OpenDocumentButton::impl_init( const sal_Char* _pAsciiModuleName )
180 : {
181 : OSL_ENSURE( _pAsciiModuleName, "OpenDocumentButton::impl_init: invalid module name!" );
182 0 : m_sModule = OUString::createFromAscii( _pAsciiModuleName );
183 :
184 : // our label should equal the UI text of the "Open" command
185 0 : String sLabel( GetCommandText( ".uno:Open", m_sModule ) );
186 0 : sLabel.SearchAndReplaceAllAscii( "~", String() );
187 0 : sLabel.Insert( (sal_Unicode)' ', 0 );
188 0 : SetText( sLabel );
189 :
190 : // Place icon left of text and both centered in the button.
191 0 : SetModeImage( GetCommandIcon( ".uno:Open", m_sModule ) );
192 0 : EnableImageDisplay( sal_True );
193 0 : EnableTextDisplay( sal_True );
194 0 : SetImageAlign( IMAGEALIGN_LEFT );
195 0 : SetStyle( GetStyle() | WB_CENTER );
196 0 : }
197 :
198 : //====================================================================
199 : //= OpenDocumentListBox
200 : //====================================================================
201 :
202 : //--------------------------------------------------------------------
203 0 : OpenDocumentListBox::OpenDocumentListBox( Window* _pParent, const sal_Char* _pAsciiModuleName )
204 0 : :ListBox( _pParent, WB_BORDER | WB_DROPDOWN )
205 : {
206 0 : impl_init( _pAsciiModuleName );
207 0 : }
208 :
209 : //--------------------------------------------------------------------
210 0 : extern "C" SAL_DLLPUBLIC_EXPORT Window* SAL_CALL makeOpenDocumentListBox( Window *pParent, VclBuilder::stringmap & )
211 : {
212 0 : return new OpenDocumentListBox( pParent, "com.sun.star.sdb.OfficeDatabaseDocument" );
213 : }
214 :
215 : //--------------------------------------------------------------------
216 0 : void OpenDocumentListBox::impl_init( const sal_Char* _pAsciiModuleName )
217 : {
218 : OSL_ENSURE( _pAsciiModuleName, "OpenDocumentListBox::impl_init: invalid module name!" );
219 :
220 0 : Sequence< Sequence< PropertyValue> > aHistory = SvtHistoryOptions().GetList( ePICKLIST );
221 0 : Reference< XNameAccess > xFilterFactory;
222 0 : xFilterFactory = xFilterFactory.query( ::comphelper::getProcessServiceFactory()->createInstance(
223 0 : OUString( "com.sun.star.document.FilterFactory" ) ) );
224 :
225 0 : sal_uInt32 nCount = aHistory.getLength();
226 0 : for ( sal_uInt32 nItem = 0; nItem < nCount; ++nItem )
227 : {
228 : try
229 : {
230 : // Get the current history item's properties.
231 0 : ::comphelper::SequenceAsHashMap aItemProperties( aHistory[ nItem ] );
232 0 : OUString sURL = aItemProperties.getUnpackedValueOrDefault( HISTORY_PROPERTYNAME_URL, OUString() );
233 0 : OUString sFilter = aItemProperties.getUnpackedValueOrDefault( HISTORY_PROPERTYNAME_FILTER, OUString() );
234 0 : String sTitle = aItemProperties.getUnpackedValueOrDefault( HISTORY_PROPERTYNAME_TITLE, OUString() );
235 0 : OUString sPassword = aItemProperties.getUnpackedValueOrDefault( HISTORY_PROPERTYNAME_PASSWORD, OUString() );
236 :
237 : // If the entry is an impress file then insert it into the
238 : // history list and the list box.
239 0 : Sequence< PropertyValue > aProps;
240 0 : xFilterFactory->getByName( sFilter ) >>= aProps;
241 :
242 0 : ::comphelper::SequenceAsHashMap aFilterProperties( aProps );
243 : OUString sDocumentService = aFilterProperties.getUnpackedValueOrDefault(
244 0 : OUString( "DocumentService" ), OUString() );
245 0 : if ( sDocumentService.equalsAscii( _pAsciiModuleName ) )
246 : {
247 : // yes, it's a Base document
248 0 : INetURLObject aURL;
249 0 : aURL.SetSmartURL( sURL );
250 : // The password is set only when it is not empty.
251 0 : if ( !sPassword.isEmpty() )
252 0 : aURL.SetPass( sPassword );
253 :
254 0 : if ( !sTitle.Len() )
255 0 : sTitle = aURL.getBase( INetURLObject::LAST_SEGMENT, true, INetURLObject::DECODE_UNAMBIGUOUS );
256 :
257 0 : String sDecodedURL = aURL.GetMainURL( INetURLObject::NO_DECODE );
258 :
259 0 : sal_uInt16 nPos = InsertEntry( sTitle );
260 0 : m_aURLs.insert( MapIndexToStringPair::value_type( nPos, StringPair( sDecodedURL, sFilter ) ) );
261 0 : }
262 : }
263 0 : catch( Exception& rException )
264 : {
265 : (void)rException;
266 : }
267 0 : }
268 0 : }
269 :
270 : //--------------------------------------------------------------------
271 0 : String OpenDocumentListBox::GetSelectedDocumentURL() const
272 : {
273 0 : String sURL;
274 0 : sal_uInt16 nSelected = GetSelectEntryPos();
275 0 : if ( LISTBOX_ENTRY_NOTFOUND != GetSelectEntryPos() )
276 0 : sURL = impl_getDocumentAtIndex( nSelected ).first;
277 0 : return sURL;
278 : }
279 :
280 : //--------------------------------------------------------------------
281 0 : String OpenDocumentListBox::GetSelectedDocumentFilter() const
282 : {
283 0 : String sFilter;
284 0 : sal_uInt16 nSelected = GetSelectEntryPos();
285 0 : if ( LISTBOX_ENTRY_NOTFOUND != GetSelectEntryPos() )
286 0 : sFilter = impl_getDocumentAtIndex( nSelected ).second;
287 0 : return sFilter;
288 : }
289 :
290 : //--------------------------------------------------------------------
291 0 : OpenDocumentListBox::StringPair OpenDocumentListBox::impl_getDocumentAtIndex( sal_uInt16 _nListIndex, bool _bSystemNotation ) const
292 : {
293 0 : MapIndexToStringPair::const_iterator pos = m_aURLs.find( _nListIndex );
294 : OSL_ENSURE( pos != m_aURLs.end(), "OpenDocumentListBox::impl_getDocumentAtIndex: invalid index!" );
295 :
296 0 : StringPair aDocumentDescriptor;
297 0 : if ( pos != m_aURLs.end() )
298 : {
299 0 : aDocumentDescriptor = pos->second;
300 0 : if ( _bSystemNotation && aDocumentDescriptor.first.Len() )
301 : {
302 0 : ::svt::OFileNotation aNotation( aDocumentDescriptor.first );
303 0 : aDocumentDescriptor.first = aNotation.get( ::svt::OFileNotation::N_SYSTEM );
304 : }
305 : }
306 0 : return aDocumentDescriptor;
307 : }
308 :
309 : //--------------------------------------------------------------------
310 0 : void OpenDocumentListBox::RequestHelp( const HelpEvent& _rHEvt )
311 : {
312 0 : if( !( _rHEvt.GetMode() & HELPMODE_QUICK ) )
313 0 : return;
314 0 : if ( !IsEnabled() )
315 0 : return;
316 :
317 0 : Point aRequestPos( ScreenToOutputPixel( _rHEvt.GetMousePosPixel() ) );
318 0 : sal_uInt16 nItemIndex = LISTBOX_ENTRY_NOTFOUND;
319 0 : if ( GetIndexForPoint( aRequestPos, nItemIndex ) != -1 )
320 : {
321 0 : Rectangle aItemRect( GetBoundingRectangle( nItemIndex ) );
322 : aItemRect = Rectangle(
323 0 : OutputToScreenPixel( aItemRect.TopLeft() ),
324 0 : OutputToScreenPixel( aItemRect.BottomRight() ) );
325 0 : String sHelpText = impl_getDocumentAtIndex( nItemIndex, true ).first;
326 0 : Help::ShowQuickHelp( this, aItemRect, sHelpText, QUICKHELP_LEFT | QUICKHELP_VCENTER );
327 : }
328 : }
329 :
330 : //........................................................................
331 12 : } // namespace dbaui
332 : //........................................................................
333 :
334 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|