Coverage Report

Created: 2026-10-14 09:33

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 <tools/stream.hxx>
32
#include <vcl/stdtext.hxx>
33
#include <vcl/graphicfilter.hxx>
34
#include <vcl/svapp.hxx>
35
#include <vcl/vclenum.hxx>
36
#include <vcl/weld/MessageDialog.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
void SvxOpenGraphicDialog::setMultiSelect(bool bEnable)
78
0
{
79
0
    mpImpl->aFileDlg.GetFilePicker()->setMultiSelectionMode(bEnable);
80
0
}
81
82
SvxOpenGrf_Impl::SvxOpenGrf_Impl(weld::Window* pPreferredParent,
83
                                 sal_Int16 nDialogType)
84
0
    : aFileDlg(nDialogType, FileDialogFlags::Graphic, pPreferredParent)
85
0
    , pDialogParent(pPreferredParent)
86
0
{
87
0
    uno::Reference < XFilePicker3 > xFP = aFileDlg.GetFilePicker();
88
0
    xCtrlAcc.set(xFP, UNO_QUERY);
89
0
}
90
91
92
SvxOpenGraphicDialog::SvxOpenGraphicDialog(const OUString& rTitle, weld::Window* pPreferredParent)
93
0
    : mpImpl(new SvxOpenGrf_Impl(pPreferredParent, ui::dialogs::TemplateDescription::FILEOPEN_LINK_PREVIEW))
94
0
{
95
0
    mpImpl->aFileDlg.SetTitle(rTitle);
96
0
    mpImpl->aFileDlg.SetContext(sfx2::FileDialogHelper::InsertImage);
97
0
}
98
99
SvxOpenGraphicDialog::SvxOpenGraphicDialog(const OUString& rTitle, weld::Window* pPreferredParent,
100
                                           sal_Int16 nDialogType)
101
0
    : mpImpl(new SvxOpenGrf_Impl(pPreferredParent, nDialogType))
