Coverage Report

Created: 2026-02-14 09:37

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