xref: /aoo41x/main/sfx2/source/appl/opengrf.cxx (revision d119d52d)
1*d119d52dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*d119d52dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*d119d52dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*d119d52dSAndrew Rist  * distributed with this work for additional information
6*d119d52dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*d119d52dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*d119d52dSAndrew Rist  * "License"); you may not use this file except in compliance
9*d119d52dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*d119d52dSAndrew Rist  *
11*d119d52dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*d119d52dSAndrew Rist  *
13*d119d52dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*d119d52dSAndrew Rist  * software distributed under the License is distributed on an
15*d119d52dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*d119d52dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*d119d52dSAndrew Rist  * specific language governing permissions and limitations
18*d119d52dSAndrew Rist  * under the License.
19*d119d52dSAndrew Rist  *
20*d119d52dSAndrew Rist  *************************************************************/
21*d119d52dSAndrew Rist 
22*d119d52dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sfx2.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <tools/urlobj.hxx>
28cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
29cdf0e10cSrcweir #include <com/sun/star/uno/Reference.h>
30cdf0e10cSrcweir #include <com/sun/star/lang/XInitialization.hpp>
31cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/CommonFilePickerElementIds.hpp>
32cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
33cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
34cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/FilePreviewImageFormats.hpp>
35cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/ListboxControlActions.hpp>
36cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
37cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
38cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/XFilePicker.hpp>
39cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/XFilePickerListener.hpp>
40cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/XFilePickerNotifier.hpp>
41cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/XFilePreview.hpp>
42cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/XFilterManager.hpp>
43cdf0e10cSrcweir #include <svl/urihelper.hxx>
44cdf0e10cSrcweir #include <unotools/ucbstreamhelper.hxx>
45cdf0e10cSrcweir #include <svtools/transfer.hxx>
46cdf0e10cSrcweir #include <sot/formats.hxx>
47cdf0e10cSrcweir #include <vcl/msgbox.hxx>
48cdf0e10cSrcweir #include <sfx2/filedlghelper.hxx>
49cdf0e10cSrcweir #include <sfx2/docfile.hxx>
50cdf0e10cSrcweir #include <unotools/pathoptions.hxx>
51cdf0e10cSrcweir #include <sfx2/opengrf.hxx>
52cdf0e10cSrcweir #include "app.hrc"
53cdf0e10cSrcweir #include "sfx2/sfxresid.hxx"
54cdf0e10cSrcweir 
55cdf0e10cSrcweir //-----------------------------------------------------------------------------
56cdf0e10cSrcweir 
57cdf0e10cSrcweir using namespace ::com::sun::star;
58cdf0e10cSrcweir using namespace ::com::sun::star::lang;
59cdf0e10cSrcweir using namespace ::com::sun::star::ui::dialogs;
60cdf0e10cSrcweir using namespace ::com::sun::star::uno;
61cdf0e10cSrcweir using namespace ::rtl;
62cdf0e10cSrcweir using namespace ::cppu;
63cdf0e10cSrcweir 
64cdf0e10cSrcweir 
65cdf0e10cSrcweir //-----------------------------------------------------------------------------
66cdf0e10cSrcweir 
SvxOpenGrfErr2ResId(short err)67cdf0e10cSrcweir sal_uInt16	SvxOpenGrfErr2ResId(	short	err		)
68cdf0e10cSrcweir {
69cdf0e10cSrcweir 	switch( err )
70cdf0e10cSrcweir 	{
71cdf0e10cSrcweir 		case GRFILTER_OPENERROR:
72cdf0e10cSrcweir 			return RID_SVXSTR_GRFILTER_OPENERROR;
73cdf0e10cSrcweir 		case GRFILTER_IOERROR:
74cdf0e10cSrcweir 			return RID_SVXSTR_GRFILTER_IOERROR;
75cdf0e10cSrcweir 		case GRFILTER_VERSIONERROR:
76cdf0e10cSrcweir 			return RID_SVXSTR_GRFILTER_VERSIONERROR;
77cdf0e10cSrcweir 		case GRFILTER_FILTERERROR:
78cdf0e10cSrcweir 			return RID_SVXSTR_GRFILTER_FILTERERROR;
79cdf0e10cSrcweir 		case GRFILTER_FORMATERROR:
80cdf0e10cSrcweir 		default:
81cdf0e10cSrcweir 			return RID_SVXSTR_GRFILTER_FORMATERROR;
82cdf0e10cSrcweir 	}
83cdf0e10cSrcweir }
84cdf0e10cSrcweir 
85cdf0e10cSrcweir 
86cdf0e10cSrcweir struct SvxOpenGrf_Impl
87cdf0e10cSrcweir {
88cdf0e10cSrcweir 	SvxOpenGrf_Impl			();
89cdf0e10cSrcweir 
90cdf0e10cSrcweir 	sfx2::FileDialogHelper					aFileDlg;
91cdf0e10cSrcweir     uno::Reference < XFilePickerControlAccess >	xCtrlAcc;
92cdf0e10cSrcweir };
93cdf0e10cSrcweir 
94cdf0e10cSrcweir 
SvxOpenGrf_Impl()95cdf0e10cSrcweir SvxOpenGrf_Impl::SvxOpenGrf_Impl() :
96cdf0e10cSrcweir 	aFileDlg(SFXWB_GRAPHIC)
97cdf0e10cSrcweir {
98cdf0e10cSrcweir 	uno::Reference < XFilePicker > xFP = aFileDlg.GetFilePicker();
99cdf0e10cSrcweir     xCtrlAcc = uno::Reference < XFilePickerControlAccess >(xFP, UNO_QUERY);
100cdf0e10cSrcweir }
101cdf0e10cSrcweir 
102cdf0e10cSrcweir 
SvxOpenGraphicDialog(const String & rTitle)103cdf0e10cSrcweir SvxOpenGraphicDialog::SvxOpenGraphicDialog( const String& rTitle ) :
104cdf0e10cSrcweir 	mpImpl( new SvxOpenGrf_Impl )
105cdf0e10cSrcweir {
106cdf0e10cSrcweir     mpImpl->aFileDlg.SetTitle(rTitle);
107cdf0e10cSrcweir }
108cdf0e10cSrcweir 
109cdf0e10cSrcweir 
~SvxOpenGraphicDialog()110cdf0e10cSrcweir SvxOpenGraphicDialog::~SvxOpenGraphicDialog()
111cdf0e10cSrcweir {
112cdf0e10cSrcweir }
113cdf0e10cSrcweir 
114cdf0e10cSrcweir 
Execute()115cdf0e10cSrcweir short SvxOpenGraphicDialog::Execute()
116cdf0e10cSrcweir {
117cdf0e10cSrcweir 	sal_uInt16 	nImpRet;
118cdf0e10cSrcweir 	sal_Bool	bQuitLoop(sal_False);
119cdf0e10cSrcweir 
120cdf0e10cSrcweir 	while( bQuitLoop == sal_False &&
121cdf0e10cSrcweir 		   mpImpl->aFileDlg.Execute() == ERRCODE_NONE )
122cdf0e10cSrcweir 	{
123cdf0e10cSrcweir 		if( GetPath().Len() )
124cdf0e10cSrcweir 		{
125cdf0e10cSrcweir 			GraphicFilter*	pFilter = GraphicFilter::GetGraphicFilter();
126cdf0e10cSrcweir 			INetURLObject aObj( GetPath() );
127cdf0e10cSrcweir 
128cdf0e10cSrcweir 			// check whether we can load the graphic
129cdf0e10cSrcweir 			String	aCurFilter( GetCurrentFilter() );
130cdf0e10cSrcweir 			sal_uInt16 	nFormatNum = pFilter->GetImportFormatNumber( aCurFilter );
131cdf0e10cSrcweir 			sal_uInt16 	nRetFormat = 0;
132cdf0e10cSrcweir 			sal_uInt16	nFound = USHRT_MAX;
133cdf0e10cSrcweir 
134cdf0e10cSrcweir 			// non-local?
135cdf0e10cSrcweir 			if ( INET_PROT_FILE != aObj.GetProtocol() )
136cdf0e10cSrcweir 			{
137cdf0e10cSrcweir 				SfxMedium aMed( aObj.GetMainURL( INetURLObject::NO_DECODE ), STREAM_READ, sal_True );
138cdf0e10cSrcweir 				aMed.DownLoad();
139cdf0e10cSrcweir 				SvStream* pStream = aMed.GetInStream();
140cdf0e10cSrcweir 
141cdf0e10cSrcweir 				if( pStream )
142cdf0e10cSrcweir 					nImpRet = pFilter->CanImportGraphic( aObj.GetMainURL( INetURLObject::NO_DECODE ), *pStream, nFormatNum, &nRetFormat );
143cdf0e10cSrcweir 				else
144cdf0e10cSrcweir 					nImpRet = pFilter->CanImportGraphic( aObj, nFormatNum, &nRetFormat );
145cdf0e10cSrcweir 
146cdf0e10cSrcweir 				if ( GRFILTER_OK != nImpRet )
147cdf0e10cSrcweir 				{
148cdf0e10cSrcweir 					if ( !pStream )
149cdf0e10cSrcweir 						nImpRet = pFilter->CanImportGraphic( aObj, GRFILTER_FORMAT_DONTKNOW, &nRetFormat );
150cdf0e10cSrcweir 					else
151cdf0e10cSrcweir 						nImpRet = pFilter->CanImportGraphic( aObj.GetMainURL( INetURLObject::NO_DECODE ), *pStream,
152cdf0e10cSrcweir 															 GRFILTER_FORMAT_DONTKNOW, &nRetFormat );
153cdf0e10cSrcweir 				}
154cdf0e10cSrcweir 			}
155cdf0e10cSrcweir 			else
156cdf0e10cSrcweir 			{
157cdf0e10cSrcweir 				if( (nImpRet=pFilter->CanImportGraphic( aObj, nFormatNum, &nRetFormat )) != GRFILTER_OK )
158cdf0e10cSrcweir 					nImpRet = pFilter->CanImportGraphic( aObj, GRFILTER_FORMAT_DONTKNOW, &nRetFormat );
159cdf0e10cSrcweir 			}
160cdf0e10cSrcweir 
161cdf0e10cSrcweir 			if ( GRFILTER_OK == nImpRet )
162cdf0e10cSrcweir 				nFound = nRetFormat;
163cdf0e10cSrcweir 
164cdf0e10cSrcweir 			// could not load?
165cdf0e10cSrcweir 			if ( nFound == USHRT_MAX )
166cdf0e10cSrcweir 			{
167cdf0e10cSrcweir 				WarningBox aWarningBox( NULL, WB_3DLOOK | WB_RETRY_CANCEL, String( SfxResId( SvxOpenGrfErr2ResId(nImpRet) ) ) );
168cdf0e10cSrcweir 				bQuitLoop = aWarningBox.Execute()==RET_RETRY ? sal_False : sal_True;
169cdf0e10cSrcweir 			}
170cdf0e10cSrcweir 			else
171cdf0e10cSrcweir 			{
172cdf0e10cSrcweir 				// setup appropriate filter (so next time, it will work)
173cdf0e10cSrcweir 				if( pFilter->GetImportFormatCount() )
174cdf0e10cSrcweir 				{
175cdf0e10cSrcweir 					String	aFormatName(pFilter->GetImportFormatName(nFound));
176cdf0e10cSrcweir 					SetCurrentFilter(aFormatName);
177cdf0e10cSrcweir 				}
178cdf0e10cSrcweir 
179cdf0e10cSrcweir 				return nImpRet;
180cdf0e10cSrcweir 			}
181cdf0e10cSrcweir 		}
182cdf0e10cSrcweir 	}
183cdf0e10cSrcweir 
184cdf0e10cSrcweir 	// cancel
185cdf0e10cSrcweir 	return -1;
186cdf0e10cSrcweir }
187cdf0e10cSrcweir 
188cdf0e10cSrcweir 
SetPath(const String & rPath)189cdf0e10cSrcweir void SvxOpenGraphicDialog::SetPath( const String& rPath )
190cdf0e10cSrcweir {
191cdf0e10cSrcweir 	mpImpl->aFileDlg.SetDisplayDirectory(rPath);
192cdf0e10cSrcweir }
193cdf0e10cSrcweir 
SetPath(const String & rPath,sal_Bool bLinkState)194cdf0e10cSrcweir void SvxOpenGraphicDialog::SetPath( const String& rPath, sal_Bool bLinkState )
195cdf0e10cSrcweir {
196cdf0e10cSrcweir 	SetPath(rPath);
197cdf0e10cSrcweir 	AsLink(bLinkState);
198cdf0e10cSrcweir }
199cdf0e10cSrcweir 
200cdf0e10cSrcweir 
EnableLink(sal_Bool state)201cdf0e10cSrcweir void SvxOpenGraphicDialog::EnableLink( sal_Bool	 state	)
202cdf0e10cSrcweir {
203cdf0e10cSrcweir 	if( mpImpl->xCtrlAcc.is() )
204cdf0e10cSrcweir 	{
205cdf0e10cSrcweir 		try
206cdf0e10cSrcweir 		{
207cdf0e10cSrcweir 			mpImpl->xCtrlAcc->enableControl( ExtendedFilePickerElementIds::CHECKBOX_LINK, state );
208cdf0e10cSrcweir 		}
209cdf0e10cSrcweir 		catch(IllegalArgumentException)
210cdf0e10cSrcweir 		{
211cdf0e10cSrcweir #ifdef DBG_UTIL
212cdf0e10cSrcweir 			DBG_ERROR( "Cannot enable \"link\" checkbox" );
213cdf0e10cSrcweir #endif
214cdf0e10cSrcweir 		}
215cdf0e10cSrcweir 	}
216cdf0e10cSrcweir }
217cdf0e10cSrcweir 
218cdf0e10cSrcweir 
AsLink(sal_Bool bState)219cdf0e10cSrcweir void SvxOpenGraphicDialog::AsLink(sal_Bool	bState)
220cdf0e10cSrcweir {
221cdf0e10cSrcweir 	if( mpImpl->xCtrlAcc.is() )
222cdf0e10cSrcweir 	{
223cdf0e10cSrcweir 		try
224cdf0e10cSrcweir 		{
225cdf0e10cSrcweir 			Any	aAny; aAny <<= bState;
226cdf0e10cSrcweir 			mpImpl->xCtrlAcc->setValue( ExtendedFilePickerElementIds::CHECKBOX_LINK, 0, aAny );
227cdf0e10cSrcweir 		}
228cdf0e10cSrcweir 		catch(IllegalArgumentException)
229cdf0e10cSrcweir 		{
230cdf0e10cSrcweir #ifdef DBG_UTIL
231cdf0e10cSrcweir 			DBG_ERROR( "Cannot check \"link\" checkbox" );
232cdf0e10cSrcweir #endif
233cdf0e10cSrcweir 		}
234cdf0e10cSrcweir 	}
235cdf0e10cSrcweir }
236cdf0e10cSrcweir 
237cdf0e10cSrcweir 
IsAsLink() const238cdf0e10cSrcweir sal_Bool SvxOpenGraphicDialog::IsAsLink() const
239cdf0e10cSrcweir {
240cdf0e10cSrcweir 	try
241cdf0e10cSrcweir 	{
242cdf0e10cSrcweir 		if( mpImpl->xCtrlAcc.is() )
243cdf0e10cSrcweir 		{
244cdf0e10cSrcweir 			Any aVal = mpImpl->xCtrlAcc->getValue( ExtendedFilePickerElementIds::CHECKBOX_LINK, 0 );
245cdf0e10cSrcweir 			DBG_ASSERT(aVal.hasValue(), "Value CBX_INSERT_AS_LINK not found");
246cdf0e10cSrcweir 			return aVal.hasValue() ? *(sal_Bool*) aVal.getValue() : sal_False;
247cdf0e10cSrcweir 		}
248cdf0e10cSrcweir 	}
249cdf0e10cSrcweir 	catch(IllegalArgumentException)
250cdf0e10cSrcweir 	{
251cdf0e10cSrcweir #ifdef DBG_UTIL
252cdf0e10cSrcweir 		DBG_ERROR( "Cannot access \"link\" checkbox" );
253cdf0e10cSrcweir #endif
254cdf0e10cSrcweir 	}
255cdf0e10cSrcweir 
256cdf0e10cSrcweir 	return sal_False;
257cdf0e10cSrcweir }
258cdf0e10cSrcweir 
259cdf0e10cSrcweir 
GetGraphic(Graphic & rGraphic) const260cdf0e10cSrcweir int SvxOpenGraphicDialog::GetGraphic(Graphic& rGraphic) const
261cdf0e10cSrcweir {
262cdf0e10cSrcweir 	return mpImpl->aFileDlg.GetGraphic(rGraphic);
263cdf0e10cSrcweir }
264cdf0e10cSrcweir 
265cdf0e10cSrcweir 
GetPath() const266cdf0e10cSrcweir String SvxOpenGraphicDialog::GetPath() const
267cdf0e10cSrcweir {
268cdf0e10cSrcweir 	return mpImpl->aFileDlg.GetPath();
269cdf0e10cSrcweir }
270cdf0e10cSrcweir 
271cdf0e10cSrcweir 
GetCurrentFilter() const272cdf0e10cSrcweir String SvxOpenGraphicDialog::GetCurrentFilter() const
273cdf0e10cSrcweir {
274cdf0e10cSrcweir 	return mpImpl->aFileDlg.GetCurrentFilter();
275cdf0e10cSrcweir }
276cdf0e10cSrcweir 
277cdf0e10cSrcweir 
SetCurrentFilter(const String & rStr)278cdf0e10cSrcweir void SvxOpenGraphicDialog::SetCurrentFilter(const String&	rStr)
279cdf0e10cSrcweir {
280cdf0e10cSrcweir 	mpImpl->aFileDlg.SetCurrentFilter(rStr);
281cdf0e10cSrcweir }
282cdf0e10cSrcweir 
SetControlHelpIds(const sal_Int16 * _pControlId,const char ** _pHelpId)283cdf0e10cSrcweir void SvxOpenGraphicDialog::SetControlHelpIds( const sal_Int16* _pControlId, const char** _pHelpId )
284cdf0e10cSrcweir {
285cdf0e10cSrcweir 	mpImpl->aFileDlg.SetControlHelpIds( _pControlId, _pHelpId );
286cdf0e10cSrcweir }
287cdf0e10cSrcweir 
288cdf0e10cSrcweir 
289