Coverage Report

Created: 2025-11-16 09:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sfx2/source/bastyp/helper.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
#include <sal/config.h>
21
22
#include <string_view>
23
24
#include <helper.hxx>
25
#include <com/sun/star/sdbc/XResultSet.hpp>
26
#include <com/sun/star/sdbc/XRow.hpp>
27
#include <com/sun/star/task/InteractionHandler.hpp>
28
#include <com/sun/star/ucb/CommandAbortedException.hpp>
29
#include <com/sun/star/ucb/XContentAccess.hpp>
30
#include <com/sun/star/ucb/XDynamicResultSet.hpp>
31
#include <com/sun/star/io/XInputStream.hpp>
32
#include <sal/log.hxx>
33
#include <comphelper/diagnose_ex.hxx>
34
#include <tools/debug.hxx>
35
#include <tools/urlobj.hxx>
36
#include <ucbhelper/content.hxx>
37
#include <ucbhelper/commandenvironment.hxx>
38
#include <comphelper/processfactory.hxx>
39
40
using namespace com::sun::star;
41
using namespace comphelper;
42
43
using ::std::vector;
44
45
46
std::vector<OUString> SfxContentHelper::GetResultSet( const OUString& rURL )
47
0
{
48
0
    vector<OUString> aList;
49
0
    try
50
0
    {
51
0
        ::ucbhelper::Content aCnt( rURL, uno::Reference< ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
52
0
        uno::Reference< sdbc::XResultSet > xResultSet;
53
0
        uno::Reference< ucb::XDynamicResultSet > xDynResultSet;
54
55
0
        try
56
0
        {
57
0
            xDynResultSet = aCnt.createDynamicCursor( { u"Title"_ustr, u"ContentType"_ustr, u"IsFolder"_ustr } );
58
0
            if ( xDynResultSet.is() )
59
0
                xResultSet = xDynResultSet->getStaticResultSet();
60
0
        }
61
0
        catch( const uno::Exception& )
62
0
        {
63
0
            TOOLS_WARN_EXCEPTION( "sfx.bastyp", "GetResultSet" );
64
0
        }
65
66
67
0
        if ( xResultSet.is() )
68
0
        {
69
0
            uno::Reference< sdbc::XRow > xRow( xResultSet, uno::UNO_QUERY );
70
0
            uno::Reference< ucb::XContentAccess > xContentAccess( xResultSet, uno::UNO_QUERY );
71
72
0
            try
73
0
            {
74
0
                while ( xResultSet->next() )
75
0
                {
76
0
                    OUString aTitle( xRow->getString(1) );
77
0
                    OUString aType( xRow->getString(2) );
78
0
                    OUString aRow = aTitle +
79
0
                        "\t" +
80
0
                        aType +
81
0
                        "\t" +
82
0
                        xContentAccess->queryContentIdentifierString();
83
0
                    aList.push_back( aRow );
84
0
                }
85
0
            }
86
0
            catch( const uno::Exception& )
87
0
            {
88
0
                TOOLS_WARN_EXCEPTION( "sfx.bastyp", "XContentAccess::next()" );
89
0
            }
90
0
        }
91
0
    }
92
0
    catch( const uno::Exception& )
93
0
    {
94
0
        TOOLS_WARN_EXCEPTION( "sfx.bastyp", "GetResultSet");
95
0
    }
96
97
0
    return aList;
98
0
}
99
100
101
std::vector< OUString > SfxContentHelper::GetHelpTreeViewContents( const OUString& rURL )
102
0
{
103
0
    vector< OUString > aProperties;
104
0
    try
105
0
    {
106
0
        const uno::Reference< uno::XComponentContext >& xContext = ::comphelper::getProcessComponentContext();
107
0
        uno::Reference< task::XInteractionHandler > xInteractionHandler(
108
0
            task::InteractionHandler::createWithParent(xContext, nullptr), uno::UNO_QUERY_THROW );
109
110
0
        ::ucbhelper::Content aCnt( rURL, new ::ucbhelper::CommandEnvironment( xInteractionHandler, uno::Reference< ucb::XProgressHandler >() ), comphelper::getProcessComponentContext() );
111
0
        uno::Reference< sdbc::XResultSet > xResultSet;
112
113
0
        try
114
0
        {
115
0
            uno::Reference< ucb::XDynamicResultSet > xDynResultSet = aCnt.createDynamicCursor( { u"Title"_ustr, u"IsFolder"_ustr } );
116
0
            if ( xDynResultSet.is() )
117
0
                xResultSet = xDynResultSet->getStaticResultSet();
118
0
        }
119
0
        catch( const ucb::CommandAbortedException& )
120
0
        {
121
0
        }
122
0
        catch( const uno::Exception& )
123
0
        {
124
0
        }
125
126
0
        if ( xResultSet.is() )
127
0
        {
128
0
            uno::Reference< sdbc::XRow > xRow( xResultSet, uno::UNO_QUERY );
129
0
            uno::Reference< ucb::XContentAccess > xContentAccess( xResultSet, uno::UNO_QUERY );
130
131
0
            try
132
0
            {
133
0
                while ( xResultSet->next() )
134
0
                {
135
0
                    OUString aTitle( xRow->getString(1) );
136
0
                    bool bFolder = xRow->getBoolean(2);
137
0
                    OUString aRow = aTitle + "\t" +
138
0
                        xContentAccess->queryContentIdentifierString() + "\t" +
139
0
                        (bFolder ? std::u16string_view(u"1") : std::u16string_view(u"0"));
140
0
                    aProperties.push_back( aRow );
141
0
                }
142
0
            }
143
0
            catch( const ucb::CommandAbortedException& )
144
0
            {
145
0
            }
146
0
            catch( const uno::Exception& )
147
0
            {
148
0
            }
149
0
        }
150
0
    }
151
0
    catch( const uno::Exception& )
152
0
    {
153
0
    }
154
155
0
    return aProperties;
156
0
}
157
158
159
OUString SfxContentHelper::GetActiveHelpString( const OUString& rURL )
160
0
{
161
0
    OUStringBuffer aRet;
162
0
    try
163
0
    {
164
0
        const uno::Reference< uno::XComponentContext >& xContext = ::comphelper::getProcessComponentContext();
165
0
        uno::Reference< task::XInteractionHandler > xInteractionHandler(
166
0
            task::InteractionHandler::createWithParent(xContext, nullptr), uno::UNO_QUERY_THROW );
167
0
        ::ucbhelper::Content aCnt( rURL, new ::ucbhelper::CommandEnvironment( xInteractionHandler, uno::Reference< ucb::XProgressHandler >() ), comphelper::getProcessComponentContext() );
168
        // open the "active help" stream
169
0
        uno::Reference< io::XInputStream > xStream = aCnt.openStream();
170
        // and convert it to a String
171
0
        uno::Sequence< sal_Int8 > lData;
172
0
        sal_Int32 nRead = xStream->readBytes( lData, 1024 );
173
0
        while ( nRead > 0 )
174
0
        {
175
0
            std::string_view sOldString( reinterpret_cast<char const *>(lData.getConstArray()), nRead );
176
0
            OUString sString = OStringToOUString( sOldString, RTL_TEXTENCODING_UTF8 );
177
0
            aRet.append( sString );
178
179
0
            nRead = xStream->readBytes( lData, 1024 );
180
0
        }
181
0
    }
182
0
    catch( const uno::Exception& )
183
0
    {
184
0
    }
185
186
0
    return aRet.makeStringAndClear();
187
0
}
188
189
190
bool SfxContentHelper::IsHelpErrorDocument( std::u16string_view rURL )
191
0
{
192
0
    bool bRet = false;
193
0
    try
194
0
    {
195
0
        ::ucbhelper::Content aCnt( INetURLObject( rURL ).GetMainURL( INetURLObject::DecodeMechanism::NONE ),
196
0
                      uno::Reference< ucb::XCommandEnvironment >(),
197
0
                      comphelper::getProcessComponentContext() );
198
0
        if ( !( aCnt.getPropertyValue( u"IsErrorDocument"_ustr ) >>= bRet ) )
199
0
        {
200
0
            SAL_WARN( "sfx.bastyp", "Property 'IsErrorDocument' is missing" );
201
0
        }
202
0
    }
203
0
    catch( const uno::Exception& )
204
0
    {
205
0
    }
206
207
0
    return bRet;
208
0
}
209
210
211
sal_Int64 SfxContentHelper::GetSize( std::u16string_view rContent )
212
0
{
213
0
    sal_Int64 nSize = 0;
214
0
    INetURLObject aObj( rContent );
215
0
    DBG_ASSERT( aObj.GetProtocol() != INetProtocol::NotValid, "Invalid URL!" );
216
0
    try
217
0
    {
218
0
        ::ucbhelper::Content aCnt( aObj.GetMainURL( INetURLObject::DecodeMechanism::NONE ), uno::Reference< ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
219
0
        aCnt.getPropertyValue( u"Size"_ustr ) >>= nSize;
220
0
    }
221
0
    catch( const uno::Exception& )
222
0
    {
223
0
        TOOLS_WARN_EXCEPTION( "sfx.bastyp", "" );
224
0
    }
225
0
    return nSize;
226
0
}
227
228
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */