xref: /aoo4110/main/sfx2/source/appl/opengrf.cxx (revision b1cdbd2c)
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_sfx2.hxx"
26 
27 #include <tools/urlobj.hxx>
28 #include <cppuhelper/implbase1.hxx>
29 #include <com/sun/star/uno/Reference.h>
30 #include <com/sun/star/lang/XInitialization.hpp>
31 #include <com/sun/star/ui/dialogs/CommonFilePickerElementIds.hpp>
32 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
33 #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
34 #include <com/sun/star/ui/dialogs/FilePreviewImageFormats.hpp>
35 #include <com/sun/star/ui/dialogs/ListboxControlActions.hpp>
36 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
37 #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
38 #include <com/sun/star/ui/dialogs/XFilePicker.hpp>
39 #include <com/sun/star/ui/dialogs/XFilePickerListener.hpp>
40 #include <com/sun/star/ui/dialogs/XFilePickerNotifier.hpp>
41 #include <com/sun/star/ui/dialogs/XFilePreview.hpp>
42 #include <com/sun/star/ui/dialogs/XFilterManager.hpp>
43 #include <svl/urihelper.hxx>
44 #include <unotools/ucbstreamhelper.hxx>
45 #include <svtools/transfer.hxx>
46 #include <sot/formats.hxx>
47 #include <vcl/msgbox.hxx>
48 #include <sfx2/filedlghelper.hxx>
49 #include <sfx2/docfile.hxx>
50 #include <unotools/pathoptions.hxx>
51 #include <sfx2/opengrf.hxx>
52 #include "app.hrc"
53 #include "sfx2/sfxresid.hxx"
54 
55 //-----------------------------------------------------------------------------
56 
57 using namespace ::com::sun::star;
58 using namespace ::com::sun::star::lang;
59 using namespace ::com::sun::star::ui::dialogs;
60 using namespace ::com::sun::star::uno;
61 using namespace ::rtl;
62 using namespace ::cppu;
63 
64 
65 //-----------------------------------------------------------------------------
66 
SvxOpenGrfErr2ResId(short err)67 sal_uInt16	SvxOpenGrfErr2ResId(	short	err		)
68 {
69 	switch( err )
70 	{
71 		case GRFILTER_OPENERROR:
72 			return RID_SVXSTR_GRFILTER_OPENERROR;
73 		case GRFILTER_IOERROR:
74 			return RID_SVXSTR_GRFILTER_IOERROR;
75 		case GRFILTER_VERSIONERROR:
76 			return RID_SVXSTR_GRFILTER_VERSIONERROR;
77 		case GRFILTER_FILTERERROR:
78 			return RID_SVXSTR_GRFILTER_FILTERERROR;
79 		case GRFILTER_FORMATERROR:
80 		default:
81 			return RID_SVXSTR_GRFILTER_FORMATERROR;
82 	}
83 }
84 
85 
86 struct SvxOpenGrf_Impl
87 {
88 	SvxOpenGrf_Impl			();
89 
90 	sfx2::FileDialogHelper					aFileDlg;
91     uno::Reference < XFilePickerControlAccess >	xCtrlAcc;
92 };
93 
94 
SvxOpenGrf_Impl()95 SvxOpenGrf_Impl::SvxOpenGrf_Impl() :
96 	aFileDlg(SFXWB_GRAPHIC)
97 {
98 	uno::Reference < XFilePicker > xFP = aFileDlg.GetFilePicker();
99     xCtrlAcc = uno::Reference < XFilePickerControlAccess >(xFP, UNO_QUERY);
100 }
101 
102 
SvxOpenGraphicDialog(const String & rTitle)103 SvxOpenGraphicDialog::SvxOpenGraphicDialog( const String& rTitle ) :
104 	mpImpl( new SvxOpenGrf_Impl )
105 {
106     mpImpl->aFileDlg.SetTitle(rTitle);
107 }
108 
109 
~SvxOpenGraphicDialog()110 SvxOpenGraphicDialog::~SvxOpenGraphicDialog()
111 {
112 }
113 
114 
Execute()115 short SvxOpenGraphicDialog::Execute()
116 {
117 	sal_uInt16 	nImpRet;
118 	sal_Bool	bQuitLoop(sal_False);
119 
120 	while( bQuitLoop == sal_False &&
121 		   mpImpl->aFileDlg.Execute() == ERRCODE_NONE )
122 	{
123 		if( GetPath().Len() )
124 		{
125 			GraphicFilter*	pFilter = GraphicFilter::GetGraphicFilter();
126 			INetURLObject aObj( GetPath() );
127 
128 			// check whether we can load the graphic
129 			String	aCurFilter( GetCurrentFilter() );
130 			sal_uInt16 	nFormatNum = pFilter->GetImportFormatNumber( aCurFilter );
131 			sal_uInt16 	nRetFormat = 0;
132 			sal_uInt16	nFound = USHRT_MAX;
133 
134 			// non-local?
135 			if ( INET_PROT_FILE != aObj.GetProtocol() )
136 			{
137 				SfxMedium aMed( aObj.GetMainURL( INetURLObject::NO_DECODE ), STREAM_READ, sal_True );
138 				aMed.DownLoad();
139 				SvStream* pStream = aMed.GetInStream();
140 
141 				if( pStream )
142 					nImpRet = pFilter->CanImportGraphic( aObj.GetMainURL( INetURLObject::NO_DECODE ), *pStream, nFormatNum, &nRetFormat );
143 				else
144 					nImpRet = pFilter->CanImportGraphic( aObj, nFormatNum, &nRetFormat );
145 
146 				if ( GRFILTER_OK != nImpRet )
147 				{
148 					if ( !pStream )
149 						nImpRet = pFilter->CanImportGraphic( aObj, GRFILTER_FORMAT_DONTKNOW, &nRetFormat );
150 					else
151 						nImpRet = pFilter->CanImportGraphic( aObj.GetMainURL( INetURLObject::NO_DECODE ), *pStream,
152 															 GRFILTER_FORMAT_DONTKNOW, &nRetFormat );
153 				}
154 			}
155 			else
156 			{
157 				if( (nImpRet=pFilter->CanImportGraphic( aObj, nFormatNum, &nRetFormat )) != GRFILTER_OK )
158 					nImpRet = pFilter->CanImportGraphic( aObj, GRFILTER_FORMAT_DONTKNOW, &nRetFormat );
159 			}
160 
161 			if ( GRFILTER_OK == nImpRet )
162 				nFound = nRetFormat;
163 
164 			// could not load?
165 			if ( nFound == USHRT_MAX )
166 			{
167 				WarningBox aWarningBox( NULL, WB_3DLOOK | WB_RETRY_CANCEL, String( SfxResId( SvxOpenGrfErr2ResId(nImpRet) ) ) );
168 				bQuitLoop = aWarningBox.Execute()==RET_RETRY ? sal_False : sal_True;
169 			}
170 			else
171 			{
172 				// setup appropriate filter (so next time, it will work)
173 				if( pFilter->GetImportFormatCount() )
174 				{
175 					String	aFormatName(pFilter->GetImportFormatName(nFound));
176 					SetCurrentFilter(aFormatName);
177 				}
178 
179 				return nImpRet;
180 			}
181 		}
182 	}
183 
184 	// cancel
185 	return -1;
186 }
187 
188 
SetPath(const String & rPath)189 void SvxOpenGraphicDialog::SetPath( const String& rPath )
190 {
191 	mpImpl->aFileDlg.SetDisplayDirectory(rPath);
192 }
193 
SetPath(const String & rPath,sal_Bool bLinkState)194 void SvxOpenGraphicDialog::SetPath( const String& rPath, sal_Bool bLinkState )
195 {
196 	SetPath(rPath);
197 	AsLink(bLinkState);
198 }
199 
200 
EnableLink(sal_Bool state)201 void SvxOpenGraphicDialog::EnableLink( sal_Bool	 state	)
202 {
203 	if( mpImpl->xCtrlAcc.is() )
204 	{
205 		try
206 		{
207 			mpImpl->xCtrlAcc->enableControl( ExtendedFilePickerElementIds::CHECKBOX_LINK, state );
208 		}
209 		catch(IllegalArgumentException)
210 		{
211 #ifdef DBG_UTIL
212 			DBG_ERROR( "Cannot enable \"link\" checkbox" );
213 #endif
214 		}
215 	}
216 }
217 
218 
AsLink(sal_Bool bState)219 void SvxOpenGraphicDialog::AsLink(sal_Bool	bState)
220 {
221 	if( mpImpl->xCtrlAcc.is() )
222 	{
223 		try
224 		{
225 			Any	aAny; aAny <<= bState;
226 			mpImpl->xCtrlAcc->setValue( ExtendedFilePickerElementIds::CHECKBOX_LINK, 0, aAny );
227 		}
228 		catch(IllegalArgumentException)
229 		{
230 #ifdef DBG_UTIL
231 			DBG_ERROR( "Cannot check \"link\" checkbox" );
232 #endif
233 		}
234 	}
235 }
236 
237 
IsAsLink() const238 sal_Bool SvxOpenGraphicDialog::IsAsLink() const
239 {
240 	try
241 	{
242 		if( mpImpl->xCtrlAcc.is() )
243 		{
244 			Any aVal = mpImpl->xCtrlAcc->getValue( ExtendedFilePickerElementIds::CHECKBOX_LINK, 0 );
245 			DBG_ASSERT(aVal.hasValue(), "Value CBX_INSERT_AS_LINK not found");
246 			return aVal.hasValue() ? *(sal_Bool*) aVal.getValue() : sal_False;
247 		}
248 	}
249 	catch(IllegalArgumentException)
250 	{
251 #ifdef DBG_UTIL
252 		DBG_ERROR( "Cannot access \"link\" checkbox" );
253 #endif
254 	}
255 
256 	return sal_False;
257 }
258 
259 
GetGraphic(Graphic & rGraphic) const260 int SvxOpenGraphicDialog::GetGraphic(Graphic& rGraphic) const
261 {
262 	return mpImpl->aFileDlg.GetGraphic(rGraphic);
263 }
264 
265 
GetPath() const266 String SvxOpenGraphicDialog::GetPath() const
267 {
268 	return mpImpl->aFileDlg.GetPath();
269 }
270 
271 
GetCurrentFilter() const272 String SvxOpenGraphicDialog::GetCurrentFilter() const
273 {
274 	return mpImpl->aFileDlg.GetCurrentFilter();
275 }
276 
277 
SetCurrentFilter(const String & rStr)278 void SvxOpenGraphicDialog::SetCurrentFilter(const String&	rStr)
279 {
280 	mpImpl->aFileDlg.SetCurrentFilter(rStr);
281 }
282 
SetControlHelpIds(const sal_Int16 * _pControlId,const char ** _pHelpId)283 void SvxOpenGraphicDialog::SetControlHelpIds( const sal_Int16* _pControlId, const char** _pHelpId )
284 {
285 	mpImpl->aFileDlg.SetControlHelpIds( _pControlId, _pHelpId );
286 }
287 
288 
289