merge the formfield patch from ooo-build
[ooovba.git] / desktop / source / deployment / misc / dp_ucb.cxx
blob5d17a87daa03222193b54ba974c0beb8eb88eb1a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: dp_ucb.cxx,v $
10 * $Revision: 1.13 $
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"
35 #include "dp_misc.h"
36 #include "dp_ucb.h"
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;
53 namespace dp_misc
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,
65 bool throw_exc )
67 try {
68 // Existense check...
69 // content ctor/isFolder() will throw exception in case the resource
70 // does not exist.
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;
85 return true;
87 catch (RuntimeException &) {
88 throw;
90 catch (Exception &) {
91 if (throw_exc)
92 throw;
94 return false;
97 //==============================================================================
98 bool create_folder(
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;
109 return true;
113 OUString url( url_ );
114 // xxx todo: find parent
115 sal_Int32 slash = url.lastIndexOf( '/' );
116 if (slash < 0) {
117 // fallback:
118 url = expandUnoRcUrl( url );
119 slash = url.lastIndexOf( '/' );
121 if (slash < 0) {
122 // invalid: has to be at least "auth:/..."
123 if (throw_exc)
124 throw ContentCreationException(
125 OUSTR("Cannot create folder (invalid path): ") + url,
126 Reference<XInterface>(), ContentCreationError_UNKNOWN );
127 return false;
129 ::ucbhelper::Content parentContent;
130 if (! create_folder(
131 &parentContent, url.copy( 0, slash ), xCmdEnv, throw_exc ))
132 return false;
133 Reference<XContentCreator> xCreator( parentContent.get(), UNO_QUERY );
134 if (xCreator.is())
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 )
143 // look KIND_FOLDER:
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") ))
152 continue;
154 try {
155 if (parentContent.insertNewContent(
156 info.Type,
157 Sequence<OUString>( &StrTitle::get(), 1 ),
158 Sequence<Any>( &title, 1 ),
159 ucb_content )) {
160 if (ret_ucb_content != 0)
161 *ret_ucb_content = ucb_content;
162 return true;
165 catch (RuntimeException &) {
166 throw;
168 catch (CommandFailedException &) {
169 // Interaction Handler already handled the error
170 // that has occured...
172 catch (Exception &) {
173 if (throw_exc)
174 throw;
175 return false;
180 if (throw_exc)
181 throw ContentCreationException(
182 OUSTR("Cannot create folder: ") + url,
183 Reference<XInterface>(), ContentCreationError_UNKNOWN );
184 return false;
187 //==============================================================================
188 bool erase_path( OUString const & url,
189 Reference<XCommandEnvironment> const & xCmdEnv,
190 bool throw_exc )
192 ::ucbhelper::Content ucb_content;
193 if (create_ucb_content( &ucb_content, url, xCmdEnv, false /* no throw */ ))
195 try {
196 ucb_content.executeCommand(
197 OUSTR("delete"), Any( true /* delete physically */ ) );
199 catch (RuntimeException &) {
200 throw;
202 catch (Exception &) {
203 if (throw_exc)
204 throw;
205 return false;
208 return true;
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(
219 OUSTR(
220 "::ucbhelper::Content::openStream( XOutputStream ) failed!"),
221 0 );
222 return bytes;
225 //==============================================================================
226 bool readLine( OUString * res, OUString const & startingWith,
227 ::ucbhelper::Content & ucb_content, rtl_TextEncoding textenc )
229 // read whole file:
230 ::rtl::ByteSequence bytes( readFile( ucb_content ) );
231 OUString file( reinterpret_cast<sal_Char const *>(bytes.getConstArray()),
232 bytes.getLength(), textenc );
233 sal_Int32 pos = 0;
234 for (;;)
236 if (file.match( startingWith, pos ))
238 ::rtl::OUStringBuffer buf;
239 sal_Int32 start = pos;
240 pos += startingWith.getLength();
241 for (;;)
243 pos = file.indexOf( LF, pos );
244 if (pos < 0) { // EOF
245 buf.append( file.copy( start ) );
247 else
249 if (pos > 0 && file[ pos - 1 ] == CR)
251 // consume extra CR
252 buf.append( file.copy( start, pos - start - 1 ) );
253 ++pos;
255 else
256 buf.append( file.copy( start, pos - start ) );
257 ++pos; // consume LF
258 // check next line:
259 if (pos < file.getLength() &&
260 (file[ pos ] == ' ' || file[ pos ] == '\t'))
262 buf.append( static_cast<sal_Unicode>(' ') );
263 ++pos;
264 start = pos;
265 continue;
268 break;
270 *res = buf.makeStringAndClear();
271 return true;
273 // next line:
274 sal_Int32 next_lf = file.indexOf( LF, pos );
275 if (next_lf < 0) // EOF
276 break;
277 pos = next_lf + 1;
279 return false;