102
0
{
103
0
    mpImpl->aFileDlg.SetTitle(rTitle);
104
0
    mpImpl->aFileDlg.SetContext(sfx2::FileDialogHelper::InsertImage);
105
0
}
106
107
SvxOpenGraphicDialog::~SvxOpenGraphicDialog()
108
0
{
109
0
}
110
111
ErrCode SvxOpenGraphicDialog::Execute()
112
0
{
113
0
    ErrCode nImpRet;
114
0
    bool    bQuitLoop(false);
115
116
0
    while( !bQuitLoop &&
117
0
           mpImpl->aFileDlg.Execute() == ERRCODE_NONE )
118
0
    {
119
0
        if( !GetPath().isEmpty() )
120
0
        {
121
0
            GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
122
0
            INetURLObject aObj( GetPath() );
123
124
            // check whether we can load the graphic
125
0
            OUString  aCurFilter( GetCurrentFilter() );
126
0
            sal_uInt16  nFormatNum = rFilter.GetImportFormatNumber( aCurFilter );
127
0
            sal_uInt16  nRetFormat = 0;
128
0
            sal_uInt16  nFound = USHRT_MAX;
129
130
            // non-local?
131
0
            if ( INetProtocol::File != aObj.GetProtocol() )
132
0
            {
133
0
                SfxMedium aMed( aObj.GetMainURL( INetURLObject::DecodeMechanism::NONE ), StreamMode::READ );
134
0
                aMed.Download();
135
0
                SvStream* pStream = aMed.GetInStream();
136
137
0
                if( pStream )
138
0
                    nImpRet = rFilter.CanImportGraphic( aObj.GetMainURL( INetURLObject::DecodeMechanism::NONE ), *pStream, nFormatNum, &nRetFormat );
139
0
                else
140
0
                    nImpRet = rFilter.CanImportGraphic( aObj, nFormatNum, &nRetFormat );
141
142
0
                if ( ERRCODE_NONE != nImpRet )
143
0
                {
144
0
                    if ( !pStream )
145
0
                        nImpRet = rFilter.CanImportGraphic( aObj, GRFILTER_FORMAT_DONTKNOW, &nRetFormat );
146
0
                    else
147
0
                        nImpRet = rFilter.CanImportGraphic( aObj.GetMainURL( INetURLObject::DecodeMechanism::NONE ), *pStream,
148
0
                                                             GRFILTER_FORMAT_DONTKNOW, &nRetFormat );
149
0
                }
150
0
            }
151
0
            else
152
0
            {
153
0
                nImpRet = rFilter.CanImportGraphic( aObj, nFormatNum, &nRetFormat );
154
0
                if( nImpRet != ERRCODE_NONE )
155
0
                    nImpRet = rFilter.CanImportGraphic( aObj, GRFILTER_FORMAT_DONTKNOW, &nRetFormat );
156
0
            }
157
158
0
            if ( ERRCODE_NONE == nImpRet )
159
0
                nFound = nRetFormat;
160
161
            // could not load?
162
0
            if ( nFound == USHRT_MAX )
163
0
            {
164
0
                std::unique_ptr<weld::MessageDialog> xWarn(Application::CreateMessageDialog(mpImpl->pDialogParent,
165
0
                                                           VclMessageType::Warning, VclButtonsType::NONE,
166
0
                                                           SfxResId(SvxOpenGrfErr2ResId(nImpRet))));
167
0
                xWarn->add_button(GetStandardText(StandardButtonType::Retry), RET_RETRY);
168
0
                xWarn->add_button(GetStandardText(StandardButtonType::Cancel), RET_CANCEL);
169
0
                bQuitLoop = xWarn->run() != RET_RETRY;
170
0
            }
171
0
            else
172
0
            {
173
0
                if( rFilter.GetImportFormatCount() )
174
0
                {
175
                    // store detected appropriate filter
176
0
                    OUString aFormatName(rFilter.GetImportFormatName(nFound));
177
0
                    SetDetectedFilter(aFormatName);
178
0
                }
179
0
                else
180
0
                {
181
0
                    SetDetectedFilter(mpImpl->aFileDlg.GetCurrentFilter());
182
0
                }
183
184
0
                return nImpRet;
185
0
            }
186
0
        }
187
0
    }
188
189
    // cancel
190
0
    return ErrCode(sal_uInt32(-1));
191
0
}
192
193
194
void SvxOpenGraphicDialog::SetPath( const OUString& rPath, bool bLinkState )
195
0
{
196
0
    mpImpl->aFileDlg.SetDisplayDirectory(rPath);
197
0
    AsLink(bLinkState);
198
0
}
199
200
201
void SvxOpenGraphicDialog::EnableLink( bool state )
202
0
{
203
0
    if( !mpImpl->xCtrlAcc.is() )
204
0
        return;
205
206
0
    try
207
0
    {
208
0
        mpImpl->xCtrlAcc->enableControl( ExtendedFilePickerElementIds::CHECKBOX_LINK, state );
209
0
    }
210
0
    catch(const IllegalArgumentException&)
211
0
    {
212
#ifdef DBG_UTIL
213
        OSL_FAIL( "Cannot enable \"link\" checkbox" );
214
#endif
215
0
    }
216
0
}
217
218
219
void SvxOpenGraphicDialog::AsLink(bool bState)
220
0
{
221
0
    if( !mpImpl->xCtrlAcc.is() )
222
0
        return;
223
224
0
    try
225
0
    {
226
0
        mpImpl->xCtrlAcc->setValue( ExtendedFilePickerElementIds::CHECKBOX_LINK, 0, Any(bState) );
227
0
    }
228
0
    catch(const IllegalArgumentException&)
229
0
    {
230
#ifdef DBG_UTIL
231
        OSL_FAIL( "Cannot check \"link\" checkbox" );
232
#endif
233
0
    }
234
0
}
235
236
237
bool SvxOpenGraphicDialog::IsAsLink() const
238
0
{
239
0
    try
240
0
    {
241
0
        if( mpImpl->xCtrlAcc.is() )
242
0
        {
243
0
            Any aVal = mpImpl->xCtrlAcc->getValue( ExtendedFilePickerElementIds::CHECKBOX_LINK, 0 );
244
0
            DBG_ASSERT(aVal.hasValue(), "Value CBX_INSERT_AS_LINK not found");
245
0
            return aVal.hasValue() && *o3tl::doAccess<bool>(aVal);
246
0
        }
247
0
    }
248
0
    catch(const IllegalArgumentException&)
249
0
    {
250
#ifdef DBG_UTIL
251
        OSL_FAIL( "Cannot access \"link\" checkbox" );
252
#endif
253
0
    }
254
255
0
    return false;
256
0
}
257
258
ErrCode SvxOpenGraphicDialog::GetGraphic(Graphic& rGraphic) const
259
0
{
260
0
    return mpImpl->aFileDlg.GetGraphic(rGraphic);
261
0
}
262
263
OUString SvxOpenGraphicDialog::GetPath() const
264
0
{
265
0
    return mpImpl->aFileDlg.GetPath();
266
0
}
267
268
Sequence< OUString > SvxOpenGraphicDialog::GetSelectedFiles() const
269
0
{
270
0
    return mpImpl->aFileDlg.GetSelectedFiles();
271
0
}
272
273
OUString SvxOpenGraphicDialog::GetCurrentFilter() const
274
0
{
275
0
    return mpImpl->aFileDlg.GetCurrentFilter();
276
0
}
277
278
OUString const & SvxOpenGraphicDialog::GetDetectedFilter() const
279
0
{
280
0
    return mpImpl->sDetectedFilter;
281
0
}
282
283
void SvxOpenGraphicDialog::SetCurrentFilter(const OUString& rStr)
284
0
{
285
0
    mpImpl->aFileDlg.SetCurrentFilter(rStr);
286
0
}
287
288
void SvxOpenGraphicDialog::SetDetectedFilter(const OUString& rStr)
289
0
{
290
0
    mpImpl->sDetectedFilter = rStr;
291
0
}
292
293
Reference<ui::dialogs::XFilePickerControlAccess> const & SvxOpenGraphicDialog::GetFilePickerControlAccess() const
294
0
{
295
0
    return mpImpl->xCtrlAcc;
296
0
}
297
298
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */