LibreOffice Module sfx2 (master) 1
helper.cxx
Go to the documentation of this file.
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>
34#include <tools/debug.hxx>
35#include <tools/urlobj.hxx>
36#include <ucbhelper/content.hxx>
39
40using namespace com::sun::star;
41using namespace comphelper;
42using namespace osl;
43
44using ::std::vector;
45
46
47std::vector<OUString> SfxContentHelper::GetResultSet( const OUString& rURL )
48{
49 vector<OUString> aList;
50 try
51 {
52 ::ucbhelper::Content aCnt( rURL, uno::Reference< ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
53 uno::Reference< sdbc::XResultSet > xResultSet;
54 uno::Reference< ucb::XDynamicResultSet > xDynResultSet;
55
56 try
57 {
58 xDynResultSet = aCnt.createDynamicCursor( { "Title", "ContentType", "IsFolder" } );
59 if ( xDynResultSet.is() )
60 xResultSet = xDynResultSet->getStaticResultSet();
61 }
62 catch( const uno::Exception& )
63 {
64 TOOLS_WARN_EXCEPTION( "sfx.bastyp", "GetResultSet" );
65 }
66
67
68 if ( xResultSet.is() )
69 {
70 uno::Reference< sdbc::XRow > xRow( xResultSet, uno::UNO_QUERY );
71 uno::Reference< ucb::XContentAccess > xContentAccess( xResultSet, uno::UNO_QUERY );
72
73 try
74 {
75 while ( xResultSet->next() )
76 {
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 );
85 }
86 }
87 catch( const uno::Exception& )
88 {
89 TOOLS_WARN_EXCEPTION( "sfx.bastyp", "XContentAccess::next()" );
90 }
91 }
92 }
93 catch( const uno::Exception& )
94 {
95 TOOLS_WARN_EXCEPTION( "sfx.bastyp", "GetResultSet");
96 }
97
98 return aList;
99}
100
101
102std::vector< OUString > SfxContentHelper::GetHelpTreeViewContents( const OUString& rURL )
103{
104 vector< OUString > aProperties;
105 try
106 {
107 uno::Reference< uno::XComponentContext > xContext = ::comphelper::getProcessComponentContext();
108 uno::Reference< task::XInteractionHandler > xInteractionHandler(
109 task::InteractionHandler::createWithParent(xContext, nullptr), uno::UNO_QUERY_THROW );
110
111 ::ucbhelper::Content aCnt( rURL, new ::ucbhelper::CommandEnvironment( xInteractionHandler, uno::Reference< ucb::XProgressHandler >() ), comphelper::getProcessComponentContext() );
112 uno::Reference< sdbc::XResultSet > xResultSet;
113
114 try
115 {
116 uno::Reference< ucb::XDynamicResultSet > xDynResultSet = aCnt.createDynamicCursor( { "Title", "IsFolder" } );
117 if ( xDynResultSet.is() )
118 xResultSet = xDynResultSet->getStaticResultSet();
119 }
120 catch( const ucb::CommandAbortedException& )
121 {
122 }
123 catch( const uno::Exception& )
124 {
125 }
126
127 if ( xResultSet.is() )
128 {
129 uno::Reference< sdbc::XRow > xRow( xResultSet, uno::UNO_QUERY );
130 uno::Reference< ucb::XContentAccess > xContentAccess( xResultSet, uno::UNO_QUERY );
131
132 try
133 {
134 while ( xResultSet->next() )
135 {
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 );
142 }
143 }
144 catch( const ucb::CommandAbortedException& )
145 {
146 }
147 catch( const uno::Exception& )
148 {
149 }
150 }
151 }
152 catch( const uno::Exception& )
153 {
154 }
155
156 return aProperties;
157}
158
159
160OUString SfxContentHelper::GetActiveHelpString( const OUString& rURL )
161{
162 OUStringBuffer aRet;
163 try
164 {
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 )
175 {
176 std::string_view sOldString( reinterpret_cast<char const *>(lData.getConstArray()), nRead );
177 OUString sString = OStringToOUString( sOldString, RTL_TEXTENCODING_UTF8 );
178 aRet.append( sString );
179
180 nRead = xStream->readBytes( lData, 1024 );
181 }
182 }
183 catch( const uno::Exception& )
184 {
185 }
186
187 return aRet.makeStringAndClear();
188}
189
190
191bool SfxContentHelper::IsHelpErrorDocument( std::u16string_view rURL )
192{
193 bool bRet = false;
194 try
195 {
197 uno::Reference< ucb::XCommandEnvironment >(),
199 if ( !( aCnt.getPropertyValue( "IsErrorDocument" ) >>= bRet ) )
200 {
201 SAL_WARN( "sfx.bastyp", "Property 'IsErrorDocument' is missing" );
202 }
203 }
204 catch( const uno::Exception& )
205 {
206 }
207
208 return bRet;
209}
210
211
212sal_Int64 SfxContentHelper::GetSize( std::u16string_view rContent )
213{
214 sal_Int64 nSize = 0;
215 INetURLObject aObj( rContent );
216 DBG_ASSERT( aObj.GetProtocol() != INetProtocol::NotValid, "Invalid URL!" );
217 try
218 {
219 ::ucbhelper::Content aCnt( aObj.GetMainURL( INetURLObject::DecodeMechanism::NONE ), uno::Reference< ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
220 aCnt.getPropertyValue( "Size" ) >>= nSize;
221 }
222 catch( const uno::Exception& )
223 {
224 TOOLS_WARN_EXCEPTION( "sfx.bastyp", "" );
225 }
226 return nSize;
227}
228
229/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
PropertiesInfo aProperties
Reference< XInputStream > xStream
OUString GetMainURL(DecodeMechanism eMechanism, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
INetProtocol GetProtocol() const
static OUString GetActiveHelpString(const OUString &rURL)
Definition: helper.cxx:160
static sal_Int64 GetSize(std::u16string_view rContent)
Definition: helper.cxx:212
static std::vector< OUString > GetResultSet(const OUString &rURL)
Definition: helper.cxx:47
static std::vector< OUString > GetHelpTreeViewContents(const OUString &rURL)
Definition: helper.cxx:102
static bool IsHelpErrorDocument(std::u16string_view rURL)
Definition: helper.cxx:191
css::uno::Any getPropertyValue(const OUString &rPropertyName)
css::uno::Reference< css::io::XInputStream > openStream()
css::uno::Reference< css::ucb::XDynamicResultSet > createDynamicCursor(const css::uno::Sequence< OUString > &rPropertyNames, ResultSetInclude eMode=INCLUDE_FOLDERS_AND_DOCUMENTS)
#define DBG_ASSERT(sCon, aError)
#define TOOLS_WARN_EXCEPTION(area, stream)
float u
#define SAL_WARN(area, stream)
Reference< XComponentContext > getProcessComponentContext()