12722ceddSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 32722ceddSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 42722ceddSAndrew Rist * or more contributor license agreements. See the NOTICE file 52722ceddSAndrew Rist * distributed with this work for additional information 62722ceddSAndrew Rist * regarding copyright ownership. The ASF licenses this file 72722ceddSAndrew Rist * to you under the Apache License, Version 2.0 (the 82722ceddSAndrew Rist * "License"); you may not use this file except in compliance 92722ceddSAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 112722ceddSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 132722ceddSAndrew Rist * Unless required by applicable law or agreed to in writing, 142722ceddSAndrew Rist * software distributed under the License is distributed on an 152722ceddSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 162722ceddSAndrew Rist * KIND, either express or implied. See the License for the 172722ceddSAndrew Rist * specific language governing permissions and limitations 182722ceddSAndrew Rist * under the License. 19cdf0e10cSrcweir * 202722ceddSAndrew Rist *************************************************************/ 212722ceddSAndrew Rist 222722ceddSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_desktop.hxx" 26cdf0e10cSrcweir 27cdf0e10cSrcweir #include "dp_misc.hrc" 28cdf0e10cSrcweir #include "dp_misc.h" 29cdf0e10cSrcweir #include "dp_ucb.h" 30cdf0e10cSrcweir #include "rtl/uri.hxx" 31cdf0e10cSrcweir #include "rtl/ustrbuf.hxx" 32cdf0e10cSrcweir #include "ucbhelper/content.hxx" 33cdf0e10cSrcweir #include "xmlscript/xml_helper.hxx" 34cdf0e10cSrcweir #include "com/sun/star/io/XInputStream.hpp" 35cdf0e10cSrcweir #include "com/sun/star/ucb/CommandFailedException.hpp" 36cdf0e10cSrcweir #include "com/sun/star/ucb/ContentInfo.hpp" 37cdf0e10cSrcweir #include "com/sun/star/ucb/ContentInfoAttribute.hpp" 38cdf0e10cSrcweir 39cdf0e10cSrcweir 40cdf0e10cSrcweir using namespace ::com::sun::star; 41cdf0e10cSrcweir using namespace ::com::sun::star::uno; 42cdf0e10cSrcweir using namespace ::com::sun::star::ucb; 43cdf0e10cSrcweir using ::rtl::OUString; 44cdf0e10cSrcweir 45cdf0e10cSrcweir namespace dp_misc 46cdf0e10cSrcweir { 47cdf0e10cSrcweir 48cdf0e10cSrcweir const OUString StrTitle::operator () () 49cdf0e10cSrcweir { 50cdf0e10cSrcweir return OUSTR("Title"); 51cdf0e10cSrcweir } 52cdf0e10cSrcweir 53cdf0e10cSrcweir //============================================================================== 54cdf0e10cSrcweir bool create_ucb_content( 55cdf0e10cSrcweir ::ucbhelper::Content * ret_ucbContent, OUString const & url, 56cdf0e10cSrcweir Reference<XCommandEnvironment> const & xCmdEnv, 57cdf0e10cSrcweir bool throw_exc ) 58cdf0e10cSrcweir { 59cdf0e10cSrcweir try { 60cdf0e10cSrcweir // Existense check... 61cdf0e10cSrcweir // content ctor/isFolder() will throw exception in case the resource 62cdf0e10cSrcweir // does not exist. 63cdf0e10cSrcweir 64cdf0e10cSrcweir // dilemma: no chance to use the given iahandler here, because it would 65cdf0e10cSrcweir // raise no such file dialogs, else no interaction for 66cdf0e10cSrcweir // passwords, ...? xxx todo 67cdf0e10cSrcweir ::ucbhelper::Content ucbContent( 68cdf0e10cSrcweir url, Reference<XCommandEnvironment>() ); 69cdf0e10cSrcweir 70cdf0e10cSrcweir ucbContent.isFolder(); 71cdf0e10cSrcweir 72cdf0e10cSrcweir if (ret_ucbContent != 0) 73cdf0e10cSrcweir { 74cdf0e10cSrcweir ucbContent.setCommandEnvironment( xCmdEnv ); 75cdf0e10cSrcweir *ret_ucbContent = ucbContent; 76cdf0e10cSrcweir } 77cdf0e10cSrcweir return true; 78cdf0e10cSrcweir } 79cdf0e10cSrcweir catch (RuntimeException &) { 80cdf0e10cSrcweir throw; 81cdf0e10cSrcweir } 82cdf0e10cSrcweir catch (Exception &) { 83cdf0e10cSrcweir if (throw_exc) 84cdf0e10cSrcweir throw; 85cdf0e10cSrcweir } 86cdf0e10cSrcweir return false; 87cdf0e10cSrcweir } 88cdf0e10cSrcweir 89cdf0e10cSrcweir //============================================================================== 90cdf0e10cSrcweir bool create_folder( 91cdf0e10cSrcweir ::ucbhelper::Content * ret_ucb_content, OUString const & url_, 92cdf0e10cSrcweir Reference<XCommandEnvironment> const & xCmdEnv, bool throw_exc ) 93cdf0e10cSrcweir { 94cdf0e10cSrcweir ::ucbhelper::Content ucb_content; 95cdf0e10cSrcweir if (create_ucb_content( 96cdf0e10cSrcweir &ucb_content, url_, xCmdEnv, false /* no throw */ )) 97cdf0e10cSrcweir { 98cdf0e10cSrcweir if (ucb_content.isFolder()) { 99cdf0e10cSrcweir if (ret_ucb_content != 0) 100cdf0e10cSrcweir *ret_ucb_content = ucb_content; 101cdf0e10cSrcweir return true; 102cdf0e10cSrcweir } 103cdf0e10cSrcweir } 104cdf0e10cSrcweir 105cdf0e10cSrcweir OUString url( url_ ); 106cdf0e10cSrcweir // xxx todo: find parent 107cdf0e10cSrcweir sal_Int32 slash = url.lastIndexOf( '/' ); 108cdf0e10cSrcweir if (slash < 0) { 109cdf0e10cSrcweir // fallback: 110cdf0e10cSrcweir url = expandUnoRcUrl( url ); 111cdf0e10cSrcweir slash = url.lastIndexOf( '/' ); 112cdf0e10cSrcweir } 113cdf0e10cSrcweir if (slash < 0) { 114cdf0e10cSrcweir // invalid: has to be at least "auth:/..." 115cdf0e10cSrcweir if (throw_exc) 116cdf0e10cSrcweir throw ContentCreationException( 117cdf0e10cSrcweir OUSTR("Cannot create folder (invalid path): ") + url, 118cdf0e10cSrcweir Reference<XInterface>(), ContentCreationError_UNKNOWN ); 119cdf0e10cSrcweir return false; 120cdf0e10cSrcweir } 121cdf0e10cSrcweir ::ucbhelper::Content parentContent; 122cdf0e10cSrcweir if (! create_folder( 123cdf0e10cSrcweir &parentContent, url.copy( 0, slash ), xCmdEnv, throw_exc )) 124cdf0e10cSrcweir return false; 125cdf0e10cSrcweir const Any title( ::rtl::Uri::decode( url.copy( slash + 1 ), 126cdf0e10cSrcweir rtl_UriDecodeWithCharset, 127cdf0e10cSrcweir RTL_TEXTENCODING_UTF8 ) ); 128cdf0e10cSrcweir const Sequence<ContentInfo> infos( 129cdf0e10cSrcweir parentContent.queryCreatableContentsInfo() ); 130cdf0e10cSrcweir for ( sal_Int32 pos = 0; pos < infos.getLength(); ++pos ) 131cdf0e10cSrcweir { 132cdf0e10cSrcweir // look KIND_FOLDER: 133cdf0e10cSrcweir ContentInfo const & info = infos[ pos ]; 134cdf0e10cSrcweir if ((info.Attributes & ContentInfoAttribute::KIND_FOLDER) != 0) 135cdf0e10cSrcweir { 136cdf0e10cSrcweir // make sure the only required bootstrap property is "Title": 137cdf0e10cSrcweir Sequence<beans::Property> const & rProps = info.Properties; 138cdf0e10cSrcweir if (rProps.getLength() != 1 || 139cdf0e10cSrcweir !rProps[ 0 ].Name.equalsAsciiL( 140cdf0e10cSrcweir RTL_CONSTASCII_STRINGPARAM("Title") )) 141cdf0e10cSrcweir continue; 142cdf0e10cSrcweir 143cdf0e10cSrcweir try { 144cdf0e10cSrcweir if (parentContent.insertNewContent( 145cdf0e10cSrcweir info.Type, 146cdf0e10cSrcweir Sequence<OUString>( &StrTitle::get(), 1 ), 147cdf0e10cSrcweir Sequence<Any>( &title, 1 ), 148cdf0e10cSrcweir ucb_content )) { 149cdf0e10cSrcweir if (ret_ucb_content != 0) 150cdf0e10cSrcweir *ret_ucb_content = ucb_content; 151cdf0e10cSrcweir return true; 152cdf0e10cSrcweir } 153cdf0e10cSrcweir } 154cdf0e10cSrcweir catch (RuntimeException &) { 155cdf0e10cSrcweir throw; 156cdf0e10cSrcweir } 157cdf0e10cSrcweir catch (CommandFailedException &) { 158cdf0e10cSrcweir // Interaction Handler already handled the error 159*07a3d7f1SPedro Giffuni // that has occurred... 160cdf0e10cSrcweir } 161cdf0e10cSrcweir catch (Exception &) { 162cdf0e10cSrcweir if (throw_exc) 163cdf0e10cSrcweir throw; 164cdf0e10cSrcweir return false; 165cdf0e10cSrcweir } 166cdf0e10cSrcweir } 167cdf0e10cSrcweir } 168cdf0e10cSrcweir if (throw_exc) 169cdf0e10cSrcweir throw ContentCreationException( 170cdf0e10cSrcweir OUSTR("Cannot create folder: ") + url, 171cdf0e10cSrcweir Reference<XInterface>(), ContentCreationError_UNKNOWN ); 172cdf0e10cSrcweir return false; 173cdf0e10cSrcweir } 174cdf0e10cSrcweir 175cdf0e10cSrcweir //============================================================================== 176cdf0e10cSrcweir bool erase_path( OUString const & url, 177cdf0e10cSrcweir Reference<XCommandEnvironment> const & xCmdEnv, 178cdf0e10cSrcweir bool throw_exc ) 179cdf0e10cSrcweir { 180cdf0e10cSrcweir ::ucbhelper::Content ucb_content; 181cdf0e10cSrcweir if (create_ucb_content( &ucb_content, url, xCmdEnv, false /* no throw */ )) 182cdf0e10cSrcweir { 183cdf0e10cSrcweir try { 184cdf0e10cSrcweir ucb_content.executeCommand( 185cdf0e10cSrcweir OUSTR("delete"), Any( true /* delete physically */ ) ); 186cdf0e10cSrcweir } 187cdf0e10cSrcweir catch (RuntimeException &) { 188cdf0e10cSrcweir throw; 189cdf0e10cSrcweir } 190cdf0e10cSrcweir catch (Exception &) { 191cdf0e10cSrcweir if (throw_exc) 192cdf0e10cSrcweir throw; 193cdf0e10cSrcweir return false; 194cdf0e10cSrcweir } 195cdf0e10cSrcweir } 196cdf0e10cSrcweir return true; 197cdf0e10cSrcweir } 198cdf0e10cSrcweir 199cdf0e10cSrcweir //============================================================================== 200cdf0e10cSrcweir ::rtl::ByteSequence readFile( ::ucbhelper::Content & ucb_content ) 201cdf0e10cSrcweir { 202cdf0e10cSrcweir ::rtl::ByteSequence bytes; 203cdf0e10cSrcweir Reference<io::XOutputStream> xStream( 204cdf0e10cSrcweir ::xmlscript::createOutputStream( &bytes ) ); 205cdf0e10cSrcweir if (! ucb_content.openStream( xStream )) 206cdf0e10cSrcweir throw RuntimeException( 207cdf0e10cSrcweir OUSTR( 208cdf0e10cSrcweir "::ucbhelper::Content::openStream( XOutputStream ) failed!"), 209cdf0e10cSrcweir 0 ); 210cdf0e10cSrcweir return bytes; 211cdf0e10cSrcweir } 212cdf0e10cSrcweir 213cdf0e10cSrcweir //============================================================================== 214cdf0e10cSrcweir bool readLine( OUString * res, OUString const & startingWith, 215cdf0e10cSrcweir ::ucbhelper::Content & ucb_content, rtl_TextEncoding textenc ) 216cdf0e10cSrcweir { 217cdf0e10cSrcweir // read whole file: 218cdf0e10cSrcweir ::rtl::ByteSequence bytes( readFile( ucb_content ) ); 219cdf0e10cSrcweir OUString file( reinterpret_cast<sal_Char const *>(bytes.getConstArray()), 220cdf0e10cSrcweir bytes.getLength(), textenc ); 221cdf0e10cSrcweir sal_Int32 pos = 0; 222cdf0e10cSrcweir for (;;) 223cdf0e10cSrcweir { 224cdf0e10cSrcweir if (file.match( startingWith, pos )) 225cdf0e10cSrcweir { 226cdf0e10cSrcweir ::rtl::OUStringBuffer buf; 227cdf0e10cSrcweir sal_Int32 start = pos; 228cdf0e10cSrcweir pos += startingWith.getLength(); 229cdf0e10cSrcweir for (;;) 230cdf0e10cSrcweir { 231cdf0e10cSrcweir pos = file.indexOf( LF, pos ); 232cdf0e10cSrcweir if (pos < 0) { // EOF 233cdf0e10cSrcweir buf.append( file.copy( start ) ); 234cdf0e10cSrcweir } 235cdf0e10cSrcweir else 236cdf0e10cSrcweir { 237cdf0e10cSrcweir if (pos > 0 && file[ pos - 1 ] == CR) 238cdf0e10cSrcweir { 239cdf0e10cSrcweir // consume extra CR 240cdf0e10cSrcweir buf.append( file.copy( start, pos - start - 1 ) ); 241cdf0e10cSrcweir ++pos; 242cdf0e10cSrcweir } 243cdf0e10cSrcweir else 244cdf0e10cSrcweir buf.append( file.copy( start, pos - start ) ); 245cdf0e10cSrcweir ++pos; // consume LF 246cdf0e10cSrcweir // check next line: 247cdf0e10cSrcweir if (pos < file.getLength() && 248cdf0e10cSrcweir (file[ pos ] == ' ' || file[ pos ] == '\t')) 249cdf0e10cSrcweir { 250cdf0e10cSrcweir buf.append( static_cast<sal_Unicode>(' ') ); 251cdf0e10cSrcweir ++pos; 252cdf0e10cSrcweir start = pos; 253cdf0e10cSrcweir continue; 254cdf0e10cSrcweir } 255cdf0e10cSrcweir } 256cdf0e10cSrcweir break; 257cdf0e10cSrcweir } 258cdf0e10cSrcweir *res = buf.makeStringAndClear(); 259cdf0e10cSrcweir return true; 260cdf0e10cSrcweir } 261cdf0e10cSrcweir // next line: 262cdf0e10cSrcweir sal_Int32 next_lf = file.indexOf( LF, pos ); 263cdf0e10cSrcweir if (next_lf < 0) // EOF 264cdf0e10cSrcweir break; 265cdf0e10cSrcweir pos = next_lf + 1; 266cdf0e10cSrcweir } 267cdf0e10cSrcweir return false; 268cdf0e10cSrcweir } 269cdf0e10cSrcweir 270cdf0e10cSrcweir bool readProperties( ::std::list< ::std::pair< ::rtl::OUString, ::rtl::OUString> > & out_result, 271cdf0e10cSrcweir ::ucbhelper::Content & ucb_content ) 272cdf0e10cSrcweir { 273cdf0e10cSrcweir // read whole file: 274cdf0e10cSrcweir ::rtl::ByteSequence bytes( readFile( ucb_content ) ); 275cdf0e10cSrcweir OUString file( reinterpret_cast<sal_Char const *>(bytes.getConstArray()), 276cdf0e10cSrcweir bytes.getLength(), RTL_TEXTENCODING_UTF8); 277cdf0e10cSrcweir sal_Int32 pos = 0; 278cdf0e10cSrcweir 279cdf0e10cSrcweir for (;;) 280cdf0e10cSrcweir { 281cdf0e10cSrcweir 282cdf0e10cSrcweir ::rtl::OUStringBuffer buf; 283cdf0e10cSrcweir sal_Int32 start = pos; 284cdf0e10cSrcweir 285cdf0e10cSrcweir bool bEOF = false; 286cdf0e10cSrcweir pos = file.indexOf( LF, pos ); 287cdf0e10cSrcweir if (pos < 0) { // EOF 288cdf0e10cSrcweir buf.append( file.copy( start ) ); 289cdf0e10cSrcweir bEOF = true; 290cdf0e10cSrcweir } 291cdf0e10cSrcweir else 292cdf0e10cSrcweir { 293cdf0e10cSrcweir if (pos > 0 && file[ pos - 1 ] == CR) 294cdf0e10cSrcweir // consume extra CR 295cdf0e10cSrcweir buf.append( file.copy( start, pos - start - 1 ) ); 296cdf0e10cSrcweir else 297cdf0e10cSrcweir buf.append( file.copy( start, pos - start ) ); 298cdf0e10cSrcweir pos++; 299cdf0e10cSrcweir } 300cdf0e10cSrcweir OUString aLine = buf.makeStringAndClear(); 301cdf0e10cSrcweir 302cdf0e10cSrcweir sal_Int32 posEqual = aLine.indexOf('='); 303cdf0e10cSrcweir if (posEqual > 0 && (posEqual + 1) < aLine.getLength()) 304cdf0e10cSrcweir { 305cdf0e10cSrcweir OUString name = aLine.copy(0, posEqual); 306cdf0e10cSrcweir OUString value = aLine.copy(posEqual + 1); 307cdf0e10cSrcweir out_result.push_back(::std::make_pair(name, value)); 308cdf0e10cSrcweir } 309cdf0e10cSrcweir 310cdf0e10cSrcweir if (bEOF) 311cdf0e10cSrcweir break; 312cdf0e10cSrcweir } 313cdf0e10cSrcweir return false; 314cdf0e10cSrcweir } 315cdf0e10cSrcweir 316cdf0e10cSrcweir } 317