Bump version to 4.1-6
[LibreOffice.git] / sfx2 / source / bastyp / helper.cxx
blob24b5de1b3a80749fd5c048205e56d35ceba7ec72
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, ::ucbhelper::INCLUDE_FOLDERS_AND_DOCUMENTS );
75 if ( xDynResultSet.is() )
76 xResultSet = xDynResultSet->getStaticResultSet();
78 catch( const ucb::CommandAbortedException& )
80 SAL_WARN( "sfx2.bastyp", "GetResultSet: CommandAbortedException" );
82 catch( const uno::Exception& )
84 SAL_WARN( "sfx2.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 String aTitle( xRow->getString(1) );
98 String aType( xRow->getString(2) );
99 String aRow = aTitle;
100 aRow += '\t';
101 aRow += aType;
102 aRow += '\t';
103 aRow += String( xContentAccess->queryContentIdentifierString() );
104 aList.push_back( OUString( aRow ) );
107 catch( const ucb::CommandAbortedException& )
109 SAL_WARN( "sfx2.bastyp", "XContentAccess::next(): CommandAbortedException" );
111 catch( const uno::Exception& )
113 SAL_WARN( "sfx2.bastyp", "XContentAccess::next(): Any other exception" );
117 catch( const uno::Exception& e )
119 SAL_WARN( "sfx2.bastyp", "GetResultSet: Any other exception: " << e.Message );
122 return aList;
125 // -----------------------------------------------------------------------
127 std::vector< OUString > SfxContentHelper::GetHelpTreeViewContents( const OUString& rURL )
129 vector< OUString > aProperties;
132 uno::Reference< uno::XComponentContext > xContext = ::comphelper::getProcessComponentContext();
133 uno::Reference< task::XInteractionHandler > xInteractionHandler(
134 task::InteractionHandler::createWithParent(xContext, 0), uno::UNO_QUERY_THROW );
136 ::ucbhelper::Content aCnt( rURL, new ::ucbhelper::CommandEnvironment( xInteractionHandler, uno::Reference< ucb::XProgressHandler >() ), comphelper::getProcessComponentContext() );
137 uno::Reference< sdbc::XResultSet > xResultSet;
138 uno::Sequence< OUString > aProps(2);
139 OUString* pProps = aProps.getArray();
140 pProps[0] = "Title";
141 pProps[1] = "IsFolder";
145 uno::Reference< ucb::XDynamicResultSet > xDynResultSet;
146 xDynResultSet = aCnt.createDynamicCursor( aProps, ::ucbhelper::INCLUDE_FOLDERS_AND_DOCUMENTS );
147 if ( xDynResultSet.is() )
148 xResultSet = xDynResultSet->getStaticResultSet();
150 catch( const ucb::CommandAbortedException& )
153 catch( const uno::Exception& )
157 if ( xResultSet.is() )
159 uno::Reference< sdbc::XRow > xRow( xResultSet, uno::UNO_QUERY );
160 uno::Reference< ucb::XContentAccess > xContentAccess( xResultSet, uno::UNO_QUERY );
164 while ( xResultSet->next() )
166 String aTitle( xRow->getString(1) );
167 sal_Bool bFolder = xRow->getBoolean(2);
168 String aRow = aTitle;
169 aRow += '\t';
170 aRow += String( xContentAccess->queryContentIdentifierString() );
171 aRow += '\t';
172 aRow += bFolder ? '1' : '0';
173 aProperties.push_back( OUString( aRow ) );
176 catch( const ucb::CommandAbortedException& )
179 catch( const uno::Exception& )
184 catch( const uno::Exception& )
188 return aProperties;
191 // -----------------------------------------------------------------------
193 OUString SfxContentHelper::GetActiveHelpString( const OUString& rURL )
195 OUStringBuffer aRet;
198 uno::Reference< uno::XComponentContext > xContext = ::comphelper::getProcessComponentContext();
199 uno::Reference< task::XInteractionHandler > xInteractionHandler(
200 task::InteractionHandler::createWithParent(xContext, 0), uno::UNO_QUERY_THROW );
201 ::ucbhelper::Content aCnt( rURL, new ::ucbhelper::CommandEnvironment( xInteractionHandler, uno::Reference< ucb::XProgressHandler >() ), comphelper::getProcessComponentContext() );
202 // open the "active help" stream
203 uno::Reference< io::XInputStream > xStream = aCnt.openStream();
204 // and convert it to a String
205 uno::Sequence< sal_Int8 > lData;
206 sal_Int32 nRead = xStream->readBytes( lData, 1024 );
207 while ( nRead > 0 )
209 OString sOldString( (sal_Char*)lData.getConstArray(), nRead );
210 OUString sString = OStringToOUString( sOldString, RTL_TEXTENCODING_UTF8 );
211 aRet.append( sString );
213 nRead = xStream->readBytes( lData, 1024 );
216 catch( const uno::Exception& )
220 return aRet.makeStringAndClear();
223 // -----------------------------------------------------------------------
225 bool SfxContentHelper::IsHelpErrorDocument( const OUString& rURL )
227 bool bRet = false;
230 ::ucbhelper::Content aCnt( INetURLObject( rURL ).GetMainURL( INetURLObject::NO_DECODE ),
231 uno::Reference< ucb::XCommandEnvironment >(),
232 comphelper::getProcessComponentContext() );
233 if ( !( aCnt.getPropertyValue( "IsErrorDocument" ) >>= bRet ) )
235 SAL_WARN( "sfx2.bastyp", "Property 'IsErrorDocument' is missing" );
238 catch( const uno::Exception& )
242 return bRet;
245 // -----------------------------------------------------------------------
247 sal_Int64 SfxContentHelper::GetSize( const OUString& rContent )
249 sal_Int64 nSize = 0;
250 INetURLObject aObj( rContent );
251 DBG_ASSERT( aObj.GetProtocol() != INET_PROT_NOT_VALID, "Invalid URL!" );
254 ::ucbhelper::Content aCnt( aObj.GetMainURL( INetURLObject::NO_DECODE ), uno::Reference< ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
255 aCnt.getPropertyValue( "Size" ) >>= nSize;
257 catch( const ucb::CommandAbortedException& )
259 SAL_WARN( "sfx2.bastyp", "CommandAbortedException" );
261 catch( const uno::Exception& )
263 SAL_WARN( "sfx2.bastyp", "Any other exception" );
265 return nSize;
268 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */