1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
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 using namespace ::cppu
;
49 static TranslateId
SvxOpenGrfErr2ResId( ErrCode err
)
51 if (err
== ERRCODE_GRFILTER_OPENERROR
)
52 return RID_SVXSTR_GRFILTER_OPENERROR
;
53 else if (err
== ERRCODE_GRFILTER_IOERROR
)
54 return RID_SVXSTR_GRFILTER_IOERROR
;
55 else if (err
== ERRCODE_GRFILTER_VERSIONERROR
)
56 return RID_SVXSTR_GRFILTER_VERSIONERROR
;
57 else if (err
== ERRCODE_GRFILTER_FILTERERROR
)
58 return RID_SVXSTR_GRFILTER_FILTERERROR
;
60 return RID_SVXSTR_GRFILTER_FORMATERROR
;
63 struct SvxOpenGrf_Impl
65 SvxOpenGrf_Impl(weld::Window
* pPreferredParent
,
66 sal_Int16 nDialogType
);
68 sfx2::FileDialogHelper aFileDlg
;
69 OUString sDetectedFilter
;
70 uno::Reference
< XFilePickerControlAccess
> xCtrlAcc
;
74 SvxOpenGrf_Impl::SvxOpenGrf_Impl(weld::Window
* pPreferredParent
,
75 sal_Int16 nDialogType
)
76 : aFileDlg(nDialogType
, FileDialogFlags::Graphic
, pPreferredParent
)
78 uno::Reference
< XFilePicker3
> xFP
= aFileDlg
.GetFilePicker();
79 xCtrlAcc
.set(xFP
, UNO_QUERY
);
83 SvxOpenGraphicDialog::SvxOpenGraphicDialog(const OUString
& rTitle
, weld::Window
* pPreferredParent
)
84 : mpImpl(new SvxOpenGrf_Impl(pPreferredParent
, ui::dialogs::TemplateDescription::FILEOPEN_LINK_PREVIEW
))
86 mpImpl
->aFileDlg
.SetTitle(rTitle
);
87 mpImpl
->aFileDlg
.SetContext(sfx2::FileDialogHelper::InsertImage
);
90 SvxOpenGraphicDialog::SvxOpenGraphicDialog(const OUString
& rTitle
, weld::Window
* pPreferredParent
,
91 sal_Int16 nDialogType
)
92 : mpImpl(new SvxOpenGrf_Impl(pPreferredParent
, nDialogType
))
94 mpImpl
->aFileDlg
.SetTitle(rTitle
);
95 mpImpl
->aFileDlg
.SetContext(sfx2::FileDialogHelper::InsertImage
);
98 SvxOpenGraphicDialog::~SvxOpenGraphicDialog()
102 ErrCode
SvxOpenGraphicDialog::Execute()
105 bool bQuitLoop(false);
108 mpImpl
->aFileDlg
.Execute() == ERRCODE_NONE
)
110 if( !GetPath().isEmpty() )
112 GraphicFilter
& rFilter
= GraphicFilter::GetGraphicFilter();
113 INetURLObject
aObj( GetPath() );
115 // check whether we can load the graphic
116 OUString
aCurFilter( GetCurrentFilter() );
117 sal_uInt16 nFormatNum
= rFilter
.GetImportFormatNumber( aCurFilter
);
118 sal_uInt16 nRetFormat
= 0;
119 sal_uInt16 nFound
= USHRT_MAX
;
122 if ( INetProtocol::File
!= aObj
.GetProtocol() )
124 SfxMedium
aMed( aObj
.GetMainURL( INetURLObject::DecodeMechanism::NONE
), StreamMode::READ
);
126 SvStream
* pStream
= aMed
.GetInStream();
129 nImpRet
= rFilter
.CanImportGraphic( aObj
.GetMainURL( INetURLObject::DecodeMechanism::NONE
), *pStream
, nFormatNum
, &nRetFormat
);
131 nImpRet
= rFilter
.CanImportGraphic( aObj
, nFormatNum
, &nRetFormat
);
133 if ( ERRCODE_NONE
!= nImpRet
)
136 nImpRet
= rFilter
.CanImportGraphic( aObj
, GRFILTER_FORMAT_DONTKNOW
, &nRetFormat
);
138 nImpRet
= rFilter
.CanImportGraphic( aObj
.GetMainURL( INetURLObject::DecodeMechanism::NONE
), *pStream
,
139 GRFILTER_FORMAT_DONTKNOW
, &nRetFormat
);
144 nImpRet
= rFilter
.CanImportGraphic( aObj
, nFormatNum
, &nRetFormat
);
145 if( nImpRet
!= ERRCODE_NONE
)
146 nImpRet
= rFilter
.CanImportGraphic( aObj
, GRFILTER_FORMAT_DONTKNOW
, &nRetFormat
);
149 if ( ERRCODE_NONE
== nImpRet
)
153 if ( nFound
== USHRT_MAX
)
155 std::unique_ptr
<weld::MessageDialog
> xWarn(Application::CreateMessageDialog(nullptr,
156 VclMessageType::Warning
, VclButtonsType::NONE
,
157 SfxResId(SvxOpenGrfErr2ResId(nImpRet
))));
158 xWarn
->add_button(GetStandardText(StandardButtonType::Retry
), RET_RETRY
);
159 xWarn
->add_button(GetStandardText(StandardButtonType::Cancel
), RET_CANCEL
);
160 bQuitLoop
= xWarn
->run() != RET_RETRY
;
164 if( rFilter
.GetImportFormatCount() )
166 // store detected appropriate filter
167 OUString
aFormatName(rFilter
.GetImportFormatName(nFound
));
168 SetDetectedFilter(aFormatName
);
172 SetDetectedFilter(mpImpl
->aFileDlg
.GetCurrentFilter());
181 return ErrCode(sal_uInt32(-1));
185 void SvxOpenGraphicDialog::SetPath( const OUString
& rPath
, bool bLinkState
)
187 mpImpl
->aFileDlg
.SetDisplayDirectory(rPath
);
192 void SvxOpenGraphicDialog::EnableLink( bool state
)
194 if( !mpImpl
->xCtrlAcc
.is() )
199 mpImpl
->xCtrlAcc
->enableControl( ExtendedFilePickerElementIds::CHECKBOX_LINK
, state
);
201 catch(const IllegalArgumentException
&)
204 OSL_FAIL( "Cannot enable \"link\" checkbox" );
210 void SvxOpenGraphicDialog::AsLink(bool bState
)
212 if( !mpImpl
->xCtrlAcc
.is() )
217 mpImpl
->xCtrlAcc
->setValue( ExtendedFilePickerElementIds::CHECKBOX_LINK
, 0, Any(bState
) );
219 catch(const IllegalArgumentException
&)
222 OSL_FAIL( "Cannot check \"link\" checkbox" );
228 bool SvxOpenGraphicDialog::IsAsLink() const
232 if( mpImpl
->xCtrlAcc
.is() )
234 Any aVal
= mpImpl
->xCtrlAcc
->getValue( ExtendedFilePickerElementIds::CHECKBOX_LINK
, 0 );
235 DBG_ASSERT(aVal
.hasValue(), "Value CBX_INSERT_AS_LINK not found");
236 return aVal
.hasValue() && *o3tl::doAccess
<bool>(aVal
);
239 catch(const IllegalArgumentException
&)
242 OSL_FAIL( "Cannot access \"link\" checkbox" );
249 ErrCode
SvxOpenGraphicDialog::GetGraphic(Graphic
& rGraphic
) const
251 return mpImpl
->aFileDlg
.GetGraphic(rGraphic
);
254 OUString
SvxOpenGraphicDialog::GetPath() const
256 return mpImpl
->aFileDlg
.GetPath();
259 OUString
SvxOpenGraphicDialog::GetCurrentFilter() const
261 return mpImpl
->aFileDlg
.GetCurrentFilter();
264 OUString
const & SvxOpenGraphicDialog::GetDetectedFilter() const
266 return mpImpl
->sDetectedFilter
;
269 void SvxOpenGraphicDialog::SetCurrentFilter(const OUString
& rStr
)
271 mpImpl
->aFileDlg
.SetCurrentFilter(rStr
);
274 void SvxOpenGraphicDialog::SetDetectedFilter(const OUString
& rStr
)
276 mpImpl
->sDetectedFilter
= rStr
;
279 Reference
<ui::dialogs::XFilePickerControlAccess
> const & SvxOpenGraphicDialog::GetFilePickerControlAccess() const
281 return mpImpl
->xCtrlAcc
;
284 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */