build fix
[LibreOffice.git] / sfx2 / source / bastyp / helper.cxx
blobeeb981e38a3fa924926b9a70eb1e872d5dcca173
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 .
21 #include "helper.hxx"
22 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
23 #include <com/sun/star/sdbc/XResultSet.hpp>
24 #include <com/sun/star/sdbc/XRow.hpp>
25 #include <com/sun/star/task/InteractionHandler.hpp>
26 #include <com/sun/star/ucb/CommandAbortedException.hpp>
27 #include <com/sun/star/ucb/IllegalIdentifierException.hpp>
28 #include <com/sun/star/ucb/NameClash.hpp>
29 #include <com/sun/star/ucb/NumberedSortingInfo.hpp>
30 #include <com/sun/star/ucb/TransferInfo.hpp>
31 #include <com/sun/star/ucb/XAnyCompareFactory.hpp>
32 #include <com/sun/star/ucb/XCommandInfo.hpp>
33 #include <com/sun/star/ucb/XContentAccess.hpp>
34 #include <com/sun/star/ucb/XDynamicResultSet.hpp>
35 #include <com/sun/star/ucb/XSortedDynamicResultSetFactory.hpp>
36 #include <com/sun/star/util/DateTime.hpp>
37 #include <com/sun/star/io/XInputStream.hpp>
38 #include <unotools/localedatawrapper.hxx>
39 #include <rtl/strbuf.hxx>
41 #include <tools/debug.hxx>
42 #include <tools/urlobj.hxx>
43 #include <tools/datetime.hxx>
44 #include <vcl/svapp.hxx>
45 #include <ucbhelper/content.hxx>
46 #include <ucbhelper/commandenvironment.hxx>
47 #include <comphelper/processfactory.hxx>
48 #include <osl/file.hxx>
49 #include <vector>
51 using namespace com::sun::star;
52 using namespace comphelper;
53 using namespace osl;
55 using ::std::vector;
58 std::vector<OUString> SfxContentHelper::GetResultSet( const OUString& rURL )
60 vector<OUString> aList;
61 try
63 ::ucbhelper::Content aCnt( rURL, uno::Reference< ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
64 uno::Reference< sdbc::XResultSet > xResultSet;
65 uno::Reference< ucb::XDynamicResultSet > xDynResultSet;
66 uno::Sequence< OUString > aProps(3);
67 OUString* pProps = aProps.getArray();
68 pProps[0] = "Title";
69 pProps[1] = "ContentType";
70 pProps[2] = "IsFolder";
72 try
74 xDynResultSet = aCnt.createDynamicCursor( aProps );
75 if ( xDynResultSet.is() )
76 xResultSet = xDynResultSet->getStaticResultSet();
78 catch( const ucb::CommandAbortedException& )
80 SAL_WARN( "sfx.bastyp", "GetResultSet: CommandAbortedException" );
82 catch( const uno::Exception& )
84 SAL_WARN( "sfx.bastyp", "GetResultSet: Any other exception" );
88 if ( xResultSet.is() )
90 uno::Reference< sdbc::XRow > xRow( xResultSet, uno::UNO_QUERY );
91 uno::Reference< ucb::XContentAccess > xContentAccess( xResultSet, uno::UNO_QUERY );
93 try
95 while ( xResultSet->next() )
97 OUString aTitle( xRow->getString(1) );
98 OUString aType( xRow->getString(2) );
99 OUString aRow = aTitle;
100 aRow += "\t";
101 aRow += aType;
102 aRow += "\t";
103 aRow += xContentAccess->queryContentIdentifierString();
104 aList.push_back( OUString( aRow ) );
107 catch( const ucb::CommandAbortedException& )
109 SAL_WARN( "sfx.bastyp", "XContentAccess::next(): CommandAbortedException" );
111 catch( const uno::Exception& )
113 SAL_WARN( "sfx.bastyp", "XContentAccess::next(): Any other exception" );
117 catch( const uno::Exception& e )
119 SAL_WARN( "sfx.bastyp", "GetResultSet: Any other exception: " << e.Message );
122 return aList;
126 std::vector< OUString > SfxContentHelper::GetHelpTreeViewContents( const OUString& rURL )
128 vector< OUString > aProperties;
131 uno::Reference< uno::XComponentContext > xContext = ::comphelper::getProcessComponentContext();
132 uno::Reference< task::XInteractionHandler > xInteractionHandler(
133 task::InteractionHandler::createWithParent(xContext, nullptr), uno::UNO_QUERY_THROW );
135 ::ucbhelper::Content aCnt( rURL, new ::ucbhelper::CommandEnvironment( xInteractionHandler, uno::Reference< ucb::XProgressHandler >() ), comphelper::getProcessComponentContext() );
136 uno::Reference< sdbc::XResultSet > xResultSet;
137 uno::Sequence< OUString > aProps(2);
138 OUString* pProps = aProps.getArray();
139 pProps[0] = "Title";
140 pProps[1] = "IsFolder";
144 uno::Reference< ucb::XDynamicResultSet > xDynResultSet;
145 xDynResultSet = aCnt.createDynamicCursor( aProps );
146 if ( xDynResultSet.is() )
147 xResultSet = xDynResultSet->getStaticResultSet();
149 catch( const ucb::CommandAbortedException& )
152 catch( const uno::Exception& )
156 if ( xResultSet.is() )
158 uno::Reference< sdbc::XRow > xRow( xResultSet, uno::UNO_QUERY );
159 uno::Reference< ucb::XContentAccess > xContentAccess( xResultSet, uno::UNO_QUERY );
163 while ( xResultSet->next() )
165 OUString aTitle( xRow->getString(1) );
166 bool bFolder = xRow->getBoolean(2);
167 OUString aRow = aTitle;
168 aRow += "\t";
169 aRow += xContentAccess->queryContentIdentifierString();
170 aRow += "\t";
171 aRow += bFolder ? OUString("1") : OUString("0");
172 aProperties.push_back( aRow );
175 catch( const ucb::CommandAbortedException& )
178 catch( const uno::Exception& )
183 catch( const uno::Exception& )
187 return aProperties;
191 OUString SfxContentHelper::GetActiveHelpString( const OUString& rURL )
193 OUStringBuffer aRet;
196 uno::Reference< uno::XComponentContext > xContext = ::comphelper::getProcessComponentContext();
197 uno::Reference< task::XInteractionHandler > xInteractionHandler(
198 task::InteractionHandler::createWithParent(xContext, nullptr), uno::UNO_QUERY_THROW );
199 ::ucbhelper::Content aCnt( rURL, new ::ucbhelper::CommandEnvironment( xInteractionHandler, uno::Reference< ucb::XProgressHandler >() ), comphelper::getProcessComponentContext() );
200 // open the "active help" stream
201 uno::Reference< io::XInputStream > xStream = aCnt.openStream();
202 // and convert it to a String
203 uno::Sequence< sal_Int8 > lData;
204 sal_Int32 nRead = xStream->readBytes( lData, 1024 );
205 while ( nRead > 0 )
207 OString sOldString( reinterpret_cast<char const *>(lData.getConstArray()), nRead );
208 OUString sString = OStringToOUString( sOldString, RTL_TEXTENCODING_UTF8 );
209 aRet.append( sString );
211 nRead = xStream->readBytes( lData, 1024 );
214 catch( const uno::Exception& )
218 return aRet.makeStringAndClear();
222 bool SfxContentHelper::IsHelpErrorDocument( const OUString& rURL )
224 bool bRet = false;
227 ::ucbhelper::Content aCnt( INetURLObject( rURL ).GetMainURL( INetURLObject::NO_DECODE ),
228 uno::Reference< ucb::XCommandEnvironment >(),
229 comphelper::getProcessComponentContext() );
230 if ( !( aCnt.getPropertyValue( "IsErrorDocument" ) >>= bRet ) )
232 SAL_WARN( "sfx.bastyp", "Property 'IsErrorDocument' is missing" );
235 catch( const uno::Exception& )
239 return bRet;
243 sal_Int64 SfxContentHelper::GetSize( const OUString& rContent )
245 sal_Int64 nSize = 0;
246 INetURLObject aObj( rContent );
247 DBG_ASSERT( aObj.GetProtocol() != INetProtocol::NotValid, "Invalid URL!" );
250 ::ucbhelper::Content aCnt( aObj.GetMainURL( INetURLObject::NO_DECODE ), uno::Reference< ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
251 aCnt.getPropertyValue( "Size" ) >>= nSize;
253 catch( const ucb::CommandAbortedException& )
255 SAL_WARN( "sfx.bastyp", "CommandAbortedException" );
257 catch( const uno::Exception& )
259 SAL_WARN( "sfx.bastyp", "Any other exception" );
261 return nSize;
264 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */