1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: dp_ucb.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_desktop.hxx"
34 #include "dp_misc.hrc"
37 #include "rtl/uri.hxx"
38 #include "rtl/ustrbuf.hxx"
39 #include "ucbhelper/content.hxx"
40 #include "xmlscript/xml_helper.hxx"
41 #include "com/sun/star/io/XInputStream.hpp"
42 #include "com/sun/star/ucb/CommandFailedException.hpp"
43 #include "com/sun/star/ucb/XContentCreator.hpp"
44 #include "com/sun/star/ucb/ContentInfo.hpp"
45 #include "com/sun/star/ucb/ContentInfoAttribute.hpp"
48 using namespace ::com::sun::star
;
49 using namespace ::com::sun::star::uno
;
50 using namespace ::com::sun::star::ucb
;
51 using ::rtl::OUString
;
56 const OUString
StrTitle::operator () ()
58 return OUSTR("Title");
61 //==============================================================================
62 bool create_ucb_content(
63 ::ucbhelper::Content
* ret_ucbContent
, OUString
const & url
,
64 Reference
<XCommandEnvironment
> const & xCmdEnv
,
69 // content ctor/isFolder() will throw exception in case the resource
72 // dilemma: no chance to use the given iahandler here, because it would
73 // raise no such file dialogs, else no interaction for
74 // passwords, ...? xxx todo
75 ::ucbhelper::Content
ucbContent(
76 url
, Reference
<XCommandEnvironment
>() );
78 ucbContent
.isFolder();
80 if (ret_ucbContent
!= 0)
82 ucbContent
.setCommandEnvironment( xCmdEnv
);
83 *ret_ucbContent
= ucbContent
;
87 catch (RuntimeException
&) {
97 //==============================================================================
99 ::ucbhelper::Content
* ret_ucb_content
, OUString
const & url_
,
100 Reference
<XCommandEnvironment
> const & xCmdEnv
, bool throw_exc
)
102 ::ucbhelper::Content ucb_content
;
103 if (create_ucb_content(
104 &ucb_content
, url_
, xCmdEnv
, false /* no throw */ ))
106 if (ucb_content
.isFolder()) {
107 if (ret_ucb_content
!= 0)
108 *ret_ucb_content
= ucb_content
;
113 OUString
url( url_
);
114 // xxx todo: find parent
115 sal_Int32 slash
= url
.lastIndexOf( '/' );
118 url
= expandUnoRcUrl( url
);
119 slash
= url
.lastIndexOf( '/' );
122 // invalid: has to be at least "auth:/..."
124 throw ContentCreationException(
125 OUSTR("Cannot create folder (invalid path): ") + url
,
126 Reference
<XInterface
>(), ContentCreationError_UNKNOWN
);
129 ::ucbhelper::Content parentContent
;
131 &parentContent
, url
.copy( 0, slash
), xCmdEnv
, throw_exc
))
133 Reference
<XContentCreator
> xCreator( parentContent
.get(), UNO_QUERY
);
136 const Any
title( ::rtl::Uri::decode( url
.copy( slash
+ 1 ),
137 rtl_UriDecodeWithCharset
,
138 RTL_TEXTENCODING_UTF8
) );
139 const Sequence
<ContentInfo
> infos(
140 xCreator
->queryCreatableContentsInfo() );
141 for ( sal_Int32 pos
= 0; pos
< infos
.getLength(); ++pos
)
144 ContentInfo
const & info
= infos
[ pos
];
145 if ((info
.Attributes
& ContentInfoAttribute::KIND_FOLDER
) != 0)
147 // make sure the only required bootstrap property is "Title":
148 Sequence
<beans::Property
> const & rProps
= info
.Properties
;
149 if (rProps
.getLength() != 1 ||
150 !rProps
[ 0 ].Name
.equalsAsciiL(
151 RTL_CONSTASCII_STRINGPARAM("Title") ))
155 if (parentContent
.insertNewContent(
157 Sequence
<OUString
>( &StrTitle::get(), 1 ),
158 Sequence
<Any
>( &title
, 1 ),
160 if (ret_ucb_content
!= 0)
161 *ret_ucb_content
= ucb_content
;
165 catch (RuntimeException
&) {
168 catch (CommandFailedException
&) {
169 // Interaction Handler already handled the error
170 // that has occured...
172 catch (Exception
&) {
181 throw ContentCreationException(
182 OUSTR("Cannot create folder: ") + url
,
183 Reference
<XInterface
>(), ContentCreationError_UNKNOWN
);
187 //==============================================================================
188 bool erase_path( OUString
const & url
,
189 Reference
<XCommandEnvironment
> const & xCmdEnv
,
192 ::ucbhelper::Content ucb_content
;
193 if (create_ucb_content( &ucb_content
, url
, xCmdEnv
, false /* no throw */ ))
196 ucb_content
.executeCommand(
197 OUSTR("delete"), Any( true /* delete physically */ ) );
199 catch (RuntimeException
&) {
202 catch (Exception
&) {
211 //==============================================================================
212 ::rtl::ByteSequence
readFile( ::ucbhelper::Content
& ucb_content
)
214 ::rtl::ByteSequence bytes
;
215 Reference
<io::XOutputStream
> xStream(
216 ::xmlscript::createOutputStream( &bytes
) );
217 if (! ucb_content
.openStream( xStream
))
218 throw RuntimeException(
220 "::ucbhelper::Content::openStream( XOutputStream ) failed!"),
225 //==============================================================================
226 bool readLine( OUString
* res
, OUString
const & startingWith
,
227 ::ucbhelper::Content
& ucb_content
, rtl_TextEncoding textenc
)
230 ::rtl::ByteSequence
bytes( readFile( ucb_content
) );
231 OUString
file( reinterpret_cast<sal_Char
const *>(bytes
.getConstArray()),
232 bytes
.getLength(), textenc
);
236 if (file
.match( startingWith
, pos
))
238 ::rtl::OUStringBuffer buf
;
239 sal_Int32 start
= pos
;
240 pos
+= startingWith
.getLength();
243 pos
= file
.indexOf( LF
, pos
);
244 if (pos
< 0) { // EOF
245 buf
.append( file
.copy( start
) );
249 if (pos
> 0 && file
[ pos
- 1 ] == CR
)
252 buf
.append( file
.copy( start
, pos
- start
- 1 ) );
256 buf
.append( file
.copy( start
, pos
- start
) );
259 if (pos
< file
.getLength() &&
260 (file
[ pos
] == ' ' || file
[ pos
] == '\t'))
262 buf
.append( static_cast<sal_Unicode
>(' ') );
270 *res
= buf
.makeStringAndClear();
274 sal_Int32 next_lf
= file
.indexOf( LF
, pos
);
275 if (next_lf
< 0) // EOF