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