/src/libreoffice/sfx2/source/appl/opengrf.cxx
Line | Count | Source (jump to first uncovered line) |
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/debug.hxx> |
22 | | #include <tools/urlobj.hxx> |
23 | | #include <com/sun/star/uno/Reference.h> |
24 | | #include <com/sun/star/lang/IllegalArgumentException.hpp> |
25 | | #include <com/sun/star/ui/dialogs/CommonFilePickerElementIds.hpp> |
26 | | #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp> |
27 | | #include <com/sun/star/ui/dialogs/TemplateDescription.hpp> |
28 | | #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp> |
29 | | #include <com/sun/star/ui/dialogs/XFilePicker3.hpp> |
30 | | #include <o3tl/any.hxx> |
31 | | #include <vcl/stdtext.hxx> |
32 | | #include <vcl/graphicfilter.hxx> |
33 | | #include <vcl/svapp.hxx> |
34 | | #include <vcl/weld.hxx> |
35 | | #include <sfx2/filedlghelper.hxx> |
36 | | #include <sfx2/docfile.hxx> |
37 | | #include <sfx2/opengrf.hxx> |
38 | | #include <sfx2/strings.hrc> |
39 | | #include <sfx2/sfxresid.hxx> |
40 | | #include <osl/diagnose.h> |
41 | | |
42 | | |
43 | | using namespace ::com::sun::star; |
44 | | using namespace ::com::sun::star::lang; |
45 | | using namespace ::com::sun::star::ui::dialogs; |
46 | | using namespace ::com::sun::star::uno; |
47 | | |
48 | | static TranslateId SvxOpenGrfErr2ResId( ErrCode err ) |
49 | 0 | { |
50 | 0 | if (err == ERRCODE_GRFILTER_OPENERROR) |
51 | 0 | return RID_SVXSTR_GRFILTER_OPENERROR; |
52 | 0 | else if (err == ERRCODE_GRFILTER_IOERROR) |
53 | 0 | return RID_SVXSTR_GRFILTER_IOERROR; |
54 | 0 | else if (err == ERRCODE_GRFILTER_VERSIONERROR) |
55 | 0 | return RID_SVXSTR_GRFILTER_VERSIONERROR; |
56 | 0 | else if (err == ERRCODE_GRFILTER_FILTERERROR) |
57 | 0 | return RID_SVXSTR_GRFILTER_FILTERERROR; |
58 | 0 | else |
59 | 0 | return RID_SVXSTR_GRFILTER_FORMATERROR; |
60 | 0 | } |
61 | | |
62 | | struct SvxOpenGrf_Impl |
63 | | { |
64 | | SvxOpenGrf_Impl(weld::Window* pPreferredParent, |
65 | | sal_Int16 nDialogType); |
66 | | |
67 | | sfx2::FileDialogHelper aFileDlg; |
68 | | OUString sDetectedFilter; |
69 | | weld::Window* pDialogParent; |
70 | | uno::Reference < XFilePickerControlAccess > xCtrlAcc; |
71 | | }; |
72 | | |
73 | | |
74 | | SvxOpenGrf_Impl::SvxOpenGrf_Impl(weld::Window* pPreferredParent, |
75 | | sal_Int16 nDialogType) |
76 | 0 | : aFileDlg(nDialogType, FileDialogFlags::Graphic, pPreferredParent) |
77 | 0 | , pDialogParent(pPreferredParent) |
78 | 0 | { |
79 | 0 | uno::Reference < XFilePicker3 > xFP = aFileDlg.GetFilePicker(); |
80 | 0 | xCtrlAcc.set(xFP, UNO_QUERY); |
81 | 0 | } |
82 | | |
83 | | |
84 | | SvxOpenGraphicDialog::SvxOpenGraphicDialog(const OUString& rTitle, weld::Window* pPreferredParent) |
85 | 0 | : mpImpl(new SvxOpenGrf_Impl(pPreferredParent, ui::dialogs::TemplateDescription::FILEOPEN_LINK_PREVIEW)) |
86 | 0 | { |
87 | 0 | mpImpl->aFileDlg.SetTitle(rTitle); |
88 | 0 | mpImpl->aFileDlg.SetContext(sfx2::FileDialogHelper::InsertImage); |
89 | 0 | } |
90 | | |
91 | | SvxOpenGraphicDialog::SvxOpenGraphicDialog(const OUString& rTitle, weld::Window* pPreferredParent, |
92 | | sal_Int16 nDialogType) |
93 | 0 | : mpImpl(new SvxOpenGrf_Impl(pPreferredParent, nDialogType)) |
94 | 0 | { |
95 | 0 | mpImpl->aFileDlg.SetTitle(rTitle); |
96 | 0 | mpImpl->aFileDlg.SetContext(sfx2::FileDialogHelper::InsertImage); |
97 | 0 | } |
98 | | |
99 | | SvxOpenGraphicDialog::~SvxOpenGraphicDialog() |
100 | 0 | { |
101 | 0 | } |
102 | | |
103 | | ErrCode SvxOpenGraphicDialog::Execute() |
104 | 0 | { |
105 | 0 | ErrCode nImpRet; |
106 | 0 | bool bQuitLoop(false); |
107 | |
|
108 | 0 | while( !bQuitLoop && |
109 | 0 | mpImpl->aFileDlg.Execute() == ERRCODE_NONE ) |
110 | 0 | { |
111 | 0 | if( !GetPath().isEmpty() ) |
112 | 0 | { |
113 | 0 | GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter(); |
114 | 0 | INetURLObject aObj( GetPath() ); |
115 | | |
116 | | // check whether we can load the graphic |
117 | 0 | OUString aCurFilter( GetCurrentFilter() ); |
118 | 0 | sal_uInt16 nFormatNum = rFilter.GetImportFormatNumber( aCurFilter ); |
119 | 0 | sal_uInt16 nRetFormat = 0; |
120 | 0 | sal_uInt16 nFound = USHRT_MAX; |
121 | | |
122 | | // non-local? |
123 | 0 | if ( INetProtocol::File != aObj.GetProtocol() ) |
124 | 0 | { |
125 | 0 | SfxMedium aMed( aObj.GetMainURL( INetURLObject::DecodeMechanism::NONE ), StreamMode::READ ); |
126 | 0 | aMed.Download(); |
127 | 0 | SvStream* pStream = aMed.GetInStream(); |
128 | |
|
129 | 0 | if( pStream ) |
130 | 0 | nImpRet = rFilter.CanImportGraphic( aObj.GetMainURL( INetURLObject::DecodeMechanism::NONE ), *pStream, nFormatNum, &nRetFormat ); |
131 | 0 | else |
132 | 0 | nImpRet = rFilter.CanImportGraphic( aObj, nFormatNum, &nRetFormat ); |
133 | |
|
134 | 0 | if ( ERRCODE_NONE != nImpRet ) |
135 | 0 | { |
136 | 0 | if ( !pStream ) |
137 | 0 | nImpRet = rFilter.CanImportGraphic( aObj, GRFILTER_FORMAT_DONTKNOW, &nRetFormat ); |
138 | 0 | else |
139 | 0 | nImpRet = rFilter.CanImportGraphic( aObj.GetMainURL( INetURLObject::DecodeMechanism::NONE ), *pStream, |
140 | 0 | GRFILTER_FORMAT_DONTKNOW, &nRetFormat ); |
141 | 0 | } |
142 | 0 | } |
143 | 0 | else |
144 | 0 | { |
145 | 0 | nImpRet = rFilter.CanImportGraphic( aObj, nFormatNum, &nRetFormat ); |
146 | 0 | if( nImpRet != ERRCODE_NONE ) |
147 | 0 | nImpRet = rFilter.CanImportGraphic( aObj, GRFILTER_FORMAT_DONTKNOW, &nRetFormat ); |
148 | 0 | } |
149 | |
|
150 | 0 | if ( ERRCODE_NONE == nImpRet ) |
151 | 0 | nFound = nRetFormat; |
152 | | |
153 | | // could not load? |
154 | 0 | if ( nFound == USHRT_MAX ) |
155 | 0 | { |
156 | 0 | std::unique_ptr<weld::MessageDialog> xWarn(Application::CreateMessageDialog(mpImpl->pDialogParent, |
157 | 0 | VclMessageType::Warning, VclButtonsType::NONE, |
158 | 0 | SfxResId(SvxOpenGrfErr2ResId(nImpRet)))); |
159 | 0 | xWarn->add_button(GetStandardText(StandardButtonType::Retry), RET_RETRY); |
160 | 0 | xWarn->add_button(GetStandardText(StandardButtonType::Cancel), RET_CANCEL); |
161 | 0 | bQuitLoop = xWarn->run() != RET_RETRY; |
162 | 0 | } |
163 | 0 | else |
164 | 0 | { |
165 | 0 | if( rFilter.GetImportFormatCount() ) |
166 | 0 | { |
167 | | // store detected appropriate filter |
168 | 0 | OUString aFormatName(rFilter.GetImportFormatName(nFound)); |
169 | 0 | SetDetectedFilter(aFormatName); |
170 | 0 | } |
171 | 0 | else |
172 | 0 | { |
173 | 0 | SetDetectedFilter(mpImpl->aFileDlg.GetCurrentFilter()); |
174 | 0 | } |
175 | |
|
176 | 0 | return nImpRet; |
177 | 0 | } |
178 | 0 | } |
179 | 0 | } |
180 | | |
181 | | // cancel |
182 | 0 | return ErrCode(sal_uInt32(-1)); |
183 | 0 | } |
184 | | |
185 | | |
186 | | void SvxOpenGraphicDialog::SetPath( const OUString& rPath, bool bLinkState ) |
187 | 0 | { |
188 | 0 | mpImpl->aFileDlg.SetDisplayDirectory(rPath); |
189 | 0 | AsLink(bLinkState); |
190 | 0 | } |
191 | | |
192 | | |
193 | | void SvxOpenGraphicDialog::EnableLink( bool state ) |
194 | 0 | { |
195 | 0 | if( !mpImpl->xCtrlAcc.is() ) |
196 | 0 | return; |
197 | | |
198 | 0 | try |
199 | 0 | { |
200 | 0 | mpImpl->xCtrlAcc->enableControl( ExtendedFilePickerElementIds::CHECKBOX_LINK, state ); |
201 | 0 | } |
202 | 0 | catch(const IllegalArgumentException&) |
203 | 0 | { |
204 | | #ifdef DBG_UTIL |
205 | | OSL_FAIL( "Cannot enable \"link\" checkbox" ); |
206 | | #endif |
207 | 0 | } |
208 | 0 | } |
209 | | |
210 | | |
211 | | void SvxOpenGraphicDialog::AsLink(bool bState) |
212 | 0 | { |
213 | 0 | if( !mpImpl->xCtrlAcc.is() ) |
214 | 0 | return; |
215 | | |
216 | 0 | try |
217 | 0 | { |
218 | 0 | mpImpl->xCtrlAcc->setValue( ExtendedFilePickerElementIds::CHECKBOX_LINK, 0, Any(bState) ); |
219 | 0 | } |
220 | 0 | catch(const IllegalArgumentException&) |
221 | 0 | { |
222 | | #ifdef DBG_UTIL |
223 | | OSL_FAIL( "Cannot check \"link\" checkbox" ); |
224 | | #endif |
225 | 0 | } |
226 | 0 | } |
227 | | |
228 | | |
229 | | bool SvxOpenGraphicDialog::IsAsLink() const |
230 | 0 | { |
231 | 0 | try |
232 | 0 | { |
233 | 0 | if( mpImpl->xCtrlAcc.is() ) |
234 | 0 | { |
235 | 0 | Any aVal = mpImpl->xCtrlAcc->getValue( ExtendedFilePickerElementIds::CHECKBOX_LINK, 0 ); |
236 | 0 | DBG_ASSERT(aVal.hasValue(), "Value CBX_INSERT_AS_LINK not found"); |
237 | 0 | return aVal.hasValue() && *o3tl::doAccess<bool>(aVal); |
238 | 0 | } |
239 | 0 | } |
240 | 0 | catch(const IllegalArgumentException&) |
241 | 0 | { |
242 | | #ifdef DBG_UTIL |
243 | | OSL_FAIL( "Cannot access \"link\" checkbox" ); |
244 | | #endif |
245 | 0 | } |
246 | | |
247 | 0 | return false; |
248 | 0 | } |
249 | | |
250 | | ErrCode SvxOpenGraphicDialog::GetGraphic(Graphic& rGraphic) const |
251 | 0 | { |
252 | 0 | return mpImpl->aFileDlg.GetGraphic(rGraphic); |
253 | 0 | } |
254 | | |
255 | | OUString SvxOpenGraphicDialog::GetPath() const |
256 | 0 | { |
257 | 0 | return mpImpl->aFileDlg.GetPath(); |
258 | 0 | } |
259 | | |
260 | | OUString SvxOpenGraphicDialog::GetCurrentFilter() const |
261 | 0 | { |
262 | 0 | return mpImpl->aFileDlg.GetCurrentFilter(); |
263 | 0 | } |
264 | | |
265 | | OUString const & SvxOpenGraphicDialog::GetDetectedFilter() const |
266 | 0 | { |
267 | 0 | return mpImpl->sDetectedFilter; |
268 | 0 | } |
269 | | |
270 | | void SvxOpenGraphicDialog::SetCurrentFilter(const OUString& rStr) |
271 | 0 | { |
272 | 0 | mpImpl->aFileDlg.SetCurrentFilter(rStr); |
273 | 0 | } |
274 | | |
275 | | void SvxOpenGraphicDialog::SetDetectedFilter(const OUString& rStr) |
276 | 0 | { |
277 | 0 | mpImpl->sDetectedFilter = rStr; |
278 | 0 | } |
279 | | |
280 | | Reference<ui::dialogs::XFilePickerControlAccess> const & SvxOpenGraphicDialog::GetFilePickerControlAccess() const |
281 | 0 | { |
282 | 0 | return mpImpl->xCtrlAcc; |
283 | 0 | } |
284 | | |
285 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |