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