Line data Source code
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 :
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>
40 :
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>
50 :
51 : using namespace com::sun::star;
52 : using namespace comphelper;
53 : using namespace osl;
54 :
55 : using ::std::vector;
56 :
57 : using ::rtl::OUString;
58 : using ::rtl::OStringBuffer;
59 : using ::rtl::OStringToOUString;
60 :
61 : typedef vector< OUString* > StringList_Impl;
62 :
63 : #define CONVERT_DATETIME( aUnoDT, aToolsDT ) \
64 : aToolsDT = DateTime( Date( aUnoDT.Day, aUnoDT.Month, aUnoDT.Year ), \
65 : Time( aUnoDT.Hours, aUnoDT.Minutes, aUnoDT.Seconds, aUnoDT.HundredthSeconds ) );
66 :
67 0 : void AppendDateTime_Impl( const util::DateTime rDT,
68 : String& rRow, const LocaleDataWrapper& rWrapper )
69 : {
70 0 : DateTime aDT( DateTime::EMPTY );
71 0 : CONVERT_DATETIME( rDT, aDT );
72 0 : String aDateStr = rWrapper.getDate( aDT );
73 0 : aDateStr += rtl::OUString(", ");
74 0 : aDateStr += rWrapper.getTime( aDT );
75 0 : rRow += aDateStr;
76 0 : }
77 :
78 : // -----------------------------------------------------------------------
79 :
80 19 : uno::Sequence < OUString > SfxContentHelper::GetResultSet( const String& rURL )
81 : {
82 19 : StringList_Impl* pList = NULL;
83 : try
84 : {
85 38 : ::ucbhelper::Content aCnt( rURL, uno::Reference< ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
86 0 : uno::Reference< sdbc::XResultSet > xResultSet;
87 0 : uno::Reference< ucb::XDynamicResultSet > xDynResultSet;
88 0 : uno::Sequence< OUString > aProps(3);
89 0 : OUString* pProps = aProps.getArray();
90 0 : pProps[0] = "Title";
91 0 : pProps[1] = "ContentType";
92 0 : pProps[2] = "IsFolder";
93 :
94 : try
95 : {
96 0 : xDynResultSet = aCnt.createDynamicCursor( aProps, ::ucbhelper::INCLUDE_FOLDERS_AND_DOCUMENTS );
97 0 : if ( xDynResultSet.is() )
98 0 : xResultSet = xDynResultSet->getStaticResultSet();
99 : }
100 0 : catch( const ucb::CommandAbortedException& )
101 : {
102 : SAL_WARN( "sfx2.bastyp", "GetResultSet: CommandAbortedException" );
103 : }
104 0 : catch( const uno::Exception& )
105 : {
106 : SAL_WARN( "sfx2.bastyp", "GetResultSet: Any other exception" );
107 : }
108 :
109 :
110 0 : if ( xResultSet.is() )
111 : {
112 0 : pList = new StringList_Impl();
113 0 : uno::Reference< sdbc::XRow > xRow( xResultSet, uno::UNO_QUERY );
114 0 : uno::Reference< ucb::XContentAccess > xContentAccess( xResultSet, uno::UNO_QUERY );
115 :
116 : try
117 : {
118 0 : while ( xResultSet->next() )
119 : {
120 0 : String aTitle( xRow->getString(1) );
121 0 : String aType( xRow->getString(2) );
122 0 : String aRow = aTitle;
123 0 : aRow += '\t';
124 0 : aRow += aType;
125 0 : aRow += '\t';
126 0 : aRow += String( xContentAccess->queryContentIdentifierString() );
127 0 : OUString* pRow = new OUString( aRow );
128 0 : pList->push_back( pRow );
129 0 : }
130 : }
131 0 : catch( const ucb::CommandAbortedException& )
132 : {
133 : SAL_WARN( "sfx2.bastyp", "XContentAccess::next(): CommandAbortedException" );
134 : }
135 0 : catch( const uno::Exception& )
136 : {
137 : SAL_WARN( "sfx2.bastyp", "XContentAccess::next(): Any other exception" );
138 0 : }
139 0 : }
140 : }
141 19 : catch( const uno::Exception& e )
142 : {
143 : SAL_WARN( "sfx2.bastyp", "GetResultSet: Any other exception: " << e.Message );
144 : }
145 :
146 19 : if ( pList )
147 : {
148 0 : size_t nCount = pList->size();
149 0 : uno::Sequence < OUString > aRet( nCount );
150 0 : OUString* pRet = aRet.getArray();
151 0 : for ( size_t i = 0; i < nCount; ++i )
152 : {
153 0 : OUString* pEntry = pList->at(i);
154 0 : pRet[i] = *( pEntry );
155 0 : delete pEntry;
156 : }
157 0 : pList->clear();
158 0 : delete pList;
159 0 : return aRet;
160 : }
161 : else
162 19 : return uno::Sequence < OUString > ();
163 : }
164 :
165 : // -----------------------------------------------------------------------
166 :
167 0 : uno::Sequence< OUString > SfxContentHelper::GetHelpTreeViewContents( const String& rURL )
168 : {
169 0 : StringList_Impl* pProperties = NULL;
170 : try
171 : {
172 0 : uno::Reference< uno::XComponentContext > xContext = ::comphelper::getProcessComponentContext();
173 : uno::Reference< task::XInteractionHandler > xInteractionHandler(
174 0 : task::InteractionHandler::createWithParent(xContext, 0), uno::UNO_QUERY_THROW );
175 :
176 0 : ::ucbhelper::Content aCnt( rURL, new ::ucbhelper::CommandEnvironment( xInteractionHandler, uno::Reference< ucb::XProgressHandler >() ), comphelper::getProcessComponentContext() );
177 0 : uno::Reference< sdbc::XResultSet > xResultSet;
178 0 : uno::Sequence< OUString > aProps(2);
179 0 : OUString* pProps = aProps.getArray();
180 0 : pProps[0] = "Title";
181 0 : pProps[1] = "IsFolder";
182 :
183 : try
184 : {
185 0 : uno::Reference< ucb::XDynamicResultSet > xDynResultSet;
186 0 : xDynResultSet = aCnt.createDynamicCursor( aProps, ::ucbhelper::INCLUDE_FOLDERS_AND_DOCUMENTS );
187 0 : if ( xDynResultSet.is() )
188 0 : xResultSet = xDynResultSet->getStaticResultSet();
189 : }
190 0 : catch( const ucb::CommandAbortedException& )
191 : {
192 : }
193 0 : catch( const uno::Exception& )
194 : {
195 : }
196 :
197 0 : if ( xResultSet.is() )
198 : {
199 0 : pProperties = new StringList_Impl();
200 0 : uno::Reference< sdbc::XRow > xRow( xResultSet, uno::UNO_QUERY );
201 0 : uno::Reference< ucb::XContentAccess > xContentAccess( xResultSet, uno::UNO_QUERY );
202 :
203 : try
204 : {
205 0 : while ( xResultSet->next() )
206 : {
207 0 : String aTitle( xRow->getString(1) );
208 0 : sal_Bool bFolder = xRow->getBoolean(2);
209 0 : String aRow = aTitle;
210 0 : aRow += '\t';
211 0 : aRow += String( xContentAccess->queryContentIdentifierString() );
212 0 : aRow += '\t';
213 0 : aRow += bFolder ? '1' : '0';
214 0 : OUString* pRow = new OUString( aRow );
215 0 : pProperties->push_back( pRow );
216 0 : }
217 : }
218 0 : catch( const ucb::CommandAbortedException& )
219 : {
220 : }
221 0 : catch( const uno::Exception& )
222 : {
223 0 : }
224 0 : }
225 : }
226 0 : catch( const uno::Exception& )
227 : {
228 : }
229 :
230 0 : if ( pProperties )
231 : {
232 0 : size_t nCount = pProperties->size();
233 0 : uno::Sequence < OUString > aRet( nCount );
234 0 : OUString* pRet = aRet.getArray();
235 0 : for ( size_t i = 0; i < nCount; ++i )
236 : {
237 0 : OUString* pProperty = pProperties->at(i);
238 0 : pRet[i] = *( pProperty );
239 0 : delete pProperty;
240 : }
241 0 : pProperties->clear();
242 0 : delete pProperties;
243 0 : return aRet;
244 : }
245 : else
246 0 : return uno::Sequence < OUString > ();
247 : }
248 :
249 : // -----------------------------------------------------------------------
250 :
251 0 : String SfxContentHelper::GetActiveHelpString( const String& rURL )
252 : {
253 0 : String aRet;
254 : try
255 : {
256 0 : uno::Reference< uno::XComponentContext > xContext = ::comphelper::getProcessComponentContext();
257 : uno::Reference< task::XInteractionHandler > xInteractionHandler(
258 0 : task::InteractionHandler::createWithParent(xContext, 0), uno::UNO_QUERY_THROW );
259 0 : ::ucbhelper::Content aCnt( rURL, new ::ucbhelper::CommandEnvironment( xInteractionHandler, uno::Reference< ucb::XProgressHandler >() ), comphelper::getProcessComponentContext() );
260 : // open the "active help" stream
261 0 : uno::Reference< io::XInputStream > xStream = aCnt.openStream();
262 : // and convert it to a String
263 0 : uno::Sequence< sal_Int8 > lData;
264 0 : sal_Int32 nRead = xStream->readBytes( lData, 1024 );
265 0 : while ( nRead > 0 )
266 : {
267 0 : OStringBuffer sBuffer( nRead );
268 0 : for( sal_Int32 i = 0; i < nRead; ++i )
269 0 : sBuffer.append( (sal_Char)lData[i] );
270 0 : OUString sString = OStringToOUString( sBuffer.makeStringAndClear(), RTL_TEXTENCODING_UTF8 );
271 0 : aRet += String( sString );
272 :
273 0 : nRead = xStream->readBytes( lData, 1024 );
274 0 : }
275 : }
276 0 : catch( const uno::Exception& )
277 : {
278 : }
279 :
280 0 : return aRet;
281 : }
282 :
283 : // -----------------------------------------------------------------------
284 :
285 0 : sal_Bool SfxContentHelper::IsHelpErrorDocument( const String& rURL )
286 : {
287 0 : sal_Bool bRet = sal_False;
288 : try
289 : {
290 : ::ucbhelper::Content aCnt( INetURLObject( rURL ).GetMainURL( INetURLObject::NO_DECODE ),
291 : uno::Reference< ucb::XCommandEnvironment >(),
292 0 : comphelper::getProcessComponentContext() );
293 0 : if ( !( aCnt.getPropertyValue( "IsErrorDocument" ) >>= bRet ) )
294 : {
295 : SAL_WARN( "sfx2.bastyp", "Property 'IsErrorDocument' is missing" );
296 0 : }
297 : }
298 0 : catch( const uno::Exception& )
299 : {
300 : }
301 :
302 0 : return bRet;
303 : }
304 :
305 : // -----------------------------------------------------------------------
306 :
307 0 : sal_uIntPtr SfxContentHelper::GetSize( const String& rContent )
308 : {
309 0 : sal_uIntPtr nSize = 0;
310 0 : sal_Int64 nTemp = 0;
311 0 : INetURLObject aObj( rContent );
312 : DBG_ASSERT( aObj.GetProtocol() != INET_PROT_NOT_VALID, "Invalid URL!" );
313 : try
314 : {
315 0 : ::ucbhelper::Content aCnt( aObj.GetMainURL( INetURLObject::NO_DECODE ), uno::Reference< ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
316 0 : aCnt.getPropertyValue( "Size" ) >>= nTemp;
317 : }
318 0 : catch( const ucb::CommandAbortedException& )
319 : {
320 : SAL_WARN( "sfx2.bastyp", "CommandAbortedException" );
321 : }
322 0 : catch( const uno::Exception& )
323 : {
324 : SAL_WARN( "sfx2.bastyp", "Any other exception" );
325 : }
326 0 : nSize = (sal_uInt32)nTemp;
327 0 : return nSize;
328 : }
329 :
330 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|