Update ooo320-m1
[ooovba.git] / scripting / source / inc / util / MiscUtils.hxx
blob0f9a664dcada50398967be6513f5ff767392a6b7
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: MiscUtils.hxx,v $
10 * $Revision: 1.12 $
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 #ifndef _SCRIPT_FRAMEWORK_MISCUTILS_HXX_
32 #define _SCRIPT_FRAMEWORK_MISCUTILS_HXX_
34 #include <rtl/ustring.hxx>
35 #include <tools/urlobj.hxx>
37 #include <ucbhelper/content.hxx>
38 #include <com/sun/star/uno/XComponentContext.hpp>
39 #include <com/sun/star/frame/XModel.hpp>
40 #include <com/sun/star/frame/XTransientDocumentsDocumentContentFactory.hpp>
41 #include <com/sun/star/beans/XPropertySet.hpp>
42 #include <com/sun/star/container/XContentEnumerationAccess.hpp>
43 #include <com/sun/star/ucb/XCommandEnvironment.hpp>
44 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
45 #include <com/sun/star/ucb/XContentAccess.hpp>
46 #include <com/sun/star/sdbc/XResultSet.hpp>
47 #include <com/sun/star/sdbc/XRow.hpp>
48 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
51 #include "util.hxx"
53 namespace sf_misc
55 // for simplification
56 #define css ::com::sun::star
58 class MiscUtils
60 public:
61 static css::uno::Sequence< ::rtl::OUString > allOpenTDocUrls( const css::uno::Reference< css::uno::XComponentContext >& xCtx)
63 css::uno::Sequence< ::rtl::OUString > result;
64 try
66 if ( !xCtx.is() )
68 return result;
70 css::uno::Reference < css::lang::XMultiComponentFactory > xFac( xCtx->getServiceManager(), css::uno::UNO_QUERY );
71 if ( xFac.is() )
73 css::uno::Reference < com::sun::star::ucb::XSimpleFileAccess > xSFA( xFac->createInstanceWithContext( OUSTR("com.sun.star.ucb.SimpleFileAccess"), xCtx ), css::uno::UNO_QUERY );
74 if ( xSFA.is() )
76 result = xSFA->getFolderContents( OUSTR("vnd.sun.star.tdoc:/"), true );
80 catch ( css::uno::Exception& )
83 return result;
86 static ::rtl::OUString xModelToTdocUrl( const css::uno::Reference< css::frame::XModel >& xModel,
87 const css::uno::Reference< css::uno::XComponentContext >& xContext )
89 css::uno::Reference< css::lang::XMultiComponentFactory > xMCF(
90 xContext->getServiceManager() );
91 css::uno::Reference<
92 css::frame::XTransientDocumentsDocumentContentFactory > xDocFac;
93 try
95 xDocFac =
96 css::uno::Reference<
97 css::frame::XTransientDocumentsDocumentContentFactory >(
98 xMCF->createInstanceWithContext(
99 rtl::OUString(
100 RTL_CONSTASCII_USTRINGPARAM(
101 "com.sun.star.frame.TransientDocumentsDocumentContentFactory" ) ),
102 xContext ),
103 css::uno::UNO_QUERY );
105 catch ( css::uno::Exception const & )
107 // handled below
110 if ( xDocFac.is() )
114 css::uno::Reference< css::ucb::XContent > xContent(
115 xDocFac->createDocumentContent( xModel ) );
116 return xContent->getIdentifier()->getContentIdentifier();
118 catch ( css::lang::IllegalArgumentException const & )
120 OSL_ENSURE( false, "Invalid document model!" );
124 OSL_ENSURE( false, "Unable to obtain URL for document model!" );
125 return rtl::OUString();
127 static css::uno::Reference< css::frame::XModel > tDocUrlToModel( const ::rtl::OUString& url )
129 css::uno::Any result;
133 ::ucbhelper::Content root( url, NULL );
134 ::rtl::OUString propName = OUSTR("DocumentModel");
135 result = getUCBProperty( root, propName );
137 catch ( css::ucb::ContentCreationException& )
139 // carry on, empty value will be returned
141 catch ( css::uno::RuntimeException& )
143 // carry on, empty value will be returned
146 css::uno::Reference< css::frame::XModel > xModel(
147 result, css::uno::UNO_QUERY );
149 return xModel;
153 static css::uno::Any getUCBProperty( ::ucbhelper::Content& content, ::rtl::OUString& prop )
155 css::uno::Any result;
158 result = content.getPropertyValue( prop );
160 catch ( css::uno::Exception& )
163 return result;
166 private:
167 static ::rtl::OUString parseLocationName( const ::rtl::OUString& location )
169 // strip out the last leaf of location name
170 // e.g. file://dir1/dir2/Blah.sxw - > Blah.sxw
171 ::rtl::OUString temp = location;
172 INetURLObject aURLObj( temp );
173 if ( !aURLObj.HasError() )
174 temp = aURLObj.getName( INetURLObject::LAST_SEGMENT, true, INetURLObject::DECODE_WITH_CHARSET );
175 return temp;
179 } // namespace sf_misc
180 #endif //