LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / sfx2 / source / bastyp / helper.cxx
blob5138a790e15a3e1b87436b3acfbd502e388bd118
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <sal/config.h>
22 #include <string_view>
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 <tools/diagnose_ex.h>
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>
40 using namespace com::sun::star;
41 using namespace comphelper;
42 using namespace osl;
44 using ::std::vector;
47 std::vector<OUString> SfxContentHelper::GetResultSet( const OUString& rURL )
49 vector<OUString> aList;
50 try
52 ::ucbhelper::Content aCnt( rURL, uno::Reference< ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
53 uno::Reference< sdbc::XResultSet > xResultSet;
54 uno::Reference< ucb::XDynamicResultSet > xDynResultSet;
56 try
58 xDynResultSet = aCnt.createDynamicCursor( { "Title", "ContentType", "IsFolder" } );
59 if ( xDynResultSet.is() )
60 xResultSet = xDynResultSet->getStaticResultSet();
62 catch( const uno::Exception& )
64 TOOLS_WARN_EXCEPTION( "sfx.bastyp", "GetResultSet" );
68 if ( xResultSet.is() )
70 uno::Reference< sdbc::XRow > xRow( xResultSet, uno::UNO_QUERY );
71 uno::Reference< ucb::XContentAccess > xContentAccess( xResultSet, uno::UNO_QUERY );
73 try
75 while ( xResultSet->next() )
77 OUString aTitle( xRow->getString(1) );
78 OUString aType( xRow->getString(2) );
79 OUString aRow = aTitle +
80 "\t" +
81 aType +
82 "\t" +
83 xContentAccess->queryContentIdentifierString();
84 aList.push_back( aRow );
87 catch( const uno::Exception& )
89 TOOLS_WARN_EXCEPTION( "sfx.bastyp", "XContentAccess::next()" );
93 catch( const uno::Exception& )
95 TOOLS_WARN_EXCEPTION( "sfx.bastyp", "GetResultSet");
98 return aList;
102 std::vector< OUString > SfxContentHelper::GetHelpTreeViewContents( const OUString& rURL )
104 vector< OUString > aProperties;
107 uno::Reference< uno::XComponentContext > xContext = ::comphelper::getProcessComponentContext();
108 uno::Reference< task::XInteractionHandler > xInteractionHandler(
109 task::InteractionHandler::createWithParent(xContext, nullptr), uno::UNO_QUERY_THROW );
111 ::ucbhelper::Content aCnt( rURL, new ::ucbhelper::CommandEnvironment( xInteractionHandler, uno::Reference< ucb::XProgressHandler >() ), comphelper::getProcessComponentContext() );
112 uno::Reference< sdbc::XResultSet > xResultSet;
116 uno::Reference< ucb::XDynamicResultSet > xDynResultSet = aCnt.createDynamicCursor( { "Title", "IsFolder" } );
117 if ( xDynResultSet.is() )
118 xResultSet = xDynResultSet->getStaticResultSet();
120 catch( const ucb::CommandAbortedException& )
123 catch( const uno::Exception& )
127 if ( xResultSet.is() )
129 uno::Reference< sdbc::XRow > xRow( xResultSet, uno::UNO_QUERY );
130 uno::Reference< ucb::XContentAccess > xContentAccess( xResultSet, uno::UNO_QUERY );
134 while ( xResultSet->next() )
136 OUString aTitle( xRow->getString(1) );
137 bool bFolder = xRow->getBoolean(2);
138 OUString aRow = aTitle + "\t" +
139 xContentAccess->queryContentIdentifierString() + "\t" +
140 (bFolder ? std::u16string_view(u"1") : std::u16string_view(u"0"));
141 aProperties.push_back( aRow );
144 catch( const ucb::CommandAbortedException& )
147 catch( const uno::Exception& )
152 catch( const uno::Exception& )
156 return aProperties;
160 OUString SfxContentHelper::GetActiveHelpString( const OUString& rURL )
162 OUStringBuffer aRet;
165 uno::Reference< uno::XComponentContext > xContext = ::comphelper::getProcessComponentContext();
166 uno::Reference< task::XInteractionHandler > xInteractionHandler(
167 task::InteractionHandler::createWithParent(xContext, nullptr), uno::UNO_QUERY_THROW );
168 ::ucbhelper::Content aCnt( rURL, new ::ucbhelper::CommandEnvironment( xInteractionHandler, uno::Reference< ucb::XProgressHandler >() ), comphelper::getProcessComponentContext() );
169 // open the "active help" stream
170 uno::Reference< io::XInputStream > xStream = aCnt.openStream();
171 // and convert it to a String
172 uno::Sequence< sal_Int8 > lData;
173 sal_Int32 nRead = xStream->readBytes( lData, 1024 );
174 while ( nRead > 0 )
176 OString sOldString( reinterpret_cast<char const *>(lData.getConstArray()), nRead );
177 OUString sString = OStringToOUString( sOldString, RTL_TEXTENCODING_UTF8 );
178 aRet.append( sString );
180 nRead = xStream->readBytes( lData, 1024 );
183 catch( const uno::Exception& )
187 return aRet.makeStringAndClear();
191 bool SfxContentHelper::IsHelpErrorDocument( const OUString& rURL )
193 bool bRet = false;
196 ::ucbhelper::Content aCnt( INetURLObject( rURL ).GetMainURL( INetURLObject::DecodeMechanism::NONE ),
197 uno::Reference< ucb::XCommandEnvironment >(),
198 comphelper::getProcessComponentContext() );
199 if ( !( aCnt.getPropertyValue( "IsErrorDocument" ) >>= bRet ) )
201 SAL_WARN( "sfx.bastyp", "Property 'IsErrorDocument' is missing" );
204 catch( const uno::Exception& )
208 return bRet;
212 sal_Int64 SfxContentHelper::GetSize( const OUString& rContent )
214 sal_Int64 nSize = 0;
215 INetURLObject aObj( rContent );
216 DBG_ASSERT( aObj.GetProtocol() != INetProtocol::NotValid, "Invalid URL!" );
219 ::ucbhelper::Content aCnt( aObj.GetMainURL( INetURLObject::DecodeMechanism::NONE ), uno::Reference< ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
220 aCnt.getPropertyValue( "Size" ) >>= nSize;
222 catch( const uno::Exception& )
224 TOOLS_WARN_EXCEPTION( "sfx.bastyp", "" );
226 return nSize;
229 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */