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