Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / sfx2 / source / bastyp / helper.cxx
blob50b9196950c21bee7ffcef8cc5b0a710e74cb96e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include "helper.hxx"
31 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
32 #include <com/sun/star/sdbc/XResultSet.hpp>
33 #include <com/sun/star/sdbc/XRow.hpp>
34 #include <com/sun/star/ucb/CommandAbortedException.hpp>
35 #include <com/sun/star/ucb/IllegalIdentifierException.hpp>
36 #include <com/sun/star/ucb/NameClash.hpp>
37 #include <com/sun/star/ucb/NumberedSortingInfo.hpp>
38 #include <com/sun/star/ucb/TransferInfo.hpp>
39 #include <com/sun/star/ucb/XAnyCompareFactory.hpp>
40 #include <com/sun/star/ucb/XCommandInfo.hpp>
41 #include <com/sun/star/ucb/XContentAccess.hpp>
42 #include <com/sun/star/ucb/XDynamicResultSet.hpp>
43 #include <com/sun/star/ucb/XSortedDynamicResultSetFactory.hpp>
44 #include <com/sun/star/util/DateTime.hpp>
45 #include <com/sun/star/io/XInputStream.hpp>
46 #include <unotools/localedatawrapper.hxx>
47 #include <rtl/oustringostreaminserter.hxx>
48 #include <rtl/strbuf.hxx>
50 #include <tools/debug.hxx>
51 #include <tools/urlobj.hxx>
52 #include <tools/datetime.hxx>
53 #include <vcl/svapp.hxx>
54 #include <ucbhelper/content.hxx>
55 #include <ucbhelper/commandenvironment.hxx>
56 #include <comphelper/processfactory.hxx>
57 #include <osl/file.hxx>
58 #include <vector>
60 using namespace com::sun::star;
61 using namespace comphelper;
62 using namespace osl;
64 using ::std::vector;
66 using ::rtl::OUString;
67 using ::rtl::OStringBuffer;
68 using ::rtl::OStringToOUString;
70 typedef vector< OUString* > StringList_Impl;
72 #define CONVERT_DATETIME( aUnoDT, aToolsDT ) \
73 aToolsDT = DateTime( Date( aUnoDT.Day, aUnoDT.Month, aUnoDT.Year ), \
74 Time( aUnoDT.Hours, aUnoDT.Minutes, aUnoDT.Seconds, aUnoDT.HundredthSeconds ) );
76 void AppendDateTime_Impl( const util::DateTime rDT,
77 String& rRow, const LocaleDataWrapper& rWrapper )
79 DateTime aDT( DateTime::EMPTY );
80 CONVERT_DATETIME( rDT, aDT );
81 String aDateStr = rWrapper.getDate( aDT );
82 aDateStr += String::CreateFromAscii( ", " );
83 aDateStr += rWrapper.getTime( aDT );
84 rRow += aDateStr;
87 // -----------------------------------------------------------------------
89 uno::Sequence < OUString > SfxContentHelper::GetResultSet( const String& rURL )
91 StringList_Impl* pList = NULL;
92 try
94 ::ucbhelper::Content aCnt( rURL, uno::Reference< ucb::XCommandEnvironment >() );
95 uno::Reference< sdbc::XResultSet > xResultSet;
96 uno::Reference< ucb::XDynamicResultSet > xDynResultSet;
97 uno::Sequence< OUString > aProps(3);
98 OUString* pProps = aProps.getArray();
99 pProps[0] = "Title";
100 pProps[1] = "ContentType";
101 pProps[2] = "IsFolder";
105 xDynResultSet = aCnt.createDynamicCursor( aProps, ::ucbhelper::INCLUDE_FOLDERS_AND_DOCUMENTS );
106 if ( xDynResultSet.is() )
107 xResultSet = xDynResultSet->getStaticResultSet();
109 catch( const ucb::CommandAbortedException& )
111 SAL_WARN( "sfx2.bastyp", "GetResultSet: CommandAbortedException" );
113 catch( const uno::Exception& )
115 SAL_WARN( "sfx2.bastyp", "GetResultSet: Any other exception" );
119 if ( xResultSet.is() )
121 pList = new StringList_Impl();
122 uno::Reference< sdbc::XRow > xRow( xResultSet, uno::UNO_QUERY );
123 uno::Reference< ucb::XContentAccess > xContentAccess( xResultSet, uno::UNO_QUERY );
127 while ( xResultSet->next() )
129 String aTitle( xRow->getString(1) );
130 String aType( xRow->getString(2) );
131 String aRow = aTitle;
132 aRow += '\t';
133 aRow += aType;
134 aRow += '\t';
135 aRow += String( xContentAccess->queryContentIdentifierString() );
136 OUString* pRow = new OUString( aRow );
137 pList->push_back( pRow );
140 catch( const ucb::CommandAbortedException& )
142 SAL_WARN( "sfx2.bastyp", "XContentAccess::next(): CommandAbortedException" );
144 catch( const uno::Exception& )
146 SAL_WARN( "sfx2.bastyp", "XContentAccess::next(): Any other exception" );
150 catch( const uno::Exception& e )
152 SAL_WARN( "sfx2.bastyp", "GetResultSet: Any other exception: " << e.Message );
155 if ( pList )
157 size_t nCount = pList->size();
158 uno::Sequence < OUString > aRet( nCount );
159 OUString* pRet = aRet.getArray();
160 for ( size_t i = 0; i < nCount; ++i )
162 OUString* pEntry = pList->at(i);
163 pRet[i] = *( pEntry );
164 delete pEntry;
166 pList->clear();
167 delete pList;
168 return aRet;
170 else
171 return uno::Sequence < OUString > ();
174 // -----------------------------------------------------------------------
176 uno::Sequence< OUString > SfxContentHelper::GetHelpTreeViewContents( const String& rURL )
178 StringList_Impl* pProperties = NULL;
181 uno::Reference< lang::XMultiServiceFactory > xFactory = ::comphelper::getProcessServiceFactory();
182 uno::Reference< task::XInteractionHandler > xInteractionHandler = uno::Reference< task::XInteractionHandler > (
183 xFactory->createInstance( "com.sun.star.task.InteractionHandler" ), uno::UNO_QUERY );
185 ::ucbhelper::Content aCnt( rURL, new ::ucbhelper::CommandEnvironment( xInteractionHandler, uno::Reference< ucb::XProgressHandler >() ) );
186 uno::Reference< sdbc::XResultSet > xResultSet;
187 uno::Sequence< OUString > aProps(2);
188 OUString* pProps = aProps.getArray();
189 pProps[0] = "Title";
190 pProps[1] = "IsFolder";
194 uno::Reference< ucb::XDynamicResultSet > xDynResultSet;
195 xDynResultSet = aCnt.createDynamicCursor( aProps, ::ucbhelper::INCLUDE_FOLDERS_AND_DOCUMENTS );
196 if ( xDynResultSet.is() )
197 xResultSet = xDynResultSet->getStaticResultSet();
199 catch( const ucb::CommandAbortedException& )
202 catch( const uno::Exception& )
206 if ( xResultSet.is() )
208 pProperties = new StringList_Impl();
209 uno::Reference< sdbc::XRow > xRow( xResultSet, uno::UNO_QUERY );
210 uno::Reference< ucb::XContentAccess > xContentAccess( xResultSet, uno::UNO_QUERY );
214 while ( xResultSet->next() )
216 String aTitle( xRow->getString(1) );
217 sal_Bool bFolder = xRow->getBoolean(2);
218 String aRow = aTitle;
219 aRow += '\t';
220 aRow += String( xContentAccess->queryContentIdentifierString() );
221 aRow += '\t';
222 aRow += bFolder ? '1' : '0';
223 OUString* pRow = new OUString( aRow );
224 pProperties->push_back( pRow );
227 catch( const ucb::CommandAbortedException& )
230 catch( const uno::Exception& )
235 catch( const uno::Exception& )
239 if ( pProperties )
241 size_t nCount = pProperties->size();
242 uno::Sequence < OUString > aRet( nCount );
243 OUString* pRet = aRet.getArray();
244 for ( size_t i = 0; i < nCount; ++i )
246 OUString* pProperty = pProperties->at(i);
247 pRet[i] = *( pProperty );
248 delete pProperty;
250 pProperties->clear();
251 delete pProperties;
252 return aRet;
254 else
255 return uno::Sequence < OUString > ();
258 // -----------------------------------------------------------------------
260 String SfxContentHelper::GetActiveHelpString( const String& rURL )
262 String aRet;
265 uno::Reference< lang::XMultiServiceFactory > xFactory = ::comphelper::getProcessServiceFactory();
266 uno::Reference< task::XInteractionHandler > xInteractionHandler = uno::Reference< task::XInteractionHandler > (
267 xFactory->createInstance( "com.sun.star.task.InteractionHandler" ), uno::UNO_QUERY );
268 ::ucbhelper::Content aCnt( rURL, new ::ucbhelper::CommandEnvironment( xInteractionHandler, uno::Reference< ucb::XProgressHandler >() ) );
269 // open the "active help" stream
270 uno::Reference< io::XInputStream > xStream = aCnt.openStream();
271 // and convert it to a String
272 uno::Sequence< sal_Int8 > lData;
273 sal_Int32 nRead = xStream->readBytes( lData, 1024 );
274 while ( nRead > 0 )
276 OStringBuffer sBuffer( nRead );
277 for( sal_Int32 i = 0; i < nRead; ++i )
278 sBuffer.append( (sal_Char)lData[i] );
279 OUString sString = OStringToOUString( sBuffer.makeStringAndClear(), RTL_TEXTENCODING_UTF8 );
280 aRet += String( sString );
282 nRead = xStream->readBytes( lData, 1024 );
285 catch( const uno::Exception& )
289 return aRet;
292 // -----------------------------------------------------------------------
294 sal_Bool SfxContentHelper::IsHelpErrorDocument( const String& rURL )
296 sal_Bool bRet = sal_False;
299 ::ucbhelper::Content aCnt( INetURLObject( rURL ).GetMainURL( INetURLObject::NO_DECODE ),
300 uno::Reference< ucb::XCommandEnvironment > () );
301 if ( !( aCnt.getPropertyValue( "IsErrorDocument" ) >>= bRet ) )
303 SAL_WARN( "sfx2.bastyp", "Property 'IsErrorDocument' is missing" );
306 catch( const uno::Exception& )
310 return bRet;
313 // -----------------------------------------------------------------------
315 sal_uIntPtr SfxContentHelper::GetSize( const String& rContent )
317 sal_uIntPtr nSize = 0;
318 sal_Int64 nTemp = 0;
319 INetURLObject aObj( rContent );
320 DBG_ASSERT( aObj.GetProtocol() != INET_PROT_NOT_VALID, "Invalid URL!" );
323 ::ucbhelper::Content aCnt( aObj.GetMainURL( INetURLObject::NO_DECODE ), uno::Reference< ucb::XCommandEnvironment > () );
324 aCnt.getPropertyValue( "Size" ) >>= nTemp;
326 catch( const ucb::CommandAbortedException& )
328 SAL_WARN( "sfx2.bastyp", "CommandAbortedException" );
330 catch( const uno::Exception& )
332 SAL_WARN( "sfx2.bastyp", "Any other exception" );
334 nSize = (sal_uInt32)nTemp;
335 return nSize;
338 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */